blob: 2d5eb332082753b7c96df402a2017c03dc96cd45 [file] [log] [blame]
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001/*
2 * Generic ring buffer
3 *
4 * Copyright (C) 2008 Steven Rostedt <srostedt@redhat.com>
5 */
6#include <linux/ring_buffer.h>
Ingo Molnar14131f22009-02-26 18:47:11 +01007#include <linux/trace_clock.h>
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04008#include <linux/spinlock.h>
9#include <linux/debugfs.h>
10#include <linux/uaccess.h>
Steven Rostedta81bd802009-02-06 01:45:16 -050011#include <linux/hardirq.h>
Vegard Nossum1744a212009-02-28 08:29:44 +010012#include <linux/kmemcheck.h>
Steven Rostedt7a8e76a2008-09-29 23:02:38 -040013#include <linux/module.h>
14#include <linux/percpu.h>
15#include <linux/mutex.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090016#include <linux/slab.h>
Steven Rostedt7a8e76a2008-09-29 23:02:38 -040017#include <linux/init.h>
18#include <linux/hash.h>
19#include <linux/list.h>
Steven Rostedt554f7862009-03-11 22:00:13 -040020#include <linux/cpu.h>
Steven Rostedt7a8e76a2008-09-29 23:02:38 -040021#include <linux/fs.h>
22
Christoph Lameter79615762010-01-05 15:34:50 +090023#include <asm/local.h>
Steven Rostedt182e9f52008-11-03 23:15:56 -050024#include "trace.h"
25
Steven Rostedt033601a2008-11-21 12:41:55 -050026/*
Steven Rostedtd1b182a2009-04-15 16:53:47 -040027 * The ring buffer header is special. We must manually up keep it.
28 */
29int ring_buffer_print_entry_header(struct trace_seq *s)
30{
31 int ret;
32
Lai Jiangshan334d4162009-04-24 11:27:05 +080033 ret = trace_seq_printf(s, "# compressed entry header\n");
34 ret = trace_seq_printf(s, "\ttype_len : 5 bits\n");
Steven Rostedtd1b182a2009-04-15 16:53:47 -040035 ret = trace_seq_printf(s, "\ttime_delta : 27 bits\n");
36 ret = trace_seq_printf(s, "\tarray : 32 bits\n");
37 ret = trace_seq_printf(s, "\n");
38 ret = trace_seq_printf(s, "\tpadding : type == %d\n",
39 RINGBUF_TYPE_PADDING);
40 ret = trace_seq_printf(s, "\ttime_extend : type == %d\n",
41 RINGBUF_TYPE_TIME_EXTEND);
Lai Jiangshan334d4162009-04-24 11:27:05 +080042 ret = trace_seq_printf(s, "\tdata max type_len == %d\n",
43 RINGBUF_TYPE_DATA_TYPE_LEN_MAX);
Steven Rostedtd1b182a2009-04-15 16:53:47 -040044
45 return ret;
46}
47
48/*
Steven Rostedt5cc98542009-03-12 22:24:17 -040049 * The ring buffer is made up of a list of pages. A separate list of pages is
50 * allocated for each CPU. A writer may only write to a buffer that is
51 * associated with the CPU it is currently executing on. A reader may read
52 * from any per cpu buffer.
53 *
54 * The reader is special. For each per cpu buffer, the reader has its own
55 * reader page. When a reader has read the entire reader page, this reader
56 * page is swapped with another page in the ring buffer.
57 *
58 * Now, as long as the writer is off the reader page, the reader can do what
59 * ever it wants with that page. The writer will never write to that page
60 * again (as long as it is out of the ring buffer).
61 *
62 * Here's some silly ASCII art.
63 *
64 * +------+
65 * |reader| RING BUFFER
66 * |page |
67 * +------+ +---+ +---+ +---+
68 * | |-->| |-->| |
69 * +---+ +---+ +---+
70 * ^ |
71 * | |
72 * +---------------+
73 *
74 *
75 * +------+
76 * |reader| RING BUFFER
77 * |page |------------------v
78 * +------+ +---+ +---+ +---+
79 * | |-->| |-->| |
80 * +---+ +---+ +---+
81 * ^ |
82 * | |
83 * +---------------+
84 *
85 *
86 * +------+
87 * |reader| RING BUFFER
88 * |page |------------------v
89 * +------+ +---+ +---+ +---+
90 * ^ | |-->| |-->| |
91 * | +---+ +---+ +---+
92 * | |
93 * | |
94 * +------------------------------+
95 *
96 *
97 * +------+
98 * |buffer| RING BUFFER
99 * |page |------------------v
100 * +------+ +---+ +---+ +---+
101 * ^ | | | |-->| |
102 * | New +---+ +---+ +---+
103 * | Reader------^ |
104 * | page |
105 * +------------------------------+
106 *
107 *
108 * After we make this swap, the reader can hand this page off to the splice
109 * code and be done with it. It can even allocate a new page if it needs to
110 * and swap that into the ring buffer.
111 *
112 * We will be using cmpxchg soon to make all this lockless.
113 *
114 */
115
116/*
Steven Rostedt033601a2008-11-21 12:41:55 -0500117 * A fast way to enable or disable all ring buffers is to
118 * call tracing_on or tracing_off. Turning off the ring buffers
119 * prevents all ring buffers from being recorded to.
120 * Turning this switch on, makes it OK to write to the
121 * ring buffer, if the ring buffer is enabled itself.
122 *
123 * There's three layers that must be on in order to write
124 * to the ring buffer.
125 *
126 * 1) This global flag must be set.
127 * 2) The ring buffer must be enabled for recording.
128 * 3) The per cpu buffer must be enabled for recording.
129 *
130 * In case of an anomaly, this global flag has a bit set that
131 * will permantly disable all ring buffers.
132 */
133
134/*
135 * Global flag to disable all recording to ring buffers
136 * This has two bits: ON, DISABLED
137 *
138 * ON DISABLED
139 * ---- ----------
140 * 0 0 : ring buffers are off
141 * 1 0 : ring buffers are on
142 * X 1 : ring buffers are permanently disabled
143 */
144
145enum {
146 RB_BUFFERS_ON_BIT = 0,
147 RB_BUFFERS_DISABLED_BIT = 1,
148};
149
150enum {
151 RB_BUFFERS_ON = 1 << RB_BUFFERS_ON_BIT,
152 RB_BUFFERS_DISABLED = 1 << RB_BUFFERS_DISABLED_BIT,
153};
154
Hannes Eder5e398412009-02-10 19:44:34 +0100155static unsigned long ring_buffer_flags __read_mostly = RB_BUFFERS_ON;
Steven Rostedta3583242008-11-11 15:01:42 -0500156
Steven Rostedt499e5472012-02-22 15:50:28 -0500157/* Used for individual buffers (after the counter) */
158#define RB_BUFFER_OFF (1 << 20)
159
Steven Rostedt474d32b2009-03-03 19:51:40 -0500160#define BUF_PAGE_HDR_SIZE offsetof(struct buffer_data_page, data)
161
Steven Rostedta3583242008-11-11 15:01:42 -0500162/**
Steven Rostedt033601a2008-11-21 12:41:55 -0500163 * tracing_off_permanent - permanently disable ring buffers
164 *
165 * This function, once called, will disable all ring buffers
Wenji Huangc3706f02009-02-10 01:03:18 -0500166 * permanently.
Steven Rostedt033601a2008-11-21 12:41:55 -0500167 */
168void tracing_off_permanent(void)
169{
170 set_bit(RB_BUFFERS_DISABLED_BIT, &ring_buffer_flags);
Steven Rostedta3583242008-11-11 15:01:42 -0500171}
172
Steven Rostedte3d6bf02009-03-03 13:53:07 -0500173#define RB_EVNT_HDR_SIZE (offsetof(struct ring_buffer_event, array))
Andrew Morton67d34722009-01-09 12:27:09 -0800174#define RB_ALIGNMENT 4U
Lai Jiangshan334d4162009-04-24 11:27:05 +0800175#define RB_MAX_SMALL_DATA (RB_ALIGNMENT * RINGBUF_TYPE_DATA_TYPE_LEN_MAX)
Steven Rostedtc7b09302009-06-11 11:12:00 -0400176#define RB_EVNT_MIN_SIZE 8U /* two 32bit words */
Lai Jiangshan334d4162009-04-24 11:27:05 +0800177
Steven Rostedt22710482010-03-18 17:54:19 -0400178#if !defined(CONFIG_64BIT) || defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)
179# define RB_FORCE_8BYTE_ALIGNMENT 0
180# define RB_ARCH_ALIGNMENT RB_ALIGNMENT
181#else
182# define RB_FORCE_8BYTE_ALIGNMENT 1
183# define RB_ARCH_ALIGNMENT 8U
184#endif
185
Lai Jiangshan334d4162009-04-24 11:27:05 +0800186/* define RINGBUF_TYPE_DATA for 'case RINGBUF_TYPE_DATA:' */
187#define RINGBUF_TYPE_DATA 0 ... RINGBUF_TYPE_DATA_TYPE_LEN_MAX
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400188
189enum {
190 RB_LEN_TIME_EXTEND = 8,
191 RB_LEN_TIME_STAMP = 16,
192};
193
Steven Rostedt69d1b832010-10-07 18:18:05 -0400194#define skip_time_extend(event) \
195 ((struct ring_buffer_event *)((char *)event + RB_LEN_TIME_EXTEND))
196
Tom Zanussi2d622712009-03-22 03:30:49 -0500197static inline int rb_null_event(struct ring_buffer_event *event)
198{
Steven Rostedta1863c22009-09-03 10:23:58 -0400199 return event->type_len == RINGBUF_TYPE_PADDING && !event->time_delta;
Tom Zanussi2d622712009-03-22 03:30:49 -0500200}
201
202static void rb_event_set_padding(struct ring_buffer_event *event)
203{
Steven Rostedta1863c22009-09-03 10:23:58 -0400204 /* padding has a NULL time_delta */
Lai Jiangshan334d4162009-04-24 11:27:05 +0800205 event->type_len = RINGBUF_TYPE_PADDING;
Tom Zanussi2d622712009-03-22 03:30:49 -0500206 event->time_delta = 0;
207}
208
Tom Zanussi2d622712009-03-22 03:30:49 -0500209static unsigned
210rb_event_data_length(struct ring_buffer_event *event)
211{
212 unsigned length;
213
Lai Jiangshan334d4162009-04-24 11:27:05 +0800214 if (event->type_len)
215 length = event->type_len * RB_ALIGNMENT;
Tom Zanussi2d622712009-03-22 03:30:49 -0500216 else
217 length = event->array[0];
218 return length + RB_EVNT_HDR_SIZE;
219}
220
Steven Rostedt69d1b832010-10-07 18:18:05 -0400221/*
222 * Return the length of the given event. Will return
223 * the length of the time extend if the event is a
224 * time extend.
225 */
226static inline unsigned
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400227rb_event_length(struct ring_buffer_event *event)
228{
Lai Jiangshan334d4162009-04-24 11:27:05 +0800229 switch (event->type_len) {
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400230 case RINGBUF_TYPE_PADDING:
Tom Zanussi2d622712009-03-22 03:30:49 -0500231 if (rb_null_event(event))
232 /* undefined */
233 return -1;
Lai Jiangshan334d4162009-04-24 11:27:05 +0800234 return event->array[0] + RB_EVNT_HDR_SIZE;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400235
236 case RINGBUF_TYPE_TIME_EXTEND:
237 return RB_LEN_TIME_EXTEND;
238
239 case RINGBUF_TYPE_TIME_STAMP:
240 return RB_LEN_TIME_STAMP;
241
242 case RINGBUF_TYPE_DATA:
Tom Zanussi2d622712009-03-22 03:30:49 -0500243 return rb_event_data_length(event);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400244 default:
245 BUG();
246 }
247 /* not hit */
248 return 0;
249}
250
Steven Rostedt69d1b832010-10-07 18:18:05 -0400251/*
252 * Return total length of time extend and data,
253 * or just the event length for all other events.
254 */
255static inline unsigned
256rb_event_ts_length(struct ring_buffer_event *event)
257{
258 unsigned len = 0;
259
260 if (event->type_len == RINGBUF_TYPE_TIME_EXTEND) {
261 /* time extends include the data event after it */
262 len = RB_LEN_TIME_EXTEND;
263 event = skip_time_extend(event);
264 }
265 return len + rb_event_length(event);
266}
267
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400268/**
269 * ring_buffer_event_length - return the length of the event
270 * @event: the event to get the length of
Steven Rostedt69d1b832010-10-07 18:18:05 -0400271 *
272 * Returns the size of the data load of a data event.
273 * If the event is something other than a data event, it
274 * returns the size of the event itself. With the exception
275 * of a TIME EXTEND, where it still returns the size of the
276 * data load of the data event after it.
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400277 */
278unsigned ring_buffer_event_length(struct ring_buffer_event *event)
279{
Steven Rostedt69d1b832010-10-07 18:18:05 -0400280 unsigned length;
281
282 if (event->type_len == RINGBUF_TYPE_TIME_EXTEND)
283 event = skip_time_extend(event);
284
285 length = rb_event_length(event);
Lai Jiangshan334d4162009-04-24 11:27:05 +0800286 if (event->type_len > RINGBUF_TYPE_DATA_TYPE_LEN_MAX)
Robert Richter465634a2009-01-07 15:32:11 +0100287 return length;
288 length -= RB_EVNT_HDR_SIZE;
289 if (length > RB_MAX_SMALL_DATA + sizeof(event->array[0]))
290 length -= sizeof(event->array[0]);
291 return length;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400292}
Robert Richterc4f50182008-12-11 16:49:22 +0100293EXPORT_SYMBOL_GPL(ring_buffer_event_length);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400294
295/* inline for ring buffer fast paths */
Andrew Morton34a148b2009-01-09 12:27:09 -0800296static void *
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400297rb_event_data(struct ring_buffer_event *event)
298{
Steven Rostedt69d1b832010-10-07 18:18:05 -0400299 if (event->type_len == RINGBUF_TYPE_TIME_EXTEND)
300 event = skip_time_extend(event);
Lai Jiangshan334d4162009-04-24 11:27:05 +0800301 BUG_ON(event->type_len > RINGBUF_TYPE_DATA_TYPE_LEN_MAX);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400302 /* If length is in len field, then array[0] has the data */
Lai Jiangshan334d4162009-04-24 11:27:05 +0800303 if (event->type_len)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400304 return (void *)&event->array[0];
305 /* Otherwise length is in array[0] and array[1] has the data */
306 return (void *)&event->array[1];
307}
308
309/**
310 * ring_buffer_event_data - return the data of the event
311 * @event: the event to get the data from
312 */
313void *ring_buffer_event_data(struct ring_buffer_event *event)
314{
315 return rb_event_data(event);
316}
Robert Richterc4f50182008-12-11 16:49:22 +0100317EXPORT_SYMBOL_GPL(ring_buffer_event_data);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400318
319#define for_each_buffer_cpu(buffer, cpu) \
Rusty Russell9e01c1b2009-01-01 10:12:22 +1030320 for_each_cpu(cpu, buffer->cpumask)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400321
322#define TS_SHIFT 27
323#define TS_MASK ((1ULL << TS_SHIFT) - 1)
324#define TS_DELTA_TEST (~TS_MASK)
325
Steven Rostedt66a8cb92010-03-31 13:21:56 -0400326/* Flag when events were overwritten */
327#define RB_MISSED_EVENTS (1 << 31)
Steven Rostedtff0ff842010-03-31 22:11:42 -0400328/* Missed count stored at end */
329#define RB_MISSED_STORED (1 << 30)
Steven Rostedt66a8cb92010-03-31 13:21:56 -0400330
Steven Rostedtabc9b562008-12-02 15:34:06 -0500331struct buffer_data_page {
Steven Rostedte4c2ce82008-10-01 11:14:54 -0400332 u64 time_stamp; /* page time stamp */
Wenji Huangc3706f02009-02-10 01:03:18 -0500333 local_t commit; /* write committed index */
Steven Rostedtabc9b562008-12-02 15:34:06 -0500334 unsigned char data[]; /* data of buffer page */
335};
336
Steven Rostedt77ae3652009-03-27 11:00:29 -0400337/*
338 * Note, the buffer_page list must be first. The buffer pages
339 * are allocated in cache lines, which means that each buffer
340 * page will be at the beginning of a cache line, and thus
341 * the least significant bits will be zero. We use this to
342 * add flags in the list struct pointers, to make the ring buffer
343 * lockless.
344 */
Steven Rostedtabc9b562008-12-02 15:34:06 -0500345struct buffer_page {
Steven Rostedt778c55d2009-05-01 18:44:45 -0400346 struct list_head list; /* list of buffer pages */
Steven Rostedtabc9b562008-12-02 15:34:06 -0500347 local_t write; /* index for next write */
Steven Rostedt6f807ac2008-10-04 02:00:58 -0400348 unsigned read; /* index for next read */
Steven Rostedt778c55d2009-05-01 18:44:45 -0400349 local_t entries; /* entries on this page */
Steven Rostedtff0ff842010-03-31 22:11:42 -0400350 unsigned long real_end; /* real end of data */
Steven Rostedtabc9b562008-12-02 15:34:06 -0500351 struct buffer_data_page *page; /* Actual data page */
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400352};
353
Steven Rostedt77ae3652009-03-27 11:00:29 -0400354/*
355 * The buffer page counters, write and entries, must be reset
356 * atomically when crossing page boundaries. To synchronize this
357 * update, two counters are inserted into the number. One is
358 * the actual counter for the write position or count on the page.
359 *
360 * The other is a counter of updaters. Before an update happens
361 * the update partition of the counter is incremented. This will
362 * allow the updater to update the counter atomically.
363 *
364 * The counter is 20 bits, and the state data is 12.
365 */
366#define RB_WRITE_MASK 0xfffff
367#define RB_WRITE_INTCNT (1 << 20)
368
Steven Rostedt044fa782008-12-02 23:50:03 -0500369static void rb_init_page(struct buffer_data_page *bpage)
Steven Rostedtabc9b562008-12-02 15:34:06 -0500370{
Steven Rostedt044fa782008-12-02 23:50:03 -0500371 local_set(&bpage->commit, 0);
Steven Rostedtabc9b562008-12-02 15:34:06 -0500372}
373
Steven Rostedt474d32b2009-03-03 19:51:40 -0500374/**
375 * ring_buffer_page_len - the size of data on the page.
376 * @page: The page to read
377 *
378 * Returns the amount of data on the page, including buffer page header.
379 */
Steven Rostedtef7a4a12009-03-03 00:27:49 -0500380size_t ring_buffer_page_len(void *page)
381{
Steven Rostedt474d32b2009-03-03 19:51:40 -0500382 return local_read(&((struct buffer_data_page *)page)->commit)
383 + BUF_PAGE_HDR_SIZE;
Steven Rostedtef7a4a12009-03-03 00:27:49 -0500384}
385
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400386/*
Steven Rostedted568292008-09-29 23:02:40 -0400387 * Also stolen from mm/slob.c. Thanks to Mathieu Desnoyers for pointing
388 * this issue out.
389 */
Andrew Morton34a148b2009-01-09 12:27:09 -0800390static void free_buffer_page(struct buffer_page *bpage)
Steven Rostedted568292008-09-29 23:02:40 -0400391{
Andrew Morton34a148b2009-01-09 12:27:09 -0800392 free_page((unsigned long)bpage->page);
Steven Rostedte4c2ce82008-10-01 11:14:54 -0400393 kfree(bpage);
Steven Rostedted568292008-09-29 23:02:40 -0400394}
395
396/*
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400397 * We need to fit the time_stamp delta into 27 bits.
398 */
399static inline int test_time_stamp(u64 delta)
400{
401 if (delta & TS_DELTA_TEST)
402 return 1;
403 return 0;
404}
405
Steven Rostedt474d32b2009-03-03 19:51:40 -0500406#define BUF_PAGE_SIZE (PAGE_SIZE - BUF_PAGE_HDR_SIZE)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400407
Steven Rostedtbe957c42009-05-11 14:42:53 -0400408/* Max payload is BUF_PAGE_SIZE - header (8bytes) */
409#define BUF_MAX_DATA_SIZE (BUF_PAGE_SIZE - (sizeof(u32) * 2))
410
Steven Rostedtd1b182a2009-04-15 16:53:47 -0400411int ring_buffer_print_page_header(struct trace_seq *s)
412{
413 struct buffer_data_page field;
414 int ret;
415
416 ret = trace_seq_printf(s, "\tfield: u64 timestamp;\t"
Tom Zanussi26a50742009-10-06 01:09:50 -0500417 "offset:0;\tsize:%u;\tsigned:%u;\n",
418 (unsigned int)sizeof(field.time_stamp),
419 (unsigned int)is_signed_type(u64));
Steven Rostedtd1b182a2009-04-15 16:53:47 -0400420
421 ret = trace_seq_printf(s, "\tfield: local_t commit;\t"
Tom Zanussi26a50742009-10-06 01:09:50 -0500422 "offset:%u;\tsize:%u;\tsigned:%u;\n",
Steven Rostedtd1b182a2009-04-15 16:53:47 -0400423 (unsigned int)offsetof(typeof(field), commit),
Tom Zanussi26a50742009-10-06 01:09:50 -0500424 (unsigned int)sizeof(field.commit),
425 (unsigned int)is_signed_type(long));
Steven Rostedtd1b182a2009-04-15 16:53:47 -0400426
Steven Rostedt66a8cb92010-03-31 13:21:56 -0400427 ret = trace_seq_printf(s, "\tfield: int overwrite;\t"
428 "offset:%u;\tsize:%u;\tsigned:%u;\n",
429 (unsigned int)offsetof(typeof(field), commit),
430 1,
431 (unsigned int)is_signed_type(long));
432
Steven Rostedtd1b182a2009-04-15 16:53:47 -0400433 ret = trace_seq_printf(s, "\tfield: char data;\t"
Tom Zanussi26a50742009-10-06 01:09:50 -0500434 "offset:%u;\tsize:%u;\tsigned:%u;\n",
Steven Rostedtd1b182a2009-04-15 16:53:47 -0400435 (unsigned int)offsetof(typeof(field), data),
Tom Zanussi26a50742009-10-06 01:09:50 -0500436 (unsigned int)BUF_PAGE_SIZE,
437 (unsigned int)is_signed_type(char));
Steven Rostedtd1b182a2009-04-15 16:53:47 -0400438
439 return ret;
440}
441
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400442/*
443 * head_page == tail_page && head == tail then buffer is empty.
444 */
445struct ring_buffer_per_cpu {
446 int cpu;
Richard Kennedy985023d2010-03-25 11:27:36 +0000447 atomic_t record_disabled;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400448 struct ring_buffer *buffer;
Thomas Gleixner5389f6f2009-07-25 17:13:33 +0200449 raw_spinlock_t reader_lock; /* serialize readers */
Thomas Gleixner445c8952009-12-02 19:49:50 +0100450 arch_spinlock_t lock;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400451 struct lock_class_key lock_key;
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -0800452 unsigned int nr_pages;
Steven Rostedt3adc54f2009-03-30 15:32:01 -0400453 struct list_head *pages;
Steven Rostedt6f807ac2008-10-04 02:00:58 -0400454 struct buffer_page *head_page; /* read from head */
455 struct buffer_page *tail_page; /* write to tail */
Wenji Huangc3706f02009-02-10 01:03:18 -0500456 struct buffer_page *commit_page; /* committed pages */
Steven Rostedtd7690412008-10-01 00:29:53 -0400457 struct buffer_page *reader_page;
Steven Rostedt66a8cb92010-03-31 13:21:56 -0400458 unsigned long lost_events;
459 unsigned long last_overrun;
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -0700460 local_t entries_bytes;
Steven Rostedt77ae3652009-03-27 11:00:29 -0400461 local_t commit_overrun;
462 local_t overrun;
Steven Rostedte4906ef2009-04-30 20:49:44 -0400463 local_t entries;
Steven Rostedtfa743952009-06-16 12:37:57 -0400464 local_t committing;
465 local_t commits;
Steven Rostedt77ae3652009-03-27 11:00:29 -0400466 unsigned long read;
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -0700467 unsigned long read_bytes;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400468 u64 write_stamp;
469 u64 read_stamp;
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -0800470 /* ring buffer pages to update, > 0 to add, < 0 to remove */
471 int nr_pages_to_update;
472 struct list_head new_pages; /* new pages to add */
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400473};
474
475struct ring_buffer {
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400476 unsigned flags;
477 int cpus;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400478 atomic_t record_disabled;
Arnaldo Carvalho de Melo00f62f62009-02-09 17:04:06 -0200479 cpumask_var_t cpumask;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400480
Peter Zijlstra1f8a6a12009-06-08 18:18:39 +0200481 struct lock_class_key *reader_lock_key;
482
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400483 struct mutex mutex;
484
485 struct ring_buffer_per_cpu **buffers;
Steven Rostedt554f7862009-03-11 22:00:13 -0400486
Steven Rostedt59222ef2009-03-12 11:46:03 -0400487#ifdef CONFIG_HOTPLUG_CPU
Steven Rostedt554f7862009-03-11 22:00:13 -0400488 struct notifier_block cpu_notify;
489#endif
Steven Rostedt37886f62009-03-17 17:22:06 -0400490 u64 (*clock)(void);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400491};
492
493struct ring_buffer_iter {
494 struct ring_buffer_per_cpu *cpu_buffer;
495 unsigned long head;
496 struct buffer_page *head_page;
Steven Rostedt492a74f2010-01-25 15:17:47 -0500497 struct buffer_page *cache_reader_page;
498 unsigned long cache_read;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400499 u64 read_stamp;
500};
501
Steven Rostedtf536aaf2008-11-10 23:07:30 -0500502/* buffer may be either ring_buffer or ring_buffer_per_cpu */
Steven Rostedt077c5402009-09-03 19:53:46 -0400503#define RB_WARN_ON(b, cond) \
504 ({ \
505 int _____ret = unlikely(cond); \
506 if (_____ret) { \
507 if (__same_type(*(b), struct ring_buffer_per_cpu)) { \
508 struct ring_buffer_per_cpu *__b = \
509 (void *)b; \
510 atomic_inc(&__b->buffer->record_disabled); \
511 } else \
512 atomic_inc(&b->record_disabled); \
513 WARN_ON(1); \
514 } \
515 _____ret; \
Steven Rostedt3e89c7b2008-11-11 15:28:41 -0500516 })
Steven Rostedtf536aaf2008-11-10 23:07:30 -0500517
Steven Rostedt37886f62009-03-17 17:22:06 -0400518/* Up this if you want to test the TIME_EXTENTS and normalization */
519#define DEBUG_SHIFT 0
520
Jiri Olsa6d3f1e12009-10-23 19:36:19 -0400521static inline u64 rb_time_stamp(struct ring_buffer *buffer)
Steven Rostedt88eb0122009-05-11 16:28:23 -0400522{
523 /* shift to debug/test normalization and TIME_EXTENTS */
524 return buffer->clock() << DEBUG_SHIFT;
525}
526
Steven Rostedt37886f62009-03-17 17:22:06 -0400527u64 ring_buffer_time_stamp(struct ring_buffer *buffer, int cpu)
528{
529 u64 time;
530
531 preempt_disable_notrace();
Jiri Olsa6d3f1e12009-10-23 19:36:19 -0400532 time = rb_time_stamp(buffer);
Steven Rostedt37886f62009-03-17 17:22:06 -0400533 preempt_enable_no_resched_notrace();
534
535 return time;
536}
537EXPORT_SYMBOL_GPL(ring_buffer_time_stamp);
538
539void ring_buffer_normalize_time_stamp(struct ring_buffer *buffer,
540 int cpu, u64 *ts)
541{
542 /* Just stupid testing the normalize function and deltas */
543 *ts >>= DEBUG_SHIFT;
544}
545EXPORT_SYMBOL_GPL(ring_buffer_normalize_time_stamp);
546
Steven Rostedt77ae3652009-03-27 11:00:29 -0400547/*
548 * Making the ring buffer lockless makes things tricky.
549 * Although writes only happen on the CPU that they are on,
550 * and they only need to worry about interrupts. Reads can
551 * happen on any CPU.
552 *
553 * The reader page is always off the ring buffer, but when the
554 * reader finishes with a page, it needs to swap its page with
555 * a new one from the buffer. The reader needs to take from
556 * the head (writes go to the tail). But if a writer is in overwrite
557 * mode and wraps, it must push the head page forward.
558 *
559 * Here lies the problem.
560 *
561 * The reader must be careful to replace only the head page, and
562 * not another one. As described at the top of the file in the
563 * ASCII art, the reader sets its old page to point to the next
564 * page after head. It then sets the page after head to point to
565 * the old reader page. But if the writer moves the head page
566 * during this operation, the reader could end up with the tail.
567 *
568 * We use cmpxchg to help prevent this race. We also do something
569 * special with the page before head. We set the LSB to 1.
570 *
571 * When the writer must push the page forward, it will clear the
572 * bit that points to the head page, move the head, and then set
573 * the bit that points to the new head page.
574 *
575 * We also don't want an interrupt coming in and moving the head
576 * page on another writer. Thus we use the second LSB to catch
577 * that too. Thus:
578 *
579 * head->list->prev->next bit 1 bit 0
580 * ------- -------
581 * Normal page 0 0
582 * Points to head page 0 1
583 * New head page 1 0
584 *
585 * Note we can not trust the prev pointer of the head page, because:
586 *
587 * +----+ +-----+ +-----+
588 * | |------>| T |---X--->| N |
589 * | |<------| | | |
590 * +----+ +-----+ +-----+
591 * ^ ^ |
592 * | +-----+ | |
593 * +----------| R |----------+ |
594 * | |<-----------+
595 * +-----+
596 *
597 * Key: ---X--> HEAD flag set in pointer
598 * T Tail page
599 * R Reader page
600 * N Next page
601 *
602 * (see __rb_reserve_next() to see where this happens)
603 *
604 * What the above shows is that the reader just swapped out
605 * the reader page with a page in the buffer, but before it
606 * could make the new header point back to the new page added
607 * it was preempted by a writer. The writer moved forward onto
608 * the new page added by the reader and is about to move forward
609 * again.
610 *
611 * You can see, it is legitimate for the previous pointer of
612 * the head (or any page) not to point back to itself. But only
613 * temporarially.
614 */
615
616#define RB_PAGE_NORMAL 0UL
617#define RB_PAGE_HEAD 1UL
618#define RB_PAGE_UPDATE 2UL
619
620
621#define RB_FLAG_MASK 3UL
622
623/* PAGE_MOVED is not part of the mask */
624#define RB_PAGE_MOVED 4UL
625
626/*
627 * rb_list_head - remove any bit
628 */
629static struct list_head *rb_list_head(struct list_head *list)
630{
631 unsigned long val = (unsigned long)list;
632
633 return (struct list_head *)(val & ~RB_FLAG_MASK);
634}
635
636/*
Jiri Olsa6d3f1e12009-10-23 19:36:19 -0400637 * rb_is_head_page - test if the given page is the head page
Steven Rostedt77ae3652009-03-27 11:00:29 -0400638 *
639 * Because the reader may move the head_page pointer, we can
640 * not trust what the head page is (it may be pointing to
641 * the reader page). But if the next page is a header page,
642 * its flags will be non zero.
643 */
Jesper Juhl42b16b32011-01-17 00:09:38 +0100644static inline int
Steven Rostedt77ae3652009-03-27 11:00:29 -0400645rb_is_head_page(struct ring_buffer_per_cpu *cpu_buffer,
646 struct buffer_page *page, struct list_head *list)
647{
648 unsigned long val;
649
650 val = (unsigned long)list->next;
651
652 if ((val & ~RB_FLAG_MASK) != (unsigned long)&page->list)
653 return RB_PAGE_MOVED;
654
655 return val & RB_FLAG_MASK;
656}
657
658/*
659 * rb_is_reader_page
660 *
661 * The unique thing about the reader page, is that, if the
662 * writer is ever on it, the previous pointer never points
663 * back to the reader page.
664 */
665static int rb_is_reader_page(struct buffer_page *page)
666{
667 struct list_head *list = page->list.prev;
668
669 return rb_list_head(list->next) != &page->list;
670}
671
672/*
673 * rb_set_list_to_head - set a list_head to be pointing to head.
674 */
675static void rb_set_list_to_head(struct ring_buffer_per_cpu *cpu_buffer,
676 struct list_head *list)
677{
678 unsigned long *ptr;
679
680 ptr = (unsigned long *)&list->next;
681 *ptr |= RB_PAGE_HEAD;
682 *ptr &= ~RB_PAGE_UPDATE;
683}
684
685/*
686 * rb_head_page_activate - sets up head page
687 */
688static void rb_head_page_activate(struct ring_buffer_per_cpu *cpu_buffer)
689{
690 struct buffer_page *head;
691
692 head = cpu_buffer->head_page;
693 if (!head)
694 return;
695
696 /*
697 * Set the previous list pointer to have the HEAD flag.
698 */
699 rb_set_list_to_head(cpu_buffer, head->list.prev);
700}
701
702static void rb_list_head_clear(struct list_head *list)
703{
704 unsigned long *ptr = (unsigned long *)&list->next;
705
706 *ptr &= ~RB_FLAG_MASK;
707}
708
709/*
710 * rb_head_page_dactivate - clears head page ptr (for free list)
711 */
712static void
713rb_head_page_deactivate(struct ring_buffer_per_cpu *cpu_buffer)
714{
715 struct list_head *hd;
716
717 /* Go through the whole list and clear any pointers found. */
718 rb_list_head_clear(cpu_buffer->pages);
719
720 list_for_each(hd, cpu_buffer->pages)
721 rb_list_head_clear(hd);
722}
723
724static int rb_head_page_set(struct ring_buffer_per_cpu *cpu_buffer,
725 struct buffer_page *head,
726 struct buffer_page *prev,
727 int old_flag, int new_flag)
728{
729 struct list_head *list;
730 unsigned long val = (unsigned long)&head->list;
731 unsigned long ret;
732
733 list = &prev->list;
734
735 val &= ~RB_FLAG_MASK;
736
Steven Rostedt08a40812009-09-14 09:31:35 -0400737 ret = cmpxchg((unsigned long *)&list->next,
738 val | old_flag, val | new_flag);
Steven Rostedt77ae3652009-03-27 11:00:29 -0400739
740 /* check if the reader took the page */
741 if ((ret & ~RB_FLAG_MASK) != val)
742 return RB_PAGE_MOVED;
743
744 return ret & RB_FLAG_MASK;
745}
746
747static int rb_head_page_set_update(struct ring_buffer_per_cpu *cpu_buffer,
748 struct buffer_page *head,
749 struct buffer_page *prev,
750 int old_flag)
751{
752 return rb_head_page_set(cpu_buffer, head, prev,
753 old_flag, RB_PAGE_UPDATE);
754}
755
756static int rb_head_page_set_head(struct ring_buffer_per_cpu *cpu_buffer,
757 struct buffer_page *head,
758 struct buffer_page *prev,
759 int old_flag)
760{
761 return rb_head_page_set(cpu_buffer, head, prev,
762 old_flag, RB_PAGE_HEAD);
763}
764
765static int rb_head_page_set_normal(struct ring_buffer_per_cpu *cpu_buffer,
766 struct buffer_page *head,
767 struct buffer_page *prev,
768 int old_flag)
769{
770 return rb_head_page_set(cpu_buffer, head, prev,
771 old_flag, RB_PAGE_NORMAL);
772}
773
774static inline void rb_inc_page(struct ring_buffer_per_cpu *cpu_buffer,
775 struct buffer_page **bpage)
776{
777 struct list_head *p = rb_list_head((*bpage)->list.next);
778
779 *bpage = list_entry(p, struct buffer_page, list);
780}
781
782static struct buffer_page *
783rb_set_head_page(struct ring_buffer_per_cpu *cpu_buffer)
784{
785 struct buffer_page *head;
786 struct buffer_page *page;
787 struct list_head *list;
788 int i;
789
790 if (RB_WARN_ON(cpu_buffer, !cpu_buffer->head_page))
791 return NULL;
792
793 /* sanity check */
794 list = cpu_buffer->pages;
795 if (RB_WARN_ON(cpu_buffer, rb_list_head(list->prev->next) != list))
796 return NULL;
797
798 page = head = cpu_buffer->head_page;
799 /*
800 * It is possible that the writer moves the header behind
801 * where we started, and we miss in one loop.
802 * A second loop should grab the header, but we'll do
803 * three loops just because I'm paranoid.
804 */
805 for (i = 0; i < 3; i++) {
806 do {
807 if (rb_is_head_page(cpu_buffer, page, page->list.prev)) {
808 cpu_buffer->head_page = page;
809 return page;
810 }
811 rb_inc_page(cpu_buffer, &page);
812 } while (page != head);
813 }
814
815 RB_WARN_ON(cpu_buffer, 1);
816
817 return NULL;
818}
819
820static int rb_head_page_replace(struct buffer_page *old,
821 struct buffer_page *new)
822{
823 unsigned long *ptr = (unsigned long *)&old->list.prev->next;
824 unsigned long val;
825 unsigned long ret;
826
827 val = *ptr & ~RB_FLAG_MASK;
828 val |= RB_PAGE_HEAD;
829
Steven Rostedt08a40812009-09-14 09:31:35 -0400830 ret = cmpxchg(ptr, val, (unsigned long)&new->list);
Steven Rostedt77ae3652009-03-27 11:00:29 -0400831
832 return ret == val;
833}
834
835/*
836 * rb_tail_page_update - move the tail page forward
837 *
838 * Returns 1 if moved tail page, 0 if someone else did.
839 */
840static int rb_tail_page_update(struct ring_buffer_per_cpu *cpu_buffer,
841 struct buffer_page *tail_page,
842 struct buffer_page *next_page)
843{
844 struct buffer_page *old_tail;
845 unsigned long old_entries;
846 unsigned long old_write;
847 int ret = 0;
848
849 /*
850 * The tail page now needs to be moved forward.
851 *
852 * We need to reset the tail page, but without messing
853 * with possible erasing of data brought in by interrupts
854 * that have moved the tail page and are currently on it.
855 *
856 * We add a counter to the write field to denote this.
857 */
858 old_write = local_add_return(RB_WRITE_INTCNT, &next_page->write);
859 old_entries = local_add_return(RB_WRITE_INTCNT, &next_page->entries);
860
861 /*
862 * Just make sure we have seen our old_write and synchronize
863 * with any interrupts that come in.
864 */
865 barrier();
866
867 /*
868 * If the tail page is still the same as what we think
869 * it is, then it is up to us to update the tail
870 * pointer.
871 */
872 if (tail_page == cpu_buffer->tail_page) {
873 /* Zero the write counter */
874 unsigned long val = old_write & ~RB_WRITE_MASK;
875 unsigned long eval = old_entries & ~RB_WRITE_MASK;
876
877 /*
878 * This will only succeed if an interrupt did
879 * not come in and change it. In which case, we
880 * do not want to modify it.
Lai Jiangshanda706d82009-07-15 16:27:30 +0800881 *
882 * We add (void) to let the compiler know that we do not care
883 * about the return value of these functions. We use the
884 * cmpxchg to only update if an interrupt did not already
885 * do it for us. If the cmpxchg fails, we don't care.
Steven Rostedt77ae3652009-03-27 11:00:29 -0400886 */
Lai Jiangshanda706d82009-07-15 16:27:30 +0800887 (void)local_cmpxchg(&next_page->write, old_write, val);
888 (void)local_cmpxchg(&next_page->entries, old_entries, eval);
Steven Rostedt77ae3652009-03-27 11:00:29 -0400889
890 /*
891 * No need to worry about races with clearing out the commit.
892 * it only can increment when a commit takes place. But that
893 * only happens in the outer most nested commit.
894 */
895 local_set(&next_page->page->commit, 0);
896
897 old_tail = cmpxchg(&cpu_buffer->tail_page,
898 tail_page, next_page);
899
900 if (old_tail == tail_page)
901 ret = 1;
902 }
903
904 return ret;
905}
906
907static int rb_check_bpage(struct ring_buffer_per_cpu *cpu_buffer,
908 struct buffer_page *bpage)
909{
910 unsigned long val = (unsigned long)bpage;
911
912 if (RB_WARN_ON(cpu_buffer, val & RB_FLAG_MASK))
913 return 1;
914
915 return 0;
916}
917
918/**
919 * rb_check_list - make sure a pointer to a list has the last bits zero
920 */
921static int rb_check_list(struct ring_buffer_per_cpu *cpu_buffer,
922 struct list_head *list)
923{
924 if (RB_WARN_ON(cpu_buffer, rb_list_head(list->prev) != list->prev))
925 return 1;
926 if (RB_WARN_ON(cpu_buffer, rb_list_head(list->next) != list->next))
927 return 1;
928 return 0;
929}
930
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400931/**
932 * check_pages - integrity check of buffer pages
933 * @cpu_buffer: CPU buffer with pages to test
934 *
Wenji Huangc3706f02009-02-10 01:03:18 -0500935 * As a safety measure we check to make sure the data pages have not
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400936 * been corrupted.
937 */
938static int rb_check_pages(struct ring_buffer_per_cpu *cpu_buffer)
939{
Steven Rostedt3adc54f2009-03-30 15:32:01 -0400940 struct list_head *head = cpu_buffer->pages;
Steven Rostedt044fa782008-12-02 23:50:03 -0500941 struct buffer_page *bpage, *tmp;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400942
Steven Rostedt77ae3652009-03-27 11:00:29 -0400943 rb_head_page_deactivate(cpu_buffer);
944
Steven Rostedt3e89c7b2008-11-11 15:28:41 -0500945 if (RB_WARN_ON(cpu_buffer, head->next->prev != head))
946 return -1;
947 if (RB_WARN_ON(cpu_buffer, head->prev->next != head))
948 return -1;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400949
Steven Rostedt77ae3652009-03-27 11:00:29 -0400950 if (rb_check_list(cpu_buffer, head))
951 return -1;
952
Steven Rostedt044fa782008-12-02 23:50:03 -0500953 list_for_each_entry_safe(bpage, tmp, head, list) {
Steven Rostedt3e89c7b2008-11-11 15:28:41 -0500954 if (RB_WARN_ON(cpu_buffer,
Steven Rostedt044fa782008-12-02 23:50:03 -0500955 bpage->list.next->prev != &bpage->list))
Steven Rostedt3e89c7b2008-11-11 15:28:41 -0500956 return -1;
957 if (RB_WARN_ON(cpu_buffer,
Steven Rostedt044fa782008-12-02 23:50:03 -0500958 bpage->list.prev->next != &bpage->list))
Steven Rostedt3e89c7b2008-11-11 15:28:41 -0500959 return -1;
Steven Rostedt77ae3652009-03-27 11:00:29 -0400960 if (rb_check_list(cpu_buffer, &bpage->list))
961 return -1;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400962 }
963
Steven Rostedt77ae3652009-03-27 11:00:29 -0400964 rb_head_page_activate(cpu_buffer);
965
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400966 return 0;
967}
968
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -0800969static int __rb_allocate_pages(int nr_pages, struct list_head *pages, int cpu)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400970{
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -0800971 int i;
Steven Rostedt044fa782008-12-02 23:50:03 -0500972 struct buffer_page *bpage, *tmp;
Steven Rostedt3adc54f2009-03-30 15:32:01 -0400973
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400974 for (i = 0; i < nr_pages; i++) {
Vaibhav Nagarnaik7ea59062011-05-03 17:56:42 -0700975 struct page *page;
Vaibhav Nagarnaikd7ec4bf2011-06-07 17:01:42 -0700976 /*
977 * __GFP_NORETRY flag makes sure that the allocation fails
978 * gracefully without invoking oom-killer and the system is
979 * not destabilized.
980 */
Steven Rostedt044fa782008-12-02 23:50:03 -0500981 bpage = kzalloc_node(ALIGN(sizeof(*bpage), cache_line_size()),
Vaibhav Nagarnaikd7ec4bf2011-06-07 17:01:42 -0700982 GFP_KERNEL | __GFP_NORETRY,
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -0800983 cpu_to_node(cpu));
Steven Rostedt044fa782008-12-02 23:50:03 -0500984 if (!bpage)
Steven Rostedte4c2ce82008-10-01 11:14:54 -0400985 goto free_pages;
Steven Rostedt77ae3652009-03-27 11:00:29 -0400986
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -0800987 list_add(&bpage->list, pages);
Steven Rostedt77ae3652009-03-27 11:00:29 -0400988
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -0800989 page = alloc_pages_node(cpu_to_node(cpu),
Vaibhav Nagarnaikd7ec4bf2011-06-07 17:01:42 -0700990 GFP_KERNEL | __GFP_NORETRY, 0);
Vaibhav Nagarnaik7ea59062011-05-03 17:56:42 -0700991 if (!page)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400992 goto free_pages;
Vaibhav Nagarnaik7ea59062011-05-03 17:56:42 -0700993 bpage->page = page_address(page);
Steven Rostedt044fa782008-12-02 23:50:03 -0500994 rb_init_page(bpage->page);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400995 }
996
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -0800997 return 0;
998
999free_pages:
1000 list_for_each_entry_safe(bpage, tmp, pages, list) {
1001 list_del_init(&bpage->list);
1002 free_buffer_page(bpage);
1003 }
1004
1005 return -ENOMEM;
1006}
1007
1008static int rb_allocate_pages(struct ring_buffer_per_cpu *cpu_buffer,
1009 unsigned nr_pages)
1010{
1011 LIST_HEAD(pages);
1012
1013 WARN_ON(!nr_pages);
1014
1015 if (__rb_allocate_pages(nr_pages, &pages, cpu_buffer->cpu))
1016 return -ENOMEM;
1017
Steven Rostedt3adc54f2009-03-30 15:32:01 -04001018 /*
1019 * The ring buffer page list is a circular list that does not
1020 * start and end with a list head. All page list items point to
1021 * other pages.
1022 */
1023 cpu_buffer->pages = pages.next;
1024 list_del(&pages);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001025
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001026 cpu_buffer->nr_pages = nr_pages;
1027
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001028 rb_check_pages(cpu_buffer);
1029
1030 return 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001031}
1032
1033static struct ring_buffer_per_cpu *
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001034rb_allocate_cpu_buffer(struct ring_buffer *buffer, int nr_pages, int cpu)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001035{
1036 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedt044fa782008-12-02 23:50:03 -05001037 struct buffer_page *bpage;
Vaibhav Nagarnaik7ea59062011-05-03 17:56:42 -07001038 struct page *page;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001039 int ret;
1040
1041 cpu_buffer = kzalloc_node(ALIGN(sizeof(*cpu_buffer), cache_line_size()),
1042 GFP_KERNEL, cpu_to_node(cpu));
1043 if (!cpu_buffer)
1044 return NULL;
1045
1046 cpu_buffer->cpu = cpu;
1047 cpu_buffer->buffer = buffer;
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02001048 raw_spin_lock_init(&cpu_buffer->reader_lock);
Peter Zijlstra1f8a6a12009-06-08 18:18:39 +02001049 lockdep_set_class(&cpu_buffer->reader_lock, buffer->reader_lock_key);
Thomas Gleixneredc35bd2009-12-03 12:38:57 +01001050 cpu_buffer->lock = (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001051
Steven Rostedt044fa782008-12-02 23:50:03 -05001052 bpage = kzalloc_node(ALIGN(sizeof(*bpage), cache_line_size()),
Steven Rostedte4c2ce82008-10-01 11:14:54 -04001053 GFP_KERNEL, cpu_to_node(cpu));
Steven Rostedt044fa782008-12-02 23:50:03 -05001054 if (!bpage)
Steven Rostedte4c2ce82008-10-01 11:14:54 -04001055 goto fail_free_buffer;
1056
Steven Rostedt77ae3652009-03-27 11:00:29 -04001057 rb_check_bpage(cpu_buffer, bpage);
1058
Steven Rostedt044fa782008-12-02 23:50:03 -05001059 cpu_buffer->reader_page = bpage;
Vaibhav Nagarnaik7ea59062011-05-03 17:56:42 -07001060 page = alloc_pages_node(cpu_to_node(cpu), GFP_KERNEL, 0);
1061 if (!page)
Steven Rostedte4c2ce82008-10-01 11:14:54 -04001062 goto fail_free_reader;
Vaibhav Nagarnaik7ea59062011-05-03 17:56:42 -07001063 bpage->page = page_address(page);
Steven Rostedt044fa782008-12-02 23:50:03 -05001064 rb_init_page(bpage->page);
Steven Rostedte4c2ce82008-10-01 11:14:54 -04001065
Steven Rostedtd7690412008-10-01 00:29:53 -04001066 INIT_LIST_HEAD(&cpu_buffer->reader_page->list);
Steven Rostedtd7690412008-10-01 00:29:53 -04001067
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001068 ret = rb_allocate_pages(cpu_buffer, nr_pages);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001069 if (ret < 0)
Steven Rostedtd7690412008-10-01 00:29:53 -04001070 goto fail_free_reader;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001071
1072 cpu_buffer->head_page
Steven Rostedt3adc54f2009-03-30 15:32:01 -04001073 = list_entry(cpu_buffer->pages, struct buffer_page, list);
Steven Rostedtbf41a152008-10-04 02:00:59 -04001074 cpu_buffer->tail_page = cpu_buffer->commit_page = cpu_buffer->head_page;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001075
Steven Rostedt77ae3652009-03-27 11:00:29 -04001076 rb_head_page_activate(cpu_buffer);
1077
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001078 return cpu_buffer;
1079
Steven Rostedtd7690412008-10-01 00:29:53 -04001080 fail_free_reader:
1081 free_buffer_page(cpu_buffer->reader_page);
1082
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001083 fail_free_buffer:
1084 kfree(cpu_buffer);
1085 return NULL;
1086}
1087
1088static void rb_free_cpu_buffer(struct ring_buffer_per_cpu *cpu_buffer)
1089{
Steven Rostedt3adc54f2009-03-30 15:32:01 -04001090 struct list_head *head = cpu_buffer->pages;
Steven Rostedt044fa782008-12-02 23:50:03 -05001091 struct buffer_page *bpage, *tmp;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001092
Steven Rostedtd7690412008-10-01 00:29:53 -04001093 free_buffer_page(cpu_buffer->reader_page);
1094
Steven Rostedt77ae3652009-03-27 11:00:29 -04001095 rb_head_page_deactivate(cpu_buffer);
1096
Steven Rostedt3adc54f2009-03-30 15:32:01 -04001097 if (head) {
1098 list_for_each_entry_safe(bpage, tmp, head, list) {
1099 list_del_init(&bpage->list);
1100 free_buffer_page(bpage);
1101 }
1102 bpage = list_entry(head, struct buffer_page, list);
Steven Rostedt044fa782008-12-02 23:50:03 -05001103 free_buffer_page(bpage);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001104 }
Steven Rostedt3adc54f2009-03-30 15:32:01 -04001105
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001106 kfree(cpu_buffer);
1107}
1108
Steven Rostedt59222ef2009-03-12 11:46:03 -04001109#ifdef CONFIG_HOTPLUG_CPU
Frederic Weisbecker09c9e842009-03-21 04:33:36 +01001110static int rb_cpu_notify(struct notifier_block *self,
1111 unsigned long action, void *hcpu);
Steven Rostedt554f7862009-03-11 22:00:13 -04001112#endif
1113
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001114/**
1115 * ring_buffer_alloc - allocate a new ring_buffer
Robert Richter68814b52008-11-24 12:24:12 +01001116 * @size: the size in bytes per cpu that is needed.
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001117 * @flags: attributes to set for the ring buffer.
1118 *
1119 * Currently the only flag that is available is the RB_FL_OVERWRITE
1120 * flag. This flag means that the buffer will overwrite old data
1121 * when the buffer wraps. If this flag is not set, the buffer will
1122 * drop data when the tail hits the head.
1123 */
Peter Zijlstra1f8a6a12009-06-08 18:18:39 +02001124struct ring_buffer *__ring_buffer_alloc(unsigned long size, unsigned flags,
1125 struct lock_class_key *key)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001126{
1127 struct ring_buffer *buffer;
1128 int bsize;
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001129 int cpu, nr_pages;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001130
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001131 /* keep it in its own cache line */
1132 buffer = kzalloc(ALIGN(sizeof(*buffer), cache_line_size()),
1133 GFP_KERNEL);
1134 if (!buffer)
1135 return NULL;
1136
Rusty Russell9e01c1b2009-01-01 10:12:22 +10301137 if (!alloc_cpumask_var(&buffer->cpumask, GFP_KERNEL))
1138 goto fail_free_buffer;
1139
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001140 nr_pages = DIV_ROUND_UP(size, BUF_PAGE_SIZE);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001141 buffer->flags = flags;
Steven Rostedt37886f62009-03-17 17:22:06 -04001142 buffer->clock = trace_clock_local;
Peter Zijlstra1f8a6a12009-06-08 18:18:39 +02001143 buffer->reader_lock_key = key;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001144
1145 /* need at least two pages */
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001146 if (nr_pages < 2)
1147 nr_pages = 2;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001148
Frederic Weisbecker3bf832c2009-03-19 14:47:33 +01001149 /*
1150 * In case of non-hotplug cpu, if the ring-buffer is allocated
1151 * in early initcall, it will not be notified of secondary cpus.
1152 * In that off case, we need to allocate for all possible cpus.
1153 */
1154#ifdef CONFIG_HOTPLUG_CPU
Steven Rostedt554f7862009-03-11 22:00:13 -04001155 get_online_cpus();
1156 cpumask_copy(buffer->cpumask, cpu_online_mask);
Frederic Weisbecker3bf832c2009-03-19 14:47:33 +01001157#else
1158 cpumask_copy(buffer->cpumask, cpu_possible_mask);
1159#endif
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001160 buffer->cpus = nr_cpu_ids;
1161
1162 bsize = sizeof(void *) * nr_cpu_ids;
1163 buffer->buffers = kzalloc(ALIGN(bsize, cache_line_size()),
1164 GFP_KERNEL);
1165 if (!buffer->buffers)
Rusty Russell9e01c1b2009-01-01 10:12:22 +10301166 goto fail_free_cpumask;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001167
1168 for_each_buffer_cpu(buffer, cpu) {
1169 buffer->buffers[cpu] =
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001170 rb_allocate_cpu_buffer(buffer, nr_pages, cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001171 if (!buffer->buffers[cpu])
1172 goto fail_free_buffers;
1173 }
1174
Steven Rostedt59222ef2009-03-12 11:46:03 -04001175#ifdef CONFIG_HOTPLUG_CPU
Steven Rostedt554f7862009-03-11 22:00:13 -04001176 buffer->cpu_notify.notifier_call = rb_cpu_notify;
1177 buffer->cpu_notify.priority = 0;
1178 register_cpu_notifier(&buffer->cpu_notify);
1179#endif
1180
1181 put_online_cpus();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001182 mutex_init(&buffer->mutex);
1183
1184 return buffer;
1185
1186 fail_free_buffers:
1187 for_each_buffer_cpu(buffer, cpu) {
1188 if (buffer->buffers[cpu])
1189 rb_free_cpu_buffer(buffer->buffers[cpu]);
1190 }
1191 kfree(buffer->buffers);
1192
Rusty Russell9e01c1b2009-01-01 10:12:22 +10301193 fail_free_cpumask:
1194 free_cpumask_var(buffer->cpumask);
Steven Rostedt554f7862009-03-11 22:00:13 -04001195 put_online_cpus();
Rusty Russell9e01c1b2009-01-01 10:12:22 +10301196
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001197 fail_free_buffer:
1198 kfree(buffer);
1199 return NULL;
1200}
Peter Zijlstra1f8a6a12009-06-08 18:18:39 +02001201EXPORT_SYMBOL_GPL(__ring_buffer_alloc);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001202
1203/**
1204 * ring_buffer_free - free a ring buffer.
1205 * @buffer: the buffer to free.
1206 */
1207void
1208ring_buffer_free(struct ring_buffer *buffer)
1209{
1210 int cpu;
1211
Steven Rostedt554f7862009-03-11 22:00:13 -04001212 get_online_cpus();
1213
Steven Rostedt59222ef2009-03-12 11:46:03 -04001214#ifdef CONFIG_HOTPLUG_CPU
Steven Rostedt554f7862009-03-11 22:00:13 -04001215 unregister_cpu_notifier(&buffer->cpu_notify);
1216#endif
1217
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001218 for_each_buffer_cpu(buffer, cpu)
1219 rb_free_cpu_buffer(buffer->buffers[cpu]);
1220
Steven Rostedt554f7862009-03-11 22:00:13 -04001221 put_online_cpus();
1222
Eric Dumazetbd3f0222009-08-07 12:49:29 +02001223 kfree(buffer->buffers);
Rusty Russell9e01c1b2009-01-01 10:12:22 +10301224 free_cpumask_var(buffer->cpumask);
1225
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001226 kfree(buffer);
1227}
Robert Richterc4f50182008-12-11 16:49:22 +01001228EXPORT_SYMBOL_GPL(ring_buffer_free);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001229
Steven Rostedt37886f62009-03-17 17:22:06 -04001230void ring_buffer_set_clock(struct ring_buffer *buffer,
1231 u64 (*clock)(void))
1232{
1233 buffer->clock = clock;
1234}
1235
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001236static void rb_reset_cpu(struct ring_buffer_per_cpu *cpu_buffer);
1237
1238static void
1239rb_remove_pages(struct ring_buffer_per_cpu *cpu_buffer, unsigned nr_pages)
1240{
Steven Rostedt044fa782008-12-02 23:50:03 -05001241 struct buffer_page *bpage;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001242 struct list_head *p;
1243 unsigned i;
1244
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02001245 raw_spin_lock_irq(&cpu_buffer->reader_lock);
Steven Rostedt77ae3652009-03-27 11:00:29 -04001246 rb_head_page_deactivate(cpu_buffer);
1247
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001248 for (i = 0; i < nr_pages; i++) {
Steven Rostedt3adc54f2009-03-30 15:32:01 -04001249 if (RB_WARN_ON(cpu_buffer, list_empty(cpu_buffer->pages)))
Julia Lawall292f60c2010-03-29 17:37:02 +02001250 goto out;
Steven Rostedt3adc54f2009-03-30 15:32:01 -04001251 p = cpu_buffer->pages->next;
Steven Rostedt044fa782008-12-02 23:50:03 -05001252 bpage = list_entry(p, struct buffer_page, list);
1253 list_del_init(&bpage->list);
1254 free_buffer_page(bpage);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001255 }
Steven Rostedt3adc54f2009-03-30 15:32:01 -04001256 if (RB_WARN_ON(cpu_buffer, list_empty(cpu_buffer->pages)))
Julia Lawall292f60c2010-03-29 17:37:02 +02001257 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001258
1259 rb_reset_cpu(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001260 rb_check_pages(cpu_buffer);
1261
Julia Lawall292f60c2010-03-29 17:37:02 +02001262out:
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02001263 raw_spin_unlock_irq(&cpu_buffer->reader_lock);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001264}
1265
1266static void
1267rb_insert_pages(struct ring_buffer_per_cpu *cpu_buffer,
1268 struct list_head *pages, unsigned nr_pages)
1269{
Steven Rostedt044fa782008-12-02 23:50:03 -05001270 struct buffer_page *bpage;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001271 struct list_head *p;
1272 unsigned i;
1273
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02001274 raw_spin_lock_irq(&cpu_buffer->reader_lock);
Steven Rostedt77ae3652009-03-27 11:00:29 -04001275 rb_head_page_deactivate(cpu_buffer);
1276
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001277 for (i = 0; i < nr_pages; i++) {
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05001278 if (RB_WARN_ON(cpu_buffer, list_empty(pages)))
Julia Lawall292f60c2010-03-29 17:37:02 +02001279 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001280 p = pages->next;
Steven Rostedt044fa782008-12-02 23:50:03 -05001281 bpage = list_entry(p, struct buffer_page, list);
1282 list_del_init(&bpage->list);
Steven Rostedt3adc54f2009-03-30 15:32:01 -04001283 list_add_tail(&bpage->list, cpu_buffer->pages);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001284 }
1285 rb_reset_cpu(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001286 rb_check_pages(cpu_buffer);
1287
Julia Lawall292f60c2010-03-29 17:37:02 +02001288out:
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02001289 raw_spin_unlock_irq(&cpu_buffer->reader_lock);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001290}
1291
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001292static void update_pages_handler(struct ring_buffer_per_cpu *cpu_buffer)
1293{
1294 if (cpu_buffer->nr_pages_to_update > 0)
1295 rb_insert_pages(cpu_buffer, &cpu_buffer->new_pages,
1296 cpu_buffer->nr_pages_to_update);
1297 else
1298 rb_remove_pages(cpu_buffer, -cpu_buffer->nr_pages_to_update);
1299 cpu_buffer->nr_pages += cpu_buffer->nr_pages_to_update;
1300 /* reset this value */
1301 cpu_buffer->nr_pages_to_update = 0;
1302}
1303
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001304/**
1305 * ring_buffer_resize - resize the ring buffer
1306 * @buffer: the buffer to resize.
1307 * @size: the new size.
1308 *
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001309 * Minimum size is 2 * BUF_PAGE_SIZE.
1310 *
1311 * Returns -1 on failure.
1312 */
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001313int ring_buffer_resize(struct ring_buffer *buffer, unsigned long size,
1314 int cpu_id)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001315{
1316 struct ring_buffer_per_cpu *cpu_buffer;
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001317 unsigned nr_pages;
1318 int cpu;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001319
Ingo Molnaree51a1d2008-11-13 14:58:31 +01001320 /*
1321 * Always succeed at resizing a non-existent buffer:
1322 */
1323 if (!buffer)
1324 return size;
1325
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001326 size = DIV_ROUND_UP(size, BUF_PAGE_SIZE);
1327 size *= BUF_PAGE_SIZE;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001328
1329 /* we need a minimum of two pages */
1330 if (size < BUF_PAGE_SIZE * 2)
1331 size = BUF_PAGE_SIZE * 2;
1332
Steven Rostedt18421012009-12-10 22:54:27 -05001333 atomic_inc(&buffer->record_disabled);
1334
1335 /* Make sure all writers are done with this buffer. */
1336 synchronize_sched();
1337
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001338 mutex_lock(&buffer->mutex);
Steven Rostedt554f7862009-03-11 22:00:13 -04001339 get_online_cpus();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001340
1341 nr_pages = DIV_ROUND_UP(size, BUF_PAGE_SIZE);
1342
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001343 if (cpu_id == RING_BUFFER_ALL_CPUS) {
1344 /* calculate the pages to update */
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001345 for_each_buffer_cpu(buffer, cpu) {
1346 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001347
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001348 cpu_buffer->nr_pages_to_update = nr_pages -
1349 cpu_buffer->nr_pages;
Steven Rostedtf536aaf2008-11-10 23:07:30 -05001350
Vaibhav Nagarnaikd7ec4bf2011-06-07 17:01:42 -07001351 /*
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001352 * nothing more to do for removing pages or no update
Vaibhav Nagarnaikd7ec4bf2011-06-07 17:01:42 -07001353 */
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001354 if (cpu_buffer->nr_pages_to_update <= 0)
1355 continue;
1356
1357 /*
1358 * to add pages, make sure all new pages can be
1359 * allocated without receiving ENOMEM
1360 */
1361 INIT_LIST_HEAD(&cpu_buffer->new_pages);
1362 if (__rb_allocate_pages(cpu_buffer->nr_pages_to_update,
1363 &cpu_buffer->new_pages, cpu))
1364 /* not enough memory for new pages */
1365 goto no_mem;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001366 }
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001367
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001368 /* wait for all the updates to complete */
1369 for_each_buffer_cpu(buffer, cpu) {
1370 cpu_buffer = buffer->buffers[cpu];
1371 if (cpu_buffer->nr_pages_to_update) {
1372 update_pages_handler(cpu_buffer);
1373 }
1374 }
1375 } else {
1376 cpu_buffer = buffer->buffers[cpu_id];
1377 if (nr_pages == cpu_buffer->nr_pages)
1378 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001379
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001380 cpu_buffer->nr_pages_to_update = nr_pages -
1381 cpu_buffer->nr_pages;
1382
1383 INIT_LIST_HEAD(&cpu_buffer->new_pages);
1384 if (cpu_buffer->nr_pages_to_update > 0 &&
1385 __rb_allocate_pages(cpu_buffer->nr_pages_to_update,
1386 &cpu_buffer->new_pages, cpu_id))
1387 goto no_mem;
1388
1389 update_pages_handler(cpu_buffer);
1390 }
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001391
1392 out:
Steven Rostedt554f7862009-03-11 22:00:13 -04001393 put_online_cpus();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001394 mutex_unlock(&buffer->mutex);
1395
Steven Rostedt18421012009-12-10 22:54:27 -05001396 atomic_dec(&buffer->record_disabled);
1397
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001398 return size;
1399
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001400 no_mem:
1401 for_each_buffer_cpu(buffer, cpu) {
1402 struct buffer_page *bpage, *tmp;
1403 cpu_buffer = buffer->buffers[cpu];
1404 /* reset this number regardless */
1405 cpu_buffer->nr_pages_to_update = 0;
1406 if (list_empty(&cpu_buffer->new_pages))
1407 continue;
1408 list_for_each_entry_safe(bpage, tmp, &cpu_buffer->new_pages,
1409 list) {
1410 list_del_init(&bpage->list);
1411 free_buffer_page(bpage);
1412 }
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001413 }
Steven Rostedt554f7862009-03-11 22:00:13 -04001414 put_online_cpus();
Vegard Nossum641d2f62008-11-18 19:22:13 +01001415 mutex_unlock(&buffer->mutex);
Steven Rostedt18421012009-12-10 22:54:27 -05001416 atomic_dec(&buffer->record_disabled);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001417 return -ENOMEM;
1418}
Robert Richterc4f50182008-12-11 16:49:22 +01001419EXPORT_SYMBOL_GPL(ring_buffer_resize);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001420
David Sharp750912f2010-12-08 13:46:47 -08001421void ring_buffer_change_overwrite(struct ring_buffer *buffer, int val)
1422{
1423 mutex_lock(&buffer->mutex);
1424 if (val)
1425 buffer->flags |= RB_FL_OVERWRITE;
1426 else
1427 buffer->flags &= ~RB_FL_OVERWRITE;
1428 mutex_unlock(&buffer->mutex);
1429}
1430EXPORT_SYMBOL_GPL(ring_buffer_change_overwrite);
1431
Steven Rostedt8789a9e2008-12-02 15:34:07 -05001432static inline void *
Steven Rostedt044fa782008-12-02 23:50:03 -05001433__rb_data_page_index(struct buffer_data_page *bpage, unsigned index)
Steven Rostedt8789a9e2008-12-02 15:34:07 -05001434{
Steven Rostedt044fa782008-12-02 23:50:03 -05001435 return bpage->data + index;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05001436}
1437
Steven Rostedt044fa782008-12-02 23:50:03 -05001438static inline void *__rb_page_index(struct buffer_page *bpage, unsigned index)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001439{
Steven Rostedt044fa782008-12-02 23:50:03 -05001440 return bpage->page->data + index;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001441}
1442
1443static inline struct ring_buffer_event *
Steven Rostedtd7690412008-10-01 00:29:53 -04001444rb_reader_event(struct ring_buffer_per_cpu *cpu_buffer)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001445{
Steven Rostedt6f807ac2008-10-04 02:00:58 -04001446 return __rb_page_index(cpu_buffer->reader_page,
1447 cpu_buffer->reader_page->read);
1448}
1449
1450static inline struct ring_buffer_event *
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001451rb_iter_head_event(struct ring_buffer_iter *iter)
1452{
Steven Rostedt6f807ac2008-10-04 02:00:58 -04001453 return __rb_page_index(iter->head_page, iter->head);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001454}
1455
Steven Rostedt77ae3652009-03-27 11:00:29 -04001456static inline unsigned long rb_page_write(struct buffer_page *bpage)
Steven Rostedtbf41a152008-10-04 02:00:59 -04001457{
Steven Rostedt77ae3652009-03-27 11:00:29 -04001458 return local_read(&bpage->write) & RB_WRITE_MASK;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001459}
1460
1461static inline unsigned rb_page_commit(struct buffer_page *bpage)
1462{
Steven Rostedtabc9b562008-12-02 15:34:06 -05001463 return local_read(&bpage->page->commit);
Steven Rostedtbf41a152008-10-04 02:00:59 -04001464}
1465
Steven Rostedt77ae3652009-03-27 11:00:29 -04001466static inline unsigned long rb_page_entries(struct buffer_page *bpage)
1467{
1468 return local_read(&bpage->entries) & RB_WRITE_MASK;
1469}
1470
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001471/* Size is determined by what has been committed */
Steven Rostedtbf41a152008-10-04 02:00:59 -04001472static inline unsigned rb_page_size(struct buffer_page *bpage)
1473{
1474 return rb_page_commit(bpage);
1475}
1476
1477static inline unsigned
1478rb_commit_index(struct ring_buffer_per_cpu *cpu_buffer)
1479{
1480 return rb_page_commit(cpu_buffer->commit_page);
1481}
1482
Steven Rostedtbf41a152008-10-04 02:00:59 -04001483static inline unsigned
1484rb_event_index(struct ring_buffer_event *event)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001485{
Steven Rostedtbf41a152008-10-04 02:00:59 -04001486 unsigned long addr = (unsigned long)event;
1487
Steven Rostedt22f470f2009-06-11 09:29:58 -04001488 return (addr & ~PAGE_MASK) - BUF_PAGE_HDR_SIZE;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001489}
1490
Steven Rostedt0f0c85f2009-05-11 16:08:00 -04001491static inline int
Steven Rostedtfa743952009-06-16 12:37:57 -04001492rb_event_is_commit(struct ring_buffer_per_cpu *cpu_buffer,
1493 struct ring_buffer_event *event)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001494{
Steven Rostedtbf41a152008-10-04 02:00:59 -04001495 unsigned long addr = (unsigned long)event;
1496 unsigned long index;
1497
1498 index = rb_event_index(event);
1499 addr &= PAGE_MASK;
1500
1501 return cpu_buffer->commit_page->page == (void *)addr &&
1502 rb_commit_index(cpu_buffer) == index;
1503}
1504
Andrew Morton34a148b2009-01-09 12:27:09 -08001505static void
Steven Rostedtbf41a152008-10-04 02:00:59 -04001506rb_set_commit_to_write(struct ring_buffer_per_cpu *cpu_buffer)
1507{
Steven Rostedt77ae3652009-03-27 11:00:29 -04001508 unsigned long max_count;
1509
Steven Rostedtbf41a152008-10-04 02:00:59 -04001510 /*
1511 * We only race with interrupts and NMIs on this CPU.
1512 * If we own the commit event, then we can commit
1513 * all others that interrupted us, since the interruptions
1514 * are in stack format (they finish before they come
1515 * back to us). This allows us to do a simple loop to
1516 * assign the commit to the tail.
1517 */
Steven Rostedta8ccf1d2008-12-23 11:32:24 -05001518 again:
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001519 max_count = cpu_buffer->nr_pages * 100;
Steven Rostedt77ae3652009-03-27 11:00:29 -04001520
Steven Rostedtbf41a152008-10-04 02:00:59 -04001521 while (cpu_buffer->commit_page != cpu_buffer->tail_page) {
Steven Rostedt77ae3652009-03-27 11:00:29 -04001522 if (RB_WARN_ON(cpu_buffer, !(--max_count)))
1523 return;
1524 if (RB_WARN_ON(cpu_buffer,
1525 rb_is_reader_page(cpu_buffer->tail_page)))
1526 return;
1527 local_set(&cpu_buffer->commit_page->page->commit,
1528 rb_page_write(cpu_buffer->commit_page));
Steven Rostedtbf41a152008-10-04 02:00:59 -04001529 rb_inc_page(cpu_buffer, &cpu_buffer->commit_page);
Steven Rostedtabc9b562008-12-02 15:34:06 -05001530 cpu_buffer->write_stamp =
1531 cpu_buffer->commit_page->page->time_stamp;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001532 /* add barrier to keep gcc from optimizing too much */
1533 barrier();
1534 }
1535 while (rb_commit_index(cpu_buffer) !=
1536 rb_page_write(cpu_buffer->commit_page)) {
Steven Rostedt77ae3652009-03-27 11:00:29 -04001537
1538 local_set(&cpu_buffer->commit_page->page->commit,
1539 rb_page_write(cpu_buffer->commit_page));
1540 RB_WARN_ON(cpu_buffer,
1541 local_read(&cpu_buffer->commit_page->page->commit) &
1542 ~RB_WRITE_MASK);
Steven Rostedtbf41a152008-10-04 02:00:59 -04001543 barrier();
1544 }
Steven Rostedta8ccf1d2008-12-23 11:32:24 -05001545
1546 /* again, keep gcc from optimizing */
1547 barrier();
1548
1549 /*
1550 * If an interrupt came in just after the first while loop
1551 * and pushed the tail page forward, we will be left with
1552 * a dangling commit that will never go forward.
1553 */
1554 if (unlikely(cpu_buffer->commit_page != cpu_buffer->tail_page))
1555 goto again;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001556}
1557
Steven Rostedtd7690412008-10-01 00:29:53 -04001558static void rb_reset_reader_page(struct ring_buffer_per_cpu *cpu_buffer)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001559{
Steven Rostedtabc9b562008-12-02 15:34:06 -05001560 cpu_buffer->read_stamp = cpu_buffer->reader_page->page->time_stamp;
Steven Rostedt6f807ac2008-10-04 02:00:58 -04001561 cpu_buffer->reader_page->read = 0;
Steven Rostedtd7690412008-10-01 00:29:53 -04001562}
1563
Andrew Morton34a148b2009-01-09 12:27:09 -08001564static void rb_inc_iter(struct ring_buffer_iter *iter)
Steven Rostedtd7690412008-10-01 00:29:53 -04001565{
1566 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
1567
1568 /*
1569 * The iterator could be on the reader page (it starts there).
1570 * But the head could have moved, since the reader was
1571 * found. Check for this case and assign the iterator
1572 * to the head page instead of next.
1573 */
1574 if (iter->head_page == cpu_buffer->reader_page)
Steven Rostedt77ae3652009-03-27 11:00:29 -04001575 iter->head_page = rb_set_head_page(cpu_buffer);
Steven Rostedtd7690412008-10-01 00:29:53 -04001576 else
1577 rb_inc_page(cpu_buffer, &iter->head_page);
1578
Steven Rostedtabc9b562008-12-02 15:34:06 -05001579 iter->read_stamp = iter->head_page->page->time_stamp;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001580 iter->head = 0;
1581}
1582
Steven Rostedt69d1b832010-10-07 18:18:05 -04001583/* Slow path, do not inline */
1584static noinline struct ring_buffer_event *
1585rb_add_time_stamp(struct ring_buffer_event *event, u64 delta)
1586{
1587 event->type_len = RINGBUF_TYPE_TIME_EXTEND;
1588
1589 /* Not the first event on the page? */
1590 if (rb_event_index(event)) {
1591 event->time_delta = delta & TS_MASK;
1592 event->array[0] = delta >> TS_SHIFT;
1593 } else {
1594 /* nope, just zero it */
1595 event->time_delta = 0;
1596 event->array[0] = 0;
1597 }
1598
1599 return skip_time_extend(event);
1600}
1601
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001602/**
1603 * ring_buffer_update_event - update event type and data
1604 * @event: the even to update
1605 * @type: the type of event
1606 * @length: the size of the event field in the ring buffer
1607 *
1608 * Update the type and data fields of the event. The length
1609 * is the actual size that is written to the ring buffer,
1610 * and with this, we can determine what to place into the
1611 * data field.
1612 */
Andrew Morton34a148b2009-01-09 12:27:09 -08001613static void
Steven Rostedt69d1b832010-10-07 18:18:05 -04001614rb_update_event(struct ring_buffer_per_cpu *cpu_buffer,
1615 struct ring_buffer_event *event, unsigned length,
1616 int add_timestamp, u64 delta)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001617{
Steven Rostedt69d1b832010-10-07 18:18:05 -04001618 /* Only a commit updates the timestamp */
1619 if (unlikely(!rb_event_is_commit(cpu_buffer, event)))
1620 delta = 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001621
Steven Rostedt69d1b832010-10-07 18:18:05 -04001622 /*
1623 * If we need to add a timestamp, then we
1624 * add it to the start of the resevered space.
1625 */
1626 if (unlikely(add_timestamp)) {
1627 event = rb_add_time_stamp(event, delta);
1628 length -= RB_LEN_TIME_EXTEND;
1629 delta = 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001630 }
Steven Rostedt69d1b832010-10-07 18:18:05 -04001631
1632 event->time_delta = delta;
1633 length -= RB_EVNT_HDR_SIZE;
1634 if (length > RB_MAX_SMALL_DATA || RB_FORCE_8BYTE_ALIGNMENT) {
1635 event->type_len = 0;
1636 event->array[0] = length;
1637 } else
1638 event->type_len = DIV_ROUND_UP(length, RB_ALIGNMENT);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001639}
1640
Steven Rostedt77ae3652009-03-27 11:00:29 -04001641/*
1642 * rb_handle_head_page - writer hit the head page
1643 *
1644 * Returns: +1 to retry page
1645 * 0 to continue
1646 * -1 on error
1647 */
1648static int
1649rb_handle_head_page(struct ring_buffer_per_cpu *cpu_buffer,
1650 struct buffer_page *tail_page,
1651 struct buffer_page *next_page)
1652{
1653 struct buffer_page *new_head;
1654 int entries;
1655 int type;
1656 int ret;
1657
1658 entries = rb_page_entries(next_page);
1659
1660 /*
1661 * The hard part is here. We need to move the head
1662 * forward, and protect against both readers on
1663 * other CPUs and writers coming in via interrupts.
1664 */
1665 type = rb_head_page_set_update(cpu_buffer, next_page, tail_page,
1666 RB_PAGE_HEAD);
1667
1668 /*
1669 * type can be one of four:
1670 * NORMAL - an interrupt already moved it for us
1671 * HEAD - we are the first to get here.
1672 * UPDATE - we are the interrupt interrupting
1673 * a current move.
1674 * MOVED - a reader on another CPU moved the next
1675 * pointer to its reader page. Give up
1676 * and try again.
1677 */
1678
1679 switch (type) {
1680 case RB_PAGE_HEAD:
1681 /*
1682 * We changed the head to UPDATE, thus
1683 * it is our responsibility to update
1684 * the counters.
1685 */
1686 local_add(entries, &cpu_buffer->overrun);
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -07001687 local_sub(BUF_PAGE_SIZE, &cpu_buffer->entries_bytes);
Steven Rostedt77ae3652009-03-27 11:00:29 -04001688
1689 /*
1690 * The entries will be zeroed out when we move the
1691 * tail page.
1692 */
1693
1694 /* still more to do */
1695 break;
1696
1697 case RB_PAGE_UPDATE:
1698 /*
1699 * This is an interrupt that interrupt the
1700 * previous update. Still more to do.
1701 */
1702 break;
1703 case RB_PAGE_NORMAL:
1704 /*
1705 * An interrupt came in before the update
1706 * and processed this for us.
1707 * Nothing left to do.
1708 */
1709 return 1;
1710 case RB_PAGE_MOVED:
1711 /*
1712 * The reader is on another CPU and just did
1713 * a swap with our next_page.
1714 * Try again.
1715 */
1716 return 1;
1717 default:
1718 RB_WARN_ON(cpu_buffer, 1); /* WTF??? */
1719 return -1;
1720 }
1721
1722 /*
1723 * Now that we are here, the old head pointer is
1724 * set to UPDATE. This will keep the reader from
1725 * swapping the head page with the reader page.
1726 * The reader (on another CPU) will spin till
1727 * we are finished.
1728 *
1729 * We just need to protect against interrupts
1730 * doing the job. We will set the next pointer
1731 * to HEAD. After that, we set the old pointer
1732 * to NORMAL, but only if it was HEAD before.
1733 * otherwise we are an interrupt, and only
1734 * want the outer most commit to reset it.
1735 */
1736 new_head = next_page;
1737 rb_inc_page(cpu_buffer, &new_head);
1738
1739 ret = rb_head_page_set_head(cpu_buffer, new_head, next_page,
1740 RB_PAGE_NORMAL);
1741
1742 /*
1743 * Valid returns are:
1744 * HEAD - an interrupt came in and already set it.
1745 * NORMAL - One of two things:
1746 * 1) We really set it.
1747 * 2) A bunch of interrupts came in and moved
1748 * the page forward again.
1749 */
1750 switch (ret) {
1751 case RB_PAGE_HEAD:
1752 case RB_PAGE_NORMAL:
1753 /* OK */
1754 break;
1755 default:
1756 RB_WARN_ON(cpu_buffer, 1);
1757 return -1;
1758 }
1759
1760 /*
1761 * It is possible that an interrupt came in,
1762 * set the head up, then more interrupts came in
1763 * and moved it again. When we get back here,
1764 * the page would have been set to NORMAL but we
1765 * just set it back to HEAD.
1766 *
1767 * How do you detect this? Well, if that happened
1768 * the tail page would have moved.
1769 */
1770 if (ret == RB_PAGE_NORMAL) {
1771 /*
1772 * If the tail had moved passed next, then we need
1773 * to reset the pointer.
1774 */
1775 if (cpu_buffer->tail_page != tail_page &&
1776 cpu_buffer->tail_page != next_page)
1777 rb_head_page_set_normal(cpu_buffer, new_head,
1778 next_page,
1779 RB_PAGE_HEAD);
1780 }
1781
1782 /*
1783 * If this was the outer most commit (the one that
1784 * changed the original pointer from HEAD to UPDATE),
1785 * then it is up to us to reset it to NORMAL.
1786 */
1787 if (type == RB_PAGE_HEAD) {
1788 ret = rb_head_page_set_normal(cpu_buffer, next_page,
1789 tail_page,
1790 RB_PAGE_UPDATE);
1791 if (RB_WARN_ON(cpu_buffer,
1792 ret != RB_PAGE_UPDATE))
1793 return -1;
1794 }
1795
1796 return 0;
1797}
1798
Andrew Morton34a148b2009-01-09 12:27:09 -08001799static unsigned rb_calculate_event_length(unsigned length)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001800{
1801 struct ring_buffer_event event; /* Used only for sizeof array */
1802
1803 /* zero length can cause confusions */
1804 if (!length)
1805 length = 1;
1806
Steven Rostedt22710482010-03-18 17:54:19 -04001807 if (length > RB_MAX_SMALL_DATA || RB_FORCE_8BYTE_ALIGNMENT)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001808 length += sizeof(event.array[0]);
1809
1810 length += RB_EVNT_HDR_SIZE;
Steven Rostedt22710482010-03-18 17:54:19 -04001811 length = ALIGN(length, RB_ARCH_ALIGNMENT);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001812
1813 return length;
1814}
1815
Steven Rostedtc7b09302009-06-11 11:12:00 -04001816static inline void
1817rb_reset_tail(struct ring_buffer_per_cpu *cpu_buffer,
1818 struct buffer_page *tail_page,
1819 unsigned long tail, unsigned long length)
1820{
1821 struct ring_buffer_event *event;
1822
1823 /*
1824 * Only the event that crossed the page boundary
1825 * must fill the old tail_page with padding.
1826 */
1827 if (tail >= BUF_PAGE_SIZE) {
Steven Rostedtb3230c82010-05-21 11:55:21 -04001828 /*
1829 * If the page was filled, then we still need
1830 * to update the real_end. Reset it to zero
1831 * and the reader will ignore it.
1832 */
1833 if (tail == BUF_PAGE_SIZE)
1834 tail_page->real_end = 0;
1835
Steven Rostedtc7b09302009-06-11 11:12:00 -04001836 local_sub(length, &tail_page->write);
1837 return;
1838 }
1839
1840 event = __rb_page_index(tail_page, tail);
Linus Torvaldsb0b70652009-06-20 10:56:46 -07001841 kmemcheck_annotate_bitfield(event, bitfield);
Steven Rostedtc7b09302009-06-11 11:12:00 -04001842
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -07001843 /* account for padding bytes */
1844 local_add(BUF_PAGE_SIZE - tail, &cpu_buffer->entries_bytes);
1845
Steven Rostedtc7b09302009-06-11 11:12:00 -04001846 /*
Steven Rostedtff0ff842010-03-31 22:11:42 -04001847 * Save the original length to the meta data.
1848 * This will be used by the reader to add lost event
1849 * counter.
1850 */
1851 tail_page->real_end = tail;
1852
1853 /*
Steven Rostedtc7b09302009-06-11 11:12:00 -04001854 * If this event is bigger than the minimum size, then
1855 * we need to be careful that we don't subtract the
1856 * write counter enough to allow another writer to slip
1857 * in on this page.
1858 * We put in a discarded commit instead, to make sure
1859 * that this space is not used again.
1860 *
1861 * If we are less than the minimum size, we don't need to
1862 * worry about it.
1863 */
1864 if (tail > (BUF_PAGE_SIZE - RB_EVNT_MIN_SIZE)) {
1865 /* No room for any events */
1866
1867 /* Mark the rest of the page with padding */
1868 rb_event_set_padding(event);
1869
1870 /* Set the write back to the previous setting */
1871 local_sub(length, &tail_page->write);
1872 return;
1873 }
1874
1875 /* Put in a discarded event */
1876 event->array[0] = (BUF_PAGE_SIZE - tail) - RB_EVNT_HDR_SIZE;
1877 event->type_len = RINGBUF_TYPE_PADDING;
1878 /* time delta must be non zero */
1879 event->time_delta = 1;
Steven Rostedtc7b09302009-06-11 11:12:00 -04001880
1881 /* Set write to end of buffer */
1882 length = (tail + length) - BUF_PAGE_SIZE;
1883 local_sub(length, &tail_page->write);
1884}
Steven Rostedt6634ff22009-05-06 15:30:07 -04001885
Steven Rostedt747e94a2010-10-08 13:51:48 -04001886/*
1887 * This is the slow path, force gcc not to inline it.
1888 */
1889static noinline struct ring_buffer_event *
Steven Rostedt6634ff22009-05-06 15:30:07 -04001890rb_move_tail(struct ring_buffer_per_cpu *cpu_buffer,
1891 unsigned long length, unsigned long tail,
Steven Rostedte8bc43e2010-10-20 10:58:02 -04001892 struct buffer_page *tail_page, u64 ts)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001893{
Steven Rostedt5a50e332009-11-17 08:43:01 -05001894 struct buffer_page *commit_page = cpu_buffer->commit_page;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001895 struct ring_buffer *buffer = cpu_buffer->buffer;
Steven Rostedt77ae3652009-03-27 11:00:29 -04001896 struct buffer_page *next_page;
1897 int ret;
Steven Rostedtaa20ae82009-05-05 21:16:11 -04001898
1899 next_page = tail_page;
1900
Steven Rostedtaa20ae82009-05-05 21:16:11 -04001901 rb_inc_page(cpu_buffer, &next_page);
1902
Steven Rostedtaa20ae82009-05-05 21:16:11 -04001903 /*
1904 * If for some reason, we had an interrupt storm that made
1905 * it all the way around the buffer, bail, and warn
1906 * about it.
1907 */
1908 if (unlikely(next_page == commit_page)) {
Steven Rostedt77ae3652009-03-27 11:00:29 -04001909 local_inc(&cpu_buffer->commit_overrun);
Steven Rostedtaa20ae82009-05-05 21:16:11 -04001910 goto out_reset;
1911 }
1912
Steven Rostedt77ae3652009-03-27 11:00:29 -04001913 /*
1914 * This is where the fun begins!
1915 *
1916 * We are fighting against races between a reader that
1917 * could be on another CPU trying to swap its reader
1918 * page with the buffer head.
1919 *
1920 * We are also fighting against interrupts coming in and
1921 * moving the head or tail on us as well.
1922 *
1923 * If the next page is the head page then we have filled
1924 * the buffer, unless the commit page is still on the
1925 * reader page.
1926 */
1927 if (rb_is_head_page(cpu_buffer, next_page, &tail_page->list)) {
Steven Rostedtaa20ae82009-05-05 21:16:11 -04001928
Steven Rostedt77ae3652009-03-27 11:00:29 -04001929 /*
1930 * If the commit is not on the reader page, then
1931 * move the header page.
1932 */
1933 if (!rb_is_reader_page(cpu_buffer->commit_page)) {
1934 /*
1935 * If we are not in overwrite mode,
1936 * this is easy, just stop here.
1937 */
1938 if (!(buffer->flags & RB_FL_OVERWRITE))
1939 goto out_reset;
Steven Rostedtaa20ae82009-05-05 21:16:11 -04001940
Steven Rostedt77ae3652009-03-27 11:00:29 -04001941 ret = rb_handle_head_page(cpu_buffer,
1942 tail_page,
1943 next_page);
1944 if (ret < 0)
1945 goto out_reset;
1946 if (ret)
1947 goto out_again;
1948 } else {
1949 /*
1950 * We need to be careful here too. The
1951 * commit page could still be on the reader
1952 * page. We could have a small buffer, and
1953 * have filled up the buffer with events
1954 * from interrupts and such, and wrapped.
1955 *
1956 * Note, if the tail page is also the on the
1957 * reader_page, we let it move out.
1958 */
1959 if (unlikely((cpu_buffer->commit_page !=
1960 cpu_buffer->tail_page) &&
1961 (cpu_buffer->commit_page ==
1962 cpu_buffer->reader_page))) {
1963 local_inc(&cpu_buffer->commit_overrun);
1964 goto out_reset;
1965 }
Steven Rostedtaa20ae82009-05-05 21:16:11 -04001966 }
1967 }
1968
Steven Rostedt77ae3652009-03-27 11:00:29 -04001969 ret = rb_tail_page_update(cpu_buffer, tail_page, next_page);
1970 if (ret) {
1971 /*
1972 * Nested commits always have zero deltas, so
1973 * just reread the time stamp
1974 */
Steven Rostedte8bc43e2010-10-20 10:58:02 -04001975 ts = rb_time_stamp(buffer);
1976 next_page->page->time_stamp = ts;
Steven Rostedtaa20ae82009-05-05 21:16:11 -04001977 }
1978
Steven Rostedt77ae3652009-03-27 11:00:29 -04001979 out_again:
Steven Rostedtaa20ae82009-05-05 21:16:11 -04001980
Steven Rostedt77ae3652009-03-27 11:00:29 -04001981 rb_reset_tail(cpu_buffer, tail_page, tail, length);
Steven Rostedtaa20ae82009-05-05 21:16:11 -04001982
1983 /* fail and let the caller try again */
1984 return ERR_PTR(-EAGAIN);
1985
Steven Rostedt45141d42009-02-12 13:19:48 -05001986 out_reset:
Lai Jiangshan6f3b3442009-01-12 11:06:18 +08001987 /* reset write */
Steven Rostedtc7b09302009-06-11 11:12:00 -04001988 rb_reset_tail(cpu_buffer, tail_page, tail, length);
Lai Jiangshan6f3b3442009-01-12 11:06:18 +08001989
Steven Rostedtbf41a152008-10-04 02:00:59 -04001990 return NULL;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001991}
1992
Steven Rostedt6634ff22009-05-06 15:30:07 -04001993static struct ring_buffer_event *
1994__rb_reserve_next(struct ring_buffer_per_cpu *cpu_buffer,
Steven Rostedt69d1b832010-10-07 18:18:05 -04001995 unsigned long length, u64 ts,
1996 u64 delta, int add_timestamp)
Steven Rostedt6634ff22009-05-06 15:30:07 -04001997{
Steven Rostedt5a50e332009-11-17 08:43:01 -05001998 struct buffer_page *tail_page;
Steven Rostedt6634ff22009-05-06 15:30:07 -04001999 struct ring_buffer_event *event;
2000 unsigned long tail, write;
2001
Steven Rostedt69d1b832010-10-07 18:18:05 -04002002 /*
2003 * If the time delta since the last event is too big to
2004 * hold in the time field of the event, then we append a
2005 * TIME EXTEND event ahead of the data event.
2006 */
2007 if (unlikely(add_timestamp))
2008 length += RB_LEN_TIME_EXTEND;
2009
Steven Rostedt6634ff22009-05-06 15:30:07 -04002010 tail_page = cpu_buffer->tail_page;
2011 write = local_add_return(length, &tail_page->write);
Steven Rostedt77ae3652009-03-27 11:00:29 -04002012
2013 /* set write to only the index of the write */
2014 write &= RB_WRITE_MASK;
Steven Rostedt6634ff22009-05-06 15:30:07 -04002015 tail = write - length;
2016
2017 /* See if we shot pass the end of this buffer page */
Steven Rostedt747e94a2010-10-08 13:51:48 -04002018 if (unlikely(write > BUF_PAGE_SIZE))
Steven Rostedt6634ff22009-05-06 15:30:07 -04002019 return rb_move_tail(cpu_buffer, length, tail,
Steven Rostedt5a50e332009-11-17 08:43:01 -05002020 tail_page, ts);
Steven Rostedt6634ff22009-05-06 15:30:07 -04002021
2022 /* We reserved something on the buffer */
2023
Steven Rostedt6634ff22009-05-06 15:30:07 -04002024 event = __rb_page_index(tail_page, tail);
Vegard Nossum1744a212009-02-28 08:29:44 +01002025 kmemcheck_annotate_bitfield(event, bitfield);
Steven Rostedt69d1b832010-10-07 18:18:05 -04002026 rb_update_event(cpu_buffer, event, length, add_timestamp, delta);
Steven Rostedt6634ff22009-05-06 15:30:07 -04002027
Steven Rostedt69d1b832010-10-07 18:18:05 -04002028 local_inc(&tail_page->entries);
Steven Rostedt6634ff22009-05-06 15:30:07 -04002029
2030 /*
Steven Rostedtfa743952009-06-16 12:37:57 -04002031 * If this is the first commit on the page, then update
2032 * its timestamp.
Steven Rostedt6634ff22009-05-06 15:30:07 -04002033 */
Steven Rostedtfa743952009-06-16 12:37:57 -04002034 if (!tail)
Steven Rostedte8bc43e2010-10-20 10:58:02 -04002035 tail_page->page->time_stamp = ts;
Steven Rostedt6634ff22009-05-06 15:30:07 -04002036
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -07002037 /* account for these added bytes */
2038 local_add(length, &cpu_buffer->entries_bytes);
2039
Steven Rostedt6634ff22009-05-06 15:30:07 -04002040 return event;
2041}
2042
Steven Rostedtedd813bf2009-06-02 23:00:53 -04002043static inline int
2044rb_try_to_discard(struct ring_buffer_per_cpu *cpu_buffer,
2045 struct ring_buffer_event *event)
2046{
2047 unsigned long new_index, old_index;
2048 struct buffer_page *bpage;
2049 unsigned long index;
2050 unsigned long addr;
2051
2052 new_index = rb_event_index(event);
Steven Rostedt69d1b832010-10-07 18:18:05 -04002053 old_index = new_index + rb_event_ts_length(event);
Steven Rostedtedd813bf2009-06-02 23:00:53 -04002054 addr = (unsigned long)event;
2055 addr &= PAGE_MASK;
2056
2057 bpage = cpu_buffer->tail_page;
2058
2059 if (bpage->page == (void *)addr && rb_page_write(bpage) == old_index) {
Steven Rostedt77ae3652009-03-27 11:00:29 -04002060 unsigned long write_mask =
2061 local_read(&bpage->write) & ~RB_WRITE_MASK;
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -07002062 unsigned long event_length = rb_event_length(event);
Steven Rostedtedd813bf2009-06-02 23:00:53 -04002063 /*
2064 * This is on the tail page. It is possible that
2065 * a write could come in and move the tail page
2066 * and write to the next page. That is fine
2067 * because we just shorten what is on this page.
2068 */
Steven Rostedt77ae3652009-03-27 11:00:29 -04002069 old_index += write_mask;
2070 new_index += write_mask;
Steven Rostedtedd813bf2009-06-02 23:00:53 -04002071 index = local_cmpxchg(&bpage->write, old_index, new_index);
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -07002072 if (index == old_index) {
2073 /* update counters */
2074 local_sub(event_length, &cpu_buffer->entries_bytes);
Steven Rostedtedd813bf2009-06-02 23:00:53 -04002075 return 1;
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -07002076 }
Steven Rostedtedd813bf2009-06-02 23:00:53 -04002077 }
2078
2079 /* could not discard */
2080 return 0;
2081}
2082
Steven Rostedtfa743952009-06-16 12:37:57 -04002083static void rb_start_commit(struct ring_buffer_per_cpu *cpu_buffer)
2084{
2085 local_inc(&cpu_buffer->committing);
2086 local_inc(&cpu_buffer->commits);
2087}
2088
Steven Rostedtd9abde22010-10-19 13:17:08 -04002089static inline void rb_end_commit(struct ring_buffer_per_cpu *cpu_buffer)
Steven Rostedtfa743952009-06-16 12:37:57 -04002090{
2091 unsigned long commits;
2092
2093 if (RB_WARN_ON(cpu_buffer,
2094 !local_read(&cpu_buffer->committing)))
2095 return;
2096
2097 again:
2098 commits = local_read(&cpu_buffer->commits);
2099 /* synchronize with interrupts */
2100 barrier();
2101 if (local_read(&cpu_buffer->committing) == 1)
2102 rb_set_commit_to_write(cpu_buffer);
2103
2104 local_dec(&cpu_buffer->committing);
2105
2106 /* synchronize with interrupts */
2107 barrier();
2108
2109 /*
2110 * Need to account for interrupts coming in between the
2111 * updating of the commit page and the clearing of the
2112 * committing counter.
2113 */
2114 if (unlikely(local_read(&cpu_buffer->commits) != commits) &&
2115 !local_read(&cpu_buffer->committing)) {
2116 local_inc(&cpu_buffer->committing);
2117 goto again;
2118 }
2119}
2120
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002121static struct ring_buffer_event *
Steven Rostedt62f0b3e2009-09-04 14:11:34 -04002122rb_reserve_next_event(struct ring_buffer *buffer,
2123 struct ring_buffer_per_cpu *cpu_buffer,
Steven Rostedt1cd8d732009-05-11 14:08:09 -04002124 unsigned long length)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002125{
2126 struct ring_buffer_event *event;
Steven Rostedt69d1b832010-10-07 18:18:05 -04002127 u64 ts, delta;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002128 int nr_loops = 0;
Steven Rostedt69d1b832010-10-07 18:18:05 -04002129 int add_timestamp;
Steven Rostedt140ff892010-10-08 10:50:30 -04002130 u64 diff;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002131
Steven Rostedtfa743952009-06-16 12:37:57 -04002132 rb_start_commit(cpu_buffer);
2133
Steven Rostedt85bac322009-09-04 14:24:40 -04002134#ifdef CONFIG_RING_BUFFER_ALLOW_SWAP
Steven Rostedt62f0b3e2009-09-04 14:11:34 -04002135 /*
2136 * Due to the ability to swap a cpu buffer from a buffer
2137 * it is possible it was swapped before we committed.
2138 * (committing stops a swap). We check for it here and
2139 * if it happened, we have to fail the write.
2140 */
2141 barrier();
2142 if (unlikely(ACCESS_ONCE(cpu_buffer->buffer) != buffer)) {
2143 local_dec(&cpu_buffer->committing);
2144 local_dec(&cpu_buffer->commits);
2145 return NULL;
2146 }
Steven Rostedt85bac322009-09-04 14:24:40 -04002147#endif
Steven Rostedt62f0b3e2009-09-04 14:11:34 -04002148
Steven Rostedtbe957c42009-05-11 14:42:53 -04002149 length = rb_calculate_event_length(length);
Steven Rostedtbf41a152008-10-04 02:00:59 -04002150 again:
Steven Rostedt69d1b832010-10-07 18:18:05 -04002151 add_timestamp = 0;
2152 delta = 0;
2153
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002154 /*
2155 * We allow for interrupts to reenter here and do a trace.
2156 * If one does, it will cause this original code to loop
2157 * back here. Even with heavy interrupts happening, this
2158 * should only happen a few times in a row. If this happens
2159 * 1000 times in a row, there must be either an interrupt
2160 * storm or we have something buggy.
2161 * Bail!
2162 */
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05002163 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 1000))
Steven Rostedtfa743952009-06-16 12:37:57 -04002164 goto out_fail;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002165
Jiri Olsa6d3f1e12009-10-23 19:36:19 -04002166 ts = rb_time_stamp(cpu_buffer->buffer);
Steven Rostedt140ff892010-10-08 10:50:30 -04002167 diff = ts - cpu_buffer->write_stamp;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002168
Steven Rostedt140ff892010-10-08 10:50:30 -04002169 /* make sure this diff is calculated here */
2170 barrier();
Steven Rostedtbf41a152008-10-04 02:00:59 -04002171
Steven Rostedt140ff892010-10-08 10:50:30 -04002172 /* Did the write stamp get updated already? */
2173 if (likely(ts >= cpu_buffer->write_stamp)) {
Steven Rostedt168b6b12009-05-11 22:11:05 -04002174 delta = diff;
2175 if (unlikely(test_time_stamp(delta))) {
Jiri Olsa31274d72011-02-18 15:52:19 +01002176 int local_clock_stable = 1;
2177#ifdef CONFIG_HAVE_UNSTABLE_SCHED_CLOCK
2178 local_clock_stable = sched_clock_stable;
2179#endif
Steven Rostedt69d1b832010-10-07 18:18:05 -04002180 WARN_ONCE(delta > (1ULL << 59),
Jiri Olsa31274d72011-02-18 15:52:19 +01002181 KERN_WARNING "Delta way too big! %llu ts=%llu write stamp = %llu\n%s",
Steven Rostedt69d1b832010-10-07 18:18:05 -04002182 (unsigned long long)delta,
2183 (unsigned long long)ts,
Jiri Olsa31274d72011-02-18 15:52:19 +01002184 (unsigned long long)cpu_buffer->write_stamp,
2185 local_clock_stable ? "" :
2186 "If you just came from a suspend/resume,\n"
2187 "please switch to the trace global clock:\n"
2188 " echo global > /sys/kernel/debug/tracing/trace_clock\n");
Steven Rostedt69d1b832010-10-07 18:18:05 -04002189 add_timestamp = 1;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002190 }
Steven Rostedt168b6b12009-05-11 22:11:05 -04002191 }
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002192
Steven Rostedt69d1b832010-10-07 18:18:05 -04002193 event = __rb_reserve_next(cpu_buffer, length, ts,
2194 delta, add_timestamp);
Steven Rostedt168b6b12009-05-11 22:11:05 -04002195 if (unlikely(PTR_ERR(event) == -EAGAIN))
Steven Rostedtbf41a152008-10-04 02:00:59 -04002196 goto again;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002197
Steven Rostedtfa743952009-06-16 12:37:57 -04002198 if (!event)
2199 goto out_fail;
Steven Rostedtbf41a152008-10-04 02:00:59 -04002200
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002201 return event;
Steven Rostedtfa743952009-06-16 12:37:57 -04002202
2203 out_fail:
2204 rb_end_commit(cpu_buffer);
2205 return NULL;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002206}
2207
Paul Mundt1155de42009-06-25 14:30:12 +09002208#ifdef CONFIG_TRACING
2209
Steven Rostedtaa18efb2009-04-20 16:16:11 -04002210#define TRACE_RECURSIVE_DEPTH 16
Steven Rostedt261842b2009-04-16 21:41:52 -04002211
Steven Rostedtd9abde22010-10-19 13:17:08 -04002212/* Keep this code out of the fast path cache */
2213static noinline void trace_recursive_fail(void)
Steven Rostedt261842b2009-04-16 21:41:52 -04002214{
Steven Rostedtaa18efb2009-04-20 16:16:11 -04002215 /* Disable all tracing before we do anything else */
2216 tracing_off_permanent();
Frederic Weisbeckere057a5e2009-04-19 23:38:12 +02002217
Steven Rostedt7d7d2b82009-04-27 12:37:49 -04002218 printk_once(KERN_WARNING "Tracing recursion: depth[%ld]:"
Steven Rostedtaa18efb2009-04-20 16:16:11 -04002219 "HC[%lu]:SC[%lu]:NMI[%lu]\n",
Steven Rostedtb1cff0a2011-05-25 14:27:43 -04002220 trace_recursion_buffer(),
Steven Rostedtaa18efb2009-04-20 16:16:11 -04002221 hardirq_count() >> HARDIRQ_SHIFT,
2222 softirq_count() >> SOFTIRQ_SHIFT,
2223 in_nmi());
Frederic Weisbeckere057a5e2009-04-19 23:38:12 +02002224
Steven Rostedtaa18efb2009-04-20 16:16:11 -04002225 WARN_ON_ONCE(1);
Steven Rostedtd9abde22010-10-19 13:17:08 -04002226}
2227
2228static inline int trace_recursive_lock(void)
2229{
Steven Rostedtb1cff0a2011-05-25 14:27:43 -04002230 trace_recursion_inc();
Steven Rostedtd9abde22010-10-19 13:17:08 -04002231
Steven Rostedtb1cff0a2011-05-25 14:27:43 -04002232 if (likely(trace_recursion_buffer() < TRACE_RECURSIVE_DEPTH))
Steven Rostedtd9abde22010-10-19 13:17:08 -04002233 return 0;
2234
2235 trace_recursive_fail();
2236
Steven Rostedtaa18efb2009-04-20 16:16:11 -04002237 return -1;
Steven Rostedt261842b2009-04-16 21:41:52 -04002238}
2239
Steven Rostedtd9abde22010-10-19 13:17:08 -04002240static inline void trace_recursive_unlock(void)
Steven Rostedt261842b2009-04-16 21:41:52 -04002241{
Steven Rostedtb1cff0a2011-05-25 14:27:43 -04002242 WARN_ON_ONCE(!trace_recursion_buffer());
Steven Rostedt261842b2009-04-16 21:41:52 -04002243
Steven Rostedtb1cff0a2011-05-25 14:27:43 -04002244 trace_recursion_dec();
Steven Rostedt261842b2009-04-16 21:41:52 -04002245}
2246
Paul Mundt1155de42009-06-25 14:30:12 +09002247#else
2248
2249#define trace_recursive_lock() (0)
2250#define trace_recursive_unlock() do { } while (0)
2251
2252#endif
2253
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002254/**
2255 * ring_buffer_lock_reserve - reserve a part of the buffer
2256 * @buffer: the ring buffer to reserve from
2257 * @length: the length of the data to reserve (excluding event header)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002258 *
2259 * Returns a reseverd event on the ring buffer to copy directly to.
2260 * The user of this interface will need to get the body to write into
2261 * and can use the ring_buffer_event_data() interface.
2262 *
2263 * The length is the length of the data needed, not the event length
2264 * which also includes the event header.
2265 *
2266 * Must be paired with ring_buffer_unlock_commit, unless NULL is returned.
2267 * If NULL is returned, then nothing has been allocated or locked.
2268 */
2269struct ring_buffer_event *
Arnaldo Carvalho de Melo0a987752009-02-05 16:12:56 -02002270ring_buffer_lock_reserve(struct ring_buffer *buffer, unsigned long length)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002271{
2272 struct ring_buffer_per_cpu *cpu_buffer;
2273 struct ring_buffer_event *event;
Steven Rostedt5168ae52010-06-03 09:36:50 -04002274 int cpu;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002275
Steven Rostedt033601a2008-11-21 12:41:55 -05002276 if (ring_buffer_flags != RB_BUFFERS_ON)
Steven Rostedta3583242008-11-11 15:01:42 -05002277 return NULL;
2278
Steven Rostedtbf41a152008-10-04 02:00:59 -04002279 /* If we are tracing schedule, we don't want to recurse */
Steven Rostedt5168ae52010-06-03 09:36:50 -04002280 preempt_disable_notrace();
Steven Rostedtbf41a152008-10-04 02:00:59 -04002281
Lai Jiangshan52fbe9c2010-03-08 14:50:43 +08002282 if (atomic_read(&buffer->record_disabled))
2283 goto out_nocheck;
2284
Steven Rostedt261842b2009-04-16 21:41:52 -04002285 if (trace_recursive_lock())
2286 goto out_nocheck;
2287
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002288 cpu = raw_smp_processor_id();
2289
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302290 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedtd7690412008-10-01 00:29:53 -04002291 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002292
2293 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002294
2295 if (atomic_read(&cpu_buffer->record_disabled))
Steven Rostedtd7690412008-10-01 00:29:53 -04002296 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002297
Steven Rostedtbe957c42009-05-11 14:42:53 -04002298 if (length > BUF_MAX_DATA_SIZE)
Steven Rostedtbf41a152008-10-04 02:00:59 -04002299 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002300
Steven Rostedt62f0b3e2009-09-04 14:11:34 -04002301 event = rb_reserve_next_event(buffer, cpu_buffer, length);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002302 if (!event)
Steven Rostedtd7690412008-10-01 00:29:53 -04002303 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002304
2305 return event;
2306
Steven Rostedtd7690412008-10-01 00:29:53 -04002307 out:
Steven Rostedt261842b2009-04-16 21:41:52 -04002308 trace_recursive_unlock();
2309
2310 out_nocheck:
Steven Rostedt5168ae52010-06-03 09:36:50 -04002311 preempt_enable_notrace();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002312 return NULL;
2313}
Robert Richterc4f50182008-12-11 16:49:22 +01002314EXPORT_SYMBOL_GPL(ring_buffer_lock_reserve);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002315
Steven Rostedta1863c22009-09-03 10:23:58 -04002316static void
2317rb_update_write_stamp(struct ring_buffer_per_cpu *cpu_buffer,
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002318 struct ring_buffer_event *event)
2319{
Steven Rostedt69d1b832010-10-07 18:18:05 -04002320 u64 delta;
2321
Steven Rostedtfa743952009-06-16 12:37:57 -04002322 /*
2323 * The event first in the commit queue updates the
2324 * time stamp.
2325 */
Steven Rostedt69d1b832010-10-07 18:18:05 -04002326 if (rb_event_is_commit(cpu_buffer, event)) {
2327 /*
2328 * A commit event that is first on a page
2329 * updates the write timestamp with the page stamp
2330 */
2331 if (!rb_event_index(event))
2332 cpu_buffer->write_stamp =
2333 cpu_buffer->commit_page->page->time_stamp;
2334 else if (event->type_len == RINGBUF_TYPE_TIME_EXTEND) {
2335 delta = event->array[0];
2336 delta <<= TS_SHIFT;
2337 delta += event->time_delta;
2338 cpu_buffer->write_stamp += delta;
2339 } else
2340 cpu_buffer->write_stamp += event->time_delta;
2341 }
Steven Rostedta1863c22009-09-03 10:23:58 -04002342}
Steven Rostedtbf41a152008-10-04 02:00:59 -04002343
Steven Rostedta1863c22009-09-03 10:23:58 -04002344static void rb_commit(struct ring_buffer_per_cpu *cpu_buffer,
2345 struct ring_buffer_event *event)
2346{
2347 local_inc(&cpu_buffer->entries);
2348 rb_update_write_stamp(cpu_buffer, event);
Steven Rostedtfa743952009-06-16 12:37:57 -04002349 rb_end_commit(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002350}
2351
2352/**
2353 * ring_buffer_unlock_commit - commit a reserved
2354 * @buffer: The buffer to commit to
2355 * @event: The event pointer to commit.
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002356 *
2357 * This commits the data to the ring buffer, and releases any locks held.
2358 *
2359 * Must be paired with ring_buffer_lock_reserve.
2360 */
2361int ring_buffer_unlock_commit(struct ring_buffer *buffer,
Arnaldo Carvalho de Melo0a987752009-02-05 16:12:56 -02002362 struct ring_buffer_event *event)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002363{
2364 struct ring_buffer_per_cpu *cpu_buffer;
2365 int cpu = raw_smp_processor_id();
2366
2367 cpu_buffer = buffer->buffers[cpu];
2368
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002369 rb_commit(cpu_buffer, event);
2370
Steven Rostedt261842b2009-04-16 21:41:52 -04002371 trace_recursive_unlock();
2372
Steven Rostedt5168ae52010-06-03 09:36:50 -04002373 preempt_enable_notrace();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002374
2375 return 0;
2376}
Robert Richterc4f50182008-12-11 16:49:22 +01002377EXPORT_SYMBOL_GPL(ring_buffer_unlock_commit);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002378
Frederic Weisbeckerf3b9aae2009-04-19 23:39:33 +02002379static inline void rb_event_discard(struct ring_buffer_event *event)
2380{
Steven Rostedt69d1b832010-10-07 18:18:05 -04002381 if (event->type_len == RINGBUF_TYPE_TIME_EXTEND)
2382 event = skip_time_extend(event);
2383
Lai Jiangshan334d4162009-04-24 11:27:05 +08002384 /* array[0] holds the actual length for the discarded event */
2385 event->array[0] = rb_event_data_length(event) - RB_EVNT_HDR_SIZE;
2386 event->type_len = RINGBUF_TYPE_PADDING;
Frederic Weisbeckerf3b9aae2009-04-19 23:39:33 +02002387 /* time delta must be non zero */
2388 if (!event->time_delta)
2389 event->time_delta = 1;
2390}
2391
Steven Rostedta1863c22009-09-03 10:23:58 -04002392/*
2393 * Decrement the entries to the page that an event is on.
2394 * The event does not even need to exist, only the pointer
2395 * to the page it is on. This may only be called before the commit
2396 * takes place.
2397 */
2398static inline void
2399rb_decrement_entry(struct ring_buffer_per_cpu *cpu_buffer,
2400 struct ring_buffer_event *event)
2401{
2402 unsigned long addr = (unsigned long)event;
2403 struct buffer_page *bpage = cpu_buffer->commit_page;
2404 struct buffer_page *start;
2405
2406 addr &= PAGE_MASK;
2407
2408 /* Do the likely case first */
2409 if (likely(bpage->page == (void *)addr)) {
2410 local_dec(&bpage->entries);
2411 return;
2412 }
2413
2414 /*
2415 * Because the commit page may be on the reader page we
2416 * start with the next page and check the end loop there.
2417 */
2418 rb_inc_page(cpu_buffer, &bpage);
2419 start = bpage;
2420 do {
2421 if (bpage->page == (void *)addr) {
2422 local_dec(&bpage->entries);
2423 return;
2424 }
2425 rb_inc_page(cpu_buffer, &bpage);
2426 } while (bpage != start);
2427
2428 /* commit not part of this buffer?? */
2429 RB_WARN_ON(cpu_buffer, 1);
2430}
2431
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002432/**
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002433 * ring_buffer_commit_discard - discard an event that has not been committed
2434 * @buffer: the ring buffer
2435 * @event: non committed event to discard
2436 *
Steven Rostedtdc892f72009-09-03 15:33:41 -04002437 * Sometimes an event that is in the ring buffer needs to be ignored.
2438 * This function lets the user discard an event in the ring buffer
2439 * and then that event will not be read later.
2440 *
2441 * This function only works if it is called before the the item has been
2442 * committed. It will try to free the event from the ring buffer
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002443 * if another event has not been added behind it.
2444 *
2445 * If another event has been added behind it, it will set the event
2446 * up as discarded, and perform the commit.
2447 *
2448 * If this function is called, do not call ring_buffer_unlock_commit on
2449 * the event.
2450 */
2451void ring_buffer_discard_commit(struct ring_buffer *buffer,
2452 struct ring_buffer_event *event)
2453{
2454 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002455 int cpu;
2456
2457 /* The event is discarded regardless */
Frederic Weisbeckerf3b9aae2009-04-19 23:39:33 +02002458 rb_event_discard(event);
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002459
Steven Rostedtfa743952009-06-16 12:37:57 -04002460 cpu = smp_processor_id();
2461 cpu_buffer = buffer->buffers[cpu];
2462
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002463 /*
2464 * This must only be called if the event has not been
2465 * committed yet. Thus we can assume that preemption
2466 * is still disabled.
2467 */
Steven Rostedtfa743952009-06-16 12:37:57 -04002468 RB_WARN_ON(buffer, !local_read(&cpu_buffer->committing));
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002469
Steven Rostedta1863c22009-09-03 10:23:58 -04002470 rb_decrement_entry(cpu_buffer, event);
Steven Rostedt0f2541d2009-08-05 12:02:48 -04002471 if (rb_try_to_discard(cpu_buffer, event))
Steven Rostedtedd813bf2009-06-02 23:00:53 -04002472 goto out;
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002473
2474 /*
2475 * The commit is still visible by the reader, so we
Steven Rostedta1863c22009-09-03 10:23:58 -04002476 * must still update the timestamp.
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002477 */
Steven Rostedta1863c22009-09-03 10:23:58 -04002478 rb_update_write_stamp(cpu_buffer, event);
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002479 out:
Steven Rostedtfa743952009-06-16 12:37:57 -04002480 rb_end_commit(cpu_buffer);
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002481
Frederic Weisbeckerf3b9aae2009-04-19 23:39:33 +02002482 trace_recursive_unlock();
2483
Steven Rostedt5168ae52010-06-03 09:36:50 -04002484 preempt_enable_notrace();
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002485
2486}
2487EXPORT_SYMBOL_GPL(ring_buffer_discard_commit);
2488
2489/**
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002490 * ring_buffer_write - write data to the buffer without reserving
2491 * @buffer: The ring buffer to write to.
2492 * @length: The length of the data being written (excluding the event header)
2493 * @data: The data to write to the buffer.
2494 *
2495 * This is like ring_buffer_lock_reserve and ring_buffer_unlock_commit as
2496 * one function. If you already have the data to write to the buffer, it
2497 * may be easier to simply call this function.
2498 *
2499 * Note, like ring_buffer_lock_reserve, the length is the length of the data
2500 * and not the length of the event which would hold the header.
2501 */
2502int ring_buffer_write(struct ring_buffer *buffer,
2503 unsigned long length,
2504 void *data)
2505{
2506 struct ring_buffer_per_cpu *cpu_buffer;
2507 struct ring_buffer_event *event;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002508 void *body;
2509 int ret = -EBUSY;
Steven Rostedt5168ae52010-06-03 09:36:50 -04002510 int cpu;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002511
Steven Rostedt033601a2008-11-21 12:41:55 -05002512 if (ring_buffer_flags != RB_BUFFERS_ON)
Steven Rostedta3583242008-11-11 15:01:42 -05002513 return -EBUSY;
2514
Steven Rostedt5168ae52010-06-03 09:36:50 -04002515 preempt_disable_notrace();
Steven Rostedtbf41a152008-10-04 02:00:59 -04002516
Lai Jiangshan52fbe9c2010-03-08 14:50:43 +08002517 if (atomic_read(&buffer->record_disabled))
2518 goto out;
2519
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002520 cpu = raw_smp_processor_id();
2521
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302522 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedtd7690412008-10-01 00:29:53 -04002523 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002524
2525 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002526
2527 if (atomic_read(&cpu_buffer->record_disabled))
2528 goto out;
2529
Steven Rostedtbe957c42009-05-11 14:42:53 -04002530 if (length > BUF_MAX_DATA_SIZE)
2531 goto out;
2532
Steven Rostedt62f0b3e2009-09-04 14:11:34 -04002533 event = rb_reserve_next_event(buffer, cpu_buffer, length);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002534 if (!event)
2535 goto out;
2536
2537 body = rb_event_data(event);
2538
2539 memcpy(body, data, length);
2540
2541 rb_commit(cpu_buffer, event);
2542
2543 ret = 0;
2544 out:
Steven Rostedt5168ae52010-06-03 09:36:50 -04002545 preempt_enable_notrace();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002546
2547 return ret;
2548}
Robert Richterc4f50182008-12-11 16:49:22 +01002549EXPORT_SYMBOL_GPL(ring_buffer_write);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002550
Andrew Morton34a148b2009-01-09 12:27:09 -08002551static int rb_per_cpu_empty(struct ring_buffer_per_cpu *cpu_buffer)
Steven Rostedtbf41a152008-10-04 02:00:59 -04002552{
2553 struct buffer_page *reader = cpu_buffer->reader_page;
Steven Rostedt77ae3652009-03-27 11:00:29 -04002554 struct buffer_page *head = rb_set_head_page(cpu_buffer);
Steven Rostedtbf41a152008-10-04 02:00:59 -04002555 struct buffer_page *commit = cpu_buffer->commit_page;
2556
Steven Rostedt77ae3652009-03-27 11:00:29 -04002557 /* In case of error, head will be NULL */
2558 if (unlikely(!head))
2559 return 1;
2560
Steven Rostedtbf41a152008-10-04 02:00:59 -04002561 return reader->read == rb_page_commit(reader) &&
2562 (commit == reader ||
2563 (commit == head &&
2564 head->read == rb_page_commit(commit)));
2565}
2566
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002567/**
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002568 * ring_buffer_record_disable - stop all writes into the buffer
2569 * @buffer: The ring buffer to stop writes to.
2570 *
2571 * This prevents all writes to the buffer. Any attempt to write
2572 * to the buffer after this will fail and return NULL.
2573 *
2574 * The caller should call synchronize_sched() after this.
2575 */
2576void ring_buffer_record_disable(struct ring_buffer *buffer)
2577{
2578 atomic_inc(&buffer->record_disabled);
2579}
Robert Richterc4f50182008-12-11 16:49:22 +01002580EXPORT_SYMBOL_GPL(ring_buffer_record_disable);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002581
2582/**
2583 * ring_buffer_record_enable - enable writes to the buffer
2584 * @buffer: The ring buffer to enable writes
2585 *
2586 * Note, multiple disables will need the same number of enables
Adam Buchbinderc41b20e2009-12-11 16:35:39 -05002587 * to truly enable the writing (much like preempt_disable).
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002588 */
2589void ring_buffer_record_enable(struct ring_buffer *buffer)
2590{
2591 atomic_dec(&buffer->record_disabled);
2592}
Robert Richterc4f50182008-12-11 16:49:22 +01002593EXPORT_SYMBOL_GPL(ring_buffer_record_enable);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002594
2595/**
Steven Rostedt499e5472012-02-22 15:50:28 -05002596 * ring_buffer_record_off - stop all writes into the buffer
2597 * @buffer: The ring buffer to stop writes to.
2598 *
2599 * This prevents all writes to the buffer. Any attempt to write
2600 * to the buffer after this will fail and return NULL.
2601 *
2602 * This is different than ring_buffer_record_disable() as
2603 * it works like an on/off switch, where as the disable() verison
2604 * must be paired with a enable().
2605 */
2606void ring_buffer_record_off(struct ring_buffer *buffer)
2607{
2608 unsigned int rd;
2609 unsigned int new_rd;
2610
2611 do {
2612 rd = atomic_read(&buffer->record_disabled);
2613 new_rd = rd | RB_BUFFER_OFF;
2614 } while (atomic_cmpxchg(&buffer->record_disabled, rd, new_rd) != rd);
2615}
2616EXPORT_SYMBOL_GPL(ring_buffer_record_off);
2617
2618/**
2619 * ring_buffer_record_on - restart writes into the buffer
2620 * @buffer: The ring buffer to start writes to.
2621 *
2622 * This enables all writes to the buffer that was disabled by
2623 * ring_buffer_record_off().
2624 *
2625 * This is different than ring_buffer_record_enable() as
2626 * it works like an on/off switch, where as the enable() verison
2627 * must be paired with a disable().
2628 */
2629void ring_buffer_record_on(struct ring_buffer *buffer)
2630{
2631 unsigned int rd;
2632 unsigned int new_rd;
2633
2634 do {
2635 rd = atomic_read(&buffer->record_disabled);
2636 new_rd = rd & ~RB_BUFFER_OFF;
2637 } while (atomic_cmpxchg(&buffer->record_disabled, rd, new_rd) != rd);
2638}
2639EXPORT_SYMBOL_GPL(ring_buffer_record_on);
2640
2641/**
2642 * ring_buffer_record_is_on - return true if the ring buffer can write
2643 * @buffer: The ring buffer to see if write is enabled
2644 *
2645 * Returns true if the ring buffer is in a state that it accepts writes.
2646 */
2647int ring_buffer_record_is_on(struct ring_buffer *buffer)
2648{
2649 return !atomic_read(&buffer->record_disabled);
2650}
2651
2652/**
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002653 * ring_buffer_record_disable_cpu - stop all writes into the cpu_buffer
2654 * @buffer: The ring buffer to stop writes to.
2655 * @cpu: The CPU buffer to stop
2656 *
2657 * This prevents all writes to the buffer. Any attempt to write
2658 * to the buffer after this will fail and return NULL.
2659 *
2660 * The caller should call synchronize_sched() after this.
2661 */
2662void ring_buffer_record_disable_cpu(struct ring_buffer *buffer, int cpu)
2663{
2664 struct ring_buffer_per_cpu *cpu_buffer;
2665
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302666 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04002667 return;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002668
2669 cpu_buffer = buffer->buffers[cpu];
2670 atomic_inc(&cpu_buffer->record_disabled);
2671}
Robert Richterc4f50182008-12-11 16:49:22 +01002672EXPORT_SYMBOL_GPL(ring_buffer_record_disable_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002673
2674/**
2675 * ring_buffer_record_enable_cpu - enable writes to the buffer
2676 * @buffer: The ring buffer to enable writes
2677 * @cpu: The CPU to enable.
2678 *
2679 * Note, multiple disables will need the same number of enables
Adam Buchbinderc41b20e2009-12-11 16:35:39 -05002680 * to truly enable the writing (much like preempt_disable).
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002681 */
2682void ring_buffer_record_enable_cpu(struct ring_buffer *buffer, int cpu)
2683{
2684 struct ring_buffer_per_cpu *cpu_buffer;
2685
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302686 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04002687 return;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002688
2689 cpu_buffer = buffer->buffers[cpu];
2690 atomic_dec(&cpu_buffer->record_disabled);
2691}
Robert Richterc4f50182008-12-11 16:49:22 +01002692EXPORT_SYMBOL_GPL(ring_buffer_record_enable_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002693
Steven Rostedtf6195aa2010-09-01 12:23:12 -04002694/*
2695 * The total entries in the ring buffer is the running counter
2696 * of entries entered into the ring buffer, minus the sum of
2697 * the entries read from the ring buffer and the number of
2698 * entries that were overwritten.
2699 */
2700static inline unsigned long
2701rb_num_of_entries(struct ring_buffer_per_cpu *cpu_buffer)
2702{
2703 return local_read(&cpu_buffer->entries) -
2704 (local_read(&cpu_buffer->overrun) + cpu_buffer->read);
2705}
2706
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002707/**
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -07002708 * ring_buffer_oldest_event_ts - get the oldest event timestamp from the buffer
2709 * @buffer: The ring buffer
2710 * @cpu: The per CPU buffer to read from.
2711 */
2712unsigned long ring_buffer_oldest_event_ts(struct ring_buffer *buffer, int cpu)
2713{
2714 unsigned long flags;
2715 struct ring_buffer_per_cpu *cpu_buffer;
2716 struct buffer_page *bpage;
2717 unsigned long ret;
2718
2719 if (!cpumask_test_cpu(cpu, buffer->cpumask))
2720 return 0;
2721
2722 cpu_buffer = buffer->buffers[cpu];
Linus Torvalds7115e3f2011-10-26 17:03:38 +02002723 raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -07002724 /*
2725 * if the tail is on reader_page, oldest time stamp is on the reader
2726 * page
2727 */
2728 if (cpu_buffer->tail_page == cpu_buffer->reader_page)
2729 bpage = cpu_buffer->reader_page;
2730 else
2731 bpage = rb_set_head_page(cpu_buffer);
2732 ret = bpage->page->time_stamp;
Linus Torvalds7115e3f2011-10-26 17:03:38 +02002733 raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -07002734
2735 return ret;
2736}
2737EXPORT_SYMBOL_GPL(ring_buffer_oldest_event_ts);
2738
2739/**
2740 * ring_buffer_bytes_cpu - get the number of bytes consumed in a cpu buffer
2741 * @buffer: The ring buffer
2742 * @cpu: The per CPU buffer to read from.
2743 */
2744unsigned long ring_buffer_bytes_cpu(struct ring_buffer *buffer, int cpu)
2745{
2746 struct ring_buffer_per_cpu *cpu_buffer;
2747 unsigned long ret;
2748
2749 if (!cpumask_test_cpu(cpu, buffer->cpumask))
2750 return 0;
2751
2752 cpu_buffer = buffer->buffers[cpu];
2753 ret = local_read(&cpu_buffer->entries_bytes) - cpu_buffer->read_bytes;
2754
2755 return ret;
2756}
2757EXPORT_SYMBOL_GPL(ring_buffer_bytes_cpu);
2758
2759/**
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002760 * ring_buffer_entries_cpu - get the number of entries in a cpu buffer
2761 * @buffer: The ring buffer
2762 * @cpu: The per CPU buffer to get the entries from.
2763 */
2764unsigned long ring_buffer_entries_cpu(struct ring_buffer *buffer, int cpu)
2765{
2766 struct ring_buffer_per_cpu *cpu_buffer;
2767
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302768 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04002769 return 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002770
2771 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt554f7862009-03-11 22:00:13 -04002772
Steven Rostedtf6195aa2010-09-01 12:23:12 -04002773 return rb_num_of_entries(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002774}
Robert Richterc4f50182008-12-11 16:49:22 +01002775EXPORT_SYMBOL_GPL(ring_buffer_entries_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002776
2777/**
2778 * ring_buffer_overrun_cpu - get the number of overruns in a cpu_buffer
2779 * @buffer: The ring buffer
2780 * @cpu: The per CPU buffer to get the number of overruns from
2781 */
2782unsigned long ring_buffer_overrun_cpu(struct ring_buffer *buffer, int cpu)
2783{
2784 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedt8aabee52009-03-12 13:13:49 -04002785 unsigned long ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002786
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302787 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04002788 return 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002789
2790 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt77ae3652009-03-27 11:00:29 -04002791 ret = local_read(&cpu_buffer->overrun);
Steven Rostedt554f7862009-03-11 22:00:13 -04002792
2793 return ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002794}
Robert Richterc4f50182008-12-11 16:49:22 +01002795EXPORT_SYMBOL_GPL(ring_buffer_overrun_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002796
2797/**
Steven Rostedtf0d2c682009-04-29 13:43:37 -04002798 * ring_buffer_commit_overrun_cpu - get the number of overruns caused by commits
2799 * @buffer: The ring buffer
2800 * @cpu: The per CPU buffer to get the number of overruns from
2801 */
2802unsigned long
2803ring_buffer_commit_overrun_cpu(struct ring_buffer *buffer, int cpu)
2804{
2805 struct ring_buffer_per_cpu *cpu_buffer;
2806 unsigned long ret;
2807
2808 if (!cpumask_test_cpu(cpu, buffer->cpumask))
2809 return 0;
2810
2811 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt77ae3652009-03-27 11:00:29 -04002812 ret = local_read(&cpu_buffer->commit_overrun);
Steven Rostedtf0d2c682009-04-29 13:43:37 -04002813
2814 return ret;
2815}
2816EXPORT_SYMBOL_GPL(ring_buffer_commit_overrun_cpu);
2817
2818/**
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002819 * ring_buffer_entries - get the number of entries in a buffer
2820 * @buffer: The ring buffer
2821 *
2822 * Returns the total number of entries in the ring buffer
2823 * (all CPU entries)
2824 */
2825unsigned long ring_buffer_entries(struct ring_buffer *buffer)
2826{
2827 struct ring_buffer_per_cpu *cpu_buffer;
2828 unsigned long entries = 0;
2829 int cpu;
2830
2831 /* if you care about this being correct, lock the buffer */
2832 for_each_buffer_cpu(buffer, cpu) {
2833 cpu_buffer = buffer->buffers[cpu];
Steven Rostedtf6195aa2010-09-01 12:23:12 -04002834 entries += rb_num_of_entries(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002835 }
2836
2837 return entries;
2838}
Robert Richterc4f50182008-12-11 16:49:22 +01002839EXPORT_SYMBOL_GPL(ring_buffer_entries);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002840
2841/**
Jiri Olsa67b394f2009-10-23 19:36:18 -04002842 * ring_buffer_overruns - get the number of overruns in buffer
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002843 * @buffer: The ring buffer
2844 *
2845 * Returns the total number of overruns in the ring buffer
2846 * (all CPU entries)
2847 */
2848unsigned long ring_buffer_overruns(struct ring_buffer *buffer)
2849{
2850 struct ring_buffer_per_cpu *cpu_buffer;
2851 unsigned long overruns = 0;
2852 int cpu;
2853
2854 /* if you care about this being correct, lock the buffer */
2855 for_each_buffer_cpu(buffer, cpu) {
2856 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt77ae3652009-03-27 11:00:29 -04002857 overruns += local_read(&cpu_buffer->overrun);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002858 }
2859
2860 return overruns;
2861}
Robert Richterc4f50182008-12-11 16:49:22 +01002862EXPORT_SYMBOL_GPL(ring_buffer_overruns);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002863
Steven Rostedt642edba2008-11-12 00:01:26 -05002864static void rb_iter_reset(struct ring_buffer_iter *iter)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002865{
2866 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
2867
Steven Rostedtd7690412008-10-01 00:29:53 -04002868 /* Iterator usage is expected to have record disabled */
2869 if (list_empty(&cpu_buffer->reader_page->list)) {
Steven Rostedt77ae3652009-03-27 11:00:29 -04002870 iter->head_page = rb_set_head_page(cpu_buffer);
2871 if (unlikely(!iter->head_page))
2872 return;
2873 iter->head = iter->head_page->read;
Steven Rostedtd7690412008-10-01 00:29:53 -04002874 } else {
2875 iter->head_page = cpu_buffer->reader_page;
Steven Rostedt6f807ac2008-10-04 02:00:58 -04002876 iter->head = cpu_buffer->reader_page->read;
Steven Rostedtd7690412008-10-01 00:29:53 -04002877 }
2878 if (iter->head)
2879 iter->read_stamp = cpu_buffer->read_stamp;
2880 else
Steven Rostedtabc9b562008-12-02 15:34:06 -05002881 iter->read_stamp = iter->head_page->page->time_stamp;
Steven Rostedt492a74f2010-01-25 15:17:47 -05002882 iter->cache_reader_page = cpu_buffer->reader_page;
2883 iter->cache_read = cpu_buffer->read;
Steven Rostedt642edba2008-11-12 00:01:26 -05002884}
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002885
Steven Rostedt642edba2008-11-12 00:01:26 -05002886/**
2887 * ring_buffer_iter_reset - reset an iterator
2888 * @iter: The iterator to reset
2889 *
2890 * Resets the iterator, so that it will start from the beginning
2891 * again.
2892 */
2893void ring_buffer_iter_reset(struct ring_buffer_iter *iter)
2894{
Steven Rostedt554f7862009-03-11 22:00:13 -04002895 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedt642edba2008-11-12 00:01:26 -05002896 unsigned long flags;
2897
Steven Rostedt554f7862009-03-11 22:00:13 -04002898 if (!iter)
2899 return;
2900
2901 cpu_buffer = iter->cpu_buffer;
2902
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02002903 raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
Steven Rostedt642edba2008-11-12 00:01:26 -05002904 rb_iter_reset(iter);
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02002905 raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002906}
Robert Richterc4f50182008-12-11 16:49:22 +01002907EXPORT_SYMBOL_GPL(ring_buffer_iter_reset);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002908
2909/**
2910 * ring_buffer_iter_empty - check if an iterator has no more to read
2911 * @iter: The iterator to check
2912 */
2913int ring_buffer_iter_empty(struct ring_buffer_iter *iter)
2914{
2915 struct ring_buffer_per_cpu *cpu_buffer;
2916
2917 cpu_buffer = iter->cpu_buffer;
2918
Steven Rostedtbf41a152008-10-04 02:00:59 -04002919 return iter->head_page == cpu_buffer->commit_page &&
2920 iter->head == rb_commit_index(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002921}
Robert Richterc4f50182008-12-11 16:49:22 +01002922EXPORT_SYMBOL_GPL(ring_buffer_iter_empty);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002923
2924static void
2925rb_update_read_stamp(struct ring_buffer_per_cpu *cpu_buffer,
2926 struct ring_buffer_event *event)
2927{
2928 u64 delta;
2929
Lai Jiangshan334d4162009-04-24 11:27:05 +08002930 switch (event->type_len) {
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002931 case RINGBUF_TYPE_PADDING:
2932 return;
2933
2934 case RINGBUF_TYPE_TIME_EXTEND:
2935 delta = event->array[0];
2936 delta <<= TS_SHIFT;
2937 delta += event->time_delta;
2938 cpu_buffer->read_stamp += delta;
2939 return;
2940
2941 case RINGBUF_TYPE_TIME_STAMP:
2942 /* FIXME: not implemented */
2943 return;
2944
2945 case RINGBUF_TYPE_DATA:
2946 cpu_buffer->read_stamp += event->time_delta;
2947 return;
2948
2949 default:
2950 BUG();
2951 }
2952 return;
2953}
2954
2955static void
2956rb_update_iter_read_stamp(struct ring_buffer_iter *iter,
2957 struct ring_buffer_event *event)
2958{
2959 u64 delta;
2960
Lai Jiangshan334d4162009-04-24 11:27:05 +08002961 switch (event->type_len) {
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002962 case RINGBUF_TYPE_PADDING:
2963 return;
2964
2965 case RINGBUF_TYPE_TIME_EXTEND:
2966 delta = event->array[0];
2967 delta <<= TS_SHIFT;
2968 delta += event->time_delta;
2969 iter->read_stamp += delta;
2970 return;
2971
2972 case RINGBUF_TYPE_TIME_STAMP:
2973 /* FIXME: not implemented */
2974 return;
2975
2976 case RINGBUF_TYPE_DATA:
2977 iter->read_stamp += event->time_delta;
2978 return;
2979
2980 default:
2981 BUG();
2982 }
2983 return;
2984}
2985
Steven Rostedtd7690412008-10-01 00:29:53 -04002986static struct buffer_page *
2987rb_get_reader_page(struct ring_buffer_per_cpu *cpu_buffer)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002988{
Steven Rostedtd7690412008-10-01 00:29:53 -04002989 struct buffer_page *reader = NULL;
Steven Rostedt66a8cb92010-03-31 13:21:56 -04002990 unsigned long overwrite;
Steven Rostedtd7690412008-10-01 00:29:53 -04002991 unsigned long flags;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002992 int nr_loops = 0;
Steven Rostedt77ae3652009-03-27 11:00:29 -04002993 int ret;
Steven Rostedtd7690412008-10-01 00:29:53 -04002994
Steven Rostedt3e03fb72008-11-06 00:09:43 -05002995 local_irq_save(flags);
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01002996 arch_spin_lock(&cpu_buffer->lock);
Steven Rostedtd7690412008-10-01 00:29:53 -04002997
2998 again:
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002999 /*
3000 * This should normally only loop twice. But because the
3001 * start of the reader inserts an empty page, it causes
3002 * a case where we will loop three times. There should be no
3003 * reason to loop four times (that I know of).
3004 */
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05003005 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 3)) {
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003006 reader = NULL;
3007 goto out;
3008 }
3009
Steven Rostedtd7690412008-10-01 00:29:53 -04003010 reader = cpu_buffer->reader_page;
3011
3012 /* If there's more to read, return this page */
Steven Rostedtbf41a152008-10-04 02:00:59 -04003013 if (cpu_buffer->reader_page->read < rb_page_size(reader))
Steven Rostedtd7690412008-10-01 00:29:53 -04003014 goto out;
3015
3016 /* Never should we have an index greater than the size */
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05003017 if (RB_WARN_ON(cpu_buffer,
3018 cpu_buffer->reader_page->read > rb_page_size(reader)))
3019 goto out;
Steven Rostedtd7690412008-10-01 00:29:53 -04003020
3021 /* check if we caught up to the tail */
3022 reader = NULL;
Steven Rostedtbf41a152008-10-04 02:00:59 -04003023 if (cpu_buffer->commit_page == cpu_buffer->reader_page)
Steven Rostedtd7690412008-10-01 00:29:53 -04003024 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003025
3026 /*
Steven Rostedtd7690412008-10-01 00:29:53 -04003027 * Reset the reader page to size zero.
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003028 */
Steven Rostedt77ae3652009-03-27 11:00:29 -04003029 local_set(&cpu_buffer->reader_page->write, 0);
3030 local_set(&cpu_buffer->reader_page->entries, 0);
3031 local_set(&cpu_buffer->reader_page->page->commit, 0);
Steven Rostedtff0ff842010-03-31 22:11:42 -04003032 cpu_buffer->reader_page->real_end = 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003033
Steven Rostedt77ae3652009-03-27 11:00:29 -04003034 spin:
3035 /*
3036 * Splice the empty reader page into the list around the head.
3037 */
3038 reader = rb_set_head_page(cpu_buffer);
Steven Rostedt0e1ff5d2010-01-06 20:40:44 -05003039 cpu_buffer->reader_page->list.next = rb_list_head(reader->list.next);
Steven Rostedtd7690412008-10-01 00:29:53 -04003040 cpu_buffer->reader_page->list.prev = reader->list.prev;
Steven Rostedtbf41a152008-10-04 02:00:59 -04003041
Steven Rostedt3adc54f2009-03-30 15:32:01 -04003042 /*
3043 * cpu_buffer->pages just needs to point to the buffer, it
3044 * has no specific buffer page to point to. Lets move it out
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003045 * of our way so we don't accidentally swap it.
Steven Rostedt3adc54f2009-03-30 15:32:01 -04003046 */
3047 cpu_buffer->pages = reader->list.prev;
3048
Steven Rostedt77ae3652009-03-27 11:00:29 -04003049 /* The reader page will be pointing to the new head */
3050 rb_set_list_to_head(cpu_buffer, &cpu_buffer->reader_page->list);
Steven Rostedtd7690412008-10-01 00:29:53 -04003051
3052 /*
Steven Rostedt66a8cb92010-03-31 13:21:56 -04003053 * We want to make sure we read the overruns after we set up our
3054 * pointers to the next object. The writer side does a
3055 * cmpxchg to cross pages which acts as the mb on the writer
3056 * side. Note, the reader will constantly fail the swap
3057 * while the writer is updating the pointers, so this
3058 * guarantees that the overwrite recorded here is the one we
3059 * want to compare with the last_overrun.
3060 */
3061 smp_mb();
3062 overwrite = local_read(&(cpu_buffer->overrun));
3063
3064 /*
Steven Rostedt77ae3652009-03-27 11:00:29 -04003065 * Here's the tricky part.
3066 *
3067 * We need to move the pointer past the header page.
3068 * But we can only do that if a writer is not currently
3069 * moving it. The page before the header page has the
3070 * flag bit '1' set if it is pointing to the page we want.
3071 * but if the writer is in the process of moving it
3072 * than it will be '2' or already moved '0'.
Steven Rostedtd7690412008-10-01 00:29:53 -04003073 */
Steven Rostedtd7690412008-10-01 00:29:53 -04003074
Steven Rostedt77ae3652009-03-27 11:00:29 -04003075 ret = rb_head_page_replace(reader, cpu_buffer->reader_page);
3076
3077 /*
3078 * If we did not convert it, then we must try again.
3079 */
3080 if (!ret)
3081 goto spin;
3082
3083 /*
3084 * Yeah! We succeeded in replacing the page.
3085 *
3086 * Now make the new head point back to the reader page.
3087 */
David Sharp5ded3dc2010-01-06 17:12:07 -08003088 rb_list_head(reader->list.next)->prev = &cpu_buffer->reader_page->list;
Steven Rostedt77ae3652009-03-27 11:00:29 -04003089 rb_inc_page(cpu_buffer, &cpu_buffer->head_page);
Steven Rostedtd7690412008-10-01 00:29:53 -04003090
3091 /* Finally update the reader page to the new head */
3092 cpu_buffer->reader_page = reader;
3093 rb_reset_reader_page(cpu_buffer);
3094
Steven Rostedt66a8cb92010-03-31 13:21:56 -04003095 if (overwrite != cpu_buffer->last_overrun) {
3096 cpu_buffer->lost_events = overwrite - cpu_buffer->last_overrun;
3097 cpu_buffer->last_overrun = overwrite;
3098 }
3099
Steven Rostedtd7690412008-10-01 00:29:53 -04003100 goto again;
3101
3102 out:
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01003103 arch_spin_unlock(&cpu_buffer->lock);
Steven Rostedt3e03fb72008-11-06 00:09:43 -05003104 local_irq_restore(flags);
Steven Rostedtd7690412008-10-01 00:29:53 -04003105
3106 return reader;
3107}
3108
3109static void rb_advance_reader(struct ring_buffer_per_cpu *cpu_buffer)
3110{
3111 struct ring_buffer_event *event;
3112 struct buffer_page *reader;
3113 unsigned length;
3114
3115 reader = rb_get_reader_page(cpu_buffer);
3116
3117 /* This function should not be called when buffer is empty */
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05003118 if (RB_WARN_ON(cpu_buffer, !reader))
3119 return;
Steven Rostedtd7690412008-10-01 00:29:53 -04003120
3121 event = rb_reader_event(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003122
Steven Rostedta1863c22009-09-03 10:23:58 -04003123 if (event->type_len <= RINGBUF_TYPE_DATA_TYPE_LEN_MAX)
Steven Rostedte4906ef2009-04-30 20:49:44 -04003124 cpu_buffer->read++;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003125
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003126 rb_update_read_stamp(cpu_buffer, event);
3127
Steven Rostedtd7690412008-10-01 00:29:53 -04003128 length = rb_event_length(event);
Steven Rostedt6f807ac2008-10-04 02:00:58 -04003129 cpu_buffer->reader_page->read += length;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003130}
3131
3132static void rb_advance_iter(struct ring_buffer_iter *iter)
3133{
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003134 struct ring_buffer_per_cpu *cpu_buffer;
3135 struct ring_buffer_event *event;
3136 unsigned length;
3137
3138 cpu_buffer = iter->cpu_buffer;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003139
3140 /*
3141 * Check if we are at the end of the buffer.
3142 */
Steven Rostedtbf41a152008-10-04 02:00:59 -04003143 if (iter->head >= rb_page_size(iter->head_page)) {
Steven Rostedtea05b572009-06-03 09:30:10 -04003144 /* discarded commits can make the page empty */
3145 if (iter->head_page == cpu_buffer->commit_page)
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05003146 return;
Steven Rostedtd7690412008-10-01 00:29:53 -04003147 rb_inc_iter(iter);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003148 return;
3149 }
3150
3151 event = rb_iter_head_event(iter);
3152
3153 length = rb_event_length(event);
3154
3155 /*
3156 * This should not be called to advance the header if we are
3157 * at the tail of the buffer.
3158 */
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05003159 if (RB_WARN_ON(cpu_buffer,
Steven Rostedtf536aaf2008-11-10 23:07:30 -05003160 (iter->head_page == cpu_buffer->commit_page) &&
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05003161 (iter->head + length > rb_commit_index(cpu_buffer))))
3162 return;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003163
3164 rb_update_iter_read_stamp(iter, event);
3165
3166 iter->head += length;
3167
3168 /* check for end of page padding */
Steven Rostedtbf41a152008-10-04 02:00:59 -04003169 if ((iter->head >= rb_page_size(iter->head_page)) &&
3170 (iter->head_page != cpu_buffer->commit_page))
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003171 rb_advance_iter(iter);
3172}
3173
Steven Rostedt66a8cb92010-03-31 13:21:56 -04003174static int rb_lost_events(struct ring_buffer_per_cpu *cpu_buffer)
3175{
3176 return cpu_buffer->lost_events;
3177}
3178
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003179static struct ring_buffer_event *
Steven Rostedt66a8cb92010-03-31 13:21:56 -04003180rb_buffer_peek(struct ring_buffer_per_cpu *cpu_buffer, u64 *ts,
3181 unsigned long *lost_events)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003182{
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003183 struct ring_buffer_event *event;
Steven Rostedtd7690412008-10-01 00:29:53 -04003184 struct buffer_page *reader;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003185 int nr_loops = 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003186
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003187 again:
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003188 /*
Steven Rostedt69d1b832010-10-07 18:18:05 -04003189 * We repeat when a time extend is encountered.
3190 * Since the time extend is always attached to a data event,
3191 * we should never loop more than once.
3192 * (We never hit the following condition more than twice).
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003193 */
Steven Rostedt69d1b832010-10-07 18:18:05 -04003194 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 2))
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003195 return NULL;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003196
Steven Rostedtd7690412008-10-01 00:29:53 -04003197 reader = rb_get_reader_page(cpu_buffer);
3198 if (!reader)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003199 return NULL;
3200
Steven Rostedtd7690412008-10-01 00:29:53 -04003201 event = rb_reader_event(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003202
Lai Jiangshan334d4162009-04-24 11:27:05 +08003203 switch (event->type_len) {
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003204 case RINGBUF_TYPE_PADDING:
Tom Zanussi2d622712009-03-22 03:30:49 -05003205 if (rb_null_event(event))
3206 RB_WARN_ON(cpu_buffer, 1);
3207 /*
3208 * Because the writer could be discarding every
3209 * event it creates (which would probably be bad)
3210 * if we were to go back to "again" then we may never
3211 * catch up, and will trigger the warn on, or lock
3212 * the box. Return the padding, and we will release
3213 * the current locks, and try again.
3214 */
Tom Zanussi2d622712009-03-22 03:30:49 -05003215 return event;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003216
3217 case RINGBUF_TYPE_TIME_EXTEND:
3218 /* Internal data, OK to advance */
Steven Rostedtd7690412008-10-01 00:29:53 -04003219 rb_advance_reader(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003220 goto again;
3221
3222 case RINGBUF_TYPE_TIME_STAMP:
3223 /* FIXME: not implemented */
Steven Rostedtd7690412008-10-01 00:29:53 -04003224 rb_advance_reader(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003225 goto again;
3226
3227 case RINGBUF_TYPE_DATA:
3228 if (ts) {
3229 *ts = cpu_buffer->read_stamp + event->time_delta;
Robert Richterd8eeb2d2009-07-31 14:58:04 +02003230 ring_buffer_normalize_time_stamp(cpu_buffer->buffer,
Steven Rostedt37886f62009-03-17 17:22:06 -04003231 cpu_buffer->cpu, ts);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003232 }
Steven Rostedt66a8cb92010-03-31 13:21:56 -04003233 if (lost_events)
3234 *lost_events = rb_lost_events(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003235 return event;
3236
3237 default:
3238 BUG();
3239 }
3240
3241 return NULL;
3242}
Robert Richterc4f50182008-12-11 16:49:22 +01003243EXPORT_SYMBOL_GPL(ring_buffer_peek);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003244
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003245static struct ring_buffer_event *
3246rb_iter_peek(struct ring_buffer_iter *iter, u64 *ts)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003247{
3248 struct ring_buffer *buffer;
3249 struct ring_buffer_per_cpu *cpu_buffer;
3250 struct ring_buffer_event *event;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003251 int nr_loops = 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003252
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003253 cpu_buffer = iter->cpu_buffer;
3254 buffer = cpu_buffer->buffer;
3255
Steven Rostedt492a74f2010-01-25 15:17:47 -05003256 /*
3257 * Check if someone performed a consuming read to
3258 * the buffer. A consuming read invalidates the iterator
3259 * and we need to reset the iterator in this case.
3260 */
3261 if (unlikely(iter->cache_read != cpu_buffer->read ||
3262 iter->cache_reader_page != cpu_buffer->reader_page))
3263 rb_iter_reset(iter);
3264
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003265 again:
Steven Rostedt3c05d742010-01-26 16:14:08 -05003266 if (ring_buffer_iter_empty(iter))
3267 return NULL;
3268
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003269 /*
Steven Rostedt69d1b832010-10-07 18:18:05 -04003270 * We repeat when a time extend is encountered.
3271 * Since the time extend is always attached to a data event,
3272 * we should never loop more than once.
3273 * (We never hit the following condition more than twice).
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003274 */
Steven Rostedt69d1b832010-10-07 18:18:05 -04003275 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 2))
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003276 return NULL;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003277
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003278 if (rb_per_cpu_empty(cpu_buffer))
3279 return NULL;
3280
Steven Rostedt3c05d742010-01-26 16:14:08 -05003281 if (iter->head >= local_read(&iter->head_page->page->commit)) {
3282 rb_inc_iter(iter);
3283 goto again;
3284 }
3285
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003286 event = rb_iter_head_event(iter);
3287
Lai Jiangshan334d4162009-04-24 11:27:05 +08003288 switch (event->type_len) {
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003289 case RINGBUF_TYPE_PADDING:
Tom Zanussi2d622712009-03-22 03:30:49 -05003290 if (rb_null_event(event)) {
3291 rb_inc_iter(iter);
3292 goto again;
3293 }
3294 rb_advance_iter(iter);
3295 return event;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003296
3297 case RINGBUF_TYPE_TIME_EXTEND:
3298 /* Internal data, OK to advance */
3299 rb_advance_iter(iter);
3300 goto again;
3301
3302 case RINGBUF_TYPE_TIME_STAMP:
3303 /* FIXME: not implemented */
3304 rb_advance_iter(iter);
3305 goto again;
3306
3307 case RINGBUF_TYPE_DATA:
3308 if (ts) {
3309 *ts = iter->read_stamp + event->time_delta;
Steven Rostedt37886f62009-03-17 17:22:06 -04003310 ring_buffer_normalize_time_stamp(buffer,
3311 cpu_buffer->cpu, ts);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003312 }
3313 return event;
3314
3315 default:
3316 BUG();
3317 }
3318
3319 return NULL;
3320}
Robert Richterc4f50182008-12-11 16:49:22 +01003321EXPORT_SYMBOL_GPL(ring_buffer_iter_peek);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003322
Steven Rostedt8d707e82009-06-16 21:22:48 -04003323static inline int rb_ok_to_lock(void)
3324{
3325 /*
3326 * If an NMI die dumps out the content of the ring buffer
3327 * do not grab locks. We also permanently disable the ring
3328 * buffer too. A one time deal is all you get from reading
3329 * the ring buffer from an NMI.
3330 */
Steven Rostedt464e85e2009-08-05 15:26:37 -04003331 if (likely(!in_nmi()))
Steven Rostedt8d707e82009-06-16 21:22:48 -04003332 return 1;
3333
3334 tracing_off_permanent();
3335 return 0;
3336}
3337
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003338/**
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003339 * ring_buffer_peek - peek at the next event to be read
3340 * @buffer: The ring buffer to read
3341 * @cpu: The cpu to peak at
3342 * @ts: The timestamp counter of this event.
Steven Rostedt66a8cb92010-03-31 13:21:56 -04003343 * @lost_events: a variable to store if events were lost (may be NULL)
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003344 *
3345 * This will return the event that will be read next, but does
3346 * not consume the data.
3347 */
3348struct ring_buffer_event *
Steven Rostedt66a8cb92010-03-31 13:21:56 -04003349ring_buffer_peek(struct ring_buffer *buffer, int cpu, u64 *ts,
3350 unsigned long *lost_events)
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003351{
3352 struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
Steven Rostedt8aabee52009-03-12 13:13:49 -04003353 struct ring_buffer_event *event;
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003354 unsigned long flags;
Steven Rostedt8d707e82009-06-16 21:22:48 -04003355 int dolock;
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003356
Steven Rostedt554f7862009-03-11 22:00:13 -04003357 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04003358 return NULL;
Steven Rostedt554f7862009-03-11 22:00:13 -04003359
Steven Rostedt8d707e82009-06-16 21:22:48 -04003360 dolock = rb_ok_to_lock();
Tom Zanussi2d622712009-03-22 03:30:49 -05003361 again:
Steven Rostedt8d707e82009-06-16 21:22:48 -04003362 local_irq_save(flags);
3363 if (dolock)
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02003364 raw_spin_lock(&cpu_buffer->reader_lock);
Steven Rostedt66a8cb92010-03-31 13:21:56 -04003365 event = rb_buffer_peek(cpu_buffer, ts, lost_events);
Robert Richter469535a2009-07-30 19:19:18 +02003366 if (event && event->type_len == RINGBUF_TYPE_PADDING)
3367 rb_advance_reader(cpu_buffer);
Steven Rostedt8d707e82009-06-16 21:22:48 -04003368 if (dolock)
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02003369 raw_spin_unlock(&cpu_buffer->reader_lock);
Steven Rostedt8d707e82009-06-16 21:22:48 -04003370 local_irq_restore(flags);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003371
Steven Rostedt1b959e12009-09-03 10:12:13 -04003372 if (event && event->type_len == RINGBUF_TYPE_PADDING)
Tom Zanussi2d622712009-03-22 03:30:49 -05003373 goto again;
Tom Zanussi2d622712009-03-22 03:30:49 -05003374
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003375 return event;
3376}
3377
3378/**
3379 * ring_buffer_iter_peek - peek at the next event to be read
3380 * @iter: The ring buffer iterator
3381 * @ts: The timestamp counter of this event.
3382 *
3383 * This will return the event that will be read next, but does
3384 * not increment the iterator.
3385 */
3386struct ring_buffer_event *
3387ring_buffer_iter_peek(struct ring_buffer_iter *iter, u64 *ts)
3388{
3389 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
3390 struct ring_buffer_event *event;
3391 unsigned long flags;
3392
Tom Zanussi2d622712009-03-22 03:30:49 -05003393 again:
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02003394 raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003395 event = rb_iter_peek(iter, ts);
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02003396 raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003397
Steven Rostedt1b959e12009-09-03 10:12:13 -04003398 if (event && event->type_len == RINGBUF_TYPE_PADDING)
Tom Zanussi2d622712009-03-22 03:30:49 -05003399 goto again;
Tom Zanussi2d622712009-03-22 03:30:49 -05003400
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003401 return event;
3402}
3403
3404/**
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003405 * ring_buffer_consume - return an event and consume it
3406 * @buffer: The ring buffer to get the next event from
Steven Rostedt66a8cb92010-03-31 13:21:56 -04003407 * @cpu: the cpu to read the buffer from
3408 * @ts: a variable to store the timestamp (may be NULL)
3409 * @lost_events: a variable to store if events were lost (may be NULL)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003410 *
3411 * Returns the next event in the ring buffer, and that event is consumed.
3412 * Meaning, that sequential reads will keep returning a different event,
3413 * and eventually empty the ring buffer if the producer is slower.
3414 */
3415struct ring_buffer_event *
Steven Rostedt66a8cb92010-03-31 13:21:56 -04003416ring_buffer_consume(struct ring_buffer *buffer, int cpu, u64 *ts,
3417 unsigned long *lost_events)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003418{
Steven Rostedt554f7862009-03-11 22:00:13 -04003419 struct ring_buffer_per_cpu *cpu_buffer;
3420 struct ring_buffer_event *event = NULL;
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003421 unsigned long flags;
Steven Rostedt8d707e82009-06-16 21:22:48 -04003422 int dolock;
3423
3424 dolock = rb_ok_to_lock();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003425
Tom Zanussi2d622712009-03-22 03:30:49 -05003426 again:
Steven Rostedt554f7862009-03-11 22:00:13 -04003427 /* might be called in atomic */
3428 preempt_disable();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003429
Steven Rostedt554f7862009-03-11 22:00:13 -04003430 if (!cpumask_test_cpu(cpu, buffer->cpumask))
3431 goto out;
3432
3433 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt8d707e82009-06-16 21:22:48 -04003434 local_irq_save(flags);
3435 if (dolock)
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02003436 raw_spin_lock(&cpu_buffer->reader_lock);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003437
Steven Rostedt66a8cb92010-03-31 13:21:56 -04003438 event = rb_buffer_peek(cpu_buffer, ts, lost_events);
3439 if (event) {
3440 cpu_buffer->lost_events = 0;
Robert Richter469535a2009-07-30 19:19:18 +02003441 rb_advance_reader(cpu_buffer);
Steven Rostedt66a8cb92010-03-31 13:21:56 -04003442 }
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003443
Steven Rostedt8d707e82009-06-16 21:22:48 -04003444 if (dolock)
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02003445 raw_spin_unlock(&cpu_buffer->reader_lock);
Steven Rostedt8d707e82009-06-16 21:22:48 -04003446 local_irq_restore(flags);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003447
Steven Rostedt554f7862009-03-11 22:00:13 -04003448 out:
3449 preempt_enable();
3450
Steven Rostedt1b959e12009-09-03 10:12:13 -04003451 if (event && event->type_len == RINGBUF_TYPE_PADDING)
Tom Zanussi2d622712009-03-22 03:30:49 -05003452 goto again;
Tom Zanussi2d622712009-03-22 03:30:49 -05003453
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003454 return event;
3455}
Robert Richterc4f50182008-12-11 16:49:22 +01003456EXPORT_SYMBOL_GPL(ring_buffer_consume);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003457
3458/**
David Miller72c9ddf2010-04-20 15:47:11 -07003459 * ring_buffer_read_prepare - Prepare for a non consuming read of the buffer
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003460 * @buffer: The ring buffer to read from
3461 * @cpu: The cpu buffer to iterate over
3462 *
David Miller72c9ddf2010-04-20 15:47:11 -07003463 * This performs the initial preparations necessary to iterate
3464 * through the buffer. Memory is allocated, buffer recording
3465 * is disabled, and the iterator pointer is returned to the caller.
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003466 *
David Miller72c9ddf2010-04-20 15:47:11 -07003467 * Disabling buffer recordng prevents the reading from being
3468 * corrupted. This is not a consuming read, so a producer is not
3469 * expected.
3470 *
3471 * After a sequence of ring_buffer_read_prepare calls, the user is
3472 * expected to make at least one call to ring_buffer_prepare_sync.
3473 * Afterwards, ring_buffer_read_start is invoked to get things going
3474 * for real.
3475 *
3476 * This overall must be paired with ring_buffer_finish.
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003477 */
3478struct ring_buffer_iter *
David Miller72c9ddf2010-04-20 15:47:11 -07003479ring_buffer_read_prepare(struct ring_buffer *buffer, int cpu)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003480{
3481 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedt8aabee52009-03-12 13:13:49 -04003482 struct ring_buffer_iter *iter;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003483
Rusty Russell9e01c1b2009-01-01 10:12:22 +10303484 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04003485 return NULL;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003486
3487 iter = kmalloc(sizeof(*iter), GFP_KERNEL);
3488 if (!iter)
Steven Rostedt8aabee52009-03-12 13:13:49 -04003489 return NULL;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003490
3491 cpu_buffer = buffer->buffers[cpu];
3492
3493 iter->cpu_buffer = cpu_buffer;
3494
3495 atomic_inc(&cpu_buffer->record_disabled);
David Miller72c9ddf2010-04-20 15:47:11 -07003496
3497 return iter;
3498}
3499EXPORT_SYMBOL_GPL(ring_buffer_read_prepare);
3500
3501/**
3502 * ring_buffer_read_prepare_sync - Synchronize a set of prepare calls
3503 *
3504 * All previously invoked ring_buffer_read_prepare calls to prepare
3505 * iterators will be synchronized. Afterwards, read_buffer_read_start
3506 * calls on those iterators are allowed.
3507 */
3508void
3509ring_buffer_read_prepare_sync(void)
3510{
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003511 synchronize_sched();
David Miller72c9ddf2010-04-20 15:47:11 -07003512}
3513EXPORT_SYMBOL_GPL(ring_buffer_read_prepare_sync);
3514
3515/**
3516 * ring_buffer_read_start - start a non consuming read of the buffer
3517 * @iter: The iterator returned by ring_buffer_read_prepare
3518 *
3519 * This finalizes the startup of an iteration through the buffer.
3520 * The iterator comes from a call to ring_buffer_read_prepare and
3521 * an intervening ring_buffer_read_prepare_sync must have been
3522 * performed.
3523 *
3524 * Must be paired with ring_buffer_finish.
3525 */
3526void
3527ring_buffer_read_start(struct ring_buffer_iter *iter)
3528{
3529 struct ring_buffer_per_cpu *cpu_buffer;
3530 unsigned long flags;
3531
3532 if (!iter)
3533 return;
3534
3535 cpu_buffer = iter->cpu_buffer;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003536
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02003537 raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01003538 arch_spin_lock(&cpu_buffer->lock);
Steven Rostedt642edba2008-11-12 00:01:26 -05003539 rb_iter_reset(iter);
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01003540 arch_spin_unlock(&cpu_buffer->lock);
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02003541 raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003542}
Robert Richterc4f50182008-12-11 16:49:22 +01003543EXPORT_SYMBOL_GPL(ring_buffer_read_start);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003544
3545/**
3546 * ring_buffer_finish - finish reading the iterator of the buffer
3547 * @iter: The iterator retrieved by ring_buffer_start
3548 *
3549 * This re-enables the recording to the buffer, and frees the
3550 * iterator.
3551 */
3552void
3553ring_buffer_read_finish(struct ring_buffer_iter *iter)
3554{
3555 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
3556
3557 atomic_dec(&cpu_buffer->record_disabled);
3558 kfree(iter);
3559}
Robert Richterc4f50182008-12-11 16:49:22 +01003560EXPORT_SYMBOL_GPL(ring_buffer_read_finish);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003561
3562/**
3563 * ring_buffer_read - read the next item in the ring buffer by the iterator
3564 * @iter: The ring buffer iterator
3565 * @ts: The time stamp of the event read.
3566 *
3567 * This reads the next event in the ring buffer and increments the iterator.
3568 */
3569struct ring_buffer_event *
3570ring_buffer_read(struct ring_buffer_iter *iter, u64 *ts)
3571{
3572 struct ring_buffer_event *event;
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003573 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
3574 unsigned long flags;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003575
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02003576 raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
Steven Rostedt7e9391c2009-09-03 10:02:09 -04003577 again:
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003578 event = rb_iter_peek(iter, ts);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003579 if (!event)
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003580 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003581
Steven Rostedt7e9391c2009-09-03 10:02:09 -04003582 if (event->type_len == RINGBUF_TYPE_PADDING)
3583 goto again;
3584
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003585 rb_advance_iter(iter);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003586 out:
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02003587 raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003588
3589 return event;
3590}
Robert Richterc4f50182008-12-11 16:49:22 +01003591EXPORT_SYMBOL_GPL(ring_buffer_read);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003592
3593/**
3594 * ring_buffer_size - return the size of the ring buffer (in bytes)
3595 * @buffer: The ring buffer.
3596 */
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08003597unsigned long ring_buffer_size(struct ring_buffer *buffer, int cpu)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003598{
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08003599 /*
3600 * Earlier, this method returned
3601 * BUF_PAGE_SIZE * buffer->nr_pages
3602 * Since the nr_pages field is now removed, we have converted this to
3603 * return the per cpu buffer value.
3604 */
3605 if (!cpumask_test_cpu(cpu, buffer->cpumask))
3606 return 0;
3607
3608 return BUF_PAGE_SIZE * buffer->buffers[cpu]->nr_pages;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003609}
Robert Richterc4f50182008-12-11 16:49:22 +01003610EXPORT_SYMBOL_GPL(ring_buffer_size);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003611
3612static void
3613rb_reset_cpu(struct ring_buffer_per_cpu *cpu_buffer)
3614{
Steven Rostedt77ae3652009-03-27 11:00:29 -04003615 rb_head_page_deactivate(cpu_buffer);
3616
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003617 cpu_buffer->head_page
Steven Rostedt3adc54f2009-03-30 15:32:01 -04003618 = list_entry(cpu_buffer->pages, struct buffer_page, list);
Steven Rostedtbf41a152008-10-04 02:00:59 -04003619 local_set(&cpu_buffer->head_page->write, 0);
Steven Rostedt778c55d2009-05-01 18:44:45 -04003620 local_set(&cpu_buffer->head_page->entries, 0);
Steven Rostedtabc9b562008-12-02 15:34:06 -05003621 local_set(&cpu_buffer->head_page->page->commit, 0);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003622
Steven Rostedt6f807ac2008-10-04 02:00:58 -04003623 cpu_buffer->head_page->read = 0;
Steven Rostedtbf41a152008-10-04 02:00:59 -04003624
3625 cpu_buffer->tail_page = cpu_buffer->head_page;
3626 cpu_buffer->commit_page = cpu_buffer->head_page;
3627
3628 INIT_LIST_HEAD(&cpu_buffer->reader_page->list);
3629 local_set(&cpu_buffer->reader_page->write, 0);
Steven Rostedt778c55d2009-05-01 18:44:45 -04003630 local_set(&cpu_buffer->reader_page->entries, 0);
Steven Rostedtabc9b562008-12-02 15:34:06 -05003631 local_set(&cpu_buffer->reader_page->page->commit, 0);
Steven Rostedt6f807ac2008-10-04 02:00:58 -04003632 cpu_buffer->reader_page->read = 0;
Steven Rostedtd7690412008-10-01 00:29:53 -04003633
Steven Rostedt77ae3652009-03-27 11:00:29 -04003634 local_set(&cpu_buffer->commit_overrun, 0);
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -07003635 local_set(&cpu_buffer->entries_bytes, 0);
Steven Rostedt77ae3652009-03-27 11:00:29 -04003636 local_set(&cpu_buffer->overrun, 0);
Steven Rostedte4906ef2009-04-30 20:49:44 -04003637 local_set(&cpu_buffer->entries, 0);
Steven Rostedtfa743952009-06-16 12:37:57 -04003638 local_set(&cpu_buffer->committing, 0);
3639 local_set(&cpu_buffer->commits, 0);
Steven Rostedt77ae3652009-03-27 11:00:29 -04003640 cpu_buffer->read = 0;
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -07003641 cpu_buffer->read_bytes = 0;
Steven Rostedt69507c02009-01-21 18:45:57 -05003642
3643 cpu_buffer->write_stamp = 0;
3644 cpu_buffer->read_stamp = 0;
Steven Rostedt77ae3652009-03-27 11:00:29 -04003645
Steven Rostedt66a8cb92010-03-31 13:21:56 -04003646 cpu_buffer->lost_events = 0;
3647 cpu_buffer->last_overrun = 0;
3648
Steven Rostedt77ae3652009-03-27 11:00:29 -04003649 rb_head_page_activate(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003650}
3651
3652/**
3653 * ring_buffer_reset_cpu - reset a ring buffer per CPU buffer
3654 * @buffer: The ring buffer to reset a per cpu buffer of
3655 * @cpu: The CPU buffer to be reset
3656 */
3657void ring_buffer_reset_cpu(struct ring_buffer *buffer, int cpu)
3658{
3659 struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
3660 unsigned long flags;
3661
Rusty Russell9e01c1b2009-01-01 10:12:22 +10303662 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04003663 return;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003664
Steven Rostedt41ede232009-05-01 20:26:54 -04003665 atomic_inc(&cpu_buffer->record_disabled);
3666
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02003667 raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003668
Steven Rostedt41b6a952009-09-02 09:59:48 -04003669 if (RB_WARN_ON(cpu_buffer, local_read(&cpu_buffer->committing)))
3670 goto out;
3671
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01003672 arch_spin_lock(&cpu_buffer->lock);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003673
3674 rb_reset_cpu(cpu_buffer);
3675
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01003676 arch_spin_unlock(&cpu_buffer->lock);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003677
Steven Rostedt41b6a952009-09-02 09:59:48 -04003678 out:
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02003679 raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
Steven Rostedt41ede232009-05-01 20:26:54 -04003680
3681 atomic_dec(&cpu_buffer->record_disabled);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003682}
Robert Richterc4f50182008-12-11 16:49:22 +01003683EXPORT_SYMBOL_GPL(ring_buffer_reset_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003684
3685/**
3686 * ring_buffer_reset - reset a ring buffer
3687 * @buffer: The ring buffer to reset all cpu buffers
3688 */
3689void ring_buffer_reset(struct ring_buffer *buffer)
3690{
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003691 int cpu;
3692
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003693 for_each_buffer_cpu(buffer, cpu)
Steven Rostedtd7690412008-10-01 00:29:53 -04003694 ring_buffer_reset_cpu(buffer, cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003695}
Robert Richterc4f50182008-12-11 16:49:22 +01003696EXPORT_SYMBOL_GPL(ring_buffer_reset);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003697
3698/**
3699 * rind_buffer_empty - is the ring buffer empty?
3700 * @buffer: The ring buffer to test
3701 */
3702int ring_buffer_empty(struct ring_buffer *buffer)
3703{
3704 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedtd4788202009-06-17 00:39:43 -04003705 unsigned long flags;
Steven Rostedt8d707e82009-06-16 21:22:48 -04003706 int dolock;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003707 int cpu;
Steven Rostedtd4788202009-06-17 00:39:43 -04003708 int ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003709
Steven Rostedt8d707e82009-06-16 21:22:48 -04003710 dolock = rb_ok_to_lock();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003711
3712 /* yes this is racy, but if you don't like the race, lock the buffer */
3713 for_each_buffer_cpu(buffer, cpu) {
3714 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt8d707e82009-06-16 21:22:48 -04003715 local_irq_save(flags);
3716 if (dolock)
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02003717 raw_spin_lock(&cpu_buffer->reader_lock);
Steven Rostedtd4788202009-06-17 00:39:43 -04003718 ret = rb_per_cpu_empty(cpu_buffer);
Steven Rostedt8d707e82009-06-16 21:22:48 -04003719 if (dolock)
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02003720 raw_spin_unlock(&cpu_buffer->reader_lock);
Steven Rostedt8d707e82009-06-16 21:22:48 -04003721 local_irq_restore(flags);
3722
Steven Rostedtd4788202009-06-17 00:39:43 -04003723 if (!ret)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003724 return 0;
3725 }
Steven Rostedt554f7862009-03-11 22:00:13 -04003726
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003727 return 1;
3728}
Robert Richterc4f50182008-12-11 16:49:22 +01003729EXPORT_SYMBOL_GPL(ring_buffer_empty);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003730
3731/**
3732 * ring_buffer_empty_cpu - is a cpu buffer of a ring buffer empty?
3733 * @buffer: The ring buffer
3734 * @cpu: The CPU buffer to test
3735 */
3736int ring_buffer_empty_cpu(struct ring_buffer *buffer, int cpu)
3737{
3738 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedtd4788202009-06-17 00:39:43 -04003739 unsigned long flags;
Steven Rostedt8d707e82009-06-16 21:22:48 -04003740 int dolock;
Steven Rostedt8aabee52009-03-12 13:13:49 -04003741 int ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003742
Rusty Russell9e01c1b2009-01-01 10:12:22 +10303743 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04003744 return 1;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003745
Steven Rostedt8d707e82009-06-16 21:22:48 -04003746 dolock = rb_ok_to_lock();
Steven Rostedt554f7862009-03-11 22:00:13 -04003747
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003748 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt8d707e82009-06-16 21:22:48 -04003749 local_irq_save(flags);
3750 if (dolock)
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02003751 raw_spin_lock(&cpu_buffer->reader_lock);
Steven Rostedt554f7862009-03-11 22:00:13 -04003752 ret = rb_per_cpu_empty(cpu_buffer);
Steven Rostedt8d707e82009-06-16 21:22:48 -04003753 if (dolock)
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02003754 raw_spin_unlock(&cpu_buffer->reader_lock);
Steven Rostedt8d707e82009-06-16 21:22:48 -04003755 local_irq_restore(flags);
Steven Rostedt554f7862009-03-11 22:00:13 -04003756
3757 return ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003758}
Robert Richterc4f50182008-12-11 16:49:22 +01003759EXPORT_SYMBOL_GPL(ring_buffer_empty_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003760
Steven Rostedt85bac322009-09-04 14:24:40 -04003761#ifdef CONFIG_RING_BUFFER_ALLOW_SWAP
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003762/**
3763 * ring_buffer_swap_cpu - swap a CPU buffer between two ring buffers
3764 * @buffer_a: One buffer to swap with
3765 * @buffer_b: The other buffer to swap with
3766 *
3767 * This function is useful for tracers that want to take a "snapshot"
3768 * of a CPU buffer and has another back up buffer lying around.
3769 * it is expected that the tracer handles the cpu buffer not being
3770 * used at the moment.
3771 */
3772int ring_buffer_swap_cpu(struct ring_buffer *buffer_a,
3773 struct ring_buffer *buffer_b, int cpu)
3774{
3775 struct ring_buffer_per_cpu *cpu_buffer_a;
3776 struct ring_buffer_per_cpu *cpu_buffer_b;
Steven Rostedt554f7862009-03-11 22:00:13 -04003777 int ret = -EINVAL;
3778
Rusty Russell9e01c1b2009-01-01 10:12:22 +10303779 if (!cpumask_test_cpu(cpu, buffer_a->cpumask) ||
3780 !cpumask_test_cpu(cpu, buffer_b->cpumask))
Steven Rostedt554f7862009-03-11 22:00:13 -04003781 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003782
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08003783 cpu_buffer_a = buffer_a->buffers[cpu];
3784 cpu_buffer_b = buffer_b->buffers[cpu];
3785
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003786 /* At least make sure the two buffers are somewhat the same */
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08003787 if (cpu_buffer_a->nr_pages != cpu_buffer_b->nr_pages)
Steven Rostedt554f7862009-03-11 22:00:13 -04003788 goto out;
3789
3790 ret = -EAGAIN;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003791
Steven Rostedt97b17ef2009-01-21 15:24:56 -05003792 if (ring_buffer_flags != RB_BUFFERS_ON)
Steven Rostedt554f7862009-03-11 22:00:13 -04003793 goto out;
Steven Rostedt97b17ef2009-01-21 15:24:56 -05003794
3795 if (atomic_read(&buffer_a->record_disabled))
Steven Rostedt554f7862009-03-11 22:00:13 -04003796 goto out;
Steven Rostedt97b17ef2009-01-21 15:24:56 -05003797
3798 if (atomic_read(&buffer_b->record_disabled))
Steven Rostedt554f7862009-03-11 22:00:13 -04003799 goto out;
Steven Rostedt97b17ef2009-01-21 15:24:56 -05003800
Steven Rostedt97b17ef2009-01-21 15:24:56 -05003801 if (atomic_read(&cpu_buffer_a->record_disabled))
Steven Rostedt554f7862009-03-11 22:00:13 -04003802 goto out;
Steven Rostedt97b17ef2009-01-21 15:24:56 -05003803
3804 if (atomic_read(&cpu_buffer_b->record_disabled))
Steven Rostedt554f7862009-03-11 22:00:13 -04003805 goto out;
Steven Rostedt97b17ef2009-01-21 15:24:56 -05003806
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003807 /*
3808 * We can't do a synchronize_sched here because this
3809 * function can be called in atomic context.
3810 * Normally this will be called from the same CPU as cpu.
3811 * If not it's up to the caller to protect this.
3812 */
3813 atomic_inc(&cpu_buffer_a->record_disabled);
3814 atomic_inc(&cpu_buffer_b->record_disabled);
3815
Steven Rostedt98277992009-09-02 10:56:15 -04003816 ret = -EBUSY;
3817 if (local_read(&cpu_buffer_a->committing))
3818 goto out_dec;
3819 if (local_read(&cpu_buffer_b->committing))
3820 goto out_dec;
3821
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003822 buffer_a->buffers[cpu] = cpu_buffer_b;
3823 buffer_b->buffers[cpu] = cpu_buffer_a;
3824
3825 cpu_buffer_b->buffer = buffer_a;
3826 cpu_buffer_a->buffer = buffer_b;
3827
Steven Rostedt98277992009-09-02 10:56:15 -04003828 ret = 0;
3829
3830out_dec:
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003831 atomic_dec(&cpu_buffer_a->record_disabled);
3832 atomic_dec(&cpu_buffer_b->record_disabled);
Steven Rostedt554f7862009-03-11 22:00:13 -04003833out:
Steven Rostedt554f7862009-03-11 22:00:13 -04003834 return ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003835}
Robert Richterc4f50182008-12-11 16:49:22 +01003836EXPORT_SYMBOL_GPL(ring_buffer_swap_cpu);
Steven Rostedt85bac322009-09-04 14:24:40 -04003837#endif /* CONFIG_RING_BUFFER_ALLOW_SWAP */
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003838
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003839/**
3840 * ring_buffer_alloc_read_page - allocate a page to read from buffer
3841 * @buffer: the buffer to allocate for.
3842 *
3843 * This function is used in conjunction with ring_buffer_read_page.
3844 * When reading a full page from the ring buffer, these functions
3845 * can be used to speed up the process. The calling function should
3846 * allocate a few pages first with this function. Then when it
3847 * needs to get pages from the ring buffer, it passes the result
3848 * of this function into ring_buffer_read_page, which will swap
3849 * the page that was allocated, with the read page of the buffer.
3850 *
3851 * Returns:
3852 * The page allocated, or NULL on error.
3853 */
Vaibhav Nagarnaik7ea59062011-05-03 17:56:42 -07003854void *ring_buffer_alloc_read_page(struct ring_buffer *buffer, int cpu)
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003855{
Steven Rostedt044fa782008-12-02 23:50:03 -05003856 struct buffer_data_page *bpage;
Vaibhav Nagarnaik7ea59062011-05-03 17:56:42 -07003857 struct page *page;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003858
Vaibhav Nagarnaikd7ec4bf2011-06-07 17:01:42 -07003859 page = alloc_pages_node(cpu_to_node(cpu),
3860 GFP_KERNEL | __GFP_NORETRY, 0);
Vaibhav Nagarnaik7ea59062011-05-03 17:56:42 -07003861 if (!page)
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003862 return NULL;
3863
Vaibhav Nagarnaik7ea59062011-05-03 17:56:42 -07003864 bpage = page_address(page);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003865
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003866 rb_init_page(bpage);
3867
Steven Rostedt044fa782008-12-02 23:50:03 -05003868 return bpage;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003869}
Steven Rostedtd6ce96d2009-05-05 01:15:24 -04003870EXPORT_SYMBOL_GPL(ring_buffer_alloc_read_page);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003871
3872/**
3873 * ring_buffer_free_read_page - free an allocated read page
3874 * @buffer: the buffer the page was allocate for
3875 * @data: the page to free
3876 *
3877 * Free a page allocated from ring_buffer_alloc_read_page.
3878 */
3879void ring_buffer_free_read_page(struct ring_buffer *buffer, void *data)
3880{
3881 free_page((unsigned long)data);
3882}
Steven Rostedtd6ce96d2009-05-05 01:15:24 -04003883EXPORT_SYMBOL_GPL(ring_buffer_free_read_page);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003884
3885/**
3886 * ring_buffer_read_page - extract a page from the ring buffer
3887 * @buffer: buffer to extract from
3888 * @data_page: the page to use allocated from ring_buffer_alloc_read_page
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003889 * @len: amount to extract
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003890 * @cpu: the cpu of the buffer to extract
3891 * @full: should the extraction only happen when the page is full.
3892 *
3893 * This function will pull out a page from the ring buffer and consume it.
3894 * @data_page must be the address of the variable that was returned
3895 * from ring_buffer_alloc_read_page. This is because the page might be used
3896 * to swap with a page in the ring buffer.
3897 *
3898 * for example:
Lai Jiangshanb85fa012009-02-09 14:21:14 +08003899 * rpage = ring_buffer_alloc_read_page(buffer);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003900 * if (!rpage)
3901 * return error;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003902 * ret = ring_buffer_read_page(buffer, &rpage, len, cpu, 0);
Lai Jiangshan667d2412009-02-09 14:21:17 +08003903 * if (ret >= 0)
3904 * process_page(rpage, ret);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003905 *
3906 * When @full is set, the function will not return true unless
3907 * the writer is off the reader page.
3908 *
3909 * Note: it is up to the calling functions to handle sleeps and wakeups.
3910 * The ring buffer can be used anywhere in the kernel and can not
3911 * blindly call wake_up. The layer that uses the ring buffer must be
3912 * responsible for that.
3913 *
3914 * Returns:
Lai Jiangshan667d2412009-02-09 14:21:17 +08003915 * >=0 if data has been transferred, returns the offset of consumed data.
3916 * <0 if no data has been transferred.
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003917 */
3918int ring_buffer_read_page(struct ring_buffer *buffer,
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003919 void **data_page, size_t len, int cpu, int full)
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003920{
3921 struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
3922 struct ring_buffer_event *event;
Steven Rostedt044fa782008-12-02 23:50:03 -05003923 struct buffer_data_page *bpage;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003924 struct buffer_page *reader;
Steven Rostedtff0ff842010-03-31 22:11:42 -04003925 unsigned long missed_events;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003926 unsigned long flags;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003927 unsigned int commit;
Lai Jiangshan667d2412009-02-09 14:21:17 +08003928 unsigned int read;
Steven Rostedt4f3640f2009-03-03 23:52:42 -05003929 u64 save_timestamp;
Lai Jiangshan667d2412009-02-09 14:21:17 +08003930 int ret = -1;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003931
Steven Rostedt554f7862009-03-11 22:00:13 -04003932 if (!cpumask_test_cpu(cpu, buffer->cpumask))
3933 goto out;
3934
Steven Rostedt474d32b2009-03-03 19:51:40 -05003935 /*
3936 * If len is not big enough to hold the page header, then
3937 * we can not copy anything.
3938 */
3939 if (len <= BUF_PAGE_HDR_SIZE)
Steven Rostedt554f7862009-03-11 22:00:13 -04003940 goto out;
Steven Rostedt474d32b2009-03-03 19:51:40 -05003941
3942 len -= BUF_PAGE_HDR_SIZE;
3943
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003944 if (!data_page)
Steven Rostedt554f7862009-03-11 22:00:13 -04003945 goto out;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003946
Steven Rostedt044fa782008-12-02 23:50:03 -05003947 bpage = *data_page;
3948 if (!bpage)
Steven Rostedt554f7862009-03-11 22:00:13 -04003949 goto out;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003950
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02003951 raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003952
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003953 reader = rb_get_reader_page(cpu_buffer);
3954 if (!reader)
Steven Rostedt554f7862009-03-11 22:00:13 -04003955 goto out_unlock;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003956
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003957 event = rb_reader_event(cpu_buffer);
Lai Jiangshan667d2412009-02-09 14:21:17 +08003958
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003959 read = reader->read;
3960 commit = rb_page_commit(reader);
3961
Steven Rostedt66a8cb92010-03-31 13:21:56 -04003962 /* Check if any events were dropped */
Steven Rostedtff0ff842010-03-31 22:11:42 -04003963 missed_events = cpu_buffer->lost_events;
Steven Rostedt66a8cb92010-03-31 13:21:56 -04003964
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003965 /*
Steven Rostedt474d32b2009-03-03 19:51:40 -05003966 * If this page has been partially read or
3967 * if len is not big enough to read the rest of the page or
3968 * a writer is still on the page, then
3969 * we must copy the data from the page to the buffer.
3970 * Otherwise, we can simply swap the page with the one passed in.
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003971 */
Steven Rostedt474d32b2009-03-03 19:51:40 -05003972 if (read || (len < (commit - read)) ||
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003973 cpu_buffer->reader_page == cpu_buffer->commit_page) {
Lai Jiangshan667d2412009-02-09 14:21:17 +08003974 struct buffer_data_page *rpage = cpu_buffer->reader_page->page;
Steven Rostedt474d32b2009-03-03 19:51:40 -05003975 unsigned int rpos = read;
3976 unsigned int pos = 0;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003977 unsigned int size;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003978
3979 if (full)
Steven Rostedt554f7862009-03-11 22:00:13 -04003980 goto out_unlock;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003981
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003982 if (len > (commit - read))
3983 len = (commit - read);
3984
Steven Rostedt69d1b832010-10-07 18:18:05 -04003985 /* Always keep the time extend and data together */
3986 size = rb_event_ts_length(event);
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003987
3988 if (len < size)
Steven Rostedt554f7862009-03-11 22:00:13 -04003989 goto out_unlock;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003990
Steven Rostedt4f3640f2009-03-03 23:52:42 -05003991 /* save the current timestamp, since the user will need it */
3992 save_timestamp = cpu_buffer->read_stamp;
3993
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003994 /* Need to copy one event at a time */
3995 do {
David Sharpe1e35922010-12-22 16:38:24 -08003996 /* We need the size of one event, because
3997 * rb_advance_reader only advances by one event,
3998 * whereas rb_event_ts_length may include the size of
3999 * one or two events.
4000 * We have already ensured there's enough space if this
4001 * is a time extend. */
4002 size = rb_event_length(event);
Steven Rostedt474d32b2009-03-03 19:51:40 -05004003 memcpy(bpage->data + pos, rpage->data + rpos, size);
Steven Rostedtef7a4a12009-03-03 00:27:49 -05004004
4005 len -= size;
4006
4007 rb_advance_reader(cpu_buffer);
Steven Rostedt474d32b2009-03-03 19:51:40 -05004008 rpos = reader->read;
4009 pos += size;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05004010
Huang Ying18fab912010-07-28 14:14:01 +08004011 if (rpos >= commit)
4012 break;
4013
Steven Rostedtef7a4a12009-03-03 00:27:49 -05004014 event = rb_reader_event(cpu_buffer);
Steven Rostedt69d1b832010-10-07 18:18:05 -04004015 /* Always keep the time extend and data together */
4016 size = rb_event_ts_length(event);
David Sharpe1e35922010-12-22 16:38:24 -08004017 } while (len >= size);
Lai Jiangshan667d2412009-02-09 14:21:17 +08004018
4019 /* update bpage */
Steven Rostedtef7a4a12009-03-03 00:27:49 -05004020 local_set(&bpage->commit, pos);
Steven Rostedt4f3640f2009-03-03 23:52:42 -05004021 bpage->time_stamp = save_timestamp;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05004022
Steven Rostedt474d32b2009-03-03 19:51:40 -05004023 /* we copied everything to the beginning */
4024 read = 0;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004025 } else {
Steven Rostedtafbab762009-05-01 19:40:05 -04004026 /* update the entry counter */
Steven Rostedt77ae3652009-03-27 11:00:29 -04004027 cpu_buffer->read += rb_page_entries(reader);
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -07004028 cpu_buffer->read_bytes += BUF_PAGE_SIZE;
Steven Rostedtafbab762009-05-01 19:40:05 -04004029
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004030 /* swap the pages */
Steven Rostedt044fa782008-12-02 23:50:03 -05004031 rb_init_page(bpage);
Steven Rostedtef7a4a12009-03-03 00:27:49 -05004032 bpage = reader->page;
4033 reader->page = *data_page;
4034 local_set(&reader->write, 0);
Steven Rostedt778c55d2009-05-01 18:44:45 -04004035 local_set(&reader->entries, 0);
Steven Rostedtef7a4a12009-03-03 00:27:49 -05004036 reader->read = 0;
Steven Rostedt044fa782008-12-02 23:50:03 -05004037 *data_page = bpage;
Steven Rostedtff0ff842010-03-31 22:11:42 -04004038
4039 /*
4040 * Use the real_end for the data size,
4041 * This gives us a chance to store the lost events
4042 * on the page.
4043 */
4044 if (reader->real_end)
4045 local_set(&bpage->commit, reader->real_end);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004046 }
Lai Jiangshan667d2412009-02-09 14:21:17 +08004047 ret = read;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004048
Steven Rostedt66a8cb92010-03-31 13:21:56 -04004049 cpu_buffer->lost_events = 0;
Steven Rostedt2711ca22010-05-21 13:32:26 -04004050
4051 commit = local_read(&bpage->commit);
Steven Rostedt66a8cb92010-03-31 13:21:56 -04004052 /*
4053 * Set a flag in the commit field if we lost events
4054 */
Steven Rostedtff0ff842010-03-31 22:11:42 -04004055 if (missed_events) {
Steven Rostedtff0ff842010-03-31 22:11:42 -04004056 /* If there is room at the end of the page to save the
4057 * missed events, then record it there.
4058 */
4059 if (BUF_PAGE_SIZE - commit >= sizeof(missed_events)) {
4060 memcpy(&bpage->data[commit], &missed_events,
4061 sizeof(missed_events));
4062 local_add(RB_MISSED_STORED, &bpage->commit);
Steven Rostedt2711ca22010-05-21 13:32:26 -04004063 commit += sizeof(missed_events);
Steven Rostedtff0ff842010-03-31 22:11:42 -04004064 }
Steven Rostedt66a8cb92010-03-31 13:21:56 -04004065 local_add(RB_MISSED_EVENTS, &bpage->commit);
Steven Rostedtff0ff842010-03-31 22:11:42 -04004066 }
Steven Rostedt66a8cb92010-03-31 13:21:56 -04004067
Steven Rostedt2711ca22010-05-21 13:32:26 -04004068 /*
4069 * This page may be off to user land. Zero it out here.
4070 */
4071 if (commit < BUF_PAGE_SIZE)
4072 memset(&bpage->data[commit], 0, BUF_PAGE_SIZE - commit);
4073
Steven Rostedt554f7862009-03-11 22:00:13 -04004074 out_unlock:
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02004075 raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004076
Steven Rostedt554f7862009-03-11 22:00:13 -04004077 out:
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004078 return ret;
4079}
Steven Rostedtd6ce96d2009-05-05 01:15:24 -04004080EXPORT_SYMBOL_GPL(ring_buffer_read_page);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004081
Steven Rostedt59222ef2009-03-12 11:46:03 -04004082#ifdef CONFIG_HOTPLUG_CPU
Frederic Weisbecker09c9e842009-03-21 04:33:36 +01004083static int rb_cpu_notify(struct notifier_block *self,
4084 unsigned long action, void *hcpu)
Steven Rostedt554f7862009-03-11 22:00:13 -04004085{
4086 struct ring_buffer *buffer =
4087 container_of(self, struct ring_buffer, cpu_notify);
4088 long cpu = (long)hcpu;
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08004089 int cpu_i, nr_pages_same;
4090 unsigned int nr_pages;
Steven Rostedt554f7862009-03-11 22:00:13 -04004091
4092 switch (action) {
4093 case CPU_UP_PREPARE:
4094 case CPU_UP_PREPARE_FROZEN:
Rusty Russell3f237a72009-06-12 21:15:30 +09304095 if (cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt554f7862009-03-11 22:00:13 -04004096 return NOTIFY_OK;
4097
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08004098 nr_pages = 0;
4099 nr_pages_same = 1;
4100 /* check if all cpu sizes are same */
4101 for_each_buffer_cpu(buffer, cpu_i) {
4102 /* fill in the size from first enabled cpu */
4103 if (nr_pages == 0)
4104 nr_pages = buffer->buffers[cpu_i]->nr_pages;
4105 if (nr_pages != buffer->buffers[cpu_i]->nr_pages) {
4106 nr_pages_same = 0;
4107 break;
4108 }
4109 }
4110 /* allocate minimum pages, user can later expand it */
4111 if (!nr_pages_same)
4112 nr_pages = 2;
Steven Rostedt554f7862009-03-11 22:00:13 -04004113 buffer->buffers[cpu] =
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08004114 rb_allocate_cpu_buffer(buffer, nr_pages, cpu);
Steven Rostedt554f7862009-03-11 22:00:13 -04004115 if (!buffer->buffers[cpu]) {
4116 WARN(1, "failed to allocate ring buffer on CPU %ld\n",
4117 cpu);
4118 return NOTIFY_OK;
4119 }
4120 smp_wmb();
Rusty Russell3f237a72009-06-12 21:15:30 +09304121 cpumask_set_cpu(cpu, buffer->cpumask);
Steven Rostedt554f7862009-03-11 22:00:13 -04004122 break;
4123 case CPU_DOWN_PREPARE:
4124 case CPU_DOWN_PREPARE_FROZEN:
4125 /*
4126 * Do nothing.
4127 * If we were to free the buffer, then the user would
4128 * lose any trace that was in the buffer.
4129 */
4130 break;
4131 default:
4132 break;
4133 }
4134 return NOTIFY_OK;
4135}
4136#endif