blob: b59aea2c48c287f5de894efcba7d53c02fd6f279 [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>
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -050011#include <linux/irq_work.h>
Steven Rostedt7a8e76a2008-09-29 23:02:38 -040012#include <linux/debugfs.h>
13#include <linux/uaccess.h>
Steven Rostedta81bd802009-02-06 01:45:16 -050014#include <linux/hardirq.h>
Steven Rostedt (Red Hat)6c43e552013-03-15 11:32:53 -040015#include <linux/kthread.h> /* for self test */
Vegard Nossum1744a212009-02-28 08:29:44 +010016#include <linux/kmemcheck.h>
Steven Rostedt7a8e76a2008-09-29 23:02:38 -040017#include <linux/module.h>
18#include <linux/percpu.h>
19#include <linux/mutex.h>
Steven Rostedt (Red Hat)6c43e552013-03-15 11:32:53 -040020#include <linux/delay.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090021#include <linux/slab.h>
Steven Rostedt7a8e76a2008-09-29 23:02:38 -040022#include <linux/init.h>
23#include <linux/hash.h>
24#include <linux/list.h>
Steven Rostedt554f7862009-03-11 22:00:13 -040025#include <linux/cpu.h>
Steven Rostedt7a8e76a2008-09-29 23:02:38 -040026#include <linux/fs.h>
27
Christoph Lameter79615762010-01-05 15:34:50 +090028#include <asm/local.h>
Steven Rostedt182e9f52008-11-03 23:15:56 -050029
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -070030static void update_pages_handler(struct work_struct *work);
31
Steven Rostedt033601a2008-11-21 12:41:55 -050032/*
Steven Rostedtd1b182a2009-04-15 16:53:47 -040033 * The ring buffer header is special. We must manually up keep it.
34 */
35int ring_buffer_print_entry_header(struct trace_seq *s)
36{
37 int ret;
38
Lai Jiangshan334d4162009-04-24 11:27:05 +080039 ret = trace_seq_printf(s, "# compressed entry header\n");
40 ret = trace_seq_printf(s, "\ttype_len : 5 bits\n");
Steven Rostedtd1b182a2009-04-15 16:53:47 -040041 ret = trace_seq_printf(s, "\ttime_delta : 27 bits\n");
42 ret = trace_seq_printf(s, "\tarray : 32 bits\n");
43 ret = trace_seq_printf(s, "\n");
44 ret = trace_seq_printf(s, "\tpadding : type == %d\n",
45 RINGBUF_TYPE_PADDING);
46 ret = trace_seq_printf(s, "\ttime_extend : type == %d\n",
47 RINGBUF_TYPE_TIME_EXTEND);
Lai Jiangshan334d4162009-04-24 11:27:05 +080048 ret = trace_seq_printf(s, "\tdata max type_len == %d\n",
49 RINGBUF_TYPE_DATA_TYPE_LEN_MAX);
Steven Rostedtd1b182a2009-04-15 16:53:47 -040050
51 return ret;
52}
53
54/*
Steven Rostedt5cc98542009-03-12 22:24:17 -040055 * The ring buffer is made up of a list of pages. A separate list of pages is
56 * allocated for each CPU. A writer may only write to a buffer that is
57 * associated with the CPU it is currently executing on. A reader may read
58 * from any per cpu buffer.
59 *
60 * The reader is special. For each per cpu buffer, the reader has its own
61 * reader page. When a reader has read the entire reader page, this reader
62 * page is swapped with another page in the ring buffer.
63 *
64 * Now, as long as the writer is off the reader page, the reader can do what
65 * ever it wants with that page. The writer will never write to that page
66 * again (as long as it is out of the ring buffer).
67 *
68 * Here's some silly ASCII art.
69 *
70 * +------+
71 * |reader| RING BUFFER
72 * |page |
73 * +------+ +---+ +---+ +---+
74 * | |-->| |-->| |
75 * +---+ +---+ +---+
76 * ^ |
77 * | |
78 * +---------------+
79 *
80 *
81 * +------+
82 * |reader| RING BUFFER
83 * |page |------------------v
84 * +------+ +---+ +---+ +---+
85 * | |-->| |-->| |
86 * +---+ +---+ +---+
87 * ^ |
88 * | |
89 * +---------------+
90 *
91 *
92 * +------+
93 * |reader| RING BUFFER
94 * |page |------------------v
95 * +------+ +---+ +---+ +---+
96 * ^ | |-->| |-->| |
97 * | +---+ +---+ +---+
98 * | |
99 * | |
100 * +------------------------------+
101 *
102 *
103 * +------+
104 * |buffer| RING BUFFER
105 * |page |------------------v
106 * +------+ +---+ +---+ +---+
107 * ^ | | | |-->| |
108 * | New +---+ +---+ +---+
109 * | Reader------^ |
110 * | page |
111 * +------------------------------+
112 *
113 *
114 * After we make this swap, the reader can hand this page off to the splice
115 * code and be done with it. It can even allocate a new page if it needs to
116 * and swap that into the ring buffer.
117 *
118 * We will be using cmpxchg soon to make all this lockless.
119 *
120 */
121
122/*
Steven Rostedt033601a2008-11-21 12:41:55 -0500123 * A fast way to enable or disable all ring buffers is to
124 * call tracing_on or tracing_off. Turning off the ring buffers
125 * prevents all ring buffers from being recorded to.
126 * Turning this switch on, makes it OK to write to the
127 * ring buffer, if the ring buffer is enabled itself.
128 *
129 * There's three layers that must be on in order to write
130 * to the ring buffer.
131 *
132 * 1) This global flag must be set.
133 * 2) The ring buffer must be enabled for recording.
134 * 3) The per cpu buffer must be enabled for recording.
135 *
136 * In case of an anomaly, this global flag has a bit set that
137 * will permantly disable all ring buffers.
138 */
139
140/*
141 * Global flag to disable all recording to ring buffers
142 * This has two bits: ON, DISABLED
143 *
144 * ON DISABLED
145 * ---- ----------
146 * 0 0 : ring buffers are off
147 * 1 0 : ring buffers are on
148 * X 1 : ring buffers are permanently disabled
149 */
150
151enum {
152 RB_BUFFERS_ON_BIT = 0,
153 RB_BUFFERS_DISABLED_BIT = 1,
154};
155
156enum {
157 RB_BUFFERS_ON = 1 << RB_BUFFERS_ON_BIT,
158 RB_BUFFERS_DISABLED = 1 << RB_BUFFERS_DISABLED_BIT,
159};
160
Hannes Eder5e398412009-02-10 19:44:34 +0100161static unsigned long ring_buffer_flags __read_mostly = RB_BUFFERS_ON;
Steven Rostedta3583242008-11-11 15:01:42 -0500162
Steven Rostedt499e5472012-02-22 15:50:28 -0500163/* Used for individual buffers (after the counter) */
164#define RB_BUFFER_OFF (1 << 20)
165
Steven Rostedt474d32b2009-03-03 19:51:40 -0500166#define BUF_PAGE_HDR_SIZE offsetof(struct buffer_data_page, data)
167
Steven Rostedta3583242008-11-11 15:01:42 -0500168/**
Steven Rostedt033601a2008-11-21 12:41:55 -0500169 * tracing_off_permanent - permanently disable ring buffers
170 *
171 * This function, once called, will disable all ring buffers
Wenji Huangc3706f02009-02-10 01:03:18 -0500172 * permanently.
Steven Rostedt033601a2008-11-21 12:41:55 -0500173 */
174void tracing_off_permanent(void)
175{
176 set_bit(RB_BUFFERS_DISABLED_BIT, &ring_buffer_flags);
Steven Rostedta3583242008-11-11 15:01:42 -0500177}
178
Steven Rostedte3d6bf02009-03-03 13:53:07 -0500179#define RB_EVNT_HDR_SIZE (offsetof(struct ring_buffer_event, array))
Andrew Morton67d34722009-01-09 12:27:09 -0800180#define RB_ALIGNMENT 4U
Lai Jiangshan334d4162009-04-24 11:27:05 +0800181#define RB_MAX_SMALL_DATA (RB_ALIGNMENT * RINGBUF_TYPE_DATA_TYPE_LEN_MAX)
Steven Rostedtc7b09302009-06-11 11:12:00 -0400182#define RB_EVNT_MIN_SIZE 8U /* two 32bit words */
Lai Jiangshan334d4162009-04-24 11:27:05 +0800183
James Hogan649508f2012-05-30 12:11:19 +0100184#ifndef CONFIG_HAVE_64BIT_ALIGNED_ACCESS
Steven Rostedt22710482010-03-18 17:54:19 -0400185# define RB_FORCE_8BYTE_ALIGNMENT 0
186# define RB_ARCH_ALIGNMENT RB_ALIGNMENT
187#else
188# define RB_FORCE_8BYTE_ALIGNMENT 1
189# define RB_ARCH_ALIGNMENT 8U
190#endif
191
James Hogan649508f2012-05-30 12:11:19 +0100192#define RB_ALIGN_DATA __aligned(RB_ARCH_ALIGNMENT)
193
Lai Jiangshan334d4162009-04-24 11:27:05 +0800194/* define RINGBUF_TYPE_DATA for 'case RINGBUF_TYPE_DATA:' */
195#define RINGBUF_TYPE_DATA 0 ... RINGBUF_TYPE_DATA_TYPE_LEN_MAX
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400196
197enum {
198 RB_LEN_TIME_EXTEND = 8,
199 RB_LEN_TIME_STAMP = 16,
200};
201
Steven Rostedt69d1b832010-10-07 18:18:05 -0400202#define skip_time_extend(event) \
203 ((struct ring_buffer_event *)((char *)event + RB_LEN_TIME_EXTEND))
204
Tom Zanussi2d622712009-03-22 03:30:49 -0500205static inline int rb_null_event(struct ring_buffer_event *event)
206{
Steven Rostedta1863c22009-09-03 10:23:58 -0400207 return event->type_len == RINGBUF_TYPE_PADDING && !event->time_delta;
Tom Zanussi2d622712009-03-22 03:30:49 -0500208}
209
210static void rb_event_set_padding(struct ring_buffer_event *event)
211{
Steven Rostedta1863c22009-09-03 10:23:58 -0400212 /* padding has a NULL time_delta */
Lai Jiangshan334d4162009-04-24 11:27:05 +0800213 event->type_len = RINGBUF_TYPE_PADDING;
Tom Zanussi2d622712009-03-22 03:30:49 -0500214 event->time_delta = 0;
215}
216
Tom Zanussi2d622712009-03-22 03:30:49 -0500217static unsigned
218rb_event_data_length(struct ring_buffer_event *event)
219{
220 unsigned length;
221
Lai Jiangshan334d4162009-04-24 11:27:05 +0800222 if (event->type_len)
223 length = event->type_len * RB_ALIGNMENT;
Tom Zanussi2d622712009-03-22 03:30:49 -0500224 else
225 length = event->array[0];
226 return length + RB_EVNT_HDR_SIZE;
227}
228
Steven Rostedt69d1b832010-10-07 18:18:05 -0400229/*
230 * Return the length of the given event. Will return
231 * the length of the time extend if the event is a
232 * time extend.
233 */
234static inline unsigned
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400235rb_event_length(struct ring_buffer_event *event)
236{
Lai Jiangshan334d4162009-04-24 11:27:05 +0800237 switch (event->type_len) {
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400238 case RINGBUF_TYPE_PADDING:
Tom Zanussi2d622712009-03-22 03:30:49 -0500239 if (rb_null_event(event))
240 /* undefined */
241 return -1;
Lai Jiangshan334d4162009-04-24 11:27:05 +0800242 return event->array[0] + RB_EVNT_HDR_SIZE;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400243
244 case RINGBUF_TYPE_TIME_EXTEND:
245 return RB_LEN_TIME_EXTEND;
246
247 case RINGBUF_TYPE_TIME_STAMP:
248 return RB_LEN_TIME_STAMP;
249
250 case RINGBUF_TYPE_DATA:
Tom Zanussi2d622712009-03-22 03:30:49 -0500251 return rb_event_data_length(event);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400252 default:
253 BUG();
254 }
255 /* not hit */
256 return 0;
257}
258
Steven Rostedt69d1b832010-10-07 18:18:05 -0400259/*
260 * Return total length of time extend and data,
261 * or just the event length for all other events.
262 */
263static inline unsigned
264rb_event_ts_length(struct ring_buffer_event *event)
265{
266 unsigned len = 0;
267
268 if (event->type_len == RINGBUF_TYPE_TIME_EXTEND) {
269 /* time extends include the data event after it */
270 len = RB_LEN_TIME_EXTEND;
271 event = skip_time_extend(event);
272 }
273 return len + rb_event_length(event);
274}
275
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400276/**
277 * ring_buffer_event_length - return the length of the event
278 * @event: the event to get the length of
Steven Rostedt69d1b832010-10-07 18:18:05 -0400279 *
280 * Returns the size of the data load of a data event.
281 * If the event is something other than a data event, it
282 * returns the size of the event itself. With the exception
283 * of a TIME EXTEND, where it still returns the size of the
284 * data load of the data event after it.
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400285 */
286unsigned ring_buffer_event_length(struct ring_buffer_event *event)
287{
Steven Rostedt69d1b832010-10-07 18:18:05 -0400288 unsigned length;
289
290 if (event->type_len == RINGBUF_TYPE_TIME_EXTEND)
291 event = skip_time_extend(event);
292
293 length = rb_event_length(event);
Lai Jiangshan334d4162009-04-24 11:27:05 +0800294 if (event->type_len > RINGBUF_TYPE_DATA_TYPE_LEN_MAX)
Robert Richter465634a2009-01-07 15:32:11 +0100295 return length;
296 length -= RB_EVNT_HDR_SIZE;
297 if (length > RB_MAX_SMALL_DATA + sizeof(event->array[0]))
298 length -= sizeof(event->array[0]);
299 return length;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400300}
Robert Richterc4f50182008-12-11 16:49:22 +0100301EXPORT_SYMBOL_GPL(ring_buffer_event_length);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400302
303/* inline for ring buffer fast paths */
Andrew Morton34a148b2009-01-09 12:27:09 -0800304static void *
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400305rb_event_data(struct ring_buffer_event *event)
306{
Steven Rostedt69d1b832010-10-07 18:18:05 -0400307 if (event->type_len == RINGBUF_TYPE_TIME_EXTEND)
308 event = skip_time_extend(event);
Lai Jiangshan334d4162009-04-24 11:27:05 +0800309 BUG_ON(event->type_len > RINGBUF_TYPE_DATA_TYPE_LEN_MAX);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400310 /* If length is in len field, then array[0] has the data */
Lai Jiangshan334d4162009-04-24 11:27:05 +0800311 if (event->type_len)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400312 return (void *)&event->array[0];
313 /* Otherwise length is in array[0] and array[1] has the data */
314 return (void *)&event->array[1];
315}
316
317/**
318 * ring_buffer_event_data - return the data of the event
319 * @event: the event to get the data from
320 */
321void *ring_buffer_event_data(struct ring_buffer_event *event)
322{
323 return rb_event_data(event);
324}
Robert Richterc4f50182008-12-11 16:49:22 +0100325EXPORT_SYMBOL_GPL(ring_buffer_event_data);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400326
327#define for_each_buffer_cpu(buffer, cpu) \
Rusty Russell9e01c1b2009-01-01 10:12:22 +1030328 for_each_cpu(cpu, buffer->cpumask)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400329
330#define TS_SHIFT 27
331#define TS_MASK ((1ULL << TS_SHIFT) - 1)
332#define TS_DELTA_TEST (~TS_MASK)
333
Steven Rostedt66a8cb92010-03-31 13:21:56 -0400334/* Flag when events were overwritten */
335#define RB_MISSED_EVENTS (1 << 31)
Steven Rostedtff0ff842010-03-31 22:11:42 -0400336/* Missed count stored at end */
337#define RB_MISSED_STORED (1 << 30)
Steven Rostedt66a8cb92010-03-31 13:21:56 -0400338
Steven Rostedtabc9b562008-12-02 15:34:06 -0500339struct buffer_data_page {
Steven Rostedte4c2ce82008-10-01 11:14:54 -0400340 u64 time_stamp; /* page time stamp */
Wenji Huangc3706f02009-02-10 01:03:18 -0500341 local_t commit; /* write committed index */
James Hogan649508f2012-05-30 12:11:19 +0100342 unsigned char data[] RB_ALIGN_DATA; /* data of buffer page */
Steven Rostedtabc9b562008-12-02 15:34:06 -0500343};
344
Steven Rostedt77ae3652009-03-27 11:00:29 -0400345/*
346 * Note, the buffer_page list must be first. The buffer pages
347 * are allocated in cache lines, which means that each buffer
348 * page will be at the beginning of a cache line, and thus
349 * the least significant bits will be zero. We use this to
350 * add flags in the list struct pointers, to make the ring buffer
351 * lockless.
352 */
Steven Rostedtabc9b562008-12-02 15:34:06 -0500353struct buffer_page {
Steven Rostedt778c55d2009-05-01 18:44:45 -0400354 struct list_head list; /* list of buffer pages */
Steven Rostedtabc9b562008-12-02 15:34:06 -0500355 local_t write; /* index for next write */
Steven Rostedt6f807ac2008-10-04 02:00:58 -0400356 unsigned read; /* index for next read */
Steven Rostedt778c55d2009-05-01 18:44:45 -0400357 local_t entries; /* entries on this page */
Steven Rostedtff0ff842010-03-31 22:11:42 -0400358 unsigned long real_end; /* real end of data */
Steven Rostedtabc9b562008-12-02 15:34:06 -0500359 struct buffer_data_page *page; /* Actual data page */
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400360};
361
Steven Rostedt77ae3652009-03-27 11:00:29 -0400362/*
363 * The buffer page counters, write and entries, must be reset
364 * atomically when crossing page boundaries. To synchronize this
365 * update, two counters are inserted into the number. One is
366 * the actual counter for the write position or count on the page.
367 *
368 * The other is a counter of updaters. Before an update happens
369 * the update partition of the counter is incremented. This will
370 * allow the updater to update the counter atomically.
371 *
372 * The counter is 20 bits, and the state data is 12.
373 */
374#define RB_WRITE_MASK 0xfffff
375#define RB_WRITE_INTCNT (1 << 20)
376
Steven Rostedt044fa782008-12-02 23:50:03 -0500377static void rb_init_page(struct buffer_data_page *bpage)
Steven Rostedtabc9b562008-12-02 15:34:06 -0500378{
Steven Rostedt044fa782008-12-02 23:50:03 -0500379 local_set(&bpage->commit, 0);
Steven Rostedtabc9b562008-12-02 15:34:06 -0500380}
381
Steven Rostedt474d32b2009-03-03 19:51:40 -0500382/**
383 * ring_buffer_page_len - the size of data on the page.
384 * @page: The page to read
385 *
386 * Returns the amount of data on the page, including buffer page header.
387 */
Steven Rostedtef7a4a12009-03-03 00:27:49 -0500388size_t ring_buffer_page_len(void *page)
389{
Steven Rostedt474d32b2009-03-03 19:51:40 -0500390 return local_read(&((struct buffer_data_page *)page)->commit)
391 + BUF_PAGE_HDR_SIZE;
Steven Rostedtef7a4a12009-03-03 00:27:49 -0500392}
393
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400394/*
Steven Rostedted568292008-09-29 23:02:40 -0400395 * Also stolen from mm/slob.c. Thanks to Mathieu Desnoyers for pointing
396 * this issue out.
397 */
Andrew Morton34a148b2009-01-09 12:27:09 -0800398static void free_buffer_page(struct buffer_page *bpage)
Steven Rostedted568292008-09-29 23:02:40 -0400399{
Andrew Morton34a148b2009-01-09 12:27:09 -0800400 free_page((unsigned long)bpage->page);
Steven Rostedte4c2ce82008-10-01 11:14:54 -0400401 kfree(bpage);
Steven Rostedted568292008-09-29 23:02:40 -0400402}
403
404/*
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400405 * We need to fit the time_stamp delta into 27 bits.
406 */
407static inline int test_time_stamp(u64 delta)
408{
409 if (delta & TS_DELTA_TEST)
410 return 1;
411 return 0;
412}
413
Steven Rostedt474d32b2009-03-03 19:51:40 -0500414#define BUF_PAGE_SIZE (PAGE_SIZE - BUF_PAGE_HDR_SIZE)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400415
Steven Rostedtbe957c42009-05-11 14:42:53 -0400416/* Max payload is BUF_PAGE_SIZE - header (8bytes) */
417#define BUF_MAX_DATA_SIZE (BUF_PAGE_SIZE - (sizeof(u32) * 2))
418
Steven Rostedtd1b182a2009-04-15 16:53:47 -0400419int ring_buffer_print_page_header(struct trace_seq *s)
420{
421 struct buffer_data_page field;
422 int ret;
423
424 ret = trace_seq_printf(s, "\tfield: u64 timestamp;\t"
Tom Zanussi26a50742009-10-06 01:09:50 -0500425 "offset:0;\tsize:%u;\tsigned:%u;\n",
426 (unsigned int)sizeof(field.time_stamp),
427 (unsigned int)is_signed_type(u64));
Steven Rostedtd1b182a2009-04-15 16:53:47 -0400428
429 ret = trace_seq_printf(s, "\tfield: local_t commit;\t"
Tom Zanussi26a50742009-10-06 01:09:50 -0500430 "offset:%u;\tsize:%u;\tsigned:%u;\n",
Steven Rostedtd1b182a2009-04-15 16:53:47 -0400431 (unsigned int)offsetof(typeof(field), commit),
Tom Zanussi26a50742009-10-06 01:09:50 -0500432 (unsigned int)sizeof(field.commit),
433 (unsigned int)is_signed_type(long));
Steven Rostedtd1b182a2009-04-15 16:53:47 -0400434
Steven Rostedt66a8cb92010-03-31 13:21:56 -0400435 ret = trace_seq_printf(s, "\tfield: int overwrite;\t"
436 "offset:%u;\tsize:%u;\tsigned:%u;\n",
437 (unsigned int)offsetof(typeof(field), commit),
438 1,
439 (unsigned int)is_signed_type(long));
440
Steven Rostedtd1b182a2009-04-15 16:53:47 -0400441 ret = trace_seq_printf(s, "\tfield: char data;\t"
Tom Zanussi26a50742009-10-06 01:09:50 -0500442 "offset:%u;\tsize:%u;\tsigned:%u;\n",
Steven Rostedtd1b182a2009-04-15 16:53:47 -0400443 (unsigned int)offsetof(typeof(field), data),
Tom Zanussi26a50742009-10-06 01:09:50 -0500444 (unsigned int)BUF_PAGE_SIZE,
445 (unsigned int)is_signed_type(char));
Steven Rostedtd1b182a2009-04-15 16:53:47 -0400446
447 return ret;
448}
449
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -0500450struct rb_irq_work {
451 struct irq_work work;
452 wait_queue_head_t waiters;
453 bool waiters_pending;
454};
455
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400456/*
457 * head_page == tail_page && head == tail then buffer is empty.
458 */
459struct ring_buffer_per_cpu {
460 int cpu;
Richard Kennedy985023d2010-03-25 11:27:36 +0000461 atomic_t record_disabled;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400462 struct ring_buffer *buffer;
Thomas Gleixner5389f6f2009-07-25 17:13:33 +0200463 raw_spinlock_t reader_lock; /* serialize readers */
Thomas Gleixner445c8952009-12-02 19:49:50 +0100464 arch_spinlock_t lock;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400465 struct lock_class_key lock_key;
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -0800466 unsigned int nr_pages;
Steven Rostedt3adc54f2009-03-30 15:32:01 -0400467 struct list_head *pages;
Steven Rostedt6f807ac2008-10-04 02:00:58 -0400468 struct buffer_page *head_page; /* read from head */
469 struct buffer_page *tail_page; /* write to tail */
Wenji Huangc3706f02009-02-10 01:03:18 -0500470 struct buffer_page *commit_page; /* committed pages */
Steven Rostedtd7690412008-10-01 00:29:53 -0400471 struct buffer_page *reader_page;
Steven Rostedt66a8cb92010-03-31 13:21:56 -0400472 unsigned long lost_events;
473 unsigned long last_overrun;
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -0700474 local_t entries_bytes;
Steven Rostedte4906ef2009-04-30 20:49:44 -0400475 local_t entries;
Slava Pestov884bfe82011-07-15 14:23:58 -0700476 local_t overrun;
477 local_t commit_overrun;
478 local_t dropped_events;
Steven Rostedtfa743952009-06-16 12:37:57 -0400479 local_t committing;
480 local_t commits;
Steven Rostedt77ae3652009-03-27 11:00:29 -0400481 unsigned long read;
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -0700482 unsigned long read_bytes;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400483 u64 write_stamp;
484 u64 read_stamp;
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -0800485 /* ring buffer pages to update, > 0 to add, < 0 to remove */
486 int nr_pages_to_update;
487 struct list_head new_pages; /* new pages to add */
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -0700488 struct work_struct update_pages_work;
Vaibhav Nagarnaik05fdd702012-05-18 13:29:51 -0700489 struct completion update_done;
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -0500490
491 struct rb_irq_work irq_work;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400492};
493
494struct ring_buffer {
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400495 unsigned flags;
496 int cpus;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400497 atomic_t record_disabled;
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -0700498 atomic_t resize_disabled;
Arnaldo Carvalho de Melo00f62f62009-02-09 17:04:06 -0200499 cpumask_var_t cpumask;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400500
Peter Zijlstra1f8a6a12009-06-08 18:18:39 +0200501 struct lock_class_key *reader_lock_key;
502
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400503 struct mutex mutex;
504
505 struct ring_buffer_per_cpu **buffers;
Steven Rostedt554f7862009-03-11 22:00:13 -0400506
Steven Rostedt59222ef2009-03-12 11:46:03 -0400507#ifdef CONFIG_HOTPLUG_CPU
Steven Rostedt554f7862009-03-11 22:00:13 -0400508 struct notifier_block cpu_notify;
509#endif
Steven Rostedt37886f62009-03-17 17:22:06 -0400510 u64 (*clock)(void);
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -0500511
512 struct rb_irq_work irq_work;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400513};
514
515struct ring_buffer_iter {
516 struct ring_buffer_per_cpu *cpu_buffer;
517 unsigned long head;
518 struct buffer_page *head_page;
Steven Rostedt492a74f2010-01-25 15:17:47 -0500519 struct buffer_page *cache_reader_page;
520 unsigned long cache_read;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400521 u64 read_stamp;
522};
523
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -0500524/*
525 * rb_wake_up_waiters - wake up tasks waiting for ring buffer input
526 *
527 * Schedules a delayed work to wake up any task that is blocked on the
528 * ring buffer waiters queue.
529 */
530static void rb_wake_up_waiters(struct irq_work *work)
531{
532 struct rb_irq_work *rbwork = container_of(work, struct rb_irq_work, work);
533
534 wake_up_all(&rbwork->waiters);
535}
536
537/**
538 * ring_buffer_wait - wait for input to the ring buffer
539 * @buffer: buffer to wait on
540 * @cpu: the cpu buffer to wait on
541 *
542 * If @cpu == RING_BUFFER_ALL_CPUS then the task will wake up as soon
543 * as data is added to any of the @buffer's cpu buffers. Otherwise
544 * it will wait for data to be added to a specific cpu buffer.
545 */
546void ring_buffer_wait(struct ring_buffer *buffer, int cpu)
547{
548 struct ring_buffer_per_cpu *cpu_buffer;
549 DEFINE_WAIT(wait);
550 struct rb_irq_work *work;
551
552 /*
553 * Depending on what the caller is waiting for, either any
554 * data in any cpu buffer, or a specific buffer, put the
555 * caller on the appropriate wait queue.
556 */
557 if (cpu == RING_BUFFER_ALL_CPUS)
558 work = &buffer->irq_work;
559 else {
560 cpu_buffer = buffer->buffers[cpu];
561 work = &cpu_buffer->irq_work;
562 }
563
564
565 prepare_to_wait(&work->waiters, &wait, TASK_INTERRUPTIBLE);
566
567 /*
568 * The events can happen in critical sections where
569 * checking a work queue can cause deadlocks.
570 * After adding a task to the queue, this flag is set
571 * only to notify events to try to wake up the queue
572 * using irq_work.
573 *
574 * We don't clear it even if the buffer is no longer
575 * empty. The flag only causes the next event to run
576 * irq_work to do the work queue wake up. The worse
577 * that can happen if we race with !trace_empty() is that
578 * an event will cause an irq_work to try to wake up
579 * an empty queue.
580 *
581 * There's no reason to protect this flag either, as
582 * the work queue and irq_work logic will do the necessary
583 * synchronization for the wake ups. The only thing
584 * that is necessary is that the wake up happens after
585 * a task has been queued. It's OK for spurious wake ups.
586 */
587 work->waiters_pending = true;
588
589 if ((cpu == RING_BUFFER_ALL_CPUS && ring_buffer_empty(buffer)) ||
590 (cpu != RING_BUFFER_ALL_CPUS && ring_buffer_empty_cpu(buffer, cpu)))
591 schedule();
592
593 finish_wait(&work->waiters, &wait);
594}
595
596/**
597 * ring_buffer_poll_wait - poll on buffer input
598 * @buffer: buffer to wait on
599 * @cpu: the cpu buffer to wait on
600 * @filp: the file descriptor
601 * @poll_table: The poll descriptor
602 *
603 * If @cpu == RING_BUFFER_ALL_CPUS then the task will wake up as soon
604 * as data is added to any of the @buffer's cpu buffers. Otherwise
605 * it will wait for data to be added to a specific cpu buffer.
606 *
607 * Returns POLLIN | POLLRDNORM if data exists in the buffers,
608 * zero otherwise.
609 */
610int ring_buffer_poll_wait(struct ring_buffer *buffer, int cpu,
611 struct file *filp, poll_table *poll_table)
612{
613 struct ring_buffer_per_cpu *cpu_buffer;
614 struct rb_irq_work *work;
615
616 if ((cpu == RING_BUFFER_ALL_CPUS && !ring_buffer_empty(buffer)) ||
617 (cpu != RING_BUFFER_ALL_CPUS && !ring_buffer_empty_cpu(buffer, cpu)))
618 return POLLIN | POLLRDNORM;
619
620 if (cpu == RING_BUFFER_ALL_CPUS)
621 work = &buffer->irq_work;
622 else {
623 cpu_buffer = buffer->buffers[cpu];
624 work = &cpu_buffer->irq_work;
625 }
626
627 work->waiters_pending = true;
628 poll_wait(filp, &work->waiters, poll_table);
629
630 if ((cpu == RING_BUFFER_ALL_CPUS && !ring_buffer_empty(buffer)) ||
631 (cpu != RING_BUFFER_ALL_CPUS && !ring_buffer_empty_cpu(buffer, cpu)))
632 return POLLIN | POLLRDNORM;
633 return 0;
634}
635
Steven Rostedtf536aaf2008-11-10 23:07:30 -0500636/* buffer may be either ring_buffer or ring_buffer_per_cpu */
Steven Rostedt077c5402009-09-03 19:53:46 -0400637#define RB_WARN_ON(b, cond) \
638 ({ \
639 int _____ret = unlikely(cond); \
640 if (_____ret) { \
641 if (__same_type(*(b), struct ring_buffer_per_cpu)) { \
642 struct ring_buffer_per_cpu *__b = \
643 (void *)b; \
644 atomic_inc(&__b->buffer->record_disabled); \
645 } else \
646 atomic_inc(&b->record_disabled); \
647 WARN_ON(1); \
648 } \
649 _____ret; \
Steven Rostedt3e89c7b2008-11-11 15:28:41 -0500650 })
Steven Rostedtf536aaf2008-11-10 23:07:30 -0500651
Steven Rostedt37886f62009-03-17 17:22:06 -0400652/* Up this if you want to test the TIME_EXTENTS and normalization */
653#define DEBUG_SHIFT 0
654
Jiri Olsa6d3f1e12009-10-23 19:36:19 -0400655static inline u64 rb_time_stamp(struct ring_buffer *buffer)
Steven Rostedt88eb0122009-05-11 16:28:23 -0400656{
657 /* shift to debug/test normalization and TIME_EXTENTS */
658 return buffer->clock() << DEBUG_SHIFT;
659}
660
Steven Rostedt37886f62009-03-17 17:22:06 -0400661u64 ring_buffer_time_stamp(struct ring_buffer *buffer, int cpu)
662{
663 u64 time;
664
665 preempt_disable_notrace();
Jiri Olsa6d3f1e12009-10-23 19:36:19 -0400666 time = rb_time_stamp(buffer);
Steven Rostedt37886f62009-03-17 17:22:06 -0400667 preempt_enable_no_resched_notrace();
668
669 return time;
670}
671EXPORT_SYMBOL_GPL(ring_buffer_time_stamp);
672
673void ring_buffer_normalize_time_stamp(struct ring_buffer *buffer,
674 int cpu, u64 *ts)
675{
676 /* Just stupid testing the normalize function and deltas */
677 *ts >>= DEBUG_SHIFT;
678}
679EXPORT_SYMBOL_GPL(ring_buffer_normalize_time_stamp);
680
Steven Rostedt77ae3652009-03-27 11:00:29 -0400681/*
682 * Making the ring buffer lockless makes things tricky.
683 * Although writes only happen on the CPU that they are on,
684 * and they only need to worry about interrupts. Reads can
685 * happen on any CPU.
686 *
687 * The reader page is always off the ring buffer, but when the
688 * reader finishes with a page, it needs to swap its page with
689 * a new one from the buffer. The reader needs to take from
690 * the head (writes go to the tail). But if a writer is in overwrite
691 * mode and wraps, it must push the head page forward.
692 *
693 * Here lies the problem.
694 *
695 * The reader must be careful to replace only the head page, and
696 * not another one. As described at the top of the file in the
697 * ASCII art, the reader sets its old page to point to the next
698 * page after head. It then sets the page after head to point to
699 * the old reader page. But if the writer moves the head page
700 * during this operation, the reader could end up with the tail.
701 *
702 * We use cmpxchg to help prevent this race. We also do something
703 * special with the page before head. We set the LSB to 1.
704 *
705 * When the writer must push the page forward, it will clear the
706 * bit that points to the head page, move the head, and then set
707 * the bit that points to the new head page.
708 *
709 * We also don't want an interrupt coming in and moving the head
710 * page on another writer. Thus we use the second LSB to catch
711 * that too. Thus:
712 *
713 * head->list->prev->next bit 1 bit 0
714 * ------- -------
715 * Normal page 0 0
716 * Points to head page 0 1
717 * New head page 1 0
718 *
719 * Note we can not trust the prev pointer of the head page, because:
720 *
721 * +----+ +-----+ +-----+
722 * | |------>| T |---X--->| N |
723 * | |<------| | | |
724 * +----+ +-----+ +-----+
725 * ^ ^ |
726 * | +-----+ | |
727 * +----------| R |----------+ |
728 * | |<-----------+
729 * +-----+
730 *
731 * Key: ---X--> HEAD flag set in pointer
732 * T Tail page
733 * R Reader page
734 * N Next page
735 *
736 * (see __rb_reserve_next() to see where this happens)
737 *
738 * What the above shows is that the reader just swapped out
739 * the reader page with a page in the buffer, but before it
740 * could make the new header point back to the new page added
741 * it was preempted by a writer. The writer moved forward onto
742 * the new page added by the reader and is about to move forward
743 * again.
744 *
745 * You can see, it is legitimate for the previous pointer of
746 * the head (or any page) not to point back to itself. But only
747 * temporarially.
748 */
749
750#define RB_PAGE_NORMAL 0UL
751#define RB_PAGE_HEAD 1UL
752#define RB_PAGE_UPDATE 2UL
753
754
755#define RB_FLAG_MASK 3UL
756
757/* PAGE_MOVED is not part of the mask */
758#define RB_PAGE_MOVED 4UL
759
760/*
761 * rb_list_head - remove any bit
762 */
763static struct list_head *rb_list_head(struct list_head *list)
764{
765 unsigned long val = (unsigned long)list;
766
767 return (struct list_head *)(val & ~RB_FLAG_MASK);
768}
769
770/*
Jiri Olsa6d3f1e12009-10-23 19:36:19 -0400771 * rb_is_head_page - test if the given page is the head page
Steven Rostedt77ae3652009-03-27 11:00:29 -0400772 *
773 * Because the reader may move the head_page pointer, we can
774 * not trust what the head page is (it may be pointing to
775 * the reader page). But if the next page is a header page,
776 * its flags will be non zero.
777 */
Jesper Juhl42b16b32011-01-17 00:09:38 +0100778static inline int
Steven Rostedt77ae3652009-03-27 11:00:29 -0400779rb_is_head_page(struct ring_buffer_per_cpu *cpu_buffer,
780 struct buffer_page *page, struct list_head *list)
781{
782 unsigned long val;
783
784 val = (unsigned long)list->next;
785
786 if ((val & ~RB_FLAG_MASK) != (unsigned long)&page->list)
787 return RB_PAGE_MOVED;
788
789 return val & RB_FLAG_MASK;
790}
791
792/*
793 * rb_is_reader_page
794 *
795 * The unique thing about the reader page, is that, if the
796 * writer is ever on it, the previous pointer never points
797 * back to the reader page.
798 */
799static int rb_is_reader_page(struct buffer_page *page)
800{
801 struct list_head *list = page->list.prev;
802
803 return rb_list_head(list->next) != &page->list;
804}
805
806/*
807 * rb_set_list_to_head - set a list_head to be pointing to head.
808 */
809static void rb_set_list_to_head(struct ring_buffer_per_cpu *cpu_buffer,
810 struct list_head *list)
811{
812 unsigned long *ptr;
813
814 ptr = (unsigned long *)&list->next;
815 *ptr |= RB_PAGE_HEAD;
816 *ptr &= ~RB_PAGE_UPDATE;
817}
818
819/*
820 * rb_head_page_activate - sets up head page
821 */
822static void rb_head_page_activate(struct ring_buffer_per_cpu *cpu_buffer)
823{
824 struct buffer_page *head;
825
826 head = cpu_buffer->head_page;
827 if (!head)
828 return;
829
830 /*
831 * Set the previous list pointer to have the HEAD flag.
832 */
833 rb_set_list_to_head(cpu_buffer, head->list.prev);
834}
835
836static void rb_list_head_clear(struct list_head *list)
837{
838 unsigned long *ptr = (unsigned long *)&list->next;
839
840 *ptr &= ~RB_FLAG_MASK;
841}
842
843/*
844 * rb_head_page_dactivate - clears head page ptr (for free list)
845 */
846static void
847rb_head_page_deactivate(struct ring_buffer_per_cpu *cpu_buffer)
848{
849 struct list_head *hd;
850
851 /* Go through the whole list and clear any pointers found. */
852 rb_list_head_clear(cpu_buffer->pages);
853
854 list_for_each(hd, cpu_buffer->pages)
855 rb_list_head_clear(hd);
856}
857
858static int rb_head_page_set(struct ring_buffer_per_cpu *cpu_buffer,
859 struct buffer_page *head,
860 struct buffer_page *prev,
861 int old_flag, int new_flag)
862{
863 struct list_head *list;
864 unsigned long val = (unsigned long)&head->list;
865 unsigned long ret;
866
867 list = &prev->list;
868
869 val &= ~RB_FLAG_MASK;
870
Steven Rostedt08a40812009-09-14 09:31:35 -0400871 ret = cmpxchg((unsigned long *)&list->next,
872 val | old_flag, val | new_flag);
Steven Rostedt77ae3652009-03-27 11:00:29 -0400873
874 /* check if the reader took the page */
875 if ((ret & ~RB_FLAG_MASK) != val)
876 return RB_PAGE_MOVED;
877
878 return ret & RB_FLAG_MASK;
879}
880
881static int rb_head_page_set_update(struct ring_buffer_per_cpu *cpu_buffer,
882 struct buffer_page *head,
883 struct buffer_page *prev,
884 int old_flag)
885{
886 return rb_head_page_set(cpu_buffer, head, prev,
887 old_flag, RB_PAGE_UPDATE);
888}
889
890static int rb_head_page_set_head(struct ring_buffer_per_cpu *cpu_buffer,
891 struct buffer_page *head,
892 struct buffer_page *prev,
893 int old_flag)
894{
895 return rb_head_page_set(cpu_buffer, head, prev,
896 old_flag, RB_PAGE_HEAD);
897}
898
899static int rb_head_page_set_normal(struct ring_buffer_per_cpu *cpu_buffer,
900 struct buffer_page *head,
901 struct buffer_page *prev,
902 int old_flag)
903{
904 return rb_head_page_set(cpu_buffer, head, prev,
905 old_flag, RB_PAGE_NORMAL);
906}
907
908static inline void rb_inc_page(struct ring_buffer_per_cpu *cpu_buffer,
909 struct buffer_page **bpage)
910{
911 struct list_head *p = rb_list_head((*bpage)->list.next);
912
913 *bpage = list_entry(p, struct buffer_page, list);
914}
915
916static struct buffer_page *
917rb_set_head_page(struct ring_buffer_per_cpu *cpu_buffer)
918{
919 struct buffer_page *head;
920 struct buffer_page *page;
921 struct list_head *list;
922 int i;
923
924 if (RB_WARN_ON(cpu_buffer, !cpu_buffer->head_page))
925 return NULL;
926
927 /* sanity check */
928 list = cpu_buffer->pages;
929 if (RB_WARN_ON(cpu_buffer, rb_list_head(list->prev->next) != list))
930 return NULL;
931
932 page = head = cpu_buffer->head_page;
933 /*
934 * It is possible that the writer moves the header behind
935 * where we started, and we miss in one loop.
936 * A second loop should grab the header, but we'll do
937 * three loops just because I'm paranoid.
938 */
939 for (i = 0; i < 3; i++) {
940 do {
941 if (rb_is_head_page(cpu_buffer, page, page->list.prev)) {
942 cpu_buffer->head_page = page;
943 return page;
944 }
945 rb_inc_page(cpu_buffer, &page);
946 } while (page != head);
947 }
948
949 RB_WARN_ON(cpu_buffer, 1);
950
951 return NULL;
952}
953
954static int rb_head_page_replace(struct buffer_page *old,
955 struct buffer_page *new)
956{
957 unsigned long *ptr = (unsigned long *)&old->list.prev->next;
958 unsigned long val;
959 unsigned long ret;
960
961 val = *ptr & ~RB_FLAG_MASK;
962 val |= RB_PAGE_HEAD;
963
Steven Rostedt08a40812009-09-14 09:31:35 -0400964 ret = cmpxchg(ptr, val, (unsigned long)&new->list);
Steven Rostedt77ae3652009-03-27 11:00:29 -0400965
966 return ret == val;
967}
968
969/*
970 * rb_tail_page_update - move the tail page forward
971 *
972 * Returns 1 if moved tail page, 0 if someone else did.
973 */
974static int rb_tail_page_update(struct ring_buffer_per_cpu *cpu_buffer,
975 struct buffer_page *tail_page,
976 struct buffer_page *next_page)
977{
978 struct buffer_page *old_tail;
979 unsigned long old_entries;
980 unsigned long old_write;
981 int ret = 0;
982
983 /*
984 * The tail page now needs to be moved forward.
985 *
986 * We need to reset the tail page, but without messing
987 * with possible erasing of data brought in by interrupts
988 * that have moved the tail page and are currently on it.
989 *
990 * We add a counter to the write field to denote this.
991 */
992 old_write = local_add_return(RB_WRITE_INTCNT, &next_page->write);
993 old_entries = local_add_return(RB_WRITE_INTCNT, &next_page->entries);
994
995 /*
996 * Just make sure we have seen our old_write and synchronize
997 * with any interrupts that come in.
998 */
999 barrier();
1000
1001 /*
1002 * If the tail page is still the same as what we think
1003 * it is, then it is up to us to update the tail
1004 * pointer.
1005 */
1006 if (tail_page == cpu_buffer->tail_page) {
1007 /* Zero the write counter */
1008 unsigned long val = old_write & ~RB_WRITE_MASK;
1009 unsigned long eval = old_entries & ~RB_WRITE_MASK;
1010
1011 /*
1012 * This will only succeed if an interrupt did
1013 * not come in and change it. In which case, we
1014 * do not want to modify it.
Lai Jiangshanda706d82009-07-15 16:27:30 +08001015 *
1016 * We add (void) to let the compiler know that we do not care
1017 * about the return value of these functions. We use the
1018 * cmpxchg to only update if an interrupt did not already
1019 * do it for us. If the cmpxchg fails, we don't care.
Steven Rostedt77ae3652009-03-27 11:00:29 -04001020 */
Lai Jiangshanda706d82009-07-15 16:27:30 +08001021 (void)local_cmpxchg(&next_page->write, old_write, val);
1022 (void)local_cmpxchg(&next_page->entries, old_entries, eval);
Steven Rostedt77ae3652009-03-27 11:00:29 -04001023
1024 /*
1025 * No need to worry about races with clearing out the commit.
1026 * it only can increment when a commit takes place. But that
1027 * only happens in the outer most nested commit.
1028 */
1029 local_set(&next_page->page->commit, 0);
1030
1031 old_tail = cmpxchg(&cpu_buffer->tail_page,
1032 tail_page, next_page);
1033
1034 if (old_tail == tail_page)
1035 ret = 1;
1036 }
1037
1038 return ret;
1039}
1040
1041static int rb_check_bpage(struct ring_buffer_per_cpu *cpu_buffer,
1042 struct buffer_page *bpage)
1043{
1044 unsigned long val = (unsigned long)bpage;
1045
1046 if (RB_WARN_ON(cpu_buffer, val & RB_FLAG_MASK))
1047 return 1;
1048
1049 return 0;
1050}
1051
1052/**
1053 * rb_check_list - make sure a pointer to a list has the last bits zero
1054 */
1055static int rb_check_list(struct ring_buffer_per_cpu *cpu_buffer,
1056 struct list_head *list)
1057{
1058 if (RB_WARN_ON(cpu_buffer, rb_list_head(list->prev) != list->prev))
1059 return 1;
1060 if (RB_WARN_ON(cpu_buffer, rb_list_head(list->next) != list->next))
1061 return 1;
1062 return 0;
1063}
1064
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001065/**
1066 * check_pages - integrity check of buffer pages
1067 * @cpu_buffer: CPU buffer with pages to test
1068 *
Wenji Huangc3706f02009-02-10 01:03:18 -05001069 * As a safety measure we check to make sure the data pages have not
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001070 * been corrupted.
1071 */
1072static int rb_check_pages(struct ring_buffer_per_cpu *cpu_buffer)
1073{
Steven Rostedt3adc54f2009-03-30 15:32:01 -04001074 struct list_head *head = cpu_buffer->pages;
Steven Rostedt044fa782008-12-02 23:50:03 -05001075 struct buffer_page *bpage, *tmp;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001076
Steven Rostedt308f7ee2012-05-16 19:46:32 -04001077 /* Reset the head page if it exists */
1078 if (cpu_buffer->head_page)
1079 rb_set_head_page(cpu_buffer);
1080
Steven Rostedt77ae3652009-03-27 11:00:29 -04001081 rb_head_page_deactivate(cpu_buffer);
1082
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05001083 if (RB_WARN_ON(cpu_buffer, head->next->prev != head))
1084 return -1;
1085 if (RB_WARN_ON(cpu_buffer, head->prev->next != head))
1086 return -1;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001087
Steven Rostedt77ae3652009-03-27 11:00:29 -04001088 if (rb_check_list(cpu_buffer, head))
1089 return -1;
1090
Steven Rostedt044fa782008-12-02 23:50:03 -05001091 list_for_each_entry_safe(bpage, tmp, head, list) {
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05001092 if (RB_WARN_ON(cpu_buffer,
Steven Rostedt044fa782008-12-02 23:50:03 -05001093 bpage->list.next->prev != &bpage->list))
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05001094 return -1;
1095 if (RB_WARN_ON(cpu_buffer,
Steven Rostedt044fa782008-12-02 23:50:03 -05001096 bpage->list.prev->next != &bpage->list))
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05001097 return -1;
Steven Rostedt77ae3652009-03-27 11:00:29 -04001098 if (rb_check_list(cpu_buffer, &bpage->list))
1099 return -1;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001100 }
1101
Steven Rostedt77ae3652009-03-27 11:00:29 -04001102 rb_head_page_activate(cpu_buffer);
1103
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001104 return 0;
1105}
1106
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001107static int __rb_allocate_pages(int nr_pages, struct list_head *pages, int cpu)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001108{
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001109 int i;
Steven Rostedt044fa782008-12-02 23:50:03 -05001110 struct buffer_page *bpage, *tmp;
Steven Rostedt3adc54f2009-03-30 15:32:01 -04001111
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001112 for (i = 0; i < nr_pages; i++) {
Vaibhav Nagarnaik7ea59062011-05-03 17:56:42 -07001113 struct page *page;
Vaibhav Nagarnaikd7ec4bf2011-06-07 17:01:42 -07001114 /*
1115 * __GFP_NORETRY flag makes sure that the allocation fails
1116 * gracefully without invoking oom-killer and the system is
1117 * not destabilized.
1118 */
Steven Rostedt044fa782008-12-02 23:50:03 -05001119 bpage = kzalloc_node(ALIGN(sizeof(*bpage), cache_line_size()),
Vaibhav Nagarnaikd7ec4bf2011-06-07 17:01:42 -07001120 GFP_KERNEL | __GFP_NORETRY,
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001121 cpu_to_node(cpu));
Steven Rostedt044fa782008-12-02 23:50:03 -05001122 if (!bpage)
Steven Rostedte4c2ce82008-10-01 11:14:54 -04001123 goto free_pages;
Steven Rostedt77ae3652009-03-27 11:00:29 -04001124
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001125 list_add(&bpage->list, pages);
Steven Rostedt77ae3652009-03-27 11:00:29 -04001126
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001127 page = alloc_pages_node(cpu_to_node(cpu),
Vaibhav Nagarnaikd7ec4bf2011-06-07 17:01:42 -07001128 GFP_KERNEL | __GFP_NORETRY, 0);
Vaibhav Nagarnaik7ea59062011-05-03 17:56:42 -07001129 if (!page)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001130 goto free_pages;
Vaibhav Nagarnaik7ea59062011-05-03 17:56:42 -07001131 bpage->page = page_address(page);
Steven Rostedt044fa782008-12-02 23:50:03 -05001132 rb_init_page(bpage->page);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001133 }
1134
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001135 return 0;
1136
1137free_pages:
1138 list_for_each_entry_safe(bpage, tmp, pages, list) {
1139 list_del_init(&bpage->list);
1140 free_buffer_page(bpage);
1141 }
1142
1143 return -ENOMEM;
1144}
1145
1146static int rb_allocate_pages(struct ring_buffer_per_cpu *cpu_buffer,
1147 unsigned nr_pages)
1148{
1149 LIST_HEAD(pages);
1150
1151 WARN_ON(!nr_pages);
1152
1153 if (__rb_allocate_pages(nr_pages, &pages, cpu_buffer->cpu))
1154 return -ENOMEM;
1155
Steven Rostedt3adc54f2009-03-30 15:32:01 -04001156 /*
1157 * The ring buffer page list is a circular list that does not
1158 * start and end with a list head. All page list items point to
1159 * other pages.
1160 */
1161 cpu_buffer->pages = pages.next;
1162 list_del(&pages);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001163
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001164 cpu_buffer->nr_pages = nr_pages;
1165
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001166 rb_check_pages(cpu_buffer);
1167
1168 return 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001169}
1170
1171static struct ring_buffer_per_cpu *
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001172rb_allocate_cpu_buffer(struct ring_buffer *buffer, int nr_pages, int cpu)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001173{
1174 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedt044fa782008-12-02 23:50:03 -05001175 struct buffer_page *bpage;
Vaibhav Nagarnaik7ea59062011-05-03 17:56:42 -07001176 struct page *page;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001177 int ret;
1178
1179 cpu_buffer = kzalloc_node(ALIGN(sizeof(*cpu_buffer), cache_line_size()),
1180 GFP_KERNEL, cpu_to_node(cpu));
1181 if (!cpu_buffer)
1182 return NULL;
1183
1184 cpu_buffer->cpu = cpu;
1185 cpu_buffer->buffer = buffer;
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02001186 raw_spin_lock_init(&cpu_buffer->reader_lock);
Peter Zijlstra1f8a6a12009-06-08 18:18:39 +02001187 lockdep_set_class(&cpu_buffer->reader_lock, buffer->reader_lock_key);
Thomas Gleixneredc35bd2009-12-03 12:38:57 +01001188 cpu_buffer->lock = (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001189 INIT_WORK(&cpu_buffer->update_pages_work, update_pages_handler);
Vaibhav Nagarnaik05fdd702012-05-18 13:29:51 -07001190 init_completion(&cpu_buffer->update_done);
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -05001191 init_irq_work(&cpu_buffer->irq_work.work, rb_wake_up_waiters);
Steven Rostedt (Red Hat)f1dc6722013-03-04 17:33:05 -05001192 init_waitqueue_head(&cpu_buffer->irq_work.waiters);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001193
Steven Rostedt044fa782008-12-02 23:50:03 -05001194 bpage = kzalloc_node(ALIGN(sizeof(*bpage), cache_line_size()),
Steven Rostedte4c2ce82008-10-01 11:14:54 -04001195 GFP_KERNEL, cpu_to_node(cpu));
Steven Rostedt044fa782008-12-02 23:50:03 -05001196 if (!bpage)
Steven Rostedte4c2ce82008-10-01 11:14:54 -04001197 goto fail_free_buffer;
1198
Steven Rostedt77ae3652009-03-27 11:00:29 -04001199 rb_check_bpage(cpu_buffer, bpage);
1200
Steven Rostedt044fa782008-12-02 23:50:03 -05001201 cpu_buffer->reader_page = bpage;
Vaibhav Nagarnaik7ea59062011-05-03 17:56:42 -07001202 page = alloc_pages_node(cpu_to_node(cpu), GFP_KERNEL, 0);
1203 if (!page)
Steven Rostedte4c2ce82008-10-01 11:14:54 -04001204 goto fail_free_reader;
Vaibhav Nagarnaik7ea59062011-05-03 17:56:42 -07001205 bpage->page = page_address(page);
Steven Rostedt044fa782008-12-02 23:50:03 -05001206 rb_init_page(bpage->page);
Steven Rostedte4c2ce82008-10-01 11:14:54 -04001207
Steven Rostedtd7690412008-10-01 00:29:53 -04001208 INIT_LIST_HEAD(&cpu_buffer->reader_page->list);
Vaibhav Nagarnaik44b99462012-06-22 11:50:05 -07001209 INIT_LIST_HEAD(&cpu_buffer->new_pages);
Steven Rostedtd7690412008-10-01 00:29:53 -04001210
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001211 ret = rb_allocate_pages(cpu_buffer, nr_pages);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001212 if (ret < 0)
Steven Rostedtd7690412008-10-01 00:29:53 -04001213 goto fail_free_reader;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001214
1215 cpu_buffer->head_page
Steven Rostedt3adc54f2009-03-30 15:32:01 -04001216 = list_entry(cpu_buffer->pages, struct buffer_page, list);
Steven Rostedtbf41a152008-10-04 02:00:59 -04001217 cpu_buffer->tail_page = cpu_buffer->commit_page = cpu_buffer->head_page;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001218
Steven Rostedt77ae3652009-03-27 11:00:29 -04001219 rb_head_page_activate(cpu_buffer);
1220
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001221 return cpu_buffer;
1222
Steven Rostedtd7690412008-10-01 00:29:53 -04001223 fail_free_reader:
1224 free_buffer_page(cpu_buffer->reader_page);
1225
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001226 fail_free_buffer:
1227 kfree(cpu_buffer);
1228 return NULL;
1229}
1230
1231static void rb_free_cpu_buffer(struct ring_buffer_per_cpu *cpu_buffer)
1232{
Steven Rostedt3adc54f2009-03-30 15:32:01 -04001233 struct list_head *head = cpu_buffer->pages;
Steven Rostedt044fa782008-12-02 23:50:03 -05001234 struct buffer_page *bpage, *tmp;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001235
Steven Rostedtd7690412008-10-01 00:29:53 -04001236 free_buffer_page(cpu_buffer->reader_page);
1237
Steven Rostedt77ae3652009-03-27 11:00:29 -04001238 rb_head_page_deactivate(cpu_buffer);
1239
Steven Rostedt3adc54f2009-03-30 15:32:01 -04001240 if (head) {
1241 list_for_each_entry_safe(bpage, tmp, head, list) {
1242 list_del_init(&bpage->list);
1243 free_buffer_page(bpage);
1244 }
1245 bpage = list_entry(head, struct buffer_page, list);
Steven Rostedt044fa782008-12-02 23:50:03 -05001246 free_buffer_page(bpage);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001247 }
Steven Rostedt3adc54f2009-03-30 15:32:01 -04001248
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001249 kfree(cpu_buffer);
1250}
1251
Steven Rostedt59222ef2009-03-12 11:46:03 -04001252#ifdef CONFIG_HOTPLUG_CPU
Frederic Weisbecker09c9e842009-03-21 04:33:36 +01001253static int rb_cpu_notify(struct notifier_block *self,
1254 unsigned long action, void *hcpu);
Steven Rostedt554f7862009-03-11 22:00:13 -04001255#endif
1256
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001257/**
1258 * ring_buffer_alloc - allocate a new ring_buffer
Robert Richter68814b52008-11-24 12:24:12 +01001259 * @size: the size in bytes per cpu that is needed.
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001260 * @flags: attributes to set for the ring buffer.
1261 *
1262 * Currently the only flag that is available is the RB_FL_OVERWRITE
1263 * flag. This flag means that the buffer will overwrite old data
1264 * when the buffer wraps. If this flag is not set, the buffer will
1265 * drop data when the tail hits the head.
1266 */
Peter Zijlstra1f8a6a12009-06-08 18:18:39 +02001267struct ring_buffer *__ring_buffer_alloc(unsigned long size, unsigned flags,
1268 struct lock_class_key *key)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001269{
1270 struct ring_buffer *buffer;
1271 int bsize;
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001272 int cpu, nr_pages;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001273
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001274 /* keep it in its own cache line */
1275 buffer = kzalloc(ALIGN(sizeof(*buffer), cache_line_size()),
1276 GFP_KERNEL);
1277 if (!buffer)
1278 return NULL;
1279
Rusty Russell9e01c1b2009-01-01 10:12:22 +10301280 if (!alloc_cpumask_var(&buffer->cpumask, GFP_KERNEL))
1281 goto fail_free_buffer;
1282
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001283 nr_pages = DIV_ROUND_UP(size, BUF_PAGE_SIZE);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001284 buffer->flags = flags;
Steven Rostedt37886f62009-03-17 17:22:06 -04001285 buffer->clock = trace_clock_local;
Peter Zijlstra1f8a6a12009-06-08 18:18:39 +02001286 buffer->reader_lock_key = key;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001287
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -05001288 init_irq_work(&buffer->irq_work.work, rb_wake_up_waiters);
Steven Rostedt (Red Hat)f1dc6722013-03-04 17:33:05 -05001289 init_waitqueue_head(&buffer->irq_work.waiters);
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -05001290
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001291 /* need at least two pages */
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001292 if (nr_pages < 2)
1293 nr_pages = 2;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001294
Frederic Weisbecker3bf832c2009-03-19 14:47:33 +01001295 /*
1296 * In case of non-hotplug cpu, if the ring-buffer is allocated
1297 * in early initcall, it will not be notified of secondary cpus.
1298 * In that off case, we need to allocate for all possible cpus.
1299 */
1300#ifdef CONFIG_HOTPLUG_CPU
Steven Rostedt554f7862009-03-11 22:00:13 -04001301 get_online_cpus();
1302 cpumask_copy(buffer->cpumask, cpu_online_mask);
Frederic Weisbecker3bf832c2009-03-19 14:47:33 +01001303#else
1304 cpumask_copy(buffer->cpumask, cpu_possible_mask);
1305#endif
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001306 buffer->cpus = nr_cpu_ids;
1307
1308 bsize = sizeof(void *) * nr_cpu_ids;
1309 buffer->buffers = kzalloc(ALIGN(bsize, cache_line_size()),
1310 GFP_KERNEL);
1311 if (!buffer->buffers)
Rusty Russell9e01c1b2009-01-01 10:12:22 +10301312 goto fail_free_cpumask;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001313
1314 for_each_buffer_cpu(buffer, cpu) {
1315 buffer->buffers[cpu] =
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001316 rb_allocate_cpu_buffer(buffer, nr_pages, cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001317 if (!buffer->buffers[cpu])
1318 goto fail_free_buffers;
1319 }
1320
Steven Rostedt59222ef2009-03-12 11:46:03 -04001321#ifdef CONFIG_HOTPLUG_CPU
Steven Rostedt554f7862009-03-11 22:00:13 -04001322 buffer->cpu_notify.notifier_call = rb_cpu_notify;
1323 buffer->cpu_notify.priority = 0;
1324 register_cpu_notifier(&buffer->cpu_notify);
1325#endif
1326
1327 put_online_cpus();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001328 mutex_init(&buffer->mutex);
1329
1330 return buffer;
1331
1332 fail_free_buffers:
1333 for_each_buffer_cpu(buffer, cpu) {
1334 if (buffer->buffers[cpu])
1335 rb_free_cpu_buffer(buffer->buffers[cpu]);
1336 }
1337 kfree(buffer->buffers);
1338
Rusty Russell9e01c1b2009-01-01 10:12:22 +10301339 fail_free_cpumask:
1340 free_cpumask_var(buffer->cpumask);
Steven Rostedt554f7862009-03-11 22:00:13 -04001341 put_online_cpus();
Rusty Russell9e01c1b2009-01-01 10:12:22 +10301342
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001343 fail_free_buffer:
1344 kfree(buffer);
1345 return NULL;
1346}
Peter Zijlstra1f8a6a12009-06-08 18:18:39 +02001347EXPORT_SYMBOL_GPL(__ring_buffer_alloc);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001348
1349/**
1350 * ring_buffer_free - free a ring buffer.
1351 * @buffer: the buffer to free.
1352 */
1353void
1354ring_buffer_free(struct ring_buffer *buffer)
1355{
1356 int cpu;
1357
Steven Rostedt554f7862009-03-11 22:00:13 -04001358 get_online_cpus();
1359
Steven Rostedt59222ef2009-03-12 11:46:03 -04001360#ifdef CONFIG_HOTPLUG_CPU
Steven Rostedt554f7862009-03-11 22:00:13 -04001361 unregister_cpu_notifier(&buffer->cpu_notify);
1362#endif
1363
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001364 for_each_buffer_cpu(buffer, cpu)
1365 rb_free_cpu_buffer(buffer->buffers[cpu]);
1366
Steven Rostedt554f7862009-03-11 22:00:13 -04001367 put_online_cpus();
1368
Eric Dumazetbd3f0222009-08-07 12:49:29 +02001369 kfree(buffer->buffers);
Rusty Russell9e01c1b2009-01-01 10:12:22 +10301370 free_cpumask_var(buffer->cpumask);
1371
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001372 kfree(buffer);
1373}
Robert Richterc4f50182008-12-11 16:49:22 +01001374EXPORT_SYMBOL_GPL(ring_buffer_free);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001375
Steven Rostedt37886f62009-03-17 17:22:06 -04001376void ring_buffer_set_clock(struct ring_buffer *buffer,
1377 u64 (*clock)(void))
1378{
1379 buffer->clock = clock;
1380}
1381
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001382static void rb_reset_cpu(struct ring_buffer_per_cpu *cpu_buffer);
1383
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001384static inline unsigned long rb_page_entries(struct buffer_page *bpage)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001385{
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001386 return local_read(&bpage->entries) & RB_WRITE_MASK;
1387}
1388
1389static inline unsigned long rb_page_write(struct buffer_page *bpage)
1390{
1391 return local_read(&bpage->write) & RB_WRITE_MASK;
1392}
1393
Vaibhav Nagarnaik5040b4b2012-05-03 18:59:51 -07001394static int
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001395rb_remove_pages(struct ring_buffer_per_cpu *cpu_buffer, unsigned int nr_pages)
1396{
1397 struct list_head *tail_page, *to_remove, *next_page;
1398 struct buffer_page *to_remove_page, *tmp_iter_page;
1399 struct buffer_page *last_page, *first_page;
1400 unsigned int nr_removed;
1401 unsigned long head_bit;
1402 int page_entries;
1403
1404 head_bit = 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001405
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02001406 raw_spin_lock_irq(&cpu_buffer->reader_lock);
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001407 atomic_inc(&cpu_buffer->record_disabled);
1408 /*
1409 * We don't race with the readers since we have acquired the reader
1410 * lock. We also don't race with writers after disabling recording.
1411 * This makes it easy to figure out the first and the last page to be
1412 * removed from the list. We unlink all the pages in between including
1413 * the first and last pages. This is done in a busy loop so that we
1414 * lose the least number of traces.
1415 * The pages are freed after we restart recording and unlock readers.
1416 */
1417 tail_page = &cpu_buffer->tail_page->list;
Steven Rostedt77ae3652009-03-27 11:00:29 -04001418
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001419 /*
1420 * tail page might be on reader page, we remove the next page
1421 * from the ring buffer
1422 */
1423 if (cpu_buffer->tail_page == cpu_buffer->reader_page)
1424 tail_page = rb_list_head(tail_page->next);
1425 to_remove = tail_page;
1426
1427 /* start of pages to remove */
1428 first_page = list_entry(rb_list_head(to_remove->next),
1429 struct buffer_page, list);
1430
1431 for (nr_removed = 0; nr_removed < nr_pages; nr_removed++) {
1432 to_remove = rb_list_head(to_remove)->next;
1433 head_bit |= (unsigned long)to_remove & RB_PAGE_HEAD;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001434 }
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001435
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001436 next_page = rb_list_head(to_remove)->next;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001437
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001438 /*
1439 * Now we remove all pages between tail_page and next_page.
1440 * Make sure that we have head_bit value preserved for the
1441 * next page
1442 */
1443 tail_page->next = (struct list_head *)((unsigned long)next_page |
1444 head_bit);
1445 next_page = rb_list_head(next_page);
1446 next_page->prev = tail_page;
1447
1448 /* make sure pages points to a valid page in the ring buffer */
1449 cpu_buffer->pages = next_page;
1450
1451 /* update head page */
1452 if (head_bit)
1453 cpu_buffer->head_page = list_entry(next_page,
1454 struct buffer_page, list);
1455
1456 /*
1457 * change read pointer to make sure any read iterators reset
1458 * themselves
1459 */
1460 cpu_buffer->read = 0;
1461
1462 /* pages are removed, resume tracing and then free the pages */
1463 atomic_dec(&cpu_buffer->record_disabled);
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02001464 raw_spin_unlock_irq(&cpu_buffer->reader_lock);
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001465
1466 RB_WARN_ON(cpu_buffer, list_empty(cpu_buffer->pages));
1467
1468 /* last buffer page to remove */
1469 last_page = list_entry(rb_list_head(to_remove), struct buffer_page,
1470 list);
1471 tmp_iter_page = first_page;
1472
1473 do {
1474 to_remove_page = tmp_iter_page;
1475 rb_inc_page(cpu_buffer, &tmp_iter_page);
1476
1477 /* update the counters */
1478 page_entries = rb_page_entries(to_remove_page);
1479 if (page_entries) {
1480 /*
1481 * If something was added to this page, it was full
1482 * since it is not the tail page. So we deduct the
1483 * bytes consumed in ring buffer from here.
Vaibhav Nagarnaik48fdc722012-06-29 12:31:41 -07001484 * Increment overrun to account for the lost events.
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001485 */
Vaibhav Nagarnaik48fdc722012-06-29 12:31:41 -07001486 local_add(page_entries, &cpu_buffer->overrun);
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001487 local_sub(BUF_PAGE_SIZE, &cpu_buffer->entries_bytes);
1488 }
1489
1490 /*
1491 * We have already removed references to this list item, just
1492 * free up the buffer_page and its page
1493 */
1494 free_buffer_page(to_remove_page);
1495 nr_removed--;
1496
1497 } while (to_remove_page != last_page);
1498
1499 RB_WARN_ON(cpu_buffer, nr_removed);
Vaibhav Nagarnaik5040b4b2012-05-03 18:59:51 -07001500
1501 return nr_removed == 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001502}
1503
Vaibhav Nagarnaik5040b4b2012-05-03 18:59:51 -07001504static int
1505rb_insert_pages(struct ring_buffer_per_cpu *cpu_buffer)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001506{
Vaibhav Nagarnaik5040b4b2012-05-03 18:59:51 -07001507 struct list_head *pages = &cpu_buffer->new_pages;
1508 int retries, success;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001509
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02001510 raw_spin_lock_irq(&cpu_buffer->reader_lock);
Vaibhav Nagarnaik5040b4b2012-05-03 18:59:51 -07001511 /*
1512 * We are holding the reader lock, so the reader page won't be swapped
1513 * in the ring buffer. Now we are racing with the writer trying to
1514 * move head page and the tail page.
1515 * We are going to adapt the reader page update process where:
1516 * 1. We first splice the start and end of list of new pages between
1517 * the head page and its previous page.
1518 * 2. We cmpxchg the prev_page->next to point from head page to the
1519 * start of new pages list.
1520 * 3. Finally, we update the head->prev to the end of new list.
1521 *
1522 * We will try this process 10 times, to make sure that we don't keep
1523 * spinning.
1524 */
1525 retries = 10;
1526 success = 0;
1527 while (retries--) {
1528 struct list_head *head_page, *prev_page, *r;
1529 struct list_head *last_page, *first_page;
1530 struct list_head *head_page_with_bit;
Steven Rostedt77ae3652009-03-27 11:00:29 -04001531
Vaibhav Nagarnaik5040b4b2012-05-03 18:59:51 -07001532 head_page = &rb_set_head_page(cpu_buffer)->list;
Steven Rostedt54f7be52012-11-29 22:27:22 -05001533 if (!head_page)
1534 break;
Vaibhav Nagarnaik5040b4b2012-05-03 18:59:51 -07001535 prev_page = head_page->prev;
1536
1537 first_page = pages->next;
1538 last_page = pages->prev;
1539
1540 head_page_with_bit = (struct list_head *)
1541 ((unsigned long)head_page | RB_PAGE_HEAD);
1542
1543 last_page->next = head_page_with_bit;
1544 first_page->prev = prev_page;
1545
1546 r = cmpxchg(&prev_page->next, head_page_with_bit, first_page);
1547
1548 if (r == head_page_with_bit) {
1549 /*
1550 * yay, we replaced the page pointer to our new list,
1551 * now, we just have to update to head page's prev
1552 * pointer to point to end of list
1553 */
1554 head_page->prev = last_page;
1555 success = 1;
1556 break;
1557 }
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001558 }
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001559
Vaibhav Nagarnaik5040b4b2012-05-03 18:59:51 -07001560 if (success)
1561 INIT_LIST_HEAD(pages);
1562 /*
1563 * If we weren't successful in adding in new pages, warn and stop
1564 * tracing
1565 */
1566 RB_WARN_ON(cpu_buffer, !success);
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02001567 raw_spin_unlock_irq(&cpu_buffer->reader_lock);
Vaibhav Nagarnaik5040b4b2012-05-03 18:59:51 -07001568
1569 /* free pages if they weren't inserted */
1570 if (!success) {
1571 struct buffer_page *bpage, *tmp;
1572 list_for_each_entry_safe(bpage, tmp, &cpu_buffer->new_pages,
1573 list) {
1574 list_del_init(&bpage->list);
1575 free_buffer_page(bpage);
1576 }
1577 }
1578 return success;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001579}
1580
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001581static void rb_update_pages(struct ring_buffer_per_cpu *cpu_buffer)
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001582{
Vaibhav Nagarnaik5040b4b2012-05-03 18:59:51 -07001583 int success;
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001584
Vaibhav Nagarnaik5040b4b2012-05-03 18:59:51 -07001585 if (cpu_buffer->nr_pages_to_update > 0)
1586 success = rb_insert_pages(cpu_buffer);
1587 else
1588 success = rb_remove_pages(cpu_buffer,
1589 -cpu_buffer->nr_pages_to_update);
1590
1591 if (success)
1592 cpu_buffer->nr_pages += cpu_buffer->nr_pages_to_update;
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001593}
1594
1595static void update_pages_handler(struct work_struct *work)
1596{
1597 struct ring_buffer_per_cpu *cpu_buffer = container_of(work,
1598 struct ring_buffer_per_cpu, update_pages_work);
1599 rb_update_pages(cpu_buffer);
Vaibhav Nagarnaik05fdd702012-05-18 13:29:51 -07001600 complete(&cpu_buffer->update_done);
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001601}
1602
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001603/**
1604 * ring_buffer_resize - resize the ring buffer
1605 * @buffer: the buffer to resize.
1606 * @size: the new size.
1607 *
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001608 * Minimum size is 2 * BUF_PAGE_SIZE.
1609 *
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001610 * Returns 0 on success and < 0 on failure.
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001611 */
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001612int ring_buffer_resize(struct ring_buffer *buffer, unsigned long size,
1613 int cpu_id)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001614{
1615 struct ring_buffer_per_cpu *cpu_buffer;
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001616 unsigned nr_pages;
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001617 int cpu, err = 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001618
Ingo Molnaree51a1d2008-11-13 14:58:31 +01001619 /*
1620 * Always succeed at resizing a non-existent buffer:
1621 */
1622 if (!buffer)
1623 return size;
1624
Steven Rostedt6a31e1f2012-05-23 15:35:17 -04001625 /* Make sure the requested buffer exists */
1626 if (cpu_id != RING_BUFFER_ALL_CPUS &&
1627 !cpumask_test_cpu(cpu_id, buffer->cpumask))
1628 return size;
1629
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001630 size = DIV_ROUND_UP(size, BUF_PAGE_SIZE);
1631 size *= BUF_PAGE_SIZE;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001632
1633 /* we need a minimum of two pages */
1634 if (size < BUF_PAGE_SIZE * 2)
1635 size = BUF_PAGE_SIZE * 2;
1636
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001637 nr_pages = DIV_ROUND_UP(size, BUF_PAGE_SIZE);
1638
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001639 /*
1640 * Don't succeed if resizing is disabled, as a reader might be
1641 * manipulating the ring buffer and is expecting a sane state while
1642 * this is true.
1643 */
1644 if (atomic_read(&buffer->resize_disabled))
1645 return -EBUSY;
1646
1647 /* prevent another thread from changing buffer sizes */
1648 mutex_lock(&buffer->mutex);
1649
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001650 if (cpu_id == RING_BUFFER_ALL_CPUS) {
1651 /* calculate the pages to update */
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001652 for_each_buffer_cpu(buffer, cpu) {
1653 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001654
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001655 cpu_buffer->nr_pages_to_update = nr_pages -
1656 cpu_buffer->nr_pages;
Vaibhav Nagarnaikd7ec4bf2011-06-07 17:01:42 -07001657 /*
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001658 * nothing more to do for removing pages or no update
Vaibhav Nagarnaikd7ec4bf2011-06-07 17:01:42 -07001659 */
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001660 if (cpu_buffer->nr_pages_to_update <= 0)
1661 continue;
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001662 /*
1663 * to add pages, make sure all new pages can be
1664 * allocated without receiving ENOMEM
1665 */
1666 INIT_LIST_HEAD(&cpu_buffer->new_pages);
1667 if (__rb_allocate_pages(cpu_buffer->nr_pages_to_update,
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001668 &cpu_buffer->new_pages, cpu)) {
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001669 /* not enough memory for new pages */
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001670 err = -ENOMEM;
1671 goto out_err;
1672 }
1673 }
1674
1675 get_online_cpus();
1676 /*
1677 * Fire off all the required work handlers
Vaibhav Nagarnaik05fdd702012-05-18 13:29:51 -07001678 * We can't schedule on offline CPUs, but it's not necessary
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001679 * since we can change their buffer sizes without any race.
1680 */
1681 for_each_buffer_cpu(buffer, cpu) {
1682 cpu_buffer = buffer->buffers[cpu];
Vaibhav Nagarnaik05fdd702012-05-18 13:29:51 -07001683 if (!cpu_buffer->nr_pages_to_update)
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001684 continue;
1685
Steven Rostedt (Red Hat)f5eb5582013-03-07 09:27:42 -05001686 /* The update must run on the CPU that is being updated. */
1687 preempt_disable();
1688 if (cpu == smp_processor_id() || !cpu_online(cpu)) {
1689 rb_update_pages(cpu_buffer);
1690 cpu_buffer->nr_pages_to_update = 0;
1691 } else {
1692 /*
1693 * Can not disable preemption for schedule_work_on()
1694 * on PREEMPT_RT.
1695 */
1696 preempt_enable();
Vaibhav Nagarnaik05fdd702012-05-18 13:29:51 -07001697 schedule_work_on(cpu,
1698 &cpu_buffer->update_pages_work);
Steven Rostedt (Red Hat)f5eb5582013-03-07 09:27:42 -05001699 preempt_disable();
1700 }
1701 preempt_enable();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001702 }
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001703
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001704 /* wait for all the updates to complete */
1705 for_each_buffer_cpu(buffer, cpu) {
1706 cpu_buffer = buffer->buffers[cpu];
Vaibhav Nagarnaik05fdd702012-05-18 13:29:51 -07001707 if (!cpu_buffer->nr_pages_to_update)
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001708 continue;
1709
Vaibhav Nagarnaik05fdd702012-05-18 13:29:51 -07001710 if (cpu_online(cpu))
1711 wait_for_completion(&cpu_buffer->update_done);
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001712 cpu_buffer->nr_pages_to_update = 0;
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001713 }
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001714
1715 put_online_cpus();
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001716 } else {
Vaibhav Nagarnaik8e49f412012-10-10 16:40:27 -07001717 /* Make sure this CPU has been intitialized */
1718 if (!cpumask_test_cpu(cpu_id, buffer->cpumask))
1719 goto out;
1720
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001721 cpu_buffer = buffer->buffers[cpu_id];
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001722
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001723 if (nr_pages == cpu_buffer->nr_pages)
1724 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001725
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001726 cpu_buffer->nr_pages_to_update = nr_pages -
1727 cpu_buffer->nr_pages;
1728
1729 INIT_LIST_HEAD(&cpu_buffer->new_pages);
1730 if (cpu_buffer->nr_pages_to_update > 0 &&
1731 __rb_allocate_pages(cpu_buffer->nr_pages_to_update,
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001732 &cpu_buffer->new_pages, cpu_id)) {
1733 err = -ENOMEM;
1734 goto out_err;
1735 }
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001736
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001737 get_online_cpus();
1738
Steven Rostedt (Red Hat)f5eb5582013-03-07 09:27:42 -05001739 preempt_disable();
1740 /* The update must run on the CPU that is being updated. */
1741 if (cpu_id == smp_processor_id() || !cpu_online(cpu_id))
1742 rb_update_pages(cpu_buffer);
1743 else {
1744 /*
1745 * Can not disable preemption for schedule_work_on()
1746 * on PREEMPT_RT.
1747 */
1748 preempt_enable();
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001749 schedule_work_on(cpu_id,
1750 &cpu_buffer->update_pages_work);
Vaibhav Nagarnaik05fdd702012-05-18 13:29:51 -07001751 wait_for_completion(&cpu_buffer->update_done);
Steven Rostedt (Red Hat)f5eb5582013-03-07 09:27:42 -05001752 preempt_disable();
1753 }
1754 preempt_enable();
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001755
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001756 cpu_buffer->nr_pages_to_update = 0;
Vaibhav Nagarnaik05fdd702012-05-18 13:29:51 -07001757 put_online_cpus();
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001758 }
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001759
1760 out:
Steven Rostedt659f4512012-05-14 17:02:33 -04001761 /*
1762 * The ring buffer resize can happen with the ring buffer
1763 * enabled, so that the update disturbs the tracing as little
1764 * as possible. But if the buffer is disabled, we do not need
1765 * to worry about that, and we can take the time to verify
1766 * that the buffer is not corrupt.
1767 */
1768 if (atomic_read(&buffer->record_disabled)) {
1769 atomic_inc(&buffer->record_disabled);
1770 /*
1771 * Even though the buffer was disabled, we must make sure
1772 * that it is truly disabled before calling rb_check_pages.
1773 * There could have been a race between checking
1774 * record_disable and incrementing it.
1775 */
1776 synchronize_sched();
1777 for_each_buffer_cpu(buffer, cpu) {
1778 cpu_buffer = buffer->buffers[cpu];
1779 rb_check_pages(cpu_buffer);
1780 }
1781 atomic_dec(&buffer->record_disabled);
1782 }
1783
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001784 mutex_unlock(&buffer->mutex);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001785 return size;
1786
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001787 out_err:
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001788 for_each_buffer_cpu(buffer, cpu) {
1789 struct buffer_page *bpage, *tmp;
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001790
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001791 cpu_buffer = buffer->buffers[cpu];
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001792 cpu_buffer->nr_pages_to_update = 0;
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001793
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001794 if (list_empty(&cpu_buffer->new_pages))
1795 continue;
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001796
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001797 list_for_each_entry_safe(bpage, tmp, &cpu_buffer->new_pages,
1798 list) {
1799 list_del_init(&bpage->list);
1800 free_buffer_page(bpage);
1801 }
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001802 }
Vegard Nossum641d2f62008-11-18 19:22:13 +01001803 mutex_unlock(&buffer->mutex);
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001804 return err;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001805}
Robert Richterc4f50182008-12-11 16:49:22 +01001806EXPORT_SYMBOL_GPL(ring_buffer_resize);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001807
David Sharp750912f2010-12-08 13:46:47 -08001808void ring_buffer_change_overwrite(struct ring_buffer *buffer, int val)
1809{
1810 mutex_lock(&buffer->mutex);
1811 if (val)
1812 buffer->flags |= RB_FL_OVERWRITE;
1813 else
1814 buffer->flags &= ~RB_FL_OVERWRITE;
1815 mutex_unlock(&buffer->mutex);
1816}
1817EXPORT_SYMBOL_GPL(ring_buffer_change_overwrite);
1818
Steven Rostedt8789a9e2008-12-02 15:34:07 -05001819static inline void *
Steven Rostedt044fa782008-12-02 23:50:03 -05001820__rb_data_page_index(struct buffer_data_page *bpage, unsigned index)
Steven Rostedt8789a9e2008-12-02 15:34:07 -05001821{
Steven Rostedt044fa782008-12-02 23:50:03 -05001822 return bpage->data + index;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05001823}
1824
Steven Rostedt044fa782008-12-02 23:50:03 -05001825static inline void *__rb_page_index(struct buffer_page *bpage, unsigned index)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001826{
Steven Rostedt044fa782008-12-02 23:50:03 -05001827 return bpage->page->data + index;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001828}
1829
1830static inline struct ring_buffer_event *
Steven Rostedtd7690412008-10-01 00:29:53 -04001831rb_reader_event(struct ring_buffer_per_cpu *cpu_buffer)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001832{
Steven Rostedt6f807ac2008-10-04 02:00:58 -04001833 return __rb_page_index(cpu_buffer->reader_page,
1834 cpu_buffer->reader_page->read);
1835}
1836
1837static inline struct ring_buffer_event *
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001838rb_iter_head_event(struct ring_buffer_iter *iter)
1839{
Steven Rostedt6f807ac2008-10-04 02:00:58 -04001840 return __rb_page_index(iter->head_page, iter->head);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001841}
1842
Steven Rostedtbf41a152008-10-04 02:00:59 -04001843static inline unsigned rb_page_commit(struct buffer_page *bpage)
1844{
Steven Rostedtabc9b562008-12-02 15:34:06 -05001845 return local_read(&bpage->page->commit);
Steven Rostedtbf41a152008-10-04 02:00:59 -04001846}
1847
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001848/* Size is determined by what has been committed */
Steven Rostedtbf41a152008-10-04 02:00:59 -04001849static inline unsigned rb_page_size(struct buffer_page *bpage)
1850{
1851 return rb_page_commit(bpage);
1852}
1853
1854static inline unsigned
1855rb_commit_index(struct ring_buffer_per_cpu *cpu_buffer)
1856{
1857 return rb_page_commit(cpu_buffer->commit_page);
1858}
1859
Steven Rostedtbf41a152008-10-04 02:00:59 -04001860static inline unsigned
1861rb_event_index(struct ring_buffer_event *event)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001862{
Steven Rostedtbf41a152008-10-04 02:00:59 -04001863 unsigned long addr = (unsigned long)event;
1864
Steven Rostedt22f470f2009-06-11 09:29:58 -04001865 return (addr & ~PAGE_MASK) - BUF_PAGE_HDR_SIZE;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001866}
1867
Steven Rostedt0f0c85f2009-05-11 16:08:00 -04001868static inline int
Steven Rostedtfa743952009-06-16 12:37:57 -04001869rb_event_is_commit(struct ring_buffer_per_cpu *cpu_buffer,
1870 struct ring_buffer_event *event)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001871{
Steven Rostedtbf41a152008-10-04 02:00:59 -04001872 unsigned long addr = (unsigned long)event;
1873 unsigned long index;
1874
1875 index = rb_event_index(event);
1876 addr &= PAGE_MASK;
1877
1878 return cpu_buffer->commit_page->page == (void *)addr &&
1879 rb_commit_index(cpu_buffer) == index;
1880}
1881
Andrew Morton34a148b2009-01-09 12:27:09 -08001882static void
Steven Rostedtbf41a152008-10-04 02:00:59 -04001883rb_set_commit_to_write(struct ring_buffer_per_cpu *cpu_buffer)
1884{
Steven Rostedt77ae3652009-03-27 11:00:29 -04001885 unsigned long max_count;
1886
Steven Rostedtbf41a152008-10-04 02:00:59 -04001887 /*
1888 * We only race with interrupts and NMIs on this CPU.
1889 * If we own the commit event, then we can commit
1890 * all others that interrupted us, since the interruptions
1891 * are in stack format (they finish before they come
1892 * back to us). This allows us to do a simple loop to
1893 * assign the commit to the tail.
1894 */
Steven Rostedta8ccf1d2008-12-23 11:32:24 -05001895 again:
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001896 max_count = cpu_buffer->nr_pages * 100;
Steven Rostedt77ae3652009-03-27 11:00:29 -04001897
Steven Rostedtbf41a152008-10-04 02:00:59 -04001898 while (cpu_buffer->commit_page != cpu_buffer->tail_page) {
Steven Rostedt77ae3652009-03-27 11:00:29 -04001899 if (RB_WARN_ON(cpu_buffer, !(--max_count)))
1900 return;
1901 if (RB_WARN_ON(cpu_buffer,
1902 rb_is_reader_page(cpu_buffer->tail_page)))
1903 return;
1904 local_set(&cpu_buffer->commit_page->page->commit,
1905 rb_page_write(cpu_buffer->commit_page));
Steven Rostedtbf41a152008-10-04 02:00:59 -04001906 rb_inc_page(cpu_buffer, &cpu_buffer->commit_page);
Steven Rostedtabc9b562008-12-02 15:34:06 -05001907 cpu_buffer->write_stamp =
1908 cpu_buffer->commit_page->page->time_stamp;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001909 /* add barrier to keep gcc from optimizing too much */
1910 barrier();
1911 }
1912 while (rb_commit_index(cpu_buffer) !=
1913 rb_page_write(cpu_buffer->commit_page)) {
Steven Rostedt77ae3652009-03-27 11:00:29 -04001914
1915 local_set(&cpu_buffer->commit_page->page->commit,
1916 rb_page_write(cpu_buffer->commit_page));
1917 RB_WARN_ON(cpu_buffer,
1918 local_read(&cpu_buffer->commit_page->page->commit) &
1919 ~RB_WRITE_MASK);
Steven Rostedtbf41a152008-10-04 02:00:59 -04001920 barrier();
1921 }
Steven Rostedta8ccf1d2008-12-23 11:32:24 -05001922
1923 /* again, keep gcc from optimizing */
1924 barrier();
1925
1926 /*
1927 * If an interrupt came in just after the first while loop
1928 * and pushed the tail page forward, we will be left with
1929 * a dangling commit that will never go forward.
1930 */
1931 if (unlikely(cpu_buffer->commit_page != cpu_buffer->tail_page))
1932 goto again;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001933}
1934
Steven Rostedtd7690412008-10-01 00:29:53 -04001935static void rb_reset_reader_page(struct ring_buffer_per_cpu *cpu_buffer)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001936{
Steven Rostedtabc9b562008-12-02 15:34:06 -05001937 cpu_buffer->read_stamp = cpu_buffer->reader_page->page->time_stamp;
Steven Rostedt6f807ac2008-10-04 02:00:58 -04001938 cpu_buffer->reader_page->read = 0;
Steven Rostedtd7690412008-10-01 00:29:53 -04001939}
1940
Andrew Morton34a148b2009-01-09 12:27:09 -08001941static void rb_inc_iter(struct ring_buffer_iter *iter)
Steven Rostedtd7690412008-10-01 00:29:53 -04001942{
1943 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
1944
1945 /*
1946 * The iterator could be on the reader page (it starts there).
1947 * But the head could have moved, since the reader was
1948 * found. Check for this case and assign the iterator
1949 * to the head page instead of next.
1950 */
1951 if (iter->head_page == cpu_buffer->reader_page)
Steven Rostedt77ae3652009-03-27 11:00:29 -04001952 iter->head_page = rb_set_head_page(cpu_buffer);
Steven Rostedtd7690412008-10-01 00:29:53 -04001953 else
1954 rb_inc_page(cpu_buffer, &iter->head_page);
1955
Steven Rostedtabc9b562008-12-02 15:34:06 -05001956 iter->read_stamp = iter->head_page->page->time_stamp;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001957 iter->head = 0;
1958}
1959
Steven Rostedt69d1b832010-10-07 18:18:05 -04001960/* Slow path, do not inline */
1961static noinline struct ring_buffer_event *
1962rb_add_time_stamp(struct ring_buffer_event *event, u64 delta)
1963{
1964 event->type_len = RINGBUF_TYPE_TIME_EXTEND;
1965
1966 /* Not the first event on the page? */
1967 if (rb_event_index(event)) {
1968 event->time_delta = delta & TS_MASK;
1969 event->array[0] = delta >> TS_SHIFT;
1970 } else {
1971 /* nope, just zero it */
1972 event->time_delta = 0;
1973 event->array[0] = 0;
1974 }
1975
1976 return skip_time_extend(event);
1977}
1978
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001979/**
David Sharp01e3e712012-06-07 16:46:24 -07001980 * rb_update_event - update event type and data
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001981 * @event: the even to update
1982 * @type: the type of event
1983 * @length: the size of the event field in the ring buffer
1984 *
1985 * Update the type and data fields of the event. The length
1986 * is the actual size that is written to the ring buffer,
1987 * and with this, we can determine what to place into the
1988 * data field.
1989 */
Andrew Morton34a148b2009-01-09 12:27:09 -08001990static void
Steven Rostedt69d1b832010-10-07 18:18:05 -04001991rb_update_event(struct ring_buffer_per_cpu *cpu_buffer,
1992 struct ring_buffer_event *event, unsigned length,
1993 int add_timestamp, u64 delta)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001994{
Steven Rostedt69d1b832010-10-07 18:18:05 -04001995 /* Only a commit updates the timestamp */
1996 if (unlikely(!rb_event_is_commit(cpu_buffer, event)))
1997 delta = 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001998
Steven Rostedt69d1b832010-10-07 18:18:05 -04001999 /*
2000 * If we need to add a timestamp, then we
2001 * add it to the start of the resevered space.
2002 */
2003 if (unlikely(add_timestamp)) {
2004 event = rb_add_time_stamp(event, delta);
2005 length -= RB_LEN_TIME_EXTEND;
2006 delta = 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002007 }
Steven Rostedt69d1b832010-10-07 18:18:05 -04002008
2009 event->time_delta = delta;
2010 length -= RB_EVNT_HDR_SIZE;
2011 if (length > RB_MAX_SMALL_DATA || RB_FORCE_8BYTE_ALIGNMENT) {
2012 event->type_len = 0;
2013 event->array[0] = length;
2014 } else
2015 event->type_len = DIV_ROUND_UP(length, RB_ALIGNMENT);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002016}
2017
Steven Rostedt77ae3652009-03-27 11:00:29 -04002018/*
2019 * rb_handle_head_page - writer hit the head page
2020 *
2021 * Returns: +1 to retry page
2022 * 0 to continue
2023 * -1 on error
2024 */
2025static int
2026rb_handle_head_page(struct ring_buffer_per_cpu *cpu_buffer,
2027 struct buffer_page *tail_page,
2028 struct buffer_page *next_page)
2029{
2030 struct buffer_page *new_head;
2031 int entries;
2032 int type;
2033 int ret;
2034
2035 entries = rb_page_entries(next_page);
2036
2037 /*
2038 * The hard part is here. We need to move the head
2039 * forward, and protect against both readers on
2040 * other CPUs and writers coming in via interrupts.
2041 */
2042 type = rb_head_page_set_update(cpu_buffer, next_page, tail_page,
2043 RB_PAGE_HEAD);
2044
2045 /*
2046 * type can be one of four:
2047 * NORMAL - an interrupt already moved it for us
2048 * HEAD - we are the first to get here.
2049 * UPDATE - we are the interrupt interrupting
2050 * a current move.
2051 * MOVED - a reader on another CPU moved the next
2052 * pointer to its reader page. Give up
2053 * and try again.
2054 */
2055
2056 switch (type) {
2057 case RB_PAGE_HEAD:
2058 /*
2059 * We changed the head to UPDATE, thus
2060 * it is our responsibility to update
2061 * the counters.
2062 */
2063 local_add(entries, &cpu_buffer->overrun);
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -07002064 local_sub(BUF_PAGE_SIZE, &cpu_buffer->entries_bytes);
Steven Rostedt77ae3652009-03-27 11:00:29 -04002065
2066 /*
2067 * The entries will be zeroed out when we move the
2068 * tail page.
2069 */
2070
2071 /* still more to do */
2072 break;
2073
2074 case RB_PAGE_UPDATE:
2075 /*
2076 * This is an interrupt that interrupt the
2077 * previous update. Still more to do.
2078 */
2079 break;
2080 case RB_PAGE_NORMAL:
2081 /*
2082 * An interrupt came in before the update
2083 * and processed this for us.
2084 * Nothing left to do.
2085 */
2086 return 1;
2087 case RB_PAGE_MOVED:
2088 /*
2089 * The reader is on another CPU and just did
2090 * a swap with our next_page.
2091 * Try again.
2092 */
2093 return 1;
2094 default:
2095 RB_WARN_ON(cpu_buffer, 1); /* WTF??? */
2096 return -1;
2097 }
2098
2099 /*
2100 * Now that we are here, the old head pointer is
2101 * set to UPDATE. This will keep the reader from
2102 * swapping the head page with the reader page.
2103 * The reader (on another CPU) will spin till
2104 * we are finished.
2105 *
2106 * We just need to protect against interrupts
2107 * doing the job. We will set the next pointer
2108 * to HEAD. After that, we set the old pointer
2109 * to NORMAL, but only if it was HEAD before.
2110 * otherwise we are an interrupt, and only
2111 * want the outer most commit to reset it.
2112 */
2113 new_head = next_page;
2114 rb_inc_page(cpu_buffer, &new_head);
2115
2116 ret = rb_head_page_set_head(cpu_buffer, new_head, next_page,
2117 RB_PAGE_NORMAL);
2118
2119 /*
2120 * Valid returns are:
2121 * HEAD - an interrupt came in and already set it.
2122 * NORMAL - One of two things:
2123 * 1) We really set it.
2124 * 2) A bunch of interrupts came in and moved
2125 * the page forward again.
2126 */
2127 switch (ret) {
2128 case RB_PAGE_HEAD:
2129 case RB_PAGE_NORMAL:
2130 /* OK */
2131 break;
2132 default:
2133 RB_WARN_ON(cpu_buffer, 1);
2134 return -1;
2135 }
2136
2137 /*
2138 * It is possible that an interrupt came in,
2139 * set the head up, then more interrupts came in
2140 * and moved it again. When we get back here,
2141 * the page would have been set to NORMAL but we
2142 * just set it back to HEAD.
2143 *
2144 * How do you detect this? Well, if that happened
2145 * the tail page would have moved.
2146 */
2147 if (ret == RB_PAGE_NORMAL) {
2148 /*
2149 * If the tail had moved passed next, then we need
2150 * to reset the pointer.
2151 */
2152 if (cpu_buffer->tail_page != tail_page &&
2153 cpu_buffer->tail_page != next_page)
2154 rb_head_page_set_normal(cpu_buffer, new_head,
2155 next_page,
2156 RB_PAGE_HEAD);
2157 }
2158
2159 /*
2160 * If this was the outer most commit (the one that
2161 * changed the original pointer from HEAD to UPDATE),
2162 * then it is up to us to reset it to NORMAL.
2163 */
2164 if (type == RB_PAGE_HEAD) {
2165 ret = rb_head_page_set_normal(cpu_buffer, next_page,
2166 tail_page,
2167 RB_PAGE_UPDATE);
2168 if (RB_WARN_ON(cpu_buffer,
2169 ret != RB_PAGE_UPDATE))
2170 return -1;
2171 }
2172
2173 return 0;
2174}
2175
Andrew Morton34a148b2009-01-09 12:27:09 -08002176static unsigned rb_calculate_event_length(unsigned length)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002177{
2178 struct ring_buffer_event event; /* Used only for sizeof array */
2179
2180 /* zero length can cause confusions */
2181 if (!length)
2182 length = 1;
2183
Steven Rostedt22710482010-03-18 17:54:19 -04002184 if (length > RB_MAX_SMALL_DATA || RB_FORCE_8BYTE_ALIGNMENT)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002185 length += sizeof(event.array[0]);
2186
2187 length += RB_EVNT_HDR_SIZE;
Steven Rostedt22710482010-03-18 17:54:19 -04002188 length = ALIGN(length, RB_ARCH_ALIGNMENT);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002189
2190 return length;
2191}
2192
Steven Rostedtc7b09302009-06-11 11:12:00 -04002193static inline void
2194rb_reset_tail(struct ring_buffer_per_cpu *cpu_buffer,
2195 struct buffer_page *tail_page,
2196 unsigned long tail, unsigned long length)
2197{
2198 struct ring_buffer_event *event;
2199
2200 /*
2201 * Only the event that crossed the page boundary
2202 * must fill the old tail_page with padding.
2203 */
2204 if (tail >= BUF_PAGE_SIZE) {
Steven Rostedtb3230c82010-05-21 11:55:21 -04002205 /*
2206 * If the page was filled, then we still need
2207 * to update the real_end. Reset it to zero
2208 * and the reader will ignore it.
2209 */
2210 if (tail == BUF_PAGE_SIZE)
2211 tail_page->real_end = 0;
2212
Steven Rostedtc7b09302009-06-11 11:12:00 -04002213 local_sub(length, &tail_page->write);
2214 return;
2215 }
2216
2217 event = __rb_page_index(tail_page, tail);
Linus Torvaldsb0b70652009-06-20 10:56:46 -07002218 kmemcheck_annotate_bitfield(event, bitfield);
Steven Rostedtc7b09302009-06-11 11:12:00 -04002219
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -07002220 /* account for padding bytes */
2221 local_add(BUF_PAGE_SIZE - tail, &cpu_buffer->entries_bytes);
2222
Steven Rostedtc7b09302009-06-11 11:12:00 -04002223 /*
Steven Rostedtff0ff842010-03-31 22:11:42 -04002224 * Save the original length to the meta data.
2225 * This will be used by the reader to add lost event
2226 * counter.
2227 */
2228 tail_page->real_end = tail;
2229
2230 /*
Steven Rostedtc7b09302009-06-11 11:12:00 -04002231 * If this event is bigger than the minimum size, then
2232 * we need to be careful that we don't subtract the
2233 * write counter enough to allow another writer to slip
2234 * in on this page.
2235 * We put in a discarded commit instead, to make sure
2236 * that this space is not used again.
2237 *
2238 * If we are less than the minimum size, we don't need to
2239 * worry about it.
2240 */
2241 if (tail > (BUF_PAGE_SIZE - RB_EVNT_MIN_SIZE)) {
2242 /* No room for any events */
2243
2244 /* Mark the rest of the page with padding */
2245 rb_event_set_padding(event);
2246
2247 /* Set the write back to the previous setting */
2248 local_sub(length, &tail_page->write);
2249 return;
2250 }
2251
2252 /* Put in a discarded event */
2253 event->array[0] = (BUF_PAGE_SIZE - tail) - RB_EVNT_HDR_SIZE;
2254 event->type_len = RINGBUF_TYPE_PADDING;
2255 /* time delta must be non zero */
2256 event->time_delta = 1;
Steven Rostedtc7b09302009-06-11 11:12:00 -04002257
2258 /* Set write to end of buffer */
2259 length = (tail + length) - BUF_PAGE_SIZE;
2260 local_sub(length, &tail_page->write);
2261}
Steven Rostedt6634ff22009-05-06 15:30:07 -04002262
Steven Rostedt747e94a2010-10-08 13:51:48 -04002263/*
2264 * This is the slow path, force gcc not to inline it.
2265 */
2266static noinline struct ring_buffer_event *
Steven Rostedt6634ff22009-05-06 15:30:07 -04002267rb_move_tail(struct ring_buffer_per_cpu *cpu_buffer,
2268 unsigned long length, unsigned long tail,
Steven Rostedte8bc43e2010-10-20 10:58:02 -04002269 struct buffer_page *tail_page, u64 ts)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002270{
Steven Rostedt5a50e332009-11-17 08:43:01 -05002271 struct buffer_page *commit_page = cpu_buffer->commit_page;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002272 struct ring_buffer *buffer = cpu_buffer->buffer;
Steven Rostedt77ae3652009-03-27 11:00:29 -04002273 struct buffer_page *next_page;
2274 int ret;
Steven Rostedtaa20ae82009-05-05 21:16:11 -04002275
2276 next_page = tail_page;
2277
Steven Rostedtaa20ae82009-05-05 21:16:11 -04002278 rb_inc_page(cpu_buffer, &next_page);
2279
Steven Rostedtaa20ae82009-05-05 21:16:11 -04002280 /*
2281 * If for some reason, we had an interrupt storm that made
2282 * it all the way around the buffer, bail, and warn
2283 * about it.
2284 */
2285 if (unlikely(next_page == commit_page)) {
Steven Rostedt77ae3652009-03-27 11:00:29 -04002286 local_inc(&cpu_buffer->commit_overrun);
Steven Rostedtaa20ae82009-05-05 21:16:11 -04002287 goto out_reset;
2288 }
2289
Steven Rostedt77ae3652009-03-27 11:00:29 -04002290 /*
2291 * This is where the fun begins!
2292 *
2293 * We are fighting against races between a reader that
2294 * could be on another CPU trying to swap its reader
2295 * page with the buffer head.
2296 *
2297 * We are also fighting against interrupts coming in and
2298 * moving the head or tail on us as well.
2299 *
2300 * If the next page is the head page then we have filled
2301 * the buffer, unless the commit page is still on the
2302 * reader page.
2303 */
2304 if (rb_is_head_page(cpu_buffer, next_page, &tail_page->list)) {
Steven Rostedtaa20ae82009-05-05 21:16:11 -04002305
Steven Rostedt77ae3652009-03-27 11:00:29 -04002306 /*
2307 * If the commit is not on the reader page, then
2308 * move the header page.
2309 */
2310 if (!rb_is_reader_page(cpu_buffer->commit_page)) {
2311 /*
2312 * If we are not in overwrite mode,
2313 * this is easy, just stop here.
2314 */
Slava Pestov884bfe82011-07-15 14:23:58 -07002315 if (!(buffer->flags & RB_FL_OVERWRITE)) {
2316 local_inc(&cpu_buffer->dropped_events);
Steven Rostedt77ae3652009-03-27 11:00:29 -04002317 goto out_reset;
Slava Pestov884bfe82011-07-15 14:23:58 -07002318 }
Steven Rostedtaa20ae82009-05-05 21:16:11 -04002319
Steven Rostedt77ae3652009-03-27 11:00:29 -04002320 ret = rb_handle_head_page(cpu_buffer,
2321 tail_page,
2322 next_page);
2323 if (ret < 0)
2324 goto out_reset;
2325 if (ret)
2326 goto out_again;
2327 } else {
2328 /*
2329 * We need to be careful here too. The
2330 * commit page could still be on the reader
2331 * page. We could have a small buffer, and
2332 * have filled up the buffer with events
2333 * from interrupts and such, and wrapped.
2334 *
2335 * Note, if the tail page is also the on the
2336 * reader_page, we let it move out.
2337 */
2338 if (unlikely((cpu_buffer->commit_page !=
2339 cpu_buffer->tail_page) &&
2340 (cpu_buffer->commit_page ==
2341 cpu_buffer->reader_page))) {
2342 local_inc(&cpu_buffer->commit_overrun);
2343 goto out_reset;
2344 }
Steven Rostedtaa20ae82009-05-05 21:16:11 -04002345 }
2346 }
2347
Steven Rostedt77ae3652009-03-27 11:00:29 -04002348 ret = rb_tail_page_update(cpu_buffer, tail_page, next_page);
2349 if (ret) {
2350 /*
2351 * Nested commits always have zero deltas, so
2352 * just reread the time stamp
2353 */
Steven Rostedte8bc43e2010-10-20 10:58:02 -04002354 ts = rb_time_stamp(buffer);
2355 next_page->page->time_stamp = ts;
Steven Rostedtaa20ae82009-05-05 21:16:11 -04002356 }
2357
Steven Rostedt77ae3652009-03-27 11:00:29 -04002358 out_again:
Steven Rostedtaa20ae82009-05-05 21:16:11 -04002359
Steven Rostedt77ae3652009-03-27 11:00:29 -04002360 rb_reset_tail(cpu_buffer, tail_page, tail, length);
Steven Rostedtaa20ae82009-05-05 21:16:11 -04002361
2362 /* fail and let the caller try again */
2363 return ERR_PTR(-EAGAIN);
2364
Steven Rostedt45141d42009-02-12 13:19:48 -05002365 out_reset:
Lai Jiangshan6f3b3442009-01-12 11:06:18 +08002366 /* reset write */
Steven Rostedtc7b09302009-06-11 11:12:00 -04002367 rb_reset_tail(cpu_buffer, tail_page, tail, length);
Lai Jiangshan6f3b3442009-01-12 11:06:18 +08002368
Steven Rostedtbf41a152008-10-04 02:00:59 -04002369 return NULL;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002370}
2371
Steven Rostedt6634ff22009-05-06 15:30:07 -04002372static struct ring_buffer_event *
2373__rb_reserve_next(struct ring_buffer_per_cpu *cpu_buffer,
Steven Rostedt69d1b832010-10-07 18:18:05 -04002374 unsigned long length, u64 ts,
2375 u64 delta, int add_timestamp)
Steven Rostedt6634ff22009-05-06 15:30:07 -04002376{
Steven Rostedt5a50e332009-11-17 08:43:01 -05002377 struct buffer_page *tail_page;
Steven Rostedt6634ff22009-05-06 15:30:07 -04002378 struct ring_buffer_event *event;
2379 unsigned long tail, write;
2380
Steven Rostedt69d1b832010-10-07 18:18:05 -04002381 /*
2382 * If the time delta since the last event is too big to
2383 * hold in the time field of the event, then we append a
2384 * TIME EXTEND event ahead of the data event.
2385 */
2386 if (unlikely(add_timestamp))
2387 length += RB_LEN_TIME_EXTEND;
2388
Steven Rostedt6634ff22009-05-06 15:30:07 -04002389 tail_page = cpu_buffer->tail_page;
2390 write = local_add_return(length, &tail_page->write);
Steven Rostedt77ae3652009-03-27 11:00:29 -04002391
2392 /* set write to only the index of the write */
2393 write &= RB_WRITE_MASK;
Steven Rostedt6634ff22009-05-06 15:30:07 -04002394 tail = write - length;
2395
2396 /* See if we shot pass the end of this buffer page */
Steven Rostedt747e94a2010-10-08 13:51:48 -04002397 if (unlikely(write > BUF_PAGE_SIZE))
Steven Rostedt6634ff22009-05-06 15:30:07 -04002398 return rb_move_tail(cpu_buffer, length, tail,
Steven Rostedt5a50e332009-11-17 08:43:01 -05002399 tail_page, ts);
Steven Rostedt6634ff22009-05-06 15:30:07 -04002400
2401 /* We reserved something on the buffer */
2402
Steven Rostedt6634ff22009-05-06 15:30:07 -04002403 event = __rb_page_index(tail_page, tail);
Vegard Nossum1744a212009-02-28 08:29:44 +01002404 kmemcheck_annotate_bitfield(event, bitfield);
Steven Rostedt69d1b832010-10-07 18:18:05 -04002405 rb_update_event(cpu_buffer, event, length, add_timestamp, delta);
Steven Rostedt6634ff22009-05-06 15:30:07 -04002406
Steven Rostedt69d1b832010-10-07 18:18:05 -04002407 local_inc(&tail_page->entries);
Steven Rostedt6634ff22009-05-06 15:30:07 -04002408
2409 /*
Steven Rostedtfa743952009-06-16 12:37:57 -04002410 * If this is the first commit on the page, then update
2411 * its timestamp.
Steven Rostedt6634ff22009-05-06 15:30:07 -04002412 */
Steven Rostedtfa743952009-06-16 12:37:57 -04002413 if (!tail)
Steven Rostedte8bc43e2010-10-20 10:58:02 -04002414 tail_page->page->time_stamp = ts;
Steven Rostedt6634ff22009-05-06 15:30:07 -04002415
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -07002416 /* account for these added bytes */
2417 local_add(length, &cpu_buffer->entries_bytes);
2418
Steven Rostedt6634ff22009-05-06 15:30:07 -04002419 return event;
2420}
2421
Steven Rostedtedd813bf2009-06-02 23:00:53 -04002422static inline int
2423rb_try_to_discard(struct ring_buffer_per_cpu *cpu_buffer,
2424 struct ring_buffer_event *event)
2425{
2426 unsigned long new_index, old_index;
2427 struct buffer_page *bpage;
2428 unsigned long index;
2429 unsigned long addr;
2430
2431 new_index = rb_event_index(event);
Steven Rostedt69d1b832010-10-07 18:18:05 -04002432 old_index = new_index + rb_event_ts_length(event);
Steven Rostedtedd813bf2009-06-02 23:00:53 -04002433 addr = (unsigned long)event;
2434 addr &= PAGE_MASK;
2435
2436 bpage = cpu_buffer->tail_page;
2437
2438 if (bpage->page == (void *)addr && rb_page_write(bpage) == old_index) {
Steven Rostedt77ae3652009-03-27 11:00:29 -04002439 unsigned long write_mask =
2440 local_read(&bpage->write) & ~RB_WRITE_MASK;
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -07002441 unsigned long event_length = rb_event_length(event);
Steven Rostedtedd813bf2009-06-02 23:00:53 -04002442 /*
2443 * This is on the tail page. It is possible that
2444 * a write could come in and move the tail page
2445 * and write to the next page. That is fine
2446 * because we just shorten what is on this page.
2447 */
Steven Rostedt77ae3652009-03-27 11:00:29 -04002448 old_index += write_mask;
2449 new_index += write_mask;
Steven Rostedtedd813bf2009-06-02 23:00:53 -04002450 index = local_cmpxchg(&bpage->write, old_index, new_index);
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -07002451 if (index == old_index) {
2452 /* update counters */
2453 local_sub(event_length, &cpu_buffer->entries_bytes);
Steven Rostedtedd813bf2009-06-02 23:00:53 -04002454 return 1;
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -07002455 }
Steven Rostedtedd813bf2009-06-02 23:00:53 -04002456 }
2457
2458 /* could not discard */
2459 return 0;
2460}
2461
Steven Rostedtfa743952009-06-16 12:37:57 -04002462static void rb_start_commit(struct ring_buffer_per_cpu *cpu_buffer)
2463{
2464 local_inc(&cpu_buffer->committing);
2465 local_inc(&cpu_buffer->commits);
2466}
2467
Steven Rostedtd9abde22010-10-19 13:17:08 -04002468static inline void rb_end_commit(struct ring_buffer_per_cpu *cpu_buffer)
Steven Rostedtfa743952009-06-16 12:37:57 -04002469{
2470 unsigned long commits;
2471
2472 if (RB_WARN_ON(cpu_buffer,
2473 !local_read(&cpu_buffer->committing)))
2474 return;
2475
2476 again:
2477 commits = local_read(&cpu_buffer->commits);
2478 /* synchronize with interrupts */
2479 barrier();
2480 if (local_read(&cpu_buffer->committing) == 1)
2481 rb_set_commit_to_write(cpu_buffer);
2482
2483 local_dec(&cpu_buffer->committing);
2484
2485 /* synchronize with interrupts */
2486 barrier();
2487
2488 /*
2489 * Need to account for interrupts coming in between the
2490 * updating of the commit page and the clearing of the
2491 * committing counter.
2492 */
2493 if (unlikely(local_read(&cpu_buffer->commits) != commits) &&
2494 !local_read(&cpu_buffer->committing)) {
2495 local_inc(&cpu_buffer->committing);
2496 goto again;
2497 }
2498}
2499
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002500static struct ring_buffer_event *
Steven Rostedt62f0b3e2009-09-04 14:11:34 -04002501rb_reserve_next_event(struct ring_buffer *buffer,
2502 struct ring_buffer_per_cpu *cpu_buffer,
Steven Rostedt1cd8d732009-05-11 14:08:09 -04002503 unsigned long length)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002504{
2505 struct ring_buffer_event *event;
Steven Rostedt69d1b832010-10-07 18:18:05 -04002506 u64 ts, delta;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002507 int nr_loops = 0;
Steven Rostedt69d1b832010-10-07 18:18:05 -04002508 int add_timestamp;
Steven Rostedt140ff892010-10-08 10:50:30 -04002509 u64 diff;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002510
Steven Rostedtfa743952009-06-16 12:37:57 -04002511 rb_start_commit(cpu_buffer);
2512
Steven Rostedt85bac322009-09-04 14:24:40 -04002513#ifdef CONFIG_RING_BUFFER_ALLOW_SWAP
Steven Rostedt62f0b3e2009-09-04 14:11:34 -04002514 /*
2515 * Due to the ability to swap a cpu buffer from a buffer
2516 * it is possible it was swapped before we committed.
2517 * (committing stops a swap). We check for it here and
2518 * if it happened, we have to fail the write.
2519 */
2520 barrier();
2521 if (unlikely(ACCESS_ONCE(cpu_buffer->buffer) != buffer)) {
2522 local_dec(&cpu_buffer->committing);
2523 local_dec(&cpu_buffer->commits);
2524 return NULL;
2525 }
Steven Rostedt85bac322009-09-04 14:24:40 -04002526#endif
Steven Rostedt62f0b3e2009-09-04 14:11:34 -04002527
Steven Rostedtbe957c42009-05-11 14:42:53 -04002528 length = rb_calculate_event_length(length);
Steven Rostedtbf41a152008-10-04 02:00:59 -04002529 again:
Steven Rostedt69d1b832010-10-07 18:18:05 -04002530 add_timestamp = 0;
2531 delta = 0;
2532
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002533 /*
2534 * We allow for interrupts to reenter here and do a trace.
2535 * If one does, it will cause this original code to loop
2536 * back here. Even with heavy interrupts happening, this
2537 * should only happen a few times in a row. If this happens
2538 * 1000 times in a row, there must be either an interrupt
2539 * storm or we have something buggy.
2540 * Bail!
2541 */
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05002542 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 1000))
Steven Rostedtfa743952009-06-16 12:37:57 -04002543 goto out_fail;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002544
Jiri Olsa6d3f1e12009-10-23 19:36:19 -04002545 ts = rb_time_stamp(cpu_buffer->buffer);
Steven Rostedt140ff892010-10-08 10:50:30 -04002546 diff = ts - cpu_buffer->write_stamp;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002547
Steven Rostedt140ff892010-10-08 10:50:30 -04002548 /* make sure this diff is calculated here */
2549 barrier();
Steven Rostedtbf41a152008-10-04 02:00:59 -04002550
Steven Rostedt140ff892010-10-08 10:50:30 -04002551 /* Did the write stamp get updated already? */
2552 if (likely(ts >= cpu_buffer->write_stamp)) {
Steven Rostedt168b6b12009-05-11 22:11:05 -04002553 delta = diff;
2554 if (unlikely(test_time_stamp(delta))) {
Jiri Olsa31274d72011-02-18 15:52:19 +01002555 int local_clock_stable = 1;
2556#ifdef CONFIG_HAVE_UNSTABLE_SCHED_CLOCK
2557 local_clock_stable = sched_clock_stable;
2558#endif
Steven Rostedt69d1b832010-10-07 18:18:05 -04002559 WARN_ONCE(delta > (1ULL << 59),
Jiri Olsa31274d72011-02-18 15:52:19 +01002560 KERN_WARNING "Delta way too big! %llu ts=%llu write stamp = %llu\n%s",
Steven Rostedt69d1b832010-10-07 18:18:05 -04002561 (unsigned long long)delta,
2562 (unsigned long long)ts,
Jiri Olsa31274d72011-02-18 15:52:19 +01002563 (unsigned long long)cpu_buffer->write_stamp,
2564 local_clock_stable ? "" :
2565 "If you just came from a suspend/resume,\n"
2566 "please switch to the trace global clock:\n"
2567 " echo global > /sys/kernel/debug/tracing/trace_clock\n");
Steven Rostedt69d1b832010-10-07 18:18:05 -04002568 add_timestamp = 1;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002569 }
Steven Rostedt168b6b12009-05-11 22:11:05 -04002570 }
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002571
Steven Rostedt69d1b832010-10-07 18:18:05 -04002572 event = __rb_reserve_next(cpu_buffer, length, ts,
2573 delta, add_timestamp);
Steven Rostedt168b6b12009-05-11 22:11:05 -04002574 if (unlikely(PTR_ERR(event) == -EAGAIN))
Steven Rostedtbf41a152008-10-04 02:00:59 -04002575 goto again;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002576
Steven Rostedtfa743952009-06-16 12:37:57 -04002577 if (!event)
2578 goto out_fail;
Steven Rostedtbf41a152008-10-04 02:00:59 -04002579
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002580 return event;
Steven Rostedtfa743952009-06-16 12:37:57 -04002581
2582 out_fail:
2583 rb_end_commit(cpu_buffer);
2584 return NULL;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002585}
2586
Paul Mundt1155de42009-06-25 14:30:12 +09002587#ifdef CONFIG_TRACING
2588
Steven Rostedt567cd4d2012-11-02 18:33:05 -04002589/*
2590 * The lock and unlock are done within a preempt disable section.
2591 * The current_context per_cpu variable can only be modified
2592 * by the current task between lock and unlock. But it can
2593 * be modified more than once via an interrupt. To pass this
2594 * information from the lock to the unlock without having to
2595 * access the 'in_interrupt()' functions again (which do show
2596 * a bit of overhead in something as critical as function tracing,
2597 * we use a bitmask trick.
2598 *
2599 * bit 0 = NMI context
2600 * bit 1 = IRQ context
2601 * bit 2 = SoftIRQ context
2602 * bit 3 = normal context.
2603 *
2604 * This works because this is the order of contexts that can
2605 * preempt other contexts. A SoftIRQ never preempts an IRQ
2606 * context.
2607 *
2608 * When the context is determined, the corresponding bit is
2609 * checked and set (if it was set, then a recursion of that context
2610 * happened).
2611 *
2612 * On unlock, we need to clear this bit. To do so, just subtract
2613 * 1 from the current_context and AND it to itself.
2614 *
2615 * (binary)
2616 * 101 - 1 = 100
2617 * 101 & 100 = 100 (clearing bit zero)
2618 *
2619 * 1010 - 1 = 1001
2620 * 1010 & 1001 = 1000 (clearing bit 1)
2621 *
2622 * The least significant bit can be cleared this way, and it
2623 * just so happens that it is the same bit corresponding to
2624 * the current context.
2625 */
2626static DEFINE_PER_CPU(unsigned int, current_context);
Steven Rostedt261842b2009-04-16 21:41:52 -04002627
Steven Rostedt567cd4d2012-11-02 18:33:05 -04002628static __always_inline int trace_recursive_lock(void)
Steven Rostedt261842b2009-04-16 21:41:52 -04002629{
Steven Rostedt567cd4d2012-11-02 18:33:05 -04002630 unsigned int val = this_cpu_read(current_context);
2631 int bit;
Frederic Weisbeckere057a5e2009-04-19 23:38:12 +02002632
Steven Rostedt567cd4d2012-11-02 18:33:05 -04002633 if (in_interrupt()) {
2634 if (in_nmi())
2635 bit = 0;
2636 else if (in_irq())
2637 bit = 1;
2638 else
2639 bit = 2;
2640 } else
2641 bit = 3;
Frederic Weisbeckere057a5e2009-04-19 23:38:12 +02002642
Steven Rostedt567cd4d2012-11-02 18:33:05 -04002643 if (unlikely(val & (1 << bit)))
2644 return 1;
2645
2646 val |= (1 << bit);
2647 this_cpu_write(current_context, val);
2648
2649 return 0;
Steven Rostedtd9abde22010-10-19 13:17:08 -04002650}
2651
Steven Rostedt567cd4d2012-11-02 18:33:05 -04002652static __always_inline void trace_recursive_unlock(void)
Steven Rostedtd9abde22010-10-19 13:17:08 -04002653{
Steven Rostedt567cd4d2012-11-02 18:33:05 -04002654 unsigned int val = this_cpu_read(current_context);
Steven Rostedtd9abde22010-10-19 13:17:08 -04002655
Steven Rostedt567cd4d2012-11-02 18:33:05 -04002656 val--;
2657 val &= this_cpu_read(current_context);
2658 this_cpu_write(current_context, val);
Steven Rostedt261842b2009-04-16 21:41:52 -04002659}
2660
Paul Mundt1155de42009-06-25 14:30:12 +09002661#else
2662
2663#define trace_recursive_lock() (0)
2664#define trace_recursive_unlock() do { } while (0)
2665
2666#endif
2667
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002668/**
2669 * ring_buffer_lock_reserve - reserve a part of the buffer
2670 * @buffer: the ring buffer to reserve from
2671 * @length: the length of the data to reserve (excluding event header)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002672 *
2673 * Returns a reseverd event on the ring buffer to copy directly to.
2674 * The user of this interface will need to get the body to write into
2675 * and can use the ring_buffer_event_data() interface.
2676 *
2677 * The length is the length of the data needed, not the event length
2678 * which also includes the event header.
2679 *
2680 * Must be paired with ring_buffer_unlock_commit, unless NULL is returned.
2681 * If NULL is returned, then nothing has been allocated or locked.
2682 */
2683struct ring_buffer_event *
Arnaldo Carvalho de Melo0a987752009-02-05 16:12:56 -02002684ring_buffer_lock_reserve(struct ring_buffer *buffer, unsigned long length)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002685{
2686 struct ring_buffer_per_cpu *cpu_buffer;
2687 struct ring_buffer_event *event;
Steven Rostedt5168ae52010-06-03 09:36:50 -04002688 int cpu;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002689
Steven Rostedt033601a2008-11-21 12:41:55 -05002690 if (ring_buffer_flags != RB_BUFFERS_ON)
Steven Rostedta3583242008-11-11 15:01:42 -05002691 return NULL;
2692
Steven Rostedtbf41a152008-10-04 02:00:59 -04002693 /* If we are tracing schedule, we don't want to recurse */
Steven Rostedt5168ae52010-06-03 09:36:50 -04002694 preempt_disable_notrace();
Steven Rostedtbf41a152008-10-04 02:00:59 -04002695
Lai Jiangshan52fbe9c2010-03-08 14:50:43 +08002696 if (atomic_read(&buffer->record_disabled))
2697 goto out_nocheck;
2698
Steven Rostedt261842b2009-04-16 21:41:52 -04002699 if (trace_recursive_lock())
2700 goto out_nocheck;
2701
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002702 cpu = raw_smp_processor_id();
2703
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302704 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedtd7690412008-10-01 00:29:53 -04002705 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002706
2707 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002708
2709 if (atomic_read(&cpu_buffer->record_disabled))
Steven Rostedtd7690412008-10-01 00:29:53 -04002710 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002711
Steven Rostedtbe957c42009-05-11 14:42:53 -04002712 if (length > BUF_MAX_DATA_SIZE)
Steven Rostedtbf41a152008-10-04 02:00:59 -04002713 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002714
Steven Rostedt62f0b3e2009-09-04 14:11:34 -04002715 event = rb_reserve_next_event(buffer, cpu_buffer, length);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002716 if (!event)
Steven Rostedtd7690412008-10-01 00:29:53 -04002717 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002718
2719 return event;
2720
Steven Rostedtd7690412008-10-01 00:29:53 -04002721 out:
Steven Rostedt261842b2009-04-16 21:41:52 -04002722 trace_recursive_unlock();
2723
2724 out_nocheck:
Steven Rostedt5168ae52010-06-03 09:36:50 -04002725 preempt_enable_notrace();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002726 return NULL;
2727}
Robert Richterc4f50182008-12-11 16:49:22 +01002728EXPORT_SYMBOL_GPL(ring_buffer_lock_reserve);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002729
Steven Rostedta1863c22009-09-03 10:23:58 -04002730static void
2731rb_update_write_stamp(struct ring_buffer_per_cpu *cpu_buffer,
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002732 struct ring_buffer_event *event)
2733{
Steven Rostedt69d1b832010-10-07 18:18:05 -04002734 u64 delta;
2735
Steven Rostedtfa743952009-06-16 12:37:57 -04002736 /*
2737 * The event first in the commit queue updates the
2738 * time stamp.
2739 */
Steven Rostedt69d1b832010-10-07 18:18:05 -04002740 if (rb_event_is_commit(cpu_buffer, event)) {
2741 /*
2742 * A commit event that is first on a page
2743 * updates the write timestamp with the page stamp
2744 */
2745 if (!rb_event_index(event))
2746 cpu_buffer->write_stamp =
2747 cpu_buffer->commit_page->page->time_stamp;
2748 else if (event->type_len == RINGBUF_TYPE_TIME_EXTEND) {
2749 delta = event->array[0];
2750 delta <<= TS_SHIFT;
2751 delta += event->time_delta;
2752 cpu_buffer->write_stamp += delta;
2753 } else
2754 cpu_buffer->write_stamp += event->time_delta;
2755 }
Steven Rostedta1863c22009-09-03 10:23:58 -04002756}
Steven Rostedtbf41a152008-10-04 02:00:59 -04002757
Steven Rostedta1863c22009-09-03 10:23:58 -04002758static void rb_commit(struct ring_buffer_per_cpu *cpu_buffer,
2759 struct ring_buffer_event *event)
2760{
2761 local_inc(&cpu_buffer->entries);
2762 rb_update_write_stamp(cpu_buffer, event);
Steven Rostedtfa743952009-06-16 12:37:57 -04002763 rb_end_commit(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002764}
2765
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -05002766static __always_inline void
2767rb_wakeups(struct ring_buffer *buffer, struct ring_buffer_per_cpu *cpu_buffer)
2768{
2769 if (buffer->irq_work.waiters_pending) {
2770 buffer->irq_work.waiters_pending = false;
2771 /* irq_work_queue() supplies it's own memory barriers */
2772 irq_work_queue(&buffer->irq_work.work);
2773 }
2774
2775 if (cpu_buffer->irq_work.waiters_pending) {
2776 cpu_buffer->irq_work.waiters_pending = false;
2777 /* irq_work_queue() supplies it's own memory barriers */
2778 irq_work_queue(&cpu_buffer->irq_work.work);
2779 }
2780}
2781
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002782/**
2783 * ring_buffer_unlock_commit - commit a reserved
2784 * @buffer: The buffer to commit to
2785 * @event: The event pointer to commit.
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002786 *
2787 * This commits the data to the ring buffer, and releases any locks held.
2788 *
2789 * Must be paired with ring_buffer_lock_reserve.
2790 */
2791int ring_buffer_unlock_commit(struct ring_buffer *buffer,
Arnaldo Carvalho de Melo0a987752009-02-05 16:12:56 -02002792 struct ring_buffer_event *event)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002793{
2794 struct ring_buffer_per_cpu *cpu_buffer;
2795 int cpu = raw_smp_processor_id();
2796
2797 cpu_buffer = buffer->buffers[cpu];
2798
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002799 rb_commit(cpu_buffer, event);
2800
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -05002801 rb_wakeups(buffer, cpu_buffer);
2802
Steven Rostedt261842b2009-04-16 21:41:52 -04002803 trace_recursive_unlock();
2804
Steven Rostedt5168ae52010-06-03 09:36:50 -04002805 preempt_enable_notrace();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002806
2807 return 0;
2808}
Robert Richterc4f50182008-12-11 16:49:22 +01002809EXPORT_SYMBOL_GPL(ring_buffer_unlock_commit);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002810
Frederic Weisbeckerf3b9aae2009-04-19 23:39:33 +02002811static inline void rb_event_discard(struct ring_buffer_event *event)
2812{
Steven Rostedt69d1b832010-10-07 18:18:05 -04002813 if (event->type_len == RINGBUF_TYPE_TIME_EXTEND)
2814 event = skip_time_extend(event);
2815
Lai Jiangshan334d4162009-04-24 11:27:05 +08002816 /* array[0] holds the actual length for the discarded event */
2817 event->array[0] = rb_event_data_length(event) - RB_EVNT_HDR_SIZE;
2818 event->type_len = RINGBUF_TYPE_PADDING;
Frederic Weisbeckerf3b9aae2009-04-19 23:39:33 +02002819 /* time delta must be non zero */
2820 if (!event->time_delta)
2821 event->time_delta = 1;
2822}
2823
Steven Rostedta1863c22009-09-03 10:23:58 -04002824/*
2825 * Decrement the entries to the page that an event is on.
2826 * The event does not even need to exist, only the pointer
2827 * to the page it is on. This may only be called before the commit
2828 * takes place.
2829 */
2830static inline void
2831rb_decrement_entry(struct ring_buffer_per_cpu *cpu_buffer,
2832 struct ring_buffer_event *event)
2833{
2834 unsigned long addr = (unsigned long)event;
2835 struct buffer_page *bpage = cpu_buffer->commit_page;
2836 struct buffer_page *start;
2837
2838 addr &= PAGE_MASK;
2839
2840 /* Do the likely case first */
2841 if (likely(bpage->page == (void *)addr)) {
2842 local_dec(&bpage->entries);
2843 return;
2844 }
2845
2846 /*
2847 * Because the commit page may be on the reader page we
2848 * start with the next page and check the end loop there.
2849 */
2850 rb_inc_page(cpu_buffer, &bpage);
2851 start = bpage;
2852 do {
2853 if (bpage->page == (void *)addr) {
2854 local_dec(&bpage->entries);
2855 return;
2856 }
2857 rb_inc_page(cpu_buffer, &bpage);
2858 } while (bpage != start);
2859
2860 /* commit not part of this buffer?? */
2861 RB_WARN_ON(cpu_buffer, 1);
2862}
2863
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002864/**
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002865 * ring_buffer_commit_discard - discard an event that has not been committed
2866 * @buffer: the ring buffer
2867 * @event: non committed event to discard
2868 *
Steven Rostedtdc892f72009-09-03 15:33:41 -04002869 * Sometimes an event that is in the ring buffer needs to be ignored.
2870 * This function lets the user discard an event in the ring buffer
2871 * and then that event will not be read later.
2872 *
2873 * This function only works if it is called before the the item has been
2874 * committed. It will try to free the event from the ring buffer
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002875 * if another event has not been added behind it.
2876 *
2877 * If another event has been added behind it, it will set the event
2878 * up as discarded, and perform the commit.
2879 *
2880 * If this function is called, do not call ring_buffer_unlock_commit on
2881 * the event.
2882 */
2883void ring_buffer_discard_commit(struct ring_buffer *buffer,
2884 struct ring_buffer_event *event)
2885{
2886 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002887 int cpu;
2888
2889 /* The event is discarded regardless */
Frederic Weisbeckerf3b9aae2009-04-19 23:39:33 +02002890 rb_event_discard(event);
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002891
Steven Rostedtfa743952009-06-16 12:37:57 -04002892 cpu = smp_processor_id();
2893 cpu_buffer = buffer->buffers[cpu];
2894
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002895 /*
2896 * This must only be called if the event has not been
2897 * committed yet. Thus we can assume that preemption
2898 * is still disabled.
2899 */
Steven Rostedtfa743952009-06-16 12:37:57 -04002900 RB_WARN_ON(buffer, !local_read(&cpu_buffer->committing));
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002901
Steven Rostedta1863c22009-09-03 10:23:58 -04002902 rb_decrement_entry(cpu_buffer, event);
Steven Rostedt0f2541d2009-08-05 12:02:48 -04002903 if (rb_try_to_discard(cpu_buffer, event))
Steven Rostedtedd813bf2009-06-02 23:00:53 -04002904 goto out;
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002905
2906 /*
2907 * The commit is still visible by the reader, so we
Steven Rostedta1863c22009-09-03 10:23:58 -04002908 * must still update the timestamp.
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002909 */
Steven Rostedta1863c22009-09-03 10:23:58 -04002910 rb_update_write_stamp(cpu_buffer, event);
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002911 out:
Steven Rostedtfa743952009-06-16 12:37:57 -04002912 rb_end_commit(cpu_buffer);
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002913
Frederic Weisbeckerf3b9aae2009-04-19 23:39:33 +02002914 trace_recursive_unlock();
2915
Steven Rostedt5168ae52010-06-03 09:36:50 -04002916 preempt_enable_notrace();
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002917
2918}
2919EXPORT_SYMBOL_GPL(ring_buffer_discard_commit);
2920
2921/**
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002922 * ring_buffer_write - write data to the buffer without reserving
2923 * @buffer: The ring buffer to write to.
2924 * @length: The length of the data being written (excluding the event header)
2925 * @data: The data to write to the buffer.
2926 *
2927 * This is like ring_buffer_lock_reserve and ring_buffer_unlock_commit as
2928 * one function. If you already have the data to write to the buffer, it
2929 * may be easier to simply call this function.
2930 *
2931 * Note, like ring_buffer_lock_reserve, the length is the length of the data
2932 * and not the length of the event which would hold the header.
2933 */
2934int ring_buffer_write(struct ring_buffer *buffer,
David Sharp01e3e712012-06-07 16:46:24 -07002935 unsigned long length,
2936 void *data)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002937{
2938 struct ring_buffer_per_cpu *cpu_buffer;
2939 struct ring_buffer_event *event;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002940 void *body;
2941 int ret = -EBUSY;
Steven Rostedt5168ae52010-06-03 09:36:50 -04002942 int cpu;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002943
Steven Rostedt033601a2008-11-21 12:41:55 -05002944 if (ring_buffer_flags != RB_BUFFERS_ON)
Steven Rostedta3583242008-11-11 15:01:42 -05002945 return -EBUSY;
2946
Steven Rostedt5168ae52010-06-03 09:36:50 -04002947 preempt_disable_notrace();
Steven Rostedtbf41a152008-10-04 02:00:59 -04002948
Lai Jiangshan52fbe9c2010-03-08 14:50:43 +08002949 if (atomic_read(&buffer->record_disabled))
2950 goto out;
2951
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002952 cpu = raw_smp_processor_id();
2953
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302954 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedtd7690412008-10-01 00:29:53 -04002955 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002956
2957 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002958
2959 if (atomic_read(&cpu_buffer->record_disabled))
2960 goto out;
2961
Steven Rostedtbe957c42009-05-11 14:42:53 -04002962 if (length > BUF_MAX_DATA_SIZE)
2963 goto out;
2964
Steven Rostedt62f0b3e2009-09-04 14:11:34 -04002965 event = rb_reserve_next_event(buffer, cpu_buffer, length);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002966 if (!event)
2967 goto out;
2968
2969 body = rb_event_data(event);
2970
2971 memcpy(body, data, length);
2972
2973 rb_commit(cpu_buffer, event);
2974
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -05002975 rb_wakeups(buffer, cpu_buffer);
2976
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002977 ret = 0;
2978 out:
Steven Rostedt5168ae52010-06-03 09:36:50 -04002979 preempt_enable_notrace();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002980
2981 return ret;
2982}
Robert Richterc4f50182008-12-11 16:49:22 +01002983EXPORT_SYMBOL_GPL(ring_buffer_write);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002984
Andrew Morton34a148b2009-01-09 12:27:09 -08002985static int rb_per_cpu_empty(struct ring_buffer_per_cpu *cpu_buffer)
Steven Rostedtbf41a152008-10-04 02:00:59 -04002986{
2987 struct buffer_page *reader = cpu_buffer->reader_page;
Steven Rostedt77ae3652009-03-27 11:00:29 -04002988 struct buffer_page *head = rb_set_head_page(cpu_buffer);
Steven Rostedtbf41a152008-10-04 02:00:59 -04002989 struct buffer_page *commit = cpu_buffer->commit_page;
2990
Steven Rostedt77ae3652009-03-27 11:00:29 -04002991 /* In case of error, head will be NULL */
2992 if (unlikely(!head))
2993 return 1;
2994
Steven Rostedtbf41a152008-10-04 02:00:59 -04002995 return reader->read == rb_page_commit(reader) &&
2996 (commit == reader ||
2997 (commit == head &&
2998 head->read == rb_page_commit(commit)));
2999}
3000
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003001/**
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003002 * ring_buffer_record_disable - stop all writes into the buffer
3003 * @buffer: The ring buffer to stop writes to.
3004 *
3005 * This prevents all writes to the buffer. Any attempt to write
3006 * to the buffer after this will fail and return NULL.
3007 *
3008 * The caller should call synchronize_sched() after this.
3009 */
3010void ring_buffer_record_disable(struct ring_buffer *buffer)
3011{
3012 atomic_inc(&buffer->record_disabled);
3013}
Robert Richterc4f50182008-12-11 16:49:22 +01003014EXPORT_SYMBOL_GPL(ring_buffer_record_disable);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003015
3016/**
3017 * ring_buffer_record_enable - enable writes to the buffer
3018 * @buffer: The ring buffer to enable writes
3019 *
3020 * Note, multiple disables will need the same number of enables
Adam Buchbinderc41b20e2009-12-11 16:35:39 -05003021 * to truly enable the writing (much like preempt_disable).
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003022 */
3023void ring_buffer_record_enable(struct ring_buffer *buffer)
3024{
3025 atomic_dec(&buffer->record_disabled);
3026}
Robert Richterc4f50182008-12-11 16:49:22 +01003027EXPORT_SYMBOL_GPL(ring_buffer_record_enable);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003028
3029/**
Steven Rostedt499e5472012-02-22 15:50:28 -05003030 * ring_buffer_record_off - stop all writes into the buffer
3031 * @buffer: The ring buffer to stop writes to.
3032 *
3033 * This prevents all writes to the buffer. Any attempt to write
3034 * to the buffer after this will fail and return NULL.
3035 *
3036 * This is different than ring_buffer_record_disable() as
Wang Tianhong87abb3b2012-08-02 14:02:00 +08003037 * it works like an on/off switch, where as the disable() version
Steven Rostedt499e5472012-02-22 15:50:28 -05003038 * must be paired with a enable().
3039 */
3040void ring_buffer_record_off(struct ring_buffer *buffer)
3041{
3042 unsigned int rd;
3043 unsigned int new_rd;
3044
3045 do {
3046 rd = atomic_read(&buffer->record_disabled);
3047 new_rd = rd | RB_BUFFER_OFF;
3048 } while (atomic_cmpxchg(&buffer->record_disabled, rd, new_rd) != rd);
3049}
3050EXPORT_SYMBOL_GPL(ring_buffer_record_off);
3051
3052/**
3053 * ring_buffer_record_on - restart writes into the buffer
3054 * @buffer: The ring buffer to start writes to.
3055 *
3056 * This enables all writes to the buffer that was disabled by
3057 * ring_buffer_record_off().
3058 *
3059 * This is different than ring_buffer_record_enable() as
Wang Tianhong87abb3b2012-08-02 14:02:00 +08003060 * it works like an on/off switch, where as the enable() version
Steven Rostedt499e5472012-02-22 15:50:28 -05003061 * must be paired with a disable().
3062 */
3063void ring_buffer_record_on(struct ring_buffer *buffer)
3064{
3065 unsigned int rd;
3066 unsigned int new_rd;
3067
3068 do {
3069 rd = atomic_read(&buffer->record_disabled);
3070 new_rd = rd & ~RB_BUFFER_OFF;
3071 } while (atomic_cmpxchg(&buffer->record_disabled, rd, new_rd) != rd);
3072}
3073EXPORT_SYMBOL_GPL(ring_buffer_record_on);
3074
3075/**
3076 * ring_buffer_record_is_on - return true if the ring buffer can write
3077 * @buffer: The ring buffer to see if write is enabled
3078 *
3079 * Returns true if the ring buffer is in a state that it accepts writes.
3080 */
3081int ring_buffer_record_is_on(struct ring_buffer *buffer)
3082{
3083 return !atomic_read(&buffer->record_disabled);
3084}
3085
3086/**
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003087 * ring_buffer_record_disable_cpu - stop all writes into the cpu_buffer
3088 * @buffer: The ring buffer to stop writes to.
3089 * @cpu: The CPU buffer to stop
3090 *
3091 * This prevents all writes to the buffer. Any attempt to write
3092 * to the buffer after this will fail and return NULL.
3093 *
3094 * The caller should call synchronize_sched() after this.
3095 */
3096void ring_buffer_record_disable_cpu(struct ring_buffer *buffer, int cpu)
3097{
3098 struct ring_buffer_per_cpu *cpu_buffer;
3099
Rusty Russell9e01c1b2009-01-01 10:12:22 +10303100 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04003101 return;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003102
3103 cpu_buffer = buffer->buffers[cpu];
3104 atomic_inc(&cpu_buffer->record_disabled);
3105}
Robert Richterc4f50182008-12-11 16:49:22 +01003106EXPORT_SYMBOL_GPL(ring_buffer_record_disable_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003107
3108/**
3109 * ring_buffer_record_enable_cpu - enable writes to the buffer
3110 * @buffer: The ring buffer to enable writes
3111 * @cpu: The CPU to enable.
3112 *
3113 * Note, multiple disables will need the same number of enables
Adam Buchbinderc41b20e2009-12-11 16:35:39 -05003114 * to truly enable the writing (much like preempt_disable).
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003115 */
3116void ring_buffer_record_enable_cpu(struct ring_buffer *buffer, int cpu)
3117{
3118 struct ring_buffer_per_cpu *cpu_buffer;
3119
Rusty Russell9e01c1b2009-01-01 10:12:22 +10303120 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04003121 return;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003122
3123 cpu_buffer = buffer->buffers[cpu];
3124 atomic_dec(&cpu_buffer->record_disabled);
3125}
Robert Richterc4f50182008-12-11 16:49:22 +01003126EXPORT_SYMBOL_GPL(ring_buffer_record_enable_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003127
Steven Rostedtf6195aa2010-09-01 12:23:12 -04003128/*
3129 * The total entries in the ring buffer is the running counter
3130 * of entries entered into the ring buffer, minus the sum of
3131 * the entries read from the ring buffer and the number of
3132 * entries that were overwritten.
3133 */
3134static inline unsigned long
3135rb_num_of_entries(struct ring_buffer_per_cpu *cpu_buffer)
3136{
3137 return local_read(&cpu_buffer->entries) -
3138 (local_read(&cpu_buffer->overrun) + cpu_buffer->read);
3139}
3140
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003141/**
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -07003142 * ring_buffer_oldest_event_ts - get the oldest event timestamp from the buffer
3143 * @buffer: The ring buffer
3144 * @cpu: The per CPU buffer to read from.
3145 */
Yoshihiro YUNOMAE50ecf2c2012-10-11 16:27:54 -07003146u64 ring_buffer_oldest_event_ts(struct ring_buffer *buffer, int cpu)
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -07003147{
3148 unsigned long flags;
3149 struct ring_buffer_per_cpu *cpu_buffer;
3150 struct buffer_page *bpage;
Linus Torvaldsda830e52012-12-11 18:18:58 -08003151 u64 ret = 0;
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -07003152
3153 if (!cpumask_test_cpu(cpu, buffer->cpumask))
3154 return 0;
3155
3156 cpu_buffer = buffer->buffers[cpu];
Linus Torvalds7115e3f2011-10-26 17:03:38 +02003157 raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -07003158 /*
3159 * if the tail is on reader_page, oldest time stamp is on the reader
3160 * page
3161 */
3162 if (cpu_buffer->tail_page == cpu_buffer->reader_page)
3163 bpage = cpu_buffer->reader_page;
3164 else
3165 bpage = rb_set_head_page(cpu_buffer);
Steven Rostedt54f7be52012-11-29 22:27:22 -05003166 if (bpage)
3167 ret = bpage->page->time_stamp;
Linus Torvalds7115e3f2011-10-26 17:03:38 +02003168 raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -07003169
3170 return ret;
3171}
3172EXPORT_SYMBOL_GPL(ring_buffer_oldest_event_ts);
3173
3174/**
3175 * ring_buffer_bytes_cpu - get the number of bytes consumed in a cpu buffer
3176 * @buffer: The ring buffer
3177 * @cpu: The per CPU buffer to read from.
3178 */
3179unsigned long ring_buffer_bytes_cpu(struct ring_buffer *buffer, int cpu)
3180{
3181 struct ring_buffer_per_cpu *cpu_buffer;
3182 unsigned long ret;
3183
3184 if (!cpumask_test_cpu(cpu, buffer->cpumask))
3185 return 0;
3186
3187 cpu_buffer = buffer->buffers[cpu];
3188 ret = local_read(&cpu_buffer->entries_bytes) - cpu_buffer->read_bytes;
3189
3190 return ret;
3191}
3192EXPORT_SYMBOL_GPL(ring_buffer_bytes_cpu);
3193
3194/**
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003195 * ring_buffer_entries_cpu - get the number of entries in a cpu buffer
3196 * @buffer: The ring buffer
3197 * @cpu: The per CPU buffer to get the entries from.
3198 */
3199unsigned long ring_buffer_entries_cpu(struct ring_buffer *buffer, int cpu)
3200{
3201 struct ring_buffer_per_cpu *cpu_buffer;
3202
Rusty Russell9e01c1b2009-01-01 10:12:22 +10303203 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04003204 return 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003205
3206 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt554f7862009-03-11 22:00:13 -04003207
Steven Rostedtf6195aa2010-09-01 12:23:12 -04003208 return rb_num_of_entries(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003209}
Robert Richterc4f50182008-12-11 16:49:22 +01003210EXPORT_SYMBOL_GPL(ring_buffer_entries_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003211
3212/**
Slava Pestov884bfe82011-07-15 14:23:58 -07003213 * ring_buffer_overrun_cpu - get the number of overruns caused by the ring
3214 * buffer wrapping around (only if RB_FL_OVERWRITE is on).
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003215 * @buffer: The ring buffer
3216 * @cpu: The per CPU buffer to get the number of overruns from
3217 */
3218unsigned long ring_buffer_overrun_cpu(struct ring_buffer *buffer, int cpu)
3219{
3220 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedt8aabee52009-03-12 13:13:49 -04003221 unsigned long ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003222
Rusty Russell9e01c1b2009-01-01 10:12:22 +10303223 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04003224 return 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003225
3226 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt77ae3652009-03-27 11:00:29 -04003227 ret = local_read(&cpu_buffer->overrun);
Steven Rostedt554f7862009-03-11 22:00:13 -04003228
3229 return ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003230}
Robert Richterc4f50182008-12-11 16:49:22 +01003231EXPORT_SYMBOL_GPL(ring_buffer_overrun_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003232
3233/**
Slava Pestov884bfe82011-07-15 14:23:58 -07003234 * ring_buffer_commit_overrun_cpu - get the number of overruns caused by
3235 * commits failing due to the buffer wrapping around while there are uncommitted
3236 * events, such as during an interrupt storm.
Steven Rostedtf0d2c682009-04-29 13:43:37 -04003237 * @buffer: The ring buffer
3238 * @cpu: The per CPU buffer to get the number of overruns from
3239 */
3240unsigned long
3241ring_buffer_commit_overrun_cpu(struct ring_buffer *buffer, int cpu)
3242{
3243 struct ring_buffer_per_cpu *cpu_buffer;
3244 unsigned long ret;
3245
3246 if (!cpumask_test_cpu(cpu, buffer->cpumask))
3247 return 0;
3248
3249 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt77ae3652009-03-27 11:00:29 -04003250 ret = local_read(&cpu_buffer->commit_overrun);
Steven Rostedtf0d2c682009-04-29 13:43:37 -04003251
3252 return ret;
3253}
3254EXPORT_SYMBOL_GPL(ring_buffer_commit_overrun_cpu);
3255
3256/**
Slava Pestov884bfe82011-07-15 14:23:58 -07003257 * ring_buffer_dropped_events_cpu - get the number of dropped events caused by
3258 * the ring buffer filling up (only if RB_FL_OVERWRITE is off).
3259 * @buffer: The ring buffer
3260 * @cpu: The per CPU buffer to get the number of overruns from
3261 */
3262unsigned long
3263ring_buffer_dropped_events_cpu(struct ring_buffer *buffer, int cpu)
3264{
3265 struct ring_buffer_per_cpu *cpu_buffer;
3266 unsigned long ret;
3267
3268 if (!cpumask_test_cpu(cpu, buffer->cpumask))
3269 return 0;
3270
3271 cpu_buffer = buffer->buffers[cpu];
3272 ret = local_read(&cpu_buffer->dropped_events);
3273
3274 return ret;
3275}
3276EXPORT_SYMBOL_GPL(ring_buffer_dropped_events_cpu);
3277
3278/**
Steven Rostedt (Red Hat)ad964702013-01-29 17:45:49 -05003279 * ring_buffer_read_events_cpu - get the number of events successfully read
3280 * @buffer: The ring buffer
3281 * @cpu: The per CPU buffer to get the number of events read
3282 */
3283unsigned long
3284ring_buffer_read_events_cpu(struct ring_buffer *buffer, int cpu)
3285{
3286 struct ring_buffer_per_cpu *cpu_buffer;
3287
3288 if (!cpumask_test_cpu(cpu, buffer->cpumask))
3289 return 0;
3290
3291 cpu_buffer = buffer->buffers[cpu];
3292 return cpu_buffer->read;
3293}
3294EXPORT_SYMBOL_GPL(ring_buffer_read_events_cpu);
3295
3296/**
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003297 * ring_buffer_entries - get the number of entries in a buffer
3298 * @buffer: The ring buffer
3299 *
3300 * Returns the total number of entries in the ring buffer
3301 * (all CPU entries)
3302 */
3303unsigned long ring_buffer_entries(struct ring_buffer *buffer)
3304{
3305 struct ring_buffer_per_cpu *cpu_buffer;
3306 unsigned long entries = 0;
3307 int cpu;
3308
3309 /* if you care about this being correct, lock the buffer */
3310 for_each_buffer_cpu(buffer, cpu) {
3311 cpu_buffer = buffer->buffers[cpu];
Steven Rostedtf6195aa2010-09-01 12:23:12 -04003312 entries += rb_num_of_entries(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003313 }
3314
3315 return entries;
3316}
Robert Richterc4f50182008-12-11 16:49:22 +01003317EXPORT_SYMBOL_GPL(ring_buffer_entries);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003318
3319/**
Jiri Olsa67b394f2009-10-23 19:36:18 -04003320 * ring_buffer_overruns - get the number of overruns in buffer
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003321 * @buffer: The ring buffer
3322 *
3323 * Returns the total number of overruns in the ring buffer
3324 * (all CPU entries)
3325 */
3326unsigned long ring_buffer_overruns(struct ring_buffer *buffer)
3327{
3328 struct ring_buffer_per_cpu *cpu_buffer;
3329 unsigned long overruns = 0;
3330 int cpu;
3331
3332 /* if you care about this being correct, lock the buffer */
3333 for_each_buffer_cpu(buffer, cpu) {
3334 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt77ae3652009-03-27 11:00:29 -04003335 overruns += local_read(&cpu_buffer->overrun);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003336 }
3337
3338 return overruns;
3339}
Robert Richterc4f50182008-12-11 16:49:22 +01003340EXPORT_SYMBOL_GPL(ring_buffer_overruns);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003341
Steven Rostedt642edba2008-11-12 00:01:26 -05003342static void rb_iter_reset(struct ring_buffer_iter *iter)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003343{
3344 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
3345
Steven Rostedtd7690412008-10-01 00:29:53 -04003346 /* Iterator usage is expected to have record disabled */
3347 if (list_empty(&cpu_buffer->reader_page->list)) {
Steven Rostedt77ae3652009-03-27 11:00:29 -04003348 iter->head_page = rb_set_head_page(cpu_buffer);
3349 if (unlikely(!iter->head_page))
3350 return;
3351 iter->head = iter->head_page->read;
Steven Rostedtd7690412008-10-01 00:29:53 -04003352 } else {
3353 iter->head_page = cpu_buffer->reader_page;
Steven Rostedt6f807ac2008-10-04 02:00:58 -04003354 iter->head = cpu_buffer->reader_page->read;
Steven Rostedtd7690412008-10-01 00:29:53 -04003355 }
3356 if (iter->head)
3357 iter->read_stamp = cpu_buffer->read_stamp;
3358 else
Steven Rostedtabc9b562008-12-02 15:34:06 -05003359 iter->read_stamp = iter->head_page->page->time_stamp;
Steven Rostedt492a74f2010-01-25 15:17:47 -05003360 iter->cache_reader_page = cpu_buffer->reader_page;
3361 iter->cache_read = cpu_buffer->read;
Steven Rostedt642edba2008-11-12 00:01:26 -05003362}
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003363
Steven Rostedt642edba2008-11-12 00:01:26 -05003364/**
3365 * ring_buffer_iter_reset - reset an iterator
3366 * @iter: The iterator to reset
3367 *
3368 * Resets the iterator, so that it will start from the beginning
3369 * again.
3370 */
3371void ring_buffer_iter_reset(struct ring_buffer_iter *iter)
3372{
Steven Rostedt554f7862009-03-11 22:00:13 -04003373 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedt642edba2008-11-12 00:01:26 -05003374 unsigned long flags;
3375
Steven Rostedt554f7862009-03-11 22:00:13 -04003376 if (!iter)
3377 return;
3378
3379 cpu_buffer = iter->cpu_buffer;
3380
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02003381 raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
Steven Rostedt642edba2008-11-12 00:01:26 -05003382 rb_iter_reset(iter);
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02003383 raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003384}
Robert Richterc4f50182008-12-11 16:49:22 +01003385EXPORT_SYMBOL_GPL(ring_buffer_iter_reset);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003386
3387/**
3388 * ring_buffer_iter_empty - check if an iterator has no more to read
3389 * @iter: The iterator to check
3390 */
3391int ring_buffer_iter_empty(struct ring_buffer_iter *iter)
3392{
3393 struct ring_buffer_per_cpu *cpu_buffer;
3394
3395 cpu_buffer = iter->cpu_buffer;
3396
Steven Rostedtbf41a152008-10-04 02:00:59 -04003397 return iter->head_page == cpu_buffer->commit_page &&
3398 iter->head == rb_commit_index(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003399}
Robert Richterc4f50182008-12-11 16:49:22 +01003400EXPORT_SYMBOL_GPL(ring_buffer_iter_empty);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003401
3402static void
3403rb_update_read_stamp(struct ring_buffer_per_cpu *cpu_buffer,
3404 struct ring_buffer_event *event)
3405{
3406 u64 delta;
3407
Lai Jiangshan334d4162009-04-24 11:27:05 +08003408 switch (event->type_len) {
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003409 case RINGBUF_TYPE_PADDING:
3410 return;
3411
3412 case RINGBUF_TYPE_TIME_EXTEND:
3413 delta = event->array[0];
3414 delta <<= TS_SHIFT;
3415 delta += event->time_delta;
3416 cpu_buffer->read_stamp += delta;
3417 return;
3418
3419 case RINGBUF_TYPE_TIME_STAMP:
3420 /* FIXME: not implemented */
3421 return;
3422
3423 case RINGBUF_TYPE_DATA:
3424 cpu_buffer->read_stamp += event->time_delta;
3425 return;
3426
3427 default:
3428 BUG();
3429 }
3430 return;
3431}
3432
3433static void
3434rb_update_iter_read_stamp(struct ring_buffer_iter *iter,
3435 struct ring_buffer_event *event)
3436{
3437 u64 delta;
3438
Lai Jiangshan334d4162009-04-24 11:27:05 +08003439 switch (event->type_len) {
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003440 case RINGBUF_TYPE_PADDING:
3441 return;
3442
3443 case RINGBUF_TYPE_TIME_EXTEND:
3444 delta = event->array[0];
3445 delta <<= TS_SHIFT;
3446 delta += event->time_delta;
3447 iter->read_stamp += delta;
3448 return;
3449
3450 case RINGBUF_TYPE_TIME_STAMP:
3451 /* FIXME: not implemented */
3452 return;
3453
3454 case RINGBUF_TYPE_DATA:
3455 iter->read_stamp += event->time_delta;
3456 return;
3457
3458 default:
3459 BUG();
3460 }
3461 return;
3462}
3463
Steven Rostedtd7690412008-10-01 00:29:53 -04003464static struct buffer_page *
3465rb_get_reader_page(struct ring_buffer_per_cpu *cpu_buffer)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003466{
Steven Rostedtd7690412008-10-01 00:29:53 -04003467 struct buffer_page *reader = NULL;
Steven Rostedt66a8cb92010-03-31 13:21:56 -04003468 unsigned long overwrite;
Steven Rostedtd7690412008-10-01 00:29:53 -04003469 unsigned long flags;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003470 int nr_loops = 0;
Steven Rostedt77ae3652009-03-27 11:00:29 -04003471 int ret;
Steven Rostedtd7690412008-10-01 00:29:53 -04003472
Steven Rostedt3e03fb72008-11-06 00:09:43 -05003473 local_irq_save(flags);
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01003474 arch_spin_lock(&cpu_buffer->lock);
Steven Rostedtd7690412008-10-01 00:29:53 -04003475
3476 again:
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003477 /*
3478 * This should normally only loop twice. But because the
3479 * start of the reader inserts an empty page, it causes
3480 * a case where we will loop three times. There should be no
3481 * reason to loop four times (that I know of).
3482 */
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05003483 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 3)) {
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003484 reader = NULL;
3485 goto out;
3486 }
3487
Steven Rostedtd7690412008-10-01 00:29:53 -04003488 reader = cpu_buffer->reader_page;
3489
3490 /* If there's more to read, return this page */
Steven Rostedtbf41a152008-10-04 02:00:59 -04003491 if (cpu_buffer->reader_page->read < rb_page_size(reader))
Steven Rostedtd7690412008-10-01 00:29:53 -04003492 goto out;
3493
3494 /* Never should we have an index greater than the size */
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05003495 if (RB_WARN_ON(cpu_buffer,
3496 cpu_buffer->reader_page->read > rb_page_size(reader)))
3497 goto out;
Steven Rostedtd7690412008-10-01 00:29:53 -04003498
3499 /* check if we caught up to the tail */
3500 reader = NULL;
Steven Rostedtbf41a152008-10-04 02:00:59 -04003501 if (cpu_buffer->commit_page == cpu_buffer->reader_page)
Steven Rostedtd7690412008-10-01 00:29:53 -04003502 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003503
Steven Rostedta5fb8332012-06-28 13:35:04 -04003504 /* Don't bother swapping if the ring buffer is empty */
3505 if (rb_num_of_entries(cpu_buffer) == 0)
3506 goto out;
3507
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003508 /*
Steven Rostedtd7690412008-10-01 00:29:53 -04003509 * Reset the reader page to size zero.
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003510 */
Steven Rostedt77ae3652009-03-27 11:00:29 -04003511 local_set(&cpu_buffer->reader_page->write, 0);
3512 local_set(&cpu_buffer->reader_page->entries, 0);
3513 local_set(&cpu_buffer->reader_page->page->commit, 0);
Steven Rostedtff0ff842010-03-31 22:11:42 -04003514 cpu_buffer->reader_page->real_end = 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003515
Steven Rostedt77ae3652009-03-27 11:00:29 -04003516 spin:
3517 /*
3518 * Splice the empty reader page into the list around the head.
3519 */
3520 reader = rb_set_head_page(cpu_buffer);
Steven Rostedt54f7be52012-11-29 22:27:22 -05003521 if (!reader)
3522 goto out;
Steven Rostedt0e1ff5d2010-01-06 20:40:44 -05003523 cpu_buffer->reader_page->list.next = rb_list_head(reader->list.next);
Steven Rostedtd7690412008-10-01 00:29:53 -04003524 cpu_buffer->reader_page->list.prev = reader->list.prev;
Steven Rostedtbf41a152008-10-04 02:00:59 -04003525
Steven Rostedt3adc54f2009-03-30 15:32:01 -04003526 /*
3527 * cpu_buffer->pages just needs to point to the buffer, it
3528 * has no specific buffer page to point to. Lets move it out
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003529 * of our way so we don't accidentally swap it.
Steven Rostedt3adc54f2009-03-30 15:32:01 -04003530 */
3531 cpu_buffer->pages = reader->list.prev;
3532
Steven Rostedt77ae3652009-03-27 11:00:29 -04003533 /* The reader page will be pointing to the new head */
3534 rb_set_list_to_head(cpu_buffer, &cpu_buffer->reader_page->list);
Steven Rostedtd7690412008-10-01 00:29:53 -04003535
3536 /*
Steven Rostedt66a8cb92010-03-31 13:21:56 -04003537 * We want to make sure we read the overruns after we set up our
3538 * pointers to the next object. The writer side does a
3539 * cmpxchg to cross pages which acts as the mb on the writer
3540 * side. Note, the reader will constantly fail the swap
3541 * while the writer is updating the pointers, so this
3542 * guarantees that the overwrite recorded here is the one we
3543 * want to compare with the last_overrun.
3544 */
3545 smp_mb();
3546 overwrite = local_read(&(cpu_buffer->overrun));
3547
3548 /*
Steven Rostedt77ae3652009-03-27 11:00:29 -04003549 * Here's the tricky part.
3550 *
3551 * We need to move the pointer past the header page.
3552 * But we can only do that if a writer is not currently
3553 * moving it. The page before the header page has the
3554 * flag bit '1' set if it is pointing to the page we want.
3555 * but if the writer is in the process of moving it
3556 * than it will be '2' or already moved '0'.
Steven Rostedtd7690412008-10-01 00:29:53 -04003557 */
Steven Rostedtd7690412008-10-01 00:29:53 -04003558
Steven Rostedt77ae3652009-03-27 11:00:29 -04003559 ret = rb_head_page_replace(reader, cpu_buffer->reader_page);
3560
3561 /*
3562 * If we did not convert it, then we must try again.
3563 */
3564 if (!ret)
3565 goto spin;
3566
3567 /*
3568 * Yeah! We succeeded in replacing the page.
3569 *
3570 * Now make the new head point back to the reader page.
3571 */
David Sharp5ded3dc2010-01-06 17:12:07 -08003572 rb_list_head(reader->list.next)->prev = &cpu_buffer->reader_page->list;
Steven Rostedt77ae3652009-03-27 11:00:29 -04003573 rb_inc_page(cpu_buffer, &cpu_buffer->head_page);
Steven Rostedtd7690412008-10-01 00:29:53 -04003574
3575 /* Finally update the reader page to the new head */
3576 cpu_buffer->reader_page = reader;
3577 rb_reset_reader_page(cpu_buffer);
3578
Steven Rostedt66a8cb92010-03-31 13:21:56 -04003579 if (overwrite != cpu_buffer->last_overrun) {
3580 cpu_buffer->lost_events = overwrite - cpu_buffer->last_overrun;
3581 cpu_buffer->last_overrun = overwrite;
3582 }
3583
Steven Rostedtd7690412008-10-01 00:29:53 -04003584 goto again;
3585
3586 out:
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01003587 arch_spin_unlock(&cpu_buffer->lock);
Steven Rostedt3e03fb72008-11-06 00:09:43 -05003588 local_irq_restore(flags);
Steven Rostedtd7690412008-10-01 00:29:53 -04003589
3590 return reader;
3591}
3592
3593static void rb_advance_reader(struct ring_buffer_per_cpu *cpu_buffer)
3594{
3595 struct ring_buffer_event *event;
3596 struct buffer_page *reader;
3597 unsigned length;
3598
3599 reader = rb_get_reader_page(cpu_buffer);
3600
3601 /* This function should not be called when buffer is empty */
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05003602 if (RB_WARN_ON(cpu_buffer, !reader))
3603 return;
Steven Rostedtd7690412008-10-01 00:29:53 -04003604
3605 event = rb_reader_event(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003606
Steven Rostedta1863c22009-09-03 10:23:58 -04003607 if (event->type_len <= RINGBUF_TYPE_DATA_TYPE_LEN_MAX)
Steven Rostedte4906ef2009-04-30 20:49:44 -04003608 cpu_buffer->read++;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003609
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003610 rb_update_read_stamp(cpu_buffer, event);
3611
Steven Rostedtd7690412008-10-01 00:29:53 -04003612 length = rb_event_length(event);
Steven Rostedt6f807ac2008-10-04 02:00:58 -04003613 cpu_buffer->reader_page->read += length;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003614}
3615
3616static void rb_advance_iter(struct ring_buffer_iter *iter)
3617{
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003618 struct ring_buffer_per_cpu *cpu_buffer;
3619 struct ring_buffer_event *event;
3620 unsigned length;
3621
3622 cpu_buffer = iter->cpu_buffer;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003623
3624 /*
3625 * Check if we are at the end of the buffer.
3626 */
Steven Rostedtbf41a152008-10-04 02:00:59 -04003627 if (iter->head >= rb_page_size(iter->head_page)) {
Steven Rostedtea05b572009-06-03 09:30:10 -04003628 /* discarded commits can make the page empty */
3629 if (iter->head_page == cpu_buffer->commit_page)
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05003630 return;
Steven Rostedtd7690412008-10-01 00:29:53 -04003631 rb_inc_iter(iter);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003632 return;
3633 }
3634
3635 event = rb_iter_head_event(iter);
3636
3637 length = rb_event_length(event);
3638
3639 /*
3640 * This should not be called to advance the header if we are
3641 * at the tail of the buffer.
3642 */
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05003643 if (RB_WARN_ON(cpu_buffer,
Steven Rostedtf536aaf2008-11-10 23:07:30 -05003644 (iter->head_page == cpu_buffer->commit_page) &&
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05003645 (iter->head + length > rb_commit_index(cpu_buffer))))
3646 return;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003647
3648 rb_update_iter_read_stamp(iter, event);
3649
3650 iter->head += length;
3651
3652 /* check for end of page padding */
Steven Rostedtbf41a152008-10-04 02:00:59 -04003653 if ((iter->head >= rb_page_size(iter->head_page)) &&
3654 (iter->head_page != cpu_buffer->commit_page))
Steven Rostedt771e0382012-11-30 10:41:57 -05003655 rb_inc_iter(iter);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003656}
3657
Steven Rostedt66a8cb92010-03-31 13:21:56 -04003658static int rb_lost_events(struct ring_buffer_per_cpu *cpu_buffer)
3659{
3660 return cpu_buffer->lost_events;
3661}
3662
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003663static struct ring_buffer_event *
Steven Rostedt66a8cb92010-03-31 13:21:56 -04003664rb_buffer_peek(struct ring_buffer_per_cpu *cpu_buffer, u64 *ts,
3665 unsigned long *lost_events)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003666{
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003667 struct ring_buffer_event *event;
Steven Rostedtd7690412008-10-01 00:29:53 -04003668 struct buffer_page *reader;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003669 int nr_loops = 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003670
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003671 again:
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003672 /*
Steven Rostedt69d1b832010-10-07 18:18:05 -04003673 * We repeat when a time extend is encountered.
3674 * Since the time extend is always attached to a data event,
3675 * we should never loop more than once.
3676 * (We never hit the following condition more than twice).
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003677 */
Steven Rostedt69d1b832010-10-07 18:18:05 -04003678 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 2))
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003679 return NULL;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003680
Steven Rostedtd7690412008-10-01 00:29:53 -04003681 reader = rb_get_reader_page(cpu_buffer);
3682 if (!reader)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003683 return NULL;
3684
Steven Rostedtd7690412008-10-01 00:29:53 -04003685 event = rb_reader_event(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003686
Lai Jiangshan334d4162009-04-24 11:27:05 +08003687 switch (event->type_len) {
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003688 case RINGBUF_TYPE_PADDING:
Tom Zanussi2d622712009-03-22 03:30:49 -05003689 if (rb_null_event(event))
3690 RB_WARN_ON(cpu_buffer, 1);
3691 /*
3692 * Because the writer could be discarding every
3693 * event it creates (which would probably be bad)
3694 * if we were to go back to "again" then we may never
3695 * catch up, and will trigger the warn on, or lock
3696 * the box. Return the padding, and we will release
3697 * the current locks, and try again.
3698 */
Tom Zanussi2d622712009-03-22 03:30:49 -05003699 return event;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003700
3701 case RINGBUF_TYPE_TIME_EXTEND:
3702 /* Internal data, OK to advance */
Steven Rostedtd7690412008-10-01 00:29:53 -04003703 rb_advance_reader(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003704 goto again;
3705
3706 case RINGBUF_TYPE_TIME_STAMP:
3707 /* FIXME: not implemented */
Steven Rostedtd7690412008-10-01 00:29:53 -04003708 rb_advance_reader(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003709 goto again;
3710
3711 case RINGBUF_TYPE_DATA:
3712 if (ts) {
3713 *ts = cpu_buffer->read_stamp + event->time_delta;
Robert Richterd8eeb2d2009-07-31 14:58:04 +02003714 ring_buffer_normalize_time_stamp(cpu_buffer->buffer,
Steven Rostedt37886f62009-03-17 17:22:06 -04003715 cpu_buffer->cpu, ts);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003716 }
Steven Rostedt66a8cb92010-03-31 13:21:56 -04003717 if (lost_events)
3718 *lost_events = rb_lost_events(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003719 return event;
3720
3721 default:
3722 BUG();
3723 }
3724
3725 return NULL;
3726}
Robert Richterc4f50182008-12-11 16:49:22 +01003727EXPORT_SYMBOL_GPL(ring_buffer_peek);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003728
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003729static struct ring_buffer_event *
3730rb_iter_peek(struct ring_buffer_iter *iter, u64 *ts)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003731{
3732 struct ring_buffer *buffer;
3733 struct ring_buffer_per_cpu *cpu_buffer;
3734 struct ring_buffer_event *event;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003735 int nr_loops = 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003736
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003737 cpu_buffer = iter->cpu_buffer;
3738 buffer = cpu_buffer->buffer;
3739
Steven Rostedt492a74f2010-01-25 15:17:47 -05003740 /*
3741 * Check if someone performed a consuming read to
3742 * the buffer. A consuming read invalidates the iterator
3743 * and we need to reset the iterator in this case.
3744 */
3745 if (unlikely(iter->cache_read != cpu_buffer->read ||
3746 iter->cache_reader_page != cpu_buffer->reader_page))
3747 rb_iter_reset(iter);
3748
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003749 again:
Steven Rostedt3c05d742010-01-26 16:14:08 -05003750 if (ring_buffer_iter_empty(iter))
3751 return NULL;
3752
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003753 /*
Steven Rostedt69d1b832010-10-07 18:18:05 -04003754 * We repeat when a time extend is encountered.
3755 * Since the time extend is always attached to a data event,
3756 * we should never loop more than once.
3757 * (We never hit the following condition more than twice).
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003758 */
Steven Rostedt69d1b832010-10-07 18:18:05 -04003759 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 2))
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003760 return NULL;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003761
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003762 if (rb_per_cpu_empty(cpu_buffer))
3763 return NULL;
3764
Steven Rostedt3c05d742010-01-26 16:14:08 -05003765 if (iter->head >= local_read(&iter->head_page->page->commit)) {
3766 rb_inc_iter(iter);
3767 goto again;
3768 }
3769
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003770 event = rb_iter_head_event(iter);
3771
Lai Jiangshan334d4162009-04-24 11:27:05 +08003772 switch (event->type_len) {
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003773 case RINGBUF_TYPE_PADDING:
Tom Zanussi2d622712009-03-22 03:30:49 -05003774 if (rb_null_event(event)) {
3775 rb_inc_iter(iter);
3776 goto again;
3777 }
3778 rb_advance_iter(iter);
3779 return event;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003780
3781 case RINGBUF_TYPE_TIME_EXTEND:
3782 /* Internal data, OK to advance */
3783 rb_advance_iter(iter);
3784 goto again;
3785
3786 case RINGBUF_TYPE_TIME_STAMP:
3787 /* FIXME: not implemented */
3788 rb_advance_iter(iter);
3789 goto again;
3790
3791 case RINGBUF_TYPE_DATA:
3792 if (ts) {
3793 *ts = iter->read_stamp + event->time_delta;
Steven Rostedt37886f62009-03-17 17:22:06 -04003794 ring_buffer_normalize_time_stamp(buffer,
3795 cpu_buffer->cpu, ts);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003796 }
3797 return event;
3798
3799 default:
3800 BUG();
3801 }
3802
3803 return NULL;
3804}
Robert Richterc4f50182008-12-11 16:49:22 +01003805EXPORT_SYMBOL_GPL(ring_buffer_iter_peek);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003806
Steven Rostedt8d707e82009-06-16 21:22:48 -04003807static inline int rb_ok_to_lock(void)
3808{
3809 /*
3810 * If an NMI die dumps out the content of the ring buffer
3811 * do not grab locks. We also permanently disable the ring
3812 * buffer too. A one time deal is all you get from reading
3813 * the ring buffer from an NMI.
3814 */
Steven Rostedt464e85e2009-08-05 15:26:37 -04003815 if (likely(!in_nmi()))
Steven Rostedt8d707e82009-06-16 21:22:48 -04003816 return 1;
3817
3818 tracing_off_permanent();
3819 return 0;
3820}
3821
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003822/**
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003823 * ring_buffer_peek - peek at the next event to be read
3824 * @buffer: The ring buffer to read
3825 * @cpu: The cpu to peak at
3826 * @ts: The timestamp counter of this event.
Steven Rostedt66a8cb92010-03-31 13:21:56 -04003827 * @lost_events: a variable to store if events were lost (may be NULL)
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003828 *
3829 * This will return the event that will be read next, but does
3830 * not consume the data.
3831 */
3832struct ring_buffer_event *
Steven Rostedt66a8cb92010-03-31 13:21:56 -04003833ring_buffer_peek(struct ring_buffer *buffer, int cpu, u64 *ts,
3834 unsigned long *lost_events)
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003835{
3836 struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
Steven Rostedt8aabee52009-03-12 13:13:49 -04003837 struct ring_buffer_event *event;
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003838 unsigned long flags;
Steven Rostedt8d707e82009-06-16 21:22:48 -04003839 int dolock;
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003840
Steven Rostedt554f7862009-03-11 22:00:13 -04003841 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04003842 return NULL;
Steven Rostedt554f7862009-03-11 22:00:13 -04003843
Steven Rostedt8d707e82009-06-16 21:22:48 -04003844 dolock = rb_ok_to_lock();
Tom Zanussi2d622712009-03-22 03:30:49 -05003845 again:
Steven Rostedt8d707e82009-06-16 21:22:48 -04003846 local_irq_save(flags);
3847 if (dolock)
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02003848 raw_spin_lock(&cpu_buffer->reader_lock);
Steven Rostedt66a8cb92010-03-31 13:21:56 -04003849 event = rb_buffer_peek(cpu_buffer, ts, lost_events);
Robert Richter469535a2009-07-30 19:19:18 +02003850 if (event && event->type_len == RINGBUF_TYPE_PADDING)
3851 rb_advance_reader(cpu_buffer);
Steven Rostedt8d707e82009-06-16 21:22:48 -04003852 if (dolock)
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02003853 raw_spin_unlock(&cpu_buffer->reader_lock);
Steven Rostedt8d707e82009-06-16 21:22:48 -04003854 local_irq_restore(flags);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003855
Steven Rostedt1b959e12009-09-03 10:12:13 -04003856 if (event && event->type_len == RINGBUF_TYPE_PADDING)
Tom Zanussi2d622712009-03-22 03:30:49 -05003857 goto again;
Tom Zanussi2d622712009-03-22 03:30:49 -05003858
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003859 return event;
3860}
3861
3862/**
3863 * ring_buffer_iter_peek - peek at the next event to be read
3864 * @iter: The ring buffer iterator
3865 * @ts: The timestamp counter of this event.
3866 *
3867 * This will return the event that will be read next, but does
3868 * not increment the iterator.
3869 */
3870struct ring_buffer_event *
3871ring_buffer_iter_peek(struct ring_buffer_iter *iter, u64 *ts)
3872{
3873 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
3874 struct ring_buffer_event *event;
3875 unsigned long flags;
3876
Tom Zanussi2d622712009-03-22 03:30:49 -05003877 again:
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02003878 raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003879 event = rb_iter_peek(iter, ts);
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02003880 raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003881
Steven Rostedt1b959e12009-09-03 10:12:13 -04003882 if (event && event->type_len == RINGBUF_TYPE_PADDING)
Tom Zanussi2d622712009-03-22 03:30:49 -05003883 goto again;
Tom Zanussi2d622712009-03-22 03:30:49 -05003884
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003885 return event;
3886}
3887
3888/**
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003889 * ring_buffer_consume - return an event and consume it
3890 * @buffer: The ring buffer to get the next event from
Steven Rostedt66a8cb92010-03-31 13:21:56 -04003891 * @cpu: the cpu to read the buffer from
3892 * @ts: a variable to store the timestamp (may be NULL)
3893 * @lost_events: a variable to store if events were lost (may be NULL)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003894 *
3895 * Returns the next event in the ring buffer, and that event is consumed.
3896 * Meaning, that sequential reads will keep returning a different event,
3897 * and eventually empty the ring buffer if the producer is slower.
3898 */
3899struct ring_buffer_event *
Steven Rostedt66a8cb92010-03-31 13:21:56 -04003900ring_buffer_consume(struct ring_buffer *buffer, int cpu, u64 *ts,
3901 unsigned long *lost_events)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003902{
Steven Rostedt554f7862009-03-11 22:00:13 -04003903 struct ring_buffer_per_cpu *cpu_buffer;
3904 struct ring_buffer_event *event = NULL;
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003905 unsigned long flags;
Steven Rostedt8d707e82009-06-16 21:22:48 -04003906 int dolock;
3907
3908 dolock = rb_ok_to_lock();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003909
Tom Zanussi2d622712009-03-22 03:30:49 -05003910 again:
Steven Rostedt554f7862009-03-11 22:00:13 -04003911 /* might be called in atomic */
3912 preempt_disable();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003913
Steven Rostedt554f7862009-03-11 22:00:13 -04003914 if (!cpumask_test_cpu(cpu, buffer->cpumask))
3915 goto out;
3916
3917 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt8d707e82009-06-16 21:22:48 -04003918 local_irq_save(flags);
3919 if (dolock)
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02003920 raw_spin_lock(&cpu_buffer->reader_lock);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003921
Steven Rostedt66a8cb92010-03-31 13:21:56 -04003922 event = rb_buffer_peek(cpu_buffer, ts, lost_events);
3923 if (event) {
3924 cpu_buffer->lost_events = 0;
Robert Richter469535a2009-07-30 19:19:18 +02003925 rb_advance_reader(cpu_buffer);
Steven Rostedt66a8cb92010-03-31 13:21:56 -04003926 }
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003927
Steven Rostedt8d707e82009-06-16 21:22:48 -04003928 if (dolock)
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02003929 raw_spin_unlock(&cpu_buffer->reader_lock);
Steven Rostedt8d707e82009-06-16 21:22:48 -04003930 local_irq_restore(flags);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003931
Steven Rostedt554f7862009-03-11 22:00:13 -04003932 out:
3933 preempt_enable();
3934
Steven Rostedt1b959e12009-09-03 10:12:13 -04003935 if (event && event->type_len == RINGBUF_TYPE_PADDING)
Tom Zanussi2d622712009-03-22 03:30:49 -05003936 goto again;
Tom Zanussi2d622712009-03-22 03:30:49 -05003937
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003938 return event;
3939}
Robert Richterc4f50182008-12-11 16:49:22 +01003940EXPORT_SYMBOL_GPL(ring_buffer_consume);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003941
3942/**
David Miller72c9ddf2010-04-20 15:47:11 -07003943 * ring_buffer_read_prepare - Prepare for a non consuming read of the buffer
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003944 * @buffer: The ring buffer to read from
3945 * @cpu: The cpu buffer to iterate over
3946 *
David Miller72c9ddf2010-04-20 15:47:11 -07003947 * This performs the initial preparations necessary to iterate
3948 * through the buffer. Memory is allocated, buffer recording
3949 * is disabled, and the iterator pointer is returned to the caller.
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003950 *
David Miller72c9ddf2010-04-20 15:47:11 -07003951 * Disabling buffer recordng prevents the reading from being
3952 * corrupted. This is not a consuming read, so a producer is not
3953 * expected.
3954 *
3955 * After a sequence of ring_buffer_read_prepare calls, the user is
3956 * expected to make at least one call to ring_buffer_prepare_sync.
3957 * Afterwards, ring_buffer_read_start is invoked to get things going
3958 * for real.
3959 *
3960 * This overall must be paired with ring_buffer_finish.
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003961 */
3962struct ring_buffer_iter *
David Miller72c9ddf2010-04-20 15:47:11 -07003963ring_buffer_read_prepare(struct ring_buffer *buffer, int cpu)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003964{
3965 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedt8aabee52009-03-12 13:13:49 -04003966 struct ring_buffer_iter *iter;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003967
Rusty Russell9e01c1b2009-01-01 10:12:22 +10303968 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04003969 return NULL;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003970
3971 iter = kmalloc(sizeof(*iter), GFP_KERNEL);
3972 if (!iter)
Steven Rostedt8aabee52009-03-12 13:13:49 -04003973 return NULL;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003974
3975 cpu_buffer = buffer->buffers[cpu];
3976
3977 iter->cpu_buffer = cpu_buffer;
3978
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07003979 atomic_inc(&buffer->resize_disabled);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003980 atomic_inc(&cpu_buffer->record_disabled);
David Miller72c9ddf2010-04-20 15:47:11 -07003981
3982 return iter;
3983}
3984EXPORT_SYMBOL_GPL(ring_buffer_read_prepare);
3985
3986/**
3987 * ring_buffer_read_prepare_sync - Synchronize a set of prepare calls
3988 *
3989 * All previously invoked ring_buffer_read_prepare calls to prepare
3990 * iterators will be synchronized. Afterwards, read_buffer_read_start
3991 * calls on those iterators are allowed.
3992 */
3993void
3994ring_buffer_read_prepare_sync(void)
3995{
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003996 synchronize_sched();
David Miller72c9ddf2010-04-20 15:47:11 -07003997}
3998EXPORT_SYMBOL_GPL(ring_buffer_read_prepare_sync);
3999
4000/**
4001 * ring_buffer_read_start - start a non consuming read of the buffer
4002 * @iter: The iterator returned by ring_buffer_read_prepare
4003 *
4004 * This finalizes the startup of an iteration through the buffer.
4005 * The iterator comes from a call to ring_buffer_read_prepare and
4006 * an intervening ring_buffer_read_prepare_sync must have been
4007 * performed.
4008 *
4009 * Must be paired with ring_buffer_finish.
4010 */
4011void
4012ring_buffer_read_start(struct ring_buffer_iter *iter)
4013{
4014 struct ring_buffer_per_cpu *cpu_buffer;
4015 unsigned long flags;
4016
4017 if (!iter)
4018 return;
4019
4020 cpu_buffer = iter->cpu_buffer;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004021
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02004022 raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01004023 arch_spin_lock(&cpu_buffer->lock);
Steven Rostedt642edba2008-11-12 00:01:26 -05004024 rb_iter_reset(iter);
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01004025 arch_spin_unlock(&cpu_buffer->lock);
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02004026 raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004027}
Robert Richterc4f50182008-12-11 16:49:22 +01004028EXPORT_SYMBOL_GPL(ring_buffer_read_start);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004029
4030/**
4031 * ring_buffer_finish - finish reading the iterator of the buffer
4032 * @iter: The iterator retrieved by ring_buffer_start
4033 *
4034 * This re-enables the recording to the buffer, and frees the
4035 * iterator.
4036 */
4037void
4038ring_buffer_read_finish(struct ring_buffer_iter *iter)
4039{
4040 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
Steven Rostedt9366c1b2012-11-29 22:31:16 -05004041 unsigned long flags;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004042
Steven Rostedt659f4512012-05-14 17:02:33 -04004043 /*
4044 * Ring buffer is disabled from recording, here's a good place
Steven Rostedt9366c1b2012-11-29 22:31:16 -05004045 * to check the integrity of the ring buffer.
4046 * Must prevent readers from trying to read, as the check
4047 * clears the HEAD page and readers require it.
Steven Rostedt659f4512012-05-14 17:02:33 -04004048 */
Steven Rostedt9366c1b2012-11-29 22:31:16 -05004049 raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
Steven Rostedt659f4512012-05-14 17:02:33 -04004050 rb_check_pages(cpu_buffer);
Steven Rostedt9366c1b2012-11-29 22:31:16 -05004051 raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
Steven Rostedt659f4512012-05-14 17:02:33 -04004052
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004053 atomic_dec(&cpu_buffer->record_disabled);
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07004054 atomic_dec(&cpu_buffer->buffer->resize_disabled);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004055 kfree(iter);
4056}
Robert Richterc4f50182008-12-11 16:49:22 +01004057EXPORT_SYMBOL_GPL(ring_buffer_read_finish);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004058
4059/**
4060 * ring_buffer_read - read the next item in the ring buffer by the iterator
4061 * @iter: The ring buffer iterator
4062 * @ts: The time stamp of the event read.
4063 *
4064 * This reads the next event in the ring buffer and increments the iterator.
4065 */
4066struct ring_buffer_event *
4067ring_buffer_read(struct ring_buffer_iter *iter, u64 *ts)
4068{
4069 struct ring_buffer_event *event;
Steven Rostedtf83c9d02008-11-11 18:47:44 +01004070 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
4071 unsigned long flags;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004072
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02004073 raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
Steven Rostedt7e9391c2009-09-03 10:02:09 -04004074 again:
Steven Rostedtf83c9d02008-11-11 18:47:44 +01004075 event = rb_iter_peek(iter, ts);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004076 if (!event)
Steven Rostedtf83c9d02008-11-11 18:47:44 +01004077 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004078
Steven Rostedt7e9391c2009-09-03 10:02:09 -04004079 if (event->type_len == RINGBUF_TYPE_PADDING)
4080 goto again;
4081
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004082 rb_advance_iter(iter);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01004083 out:
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02004084 raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004085
4086 return event;
4087}
Robert Richterc4f50182008-12-11 16:49:22 +01004088EXPORT_SYMBOL_GPL(ring_buffer_read);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004089
4090/**
4091 * ring_buffer_size - return the size of the ring buffer (in bytes)
4092 * @buffer: The ring buffer.
4093 */
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08004094unsigned long ring_buffer_size(struct ring_buffer *buffer, int cpu)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004095{
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08004096 /*
4097 * Earlier, this method returned
4098 * BUF_PAGE_SIZE * buffer->nr_pages
4099 * Since the nr_pages field is now removed, we have converted this to
4100 * return the per cpu buffer value.
4101 */
4102 if (!cpumask_test_cpu(cpu, buffer->cpumask))
4103 return 0;
4104
4105 return BUF_PAGE_SIZE * buffer->buffers[cpu]->nr_pages;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004106}
Robert Richterc4f50182008-12-11 16:49:22 +01004107EXPORT_SYMBOL_GPL(ring_buffer_size);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004108
4109static void
4110rb_reset_cpu(struct ring_buffer_per_cpu *cpu_buffer)
4111{
Steven Rostedt77ae3652009-03-27 11:00:29 -04004112 rb_head_page_deactivate(cpu_buffer);
4113
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004114 cpu_buffer->head_page
Steven Rostedt3adc54f2009-03-30 15:32:01 -04004115 = list_entry(cpu_buffer->pages, struct buffer_page, list);
Steven Rostedtbf41a152008-10-04 02:00:59 -04004116 local_set(&cpu_buffer->head_page->write, 0);
Steven Rostedt778c55d2009-05-01 18:44:45 -04004117 local_set(&cpu_buffer->head_page->entries, 0);
Steven Rostedtabc9b562008-12-02 15:34:06 -05004118 local_set(&cpu_buffer->head_page->page->commit, 0);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004119
Steven Rostedt6f807ac2008-10-04 02:00:58 -04004120 cpu_buffer->head_page->read = 0;
Steven Rostedtbf41a152008-10-04 02:00:59 -04004121
4122 cpu_buffer->tail_page = cpu_buffer->head_page;
4123 cpu_buffer->commit_page = cpu_buffer->head_page;
4124
4125 INIT_LIST_HEAD(&cpu_buffer->reader_page->list);
Vaibhav Nagarnaik5040b4b2012-05-03 18:59:51 -07004126 INIT_LIST_HEAD(&cpu_buffer->new_pages);
Steven Rostedtbf41a152008-10-04 02:00:59 -04004127 local_set(&cpu_buffer->reader_page->write, 0);
Steven Rostedt778c55d2009-05-01 18:44:45 -04004128 local_set(&cpu_buffer->reader_page->entries, 0);
Steven Rostedtabc9b562008-12-02 15:34:06 -05004129 local_set(&cpu_buffer->reader_page->page->commit, 0);
Steven Rostedt6f807ac2008-10-04 02:00:58 -04004130 cpu_buffer->reader_page->read = 0;
Steven Rostedtd7690412008-10-01 00:29:53 -04004131
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -07004132 local_set(&cpu_buffer->entries_bytes, 0);
Steven Rostedt77ae3652009-03-27 11:00:29 -04004133 local_set(&cpu_buffer->overrun, 0);
Slava Pestov884bfe82011-07-15 14:23:58 -07004134 local_set(&cpu_buffer->commit_overrun, 0);
4135 local_set(&cpu_buffer->dropped_events, 0);
Steven Rostedte4906ef2009-04-30 20:49:44 -04004136 local_set(&cpu_buffer->entries, 0);
Steven Rostedtfa743952009-06-16 12:37:57 -04004137 local_set(&cpu_buffer->committing, 0);
4138 local_set(&cpu_buffer->commits, 0);
Steven Rostedt77ae3652009-03-27 11:00:29 -04004139 cpu_buffer->read = 0;
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -07004140 cpu_buffer->read_bytes = 0;
Steven Rostedt69507c02009-01-21 18:45:57 -05004141
4142 cpu_buffer->write_stamp = 0;
4143 cpu_buffer->read_stamp = 0;
Steven Rostedt77ae3652009-03-27 11:00:29 -04004144
Steven Rostedt66a8cb92010-03-31 13:21:56 -04004145 cpu_buffer->lost_events = 0;
4146 cpu_buffer->last_overrun = 0;
4147
Steven Rostedt77ae3652009-03-27 11:00:29 -04004148 rb_head_page_activate(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004149}
4150
4151/**
4152 * ring_buffer_reset_cpu - reset a ring buffer per CPU buffer
4153 * @buffer: The ring buffer to reset a per cpu buffer of
4154 * @cpu: The CPU buffer to be reset
4155 */
4156void ring_buffer_reset_cpu(struct ring_buffer *buffer, int cpu)
4157{
4158 struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
4159 unsigned long flags;
4160
Rusty Russell9e01c1b2009-01-01 10:12:22 +10304161 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04004162 return;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004163
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07004164 atomic_inc(&buffer->resize_disabled);
Steven Rostedt41ede232009-05-01 20:26:54 -04004165 atomic_inc(&cpu_buffer->record_disabled);
4166
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07004167 /* Make sure all commits have finished */
4168 synchronize_sched();
4169
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02004170 raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01004171
Steven Rostedt41b6a952009-09-02 09:59:48 -04004172 if (RB_WARN_ON(cpu_buffer, local_read(&cpu_buffer->committing)))
4173 goto out;
4174
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01004175 arch_spin_lock(&cpu_buffer->lock);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004176
4177 rb_reset_cpu(cpu_buffer);
4178
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01004179 arch_spin_unlock(&cpu_buffer->lock);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01004180
Steven Rostedt41b6a952009-09-02 09:59:48 -04004181 out:
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02004182 raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
Steven Rostedt41ede232009-05-01 20:26:54 -04004183
4184 atomic_dec(&cpu_buffer->record_disabled);
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07004185 atomic_dec(&buffer->resize_disabled);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004186}
Robert Richterc4f50182008-12-11 16:49:22 +01004187EXPORT_SYMBOL_GPL(ring_buffer_reset_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004188
4189/**
4190 * ring_buffer_reset - reset a ring buffer
4191 * @buffer: The ring buffer to reset all cpu buffers
4192 */
4193void ring_buffer_reset(struct ring_buffer *buffer)
4194{
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004195 int cpu;
4196
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004197 for_each_buffer_cpu(buffer, cpu)
Steven Rostedtd7690412008-10-01 00:29:53 -04004198 ring_buffer_reset_cpu(buffer, cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004199}
Robert Richterc4f50182008-12-11 16:49:22 +01004200EXPORT_SYMBOL_GPL(ring_buffer_reset);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004201
4202/**
4203 * rind_buffer_empty - is the ring buffer empty?
4204 * @buffer: The ring buffer to test
4205 */
4206int ring_buffer_empty(struct ring_buffer *buffer)
4207{
4208 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedtd4788202009-06-17 00:39:43 -04004209 unsigned long flags;
Steven Rostedt8d707e82009-06-16 21:22:48 -04004210 int dolock;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004211 int cpu;
Steven Rostedtd4788202009-06-17 00:39:43 -04004212 int ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004213
Steven Rostedt8d707e82009-06-16 21:22:48 -04004214 dolock = rb_ok_to_lock();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004215
4216 /* yes this is racy, but if you don't like the race, lock the buffer */
4217 for_each_buffer_cpu(buffer, cpu) {
4218 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt8d707e82009-06-16 21:22:48 -04004219 local_irq_save(flags);
4220 if (dolock)
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02004221 raw_spin_lock(&cpu_buffer->reader_lock);
Steven Rostedtd4788202009-06-17 00:39:43 -04004222 ret = rb_per_cpu_empty(cpu_buffer);
Steven Rostedt8d707e82009-06-16 21:22:48 -04004223 if (dolock)
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02004224 raw_spin_unlock(&cpu_buffer->reader_lock);
Steven Rostedt8d707e82009-06-16 21:22:48 -04004225 local_irq_restore(flags);
4226
Steven Rostedtd4788202009-06-17 00:39:43 -04004227 if (!ret)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004228 return 0;
4229 }
Steven Rostedt554f7862009-03-11 22:00:13 -04004230
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004231 return 1;
4232}
Robert Richterc4f50182008-12-11 16:49:22 +01004233EXPORT_SYMBOL_GPL(ring_buffer_empty);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004234
4235/**
4236 * ring_buffer_empty_cpu - is a cpu buffer of a ring buffer empty?
4237 * @buffer: The ring buffer
4238 * @cpu: The CPU buffer to test
4239 */
4240int ring_buffer_empty_cpu(struct ring_buffer *buffer, int cpu)
4241{
4242 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedtd4788202009-06-17 00:39:43 -04004243 unsigned long flags;
Steven Rostedt8d707e82009-06-16 21:22:48 -04004244 int dolock;
Steven Rostedt8aabee52009-03-12 13:13:49 -04004245 int ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004246
Rusty Russell9e01c1b2009-01-01 10:12:22 +10304247 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04004248 return 1;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004249
Steven Rostedt8d707e82009-06-16 21:22:48 -04004250 dolock = rb_ok_to_lock();
Steven Rostedt554f7862009-03-11 22:00:13 -04004251
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004252 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt8d707e82009-06-16 21:22:48 -04004253 local_irq_save(flags);
4254 if (dolock)
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02004255 raw_spin_lock(&cpu_buffer->reader_lock);
Steven Rostedt554f7862009-03-11 22:00:13 -04004256 ret = rb_per_cpu_empty(cpu_buffer);
Steven Rostedt8d707e82009-06-16 21:22:48 -04004257 if (dolock)
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02004258 raw_spin_unlock(&cpu_buffer->reader_lock);
Steven Rostedt8d707e82009-06-16 21:22:48 -04004259 local_irq_restore(flags);
Steven Rostedt554f7862009-03-11 22:00:13 -04004260
4261 return ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004262}
Robert Richterc4f50182008-12-11 16:49:22 +01004263EXPORT_SYMBOL_GPL(ring_buffer_empty_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004264
Steven Rostedt85bac322009-09-04 14:24:40 -04004265#ifdef CONFIG_RING_BUFFER_ALLOW_SWAP
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004266/**
4267 * ring_buffer_swap_cpu - swap a CPU buffer between two ring buffers
4268 * @buffer_a: One buffer to swap with
4269 * @buffer_b: The other buffer to swap with
4270 *
4271 * This function is useful for tracers that want to take a "snapshot"
4272 * of a CPU buffer and has another back up buffer lying around.
4273 * it is expected that the tracer handles the cpu buffer not being
4274 * used at the moment.
4275 */
4276int ring_buffer_swap_cpu(struct ring_buffer *buffer_a,
4277 struct ring_buffer *buffer_b, int cpu)
4278{
4279 struct ring_buffer_per_cpu *cpu_buffer_a;
4280 struct ring_buffer_per_cpu *cpu_buffer_b;
Steven Rostedt554f7862009-03-11 22:00:13 -04004281 int ret = -EINVAL;
4282
Rusty Russell9e01c1b2009-01-01 10:12:22 +10304283 if (!cpumask_test_cpu(cpu, buffer_a->cpumask) ||
4284 !cpumask_test_cpu(cpu, buffer_b->cpumask))
Steven Rostedt554f7862009-03-11 22:00:13 -04004285 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004286
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08004287 cpu_buffer_a = buffer_a->buffers[cpu];
4288 cpu_buffer_b = buffer_b->buffers[cpu];
4289
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004290 /* At least make sure the two buffers are somewhat the same */
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08004291 if (cpu_buffer_a->nr_pages != cpu_buffer_b->nr_pages)
Steven Rostedt554f7862009-03-11 22:00:13 -04004292 goto out;
4293
4294 ret = -EAGAIN;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004295
Steven Rostedt97b17ef2009-01-21 15:24:56 -05004296 if (ring_buffer_flags != RB_BUFFERS_ON)
Steven Rostedt554f7862009-03-11 22:00:13 -04004297 goto out;
Steven Rostedt97b17ef2009-01-21 15:24:56 -05004298
4299 if (atomic_read(&buffer_a->record_disabled))
Steven Rostedt554f7862009-03-11 22:00:13 -04004300 goto out;
Steven Rostedt97b17ef2009-01-21 15:24:56 -05004301
4302 if (atomic_read(&buffer_b->record_disabled))
Steven Rostedt554f7862009-03-11 22:00:13 -04004303 goto out;
Steven Rostedt97b17ef2009-01-21 15:24:56 -05004304
Steven Rostedt97b17ef2009-01-21 15:24:56 -05004305 if (atomic_read(&cpu_buffer_a->record_disabled))
Steven Rostedt554f7862009-03-11 22:00:13 -04004306 goto out;
Steven Rostedt97b17ef2009-01-21 15:24:56 -05004307
4308 if (atomic_read(&cpu_buffer_b->record_disabled))
Steven Rostedt554f7862009-03-11 22:00:13 -04004309 goto out;
Steven Rostedt97b17ef2009-01-21 15:24:56 -05004310
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004311 /*
4312 * We can't do a synchronize_sched here because this
4313 * function can be called in atomic context.
4314 * Normally this will be called from the same CPU as cpu.
4315 * If not it's up to the caller to protect this.
4316 */
4317 atomic_inc(&cpu_buffer_a->record_disabled);
4318 atomic_inc(&cpu_buffer_b->record_disabled);
4319
Steven Rostedt98277992009-09-02 10:56:15 -04004320 ret = -EBUSY;
4321 if (local_read(&cpu_buffer_a->committing))
4322 goto out_dec;
4323 if (local_read(&cpu_buffer_b->committing))
4324 goto out_dec;
4325
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004326 buffer_a->buffers[cpu] = cpu_buffer_b;
4327 buffer_b->buffers[cpu] = cpu_buffer_a;
4328
4329 cpu_buffer_b->buffer = buffer_a;
4330 cpu_buffer_a->buffer = buffer_b;
4331
Steven Rostedt98277992009-09-02 10:56:15 -04004332 ret = 0;
4333
4334out_dec:
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004335 atomic_dec(&cpu_buffer_a->record_disabled);
4336 atomic_dec(&cpu_buffer_b->record_disabled);
Steven Rostedt554f7862009-03-11 22:00:13 -04004337out:
Steven Rostedt554f7862009-03-11 22:00:13 -04004338 return ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004339}
Robert Richterc4f50182008-12-11 16:49:22 +01004340EXPORT_SYMBOL_GPL(ring_buffer_swap_cpu);
Steven Rostedt85bac322009-09-04 14:24:40 -04004341#endif /* CONFIG_RING_BUFFER_ALLOW_SWAP */
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004342
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004343/**
4344 * ring_buffer_alloc_read_page - allocate a page to read from buffer
4345 * @buffer: the buffer to allocate for.
4346 *
4347 * This function is used in conjunction with ring_buffer_read_page.
4348 * When reading a full page from the ring buffer, these functions
4349 * can be used to speed up the process. The calling function should
4350 * allocate a few pages first with this function. Then when it
4351 * needs to get pages from the ring buffer, it passes the result
4352 * of this function into ring_buffer_read_page, which will swap
4353 * the page that was allocated, with the read page of the buffer.
4354 *
4355 * Returns:
4356 * The page allocated, or NULL on error.
4357 */
Vaibhav Nagarnaik7ea59062011-05-03 17:56:42 -07004358void *ring_buffer_alloc_read_page(struct ring_buffer *buffer, int cpu)
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004359{
Steven Rostedt044fa782008-12-02 23:50:03 -05004360 struct buffer_data_page *bpage;
Vaibhav Nagarnaik7ea59062011-05-03 17:56:42 -07004361 struct page *page;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004362
Vaibhav Nagarnaikd7ec4bf2011-06-07 17:01:42 -07004363 page = alloc_pages_node(cpu_to_node(cpu),
4364 GFP_KERNEL | __GFP_NORETRY, 0);
Vaibhav Nagarnaik7ea59062011-05-03 17:56:42 -07004365 if (!page)
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004366 return NULL;
4367
Vaibhav Nagarnaik7ea59062011-05-03 17:56:42 -07004368 bpage = page_address(page);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004369
Steven Rostedtef7a4a12009-03-03 00:27:49 -05004370 rb_init_page(bpage);
4371
Steven Rostedt044fa782008-12-02 23:50:03 -05004372 return bpage;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004373}
Steven Rostedtd6ce96d2009-05-05 01:15:24 -04004374EXPORT_SYMBOL_GPL(ring_buffer_alloc_read_page);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004375
4376/**
4377 * ring_buffer_free_read_page - free an allocated read page
4378 * @buffer: the buffer the page was allocate for
4379 * @data: the page to free
4380 *
4381 * Free a page allocated from ring_buffer_alloc_read_page.
4382 */
4383void ring_buffer_free_read_page(struct ring_buffer *buffer, void *data)
4384{
4385 free_page((unsigned long)data);
4386}
Steven Rostedtd6ce96d2009-05-05 01:15:24 -04004387EXPORT_SYMBOL_GPL(ring_buffer_free_read_page);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004388
4389/**
4390 * ring_buffer_read_page - extract a page from the ring buffer
4391 * @buffer: buffer to extract from
4392 * @data_page: the page to use allocated from ring_buffer_alloc_read_page
Steven Rostedtef7a4a12009-03-03 00:27:49 -05004393 * @len: amount to extract
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004394 * @cpu: the cpu of the buffer to extract
4395 * @full: should the extraction only happen when the page is full.
4396 *
4397 * This function will pull out a page from the ring buffer and consume it.
4398 * @data_page must be the address of the variable that was returned
4399 * from ring_buffer_alloc_read_page. This is because the page might be used
4400 * to swap with a page in the ring buffer.
4401 *
4402 * for example:
Lai Jiangshanb85fa012009-02-09 14:21:14 +08004403 * rpage = ring_buffer_alloc_read_page(buffer);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004404 * if (!rpage)
4405 * return error;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05004406 * ret = ring_buffer_read_page(buffer, &rpage, len, cpu, 0);
Lai Jiangshan667d2412009-02-09 14:21:17 +08004407 * if (ret >= 0)
4408 * process_page(rpage, ret);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004409 *
4410 * When @full is set, the function will not return true unless
4411 * the writer is off the reader page.
4412 *
4413 * Note: it is up to the calling functions to handle sleeps and wakeups.
4414 * The ring buffer can be used anywhere in the kernel and can not
4415 * blindly call wake_up. The layer that uses the ring buffer must be
4416 * responsible for that.
4417 *
4418 * Returns:
Lai Jiangshan667d2412009-02-09 14:21:17 +08004419 * >=0 if data has been transferred, returns the offset of consumed data.
4420 * <0 if no data has been transferred.
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004421 */
4422int ring_buffer_read_page(struct ring_buffer *buffer,
Steven Rostedtef7a4a12009-03-03 00:27:49 -05004423 void **data_page, size_t len, int cpu, int full)
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004424{
4425 struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
4426 struct ring_buffer_event *event;
Steven Rostedt044fa782008-12-02 23:50:03 -05004427 struct buffer_data_page *bpage;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05004428 struct buffer_page *reader;
Steven Rostedtff0ff842010-03-31 22:11:42 -04004429 unsigned long missed_events;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004430 unsigned long flags;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05004431 unsigned int commit;
Lai Jiangshan667d2412009-02-09 14:21:17 +08004432 unsigned int read;
Steven Rostedt4f3640f2009-03-03 23:52:42 -05004433 u64 save_timestamp;
Lai Jiangshan667d2412009-02-09 14:21:17 +08004434 int ret = -1;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004435
Steven Rostedt554f7862009-03-11 22:00:13 -04004436 if (!cpumask_test_cpu(cpu, buffer->cpumask))
4437 goto out;
4438
Steven Rostedt474d32b2009-03-03 19:51:40 -05004439 /*
4440 * If len is not big enough to hold the page header, then
4441 * we can not copy anything.
4442 */
4443 if (len <= BUF_PAGE_HDR_SIZE)
Steven Rostedt554f7862009-03-11 22:00:13 -04004444 goto out;
Steven Rostedt474d32b2009-03-03 19:51:40 -05004445
4446 len -= BUF_PAGE_HDR_SIZE;
4447
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004448 if (!data_page)
Steven Rostedt554f7862009-03-11 22:00:13 -04004449 goto out;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004450
Steven Rostedt044fa782008-12-02 23:50:03 -05004451 bpage = *data_page;
4452 if (!bpage)
Steven Rostedt554f7862009-03-11 22:00:13 -04004453 goto out;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004454
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02004455 raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004456
Steven Rostedtef7a4a12009-03-03 00:27:49 -05004457 reader = rb_get_reader_page(cpu_buffer);
4458 if (!reader)
Steven Rostedt554f7862009-03-11 22:00:13 -04004459 goto out_unlock;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004460
Steven Rostedtef7a4a12009-03-03 00:27:49 -05004461 event = rb_reader_event(cpu_buffer);
Lai Jiangshan667d2412009-02-09 14:21:17 +08004462
Steven Rostedtef7a4a12009-03-03 00:27:49 -05004463 read = reader->read;
4464 commit = rb_page_commit(reader);
4465
Steven Rostedt66a8cb92010-03-31 13:21:56 -04004466 /* Check if any events were dropped */
Steven Rostedtff0ff842010-03-31 22:11:42 -04004467 missed_events = cpu_buffer->lost_events;
Steven Rostedt66a8cb92010-03-31 13:21:56 -04004468
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004469 /*
Steven Rostedt474d32b2009-03-03 19:51:40 -05004470 * If this page has been partially read or
4471 * if len is not big enough to read the rest of the page or
4472 * a writer is still on the page, then
4473 * we must copy the data from the page to the buffer.
4474 * Otherwise, we can simply swap the page with the one passed in.
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004475 */
Steven Rostedt474d32b2009-03-03 19:51:40 -05004476 if (read || (len < (commit - read)) ||
Steven Rostedtef7a4a12009-03-03 00:27:49 -05004477 cpu_buffer->reader_page == cpu_buffer->commit_page) {
Lai Jiangshan667d2412009-02-09 14:21:17 +08004478 struct buffer_data_page *rpage = cpu_buffer->reader_page->page;
Steven Rostedt474d32b2009-03-03 19:51:40 -05004479 unsigned int rpos = read;
4480 unsigned int pos = 0;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05004481 unsigned int size;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004482
4483 if (full)
Steven Rostedt554f7862009-03-11 22:00:13 -04004484 goto out_unlock;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004485
Steven Rostedtef7a4a12009-03-03 00:27:49 -05004486 if (len > (commit - read))
4487 len = (commit - read);
4488
Steven Rostedt69d1b832010-10-07 18:18:05 -04004489 /* Always keep the time extend and data together */
4490 size = rb_event_ts_length(event);
Steven Rostedtef7a4a12009-03-03 00:27:49 -05004491
4492 if (len < size)
Steven Rostedt554f7862009-03-11 22:00:13 -04004493 goto out_unlock;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05004494
Steven Rostedt4f3640f2009-03-03 23:52:42 -05004495 /* save the current timestamp, since the user will need it */
4496 save_timestamp = cpu_buffer->read_stamp;
4497
Steven Rostedtef7a4a12009-03-03 00:27:49 -05004498 /* Need to copy one event at a time */
4499 do {
David Sharpe1e35922010-12-22 16:38:24 -08004500 /* We need the size of one event, because
4501 * rb_advance_reader only advances by one event,
4502 * whereas rb_event_ts_length may include the size of
4503 * one or two events.
4504 * We have already ensured there's enough space if this
4505 * is a time extend. */
4506 size = rb_event_length(event);
Steven Rostedt474d32b2009-03-03 19:51:40 -05004507 memcpy(bpage->data + pos, rpage->data + rpos, size);
Steven Rostedtef7a4a12009-03-03 00:27:49 -05004508
4509 len -= size;
4510
4511 rb_advance_reader(cpu_buffer);
Steven Rostedt474d32b2009-03-03 19:51:40 -05004512 rpos = reader->read;
4513 pos += size;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05004514
Huang Ying18fab912010-07-28 14:14:01 +08004515 if (rpos >= commit)
4516 break;
4517
Steven Rostedtef7a4a12009-03-03 00:27:49 -05004518 event = rb_reader_event(cpu_buffer);
Steven Rostedt69d1b832010-10-07 18:18:05 -04004519 /* Always keep the time extend and data together */
4520 size = rb_event_ts_length(event);
David Sharpe1e35922010-12-22 16:38:24 -08004521 } while (len >= size);
Lai Jiangshan667d2412009-02-09 14:21:17 +08004522
4523 /* update bpage */
Steven Rostedtef7a4a12009-03-03 00:27:49 -05004524 local_set(&bpage->commit, pos);
Steven Rostedt4f3640f2009-03-03 23:52:42 -05004525 bpage->time_stamp = save_timestamp;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05004526
Steven Rostedt474d32b2009-03-03 19:51:40 -05004527 /* we copied everything to the beginning */
4528 read = 0;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004529 } else {
Steven Rostedtafbab762009-05-01 19:40:05 -04004530 /* update the entry counter */
Steven Rostedt77ae3652009-03-27 11:00:29 -04004531 cpu_buffer->read += rb_page_entries(reader);
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -07004532 cpu_buffer->read_bytes += BUF_PAGE_SIZE;
Steven Rostedtafbab762009-05-01 19:40:05 -04004533
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004534 /* swap the pages */
Steven Rostedt044fa782008-12-02 23:50:03 -05004535 rb_init_page(bpage);
Steven Rostedtef7a4a12009-03-03 00:27:49 -05004536 bpage = reader->page;
4537 reader->page = *data_page;
4538 local_set(&reader->write, 0);
Steven Rostedt778c55d2009-05-01 18:44:45 -04004539 local_set(&reader->entries, 0);
Steven Rostedtef7a4a12009-03-03 00:27:49 -05004540 reader->read = 0;
Steven Rostedt044fa782008-12-02 23:50:03 -05004541 *data_page = bpage;
Steven Rostedtff0ff842010-03-31 22:11:42 -04004542
4543 /*
4544 * Use the real_end for the data size,
4545 * This gives us a chance to store the lost events
4546 * on the page.
4547 */
4548 if (reader->real_end)
4549 local_set(&bpage->commit, reader->real_end);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004550 }
Lai Jiangshan667d2412009-02-09 14:21:17 +08004551 ret = read;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004552
Steven Rostedt66a8cb92010-03-31 13:21:56 -04004553 cpu_buffer->lost_events = 0;
Steven Rostedt2711ca22010-05-21 13:32:26 -04004554
4555 commit = local_read(&bpage->commit);
Steven Rostedt66a8cb92010-03-31 13:21:56 -04004556 /*
4557 * Set a flag in the commit field if we lost events
4558 */
Steven Rostedtff0ff842010-03-31 22:11:42 -04004559 if (missed_events) {
Steven Rostedtff0ff842010-03-31 22:11:42 -04004560 /* If there is room at the end of the page to save the
4561 * missed events, then record it there.
4562 */
4563 if (BUF_PAGE_SIZE - commit >= sizeof(missed_events)) {
4564 memcpy(&bpage->data[commit], &missed_events,
4565 sizeof(missed_events));
4566 local_add(RB_MISSED_STORED, &bpage->commit);
Steven Rostedt2711ca22010-05-21 13:32:26 -04004567 commit += sizeof(missed_events);
Steven Rostedtff0ff842010-03-31 22:11:42 -04004568 }
Steven Rostedt66a8cb92010-03-31 13:21:56 -04004569 local_add(RB_MISSED_EVENTS, &bpage->commit);
Steven Rostedtff0ff842010-03-31 22:11:42 -04004570 }
Steven Rostedt66a8cb92010-03-31 13:21:56 -04004571
Steven Rostedt2711ca22010-05-21 13:32:26 -04004572 /*
4573 * This page may be off to user land. Zero it out here.
4574 */
4575 if (commit < BUF_PAGE_SIZE)
4576 memset(&bpage->data[commit], 0, BUF_PAGE_SIZE - commit);
4577
Steven Rostedt554f7862009-03-11 22:00:13 -04004578 out_unlock:
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02004579 raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004580
Steven Rostedt554f7862009-03-11 22:00:13 -04004581 out:
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004582 return ret;
4583}
Steven Rostedtd6ce96d2009-05-05 01:15:24 -04004584EXPORT_SYMBOL_GPL(ring_buffer_read_page);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004585
Steven Rostedt59222ef2009-03-12 11:46:03 -04004586#ifdef CONFIG_HOTPLUG_CPU
Frederic Weisbecker09c9e842009-03-21 04:33:36 +01004587static int rb_cpu_notify(struct notifier_block *self,
4588 unsigned long action, void *hcpu)
Steven Rostedt554f7862009-03-11 22:00:13 -04004589{
4590 struct ring_buffer *buffer =
4591 container_of(self, struct ring_buffer, cpu_notify);
4592 long cpu = (long)hcpu;
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08004593 int cpu_i, nr_pages_same;
4594 unsigned int nr_pages;
Steven Rostedt554f7862009-03-11 22:00:13 -04004595
4596 switch (action) {
4597 case CPU_UP_PREPARE:
4598 case CPU_UP_PREPARE_FROZEN:
Rusty Russell3f237a72009-06-12 21:15:30 +09304599 if (cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt554f7862009-03-11 22:00:13 -04004600 return NOTIFY_OK;
4601
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08004602 nr_pages = 0;
4603 nr_pages_same = 1;
4604 /* check if all cpu sizes are same */
4605 for_each_buffer_cpu(buffer, cpu_i) {
4606 /* fill in the size from first enabled cpu */
4607 if (nr_pages == 0)
4608 nr_pages = buffer->buffers[cpu_i]->nr_pages;
4609 if (nr_pages != buffer->buffers[cpu_i]->nr_pages) {
4610 nr_pages_same = 0;
4611 break;
4612 }
4613 }
4614 /* allocate minimum pages, user can later expand it */
4615 if (!nr_pages_same)
4616 nr_pages = 2;
Steven Rostedt554f7862009-03-11 22:00:13 -04004617 buffer->buffers[cpu] =
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08004618 rb_allocate_cpu_buffer(buffer, nr_pages, cpu);
Steven Rostedt554f7862009-03-11 22:00:13 -04004619 if (!buffer->buffers[cpu]) {
4620 WARN(1, "failed to allocate ring buffer on CPU %ld\n",
4621 cpu);
4622 return NOTIFY_OK;
4623 }
4624 smp_wmb();
Rusty Russell3f237a72009-06-12 21:15:30 +09304625 cpumask_set_cpu(cpu, buffer->cpumask);
Steven Rostedt554f7862009-03-11 22:00:13 -04004626 break;
4627 case CPU_DOWN_PREPARE:
4628 case CPU_DOWN_PREPARE_FROZEN:
4629 /*
4630 * Do nothing.
4631 * If we were to free the buffer, then the user would
4632 * lose any trace that was in the buffer.
4633 */
4634 break;
4635 default:
4636 break;
4637 }
4638 return NOTIFY_OK;
4639}
4640#endif
Steven Rostedt (Red Hat)6c43e552013-03-15 11:32:53 -04004641
4642#ifdef CONFIG_RING_BUFFER_STARTUP_TEST
4643/*
4644 * This is a basic integrity check of the ring buffer.
4645 * Late in the boot cycle this test will run when configured in.
4646 * It will kick off a thread per CPU that will go into a loop
4647 * writing to the per cpu ring buffer various sizes of data.
4648 * Some of the data will be large items, some small.
4649 *
4650 * Another thread is created that goes into a spin, sending out
4651 * IPIs to the other CPUs to also write into the ring buffer.
4652 * this is to test the nesting ability of the buffer.
4653 *
4654 * Basic stats are recorded and reported. If something in the
4655 * ring buffer should happen that's not expected, a big warning
4656 * is displayed and all ring buffers are disabled.
4657 */
4658static struct task_struct *rb_threads[NR_CPUS] __initdata;
4659
4660struct rb_test_data {
4661 struct ring_buffer *buffer;
4662 unsigned long events;
4663 unsigned long bytes_written;
4664 unsigned long bytes_alloc;
4665 unsigned long bytes_dropped;
4666 unsigned long events_nested;
4667 unsigned long bytes_written_nested;
4668 unsigned long bytes_alloc_nested;
4669 unsigned long bytes_dropped_nested;
4670 int min_size_nested;
4671 int max_size_nested;
4672 int max_size;
4673 int min_size;
4674 int cpu;
4675 int cnt;
4676};
4677
4678static struct rb_test_data rb_data[NR_CPUS] __initdata;
4679
4680/* 1 meg per cpu */
4681#define RB_TEST_BUFFER_SIZE 1048576
4682
4683static char rb_string[] __initdata =
4684 "abcdefghijklmnopqrstuvwxyz1234567890!@#$%^&*()?+\\"
4685 "?+|:';\",.<>/?abcdefghijklmnopqrstuvwxyz1234567890"
4686 "!@#$%^&*()?+\\?+|:';\",.<>/?abcdefghijklmnopqrstuv";
4687
4688static bool rb_test_started __initdata;
4689
4690struct rb_item {
4691 int size;
4692 char str[];
4693};
4694
4695static __init int rb_write_something(struct rb_test_data *data, bool nested)
4696{
4697 struct ring_buffer_event *event;
4698 struct rb_item *item;
4699 bool started;
4700 int event_len;
4701 int size;
4702 int len;
4703 int cnt;
4704
4705 /* Have nested writes different that what is written */
4706 cnt = data->cnt + (nested ? 27 : 0);
4707
4708 /* Multiply cnt by ~e, to make some unique increment */
4709 size = (data->cnt * 68 / 25) % (sizeof(rb_string) - 1);
4710
4711 len = size + sizeof(struct rb_item);
4712
4713 started = rb_test_started;
4714 /* read rb_test_started before checking buffer enabled */
4715 smp_rmb();
4716
4717 event = ring_buffer_lock_reserve(data->buffer, len);
4718 if (!event) {
4719 /* Ignore dropped events before test starts. */
4720 if (started) {
4721 if (nested)
4722 data->bytes_dropped += len;
4723 else
4724 data->bytes_dropped_nested += len;
4725 }
4726 return len;
4727 }
4728
4729 event_len = ring_buffer_event_length(event);
4730
4731 if (RB_WARN_ON(data->buffer, event_len < len))
4732 goto out;
4733
4734 item = ring_buffer_event_data(event);
4735 item->size = size;
4736 memcpy(item->str, rb_string, size);
4737
4738 if (nested) {
4739 data->bytes_alloc_nested += event_len;
4740 data->bytes_written_nested += len;
4741 data->events_nested++;
4742 if (!data->min_size_nested || len < data->min_size_nested)
4743 data->min_size_nested = len;
4744 if (len > data->max_size_nested)
4745 data->max_size_nested = len;
4746 } else {
4747 data->bytes_alloc += event_len;
4748 data->bytes_written += len;
4749 data->events++;
4750 if (!data->min_size || len < data->min_size)
4751 data->max_size = len;
4752 if (len > data->max_size)
4753 data->max_size = len;
4754 }
4755
4756 out:
4757 ring_buffer_unlock_commit(data->buffer, event);
4758
4759 return 0;
4760}
4761
4762static __init int rb_test(void *arg)
4763{
4764 struct rb_test_data *data = arg;
4765
4766 while (!kthread_should_stop()) {
4767 rb_write_something(data, false);
4768 data->cnt++;
4769
4770 set_current_state(TASK_INTERRUPTIBLE);
4771 /* Now sleep between a min of 100-300us and a max of 1ms */
4772 usleep_range(((data->cnt % 3) + 1) * 100, 1000);
4773 }
4774
4775 return 0;
4776}
4777
4778static __init void rb_ipi(void *ignore)
4779{
4780 struct rb_test_data *data;
4781 int cpu = smp_processor_id();
4782
4783 data = &rb_data[cpu];
4784 rb_write_something(data, true);
4785}
4786
4787static __init int rb_hammer_test(void *arg)
4788{
4789 while (!kthread_should_stop()) {
4790
4791 /* Send an IPI to all cpus to write data! */
4792 smp_call_function(rb_ipi, NULL, 1);
4793 /* No sleep, but for non preempt, let others run */
4794 schedule();
4795 }
4796
4797 return 0;
4798}
4799
4800static __init int test_ringbuffer(void)
4801{
4802 struct task_struct *rb_hammer;
4803 struct ring_buffer *buffer;
4804 int cpu;
4805 int ret = 0;
4806
4807 pr_info("Running ring buffer tests...\n");
4808
4809 buffer = ring_buffer_alloc(RB_TEST_BUFFER_SIZE, RB_FL_OVERWRITE);
4810 if (WARN_ON(!buffer))
4811 return 0;
4812
4813 /* Disable buffer so that threads can't write to it yet */
4814 ring_buffer_record_off(buffer);
4815
4816 for_each_online_cpu(cpu) {
4817 rb_data[cpu].buffer = buffer;
4818 rb_data[cpu].cpu = cpu;
4819 rb_data[cpu].cnt = cpu;
4820 rb_threads[cpu] = kthread_create(rb_test, &rb_data[cpu],
4821 "rbtester/%d", cpu);
4822 if (WARN_ON(!rb_threads[cpu])) {
4823 pr_cont("FAILED\n");
4824 ret = -1;
4825 goto out_free;
4826 }
4827
4828 kthread_bind(rb_threads[cpu], cpu);
4829 wake_up_process(rb_threads[cpu]);
4830 }
4831
4832 /* Now create the rb hammer! */
4833 rb_hammer = kthread_run(rb_hammer_test, NULL, "rbhammer");
4834 if (WARN_ON(!rb_hammer)) {
4835 pr_cont("FAILED\n");
4836 ret = -1;
4837 goto out_free;
4838 }
4839
4840 ring_buffer_record_on(buffer);
4841 /*
4842 * Show buffer is enabled before setting rb_test_started.
4843 * Yes there's a small race window where events could be
4844 * dropped and the thread wont catch it. But when a ring
4845 * buffer gets enabled, there will always be some kind of
4846 * delay before other CPUs see it. Thus, we don't care about
4847 * those dropped events. We care about events dropped after
4848 * the threads see that the buffer is active.
4849 */
4850 smp_wmb();
4851 rb_test_started = true;
4852
4853 set_current_state(TASK_INTERRUPTIBLE);
4854 /* Just run for 10 seconds */;
4855 schedule_timeout(10 * HZ);
4856
4857 kthread_stop(rb_hammer);
4858
4859 out_free:
4860 for_each_online_cpu(cpu) {
4861 if (!rb_threads[cpu])
4862 break;
4863 kthread_stop(rb_threads[cpu]);
4864 }
4865 if (ret) {
4866 ring_buffer_free(buffer);
4867 return ret;
4868 }
4869
4870 /* Report! */
4871 pr_info("finished\n");
4872 for_each_online_cpu(cpu) {
4873 struct ring_buffer_event *event;
4874 struct rb_test_data *data = &rb_data[cpu];
4875 struct rb_item *item;
4876 unsigned long total_events;
4877 unsigned long total_dropped;
4878 unsigned long total_written;
4879 unsigned long total_alloc;
4880 unsigned long total_read = 0;
4881 unsigned long total_size = 0;
4882 unsigned long total_len = 0;
4883 unsigned long total_lost = 0;
4884 unsigned long lost;
4885 int big_event_size;
4886 int small_event_size;
4887
4888 ret = -1;
4889
4890 total_events = data->events + data->events_nested;
4891 total_written = data->bytes_written + data->bytes_written_nested;
4892 total_alloc = data->bytes_alloc + data->bytes_alloc_nested;
4893 total_dropped = data->bytes_dropped + data->bytes_dropped_nested;
4894
4895 big_event_size = data->max_size + data->max_size_nested;
4896 small_event_size = data->min_size + data->min_size_nested;
4897
4898 pr_info("CPU %d:\n", cpu);
4899 pr_info(" events: %ld\n", total_events);
4900 pr_info(" dropped bytes: %ld\n", total_dropped);
4901 pr_info(" alloced bytes: %ld\n", total_alloc);
4902 pr_info(" written bytes: %ld\n", total_written);
4903 pr_info(" biggest event: %d\n", big_event_size);
4904 pr_info(" smallest event: %d\n", small_event_size);
4905
4906 if (RB_WARN_ON(buffer, total_dropped))
4907 break;
4908
4909 ret = 0;
4910
4911 while ((event = ring_buffer_consume(buffer, cpu, NULL, &lost))) {
4912 total_lost += lost;
4913 item = ring_buffer_event_data(event);
4914 total_len += ring_buffer_event_length(event);
4915 total_size += item->size + sizeof(struct rb_item);
4916 if (memcmp(&item->str[0], rb_string, item->size) != 0) {
4917 pr_info("FAILED!\n");
4918 pr_info("buffer had: %.*s\n", item->size, item->str);
4919 pr_info("expected: %.*s\n", item->size, rb_string);
4920 RB_WARN_ON(buffer, 1);
4921 ret = -1;
4922 break;
4923 }
4924 total_read++;
4925 }
4926 if (ret)
4927 break;
4928
4929 ret = -1;
4930
4931 pr_info(" read events: %ld\n", total_read);
4932 pr_info(" lost events: %ld\n", total_lost);
4933 pr_info(" total events: %ld\n", total_lost + total_read);
4934 pr_info(" recorded len bytes: %ld\n", total_len);
4935 pr_info(" recorded size bytes: %ld\n", total_size);
4936 if (total_lost)
4937 pr_info(" With dropped events, record len and size may not match\n"
4938 " alloced and written from above\n");
4939 if (!total_lost) {
4940 if (RB_WARN_ON(buffer, total_len != total_alloc ||
4941 total_size != total_written))
4942 break;
4943 }
4944 if (RB_WARN_ON(buffer, total_lost + total_read != total_events))
4945 break;
4946
4947 ret = 0;
4948 }
4949 if (!ret)
4950 pr_info("Ring buffer PASSED!\n");
4951
4952 ring_buffer_free(buffer);
4953 return 0;
4954}
4955
4956late_initcall(test_ringbuffer);
4957#endif /* CONFIG_RING_BUFFER_STARTUP_TEST */