blob: 13950d9027cb901dc3f11c5e243a85fb972b507f [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
Steven Rostedt22710482010-03-18 17:54:19 -0400181#if !defined(CONFIG_64BIT) || defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)
182# 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
Lai Jiangshan334d4162009-04-24 11:27:05 +0800189/* define RINGBUF_TYPE_DATA for 'case RINGBUF_TYPE_DATA:' */
190#define RINGBUF_TYPE_DATA 0 ... RINGBUF_TYPE_DATA_TYPE_LEN_MAX
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400191
192enum {
193 RB_LEN_TIME_EXTEND = 8,
194 RB_LEN_TIME_STAMP = 16,
195};
196
Steven Rostedt69d1b832010-10-07 18:18:05 -0400197#define skip_time_extend(event) \
198 ((struct ring_buffer_event *)((char *)event + RB_LEN_TIME_EXTEND))
199
Tom Zanussi2d622712009-03-22 03:30:49 -0500200static inline int rb_null_event(struct ring_buffer_event *event)
201{
Steven Rostedta1863c22009-09-03 10:23:58 -0400202 return event->type_len == RINGBUF_TYPE_PADDING && !event->time_delta;
Tom Zanussi2d622712009-03-22 03:30:49 -0500203}
204
205static void rb_event_set_padding(struct ring_buffer_event *event)
206{
Steven Rostedta1863c22009-09-03 10:23:58 -0400207 /* padding has a NULL time_delta */
Lai Jiangshan334d4162009-04-24 11:27:05 +0800208 event->type_len = RINGBUF_TYPE_PADDING;
Tom Zanussi2d622712009-03-22 03:30:49 -0500209 event->time_delta = 0;
210}
211
Tom Zanussi2d622712009-03-22 03:30:49 -0500212static unsigned
213rb_event_data_length(struct ring_buffer_event *event)
214{
215 unsigned length;
216
Lai Jiangshan334d4162009-04-24 11:27:05 +0800217 if (event->type_len)
218 length = event->type_len * RB_ALIGNMENT;
Tom Zanussi2d622712009-03-22 03:30:49 -0500219 else
220 length = event->array[0];
221 return length + RB_EVNT_HDR_SIZE;
222}
223
Steven Rostedt69d1b832010-10-07 18:18:05 -0400224/*
225 * Return the length of the given event. Will return
226 * the length of the time extend if the event is a
227 * time extend.
228 */
229static inline unsigned
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400230rb_event_length(struct ring_buffer_event *event)
231{
Lai Jiangshan334d4162009-04-24 11:27:05 +0800232 switch (event->type_len) {
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400233 case RINGBUF_TYPE_PADDING:
Tom Zanussi2d622712009-03-22 03:30:49 -0500234 if (rb_null_event(event))
235 /* undefined */
236 return -1;
Lai Jiangshan334d4162009-04-24 11:27:05 +0800237 return event->array[0] + RB_EVNT_HDR_SIZE;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400238
239 case RINGBUF_TYPE_TIME_EXTEND:
240 return RB_LEN_TIME_EXTEND;
241
242 case RINGBUF_TYPE_TIME_STAMP:
243 return RB_LEN_TIME_STAMP;
244
245 case RINGBUF_TYPE_DATA:
Tom Zanussi2d622712009-03-22 03:30:49 -0500246 return rb_event_data_length(event);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400247 default:
248 BUG();
249 }
250 /* not hit */
251 return 0;
252}
253
Steven Rostedt69d1b832010-10-07 18:18:05 -0400254/*
255 * Return total length of time extend and data,
256 * or just the event length for all other events.
257 */
258static inline unsigned
259rb_event_ts_length(struct ring_buffer_event *event)
260{
261 unsigned len = 0;
262
263 if (event->type_len == RINGBUF_TYPE_TIME_EXTEND) {
264 /* time extends include the data event after it */
265 len = RB_LEN_TIME_EXTEND;
266 event = skip_time_extend(event);
267 }
268 return len + rb_event_length(event);
269}
270
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400271/**
272 * ring_buffer_event_length - return the length of the event
273 * @event: the event to get the length of
Steven Rostedt69d1b832010-10-07 18:18:05 -0400274 *
275 * Returns the size of the data load of a data event.
276 * If the event is something other than a data event, it
277 * returns the size of the event itself. With the exception
278 * of a TIME EXTEND, where it still returns the size of the
279 * data load of the data event after it.
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400280 */
281unsigned ring_buffer_event_length(struct ring_buffer_event *event)
282{
Steven Rostedt69d1b832010-10-07 18:18:05 -0400283 unsigned length;
284
285 if (event->type_len == RINGBUF_TYPE_TIME_EXTEND)
286 event = skip_time_extend(event);
287
288 length = rb_event_length(event);
Lai Jiangshan334d4162009-04-24 11:27:05 +0800289 if (event->type_len > RINGBUF_TYPE_DATA_TYPE_LEN_MAX)
Robert Richter465634a2009-01-07 15:32:11 +0100290 return length;
291 length -= RB_EVNT_HDR_SIZE;
292 if (length > RB_MAX_SMALL_DATA + sizeof(event->array[0]))
293 length -= sizeof(event->array[0]);
294 return length;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400295}
Robert Richterc4f50182008-12-11 16:49:22 +0100296EXPORT_SYMBOL_GPL(ring_buffer_event_length);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400297
298/* inline for ring buffer fast paths */
Andrew Morton34a148b2009-01-09 12:27:09 -0800299static void *
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400300rb_event_data(struct ring_buffer_event *event)
301{
Steven Rostedt69d1b832010-10-07 18:18:05 -0400302 if (event->type_len == RINGBUF_TYPE_TIME_EXTEND)
303 event = skip_time_extend(event);
Lai Jiangshan334d4162009-04-24 11:27:05 +0800304 BUG_ON(event->type_len > RINGBUF_TYPE_DATA_TYPE_LEN_MAX);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400305 /* If length is in len field, then array[0] has the data */
Lai Jiangshan334d4162009-04-24 11:27:05 +0800306 if (event->type_len)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400307 return (void *)&event->array[0];
308 /* Otherwise length is in array[0] and array[1] has the data */
309 return (void *)&event->array[1];
310}
311
312/**
313 * ring_buffer_event_data - return the data of the event
314 * @event: the event to get the data from
315 */
316void *ring_buffer_event_data(struct ring_buffer_event *event)
317{
318 return rb_event_data(event);
319}
Robert Richterc4f50182008-12-11 16:49:22 +0100320EXPORT_SYMBOL_GPL(ring_buffer_event_data);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400321
322#define for_each_buffer_cpu(buffer, cpu) \
Rusty Russell9e01c1b2009-01-01 10:12:22 +1030323 for_each_cpu(cpu, buffer->cpumask)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400324
325#define TS_SHIFT 27
326#define TS_MASK ((1ULL << TS_SHIFT) - 1)
327#define TS_DELTA_TEST (~TS_MASK)
328
Steven Rostedt66a8cb92010-03-31 13:21:56 -0400329/* Flag when events were overwritten */
330#define RB_MISSED_EVENTS (1 << 31)
Steven Rostedtff0ff842010-03-31 22:11:42 -0400331/* Missed count stored at end */
332#define RB_MISSED_STORED (1 << 30)
Steven Rostedt66a8cb92010-03-31 13:21:56 -0400333
Steven Rostedtabc9b562008-12-02 15:34:06 -0500334struct buffer_data_page {
Steven Rostedte4c2ce82008-10-01 11:14:54 -0400335 u64 time_stamp; /* page time stamp */
Wenji Huangc3706f02009-02-10 01:03:18 -0500336 local_t commit; /* write committed index */
Steven Rostedtabc9b562008-12-02 15:34:06 -0500337 unsigned char data[]; /* data of buffer page */
338};
339
Steven Rostedt77ae3652009-03-27 11:00:29 -0400340/*
341 * Note, the buffer_page list must be first. The buffer pages
342 * are allocated in cache lines, which means that each buffer
343 * page will be at the beginning of a cache line, and thus
344 * the least significant bits will be zero. We use this to
345 * add flags in the list struct pointers, to make the ring buffer
346 * lockless.
347 */
Steven Rostedtabc9b562008-12-02 15:34:06 -0500348struct buffer_page {
Steven Rostedt778c55d2009-05-01 18:44:45 -0400349 struct list_head list; /* list of buffer pages */
Steven Rostedtabc9b562008-12-02 15:34:06 -0500350 local_t write; /* index for next write */
Steven Rostedt6f807ac2008-10-04 02:00:58 -0400351 unsigned read; /* index for next read */
Steven Rostedt778c55d2009-05-01 18:44:45 -0400352 local_t entries; /* entries on this page */
Steven Rostedtff0ff842010-03-31 22:11:42 -0400353 unsigned long real_end; /* real end of data */
Steven Rostedtabc9b562008-12-02 15:34:06 -0500354 struct buffer_data_page *page; /* Actual data page */
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400355};
356
Steven Rostedt77ae3652009-03-27 11:00:29 -0400357/*
358 * The buffer page counters, write and entries, must be reset
359 * atomically when crossing page boundaries. To synchronize this
360 * update, two counters are inserted into the number. One is
361 * the actual counter for the write position or count on the page.
362 *
363 * The other is a counter of updaters. Before an update happens
364 * the update partition of the counter is incremented. This will
365 * allow the updater to update the counter atomically.
366 *
367 * The counter is 20 bits, and the state data is 12.
368 */
369#define RB_WRITE_MASK 0xfffff
370#define RB_WRITE_INTCNT (1 << 20)
371
Steven Rostedt044fa782008-12-02 23:50:03 -0500372static void rb_init_page(struct buffer_data_page *bpage)
Steven Rostedtabc9b562008-12-02 15:34:06 -0500373{
Steven Rostedt044fa782008-12-02 23:50:03 -0500374 local_set(&bpage->commit, 0);
Steven Rostedtabc9b562008-12-02 15:34:06 -0500375}
376
Steven Rostedt474d32b2009-03-03 19:51:40 -0500377/**
378 * ring_buffer_page_len - the size of data on the page.
379 * @page: The page to read
380 *
381 * Returns the amount of data on the page, including buffer page header.
382 */
Steven Rostedtef7a4a12009-03-03 00:27:49 -0500383size_t ring_buffer_page_len(void *page)
384{
Steven Rostedt474d32b2009-03-03 19:51:40 -0500385 return local_read(&((struct buffer_data_page *)page)->commit)
386 + BUF_PAGE_HDR_SIZE;
Steven Rostedtef7a4a12009-03-03 00:27:49 -0500387}
388
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400389/*
Steven Rostedted568292008-09-29 23:02:40 -0400390 * Also stolen from mm/slob.c. Thanks to Mathieu Desnoyers for pointing
391 * this issue out.
392 */
Andrew Morton34a148b2009-01-09 12:27:09 -0800393static void free_buffer_page(struct buffer_page *bpage)
Steven Rostedted568292008-09-29 23:02:40 -0400394{
Andrew Morton34a148b2009-01-09 12:27:09 -0800395 free_page((unsigned long)bpage->page);
Steven Rostedte4c2ce82008-10-01 11:14:54 -0400396 kfree(bpage);
Steven Rostedted568292008-09-29 23:02:40 -0400397}
398
399/*
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400400 * We need to fit the time_stamp delta into 27 bits.
401 */
402static inline int test_time_stamp(u64 delta)
403{
404 if (delta & TS_DELTA_TEST)
405 return 1;
406 return 0;
407}
408
Steven Rostedt474d32b2009-03-03 19:51:40 -0500409#define BUF_PAGE_SIZE (PAGE_SIZE - BUF_PAGE_HDR_SIZE)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400410
Steven Rostedtbe957c42009-05-11 14:42:53 -0400411/* Max payload is BUF_PAGE_SIZE - header (8bytes) */
412#define BUF_MAX_DATA_SIZE (BUF_PAGE_SIZE - (sizeof(u32) * 2))
413
Steven Rostedtd1b182a2009-04-15 16:53:47 -0400414int ring_buffer_print_page_header(struct trace_seq *s)
415{
416 struct buffer_data_page field;
417 int ret;
418
419 ret = trace_seq_printf(s, "\tfield: u64 timestamp;\t"
Tom Zanussi26a50742009-10-06 01:09:50 -0500420 "offset:0;\tsize:%u;\tsigned:%u;\n",
421 (unsigned int)sizeof(field.time_stamp),
422 (unsigned int)is_signed_type(u64));
Steven Rostedtd1b182a2009-04-15 16:53:47 -0400423
424 ret = trace_seq_printf(s, "\tfield: local_t commit;\t"
Tom Zanussi26a50742009-10-06 01:09:50 -0500425 "offset:%u;\tsize:%u;\tsigned:%u;\n",
Steven Rostedtd1b182a2009-04-15 16:53:47 -0400426 (unsigned int)offsetof(typeof(field), commit),
Tom Zanussi26a50742009-10-06 01:09:50 -0500427 (unsigned int)sizeof(field.commit),
428 (unsigned int)is_signed_type(long));
Steven Rostedtd1b182a2009-04-15 16:53:47 -0400429
Steven Rostedt66a8cb92010-03-31 13:21:56 -0400430 ret = trace_seq_printf(s, "\tfield: int overwrite;\t"
431 "offset:%u;\tsize:%u;\tsigned:%u;\n",
432 (unsigned int)offsetof(typeof(field), commit),
433 1,
434 (unsigned int)is_signed_type(long));
435
Steven Rostedtd1b182a2009-04-15 16:53:47 -0400436 ret = trace_seq_printf(s, "\tfield: char data;\t"
Tom Zanussi26a50742009-10-06 01:09:50 -0500437 "offset:%u;\tsize:%u;\tsigned:%u;\n",
Steven Rostedtd1b182a2009-04-15 16:53:47 -0400438 (unsigned int)offsetof(typeof(field), data),
Tom Zanussi26a50742009-10-06 01:09:50 -0500439 (unsigned int)BUF_PAGE_SIZE,
440 (unsigned int)is_signed_type(char));
Steven Rostedtd1b182a2009-04-15 16:53:47 -0400441
442 return ret;
443}
444
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400445/*
446 * head_page == tail_page && head == tail then buffer is empty.
447 */
448struct ring_buffer_per_cpu {
449 int cpu;
Richard Kennedy985023d2010-03-25 11:27:36 +0000450 atomic_t record_disabled;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400451 struct ring_buffer *buffer;
Thomas Gleixner5389f6f2009-07-25 17:13:33 +0200452 raw_spinlock_t reader_lock; /* serialize readers */
Thomas Gleixner445c8952009-12-02 19:49:50 +0100453 arch_spinlock_t lock;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400454 struct lock_class_key lock_key;
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -0800455 unsigned int nr_pages;
Steven Rostedt3adc54f2009-03-30 15:32:01 -0400456 struct list_head *pages;
Steven Rostedt6f807ac2008-10-04 02:00:58 -0400457 struct buffer_page *head_page; /* read from head */
458 struct buffer_page *tail_page; /* write to tail */
Wenji Huangc3706f02009-02-10 01:03:18 -0500459 struct buffer_page *commit_page; /* committed pages */
Steven Rostedtd7690412008-10-01 00:29:53 -0400460 struct buffer_page *reader_page;
Steven Rostedt66a8cb92010-03-31 13:21:56 -0400461 unsigned long lost_events;
462 unsigned long last_overrun;
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -0700463 local_t entries_bytes;
Steven Rostedte4906ef2009-04-30 20:49:44 -0400464 local_t entries;
Slava Pestov884bfe82011-07-15 14:23:58 -0700465 local_t overrun;
466 local_t commit_overrun;
467 local_t dropped_events;
Steven Rostedtfa743952009-06-16 12:37:57 -0400468 local_t committing;
469 local_t commits;
Steven Rostedt77ae3652009-03-27 11:00:29 -0400470 unsigned long read;
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -0700471 unsigned long read_bytes;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400472 u64 write_stamp;
473 u64 read_stamp;
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -0800474 /* ring buffer pages to update, > 0 to add, < 0 to remove */
475 int nr_pages_to_update;
476 struct list_head new_pages; /* new pages to add */
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -0700477 struct work_struct update_pages_work;
Vaibhav Nagarnaik05fdd702012-05-18 13:29:51 -0700478 struct completion update_done;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400479};
480
481struct ring_buffer {
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400482 unsigned flags;
483 int cpus;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400484 atomic_t record_disabled;
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -0700485 atomic_t resize_disabled;
Arnaldo Carvalho de Melo00f62f62009-02-09 17:04:06 -0200486 cpumask_var_t cpumask;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400487
Peter Zijlstra1f8a6a12009-06-08 18:18:39 +0200488 struct lock_class_key *reader_lock_key;
489
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400490 struct mutex mutex;
491
492 struct ring_buffer_per_cpu **buffers;
Steven Rostedt554f7862009-03-11 22:00:13 -0400493
Steven Rostedt59222ef2009-03-12 11:46:03 -0400494#ifdef CONFIG_HOTPLUG_CPU
Steven Rostedt554f7862009-03-11 22:00:13 -0400495 struct notifier_block cpu_notify;
496#endif
Steven Rostedt37886f62009-03-17 17:22:06 -0400497 u64 (*clock)(void);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400498};
499
500struct ring_buffer_iter {
501 struct ring_buffer_per_cpu *cpu_buffer;
502 unsigned long head;
503 struct buffer_page *head_page;
Steven Rostedt492a74f2010-01-25 15:17:47 -0500504 struct buffer_page *cache_reader_page;
505 unsigned long cache_read;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400506 u64 read_stamp;
507};
508
Steven Rostedtf536aaf2008-11-10 23:07:30 -0500509/* buffer may be either ring_buffer or ring_buffer_per_cpu */
Steven Rostedt077c5402009-09-03 19:53:46 -0400510#define RB_WARN_ON(b, cond) \
511 ({ \
512 int _____ret = unlikely(cond); \
513 if (_____ret) { \
514 if (__same_type(*(b), struct ring_buffer_per_cpu)) { \
515 struct ring_buffer_per_cpu *__b = \
516 (void *)b; \
517 atomic_inc(&__b->buffer->record_disabled); \
518 } else \
519 atomic_inc(&b->record_disabled); \
520 WARN_ON(1); \
521 } \
522 _____ret; \
Steven Rostedt3e89c7b2008-11-11 15:28:41 -0500523 })
Steven Rostedtf536aaf2008-11-10 23:07:30 -0500524
Steven Rostedt37886f62009-03-17 17:22:06 -0400525/* Up this if you want to test the TIME_EXTENTS and normalization */
526#define DEBUG_SHIFT 0
527
Jiri Olsa6d3f1e12009-10-23 19:36:19 -0400528static inline u64 rb_time_stamp(struct ring_buffer *buffer)
Steven Rostedt88eb0122009-05-11 16:28:23 -0400529{
530 /* shift to debug/test normalization and TIME_EXTENTS */
531 return buffer->clock() << DEBUG_SHIFT;
532}
533
Steven Rostedt37886f62009-03-17 17:22:06 -0400534u64 ring_buffer_time_stamp(struct ring_buffer *buffer, int cpu)
535{
536 u64 time;
537
538 preempt_disable_notrace();
Jiri Olsa6d3f1e12009-10-23 19:36:19 -0400539 time = rb_time_stamp(buffer);
Steven Rostedt37886f62009-03-17 17:22:06 -0400540 preempt_enable_no_resched_notrace();
541
542 return time;
543}
544EXPORT_SYMBOL_GPL(ring_buffer_time_stamp);
545
546void ring_buffer_normalize_time_stamp(struct ring_buffer *buffer,
547 int cpu, u64 *ts)
548{
549 /* Just stupid testing the normalize function and deltas */
550 *ts >>= DEBUG_SHIFT;
551}
552EXPORT_SYMBOL_GPL(ring_buffer_normalize_time_stamp);
553
Steven Rostedt77ae3652009-03-27 11:00:29 -0400554/*
555 * Making the ring buffer lockless makes things tricky.
556 * Although writes only happen on the CPU that they are on,
557 * and they only need to worry about interrupts. Reads can
558 * happen on any CPU.
559 *
560 * The reader page is always off the ring buffer, but when the
561 * reader finishes with a page, it needs to swap its page with
562 * a new one from the buffer. The reader needs to take from
563 * the head (writes go to the tail). But if a writer is in overwrite
564 * mode and wraps, it must push the head page forward.
565 *
566 * Here lies the problem.
567 *
568 * The reader must be careful to replace only the head page, and
569 * not another one. As described at the top of the file in the
570 * ASCII art, the reader sets its old page to point to the next
571 * page after head. It then sets the page after head to point to
572 * the old reader page. But if the writer moves the head page
573 * during this operation, the reader could end up with the tail.
574 *
575 * We use cmpxchg to help prevent this race. We also do something
576 * special with the page before head. We set the LSB to 1.
577 *
578 * When the writer must push the page forward, it will clear the
579 * bit that points to the head page, move the head, and then set
580 * the bit that points to the new head page.
581 *
582 * We also don't want an interrupt coming in and moving the head
583 * page on another writer. Thus we use the second LSB to catch
584 * that too. Thus:
585 *
586 * head->list->prev->next bit 1 bit 0
587 * ------- -------
588 * Normal page 0 0
589 * Points to head page 0 1
590 * New head page 1 0
591 *
592 * Note we can not trust the prev pointer of the head page, because:
593 *
594 * +----+ +-----+ +-----+
595 * | |------>| T |---X--->| N |
596 * | |<------| | | |
597 * +----+ +-----+ +-----+
598 * ^ ^ |
599 * | +-----+ | |
600 * +----------| R |----------+ |
601 * | |<-----------+
602 * +-----+
603 *
604 * Key: ---X--> HEAD flag set in pointer
605 * T Tail page
606 * R Reader page
607 * N Next page
608 *
609 * (see __rb_reserve_next() to see where this happens)
610 *
611 * What the above shows is that the reader just swapped out
612 * the reader page with a page in the buffer, but before it
613 * could make the new header point back to the new page added
614 * it was preempted by a writer. The writer moved forward onto
615 * the new page added by the reader and is about to move forward
616 * again.
617 *
618 * You can see, it is legitimate for the previous pointer of
619 * the head (or any page) not to point back to itself. But only
620 * temporarially.
621 */
622
623#define RB_PAGE_NORMAL 0UL
624#define RB_PAGE_HEAD 1UL
625#define RB_PAGE_UPDATE 2UL
626
627
628#define RB_FLAG_MASK 3UL
629
630/* PAGE_MOVED is not part of the mask */
631#define RB_PAGE_MOVED 4UL
632
633/*
634 * rb_list_head - remove any bit
635 */
636static struct list_head *rb_list_head(struct list_head *list)
637{
638 unsigned long val = (unsigned long)list;
639
640 return (struct list_head *)(val & ~RB_FLAG_MASK);
641}
642
643/*
Jiri Olsa6d3f1e12009-10-23 19:36:19 -0400644 * rb_is_head_page - test if the given page is the head page
Steven Rostedt77ae3652009-03-27 11:00:29 -0400645 *
646 * Because the reader may move the head_page pointer, we can
647 * not trust what the head page is (it may be pointing to
648 * the reader page). But if the next page is a header page,
649 * its flags will be non zero.
650 */
Jesper Juhl42b16b32011-01-17 00:09:38 +0100651static inline int
Steven Rostedt77ae3652009-03-27 11:00:29 -0400652rb_is_head_page(struct ring_buffer_per_cpu *cpu_buffer,
653 struct buffer_page *page, struct list_head *list)
654{
655 unsigned long val;
656
657 val = (unsigned long)list->next;
658
659 if ((val & ~RB_FLAG_MASK) != (unsigned long)&page->list)
660 return RB_PAGE_MOVED;
661
662 return val & RB_FLAG_MASK;
663}
664
665/*
666 * rb_is_reader_page
667 *
668 * The unique thing about the reader page, is that, if the
669 * writer is ever on it, the previous pointer never points
670 * back to the reader page.
671 */
672static int rb_is_reader_page(struct buffer_page *page)
673{
674 struct list_head *list = page->list.prev;
675
676 return rb_list_head(list->next) != &page->list;
677}
678
679/*
680 * rb_set_list_to_head - set a list_head to be pointing to head.
681 */
682static void rb_set_list_to_head(struct ring_buffer_per_cpu *cpu_buffer,
683 struct list_head *list)
684{
685 unsigned long *ptr;
686
687 ptr = (unsigned long *)&list->next;
688 *ptr |= RB_PAGE_HEAD;
689 *ptr &= ~RB_PAGE_UPDATE;
690}
691
692/*
693 * rb_head_page_activate - sets up head page
694 */
695static void rb_head_page_activate(struct ring_buffer_per_cpu *cpu_buffer)
696{
697 struct buffer_page *head;
698
699 head = cpu_buffer->head_page;
700 if (!head)
701 return;
702
703 /*
704 * Set the previous list pointer to have the HEAD flag.
705 */
706 rb_set_list_to_head(cpu_buffer, head->list.prev);
707}
708
709static void rb_list_head_clear(struct list_head *list)
710{
711 unsigned long *ptr = (unsigned long *)&list->next;
712
713 *ptr &= ~RB_FLAG_MASK;
714}
715
716/*
717 * rb_head_page_dactivate - clears head page ptr (for free list)
718 */
719static void
720rb_head_page_deactivate(struct ring_buffer_per_cpu *cpu_buffer)
721{
722 struct list_head *hd;
723
724 /* Go through the whole list and clear any pointers found. */
725 rb_list_head_clear(cpu_buffer->pages);
726
727 list_for_each(hd, cpu_buffer->pages)
728 rb_list_head_clear(hd);
729}
730
731static int rb_head_page_set(struct ring_buffer_per_cpu *cpu_buffer,
732 struct buffer_page *head,
733 struct buffer_page *prev,
734 int old_flag, int new_flag)
735{
736 struct list_head *list;
737 unsigned long val = (unsigned long)&head->list;
738 unsigned long ret;
739
740 list = &prev->list;
741
742 val &= ~RB_FLAG_MASK;
743
Steven Rostedt08a40812009-09-14 09:31:35 -0400744 ret = cmpxchg((unsigned long *)&list->next,
745 val | old_flag, val | new_flag);
Steven Rostedt77ae3652009-03-27 11:00:29 -0400746
747 /* check if the reader took the page */
748 if ((ret & ~RB_FLAG_MASK) != val)
749 return RB_PAGE_MOVED;
750
751 return ret & RB_FLAG_MASK;
752}
753
754static int rb_head_page_set_update(struct ring_buffer_per_cpu *cpu_buffer,
755 struct buffer_page *head,
756 struct buffer_page *prev,
757 int old_flag)
758{
759 return rb_head_page_set(cpu_buffer, head, prev,
760 old_flag, RB_PAGE_UPDATE);
761}
762
763static int rb_head_page_set_head(struct ring_buffer_per_cpu *cpu_buffer,
764 struct buffer_page *head,
765 struct buffer_page *prev,
766 int old_flag)
767{
768 return rb_head_page_set(cpu_buffer, head, prev,
769 old_flag, RB_PAGE_HEAD);
770}
771
772static int rb_head_page_set_normal(struct ring_buffer_per_cpu *cpu_buffer,
773 struct buffer_page *head,
774 struct buffer_page *prev,
775 int old_flag)
776{
777 return rb_head_page_set(cpu_buffer, head, prev,
778 old_flag, RB_PAGE_NORMAL);
779}
780
781static inline void rb_inc_page(struct ring_buffer_per_cpu *cpu_buffer,
782 struct buffer_page **bpage)
783{
784 struct list_head *p = rb_list_head((*bpage)->list.next);
785
786 *bpage = list_entry(p, struct buffer_page, list);
787}
788
789static struct buffer_page *
790rb_set_head_page(struct ring_buffer_per_cpu *cpu_buffer)
791{
792 struct buffer_page *head;
793 struct buffer_page *page;
794 struct list_head *list;
795 int i;
796
797 if (RB_WARN_ON(cpu_buffer, !cpu_buffer->head_page))
798 return NULL;
799
800 /* sanity check */
801 list = cpu_buffer->pages;
802 if (RB_WARN_ON(cpu_buffer, rb_list_head(list->prev->next) != list))
803 return NULL;
804
805 page = head = cpu_buffer->head_page;
806 /*
807 * It is possible that the writer moves the header behind
808 * where we started, and we miss in one loop.
809 * A second loop should grab the header, but we'll do
810 * three loops just because I'm paranoid.
811 */
812 for (i = 0; i < 3; i++) {
813 do {
814 if (rb_is_head_page(cpu_buffer, page, page->list.prev)) {
815 cpu_buffer->head_page = page;
816 return page;
817 }
818 rb_inc_page(cpu_buffer, &page);
819 } while (page != head);
820 }
821
822 RB_WARN_ON(cpu_buffer, 1);
823
824 return NULL;
825}
826
827static int rb_head_page_replace(struct buffer_page *old,
828 struct buffer_page *new)
829{
830 unsigned long *ptr = (unsigned long *)&old->list.prev->next;
831 unsigned long val;
832 unsigned long ret;
833
834 val = *ptr & ~RB_FLAG_MASK;
835 val |= RB_PAGE_HEAD;
836
Steven Rostedt08a40812009-09-14 09:31:35 -0400837 ret = cmpxchg(ptr, val, (unsigned long)&new->list);
Steven Rostedt77ae3652009-03-27 11:00:29 -0400838
839 return ret == val;
840}
841
842/*
843 * rb_tail_page_update - move the tail page forward
844 *
845 * Returns 1 if moved tail page, 0 if someone else did.
846 */
847static int rb_tail_page_update(struct ring_buffer_per_cpu *cpu_buffer,
848 struct buffer_page *tail_page,
849 struct buffer_page *next_page)
850{
851 struct buffer_page *old_tail;
852 unsigned long old_entries;
853 unsigned long old_write;
854 int ret = 0;
855
856 /*
857 * The tail page now needs to be moved forward.
858 *
859 * We need to reset the tail page, but without messing
860 * with possible erasing of data brought in by interrupts
861 * that have moved the tail page and are currently on it.
862 *
863 * We add a counter to the write field to denote this.
864 */
865 old_write = local_add_return(RB_WRITE_INTCNT, &next_page->write);
866 old_entries = local_add_return(RB_WRITE_INTCNT, &next_page->entries);
867
868 /*
869 * Just make sure we have seen our old_write and synchronize
870 * with any interrupts that come in.
871 */
872 barrier();
873
874 /*
875 * If the tail page is still the same as what we think
876 * it is, then it is up to us to update the tail
877 * pointer.
878 */
879 if (tail_page == cpu_buffer->tail_page) {
880 /* Zero the write counter */
881 unsigned long val = old_write & ~RB_WRITE_MASK;
882 unsigned long eval = old_entries & ~RB_WRITE_MASK;
883
884 /*
885 * This will only succeed if an interrupt did
886 * not come in and change it. In which case, we
887 * do not want to modify it.
Lai Jiangshanda706d82009-07-15 16:27:30 +0800888 *
889 * We add (void) to let the compiler know that we do not care
890 * about the return value of these functions. We use the
891 * cmpxchg to only update if an interrupt did not already
892 * do it for us. If the cmpxchg fails, we don't care.
Steven Rostedt77ae3652009-03-27 11:00:29 -0400893 */
Lai Jiangshanda706d82009-07-15 16:27:30 +0800894 (void)local_cmpxchg(&next_page->write, old_write, val);
895 (void)local_cmpxchg(&next_page->entries, old_entries, eval);
Steven Rostedt77ae3652009-03-27 11:00:29 -0400896
897 /*
898 * No need to worry about races with clearing out the commit.
899 * it only can increment when a commit takes place. But that
900 * only happens in the outer most nested commit.
901 */
902 local_set(&next_page->page->commit, 0);
903
904 old_tail = cmpxchg(&cpu_buffer->tail_page,
905 tail_page, next_page);
906
907 if (old_tail == tail_page)
908 ret = 1;
909 }
910
911 return ret;
912}
913
914static int rb_check_bpage(struct ring_buffer_per_cpu *cpu_buffer,
915 struct buffer_page *bpage)
916{
917 unsigned long val = (unsigned long)bpage;
918
919 if (RB_WARN_ON(cpu_buffer, val & RB_FLAG_MASK))
920 return 1;
921
922 return 0;
923}
924
925/**
926 * rb_check_list - make sure a pointer to a list has the last bits zero
927 */
928static int rb_check_list(struct ring_buffer_per_cpu *cpu_buffer,
929 struct list_head *list)
930{
931 if (RB_WARN_ON(cpu_buffer, rb_list_head(list->prev) != list->prev))
932 return 1;
933 if (RB_WARN_ON(cpu_buffer, rb_list_head(list->next) != list->next))
934 return 1;
935 return 0;
936}
937
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400938/**
939 * check_pages - integrity check of buffer pages
940 * @cpu_buffer: CPU buffer with pages to test
941 *
Wenji Huangc3706f02009-02-10 01:03:18 -0500942 * As a safety measure we check to make sure the data pages have not
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400943 * been corrupted.
944 */
945static int rb_check_pages(struct ring_buffer_per_cpu *cpu_buffer)
946{
Steven Rostedt3adc54f2009-03-30 15:32:01 -0400947 struct list_head *head = cpu_buffer->pages;
Steven Rostedt044fa782008-12-02 23:50:03 -0500948 struct buffer_page *bpage, *tmp;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400949
Steven Rostedt308f7ee2012-05-16 19:46:32 -0400950 /* Reset the head page if it exists */
951 if (cpu_buffer->head_page)
952 rb_set_head_page(cpu_buffer);
953
Steven Rostedt77ae3652009-03-27 11:00:29 -0400954 rb_head_page_deactivate(cpu_buffer);
955
Steven Rostedt3e89c7b2008-11-11 15:28:41 -0500956 if (RB_WARN_ON(cpu_buffer, head->next->prev != head))
957 return -1;
958 if (RB_WARN_ON(cpu_buffer, head->prev->next != head))
959 return -1;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400960
Steven Rostedt77ae3652009-03-27 11:00:29 -0400961 if (rb_check_list(cpu_buffer, head))
962 return -1;
963
Steven Rostedt044fa782008-12-02 23:50:03 -0500964 list_for_each_entry_safe(bpage, tmp, head, list) {
Steven Rostedt3e89c7b2008-11-11 15:28:41 -0500965 if (RB_WARN_ON(cpu_buffer,
Steven Rostedt044fa782008-12-02 23:50:03 -0500966 bpage->list.next->prev != &bpage->list))
Steven Rostedt3e89c7b2008-11-11 15:28:41 -0500967 return -1;
968 if (RB_WARN_ON(cpu_buffer,
Steven Rostedt044fa782008-12-02 23:50:03 -0500969 bpage->list.prev->next != &bpage->list))
Steven Rostedt3e89c7b2008-11-11 15:28:41 -0500970 return -1;
Steven Rostedt77ae3652009-03-27 11:00:29 -0400971 if (rb_check_list(cpu_buffer, &bpage->list))
972 return -1;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400973 }
974
Steven Rostedt77ae3652009-03-27 11:00:29 -0400975 rb_head_page_activate(cpu_buffer);
976
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400977 return 0;
978}
979
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -0800980static int __rb_allocate_pages(int nr_pages, struct list_head *pages, int cpu)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400981{
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -0800982 int i;
Steven Rostedt044fa782008-12-02 23:50:03 -0500983 struct buffer_page *bpage, *tmp;
Steven Rostedt3adc54f2009-03-30 15:32:01 -0400984
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400985 for (i = 0; i < nr_pages; i++) {
Vaibhav Nagarnaik7ea59062011-05-03 17:56:42 -0700986 struct page *page;
Vaibhav Nagarnaikd7ec4bf2011-06-07 17:01:42 -0700987 /*
988 * __GFP_NORETRY flag makes sure that the allocation fails
989 * gracefully without invoking oom-killer and the system is
990 * not destabilized.
991 */
Steven Rostedt044fa782008-12-02 23:50:03 -0500992 bpage = kzalloc_node(ALIGN(sizeof(*bpage), cache_line_size()),
Vaibhav Nagarnaikd7ec4bf2011-06-07 17:01:42 -0700993 GFP_KERNEL | __GFP_NORETRY,
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -0800994 cpu_to_node(cpu));
Steven Rostedt044fa782008-12-02 23:50:03 -0500995 if (!bpage)
Steven Rostedte4c2ce82008-10-01 11:14:54 -0400996 goto free_pages;
Steven Rostedt77ae3652009-03-27 11:00:29 -0400997
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -0800998 list_add(&bpage->list, pages);
Steven Rostedt77ae3652009-03-27 11:00:29 -0400999
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001000 page = alloc_pages_node(cpu_to_node(cpu),
Vaibhav Nagarnaikd7ec4bf2011-06-07 17:01:42 -07001001 GFP_KERNEL | __GFP_NORETRY, 0);
Vaibhav Nagarnaik7ea59062011-05-03 17:56:42 -07001002 if (!page)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001003 goto free_pages;
Vaibhav Nagarnaik7ea59062011-05-03 17:56:42 -07001004 bpage->page = page_address(page);
Steven Rostedt044fa782008-12-02 23:50:03 -05001005 rb_init_page(bpage->page);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001006 }
1007
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001008 return 0;
1009
1010free_pages:
1011 list_for_each_entry_safe(bpage, tmp, pages, list) {
1012 list_del_init(&bpage->list);
1013 free_buffer_page(bpage);
1014 }
1015
1016 return -ENOMEM;
1017}
1018
1019static int rb_allocate_pages(struct ring_buffer_per_cpu *cpu_buffer,
1020 unsigned nr_pages)
1021{
1022 LIST_HEAD(pages);
1023
1024 WARN_ON(!nr_pages);
1025
1026 if (__rb_allocate_pages(nr_pages, &pages, cpu_buffer->cpu))
1027 return -ENOMEM;
1028
Steven Rostedt3adc54f2009-03-30 15:32:01 -04001029 /*
1030 * The ring buffer page list is a circular list that does not
1031 * start and end with a list head. All page list items point to
1032 * other pages.
1033 */
1034 cpu_buffer->pages = pages.next;
1035 list_del(&pages);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001036
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001037 cpu_buffer->nr_pages = nr_pages;
1038
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001039 rb_check_pages(cpu_buffer);
1040
1041 return 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001042}
1043
1044static struct ring_buffer_per_cpu *
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001045rb_allocate_cpu_buffer(struct ring_buffer *buffer, int nr_pages, int cpu)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001046{
1047 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedt044fa782008-12-02 23:50:03 -05001048 struct buffer_page *bpage;
Vaibhav Nagarnaik7ea59062011-05-03 17:56:42 -07001049 struct page *page;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001050 int ret;
1051
1052 cpu_buffer = kzalloc_node(ALIGN(sizeof(*cpu_buffer), cache_line_size()),
1053 GFP_KERNEL, cpu_to_node(cpu));
1054 if (!cpu_buffer)
1055 return NULL;
1056
1057 cpu_buffer->cpu = cpu;
1058 cpu_buffer->buffer = buffer;
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02001059 raw_spin_lock_init(&cpu_buffer->reader_lock);
Peter Zijlstra1f8a6a12009-06-08 18:18:39 +02001060 lockdep_set_class(&cpu_buffer->reader_lock, buffer->reader_lock_key);
Thomas Gleixneredc35bd2009-12-03 12:38:57 +01001061 cpu_buffer->lock = (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001062 INIT_WORK(&cpu_buffer->update_pages_work, update_pages_handler);
Vaibhav Nagarnaik05fdd702012-05-18 13:29:51 -07001063 init_completion(&cpu_buffer->update_done);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001064
Steven Rostedt044fa782008-12-02 23:50:03 -05001065 bpage = kzalloc_node(ALIGN(sizeof(*bpage), cache_line_size()),
Steven Rostedte4c2ce82008-10-01 11:14:54 -04001066 GFP_KERNEL, cpu_to_node(cpu));
Steven Rostedt044fa782008-12-02 23:50:03 -05001067 if (!bpage)
Steven Rostedte4c2ce82008-10-01 11:14:54 -04001068 goto fail_free_buffer;
1069
Steven Rostedt77ae3652009-03-27 11:00:29 -04001070 rb_check_bpage(cpu_buffer, bpage);
1071
Steven Rostedt044fa782008-12-02 23:50:03 -05001072 cpu_buffer->reader_page = bpage;
Vaibhav Nagarnaik7ea59062011-05-03 17:56:42 -07001073 page = alloc_pages_node(cpu_to_node(cpu), GFP_KERNEL, 0);
1074 if (!page)
Steven Rostedte4c2ce82008-10-01 11:14:54 -04001075 goto fail_free_reader;
Vaibhav Nagarnaik7ea59062011-05-03 17:56:42 -07001076 bpage->page = page_address(page);
Steven Rostedt044fa782008-12-02 23:50:03 -05001077 rb_init_page(bpage->page);
Steven Rostedte4c2ce82008-10-01 11:14:54 -04001078
Steven Rostedtd7690412008-10-01 00:29:53 -04001079 INIT_LIST_HEAD(&cpu_buffer->reader_page->list);
Vaibhav Nagarnaik44b99462012-06-22 11:50:05 -07001080 INIT_LIST_HEAD(&cpu_buffer->new_pages);
Steven Rostedtd7690412008-10-01 00:29:53 -04001081
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001082 ret = rb_allocate_pages(cpu_buffer, nr_pages);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001083 if (ret < 0)
Steven Rostedtd7690412008-10-01 00:29:53 -04001084 goto fail_free_reader;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001085
1086 cpu_buffer->head_page
Steven Rostedt3adc54f2009-03-30 15:32:01 -04001087 = list_entry(cpu_buffer->pages, struct buffer_page, list);
Steven Rostedtbf41a152008-10-04 02:00:59 -04001088 cpu_buffer->tail_page = cpu_buffer->commit_page = cpu_buffer->head_page;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001089
Steven Rostedt77ae3652009-03-27 11:00:29 -04001090 rb_head_page_activate(cpu_buffer);
1091
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001092 return cpu_buffer;
1093
Steven Rostedtd7690412008-10-01 00:29:53 -04001094 fail_free_reader:
1095 free_buffer_page(cpu_buffer->reader_page);
1096
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001097 fail_free_buffer:
1098 kfree(cpu_buffer);
1099 return NULL;
1100}
1101
1102static void rb_free_cpu_buffer(struct ring_buffer_per_cpu *cpu_buffer)
1103{
Steven Rostedt3adc54f2009-03-30 15:32:01 -04001104 struct list_head *head = cpu_buffer->pages;
Steven Rostedt044fa782008-12-02 23:50:03 -05001105 struct buffer_page *bpage, *tmp;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001106
Steven Rostedtd7690412008-10-01 00:29:53 -04001107 free_buffer_page(cpu_buffer->reader_page);
1108
Steven Rostedt77ae3652009-03-27 11:00:29 -04001109 rb_head_page_deactivate(cpu_buffer);
1110
Steven Rostedt3adc54f2009-03-30 15:32:01 -04001111 if (head) {
1112 list_for_each_entry_safe(bpage, tmp, head, list) {
1113 list_del_init(&bpage->list);
1114 free_buffer_page(bpage);
1115 }
1116 bpage = list_entry(head, struct buffer_page, list);
Steven Rostedt044fa782008-12-02 23:50:03 -05001117 free_buffer_page(bpage);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001118 }
Steven Rostedt3adc54f2009-03-30 15:32:01 -04001119
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001120 kfree(cpu_buffer);
1121}
1122
Steven Rostedt59222ef2009-03-12 11:46:03 -04001123#ifdef CONFIG_HOTPLUG_CPU
Frederic Weisbecker09c9e842009-03-21 04:33:36 +01001124static int rb_cpu_notify(struct notifier_block *self,
1125 unsigned long action, void *hcpu);
Steven Rostedt554f7862009-03-11 22:00:13 -04001126#endif
1127
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001128/**
1129 * ring_buffer_alloc - allocate a new ring_buffer
Robert Richter68814b52008-11-24 12:24:12 +01001130 * @size: the size in bytes per cpu that is needed.
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001131 * @flags: attributes to set for the ring buffer.
1132 *
1133 * Currently the only flag that is available is the RB_FL_OVERWRITE
1134 * flag. This flag means that the buffer will overwrite old data
1135 * when the buffer wraps. If this flag is not set, the buffer will
1136 * drop data when the tail hits the head.
1137 */
Peter Zijlstra1f8a6a12009-06-08 18:18:39 +02001138struct ring_buffer *__ring_buffer_alloc(unsigned long size, unsigned flags,
1139 struct lock_class_key *key)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001140{
1141 struct ring_buffer *buffer;
1142 int bsize;
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001143 int cpu, nr_pages;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001144
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001145 /* keep it in its own cache line */
1146 buffer = kzalloc(ALIGN(sizeof(*buffer), cache_line_size()),
1147 GFP_KERNEL);
1148 if (!buffer)
1149 return NULL;
1150
Rusty Russell9e01c1b2009-01-01 10:12:22 +10301151 if (!alloc_cpumask_var(&buffer->cpumask, GFP_KERNEL))
1152 goto fail_free_buffer;
1153
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001154 nr_pages = DIV_ROUND_UP(size, BUF_PAGE_SIZE);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001155 buffer->flags = flags;
Steven Rostedt37886f62009-03-17 17:22:06 -04001156 buffer->clock = trace_clock_local;
Peter Zijlstra1f8a6a12009-06-08 18:18:39 +02001157 buffer->reader_lock_key = key;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001158
1159 /* need at least two pages */
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001160 if (nr_pages < 2)
1161 nr_pages = 2;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001162
Frederic Weisbecker3bf832c2009-03-19 14:47:33 +01001163 /*
1164 * In case of non-hotplug cpu, if the ring-buffer is allocated
1165 * in early initcall, it will not be notified of secondary cpus.
1166 * In that off case, we need to allocate for all possible cpus.
1167 */
1168#ifdef CONFIG_HOTPLUG_CPU
Steven Rostedt554f7862009-03-11 22:00:13 -04001169 get_online_cpus();
1170 cpumask_copy(buffer->cpumask, cpu_online_mask);
Frederic Weisbecker3bf832c2009-03-19 14:47:33 +01001171#else
1172 cpumask_copy(buffer->cpumask, cpu_possible_mask);
1173#endif
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001174 buffer->cpus = nr_cpu_ids;
1175
1176 bsize = sizeof(void *) * nr_cpu_ids;
1177 buffer->buffers = kzalloc(ALIGN(bsize, cache_line_size()),
1178 GFP_KERNEL);
1179 if (!buffer->buffers)
Rusty Russell9e01c1b2009-01-01 10:12:22 +10301180 goto fail_free_cpumask;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001181
1182 for_each_buffer_cpu(buffer, cpu) {
1183 buffer->buffers[cpu] =
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001184 rb_allocate_cpu_buffer(buffer, nr_pages, cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001185 if (!buffer->buffers[cpu])
1186 goto fail_free_buffers;
1187 }
1188
Steven Rostedt59222ef2009-03-12 11:46:03 -04001189#ifdef CONFIG_HOTPLUG_CPU
Steven Rostedt554f7862009-03-11 22:00:13 -04001190 buffer->cpu_notify.notifier_call = rb_cpu_notify;
1191 buffer->cpu_notify.priority = 0;
1192 register_cpu_notifier(&buffer->cpu_notify);
1193#endif
1194
1195 put_online_cpus();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001196 mutex_init(&buffer->mutex);
1197
1198 return buffer;
1199
1200 fail_free_buffers:
1201 for_each_buffer_cpu(buffer, cpu) {
1202 if (buffer->buffers[cpu])
1203 rb_free_cpu_buffer(buffer->buffers[cpu]);
1204 }
1205 kfree(buffer->buffers);
1206
Rusty Russell9e01c1b2009-01-01 10:12:22 +10301207 fail_free_cpumask:
1208 free_cpumask_var(buffer->cpumask);
Steven Rostedt554f7862009-03-11 22:00:13 -04001209 put_online_cpus();
Rusty Russell9e01c1b2009-01-01 10:12:22 +10301210
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001211 fail_free_buffer:
1212 kfree(buffer);
1213 return NULL;
1214}
Peter Zijlstra1f8a6a12009-06-08 18:18:39 +02001215EXPORT_SYMBOL_GPL(__ring_buffer_alloc);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001216
1217/**
1218 * ring_buffer_free - free a ring buffer.
1219 * @buffer: the buffer to free.
1220 */
1221void
1222ring_buffer_free(struct ring_buffer *buffer)
1223{
1224 int cpu;
1225
Steven Rostedt554f7862009-03-11 22:00:13 -04001226 get_online_cpus();
1227
Steven Rostedt59222ef2009-03-12 11:46:03 -04001228#ifdef CONFIG_HOTPLUG_CPU
Steven Rostedt554f7862009-03-11 22:00:13 -04001229 unregister_cpu_notifier(&buffer->cpu_notify);
1230#endif
1231
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001232 for_each_buffer_cpu(buffer, cpu)
1233 rb_free_cpu_buffer(buffer->buffers[cpu]);
1234
Steven Rostedt554f7862009-03-11 22:00:13 -04001235 put_online_cpus();
1236
Eric Dumazetbd3f0222009-08-07 12:49:29 +02001237 kfree(buffer->buffers);
Rusty Russell9e01c1b2009-01-01 10:12:22 +10301238 free_cpumask_var(buffer->cpumask);
1239
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001240 kfree(buffer);
1241}
Robert Richterc4f50182008-12-11 16:49:22 +01001242EXPORT_SYMBOL_GPL(ring_buffer_free);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001243
Steven Rostedt37886f62009-03-17 17:22:06 -04001244void ring_buffer_set_clock(struct ring_buffer *buffer,
1245 u64 (*clock)(void))
1246{
1247 buffer->clock = clock;
1248}
1249
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001250static void rb_reset_cpu(struct ring_buffer_per_cpu *cpu_buffer);
1251
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001252static inline unsigned long rb_page_entries(struct buffer_page *bpage)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001253{
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001254 return local_read(&bpage->entries) & RB_WRITE_MASK;
1255}
1256
1257static inline unsigned long rb_page_write(struct buffer_page *bpage)
1258{
1259 return local_read(&bpage->write) & RB_WRITE_MASK;
1260}
1261
Vaibhav Nagarnaik5040b4b2012-05-03 18:59:51 -07001262static int
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001263rb_remove_pages(struct ring_buffer_per_cpu *cpu_buffer, unsigned int nr_pages)
1264{
1265 struct list_head *tail_page, *to_remove, *next_page;
1266 struct buffer_page *to_remove_page, *tmp_iter_page;
1267 struct buffer_page *last_page, *first_page;
1268 unsigned int nr_removed;
1269 unsigned long head_bit;
1270 int page_entries;
1271
1272 head_bit = 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001273
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02001274 raw_spin_lock_irq(&cpu_buffer->reader_lock);
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001275 atomic_inc(&cpu_buffer->record_disabled);
1276 /*
1277 * We don't race with the readers since we have acquired the reader
1278 * lock. We also don't race with writers after disabling recording.
1279 * This makes it easy to figure out the first and the last page to be
1280 * removed from the list. We unlink all the pages in between including
1281 * the first and last pages. This is done in a busy loop so that we
1282 * lose the least number of traces.
1283 * The pages are freed after we restart recording and unlock readers.
1284 */
1285 tail_page = &cpu_buffer->tail_page->list;
Steven Rostedt77ae3652009-03-27 11:00:29 -04001286
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001287 /*
1288 * tail page might be on reader page, we remove the next page
1289 * from the ring buffer
1290 */
1291 if (cpu_buffer->tail_page == cpu_buffer->reader_page)
1292 tail_page = rb_list_head(tail_page->next);
1293 to_remove = tail_page;
1294
1295 /* start of pages to remove */
1296 first_page = list_entry(rb_list_head(to_remove->next),
1297 struct buffer_page, list);
1298
1299 for (nr_removed = 0; nr_removed < nr_pages; nr_removed++) {
1300 to_remove = rb_list_head(to_remove)->next;
1301 head_bit |= (unsigned long)to_remove & RB_PAGE_HEAD;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001302 }
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001303
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001304 next_page = rb_list_head(to_remove)->next;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001305
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001306 /*
1307 * Now we remove all pages between tail_page and next_page.
1308 * Make sure that we have head_bit value preserved for the
1309 * next page
1310 */
1311 tail_page->next = (struct list_head *)((unsigned long)next_page |
1312 head_bit);
1313 next_page = rb_list_head(next_page);
1314 next_page->prev = tail_page;
1315
1316 /* make sure pages points to a valid page in the ring buffer */
1317 cpu_buffer->pages = next_page;
1318
1319 /* update head page */
1320 if (head_bit)
1321 cpu_buffer->head_page = list_entry(next_page,
1322 struct buffer_page, list);
1323
1324 /*
1325 * change read pointer to make sure any read iterators reset
1326 * themselves
1327 */
1328 cpu_buffer->read = 0;
1329
1330 /* pages are removed, resume tracing and then free the pages */
1331 atomic_dec(&cpu_buffer->record_disabled);
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02001332 raw_spin_unlock_irq(&cpu_buffer->reader_lock);
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001333
1334 RB_WARN_ON(cpu_buffer, list_empty(cpu_buffer->pages));
1335
1336 /* last buffer page to remove */
1337 last_page = list_entry(rb_list_head(to_remove), struct buffer_page,
1338 list);
1339 tmp_iter_page = first_page;
1340
1341 do {
1342 to_remove_page = tmp_iter_page;
1343 rb_inc_page(cpu_buffer, &tmp_iter_page);
1344
1345 /* update the counters */
1346 page_entries = rb_page_entries(to_remove_page);
1347 if (page_entries) {
1348 /*
1349 * If something was added to this page, it was full
1350 * since it is not the tail page. So we deduct the
1351 * bytes consumed in ring buffer from here.
Vaibhav Nagarnaik48fdc722012-06-29 12:31:41 -07001352 * Increment overrun to account for the lost events.
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001353 */
Vaibhav Nagarnaik48fdc722012-06-29 12:31:41 -07001354 local_add(page_entries, &cpu_buffer->overrun);
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001355 local_sub(BUF_PAGE_SIZE, &cpu_buffer->entries_bytes);
1356 }
1357
1358 /*
1359 * We have already removed references to this list item, just
1360 * free up the buffer_page and its page
1361 */
1362 free_buffer_page(to_remove_page);
1363 nr_removed--;
1364
1365 } while (to_remove_page != last_page);
1366
1367 RB_WARN_ON(cpu_buffer, nr_removed);
Vaibhav Nagarnaik5040b4b2012-05-03 18:59:51 -07001368
1369 return nr_removed == 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001370}
1371
Vaibhav Nagarnaik5040b4b2012-05-03 18:59:51 -07001372static int
1373rb_insert_pages(struct ring_buffer_per_cpu *cpu_buffer)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001374{
Vaibhav Nagarnaik5040b4b2012-05-03 18:59:51 -07001375 struct list_head *pages = &cpu_buffer->new_pages;
1376 int retries, success;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001377
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02001378 raw_spin_lock_irq(&cpu_buffer->reader_lock);
Vaibhav Nagarnaik5040b4b2012-05-03 18:59:51 -07001379 /*
1380 * We are holding the reader lock, so the reader page won't be swapped
1381 * in the ring buffer. Now we are racing with the writer trying to
1382 * move head page and the tail page.
1383 * We are going to adapt the reader page update process where:
1384 * 1. We first splice the start and end of list of new pages between
1385 * the head page and its previous page.
1386 * 2. We cmpxchg the prev_page->next to point from head page to the
1387 * start of new pages list.
1388 * 3. Finally, we update the head->prev to the end of new list.
1389 *
1390 * We will try this process 10 times, to make sure that we don't keep
1391 * spinning.
1392 */
1393 retries = 10;
1394 success = 0;
1395 while (retries--) {
1396 struct list_head *head_page, *prev_page, *r;
1397 struct list_head *last_page, *first_page;
1398 struct list_head *head_page_with_bit;
Steven Rostedt77ae3652009-03-27 11:00:29 -04001399
Vaibhav Nagarnaik5040b4b2012-05-03 18:59:51 -07001400 head_page = &rb_set_head_page(cpu_buffer)->list;
Steven Rostedt54f7be52012-11-29 22:27:22 -05001401 if (!head_page)
1402 break;
Vaibhav Nagarnaik5040b4b2012-05-03 18:59:51 -07001403 prev_page = head_page->prev;
1404
1405 first_page = pages->next;
1406 last_page = pages->prev;
1407
1408 head_page_with_bit = (struct list_head *)
1409 ((unsigned long)head_page | RB_PAGE_HEAD);
1410
1411 last_page->next = head_page_with_bit;
1412 first_page->prev = prev_page;
1413
1414 r = cmpxchg(&prev_page->next, head_page_with_bit, first_page);
1415
1416 if (r == head_page_with_bit) {
1417 /*
1418 * yay, we replaced the page pointer to our new list,
1419 * now, we just have to update to head page's prev
1420 * pointer to point to end of list
1421 */
1422 head_page->prev = last_page;
1423 success = 1;
1424 break;
1425 }
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001426 }
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001427
Vaibhav Nagarnaik5040b4b2012-05-03 18:59:51 -07001428 if (success)
1429 INIT_LIST_HEAD(pages);
1430 /*
1431 * If we weren't successful in adding in new pages, warn and stop
1432 * tracing
1433 */
1434 RB_WARN_ON(cpu_buffer, !success);
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02001435 raw_spin_unlock_irq(&cpu_buffer->reader_lock);
Vaibhav Nagarnaik5040b4b2012-05-03 18:59:51 -07001436
1437 /* free pages if they weren't inserted */
1438 if (!success) {
1439 struct buffer_page *bpage, *tmp;
1440 list_for_each_entry_safe(bpage, tmp, &cpu_buffer->new_pages,
1441 list) {
1442 list_del_init(&bpage->list);
1443 free_buffer_page(bpage);
1444 }
1445 }
1446 return success;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001447}
1448
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001449static void rb_update_pages(struct ring_buffer_per_cpu *cpu_buffer)
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001450{
Vaibhav Nagarnaik5040b4b2012-05-03 18:59:51 -07001451 int success;
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001452
Vaibhav Nagarnaik5040b4b2012-05-03 18:59:51 -07001453 if (cpu_buffer->nr_pages_to_update > 0)
1454 success = rb_insert_pages(cpu_buffer);
1455 else
1456 success = rb_remove_pages(cpu_buffer,
1457 -cpu_buffer->nr_pages_to_update);
1458
1459 if (success)
1460 cpu_buffer->nr_pages += cpu_buffer->nr_pages_to_update;
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001461}
1462
1463static void update_pages_handler(struct work_struct *work)
1464{
1465 struct ring_buffer_per_cpu *cpu_buffer = container_of(work,
1466 struct ring_buffer_per_cpu, update_pages_work);
1467 rb_update_pages(cpu_buffer);
Vaibhav Nagarnaik05fdd702012-05-18 13:29:51 -07001468 complete(&cpu_buffer->update_done);
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001469}
1470
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001471/**
1472 * ring_buffer_resize - resize the ring buffer
1473 * @buffer: the buffer to resize.
1474 * @size: the new size.
1475 *
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001476 * Minimum size is 2 * BUF_PAGE_SIZE.
1477 *
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001478 * Returns 0 on success and < 0 on failure.
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001479 */
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001480int ring_buffer_resize(struct ring_buffer *buffer, unsigned long size,
1481 int cpu_id)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001482{
1483 struct ring_buffer_per_cpu *cpu_buffer;
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001484 unsigned nr_pages;
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001485 int cpu, err = 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001486
Ingo Molnaree51a1d2008-11-13 14:58:31 +01001487 /*
1488 * Always succeed at resizing a non-existent buffer:
1489 */
1490 if (!buffer)
1491 return size;
1492
Steven Rostedt6a31e1f2012-05-23 15:35:17 -04001493 /* Make sure the requested buffer exists */
1494 if (cpu_id != RING_BUFFER_ALL_CPUS &&
1495 !cpumask_test_cpu(cpu_id, buffer->cpumask))
1496 return size;
1497
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001498 size = DIV_ROUND_UP(size, BUF_PAGE_SIZE);
1499 size *= BUF_PAGE_SIZE;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001500
1501 /* we need a minimum of two pages */
1502 if (size < BUF_PAGE_SIZE * 2)
1503 size = BUF_PAGE_SIZE * 2;
1504
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001505 nr_pages = DIV_ROUND_UP(size, BUF_PAGE_SIZE);
1506
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001507 /*
1508 * Don't succeed if resizing is disabled, as a reader might be
1509 * manipulating the ring buffer and is expecting a sane state while
1510 * this is true.
1511 */
1512 if (atomic_read(&buffer->resize_disabled))
1513 return -EBUSY;
1514
1515 /* prevent another thread from changing buffer sizes */
1516 mutex_lock(&buffer->mutex);
1517
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001518 if (cpu_id == RING_BUFFER_ALL_CPUS) {
1519 /* calculate the pages to update */
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001520 for_each_buffer_cpu(buffer, cpu) {
1521 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001522
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001523 cpu_buffer->nr_pages_to_update = nr_pages -
1524 cpu_buffer->nr_pages;
Vaibhav Nagarnaikd7ec4bf2011-06-07 17:01:42 -07001525 /*
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001526 * nothing more to do for removing pages or no update
Vaibhav Nagarnaikd7ec4bf2011-06-07 17:01:42 -07001527 */
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001528 if (cpu_buffer->nr_pages_to_update <= 0)
1529 continue;
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001530 /*
1531 * to add pages, make sure all new pages can be
1532 * allocated without receiving ENOMEM
1533 */
1534 INIT_LIST_HEAD(&cpu_buffer->new_pages);
1535 if (__rb_allocate_pages(cpu_buffer->nr_pages_to_update,
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001536 &cpu_buffer->new_pages, cpu)) {
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001537 /* not enough memory for new pages */
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001538 err = -ENOMEM;
1539 goto out_err;
1540 }
1541 }
1542
1543 get_online_cpus();
1544 /*
1545 * Fire off all the required work handlers
Vaibhav Nagarnaik05fdd702012-05-18 13:29:51 -07001546 * We can't schedule on offline CPUs, but it's not necessary
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001547 * since we can change their buffer sizes without any race.
1548 */
1549 for_each_buffer_cpu(buffer, cpu) {
1550 cpu_buffer = buffer->buffers[cpu];
Vaibhav Nagarnaik05fdd702012-05-18 13:29:51 -07001551 if (!cpu_buffer->nr_pages_to_update)
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001552 continue;
1553
Vaibhav Nagarnaik05fdd702012-05-18 13:29:51 -07001554 if (cpu_online(cpu))
1555 schedule_work_on(cpu,
1556 &cpu_buffer->update_pages_work);
1557 else
1558 rb_update_pages(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001559 }
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001560
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001561 /* wait for all the updates to complete */
1562 for_each_buffer_cpu(buffer, cpu) {
1563 cpu_buffer = buffer->buffers[cpu];
Vaibhav Nagarnaik05fdd702012-05-18 13:29:51 -07001564 if (!cpu_buffer->nr_pages_to_update)
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001565 continue;
1566
Vaibhav Nagarnaik05fdd702012-05-18 13:29:51 -07001567 if (cpu_online(cpu))
1568 wait_for_completion(&cpu_buffer->update_done);
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001569 cpu_buffer->nr_pages_to_update = 0;
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001570 }
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001571
1572 put_online_cpus();
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001573 } else {
Vaibhav Nagarnaik8e49f412012-10-10 16:40:27 -07001574 /* Make sure this CPU has been intitialized */
1575 if (!cpumask_test_cpu(cpu_id, buffer->cpumask))
1576 goto out;
1577
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001578 cpu_buffer = buffer->buffers[cpu_id];
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001579
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001580 if (nr_pages == cpu_buffer->nr_pages)
1581 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001582
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001583 cpu_buffer->nr_pages_to_update = nr_pages -
1584 cpu_buffer->nr_pages;
1585
1586 INIT_LIST_HEAD(&cpu_buffer->new_pages);
1587 if (cpu_buffer->nr_pages_to_update > 0 &&
1588 __rb_allocate_pages(cpu_buffer->nr_pages_to_update,
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001589 &cpu_buffer->new_pages, cpu_id)) {
1590 err = -ENOMEM;
1591 goto out_err;
1592 }
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001593
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001594 get_online_cpus();
1595
1596 if (cpu_online(cpu_id)) {
1597 schedule_work_on(cpu_id,
1598 &cpu_buffer->update_pages_work);
Vaibhav Nagarnaik05fdd702012-05-18 13:29:51 -07001599 wait_for_completion(&cpu_buffer->update_done);
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001600 } else
1601 rb_update_pages(cpu_buffer);
1602
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001603 cpu_buffer->nr_pages_to_update = 0;
Vaibhav Nagarnaik05fdd702012-05-18 13:29:51 -07001604 put_online_cpus();
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001605 }
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001606
1607 out:
Steven Rostedt659f4512012-05-14 17:02:33 -04001608 /*
1609 * The ring buffer resize can happen with the ring buffer
1610 * enabled, so that the update disturbs the tracing as little
1611 * as possible. But if the buffer is disabled, we do not need
1612 * to worry about that, and we can take the time to verify
1613 * that the buffer is not corrupt.
1614 */
1615 if (atomic_read(&buffer->record_disabled)) {
1616 atomic_inc(&buffer->record_disabled);
1617 /*
1618 * Even though the buffer was disabled, we must make sure
1619 * that it is truly disabled before calling rb_check_pages.
1620 * There could have been a race between checking
1621 * record_disable and incrementing it.
1622 */
1623 synchronize_sched();
1624 for_each_buffer_cpu(buffer, cpu) {
1625 cpu_buffer = buffer->buffers[cpu];
1626 rb_check_pages(cpu_buffer);
1627 }
1628 atomic_dec(&buffer->record_disabled);
1629 }
1630
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001631 mutex_unlock(&buffer->mutex);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001632 return size;
1633
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001634 out_err:
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001635 for_each_buffer_cpu(buffer, cpu) {
1636 struct buffer_page *bpage, *tmp;
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001637
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001638 cpu_buffer = buffer->buffers[cpu];
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001639 cpu_buffer->nr_pages_to_update = 0;
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001640
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001641 if (list_empty(&cpu_buffer->new_pages))
1642 continue;
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001643
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001644 list_for_each_entry_safe(bpage, tmp, &cpu_buffer->new_pages,
1645 list) {
1646 list_del_init(&bpage->list);
1647 free_buffer_page(bpage);
1648 }
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001649 }
Vegard Nossum641d2f62008-11-18 19:22:13 +01001650 mutex_unlock(&buffer->mutex);
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001651 return err;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001652}
Robert Richterc4f50182008-12-11 16:49:22 +01001653EXPORT_SYMBOL_GPL(ring_buffer_resize);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001654
David Sharp750912f2010-12-08 13:46:47 -08001655void ring_buffer_change_overwrite(struct ring_buffer *buffer, int val)
1656{
1657 mutex_lock(&buffer->mutex);
1658 if (val)
1659 buffer->flags |= RB_FL_OVERWRITE;
1660 else
1661 buffer->flags &= ~RB_FL_OVERWRITE;
1662 mutex_unlock(&buffer->mutex);
1663}
1664EXPORT_SYMBOL_GPL(ring_buffer_change_overwrite);
1665
Steven Rostedt8789a9e2008-12-02 15:34:07 -05001666static inline void *
Steven Rostedt044fa782008-12-02 23:50:03 -05001667__rb_data_page_index(struct buffer_data_page *bpage, unsigned index)
Steven Rostedt8789a9e2008-12-02 15:34:07 -05001668{
Steven Rostedt044fa782008-12-02 23:50:03 -05001669 return bpage->data + index;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05001670}
1671
Steven Rostedt044fa782008-12-02 23:50:03 -05001672static inline void *__rb_page_index(struct buffer_page *bpage, unsigned index)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001673{
Steven Rostedt044fa782008-12-02 23:50:03 -05001674 return bpage->page->data + index;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001675}
1676
1677static inline struct ring_buffer_event *
Steven Rostedtd7690412008-10-01 00:29:53 -04001678rb_reader_event(struct ring_buffer_per_cpu *cpu_buffer)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001679{
Steven Rostedt6f807ac2008-10-04 02:00:58 -04001680 return __rb_page_index(cpu_buffer->reader_page,
1681 cpu_buffer->reader_page->read);
1682}
1683
1684static inline struct ring_buffer_event *
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001685rb_iter_head_event(struct ring_buffer_iter *iter)
1686{
Steven Rostedt6f807ac2008-10-04 02:00:58 -04001687 return __rb_page_index(iter->head_page, iter->head);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001688}
1689
Steven Rostedtbf41a152008-10-04 02:00:59 -04001690static inline unsigned rb_page_commit(struct buffer_page *bpage)
1691{
Steven Rostedtabc9b562008-12-02 15:34:06 -05001692 return local_read(&bpage->page->commit);
Steven Rostedtbf41a152008-10-04 02:00:59 -04001693}
1694
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001695/* Size is determined by what has been committed */
Steven Rostedtbf41a152008-10-04 02:00:59 -04001696static inline unsigned rb_page_size(struct buffer_page *bpage)
1697{
1698 return rb_page_commit(bpage);
1699}
1700
1701static inline unsigned
1702rb_commit_index(struct ring_buffer_per_cpu *cpu_buffer)
1703{
1704 return rb_page_commit(cpu_buffer->commit_page);
1705}
1706
Steven Rostedtbf41a152008-10-04 02:00:59 -04001707static inline unsigned
1708rb_event_index(struct ring_buffer_event *event)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001709{
Steven Rostedtbf41a152008-10-04 02:00:59 -04001710 unsigned long addr = (unsigned long)event;
1711
Steven Rostedt22f470f2009-06-11 09:29:58 -04001712 return (addr & ~PAGE_MASK) - BUF_PAGE_HDR_SIZE;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001713}
1714
Steven Rostedt0f0c85f2009-05-11 16:08:00 -04001715static inline int
Steven Rostedtfa743952009-06-16 12:37:57 -04001716rb_event_is_commit(struct ring_buffer_per_cpu *cpu_buffer,
1717 struct ring_buffer_event *event)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001718{
Steven Rostedtbf41a152008-10-04 02:00:59 -04001719 unsigned long addr = (unsigned long)event;
1720 unsigned long index;
1721
1722 index = rb_event_index(event);
1723 addr &= PAGE_MASK;
1724
1725 return cpu_buffer->commit_page->page == (void *)addr &&
1726 rb_commit_index(cpu_buffer) == index;
1727}
1728
Andrew Morton34a148b2009-01-09 12:27:09 -08001729static void
Steven Rostedtbf41a152008-10-04 02:00:59 -04001730rb_set_commit_to_write(struct ring_buffer_per_cpu *cpu_buffer)
1731{
Steven Rostedt77ae3652009-03-27 11:00:29 -04001732 unsigned long max_count;
1733
Steven Rostedtbf41a152008-10-04 02:00:59 -04001734 /*
1735 * We only race with interrupts and NMIs on this CPU.
1736 * If we own the commit event, then we can commit
1737 * all others that interrupted us, since the interruptions
1738 * are in stack format (they finish before they come
1739 * back to us). This allows us to do a simple loop to
1740 * assign the commit to the tail.
1741 */
Steven Rostedta8ccf1d2008-12-23 11:32:24 -05001742 again:
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001743 max_count = cpu_buffer->nr_pages * 100;
Steven Rostedt77ae3652009-03-27 11:00:29 -04001744
Steven Rostedtbf41a152008-10-04 02:00:59 -04001745 while (cpu_buffer->commit_page != cpu_buffer->tail_page) {
Steven Rostedt77ae3652009-03-27 11:00:29 -04001746 if (RB_WARN_ON(cpu_buffer, !(--max_count)))
1747 return;
1748 if (RB_WARN_ON(cpu_buffer,
1749 rb_is_reader_page(cpu_buffer->tail_page)))
1750 return;
1751 local_set(&cpu_buffer->commit_page->page->commit,
1752 rb_page_write(cpu_buffer->commit_page));
Steven Rostedtbf41a152008-10-04 02:00:59 -04001753 rb_inc_page(cpu_buffer, &cpu_buffer->commit_page);
Steven Rostedtabc9b562008-12-02 15:34:06 -05001754 cpu_buffer->write_stamp =
1755 cpu_buffer->commit_page->page->time_stamp;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001756 /* add barrier to keep gcc from optimizing too much */
1757 barrier();
1758 }
1759 while (rb_commit_index(cpu_buffer) !=
1760 rb_page_write(cpu_buffer->commit_page)) {
Steven Rostedt77ae3652009-03-27 11:00:29 -04001761
1762 local_set(&cpu_buffer->commit_page->page->commit,
1763 rb_page_write(cpu_buffer->commit_page));
1764 RB_WARN_ON(cpu_buffer,
1765 local_read(&cpu_buffer->commit_page->page->commit) &
1766 ~RB_WRITE_MASK);
Steven Rostedtbf41a152008-10-04 02:00:59 -04001767 barrier();
1768 }
Steven Rostedta8ccf1d2008-12-23 11:32:24 -05001769
1770 /* again, keep gcc from optimizing */
1771 barrier();
1772
1773 /*
1774 * If an interrupt came in just after the first while loop
1775 * and pushed the tail page forward, we will be left with
1776 * a dangling commit that will never go forward.
1777 */
1778 if (unlikely(cpu_buffer->commit_page != cpu_buffer->tail_page))
1779 goto again;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001780}
1781
Steven Rostedtd7690412008-10-01 00:29:53 -04001782static void rb_reset_reader_page(struct ring_buffer_per_cpu *cpu_buffer)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001783{
Steven Rostedtabc9b562008-12-02 15:34:06 -05001784 cpu_buffer->read_stamp = cpu_buffer->reader_page->page->time_stamp;
Steven Rostedt6f807ac2008-10-04 02:00:58 -04001785 cpu_buffer->reader_page->read = 0;
Steven Rostedtd7690412008-10-01 00:29:53 -04001786}
1787
Andrew Morton34a148b2009-01-09 12:27:09 -08001788static void rb_inc_iter(struct ring_buffer_iter *iter)
Steven Rostedtd7690412008-10-01 00:29:53 -04001789{
1790 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
1791
1792 /*
1793 * The iterator could be on the reader page (it starts there).
1794 * But the head could have moved, since the reader was
1795 * found. Check for this case and assign the iterator
1796 * to the head page instead of next.
1797 */
1798 if (iter->head_page == cpu_buffer->reader_page)
Steven Rostedt77ae3652009-03-27 11:00:29 -04001799 iter->head_page = rb_set_head_page(cpu_buffer);
Steven Rostedtd7690412008-10-01 00:29:53 -04001800 else
1801 rb_inc_page(cpu_buffer, &iter->head_page);
1802
Steven Rostedtabc9b562008-12-02 15:34:06 -05001803 iter->read_stamp = iter->head_page->page->time_stamp;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001804 iter->head = 0;
1805}
1806
Steven Rostedt69d1b832010-10-07 18:18:05 -04001807/* Slow path, do not inline */
1808static noinline struct ring_buffer_event *
1809rb_add_time_stamp(struct ring_buffer_event *event, u64 delta)
1810{
1811 event->type_len = RINGBUF_TYPE_TIME_EXTEND;
1812
1813 /* Not the first event on the page? */
1814 if (rb_event_index(event)) {
1815 event->time_delta = delta & TS_MASK;
1816 event->array[0] = delta >> TS_SHIFT;
1817 } else {
1818 /* nope, just zero it */
1819 event->time_delta = 0;
1820 event->array[0] = 0;
1821 }
1822
1823 return skip_time_extend(event);
1824}
1825
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001826/**
David Sharp01e3e712012-06-07 16:46:24 -07001827 * rb_update_event - update event type and data
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001828 * @event: the even to update
1829 * @type: the type of event
1830 * @length: the size of the event field in the ring buffer
1831 *
1832 * Update the type and data fields of the event. The length
1833 * is the actual size that is written to the ring buffer,
1834 * and with this, we can determine what to place into the
1835 * data field.
1836 */
Andrew Morton34a148b2009-01-09 12:27:09 -08001837static void
Steven Rostedt69d1b832010-10-07 18:18:05 -04001838rb_update_event(struct ring_buffer_per_cpu *cpu_buffer,
1839 struct ring_buffer_event *event, unsigned length,
1840 int add_timestamp, u64 delta)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001841{
Steven Rostedt69d1b832010-10-07 18:18:05 -04001842 /* Only a commit updates the timestamp */
1843 if (unlikely(!rb_event_is_commit(cpu_buffer, event)))
1844 delta = 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001845
Steven Rostedt69d1b832010-10-07 18:18:05 -04001846 /*
1847 * If we need to add a timestamp, then we
1848 * add it to the start of the resevered space.
1849 */
1850 if (unlikely(add_timestamp)) {
1851 event = rb_add_time_stamp(event, delta);
1852 length -= RB_LEN_TIME_EXTEND;
1853 delta = 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001854 }
Steven Rostedt69d1b832010-10-07 18:18:05 -04001855
1856 event->time_delta = delta;
1857 length -= RB_EVNT_HDR_SIZE;
1858 if (length > RB_MAX_SMALL_DATA || RB_FORCE_8BYTE_ALIGNMENT) {
1859 event->type_len = 0;
1860 event->array[0] = length;
1861 } else
1862 event->type_len = DIV_ROUND_UP(length, RB_ALIGNMENT);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001863}
1864
Steven Rostedt77ae3652009-03-27 11:00:29 -04001865/*
1866 * rb_handle_head_page - writer hit the head page
1867 *
1868 * Returns: +1 to retry page
1869 * 0 to continue
1870 * -1 on error
1871 */
1872static int
1873rb_handle_head_page(struct ring_buffer_per_cpu *cpu_buffer,
1874 struct buffer_page *tail_page,
1875 struct buffer_page *next_page)
1876{
1877 struct buffer_page *new_head;
1878 int entries;
1879 int type;
1880 int ret;
1881
1882 entries = rb_page_entries(next_page);
1883
1884 /*
1885 * The hard part is here. We need to move the head
1886 * forward, and protect against both readers on
1887 * other CPUs and writers coming in via interrupts.
1888 */
1889 type = rb_head_page_set_update(cpu_buffer, next_page, tail_page,
1890 RB_PAGE_HEAD);
1891
1892 /*
1893 * type can be one of four:
1894 * NORMAL - an interrupt already moved it for us
1895 * HEAD - we are the first to get here.
1896 * UPDATE - we are the interrupt interrupting
1897 * a current move.
1898 * MOVED - a reader on another CPU moved the next
1899 * pointer to its reader page. Give up
1900 * and try again.
1901 */
1902
1903 switch (type) {
1904 case RB_PAGE_HEAD:
1905 /*
1906 * We changed the head to UPDATE, thus
1907 * it is our responsibility to update
1908 * the counters.
1909 */
1910 local_add(entries, &cpu_buffer->overrun);
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -07001911 local_sub(BUF_PAGE_SIZE, &cpu_buffer->entries_bytes);
Steven Rostedt77ae3652009-03-27 11:00:29 -04001912
1913 /*
1914 * The entries will be zeroed out when we move the
1915 * tail page.
1916 */
1917
1918 /* still more to do */
1919 break;
1920
1921 case RB_PAGE_UPDATE:
1922 /*
1923 * This is an interrupt that interrupt the
1924 * previous update. Still more to do.
1925 */
1926 break;
1927 case RB_PAGE_NORMAL:
1928 /*
1929 * An interrupt came in before the update
1930 * and processed this for us.
1931 * Nothing left to do.
1932 */
1933 return 1;
1934 case RB_PAGE_MOVED:
1935 /*
1936 * The reader is on another CPU and just did
1937 * a swap with our next_page.
1938 * Try again.
1939 */
1940 return 1;
1941 default:
1942 RB_WARN_ON(cpu_buffer, 1); /* WTF??? */
1943 return -1;
1944 }
1945
1946 /*
1947 * Now that we are here, the old head pointer is
1948 * set to UPDATE. This will keep the reader from
1949 * swapping the head page with the reader page.
1950 * The reader (on another CPU) will spin till
1951 * we are finished.
1952 *
1953 * We just need to protect against interrupts
1954 * doing the job. We will set the next pointer
1955 * to HEAD. After that, we set the old pointer
1956 * to NORMAL, but only if it was HEAD before.
1957 * otherwise we are an interrupt, and only
1958 * want the outer most commit to reset it.
1959 */
1960 new_head = next_page;
1961 rb_inc_page(cpu_buffer, &new_head);
1962
1963 ret = rb_head_page_set_head(cpu_buffer, new_head, next_page,
1964 RB_PAGE_NORMAL);
1965
1966 /*
1967 * Valid returns are:
1968 * HEAD - an interrupt came in and already set it.
1969 * NORMAL - One of two things:
1970 * 1) We really set it.
1971 * 2) A bunch of interrupts came in and moved
1972 * the page forward again.
1973 */
1974 switch (ret) {
1975 case RB_PAGE_HEAD:
1976 case RB_PAGE_NORMAL:
1977 /* OK */
1978 break;
1979 default:
1980 RB_WARN_ON(cpu_buffer, 1);
1981 return -1;
1982 }
1983
1984 /*
1985 * It is possible that an interrupt came in,
1986 * set the head up, then more interrupts came in
1987 * and moved it again. When we get back here,
1988 * the page would have been set to NORMAL but we
1989 * just set it back to HEAD.
1990 *
1991 * How do you detect this? Well, if that happened
1992 * the tail page would have moved.
1993 */
1994 if (ret == RB_PAGE_NORMAL) {
1995 /*
1996 * If the tail had moved passed next, then we need
1997 * to reset the pointer.
1998 */
1999 if (cpu_buffer->tail_page != tail_page &&
2000 cpu_buffer->tail_page != next_page)
2001 rb_head_page_set_normal(cpu_buffer, new_head,
2002 next_page,
2003 RB_PAGE_HEAD);
2004 }
2005
2006 /*
2007 * If this was the outer most commit (the one that
2008 * changed the original pointer from HEAD to UPDATE),
2009 * then it is up to us to reset it to NORMAL.
2010 */
2011 if (type == RB_PAGE_HEAD) {
2012 ret = rb_head_page_set_normal(cpu_buffer, next_page,
2013 tail_page,
2014 RB_PAGE_UPDATE);
2015 if (RB_WARN_ON(cpu_buffer,
2016 ret != RB_PAGE_UPDATE))
2017 return -1;
2018 }
2019
2020 return 0;
2021}
2022
Andrew Morton34a148b2009-01-09 12:27:09 -08002023static unsigned rb_calculate_event_length(unsigned length)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002024{
2025 struct ring_buffer_event event; /* Used only for sizeof array */
2026
2027 /* zero length can cause confusions */
2028 if (!length)
2029 length = 1;
2030
Steven Rostedt22710482010-03-18 17:54:19 -04002031 if (length > RB_MAX_SMALL_DATA || RB_FORCE_8BYTE_ALIGNMENT)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002032 length += sizeof(event.array[0]);
2033
2034 length += RB_EVNT_HDR_SIZE;
Steven Rostedt22710482010-03-18 17:54:19 -04002035 length = ALIGN(length, RB_ARCH_ALIGNMENT);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002036
2037 return length;
2038}
2039
Steven Rostedtc7b09302009-06-11 11:12:00 -04002040static inline void
2041rb_reset_tail(struct ring_buffer_per_cpu *cpu_buffer,
2042 struct buffer_page *tail_page,
2043 unsigned long tail, unsigned long length)
2044{
2045 struct ring_buffer_event *event;
2046
2047 /*
2048 * Only the event that crossed the page boundary
2049 * must fill the old tail_page with padding.
2050 */
2051 if (tail >= BUF_PAGE_SIZE) {
Steven Rostedtb3230c82010-05-21 11:55:21 -04002052 /*
2053 * If the page was filled, then we still need
2054 * to update the real_end. Reset it to zero
2055 * and the reader will ignore it.
2056 */
2057 if (tail == BUF_PAGE_SIZE)
2058 tail_page->real_end = 0;
2059
Steven Rostedtc7b09302009-06-11 11:12:00 -04002060 local_sub(length, &tail_page->write);
2061 return;
2062 }
2063
2064 event = __rb_page_index(tail_page, tail);
Linus Torvaldsb0b70652009-06-20 10:56:46 -07002065 kmemcheck_annotate_bitfield(event, bitfield);
Steven Rostedtc7b09302009-06-11 11:12:00 -04002066
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -07002067 /* account for padding bytes */
2068 local_add(BUF_PAGE_SIZE - tail, &cpu_buffer->entries_bytes);
2069
Steven Rostedtc7b09302009-06-11 11:12:00 -04002070 /*
Steven Rostedtff0ff842010-03-31 22:11:42 -04002071 * Save the original length to the meta data.
2072 * This will be used by the reader to add lost event
2073 * counter.
2074 */
2075 tail_page->real_end = tail;
2076
2077 /*
Steven Rostedtc7b09302009-06-11 11:12:00 -04002078 * If this event is bigger than the minimum size, then
2079 * we need to be careful that we don't subtract the
2080 * write counter enough to allow another writer to slip
2081 * in on this page.
2082 * We put in a discarded commit instead, to make sure
2083 * that this space is not used again.
2084 *
2085 * If we are less than the minimum size, we don't need to
2086 * worry about it.
2087 */
2088 if (tail > (BUF_PAGE_SIZE - RB_EVNT_MIN_SIZE)) {
2089 /* No room for any events */
2090
2091 /* Mark the rest of the page with padding */
2092 rb_event_set_padding(event);
2093
2094 /* Set the write back to the previous setting */
2095 local_sub(length, &tail_page->write);
2096 return;
2097 }
2098
2099 /* Put in a discarded event */
2100 event->array[0] = (BUF_PAGE_SIZE - tail) - RB_EVNT_HDR_SIZE;
2101 event->type_len = RINGBUF_TYPE_PADDING;
2102 /* time delta must be non zero */
2103 event->time_delta = 1;
Steven Rostedtc7b09302009-06-11 11:12:00 -04002104
2105 /* Set write to end of buffer */
2106 length = (tail + length) - BUF_PAGE_SIZE;
2107 local_sub(length, &tail_page->write);
2108}
Steven Rostedt6634ff22009-05-06 15:30:07 -04002109
Steven Rostedt747e94a2010-10-08 13:51:48 -04002110/*
2111 * This is the slow path, force gcc not to inline it.
2112 */
2113static noinline struct ring_buffer_event *
Steven Rostedt6634ff22009-05-06 15:30:07 -04002114rb_move_tail(struct ring_buffer_per_cpu *cpu_buffer,
2115 unsigned long length, unsigned long tail,
Steven Rostedte8bc43e2010-10-20 10:58:02 -04002116 struct buffer_page *tail_page, u64 ts)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002117{
Steven Rostedt5a50e332009-11-17 08:43:01 -05002118 struct buffer_page *commit_page = cpu_buffer->commit_page;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002119 struct ring_buffer *buffer = cpu_buffer->buffer;
Steven Rostedt77ae3652009-03-27 11:00:29 -04002120 struct buffer_page *next_page;
2121 int ret;
Steven Rostedtaa20ae82009-05-05 21:16:11 -04002122
2123 next_page = tail_page;
2124
Steven Rostedtaa20ae82009-05-05 21:16:11 -04002125 rb_inc_page(cpu_buffer, &next_page);
2126
Steven Rostedtaa20ae82009-05-05 21:16:11 -04002127 /*
2128 * If for some reason, we had an interrupt storm that made
2129 * it all the way around the buffer, bail, and warn
2130 * about it.
2131 */
2132 if (unlikely(next_page == commit_page)) {
Steven Rostedt77ae3652009-03-27 11:00:29 -04002133 local_inc(&cpu_buffer->commit_overrun);
Steven Rostedtaa20ae82009-05-05 21:16:11 -04002134 goto out_reset;
2135 }
2136
Steven Rostedt77ae3652009-03-27 11:00:29 -04002137 /*
2138 * This is where the fun begins!
2139 *
2140 * We are fighting against races between a reader that
2141 * could be on another CPU trying to swap its reader
2142 * page with the buffer head.
2143 *
2144 * We are also fighting against interrupts coming in and
2145 * moving the head or tail on us as well.
2146 *
2147 * If the next page is the head page then we have filled
2148 * the buffer, unless the commit page is still on the
2149 * reader page.
2150 */
2151 if (rb_is_head_page(cpu_buffer, next_page, &tail_page->list)) {
Steven Rostedtaa20ae82009-05-05 21:16:11 -04002152
Steven Rostedt77ae3652009-03-27 11:00:29 -04002153 /*
2154 * If the commit is not on the reader page, then
2155 * move the header page.
2156 */
2157 if (!rb_is_reader_page(cpu_buffer->commit_page)) {
2158 /*
2159 * If we are not in overwrite mode,
2160 * this is easy, just stop here.
2161 */
Slava Pestov884bfe82011-07-15 14:23:58 -07002162 if (!(buffer->flags & RB_FL_OVERWRITE)) {
2163 local_inc(&cpu_buffer->dropped_events);
Steven Rostedt77ae3652009-03-27 11:00:29 -04002164 goto out_reset;
Slava Pestov884bfe82011-07-15 14:23:58 -07002165 }
Steven Rostedtaa20ae82009-05-05 21:16:11 -04002166
Steven Rostedt77ae3652009-03-27 11:00:29 -04002167 ret = rb_handle_head_page(cpu_buffer,
2168 tail_page,
2169 next_page);
2170 if (ret < 0)
2171 goto out_reset;
2172 if (ret)
2173 goto out_again;
2174 } else {
2175 /*
2176 * We need to be careful here too. The
2177 * commit page could still be on the reader
2178 * page. We could have a small buffer, and
2179 * have filled up the buffer with events
2180 * from interrupts and such, and wrapped.
2181 *
2182 * Note, if the tail page is also the on the
2183 * reader_page, we let it move out.
2184 */
2185 if (unlikely((cpu_buffer->commit_page !=
2186 cpu_buffer->tail_page) &&
2187 (cpu_buffer->commit_page ==
2188 cpu_buffer->reader_page))) {
2189 local_inc(&cpu_buffer->commit_overrun);
2190 goto out_reset;
2191 }
Steven Rostedtaa20ae82009-05-05 21:16:11 -04002192 }
2193 }
2194
Steven Rostedt77ae3652009-03-27 11:00:29 -04002195 ret = rb_tail_page_update(cpu_buffer, tail_page, next_page);
2196 if (ret) {
2197 /*
2198 * Nested commits always have zero deltas, so
2199 * just reread the time stamp
2200 */
Steven Rostedte8bc43e2010-10-20 10:58:02 -04002201 ts = rb_time_stamp(buffer);
2202 next_page->page->time_stamp = ts;
Steven Rostedtaa20ae82009-05-05 21:16:11 -04002203 }
2204
Steven Rostedt77ae3652009-03-27 11:00:29 -04002205 out_again:
Steven Rostedtaa20ae82009-05-05 21:16:11 -04002206
Steven Rostedt77ae3652009-03-27 11:00:29 -04002207 rb_reset_tail(cpu_buffer, tail_page, tail, length);
Steven Rostedtaa20ae82009-05-05 21:16:11 -04002208
2209 /* fail and let the caller try again */
2210 return ERR_PTR(-EAGAIN);
2211
Steven Rostedt45141d42009-02-12 13:19:48 -05002212 out_reset:
Lai Jiangshan6f3b3442009-01-12 11:06:18 +08002213 /* reset write */
Steven Rostedtc7b09302009-06-11 11:12:00 -04002214 rb_reset_tail(cpu_buffer, tail_page, tail, length);
Lai Jiangshan6f3b3442009-01-12 11:06:18 +08002215
Steven Rostedtbf41a152008-10-04 02:00:59 -04002216 return NULL;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002217}
2218
Steven Rostedt6634ff22009-05-06 15:30:07 -04002219static struct ring_buffer_event *
2220__rb_reserve_next(struct ring_buffer_per_cpu *cpu_buffer,
Steven Rostedt69d1b832010-10-07 18:18:05 -04002221 unsigned long length, u64 ts,
2222 u64 delta, int add_timestamp)
Steven Rostedt6634ff22009-05-06 15:30:07 -04002223{
Steven Rostedt5a50e332009-11-17 08:43:01 -05002224 struct buffer_page *tail_page;
Steven Rostedt6634ff22009-05-06 15:30:07 -04002225 struct ring_buffer_event *event;
2226 unsigned long tail, write;
2227
Steven Rostedt69d1b832010-10-07 18:18:05 -04002228 /*
2229 * If the time delta since the last event is too big to
2230 * hold in the time field of the event, then we append a
2231 * TIME EXTEND event ahead of the data event.
2232 */
2233 if (unlikely(add_timestamp))
2234 length += RB_LEN_TIME_EXTEND;
2235
Steven Rostedt6634ff22009-05-06 15:30:07 -04002236 tail_page = cpu_buffer->tail_page;
2237 write = local_add_return(length, &tail_page->write);
Steven Rostedt77ae3652009-03-27 11:00:29 -04002238
2239 /* set write to only the index of the write */
2240 write &= RB_WRITE_MASK;
Steven Rostedt6634ff22009-05-06 15:30:07 -04002241 tail = write - length;
2242
2243 /* See if we shot pass the end of this buffer page */
Steven Rostedt747e94a2010-10-08 13:51:48 -04002244 if (unlikely(write > BUF_PAGE_SIZE))
Steven Rostedt6634ff22009-05-06 15:30:07 -04002245 return rb_move_tail(cpu_buffer, length, tail,
Steven Rostedt5a50e332009-11-17 08:43:01 -05002246 tail_page, ts);
Steven Rostedt6634ff22009-05-06 15:30:07 -04002247
2248 /* We reserved something on the buffer */
2249
Steven Rostedt6634ff22009-05-06 15:30:07 -04002250 event = __rb_page_index(tail_page, tail);
Vegard Nossum1744a212009-02-28 08:29:44 +01002251 kmemcheck_annotate_bitfield(event, bitfield);
Steven Rostedt69d1b832010-10-07 18:18:05 -04002252 rb_update_event(cpu_buffer, event, length, add_timestamp, delta);
Steven Rostedt6634ff22009-05-06 15:30:07 -04002253
Steven Rostedt69d1b832010-10-07 18:18:05 -04002254 local_inc(&tail_page->entries);
Steven Rostedt6634ff22009-05-06 15:30:07 -04002255
2256 /*
Steven Rostedtfa743952009-06-16 12:37:57 -04002257 * If this is the first commit on the page, then update
2258 * its timestamp.
Steven Rostedt6634ff22009-05-06 15:30:07 -04002259 */
Steven Rostedtfa743952009-06-16 12:37:57 -04002260 if (!tail)
Steven Rostedte8bc43e2010-10-20 10:58:02 -04002261 tail_page->page->time_stamp = ts;
Steven Rostedt6634ff22009-05-06 15:30:07 -04002262
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -07002263 /* account for these added bytes */
2264 local_add(length, &cpu_buffer->entries_bytes);
2265
Steven Rostedt6634ff22009-05-06 15:30:07 -04002266 return event;
2267}
2268
Steven Rostedtedd813bf2009-06-02 23:00:53 -04002269static inline int
2270rb_try_to_discard(struct ring_buffer_per_cpu *cpu_buffer,
2271 struct ring_buffer_event *event)
2272{
2273 unsigned long new_index, old_index;
2274 struct buffer_page *bpage;
2275 unsigned long index;
2276 unsigned long addr;
2277
2278 new_index = rb_event_index(event);
Steven Rostedt69d1b832010-10-07 18:18:05 -04002279 old_index = new_index + rb_event_ts_length(event);
Steven Rostedtedd813bf2009-06-02 23:00:53 -04002280 addr = (unsigned long)event;
2281 addr &= PAGE_MASK;
2282
2283 bpage = cpu_buffer->tail_page;
2284
2285 if (bpage->page == (void *)addr && rb_page_write(bpage) == old_index) {
Steven Rostedt77ae3652009-03-27 11:00:29 -04002286 unsigned long write_mask =
2287 local_read(&bpage->write) & ~RB_WRITE_MASK;
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -07002288 unsigned long event_length = rb_event_length(event);
Steven Rostedtedd813bf2009-06-02 23:00:53 -04002289 /*
2290 * This is on the tail page. It is possible that
2291 * a write could come in and move the tail page
2292 * and write to the next page. That is fine
2293 * because we just shorten what is on this page.
2294 */
Steven Rostedt77ae3652009-03-27 11:00:29 -04002295 old_index += write_mask;
2296 new_index += write_mask;
Steven Rostedtedd813bf2009-06-02 23:00:53 -04002297 index = local_cmpxchg(&bpage->write, old_index, new_index);
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -07002298 if (index == old_index) {
2299 /* update counters */
2300 local_sub(event_length, &cpu_buffer->entries_bytes);
Steven Rostedtedd813bf2009-06-02 23:00:53 -04002301 return 1;
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -07002302 }
Steven Rostedtedd813bf2009-06-02 23:00:53 -04002303 }
2304
2305 /* could not discard */
2306 return 0;
2307}
2308
Steven Rostedtfa743952009-06-16 12:37:57 -04002309static void rb_start_commit(struct ring_buffer_per_cpu *cpu_buffer)
2310{
2311 local_inc(&cpu_buffer->committing);
2312 local_inc(&cpu_buffer->commits);
2313}
2314
Steven Rostedtd9abde22010-10-19 13:17:08 -04002315static inline void rb_end_commit(struct ring_buffer_per_cpu *cpu_buffer)
Steven Rostedtfa743952009-06-16 12:37:57 -04002316{
2317 unsigned long commits;
2318
2319 if (RB_WARN_ON(cpu_buffer,
2320 !local_read(&cpu_buffer->committing)))
2321 return;
2322
2323 again:
2324 commits = local_read(&cpu_buffer->commits);
2325 /* synchronize with interrupts */
2326 barrier();
2327 if (local_read(&cpu_buffer->committing) == 1)
2328 rb_set_commit_to_write(cpu_buffer);
2329
2330 local_dec(&cpu_buffer->committing);
2331
2332 /* synchronize with interrupts */
2333 barrier();
2334
2335 /*
2336 * Need to account for interrupts coming in between the
2337 * updating of the commit page and the clearing of the
2338 * committing counter.
2339 */
2340 if (unlikely(local_read(&cpu_buffer->commits) != commits) &&
2341 !local_read(&cpu_buffer->committing)) {
2342 local_inc(&cpu_buffer->committing);
2343 goto again;
2344 }
2345}
2346
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002347static struct ring_buffer_event *
Steven Rostedt62f0b3e2009-09-04 14:11:34 -04002348rb_reserve_next_event(struct ring_buffer *buffer,
2349 struct ring_buffer_per_cpu *cpu_buffer,
Steven Rostedt1cd8d732009-05-11 14:08:09 -04002350 unsigned long length)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002351{
2352 struct ring_buffer_event *event;
Steven Rostedt69d1b832010-10-07 18:18:05 -04002353 u64 ts, delta;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002354 int nr_loops = 0;
Steven Rostedt69d1b832010-10-07 18:18:05 -04002355 int add_timestamp;
Steven Rostedt140ff892010-10-08 10:50:30 -04002356 u64 diff;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002357
Steven Rostedtfa743952009-06-16 12:37:57 -04002358 rb_start_commit(cpu_buffer);
2359
Steven Rostedt85bac322009-09-04 14:24:40 -04002360#ifdef CONFIG_RING_BUFFER_ALLOW_SWAP
Steven Rostedt62f0b3e2009-09-04 14:11:34 -04002361 /*
2362 * Due to the ability to swap a cpu buffer from a buffer
2363 * it is possible it was swapped before we committed.
2364 * (committing stops a swap). We check for it here and
2365 * if it happened, we have to fail the write.
2366 */
2367 barrier();
2368 if (unlikely(ACCESS_ONCE(cpu_buffer->buffer) != buffer)) {
2369 local_dec(&cpu_buffer->committing);
2370 local_dec(&cpu_buffer->commits);
2371 return NULL;
2372 }
Steven Rostedt85bac322009-09-04 14:24:40 -04002373#endif
Steven Rostedt62f0b3e2009-09-04 14:11:34 -04002374
Steven Rostedtbe957c42009-05-11 14:42:53 -04002375 length = rb_calculate_event_length(length);
Steven Rostedtbf41a152008-10-04 02:00:59 -04002376 again:
Steven Rostedt69d1b832010-10-07 18:18:05 -04002377 add_timestamp = 0;
2378 delta = 0;
2379
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002380 /*
2381 * We allow for interrupts to reenter here and do a trace.
2382 * If one does, it will cause this original code to loop
2383 * back here. Even with heavy interrupts happening, this
2384 * should only happen a few times in a row. If this happens
2385 * 1000 times in a row, there must be either an interrupt
2386 * storm or we have something buggy.
2387 * Bail!
2388 */
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05002389 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 1000))
Steven Rostedtfa743952009-06-16 12:37:57 -04002390 goto out_fail;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002391
Jiri Olsa6d3f1e12009-10-23 19:36:19 -04002392 ts = rb_time_stamp(cpu_buffer->buffer);
Steven Rostedt140ff892010-10-08 10:50:30 -04002393 diff = ts - cpu_buffer->write_stamp;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002394
Steven Rostedt140ff892010-10-08 10:50:30 -04002395 /* make sure this diff is calculated here */
2396 barrier();
Steven Rostedtbf41a152008-10-04 02:00:59 -04002397
Steven Rostedt140ff892010-10-08 10:50:30 -04002398 /* Did the write stamp get updated already? */
2399 if (likely(ts >= cpu_buffer->write_stamp)) {
Steven Rostedt168b6b12009-05-11 22:11:05 -04002400 delta = diff;
2401 if (unlikely(test_time_stamp(delta))) {
Jiri Olsa31274d72011-02-18 15:52:19 +01002402 int local_clock_stable = 1;
2403#ifdef CONFIG_HAVE_UNSTABLE_SCHED_CLOCK
2404 local_clock_stable = sched_clock_stable;
2405#endif
Steven Rostedt69d1b832010-10-07 18:18:05 -04002406 WARN_ONCE(delta > (1ULL << 59),
Jiri Olsa31274d72011-02-18 15:52:19 +01002407 KERN_WARNING "Delta way too big! %llu ts=%llu write stamp = %llu\n%s",
Steven Rostedt69d1b832010-10-07 18:18:05 -04002408 (unsigned long long)delta,
2409 (unsigned long long)ts,
Jiri Olsa31274d72011-02-18 15:52:19 +01002410 (unsigned long long)cpu_buffer->write_stamp,
2411 local_clock_stable ? "" :
2412 "If you just came from a suspend/resume,\n"
2413 "please switch to the trace global clock:\n"
2414 " echo global > /sys/kernel/debug/tracing/trace_clock\n");
Steven Rostedt69d1b832010-10-07 18:18:05 -04002415 add_timestamp = 1;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002416 }
Steven Rostedt168b6b12009-05-11 22:11:05 -04002417 }
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002418
Steven Rostedt69d1b832010-10-07 18:18:05 -04002419 event = __rb_reserve_next(cpu_buffer, length, ts,
2420 delta, add_timestamp);
Steven Rostedt168b6b12009-05-11 22:11:05 -04002421 if (unlikely(PTR_ERR(event) == -EAGAIN))
Steven Rostedtbf41a152008-10-04 02:00:59 -04002422 goto again;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002423
Steven Rostedtfa743952009-06-16 12:37:57 -04002424 if (!event)
2425 goto out_fail;
Steven Rostedtbf41a152008-10-04 02:00:59 -04002426
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002427 return event;
Steven Rostedtfa743952009-06-16 12:37:57 -04002428
2429 out_fail:
2430 rb_end_commit(cpu_buffer);
2431 return NULL;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002432}
2433
Paul Mundt1155de42009-06-25 14:30:12 +09002434#ifdef CONFIG_TRACING
2435
Steven Rostedt567cd4d2012-11-02 18:33:05 -04002436/*
2437 * The lock and unlock are done within a preempt disable section.
2438 * The current_context per_cpu variable can only be modified
2439 * by the current task between lock and unlock. But it can
2440 * be modified more than once via an interrupt. To pass this
2441 * information from the lock to the unlock without having to
2442 * access the 'in_interrupt()' functions again (which do show
2443 * a bit of overhead in something as critical as function tracing,
2444 * we use a bitmask trick.
2445 *
2446 * bit 0 = NMI context
2447 * bit 1 = IRQ context
2448 * bit 2 = SoftIRQ context
2449 * bit 3 = normal context.
2450 *
2451 * This works because this is the order of contexts that can
2452 * preempt other contexts. A SoftIRQ never preempts an IRQ
2453 * context.
2454 *
2455 * When the context is determined, the corresponding bit is
2456 * checked and set (if it was set, then a recursion of that context
2457 * happened).
2458 *
2459 * On unlock, we need to clear this bit. To do so, just subtract
2460 * 1 from the current_context and AND it to itself.
2461 *
2462 * (binary)
2463 * 101 - 1 = 100
2464 * 101 & 100 = 100 (clearing bit zero)
2465 *
2466 * 1010 - 1 = 1001
2467 * 1010 & 1001 = 1000 (clearing bit 1)
2468 *
2469 * The least significant bit can be cleared this way, and it
2470 * just so happens that it is the same bit corresponding to
2471 * the current context.
2472 */
2473static DEFINE_PER_CPU(unsigned int, current_context);
Steven Rostedt261842b2009-04-16 21:41:52 -04002474
Steven Rostedt567cd4d2012-11-02 18:33:05 -04002475static __always_inline int trace_recursive_lock(void)
Steven Rostedt261842b2009-04-16 21:41:52 -04002476{
Steven Rostedt567cd4d2012-11-02 18:33:05 -04002477 unsigned int val = this_cpu_read(current_context);
2478 int bit;
Frederic Weisbeckere057a5e2009-04-19 23:38:12 +02002479
Steven Rostedt567cd4d2012-11-02 18:33:05 -04002480 if (in_interrupt()) {
2481 if (in_nmi())
2482 bit = 0;
2483 else if (in_irq())
2484 bit = 1;
2485 else
2486 bit = 2;
2487 } else
2488 bit = 3;
Frederic Weisbeckere057a5e2009-04-19 23:38:12 +02002489
Steven Rostedt567cd4d2012-11-02 18:33:05 -04002490 if (unlikely(val & (1 << bit)))
2491 return 1;
2492
2493 val |= (1 << bit);
2494 this_cpu_write(current_context, val);
2495
2496 return 0;
Steven Rostedtd9abde22010-10-19 13:17:08 -04002497}
2498
Steven Rostedt567cd4d2012-11-02 18:33:05 -04002499static __always_inline void trace_recursive_unlock(void)
Steven Rostedtd9abde22010-10-19 13:17:08 -04002500{
Steven Rostedt567cd4d2012-11-02 18:33:05 -04002501 unsigned int val = this_cpu_read(current_context);
Steven Rostedtd9abde22010-10-19 13:17:08 -04002502
Steven Rostedt567cd4d2012-11-02 18:33:05 -04002503 val--;
2504 val &= this_cpu_read(current_context);
2505 this_cpu_write(current_context, val);
Steven Rostedt261842b2009-04-16 21:41:52 -04002506}
2507
Paul Mundt1155de42009-06-25 14:30:12 +09002508#else
2509
2510#define trace_recursive_lock() (0)
2511#define trace_recursive_unlock() do { } while (0)
2512
2513#endif
2514
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002515/**
2516 * ring_buffer_lock_reserve - reserve a part of the buffer
2517 * @buffer: the ring buffer to reserve from
2518 * @length: the length of the data to reserve (excluding event header)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002519 *
2520 * Returns a reseverd event on the ring buffer to copy directly to.
2521 * The user of this interface will need to get the body to write into
2522 * and can use the ring_buffer_event_data() interface.
2523 *
2524 * The length is the length of the data needed, not the event length
2525 * which also includes the event header.
2526 *
2527 * Must be paired with ring_buffer_unlock_commit, unless NULL is returned.
2528 * If NULL is returned, then nothing has been allocated or locked.
2529 */
2530struct ring_buffer_event *
Arnaldo Carvalho de Melo0a987752009-02-05 16:12:56 -02002531ring_buffer_lock_reserve(struct ring_buffer *buffer, unsigned long length)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002532{
2533 struct ring_buffer_per_cpu *cpu_buffer;
2534 struct ring_buffer_event *event;
Steven Rostedt5168ae52010-06-03 09:36:50 -04002535 int cpu;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002536
Steven Rostedt033601a2008-11-21 12:41:55 -05002537 if (ring_buffer_flags != RB_BUFFERS_ON)
Steven Rostedta3583242008-11-11 15:01:42 -05002538 return NULL;
2539
Steven Rostedtbf41a152008-10-04 02:00:59 -04002540 /* If we are tracing schedule, we don't want to recurse */
Steven Rostedt5168ae52010-06-03 09:36:50 -04002541 preempt_disable_notrace();
Steven Rostedtbf41a152008-10-04 02:00:59 -04002542
Lai Jiangshan52fbe9c2010-03-08 14:50:43 +08002543 if (atomic_read(&buffer->record_disabled))
2544 goto out_nocheck;
2545
Steven Rostedt261842b2009-04-16 21:41:52 -04002546 if (trace_recursive_lock())
2547 goto out_nocheck;
2548
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002549 cpu = raw_smp_processor_id();
2550
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302551 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedtd7690412008-10-01 00:29:53 -04002552 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002553
2554 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002555
2556 if (atomic_read(&cpu_buffer->record_disabled))
Steven Rostedtd7690412008-10-01 00:29:53 -04002557 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002558
Steven Rostedtbe957c42009-05-11 14:42:53 -04002559 if (length > BUF_MAX_DATA_SIZE)
Steven Rostedtbf41a152008-10-04 02:00:59 -04002560 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002561
Steven Rostedt62f0b3e2009-09-04 14:11:34 -04002562 event = rb_reserve_next_event(buffer, cpu_buffer, length);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002563 if (!event)
Steven Rostedtd7690412008-10-01 00:29:53 -04002564 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002565
2566 return event;
2567
Steven Rostedtd7690412008-10-01 00:29:53 -04002568 out:
Steven Rostedt261842b2009-04-16 21:41:52 -04002569 trace_recursive_unlock();
2570
2571 out_nocheck:
Steven Rostedt5168ae52010-06-03 09:36:50 -04002572 preempt_enable_notrace();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002573 return NULL;
2574}
Robert Richterc4f50182008-12-11 16:49:22 +01002575EXPORT_SYMBOL_GPL(ring_buffer_lock_reserve);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002576
Steven Rostedta1863c22009-09-03 10:23:58 -04002577static void
2578rb_update_write_stamp(struct ring_buffer_per_cpu *cpu_buffer,
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002579 struct ring_buffer_event *event)
2580{
Steven Rostedt69d1b832010-10-07 18:18:05 -04002581 u64 delta;
2582
Steven Rostedtfa743952009-06-16 12:37:57 -04002583 /*
2584 * The event first in the commit queue updates the
2585 * time stamp.
2586 */
Steven Rostedt69d1b832010-10-07 18:18:05 -04002587 if (rb_event_is_commit(cpu_buffer, event)) {
2588 /*
2589 * A commit event that is first on a page
2590 * updates the write timestamp with the page stamp
2591 */
2592 if (!rb_event_index(event))
2593 cpu_buffer->write_stamp =
2594 cpu_buffer->commit_page->page->time_stamp;
2595 else if (event->type_len == RINGBUF_TYPE_TIME_EXTEND) {
2596 delta = event->array[0];
2597 delta <<= TS_SHIFT;
2598 delta += event->time_delta;
2599 cpu_buffer->write_stamp += delta;
2600 } else
2601 cpu_buffer->write_stamp += event->time_delta;
2602 }
Steven Rostedta1863c22009-09-03 10:23:58 -04002603}
Steven Rostedtbf41a152008-10-04 02:00:59 -04002604
Steven Rostedta1863c22009-09-03 10:23:58 -04002605static void rb_commit(struct ring_buffer_per_cpu *cpu_buffer,
2606 struct ring_buffer_event *event)
2607{
2608 local_inc(&cpu_buffer->entries);
2609 rb_update_write_stamp(cpu_buffer, event);
Steven Rostedtfa743952009-06-16 12:37:57 -04002610 rb_end_commit(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002611}
2612
2613/**
2614 * ring_buffer_unlock_commit - commit a reserved
2615 * @buffer: The buffer to commit to
2616 * @event: The event pointer to commit.
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002617 *
2618 * This commits the data to the ring buffer, and releases any locks held.
2619 *
2620 * Must be paired with ring_buffer_lock_reserve.
2621 */
2622int ring_buffer_unlock_commit(struct ring_buffer *buffer,
Arnaldo Carvalho de Melo0a987752009-02-05 16:12:56 -02002623 struct ring_buffer_event *event)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002624{
2625 struct ring_buffer_per_cpu *cpu_buffer;
2626 int cpu = raw_smp_processor_id();
2627
2628 cpu_buffer = buffer->buffers[cpu];
2629
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002630 rb_commit(cpu_buffer, event);
2631
Steven Rostedt261842b2009-04-16 21:41:52 -04002632 trace_recursive_unlock();
2633
Steven Rostedt5168ae52010-06-03 09:36:50 -04002634 preempt_enable_notrace();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002635
2636 return 0;
2637}
Robert Richterc4f50182008-12-11 16:49:22 +01002638EXPORT_SYMBOL_GPL(ring_buffer_unlock_commit);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002639
Frederic Weisbeckerf3b9aae2009-04-19 23:39:33 +02002640static inline void rb_event_discard(struct ring_buffer_event *event)
2641{
Steven Rostedt69d1b832010-10-07 18:18:05 -04002642 if (event->type_len == RINGBUF_TYPE_TIME_EXTEND)
2643 event = skip_time_extend(event);
2644
Lai Jiangshan334d4162009-04-24 11:27:05 +08002645 /* array[0] holds the actual length for the discarded event */
2646 event->array[0] = rb_event_data_length(event) - RB_EVNT_HDR_SIZE;
2647 event->type_len = RINGBUF_TYPE_PADDING;
Frederic Weisbeckerf3b9aae2009-04-19 23:39:33 +02002648 /* time delta must be non zero */
2649 if (!event->time_delta)
2650 event->time_delta = 1;
2651}
2652
Steven Rostedta1863c22009-09-03 10:23:58 -04002653/*
2654 * Decrement the entries to the page that an event is on.
2655 * The event does not even need to exist, only the pointer
2656 * to the page it is on. This may only be called before the commit
2657 * takes place.
2658 */
2659static inline void
2660rb_decrement_entry(struct ring_buffer_per_cpu *cpu_buffer,
2661 struct ring_buffer_event *event)
2662{
2663 unsigned long addr = (unsigned long)event;
2664 struct buffer_page *bpage = cpu_buffer->commit_page;
2665 struct buffer_page *start;
2666
2667 addr &= PAGE_MASK;
2668
2669 /* Do the likely case first */
2670 if (likely(bpage->page == (void *)addr)) {
2671 local_dec(&bpage->entries);
2672 return;
2673 }
2674
2675 /*
2676 * Because the commit page may be on the reader page we
2677 * start with the next page and check the end loop there.
2678 */
2679 rb_inc_page(cpu_buffer, &bpage);
2680 start = bpage;
2681 do {
2682 if (bpage->page == (void *)addr) {
2683 local_dec(&bpage->entries);
2684 return;
2685 }
2686 rb_inc_page(cpu_buffer, &bpage);
2687 } while (bpage != start);
2688
2689 /* commit not part of this buffer?? */
2690 RB_WARN_ON(cpu_buffer, 1);
2691}
2692
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002693/**
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002694 * ring_buffer_commit_discard - discard an event that has not been committed
2695 * @buffer: the ring buffer
2696 * @event: non committed event to discard
2697 *
Steven Rostedtdc892f72009-09-03 15:33:41 -04002698 * Sometimes an event that is in the ring buffer needs to be ignored.
2699 * This function lets the user discard an event in the ring buffer
2700 * and then that event will not be read later.
2701 *
2702 * This function only works if it is called before the the item has been
2703 * committed. It will try to free the event from the ring buffer
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002704 * if another event has not been added behind it.
2705 *
2706 * If another event has been added behind it, it will set the event
2707 * up as discarded, and perform the commit.
2708 *
2709 * If this function is called, do not call ring_buffer_unlock_commit on
2710 * the event.
2711 */
2712void ring_buffer_discard_commit(struct ring_buffer *buffer,
2713 struct ring_buffer_event *event)
2714{
2715 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002716 int cpu;
2717
2718 /* The event is discarded regardless */
Frederic Weisbeckerf3b9aae2009-04-19 23:39:33 +02002719 rb_event_discard(event);
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002720
Steven Rostedtfa743952009-06-16 12:37:57 -04002721 cpu = smp_processor_id();
2722 cpu_buffer = buffer->buffers[cpu];
2723
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002724 /*
2725 * This must only be called if the event has not been
2726 * committed yet. Thus we can assume that preemption
2727 * is still disabled.
2728 */
Steven Rostedtfa743952009-06-16 12:37:57 -04002729 RB_WARN_ON(buffer, !local_read(&cpu_buffer->committing));
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002730
Steven Rostedta1863c22009-09-03 10:23:58 -04002731 rb_decrement_entry(cpu_buffer, event);
Steven Rostedt0f2541d2009-08-05 12:02:48 -04002732 if (rb_try_to_discard(cpu_buffer, event))
Steven Rostedtedd813bf2009-06-02 23:00:53 -04002733 goto out;
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002734
2735 /*
2736 * The commit is still visible by the reader, so we
Steven Rostedta1863c22009-09-03 10:23:58 -04002737 * must still update the timestamp.
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002738 */
Steven Rostedta1863c22009-09-03 10:23:58 -04002739 rb_update_write_stamp(cpu_buffer, event);
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002740 out:
Steven Rostedtfa743952009-06-16 12:37:57 -04002741 rb_end_commit(cpu_buffer);
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002742
Frederic Weisbeckerf3b9aae2009-04-19 23:39:33 +02002743 trace_recursive_unlock();
2744
Steven Rostedt5168ae52010-06-03 09:36:50 -04002745 preempt_enable_notrace();
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002746
2747}
2748EXPORT_SYMBOL_GPL(ring_buffer_discard_commit);
2749
2750/**
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002751 * ring_buffer_write - write data to the buffer without reserving
2752 * @buffer: The ring buffer to write to.
2753 * @length: The length of the data being written (excluding the event header)
2754 * @data: The data to write to the buffer.
2755 *
2756 * This is like ring_buffer_lock_reserve and ring_buffer_unlock_commit as
2757 * one function. If you already have the data to write to the buffer, it
2758 * may be easier to simply call this function.
2759 *
2760 * Note, like ring_buffer_lock_reserve, the length is the length of the data
2761 * and not the length of the event which would hold the header.
2762 */
2763int ring_buffer_write(struct ring_buffer *buffer,
David Sharp01e3e712012-06-07 16:46:24 -07002764 unsigned long length,
2765 void *data)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002766{
2767 struct ring_buffer_per_cpu *cpu_buffer;
2768 struct ring_buffer_event *event;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002769 void *body;
2770 int ret = -EBUSY;
Steven Rostedt5168ae52010-06-03 09:36:50 -04002771 int cpu;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002772
Steven Rostedt033601a2008-11-21 12:41:55 -05002773 if (ring_buffer_flags != RB_BUFFERS_ON)
Steven Rostedta3583242008-11-11 15:01:42 -05002774 return -EBUSY;
2775
Steven Rostedt5168ae52010-06-03 09:36:50 -04002776 preempt_disable_notrace();
Steven Rostedtbf41a152008-10-04 02:00:59 -04002777
Lai Jiangshan52fbe9c2010-03-08 14:50:43 +08002778 if (atomic_read(&buffer->record_disabled))
2779 goto out;
2780
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002781 cpu = raw_smp_processor_id();
2782
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302783 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedtd7690412008-10-01 00:29:53 -04002784 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002785
2786 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002787
2788 if (atomic_read(&cpu_buffer->record_disabled))
2789 goto out;
2790
Steven Rostedtbe957c42009-05-11 14:42:53 -04002791 if (length > BUF_MAX_DATA_SIZE)
2792 goto out;
2793
Steven Rostedt62f0b3e2009-09-04 14:11:34 -04002794 event = rb_reserve_next_event(buffer, cpu_buffer, length);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002795 if (!event)
2796 goto out;
2797
2798 body = rb_event_data(event);
2799
2800 memcpy(body, data, length);
2801
2802 rb_commit(cpu_buffer, event);
2803
2804 ret = 0;
2805 out:
Steven Rostedt5168ae52010-06-03 09:36:50 -04002806 preempt_enable_notrace();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002807
2808 return ret;
2809}
Robert Richterc4f50182008-12-11 16:49:22 +01002810EXPORT_SYMBOL_GPL(ring_buffer_write);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002811
Andrew Morton34a148b2009-01-09 12:27:09 -08002812static int rb_per_cpu_empty(struct ring_buffer_per_cpu *cpu_buffer)
Steven Rostedtbf41a152008-10-04 02:00:59 -04002813{
2814 struct buffer_page *reader = cpu_buffer->reader_page;
Steven Rostedt77ae3652009-03-27 11:00:29 -04002815 struct buffer_page *head = rb_set_head_page(cpu_buffer);
Steven Rostedtbf41a152008-10-04 02:00:59 -04002816 struct buffer_page *commit = cpu_buffer->commit_page;
2817
Steven Rostedt77ae3652009-03-27 11:00:29 -04002818 /* In case of error, head will be NULL */
2819 if (unlikely(!head))
2820 return 1;
2821
Steven Rostedtbf41a152008-10-04 02:00:59 -04002822 return reader->read == rb_page_commit(reader) &&
2823 (commit == reader ||
2824 (commit == head &&
2825 head->read == rb_page_commit(commit)));
2826}
2827
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002828/**
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002829 * ring_buffer_record_disable - stop all writes into the buffer
2830 * @buffer: The ring buffer to stop writes to.
2831 *
2832 * This prevents all writes to the buffer. Any attempt to write
2833 * to the buffer after this will fail and return NULL.
2834 *
2835 * The caller should call synchronize_sched() after this.
2836 */
2837void ring_buffer_record_disable(struct ring_buffer *buffer)
2838{
2839 atomic_inc(&buffer->record_disabled);
2840}
Robert Richterc4f50182008-12-11 16:49:22 +01002841EXPORT_SYMBOL_GPL(ring_buffer_record_disable);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002842
2843/**
2844 * ring_buffer_record_enable - enable writes to the buffer
2845 * @buffer: The ring buffer to enable writes
2846 *
2847 * Note, multiple disables will need the same number of enables
Adam Buchbinderc41b20e2009-12-11 16:35:39 -05002848 * to truly enable the writing (much like preempt_disable).
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002849 */
2850void ring_buffer_record_enable(struct ring_buffer *buffer)
2851{
2852 atomic_dec(&buffer->record_disabled);
2853}
Robert Richterc4f50182008-12-11 16:49:22 +01002854EXPORT_SYMBOL_GPL(ring_buffer_record_enable);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002855
2856/**
Steven Rostedt499e5472012-02-22 15:50:28 -05002857 * ring_buffer_record_off - stop all writes into the buffer
2858 * @buffer: The ring buffer to stop writes to.
2859 *
2860 * This prevents all writes to the buffer. Any attempt to write
2861 * to the buffer after this will fail and return NULL.
2862 *
2863 * This is different than ring_buffer_record_disable() as
Wang Tianhong87abb3b2012-08-02 14:02:00 +08002864 * it works like an on/off switch, where as the disable() version
Steven Rostedt499e5472012-02-22 15:50:28 -05002865 * must be paired with a enable().
2866 */
2867void ring_buffer_record_off(struct ring_buffer *buffer)
2868{
2869 unsigned int rd;
2870 unsigned int new_rd;
2871
2872 do {
2873 rd = atomic_read(&buffer->record_disabled);
2874 new_rd = rd | RB_BUFFER_OFF;
2875 } while (atomic_cmpxchg(&buffer->record_disabled, rd, new_rd) != rd);
2876}
2877EXPORT_SYMBOL_GPL(ring_buffer_record_off);
2878
2879/**
2880 * ring_buffer_record_on - restart writes into the buffer
2881 * @buffer: The ring buffer to start writes to.
2882 *
2883 * This enables all writes to the buffer that was disabled by
2884 * ring_buffer_record_off().
2885 *
2886 * This is different than ring_buffer_record_enable() as
Wang Tianhong87abb3b2012-08-02 14:02:00 +08002887 * it works like an on/off switch, where as the enable() version
Steven Rostedt499e5472012-02-22 15:50:28 -05002888 * must be paired with a disable().
2889 */
2890void ring_buffer_record_on(struct ring_buffer *buffer)
2891{
2892 unsigned int rd;
2893 unsigned int new_rd;
2894
2895 do {
2896 rd = atomic_read(&buffer->record_disabled);
2897 new_rd = rd & ~RB_BUFFER_OFF;
2898 } while (atomic_cmpxchg(&buffer->record_disabled, rd, new_rd) != rd);
2899}
2900EXPORT_SYMBOL_GPL(ring_buffer_record_on);
2901
2902/**
2903 * ring_buffer_record_is_on - return true if the ring buffer can write
2904 * @buffer: The ring buffer to see if write is enabled
2905 *
2906 * Returns true if the ring buffer is in a state that it accepts writes.
2907 */
2908int ring_buffer_record_is_on(struct ring_buffer *buffer)
2909{
2910 return !atomic_read(&buffer->record_disabled);
2911}
2912
2913/**
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002914 * ring_buffer_record_disable_cpu - stop all writes into the cpu_buffer
2915 * @buffer: The ring buffer to stop writes to.
2916 * @cpu: The CPU buffer to stop
2917 *
2918 * This prevents all writes to the buffer. Any attempt to write
2919 * to the buffer after this will fail and return NULL.
2920 *
2921 * The caller should call synchronize_sched() after this.
2922 */
2923void ring_buffer_record_disable_cpu(struct ring_buffer *buffer, int cpu)
2924{
2925 struct ring_buffer_per_cpu *cpu_buffer;
2926
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302927 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04002928 return;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002929
2930 cpu_buffer = buffer->buffers[cpu];
2931 atomic_inc(&cpu_buffer->record_disabled);
2932}
Robert Richterc4f50182008-12-11 16:49:22 +01002933EXPORT_SYMBOL_GPL(ring_buffer_record_disable_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002934
2935/**
2936 * ring_buffer_record_enable_cpu - enable writes to the buffer
2937 * @buffer: The ring buffer to enable writes
2938 * @cpu: The CPU to enable.
2939 *
2940 * Note, multiple disables will need the same number of enables
Adam Buchbinderc41b20e2009-12-11 16:35:39 -05002941 * to truly enable the writing (much like preempt_disable).
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002942 */
2943void ring_buffer_record_enable_cpu(struct ring_buffer *buffer, int cpu)
2944{
2945 struct ring_buffer_per_cpu *cpu_buffer;
2946
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302947 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04002948 return;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002949
2950 cpu_buffer = buffer->buffers[cpu];
2951 atomic_dec(&cpu_buffer->record_disabled);
2952}
Robert Richterc4f50182008-12-11 16:49:22 +01002953EXPORT_SYMBOL_GPL(ring_buffer_record_enable_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002954
Steven Rostedtf6195aa2010-09-01 12:23:12 -04002955/*
2956 * The total entries in the ring buffer is the running counter
2957 * of entries entered into the ring buffer, minus the sum of
2958 * the entries read from the ring buffer and the number of
2959 * entries that were overwritten.
2960 */
2961static inline unsigned long
2962rb_num_of_entries(struct ring_buffer_per_cpu *cpu_buffer)
2963{
2964 return local_read(&cpu_buffer->entries) -
2965 (local_read(&cpu_buffer->overrun) + cpu_buffer->read);
2966}
2967
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002968/**
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -07002969 * ring_buffer_oldest_event_ts - get the oldest event timestamp from the buffer
2970 * @buffer: The ring buffer
2971 * @cpu: The per CPU buffer to read from.
2972 */
Yoshihiro YUNOMAE50ecf2c2012-10-11 16:27:54 -07002973u64 ring_buffer_oldest_event_ts(struct ring_buffer *buffer, int cpu)
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -07002974{
2975 unsigned long flags;
2976 struct ring_buffer_per_cpu *cpu_buffer;
2977 struct buffer_page *bpage;
Linus Torvaldsda830e52012-12-11 18:18:58 -08002978 u64 ret = 0;
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -07002979
2980 if (!cpumask_test_cpu(cpu, buffer->cpumask))
2981 return 0;
2982
2983 cpu_buffer = buffer->buffers[cpu];
Linus Torvalds7115e3f2011-10-26 17:03:38 +02002984 raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -07002985 /*
2986 * if the tail is on reader_page, oldest time stamp is on the reader
2987 * page
2988 */
2989 if (cpu_buffer->tail_page == cpu_buffer->reader_page)
2990 bpage = cpu_buffer->reader_page;
2991 else
2992 bpage = rb_set_head_page(cpu_buffer);
Steven Rostedt54f7be52012-11-29 22:27:22 -05002993 if (bpage)
2994 ret = bpage->page->time_stamp;
Linus Torvalds7115e3f2011-10-26 17:03:38 +02002995 raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -07002996
2997 return ret;
2998}
2999EXPORT_SYMBOL_GPL(ring_buffer_oldest_event_ts);
3000
3001/**
3002 * ring_buffer_bytes_cpu - get the number of bytes consumed in a cpu buffer
3003 * @buffer: The ring buffer
3004 * @cpu: The per CPU buffer to read from.
3005 */
3006unsigned long ring_buffer_bytes_cpu(struct ring_buffer *buffer, int cpu)
3007{
3008 struct ring_buffer_per_cpu *cpu_buffer;
3009 unsigned long ret;
3010
3011 if (!cpumask_test_cpu(cpu, buffer->cpumask))
3012 return 0;
3013
3014 cpu_buffer = buffer->buffers[cpu];
3015 ret = local_read(&cpu_buffer->entries_bytes) - cpu_buffer->read_bytes;
3016
3017 return ret;
3018}
3019EXPORT_SYMBOL_GPL(ring_buffer_bytes_cpu);
3020
3021/**
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003022 * ring_buffer_entries_cpu - get the number of entries in a cpu buffer
3023 * @buffer: The ring buffer
3024 * @cpu: The per CPU buffer to get the entries from.
3025 */
3026unsigned long ring_buffer_entries_cpu(struct ring_buffer *buffer, int cpu)
3027{
3028 struct ring_buffer_per_cpu *cpu_buffer;
3029
Rusty Russell9e01c1b2009-01-01 10:12:22 +10303030 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04003031 return 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003032
3033 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt554f7862009-03-11 22:00:13 -04003034
Steven Rostedtf6195aa2010-09-01 12:23:12 -04003035 return rb_num_of_entries(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003036}
Robert Richterc4f50182008-12-11 16:49:22 +01003037EXPORT_SYMBOL_GPL(ring_buffer_entries_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003038
3039/**
Slava Pestov884bfe82011-07-15 14:23:58 -07003040 * ring_buffer_overrun_cpu - get the number of overruns caused by the ring
3041 * buffer wrapping around (only if RB_FL_OVERWRITE is on).
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003042 * @buffer: The ring buffer
3043 * @cpu: The per CPU buffer to get the number of overruns from
3044 */
3045unsigned long ring_buffer_overrun_cpu(struct ring_buffer *buffer, int cpu)
3046{
3047 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedt8aabee52009-03-12 13:13:49 -04003048 unsigned long ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003049
Rusty Russell9e01c1b2009-01-01 10:12:22 +10303050 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04003051 return 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003052
3053 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt77ae3652009-03-27 11:00:29 -04003054 ret = local_read(&cpu_buffer->overrun);
Steven Rostedt554f7862009-03-11 22:00:13 -04003055
3056 return ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003057}
Robert Richterc4f50182008-12-11 16:49:22 +01003058EXPORT_SYMBOL_GPL(ring_buffer_overrun_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003059
3060/**
Slava Pestov884bfe82011-07-15 14:23:58 -07003061 * ring_buffer_commit_overrun_cpu - get the number of overruns caused by
3062 * commits failing due to the buffer wrapping around while there are uncommitted
3063 * events, such as during an interrupt storm.
Steven Rostedtf0d2c682009-04-29 13:43:37 -04003064 * @buffer: The ring buffer
3065 * @cpu: The per CPU buffer to get the number of overruns from
3066 */
3067unsigned long
3068ring_buffer_commit_overrun_cpu(struct ring_buffer *buffer, int cpu)
3069{
3070 struct ring_buffer_per_cpu *cpu_buffer;
3071 unsigned long ret;
3072
3073 if (!cpumask_test_cpu(cpu, buffer->cpumask))
3074 return 0;
3075
3076 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt77ae3652009-03-27 11:00:29 -04003077 ret = local_read(&cpu_buffer->commit_overrun);
Steven Rostedtf0d2c682009-04-29 13:43:37 -04003078
3079 return ret;
3080}
3081EXPORT_SYMBOL_GPL(ring_buffer_commit_overrun_cpu);
3082
3083/**
Slava Pestov884bfe82011-07-15 14:23:58 -07003084 * ring_buffer_dropped_events_cpu - get the number of dropped events caused by
3085 * the ring buffer filling up (only if RB_FL_OVERWRITE is off).
3086 * @buffer: The ring buffer
3087 * @cpu: The per CPU buffer to get the number of overruns from
3088 */
3089unsigned long
3090ring_buffer_dropped_events_cpu(struct ring_buffer *buffer, int cpu)
3091{
3092 struct ring_buffer_per_cpu *cpu_buffer;
3093 unsigned long ret;
3094
3095 if (!cpumask_test_cpu(cpu, buffer->cpumask))
3096 return 0;
3097
3098 cpu_buffer = buffer->buffers[cpu];
3099 ret = local_read(&cpu_buffer->dropped_events);
3100
3101 return ret;
3102}
3103EXPORT_SYMBOL_GPL(ring_buffer_dropped_events_cpu);
3104
3105/**
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003106 * ring_buffer_entries - get the number of entries in a buffer
3107 * @buffer: The ring buffer
3108 *
3109 * Returns the total number of entries in the ring buffer
3110 * (all CPU entries)
3111 */
3112unsigned long ring_buffer_entries(struct ring_buffer *buffer)
3113{
3114 struct ring_buffer_per_cpu *cpu_buffer;
3115 unsigned long entries = 0;
3116 int cpu;
3117
3118 /* if you care about this being correct, lock the buffer */
3119 for_each_buffer_cpu(buffer, cpu) {
3120 cpu_buffer = buffer->buffers[cpu];
Steven Rostedtf6195aa2010-09-01 12:23:12 -04003121 entries += rb_num_of_entries(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003122 }
3123
3124 return entries;
3125}
Robert Richterc4f50182008-12-11 16:49:22 +01003126EXPORT_SYMBOL_GPL(ring_buffer_entries);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003127
3128/**
Jiri Olsa67b394f2009-10-23 19:36:18 -04003129 * ring_buffer_overruns - get the number of overruns in buffer
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003130 * @buffer: The ring buffer
3131 *
3132 * Returns the total number of overruns in the ring buffer
3133 * (all CPU entries)
3134 */
3135unsigned long ring_buffer_overruns(struct ring_buffer *buffer)
3136{
3137 struct ring_buffer_per_cpu *cpu_buffer;
3138 unsigned long overruns = 0;
3139 int cpu;
3140
3141 /* if you care about this being correct, lock the buffer */
3142 for_each_buffer_cpu(buffer, cpu) {
3143 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt77ae3652009-03-27 11:00:29 -04003144 overruns += local_read(&cpu_buffer->overrun);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003145 }
3146
3147 return overruns;
3148}
Robert Richterc4f50182008-12-11 16:49:22 +01003149EXPORT_SYMBOL_GPL(ring_buffer_overruns);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003150
Steven Rostedt642edba2008-11-12 00:01:26 -05003151static void rb_iter_reset(struct ring_buffer_iter *iter)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003152{
3153 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
3154
Steven Rostedtd7690412008-10-01 00:29:53 -04003155 /* Iterator usage is expected to have record disabled */
3156 if (list_empty(&cpu_buffer->reader_page->list)) {
Steven Rostedt77ae3652009-03-27 11:00:29 -04003157 iter->head_page = rb_set_head_page(cpu_buffer);
3158 if (unlikely(!iter->head_page))
3159 return;
3160 iter->head = iter->head_page->read;
Steven Rostedtd7690412008-10-01 00:29:53 -04003161 } else {
3162 iter->head_page = cpu_buffer->reader_page;
Steven Rostedt6f807ac2008-10-04 02:00:58 -04003163 iter->head = cpu_buffer->reader_page->read;
Steven Rostedtd7690412008-10-01 00:29:53 -04003164 }
3165 if (iter->head)
3166 iter->read_stamp = cpu_buffer->read_stamp;
3167 else
Steven Rostedtabc9b562008-12-02 15:34:06 -05003168 iter->read_stamp = iter->head_page->page->time_stamp;
Steven Rostedt492a74f2010-01-25 15:17:47 -05003169 iter->cache_reader_page = cpu_buffer->reader_page;
3170 iter->cache_read = cpu_buffer->read;
Steven Rostedt642edba2008-11-12 00:01:26 -05003171}
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003172
Steven Rostedt642edba2008-11-12 00:01:26 -05003173/**
3174 * ring_buffer_iter_reset - reset an iterator
3175 * @iter: The iterator to reset
3176 *
3177 * Resets the iterator, so that it will start from the beginning
3178 * again.
3179 */
3180void ring_buffer_iter_reset(struct ring_buffer_iter *iter)
3181{
Steven Rostedt554f7862009-03-11 22:00:13 -04003182 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedt642edba2008-11-12 00:01:26 -05003183 unsigned long flags;
3184
Steven Rostedt554f7862009-03-11 22:00:13 -04003185 if (!iter)
3186 return;
3187
3188 cpu_buffer = iter->cpu_buffer;
3189
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02003190 raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
Steven Rostedt642edba2008-11-12 00:01:26 -05003191 rb_iter_reset(iter);
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02003192 raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003193}
Robert Richterc4f50182008-12-11 16:49:22 +01003194EXPORT_SYMBOL_GPL(ring_buffer_iter_reset);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003195
3196/**
3197 * ring_buffer_iter_empty - check if an iterator has no more to read
3198 * @iter: The iterator to check
3199 */
3200int ring_buffer_iter_empty(struct ring_buffer_iter *iter)
3201{
3202 struct ring_buffer_per_cpu *cpu_buffer;
3203
3204 cpu_buffer = iter->cpu_buffer;
3205
Steven Rostedtbf41a152008-10-04 02:00:59 -04003206 return iter->head_page == cpu_buffer->commit_page &&
3207 iter->head == rb_commit_index(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003208}
Robert Richterc4f50182008-12-11 16:49:22 +01003209EXPORT_SYMBOL_GPL(ring_buffer_iter_empty);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003210
3211static void
3212rb_update_read_stamp(struct ring_buffer_per_cpu *cpu_buffer,
3213 struct ring_buffer_event *event)
3214{
3215 u64 delta;
3216
Lai Jiangshan334d4162009-04-24 11:27:05 +08003217 switch (event->type_len) {
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003218 case RINGBUF_TYPE_PADDING:
3219 return;
3220
3221 case RINGBUF_TYPE_TIME_EXTEND:
3222 delta = event->array[0];
3223 delta <<= TS_SHIFT;
3224 delta += event->time_delta;
3225 cpu_buffer->read_stamp += delta;
3226 return;
3227
3228 case RINGBUF_TYPE_TIME_STAMP:
3229 /* FIXME: not implemented */
3230 return;
3231
3232 case RINGBUF_TYPE_DATA:
3233 cpu_buffer->read_stamp += event->time_delta;
3234 return;
3235
3236 default:
3237 BUG();
3238 }
3239 return;
3240}
3241
3242static void
3243rb_update_iter_read_stamp(struct ring_buffer_iter *iter,
3244 struct ring_buffer_event *event)
3245{
3246 u64 delta;
3247
Lai Jiangshan334d4162009-04-24 11:27:05 +08003248 switch (event->type_len) {
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003249 case RINGBUF_TYPE_PADDING:
3250 return;
3251
3252 case RINGBUF_TYPE_TIME_EXTEND:
3253 delta = event->array[0];
3254 delta <<= TS_SHIFT;
3255 delta += event->time_delta;
3256 iter->read_stamp += delta;
3257 return;
3258
3259 case RINGBUF_TYPE_TIME_STAMP:
3260 /* FIXME: not implemented */
3261 return;
3262
3263 case RINGBUF_TYPE_DATA:
3264 iter->read_stamp += event->time_delta;
3265 return;
3266
3267 default:
3268 BUG();
3269 }
3270 return;
3271}
3272
Steven Rostedtd7690412008-10-01 00:29:53 -04003273static struct buffer_page *
3274rb_get_reader_page(struct ring_buffer_per_cpu *cpu_buffer)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003275{
Steven Rostedtd7690412008-10-01 00:29:53 -04003276 struct buffer_page *reader = NULL;
Steven Rostedt66a8cb92010-03-31 13:21:56 -04003277 unsigned long overwrite;
Steven Rostedtd7690412008-10-01 00:29:53 -04003278 unsigned long flags;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003279 int nr_loops = 0;
Steven Rostedt77ae3652009-03-27 11:00:29 -04003280 int ret;
Steven Rostedtd7690412008-10-01 00:29:53 -04003281
Steven Rostedt3e03fb72008-11-06 00:09:43 -05003282 local_irq_save(flags);
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01003283 arch_spin_lock(&cpu_buffer->lock);
Steven Rostedtd7690412008-10-01 00:29:53 -04003284
3285 again:
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003286 /*
3287 * This should normally only loop twice. But because the
3288 * start of the reader inserts an empty page, it causes
3289 * a case where we will loop three times. There should be no
3290 * reason to loop four times (that I know of).
3291 */
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05003292 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 3)) {
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003293 reader = NULL;
3294 goto out;
3295 }
3296
Steven Rostedtd7690412008-10-01 00:29:53 -04003297 reader = cpu_buffer->reader_page;
3298
3299 /* If there's more to read, return this page */
Steven Rostedtbf41a152008-10-04 02:00:59 -04003300 if (cpu_buffer->reader_page->read < rb_page_size(reader))
Steven Rostedtd7690412008-10-01 00:29:53 -04003301 goto out;
3302
3303 /* Never should we have an index greater than the size */
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05003304 if (RB_WARN_ON(cpu_buffer,
3305 cpu_buffer->reader_page->read > rb_page_size(reader)))
3306 goto out;
Steven Rostedtd7690412008-10-01 00:29:53 -04003307
3308 /* check if we caught up to the tail */
3309 reader = NULL;
Steven Rostedtbf41a152008-10-04 02:00:59 -04003310 if (cpu_buffer->commit_page == cpu_buffer->reader_page)
Steven Rostedtd7690412008-10-01 00:29:53 -04003311 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003312
Steven Rostedta5fb8332012-06-28 13:35:04 -04003313 /* Don't bother swapping if the ring buffer is empty */
3314 if (rb_num_of_entries(cpu_buffer) == 0)
3315 goto out;
3316
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003317 /*
Steven Rostedtd7690412008-10-01 00:29:53 -04003318 * Reset the reader page to size zero.
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003319 */
Steven Rostedt77ae3652009-03-27 11:00:29 -04003320 local_set(&cpu_buffer->reader_page->write, 0);
3321 local_set(&cpu_buffer->reader_page->entries, 0);
3322 local_set(&cpu_buffer->reader_page->page->commit, 0);
Steven Rostedtff0ff842010-03-31 22:11:42 -04003323 cpu_buffer->reader_page->real_end = 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003324
Steven Rostedt77ae3652009-03-27 11:00:29 -04003325 spin:
3326 /*
3327 * Splice the empty reader page into the list around the head.
3328 */
3329 reader = rb_set_head_page(cpu_buffer);
Steven Rostedt54f7be52012-11-29 22:27:22 -05003330 if (!reader)
3331 goto out;
Steven Rostedt0e1ff5d2010-01-06 20:40:44 -05003332 cpu_buffer->reader_page->list.next = rb_list_head(reader->list.next);
Steven Rostedtd7690412008-10-01 00:29:53 -04003333 cpu_buffer->reader_page->list.prev = reader->list.prev;
Steven Rostedtbf41a152008-10-04 02:00:59 -04003334
Steven Rostedt3adc54f2009-03-30 15:32:01 -04003335 /*
3336 * cpu_buffer->pages just needs to point to the buffer, it
3337 * has no specific buffer page to point to. Lets move it out
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003338 * of our way so we don't accidentally swap it.
Steven Rostedt3adc54f2009-03-30 15:32:01 -04003339 */
3340 cpu_buffer->pages = reader->list.prev;
3341
Steven Rostedt77ae3652009-03-27 11:00:29 -04003342 /* The reader page will be pointing to the new head */
3343 rb_set_list_to_head(cpu_buffer, &cpu_buffer->reader_page->list);
Steven Rostedtd7690412008-10-01 00:29:53 -04003344
3345 /*
Steven Rostedt66a8cb92010-03-31 13:21:56 -04003346 * We want to make sure we read the overruns after we set up our
3347 * pointers to the next object. The writer side does a
3348 * cmpxchg to cross pages which acts as the mb on the writer
3349 * side. Note, the reader will constantly fail the swap
3350 * while the writer is updating the pointers, so this
3351 * guarantees that the overwrite recorded here is the one we
3352 * want to compare with the last_overrun.
3353 */
3354 smp_mb();
3355 overwrite = local_read(&(cpu_buffer->overrun));
3356
3357 /*
Steven Rostedt77ae3652009-03-27 11:00:29 -04003358 * Here's the tricky part.
3359 *
3360 * We need to move the pointer past the header page.
3361 * But we can only do that if a writer is not currently
3362 * moving it. The page before the header page has the
3363 * flag bit '1' set if it is pointing to the page we want.
3364 * but if the writer is in the process of moving it
3365 * than it will be '2' or already moved '0'.
Steven Rostedtd7690412008-10-01 00:29:53 -04003366 */
Steven Rostedtd7690412008-10-01 00:29:53 -04003367
Steven Rostedt77ae3652009-03-27 11:00:29 -04003368 ret = rb_head_page_replace(reader, cpu_buffer->reader_page);
3369
3370 /*
3371 * If we did not convert it, then we must try again.
3372 */
3373 if (!ret)
3374 goto spin;
3375
3376 /*
3377 * Yeah! We succeeded in replacing the page.
3378 *
3379 * Now make the new head point back to the reader page.
3380 */
David Sharp5ded3dc62010-01-06 17:12:07 -08003381 rb_list_head(reader->list.next)->prev = &cpu_buffer->reader_page->list;
Steven Rostedt77ae3652009-03-27 11:00:29 -04003382 rb_inc_page(cpu_buffer, &cpu_buffer->head_page);
Steven Rostedtd7690412008-10-01 00:29:53 -04003383
3384 /* Finally update the reader page to the new head */
3385 cpu_buffer->reader_page = reader;
3386 rb_reset_reader_page(cpu_buffer);
3387
Steven Rostedt66a8cb92010-03-31 13:21:56 -04003388 if (overwrite != cpu_buffer->last_overrun) {
3389 cpu_buffer->lost_events = overwrite - cpu_buffer->last_overrun;
3390 cpu_buffer->last_overrun = overwrite;
3391 }
3392
Steven Rostedtd7690412008-10-01 00:29:53 -04003393 goto again;
3394
3395 out:
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01003396 arch_spin_unlock(&cpu_buffer->lock);
Steven Rostedt3e03fb72008-11-06 00:09:43 -05003397 local_irq_restore(flags);
Steven Rostedtd7690412008-10-01 00:29:53 -04003398
3399 return reader;
3400}
3401
3402static void rb_advance_reader(struct ring_buffer_per_cpu *cpu_buffer)
3403{
3404 struct ring_buffer_event *event;
3405 struct buffer_page *reader;
3406 unsigned length;
3407
3408 reader = rb_get_reader_page(cpu_buffer);
3409
3410 /* This function should not be called when buffer is empty */
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05003411 if (RB_WARN_ON(cpu_buffer, !reader))
3412 return;
Steven Rostedtd7690412008-10-01 00:29:53 -04003413
3414 event = rb_reader_event(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003415
Steven Rostedta1863c22009-09-03 10:23:58 -04003416 if (event->type_len <= RINGBUF_TYPE_DATA_TYPE_LEN_MAX)
Steven Rostedte4906ef2009-04-30 20:49:44 -04003417 cpu_buffer->read++;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003418
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003419 rb_update_read_stamp(cpu_buffer, event);
3420
Steven Rostedtd7690412008-10-01 00:29:53 -04003421 length = rb_event_length(event);
Steven Rostedt6f807ac2008-10-04 02:00:58 -04003422 cpu_buffer->reader_page->read += length;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003423}
3424
3425static void rb_advance_iter(struct ring_buffer_iter *iter)
3426{
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003427 struct ring_buffer_per_cpu *cpu_buffer;
3428 struct ring_buffer_event *event;
3429 unsigned length;
3430
3431 cpu_buffer = iter->cpu_buffer;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003432
3433 /*
3434 * Check if we are at the end of the buffer.
3435 */
Steven Rostedtbf41a152008-10-04 02:00:59 -04003436 if (iter->head >= rb_page_size(iter->head_page)) {
Steven Rostedtea05b572009-06-03 09:30:10 -04003437 /* discarded commits can make the page empty */
3438 if (iter->head_page == cpu_buffer->commit_page)
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05003439 return;
Steven Rostedtd7690412008-10-01 00:29:53 -04003440 rb_inc_iter(iter);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003441 return;
3442 }
3443
3444 event = rb_iter_head_event(iter);
3445
3446 length = rb_event_length(event);
3447
3448 /*
3449 * This should not be called to advance the header if we are
3450 * at the tail of the buffer.
3451 */
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05003452 if (RB_WARN_ON(cpu_buffer,
Steven Rostedtf536aaf2008-11-10 23:07:30 -05003453 (iter->head_page == cpu_buffer->commit_page) &&
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05003454 (iter->head + length > rb_commit_index(cpu_buffer))))
3455 return;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003456
3457 rb_update_iter_read_stamp(iter, event);
3458
3459 iter->head += length;
3460
3461 /* check for end of page padding */
Steven Rostedtbf41a152008-10-04 02:00:59 -04003462 if ((iter->head >= rb_page_size(iter->head_page)) &&
3463 (iter->head_page != cpu_buffer->commit_page))
Steven Rostedt771e0382012-11-30 10:41:57 -05003464 rb_inc_iter(iter);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003465}
3466
Steven Rostedt66a8cb92010-03-31 13:21:56 -04003467static int rb_lost_events(struct ring_buffer_per_cpu *cpu_buffer)
3468{
3469 return cpu_buffer->lost_events;
3470}
3471
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003472static struct ring_buffer_event *
Steven Rostedt66a8cb92010-03-31 13:21:56 -04003473rb_buffer_peek(struct ring_buffer_per_cpu *cpu_buffer, u64 *ts,
3474 unsigned long *lost_events)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003475{
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003476 struct ring_buffer_event *event;
Steven Rostedtd7690412008-10-01 00:29:53 -04003477 struct buffer_page *reader;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003478 int nr_loops = 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003479
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003480 again:
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003481 /*
Steven Rostedt69d1b832010-10-07 18:18:05 -04003482 * We repeat when a time extend is encountered.
3483 * Since the time extend is always attached to a data event,
3484 * we should never loop more than once.
3485 * (We never hit the following condition more than twice).
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003486 */
Steven Rostedt69d1b832010-10-07 18:18:05 -04003487 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 2))
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003488 return NULL;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003489
Steven Rostedtd7690412008-10-01 00:29:53 -04003490 reader = rb_get_reader_page(cpu_buffer);
3491 if (!reader)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003492 return NULL;
3493
Steven Rostedtd7690412008-10-01 00:29:53 -04003494 event = rb_reader_event(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003495
Lai Jiangshan334d4162009-04-24 11:27:05 +08003496 switch (event->type_len) {
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003497 case RINGBUF_TYPE_PADDING:
Tom Zanussi2d622712009-03-22 03:30:49 -05003498 if (rb_null_event(event))
3499 RB_WARN_ON(cpu_buffer, 1);
3500 /*
3501 * Because the writer could be discarding every
3502 * event it creates (which would probably be bad)
3503 * if we were to go back to "again" then we may never
3504 * catch up, and will trigger the warn on, or lock
3505 * the box. Return the padding, and we will release
3506 * the current locks, and try again.
3507 */
Tom Zanussi2d622712009-03-22 03:30:49 -05003508 return event;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003509
3510 case RINGBUF_TYPE_TIME_EXTEND:
3511 /* Internal data, OK to advance */
Steven Rostedtd7690412008-10-01 00:29:53 -04003512 rb_advance_reader(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003513 goto again;
3514
3515 case RINGBUF_TYPE_TIME_STAMP:
3516 /* FIXME: not implemented */
Steven Rostedtd7690412008-10-01 00:29:53 -04003517 rb_advance_reader(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003518 goto again;
3519
3520 case RINGBUF_TYPE_DATA:
3521 if (ts) {
3522 *ts = cpu_buffer->read_stamp + event->time_delta;
Robert Richterd8eeb2d2009-07-31 14:58:04 +02003523 ring_buffer_normalize_time_stamp(cpu_buffer->buffer,
Steven Rostedt37886f62009-03-17 17:22:06 -04003524 cpu_buffer->cpu, ts);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003525 }
Steven Rostedt66a8cb92010-03-31 13:21:56 -04003526 if (lost_events)
3527 *lost_events = rb_lost_events(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003528 return event;
3529
3530 default:
3531 BUG();
3532 }
3533
3534 return NULL;
3535}
Robert Richterc4f50182008-12-11 16:49:22 +01003536EXPORT_SYMBOL_GPL(ring_buffer_peek);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003537
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003538static struct ring_buffer_event *
3539rb_iter_peek(struct ring_buffer_iter *iter, u64 *ts)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003540{
3541 struct ring_buffer *buffer;
3542 struct ring_buffer_per_cpu *cpu_buffer;
3543 struct ring_buffer_event *event;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003544 int nr_loops = 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003545
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003546 cpu_buffer = iter->cpu_buffer;
3547 buffer = cpu_buffer->buffer;
3548
Steven Rostedt492a74f2010-01-25 15:17:47 -05003549 /*
3550 * Check if someone performed a consuming read to
3551 * the buffer. A consuming read invalidates the iterator
3552 * and we need to reset the iterator in this case.
3553 */
3554 if (unlikely(iter->cache_read != cpu_buffer->read ||
3555 iter->cache_reader_page != cpu_buffer->reader_page))
3556 rb_iter_reset(iter);
3557
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003558 again:
Steven Rostedt3c05d742010-01-26 16:14:08 -05003559 if (ring_buffer_iter_empty(iter))
3560 return NULL;
3561
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003562 /*
Steven Rostedt69d1b832010-10-07 18:18:05 -04003563 * We repeat when a time extend is encountered.
3564 * Since the time extend is always attached to a data event,
3565 * we should never loop more than once.
3566 * (We never hit the following condition more than twice).
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003567 */
Steven Rostedt69d1b832010-10-07 18:18:05 -04003568 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 2))
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003569 return NULL;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003570
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003571 if (rb_per_cpu_empty(cpu_buffer))
3572 return NULL;
3573
Steven Rostedt3c05d742010-01-26 16:14:08 -05003574 if (iter->head >= local_read(&iter->head_page->page->commit)) {
3575 rb_inc_iter(iter);
3576 goto again;
3577 }
3578
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003579 event = rb_iter_head_event(iter);
3580
Lai Jiangshan334d4162009-04-24 11:27:05 +08003581 switch (event->type_len) {
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003582 case RINGBUF_TYPE_PADDING:
Tom Zanussi2d622712009-03-22 03:30:49 -05003583 if (rb_null_event(event)) {
3584 rb_inc_iter(iter);
3585 goto again;
3586 }
3587 rb_advance_iter(iter);
3588 return event;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003589
3590 case RINGBUF_TYPE_TIME_EXTEND:
3591 /* Internal data, OK to advance */
3592 rb_advance_iter(iter);
3593 goto again;
3594
3595 case RINGBUF_TYPE_TIME_STAMP:
3596 /* FIXME: not implemented */
3597 rb_advance_iter(iter);
3598 goto again;
3599
3600 case RINGBUF_TYPE_DATA:
3601 if (ts) {
3602 *ts = iter->read_stamp + event->time_delta;
Steven Rostedt37886f62009-03-17 17:22:06 -04003603 ring_buffer_normalize_time_stamp(buffer,
3604 cpu_buffer->cpu, ts);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003605 }
3606 return event;
3607
3608 default:
3609 BUG();
3610 }
3611
3612 return NULL;
3613}
Robert Richterc4f50182008-12-11 16:49:22 +01003614EXPORT_SYMBOL_GPL(ring_buffer_iter_peek);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003615
Steven Rostedt8d707e82009-06-16 21:22:48 -04003616static inline int rb_ok_to_lock(void)
3617{
3618 /*
3619 * If an NMI die dumps out the content of the ring buffer
3620 * do not grab locks. We also permanently disable the ring
3621 * buffer too. A one time deal is all you get from reading
3622 * the ring buffer from an NMI.
3623 */
Steven Rostedt464e85e2009-08-05 15:26:37 -04003624 if (likely(!in_nmi()))
Steven Rostedt8d707e82009-06-16 21:22:48 -04003625 return 1;
3626
3627 tracing_off_permanent();
3628 return 0;
3629}
3630
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003631/**
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003632 * ring_buffer_peek - peek at the next event to be read
3633 * @buffer: The ring buffer to read
3634 * @cpu: The cpu to peak at
3635 * @ts: The timestamp counter of this event.
Steven Rostedt66a8cb92010-03-31 13:21:56 -04003636 * @lost_events: a variable to store if events were lost (may be NULL)
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003637 *
3638 * This will return the event that will be read next, but does
3639 * not consume the data.
3640 */
3641struct ring_buffer_event *
Steven Rostedt66a8cb92010-03-31 13:21:56 -04003642ring_buffer_peek(struct ring_buffer *buffer, int cpu, u64 *ts,
3643 unsigned long *lost_events)
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003644{
3645 struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
Steven Rostedt8aabee52009-03-12 13:13:49 -04003646 struct ring_buffer_event *event;
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003647 unsigned long flags;
Steven Rostedt8d707e82009-06-16 21:22:48 -04003648 int dolock;
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003649
Steven Rostedt554f7862009-03-11 22:00:13 -04003650 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04003651 return NULL;
Steven Rostedt554f7862009-03-11 22:00:13 -04003652
Steven Rostedt8d707e82009-06-16 21:22:48 -04003653 dolock = rb_ok_to_lock();
Tom Zanussi2d622712009-03-22 03:30:49 -05003654 again:
Steven Rostedt8d707e82009-06-16 21:22:48 -04003655 local_irq_save(flags);
3656 if (dolock)
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02003657 raw_spin_lock(&cpu_buffer->reader_lock);
Steven Rostedt66a8cb92010-03-31 13:21:56 -04003658 event = rb_buffer_peek(cpu_buffer, ts, lost_events);
Robert Richter469535a2009-07-30 19:19:18 +02003659 if (event && event->type_len == RINGBUF_TYPE_PADDING)
3660 rb_advance_reader(cpu_buffer);
Steven Rostedt8d707e82009-06-16 21:22:48 -04003661 if (dolock)
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02003662 raw_spin_unlock(&cpu_buffer->reader_lock);
Steven Rostedt8d707e82009-06-16 21:22:48 -04003663 local_irq_restore(flags);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003664
Steven Rostedt1b959e12009-09-03 10:12:13 -04003665 if (event && event->type_len == RINGBUF_TYPE_PADDING)
Tom Zanussi2d622712009-03-22 03:30:49 -05003666 goto again;
Tom Zanussi2d622712009-03-22 03:30:49 -05003667
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003668 return event;
3669}
3670
3671/**
3672 * ring_buffer_iter_peek - peek at the next event to be read
3673 * @iter: The ring buffer iterator
3674 * @ts: The timestamp counter of this event.
3675 *
3676 * This will return the event that will be read next, but does
3677 * not increment the iterator.
3678 */
3679struct ring_buffer_event *
3680ring_buffer_iter_peek(struct ring_buffer_iter *iter, u64 *ts)
3681{
3682 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
3683 struct ring_buffer_event *event;
3684 unsigned long flags;
3685
Tom Zanussi2d622712009-03-22 03:30:49 -05003686 again:
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02003687 raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003688 event = rb_iter_peek(iter, ts);
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02003689 raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003690
Steven Rostedt1b959e12009-09-03 10:12:13 -04003691 if (event && event->type_len == RINGBUF_TYPE_PADDING)
Tom Zanussi2d622712009-03-22 03:30:49 -05003692 goto again;
Tom Zanussi2d622712009-03-22 03:30:49 -05003693
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003694 return event;
3695}
3696
3697/**
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003698 * ring_buffer_consume - return an event and consume it
3699 * @buffer: The ring buffer to get the next event from
Steven Rostedt66a8cb92010-03-31 13:21:56 -04003700 * @cpu: the cpu to read the buffer from
3701 * @ts: a variable to store the timestamp (may be NULL)
3702 * @lost_events: a variable to store if events were lost (may be NULL)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003703 *
3704 * Returns the next event in the ring buffer, and that event is consumed.
3705 * Meaning, that sequential reads will keep returning a different event,
3706 * and eventually empty the ring buffer if the producer is slower.
3707 */
3708struct ring_buffer_event *
Steven Rostedt66a8cb92010-03-31 13:21:56 -04003709ring_buffer_consume(struct ring_buffer *buffer, int cpu, u64 *ts,
3710 unsigned long *lost_events)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003711{
Steven Rostedt554f7862009-03-11 22:00:13 -04003712 struct ring_buffer_per_cpu *cpu_buffer;
3713 struct ring_buffer_event *event = NULL;
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003714 unsigned long flags;
Steven Rostedt8d707e82009-06-16 21:22:48 -04003715 int dolock;
3716
3717 dolock = rb_ok_to_lock();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003718
Tom Zanussi2d622712009-03-22 03:30:49 -05003719 again:
Steven Rostedt554f7862009-03-11 22:00:13 -04003720 /* might be called in atomic */
3721 preempt_disable();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003722
Steven Rostedt554f7862009-03-11 22:00:13 -04003723 if (!cpumask_test_cpu(cpu, buffer->cpumask))
3724 goto out;
3725
3726 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt8d707e82009-06-16 21:22:48 -04003727 local_irq_save(flags);
3728 if (dolock)
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02003729 raw_spin_lock(&cpu_buffer->reader_lock);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003730
Steven Rostedt66a8cb92010-03-31 13:21:56 -04003731 event = rb_buffer_peek(cpu_buffer, ts, lost_events);
3732 if (event) {
3733 cpu_buffer->lost_events = 0;
Robert Richter469535a2009-07-30 19:19:18 +02003734 rb_advance_reader(cpu_buffer);
Steven Rostedt66a8cb92010-03-31 13:21:56 -04003735 }
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003736
Steven Rostedt8d707e82009-06-16 21:22:48 -04003737 if (dolock)
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02003738 raw_spin_unlock(&cpu_buffer->reader_lock);
Steven Rostedt8d707e82009-06-16 21:22:48 -04003739 local_irq_restore(flags);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003740
Steven Rostedt554f7862009-03-11 22:00:13 -04003741 out:
3742 preempt_enable();
3743
Steven Rostedt1b959e12009-09-03 10:12:13 -04003744 if (event && event->type_len == RINGBUF_TYPE_PADDING)
Tom Zanussi2d622712009-03-22 03:30:49 -05003745 goto again;
Tom Zanussi2d622712009-03-22 03:30:49 -05003746
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003747 return event;
3748}
Robert Richterc4f50182008-12-11 16:49:22 +01003749EXPORT_SYMBOL_GPL(ring_buffer_consume);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003750
3751/**
David Miller72c9ddf2010-04-20 15:47:11 -07003752 * ring_buffer_read_prepare - Prepare for a non consuming read of the buffer
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003753 * @buffer: The ring buffer to read from
3754 * @cpu: The cpu buffer to iterate over
3755 *
David Miller72c9ddf2010-04-20 15:47:11 -07003756 * This performs the initial preparations necessary to iterate
3757 * through the buffer. Memory is allocated, buffer recording
3758 * is disabled, and the iterator pointer is returned to the caller.
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003759 *
David Miller72c9ddf2010-04-20 15:47:11 -07003760 * Disabling buffer recordng prevents the reading from being
3761 * corrupted. This is not a consuming read, so a producer is not
3762 * expected.
3763 *
3764 * After a sequence of ring_buffer_read_prepare calls, the user is
3765 * expected to make at least one call to ring_buffer_prepare_sync.
3766 * Afterwards, ring_buffer_read_start is invoked to get things going
3767 * for real.
3768 *
3769 * This overall must be paired with ring_buffer_finish.
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003770 */
3771struct ring_buffer_iter *
David Miller72c9ddf2010-04-20 15:47:11 -07003772ring_buffer_read_prepare(struct ring_buffer *buffer, int cpu)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003773{
3774 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedt8aabee52009-03-12 13:13:49 -04003775 struct ring_buffer_iter *iter;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003776
Rusty Russell9e01c1b2009-01-01 10:12:22 +10303777 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04003778 return NULL;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003779
3780 iter = kmalloc(sizeof(*iter), GFP_KERNEL);
3781 if (!iter)
Steven Rostedt8aabee52009-03-12 13:13:49 -04003782 return NULL;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003783
3784 cpu_buffer = buffer->buffers[cpu];
3785
3786 iter->cpu_buffer = cpu_buffer;
3787
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07003788 atomic_inc(&buffer->resize_disabled);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003789 atomic_inc(&cpu_buffer->record_disabled);
David Miller72c9ddf2010-04-20 15:47:11 -07003790
3791 return iter;
3792}
3793EXPORT_SYMBOL_GPL(ring_buffer_read_prepare);
3794
3795/**
3796 * ring_buffer_read_prepare_sync - Synchronize a set of prepare calls
3797 *
3798 * All previously invoked ring_buffer_read_prepare calls to prepare
3799 * iterators will be synchronized. Afterwards, read_buffer_read_start
3800 * calls on those iterators are allowed.
3801 */
3802void
3803ring_buffer_read_prepare_sync(void)
3804{
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003805 synchronize_sched();
David Miller72c9ddf2010-04-20 15:47:11 -07003806}
3807EXPORT_SYMBOL_GPL(ring_buffer_read_prepare_sync);
3808
3809/**
3810 * ring_buffer_read_start - start a non consuming read of the buffer
3811 * @iter: The iterator returned by ring_buffer_read_prepare
3812 *
3813 * This finalizes the startup of an iteration through the buffer.
3814 * The iterator comes from a call to ring_buffer_read_prepare and
3815 * an intervening ring_buffer_read_prepare_sync must have been
3816 * performed.
3817 *
3818 * Must be paired with ring_buffer_finish.
3819 */
3820void
3821ring_buffer_read_start(struct ring_buffer_iter *iter)
3822{
3823 struct ring_buffer_per_cpu *cpu_buffer;
3824 unsigned long flags;
3825
3826 if (!iter)
3827 return;
3828
3829 cpu_buffer = iter->cpu_buffer;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003830
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02003831 raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01003832 arch_spin_lock(&cpu_buffer->lock);
Steven Rostedt642edba2008-11-12 00:01:26 -05003833 rb_iter_reset(iter);
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01003834 arch_spin_unlock(&cpu_buffer->lock);
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02003835 raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003836}
Robert Richterc4f50182008-12-11 16:49:22 +01003837EXPORT_SYMBOL_GPL(ring_buffer_read_start);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003838
3839/**
3840 * ring_buffer_finish - finish reading the iterator of the buffer
3841 * @iter: The iterator retrieved by ring_buffer_start
3842 *
3843 * This re-enables the recording to the buffer, and frees the
3844 * iterator.
3845 */
3846void
3847ring_buffer_read_finish(struct ring_buffer_iter *iter)
3848{
3849 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
Steven Rostedt9366c1b2012-11-29 22:31:16 -05003850 unsigned long flags;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003851
Steven Rostedt659f4512012-05-14 17:02:33 -04003852 /*
3853 * Ring buffer is disabled from recording, here's a good place
Steven Rostedt9366c1b2012-11-29 22:31:16 -05003854 * to check the integrity of the ring buffer.
3855 * Must prevent readers from trying to read, as the check
3856 * clears the HEAD page and readers require it.
Steven Rostedt659f4512012-05-14 17:02:33 -04003857 */
Steven Rostedt9366c1b2012-11-29 22:31:16 -05003858 raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
Steven Rostedt659f4512012-05-14 17:02:33 -04003859 rb_check_pages(cpu_buffer);
Steven Rostedt9366c1b2012-11-29 22:31:16 -05003860 raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
Steven Rostedt659f4512012-05-14 17:02:33 -04003861
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003862 atomic_dec(&cpu_buffer->record_disabled);
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07003863 atomic_dec(&cpu_buffer->buffer->resize_disabled);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003864 kfree(iter);
3865}
Robert Richterc4f50182008-12-11 16:49:22 +01003866EXPORT_SYMBOL_GPL(ring_buffer_read_finish);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003867
3868/**
3869 * ring_buffer_read - read the next item in the ring buffer by the iterator
3870 * @iter: The ring buffer iterator
3871 * @ts: The time stamp of the event read.
3872 *
3873 * This reads the next event in the ring buffer and increments the iterator.
3874 */
3875struct ring_buffer_event *
3876ring_buffer_read(struct ring_buffer_iter *iter, u64 *ts)
3877{
3878 struct ring_buffer_event *event;
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003879 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
3880 unsigned long flags;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003881
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02003882 raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
Steven Rostedt7e9391c2009-09-03 10:02:09 -04003883 again:
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003884 event = rb_iter_peek(iter, ts);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003885 if (!event)
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003886 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003887
Steven Rostedt7e9391c2009-09-03 10:02:09 -04003888 if (event->type_len == RINGBUF_TYPE_PADDING)
3889 goto again;
3890
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003891 rb_advance_iter(iter);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003892 out:
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02003893 raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003894
3895 return event;
3896}
Robert Richterc4f50182008-12-11 16:49:22 +01003897EXPORT_SYMBOL_GPL(ring_buffer_read);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003898
3899/**
3900 * ring_buffer_size - return the size of the ring buffer (in bytes)
3901 * @buffer: The ring buffer.
3902 */
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08003903unsigned long ring_buffer_size(struct ring_buffer *buffer, int cpu)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003904{
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08003905 /*
3906 * Earlier, this method returned
3907 * BUF_PAGE_SIZE * buffer->nr_pages
3908 * Since the nr_pages field is now removed, we have converted this to
3909 * return the per cpu buffer value.
3910 */
3911 if (!cpumask_test_cpu(cpu, buffer->cpumask))
3912 return 0;
3913
3914 return BUF_PAGE_SIZE * buffer->buffers[cpu]->nr_pages;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003915}
Robert Richterc4f50182008-12-11 16:49:22 +01003916EXPORT_SYMBOL_GPL(ring_buffer_size);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003917
3918static void
3919rb_reset_cpu(struct ring_buffer_per_cpu *cpu_buffer)
3920{
Steven Rostedt77ae3652009-03-27 11:00:29 -04003921 rb_head_page_deactivate(cpu_buffer);
3922
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003923 cpu_buffer->head_page
Steven Rostedt3adc54f2009-03-30 15:32:01 -04003924 = list_entry(cpu_buffer->pages, struct buffer_page, list);
Steven Rostedtbf41a152008-10-04 02:00:59 -04003925 local_set(&cpu_buffer->head_page->write, 0);
Steven Rostedt778c55d2009-05-01 18:44:45 -04003926 local_set(&cpu_buffer->head_page->entries, 0);
Steven Rostedtabc9b562008-12-02 15:34:06 -05003927 local_set(&cpu_buffer->head_page->page->commit, 0);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003928
Steven Rostedt6f807ac2008-10-04 02:00:58 -04003929 cpu_buffer->head_page->read = 0;
Steven Rostedtbf41a152008-10-04 02:00:59 -04003930
3931 cpu_buffer->tail_page = cpu_buffer->head_page;
3932 cpu_buffer->commit_page = cpu_buffer->head_page;
3933
3934 INIT_LIST_HEAD(&cpu_buffer->reader_page->list);
Vaibhav Nagarnaik5040b4b2012-05-03 18:59:51 -07003935 INIT_LIST_HEAD(&cpu_buffer->new_pages);
Steven Rostedtbf41a152008-10-04 02:00:59 -04003936 local_set(&cpu_buffer->reader_page->write, 0);
Steven Rostedt778c55d2009-05-01 18:44:45 -04003937 local_set(&cpu_buffer->reader_page->entries, 0);
Steven Rostedtabc9b562008-12-02 15:34:06 -05003938 local_set(&cpu_buffer->reader_page->page->commit, 0);
Steven Rostedt6f807ac2008-10-04 02:00:58 -04003939 cpu_buffer->reader_page->read = 0;
Steven Rostedtd7690412008-10-01 00:29:53 -04003940
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -07003941 local_set(&cpu_buffer->entries_bytes, 0);
Steven Rostedt77ae3652009-03-27 11:00:29 -04003942 local_set(&cpu_buffer->overrun, 0);
Slava Pestov884bfe82011-07-15 14:23:58 -07003943 local_set(&cpu_buffer->commit_overrun, 0);
3944 local_set(&cpu_buffer->dropped_events, 0);
Steven Rostedte4906ef2009-04-30 20:49:44 -04003945 local_set(&cpu_buffer->entries, 0);
Steven Rostedtfa743952009-06-16 12:37:57 -04003946 local_set(&cpu_buffer->committing, 0);
3947 local_set(&cpu_buffer->commits, 0);
Steven Rostedt77ae3652009-03-27 11:00:29 -04003948 cpu_buffer->read = 0;
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -07003949 cpu_buffer->read_bytes = 0;
Steven Rostedt69507c02009-01-21 18:45:57 -05003950
3951 cpu_buffer->write_stamp = 0;
3952 cpu_buffer->read_stamp = 0;
Steven Rostedt77ae3652009-03-27 11:00:29 -04003953
Steven Rostedt66a8cb92010-03-31 13:21:56 -04003954 cpu_buffer->lost_events = 0;
3955 cpu_buffer->last_overrun = 0;
3956
Steven Rostedt77ae3652009-03-27 11:00:29 -04003957 rb_head_page_activate(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003958}
3959
3960/**
3961 * ring_buffer_reset_cpu - reset a ring buffer per CPU buffer
3962 * @buffer: The ring buffer to reset a per cpu buffer of
3963 * @cpu: The CPU buffer to be reset
3964 */
3965void ring_buffer_reset_cpu(struct ring_buffer *buffer, int cpu)
3966{
3967 struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
3968 unsigned long flags;
3969
Rusty Russell9e01c1b2009-01-01 10:12:22 +10303970 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04003971 return;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003972
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07003973 atomic_inc(&buffer->resize_disabled);
Steven Rostedt41ede232009-05-01 20:26:54 -04003974 atomic_inc(&cpu_buffer->record_disabled);
3975
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07003976 /* Make sure all commits have finished */
3977 synchronize_sched();
3978
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02003979 raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003980
Steven Rostedt41b6a952009-09-02 09:59:48 -04003981 if (RB_WARN_ON(cpu_buffer, local_read(&cpu_buffer->committing)))
3982 goto out;
3983
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01003984 arch_spin_lock(&cpu_buffer->lock);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003985
3986 rb_reset_cpu(cpu_buffer);
3987
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01003988 arch_spin_unlock(&cpu_buffer->lock);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003989
Steven Rostedt41b6a952009-09-02 09:59:48 -04003990 out:
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02003991 raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
Steven Rostedt41ede232009-05-01 20:26:54 -04003992
3993 atomic_dec(&cpu_buffer->record_disabled);
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07003994 atomic_dec(&buffer->resize_disabled);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003995}
Robert Richterc4f50182008-12-11 16:49:22 +01003996EXPORT_SYMBOL_GPL(ring_buffer_reset_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003997
3998/**
3999 * ring_buffer_reset - reset a ring buffer
4000 * @buffer: The ring buffer to reset all cpu buffers
4001 */
4002void ring_buffer_reset(struct ring_buffer *buffer)
4003{
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004004 int cpu;
4005
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004006 for_each_buffer_cpu(buffer, cpu)
Steven Rostedtd7690412008-10-01 00:29:53 -04004007 ring_buffer_reset_cpu(buffer, cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004008}
Robert Richterc4f50182008-12-11 16:49:22 +01004009EXPORT_SYMBOL_GPL(ring_buffer_reset);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004010
4011/**
4012 * rind_buffer_empty - is the ring buffer empty?
4013 * @buffer: The ring buffer to test
4014 */
4015int ring_buffer_empty(struct ring_buffer *buffer)
4016{
4017 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedtd4788202009-06-17 00:39:43 -04004018 unsigned long flags;
Steven Rostedt8d707e82009-06-16 21:22:48 -04004019 int dolock;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004020 int cpu;
Steven Rostedtd4788202009-06-17 00:39:43 -04004021 int ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004022
Steven Rostedt8d707e82009-06-16 21:22:48 -04004023 dolock = rb_ok_to_lock();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004024
4025 /* yes this is racy, but if you don't like the race, lock the buffer */
4026 for_each_buffer_cpu(buffer, cpu) {
4027 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt8d707e82009-06-16 21:22:48 -04004028 local_irq_save(flags);
4029 if (dolock)
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02004030 raw_spin_lock(&cpu_buffer->reader_lock);
Steven Rostedtd4788202009-06-17 00:39:43 -04004031 ret = rb_per_cpu_empty(cpu_buffer);
Steven Rostedt8d707e82009-06-16 21:22:48 -04004032 if (dolock)
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02004033 raw_spin_unlock(&cpu_buffer->reader_lock);
Steven Rostedt8d707e82009-06-16 21:22:48 -04004034 local_irq_restore(flags);
4035
Steven Rostedtd4788202009-06-17 00:39:43 -04004036 if (!ret)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004037 return 0;
4038 }
Steven Rostedt554f7862009-03-11 22:00:13 -04004039
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004040 return 1;
4041}
Robert Richterc4f50182008-12-11 16:49:22 +01004042EXPORT_SYMBOL_GPL(ring_buffer_empty);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004043
4044/**
4045 * ring_buffer_empty_cpu - is a cpu buffer of a ring buffer empty?
4046 * @buffer: The ring buffer
4047 * @cpu: The CPU buffer to test
4048 */
4049int ring_buffer_empty_cpu(struct ring_buffer *buffer, int cpu)
4050{
4051 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedtd4788202009-06-17 00:39:43 -04004052 unsigned long flags;
Steven Rostedt8d707e82009-06-16 21:22:48 -04004053 int dolock;
Steven Rostedt8aabee52009-03-12 13:13:49 -04004054 int ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004055
Rusty Russell9e01c1b2009-01-01 10:12:22 +10304056 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04004057 return 1;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004058
Steven Rostedt8d707e82009-06-16 21:22:48 -04004059 dolock = rb_ok_to_lock();
Steven Rostedt554f7862009-03-11 22:00:13 -04004060
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004061 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt8d707e82009-06-16 21:22:48 -04004062 local_irq_save(flags);
4063 if (dolock)
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02004064 raw_spin_lock(&cpu_buffer->reader_lock);
Steven Rostedt554f7862009-03-11 22:00:13 -04004065 ret = rb_per_cpu_empty(cpu_buffer);
Steven Rostedt8d707e82009-06-16 21:22:48 -04004066 if (dolock)
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02004067 raw_spin_unlock(&cpu_buffer->reader_lock);
Steven Rostedt8d707e82009-06-16 21:22:48 -04004068 local_irq_restore(flags);
Steven Rostedt554f7862009-03-11 22:00:13 -04004069
4070 return ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004071}
Robert Richterc4f50182008-12-11 16:49:22 +01004072EXPORT_SYMBOL_GPL(ring_buffer_empty_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004073
Steven Rostedt85bac322009-09-04 14:24:40 -04004074#ifdef CONFIG_RING_BUFFER_ALLOW_SWAP
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004075/**
4076 * ring_buffer_swap_cpu - swap a CPU buffer between two ring buffers
4077 * @buffer_a: One buffer to swap with
4078 * @buffer_b: The other buffer to swap with
4079 *
4080 * This function is useful for tracers that want to take a "snapshot"
4081 * of a CPU buffer and has another back up buffer lying around.
4082 * it is expected that the tracer handles the cpu buffer not being
4083 * used at the moment.
4084 */
4085int ring_buffer_swap_cpu(struct ring_buffer *buffer_a,
4086 struct ring_buffer *buffer_b, int cpu)
4087{
4088 struct ring_buffer_per_cpu *cpu_buffer_a;
4089 struct ring_buffer_per_cpu *cpu_buffer_b;
Steven Rostedt554f7862009-03-11 22:00:13 -04004090 int ret = -EINVAL;
4091
Rusty Russell9e01c1b2009-01-01 10:12:22 +10304092 if (!cpumask_test_cpu(cpu, buffer_a->cpumask) ||
4093 !cpumask_test_cpu(cpu, buffer_b->cpumask))
Steven Rostedt554f7862009-03-11 22:00:13 -04004094 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004095
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08004096 cpu_buffer_a = buffer_a->buffers[cpu];
4097 cpu_buffer_b = buffer_b->buffers[cpu];
4098
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004099 /* At least make sure the two buffers are somewhat the same */
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08004100 if (cpu_buffer_a->nr_pages != cpu_buffer_b->nr_pages)
Steven Rostedt554f7862009-03-11 22:00:13 -04004101 goto out;
4102
4103 ret = -EAGAIN;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004104
Steven Rostedt97b17ef2009-01-21 15:24:56 -05004105 if (ring_buffer_flags != RB_BUFFERS_ON)
Steven Rostedt554f7862009-03-11 22:00:13 -04004106 goto out;
Steven Rostedt97b17ef2009-01-21 15:24:56 -05004107
4108 if (atomic_read(&buffer_a->record_disabled))
Steven Rostedt554f7862009-03-11 22:00:13 -04004109 goto out;
Steven Rostedt97b17ef2009-01-21 15:24:56 -05004110
4111 if (atomic_read(&buffer_b->record_disabled))
Steven Rostedt554f7862009-03-11 22:00:13 -04004112 goto out;
Steven Rostedt97b17ef2009-01-21 15:24:56 -05004113
Steven Rostedt97b17ef2009-01-21 15:24:56 -05004114 if (atomic_read(&cpu_buffer_a->record_disabled))
Steven Rostedt554f7862009-03-11 22:00:13 -04004115 goto out;
Steven Rostedt97b17ef2009-01-21 15:24:56 -05004116
4117 if (atomic_read(&cpu_buffer_b->record_disabled))
Steven Rostedt554f7862009-03-11 22:00:13 -04004118 goto out;
Steven Rostedt97b17ef2009-01-21 15:24:56 -05004119
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004120 /*
4121 * We can't do a synchronize_sched here because this
4122 * function can be called in atomic context.
4123 * Normally this will be called from the same CPU as cpu.
4124 * If not it's up to the caller to protect this.
4125 */
4126 atomic_inc(&cpu_buffer_a->record_disabled);
4127 atomic_inc(&cpu_buffer_b->record_disabled);
4128
Steven Rostedt98277992009-09-02 10:56:15 -04004129 ret = -EBUSY;
4130 if (local_read(&cpu_buffer_a->committing))
4131 goto out_dec;
4132 if (local_read(&cpu_buffer_b->committing))
4133 goto out_dec;
4134
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004135 buffer_a->buffers[cpu] = cpu_buffer_b;
4136 buffer_b->buffers[cpu] = cpu_buffer_a;
4137
4138 cpu_buffer_b->buffer = buffer_a;
4139 cpu_buffer_a->buffer = buffer_b;
4140
Steven Rostedt98277992009-09-02 10:56:15 -04004141 ret = 0;
4142
4143out_dec:
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004144 atomic_dec(&cpu_buffer_a->record_disabled);
4145 atomic_dec(&cpu_buffer_b->record_disabled);
Steven Rostedt554f7862009-03-11 22:00:13 -04004146out:
Steven Rostedt554f7862009-03-11 22:00:13 -04004147 return ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004148}
Robert Richterc4f50182008-12-11 16:49:22 +01004149EXPORT_SYMBOL_GPL(ring_buffer_swap_cpu);
Steven Rostedt85bac322009-09-04 14:24:40 -04004150#endif /* CONFIG_RING_BUFFER_ALLOW_SWAP */
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004151
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004152/**
4153 * ring_buffer_alloc_read_page - allocate a page to read from buffer
4154 * @buffer: the buffer to allocate for.
4155 *
4156 * This function is used in conjunction with ring_buffer_read_page.
4157 * When reading a full page from the ring buffer, these functions
4158 * can be used to speed up the process. The calling function should
4159 * allocate a few pages first with this function. Then when it
4160 * needs to get pages from the ring buffer, it passes the result
4161 * of this function into ring_buffer_read_page, which will swap
4162 * the page that was allocated, with the read page of the buffer.
4163 *
4164 * Returns:
4165 * The page allocated, or NULL on error.
4166 */
Vaibhav Nagarnaik7ea59062011-05-03 17:56:42 -07004167void *ring_buffer_alloc_read_page(struct ring_buffer *buffer, int cpu)
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004168{
Steven Rostedt044fa782008-12-02 23:50:03 -05004169 struct buffer_data_page *bpage;
Vaibhav Nagarnaik7ea59062011-05-03 17:56:42 -07004170 struct page *page;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004171
Vaibhav Nagarnaikd7ec4bf2011-06-07 17:01:42 -07004172 page = alloc_pages_node(cpu_to_node(cpu),
4173 GFP_KERNEL | __GFP_NORETRY, 0);
Vaibhav Nagarnaik7ea59062011-05-03 17:56:42 -07004174 if (!page)
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004175 return NULL;
4176
Vaibhav Nagarnaik7ea59062011-05-03 17:56:42 -07004177 bpage = page_address(page);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004178
Steven Rostedtef7a4a12009-03-03 00:27:49 -05004179 rb_init_page(bpage);
4180
Steven Rostedt044fa782008-12-02 23:50:03 -05004181 return bpage;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004182}
Steven Rostedtd6ce96d2009-05-05 01:15:24 -04004183EXPORT_SYMBOL_GPL(ring_buffer_alloc_read_page);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004184
4185/**
4186 * ring_buffer_free_read_page - free an allocated read page
4187 * @buffer: the buffer the page was allocate for
4188 * @data: the page to free
4189 *
4190 * Free a page allocated from ring_buffer_alloc_read_page.
4191 */
4192void ring_buffer_free_read_page(struct ring_buffer *buffer, void *data)
4193{
4194 free_page((unsigned long)data);
4195}
Steven Rostedtd6ce96d2009-05-05 01:15:24 -04004196EXPORT_SYMBOL_GPL(ring_buffer_free_read_page);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004197
4198/**
4199 * ring_buffer_read_page - extract a page from the ring buffer
4200 * @buffer: buffer to extract from
4201 * @data_page: the page to use allocated from ring_buffer_alloc_read_page
Steven Rostedtef7a4a12009-03-03 00:27:49 -05004202 * @len: amount to extract
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004203 * @cpu: the cpu of the buffer to extract
4204 * @full: should the extraction only happen when the page is full.
4205 *
4206 * This function will pull out a page from the ring buffer and consume it.
4207 * @data_page must be the address of the variable that was returned
4208 * from ring_buffer_alloc_read_page. This is because the page might be used
4209 * to swap with a page in the ring buffer.
4210 *
4211 * for example:
Lai Jiangshanb85fa012009-02-09 14:21:14 +08004212 * rpage = ring_buffer_alloc_read_page(buffer);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004213 * if (!rpage)
4214 * return error;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05004215 * ret = ring_buffer_read_page(buffer, &rpage, len, cpu, 0);
Lai Jiangshan667d2412009-02-09 14:21:17 +08004216 * if (ret >= 0)
4217 * process_page(rpage, ret);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004218 *
4219 * When @full is set, the function will not return true unless
4220 * the writer is off the reader page.
4221 *
4222 * Note: it is up to the calling functions to handle sleeps and wakeups.
4223 * The ring buffer can be used anywhere in the kernel and can not
4224 * blindly call wake_up. The layer that uses the ring buffer must be
4225 * responsible for that.
4226 *
4227 * Returns:
Lai Jiangshan667d2412009-02-09 14:21:17 +08004228 * >=0 if data has been transferred, returns the offset of consumed data.
4229 * <0 if no data has been transferred.
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004230 */
4231int ring_buffer_read_page(struct ring_buffer *buffer,
Steven Rostedtef7a4a12009-03-03 00:27:49 -05004232 void **data_page, size_t len, int cpu, int full)
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004233{
4234 struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
4235 struct ring_buffer_event *event;
Steven Rostedt044fa782008-12-02 23:50:03 -05004236 struct buffer_data_page *bpage;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05004237 struct buffer_page *reader;
Steven Rostedtff0ff842010-03-31 22:11:42 -04004238 unsigned long missed_events;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004239 unsigned long flags;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05004240 unsigned int commit;
Lai Jiangshan667d2412009-02-09 14:21:17 +08004241 unsigned int read;
Steven Rostedt4f3640f2009-03-03 23:52:42 -05004242 u64 save_timestamp;
Lai Jiangshan667d2412009-02-09 14:21:17 +08004243 int ret = -1;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004244
Steven Rostedt554f7862009-03-11 22:00:13 -04004245 if (!cpumask_test_cpu(cpu, buffer->cpumask))
4246 goto out;
4247
Steven Rostedt474d32b2009-03-03 19:51:40 -05004248 /*
4249 * If len is not big enough to hold the page header, then
4250 * we can not copy anything.
4251 */
4252 if (len <= BUF_PAGE_HDR_SIZE)
Steven Rostedt554f7862009-03-11 22:00:13 -04004253 goto out;
Steven Rostedt474d32b2009-03-03 19:51:40 -05004254
4255 len -= BUF_PAGE_HDR_SIZE;
4256
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004257 if (!data_page)
Steven Rostedt554f7862009-03-11 22:00:13 -04004258 goto out;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004259
Steven Rostedt044fa782008-12-02 23:50:03 -05004260 bpage = *data_page;
4261 if (!bpage)
Steven Rostedt554f7862009-03-11 22:00:13 -04004262 goto out;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004263
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02004264 raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004265
Steven Rostedtef7a4a12009-03-03 00:27:49 -05004266 reader = rb_get_reader_page(cpu_buffer);
4267 if (!reader)
Steven Rostedt554f7862009-03-11 22:00:13 -04004268 goto out_unlock;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004269
Steven Rostedtef7a4a12009-03-03 00:27:49 -05004270 event = rb_reader_event(cpu_buffer);
Lai Jiangshan667d2412009-02-09 14:21:17 +08004271
Steven Rostedtef7a4a12009-03-03 00:27:49 -05004272 read = reader->read;
4273 commit = rb_page_commit(reader);
4274
Steven Rostedt66a8cb92010-03-31 13:21:56 -04004275 /* Check if any events were dropped */
Steven Rostedtff0ff842010-03-31 22:11:42 -04004276 missed_events = cpu_buffer->lost_events;
Steven Rostedt66a8cb92010-03-31 13:21:56 -04004277
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004278 /*
Steven Rostedt474d32b2009-03-03 19:51:40 -05004279 * If this page has been partially read or
4280 * if len is not big enough to read the rest of the page or
4281 * a writer is still on the page, then
4282 * we must copy the data from the page to the buffer.
4283 * Otherwise, we can simply swap the page with the one passed in.
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004284 */
Steven Rostedt474d32b2009-03-03 19:51:40 -05004285 if (read || (len < (commit - read)) ||
Steven Rostedtef7a4a12009-03-03 00:27:49 -05004286 cpu_buffer->reader_page == cpu_buffer->commit_page) {
Lai Jiangshan667d2412009-02-09 14:21:17 +08004287 struct buffer_data_page *rpage = cpu_buffer->reader_page->page;
Steven Rostedt474d32b2009-03-03 19:51:40 -05004288 unsigned int rpos = read;
4289 unsigned int pos = 0;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05004290 unsigned int size;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004291
4292 if (full)
Steven Rostedt554f7862009-03-11 22:00:13 -04004293 goto out_unlock;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004294
Steven Rostedtef7a4a12009-03-03 00:27:49 -05004295 if (len > (commit - read))
4296 len = (commit - read);
4297
Steven Rostedt69d1b832010-10-07 18:18:05 -04004298 /* Always keep the time extend and data together */
4299 size = rb_event_ts_length(event);
Steven Rostedtef7a4a12009-03-03 00:27:49 -05004300
4301 if (len < size)
Steven Rostedt554f7862009-03-11 22:00:13 -04004302 goto out_unlock;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05004303
Steven Rostedt4f3640f2009-03-03 23:52:42 -05004304 /* save the current timestamp, since the user will need it */
4305 save_timestamp = cpu_buffer->read_stamp;
4306
Steven Rostedtef7a4a12009-03-03 00:27:49 -05004307 /* Need to copy one event at a time */
4308 do {
David Sharpe1e35922010-12-22 16:38:24 -08004309 /* We need the size of one event, because
4310 * rb_advance_reader only advances by one event,
4311 * whereas rb_event_ts_length may include the size of
4312 * one or two events.
4313 * We have already ensured there's enough space if this
4314 * is a time extend. */
4315 size = rb_event_length(event);
Steven Rostedt474d32b2009-03-03 19:51:40 -05004316 memcpy(bpage->data + pos, rpage->data + rpos, size);
Steven Rostedtef7a4a12009-03-03 00:27:49 -05004317
4318 len -= size;
4319
4320 rb_advance_reader(cpu_buffer);
Steven Rostedt474d32b2009-03-03 19:51:40 -05004321 rpos = reader->read;
4322 pos += size;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05004323
Huang Ying18fab912010-07-28 14:14:01 +08004324 if (rpos >= commit)
4325 break;
4326
Steven Rostedtef7a4a12009-03-03 00:27:49 -05004327 event = rb_reader_event(cpu_buffer);
Steven Rostedt69d1b832010-10-07 18:18:05 -04004328 /* Always keep the time extend and data together */
4329 size = rb_event_ts_length(event);
David Sharpe1e35922010-12-22 16:38:24 -08004330 } while (len >= size);
Lai Jiangshan667d2412009-02-09 14:21:17 +08004331
4332 /* update bpage */
Steven Rostedtef7a4a12009-03-03 00:27:49 -05004333 local_set(&bpage->commit, pos);
Steven Rostedt4f3640f2009-03-03 23:52:42 -05004334 bpage->time_stamp = save_timestamp;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05004335
Steven Rostedt474d32b2009-03-03 19:51:40 -05004336 /* we copied everything to the beginning */
4337 read = 0;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004338 } else {
Steven Rostedtafbab762009-05-01 19:40:05 -04004339 /* update the entry counter */
Steven Rostedt77ae3652009-03-27 11:00:29 -04004340 cpu_buffer->read += rb_page_entries(reader);
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -07004341 cpu_buffer->read_bytes += BUF_PAGE_SIZE;
Steven Rostedtafbab762009-05-01 19:40:05 -04004342
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004343 /* swap the pages */
Steven Rostedt044fa782008-12-02 23:50:03 -05004344 rb_init_page(bpage);
Steven Rostedtef7a4a12009-03-03 00:27:49 -05004345 bpage = reader->page;
4346 reader->page = *data_page;
4347 local_set(&reader->write, 0);
Steven Rostedt778c55d2009-05-01 18:44:45 -04004348 local_set(&reader->entries, 0);
Steven Rostedtef7a4a12009-03-03 00:27:49 -05004349 reader->read = 0;
Steven Rostedt044fa782008-12-02 23:50:03 -05004350 *data_page = bpage;
Steven Rostedtff0ff842010-03-31 22:11:42 -04004351
4352 /*
4353 * Use the real_end for the data size,
4354 * This gives us a chance to store the lost events
4355 * on the page.
4356 */
4357 if (reader->real_end)
4358 local_set(&bpage->commit, reader->real_end);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004359 }
Lai Jiangshan667d2412009-02-09 14:21:17 +08004360 ret = read;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004361
Steven Rostedt66a8cb92010-03-31 13:21:56 -04004362 cpu_buffer->lost_events = 0;
Steven Rostedt2711ca22010-05-21 13:32:26 -04004363
4364 commit = local_read(&bpage->commit);
Steven Rostedt66a8cb92010-03-31 13:21:56 -04004365 /*
4366 * Set a flag in the commit field if we lost events
4367 */
Steven Rostedtff0ff842010-03-31 22:11:42 -04004368 if (missed_events) {
Steven Rostedtff0ff842010-03-31 22:11:42 -04004369 /* If there is room at the end of the page to save the
4370 * missed events, then record it there.
4371 */
4372 if (BUF_PAGE_SIZE - commit >= sizeof(missed_events)) {
4373 memcpy(&bpage->data[commit], &missed_events,
4374 sizeof(missed_events));
4375 local_add(RB_MISSED_STORED, &bpage->commit);
Steven Rostedt2711ca22010-05-21 13:32:26 -04004376 commit += sizeof(missed_events);
Steven Rostedtff0ff842010-03-31 22:11:42 -04004377 }
Steven Rostedt66a8cb92010-03-31 13:21:56 -04004378 local_add(RB_MISSED_EVENTS, &bpage->commit);
Steven Rostedtff0ff842010-03-31 22:11:42 -04004379 }
Steven Rostedt66a8cb92010-03-31 13:21:56 -04004380
Steven Rostedt2711ca22010-05-21 13:32:26 -04004381 /*
4382 * This page may be off to user land. Zero it out here.
4383 */
4384 if (commit < BUF_PAGE_SIZE)
4385 memset(&bpage->data[commit], 0, BUF_PAGE_SIZE - commit);
4386
Steven Rostedt554f7862009-03-11 22:00:13 -04004387 out_unlock:
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02004388 raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004389
Steven Rostedt554f7862009-03-11 22:00:13 -04004390 out:
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004391 return ret;
4392}
Steven Rostedtd6ce96d2009-05-05 01:15:24 -04004393EXPORT_SYMBOL_GPL(ring_buffer_read_page);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004394
Steven Rostedt59222ef2009-03-12 11:46:03 -04004395#ifdef CONFIG_HOTPLUG_CPU
Frederic Weisbecker09c9e842009-03-21 04:33:36 +01004396static int rb_cpu_notify(struct notifier_block *self,
4397 unsigned long action, void *hcpu)
Steven Rostedt554f7862009-03-11 22:00:13 -04004398{
4399 struct ring_buffer *buffer =
4400 container_of(self, struct ring_buffer, cpu_notify);
4401 long cpu = (long)hcpu;
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08004402 int cpu_i, nr_pages_same;
4403 unsigned int nr_pages;
Steven Rostedt554f7862009-03-11 22:00:13 -04004404
4405 switch (action) {
4406 case CPU_UP_PREPARE:
4407 case CPU_UP_PREPARE_FROZEN:
Rusty Russell3f237a72009-06-12 21:15:30 +09304408 if (cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt554f7862009-03-11 22:00:13 -04004409 return NOTIFY_OK;
4410
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08004411 nr_pages = 0;
4412 nr_pages_same = 1;
4413 /* check if all cpu sizes are same */
4414 for_each_buffer_cpu(buffer, cpu_i) {
4415 /* fill in the size from first enabled cpu */
4416 if (nr_pages == 0)
4417 nr_pages = buffer->buffers[cpu_i]->nr_pages;
4418 if (nr_pages != buffer->buffers[cpu_i]->nr_pages) {
4419 nr_pages_same = 0;
4420 break;
4421 }
4422 }
4423 /* allocate minimum pages, user can later expand it */
4424 if (!nr_pages_same)
4425 nr_pages = 2;
Steven Rostedt554f7862009-03-11 22:00:13 -04004426 buffer->buffers[cpu] =
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08004427 rb_allocate_cpu_buffer(buffer, nr_pages, cpu);
Steven Rostedt554f7862009-03-11 22:00:13 -04004428 if (!buffer->buffers[cpu]) {
4429 WARN(1, "failed to allocate ring buffer on CPU %ld\n",
4430 cpu);
4431 return NOTIFY_OK;
4432 }
4433 smp_wmb();
Rusty Russell3f237a72009-06-12 21:15:30 +09304434 cpumask_set_cpu(cpu, buffer->cpumask);
Steven Rostedt554f7862009-03-11 22:00:13 -04004435 break;
4436 case CPU_DOWN_PREPARE:
4437 case CPU_DOWN_PREPARE_FROZEN:
4438 /*
4439 * Do nothing.
4440 * If we were to free the buffer, then the user would
4441 * lose any trace that was in the buffer.
4442 */
4443 break;
4444 default:
4445 break;
4446 }
4447 return NOTIFY_OK;
4448}
4449#endif