blob: 6cbd36f422e63ded4cda9dd687d0ccd1063cfd8c [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;
Steven Rostedt3adc54f2009-03-30 15:32:01 -0400452 struct list_head *pages;
Steven Rostedt6f807ac2008-10-04 02:00:58 -0400453 struct buffer_page *head_page; /* read from head */
454 struct buffer_page *tail_page; /* write to tail */
Wenji Huangc3706f02009-02-10 01:03:18 -0500455 struct buffer_page *commit_page; /* committed pages */
Steven Rostedtd7690412008-10-01 00:29:53 -0400456 struct buffer_page *reader_page;
Steven Rostedt66a8cb92010-03-31 13:21:56 -0400457 unsigned long lost_events;
458 unsigned long last_overrun;
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -0700459 local_t entries_bytes;
Steven Rostedt77ae3652009-03-27 11:00:29 -0400460 local_t commit_overrun;
461 local_t overrun;
Steven Rostedte4906ef2009-04-30 20:49:44 -0400462 local_t entries;
Steven Rostedtfa743952009-06-16 12:37:57 -0400463 local_t committing;
464 local_t commits;
Steven Rostedt77ae3652009-03-27 11:00:29 -0400465 unsigned long read;
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -0700466 unsigned long read_bytes;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400467 u64 write_stamp;
468 u64 read_stamp;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400469};
470
471struct ring_buffer {
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400472 unsigned pages;
473 unsigned flags;
474 int cpus;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400475 atomic_t record_disabled;
Arnaldo Carvalho de Melo00f62f62009-02-09 17:04:06 -0200476 cpumask_var_t cpumask;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400477
Peter Zijlstra1f8a6a12009-06-08 18:18:39 +0200478 struct lock_class_key *reader_lock_key;
479
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400480 struct mutex mutex;
481
482 struct ring_buffer_per_cpu **buffers;
Steven Rostedt554f7862009-03-11 22:00:13 -0400483
Steven Rostedt59222ef2009-03-12 11:46:03 -0400484#ifdef CONFIG_HOTPLUG_CPU
Steven Rostedt554f7862009-03-11 22:00:13 -0400485 struct notifier_block cpu_notify;
486#endif
Steven Rostedt37886f62009-03-17 17:22:06 -0400487 u64 (*clock)(void);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400488};
489
490struct ring_buffer_iter {
491 struct ring_buffer_per_cpu *cpu_buffer;
492 unsigned long head;
493 struct buffer_page *head_page;
Steven Rostedt492a74f2010-01-25 15:17:47 -0500494 struct buffer_page *cache_reader_page;
495 unsigned long cache_read;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400496 u64 read_stamp;
497};
498
Steven Rostedtf536aaf2008-11-10 23:07:30 -0500499/* buffer may be either ring_buffer or ring_buffer_per_cpu */
Steven Rostedt077c5402009-09-03 19:53:46 -0400500#define RB_WARN_ON(b, cond) \
501 ({ \
502 int _____ret = unlikely(cond); \
503 if (_____ret) { \
504 if (__same_type(*(b), struct ring_buffer_per_cpu)) { \
505 struct ring_buffer_per_cpu *__b = \
506 (void *)b; \
507 atomic_inc(&__b->buffer->record_disabled); \
508 } else \
509 atomic_inc(&b->record_disabled); \
510 WARN_ON(1); \
511 } \
512 _____ret; \
Steven Rostedt3e89c7b2008-11-11 15:28:41 -0500513 })
Steven Rostedtf536aaf2008-11-10 23:07:30 -0500514
Steven Rostedt37886f62009-03-17 17:22:06 -0400515/* Up this if you want to test the TIME_EXTENTS and normalization */
516#define DEBUG_SHIFT 0
517
Jiri Olsa6d3f1e12009-10-23 19:36:19 -0400518static inline u64 rb_time_stamp(struct ring_buffer *buffer)
Steven Rostedt88eb0122009-05-11 16:28:23 -0400519{
520 /* shift to debug/test normalization and TIME_EXTENTS */
521 return buffer->clock() << DEBUG_SHIFT;
522}
523
Steven Rostedt37886f62009-03-17 17:22:06 -0400524u64 ring_buffer_time_stamp(struct ring_buffer *buffer, int cpu)
525{
526 u64 time;
527
528 preempt_disable_notrace();
Jiri Olsa6d3f1e12009-10-23 19:36:19 -0400529 time = rb_time_stamp(buffer);
Steven Rostedt37886f62009-03-17 17:22:06 -0400530 preempt_enable_no_resched_notrace();
531
532 return time;
533}
534EXPORT_SYMBOL_GPL(ring_buffer_time_stamp);
535
536void ring_buffer_normalize_time_stamp(struct ring_buffer *buffer,
537 int cpu, u64 *ts)
538{
539 /* Just stupid testing the normalize function and deltas */
540 *ts >>= DEBUG_SHIFT;
541}
542EXPORT_SYMBOL_GPL(ring_buffer_normalize_time_stamp);
543
Steven Rostedt77ae3652009-03-27 11:00:29 -0400544/*
545 * Making the ring buffer lockless makes things tricky.
546 * Although writes only happen on the CPU that they are on,
547 * and they only need to worry about interrupts. Reads can
548 * happen on any CPU.
549 *
550 * The reader page is always off the ring buffer, but when the
551 * reader finishes with a page, it needs to swap its page with
552 * a new one from the buffer. The reader needs to take from
553 * the head (writes go to the tail). But if a writer is in overwrite
554 * mode and wraps, it must push the head page forward.
555 *
556 * Here lies the problem.
557 *
558 * The reader must be careful to replace only the head page, and
559 * not another one. As described at the top of the file in the
560 * ASCII art, the reader sets its old page to point to the next
561 * page after head. It then sets the page after head to point to
562 * the old reader page. But if the writer moves the head page
563 * during this operation, the reader could end up with the tail.
564 *
565 * We use cmpxchg to help prevent this race. We also do something
566 * special with the page before head. We set the LSB to 1.
567 *
568 * When the writer must push the page forward, it will clear the
569 * bit that points to the head page, move the head, and then set
570 * the bit that points to the new head page.
571 *
572 * We also don't want an interrupt coming in and moving the head
573 * page on another writer. Thus we use the second LSB to catch
574 * that too. Thus:
575 *
576 * head->list->prev->next bit 1 bit 0
577 * ------- -------
578 * Normal page 0 0
579 * Points to head page 0 1
580 * New head page 1 0
581 *
582 * Note we can not trust the prev pointer of the head page, because:
583 *
584 * +----+ +-----+ +-----+
585 * | |------>| T |---X--->| N |
586 * | |<------| | | |
587 * +----+ +-----+ +-----+
588 * ^ ^ |
589 * | +-----+ | |
590 * +----------| R |----------+ |
591 * | |<-----------+
592 * +-----+
593 *
594 * Key: ---X--> HEAD flag set in pointer
595 * T Tail page
596 * R Reader page
597 * N Next page
598 *
599 * (see __rb_reserve_next() to see where this happens)
600 *
601 * What the above shows is that the reader just swapped out
602 * the reader page with a page in the buffer, but before it
603 * could make the new header point back to the new page added
604 * it was preempted by a writer. The writer moved forward onto
605 * the new page added by the reader and is about to move forward
606 * again.
607 *
608 * You can see, it is legitimate for the previous pointer of
609 * the head (or any page) not to point back to itself. But only
610 * temporarially.
611 */
612
613#define RB_PAGE_NORMAL 0UL
614#define RB_PAGE_HEAD 1UL
615#define RB_PAGE_UPDATE 2UL
616
617
618#define RB_FLAG_MASK 3UL
619
620/* PAGE_MOVED is not part of the mask */
621#define RB_PAGE_MOVED 4UL
622
623/*
624 * rb_list_head - remove any bit
625 */
626static struct list_head *rb_list_head(struct list_head *list)
627{
628 unsigned long val = (unsigned long)list;
629
630 return (struct list_head *)(val & ~RB_FLAG_MASK);
631}
632
633/*
Jiri Olsa6d3f1e12009-10-23 19:36:19 -0400634 * rb_is_head_page - test if the given page is the head page
Steven Rostedt77ae3652009-03-27 11:00:29 -0400635 *
636 * Because the reader may move the head_page pointer, we can
637 * not trust what the head page is (it may be pointing to
638 * the reader page). But if the next page is a header page,
639 * its flags will be non zero.
640 */
Jesper Juhl42b16b32011-01-17 00:09:38 +0100641static inline int
Steven Rostedt77ae3652009-03-27 11:00:29 -0400642rb_is_head_page(struct ring_buffer_per_cpu *cpu_buffer,
643 struct buffer_page *page, struct list_head *list)
644{
645 unsigned long val;
646
647 val = (unsigned long)list->next;
648
649 if ((val & ~RB_FLAG_MASK) != (unsigned long)&page->list)
650 return RB_PAGE_MOVED;
651
652 return val & RB_FLAG_MASK;
653}
654
655/*
656 * rb_is_reader_page
657 *
658 * The unique thing about the reader page, is that, if the
659 * writer is ever on it, the previous pointer never points
660 * back to the reader page.
661 */
662static int rb_is_reader_page(struct buffer_page *page)
663{
664 struct list_head *list = page->list.prev;
665
666 return rb_list_head(list->next) != &page->list;
667}
668
669/*
670 * rb_set_list_to_head - set a list_head to be pointing to head.
671 */
672static void rb_set_list_to_head(struct ring_buffer_per_cpu *cpu_buffer,
673 struct list_head *list)
674{
675 unsigned long *ptr;
676
677 ptr = (unsigned long *)&list->next;
678 *ptr |= RB_PAGE_HEAD;
679 *ptr &= ~RB_PAGE_UPDATE;
680}
681
682/*
683 * rb_head_page_activate - sets up head page
684 */
685static void rb_head_page_activate(struct ring_buffer_per_cpu *cpu_buffer)
686{
687 struct buffer_page *head;
688
689 head = cpu_buffer->head_page;
690 if (!head)
691 return;
692
693 /*
694 * Set the previous list pointer to have the HEAD flag.
695 */
696 rb_set_list_to_head(cpu_buffer, head->list.prev);
697}
698
699static void rb_list_head_clear(struct list_head *list)
700{
701 unsigned long *ptr = (unsigned long *)&list->next;
702
703 *ptr &= ~RB_FLAG_MASK;
704}
705
706/*
707 * rb_head_page_dactivate - clears head page ptr (for free list)
708 */
709static void
710rb_head_page_deactivate(struct ring_buffer_per_cpu *cpu_buffer)
711{
712 struct list_head *hd;
713
714 /* Go through the whole list and clear any pointers found. */
715 rb_list_head_clear(cpu_buffer->pages);
716
717 list_for_each(hd, cpu_buffer->pages)
718 rb_list_head_clear(hd);
719}
720
721static int rb_head_page_set(struct ring_buffer_per_cpu *cpu_buffer,
722 struct buffer_page *head,
723 struct buffer_page *prev,
724 int old_flag, int new_flag)
725{
726 struct list_head *list;
727 unsigned long val = (unsigned long)&head->list;
728 unsigned long ret;
729
730 list = &prev->list;
731
732 val &= ~RB_FLAG_MASK;
733
Steven Rostedt08a40812009-09-14 09:31:35 -0400734 ret = cmpxchg((unsigned long *)&list->next,
735 val | old_flag, val | new_flag);
Steven Rostedt77ae3652009-03-27 11:00:29 -0400736
737 /* check if the reader took the page */
738 if ((ret & ~RB_FLAG_MASK) != val)
739 return RB_PAGE_MOVED;
740
741 return ret & RB_FLAG_MASK;
742}
743
744static int rb_head_page_set_update(struct ring_buffer_per_cpu *cpu_buffer,
745 struct buffer_page *head,
746 struct buffer_page *prev,
747 int old_flag)
748{
749 return rb_head_page_set(cpu_buffer, head, prev,
750 old_flag, RB_PAGE_UPDATE);
751}
752
753static int rb_head_page_set_head(struct ring_buffer_per_cpu *cpu_buffer,
754 struct buffer_page *head,
755 struct buffer_page *prev,
756 int old_flag)
757{
758 return rb_head_page_set(cpu_buffer, head, prev,
759 old_flag, RB_PAGE_HEAD);
760}
761
762static int rb_head_page_set_normal(struct ring_buffer_per_cpu *cpu_buffer,
763 struct buffer_page *head,
764 struct buffer_page *prev,
765 int old_flag)
766{
767 return rb_head_page_set(cpu_buffer, head, prev,
768 old_flag, RB_PAGE_NORMAL);
769}
770
771static inline void rb_inc_page(struct ring_buffer_per_cpu *cpu_buffer,
772 struct buffer_page **bpage)
773{
774 struct list_head *p = rb_list_head((*bpage)->list.next);
775
776 *bpage = list_entry(p, struct buffer_page, list);
777}
778
779static struct buffer_page *
780rb_set_head_page(struct ring_buffer_per_cpu *cpu_buffer)
781{
782 struct buffer_page *head;
783 struct buffer_page *page;
784 struct list_head *list;
785 int i;
786
787 if (RB_WARN_ON(cpu_buffer, !cpu_buffer->head_page))
788 return NULL;
789
790 /* sanity check */
791 list = cpu_buffer->pages;
792 if (RB_WARN_ON(cpu_buffer, rb_list_head(list->prev->next) != list))
793 return NULL;
794
795 page = head = cpu_buffer->head_page;
796 /*
797 * It is possible that the writer moves the header behind
798 * where we started, and we miss in one loop.
799 * A second loop should grab the header, but we'll do
800 * three loops just because I'm paranoid.
801 */
802 for (i = 0; i < 3; i++) {
803 do {
804 if (rb_is_head_page(cpu_buffer, page, page->list.prev)) {
805 cpu_buffer->head_page = page;
806 return page;
807 }
808 rb_inc_page(cpu_buffer, &page);
809 } while (page != head);
810 }
811
812 RB_WARN_ON(cpu_buffer, 1);
813
814 return NULL;
815}
816
817static int rb_head_page_replace(struct buffer_page *old,
818 struct buffer_page *new)
819{
820 unsigned long *ptr = (unsigned long *)&old->list.prev->next;
821 unsigned long val;
822 unsigned long ret;
823
824 val = *ptr & ~RB_FLAG_MASK;
825 val |= RB_PAGE_HEAD;
826
Steven Rostedt08a40812009-09-14 09:31:35 -0400827 ret = cmpxchg(ptr, val, (unsigned long)&new->list);
Steven Rostedt77ae3652009-03-27 11:00:29 -0400828
829 return ret == val;
830}
831
832/*
833 * rb_tail_page_update - move the tail page forward
834 *
835 * Returns 1 if moved tail page, 0 if someone else did.
836 */
837static int rb_tail_page_update(struct ring_buffer_per_cpu *cpu_buffer,
838 struct buffer_page *tail_page,
839 struct buffer_page *next_page)
840{
841 struct buffer_page *old_tail;
842 unsigned long old_entries;
843 unsigned long old_write;
844 int ret = 0;
845
846 /*
847 * The tail page now needs to be moved forward.
848 *
849 * We need to reset the tail page, but without messing
850 * with possible erasing of data brought in by interrupts
851 * that have moved the tail page and are currently on it.
852 *
853 * We add a counter to the write field to denote this.
854 */
855 old_write = local_add_return(RB_WRITE_INTCNT, &next_page->write);
856 old_entries = local_add_return(RB_WRITE_INTCNT, &next_page->entries);
857
858 /*
859 * Just make sure we have seen our old_write and synchronize
860 * with any interrupts that come in.
861 */
862 barrier();
863
864 /*
865 * If the tail page is still the same as what we think
866 * it is, then it is up to us to update the tail
867 * pointer.
868 */
869 if (tail_page == cpu_buffer->tail_page) {
870 /* Zero the write counter */
871 unsigned long val = old_write & ~RB_WRITE_MASK;
872 unsigned long eval = old_entries & ~RB_WRITE_MASK;
873
874 /*
875 * This will only succeed if an interrupt did
876 * not come in and change it. In which case, we
877 * do not want to modify it.
Lai Jiangshanda706d82009-07-15 16:27:30 +0800878 *
879 * We add (void) to let the compiler know that we do not care
880 * about the return value of these functions. We use the
881 * cmpxchg to only update if an interrupt did not already
882 * do it for us. If the cmpxchg fails, we don't care.
Steven Rostedt77ae3652009-03-27 11:00:29 -0400883 */
Lai Jiangshanda706d82009-07-15 16:27:30 +0800884 (void)local_cmpxchg(&next_page->write, old_write, val);
885 (void)local_cmpxchg(&next_page->entries, old_entries, eval);
Steven Rostedt77ae3652009-03-27 11:00:29 -0400886
887 /*
888 * No need to worry about races with clearing out the commit.
889 * it only can increment when a commit takes place. But that
890 * only happens in the outer most nested commit.
891 */
892 local_set(&next_page->page->commit, 0);
893
894 old_tail = cmpxchg(&cpu_buffer->tail_page,
895 tail_page, next_page);
896
897 if (old_tail == tail_page)
898 ret = 1;
899 }
900
901 return ret;
902}
903
904static int rb_check_bpage(struct ring_buffer_per_cpu *cpu_buffer,
905 struct buffer_page *bpage)
906{
907 unsigned long val = (unsigned long)bpage;
908
909 if (RB_WARN_ON(cpu_buffer, val & RB_FLAG_MASK))
910 return 1;
911
912 return 0;
913}
914
915/**
916 * rb_check_list - make sure a pointer to a list has the last bits zero
917 */
918static int rb_check_list(struct ring_buffer_per_cpu *cpu_buffer,
919 struct list_head *list)
920{
921 if (RB_WARN_ON(cpu_buffer, rb_list_head(list->prev) != list->prev))
922 return 1;
923 if (RB_WARN_ON(cpu_buffer, rb_list_head(list->next) != list->next))
924 return 1;
925 return 0;
926}
927
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400928/**
929 * check_pages - integrity check of buffer pages
930 * @cpu_buffer: CPU buffer with pages to test
931 *
Wenji Huangc3706f02009-02-10 01:03:18 -0500932 * As a safety measure we check to make sure the data pages have not
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400933 * been corrupted.
934 */
935static int rb_check_pages(struct ring_buffer_per_cpu *cpu_buffer)
936{
Steven Rostedt3adc54f2009-03-30 15:32:01 -0400937 struct list_head *head = cpu_buffer->pages;
Steven Rostedt044fa782008-12-02 23:50:03 -0500938 struct buffer_page *bpage, *tmp;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400939
Steven Rostedt77ae3652009-03-27 11:00:29 -0400940 rb_head_page_deactivate(cpu_buffer);
941
Steven Rostedt3e89c7b2008-11-11 15:28:41 -0500942 if (RB_WARN_ON(cpu_buffer, head->next->prev != head))
943 return -1;
944 if (RB_WARN_ON(cpu_buffer, head->prev->next != head))
945 return -1;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400946
Steven Rostedt77ae3652009-03-27 11:00:29 -0400947 if (rb_check_list(cpu_buffer, head))
948 return -1;
949
Steven Rostedt044fa782008-12-02 23:50:03 -0500950 list_for_each_entry_safe(bpage, tmp, head, list) {
Steven Rostedt3e89c7b2008-11-11 15:28:41 -0500951 if (RB_WARN_ON(cpu_buffer,
Steven Rostedt044fa782008-12-02 23:50:03 -0500952 bpage->list.next->prev != &bpage->list))
Steven Rostedt3e89c7b2008-11-11 15:28:41 -0500953 return -1;
954 if (RB_WARN_ON(cpu_buffer,
Steven Rostedt044fa782008-12-02 23:50:03 -0500955 bpage->list.prev->next != &bpage->list))
Steven Rostedt3e89c7b2008-11-11 15:28:41 -0500956 return -1;
Steven Rostedt77ae3652009-03-27 11:00:29 -0400957 if (rb_check_list(cpu_buffer, &bpage->list))
958 return -1;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400959 }
960
Steven Rostedt77ae3652009-03-27 11:00:29 -0400961 rb_head_page_activate(cpu_buffer);
962
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400963 return 0;
964}
965
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400966static int rb_allocate_pages(struct ring_buffer_per_cpu *cpu_buffer,
967 unsigned nr_pages)
968{
Steven Rostedt044fa782008-12-02 23:50:03 -0500969 struct buffer_page *bpage, *tmp;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400970 LIST_HEAD(pages);
971 unsigned i;
972
Steven Rostedt3adc54f2009-03-30 15:32:01 -0400973 WARN_ON(!nr_pages);
974
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400975 for (i = 0; i < nr_pages; i++) {
Vaibhav Nagarnaik7ea59062011-05-03 17:56:42 -0700976 struct page *page;
Vaibhav Nagarnaikd7ec4bf2011-06-07 17:01:42 -0700977 /*
978 * __GFP_NORETRY flag makes sure that the allocation fails
979 * gracefully without invoking oom-killer and the system is
980 * not destabilized.
981 */
Steven Rostedt044fa782008-12-02 23:50:03 -0500982 bpage = kzalloc_node(ALIGN(sizeof(*bpage), cache_line_size()),
Vaibhav Nagarnaikd7ec4bf2011-06-07 17:01:42 -0700983 GFP_KERNEL | __GFP_NORETRY,
984 cpu_to_node(cpu_buffer->cpu));
Steven Rostedt044fa782008-12-02 23:50:03 -0500985 if (!bpage)
Steven Rostedte4c2ce82008-10-01 11:14:54 -0400986 goto free_pages;
Steven Rostedt77ae3652009-03-27 11:00:29 -0400987
988 rb_check_bpage(cpu_buffer, bpage);
989
Steven Rostedt044fa782008-12-02 23:50:03 -0500990 list_add(&bpage->list, &pages);
Steven Rostedte4c2ce82008-10-01 11:14:54 -0400991
Vaibhav Nagarnaik7ea59062011-05-03 17:56:42 -0700992 page = alloc_pages_node(cpu_to_node(cpu_buffer->cpu),
Vaibhav Nagarnaikd7ec4bf2011-06-07 17:01:42 -0700993 GFP_KERNEL | __GFP_NORETRY, 0);
Vaibhav Nagarnaik7ea59062011-05-03 17:56:42 -0700994 if (!page)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400995 goto free_pages;
Vaibhav Nagarnaik7ea59062011-05-03 17:56:42 -0700996 bpage->page = page_address(page);
Steven Rostedt044fa782008-12-02 23:50:03 -0500997 rb_init_page(bpage->page);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400998 }
999
Steven Rostedt3adc54f2009-03-30 15:32:01 -04001000 /*
1001 * The ring buffer page list is a circular list that does not
1002 * start and end with a list head. All page list items point to
1003 * other pages.
1004 */
1005 cpu_buffer->pages = pages.next;
1006 list_del(&pages);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001007
1008 rb_check_pages(cpu_buffer);
1009
1010 return 0;
1011
1012 free_pages:
Steven Rostedt044fa782008-12-02 23:50:03 -05001013 list_for_each_entry_safe(bpage, tmp, &pages, list) {
1014 list_del_init(&bpage->list);
1015 free_buffer_page(bpage);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001016 }
1017 return -ENOMEM;
1018}
1019
1020static struct ring_buffer_per_cpu *
1021rb_allocate_cpu_buffer(struct ring_buffer *buffer, int cpu)
1022{
1023 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedt044fa782008-12-02 23:50:03 -05001024 struct buffer_page *bpage;
Vaibhav Nagarnaik7ea59062011-05-03 17:56:42 -07001025 struct page *page;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001026 int ret;
1027
1028 cpu_buffer = kzalloc_node(ALIGN(sizeof(*cpu_buffer), cache_line_size()),
1029 GFP_KERNEL, cpu_to_node(cpu));
1030 if (!cpu_buffer)
1031 return NULL;
1032
1033 cpu_buffer->cpu = cpu;
1034 cpu_buffer->buffer = buffer;
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02001035 raw_spin_lock_init(&cpu_buffer->reader_lock);
Peter Zijlstra1f8a6a12009-06-08 18:18:39 +02001036 lockdep_set_class(&cpu_buffer->reader_lock, buffer->reader_lock_key);
Thomas Gleixneredc35bd2009-12-03 12:38:57 +01001037 cpu_buffer->lock = (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001038
Steven Rostedt044fa782008-12-02 23:50:03 -05001039 bpage = kzalloc_node(ALIGN(sizeof(*bpage), cache_line_size()),
Steven Rostedte4c2ce82008-10-01 11:14:54 -04001040 GFP_KERNEL, cpu_to_node(cpu));
Steven Rostedt044fa782008-12-02 23:50:03 -05001041 if (!bpage)
Steven Rostedte4c2ce82008-10-01 11:14:54 -04001042 goto fail_free_buffer;
1043
Steven Rostedt77ae3652009-03-27 11:00:29 -04001044 rb_check_bpage(cpu_buffer, bpage);
1045
Steven Rostedt044fa782008-12-02 23:50:03 -05001046 cpu_buffer->reader_page = bpage;
Vaibhav Nagarnaik7ea59062011-05-03 17:56:42 -07001047 page = alloc_pages_node(cpu_to_node(cpu), GFP_KERNEL, 0);
1048 if (!page)
Steven Rostedte4c2ce82008-10-01 11:14:54 -04001049 goto fail_free_reader;
Vaibhav Nagarnaik7ea59062011-05-03 17:56:42 -07001050 bpage->page = page_address(page);
Steven Rostedt044fa782008-12-02 23:50:03 -05001051 rb_init_page(bpage->page);
Steven Rostedte4c2ce82008-10-01 11:14:54 -04001052
Steven Rostedtd7690412008-10-01 00:29:53 -04001053 INIT_LIST_HEAD(&cpu_buffer->reader_page->list);
Steven Rostedtd7690412008-10-01 00:29:53 -04001054
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001055 ret = rb_allocate_pages(cpu_buffer, buffer->pages);
1056 if (ret < 0)
Steven Rostedtd7690412008-10-01 00:29:53 -04001057 goto fail_free_reader;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001058
1059 cpu_buffer->head_page
Steven Rostedt3adc54f2009-03-30 15:32:01 -04001060 = list_entry(cpu_buffer->pages, struct buffer_page, list);
Steven Rostedtbf41a152008-10-04 02:00:59 -04001061 cpu_buffer->tail_page = cpu_buffer->commit_page = cpu_buffer->head_page;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001062
Steven Rostedt77ae3652009-03-27 11:00:29 -04001063 rb_head_page_activate(cpu_buffer);
1064
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001065 return cpu_buffer;
1066
Steven Rostedtd7690412008-10-01 00:29:53 -04001067 fail_free_reader:
1068 free_buffer_page(cpu_buffer->reader_page);
1069
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001070 fail_free_buffer:
1071 kfree(cpu_buffer);
1072 return NULL;
1073}
1074
1075static void rb_free_cpu_buffer(struct ring_buffer_per_cpu *cpu_buffer)
1076{
Steven Rostedt3adc54f2009-03-30 15:32:01 -04001077 struct list_head *head = cpu_buffer->pages;
Steven Rostedt044fa782008-12-02 23:50:03 -05001078 struct buffer_page *bpage, *tmp;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001079
Steven Rostedtd7690412008-10-01 00:29:53 -04001080 free_buffer_page(cpu_buffer->reader_page);
1081
Steven Rostedt77ae3652009-03-27 11:00:29 -04001082 rb_head_page_deactivate(cpu_buffer);
1083
Steven Rostedt3adc54f2009-03-30 15:32:01 -04001084 if (head) {
1085 list_for_each_entry_safe(bpage, tmp, head, list) {
1086 list_del_init(&bpage->list);
1087 free_buffer_page(bpage);
1088 }
1089 bpage = list_entry(head, struct buffer_page, list);
Steven Rostedt044fa782008-12-02 23:50:03 -05001090 free_buffer_page(bpage);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001091 }
Steven Rostedt3adc54f2009-03-30 15:32:01 -04001092
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001093 kfree(cpu_buffer);
1094}
1095
Steven Rostedt59222ef2009-03-12 11:46:03 -04001096#ifdef CONFIG_HOTPLUG_CPU
Frederic Weisbecker09c9e842009-03-21 04:33:36 +01001097static int rb_cpu_notify(struct notifier_block *self,
1098 unsigned long action, void *hcpu);
Steven Rostedt554f7862009-03-11 22:00:13 -04001099#endif
1100
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001101/**
1102 * ring_buffer_alloc - allocate a new ring_buffer
Robert Richter68814b52008-11-24 12:24:12 +01001103 * @size: the size in bytes per cpu that is needed.
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001104 * @flags: attributes to set for the ring buffer.
1105 *
1106 * Currently the only flag that is available is the RB_FL_OVERWRITE
1107 * flag. This flag means that the buffer will overwrite old data
1108 * when the buffer wraps. If this flag is not set, the buffer will
1109 * drop data when the tail hits the head.
1110 */
Peter Zijlstra1f8a6a12009-06-08 18:18:39 +02001111struct ring_buffer *__ring_buffer_alloc(unsigned long size, unsigned flags,
1112 struct lock_class_key *key)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001113{
1114 struct ring_buffer *buffer;
1115 int bsize;
1116 int cpu;
1117
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001118 /* keep it in its own cache line */
1119 buffer = kzalloc(ALIGN(sizeof(*buffer), cache_line_size()),
1120 GFP_KERNEL);
1121 if (!buffer)
1122 return NULL;
1123
Rusty Russell9e01c1b2009-01-01 10:12:22 +10301124 if (!alloc_cpumask_var(&buffer->cpumask, GFP_KERNEL))
1125 goto fail_free_buffer;
1126
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001127 buffer->pages = DIV_ROUND_UP(size, BUF_PAGE_SIZE);
1128 buffer->flags = flags;
Steven Rostedt37886f62009-03-17 17:22:06 -04001129 buffer->clock = trace_clock_local;
Peter Zijlstra1f8a6a12009-06-08 18:18:39 +02001130 buffer->reader_lock_key = key;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001131
1132 /* need at least two pages */
Steven Rostedt5f78abe2009-06-17 14:11:10 -04001133 if (buffer->pages < 2)
1134 buffer->pages = 2;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001135
Frederic Weisbecker3bf832c2009-03-19 14:47:33 +01001136 /*
1137 * In case of non-hotplug cpu, if the ring-buffer is allocated
1138 * in early initcall, it will not be notified of secondary cpus.
1139 * In that off case, we need to allocate for all possible cpus.
1140 */
1141#ifdef CONFIG_HOTPLUG_CPU
Steven Rostedt554f7862009-03-11 22:00:13 -04001142 get_online_cpus();
1143 cpumask_copy(buffer->cpumask, cpu_online_mask);
Frederic Weisbecker3bf832c2009-03-19 14:47:33 +01001144#else
1145 cpumask_copy(buffer->cpumask, cpu_possible_mask);
1146#endif
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001147 buffer->cpus = nr_cpu_ids;
1148
1149 bsize = sizeof(void *) * nr_cpu_ids;
1150 buffer->buffers = kzalloc(ALIGN(bsize, cache_line_size()),
1151 GFP_KERNEL);
1152 if (!buffer->buffers)
Rusty Russell9e01c1b2009-01-01 10:12:22 +10301153 goto fail_free_cpumask;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001154
1155 for_each_buffer_cpu(buffer, cpu) {
1156 buffer->buffers[cpu] =
1157 rb_allocate_cpu_buffer(buffer, cpu);
1158 if (!buffer->buffers[cpu])
1159 goto fail_free_buffers;
1160 }
1161
Steven Rostedt59222ef2009-03-12 11:46:03 -04001162#ifdef CONFIG_HOTPLUG_CPU
Steven Rostedt554f7862009-03-11 22:00:13 -04001163 buffer->cpu_notify.notifier_call = rb_cpu_notify;
1164 buffer->cpu_notify.priority = 0;
1165 register_cpu_notifier(&buffer->cpu_notify);
1166#endif
1167
1168 put_online_cpus();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001169 mutex_init(&buffer->mutex);
1170
1171 return buffer;
1172
1173 fail_free_buffers:
1174 for_each_buffer_cpu(buffer, cpu) {
1175 if (buffer->buffers[cpu])
1176 rb_free_cpu_buffer(buffer->buffers[cpu]);
1177 }
1178 kfree(buffer->buffers);
1179
Rusty Russell9e01c1b2009-01-01 10:12:22 +10301180 fail_free_cpumask:
1181 free_cpumask_var(buffer->cpumask);
Steven Rostedt554f7862009-03-11 22:00:13 -04001182 put_online_cpus();
Rusty Russell9e01c1b2009-01-01 10:12:22 +10301183
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001184 fail_free_buffer:
1185 kfree(buffer);
1186 return NULL;
1187}
Peter Zijlstra1f8a6a12009-06-08 18:18:39 +02001188EXPORT_SYMBOL_GPL(__ring_buffer_alloc);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001189
1190/**
1191 * ring_buffer_free - free a ring buffer.
1192 * @buffer: the buffer to free.
1193 */
1194void
1195ring_buffer_free(struct ring_buffer *buffer)
1196{
1197 int cpu;
1198
Steven Rostedt554f7862009-03-11 22:00:13 -04001199 get_online_cpus();
1200
Steven Rostedt59222ef2009-03-12 11:46:03 -04001201#ifdef CONFIG_HOTPLUG_CPU
Steven Rostedt554f7862009-03-11 22:00:13 -04001202 unregister_cpu_notifier(&buffer->cpu_notify);
1203#endif
1204
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001205 for_each_buffer_cpu(buffer, cpu)
1206 rb_free_cpu_buffer(buffer->buffers[cpu]);
1207
Steven Rostedt554f7862009-03-11 22:00:13 -04001208 put_online_cpus();
1209
Eric Dumazetbd3f0222009-08-07 12:49:29 +02001210 kfree(buffer->buffers);
Rusty Russell9e01c1b2009-01-01 10:12:22 +10301211 free_cpumask_var(buffer->cpumask);
1212
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001213 kfree(buffer);
1214}
Robert Richterc4f50182008-12-11 16:49:22 +01001215EXPORT_SYMBOL_GPL(ring_buffer_free);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001216
Steven Rostedt37886f62009-03-17 17:22:06 -04001217void ring_buffer_set_clock(struct ring_buffer *buffer,
1218 u64 (*clock)(void))
1219{
1220 buffer->clock = clock;
1221}
1222
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001223static void rb_reset_cpu(struct ring_buffer_per_cpu *cpu_buffer);
1224
1225static void
1226rb_remove_pages(struct ring_buffer_per_cpu *cpu_buffer, unsigned nr_pages)
1227{
Steven Rostedt044fa782008-12-02 23:50:03 -05001228 struct buffer_page *bpage;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001229 struct list_head *p;
1230 unsigned i;
1231
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02001232 raw_spin_lock_irq(&cpu_buffer->reader_lock);
Steven Rostedt77ae3652009-03-27 11:00:29 -04001233 rb_head_page_deactivate(cpu_buffer);
1234
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001235 for (i = 0; i < nr_pages; i++) {
Steven Rostedt3adc54f2009-03-30 15:32:01 -04001236 if (RB_WARN_ON(cpu_buffer, list_empty(cpu_buffer->pages)))
Julia Lawall292f60c2010-03-29 17:37:02 +02001237 goto out;
Steven Rostedt3adc54f2009-03-30 15:32:01 -04001238 p = cpu_buffer->pages->next;
Steven Rostedt044fa782008-12-02 23:50:03 -05001239 bpage = list_entry(p, struct buffer_page, list);
1240 list_del_init(&bpage->list);
1241 free_buffer_page(bpage);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001242 }
Steven Rostedt3adc54f2009-03-30 15:32:01 -04001243 if (RB_WARN_ON(cpu_buffer, list_empty(cpu_buffer->pages)))
Julia Lawall292f60c2010-03-29 17:37:02 +02001244 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001245
1246 rb_reset_cpu(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001247 rb_check_pages(cpu_buffer);
1248
Julia Lawall292f60c2010-03-29 17:37:02 +02001249out:
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02001250 raw_spin_unlock_irq(&cpu_buffer->reader_lock);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001251}
1252
1253static void
1254rb_insert_pages(struct ring_buffer_per_cpu *cpu_buffer,
1255 struct list_head *pages, unsigned nr_pages)
1256{
Steven Rostedt044fa782008-12-02 23:50:03 -05001257 struct buffer_page *bpage;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001258 struct list_head *p;
1259 unsigned i;
1260
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02001261 raw_spin_lock_irq(&cpu_buffer->reader_lock);
Steven Rostedt77ae3652009-03-27 11:00:29 -04001262 rb_head_page_deactivate(cpu_buffer);
1263
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001264 for (i = 0; i < nr_pages; i++) {
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05001265 if (RB_WARN_ON(cpu_buffer, list_empty(pages)))
Julia Lawall292f60c2010-03-29 17:37:02 +02001266 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001267 p = pages->next;
Steven Rostedt044fa782008-12-02 23:50:03 -05001268 bpage = list_entry(p, struct buffer_page, list);
1269 list_del_init(&bpage->list);
Steven Rostedt3adc54f2009-03-30 15:32:01 -04001270 list_add_tail(&bpage->list, cpu_buffer->pages);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001271 }
1272 rb_reset_cpu(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001273 rb_check_pages(cpu_buffer);
1274
Julia Lawall292f60c2010-03-29 17:37:02 +02001275out:
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02001276 raw_spin_unlock_irq(&cpu_buffer->reader_lock);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001277}
1278
1279/**
1280 * ring_buffer_resize - resize the ring buffer
1281 * @buffer: the buffer to resize.
1282 * @size: the new size.
1283 *
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001284 * Minimum size is 2 * BUF_PAGE_SIZE.
1285 *
1286 * Returns -1 on failure.
1287 */
1288int ring_buffer_resize(struct ring_buffer *buffer, unsigned long size)
1289{
1290 struct ring_buffer_per_cpu *cpu_buffer;
1291 unsigned nr_pages, rm_pages, new_pages;
Steven Rostedt044fa782008-12-02 23:50:03 -05001292 struct buffer_page *bpage, *tmp;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001293 unsigned long buffer_size;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001294 LIST_HEAD(pages);
1295 int i, cpu;
1296
Ingo Molnaree51a1d2008-11-13 14:58:31 +01001297 /*
1298 * Always succeed at resizing a non-existent buffer:
1299 */
1300 if (!buffer)
1301 return size;
1302
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001303 size = DIV_ROUND_UP(size, BUF_PAGE_SIZE);
1304 size *= BUF_PAGE_SIZE;
1305 buffer_size = buffer->pages * BUF_PAGE_SIZE;
1306
1307 /* we need a minimum of two pages */
1308 if (size < BUF_PAGE_SIZE * 2)
1309 size = BUF_PAGE_SIZE * 2;
1310
1311 if (size == buffer_size)
1312 return size;
1313
Steven Rostedt18421012009-12-10 22:54:27 -05001314 atomic_inc(&buffer->record_disabled);
1315
1316 /* Make sure all writers are done with this buffer. */
1317 synchronize_sched();
1318
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001319 mutex_lock(&buffer->mutex);
Steven Rostedt554f7862009-03-11 22:00:13 -04001320 get_online_cpus();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001321
1322 nr_pages = DIV_ROUND_UP(size, BUF_PAGE_SIZE);
1323
1324 if (size < buffer_size) {
1325
1326 /* easy case, just free pages */
Steven Rostedt554f7862009-03-11 22:00:13 -04001327 if (RB_WARN_ON(buffer, nr_pages >= buffer->pages))
1328 goto out_fail;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001329
1330 rm_pages = buffer->pages - nr_pages;
1331
1332 for_each_buffer_cpu(buffer, cpu) {
1333 cpu_buffer = buffer->buffers[cpu];
1334 rb_remove_pages(cpu_buffer, rm_pages);
1335 }
1336 goto out;
1337 }
1338
1339 /*
1340 * This is a bit more difficult. We only want to add pages
1341 * when we can allocate enough for all CPUs. We do this
1342 * by allocating all the pages and storing them on a local
1343 * link list. If we succeed in our allocation, then we
1344 * add these pages to the cpu_buffers. Otherwise we just free
1345 * them all and return -ENOMEM;
1346 */
Steven Rostedt554f7862009-03-11 22:00:13 -04001347 if (RB_WARN_ON(buffer, nr_pages <= buffer->pages))
1348 goto out_fail;
Steven Rostedtf536aaf2008-11-10 23:07:30 -05001349
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001350 new_pages = nr_pages - buffer->pages;
1351
1352 for_each_buffer_cpu(buffer, cpu) {
1353 for (i = 0; i < new_pages; i++) {
Vaibhav Nagarnaik7ea59062011-05-03 17:56:42 -07001354 struct page *page;
Vaibhav Nagarnaikd7ec4bf2011-06-07 17:01:42 -07001355 /*
1356 * __GFP_NORETRY flag makes sure that the allocation
1357 * fails gracefully without invoking oom-killer and
1358 * the system is not destabilized.
1359 */
Steven Rostedt044fa782008-12-02 23:50:03 -05001360 bpage = kzalloc_node(ALIGN(sizeof(*bpage),
Steven Rostedte4c2ce82008-10-01 11:14:54 -04001361 cache_line_size()),
Vaibhav Nagarnaikd7ec4bf2011-06-07 17:01:42 -07001362 GFP_KERNEL | __GFP_NORETRY,
1363 cpu_to_node(cpu));
Steven Rostedt044fa782008-12-02 23:50:03 -05001364 if (!bpage)
Steven Rostedte4c2ce82008-10-01 11:14:54 -04001365 goto free_pages;
Steven Rostedt044fa782008-12-02 23:50:03 -05001366 list_add(&bpage->list, &pages);
Vaibhav Nagarnaikd7ec4bf2011-06-07 17:01:42 -07001367 page = alloc_pages_node(cpu_to_node(cpu),
1368 GFP_KERNEL | __GFP_NORETRY, 0);
Vaibhav Nagarnaik7ea59062011-05-03 17:56:42 -07001369 if (!page)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001370 goto free_pages;
Vaibhav Nagarnaik7ea59062011-05-03 17:56:42 -07001371 bpage->page = page_address(page);
Steven Rostedt044fa782008-12-02 23:50:03 -05001372 rb_init_page(bpage->page);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001373 }
1374 }
1375
1376 for_each_buffer_cpu(buffer, cpu) {
1377 cpu_buffer = buffer->buffers[cpu];
1378 rb_insert_pages(cpu_buffer, &pages, new_pages);
1379 }
1380
Steven Rostedt554f7862009-03-11 22:00:13 -04001381 if (RB_WARN_ON(buffer, !list_empty(&pages)))
1382 goto out_fail;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001383
1384 out:
1385 buffer->pages = nr_pages;
Steven Rostedt554f7862009-03-11 22:00:13 -04001386 put_online_cpus();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001387 mutex_unlock(&buffer->mutex);
1388
Steven Rostedt18421012009-12-10 22:54:27 -05001389 atomic_dec(&buffer->record_disabled);
1390
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001391 return size;
1392
1393 free_pages:
Steven Rostedt044fa782008-12-02 23:50:03 -05001394 list_for_each_entry_safe(bpage, tmp, &pages, list) {
1395 list_del_init(&bpage->list);
1396 free_buffer_page(bpage);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001397 }
Steven Rostedt554f7862009-03-11 22:00:13 -04001398 put_online_cpus();
Vegard Nossum641d2f62008-11-18 19:22:13 +01001399 mutex_unlock(&buffer->mutex);
Steven Rostedt18421012009-12-10 22:54:27 -05001400 atomic_dec(&buffer->record_disabled);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001401 return -ENOMEM;
Steven Rostedt554f7862009-03-11 22:00:13 -04001402
1403 /*
1404 * Something went totally wrong, and we are too paranoid
1405 * to even clean up the mess.
1406 */
1407 out_fail:
1408 put_online_cpus();
1409 mutex_unlock(&buffer->mutex);
Steven Rostedt18421012009-12-10 22:54:27 -05001410 atomic_dec(&buffer->record_disabled);
Steven Rostedt554f7862009-03-11 22:00:13 -04001411 return -1;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001412}
Robert Richterc4f50182008-12-11 16:49:22 +01001413EXPORT_SYMBOL_GPL(ring_buffer_resize);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001414
David Sharp750912f2010-12-08 13:46:47 -08001415void ring_buffer_change_overwrite(struct ring_buffer *buffer, int val)
1416{
1417 mutex_lock(&buffer->mutex);
1418 if (val)
1419 buffer->flags |= RB_FL_OVERWRITE;
1420 else
1421 buffer->flags &= ~RB_FL_OVERWRITE;
1422 mutex_unlock(&buffer->mutex);
1423}
1424EXPORT_SYMBOL_GPL(ring_buffer_change_overwrite);
1425
Steven Rostedt8789a9e2008-12-02 15:34:07 -05001426static inline void *
Steven Rostedt044fa782008-12-02 23:50:03 -05001427__rb_data_page_index(struct buffer_data_page *bpage, unsigned index)
Steven Rostedt8789a9e2008-12-02 15:34:07 -05001428{
Steven Rostedt044fa782008-12-02 23:50:03 -05001429 return bpage->data + index;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05001430}
1431
Steven Rostedt044fa782008-12-02 23:50:03 -05001432static inline void *__rb_page_index(struct buffer_page *bpage, unsigned index)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001433{
Steven Rostedt044fa782008-12-02 23:50:03 -05001434 return bpage->page->data + index;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001435}
1436
1437static inline struct ring_buffer_event *
Steven Rostedtd7690412008-10-01 00:29:53 -04001438rb_reader_event(struct ring_buffer_per_cpu *cpu_buffer)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001439{
Steven Rostedt6f807ac2008-10-04 02:00:58 -04001440 return __rb_page_index(cpu_buffer->reader_page,
1441 cpu_buffer->reader_page->read);
1442}
1443
1444static inline struct ring_buffer_event *
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001445rb_iter_head_event(struct ring_buffer_iter *iter)
1446{
Steven Rostedt6f807ac2008-10-04 02:00:58 -04001447 return __rb_page_index(iter->head_page, iter->head);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001448}
1449
Steven Rostedt77ae3652009-03-27 11:00:29 -04001450static inline unsigned long rb_page_write(struct buffer_page *bpage)
Steven Rostedtbf41a152008-10-04 02:00:59 -04001451{
Steven Rostedt77ae3652009-03-27 11:00:29 -04001452 return local_read(&bpage->write) & RB_WRITE_MASK;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001453}
1454
1455static inline unsigned rb_page_commit(struct buffer_page *bpage)
1456{
Steven Rostedtabc9b562008-12-02 15:34:06 -05001457 return local_read(&bpage->page->commit);
Steven Rostedtbf41a152008-10-04 02:00:59 -04001458}
1459
Steven Rostedt77ae3652009-03-27 11:00:29 -04001460static inline unsigned long rb_page_entries(struct buffer_page *bpage)
1461{
1462 return local_read(&bpage->entries) & RB_WRITE_MASK;
1463}
1464
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001465/* Size is determined by what has been committed */
Steven Rostedtbf41a152008-10-04 02:00:59 -04001466static inline unsigned rb_page_size(struct buffer_page *bpage)
1467{
1468 return rb_page_commit(bpage);
1469}
1470
1471static inline unsigned
1472rb_commit_index(struct ring_buffer_per_cpu *cpu_buffer)
1473{
1474 return rb_page_commit(cpu_buffer->commit_page);
1475}
1476
Steven Rostedtbf41a152008-10-04 02:00:59 -04001477static inline unsigned
1478rb_event_index(struct ring_buffer_event *event)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001479{
Steven Rostedtbf41a152008-10-04 02:00:59 -04001480 unsigned long addr = (unsigned long)event;
1481
Steven Rostedt22f470f2009-06-11 09:29:58 -04001482 return (addr & ~PAGE_MASK) - BUF_PAGE_HDR_SIZE;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001483}
1484
Steven Rostedt0f0c85f2009-05-11 16:08:00 -04001485static inline int
Steven Rostedtfa743952009-06-16 12:37:57 -04001486rb_event_is_commit(struct ring_buffer_per_cpu *cpu_buffer,
1487 struct ring_buffer_event *event)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001488{
Steven Rostedtbf41a152008-10-04 02:00:59 -04001489 unsigned long addr = (unsigned long)event;
1490 unsigned long index;
1491
1492 index = rb_event_index(event);
1493 addr &= PAGE_MASK;
1494
1495 return cpu_buffer->commit_page->page == (void *)addr &&
1496 rb_commit_index(cpu_buffer) == index;
1497}
1498
Andrew Morton34a148b2009-01-09 12:27:09 -08001499static void
Steven Rostedtbf41a152008-10-04 02:00:59 -04001500rb_set_commit_to_write(struct ring_buffer_per_cpu *cpu_buffer)
1501{
Steven Rostedt77ae3652009-03-27 11:00:29 -04001502 unsigned long max_count;
1503
Steven Rostedtbf41a152008-10-04 02:00:59 -04001504 /*
1505 * We only race with interrupts and NMIs on this CPU.
1506 * If we own the commit event, then we can commit
1507 * all others that interrupted us, since the interruptions
1508 * are in stack format (they finish before they come
1509 * back to us). This allows us to do a simple loop to
1510 * assign the commit to the tail.
1511 */
Steven Rostedta8ccf1d2008-12-23 11:32:24 -05001512 again:
Steven Rostedt77ae3652009-03-27 11:00:29 -04001513 max_count = cpu_buffer->buffer->pages * 100;
1514
Steven Rostedtbf41a152008-10-04 02:00:59 -04001515 while (cpu_buffer->commit_page != cpu_buffer->tail_page) {
Steven Rostedt77ae3652009-03-27 11:00:29 -04001516 if (RB_WARN_ON(cpu_buffer, !(--max_count)))
1517 return;
1518 if (RB_WARN_ON(cpu_buffer,
1519 rb_is_reader_page(cpu_buffer->tail_page)))
1520 return;
1521 local_set(&cpu_buffer->commit_page->page->commit,
1522 rb_page_write(cpu_buffer->commit_page));
Steven Rostedtbf41a152008-10-04 02:00:59 -04001523 rb_inc_page(cpu_buffer, &cpu_buffer->commit_page);
Steven Rostedtabc9b562008-12-02 15:34:06 -05001524 cpu_buffer->write_stamp =
1525 cpu_buffer->commit_page->page->time_stamp;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001526 /* add barrier to keep gcc from optimizing too much */
1527 barrier();
1528 }
1529 while (rb_commit_index(cpu_buffer) !=
1530 rb_page_write(cpu_buffer->commit_page)) {
Steven Rostedt77ae3652009-03-27 11:00:29 -04001531
1532 local_set(&cpu_buffer->commit_page->page->commit,
1533 rb_page_write(cpu_buffer->commit_page));
1534 RB_WARN_ON(cpu_buffer,
1535 local_read(&cpu_buffer->commit_page->page->commit) &
1536 ~RB_WRITE_MASK);
Steven Rostedtbf41a152008-10-04 02:00:59 -04001537 barrier();
1538 }
Steven Rostedta8ccf1d2008-12-23 11:32:24 -05001539
1540 /* again, keep gcc from optimizing */
1541 barrier();
1542
1543 /*
1544 * If an interrupt came in just after the first while loop
1545 * and pushed the tail page forward, we will be left with
1546 * a dangling commit that will never go forward.
1547 */
1548 if (unlikely(cpu_buffer->commit_page != cpu_buffer->tail_page))
1549 goto again;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001550}
1551
Andrew Morton34a148b2009-01-09 12:27:09 -08001552static void rb_inc_iter(struct ring_buffer_iter *iter)
Steven Rostedtd7690412008-10-01 00:29:53 -04001553{
1554 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
1555
1556 /*
1557 * The iterator could be on the reader page (it starts there).
1558 * But the head could have moved, since the reader was
1559 * found. Check for this case and assign the iterator
1560 * to the head page instead of next.
1561 */
1562 if (iter->head_page == cpu_buffer->reader_page)
Steven Rostedt77ae3652009-03-27 11:00:29 -04001563 iter->head_page = rb_set_head_page(cpu_buffer);
Steven Rostedtd7690412008-10-01 00:29:53 -04001564 else
1565 rb_inc_page(cpu_buffer, &iter->head_page);
1566
Steven Rostedtabc9b562008-12-02 15:34:06 -05001567 iter->read_stamp = iter->head_page->page->time_stamp;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001568 iter->head = 0;
1569}
1570
Steven Rostedt69d1b832010-10-07 18:18:05 -04001571/* Slow path, do not inline */
1572static noinline struct ring_buffer_event *
1573rb_add_time_stamp(struct ring_buffer_event *event, u64 delta)
1574{
1575 event->type_len = RINGBUF_TYPE_TIME_EXTEND;
1576
1577 /* Not the first event on the page? */
1578 if (rb_event_index(event)) {
1579 event->time_delta = delta & TS_MASK;
1580 event->array[0] = delta >> TS_SHIFT;
1581 } else {
1582 /* nope, just zero it */
1583 event->time_delta = 0;
1584 event->array[0] = 0;
1585 }
1586
1587 return skip_time_extend(event);
1588}
1589
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001590/**
1591 * ring_buffer_update_event - update event type and data
1592 * @event: the even to update
1593 * @type: the type of event
1594 * @length: the size of the event field in the ring buffer
1595 *
1596 * Update the type and data fields of the event. The length
1597 * is the actual size that is written to the ring buffer,
1598 * and with this, we can determine what to place into the
1599 * data field.
1600 */
Andrew Morton34a148b2009-01-09 12:27:09 -08001601static void
Steven Rostedt69d1b832010-10-07 18:18:05 -04001602rb_update_event(struct ring_buffer_per_cpu *cpu_buffer,
1603 struct ring_buffer_event *event, unsigned length,
1604 int add_timestamp, u64 delta)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001605{
Steven Rostedt69d1b832010-10-07 18:18:05 -04001606 /* Only a commit updates the timestamp */
1607 if (unlikely(!rb_event_is_commit(cpu_buffer, event)))
1608 delta = 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001609
Steven Rostedt69d1b832010-10-07 18:18:05 -04001610 /*
1611 * If we need to add a timestamp, then we
1612 * add it to the start of the resevered space.
1613 */
1614 if (unlikely(add_timestamp)) {
1615 event = rb_add_time_stamp(event, delta);
1616 length -= RB_LEN_TIME_EXTEND;
1617 delta = 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001618 }
Steven Rostedt69d1b832010-10-07 18:18:05 -04001619
1620 event->time_delta = delta;
1621 length -= RB_EVNT_HDR_SIZE;
1622 if (length > RB_MAX_SMALL_DATA || RB_FORCE_8BYTE_ALIGNMENT) {
1623 event->type_len = 0;
1624 event->array[0] = length;
1625 } else
1626 event->type_len = DIV_ROUND_UP(length, RB_ALIGNMENT);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001627}
1628
Steven Rostedt77ae3652009-03-27 11:00:29 -04001629/*
1630 * rb_handle_head_page - writer hit the head page
1631 *
1632 * Returns: +1 to retry page
1633 * 0 to continue
1634 * -1 on error
1635 */
1636static int
1637rb_handle_head_page(struct ring_buffer_per_cpu *cpu_buffer,
1638 struct buffer_page *tail_page,
1639 struct buffer_page *next_page)
1640{
1641 struct buffer_page *new_head;
1642 int entries;
1643 int type;
1644 int ret;
1645
1646 entries = rb_page_entries(next_page);
1647
1648 /*
1649 * The hard part is here. We need to move the head
1650 * forward, and protect against both readers on
1651 * other CPUs and writers coming in via interrupts.
1652 */
1653 type = rb_head_page_set_update(cpu_buffer, next_page, tail_page,
1654 RB_PAGE_HEAD);
1655
1656 /*
1657 * type can be one of four:
1658 * NORMAL - an interrupt already moved it for us
1659 * HEAD - we are the first to get here.
1660 * UPDATE - we are the interrupt interrupting
1661 * a current move.
1662 * MOVED - a reader on another CPU moved the next
1663 * pointer to its reader page. Give up
1664 * and try again.
1665 */
1666
1667 switch (type) {
1668 case RB_PAGE_HEAD:
1669 /*
1670 * We changed the head to UPDATE, thus
1671 * it is our responsibility to update
1672 * the counters.
1673 */
1674 local_add(entries, &cpu_buffer->overrun);
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -07001675 local_sub(BUF_PAGE_SIZE, &cpu_buffer->entries_bytes);
Steven Rostedt77ae3652009-03-27 11:00:29 -04001676
1677 /*
1678 * The entries will be zeroed out when we move the
1679 * tail page.
1680 */
1681
1682 /* still more to do */
1683 break;
1684
1685 case RB_PAGE_UPDATE:
1686 /*
1687 * This is an interrupt that interrupt the
1688 * previous update. Still more to do.
1689 */
1690 break;
1691 case RB_PAGE_NORMAL:
1692 /*
1693 * An interrupt came in before the update
1694 * and processed this for us.
1695 * Nothing left to do.
1696 */
1697 return 1;
1698 case RB_PAGE_MOVED:
1699 /*
1700 * The reader is on another CPU and just did
1701 * a swap with our next_page.
1702 * Try again.
1703 */
1704 return 1;
1705 default:
1706 RB_WARN_ON(cpu_buffer, 1); /* WTF??? */
1707 return -1;
1708 }
1709
1710 /*
1711 * Now that we are here, the old head pointer is
1712 * set to UPDATE. This will keep the reader from
1713 * swapping the head page with the reader page.
1714 * The reader (on another CPU) will spin till
1715 * we are finished.
1716 *
1717 * We just need to protect against interrupts
1718 * doing the job. We will set the next pointer
1719 * to HEAD. After that, we set the old pointer
1720 * to NORMAL, but only if it was HEAD before.
1721 * otherwise we are an interrupt, and only
1722 * want the outer most commit to reset it.
1723 */
1724 new_head = next_page;
1725 rb_inc_page(cpu_buffer, &new_head);
1726
1727 ret = rb_head_page_set_head(cpu_buffer, new_head, next_page,
1728 RB_PAGE_NORMAL);
1729
1730 /*
1731 * Valid returns are:
1732 * HEAD - an interrupt came in and already set it.
1733 * NORMAL - One of two things:
1734 * 1) We really set it.
1735 * 2) A bunch of interrupts came in and moved
1736 * the page forward again.
1737 */
1738 switch (ret) {
1739 case RB_PAGE_HEAD:
1740 case RB_PAGE_NORMAL:
1741 /* OK */
1742 break;
1743 default:
1744 RB_WARN_ON(cpu_buffer, 1);
1745 return -1;
1746 }
1747
1748 /*
1749 * It is possible that an interrupt came in,
1750 * set the head up, then more interrupts came in
1751 * and moved it again. When we get back here,
1752 * the page would have been set to NORMAL but we
1753 * just set it back to HEAD.
1754 *
1755 * How do you detect this? Well, if that happened
1756 * the tail page would have moved.
1757 */
1758 if (ret == RB_PAGE_NORMAL) {
1759 /*
1760 * If the tail had moved passed next, then we need
1761 * to reset the pointer.
1762 */
1763 if (cpu_buffer->tail_page != tail_page &&
1764 cpu_buffer->tail_page != next_page)
1765 rb_head_page_set_normal(cpu_buffer, new_head,
1766 next_page,
1767 RB_PAGE_HEAD);
1768 }
1769
1770 /*
1771 * If this was the outer most commit (the one that
1772 * changed the original pointer from HEAD to UPDATE),
1773 * then it is up to us to reset it to NORMAL.
1774 */
1775 if (type == RB_PAGE_HEAD) {
1776 ret = rb_head_page_set_normal(cpu_buffer, next_page,
1777 tail_page,
1778 RB_PAGE_UPDATE);
1779 if (RB_WARN_ON(cpu_buffer,
1780 ret != RB_PAGE_UPDATE))
1781 return -1;
1782 }
1783
1784 return 0;
1785}
1786
Andrew Morton34a148b2009-01-09 12:27:09 -08001787static unsigned rb_calculate_event_length(unsigned length)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001788{
1789 struct ring_buffer_event event; /* Used only for sizeof array */
1790
1791 /* zero length can cause confusions */
1792 if (!length)
1793 length = 1;
1794
Steven Rostedt22710482010-03-18 17:54:19 -04001795 if (length > RB_MAX_SMALL_DATA || RB_FORCE_8BYTE_ALIGNMENT)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001796 length += sizeof(event.array[0]);
1797
1798 length += RB_EVNT_HDR_SIZE;
Steven Rostedt22710482010-03-18 17:54:19 -04001799 length = ALIGN(length, RB_ARCH_ALIGNMENT);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001800
1801 return length;
1802}
1803
Steven Rostedtc7b09302009-06-11 11:12:00 -04001804static inline void
1805rb_reset_tail(struct ring_buffer_per_cpu *cpu_buffer,
1806 struct buffer_page *tail_page,
1807 unsigned long tail, unsigned long length)
1808{
1809 struct ring_buffer_event *event;
1810
1811 /*
1812 * Only the event that crossed the page boundary
1813 * must fill the old tail_page with padding.
1814 */
1815 if (tail >= BUF_PAGE_SIZE) {
Steven Rostedtb3230c82010-05-21 11:55:21 -04001816 /*
1817 * If the page was filled, then we still need
1818 * to update the real_end. Reset it to zero
1819 * and the reader will ignore it.
1820 */
1821 if (tail == BUF_PAGE_SIZE)
1822 tail_page->real_end = 0;
1823
Steven Rostedtc7b09302009-06-11 11:12:00 -04001824 local_sub(length, &tail_page->write);
1825 return;
1826 }
1827
1828 event = __rb_page_index(tail_page, tail);
Linus Torvaldsb0b70652009-06-20 10:56:46 -07001829 kmemcheck_annotate_bitfield(event, bitfield);
Steven Rostedtc7b09302009-06-11 11:12:00 -04001830
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -07001831 /* account for padding bytes */
1832 local_add(BUF_PAGE_SIZE - tail, &cpu_buffer->entries_bytes);
1833
Steven Rostedtc7b09302009-06-11 11:12:00 -04001834 /*
Steven Rostedtff0ff842010-03-31 22:11:42 -04001835 * Save the original length to the meta data.
1836 * This will be used by the reader to add lost event
1837 * counter.
1838 */
1839 tail_page->real_end = tail;
1840
1841 /*
Steven Rostedtc7b09302009-06-11 11:12:00 -04001842 * If this event is bigger than the minimum size, then
1843 * we need to be careful that we don't subtract the
1844 * write counter enough to allow another writer to slip
1845 * in on this page.
1846 * We put in a discarded commit instead, to make sure
1847 * that this space is not used again.
1848 *
1849 * If we are less than the minimum size, we don't need to
1850 * worry about it.
1851 */
1852 if (tail > (BUF_PAGE_SIZE - RB_EVNT_MIN_SIZE)) {
1853 /* No room for any events */
1854
1855 /* Mark the rest of the page with padding */
1856 rb_event_set_padding(event);
1857
1858 /* Set the write back to the previous setting */
1859 local_sub(length, &tail_page->write);
1860 return;
1861 }
1862
1863 /* Put in a discarded event */
1864 event->array[0] = (BUF_PAGE_SIZE - tail) - RB_EVNT_HDR_SIZE;
1865 event->type_len = RINGBUF_TYPE_PADDING;
1866 /* time delta must be non zero */
1867 event->time_delta = 1;
Steven Rostedtc7b09302009-06-11 11:12:00 -04001868
1869 /* Set write to end of buffer */
1870 length = (tail + length) - BUF_PAGE_SIZE;
1871 local_sub(length, &tail_page->write);
1872}
Steven Rostedt6634ff22009-05-06 15:30:07 -04001873
Steven Rostedt747e94a2010-10-08 13:51:48 -04001874/*
1875 * This is the slow path, force gcc not to inline it.
1876 */
1877static noinline struct ring_buffer_event *
Steven Rostedt6634ff22009-05-06 15:30:07 -04001878rb_move_tail(struct ring_buffer_per_cpu *cpu_buffer,
1879 unsigned long length, unsigned long tail,
Steven Rostedte8bc43e2010-10-20 10:58:02 -04001880 struct buffer_page *tail_page, u64 ts)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001881{
Steven Rostedt5a50e332009-11-17 08:43:01 -05001882 struct buffer_page *commit_page = cpu_buffer->commit_page;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001883 struct ring_buffer *buffer = cpu_buffer->buffer;
Steven Rostedt77ae3652009-03-27 11:00:29 -04001884 struct buffer_page *next_page;
1885 int ret;
Steven Rostedtaa20ae82009-05-05 21:16:11 -04001886
1887 next_page = tail_page;
1888
Steven Rostedtaa20ae82009-05-05 21:16:11 -04001889 rb_inc_page(cpu_buffer, &next_page);
1890
Steven Rostedtaa20ae82009-05-05 21:16:11 -04001891 /*
1892 * If for some reason, we had an interrupt storm that made
1893 * it all the way around the buffer, bail, and warn
1894 * about it.
1895 */
1896 if (unlikely(next_page == commit_page)) {
Steven Rostedt77ae3652009-03-27 11:00:29 -04001897 local_inc(&cpu_buffer->commit_overrun);
Steven Rostedtaa20ae82009-05-05 21:16:11 -04001898 goto out_reset;
1899 }
1900
Steven Rostedt77ae3652009-03-27 11:00:29 -04001901 /*
1902 * This is where the fun begins!
1903 *
1904 * We are fighting against races between a reader that
1905 * could be on another CPU trying to swap its reader
1906 * page with the buffer head.
1907 *
1908 * We are also fighting against interrupts coming in and
1909 * moving the head or tail on us as well.
1910 *
1911 * If the next page is the head page then we have filled
1912 * the buffer, unless the commit page is still on the
1913 * reader page.
1914 */
1915 if (rb_is_head_page(cpu_buffer, next_page, &tail_page->list)) {
Steven Rostedtaa20ae82009-05-05 21:16:11 -04001916
Steven Rostedt77ae3652009-03-27 11:00:29 -04001917 /*
1918 * If the commit is not on the reader page, then
1919 * move the header page.
1920 */
1921 if (!rb_is_reader_page(cpu_buffer->commit_page)) {
1922 /*
1923 * If we are not in overwrite mode,
1924 * this is easy, just stop here.
1925 */
1926 if (!(buffer->flags & RB_FL_OVERWRITE))
1927 goto out_reset;
Steven Rostedtaa20ae82009-05-05 21:16:11 -04001928
Steven Rostedt77ae3652009-03-27 11:00:29 -04001929 ret = rb_handle_head_page(cpu_buffer,
1930 tail_page,
1931 next_page);
1932 if (ret < 0)
1933 goto out_reset;
1934 if (ret)
1935 goto out_again;
1936 } else {
1937 /*
1938 * We need to be careful here too. The
1939 * commit page could still be on the reader
1940 * page. We could have a small buffer, and
1941 * have filled up the buffer with events
1942 * from interrupts and such, and wrapped.
1943 *
1944 * Note, if the tail page is also the on the
1945 * reader_page, we let it move out.
1946 */
1947 if (unlikely((cpu_buffer->commit_page !=
1948 cpu_buffer->tail_page) &&
1949 (cpu_buffer->commit_page ==
1950 cpu_buffer->reader_page))) {
1951 local_inc(&cpu_buffer->commit_overrun);
1952 goto out_reset;
1953 }
Steven Rostedtaa20ae82009-05-05 21:16:11 -04001954 }
1955 }
1956
Steven Rostedt77ae3652009-03-27 11:00:29 -04001957 ret = rb_tail_page_update(cpu_buffer, tail_page, next_page);
1958 if (ret) {
1959 /*
1960 * Nested commits always have zero deltas, so
1961 * just reread the time stamp
1962 */
Steven Rostedte8bc43e2010-10-20 10:58:02 -04001963 ts = rb_time_stamp(buffer);
1964 next_page->page->time_stamp = ts;
Steven Rostedtaa20ae82009-05-05 21:16:11 -04001965 }
1966
Steven Rostedt77ae3652009-03-27 11:00:29 -04001967 out_again:
Steven Rostedtaa20ae82009-05-05 21:16:11 -04001968
Steven Rostedt77ae3652009-03-27 11:00:29 -04001969 rb_reset_tail(cpu_buffer, tail_page, tail, length);
Steven Rostedtaa20ae82009-05-05 21:16:11 -04001970
1971 /* fail and let the caller try again */
1972 return ERR_PTR(-EAGAIN);
1973
Steven Rostedt45141d42009-02-12 13:19:48 -05001974 out_reset:
Lai Jiangshan6f3b3442009-01-12 11:06:18 +08001975 /* reset write */
Steven Rostedtc7b09302009-06-11 11:12:00 -04001976 rb_reset_tail(cpu_buffer, tail_page, tail, length);
Lai Jiangshan6f3b3442009-01-12 11:06:18 +08001977
Steven Rostedtbf41a152008-10-04 02:00:59 -04001978 return NULL;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001979}
1980
Steven Rostedt6634ff22009-05-06 15:30:07 -04001981static struct ring_buffer_event *
1982__rb_reserve_next(struct ring_buffer_per_cpu *cpu_buffer,
Steven Rostedt69d1b832010-10-07 18:18:05 -04001983 unsigned long length, u64 ts,
1984 u64 delta, int add_timestamp)
Steven Rostedt6634ff22009-05-06 15:30:07 -04001985{
Steven Rostedt5a50e332009-11-17 08:43:01 -05001986 struct buffer_page *tail_page;
Steven Rostedt6634ff22009-05-06 15:30:07 -04001987 struct ring_buffer_event *event;
1988 unsigned long tail, write;
1989
Steven Rostedt69d1b832010-10-07 18:18:05 -04001990 /*
1991 * If the time delta since the last event is too big to
1992 * hold in the time field of the event, then we append a
1993 * TIME EXTEND event ahead of the data event.
1994 */
1995 if (unlikely(add_timestamp))
1996 length += RB_LEN_TIME_EXTEND;
1997
Steven Rostedt6634ff22009-05-06 15:30:07 -04001998 tail_page = cpu_buffer->tail_page;
1999 write = local_add_return(length, &tail_page->write);
Steven Rostedt77ae3652009-03-27 11:00:29 -04002000
2001 /* set write to only the index of the write */
2002 write &= RB_WRITE_MASK;
Steven Rostedt6634ff22009-05-06 15:30:07 -04002003 tail = write - length;
2004
Steven Rostedt (Red Hat)17ff13b2014-02-11 13:38:54 -05002005 /*
2006 * If this is the first commit on the page, then it has the same
2007 * timestamp as the page itself.
2008 */
2009 if (!tail)
2010 delta = 0;
2011
Steven Rostedt6634ff22009-05-06 15:30:07 -04002012 /* See if we shot pass the end of this buffer page */
Steven Rostedt747e94a2010-10-08 13:51:48 -04002013 if (unlikely(write > BUF_PAGE_SIZE))
Steven Rostedt6634ff22009-05-06 15:30:07 -04002014 return rb_move_tail(cpu_buffer, length, tail,
Steven Rostedt5a50e332009-11-17 08:43:01 -05002015 tail_page, ts);
Steven Rostedt6634ff22009-05-06 15:30:07 -04002016
2017 /* We reserved something on the buffer */
2018
Steven Rostedt6634ff22009-05-06 15:30:07 -04002019 event = __rb_page_index(tail_page, tail);
Vegard Nossum1744a212009-02-28 08:29:44 +01002020 kmemcheck_annotate_bitfield(event, bitfield);
Steven Rostedt69d1b832010-10-07 18:18:05 -04002021 rb_update_event(cpu_buffer, event, length, add_timestamp, delta);
Steven Rostedt6634ff22009-05-06 15:30:07 -04002022
Steven Rostedt69d1b832010-10-07 18:18:05 -04002023 local_inc(&tail_page->entries);
Steven Rostedt6634ff22009-05-06 15:30:07 -04002024
2025 /*
Steven Rostedtfa743952009-06-16 12:37:57 -04002026 * If this is the first commit on the page, then update
2027 * its timestamp.
Steven Rostedt6634ff22009-05-06 15:30:07 -04002028 */
Steven Rostedtfa743952009-06-16 12:37:57 -04002029 if (!tail)
Steven Rostedte8bc43e2010-10-20 10:58:02 -04002030 tail_page->page->time_stamp = ts;
Steven Rostedt6634ff22009-05-06 15:30:07 -04002031
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -07002032 /* account for these added bytes */
2033 local_add(length, &cpu_buffer->entries_bytes);
2034
Steven Rostedt6634ff22009-05-06 15:30:07 -04002035 return event;
2036}
2037
Steven Rostedtedd813b2009-06-02 23:00:53 -04002038static inline int
2039rb_try_to_discard(struct ring_buffer_per_cpu *cpu_buffer,
2040 struct ring_buffer_event *event)
2041{
2042 unsigned long new_index, old_index;
2043 struct buffer_page *bpage;
2044 unsigned long index;
2045 unsigned long addr;
2046
2047 new_index = rb_event_index(event);
Steven Rostedt69d1b832010-10-07 18:18:05 -04002048 old_index = new_index + rb_event_ts_length(event);
Steven Rostedtedd813b2009-06-02 23:00:53 -04002049 addr = (unsigned long)event;
2050 addr &= PAGE_MASK;
2051
2052 bpage = cpu_buffer->tail_page;
2053
2054 if (bpage->page == (void *)addr && rb_page_write(bpage) == old_index) {
Steven Rostedt77ae3652009-03-27 11:00:29 -04002055 unsigned long write_mask =
2056 local_read(&bpage->write) & ~RB_WRITE_MASK;
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -07002057 unsigned long event_length = rb_event_length(event);
Steven Rostedtedd813b2009-06-02 23:00:53 -04002058 /*
2059 * This is on the tail page. It is possible that
2060 * a write could come in and move the tail page
2061 * and write to the next page. That is fine
2062 * because we just shorten what is on this page.
2063 */
Steven Rostedt77ae3652009-03-27 11:00:29 -04002064 old_index += write_mask;
2065 new_index += write_mask;
Steven Rostedtedd813b2009-06-02 23:00:53 -04002066 index = local_cmpxchg(&bpage->write, old_index, new_index);
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -07002067 if (index == old_index) {
2068 /* update counters */
2069 local_sub(event_length, &cpu_buffer->entries_bytes);
Steven Rostedtedd813b2009-06-02 23:00:53 -04002070 return 1;
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -07002071 }
Steven Rostedtedd813b2009-06-02 23:00:53 -04002072 }
2073
2074 /* could not discard */
2075 return 0;
2076}
2077
Steven Rostedtfa743952009-06-16 12:37:57 -04002078static void rb_start_commit(struct ring_buffer_per_cpu *cpu_buffer)
2079{
2080 local_inc(&cpu_buffer->committing);
2081 local_inc(&cpu_buffer->commits);
2082}
2083
Steven Rostedtd9abde22010-10-19 13:17:08 -04002084static inline void rb_end_commit(struct ring_buffer_per_cpu *cpu_buffer)
Steven Rostedtfa743952009-06-16 12:37:57 -04002085{
2086 unsigned long commits;
2087
2088 if (RB_WARN_ON(cpu_buffer,
2089 !local_read(&cpu_buffer->committing)))
2090 return;
2091
2092 again:
2093 commits = local_read(&cpu_buffer->commits);
2094 /* synchronize with interrupts */
2095 barrier();
2096 if (local_read(&cpu_buffer->committing) == 1)
2097 rb_set_commit_to_write(cpu_buffer);
2098
2099 local_dec(&cpu_buffer->committing);
2100
2101 /* synchronize with interrupts */
2102 barrier();
2103
2104 /*
2105 * Need to account for interrupts coming in between the
2106 * updating of the commit page and the clearing of the
2107 * committing counter.
2108 */
2109 if (unlikely(local_read(&cpu_buffer->commits) != commits) &&
2110 !local_read(&cpu_buffer->committing)) {
2111 local_inc(&cpu_buffer->committing);
2112 goto again;
2113 }
2114}
2115
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002116static struct ring_buffer_event *
Steven Rostedt62f0b3e2009-09-04 14:11:34 -04002117rb_reserve_next_event(struct ring_buffer *buffer,
2118 struct ring_buffer_per_cpu *cpu_buffer,
Steven Rostedt1cd8d732009-05-11 14:08:09 -04002119 unsigned long length)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002120{
2121 struct ring_buffer_event *event;
Steven Rostedt69d1b832010-10-07 18:18:05 -04002122 u64 ts, delta;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002123 int nr_loops = 0;
Steven Rostedt69d1b832010-10-07 18:18:05 -04002124 int add_timestamp;
Steven Rostedt140ff892010-10-08 10:50:30 -04002125 u64 diff;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002126
Steven Rostedtfa743952009-06-16 12:37:57 -04002127 rb_start_commit(cpu_buffer);
2128
Steven Rostedt85bac322009-09-04 14:24:40 -04002129#ifdef CONFIG_RING_BUFFER_ALLOW_SWAP
Steven Rostedt62f0b3e2009-09-04 14:11:34 -04002130 /*
2131 * Due to the ability to swap a cpu buffer from a buffer
2132 * it is possible it was swapped before we committed.
2133 * (committing stops a swap). We check for it here and
2134 * if it happened, we have to fail the write.
2135 */
2136 barrier();
2137 if (unlikely(ACCESS_ONCE(cpu_buffer->buffer) != buffer)) {
2138 local_dec(&cpu_buffer->committing);
2139 local_dec(&cpu_buffer->commits);
2140 return NULL;
2141 }
Steven Rostedt85bac322009-09-04 14:24:40 -04002142#endif
Steven Rostedt62f0b3e2009-09-04 14:11:34 -04002143
Steven Rostedtbe957c42009-05-11 14:42:53 -04002144 length = rb_calculate_event_length(length);
Steven Rostedtbf41a152008-10-04 02:00:59 -04002145 again:
Steven Rostedt69d1b832010-10-07 18:18:05 -04002146 add_timestamp = 0;
2147 delta = 0;
2148
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002149 /*
2150 * We allow for interrupts to reenter here and do a trace.
2151 * If one does, it will cause this original code to loop
2152 * back here. Even with heavy interrupts happening, this
2153 * should only happen a few times in a row. If this happens
2154 * 1000 times in a row, there must be either an interrupt
2155 * storm or we have something buggy.
2156 * Bail!
2157 */
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05002158 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 1000))
Steven Rostedtfa743952009-06-16 12:37:57 -04002159 goto out_fail;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002160
Jiri Olsa6d3f1e12009-10-23 19:36:19 -04002161 ts = rb_time_stamp(cpu_buffer->buffer);
Steven Rostedt140ff892010-10-08 10:50:30 -04002162 diff = ts - cpu_buffer->write_stamp;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002163
Steven Rostedt140ff892010-10-08 10:50:30 -04002164 /* make sure this diff is calculated here */
2165 barrier();
Steven Rostedtbf41a152008-10-04 02:00:59 -04002166
Steven Rostedt140ff892010-10-08 10:50:30 -04002167 /* Did the write stamp get updated already? */
2168 if (likely(ts >= cpu_buffer->write_stamp)) {
Steven Rostedt168b6b12009-05-11 22:11:05 -04002169 delta = diff;
2170 if (unlikely(test_time_stamp(delta))) {
Jiri Olsa31274d72011-02-18 15:52:19 +01002171 int local_clock_stable = 1;
2172#ifdef CONFIG_HAVE_UNSTABLE_SCHED_CLOCK
2173 local_clock_stable = sched_clock_stable;
2174#endif
Steven Rostedt69d1b832010-10-07 18:18:05 -04002175 WARN_ONCE(delta > (1ULL << 59),
Jiri Olsa31274d72011-02-18 15:52:19 +01002176 KERN_WARNING "Delta way too big! %llu ts=%llu write stamp = %llu\n%s",
Steven Rostedt69d1b832010-10-07 18:18:05 -04002177 (unsigned long long)delta,
2178 (unsigned long long)ts,
Jiri Olsa31274d72011-02-18 15:52:19 +01002179 (unsigned long long)cpu_buffer->write_stamp,
2180 local_clock_stable ? "" :
2181 "If you just came from a suspend/resume,\n"
2182 "please switch to the trace global clock:\n"
2183 " echo global > /sys/kernel/debug/tracing/trace_clock\n");
Steven Rostedt69d1b832010-10-07 18:18:05 -04002184 add_timestamp = 1;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002185 }
Steven Rostedt168b6b12009-05-11 22:11:05 -04002186 }
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002187
Steven Rostedt69d1b832010-10-07 18:18:05 -04002188 event = __rb_reserve_next(cpu_buffer, length, ts,
2189 delta, add_timestamp);
Steven Rostedt168b6b12009-05-11 22:11:05 -04002190 if (unlikely(PTR_ERR(event) == -EAGAIN))
Steven Rostedtbf41a152008-10-04 02:00:59 -04002191 goto again;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002192
Steven Rostedtfa743952009-06-16 12:37:57 -04002193 if (!event)
2194 goto out_fail;
Steven Rostedtbf41a152008-10-04 02:00:59 -04002195
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002196 return event;
Steven Rostedtfa743952009-06-16 12:37:57 -04002197
2198 out_fail:
2199 rb_end_commit(cpu_buffer);
2200 return NULL;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002201}
2202
Paul Mundt1155de42009-06-25 14:30:12 +09002203#ifdef CONFIG_TRACING
2204
Steven Rostedtaa18efb2009-04-20 16:16:11 -04002205#define TRACE_RECURSIVE_DEPTH 16
Steven Rostedt261842b2009-04-16 21:41:52 -04002206
Steven Rostedtd9abde22010-10-19 13:17:08 -04002207/* Keep this code out of the fast path cache */
2208static noinline void trace_recursive_fail(void)
Steven Rostedt261842b2009-04-16 21:41:52 -04002209{
Steven Rostedtaa18efb2009-04-20 16:16:11 -04002210 /* Disable all tracing before we do anything else */
2211 tracing_off_permanent();
Frederic Weisbeckere057a5e2009-04-19 23:38:12 +02002212
Steven Rostedt7d7d2b82009-04-27 12:37:49 -04002213 printk_once(KERN_WARNING "Tracing recursion: depth[%ld]:"
Steven Rostedtaa18efb2009-04-20 16:16:11 -04002214 "HC[%lu]:SC[%lu]:NMI[%lu]\n",
Steven Rostedtb1cff0a2011-05-25 14:27:43 -04002215 trace_recursion_buffer(),
Steven Rostedtaa18efb2009-04-20 16:16:11 -04002216 hardirq_count() >> HARDIRQ_SHIFT,
2217 softirq_count() >> SOFTIRQ_SHIFT,
2218 in_nmi());
Frederic Weisbeckere057a5e2009-04-19 23:38:12 +02002219
Steven Rostedtaa18efb2009-04-20 16:16:11 -04002220 WARN_ON_ONCE(1);
Steven Rostedtd9abde22010-10-19 13:17:08 -04002221}
2222
2223static inline int trace_recursive_lock(void)
2224{
Steven Rostedtb1cff0a2011-05-25 14:27:43 -04002225 trace_recursion_inc();
Steven Rostedtd9abde22010-10-19 13:17:08 -04002226
Steven Rostedtb1cff0a2011-05-25 14:27:43 -04002227 if (likely(trace_recursion_buffer() < TRACE_RECURSIVE_DEPTH))
Steven Rostedtd9abde22010-10-19 13:17:08 -04002228 return 0;
2229
2230 trace_recursive_fail();
2231
Steven Rostedtaa18efb2009-04-20 16:16:11 -04002232 return -1;
Steven Rostedt261842b2009-04-16 21:41:52 -04002233}
2234
Steven Rostedtd9abde22010-10-19 13:17:08 -04002235static inline void trace_recursive_unlock(void)
Steven Rostedt261842b2009-04-16 21:41:52 -04002236{
Steven Rostedtb1cff0a2011-05-25 14:27:43 -04002237 WARN_ON_ONCE(!trace_recursion_buffer());
Steven Rostedt261842b2009-04-16 21:41:52 -04002238
Steven Rostedtb1cff0a2011-05-25 14:27:43 -04002239 trace_recursion_dec();
Steven Rostedt261842b2009-04-16 21:41:52 -04002240}
2241
Paul Mundt1155de42009-06-25 14:30:12 +09002242#else
2243
2244#define trace_recursive_lock() (0)
2245#define trace_recursive_unlock() do { } while (0)
2246
2247#endif
2248
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002249/**
2250 * ring_buffer_lock_reserve - reserve a part of the buffer
2251 * @buffer: the ring buffer to reserve from
2252 * @length: the length of the data to reserve (excluding event header)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002253 *
2254 * Returns a reseverd event on the ring buffer to copy directly to.
2255 * The user of this interface will need to get the body to write into
2256 * and can use the ring_buffer_event_data() interface.
2257 *
2258 * The length is the length of the data needed, not the event length
2259 * which also includes the event header.
2260 *
2261 * Must be paired with ring_buffer_unlock_commit, unless NULL is returned.
2262 * If NULL is returned, then nothing has been allocated or locked.
2263 */
2264struct ring_buffer_event *
Arnaldo Carvalho de Melo0a987752009-02-05 16:12:56 -02002265ring_buffer_lock_reserve(struct ring_buffer *buffer, unsigned long length)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002266{
2267 struct ring_buffer_per_cpu *cpu_buffer;
2268 struct ring_buffer_event *event;
Steven Rostedt5168ae52010-06-03 09:36:50 -04002269 int cpu;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002270
Steven Rostedt033601a2008-11-21 12:41:55 -05002271 if (ring_buffer_flags != RB_BUFFERS_ON)
Steven Rostedta3583242008-11-11 15:01:42 -05002272 return NULL;
2273
Steven Rostedtbf41a152008-10-04 02:00:59 -04002274 /* If we are tracing schedule, we don't want to recurse */
Steven Rostedt5168ae52010-06-03 09:36:50 -04002275 preempt_disable_notrace();
Steven Rostedtbf41a152008-10-04 02:00:59 -04002276
Lai Jiangshan52fbe9c2010-03-08 14:50:43 +08002277 if (atomic_read(&buffer->record_disabled))
2278 goto out_nocheck;
2279
Steven Rostedt261842b2009-04-16 21:41:52 -04002280 if (trace_recursive_lock())
2281 goto out_nocheck;
2282
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002283 cpu = raw_smp_processor_id();
2284
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302285 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedtd7690412008-10-01 00:29:53 -04002286 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002287
2288 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002289
2290 if (atomic_read(&cpu_buffer->record_disabled))
Steven Rostedtd7690412008-10-01 00:29:53 -04002291 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002292
Steven Rostedtbe957c42009-05-11 14:42:53 -04002293 if (length > BUF_MAX_DATA_SIZE)
Steven Rostedtbf41a152008-10-04 02:00:59 -04002294 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002295
Steven Rostedt62f0b3e2009-09-04 14:11:34 -04002296 event = rb_reserve_next_event(buffer, cpu_buffer, length);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002297 if (!event)
Steven Rostedtd7690412008-10-01 00:29:53 -04002298 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002299
2300 return event;
2301
Steven Rostedtd7690412008-10-01 00:29:53 -04002302 out:
Steven Rostedt261842b2009-04-16 21:41:52 -04002303 trace_recursive_unlock();
2304
2305 out_nocheck:
Steven Rostedt5168ae52010-06-03 09:36:50 -04002306 preempt_enable_notrace();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002307 return NULL;
2308}
Robert Richterc4f50182008-12-11 16:49:22 +01002309EXPORT_SYMBOL_GPL(ring_buffer_lock_reserve);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002310
Steven Rostedta1863c22009-09-03 10:23:58 -04002311static void
2312rb_update_write_stamp(struct ring_buffer_per_cpu *cpu_buffer,
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002313 struct ring_buffer_event *event)
2314{
Steven Rostedt69d1b832010-10-07 18:18:05 -04002315 u64 delta;
2316
Steven Rostedtfa743952009-06-16 12:37:57 -04002317 /*
2318 * The event first in the commit queue updates the
2319 * time stamp.
2320 */
Steven Rostedt69d1b832010-10-07 18:18:05 -04002321 if (rb_event_is_commit(cpu_buffer, event)) {
2322 /*
2323 * A commit event that is first on a page
2324 * updates the write timestamp with the page stamp
2325 */
2326 if (!rb_event_index(event))
2327 cpu_buffer->write_stamp =
2328 cpu_buffer->commit_page->page->time_stamp;
2329 else if (event->type_len == RINGBUF_TYPE_TIME_EXTEND) {
2330 delta = event->array[0];
2331 delta <<= TS_SHIFT;
2332 delta += event->time_delta;
2333 cpu_buffer->write_stamp += delta;
2334 } else
2335 cpu_buffer->write_stamp += event->time_delta;
2336 }
Steven Rostedta1863c22009-09-03 10:23:58 -04002337}
Steven Rostedtbf41a152008-10-04 02:00:59 -04002338
Steven Rostedta1863c22009-09-03 10:23:58 -04002339static void rb_commit(struct ring_buffer_per_cpu *cpu_buffer,
2340 struct ring_buffer_event *event)
2341{
2342 local_inc(&cpu_buffer->entries);
2343 rb_update_write_stamp(cpu_buffer, event);
Steven Rostedtfa743952009-06-16 12:37:57 -04002344 rb_end_commit(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002345}
2346
2347/**
2348 * ring_buffer_unlock_commit - commit a reserved
2349 * @buffer: The buffer to commit to
2350 * @event: The event pointer to commit.
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002351 *
2352 * This commits the data to the ring buffer, and releases any locks held.
2353 *
2354 * Must be paired with ring_buffer_lock_reserve.
2355 */
2356int ring_buffer_unlock_commit(struct ring_buffer *buffer,
Arnaldo Carvalho de Melo0a987752009-02-05 16:12:56 -02002357 struct ring_buffer_event *event)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002358{
2359 struct ring_buffer_per_cpu *cpu_buffer;
2360 int cpu = raw_smp_processor_id();
2361
2362 cpu_buffer = buffer->buffers[cpu];
2363
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002364 rb_commit(cpu_buffer, event);
2365
Steven Rostedt261842b2009-04-16 21:41:52 -04002366 trace_recursive_unlock();
2367
Steven Rostedt5168ae52010-06-03 09:36:50 -04002368 preempt_enable_notrace();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002369
2370 return 0;
2371}
Robert Richterc4f50182008-12-11 16:49:22 +01002372EXPORT_SYMBOL_GPL(ring_buffer_unlock_commit);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002373
Frederic Weisbeckerf3b9aae2009-04-19 23:39:33 +02002374static inline void rb_event_discard(struct ring_buffer_event *event)
2375{
Steven Rostedt69d1b832010-10-07 18:18:05 -04002376 if (event->type_len == RINGBUF_TYPE_TIME_EXTEND)
2377 event = skip_time_extend(event);
2378
Lai Jiangshan334d4162009-04-24 11:27:05 +08002379 /* array[0] holds the actual length for the discarded event */
2380 event->array[0] = rb_event_data_length(event) - RB_EVNT_HDR_SIZE;
2381 event->type_len = RINGBUF_TYPE_PADDING;
Frederic Weisbeckerf3b9aae2009-04-19 23:39:33 +02002382 /* time delta must be non zero */
2383 if (!event->time_delta)
2384 event->time_delta = 1;
2385}
2386
Steven Rostedta1863c22009-09-03 10:23:58 -04002387/*
2388 * Decrement the entries to the page that an event is on.
2389 * The event does not even need to exist, only the pointer
2390 * to the page it is on. This may only be called before the commit
2391 * takes place.
2392 */
2393static inline void
2394rb_decrement_entry(struct ring_buffer_per_cpu *cpu_buffer,
2395 struct ring_buffer_event *event)
2396{
2397 unsigned long addr = (unsigned long)event;
2398 struct buffer_page *bpage = cpu_buffer->commit_page;
2399 struct buffer_page *start;
2400
2401 addr &= PAGE_MASK;
2402
2403 /* Do the likely case first */
2404 if (likely(bpage->page == (void *)addr)) {
2405 local_dec(&bpage->entries);
2406 return;
2407 }
2408
2409 /*
2410 * Because the commit page may be on the reader page we
2411 * start with the next page and check the end loop there.
2412 */
2413 rb_inc_page(cpu_buffer, &bpage);
2414 start = bpage;
2415 do {
2416 if (bpage->page == (void *)addr) {
2417 local_dec(&bpage->entries);
2418 return;
2419 }
2420 rb_inc_page(cpu_buffer, &bpage);
2421 } while (bpage != start);
2422
2423 /* commit not part of this buffer?? */
2424 RB_WARN_ON(cpu_buffer, 1);
2425}
2426
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002427/**
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002428 * ring_buffer_commit_discard - discard an event that has not been committed
2429 * @buffer: the ring buffer
2430 * @event: non committed event to discard
2431 *
Steven Rostedtdc892f72009-09-03 15:33:41 -04002432 * Sometimes an event that is in the ring buffer needs to be ignored.
2433 * This function lets the user discard an event in the ring buffer
2434 * and then that event will not be read later.
2435 *
2436 * This function only works if it is called before the the item has been
2437 * committed. It will try to free the event from the ring buffer
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002438 * if another event has not been added behind it.
2439 *
2440 * If another event has been added behind it, it will set the event
2441 * up as discarded, and perform the commit.
2442 *
2443 * If this function is called, do not call ring_buffer_unlock_commit on
2444 * the event.
2445 */
2446void ring_buffer_discard_commit(struct ring_buffer *buffer,
2447 struct ring_buffer_event *event)
2448{
2449 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002450 int cpu;
2451
2452 /* The event is discarded regardless */
Frederic Weisbeckerf3b9aae2009-04-19 23:39:33 +02002453 rb_event_discard(event);
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002454
Steven Rostedtfa743952009-06-16 12:37:57 -04002455 cpu = smp_processor_id();
2456 cpu_buffer = buffer->buffers[cpu];
2457
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002458 /*
2459 * This must only be called if the event has not been
2460 * committed yet. Thus we can assume that preemption
2461 * is still disabled.
2462 */
Steven Rostedtfa743952009-06-16 12:37:57 -04002463 RB_WARN_ON(buffer, !local_read(&cpu_buffer->committing));
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002464
Steven Rostedta1863c22009-09-03 10:23:58 -04002465 rb_decrement_entry(cpu_buffer, event);
Steven Rostedt0f2541d2009-08-05 12:02:48 -04002466 if (rb_try_to_discard(cpu_buffer, event))
Steven Rostedtedd813b2009-06-02 23:00:53 -04002467 goto out;
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002468
2469 /*
2470 * The commit is still visible by the reader, so we
Steven Rostedta1863c22009-09-03 10:23:58 -04002471 * must still update the timestamp.
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002472 */
Steven Rostedta1863c22009-09-03 10:23:58 -04002473 rb_update_write_stamp(cpu_buffer, event);
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002474 out:
Steven Rostedtfa743952009-06-16 12:37:57 -04002475 rb_end_commit(cpu_buffer);
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002476
Frederic Weisbeckerf3b9aae2009-04-19 23:39:33 +02002477 trace_recursive_unlock();
2478
Steven Rostedt5168ae52010-06-03 09:36:50 -04002479 preempt_enable_notrace();
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002480
2481}
2482EXPORT_SYMBOL_GPL(ring_buffer_discard_commit);
2483
2484/**
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002485 * ring_buffer_write - write data to the buffer without reserving
2486 * @buffer: The ring buffer to write to.
2487 * @length: The length of the data being written (excluding the event header)
2488 * @data: The data to write to the buffer.
2489 *
2490 * This is like ring_buffer_lock_reserve and ring_buffer_unlock_commit as
2491 * one function. If you already have the data to write to the buffer, it
2492 * may be easier to simply call this function.
2493 *
2494 * Note, like ring_buffer_lock_reserve, the length is the length of the data
2495 * and not the length of the event which would hold the header.
2496 */
2497int ring_buffer_write(struct ring_buffer *buffer,
2498 unsigned long length,
2499 void *data)
2500{
2501 struct ring_buffer_per_cpu *cpu_buffer;
2502 struct ring_buffer_event *event;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002503 void *body;
2504 int ret = -EBUSY;
Steven Rostedt5168ae52010-06-03 09:36:50 -04002505 int cpu;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002506
Steven Rostedt033601a2008-11-21 12:41:55 -05002507 if (ring_buffer_flags != RB_BUFFERS_ON)
Steven Rostedta3583242008-11-11 15:01:42 -05002508 return -EBUSY;
2509
Steven Rostedt5168ae52010-06-03 09:36:50 -04002510 preempt_disable_notrace();
Steven Rostedtbf41a152008-10-04 02:00:59 -04002511
Lai Jiangshan52fbe9c2010-03-08 14:50:43 +08002512 if (atomic_read(&buffer->record_disabled))
2513 goto out;
2514
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002515 cpu = raw_smp_processor_id();
2516
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302517 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedtd7690412008-10-01 00:29:53 -04002518 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002519
2520 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002521
2522 if (atomic_read(&cpu_buffer->record_disabled))
2523 goto out;
2524
Steven Rostedtbe957c42009-05-11 14:42:53 -04002525 if (length > BUF_MAX_DATA_SIZE)
2526 goto out;
2527
Steven Rostedt62f0b3e2009-09-04 14:11:34 -04002528 event = rb_reserve_next_event(buffer, cpu_buffer, length);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002529 if (!event)
2530 goto out;
2531
2532 body = rb_event_data(event);
2533
2534 memcpy(body, data, length);
2535
2536 rb_commit(cpu_buffer, event);
2537
2538 ret = 0;
2539 out:
Steven Rostedt5168ae52010-06-03 09:36:50 -04002540 preempt_enable_notrace();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002541
2542 return ret;
2543}
Robert Richterc4f50182008-12-11 16:49:22 +01002544EXPORT_SYMBOL_GPL(ring_buffer_write);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002545
Andrew Morton34a148b2009-01-09 12:27:09 -08002546static int rb_per_cpu_empty(struct ring_buffer_per_cpu *cpu_buffer)
Steven Rostedtbf41a152008-10-04 02:00:59 -04002547{
2548 struct buffer_page *reader = cpu_buffer->reader_page;
Steven Rostedt77ae3652009-03-27 11:00:29 -04002549 struct buffer_page *head = rb_set_head_page(cpu_buffer);
Steven Rostedtbf41a152008-10-04 02:00:59 -04002550 struct buffer_page *commit = cpu_buffer->commit_page;
2551
Steven Rostedt77ae3652009-03-27 11:00:29 -04002552 /* In case of error, head will be NULL */
2553 if (unlikely(!head))
2554 return 1;
2555
Steven Rostedtbf41a152008-10-04 02:00:59 -04002556 return reader->read == rb_page_commit(reader) &&
2557 (commit == reader ||
2558 (commit == head &&
2559 head->read == rb_page_commit(commit)));
2560}
2561
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002562/**
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002563 * ring_buffer_record_disable - stop all writes into the buffer
2564 * @buffer: The ring buffer to stop writes to.
2565 *
2566 * This prevents all writes to the buffer. Any attempt to write
2567 * to the buffer after this will fail and return NULL.
2568 *
2569 * The caller should call synchronize_sched() after this.
2570 */
2571void ring_buffer_record_disable(struct ring_buffer *buffer)
2572{
2573 atomic_inc(&buffer->record_disabled);
2574}
Robert Richterc4f50182008-12-11 16:49:22 +01002575EXPORT_SYMBOL_GPL(ring_buffer_record_disable);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002576
2577/**
2578 * ring_buffer_record_enable - enable writes to the buffer
2579 * @buffer: The ring buffer to enable writes
2580 *
2581 * Note, multiple disables will need the same number of enables
Adam Buchbinderc41b20e2009-12-11 16:35:39 -05002582 * to truly enable the writing (much like preempt_disable).
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002583 */
2584void ring_buffer_record_enable(struct ring_buffer *buffer)
2585{
2586 atomic_dec(&buffer->record_disabled);
2587}
Robert Richterc4f50182008-12-11 16:49:22 +01002588EXPORT_SYMBOL_GPL(ring_buffer_record_enable);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002589
2590/**
Steven Rostedt499e5472012-02-22 15:50:28 -05002591 * ring_buffer_record_off - stop all writes into the buffer
2592 * @buffer: The ring buffer to stop writes to.
2593 *
2594 * This prevents all writes to the buffer. Any attempt to write
2595 * to the buffer after this will fail and return NULL.
2596 *
2597 * This is different than ring_buffer_record_disable() as
2598 * it works like an on/off switch, where as the disable() verison
2599 * must be paired with a enable().
2600 */
2601void ring_buffer_record_off(struct ring_buffer *buffer)
2602{
2603 unsigned int rd;
2604 unsigned int new_rd;
2605
2606 do {
2607 rd = atomic_read(&buffer->record_disabled);
2608 new_rd = rd | RB_BUFFER_OFF;
2609 } while (atomic_cmpxchg(&buffer->record_disabled, rd, new_rd) != rd);
2610}
2611EXPORT_SYMBOL_GPL(ring_buffer_record_off);
2612
2613/**
2614 * ring_buffer_record_on - restart writes into the buffer
2615 * @buffer: The ring buffer to start writes to.
2616 *
2617 * This enables all writes to the buffer that was disabled by
2618 * ring_buffer_record_off().
2619 *
2620 * This is different than ring_buffer_record_enable() as
2621 * it works like an on/off switch, where as the enable() verison
2622 * must be paired with a disable().
2623 */
2624void ring_buffer_record_on(struct ring_buffer *buffer)
2625{
2626 unsigned int rd;
2627 unsigned int new_rd;
2628
2629 do {
2630 rd = atomic_read(&buffer->record_disabled);
2631 new_rd = rd & ~RB_BUFFER_OFF;
2632 } while (atomic_cmpxchg(&buffer->record_disabled, rd, new_rd) != rd);
2633}
2634EXPORT_SYMBOL_GPL(ring_buffer_record_on);
2635
2636/**
2637 * ring_buffer_record_is_on - return true if the ring buffer can write
2638 * @buffer: The ring buffer to see if write is enabled
2639 *
2640 * Returns true if the ring buffer is in a state that it accepts writes.
2641 */
2642int ring_buffer_record_is_on(struct ring_buffer *buffer)
2643{
2644 return !atomic_read(&buffer->record_disabled);
2645}
2646
2647/**
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002648 * ring_buffer_record_disable_cpu - stop all writes into the cpu_buffer
2649 * @buffer: The ring buffer to stop writes to.
2650 * @cpu: The CPU buffer to stop
2651 *
2652 * This prevents all writes to the buffer. Any attempt to write
2653 * to the buffer after this will fail and return NULL.
2654 *
2655 * The caller should call synchronize_sched() after this.
2656 */
2657void ring_buffer_record_disable_cpu(struct ring_buffer *buffer, int cpu)
2658{
2659 struct ring_buffer_per_cpu *cpu_buffer;
2660
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302661 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04002662 return;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002663
2664 cpu_buffer = buffer->buffers[cpu];
2665 atomic_inc(&cpu_buffer->record_disabled);
2666}
Robert Richterc4f50182008-12-11 16:49:22 +01002667EXPORT_SYMBOL_GPL(ring_buffer_record_disable_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002668
2669/**
2670 * ring_buffer_record_enable_cpu - enable writes to the buffer
2671 * @buffer: The ring buffer to enable writes
2672 * @cpu: The CPU to enable.
2673 *
2674 * Note, multiple disables will need the same number of enables
Adam Buchbinderc41b20e2009-12-11 16:35:39 -05002675 * to truly enable the writing (much like preempt_disable).
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002676 */
2677void ring_buffer_record_enable_cpu(struct ring_buffer *buffer, int cpu)
2678{
2679 struct ring_buffer_per_cpu *cpu_buffer;
2680
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302681 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04002682 return;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002683
2684 cpu_buffer = buffer->buffers[cpu];
2685 atomic_dec(&cpu_buffer->record_disabled);
2686}
Robert Richterc4f50182008-12-11 16:49:22 +01002687EXPORT_SYMBOL_GPL(ring_buffer_record_enable_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002688
Steven Rostedtf6195aa2010-09-01 12:23:12 -04002689/*
2690 * The total entries in the ring buffer is the running counter
2691 * of entries entered into the ring buffer, minus the sum of
2692 * the entries read from the ring buffer and the number of
2693 * entries that were overwritten.
2694 */
2695static inline unsigned long
2696rb_num_of_entries(struct ring_buffer_per_cpu *cpu_buffer)
2697{
2698 return local_read(&cpu_buffer->entries) -
2699 (local_read(&cpu_buffer->overrun) + cpu_buffer->read);
2700}
2701
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002702/**
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -07002703 * ring_buffer_oldest_event_ts - get the oldest event timestamp from the buffer
2704 * @buffer: The ring buffer
2705 * @cpu: The per CPU buffer to read from.
2706 */
2707unsigned long ring_buffer_oldest_event_ts(struct ring_buffer *buffer, int cpu)
2708{
2709 unsigned long flags;
2710 struct ring_buffer_per_cpu *cpu_buffer;
2711 struct buffer_page *bpage;
Steven Rostedt4d7981b2012-11-29 22:31:16 -05002712 unsigned long ret = 0;
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -07002713
2714 if (!cpumask_test_cpu(cpu, buffer->cpumask))
2715 return 0;
2716
2717 cpu_buffer = buffer->buffers[cpu];
Linus Torvalds7115e3f2011-10-26 17:03:38 +02002718 raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -07002719 /*
2720 * if the tail is on reader_page, oldest time stamp is on the reader
2721 * page
2722 */
2723 if (cpu_buffer->tail_page == cpu_buffer->reader_page)
2724 bpage = cpu_buffer->reader_page;
2725 else
2726 bpage = rb_set_head_page(cpu_buffer);
Steven Rostedt4d7981b2012-11-29 22:31:16 -05002727 if (bpage)
2728 ret = bpage->page->time_stamp;
Linus Torvalds7115e3f2011-10-26 17:03:38 +02002729 raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -07002730
2731 return ret;
2732}
2733EXPORT_SYMBOL_GPL(ring_buffer_oldest_event_ts);
2734
2735/**
2736 * ring_buffer_bytes_cpu - get the number of bytes consumed in a cpu buffer
2737 * @buffer: The ring buffer
2738 * @cpu: The per CPU buffer to read from.
2739 */
2740unsigned long ring_buffer_bytes_cpu(struct ring_buffer *buffer, int cpu)
2741{
2742 struct ring_buffer_per_cpu *cpu_buffer;
2743 unsigned long ret;
2744
2745 if (!cpumask_test_cpu(cpu, buffer->cpumask))
2746 return 0;
2747
2748 cpu_buffer = buffer->buffers[cpu];
2749 ret = local_read(&cpu_buffer->entries_bytes) - cpu_buffer->read_bytes;
2750
2751 return ret;
2752}
2753EXPORT_SYMBOL_GPL(ring_buffer_bytes_cpu);
2754
2755/**
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002756 * ring_buffer_entries_cpu - get the number of entries in a cpu buffer
2757 * @buffer: The ring buffer
2758 * @cpu: The per CPU buffer to get the entries from.
2759 */
2760unsigned long ring_buffer_entries_cpu(struct ring_buffer *buffer, int cpu)
2761{
2762 struct ring_buffer_per_cpu *cpu_buffer;
2763
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302764 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04002765 return 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002766
2767 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt554f7862009-03-11 22:00:13 -04002768
Steven Rostedtf6195aa2010-09-01 12:23:12 -04002769 return rb_num_of_entries(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002770}
Robert Richterc4f50182008-12-11 16:49:22 +01002771EXPORT_SYMBOL_GPL(ring_buffer_entries_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002772
2773/**
2774 * ring_buffer_overrun_cpu - get the number of overruns in a cpu_buffer
2775 * @buffer: The ring buffer
2776 * @cpu: The per CPU buffer to get the number of overruns from
2777 */
2778unsigned long ring_buffer_overrun_cpu(struct ring_buffer *buffer, int cpu)
2779{
2780 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedt8aabee52009-03-12 13:13:49 -04002781 unsigned long ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002782
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302783 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04002784 return 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002785
2786 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt77ae3652009-03-27 11:00:29 -04002787 ret = local_read(&cpu_buffer->overrun);
Steven Rostedt554f7862009-03-11 22:00:13 -04002788
2789 return ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002790}
Robert Richterc4f50182008-12-11 16:49:22 +01002791EXPORT_SYMBOL_GPL(ring_buffer_overrun_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002792
2793/**
Steven Rostedtf0d2c682009-04-29 13:43:37 -04002794 * ring_buffer_commit_overrun_cpu - get the number of overruns caused by commits
2795 * @buffer: The ring buffer
2796 * @cpu: The per CPU buffer to get the number of overruns from
2797 */
2798unsigned long
2799ring_buffer_commit_overrun_cpu(struct ring_buffer *buffer, int cpu)
2800{
2801 struct ring_buffer_per_cpu *cpu_buffer;
2802 unsigned long ret;
2803
2804 if (!cpumask_test_cpu(cpu, buffer->cpumask))
2805 return 0;
2806
2807 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt77ae3652009-03-27 11:00:29 -04002808 ret = local_read(&cpu_buffer->commit_overrun);
Steven Rostedtf0d2c682009-04-29 13:43:37 -04002809
2810 return ret;
2811}
2812EXPORT_SYMBOL_GPL(ring_buffer_commit_overrun_cpu);
2813
2814/**
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002815 * ring_buffer_entries - get the number of entries in a buffer
2816 * @buffer: The ring buffer
2817 *
2818 * Returns the total number of entries in the ring buffer
2819 * (all CPU entries)
2820 */
2821unsigned long ring_buffer_entries(struct ring_buffer *buffer)
2822{
2823 struct ring_buffer_per_cpu *cpu_buffer;
2824 unsigned long entries = 0;
2825 int cpu;
2826
2827 /* if you care about this being correct, lock the buffer */
2828 for_each_buffer_cpu(buffer, cpu) {
2829 cpu_buffer = buffer->buffers[cpu];
Steven Rostedtf6195aa2010-09-01 12:23:12 -04002830 entries += rb_num_of_entries(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002831 }
2832
2833 return entries;
2834}
Robert Richterc4f50182008-12-11 16:49:22 +01002835EXPORT_SYMBOL_GPL(ring_buffer_entries);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002836
2837/**
Jiri Olsa67b394f2009-10-23 19:36:18 -04002838 * ring_buffer_overruns - get the number of overruns in buffer
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002839 * @buffer: The ring buffer
2840 *
2841 * Returns the total number of overruns in the ring buffer
2842 * (all CPU entries)
2843 */
2844unsigned long ring_buffer_overruns(struct ring_buffer *buffer)
2845{
2846 struct ring_buffer_per_cpu *cpu_buffer;
2847 unsigned long overruns = 0;
2848 int cpu;
2849
2850 /* if you care about this being correct, lock the buffer */
2851 for_each_buffer_cpu(buffer, cpu) {
2852 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt77ae3652009-03-27 11:00:29 -04002853 overruns += local_read(&cpu_buffer->overrun);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002854 }
2855
2856 return overruns;
2857}
Robert Richterc4f50182008-12-11 16:49:22 +01002858EXPORT_SYMBOL_GPL(ring_buffer_overruns);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002859
Steven Rostedt642edba2008-11-12 00:01:26 -05002860static void rb_iter_reset(struct ring_buffer_iter *iter)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002861{
2862 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
2863
Steven Rostedtd7690412008-10-01 00:29:53 -04002864 /* Iterator usage is expected to have record disabled */
2865 if (list_empty(&cpu_buffer->reader_page->list)) {
Steven Rostedt77ae3652009-03-27 11:00:29 -04002866 iter->head_page = rb_set_head_page(cpu_buffer);
2867 if (unlikely(!iter->head_page))
2868 return;
2869 iter->head = iter->head_page->read;
Steven Rostedtd7690412008-10-01 00:29:53 -04002870 } else {
2871 iter->head_page = cpu_buffer->reader_page;
Steven Rostedt6f807ac2008-10-04 02:00:58 -04002872 iter->head = cpu_buffer->reader_page->read;
Steven Rostedtd7690412008-10-01 00:29:53 -04002873 }
2874 if (iter->head)
2875 iter->read_stamp = cpu_buffer->read_stamp;
2876 else
Steven Rostedtabc9b562008-12-02 15:34:06 -05002877 iter->read_stamp = iter->head_page->page->time_stamp;
Steven Rostedt492a74f2010-01-25 15:17:47 -05002878 iter->cache_reader_page = cpu_buffer->reader_page;
2879 iter->cache_read = cpu_buffer->read;
Steven Rostedt642edba2008-11-12 00:01:26 -05002880}
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002881
Steven Rostedt642edba2008-11-12 00:01:26 -05002882/**
2883 * ring_buffer_iter_reset - reset an iterator
2884 * @iter: The iterator to reset
2885 *
2886 * Resets the iterator, so that it will start from the beginning
2887 * again.
2888 */
2889void ring_buffer_iter_reset(struct ring_buffer_iter *iter)
2890{
Steven Rostedt554f7862009-03-11 22:00:13 -04002891 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedt642edba2008-11-12 00:01:26 -05002892 unsigned long flags;
2893
Steven Rostedt554f7862009-03-11 22:00:13 -04002894 if (!iter)
2895 return;
2896
2897 cpu_buffer = iter->cpu_buffer;
2898
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02002899 raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
Steven Rostedt642edba2008-11-12 00:01:26 -05002900 rb_iter_reset(iter);
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02002901 raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002902}
Robert Richterc4f50182008-12-11 16:49:22 +01002903EXPORT_SYMBOL_GPL(ring_buffer_iter_reset);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002904
2905/**
2906 * ring_buffer_iter_empty - check if an iterator has no more to read
2907 * @iter: The iterator to check
2908 */
2909int ring_buffer_iter_empty(struct ring_buffer_iter *iter)
2910{
2911 struct ring_buffer_per_cpu *cpu_buffer;
2912
2913 cpu_buffer = iter->cpu_buffer;
2914
Steven Rostedtbf41a152008-10-04 02:00:59 -04002915 return iter->head_page == cpu_buffer->commit_page &&
2916 iter->head == rb_commit_index(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002917}
Robert Richterc4f50182008-12-11 16:49:22 +01002918EXPORT_SYMBOL_GPL(ring_buffer_iter_empty);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002919
2920static void
2921rb_update_read_stamp(struct ring_buffer_per_cpu *cpu_buffer,
2922 struct ring_buffer_event *event)
2923{
2924 u64 delta;
2925
Lai Jiangshan334d4162009-04-24 11:27:05 +08002926 switch (event->type_len) {
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002927 case RINGBUF_TYPE_PADDING:
2928 return;
2929
2930 case RINGBUF_TYPE_TIME_EXTEND:
2931 delta = event->array[0];
2932 delta <<= TS_SHIFT;
2933 delta += event->time_delta;
2934 cpu_buffer->read_stamp += delta;
2935 return;
2936
2937 case RINGBUF_TYPE_TIME_STAMP:
2938 /* FIXME: not implemented */
2939 return;
2940
2941 case RINGBUF_TYPE_DATA:
2942 cpu_buffer->read_stamp += event->time_delta;
2943 return;
2944
2945 default:
2946 BUG();
2947 }
2948 return;
2949}
2950
2951static void
2952rb_update_iter_read_stamp(struct ring_buffer_iter *iter,
2953 struct ring_buffer_event *event)
2954{
2955 u64 delta;
2956
Lai Jiangshan334d4162009-04-24 11:27:05 +08002957 switch (event->type_len) {
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002958 case RINGBUF_TYPE_PADDING:
2959 return;
2960
2961 case RINGBUF_TYPE_TIME_EXTEND:
2962 delta = event->array[0];
2963 delta <<= TS_SHIFT;
2964 delta += event->time_delta;
2965 iter->read_stamp += delta;
2966 return;
2967
2968 case RINGBUF_TYPE_TIME_STAMP:
2969 /* FIXME: not implemented */
2970 return;
2971
2972 case RINGBUF_TYPE_DATA:
2973 iter->read_stamp += event->time_delta;
2974 return;
2975
2976 default:
2977 BUG();
2978 }
2979 return;
2980}
2981
Steven Rostedtd7690412008-10-01 00:29:53 -04002982static struct buffer_page *
2983rb_get_reader_page(struct ring_buffer_per_cpu *cpu_buffer)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002984{
Steven Rostedtd7690412008-10-01 00:29:53 -04002985 struct buffer_page *reader = NULL;
Steven Rostedt66a8cb92010-03-31 13:21:56 -04002986 unsigned long overwrite;
Steven Rostedtd7690412008-10-01 00:29:53 -04002987 unsigned long flags;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002988 int nr_loops = 0;
Steven Rostedt77ae3652009-03-27 11:00:29 -04002989 int ret;
Steven Rostedtd7690412008-10-01 00:29:53 -04002990
Steven Rostedt3e03fb72008-11-06 00:09:43 -05002991 local_irq_save(flags);
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01002992 arch_spin_lock(&cpu_buffer->lock);
Steven Rostedtd7690412008-10-01 00:29:53 -04002993
2994 again:
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002995 /*
2996 * This should normally only loop twice. But because the
2997 * start of the reader inserts an empty page, it causes
2998 * a case where we will loop three times. There should be no
2999 * reason to loop four times (that I know of).
3000 */
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05003001 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 3)) {
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003002 reader = NULL;
3003 goto out;
3004 }
3005
Steven Rostedtd7690412008-10-01 00:29:53 -04003006 reader = cpu_buffer->reader_page;
3007
3008 /* If there's more to read, return this page */
Steven Rostedtbf41a152008-10-04 02:00:59 -04003009 if (cpu_buffer->reader_page->read < rb_page_size(reader))
Steven Rostedtd7690412008-10-01 00:29:53 -04003010 goto out;
3011
3012 /* Never should we have an index greater than the size */
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05003013 if (RB_WARN_ON(cpu_buffer,
3014 cpu_buffer->reader_page->read > rb_page_size(reader)))
3015 goto out;
Steven Rostedtd7690412008-10-01 00:29:53 -04003016
3017 /* check if we caught up to the tail */
3018 reader = NULL;
Steven Rostedtbf41a152008-10-04 02:00:59 -04003019 if (cpu_buffer->commit_page == cpu_buffer->reader_page)
Steven Rostedtd7690412008-10-01 00:29:53 -04003020 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003021
3022 /*
Steven Rostedtd7690412008-10-01 00:29:53 -04003023 * Reset the reader page to size zero.
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003024 */
Steven Rostedt77ae3652009-03-27 11:00:29 -04003025 local_set(&cpu_buffer->reader_page->write, 0);
3026 local_set(&cpu_buffer->reader_page->entries, 0);
3027 local_set(&cpu_buffer->reader_page->page->commit, 0);
Steven Rostedtff0ff842010-03-31 22:11:42 -04003028 cpu_buffer->reader_page->real_end = 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003029
Steven Rostedt77ae3652009-03-27 11:00:29 -04003030 spin:
3031 /*
3032 * Splice the empty reader page into the list around the head.
3033 */
3034 reader = rb_set_head_page(cpu_buffer);
Steven Rostedt4d7981b2012-11-29 22:31:16 -05003035 if (!reader)
3036 goto out;
Steven Rostedt0e1ff5d2010-01-06 20:40:44 -05003037 cpu_buffer->reader_page->list.next = rb_list_head(reader->list.next);
Steven Rostedtd7690412008-10-01 00:29:53 -04003038 cpu_buffer->reader_page->list.prev = reader->list.prev;
Steven Rostedtbf41a152008-10-04 02:00:59 -04003039
Steven Rostedt3adc54f2009-03-30 15:32:01 -04003040 /*
3041 * cpu_buffer->pages just needs to point to the buffer, it
3042 * has no specific buffer page to point to. Lets move it out
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003043 * of our way so we don't accidentally swap it.
Steven Rostedt3adc54f2009-03-30 15:32:01 -04003044 */
3045 cpu_buffer->pages = reader->list.prev;
3046
Steven Rostedt77ae3652009-03-27 11:00:29 -04003047 /* The reader page will be pointing to the new head */
3048 rb_set_list_to_head(cpu_buffer, &cpu_buffer->reader_page->list);
Steven Rostedtd7690412008-10-01 00:29:53 -04003049
3050 /*
Steven Rostedt66a8cb92010-03-31 13:21:56 -04003051 * We want to make sure we read the overruns after we set up our
3052 * pointers to the next object. The writer side does a
3053 * cmpxchg to cross pages which acts as the mb on the writer
3054 * side. Note, the reader will constantly fail the swap
3055 * while the writer is updating the pointers, so this
3056 * guarantees that the overwrite recorded here is the one we
3057 * want to compare with the last_overrun.
3058 */
3059 smp_mb();
3060 overwrite = local_read(&(cpu_buffer->overrun));
3061
3062 /*
Steven Rostedt77ae3652009-03-27 11:00:29 -04003063 * Here's the tricky part.
3064 *
3065 * We need to move the pointer past the header page.
3066 * But we can only do that if a writer is not currently
3067 * moving it. The page before the header page has the
3068 * flag bit '1' set if it is pointing to the page we want.
3069 * but if the writer is in the process of moving it
3070 * than it will be '2' or already moved '0'.
Steven Rostedtd7690412008-10-01 00:29:53 -04003071 */
Steven Rostedtd7690412008-10-01 00:29:53 -04003072
Steven Rostedt77ae3652009-03-27 11:00:29 -04003073 ret = rb_head_page_replace(reader, cpu_buffer->reader_page);
3074
3075 /*
3076 * If we did not convert it, then we must try again.
3077 */
3078 if (!ret)
3079 goto spin;
3080
3081 /*
3082 * Yeah! We succeeded in replacing the page.
3083 *
3084 * Now make the new head point back to the reader page.
3085 */
David Sharp5ded3dc2010-01-06 17:12:07 -08003086 rb_list_head(reader->list.next)->prev = &cpu_buffer->reader_page->list;
Steven Rostedt77ae3652009-03-27 11:00:29 -04003087 rb_inc_page(cpu_buffer, &cpu_buffer->head_page);
Steven Rostedtd7690412008-10-01 00:29:53 -04003088
3089 /* Finally update the reader page to the new head */
3090 cpu_buffer->reader_page = reader;
Steven Rostedt (Red Hat)0308b202015-11-23 10:35:36 -05003091 cpu_buffer->reader_page->read = 0;
Steven Rostedtd7690412008-10-01 00:29:53 -04003092
Steven Rostedt66a8cb92010-03-31 13:21:56 -04003093 if (overwrite != cpu_buffer->last_overrun) {
3094 cpu_buffer->lost_events = overwrite - cpu_buffer->last_overrun;
3095 cpu_buffer->last_overrun = overwrite;
3096 }
3097
Steven Rostedtd7690412008-10-01 00:29:53 -04003098 goto again;
3099
3100 out:
Steven Rostedt (Red Hat)0308b202015-11-23 10:35:36 -05003101 /* Update the read_stamp on the first event */
3102 if (reader && reader->read == 0)
3103 cpu_buffer->read_stamp = reader->page->time_stamp;
3104
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01003105 arch_spin_unlock(&cpu_buffer->lock);
Steven Rostedt3e03fb72008-11-06 00:09:43 -05003106 local_irq_restore(flags);
Steven Rostedtd7690412008-10-01 00:29:53 -04003107
3108 return reader;
3109}
3110
3111static void rb_advance_reader(struct ring_buffer_per_cpu *cpu_buffer)
3112{
3113 struct ring_buffer_event *event;
3114 struct buffer_page *reader;
3115 unsigned length;
3116
3117 reader = rb_get_reader_page(cpu_buffer);
3118
3119 /* This function should not be called when buffer is empty */
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05003120 if (RB_WARN_ON(cpu_buffer, !reader))
3121 return;
Steven Rostedtd7690412008-10-01 00:29:53 -04003122
3123 event = rb_reader_event(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003124
Steven Rostedta1863c22009-09-03 10:23:58 -04003125 if (event->type_len <= RINGBUF_TYPE_DATA_TYPE_LEN_MAX)
Steven Rostedte4906ef2009-04-30 20:49:44 -04003126 cpu_buffer->read++;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003127
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003128 rb_update_read_stamp(cpu_buffer, event);
3129
Steven Rostedtd7690412008-10-01 00:29:53 -04003130 length = rb_event_length(event);
Steven Rostedt6f807ac2008-10-04 02:00:58 -04003131 cpu_buffer->reader_page->read += length;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003132}
3133
3134static void rb_advance_iter(struct ring_buffer_iter *iter)
3135{
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003136 struct ring_buffer_per_cpu *cpu_buffer;
3137 struct ring_buffer_event *event;
3138 unsigned length;
3139
3140 cpu_buffer = iter->cpu_buffer;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003141
3142 /*
3143 * Check if we are at the end of the buffer.
3144 */
Steven Rostedtbf41a152008-10-04 02:00:59 -04003145 if (iter->head >= rb_page_size(iter->head_page)) {
Steven Rostedtea05b572009-06-03 09:30:10 -04003146 /* discarded commits can make the page empty */
3147 if (iter->head_page == cpu_buffer->commit_page)
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05003148 return;
Steven Rostedtd7690412008-10-01 00:29:53 -04003149 rb_inc_iter(iter);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003150 return;
3151 }
3152
3153 event = rb_iter_head_event(iter);
3154
3155 length = rb_event_length(event);
3156
3157 /*
3158 * This should not be called to advance the header if we are
3159 * at the tail of the buffer.
3160 */
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05003161 if (RB_WARN_ON(cpu_buffer,
Steven Rostedtf536aaf2008-11-10 23:07:30 -05003162 (iter->head_page == cpu_buffer->commit_page) &&
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05003163 (iter->head + length > rb_commit_index(cpu_buffer))))
3164 return;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003165
3166 rb_update_iter_read_stamp(iter, event);
3167
3168 iter->head += length;
3169
3170 /* check for end of page padding */
Steven Rostedtbf41a152008-10-04 02:00:59 -04003171 if ((iter->head >= rb_page_size(iter->head_page)) &&
3172 (iter->head_page != cpu_buffer->commit_page))
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003173 rb_advance_iter(iter);
3174}
3175
Steven Rostedt66a8cb92010-03-31 13:21:56 -04003176static int rb_lost_events(struct ring_buffer_per_cpu *cpu_buffer)
3177{
3178 return cpu_buffer->lost_events;
3179}
3180
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003181static struct ring_buffer_event *
Steven Rostedt66a8cb92010-03-31 13:21:56 -04003182rb_buffer_peek(struct ring_buffer_per_cpu *cpu_buffer, u64 *ts,
3183 unsigned long *lost_events)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003184{
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003185 struct ring_buffer_event *event;
Steven Rostedtd7690412008-10-01 00:29:53 -04003186 struct buffer_page *reader;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003187 int nr_loops = 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003188
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003189 again:
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003190 /*
Steven Rostedt69d1b832010-10-07 18:18:05 -04003191 * We repeat when a time extend is encountered.
3192 * Since the time extend is always attached to a data event,
3193 * we should never loop more than once.
3194 * (We never hit the following condition more than twice).
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003195 */
Steven Rostedt69d1b832010-10-07 18:18:05 -04003196 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 2))
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003197 return NULL;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003198
Steven Rostedtd7690412008-10-01 00:29:53 -04003199 reader = rb_get_reader_page(cpu_buffer);
3200 if (!reader)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003201 return NULL;
3202
Steven Rostedtd7690412008-10-01 00:29:53 -04003203 event = rb_reader_event(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003204
Lai Jiangshan334d4162009-04-24 11:27:05 +08003205 switch (event->type_len) {
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003206 case RINGBUF_TYPE_PADDING:
Tom Zanussi2d622712009-03-22 03:30:49 -05003207 if (rb_null_event(event))
3208 RB_WARN_ON(cpu_buffer, 1);
3209 /*
3210 * Because the writer could be discarding every
3211 * event it creates (which would probably be bad)
3212 * if we were to go back to "again" then we may never
3213 * catch up, and will trigger the warn on, or lock
3214 * the box. Return the padding, and we will release
3215 * the current locks, and try again.
3216 */
Tom Zanussi2d622712009-03-22 03:30:49 -05003217 return event;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003218
3219 case RINGBUF_TYPE_TIME_EXTEND:
3220 /* Internal data, OK to advance */
Steven Rostedtd7690412008-10-01 00:29:53 -04003221 rb_advance_reader(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003222 goto again;
3223
3224 case RINGBUF_TYPE_TIME_STAMP:
3225 /* FIXME: not implemented */
Steven Rostedtd7690412008-10-01 00:29:53 -04003226 rb_advance_reader(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003227 goto again;
3228
3229 case RINGBUF_TYPE_DATA:
3230 if (ts) {
3231 *ts = cpu_buffer->read_stamp + event->time_delta;
Robert Richterd8eeb2d2009-07-31 14:58:04 +02003232 ring_buffer_normalize_time_stamp(cpu_buffer->buffer,
Steven Rostedt37886f62009-03-17 17:22:06 -04003233 cpu_buffer->cpu, ts);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003234 }
Steven Rostedt66a8cb92010-03-31 13:21:56 -04003235 if (lost_events)
3236 *lost_events = rb_lost_events(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003237 return event;
3238
3239 default:
3240 BUG();
3241 }
3242
3243 return NULL;
3244}
Robert Richterc4f50182008-12-11 16:49:22 +01003245EXPORT_SYMBOL_GPL(ring_buffer_peek);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003246
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003247static struct ring_buffer_event *
3248rb_iter_peek(struct ring_buffer_iter *iter, u64 *ts)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003249{
3250 struct ring_buffer *buffer;
3251 struct ring_buffer_per_cpu *cpu_buffer;
3252 struct ring_buffer_event *event;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003253 int nr_loops = 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003254
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003255 cpu_buffer = iter->cpu_buffer;
3256 buffer = cpu_buffer->buffer;
3257
Steven Rostedt492a74f2010-01-25 15:17:47 -05003258 /*
3259 * Check if someone performed a consuming read to
3260 * the buffer. A consuming read invalidates the iterator
3261 * and we need to reset the iterator in this case.
3262 */
3263 if (unlikely(iter->cache_read != cpu_buffer->read ||
3264 iter->cache_reader_page != cpu_buffer->reader_page))
3265 rb_iter_reset(iter);
3266
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003267 again:
Steven Rostedt3c05d742010-01-26 16:14:08 -05003268 if (ring_buffer_iter_empty(iter))
3269 return NULL;
3270
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003271 /*
Steven Rostedt69d1b832010-10-07 18:18:05 -04003272 * We repeat when a time extend is encountered.
3273 * Since the time extend is always attached to a data event,
3274 * we should never loop more than once.
3275 * (We never hit the following condition more than twice).
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003276 */
Steven Rostedt69d1b832010-10-07 18:18:05 -04003277 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 2))
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003278 return NULL;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003279
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003280 if (rb_per_cpu_empty(cpu_buffer))
3281 return NULL;
3282
Steven Rostedt3c05d742010-01-26 16:14:08 -05003283 if (iter->head >= local_read(&iter->head_page->page->commit)) {
3284 rb_inc_iter(iter);
3285 goto again;
3286 }
3287
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003288 event = rb_iter_head_event(iter);
3289
Lai Jiangshan334d4162009-04-24 11:27:05 +08003290 switch (event->type_len) {
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003291 case RINGBUF_TYPE_PADDING:
Tom Zanussi2d622712009-03-22 03:30:49 -05003292 if (rb_null_event(event)) {
3293 rb_inc_iter(iter);
3294 goto again;
3295 }
3296 rb_advance_iter(iter);
3297 return event;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003298
3299 case RINGBUF_TYPE_TIME_EXTEND:
3300 /* Internal data, OK to advance */
3301 rb_advance_iter(iter);
3302 goto again;
3303
3304 case RINGBUF_TYPE_TIME_STAMP:
3305 /* FIXME: not implemented */
3306 rb_advance_iter(iter);
3307 goto again;
3308
3309 case RINGBUF_TYPE_DATA:
3310 if (ts) {
3311 *ts = iter->read_stamp + event->time_delta;
Steven Rostedt37886f62009-03-17 17:22:06 -04003312 ring_buffer_normalize_time_stamp(buffer,
3313 cpu_buffer->cpu, ts);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003314 }
3315 return event;
3316
3317 default:
3318 BUG();
3319 }
3320
3321 return NULL;
3322}
Robert Richterc4f50182008-12-11 16:49:22 +01003323EXPORT_SYMBOL_GPL(ring_buffer_iter_peek);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003324
Steven Rostedt8d707e82009-06-16 21:22:48 -04003325static inline int rb_ok_to_lock(void)
3326{
3327 /*
3328 * If an NMI die dumps out the content of the ring buffer
3329 * do not grab locks. We also permanently disable the ring
3330 * buffer too. A one time deal is all you get from reading
3331 * the ring buffer from an NMI.
3332 */
Steven Rostedt464e85e2009-08-05 15:26:37 -04003333 if (likely(!in_nmi()))
Steven Rostedt8d707e82009-06-16 21:22:48 -04003334 return 1;
3335
3336 tracing_off_permanent();
3337 return 0;
3338}
3339
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003340/**
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003341 * ring_buffer_peek - peek at the next event to be read
3342 * @buffer: The ring buffer to read
3343 * @cpu: The cpu to peak at
3344 * @ts: The timestamp counter of this event.
Steven Rostedt66a8cb92010-03-31 13:21:56 -04003345 * @lost_events: a variable to store if events were lost (may be NULL)
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003346 *
3347 * This will return the event that will be read next, but does
3348 * not consume the data.
3349 */
3350struct ring_buffer_event *
Steven Rostedt66a8cb92010-03-31 13:21:56 -04003351ring_buffer_peek(struct ring_buffer *buffer, int cpu, u64 *ts,
3352 unsigned long *lost_events)
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003353{
3354 struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
Steven Rostedt8aabee52009-03-12 13:13:49 -04003355 struct ring_buffer_event *event;
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003356 unsigned long flags;
Steven Rostedt8d707e82009-06-16 21:22:48 -04003357 int dolock;
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003358
Steven Rostedt554f7862009-03-11 22:00:13 -04003359 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04003360 return NULL;
Steven Rostedt554f7862009-03-11 22:00:13 -04003361
Steven Rostedt8d707e82009-06-16 21:22:48 -04003362 dolock = rb_ok_to_lock();
Tom Zanussi2d622712009-03-22 03:30:49 -05003363 again:
Steven Rostedt8d707e82009-06-16 21:22:48 -04003364 local_irq_save(flags);
3365 if (dolock)
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02003366 raw_spin_lock(&cpu_buffer->reader_lock);
Steven Rostedt66a8cb92010-03-31 13:21:56 -04003367 event = rb_buffer_peek(cpu_buffer, ts, lost_events);
Robert Richter469535a2009-07-30 19:19:18 +02003368 if (event && event->type_len == RINGBUF_TYPE_PADDING)
3369 rb_advance_reader(cpu_buffer);
Steven Rostedt8d707e82009-06-16 21:22:48 -04003370 if (dolock)
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02003371 raw_spin_unlock(&cpu_buffer->reader_lock);
Steven Rostedt8d707e82009-06-16 21:22:48 -04003372 local_irq_restore(flags);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003373
Steven Rostedt1b959e12009-09-03 10:12:13 -04003374 if (event && event->type_len == RINGBUF_TYPE_PADDING)
Tom Zanussi2d622712009-03-22 03:30:49 -05003375 goto again;
Tom Zanussi2d622712009-03-22 03:30:49 -05003376
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003377 return event;
3378}
3379
3380/**
3381 * ring_buffer_iter_peek - peek at the next event to be read
3382 * @iter: The ring buffer iterator
3383 * @ts: The timestamp counter of this event.
3384 *
3385 * This will return the event that will be read next, but does
3386 * not increment the iterator.
3387 */
3388struct ring_buffer_event *
3389ring_buffer_iter_peek(struct ring_buffer_iter *iter, u64 *ts)
3390{
3391 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
3392 struct ring_buffer_event *event;
3393 unsigned long flags;
3394
Tom Zanussi2d622712009-03-22 03:30:49 -05003395 again:
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02003396 raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003397 event = rb_iter_peek(iter, ts);
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02003398 raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003399
Steven Rostedt1b959e12009-09-03 10:12:13 -04003400 if (event && event->type_len == RINGBUF_TYPE_PADDING)
Tom Zanussi2d622712009-03-22 03:30:49 -05003401 goto again;
Tom Zanussi2d622712009-03-22 03:30:49 -05003402
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003403 return event;
3404}
3405
3406/**
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003407 * ring_buffer_consume - return an event and consume it
3408 * @buffer: The ring buffer to get the next event from
Steven Rostedt66a8cb92010-03-31 13:21:56 -04003409 * @cpu: the cpu to read the buffer from
3410 * @ts: a variable to store the timestamp (may be NULL)
3411 * @lost_events: a variable to store if events were lost (may be NULL)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003412 *
3413 * Returns the next event in the ring buffer, and that event is consumed.
3414 * Meaning, that sequential reads will keep returning a different event,
3415 * and eventually empty the ring buffer if the producer is slower.
3416 */
3417struct ring_buffer_event *
Steven Rostedt66a8cb92010-03-31 13:21:56 -04003418ring_buffer_consume(struct ring_buffer *buffer, int cpu, u64 *ts,
3419 unsigned long *lost_events)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003420{
Steven Rostedt554f7862009-03-11 22:00:13 -04003421 struct ring_buffer_per_cpu *cpu_buffer;
3422 struct ring_buffer_event *event = NULL;
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003423 unsigned long flags;
Steven Rostedt8d707e82009-06-16 21:22:48 -04003424 int dolock;
3425
3426 dolock = rb_ok_to_lock();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003427
Tom Zanussi2d622712009-03-22 03:30:49 -05003428 again:
Steven Rostedt554f7862009-03-11 22:00:13 -04003429 /* might be called in atomic */
3430 preempt_disable();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003431
Steven Rostedt554f7862009-03-11 22:00:13 -04003432 if (!cpumask_test_cpu(cpu, buffer->cpumask))
3433 goto out;
3434
3435 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt8d707e82009-06-16 21:22:48 -04003436 local_irq_save(flags);
3437 if (dolock)
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02003438 raw_spin_lock(&cpu_buffer->reader_lock);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003439
Steven Rostedt66a8cb92010-03-31 13:21:56 -04003440 event = rb_buffer_peek(cpu_buffer, ts, lost_events);
3441 if (event) {
3442 cpu_buffer->lost_events = 0;
Robert Richter469535a2009-07-30 19:19:18 +02003443 rb_advance_reader(cpu_buffer);
Steven Rostedt66a8cb92010-03-31 13:21:56 -04003444 }
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003445
Steven Rostedt8d707e82009-06-16 21:22:48 -04003446 if (dolock)
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02003447 raw_spin_unlock(&cpu_buffer->reader_lock);
Steven Rostedt8d707e82009-06-16 21:22:48 -04003448 local_irq_restore(flags);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003449
Steven Rostedt554f7862009-03-11 22:00:13 -04003450 out:
3451 preempt_enable();
3452
Steven Rostedt1b959e12009-09-03 10:12:13 -04003453 if (event && event->type_len == RINGBUF_TYPE_PADDING)
Tom Zanussi2d622712009-03-22 03:30:49 -05003454 goto again;
Tom Zanussi2d622712009-03-22 03:30:49 -05003455
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003456 return event;
3457}
Robert Richterc4f50182008-12-11 16:49:22 +01003458EXPORT_SYMBOL_GPL(ring_buffer_consume);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003459
3460/**
David Miller72c9ddf2010-04-20 15:47:11 -07003461 * ring_buffer_read_prepare - Prepare for a non consuming read of the buffer
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003462 * @buffer: The ring buffer to read from
3463 * @cpu: The cpu buffer to iterate over
3464 *
David Miller72c9ddf2010-04-20 15:47:11 -07003465 * This performs the initial preparations necessary to iterate
3466 * through the buffer. Memory is allocated, buffer recording
3467 * is disabled, and the iterator pointer is returned to the caller.
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003468 *
David Miller72c9ddf2010-04-20 15:47:11 -07003469 * Disabling buffer recordng prevents the reading from being
3470 * corrupted. This is not a consuming read, so a producer is not
3471 * expected.
3472 *
3473 * After a sequence of ring_buffer_read_prepare calls, the user is
3474 * expected to make at least one call to ring_buffer_prepare_sync.
3475 * Afterwards, ring_buffer_read_start is invoked to get things going
3476 * for real.
3477 *
3478 * This overall must be paired with ring_buffer_finish.
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003479 */
3480struct ring_buffer_iter *
David Miller72c9ddf2010-04-20 15:47:11 -07003481ring_buffer_read_prepare(struct ring_buffer *buffer, int cpu)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003482{
3483 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedt8aabee52009-03-12 13:13:49 -04003484 struct ring_buffer_iter *iter;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003485
Rusty Russell9e01c1b2009-01-01 10:12:22 +10303486 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04003487 return NULL;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003488
3489 iter = kmalloc(sizeof(*iter), GFP_KERNEL);
3490 if (!iter)
Steven Rostedt8aabee52009-03-12 13:13:49 -04003491 return NULL;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003492
3493 cpu_buffer = buffer->buffers[cpu];
3494
3495 iter->cpu_buffer = cpu_buffer;
3496
3497 atomic_inc(&cpu_buffer->record_disabled);
David Miller72c9ddf2010-04-20 15:47:11 -07003498
3499 return iter;
3500}
3501EXPORT_SYMBOL_GPL(ring_buffer_read_prepare);
3502
3503/**
3504 * ring_buffer_read_prepare_sync - Synchronize a set of prepare calls
3505 *
3506 * All previously invoked ring_buffer_read_prepare calls to prepare
3507 * iterators will be synchronized. Afterwards, read_buffer_read_start
3508 * calls on those iterators are allowed.
3509 */
3510void
3511ring_buffer_read_prepare_sync(void)
3512{
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003513 synchronize_sched();
David Miller72c9ddf2010-04-20 15:47:11 -07003514}
3515EXPORT_SYMBOL_GPL(ring_buffer_read_prepare_sync);
3516
3517/**
3518 * ring_buffer_read_start - start a non consuming read of the buffer
3519 * @iter: The iterator returned by ring_buffer_read_prepare
3520 *
3521 * This finalizes the startup of an iteration through the buffer.
3522 * The iterator comes from a call to ring_buffer_read_prepare and
3523 * an intervening ring_buffer_read_prepare_sync must have been
3524 * performed.
3525 *
3526 * Must be paired with ring_buffer_finish.
3527 */
3528void
3529ring_buffer_read_start(struct ring_buffer_iter *iter)
3530{
3531 struct ring_buffer_per_cpu *cpu_buffer;
3532 unsigned long flags;
3533
3534 if (!iter)
3535 return;
3536
3537 cpu_buffer = iter->cpu_buffer;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003538
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02003539 raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01003540 arch_spin_lock(&cpu_buffer->lock);
Steven Rostedt642edba2008-11-12 00:01:26 -05003541 rb_iter_reset(iter);
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01003542 arch_spin_unlock(&cpu_buffer->lock);
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02003543 raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003544}
Robert Richterc4f50182008-12-11 16:49:22 +01003545EXPORT_SYMBOL_GPL(ring_buffer_read_start);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003546
3547/**
3548 * ring_buffer_finish - finish reading the iterator of the buffer
3549 * @iter: The iterator retrieved by ring_buffer_start
3550 *
3551 * This re-enables the recording to the buffer, and frees the
3552 * iterator.
3553 */
3554void
3555ring_buffer_read_finish(struct ring_buffer_iter *iter)
3556{
3557 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
3558
3559 atomic_dec(&cpu_buffer->record_disabled);
3560 kfree(iter);
3561}
Robert Richterc4f50182008-12-11 16:49:22 +01003562EXPORT_SYMBOL_GPL(ring_buffer_read_finish);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003563
3564/**
3565 * ring_buffer_read - read the next item in the ring buffer by the iterator
3566 * @iter: The ring buffer iterator
3567 * @ts: The time stamp of the event read.
3568 *
3569 * This reads the next event in the ring buffer and increments the iterator.
3570 */
3571struct ring_buffer_event *
3572ring_buffer_read(struct ring_buffer_iter *iter, u64 *ts)
3573{
3574 struct ring_buffer_event *event;
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003575 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
3576 unsigned long flags;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003577
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02003578 raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
Steven Rostedt7e9391c2009-09-03 10:02:09 -04003579 again:
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003580 event = rb_iter_peek(iter, ts);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003581 if (!event)
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003582 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003583
Steven Rostedt7e9391c2009-09-03 10:02:09 -04003584 if (event->type_len == RINGBUF_TYPE_PADDING)
3585 goto again;
3586
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003587 rb_advance_iter(iter);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003588 out:
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02003589 raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003590
3591 return event;
3592}
Robert Richterc4f50182008-12-11 16:49:22 +01003593EXPORT_SYMBOL_GPL(ring_buffer_read);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003594
3595/**
3596 * ring_buffer_size - return the size of the ring buffer (in bytes)
3597 * @buffer: The ring buffer.
3598 */
3599unsigned long ring_buffer_size(struct ring_buffer *buffer)
3600{
3601 return BUF_PAGE_SIZE * buffer->pages;
3602}
Robert Richterc4f50182008-12-11 16:49:22 +01003603EXPORT_SYMBOL_GPL(ring_buffer_size);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003604
3605static void
3606rb_reset_cpu(struct ring_buffer_per_cpu *cpu_buffer)
3607{
Steven Rostedt77ae3652009-03-27 11:00:29 -04003608 rb_head_page_deactivate(cpu_buffer);
3609
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003610 cpu_buffer->head_page
Steven Rostedt3adc54f2009-03-30 15:32:01 -04003611 = list_entry(cpu_buffer->pages, struct buffer_page, list);
Steven Rostedtbf41a152008-10-04 02:00:59 -04003612 local_set(&cpu_buffer->head_page->write, 0);
Steven Rostedt778c55d2009-05-01 18:44:45 -04003613 local_set(&cpu_buffer->head_page->entries, 0);
Steven Rostedtabc9b562008-12-02 15:34:06 -05003614 local_set(&cpu_buffer->head_page->page->commit, 0);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003615
Steven Rostedt6f807ac2008-10-04 02:00:58 -04003616 cpu_buffer->head_page->read = 0;
Steven Rostedtbf41a152008-10-04 02:00:59 -04003617
3618 cpu_buffer->tail_page = cpu_buffer->head_page;
3619 cpu_buffer->commit_page = cpu_buffer->head_page;
3620
3621 INIT_LIST_HEAD(&cpu_buffer->reader_page->list);
3622 local_set(&cpu_buffer->reader_page->write, 0);
Steven Rostedt778c55d2009-05-01 18:44:45 -04003623 local_set(&cpu_buffer->reader_page->entries, 0);
Steven Rostedtabc9b562008-12-02 15:34:06 -05003624 local_set(&cpu_buffer->reader_page->page->commit, 0);
Steven Rostedt6f807ac2008-10-04 02:00:58 -04003625 cpu_buffer->reader_page->read = 0;
Steven Rostedtd7690412008-10-01 00:29:53 -04003626
Steven Rostedt77ae3652009-03-27 11:00:29 -04003627 local_set(&cpu_buffer->commit_overrun, 0);
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -07003628 local_set(&cpu_buffer->entries_bytes, 0);
Steven Rostedt77ae3652009-03-27 11:00:29 -04003629 local_set(&cpu_buffer->overrun, 0);
Steven Rostedte4906ef2009-04-30 20:49:44 -04003630 local_set(&cpu_buffer->entries, 0);
Steven Rostedtfa743952009-06-16 12:37:57 -04003631 local_set(&cpu_buffer->committing, 0);
3632 local_set(&cpu_buffer->commits, 0);
Steven Rostedt77ae3652009-03-27 11:00:29 -04003633 cpu_buffer->read = 0;
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -07003634 cpu_buffer->read_bytes = 0;
Steven Rostedt69507c02009-01-21 18:45:57 -05003635
3636 cpu_buffer->write_stamp = 0;
3637 cpu_buffer->read_stamp = 0;
Steven Rostedt77ae3652009-03-27 11:00:29 -04003638
Steven Rostedt66a8cb92010-03-31 13:21:56 -04003639 cpu_buffer->lost_events = 0;
3640 cpu_buffer->last_overrun = 0;
3641
Steven Rostedt77ae3652009-03-27 11:00:29 -04003642 rb_head_page_activate(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003643}
3644
3645/**
3646 * ring_buffer_reset_cpu - reset a ring buffer per CPU buffer
3647 * @buffer: The ring buffer to reset a per cpu buffer of
3648 * @cpu: The CPU buffer to be reset
3649 */
3650void ring_buffer_reset_cpu(struct ring_buffer *buffer, int cpu)
3651{
3652 struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
3653 unsigned long flags;
3654
Rusty Russell9e01c1b2009-01-01 10:12:22 +10303655 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04003656 return;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003657
Steven Rostedt41ede232009-05-01 20:26:54 -04003658 atomic_inc(&cpu_buffer->record_disabled);
3659
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02003660 raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003661
Steven Rostedt41b6a952009-09-02 09:59:48 -04003662 if (RB_WARN_ON(cpu_buffer, local_read(&cpu_buffer->committing)))
3663 goto out;
3664
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01003665 arch_spin_lock(&cpu_buffer->lock);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003666
3667 rb_reset_cpu(cpu_buffer);
3668
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01003669 arch_spin_unlock(&cpu_buffer->lock);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003670
Steven Rostedt41b6a952009-09-02 09:59:48 -04003671 out:
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02003672 raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
Steven Rostedt41ede232009-05-01 20:26:54 -04003673
3674 atomic_dec(&cpu_buffer->record_disabled);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003675}
Robert Richterc4f50182008-12-11 16:49:22 +01003676EXPORT_SYMBOL_GPL(ring_buffer_reset_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003677
3678/**
3679 * ring_buffer_reset - reset a ring buffer
3680 * @buffer: The ring buffer to reset all cpu buffers
3681 */
3682void ring_buffer_reset(struct ring_buffer *buffer)
3683{
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003684 int cpu;
3685
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003686 for_each_buffer_cpu(buffer, cpu)
Steven Rostedtd7690412008-10-01 00:29:53 -04003687 ring_buffer_reset_cpu(buffer, cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003688}
Robert Richterc4f50182008-12-11 16:49:22 +01003689EXPORT_SYMBOL_GPL(ring_buffer_reset);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003690
3691/**
3692 * rind_buffer_empty - is the ring buffer empty?
3693 * @buffer: The ring buffer to test
3694 */
3695int ring_buffer_empty(struct ring_buffer *buffer)
3696{
3697 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedtd4788202009-06-17 00:39:43 -04003698 unsigned long flags;
Steven Rostedt8d707e82009-06-16 21:22:48 -04003699 int dolock;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003700 int cpu;
Steven Rostedtd4788202009-06-17 00:39:43 -04003701 int ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003702
Steven Rostedt8d707e82009-06-16 21:22:48 -04003703 dolock = rb_ok_to_lock();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003704
3705 /* yes this is racy, but if you don't like the race, lock the buffer */
3706 for_each_buffer_cpu(buffer, cpu) {
3707 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt8d707e82009-06-16 21:22:48 -04003708 local_irq_save(flags);
3709 if (dolock)
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02003710 raw_spin_lock(&cpu_buffer->reader_lock);
Steven Rostedtd4788202009-06-17 00:39:43 -04003711 ret = rb_per_cpu_empty(cpu_buffer);
Steven Rostedt8d707e82009-06-16 21:22:48 -04003712 if (dolock)
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02003713 raw_spin_unlock(&cpu_buffer->reader_lock);
Steven Rostedt8d707e82009-06-16 21:22:48 -04003714 local_irq_restore(flags);
3715
Steven Rostedtd4788202009-06-17 00:39:43 -04003716 if (!ret)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003717 return 0;
3718 }
Steven Rostedt554f7862009-03-11 22:00:13 -04003719
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003720 return 1;
3721}
Robert Richterc4f50182008-12-11 16:49:22 +01003722EXPORT_SYMBOL_GPL(ring_buffer_empty);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003723
3724/**
3725 * ring_buffer_empty_cpu - is a cpu buffer of a ring buffer empty?
3726 * @buffer: The ring buffer
3727 * @cpu: The CPU buffer to test
3728 */
3729int ring_buffer_empty_cpu(struct ring_buffer *buffer, int cpu)
3730{
3731 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedtd4788202009-06-17 00:39:43 -04003732 unsigned long flags;
Steven Rostedt8d707e82009-06-16 21:22:48 -04003733 int dolock;
Steven Rostedt8aabee52009-03-12 13:13:49 -04003734 int ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003735
Rusty Russell9e01c1b2009-01-01 10:12:22 +10303736 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04003737 return 1;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003738
Steven Rostedt8d707e82009-06-16 21:22:48 -04003739 dolock = rb_ok_to_lock();
Steven Rostedt554f7862009-03-11 22:00:13 -04003740
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003741 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt8d707e82009-06-16 21:22:48 -04003742 local_irq_save(flags);
3743 if (dolock)
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02003744 raw_spin_lock(&cpu_buffer->reader_lock);
Steven Rostedt554f7862009-03-11 22:00:13 -04003745 ret = rb_per_cpu_empty(cpu_buffer);
Steven Rostedt8d707e82009-06-16 21:22:48 -04003746 if (dolock)
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02003747 raw_spin_unlock(&cpu_buffer->reader_lock);
Steven Rostedt8d707e82009-06-16 21:22:48 -04003748 local_irq_restore(flags);
Steven Rostedt554f7862009-03-11 22:00:13 -04003749
3750 return ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003751}
Robert Richterc4f50182008-12-11 16:49:22 +01003752EXPORT_SYMBOL_GPL(ring_buffer_empty_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003753
Steven Rostedt85bac322009-09-04 14:24:40 -04003754#ifdef CONFIG_RING_BUFFER_ALLOW_SWAP
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003755/**
3756 * ring_buffer_swap_cpu - swap a CPU buffer between two ring buffers
3757 * @buffer_a: One buffer to swap with
3758 * @buffer_b: The other buffer to swap with
3759 *
3760 * This function is useful for tracers that want to take a "snapshot"
3761 * of a CPU buffer and has another back up buffer lying around.
3762 * it is expected that the tracer handles the cpu buffer not being
3763 * used at the moment.
3764 */
3765int ring_buffer_swap_cpu(struct ring_buffer *buffer_a,
3766 struct ring_buffer *buffer_b, int cpu)
3767{
3768 struct ring_buffer_per_cpu *cpu_buffer_a;
3769 struct ring_buffer_per_cpu *cpu_buffer_b;
Steven Rostedt554f7862009-03-11 22:00:13 -04003770 int ret = -EINVAL;
3771
Rusty Russell9e01c1b2009-01-01 10:12:22 +10303772 if (!cpumask_test_cpu(cpu, buffer_a->cpumask) ||
3773 !cpumask_test_cpu(cpu, buffer_b->cpumask))
Steven Rostedt554f7862009-03-11 22:00:13 -04003774 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003775
3776 /* At least make sure the two buffers are somewhat the same */
Lai Jiangshan6d102bc2008-12-17 17:48:23 +08003777 if (buffer_a->pages != buffer_b->pages)
Steven Rostedt554f7862009-03-11 22:00:13 -04003778 goto out;
3779
3780 ret = -EAGAIN;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003781
Steven Rostedt97b17ef2009-01-21 15:24:56 -05003782 if (ring_buffer_flags != RB_BUFFERS_ON)
Steven Rostedt554f7862009-03-11 22:00:13 -04003783 goto out;
Steven Rostedt97b17ef2009-01-21 15:24:56 -05003784
3785 if (atomic_read(&buffer_a->record_disabled))
Steven Rostedt554f7862009-03-11 22:00:13 -04003786 goto out;
Steven Rostedt97b17ef2009-01-21 15:24:56 -05003787
3788 if (atomic_read(&buffer_b->record_disabled))
Steven Rostedt554f7862009-03-11 22:00:13 -04003789 goto out;
Steven Rostedt97b17ef2009-01-21 15:24:56 -05003790
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003791 cpu_buffer_a = buffer_a->buffers[cpu];
3792 cpu_buffer_b = buffer_b->buffers[cpu];
3793
Steven Rostedt97b17ef2009-01-21 15:24:56 -05003794 if (atomic_read(&cpu_buffer_a->record_disabled))
Steven Rostedt554f7862009-03-11 22:00:13 -04003795 goto out;
Steven Rostedt97b17ef2009-01-21 15:24:56 -05003796
3797 if (atomic_read(&cpu_buffer_b->record_disabled))
Steven Rostedt554f7862009-03-11 22:00:13 -04003798 goto out;
Steven Rostedt97b17ef2009-01-21 15:24:56 -05003799
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003800 /*
3801 * We can't do a synchronize_sched here because this
3802 * function can be called in atomic context.
3803 * Normally this will be called from the same CPU as cpu.
3804 * If not it's up to the caller to protect this.
3805 */
3806 atomic_inc(&cpu_buffer_a->record_disabled);
3807 atomic_inc(&cpu_buffer_b->record_disabled);
3808
Steven Rostedt98277992009-09-02 10:56:15 -04003809 ret = -EBUSY;
3810 if (local_read(&cpu_buffer_a->committing))
3811 goto out_dec;
3812 if (local_read(&cpu_buffer_b->committing))
3813 goto out_dec;
3814
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003815 buffer_a->buffers[cpu] = cpu_buffer_b;
3816 buffer_b->buffers[cpu] = cpu_buffer_a;
3817
3818 cpu_buffer_b->buffer = buffer_a;
3819 cpu_buffer_a->buffer = buffer_b;
3820
Steven Rostedt98277992009-09-02 10:56:15 -04003821 ret = 0;
3822
3823out_dec:
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003824 atomic_dec(&cpu_buffer_a->record_disabled);
3825 atomic_dec(&cpu_buffer_b->record_disabled);
Steven Rostedt554f7862009-03-11 22:00:13 -04003826out:
Steven Rostedt554f7862009-03-11 22:00:13 -04003827 return ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003828}
Robert Richterc4f50182008-12-11 16:49:22 +01003829EXPORT_SYMBOL_GPL(ring_buffer_swap_cpu);
Steven Rostedt85bac322009-09-04 14:24:40 -04003830#endif /* CONFIG_RING_BUFFER_ALLOW_SWAP */
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003831
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003832/**
3833 * ring_buffer_alloc_read_page - allocate a page to read from buffer
3834 * @buffer: the buffer to allocate for.
3835 *
3836 * This function is used in conjunction with ring_buffer_read_page.
3837 * When reading a full page from the ring buffer, these functions
3838 * can be used to speed up the process. The calling function should
3839 * allocate a few pages first with this function. Then when it
3840 * needs to get pages from the ring buffer, it passes the result
3841 * of this function into ring_buffer_read_page, which will swap
3842 * the page that was allocated, with the read page of the buffer.
3843 *
3844 * Returns:
3845 * The page allocated, or NULL on error.
3846 */
Vaibhav Nagarnaik7ea59062011-05-03 17:56:42 -07003847void *ring_buffer_alloc_read_page(struct ring_buffer *buffer, int cpu)
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003848{
Steven Rostedt044fa782008-12-02 23:50:03 -05003849 struct buffer_data_page *bpage;
Vaibhav Nagarnaik7ea59062011-05-03 17:56:42 -07003850 struct page *page;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003851
Vaibhav Nagarnaikd7ec4bf2011-06-07 17:01:42 -07003852 page = alloc_pages_node(cpu_to_node(cpu),
3853 GFP_KERNEL | __GFP_NORETRY, 0);
Vaibhav Nagarnaik7ea59062011-05-03 17:56:42 -07003854 if (!page)
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003855 return NULL;
3856
Vaibhav Nagarnaik7ea59062011-05-03 17:56:42 -07003857 bpage = page_address(page);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003858
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003859 rb_init_page(bpage);
3860
Steven Rostedt044fa782008-12-02 23:50:03 -05003861 return bpage;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003862}
Steven Rostedtd6ce96d2009-05-05 01:15:24 -04003863EXPORT_SYMBOL_GPL(ring_buffer_alloc_read_page);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003864
3865/**
3866 * ring_buffer_free_read_page - free an allocated read page
3867 * @buffer: the buffer the page was allocate for
3868 * @data: the page to free
3869 *
3870 * Free a page allocated from ring_buffer_alloc_read_page.
3871 */
3872void ring_buffer_free_read_page(struct ring_buffer *buffer, void *data)
3873{
3874 free_page((unsigned long)data);
3875}
Steven Rostedtd6ce96d2009-05-05 01:15:24 -04003876EXPORT_SYMBOL_GPL(ring_buffer_free_read_page);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003877
3878/**
3879 * ring_buffer_read_page - extract a page from the ring buffer
3880 * @buffer: buffer to extract from
3881 * @data_page: the page to use allocated from ring_buffer_alloc_read_page
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003882 * @len: amount to extract
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003883 * @cpu: the cpu of the buffer to extract
3884 * @full: should the extraction only happen when the page is full.
3885 *
3886 * This function will pull out a page from the ring buffer and consume it.
3887 * @data_page must be the address of the variable that was returned
3888 * from ring_buffer_alloc_read_page. This is because the page might be used
3889 * to swap with a page in the ring buffer.
3890 *
3891 * for example:
Lai Jiangshanb85fa012009-02-09 14:21:14 +08003892 * rpage = ring_buffer_alloc_read_page(buffer);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003893 * if (!rpage)
3894 * return error;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003895 * ret = ring_buffer_read_page(buffer, &rpage, len, cpu, 0);
Lai Jiangshan667d2412009-02-09 14:21:17 +08003896 * if (ret >= 0)
3897 * process_page(rpage, ret);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003898 *
3899 * When @full is set, the function will not return true unless
3900 * the writer is off the reader page.
3901 *
3902 * Note: it is up to the calling functions to handle sleeps and wakeups.
3903 * The ring buffer can be used anywhere in the kernel and can not
3904 * blindly call wake_up. The layer that uses the ring buffer must be
3905 * responsible for that.
3906 *
3907 * Returns:
Lai Jiangshan667d2412009-02-09 14:21:17 +08003908 * >=0 if data has been transferred, returns the offset of consumed data.
3909 * <0 if no data has been transferred.
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003910 */
3911int ring_buffer_read_page(struct ring_buffer *buffer,
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003912 void **data_page, size_t len, int cpu, int full)
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003913{
3914 struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
3915 struct ring_buffer_event *event;
Steven Rostedt044fa782008-12-02 23:50:03 -05003916 struct buffer_data_page *bpage;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003917 struct buffer_page *reader;
Steven Rostedtff0ff842010-03-31 22:11:42 -04003918 unsigned long missed_events;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003919 unsigned long flags;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003920 unsigned int commit;
Lai Jiangshan667d2412009-02-09 14:21:17 +08003921 unsigned int read;
Steven Rostedt4f3640f2009-03-03 23:52:42 -05003922 u64 save_timestamp;
Lai Jiangshan667d2412009-02-09 14:21:17 +08003923 int ret = -1;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003924
Steven Rostedt554f7862009-03-11 22:00:13 -04003925 if (!cpumask_test_cpu(cpu, buffer->cpumask))
3926 goto out;
3927
Steven Rostedt474d32b2009-03-03 19:51:40 -05003928 /*
3929 * If len is not big enough to hold the page header, then
3930 * we can not copy anything.
3931 */
3932 if (len <= BUF_PAGE_HDR_SIZE)
Steven Rostedt554f7862009-03-11 22:00:13 -04003933 goto out;
Steven Rostedt474d32b2009-03-03 19:51:40 -05003934
3935 len -= BUF_PAGE_HDR_SIZE;
3936
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003937 if (!data_page)
Steven Rostedt554f7862009-03-11 22:00:13 -04003938 goto out;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003939
Steven Rostedt044fa782008-12-02 23:50:03 -05003940 bpage = *data_page;
3941 if (!bpage)
Steven Rostedt554f7862009-03-11 22:00:13 -04003942 goto out;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003943
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02003944 raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003945
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003946 reader = rb_get_reader_page(cpu_buffer);
3947 if (!reader)
Steven Rostedt554f7862009-03-11 22:00:13 -04003948 goto out_unlock;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003949
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003950 event = rb_reader_event(cpu_buffer);
Lai Jiangshan667d2412009-02-09 14:21:17 +08003951
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003952 read = reader->read;
3953 commit = rb_page_commit(reader);
3954
Steven Rostedt66a8cb92010-03-31 13:21:56 -04003955 /* Check if any events were dropped */
Steven Rostedtff0ff842010-03-31 22:11:42 -04003956 missed_events = cpu_buffer->lost_events;
Steven Rostedt66a8cb92010-03-31 13:21:56 -04003957
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003958 /*
Steven Rostedt474d32b2009-03-03 19:51:40 -05003959 * If this page has been partially read or
3960 * if len is not big enough to read the rest of the page or
3961 * a writer is still on the page, then
3962 * we must copy the data from the page to the buffer.
3963 * Otherwise, we can simply swap the page with the one passed in.
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003964 */
Steven Rostedt474d32b2009-03-03 19:51:40 -05003965 if (read || (len < (commit - read)) ||
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003966 cpu_buffer->reader_page == cpu_buffer->commit_page) {
Lai Jiangshan667d2412009-02-09 14:21:17 +08003967 struct buffer_data_page *rpage = cpu_buffer->reader_page->page;
Steven Rostedt474d32b2009-03-03 19:51:40 -05003968 unsigned int rpos = read;
3969 unsigned int pos = 0;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003970 unsigned int size;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003971
3972 if (full)
Steven Rostedt554f7862009-03-11 22:00:13 -04003973 goto out_unlock;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003974
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003975 if (len > (commit - read))
3976 len = (commit - read);
3977
Steven Rostedt69d1b832010-10-07 18:18:05 -04003978 /* Always keep the time extend and data together */
3979 size = rb_event_ts_length(event);
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003980
3981 if (len < size)
Steven Rostedt554f7862009-03-11 22:00:13 -04003982 goto out_unlock;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003983
Steven Rostedt4f3640f2009-03-03 23:52:42 -05003984 /* save the current timestamp, since the user will need it */
3985 save_timestamp = cpu_buffer->read_stamp;
3986
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003987 /* Need to copy one event at a time */
3988 do {
David Sharpe1e35922010-12-22 16:38:24 -08003989 /* We need the size of one event, because
3990 * rb_advance_reader only advances by one event,
3991 * whereas rb_event_ts_length may include the size of
3992 * one or two events.
3993 * We have already ensured there's enough space if this
3994 * is a time extend. */
3995 size = rb_event_length(event);
Steven Rostedt474d32b2009-03-03 19:51:40 -05003996 memcpy(bpage->data + pos, rpage->data + rpos, size);
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003997
3998 len -= size;
3999
4000 rb_advance_reader(cpu_buffer);
Steven Rostedt474d32b2009-03-03 19:51:40 -05004001 rpos = reader->read;
4002 pos += size;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05004003
Huang Ying18fab912010-07-28 14:14:01 +08004004 if (rpos >= commit)
4005 break;
4006
Steven Rostedtef7a4a12009-03-03 00:27:49 -05004007 event = rb_reader_event(cpu_buffer);
Steven Rostedt69d1b832010-10-07 18:18:05 -04004008 /* Always keep the time extend and data together */
4009 size = rb_event_ts_length(event);
David Sharpe1e35922010-12-22 16:38:24 -08004010 } while (len >= size);
Lai Jiangshan667d2412009-02-09 14:21:17 +08004011
4012 /* update bpage */
Steven Rostedtef7a4a12009-03-03 00:27:49 -05004013 local_set(&bpage->commit, pos);
Steven Rostedt4f3640f2009-03-03 23:52:42 -05004014 bpage->time_stamp = save_timestamp;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05004015
Steven Rostedt474d32b2009-03-03 19:51:40 -05004016 /* we copied everything to the beginning */
4017 read = 0;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004018 } else {
Steven Rostedtafbab762009-05-01 19:40:05 -04004019 /* update the entry counter */
Steven Rostedt77ae3652009-03-27 11:00:29 -04004020 cpu_buffer->read += rb_page_entries(reader);
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -07004021 cpu_buffer->read_bytes += BUF_PAGE_SIZE;
Steven Rostedtafbab762009-05-01 19:40:05 -04004022
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004023 /* swap the pages */
Steven Rostedt044fa782008-12-02 23:50:03 -05004024 rb_init_page(bpage);
Steven Rostedtef7a4a12009-03-03 00:27:49 -05004025 bpage = reader->page;
4026 reader->page = *data_page;
4027 local_set(&reader->write, 0);
Steven Rostedt778c55d2009-05-01 18:44:45 -04004028 local_set(&reader->entries, 0);
Steven Rostedtef7a4a12009-03-03 00:27:49 -05004029 reader->read = 0;
Steven Rostedt044fa782008-12-02 23:50:03 -05004030 *data_page = bpage;
Steven Rostedtff0ff842010-03-31 22:11:42 -04004031
4032 /*
4033 * Use the real_end for the data size,
4034 * This gives us a chance to store the lost events
4035 * on the page.
4036 */
4037 if (reader->real_end)
4038 local_set(&bpage->commit, reader->real_end);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004039 }
Lai Jiangshan667d2412009-02-09 14:21:17 +08004040 ret = read;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004041
Steven Rostedt66a8cb92010-03-31 13:21:56 -04004042 cpu_buffer->lost_events = 0;
Steven Rostedt2711ca22010-05-21 13:32:26 -04004043
4044 commit = local_read(&bpage->commit);
Steven Rostedt66a8cb92010-03-31 13:21:56 -04004045 /*
4046 * Set a flag in the commit field if we lost events
4047 */
Steven Rostedtff0ff842010-03-31 22:11:42 -04004048 if (missed_events) {
Steven Rostedtff0ff842010-03-31 22:11:42 -04004049 /* If there is room at the end of the page to save the
4050 * missed events, then record it there.
4051 */
4052 if (BUF_PAGE_SIZE - commit >= sizeof(missed_events)) {
4053 memcpy(&bpage->data[commit], &missed_events,
4054 sizeof(missed_events));
4055 local_add(RB_MISSED_STORED, &bpage->commit);
Steven Rostedt2711ca22010-05-21 13:32:26 -04004056 commit += sizeof(missed_events);
Steven Rostedtff0ff842010-03-31 22:11:42 -04004057 }
Steven Rostedt66a8cb92010-03-31 13:21:56 -04004058 local_add(RB_MISSED_EVENTS, &bpage->commit);
Steven Rostedtff0ff842010-03-31 22:11:42 -04004059 }
Steven Rostedt66a8cb92010-03-31 13:21:56 -04004060
Steven Rostedt2711ca22010-05-21 13:32:26 -04004061 /*
4062 * This page may be off to user land. Zero it out here.
4063 */
4064 if (commit < BUF_PAGE_SIZE)
4065 memset(&bpage->data[commit], 0, BUF_PAGE_SIZE - commit);
4066
Steven Rostedt554f7862009-03-11 22:00:13 -04004067 out_unlock:
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02004068 raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004069
Steven Rostedt554f7862009-03-11 22:00:13 -04004070 out:
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004071 return ret;
4072}
Steven Rostedtd6ce96d2009-05-05 01:15:24 -04004073EXPORT_SYMBOL_GPL(ring_buffer_read_page);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004074
Steven Rostedt59222ef2009-03-12 11:46:03 -04004075#ifdef CONFIG_HOTPLUG_CPU
Frederic Weisbecker09c9e842009-03-21 04:33:36 +01004076static int rb_cpu_notify(struct notifier_block *self,
4077 unsigned long action, void *hcpu)
Steven Rostedt554f7862009-03-11 22:00:13 -04004078{
4079 struct ring_buffer *buffer =
4080 container_of(self, struct ring_buffer, cpu_notify);
4081 long cpu = (long)hcpu;
4082
4083 switch (action) {
4084 case CPU_UP_PREPARE:
4085 case CPU_UP_PREPARE_FROZEN:
Rusty Russell3f237a72009-06-12 21:15:30 +09304086 if (cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt554f7862009-03-11 22:00:13 -04004087 return NOTIFY_OK;
4088
4089 buffer->buffers[cpu] =
4090 rb_allocate_cpu_buffer(buffer, cpu);
4091 if (!buffer->buffers[cpu]) {
4092 WARN(1, "failed to allocate ring buffer on CPU %ld\n",
4093 cpu);
4094 return NOTIFY_OK;
4095 }
4096 smp_wmb();
Rusty Russell3f237a72009-06-12 21:15:30 +09304097 cpumask_set_cpu(cpu, buffer->cpumask);
Steven Rostedt554f7862009-03-11 22:00:13 -04004098 break;
4099 case CPU_DOWN_PREPARE:
4100 case CPU_DOWN_PREPARE_FROZEN:
4101 /*
4102 * Do nothing.
4103 * If we were to free the buffer, then the user would
4104 * lose any trace that was in the buffer.
4105 */
4106 break;
4107 default:
4108 break;
4109 }
4110 return NOTIFY_OK;
4111}
4112#endif