blob: c3d3cd9c2a532be4abab2a94ae64237e5516369d [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 Rostedt78d904b2009-02-05 18:43:07 -05008#include <linux/ftrace_irq.h>
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04009#include <linux/spinlock.h>
10#include <linux/debugfs.h>
11#include <linux/uaccess.h>
Steven Rostedta81bd802009-02-06 01:45:16 -050012#include <linux/hardirq.h>
Vegard Nossum1744a212009-02-28 08:29:44 +010013#include <linux/kmemcheck.h>
Steven Rostedt7a8e76a2008-09-29 23:02:38 -040014#include <linux/module.h>
15#include <linux/percpu.h>
16#include <linux/mutex.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090017#include <linux/slab.h>
Steven Rostedt7a8e76a2008-09-29 23:02:38 -040018#include <linux/init.h>
19#include <linux/hash.h>
20#include <linux/list.h>
Steven Rostedt554f7862009-03-11 22:00:13 -040021#include <linux/cpu.h>
Steven Rostedt7a8e76a2008-09-29 23:02:38 -040022#include <linux/fs.h>
23
Christoph Lameter79615762010-01-05 15:34:50 +090024#include <asm/local.h>
Steven Rostedt182e9f52008-11-03 23:15:56 -050025#include "trace.h"
26
Steven Rostedt033601a2008-11-21 12:41:55 -050027/*
Steven Rostedtd1b182a2009-04-15 16:53:47 -040028 * The ring buffer header is special. We must manually up keep it.
29 */
30int ring_buffer_print_entry_header(struct trace_seq *s)
31{
32 int ret;
33
Lai Jiangshan334d4162009-04-24 11:27:05 +080034 ret = trace_seq_printf(s, "# compressed entry header\n");
35 ret = trace_seq_printf(s, "\ttype_len : 5 bits\n");
Steven Rostedtd1b182a2009-04-15 16:53:47 -040036 ret = trace_seq_printf(s, "\ttime_delta : 27 bits\n");
37 ret = trace_seq_printf(s, "\tarray : 32 bits\n");
38 ret = trace_seq_printf(s, "\n");
39 ret = trace_seq_printf(s, "\tpadding : type == %d\n",
40 RINGBUF_TYPE_PADDING);
41 ret = trace_seq_printf(s, "\ttime_extend : type == %d\n",
42 RINGBUF_TYPE_TIME_EXTEND);
Lai Jiangshan334d4162009-04-24 11:27:05 +080043 ret = trace_seq_printf(s, "\tdata max type_len == %d\n",
44 RINGBUF_TYPE_DATA_TYPE_LEN_MAX);
Steven Rostedtd1b182a2009-04-15 16:53:47 -040045
46 return ret;
47}
48
49/*
Steven Rostedt5cc98542009-03-12 22:24:17 -040050 * The ring buffer is made up of a list of pages. A separate list of pages is
51 * allocated for each CPU. A writer may only write to a buffer that is
52 * associated with the CPU it is currently executing on. A reader may read
53 * from any per cpu buffer.
54 *
55 * The reader is special. For each per cpu buffer, the reader has its own
56 * reader page. When a reader has read the entire reader page, this reader
57 * page is swapped with another page in the ring buffer.
58 *
59 * Now, as long as the writer is off the reader page, the reader can do what
60 * ever it wants with that page. The writer will never write to that page
61 * again (as long as it is out of the ring buffer).
62 *
63 * Here's some silly ASCII art.
64 *
65 * +------+
66 * |reader| RING BUFFER
67 * |page |
68 * +------+ +---+ +---+ +---+
69 * | |-->| |-->| |
70 * +---+ +---+ +---+
71 * ^ |
72 * | |
73 * +---------------+
74 *
75 *
76 * +------+
77 * |reader| RING BUFFER
78 * |page |------------------v
79 * +------+ +---+ +---+ +---+
80 * | |-->| |-->| |
81 * +---+ +---+ +---+
82 * ^ |
83 * | |
84 * +---------------+
85 *
86 *
87 * +------+
88 * |reader| RING BUFFER
89 * |page |------------------v
90 * +------+ +---+ +---+ +---+
91 * ^ | |-->| |-->| |
92 * | +---+ +---+ +---+
93 * | |
94 * | |
95 * +------------------------------+
96 *
97 *
98 * +------+
99 * |buffer| RING BUFFER
100 * |page |------------------v
101 * +------+ +---+ +---+ +---+
102 * ^ | | | |-->| |
103 * | New +---+ +---+ +---+
104 * | Reader------^ |
105 * | page |
106 * +------------------------------+
107 *
108 *
109 * After we make this swap, the reader can hand this page off to the splice
110 * code and be done with it. It can even allocate a new page if it needs to
111 * and swap that into the ring buffer.
112 *
113 * We will be using cmpxchg soon to make all this lockless.
114 *
115 */
116
117/*
Steven Rostedt033601a2008-11-21 12:41:55 -0500118 * A fast way to enable or disable all ring buffers is to
119 * call tracing_on or tracing_off. Turning off the ring buffers
120 * prevents all ring buffers from being recorded to.
121 * Turning this switch on, makes it OK to write to the
122 * ring buffer, if the ring buffer is enabled itself.
123 *
124 * There's three layers that must be on in order to write
125 * to the ring buffer.
126 *
127 * 1) This global flag must be set.
128 * 2) The ring buffer must be enabled for recording.
129 * 3) The per cpu buffer must be enabled for recording.
130 *
131 * In case of an anomaly, this global flag has a bit set that
132 * will permantly disable all ring buffers.
133 */
134
135/*
136 * Global flag to disable all recording to ring buffers
137 * This has two bits: ON, DISABLED
138 *
139 * ON DISABLED
140 * ---- ----------
141 * 0 0 : ring buffers are off
142 * 1 0 : ring buffers are on
143 * X 1 : ring buffers are permanently disabled
144 */
145
146enum {
147 RB_BUFFERS_ON_BIT = 0,
148 RB_BUFFERS_DISABLED_BIT = 1,
149};
150
151enum {
152 RB_BUFFERS_ON = 1 << RB_BUFFERS_ON_BIT,
153 RB_BUFFERS_DISABLED = 1 << RB_BUFFERS_DISABLED_BIT,
154};
155
Hannes Eder5e398412009-02-10 19:44:34 +0100156static unsigned long ring_buffer_flags __read_mostly = RB_BUFFERS_ON;
Steven Rostedta3583242008-11-11 15:01:42 -0500157
Steven Rostedt474d32b2009-03-03 19:51:40 -0500158#define BUF_PAGE_HDR_SIZE offsetof(struct buffer_data_page, data)
159
Steven Rostedta3583242008-11-11 15:01:42 -0500160/**
161 * tracing_on - enable all tracing buffers
162 *
163 * This function enables all tracing buffers that may have been
164 * disabled with tracing_off.
165 */
166void tracing_on(void)
167{
Steven Rostedt033601a2008-11-21 12:41:55 -0500168 set_bit(RB_BUFFERS_ON_BIT, &ring_buffer_flags);
Steven Rostedta3583242008-11-11 15:01:42 -0500169}
Robert Richterc4f50182008-12-11 16:49:22 +0100170EXPORT_SYMBOL_GPL(tracing_on);
Steven Rostedta3583242008-11-11 15:01:42 -0500171
172/**
173 * tracing_off - turn off all tracing buffers
174 *
175 * This function stops all tracing buffers from recording data.
176 * It does not disable any overhead the tracers themselves may
177 * be causing. This function simply causes all recording to
178 * the ring buffers to fail.
179 */
180void tracing_off(void)
181{
Steven Rostedt033601a2008-11-21 12:41:55 -0500182 clear_bit(RB_BUFFERS_ON_BIT, &ring_buffer_flags);
183}
Robert Richterc4f50182008-12-11 16:49:22 +0100184EXPORT_SYMBOL_GPL(tracing_off);
Steven Rostedt033601a2008-11-21 12:41:55 -0500185
186/**
187 * tracing_off_permanent - permanently disable ring buffers
188 *
189 * This function, once called, will disable all ring buffers
Wenji Huangc3706f02009-02-10 01:03:18 -0500190 * permanently.
Steven Rostedt033601a2008-11-21 12:41:55 -0500191 */
192void tracing_off_permanent(void)
193{
194 set_bit(RB_BUFFERS_DISABLED_BIT, &ring_buffer_flags);
Steven Rostedta3583242008-11-11 15:01:42 -0500195}
196
Steven Rostedt988ae9d2009-02-14 19:17:02 -0500197/**
198 * tracing_is_on - show state of ring buffers enabled
199 */
200int tracing_is_on(void)
201{
202 return ring_buffer_flags == RB_BUFFERS_ON;
203}
204EXPORT_SYMBOL_GPL(tracing_is_on);
205
Steven Rostedte3d6bf02009-03-03 13:53:07 -0500206#define RB_EVNT_HDR_SIZE (offsetof(struct ring_buffer_event, array))
Andrew Morton67d34722009-01-09 12:27:09 -0800207#define RB_ALIGNMENT 4U
Lai Jiangshan334d4162009-04-24 11:27:05 +0800208#define RB_MAX_SMALL_DATA (RB_ALIGNMENT * RINGBUF_TYPE_DATA_TYPE_LEN_MAX)
Steven Rostedtc7b09302009-06-11 11:12:00 -0400209#define RB_EVNT_MIN_SIZE 8U /* two 32bit words */
Lai Jiangshan334d4162009-04-24 11:27:05 +0800210
Steven Rostedt22710482010-03-18 17:54:19 -0400211#if !defined(CONFIG_64BIT) || defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)
212# define RB_FORCE_8BYTE_ALIGNMENT 0
213# define RB_ARCH_ALIGNMENT RB_ALIGNMENT
214#else
215# define RB_FORCE_8BYTE_ALIGNMENT 1
216# define RB_ARCH_ALIGNMENT 8U
217#endif
218
Lai Jiangshan334d4162009-04-24 11:27:05 +0800219/* define RINGBUF_TYPE_DATA for 'case RINGBUF_TYPE_DATA:' */
220#define RINGBUF_TYPE_DATA 0 ... RINGBUF_TYPE_DATA_TYPE_LEN_MAX
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400221
222enum {
223 RB_LEN_TIME_EXTEND = 8,
224 RB_LEN_TIME_STAMP = 16,
225};
226
Tom Zanussi2d622712009-03-22 03:30:49 -0500227static inline int rb_null_event(struct ring_buffer_event *event)
228{
Steven Rostedta1863c22009-09-03 10:23:58 -0400229 return event->type_len == RINGBUF_TYPE_PADDING && !event->time_delta;
Tom Zanussi2d622712009-03-22 03:30:49 -0500230}
231
232static void rb_event_set_padding(struct ring_buffer_event *event)
233{
Steven Rostedta1863c22009-09-03 10:23:58 -0400234 /* padding has a NULL time_delta */
Lai Jiangshan334d4162009-04-24 11:27:05 +0800235 event->type_len = RINGBUF_TYPE_PADDING;
Tom Zanussi2d622712009-03-22 03:30:49 -0500236 event->time_delta = 0;
237}
238
Tom Zanussi2d622712009-03-22 03:30:49 -0500239static unsigned
240rb_event_data_length(struct ring_buffer_event *event)
241{
242 unsigned length;
243
Lai Jiangshan334d4162009-04-24 11:27:05 +0800244 if (event->type_len)
245 length = event->type_len * RB_ALIGNMENT;
Tom Zanussi2d622712009-03-22 03:30:49 -0500246 else
247 length = event->array[0];
248 return length + RB_EVNT_HDR_SIZE;
249}
250
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400251/* inline for ring buffer fast paths */
Andrew Morton34a148b2009-01-09 12:27:09 -0800252static unsigned
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400253rb_event_length(struct ring_buffer_event *event)
254{
Lai Jiangshan334d4162009-04-24 11:27:05 +0800255 switch (event->type_len) {
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400256 case RINGBUF_TYPE_PADDING:
Tom Zanussi2d622712009-03-22 03:30:49 -0500257 if (rb_null_event(event))
258 /* undefined */
259 return -1;
Lai Jiangshan334d4162009-04-24 11:27:05 +0800260 return event->array[0] + RB_EVNT_HDR_SIZE;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400261
262 case RINGBUF_TYPE_TIME_EXTEND:
263 return RB_LEN_TIME_EXTEND;
264
265 case RINGBUF_TYPE_TIME_STAMP:
266 return RB_LEN_TIME_STAMP;
267
268 case RINGBUF_TYPE_DATA:
Tom Zanussi2d622712009-03-22 03:30:49 -0500269 return rb_event_data_length(event);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400270 default:
271 BUG();
272 }
273 /* not hit */
274 return 0;
275}
276
277/**
278 * ring_buffer_event_length - return the length of the event
279 * @event: the event to get the length of
280 */
281unsigned ring_buffer_event_length(struct ring_buffer_event *event)
282{
Robert Richter465634a2009-01-07 15:32:11 +0100283 unsigned length = rb_event_length(event);
Lai Jiangshan334d4162009-04-24 11:27:05 +0800284 if (event->type_len > RINGBUF_TYPE_DATA_TYPE_LEN_MAX)
Robert Richter465634a2009-01-07 15:32:11 +0100285 return length;
286 length -= RB_EVNT_HDR_SIZE;
287 if (length > RB_MAX_SMALL_DATA + sizeof(event->array[0]))
288 length -= sizeof(event->array[0]);
289 return length;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400290}
Robert Richterc4f50182008-12-11 16:49:22 +0100291EXPORT_SYMBOL_GPL(ring_buffer_event_length);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400292
293/* inline for ring buffer fast paths */
Andrew Morton34a148b2009-01-09 12:27:09 -0800294static void *
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400295rb_event_data(struct ring_buffer_event *event)
296{
Lai Jiangshan334d4162009-04-24 11:27:05 +0800297 BUG_ON(event->type_len > RINGBUF_TYPE_DATA_TYPE_LEN_MAX);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400298 /* If length is in len field, then array[0] has the data */
Lai Jiangshan334d4162009-04-24 11:27:05 +0800299 if (event->type_len)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400300 return (void *)&event->array[0];
301 /* Otherwise length is in array[0] and array[1] has the data */
302 return (void *)&event->array[1];
303}
304
305/**
306 * ring_buffer_event_data - return the data of the event
307 * @event: the event to get the data from
308 */
309void *ring_buffer_event_data(struct ring_buffer_event *event)
310{
311 return rb_event_data(event);
312}
Robert Richterc4f50182008-12-11 16:49:22 +0100313EXPORT_SYMBOL_GPL(ring_buffer_event_data);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400314
315#define for_each_buffer_cpu(buffer, cpu) \
Rusty Russell9e01c1b2009-01-01 10:12:22 +1030316 for_each_cpu(cpu, buffer->cpumask)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400317
318#define TS_SHIFT 27
319#define TS_MASK ((1ULL << TS_SHIFT) - 1)
320#define TS_DELTA_TEST (~TS_MASK)
321
Steven Rostedt66a8cb92010-03-31 13:21:56 -0400322/* Flag when events were overwritten */
323#define RB_MISSED_EVENTS (1 << 31)
Steven Rostedtff0ff842010-03-31 22:11:42 -0400324/* Missed count stored at end */
325#define RB_MISSED_STORED (1 << 30)
Steven Rostedt66a8cb92010-03-31 13:21:56 -0400326
Steven Rostedtabc9b562008-12-02 15:34:06 -0500327struct buffer_data_page {
Steven Rostedte4c2ce82008-10-01 11:14:54 -0400328 u64 time_stamp; /* page time stamp */
Wenji Huangc3706f02009-02-10 01:03:18 -0500329 local_t commit; /* write committed index */
Steven Rostedtabc9b562008-12-02 15:34:06 -0500330 unsigned char data[]; /* data of buffer page */
331};
332
Steven Rostedt77ae3652009-03-27 11:00:29 -0400333/*
334 * Note, the buffer_page list must be first. The buffer pages
335 * are allocated in cache lines, which means that each buffer
336 * page will be at the beginning of a cache line, and thus
337 * the least significant bits will be zero. We use this to
338 * add flags in the list struct pointers, to make the ring buffer
339 * lockless.
340 */
Steven Rostedtabc9b562008-12-02 15:34:06 -0500341struct buffer_page {
Steven Rostedt778c55d2009-05-01 18:44:45 -0400342 struct list_head list; /* list of buffer pages */
Steven Rostedtabc9b562008-12-02 15:34:06 -0500343 local_t write; /* index for next write */
Steven Rostedt6f807ac2008-10-04 02:00:58 -0400344 unsigned read; /* index for next read */
Steven Rostedt778c55d2009-05-01 18:44:45 -0400345 local_t entries; /* entries on this page */
Steven Rostedtff0ff842010-03-31 22:11:42 -0400346 unsigned long real_end; /* real end of data */
Steven Rostedtabc9b562008-12-02 15:34:06 -0500347 struct buffer_data_page *page; /* Actual data page */
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400348};
349
Steven Rostedt77ae3652009-03-27 11:00:29 -0400350/*
351 * The buffer page counters, write and entries, must be reset
352 * atomically when crossing page boundaries. To synchronize this
353 * update, two counters are inserted into the number. One is
354 * the actual counter for the write position or count on the page.
355 *
356 * The other is a counter of updaters. Before an update happens
357 * the update partition of the counter is incremented. This will
358 * allow the updater to update the counter atomically.
359 *
360 * The counter is 20 bits, and the state data is 12.
361 */
362#define RB_WRITE_MASK 0xfffff
363#define RB_WRITE_INTCNT (1 << 20)
364
Steven Rostedt044fa782008-12-02 23:50:03 -0500365static void rb_init_page(struct buffer_data_page *bpage)
Steven Rostedtabc9b562008-12-02 15:34:06 -0500366{
Steven Rostedt044fa782008-12-02 23:50:03 -0500367 local_set(&bpage->commit, 0);
Steven Rostedtabc9b562008-12-02 15:34:06 -0500368}
369
Steven Rostedt474d32b2009-03-03 19:51:40 -0500370/**
371 * ring_buffer_page_len - the size of data on the page.
372 * @page: The page to read
373 *
374 * Returns the amount of data on the page, including buffer page header.
375 */
Steven Rostedtef7a4a12009-03-03 00:27:49 -0500376size_t ring_buffer_page_len(void *page)
377{
Steven Rostedt474d32b2009-03-03 19:51:40 -0500378 return local_read(&((struct buffer_data_page *)page)->commit)
379 + BUF_PAGE_HDR_SIZE;
Steven Rostedtef7a4a12009-03-03 00:27:49 -0500380}
381
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400382/*
Steven Rostedted568292008-09-29 23:02:40 -0400383 * Also stolen from mm/slob.c. Thanks to Mathieu Desnoyers for pointing
384 * this issue out.
385 */
Andrew Morton34a148b2009-01-09 12:27:09 -0800386static void free_buffer_page(struct buffer_page *bpage)
Steven Rostedted568292008-09-29 23:02:40 -0400387{
Andrew Morton34a148b2009-01-09 12:27:09 -0800388 free_page((unsigned long)bpage->page);
Steven Rostedte4c2ce82008-10-01 11:14:54 -0400389 kfree(bpage);
Steven Rostedted568292008-09-29 23:02:40 -0400390}
391
392/*
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400393 * We need to fit the time_stamp delta into 27 bits.
394 */
395static inline int test_time_stamp(u64 delta)
396{
397 if (delta & TS_DELTA_TEST)
398 return 1;
399 return 0;
400}
401
Steven Rostedt474d32b2009-03-03 19:51:40 -0500402#define BUF_PAGE_SIZE (PAGE_SIZE - BUF_PAGE_HDR_SIZE)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400403
Steven Rostedtbe957c42009-05-11 14:42:53 -0400404/* Max payload is BUF_PAGE_SIZE - header (8bytes) */
405#define BUF_MAX_DATA_SIZE (BUF_PAGE_SIZE - (sizeof(u32) * 2))
406
Steven Rostedtea05b572009-06-03 09:30:10 -0400407/* Max number of timestamps that can fit on a page */
408#define RB_TIMESTAMPS_PER_PAGE (BUF_PAGE_SIZE / RB_LEN_TIME_STAMP)
409
Steven Rostedtd1b182a2009-04-15 16:53:47 -0400410int ring_buffer_print_page_header(struct trace_seq *s)
411{
412 struct buffer_data_page field;
413 int ret;
414
415 ret = trace_seq_printf(s, "\tfield: u64 timestamp;\t"
Tom Zanussi26a50742009-10-06 01:09:50 -0500416 "offset:0;\tsize:%u;\tsigned:%u;\n",
417 (unsigned int)sizeof(field.time_stamp),
418 (unsigned int)is_signed_type(u64));
Steven Rostedtd1b182a2009-04-15 16:53:47 -0400419
420 ret = trace_seq_printf(s, "\tfield: local_t commit;\t"
Tom Zanussi26a50742009-10-06 01:09:50 -0500421 "offset:%u;\tsize:%u;\tsigned:%u;\n",
Steven Rostedtd1b182a2009-04-15 16:53:47 -0400422 (unsigned int)offsetof(typeof(field), commit),
Tom Zanussi26a50742009-10-06 01:09:50 -0500423 (unsigned int)sizeof(field.commit),
424 (unsigned int)is_signed_type(long));
Steven Rostedtd1b182a2009-04-15 16:53:47 -0400425
Steven Rostedt66a8cb92010-03-31 13:21:56 -0400426 ret = trace_seq_printf(s, "\tfield: int overwrite;\t"
427 "offset:%u;\tsize:%u;\tsigned:%u;\n",
428 (unsigned int)offsetof(typeof(field), commit),
429 1,
430 (unsigned int)is_signed_type(long));
431
Steven Rostedtd1b182a2009-04-15 16:53:47 -0400432 ret = trace_seq_printf(s, "\tfield: char data;\t"
Tom Zanussi26a50742009-10-06 01:09:50 -0500433 "offset:%u;\tsize:%u;\tsigned:%u;\n",
Steven Rostedtd1b182a2009-04-15 16:53:47 -0400434 (unsigned int)offsetof(typeof(field), data),
Tom Zanussi26a50742009-10-06 01:09:50 -0500435 (unsigned int)BUF_PAGE_SIZE,
436 (unsigned int)is_signed_type(char));
Steven Rostedtd1b182a2009-04-15 16:53:47 -0400437
438 return ret;
439}
440
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400441/*
442 * head_page == tail_page && head == tail then buffer is empty.
443 */
444struct ring_buffer_per_cpu {
445 int cpu;
446 struct ring_buffer *buffer;
Steven Rostedt77ae3652009-03-27 11:00:29 -0400447 spinlock_t reader_lock; /* serialize readers */
Thomas Gleixner445c8952009-12-02 19:49:50 +0100448 arch_spinlock_t lock;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400449 struct lock_class_key lock_key;
Steven Rostedt3adc54f2009-03-30 15:32:01 -0400450 struct list_head *pages;
Steven Rostedt6f807ac2008-10-04 02:00:58 -0400451 struct buffer_page *head_page; /* read from head */
452 struct buffer_page *tail_page; /* write to tail */
Wenji Huangc3706f02009-02-10 01:03:18 -0500453 struct buffer_page *commit_page; /* committed pages */
Steven Rostedtd7690412008-10-01 00:29:53 -0400454 struct buffer_page *reader_page;
Steven Rostedt66a8cb92010-03-31 13:21:56 -0400455 unsigned long lost_events;
456 unsigned long last_overrun;
Steven Rostedt77ae3652009-03-27 11:00:29 -0400457 local_t commit_overrun;
458 local_t overrun;
Steven Rostedte4906ef2009-04-30 20:49:44 -0400459 local_t entries;
Steven Rostedtfa743952009-06-16 12:37:57 -0400460 local_t committing;
461 local_t commits;
Steven Rostedt77ae3652009-03-27 11:00:29 -0400462 unsigned long read;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400463 u64 write_stamp;
464 u64 read_stamp;
465 atomic_t record_disabled;
466};
467
468struct ring_buffer {
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400469 unsigned pages;
470 unsigned flags;
471 int cpus;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400472 atomic_t record_disabled;
Arnaldo Carvalho de Melo00f62f62009-02-09 17:04:06 -0200473 cpumask_var_t cpumask;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400474
Peter Zijlstra1f8a6a12009-06-08 18:18:39 +0200475 struct lock_class_key *reader_lock_key;
476
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400477 struct mutex mutex;
478
479 struct ring_buffer_per_cpu **buffers;
Steven Rostedt554f7862009-03-11 22:00:13 -0400480
Steven Rostedt59222ef2009-03-12 11:46:03 -0400481#ifdef CONFIG_HOTPLUG_CPU
Steven Rostedt554f7862009-03-11 22:00:13 -0400482 struct notifier_block cpu_notify;
483#endif
Steven Rostedt37886f62009-03-17 17:22:06 -0400484 u64 (*clock)(void);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400485};
486
487struct ring_buffer_iter {
488 struct ring_buffer_per_cpu *cpu_buffer;
489 unsigned long head;
490 struct buffer_page *head_page;
Steven Rostedt492a74f2010-01-25 15:17:47 -0500491 struct buffer_page *cache_reader_page;
492 unsigned long cache_read;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400493 u64 read_stamp;
494};
495
Steven Rostedtf536aaf2008-11-10 23:07:30 -0500496/* buffer may be either ring_buffer or ring_buffer_per_cpu */
Steven Rostedt077c5402009-09-03 19:53:46 -0400497#define RB_WARN_ON(b, cond) \
498 ({ \
499 int _____ret = unlikely(cond); \
500 if (_____ret) { \
501 if (__same_type(*(b), struct ring_buffer_per_cpu)) { \
502 struct ring_buffer_per_cpu *__b = \
503 (void *)b; \
504 atomic_inc(&__b->buffer->record_disabled); \
505 } else \
506 atomic_inc(&b->record_disabled); \
507 WARN_ON(1); \
508 } \
509 _____ret; \
Steven Rostedt3e89c7b2008-11-11 15:28:41 -0500510 })
Steven Rostedtf536aaf2008-11-10 23:07:30 -0500511
Steven Rostedt37886f62009-03-17 17:22:06 -0400512/* Up this if you want to test the TIME_EXTENTS and normalization */
513#define DEBUG_SHIFT 0
514
Jiri Olsa6d3f1e12009-10-23 19:36:19 -0400515static inline u64 rb_time_stamp(struct ring_buffer *buffer)
Steven Rostedt88eb0122009-05-11 16:28:23 -0400516{
517 /* shift to debug/test normalization and TIME_EXTENTS */
518 return buffer->clock() << DEBUG_SHIFT;
519}
520
Steven Rostedt37886f62009-03-17 17:22:06 -0400521u64 ring_buffer_time_stamp(struct ring_buffer *buffer, int cpu)
522{
523 u64 time;
524
525 preempt_disable_notrace();
Jiri Olsa6d3f1e12009-10-23 19:36:19 -0400526 time = rb_time_stamp(buffer);
Steven Rostedt37886f62009-03-17 17:22:06 -0400527 preempt_enable_no_resched_notrace();
528
529 return time;
530}
531EXPORT_SYMBOL_GPL(ring_buffer_time_stamp);
532
533void ring_buffer_normalize_time_stamp(struct ring_buffer *buffer,
534 int cpu, u64 *ts)
535{
536 /* Just stupid testing the normalize function and deltas */
537 *ts >>= DEBUG_SHIFT;
538}
539EXPORT_SYMBOL_GPL(ring_buffer_normalize_time_stamp);
540
Steven Rostedt77ae3652009-03-27 11:00:29 -0400541/*
542 * Making the ring buffer lockless makes things tricky.
543 * Although writes only happen on the CPU that they are on,
544 * and they only need to worry about interrupts. Reads can
545 * happen on any CPU.
546 *
547 * The reader page is always off the ring buffer, but when the
548 * reader finishes with a page, it needs to swap its page with
549 * a new one from the buffer. The reader needs to take from
550 * the head (writes go to the tail). But if a writer is in overwrite
551 * mode and wraps, it must push the head page forward.
552 *
553 * Here lies the problem.
554 *
555 * The reader must be careful to replace only the head page, and
556 * not another one. As described at the top of the file in the
557 * ASCII art, the reader sets its old page to point to the next
558 * page after head. It then sets the page after head to point to
559 * the old reader page. But if the writer moves the head page
560 * during this operation, the reader could end up with the tail.
561 *
562 * We use cmpxchg to help prevent this race. We also do something
563 * special with the page before head. We set the LSB to 1.
564 *
565 * When the writer must push the page forward, it will clear the
566 * bit that points to the head page, move the head, and then set
567 * the bit that points to the new head page.
568 *
569 * We also don't want an interrupt coming in and moving the head
570 * page on another writer. Thus we use the second LSB to catch
571 * that too. Thus:
572 *
573 * head->list->prev->next bit 1 bit 0
574 * ------- -------
575 * Normal page 0 0
576 * Points to head page 0 1
577 * New head page 1 0
578 *
579 * Note we can not trust the prev pointer of the head page, because:
580 *
581 * +----+ +-----+ +-----+
582 * | |------>| T |---X--->| N |
583 * | |<------| | | |
584 * +----+ +-----+ +-----+
585 * ^ ^ |
586 * | +-----+ | |
587 * +----------| R |----------+ |
588 * | |<-----------+
589 * +-----+
590 *
591 * Key: ---X--> HEAD flag set in pointer
592 * T Tail page
593 * R Reader page
594 * N Next page
595 *
596 * (see __rb_reserve_next() to see where this happens)
597 *
598 * What the above shows is that the reader just swapped out
599 * the reader page with a page in the buffer, but before it
600 * could make the new header point back to the new page added
601 * it was preempted by a writer. The writer moved forward onto
602 * the new page added by the reader and is about to move forward
603 * again.
604 *
605 * You can see, it is legitimate for the previous pointer of
606 * the head (or any page) not to point back to itself. But only
607 * temporarially.
608 */
609
610#define RB_PAGE_NORMAL 0UL
611#define RB_PAGE_HEAD 1UL
612#define RB_PAGE_UPDATE 2UL
613
614
615#define RB_FLAG_MASK 3UL
616
617/* PAGE_MOVED is not part of the mask */
618#define RB_PAGE_MOVED 4UL
619
620/*
621 * rb_list_head - remove any bit
622 */
623static struct list_head *rb_list_head(struct list_head *list)
624{
625 unsigned long val = (unsigned long)list;
626
627 return (struct list_head *)(val & ~RB_FLAG_MASK);
628}
629
630/*
Jiri Olsa6d3f1e12009-10-23 19:36:19 -0400631 * rb_is_head_page - test if the given page is the head page
Steven Rostedt77ae3652009-03-27 11:00:29 -0400632 *
633 * Because the reader may move the head_page pointer, we can
634 * not trust what the head page is (it may be pointing to
635 * the reader page). But if the next page is a header page,
636 * its flags will be non zero.
637 */
638static int inline
639rb_is_head_page(struct ring_buffer_per_cpu *cpu_buffer,
640 struct buffer_page *page, struct list_head *list)
641{
642 unsigned long val;
643
644 val = (unsigned long)list->next;
645
646 if ((val & ~RB_FLAG_MASK) != (unsigned long)&page->list)
647 return RB_PAGE_MOVED;
648
649 return val & RB_FLAG_MASK;
650}
651
652/*
653 * rb_is_reader_page
654 *
655 * The unique thing about the reader page, is that, if the
656 * writer is ever on it, the previous pointer never points
657 * back to the reader page.
658 */
659static int rb_is_reader_page(struct buffer_page *page)
660{
661 struct list_head *list = page->list.prev;
662
663 return rb_list_head(list->next) != &page->list;
664}
665
666/*
667 * rb_set_list_to_head - set a list_head to be pointing to head.
668 */
669static void rb_set_list_to_head(struct ring_buffer_per_cpu *cpu_buffer,
670 struct list_head *list)
671{
672 unsigned long *ptr;
673
674 ptr = (unsigned long *)&list->next;
675 *ptr |= RB_PAGE_HEAD;
676 *ptr &= ~RB_PAGE_UPDATE;
677}
678
679/*
680 * rb_head_page_activate - sets up head page
681 */
682static void rb_head_page_activate(struct ring_buffer_per_cpu *cpu_buffer)
683{
684 struct buffer_page *head;
685
686 head = cpu_buffer->head_page;
687 if (!head)
688 return;
689
690 /*
691 * Set the previous list pointer to have the HEAD flag.
692 */
693 rb_set_list_to_head(cpu_buffer, head->list.prev);
694}
695
696static void rb_list_head_clear(struct list_head *list)
697{
698 unsigned long *ptr = (unsigned long *)&list->next;
699
700 *ptr &= ~RB_FLAG_MASK;
701}
702
703/*
704 * rb_head_page_dactivate - clears head page ptr (for free list)
705 */
706static void
707rb_head_page_deactivate(struct ring_buffer_per_cpu *cpu_buffer)
708{
709 struct list_head *hd;
710
711 /* Go through the whole list and clear any pointers found. */
712 rb_list_head_clear(cpu_buffer->pages);
713
714 list_for_each(hd, cpu_buffer->pages)
715 rb_list_head_clear(hd);
716}
717
718static int rb_head_page_set(struct ring_buffer_per_cpu *cpu_buffer,
719 struct buffer_page *head,
720 struct buffer_page *prev,
721 int old_flag, int new_flag)
722{
723 struct list_head *list;
724 unsigned long val = (unsigned long)&head->list;
725 unsigned long ret;
726
727 list = &prev->list;
728
729 val &= ~RB_FLAG_MASK;
730
Steven Rostedt08a40812009-09-14 09:31:35 -0400731 ret = cmpxchg((unsigned long *)&list->next,
732 val | old_flag, val | new_flag);
Steven Rostedt77ae3652009-03-27 11:00:29 -0400733
734 /* check if the reader took the page */
735 if ((ret & ~RB_FLAG_MASK) != val)
736 return RB_PAGE_MOVED;
737
738 return ret & RB_FLAG_MASK;
739}
740
741static int rb_head_page_set_update(struct ring_buffer_per_cpu *cpu_buffer,
742 struct buffer_page *head,
743 struct buffer_page *prev,
744 int old_flag)
745{
746 return rb_head_page_set(cpu_buffer, head, prev,
747 old_flag, RB_PAGE_UPDATE);
748}
749
750static int rb_head_page_set_head(struct ring_buffer_per_cpu *cpu_buffer,
751 struct buffer_page *head,
752 struct buffer_page *prev,
753 int old_flag)
754{
755 return rb_head_page_set(cpu_buffer, head, prev,
756 old_flag, RB_PAGE_HEAD);
757}
758
759static int rb_head_page_set_normal(struct ring_buffer_per_cpu *cpu_buffer,
760 struct buffer_page *head,
761 struct buffer_page *prev,
762 int old_flag)
763{
764 return rb_head_page_set(cpu_buffer, head, prev,
765 old_flag, RB_PAGE_NORMAL);
766}
767
768static inline void rb_inc_page(struct ring_buffer_per_cpu *cpu_buffer,
769 struct buffer_page **bpage)
770{
771 struct list_head *p = rb_list_head((*bpage)->list.next);
772
773 *bpage = list_entry(p, struct buffer_page, list);
774}
775
776static struct buffer_page *
777rb_set_head_page(struct ring_buffer_per_cpu *cpu_buffer)
778{
779 struct buffer_page *head;
780 struct buffer_page *page;
781 struct list_head *list;
782 int i;
783
784 if (RB_WARN_ON(cpu_buffer, !cpu_buffer->head_page))
785 return NULL;
786
787 /* sanity check */
788 list = cpu_buffer->pages;
789 if (RB_WARN_ON(cpu_buffer, rb_list_head(list->prev->next) != list))
790 return NULL;
791
792 page = head = cpu_buffer->head_page;
793 /*
794 * It is possible that the writer moves the header behind
795 * where we started, and we miss in one loop.
796 * A second loop should grab the header, but we'll do
797 * three loops just because I'm paranoid.
798 */
799 for (i = 0; i < 3; i++) {
800 do {
801 if (rb_is_head_page(cpu_buffer, page, page->list.prev)) {
802 cpu_buffer->head_page = page;
803 return page;
804 }
805 rb_inc_page(cpu_buffer, &page);
806 } while (page != head);
807 }
808
809 RB_WARN_ON(cpu_buffer, 1);
810
811 return NULL;
812}
813
814static int rb_head_page_replace(struct buffer_page *old,
815 struct buffer_page *new)
816{
817 unsigned long *ptr = (unsigned long *)&old->list.prev->next;
818 unsigned long val;
819 unsigned long ret;
820
821 val = *ptr & ~RB_FLAG_MASK;
822 val |= RB_PAGE_HEAD;
823
Steven Rostedt08a40812009-09-14 09:31:35 -0400824 ret = cmpxchg(ptr, val, (unsigned long)&new->list);
Steven Rostedt77ae3652009-03-27 11:00:29 -0400825
826 return ret == val;
827}
828
829/*
830 * rb_tail_page_update - move the tail page forward
831 *
832 * Returns 1 if moved tail page, 0 if someone else did.
833 */
834static int rb_tail_page_update(struct ring_buffer_per_cpu *cpu_buffer,
835 struct buffer_page *tail_page,
836 struct buffer_page *next_page)
837{
838 struct buffer_page *old_tail;
839 unsigned long old_entries;
840 unsigned long old_write;
841 int ret = 0;
842
843 /*
844 * The tail page now needs to be moved forward.
845 *
846 * We need to reset the tail page, but without messing
847 * with possible erasing of data brought in by interrupts
848 * that have moved the tail page and are currently on it.
849 *
850 * We add a counter to the write field to denote this.
851 */
852 old_write = local_add_return(RB_WRITE_INTCNT, &next_page->write);
853 old_entries = local_add_return(RB_WRITE_INTCNT, &next_page->entries);
854
855 /*
856 * Just make sure we have seen our old_write and synchronize
857 * with any interrupts that come in.
858 */
859 barrier();
860
861 /*
862 * If the tail page is still the same as what we think
863 * it is, then it is up to us to update the tail
864 * pointer.
865 */
866 if (tail_page == cpu_buffer->tail_page) {
867 /* Zero the write counter */
868 unsigned long val = old_write & ~RB_WRITE_MASK;
869 unsigned long eval = old_entries & ~RB_WRITE_MASK;
870
871 /*
872 * This will only succeed if an interrupt did
873 * not come in and change it. In which case, we
874 * do not want to modify it.
Lai Jiangshanda706d82009-07-15 16:27:30 +0800875 *
876 * We add (void) to let the compiler know that we do not care
877 * about the return value of these functions. We use the
878 * cmpxchg to only update if an interrupt did not already
879 * do it for us. If the cmpxchg fails, we don't care.
Steven Rostedt77ae3652009-03-27 11:00:29 -0400880 */
Lai Jiangshanda706d82009-07-15 16:27:30 +0800881 (void)local_cmpxchg(&next_page->write, old_write, val);
882 (void)local_cmpxchg(&next_page->entries, old_entries, eval);
Steven Rostedt77ae3652009-03-27 11:00:29 -0400883
884 /*
885 * No need to worry about races with clearing out the commit.
886 * it only can increment when a commit takes place. But that
887 * only happens in the outer most nested commit.
888 */
889 local_set(&next_page->page->commit, 0);
890
891 old_tail = cmpxchg(&cpu_buffer->tail_page,
892 tail_page, next_page);
893
894 if (old_tail == tail_page)
895 ret = 1;
896 }
897
898 return ret;
899}
900
901static int rb_check_bpage(struct ring_buffer_per_cpu *cpu_buffer,
902 struct buffer_page *bpage)
903{
904 unsigned long val = (unsigned long)bpage;
905
906 if (RB_WARN_ON(cpu_buffer, val & RB_FLAG_MASK))
907 return 1;
908
909 return 0;
910}
911
912/**
913 * rb_check_list - make sure a pointer to a list has the last bits zero
914 */
915static int rb_check_list(struct ring_buffer_per_cpu *cpu_buffer,
916 struct list_head *list)
917{
918 if (RB_WARN_ON(cpu_buffer, rb_list_head(list->prev) != list->prev))
919 return 1;
920 if (RB_WARN_ON(cpu_buffer, rb_list_head(list->next) != list->next))
921 return 1;
922 return 0;
923}
924
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400925/**
926 * check_pages - integrity check of buffer pages
927 * @cpu_buffer: CPU buffer with pages to test
928 *
Wenji Huangc3706f02009-02-10 01:03:18 -0500929 * As a safety measure we check to make sure the data pages have not
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400930 * been corrupted.
931 */
932static int rb_check_pages(struct ring_buffer_per_cpu *cpu_buffer)
933{
Steven Rostedt3adc54f2009-03-30 15:32:01 -0400934 struct list_head *head = cpu_buffer->pages;
Steven Rostedt044fa782008-12-02 23:50:03 -0500935 struct buffer_page *bpage, *tmp;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400936
Steven Rostedt77ae3652009-03-27 11:00:29 -0400937 rb_head_page_deactivate(cpu_buffer);
938
Steven Rostedt3e89c7b2008-11-11 15:28:41 -0500939 if (RB_WARN_ON(cpu_buffer, head->next->prev != head))
940 return -1;
941 if (RB_WARN_ON(cpu_buffer, head->prev->next != head))
942 return -1;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400943
Steven Rostedt77ae3652009-03-27 11:00:29 -0400944 if (rb_check_list(cpu_buffer, head))
945 return -1;
946
Steven Rostedt044fa782008-12-02 23:50:03 -0500947 list_for_each_entry_safe(bpage, tmp, head, list) {
Steven Rostedt3e89c7b2008-11-11 15:28:41 -0500948 if (RB_WARN_ON(cpu_buffer,
Steven Rostedt044fa782008-12-02 23:50:03 -0500949 bpage->list.next->prev != &bpage->list))
Steven Rostedt3e89c7b2008-11-11 15:28:41 -0500950 return -1;
951 if (RB_WARN_ON(cpu_buffer,
Steven Rostedt044fa782008-12-02 23:50:03 -0500952 bpage->list.prev->next != &bpage->list))
Steven Rostedt3e89c7b2008-11-11 15:28:41 -0500953 return -1;
Steven Rostedt77ae3652009-03-27 11:00:29 -0400954 if (rb_check_list(cpu_buffer, &bpage->list))
955 return -1;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400956 }
957
Steven Rostedt77ae3652009-03-27 11:00:29 -0400958 rb_head_page_activate(cpu_buffer);
959
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400960 return 0;
961}
962
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400963static int rb_allocate_pages(struct ring_buffer_per_cpu *cpu_buffer,
964 unsigned nr_pages)
965{
Steven Rostedt044fa782008-12-02 23:50:03 -0500966 struct buffer_page *bpage, *tmp;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400967 unsigned long addr;
968 LIST_HEAD(pages);
969 unsigned i;
970
Steven Rostedt3adc54f2009-03-30 15:32:01 -0400971 WARN_ON(!nr_pages);
972
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400973 for (i = 0; i < nr_pages; i++) {
Steven Rostedt044fa782008-12-02 23:50:03 -0500974 bpage = kzalloc_node(ALIGN(sizeof(*bpage), cache_line_size()),
Steven Rostedtaa1e0e3b2008-10-02 19:18:09 -0400975 GFP_KERNEL, cpu_to_node(cpu_buffer->cpu));
Steven Rostedt044fa782008-12-02 23:50:03 -0500976 if (!bpage)
Steven Rostedte4c2ce82008-10-01 11:14:54 -0400977 goto free_pages;
Steven Rostedt77ae3652009-03-27 11:00:29 -0400978
979 rb_check_bpage(cpu_buffer, bpage);
980
Steven Rostedt044fa782008-12-02 23:50:03 -0500981 list_add(&bpage->list, &pages);
Steven Rostedte4c2ce82008-10-01 11:14:54 -0400982
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400983 addr = __get_free_page(GFP_KERNEL);
984 if (!addr)
985 goto free_pages;
Steven Rostedt044fa782008-12-02 23:50:03 -0500986 bpage->page = (void *)addr;
987 rb_init_page(bpage->page);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400988 }
989
Steven Rostedt3adc54f2009-03-30 15:32:01 -0400990 /*
991 * The ring buffer page list is a circular list that does not
992 * start and end with a list head. All page list items point to
993 * other pages.
994 */
995 cpu_buffer->pages = pages.next;
996 list_del(&pages);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400997
998 rb_check_pages(cpu_buffer);
999
1000 return 0;
1001
1002 free_pages:
Steven Rostedt044fa782008-12-02 23:50:03 -05001003 list_for_each_entry_safe(bpage, tmp, &pages, list) {
1004 list_del_init(&bpage->list);
1005 free_buffer_page(bpage);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001006 }
1007 return -ENOMEM;
1008}
1009
1010static struct ring_buffer_per_cpu *
1011rb_allocate_cpu_buffer(struct ring_buffer *buffer, int cpu)
1012{
1013 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedt044fa782008-12-02 23:50:03 -05001014 struct buffer_page *bpage;
Steven Rostedtd7690412008-10-01 00:29:53 -04001015 unsigned long addr;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001016 int ret;
1017
1018 cpu_buffer = kzalloc_node(ALIGN(sizeof(*cpu_buffer), cache_line_size()),
1019 GFP_KERNEL, cpu_to_node(cpu));
1020 if (!cpu_buffer)
1021 return NULL;
1022
1023 cpu_buffer->cpu = cpu;
1024 cpu_buffer->buffer = buffer;
Steven Rostedtf83c9d02008-11-11 18:47:44 +01001025 spin_lock_init(&cpu_buffer->reader_lock);
Peter Zijlstra1f8a6a12009-06-08 18:18:39 +02001026 lockdep_set_class(&cpu_buffer->reader_lock, buffer->reader_lock_key);
Thomas Gleixneredc35bd2009-12-03 12:38:57 +01001027 cpu_buffer->lock = (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001028
Steven Rostedt044fa782008-12-02 23:50:03 -05001029 bpage = kzalloc_node(ALIGN(sizeof(*bpage), cache_line_size()),
Steven Rostedte4c2ce82008-10-01 11:14:54 -04001030 GFP_KERNEL, cpu_to_node(cpu));
Steven Rostedt044fa782008-12-02 23:50:03 -05001031 if (!bpage)
Steven Rostedte4c2ce82008-10-01 11:14:54 -04001032 goto fail_free_buffer;
1033
Steven Rostedt77ae3652009-03-27 11:00:29 -04001034 rb_check_bpage(cpu_buffer, bpage);
1035
Steven Rostedt044fa782008-12-02 23:50:03 -05001036 cpu_buffer->reader_page = bpage;
Steven Rostedtd7690412008-10-01 00:29:53 -04001037 addr = __get_free_page(GFP_KERNEL);
1038 if (!addr)
Steven Rostedte4c2ce82008-10-01 11:14:54 -04001039 goto fail_free_reader;
Steven Rostedt044fa782008-12-02 23:50:03 -05001040 bpage->page = (void *)addr;
1041 rb_init_page(bpage->page);
Steven Rostedte4c2ce82008-10-01 11:14:54 -04001042
Steven Rostedtd7690412008-10-01 00:29:53 -04001043 INIT_LIST_HEAD(&cpu_buffer->reader_page->list);
Steven Rostedtd7690412008-10-01 00:29:53 -04001044
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001045 ret = rb_allocate_pages(cpu_buffer, buffer->pages);
1046 if (ret < 0)
Steven Rostedtd7690412008-10-01 00:29:53 -04001047 goto fail_free_reader;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001048
1049 cpu_buffer->head_page
Steven Rostedt3adc54f2009-03-30 15:32:01 -04001050 = list_entry(cpu_buffer->pages, struct buffer_page, list);
Steven Rostedtbf41a152008-10-04 02:00:59 -04001051 cpu_buffer->tail_page = cpu_buffer->commit_page = cpu_buffer->head_page;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001052
Steven Rostedt77ae3652009-03-27 11:00:29 -04001053 rb_head_page_activate(cpu_buffer);
1054
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001055 return cpu_buffer;
1056
Steven Rostedtd7690412008-10-01 00:29:53 -04001057 fail_free_reader:
1058 free_buffer_page(cpu_buffer->reader_page);
1059
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001060 fail_free_buffer:
1061 kfree(cpu_buffer);
1062 return NULL;
1063}
1064
1065static void rb_free_cpu_buffer(struct ring_buffer_per_cpu *cpu_buffer)
1066{
Steven Rostedt3adc54f2009-03-30 15:32:01 -04001067 struct list_head *head = cpu_buffer->pages;
Steven Rostedt044fa782008-12-02 23:50:03 -05001068 struct buffer_page *bpage, *tmp;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001069
Steven Rostedtd7690412008-10-01 00:29:53 -04001070 free_buffer_page(cpu_buffer->reader_page);
1071
Steven Rostedt77ae3652009-03-27 11:00:29 -04001072 rb_head_page_deactivate(cpu_buffer);
1073
Steven Rostedt3adc54f2009-03-30 15:32:01 -04001074 if (head) {
1075 list_for_each_entry_safe(bpage, tmp, head, list) {
1076 list_del_init(&bpage->list);
1077 free_buffer_page(bpage);
1078 }
1079 bpage = list_entry(head, struct buffer_page, list);
Steven Rostedt044fa782008-12-02 23:50:03 -05001080 free_buffer_page(bpage);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001081 }
Steven Rostedt3adc54f2009-03-30 15:32:01 -04001082
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001083 kfree(cpu_buffer);
1084}
1085
Steven Rostedt59222ef2009-03-12 11:46:03 -04001086#ifdef CONFIG_HOTPLUG_CPU
Frederic Weisbecker09c9e842009-03-21 04:33:36 +01001087static int rb_cpu_notify(struct notifier_block *self,
1088 unsigned long action, void *hcpu);
Steven Rostedt554f7862009-03-11 22:00:13 -04001089#endif
1090
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001091/**
1092 * ring_buffer_alloc - allocate a new ring_buffer
Robert Richter68814b52008-11-24 12:24:12 +01001093 * @size: the size in bytes per cpu that is needed.
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001094 * @flags: attributes to set for the ring buffer.
1095 *
1096 * Currently the only flag that is available is the RB_FL_OVERWRITE
1097 * flag. This flag means that the buffer will overwrite old data
1098 * when the buffer wraps. If this flag is not set, the buffer will
1099 * drop data when the tail hits the head.
1100 */
Peter Zijlstra1f8a6a12009-06-08 18:18:39 +02001101struct ring_buffer *__ring_buffer_alloc(unsigned long size, unsigned flags,
1102 struct lock_class_key *key)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001103{
1104 struct ring_buffer *buffer;
1105 int bsize;
1106 int cpu;
1107
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001108 /* keep it in its own cache line */
1109 buffer = kzalloc(ALIGN(sizeof(*buffer), cache_line_size()),
1110 GFP_KERNEL);
1111 if (!buffer)
1112 return NULL;
1113
Rusty Russell9e01c1b2009-01-01 10:12:22 +10301114 if (!alloc_cpumask_var(&buffer->cpumask, GFP_KERNEL))
1115 goto fail_free_buffer;
1116
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001117 buffer->pages = DIV_ROUND_UP(size, BUF_PAGE_SIZE);
1118 buffer->flags = flags;
Steven Rostedt37886f62009-03-17 17:22:06 -04001119 buffer->clock = trace_clock_local;
Peter Zijlstra1f8a6a12009-06-08 18:18:39 +02001120 buffer->reader_lock_key = key;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001121
1122 /* need at least two pages */
Steven Rostedt5f78abe2009-06-17 14:11:10 -04001123 if (buffer->pages < 2)
1124 buffer->pages = 2;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001125
Frederic Weisbecker3bf832c2009-03-19 14:47:33 +01001126 /*
1127 * In case of non-hotplug cpu, if the ring-buffer is allocated
1128 * in early initcall, it will not be notified of secondary cpus.
1129 * In that off case, we need to allocate for all possible cpus.
1130 */
1131#ifdef CONFIG_HOTPLUG_CPU
Steven Rostedt554f7862009-03-11 22:00:13 -04001132 get_online_cpus();
1133 cpumask_copy(buffer->cpumask, cpu_online_mask);
Frederic Weisbecker3bf832c2009-03-19 14:47:33 +01001134#else
1135 cpumask_copy(buffer->cpumask, cpu_possible_mask);
1136#endif
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001137 buffer->cpus = nr_cpu_ids;
1138
1139 bsize = sizeof(void *) * nr_cpu_ids;
1140 buffer->buffers = kzalloc(ALIGN(bsize, cache_line_size()),
1141 GFP_KERNEL);
1142 if (!buffer->buffers)
Rusty Russell9e01c1b2009-01-01 10:12:22 +10301143 goto fail_free_cpumask;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001144
1145 for_each_buffer_cpu(buffer, cpu) {
1146 buffer->buffers[cpu] =
1147 rb_allocate_cpu_buffer(buffer, cpu);
1148 if (!buffer->buffers[cpu])
1149 goto fail_free_buffers;
1150 }
1151
Steven Rostedt59222ef2009-03-12 11:46:03 -04001152#ifdef CONFIG_HOTPLUG_CPU
Steven Rostedt554f7862009-03-11 22:00:13 -04001153 buffer->cpu_notify.notifier_call = rb_cpu_notify;
1154 buffer->cpu_notify.priority = 0;
1155 register_cpu_notifier(&buffer->cpu_notify);
1156#endif
1157
1158 put_online_cpus();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001159 mutex_init(&buffer->mutex);
1160
1161 return buffer;
1162
1163 fail_free_buffers:
1164 for_each_buffer_cpu(buffer, cpu) {
1165 if (buffer->buffers[cpu])
1166 rb_free_cpu_buffer(buffer->buffers[cpu]);
1167 }
1168 kfree(buffer->buffers);
1169
Rusty Russell9e01c1b2009-01-01 10:12:22 +10301170 fail_free_cpumask:
1171 free_cpumask_var(buffer->cpumask);
Steven Rostedt554f7862009-03-11 22:00:13 -04001172 put_online_cpus();
Rusty Russell9e01c1b2009-01-01 10:12:22 +10301173
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001174 fail_free_buffer:
1175 kfree(buffer);
1176 return NULL;
1177}
Peter Zijlstra1f8a6a12009-06-08 18:18:39 +02001178EXPORT_SYMBOL_GPL(__ring_buffer_alloc);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001179
1180/**
1181 * ring_buffer_free - free a ring buffer.
1182 * @buffer: the buffer to free.
1183 */
1184void
1185ring_buffer_free(struct ring_buffer *buffer)
1186{
1187 int cpu;
1188
Steven Rostedt554f7862009-03-11 22:00:13 -04001189 get_online_cpus();
1190
Steven Rostedt59222ef2009-03-12 11:46:03 -04001191#ifdef CONFIG_HOTPLUG_CPU
Steven Rostedt554f7862009-03-11 22:00:13 -04001192 unregister_cpu_notifier(&buffer->cpu_notify);
1193#endif
1194
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001195 for_each_buffer_cpu(buffer, cpu)
1196 rb_free_cpu_buffer(buffer->buffers[cpu]);
1197
Steven Rostedt554f7862009-03-11 22:00:13 -04001198 put_online_cpus();
1199
Eric Dumazetbd3f0222009-08-07 12:49:29 +02001200 kfree(buffer->buffers);
Rusty Russell9e01c1b2009-01-01 10:12:22 +10301201 free_cpumask_var(buffer->cpumask);
1202
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001203 kfree(buffer);
1204}
Robert Richterc4f50182008-12-11 16:49:22 +01001205EXPORT_SYMBOL_GPL(ring_buffer_free);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001206
Steven Rostedt37886f62009-03-17 17:22:06 -04001207void ring_buffer_set_clock(struct ring_buffer *buffer,
1208 u64 (*clock)(void))
1209{
1210 buffer->clock = clock;
1211}
1212
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001213static void rb_reset_cpu(struct ring_buffer_per_cpu *cpu_buffer);
1214
1215static void
1216rb_remove_pages(struct ring_buffer_per_cpu *cpu_buffer, unsigned nr_pages)
1217{
Steven Rostedt044fa782008-12-02 23:50:03 -05001218 struct buffer_page *bpage;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001219 struct list_head *p;
1220 unsigned i;
1221
Lai Jiangshanf7112942009-11-03 19:42:45 +08001222 spin_lock_irq(&cpu_buffer->reader_lock);
Steven Rostedt77ae3652009-03-27 11:00:29 -04001223 rb_head_page_deactivate(cpu_buffer);
1224
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001225 for (i = 0; i < nr_pages; i++) {
Steven Rostedt3adc54f2009-03-30 15:32:01 -04001226 if (RB_WARN_ON(cpu_buffer, list_empty(cpu_buffer->pages)))
Julia Lawall292f60c2010-03-29 17:37:02 +02001227 goto out;
Steven Rostedt3adc54f2009-03-30 15:32:01 -04001228 p = cpu_buffer->pages->next;
Steven Rostedt044fa782008-12-02 23:50:03 -05001229 bpage = list_entry(p, struct buffer_page, list);
1230 list_del_init(&bpage->list);
1231 free_buffer_page(bpage);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001232 }
Steven Rostedt3adc54f2009-03-30 15:32:01 -04001233 if (RB_WARN_ON(cpu_buffer, list_empty(cpu_buffer->pages)))
Julia Lawall292f60c2010-03-29 17:37:02 +02001234 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001235
1236 rb_reset_cpu(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001237 rb_check_pages(cpu_buffer);
1238
Julia Lawall292f60c2010-03-29 17:37:02 +02001239out:
Steven Rostedtdd7f5942009-12-10 23:20:52 -05001240 spin_unlock_irq(&cpu_buffer->reader_lock);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001241}
1242
1243static void
1244rb_insert_pages(struct ring_buffer_per_cpu *cpu_buffer,
1245 struct list_head *pages, unsigned nr_pages)
1246{
Steven Rostedt044fa782008-12-02 23:50:03 -05001247 struct buffer_page *bpage;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001248 struct list_head *p;
1249 unsigned i;
1250
Steven Rostedt77ae3652009-03-27 11:00:29 -04001251 spin_lock_irq(&cpu_buffer->reader_lock);
1252 rb_head_page_deactivate(cpu_buffer);
1253
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001254 for (i = 0; i < nr_pages; i++) {
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05001255 if (RB_WARN_ON(cpu_buffer, list_empty(pages)))
Julia Lawall292f60c2010-03-29 17:37:02 +02001256 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001257 p = pages->next;
Steven Rostedt044fa782008-12-02 23:50:03 -05001258 bpage = list_entry(p, struct buffer_page, list);
1259 list_del_init(&bpage->list);
Steven Rostedt3adc54f2009-03-30 15:32:01 -04001260 list_add_tail(&bpage->list, cpu_buffer->pages);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001261 }
1262 rb_reset_cpu(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001263 rb_check_pages(cpu_buffer);
1264
Julia Lawall292f60c2010-03-29 17:37:02 +02001265out:
Steven Rostedtdd7f5942009-12-10 23:20:52 -05001266 spin_unlock_irq(&cpu_buffer->reader_lock);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001267}
1268
1269/**
1270 * ring_buffer_resize - resize the ring buffer
1271 * @buffer: the buffer to resize.
1272 * @size: the new size.
1273 *
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001274 * Minimum size is 2 * BUF_PAGE_SIZE.
1275 *
1276 * Returns -1 on failure.
1277 */
1278int ring_buffer_resize(struct ring_buffer *buffer, unsigned long size)
1279{
1280 struct ring_buffer_per_cpu *cpu_buffer;
1281 unsigned nr_pages, rm_pages, new_pages;
Steven Rostedt044fa782008-12-02 23:50:03 -05001282 struct buffer_page *bpage, *tmp;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001283 unsigned long buffer_size;
1284 unsigned long addr;
1285 LIST_HEAD(pages);
1286 int i, cpu;
1287
Ingo Molnaree51a1d2008-11-13 14:58:31 +01001288 /*
1289 * Always succeed at resizing a non-existent buffer:
1290 */
1291 if (!buffer)
1292 return size;
1293
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001294 size = DIV_ROUND_UP(size, BUF_PAGE_SIZE);
1295 size *= BUF_PAGE_SIZE;
1296 buffer_size = buffer->pages * BUF_PAGE_SIZE;
1297
1298 /* we need a minimum of two pages */
1299 if (size < BUF_PAGE_SIZE * 2)
1300 size = BUF_PAGE_SIZE * 2;
1301
1302 if (size == buffer_size)
1303 return size;
1304
Steven Rostedt18421012009-12-10 22:54:27 -05001305 atomic_inc(&buffer->record_disabled);
1306
1307 /* Make sure all writers are done with this buffer. */
1308 synchronize_sched();
1309
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001310 mutex_lock(&buffer->mutex);
Steven Rostedt554f7862009-03-11 22:00:13 -04001311 get_online_cpus();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001312
1313 nr_pages = DIV_ROUND_UP(size, BUF_PAGE_SIZE);
1314
1315 if (size < buffer_size) {
1316
1317 /* easy case, just free pages */
Steven Rostedt554f7862009-03-11 22:00:13 -04001318 if (RB_WARN_ON(buffer, nr_pages >= buffer->pages))
1319 goto out_fail;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001320
1321 rm_pages = buffer->pages - nr_pages;
1322
1323 for_each_buffer_cpu(buffer, cpu) {
1324 cpu_buffer = buffer->buffers[cpu];
1325 rb_remove_pages(cpu_buffer, rm_pages);
1326 }
1327 goto out;
1328 }
1329
1330 /*
1331 * This is a bit more difficult. We only want to add pages
1332 * when we can allocate enough for all CPUs. We do this
1333 * by allocating all the pages and storing them on a local
1334 * link list. If we succeed in our allocation, then we
1335 * add these pages to the cpu_buffers. Otherwise we just free
1336 * them all and return -ENOMEM;
1337 */
Steven Rostedt554f7862009-03-11 22:00:13 -04001338 if (RB_WARN_ON(buffer, nr_pages <= buffer->pages))
1339 goto out_fail;
Steven Rostedtf536aaf2008-11-10 23:07:30 -05001340
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001341 new_pages = nr_pages - buffer->pages;
1342
1343 for_each_buffer_cpu(buffer, cpu) {
1344 for (i = 0; i < new_pages; i++) {
Steven Rostedt044fa782008-12-02 23:50:03 -05001345 bpage = kzalloc_node(ALIGN(sizeof(*bpage),
Steven Rostedte4c2ce82008-10-01 11:14:54 -04001346 cache_line_size()),
1347 GFP_KERNEL, cpu_to_node(cpu));
Steven Rostedt044fa782008-12-02 23:50:03 -05001348 if (!bpage)
Steven Rostedte4c2ce82008-10-01 11:14:54 -04001349 goto free_pages;
Steven Rostedt044fa782008-12-02 23:50:03 -05001350 list_add(&bpage->list, &pages);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001351 addr = __get_free_page(GFP_KERNEL);
1352 if (!addr)
1353 goto free_pages;
Steven Rostedt044fa782008-12-02 23:50:03 -05001354 bpage->page = (void *)addr;
1355 rb_init_page(bpage->page);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001356 }
1357 }
1358
1359 for_each_buffer_cpu(buffer, cpu) {
1360 cpu_buffer = buffer->buffers[cpu];
1361 rb_insert_pages(cpu_buffer, &pages, new_pages);
1362 }
1363
Steven Rostedt554f7862009-03-11 22:00:13 -04001364 if (RB_WARN_ON(buffer, !list_empty(&pages)))
1365 goto out_fail;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001366
1367 out:
1368 buffer->pages = nr_pages;
Steven Rostedt554f7862009-03-11 22:00:13 -04001369 put_online_cpus();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001370 mutex_unlock(&buffer->mutex);
1371
Steven Rostedt18421012009-12-10 22:54:27 -05001372 atomic_dec(&buffer->record_disabled);
1373
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001374 return size;
1375
1376 free_pages:
Steven Rostedt044fa782008-12-02 23:50:03 -05001377 list_for_each_entry_safe(bpage, tmp, &pages, list) {
1378 list_del_init(&bpage->list);
1379 free_buffer_page(bpage);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001380 }
Steven Rostedt554f7862009-03-11 22:00:13 -04001381 put_online_cpus();
Vegard Nossum641d2f62008-11-18 19:22:13 +01001382 mutex_unlock(&buffer->mutex);
Steven Rostedt18421012009-12-10 22:54:27 -05001383 atomic_dec(&buffer->record_disabled);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001384 return -ENOMEM;
Steven Rostedt554f7862009-03-11 22:00:13 -04001385
1386 /*
1387 * Something went totally wrong, and we are too paranoid
1388 * to even clean up the mess.
1389 */
1390 out_fail:
1391 put_online_cpus();
1392 mutex_unlock(&buffer->mutex);
Steven Rostedt18421012009-12-10 22:54:27 -05001393 atomic_dec(&buffer->record_disabled);
Steven Rostedt554f7862009-03-11 22:00:13 -04001394 return -1;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001395}
Robert Richterc4f50182008-12-11 16:49:22 +01001396EXPORT_SYMBOL_GPL(ring_buffer_resize);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001397
Steven Rostedt8789a9e2008-12-02 15:34:07 -05001398static inline void *
Steven Rostedt044fa782008-12-02 23:50:03 -05001399__rb_data_page_index(struct buffer_data_page *bpage, unsigned index)
Steven Rostedt8789a9e2008-12-02 15:34:07 -05001400{
Steven Rostedt044fa782008-12-02 23:50:03 -05001401 return bpage->data + index;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05001402}
1403
Steven Rostedt044fa782008-12-02 23:50:03 -05001404static inline void *__rb_page_index(struct buffer_page *bpage, unsigned index)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001405{
Steven Rostedt044fa782008-12-02 23:50:03 -05001406 return bpage->page->data + index;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001407}
1408
1409static inline struct ring_buffer_event *
Steven Rostedtd7690412008-10-01 00:29:53 -04001410rb_reader_event(struct ring_buffer_per_cpu *cpu_buffer)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001411{
Steven Rostedt6f807ac2008-10-04 02:00:58 -04001412 return __rb_page_index(cpu_buffer->reader_page,
1413 cpu_buffer->reader_page->read);
1414}
1415
1416static inline struct ring_buffer_event *
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001417rb_iter_head_event(struct ring_buffer_iter *iter)
1418{
Steven Rostedt6f807ac2008-10-04 02:00:58 -04001419 return __rb_page_index(iter->head_page, iter->head);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001420}
1421
Steven Rostedt77ae3652009-03-27 11:00:29 -04001422static inline unsigned long rb_page_write(struct buffer_page *bpage)
Steven Rostedtbf41a152008-10-04 02:00:59 -04001423{
Steven Rostedt77ae3652009-03-27 11:00:29 -04001424 return local_read(&bpage->write) & RB_WRITE_MASK;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001425}
1426
1427static inline unsigned rb_page_commit(struct buffer_page *bpage)
1428{
Steven Rostedtabc9b562008-12-02 15:34:06 -05001429 return local_read(&bpage->page->commit);
Steven Rostedtbf41a152008-10-04 02:00:59 -04001430}
1431
Steven Rostedt77ae3652009-03-27 11:00:29 -04001432static inline unsigned long rb_page_entries(struct buffer_page *bpage)
1433{
1434 return local_read(&bpage->entries) & RB_WRITE_MASK;
1435}
1436
Steven Rostedtbf41a152008-10-04 02:00:59 -04001437/* Size is determined by what has been commited */
1438static inline unsigned rb_page_size(struct buffer_page *bpage)
1439{
1440 return rb_page_commit(bpage);
1441}
1442
1443static inline unsigned
1444rb_commit_index(struct ring_buffer_per_cpu *cpu_buffer)
1445{
1446 return rb_page_commit(cpu_buffer->commit_page);
1447}
1448
Steven Rostedtbf41a152008-10-04 02:00:59 -04001449static inline unsigned
1450rb_event_index(struct ring_buffer_event *event)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001451{
Steven Rostedtbf41a152008-10-04 02:00:59 -04001452 unsigned long addr = (unsigned long)event;
1453
Steven Rostedt22f470f2009-06-11 09:29:58 -04001454 return (addr & ~PAGE_MASK) - BUF_PAGE_HDR_SIZE;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001455}
1456
Steven Rostedt0f0c85f2009-05-11 16:08:00 -04001457static inline int
Steven Rostedtfa743952009-06-16 12:37:57 -04001458rb_event_is_commit(struct ring_buffer_per_cpu *cpu_buffer,
1459 struct ring_buffer_event *event)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001460{
Steven Rostedtbf41a152008-10-04 02:00:59 -04001461 unsigned long addr = (unsigned long)event;
1462 unsigned long index;
1463
1464 index = rb_event_index(event);
1465 addr &= PAGE_MASK;
1466
1467 return cpu_buffer->commit_page->page == (void *)addr &&
1468 rb_commit_index(cpu_buffer) == index;
1469}
1470
Andrew Morton34a148b2009-01-09 12:27:09 -08001471static void
Steven Rostedtbf41a152008-10-04 02:00:59 -04001472rb_set_commit_to_write(struct ring_buffer_per_cpu *cpu_buffer)
1473{
Steven Rostedt77ae3652009-03-27 11:00:29 -04001474 unsigned long max_count;
1475
Steven Rostedtbf41a152008-10-04 02:00:59 -04001476 /*
1477 * We only race with interrupts and NMIs on this CPU.
1478 * If we own the commit event, then we can commit
1479 * all others that interrupted us, since the interruptions
1480 * are in stack format (they finish before they come
1481 * back to us). This allows us to do a simple loop to
1482 * assign the commit to the tail.
1483 */
Steven Rostedta8ccf1d2008-12-23 11:32:24 -05001484 again:
Steven Rostedt77ae3652009-03-27 11:00:29 -04001485 max_count = cpu_buffer->buffer->pages * 100;
1486
Steven Rostedtbf41a152008-10-04 02:00:59 -04001487 while (cpu_buffer->commit_page != cpu_buffer->tail_page) {
Steven Rostedt77ae3652009-03-27 11:00:29 -04001488 if (RB_WARN_ON(cpu_buffer, !(--max_count)))
1489 return;
1490 if (RB_WARN_ON(cpu_buffer,
1491 rb_is_reader_page(cpu_buffer->tail_page)))
1492 return;
1493 local_set(&cpu_buffer->commit_page->page->commit,
1494 rb_page_write(cpu_buffer->commit_page));
Steven Rostedtbf41a152008-10-04 02:00:59 -04001495 rb_inc_page(cpu_buffer, &cpu_buffer->commit_page);
Steven Rostedtabc9b562008-12-02 15:34:06 -05001496 cpu_buffer->write_stamp =
1497 cpu_buffer->commit_page->page->time_stamp;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001498 /* add barrier to keep gcc from optimizing too much */
1499 barrier();
1500 }
1501 while (rb_commit_index(cpu_buffer) !=
1502 rb_page_write(cpu_buffer->commit_page)) {
Steven Rostedt77ae3652009-03-27 11:00:29 -04001503
1504 local_set(&cpu_buffer->commit_page->page->commit,
1505 rb_page_write(cpu_buffer->commit_page));
1506 RB_WARN_ON(cpu_buffer,
1507 local_read(&cpu_buffer->commit_page->page->commit) &
1508 ~RB_WRITE_MASK);
Steven Rostedtbf41a152008-10-04 02:00:59 -04001509 barrier();
1510 }
Steven Rostedta8ccf1d2008-12-23 11:32:24 -05001511
1512 /* again, keep gcc from optimizing */
1513 barrier();
1514
1515 /*
1516 * If an interrupt came in just after the first while loop
1517 * and pushed the tail page forward, we will be left with
1518 * a dangling commit that will never go forward.
1519 */
1520 if (unlikely(cpu_buffer->commit_page != cpu_buffer->tail_page))
1521 goto again;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001522}
1523
Steven Rostedtd7690412008-10-01 00:29:53 -04001524static void rb_reset_reader_page(struct ring_buffer_per_cpu *cpu_buffer)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001525{
Steven Rostedtabc9b562008-12-02 15:34:06 -05001526 cpu_buffer->read_stamp = cpu_buffer->reader_page->page->time_stamp;
Steven Rostedt6f807ac2008-10-04 02:00:58 -04001527 cpu_buffer->reader_page->read = 0;
Steven Rostedtd7690412008-10-01 00:29:53 -04001528}
1529
Andrew Morton34a148b2009-01-09 12:27:09 -08001530static void rb_inc_iter(struct ring_buffer_iter *iter)
Steven Rostedtd7690412008-10-01 00:29:53 -04001531{
1532 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
1533
1534 /*
1535 * The iterator could be on the reader page (it starts there).
1536 * But the head could have moved, since the reader was
1537 * found. Check for this case and assign the iterator
1538 * to the head page instead of next.
1539 */
1540 if (iter->head_page == cpu_buffer->reader_page)
Steven Rostedt77ae3652009-03-27 11:00:29 -04001541 iter->head_page = rb_set_head_page(cpu_buffer);
Steven Rostedtd7690412008-10-01 00:29:53 -04001542 else
1543 rb_inc_page(cpu_buffer, &iter->head_page);
1544
Steven Rostedtabc9b562008-12-02 15:34:06 -05001545 iter->read_stamp = iter->head_page->page->time_stamp;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001546 iter->head = 0;
1547}
1548
1549/**
1550 * ring_buffer_update_event - update event type and data
1551 * @event: the even to update
1552 * @type: the type of event
1553 * @length: the size of the event field in the ring buffer
1554 *
1555 * Update the type and data fields of the event. The length
1556 * is the actual size that is written to the ring buffer,
1557 * and with this, we can determine what to place into the
1558 * data field.
1559 */
Andrew Morton34a148b2009-01-09 12:27:09 -08001560static void
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001561rb_update_event(struct ring_buffer_event *event,
1562 unsigned type, unsigned length)
1563{
Lai Jiangshan334d4162009-04-24 11:27:05 +08001564 event->type_len = type;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001565
1566 switch (type) {
1567
1568 case RINGBUF_TYPE_PADDING:
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001569 case RINGBUF_TYPE_TIME_EXTEND:
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001570 case RINGBUF_TYPE_TIME_STAMP:
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001571 break;
1572
Lai Jiangshan334d4162009-04-24 11:27:05 +08001573 case 0:
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001574 length -= RB_EVNT_HDR_SIZE;
Steven Rostedt22710482010-03-18 17:54:19 -04001575 if (length > RB_MAX_SMALL_DATA || RB_FORCE_8BYTE_ALIGNMENT)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001576 event->array[0] = length;
Lai Jiangshan334d4162009-04-24 11:27:05 +08001577 else
1578 event->type_len = DIV_ROUND_UP(length, RB_ALIGNMENT);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001579 break;
1580 default:
1581 BUG();
1582 }
1583}
1584
Steven Rostedt77ae3652009-03-27 11:00:29 -04001585/*
1586 * rb_handle_head_page - writer hit the head page
1587 *
1588 * Returns: +1 to retry page
1589 * 0 to continue
1590 * -1 on error
1591 */
1592static int
1593rb_handle_head_page(struct ring_buffer_per_cpu *cpu_buffer,
1594 struct buffer_page *tail_page,
1595 struct buffer_page *next_page)
1596{
1597 struct buffer_page *new_head;
1598 int entries;
1599 int type;
1600 int ret;
1601
1602 entries = rb_page_entries(next_page);
1603
1604 /*
1605 * The hard part is here. We need to move the head
1606 * forward, and protect against both readers on
1607 * other CPUs and writers coming in via interrupts.
1608 */
1609 type = rb_head_page_set_update(cpu_buffer, next_page, tail_page,
1610 RB_PAGE_HEAD);
1611
1612 /*
1613 * type can be one of four:
1614 * NORMAL - an interrupt already moved it for us
1615 * HEAD - we are the first to get here.
1616 * UPDATE - we are the interrupt interrupting
1617 * a current move.
1618 * MOVED - a reader on another CPU moved the next
1619 * pointer to its reader page. Give up
1620 * and try again.
1621 */
1622
1623 switch (type) {
1624 case RB_PAGE_HEAD:
1625 /*
1626 * We changed the head to UPDATE, thus
1627 * it is our responsibility to update
1628 * the counters.
1629 */
1630 local_add(entries, &cpu_buffer->overrun);
1631
1632 /*
1633 * The entries will be zeroed out when we move the
1634 * tail page.
1635 */
1636
1637 /* still more to do */
1638 break;
1639
1640 case RB_PAGE_UPDATE:
1641 /*
1642 * This is an interrupt that interrupt the
1643 * previous update. Still more to do.
1644 */
1645 break;
1646 case RB_PAGE_NORMAL:
1647 /*
1648 * An interrupt came in before the update
1649 * and processed this for us.
1650 * Nothing left to do.
1651 */
1652 return 1;
1653 case RB_PAGE_MOVED:
1654 /*
1655 * The reader is on another CPU and just did
1656 * a swap with our next_page.
1657 * Try again.
1658 */
1659 return 1;
1660 default:
1661 RB_WARN_ON(cpu_buffer, 1); /* WTF??? */
1662 return -1;
1663 }
1664
1665 /*
1666 * Now that we are here, the old head pointer is
1667 * set to UPDATE. This will keep the reader from
1668 * swapping the head page with the reader page.
1669 * The reader (on another CPU) will spin till
1670 * we are finished.
1671 *
1672 * We just need to protect against interrupts
1673 * doing the job. We will set the next pointer
1674 * to HEAD. After that, we set the old pointer
1675 * to NORMAL, but only if it was HEAD before.
1676 * otherwise we are an interrupt, and only
1677 * want the outer most commit to reset it.
1678 */
1679 new_head = next_page;
1680 rb_inc_page(cpu_buffer, &new_head);
1681
1682 ret = rb_head_page_set_head(cpu_buffer, new_head, next_page,
1683 RB_PAGE_NORMAL);
1684
1685 /*
1686 * Valid returns are:
1687 * HEAD - an interrupt came in and already set it.
1688 * NORMAL - One of two things:
1689 * 1) We really set it.
1690 * 2) A bunch of interrupts came in and moved
1691 * the page forward again.
1692 */
1693 switch (ret) {
1694 case RB_PAGE_HEAD:
1695 case RB_PAGE_NORMAL:
1696 /* OK */
1697 break;
1698 default:
1699 RB_WARN_ON(cpu_buffer, 1);
1700 return -1;
1701 }
1702
1703 /*
1704 * It is possible that an interrupt came in,
1705 * set the head up, then more interrupts came in
1706 * and moved it again. When we get back here,
1707 * the page would have been set to NORMAL but we
1708 * just set it back to HEAD.
1709 *
1710 * How do you detect this? Well, if that happened
1711 * the tail page would have moved.
1712 */
1713 if (ret == RB_PAGE_NORMAL) {
1714 /*
1715 * If the tail had moved passed next, then we need
1716 * to reset the pointer.
1717 */
1718 if (cpu_buffer->tail_page != tail_page &&
1719 cpu_buffer->tail_page != next_page)
1720 rb_head_page_set_normal(cpu_buffer, new_head,
1721 next_page,
1722 RB_PAGE_HEAD);
1723 }
1724
1725 /*
1726 * If this was the outer most commit (the one that
1727 * changed the original pointer from HEAD to UPDATE),
1728 * then it is up to us to reset it to NORMAL.
1729 */
1730 if (type == RB_PAGE_HEAD) {
1731 ret = rb_head_page_set_normal(cpu_buffer, next_page,
1732 tail_page,
1733 RB_PAGE_UPDATE);
1734 if (RB_WARN_ON(cpu_buffer,
1735 ret != RB_PAGE_UPDATE))
1736 return -1;
1737 }
1738
1739 return 0;
1740}
1741
Andrew Morton34a148b2009-01-09 12:27:09 -08001742static unsigned rb_calculate_event_length(unsigned length)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001743{
1744 struct ring_buffer_event event; /* Used only for sizeof array */
1745
1746 /* zero length can cause confusions */
1747 if (!length)
1748 length = 1;
1749
Steven Rostedt22710482010-03-18 17:54:19 -04001750 if (length > RB_MAX_SMALL_DATA || RB_FORCE_8BYTE_ALIGNMENT)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001751 length += sizeof(event.array[0]);
1752
1753 length += RB_EVNT_HDR_SIZE;
Steven Rostedt22710482010-03-18 17:54:19 -04001754 length = ALIGN(length, RB_ARCH_ALIGNMENT);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001755
1756 return length;
1757}
1758
Steven Rostedtc7b09302009-06-11 11:12:00 -04001759static inline void
1760rb_reset_tail(struct ring_buffer_per_cpu *cpu_buffer,
1761 struct buffer_page *tail_page,
1762 unsigned long tail, unsigned long length)
1763{
1764 struct ring_buffer_event *event;
1765
1766 /*
1767 * Only the event that crossed the page boundary
1768 * must fill the old tail_page with padding.
1769 */
1770 if (tail >= BUF_PAGE_SIZE) {
1771 local_sub(length, &tail_page->write);
1772 return;
1773 }
1774
1775 event = __rb_page_index(tail_page, tail);
Linus Torvaldsb0b70652009-06-20 10:56:46 -07001776 kmemcheck_annotate_bitfield(event, bitfield);
Steven Rostedtc7b09302009-06-11 11:12:00 -04001777
1778 /*
Steven Rostedtff0ff842010-03-31 22:11:42 -04001779 * Save the original length to the meta data.
1780 * This will be used by the reader to add lost event
1781 * counter.
1782 */
1783 tail_page->real_end = tail;
1784
1785 /*
Steven Rostedtc7b09302009-06-11 11:12:00 -04001786 * If this event is bigger than the minimum size, then
1787 * we need to be careful that we don't subtract the
1788 * write counter enough to allow another writer to slip
1789 * in on this page.
1790 * We put in a discarded commit instead, to make sure
1791 * that this space is not used again.
1792 *
1793 * If we are less than the minimum size, we don't need to
1794 * worry about it.
1795 */
1796 if (tail > (BUF_PAGE_SIZE - RB_EVNT_MIN_SIZE)) {
1797 /* No room for any events */
1798
1799 /* Mark the rest of the page with padding */
1800 rb_event_set_padding(event);
1801
1802 /* Set the write back to the previous setting */
1803 local_sub(length, &tail_page->write);
1804 return;
1805 }
1806
1807 /* Put in a discarded event */
1808 event->array[0] = (BUF_PAGE_SIZE - tail) - RB_EVNT_HDR_SIZE;
1809 event->type_len = RINGBUF_TYPE_PADDING;
1810 /* time delta must be non zero */
1811 event->time_delta = 1;
Steven Rostedtc7b09302009-06-11 11:12:00 -04001812
1813 /* Set write to end of buffer */
1814 length = (tail + length) - BUF_PAGE_SIZE;
1815 local_sub(length, &tail_page->write);
1816}
Steven Rostedt6634ff22009-05-06 15:30:07 -04001817
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001818static struct ring_buffer_event *
Steven Rostedt6634ff22009-05-06 15:30:07 -04001819rb_move_tail(struct ring_buffer_per_cpu *cpu_buffer,
1820 unsigned long length, unsigned long tail,
Steven Rostedt6634ff22009-05-06 15:30:07 -04001821 struct buffer_page *tail_page, u64 *ts)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001822{
Steven Rostedt5a50e332009-11-17 08:43:01 -05001823 struct buffer_page *commit_page = cpu_buffer->commit_page;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001824 struct ring_buffer *buffer = cpu_buffer->buffer;
Steven Rostedt77ae3652009-03-27 11:00:29 -04001825 struct buffer_page *next_page;
1826 int ret;
Steven Rostedtaa20ae82009-05-05 21:16:11 -04001827
1828 next_page = tail_page;
1829
Steven Rostedtaa20ae82009-05-05 21:16:11 -04001830 rb_inc_page(cpu_buffer, &next_page);
1831
Steven Rostedtaa20ae82009-05-05 21:16:11 -04001832 /*
1833 * If for some reason, we had an interrupt storm that made
1834 * it all the way around the buffer, bail, and warn
1835 * about it.
1836 */
1837 if (unlikely(next_page == commit_page)) {
Steven Rostedt77ae3652009-03-27 11:00:29 -04001838 local_inc(&cpu_buffer->commit_overrun);
Steven Rostedtaa20ae82009-05-05 21:16:11 -04001839 goto out_reset;
1840 }
1841
Steven Rostedt77ae3652009-03-27 11:00:29 -04001842 /*
1843 * This is where the fun begins!
1844 *
1845 * We are fighting against races between a reader that
1846 * could be on another CPU trying to swap its reader
1847 * page with the buffer head.
1848 *
1849 * We are also fighting against interrupts coming in and
1850 * moving the head or tail on us as well.
1851 *
1852 * If the next page is the head page then we have filled
1853 * the buffer, unless the commit page is still on the
1854 * reader page.
1855 */
1856 if (rb_is_head_page(cpu_buffer, next_page, &tail_page->list)) {
Steven Rostedtaa20ae82009-05-05 21:16:11 -04001857
Steven Rostedt77ae3652009-03-27 11:00:29 -04001858 /*
1859 * If the commit is not on the reader page, then
1860 * move the header page.
1861 */
1862 if (!rb_is_reader_page(cpu_buffer->commit_page)) {
1863 /*
1864 * If we are not in overwrite mode,
1865 * this is easy, just stop here.
1866 */
1867 if (!(buffer->flags & RB_FL_OVERWRITE))
1868 goto out_reset;
Steven Rostedtaa20ae82009-05-05 21:16:11 -04001869
Steven Rostedt77ae3652009-03-27 11:00:29 -04001870 ret = rb_handle_head_page(cpu_buffer,
1871 tail_page,
1872 next_page);
1873 if (ret < 0)
1874 goto out_reset;
1875 if (ret)
1876 goto out_again;
1877 } else {
1878 /*
1879 * We need to be careful here too. The
1880 * commit page could still be on the reader
1881 * page. We could have a small buffer, and
1882 * have filled up the buffer with events
1883 * from interrupts and such, and wrapped.
1884 *
1885 * Note, if the tail page is also the on the
1886 * reader_page, we let it move out.
1887 */
1888 if (unlikely((cpu_buffer->commit_page !=
1889 cpu_buffer->tail_page) &&
1890 (cpu_buffer->commit_page ==
1891 cpu_buffer->reader_page))) {
1892 local_inc(&cpu_buffer->commit_overrun);
1893 goto out_reset;
1894 }
Steven Rostedtaa20ae82009-05-05 21:16:11 -04001895 }
1896 }
1897
Steven Rostedt77ae3652009-03-27 11:00:29 -04001898 ret = rb_tail_page_update(cpu_buffer, tail_page, next_page);
1899 if (ret) {
1900 /*
1901 * Nested commits always have zero deltas, so
1902 * just reread the time stamp
1903 */
Jiri Olsa6d3f1e12009-10-23 19:36:19 -04001904 *ts = rb_time_stamp(buffer);
Steven Rostedt77ae3652009-03-27 11:00:29 -04001905 next_page->page->time_stamp = *ts;
Steven Rostedtaa20ae82009-05-05 21:16:11 -04001906 }
1907
Steven Rostedt77ae3652009-03-27 11:00:29 -04001908 out_again:
Steven Rostedtaa20ae82009-05-05 21:16:11 -04001909
Steven Rostedt77ae3652009-03-27 11:00:29 -04001910 rb_reset_tail(cpu_buffer, tail_page, tail, length);
Steven Rostedtaa20ae82009-05-05 21:16:11 -04001911
1912 /* fail and let the caller try again */
1913 return ERR_PTR(-EAGAIN);
1914
Steven Rostedt45141d42009-02-12 13:19:48 -05001915 out_reset:
Lai Jiangshan6f3b3442009-01-12 11:06:18 +08001916 /* reset write */
Steven Rostedtc7b09302009-06-11 11:12:00 -04001917 rb_reset_tail(cpu_buffer, tail_page, tail, length);
Lai Jiangshan6f3b3442009-01-12 11:06:18 +08001918
Steven Rostedtbf41a152008-10-04 02:00:59 -04001919 return NULL;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001920}
1921
Steven Rostedt6634ff22009-05-06 15:30:07 -04001922static struct ring_buffer_event *
1923__rb_reserve_next(struct ring_buffer_per_cpu *cpu_buffer,
1924 unsigned type, unsigned long length, u64 *ts)
1925{
Steven Rostedt5a50e332009-11-17 08:43:01 -05001926 struct buffer_page *tail_page;
Steven Rostedt6634ff22009-05-06 15:30:07 -04001927 struct ring_buffer_event *event;
1928 unsigned long tail, write;
1929
Steven Rostedt6634ff22009-05-06 15:30:07 -04001930 tail_page = cpu_buffer->tail_page;
1931 write = local_add_return(length, &tail_page->write);
Steven Rostedt77ae3652009-03-27 11:00:29 -04001932
1933 /* set write to only the index of the write */
1934 write &= RB_WRITE_MASK;
Steven Rostedt6634ff22009-05-06 15:30:07 -04001935 tail = write - length;
1936
1937 /* See if we shot pass the end of this buffer page */
1938 if (write > BUF_PAGE_SIZE)
1939 return rb_move_tail(cpu_buffer, length, tail,
Steven Rostedt5a50e332009-11-17 08:43:01 -05001940 tail_page, ts);
Steven Rostedt6634ff22009-05-06 15:30:07 -04001941
1942 /* We reserved something on the buffer */
1943
Steven Rostedt6634ff22009-05-06 15:30:07 -04001944 event = __rb_page_index(tail_page, tail);
Vegard Nossum1744a212009-02-28 08:29:44 +01001945 kmemcheck_annotate_bitfield(event, bitfield);
Steven Rostedt6634ff22009-05-06 15:30:07 -04001946 rb_update_event(event, type, length);
1947
1948 /* The passed in type is zero for DATA */
1949 if (likely(!type))
1950 local_inc(&tail_page->entries);
1951
1952 /*
Steven Rostedtfa743952009-06-16 12:37:57 -04001953 * If this is the first commit on the page, then update
1954 * its timestamp.
Steven Rostedt6634ff22009-05-06 15:30:07 -04001955 */
Steven Rostedtfa743952009-06-16 12:37:57 -04001956 if (!tail)
1957 tail_page->page->time_stamp = *ts;
Steven Rostedt6634ff22009-05-06 15:30:07 -04001958
1959 return event;
1960}
1961
Steven Rostedtedd813bf2009-06-02 23:00:53 -04001962static inline int
1963rb_try_to_discard(struct ring_buffer_per_cpu *cpu_buffer,
1964 struct ring_buffer_event *event)
1965{
1966 unsigned long new_index, old_index;
1967 struct buffer_page *bpage;
1968 unsigned long index;
1969 unsigned long addr;
1970
1971 new_index = rb_event_index(event);
1972 old_index = new_index + rb_event_length(event);
1973 addr = (unsigned long)event;
1974 addr &= PAGE_MASK;
1975
1976 bpage = cpu_buffer->tail_page;
1977
1978 if (bpage->page == (void *)addr && rb_page_write(bpage) == old_index) {
Steven Rostedt77ae3652009-03-27 11:00:29 -04001979 unsigned long write_mask =
1980 local_read(&bpage->write) & ~RB_WRITE_MASK;
Steven Rostedtedd813bf2009-06-02 23:00:53 -04001981 /*
1982 * This is on the tail page. It is possible that
1983 * a write could come in and move the tail page
1984 * and write to the next page. That is fine
1985 * because we just shorten what is on this page.
1986 */
Steven Rostedt77ae3652009-03-27 11:00:29 -04001987 old_index += write_mask;
1988 new_index += write_mask;
Steven Rostedtedd813bf2009-06-02 23:00:53 -04001989 index = local_cmpxchg(&bpage->write, old_index, new_index);
1990 if (index == old_index)
1991 return 1;
1992 }
1993
1994 /* could not discard */
1995 return 0;
1996}
1997
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001998static int
1999rb_add_time_stamp(struct ring_buffer_per_cpu *cpu_buffer,
2000 u64 *ts, u64 *delta)
2001{
2002 struct ring_buffer_event *event;
Steven Rostedtbf41a152008-10-04 02:00:59 -04002003 int ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002004
Borislav Petkov95609792010-05-02 08:03:54 +02002005 WARN_ONCE(*delta > (1ULL << 59),
2006 KERN_WARNING "Delta way too big! %llu ts=%llu write stamp = %llu\n",
2007 (unsigned long long)*delta,
2008 (unsigned long long)*ts,
2009 (unsigned long long)cpu_buffer->write_stamp);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002010
2011 /*
2012 * The delta is too big, we to add a
2013 * new timestamp.
2014 */
2015 event = __rb_reserve_next(cpu_buffer,
2016 RINGBUF_TYPE_TIME_EXTEND,
2017 RB_LEN_TIME_EXTEND,
2018 ts);
2019 if (!event)
Steven Rostedtbf41a152008-10-04 02:00:59 -04002020 return -EBUSY;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002021
Steven Rostedtbf41a152008-10-04 02:00:59 -04002022 if (PTR_ERR(event) == -EAGAIN)
2023 return -EAGAIN;
2024
2025 /* Only a commited time event can update the write stamp */
Steven Rostedtfa743952009-06-16 12:37:57 -04002026 if (rb_event_is_commit(cpu_buffer, event)) {
Steven Rostedtbf41a152008-10-04 02:00:59 -04002027 /*
Steven Rostedtfa743952009-06-16 12:37:57 -04002028 * If this is the first on the page, then it was
2029 * updated with the page itself. Try to discard it
2030 * and if we can't just make it zero.
Steven Rostedtbf41a152008-10-04 02:00:59 -04002031 */
2032 if (rb_event_index(event)) {
2033 event->time_delta = *delta & TS_MASK;
2034 event->array[0] = *delta >> TS_SHIFT;
2035 } else {
Steven Rostedtea05b572009-06-03 09:30:10 -04002036 /* try to discard, since we do not need this */
2037 if (!rb_try_to_discard(cpu_buffer, event)) {
2038 /* nope, just zero it */
2039 event->time_delta = 0;
2040 event->array[0] = 0;
2041 }
Steven Rostedtbf41a152008-10-04 02:00:59 -04002042 }
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002043 cpu_buffer->write_stamp = *ts;
Steven Rostedtbf41a152008-10-04 02:00:59 -04002044 /* let the caller know this was the commit */
2045 ret = 1;
2046 } else {
Steven Rostedtedd813bf2009-06-02 23:00:53 -04002047 /* Try to discard the event */
2048 if (!rb_try_to_discard(cpu_buffer, event)) {
2049 /* Darn, this is just wasted space */
2050 event->time_delta = 0;
2051 event->array[0] = 0;
Steven Rostedtedd813bf2009-06-02 23:00:53 -04002052 }
Steven Rostedtf57a8a12009-06-05 14:11:30 -04002053 ret = 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002054 }
2055
Steven Rostedtbf41a152008-10-04 02:00:59 -04002056 *delta = 0;
2057
2058 return ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002059}
2060
Steven Rostedtfa743952009-06-16 12:37:57 -04002061static void rb_start_commit(struct ring_buffer_per_cpu *cpu_buffer)
2062{
2063 local_inc(&cpu_buffer->committing);
2064 local_inc(&cpu_buffer->commits);
2065}
2066
2067static void rb_end_commit(struct ring_buffer_per_cpu *cpu_buffer)
2068{
2069 unsigned long commits;
2070
2071 if (RB_WARN_ON(cpu_buffer,
2072 !local_read(&cpu_buffer->committing)))
2073 return;
2074
2075 again:
2076 commits = local_read(&cpu_buffer->commits);
2077 /* synchronize with interrupts */
2078 barrier();
2079 if (local_read(&cpu_buffer->committing) == 1)
2080 rb_set_commit_to_write(cpu_buffer);
2081
2082 local_dec(&cpu_buffer->committing);
2083
2084 /* synchronize with interrupts */
2085 barrier();
2086
2087 /*
2088 * Need to account for interrupts coming in between the
2089 * updating of the commit page and the clearing of the
2090 * committing counter.
2091 */
2092 if (unlikely(local_read(&cpu_buffer->commits) != commits) &&
2093 !local_read(&cpu_buffer->committing)) {
2094 local_inc(&cpu_buffer->committing);
2095 goto again;
2096 }
2097}
2098
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002099static struct ring_buffer_event *
Steven Rostedt62f0b3e2009-09-04 14:11:34 -04002100rb_reserve_next_event(struct ring_buffer *buffer,
2101 struct ring_buffer_per_cpu *cpu_buffer,
Steven Rostedt1cd8d732009-05-11 14:08:09 -04002102 unsigned long length)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002103{
2104 struct ring_buffer_event *event;
Steven Rostedt168b6b12009-05-11 22:11:05 -04002105 u64 ts, delta = 0;
Steven Rostedtbf41a152008-10-04 02:00:59 -04002106 int commit = 0;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002107 int nr_loops = 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002108
Steven Rostedtfa743952009-06-16 12:37:57 -04002109 rb_start_commit(cpu_buffer);
2110
Steven Rostedt85bac322009-09-04 14:24:40 -04002111#ifdef CONFIG_RING_BUFFER_ALLOW_SWAP
Steven Rostedt62f0b3e2009-09-04 14:11:34 -04002112 /*
2113 * Due to the ability to swap a cpu buffer from a buffer
2114 * it is possible it was swapped before we committed.
2115 * (committing stops a swap). We check for it here and
2116 * if it happened, we have to fail the write.
2117 */
2118 barrier();
2119 if (unlikely(ACCESS_ONCE(cpu_buffer->buffer) != buffer)) {
2120 local_dec(&cpu_buffer->committing);
2121 local_dec(&cpu_buffer->commits);
2122 return NULL;
2123 }
Steven Rostedt85bac322009-09-04 14:24:40 -04002124#endif
Steven Rostedt62f0b3e2009-09-04 14:11:34 -04002125
Steven Rostedtbe957c42009-05-11 14:42:53 -04002126 length = rb_calculate_event_length(length);
Steven Rostedtbf41a152008-10-04 02:00:59 -04002127 again:
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002128 /*
2129 * We allow for interrupts to reenter here and do a trace.
2130 * If one does, it will cause this original code to loop
2131 * back here. Even with heavy interrupts happening, this
2132 * should only happen a few times in a row. If this happens
2133 * 1000 times in a row, there must be either an interrupt
2134 * storm or we have something buggy.
2135 * Bail!
2136 */
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05002137 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 1000))
Steven Rostedtfa743952009-06-16 12:37:57 -04002138 goto out_fail;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002139
Jiri Olsa6d3f1e12009-10-23 19:36:19 -04002140 ts = rb_time_stamp(cpu_buffer->buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002141
Steven Rostedtbf41a152008-10-04 02:00:59 -04002142 /*
2143 * Only the first commit can update the timestamp.
2144 * Yes there is a race here. If an interrupt comes in
2145 * just after the conditional and it traces too, then it
2146 * will also check the deltas. More than one timestamp may
2147 * also be made. But only the entry that did the actual
2148 * commit will be something other than zero.
2149 */
Steven Rostedt0f0c85f2009-05-11 16:08:00 -04002150 if (likely(cpu_buffer->tail_page == cpu_buffer->commit_page &&
2151 rb_page_write(cpu_buffer->tail_page) ==
2152 rb_commit_index(cpu_buffer))) {
Steven Rostedt168b6b12009-05-11 22:11:05 -04002153 u64 diff;
Steven Rostedtbf41a152008-10-04 02:00:59 -04002154
Steven Rostedt168b6b12009-05-11 22:11:05 -04002155 diff = ts - cpu_buffer->write_stamp;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002156
Steven Rostedt168b6b12009-05-11 22:11:05 -04002157 /* make sure this diff is calculated here */
Steven Rostedtbf41a152008-10-04 02:00:59 -04002158 barrier();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002159
Steven Rostedtbf41a152008-10-04 02:00:59 -04002160 /* Did the write stamp get updated already? */
2161 if (unlikely(ts < cpu_buffer->write_stamp))
Steven Rostedt168b6b12009-05-11 22:11:05 -04002162 goto get_event;
Steven Rostedtbf41a152008-10-04 02:00:59 -04002163
Steven Rostedt168b6b12009-05-11 22:11:05 -04002164 delta = diff;
2165 if (unlikely(test_time_stamp(delta))) {
Steven Rostedtbf41a152008-10-04 02:00:59 -04002166
2167 commit = rb_add_time_stamp(cpu_buffer, &ts, &delta);
Steven Rostedtbf41a152008-10-04 02:00:59 -04002168 if (commit == -EBUSY)
Steven Rostedtfa743952009-06-16 12:37:57 -04002169 goto out_fail;
Steven Rostedtbf41a152008-10-04 02:00:59 -04002170
2171 if (commit == -EAGAIN)
2172 goto again;
2173
2174 RB_WARN_ON(cpu_buffer, commit < 0);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002175 }
Steven Rostedt168b6b12009-05-11 22:11:05 -04002176 }
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002177
Steven Rostedt168b6b12009-05-11 22:11:05 -04002178 get_event:
Steven Rostedt1cd8d732009-05-11 14:08:09 -04002179 event = __rb_reserve_next(cpu_buffer, 0, length, &ts);
Steven Rostedt168b6b12009-05-11 22:11:05 -04002180 if (unlikely(PTR_ERR(event) == -EAGAIN))
Steven Rostedtbf41a152008-10-04 02:00:59 -04002181 goto again;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002182
Steven Rostedtfa743952009-06-16 12:37:57 -04002183 if (!event)
2184 goto out_fail;
Steven Rostedtbf41a152008-10-04 02:00:59 -04002185
Steven Rostedtfa743952009-06-16 12:37:57 -04002186 if (!rb_event_is_commit(cpu_buffer, event))
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002187 delta = 0;
2188
2189 event->time_delta = delta;
2190
2191 return event;
Steven Rostedtfa743952009-06-16 12:37:57 -04002192
2193 out_fail:
2194 rb_end_commit(cpu_buffer);
2195 return NULL;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002196}
2197
Paul Mundt1155de42009-06-25 14:30:12 +09002198#ifdef CONFIG_TRACING
2199
Steven Rostedtaa18efb2009-04-20 16:16:11 -04002200#define TRACE_RECURSIVE_DEPTH 16
Steven Rostedt261842b2009-04-16 21:41:52 -04002201
2202static int trace_recursive_lock(void)
2203{
Steven Rostedtaa18efb2009-04-20 16:16:11 -04002204 current->trace_recursion++;
Steven Rostedt261842b2009-04-16 21:41:52 -04002205
Steven Rostedtaa18efb2009-04-20 16:16:11 -04002206 if (likely(current->trace_recursion < TRACE_RECURSIVE_DEPTH))
2207 return 0;
Steven Rostedt261842b2009-04-16 21:41:52 -04002208
Steven Rostedtaa18efb2009-04-20 16:16:11 -04002209 /* Disable all tracing before we do anything else */
2210 tracing_off_permanent();
Frederic Weisbeckere057a5e2009-04-19 23:38:12 +02002211
Steven Rostedt7d7d2b82009-04-27 12:37:49 -04002212 printk_once(KERN_WARNING "Tracing recursion: depth[%ld]:"
Steven Rostedtaa18efb2009-04-20 16:16:11 -04002213 "HC[%lu]:SC[%lu]:NMI[%lu]\n",
2214 current->trace_recursion,
2215 hardirq_count() >> HARDIRQ_SHIFT,
2216 softirq_count() >> SOFTIRQ_SHIFT,
2217 in_nmi());
Frederic Weisbeckere057a5e2009-04-19 23:38:12 +02002218
Steven Rostedtaa18efb2009-04-20 16:16:11 -04002219 WARN_ON_ONCE(1);
2220 return -1;
Steven Rostedt261842b2009-04-16 21:41:52 -04002221}
2222
2223static void trace_recursive_unlock(void)
2224{
Steven Rostedtaa18efb2009-04-20 16:16:11 -04002225 WARN_ON_ONCE(!current->trace_recursion);
Steven Rostedt261842b2009-04-16 21:41:52 -04002226
Steven Rostedtaa18efb2009-04-20 16:16:11 -04002227 current->trace_recursion--;
Steven Rostedt261842b2009-04-16 21:41:52 -04002228}
2229
Paul Mundt1155de42009-06-25 14:30:12 +09002230#else
2231
2232#define trace_recursive_lock() (0)
2233#define trace_recursive_unlock() do { } while (0)
2234
2235#endif
2236
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002237/**
2238 * ring_buffer_lock_reserve - reserve a part of the buffer
2239 * @buffer: the ring buffer to reserve from
2240 * @length: the length of the data to reserve (excluding event header)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002241 *
2242 * Returns a reseverd event on the ring buffer to copy directly to.
2243 * The user of this interface will need to get the body to write into
2244 * and can use the ring_buffer_event_data() interface.
2245 *
2246 * The length is the length of the data needed, not the event length
2247 * which also includes the event header.
2248 *
2249 * Must be paired with ring_buffer_unlock_commit, unless NULL is returned.
2250 * If NULL is returned, then nothing has been allocated or locked.
2251 */
2252struct ring_buffer_event *
Arnaldo Carvalho de Melo0a987752009-02-05 16:12:56 -02002253ring_buffer_lock_reserve(struct ring_buffer *buffer, unsigned long length)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002254{
2255 struct ring_buffer_per_cpu *cpu_buffer;
2256 struct ring_buffer_event *event;
Steven Rostedt5168ae52010-06-03 09:36:50 -04002257 int cpu;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002258
Steven Rostedt033601a2008-11-21 12:41:55 -05002259 if (ring_buffer_flags != RB_BUFFERS_ON)
Steven Rostedta3583242008-11-11 15:01:42 -05002260 return NULL;
2261
Steven Rostedtbf41a152008-10-04 02:00:59 -04002262 /* If we are tracing schedule, we don't want to recurse */
Steven Rostedt5168ae52010-06-03 09:36:50 -04002263 preempt_disable_notrace();
Steven Rostedtbf41a152008-10-04 02:00:59 -04002264
Lai Jiangshan52fbe9c2010-03-08 14:50:43 +08002265 if (atomic_read(&buffer->record_disabled))
2266 goto out_nocheck;
2267
Steven Rostedt261842b2009-04-16 21:41:52 -04002268 if (trace_recursive_lock())
2269 goto out_nocheck;
2270
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002271 cpu = raw_smp_processor_id();
2272
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302273 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedtd7690412008-10-01 00:29:53 -04002274 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002275
2276 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002277
2278 if (atomic_read(&cpu_buffer->record_disabled))
Steven Rostedtd7690412008-10-01 00:29:53 -04002279 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002280
Steven Rostedtbe957c42009-05-11 14:42:53 -04002281 if (length > BUF_MAX_DATA_SIZE)
Steven Rostedtbf41a152008-10-04 02:00:59 -04002282 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002283
Steven Rostedt62f0b3e2009-09-04 14:11:34 -04002284 event = rb_reserve_next_event(buffer, cpu_buffer, length);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002285 if (!event)
Steven Rostedtd7690412008-10-01 00:29:53 -04002286 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002287
2288 return event;
2289
Steven Rostedtd7690412008-10-01 00:29:53 -04002290 out:
Steven Rostedt261842b2009-04-16 21:41:52 -04002291 trace_recursive_unlock();
2292
2293 out_nocheck:
Steven Rostedt5168ae52010-06-03 09:36:50 -04002294 preempt_enable_notrace();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002295 return NULL;
2296}
Robert Richterc4f50182008-12-11 16:49:22 +01002297EXPORT_SYMBOL_GPL(ring_buffer_lock_reserve);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002298
Steven Rostedta1863c22009-09-03 10:23:58 -04002299static void
2300rb_update_write_stamp(struct ring_buffer_per_cpu *cpu_buffer,
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002301 struct ring_buffer_event *event)
2302{
Steven Rostedtfa743952009-06-16 12:37:57 -04002303 /*
2304 * The event first in the commit queue updates the
2305 * time stamp.
2306 */
2307 if (rb_event_is_commit(cpu_buffer, event))
2308 cpu_buffer->write_stamp += event->time_delta;
Steven Rostedta1863c22009-09-03 10:23:58 -04002309}
Steven Rostedtbf41a152008-10-04 02:00:59 -04002310
Steven Rostedta1863c22009-09-03 10:23:58 -04002311static void rb_commit(struct ring_buffer_per_cpu *cpu_buffer,
2312 struct ring_buffer_event *event)
2313{
2314 local_inc(&cpu_buffer->entries);
2315 rb_update_write_stamp(cpu_buffer, event);
Steven Rostedtfa743952009-06-16 12:37:57 -04002316 rb_end_commit(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002317}
2318
2319/**
2320 * ring_buffer_unlock_commit - commit a reserved
2321 * @buffer: The buffer to commit to
2322 * @event: The event pointer to commit.
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002323 *
2324 * This commits the data to the ring buffer, and releases any locks held.
2325 *
2326 * Must be paired with ring_buffer_lock_reserve.
2327 */
2328int ring_buffer_unlock_commit(struct ring_buffer *buffer,
Arnaldo Carvalho de Melo0a987752009-02-05 16:12:56 -02002329 struct ring_buffer_event *event)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002330{
2331 struct ring_buffer_per_cpu *cpu_buffer;
2332 int cpu = raw_smp_processor_id();
2333
2334 cpu_buffer = buffer->buffers[cpu];
2335
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002336 rb_commit(cpu_buffer, event);
2337
Steven Rostedt261842b2009-04-16 21:41:52 -04002338 trace_recursive_unlock();
2339
Steven Rostedt5168ae52010-06-03 09:36:50 -04002340 preempt_enable_notrace();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002341
2342 return 0;
2343}
Robert Richterc4f50182008-12-11 16:49:22 +01002344EXPORT_SYMBOL_GPL(ring_buffer_unlock_commit);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002345
Frederic Weisbeckerf3b9aae2009-04-19 23:39:33 +02002346static inline void rb_event_discard(struct ring_buffer_event *event)
2347{
Lai Jiangshan334d4162009-04-24 11:27:05 +08002348 /* array[0] holds the actual length for the discarded event */
2349 event->array[0] = rb_event_data_length(event) - RB_EVNT_HDR_SIZE;
2350 event->type_len = RINGBUF_TYPE_PADDING;
Frederic Weisbeckerf3b9aae2009-04-19 23:39:33 +02002351 /* time delta must be non zero */
2352 if (!event->time_delta)
2353 event->time_delta = 1;
2354}
2355
Steven Rostedta1863c22009-09-03 10:23:58 -04002356/*
2357 * Decrement the entries to the page that an event is on.
2358 * The event does not even need to exist, only the pointer
2359 * to the page it is on. This may only be called before the commit
2360 * takes place.
2361 */
2362static inline void
2363rb_decrement_entry(struct ring_buffer_per_cpu *cpu_buffer,
2364 struct ring_buffer_event *event)
2365{
2366 unsigned long addr = (unsigned long)event;
2367 struct buffer_page *bpage = cpu_buffer->commit_page;
2368 struct buffer_page *start;
2369
2370 addr &= PAGE_MASK;
2371
2372 /* Do the likely case first */
2373 if (likely(bpage->page == (void *)addr)) {
2374 local_dec(&bpage->entries);
2375 return;
2376 }
2377
2378 /*
2379 * Because the commit page may be on the reader page we
2380 * start with the next page and check the end loop there.
2381 */
2382 rb_inc_page(cpu_buffer, &bpage);
2383 start = bpage;
2384 do {
2385 if (bpage->page == (void *)addr) {
2386 local_dec(&bpage->entries);
2387 return;
2388 }
2389 rb_inc_page(cpu_buffer, &bpage);
2390 } while (bpage != start);
2391
2392 /* commit not part of this buffer?? */
2393 RB_WARN_ON(cpu_buffer, 1);
2394}
2395
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002396/**
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002397 * ring_buffer_commit_discard - discard an event that has not been committed
2398 * @buffer: the ring buffer
2399 * @event: non committed event to discard
2400 *
Steven Rostedtdc892f72009-09-03 15:33:41 -04002401 * Sometimes an event that is in the ring buffer needs to be ignored.
2402 * This function lets the user discard an event in the ring buffer
2403 * and then that event will not be read later.
2404 *
2405 * This function only works if it is called before the the item has been
2406 * committed. It will try to free the event from the ring buffer
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002407 * if another event has not been added behind it.
2408 *
2409 * If another event has been added behind it, it will set the event
2410 * up as discarded, and perform the commit.
2411 *
2412 * If this function is called, do not call ring_buffer_unlock_commit on
2413 * the event.
2414 */
2415void ring_buffer_discard_commit(struct ring_buffer *buffer,
2416 struct ring_buffer_event *event)
2417{
2418 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002419 int cpu;
2420
2421 /* The event is discarded regardless */
Frederic Weisbeckerf3b9aae2009-04-19 23:39:33 +02002422 rb_event_discard(event);
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002423
Steven Rostedtfa743952009-06-16 12:37:57 -04002424 cpu = smp_processor_id();
2425 cpu_buffer = buffer->buffers[cpu];
2426
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002427 /*
2428 * This must only be called if the event has not been
2429 * committed yet. Thus we can assume that preemption
2430 * is still disabled.
2431 */
Steven Rostedtfa743952009-06-16 12:37:57 -04002432 RB_WARN_ON(buffer, !local_read(&cpu_buffer->committing));
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002433
Steven Rostedta1863c22009-09-03 10:23:58 -04002434 rb_decrement_entry(cpu_buffer, event);
Steven Rostedt0f2541d2009-08-05 12:02:48 -04002435 if (rb_try_to_discard(cpu_buffer, event))
Steven Rostedtedd813bf2009-06-02 23:00:53 -04002436 goto out;
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002437
2438 /*
2439 * The commit is still visible by the reader, so we
Steven Rostedta1863c22009-09-03 10:23:58 -04002440 * must still update the timestamp.
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002441 */
Steven Rostedta1863c22009-09-03 10:23:58 -04002442 rb_update_write_stamp(cpu_buffer, event);
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002443 out:
Steven Rostedtfa743952009-06-16 12:37:57 -04002444 rb_end_commit(cpu_buffer);
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002445
Frederic Weisbeckerf3b9aae2009-04-19 23:39:33 +02002446 trace_recursive_unlock();
2447
Steven Rostedt5168ae52010-06-03 09:36:50 -04002448 preempt_enable_notrace();
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002449
2450}
2451EXPORT_SYMBOL_GPL(ring_buffer_discard_commit);
2452
2453/**
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002454 * ring_buffer_write - write data to the buffer without reserving
2455 * @buffer: The ring buffer to write to.
2456 * @length: The length of the data being written (excluding the event header)
2457 * @data: The data to write to the buffer.
2458 *
2459 * This is like ring_buffer_lock_reserve and ring_buffer_unlock_commit as
2460 * one function. If you already have the data to write to the buffer, it
2461 * may be easier to simply call this function.
2462 *
2463 * Note, like ring_buffer_lock_reserve, the length is the length of the data
2464 * and not the length of the event which would hold the header.
2465 */
2466int ring_buffer_write(struct ring_buffer *buffer,
2467 unsigned long length,
2468 void *data)
2469{
2470 struct ring_buffer_per_cpu *cpu_buffer;
2471 struct ring_buffer_event *event;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002472 void *body;
2473 int ret = -EBUSY;
Steven Rostedt5168ae52010-06-03 09:36:50 -04002474 int cpu;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002475
Steven Rostedt033601a2008-11-21 12:41:55 -05002476 if (ring_buffer_flags != RB_BUFFERS_ON)
Steven Rostedta3583242008-11-11 15:01:42 -05002477 return -EBUSY;
2478
Steven Rostedt5168ae52010-06-03 09:36:50 -04002479 preempt_disable_notrace();
Steven Rostedtbf41a152008-10-04 02:00:59 -04002480
Lai Jiangshan52fbe9c2010-03-08 14:50:43 +08002481 if (atomic_read(&buffer->record_disabled))
2482 goto out;
2483
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002484 cpu = raw_smp_processor_id();
2485
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302486 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedtd7690412008-10-01 00:29:53 -04002487 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002488
2489 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002490
2491 if (atomic_read(&cpu_buffer->record_disabled))
2492 goto out;
2493
Steven Rostedtbe957c42009-05-11 14:42:53 -04002494 if (length > BUF_MAX_DATA_SIZE)
2495 goto out;
2496
Steven Rostedt62f0b3e2009-09-04 14:11:34 -04002497 event = rb_reserve_next_event(buffer, cpu_buffer, length);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002498 if (!event)
2499 goto out;
2500
2501 body = rb_event_data(event);
2502
2503 memcpy(body, data, length);
2504
2505 rb_commit(cpu_buffer, event);
2506
2507 ret = 0;
2508 out:
Steven Rostedt5168ae52010-06-03 09:36:50 -04002509 preempt_enable_notrace();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002510
2511 return ret;
2512}
Robert Richterc4f50182008-12-11 16:49:22 +01002513EXPORT_SYMBOL_GPL(ring_buffer_write);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002514
Andrew Morton34a148b2009-01-09 12:27:09 -08002515static int rb_per_cpu_empty(struct ring_buffer_per_cpu *cpu_buffer)
Steven Rostedtbf41a152008-10-04 02:00:59 -04002516{
2517 struct buffer_page *reader = cpu_buffer->reader_page;
Steven Rostedt77ae3652009-03-27 11:00:29 -04002518 struct buffer_page *head = rb_set_head_page(cpu_buffer);
Steven Rostedtbf41a152008-10-04 02:00:59 -04002519 struct buffer_page *commit = cpu_buffer->commit_page;
2520
Steven Rostedt77ae3652009-03-27 11:00:29 -04002521 /* In case of error, head will be NULL */
2522 if (unlikely(!head))
2523 return 1;
2524
Steven Rostedtbf41a152008-10-04 02:00:59 -04002525 return reader->read == rb_page_commit(reader) &&
2526 (commit == reader ||
2527 (commit == head &&
2528 head->read == rb_page_commit(commit)));
2529}
2530
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002531/**
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002532 * ring_buffer_record_disable - stop all writes into the buffer
2533 * @buffer: The ring buffer to stop writes to.
2534 *
2535 * This prevents all writes to the buffer. Any attempt to write
2536 * to the buffer after this will fail and return NULL.
2537 *
2538 * The caller should call synchronize_sched() after this.
2539 */
2540void ring_buffer_record_disable(struct ring_buffer *buffer)
2541{
2542 atomic_inc(&buffer->record_disabled);
2543}
Robert Richterc4f50182008-12-11 16:49:22 +01002544EXPORT_SYMBOL_GPL(ring_buffer_record_disable);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002545
2546/**
2547 * ring_buffer_record_enable - enable writes to the buffer
2548 * @buffer: The ring buffer to enable writes
2549 *
2550 * Note, multiple disables will need the same number of enables
Adam Buchbinderc41b20e2009-12-11 16:35:39 -05002551 * to truly enable the writing (much like preempt_disable).
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002552 */
2553void ring_buffer_record_enable(struct ring_buffer *buffer)
2554{
2555 atomic_dec(&buffer->record_disabled);
2556}
Robert Richterc4f50182008-12-11 16:49:22 +01002557EXPORT_SYMBOL_GPL(ring_buffer_record_enable);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002558
2559/**
2560 * ring_buffer_record_disable_cpu - stop all writes into the cpu_buffer
2561 * @buffer: The ring buffer to stop writes to.
2562 * @cpu: The CPU buffer to stop
2563 *
2564 * This prevents all writes to the buffer. Any attempt to write
2565 * to the buffer after this will fail and return NULL.
2566 *
2567 * The caller should call synchronize_sched() after this.
2568 */
2569void ring_buffer_record_disable_cpu(struct ring_buffer *buffer, int cpu)
2570{
2571 struct ring_buffer_per_cpu *cpu_buffer;
2572
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302573 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04002574 return;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002575
2576 cpu_buffer = buffer->buffers[cpu];
2577 atomic_inc(&cpu_buffer->record_disabled);
2578}
Robert Richterc4f50182008-12-11 16:49:22 +01002579EXPORT_SYMBOL_GPL(ring_buffer_record_disable_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002580
2581/**
2582 * ring_buffer_record_enable_cpu - enable writes to the buffer
2583 * @buffer: The ring buffer to enable writes
2584 * @cpu: The CPU to enable.
2585 *
2586 * Note, multiple disables will need the same number of enables
Adam Buchbinderc41b20e2009-12-11 16:35:39 -05002587 * to truly enable the writing (much like preempt_disable).
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002588 */
2589void ring_buffer_record_enable_cpu(struct ring_buffer *buffer, int cpu)
2590{
2591 struct ring_buffer_per_cpu *cpu_buffer;
2592
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302593 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04002594 return;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002595
2596 cpu_buffer = buffer->buffers[cpu];
2597 atomic_dec(&cpu_buffer->record_disabled);
2598}
Robert Richterc4f50182008-12-11 16:49:22 +01002599EXPORT_SYMBOL_GPL(ring_buffer_record_enable_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002600
2601/**
2602 * ring_buffer_entries_cpu - get the number of entries in a cpu buffer
2603 * @buffer: The ring buffer
2604 * @cpu: The per CPU buffer to get the entries from.
2605 */
2606unsigned long ring_buffer_entries_cpu(struct ring_buffer *buffer, int cpu)
2607{
2608 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedt8aabee52009-03-12 13:13:49 -04002609 unsigned long ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002610
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302611 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04002612 return 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002613
2614 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt77ae3652009-03-27 11:00:29 -04002615 ret = (local_read(&cpu_buffer->entries) - local_read(&cpu_buffer->overrun))
Steven Rostedte4906ef2009-04-30 20:49:44 -04002616 - cpu_buffer->read;
Steven Rostedt554f7862009-03-11 22:00:13 -04002617
2618 return ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002619}
Robert Richterc4f50182008-12-11 16:49:22 +01002620EXPORT_SYMBOL_GPL(ring_buffer_entries_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002621
2622/**
2623 * ring_buffer_overrun_cpu - get the number of overruns in a cpu_buffer
2624 * @buffer: The ring buffer
2625 * @cpu: The per CPU buffer to get the number of overruns from
2626 */
2627unsigned long ring_buffer_overrun_cpu(struct ring_buffer *buffer, int cpu)
2628{
2629 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedt8aabee52009-03-12 13:13:49 -04002630 unsigned long ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002631
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302632 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04002633 return 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002634
2635 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt77ae3652009-03-27 11:00:29 -04002636 ret = local_read(&cpu_buffer->overrun);
Steven Rostedt554f7862009-03-11 22:00:13 -04002637
2638 return ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002639}
Robert Richterc4f50182008-12-11 16:49:22 +01002640EXPORT_SYMBOL_GPL(ring_buffer_overrun_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002641
2642/**
Steven Rostedtf0d2c682009-04-29 13:43:37 -04002643 * ring_buffer_commit_overrun_cpu - get the number of overruns caused by commits
2644 * @buffer: The ring buffer
2645 * @cpu: The per CPU buffer to get the number of overruns from
2646 */
2647unsigned long
2648ring_buffer_commit_overrun_cpu(struct ring_buffer *buffer, int cpu)
2649{
2650 struct ring_buffer_per_cpu *cpu_buffer;
2651 unsigned long ret;
2652
2653 if (!cpumask_test_cpu(cpu, buffer->cpumask))
2654 return 0;
2655
2656 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt77ae3652009-03-27 11:00:29 -04002657 ret = local_read(&cpu_buffer->commit_overrun);
Steven Rostedtf0d2c682009-04-29 13:43:37 -04002658
2659 return ret;
2660}
2661EXPORT_SYMBOL_GPL(ring_buffer_commit_overrun_cpu);
2662
2663/**
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002664 * ring_buffer_entries - get the number of entries in a buffer
2665 * @buffer: The ring buffer
2666 *
2667 * Returns the total number of entries in the ring buffer
2668 * (all CPU entries)
2669 */
2670unsigned long ring_buffer_entries(struct ring_buffer *buffer)
2671{
2672 struct ring_buffer_per_cpu *cpu_buffer;
2673 unsigned long entries = 0;
2674 int cpu;
2675
2676 /* if you care about this being correct, lock the buffer */
2677 for_each_buffer_cpu(buffer, cpu) {
2678 cpu_buffer = buffer->buffers[cpu];
Steven Rostedte4906ef2009-04-30 20:49:44 -04002679 entries += (local_read(&cpu_buffer->entries) -
Steven Rostedt77ae3652009-03-27 11:00:29 -04002680 local_read(&cpu_buffer->overrun)) - cpu_buffer->read;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002681 }
2682
2683 return entries;
2684}
Robert Richterc4f50182008-12-11 16:49:22 +01002685EXPORT_SYMBOL_GPL(ring_buffer_entries);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002686
2687/**
Jiri Olsa67b394f2009-10-23 19:36:18 -04002688 * ring_buffer_overruns - get the number of overruns in buffer
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002689 * @buffer: The ring buffer
2690 *
2691 * Returns the total number of overruns in the ring buffer
2692 * (all CPU entries)
2693 */
2694unsigned long ring_buffer_overruns(struct ring_buffer *buffer)
2695{
2696 struct ring_buffer_per_cpu *cpu_buffer;
2697 unsigned long overruns = 0;
2698 int cpu;
2699
2700 /* if you care about this being correct, lock the buffer */
2701 for_each_buffer_cpu(buffer, cpu) {
2702 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt77ae3652009-03-27 11:00:29 -04002703 overruns += local_read(&cpu_buffer->overrun);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002704 }
2705
2706 return overruns;
2707}
Robert Richterc4f50182008-12-11 16:49:22 +01002708EXPORT_SYMBOL_GPL(ring_buffer_overruns);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002709
Steven Rostedt642edba2008-11-12 00:01:26 -05002710static void rb_iter_reset(struct ring_buffer_iter *iter)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002711{
2712 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
2713
Steven Rostedtd7690412008-10-01 00:29:53 -04002714 /* Iterator usage is expected to have record disabled */
2715 if (list_empty(&cpu_buffer->reader_page->list)) {
Steven Rostedt77ae3652009-03-27 11:00:29 -04002716 iter->head_page = rb_set_head_page(cpu_buffer);
2717 if (unlikely(!iter->head_page))
2718 return;
2719 iter->head = iter->head_page->read;
Steven Rostedtd7690412008-10-01 00:29:53 -04002720 } else {
2721 iter->head_page = cpu_buffer->reader_page;
Steven Rostedt6f807ac2008-10-04 02:00:58 -04002722 iter->head = cpu_buffer->reader_page->read;
Steven Rostedtd7690412008-10-01 00:29:53 -04002723 }
2724 if (iter->head)
2725 iter->read_stamp = cpu_buffer->read_stamp;
2726 else
Steven Rostedtabc9b562008-12-02 15:34:06 -05002727 iter->read_stamp = iter->head_page->page->time_stamp;
Steven Rostedt492a74f2010-01-25 15:17:47 -05002728 iter->cache_reader_page = cpu_buffer->reader_page;
2729 iter->cache_read = cpu_buffer->read;
Steven Rostedt642edba2008-11-12 00:01:26 -05002730}
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002731
Steven Rostedt642edba2008-11-12 00:01:26 -05002732/**
2733 * ring_buffer_iter_reset - reset an iterator
2734 * @iter: The iterator to reset
2735 *
2736 * Resets the iterator, so that it will start from the beginning
2737 * again.
2738 */
2739void ring_buffer_iter_reset(struct ring_buffer_iter *iter)
2740{
Steven Rostedt554f7862009-03-11 22:00:13 -04002741 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedt642edba2008-11-12 00:01:26 -05002742 unsigned long flags;
2743
Steven Rostedt554f7862009-03-11 22:00:13 -04002744 if (!iter)
2745 return;
2746
2747 cpu_buffer = iter->cpu_buffer;
2748
Steven Rostedt642edba2008-11-12 00:01:26 -05002749 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
2750 rb_iter_reset(iter);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002751 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002752}
Robert Richterc4f50182008-12-11 16:49:22 +01002753EXPORT_SYMBOL_GPL(ring_buffer_iter_reset);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002754
2755/**
2756 * ring_buffer_iter_empty - check if an iterator has no more to read
2757 * @iter: The iterator to check
2758 */
2759int ring_buffer_iter_empty(struct ring_buffer_iter *iter)
2760{
2761 struct ring_buffer_per_cpu *cpu_buffer;
2762
2763 cpu_buffer = iter->cpu_buffer;
2764
Steven Rostedtbf41a152008-10-04 02:00:59 -04002765 return iter->head_page == cpu_buffer->commit_page &&
2766 iter->head == rb_commit_index(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002767}
Robert Richterc4f50182008-12-11 16:49:22 +01002768EXPORT_SYMBOL_GPL(ring_buffer_iter_empty);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002769
2770static void
2771rb_update_read_stamp(struct ring_buffer_per_cpu *cpu_buffer,
2772 struct ring_buffer_event *event)
2773{
2774 u64 delta;
2775
Lai Jiangshan334d4162009-04-24 11:27:05 +08002776 switch (event->type_len) {
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002777 case RINGBUF_TYPE_PADDING:
2778 return;
2779
2780 case RINGBUF_TYPE_TIME_EXTEND:
2781 delta = event->array[0];
2782 delta <<= TS_SHIFT;
2783 delta += event->time_delta;
2784 cpu_buffer->read_stamp += delta;
2785 return;
2786
2787 case RINGBUF_TYPE_TIME_STAMP:
2788 /* FIXME: not implemented */
2789 return;
2790
2791 case RINGBUF_TYPE_DATA:
2792 cpu_buffer->read_stamp += event->time_delta;
2793 return;
2794
2795 default:
2796 BUG();
2797 }
2798 return;
2799}
2800
2801static void
2802rb_update_iter_read_stamp(struct ring_buffer_iter *iter,
2803 struct ring_buffer_event *event)
2804{
2805 u64 delta;
2806
Lai Jiangshan334d4162009-04-24 11:27:05 +08002807 switch (event->type_len) {
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002808 case RINGBUF_TYPE_PADDING:
2809 return;
2810
2811 case RINGBUF_TYPE_TIME_EXTEND:
2812 delta = event->array[0];
2813 delta <<= TS_SHIFT;
2814 delta += event->time_delta;
2815 iter->read_stamp += delta;
2816 return;
2817
2818 case RINGBUF_TYPE_TIME_STAMP:
2819 /* FIXME: not implemented */
2820 return;
2821
2822 case RINGBUF_TYPE_DATA:
2823 iter->read_stamp += event->time_delta;
2824 return;
2825
2826 default:
2827 BUG();
2828 }
2829 return;
2830}
2831
Steven Rostedtd7690412008-10-01 00:29:53 -04002832static struct buffer_page *
2833rb_get_reader_page(struct ring_buffer_per_cpu *cpu_buffer)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002834{
Steven Rostedtd7690412008-10-01 00:29:53 -04002835 struct buffer_page *reader = NULL;
Steven Rostedt66a8cb92010-03-31 13:21:56 -04002836 unsigned long overwrite;
Steven Rostedtd7690412008-10-01 00:29:53 -04002837 unsigned long flags;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002838 int nr_loops = 0;
Steven Rostedt77ae3652009-03-27 11:00:29 -04002839 int ret;
Steven Rostedtd7690412008-10-01 00:29:53 -04002840
Steven Rostedt3e03fb72008-11-06 00:09:43 -05002841 local_irq_save(flags);
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01002842 arch_spin_lock(&cpu_buffer->lock);
Steven Rostedtd7690412008-10-01 00:29:53 -04002843
2844 again:
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002845 /*
2846 * This should normally only loop twice. But because the
2847 * start of the reader inserts an empty page, it causes
2848 * a case where we will loop three times. There should be no
2849 * reason to loop four times (that I know of).
2850 */
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05002851 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 3)) {
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002852 reader = NULL;
2853 goto out;
2854 }
2855
Steven Rostedtd7690412008-10-01 00:29:53 -04002856 reader = cpu_buffer->reader_page;
2857
2858 /* If there's more to read, return this page */
Steven Rostedtbf41a152008-10-04 02:00:59 -04002859 if (cpu_buffer->reader_page->read < rb_page_size(reader))
Steven Rostedtd7690412008-10-01 00:29:53 -04002860 goto out;
2861
2862 /* Never should we have an index greater than the size */
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05002863 if (RB_WARN_ON(cpu_buffer,
2864 cpu_buffer->reader_page->read > rb_page_size(reader)))
2865 goto out;
Steven Rostedtd7690412008-10-01 00:29:53 -04002866
2867 /* check if we caught up to the tail */
2868 reader = NULL;
Steven Rostedtbf41a152008-10-04 02:00:59 -04002869 if (cpu_buffer->commit_page == cpu_buffer->reader_page)
Steven Rostedtd7690412008-10-01 00:29:53 -04002870 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002871
2872 /*
Steven Rostedtd7690412008-10-01 00:29:53 -04002873 * Reset the reader page to size zero.
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002874 */
Steven Rostedt77ae3652009-03-27 11:00:29 -04002875 local_set(&cpu_buffer->reader_page->write, 0);
2876 local_set(&cpu_buffer->reader_page->entries, 0);
2877 local_set(&cpu_buffer->reader_page->page->commit, 0);
Steven Rostedtff0ff842010-03-31 22:11:42 -04002878 cpu_buffer->reader_page->real_end = 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002879
Steven Rostedt77ae3652009-03-27 11:00:29 -04002880 spin:
2881 /*
2882 * Splice the empty reader page into the list around the head.
2883 */
2884 reader = rb_set_head_page(cpu_buffer);
Steven Rostedt0e1ff5d2010-01-06 20:40:44 -05002885 cpu_buffer->reader_page->list.next = rb_list_head(reader->list.next);
Steven Rostedtd7690412008-10-01 00:29:53 -04002886 cpu_buffer->reader_page->list.prev = reader->list.prev;
Steven Rostedtbf41a152008-10-04 02:00:59 -04002887
Steven Rostedt3adc54f2009-03-30 15:32:01 -04002888 /*
2889 * cpu_buffer->pages just needs to point to the buffer, it
2890 * has no specific buffer page to point to. Lets move it out
2891 * of our way so we don't accidently swap it.
2892 */
2893 cpu_buffer->pages = reader->list.prev;
2894
Steven Rostedt77ae3652009-03-27 11:00:29 -04002895 /* The reader page will be pointing to the new head */
2896 rb_set_list_to_head(cpu_buffer, &cpu_buffer->reader_page->list);
Steven Rostedtd7690412008-10-01 00:29:53 -04002897
2898 /*
Steven Rostedt66a8cb92010-03-31 13:21:56 -04002899 * We want to make sure we read the overruns after we set up our
2900 * pointers to the next object. The writer side does a
2901 * cmpxchg to cross pages which acts as the mb on the writer
2902 * side. Note, the reader will constantly fail the swap
2903 * while the writer is updating the pointers, so this
2904 * guarantees that the overwrite recorded here is the one we
2905 * want to compare with the last_overrun.
2906 */
2907 smp_mb();
2908 overwrite = local_read(&(cpu_buffer->overrun));
2909
2910 /*
Steven Rostedt77ae3652009-03-27 11:00:29 -04002911 * Here's the tricky part.
2912 *
2913 * We need to move the pointer past the header page.
2914 * But we can only do that if a writer is not currently
2915 * moving it. The page before the header page has the
2916 * flag bit '1' set if it is pointing to the page we want.
2917 * but if the writer is in the process of moving it
2918 * than it will be '2' or already moved '0'.
Steven Rostedtd7690412008-10-01 00:29:53 -04002919 */
Steven Rostedtd7690412008-10-01 00:29:53 -04002920
Steven Rostedt77ae3652009-03-27 11:00:29 -04002921 ret = rb_head_page_replace(reader, cpu_buffer->reader_page);
2922
2923 /*
2924 * If we did not convert it, then we must try again.
2925 */
2926 if (!ret)
2927 goto spin;
2928
2929 /*
2930 * Yeah! We succeeded in replacing the page.
2931 *
2932 * Now make the new head point back to the reader page.
2933 */
David Sharp5ded3dc62010-01-06 17:12:07 -08002934 rb_list_head(reader->list.next)->prev = &cpu_buffer->reader_page->list;
Steven Rostedt77ae3652009-03-27 11:00:29 -04002935 rb_inc_page(cpu_buffer, &cpu_buffer->head_page);
Steven Rostedtd7690412008-10-01 00:29:53 -04002936
2937 /* Finally update the reader page to the new head */
2938 cpu_buffer->reader_page = reader;
2939 rb_reset_reader_page(cpu_buffer);
2940
Steven Rostedt66a8cb92010-03-31 13:21:56 -04002941 if (overwrite != cpu_buffer->last_overrun) {
2942 cpu_buffer->lost_events = overwrite - cpu_buffer->last_overrun;
2943 cpu_buffer->last_overrun = overwrite;
2944 }
2945
Steven Rostedtd7690412008-10-01 00:29:53 -04002946 goto again;
2947
2948 out:
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01002949 arch_spin_unlock(&cpu_buffer->lock);
Steven Rostedt3e03fb72008-11-06 00:09:43 -05002950 local_irq_restore(flags);
Steven Rostedtd7690412008-10-01 00:29:53 -04002951
2952 return reader;
2953}
2954
2955static void rb_advance_reader(struct ring_buffer_per_cpu *cpu_buffer)
2956{
2957 struct ring_buffer_event *event;
2958 struct buffer_page *reader;
2959 unsigned length;
2960
2961 reader = rb_get_reader_page(cpu_buffer);
2962
2963 /* This function should not be called when buffer is empty */
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05002964 if (RB_WARN_ON(cpu_buffer, !reader))
2965 return;
Steven Rostedtd7690412008-10-01 00:29:53 -04002966
2967 event = rb_reader_event(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002968
Steven Rostedta1863c22009-09-03 10:23:58 -04002969 if (event->type_len <= RINGBUF_TYPE_DATA_TYPE_LEN_MAX)
Steven Rostedte4906ef2009-04-30 20:49:44 -04002970 cpu_buffer->read++;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002971
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002972 rb_update_read_stamp(cpu_buffer, event);
2973
Steven Rostedtd7690412008-10-01 00:29:53 -04002974 length = rb_event_length(event);
Steven Rostedt6f807ac2008-10-04 02:00:58 -04002975 cpu_buffer->reader_page->read += length;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002976}
2977
2978static void rb_advance_iter(struct ring_buffer_iter *iter)
2979{
2980 struct ring_buffer *buffer;
2981 struct ring_buffer_per_cpu *cpu_buffer;
2982 struct ring_buffer_event *event;
2983 unsigned length;
2984
2985 cpu_buffer = iter->cpu_buffer;
2986 buffer = cpu_buffer->buffer;
2987
2988 /*
2989 * Check if we are at the end of the buffer.
2990 */
Steven Rostedtbf41a152008-10-04 02:00:59 -04002991 if (iter->head >= rb_page_size(iter->head_page)) {
Steven Rostedtea05b572009-06-03 09:30:10 -04002992 /* discarded commits can make the page empty */
2993 if (iter->head_page == cpu_buffer->commit_page)
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05002994 return;
Steven Rostedtd7690412008-10-01 00:29:53 -04002995 rb_inc_iter(iter);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002996 return;
2997 }
2998
2999 event = rb_iter_head_event(iter);
3000
3001 length = rb_event_length(event);
3002
3003 /*
3004 * This should not be called to advance the header if we are
3005 * at the tail of the buffer.
3006 */
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05003007 if (RB_WARN_ON(cpu_buffer,
Steven Rostedtf536aaf2008-11-10 23:07:30 -05003008 (iter->head_page == cpu_buffer->commit_page) &&
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05003009 (iter->head + length > rb_commit_index(cpu_buffer))))
3010 return;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003011
3012 rb_update_iter_read_stamp(iter, event);
3013
3014 iter->head += length;
3015
3016 /* check for end of page padding */
Steven Rostedtbf41a152008-10-04 02:00:59 -04003017 if ((iter->head >= rb_page_size(iter->head_page)) &&
3018 (iter->head_page != cpu_buffer->commit_page))
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003019 rb_advance_iter(iter);
3020}
3021
Steven Rostedt66a8cb92010-03-31 13:21:56 -04003022static int rb_lost_events(struct ring_buffer_per_cpu *cpu_buffer)
3023{
3024 return cpu_buffer->lost_events;
3025}
3026
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003027static struct ring_buffer_event *
Steven Rostedt66a8cb92010-03-31 13:21:56 -04003028rb_buffer_peek(struct ring_buffer_per_cpu *cpu_buffer, u64 *ts,
3029 unsigned long *lost_events)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003030{
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003031 struct ring_buffer_event *event;
Steven Rostedtd7690412008-10-01 00:29:53 -04003032 struct buffer_page *reader;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003033 int nr_loops = 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003034
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003035 again:
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003036 /*
3037 * We repeat when a timestamp is encountered. It is possible
3038 * to get multiple timestamps from an interrupt entering just
Steven Rostedtea05b572009-06-03 09:30:10 -04003039 * as one timestamp is about to be written, or from discarded
3040 * commits. The most that we can have is the number on a single page.
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003041 */
Steven Rostedtea05b572009-06-03 09:30:10 -04003042 if (RB_WARN_ON(cpu_buffer, ++nr_loops > RB_TIMESTAMPS_PER_PAGE))
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003043 return NULL;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003044
Steven Rostedtd7690412008-10-01 00:29:53 -04003045 reader = rb_get_reader_page(cpu_buffer);
3046 if (!reader)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003047 return NULL;
3048
Steven Rostedtd7690412008-10-01 00:29:53 -04003049 event = rb_reader_event(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003050
Lai Jiangshan334d4162009-04-24 11:27:05 +08003051 switch (event->type_len) {
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003052 case RINGBUF_TYPE_PADDING:
Tom Zanussi2d622712009-03-22 03:30:49 -05003053 if (rb_null_event(event))
3054 RB_WARN_ON(cpu_buffer, 1);
3055 /*
3056 * Because the writer could be discarding every
3057 * event it creates (which would probably be bad)
3058 * if we were to go back to "again" then we may never
3059 * catch up, and will trigger the warn on, or lock
3060 * the box. Return the padding, and we will release
3061 * the current locks, and try again.
3062 */
Tom Zanussi2d622712009-03-22 03:30:49 -05003063 return event;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003064
3065 case RINGBUF_TYPE_TIME_EXTEND:
3066 /* Internal data, OK to advance */
Steven Rostedtd7690412008-10-01 00:29:53 -04003067 rb_advance_reader(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003068 goto again;
3069
3070 case RINGBUF_TYPE_TIME_STAMP:
3071 /* FIXME: not implemented */
Steven Rostedtd7690412008-10-01 00:29:53 -04003072 rb_advance_reader(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003073 goto again;
3074
3075 case RINGBUF_TYPE_DATA:
3076 if (ts) {
3077 *ts = cpu_buffer->read_stamp + event->time_delta;
Robert Richterd8eeb2d2009-07-31 14:58:04 +02003078 ring_buffer_normalize_time_stamp(cpu_buffer->buffer,
Steven Rostedt37886f62009-03-17 17:22:06 -04003079 cpu_buffer->cpu, ts);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003080 }
Steven Rostedt66a8cb92010-03-31 13:21:56 -04003081 if (lost_events)
3082 *lost_events = rb_lost_events(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003083 return event;
3084
3085 default:
3086 BUG();
3087 }
3088
3089 return NULL;
3090}
Robert Richterc4f50182008-12-11 16:49:22 +01003091EXPORT_SYMBOL_GPL(ring_buffer_peek);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003092
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003093static struct ring_buffer_event *
3094rb_iter_peek(struct ring_buffer_iter *iter, u64 *ts)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003095{
3096 struct ring_buffer *buffer;
3097 struct ring_buffer_per_cpu *cpu_buffer;
3098 struct ring_buffer_event *event;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003099 int nr_loops = 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003100
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003101 cpu_buffer = iter->cpu_buffer;
3102 buffer = cpu_buffer->buffer;
3103
Steven Rostedt492a74f2010-01-25 15:17:47 -05003104 /*
3105 * Check if someone performed a consuming read to
3106 * the buffer. A consuming read invalidates the iterator
3107 * and we need to reset the iterator in this case.
3108 */
3109 if (unlikely(iter->cache_read != cpu_buffer->read ||
3110 iter->cache_reader_page != cpu_buffer->reader_page))
3111 rb_iter_reset(iter);
3112
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003113 again:
Steven Rostedt3c05d742010-01-26 16:14:08 -05003114 if (ring_buffer_iter_empty(iter))
3115 return NULL;
3116
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003117 /*
Steven Rostedtea05b572009-06-03 09:30:10 -04003118 * We repeat when a timestamp is encountered.
3119 * We can get multiple timestamps by nested interrupts or also
3120 * if filtering is on (discarding commits). Since discarding
3121 * commits can be frequent we can get a lot of timestamps.
3122 * But we limit them by not adding timestamps if they begin
3123 * at the start of a page.
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003124 */
Steven Rostedtea05b572009-06-03 09:30:10 -04003125 if (RB_WARN_ON(cpu_buffer, ++nr_loops > RB_TIMESTAMPS_PER_PAGE))
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003126 return NULL;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003127
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003128 if (rb_per_cpu_empty(cpu_buffer))
3129 return NULL;
3130
Steven Rostedt3c05d742010-01-26 16:14:08 -05003131 if (iter->head >= local_read(&iter->head_page->page->commit)) {
3132 rb_inc_iter(iter);
3133 goto again;
3134 }
3135
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003136 event = rb_iter_head_event(iter);
3137
Lai Jiangshan334d4162009-04-24 11:27:05 +08003138 switch (event->type_len) {
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003139 case RINGBUF_TYPE_PADDING:
Tom Zanussi2d622712009-03-22 03:30:49 -05003140 if (rb_null_event(event)) {
3141 rb_inc_iter(iter);
3142 goto again;
3143 }
3144 rb_advance_iter(iter);
3145 return event;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003146
3147 case RINGBUF_TYPE_TIME_EXTEND:
3148 /* Internal data, OK to advance */
3149 rb_advance_iter(iter);
3150 goto again;
3151
3152 case RINGBUF_TYPE_TIME_STAMP:
3153 /* FIXME: not implemented */
3154 rb_advance_iter(iter);
3155 goto again;
3156
3157 case RINGBUF_TYPE_DATA:
3158 if (ts) {
3159 *ts = iter->read_stamp + event->time_delta;
Steven Rostedt37886f62009-03-17 17:22:06 -04003160 ring_buffer_normalize_time_stamp(buffer,
3161 cpu_buffer->cpu, ts);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003162 }
3163 return event;
3164
3165 default:
3166 BUG();
3167 }
3168
3169 return NULL;
3170}
Robert Richterc4f50182008-12-11 16:49:22 +01003171EXPORT_SYMBOL_GPL(ring_buffer_iter_peek);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003172
Steven Rostedt8d707e82009-06-16 21:22:48 -04003173static inline int rb_ok_to_lock(void)
3174{
3175 /*
3176 * If an NMI die dumps out the content of the ring buffer
3177 * do not grab locks. We also permanently disable the ring
3178 * buffer too. A one time deal is all you get from reading
3179 * the ring buffer from an NMI.
3180 */
Steven Rostedt464e85e2009-08-05 15:26:37 -04003181 if (likely(!in_nmi()))
Steven Rostedt8d707e82009-06-16 21:22:48 -04003182 return 1;
3183
3184 tracing_off_permanent();
3185 return 0;
3186}
3187
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003188/**
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003189 * ring_buffer_peek - peek at the next event to be read
3190 * @buffer: The ring buffer to read
3191 * @cpu: The cpu to peak at
3192 * @ts: The timestamp counter of this event.
Steven Rostedt66a8cb92010-03-31 13:21:56 -04003193 * @lost_events: a variable to store if events were lost (may be NULL)
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003194 *
3195 * This will return the event that will be read next, but does
3196 * not consume the data.
3197 */
3198struct ring_buffer_event *
Steven Rostedt66a8cb92010-03-31 13:21:56 -04003199ring_buffer_peek(struct ring_buffer *buffer, int cpu, u64 *ts,
3200 unsigned long *lost_events)
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003201{
3202 struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
Steven Rostedt8aabee52009-03-12 13:13:49 -04003203 struct ring_buffer_event *event;
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003204 unsigned long flags;
Steven Rostedt8d707e82009-06-16 21:22:48 -04003205 int dolock;
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003206
Steven Rostedt554f7862009-03-11 22:00:13 -04003207 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04003208 return NULL;
Steven Rostedt554f7862009-03-11 22:00:13 -04003209
Steven Rostedt8d707e82009-06-16 21:22:48 -04003210 dolock = rb_ok_to_lock();
Tom Zanussi2d622712009-03-22 03:30:49 -05003211 again:
Steven Rostedt8d707e82009-06-16 21:22:48 -04003212 local_irq_save(flags);
3213 if (dolock)
3214 spin_lock(&cpu_buffer->reader_lock);
Steven Rostedt66a8cb92010-03-31 13:21:56 -04003215 event = rb_buffer_peek(cpu_buffer, ts, lost_events);
Robert Richter469535a2009-07-30 19:19:18 +02003216 if (event && event->type_len == RINGBUF_TYPE_PADDING)
3217 rb_advance_reader(cpu_buffer);
Steven Rostedt8d707e82009-06-16 21:22:48 -04003218 if (dolock)
3219 spin_unlock(&cpu_buffer->reader_lock);
3220 local_irq_restore(flags);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003221
Steven Rostedt1b959e12009-09-03 10:12:13 -04003222 if (event && event->type_len == RINGBUF_TYPE_PADDING)
Tom Zanussi2d622712009-03-22 03:30:49 -05003223 goto again;
Tom Zanussi2d622712009-03-22 03:30:49 -05003224
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003225 return event;
3226}
3227
3228/**
3229 * ring_buffer_iter_peek - peek at the next event to be read
3230 * @iter: The ring buffer iterator
3231 * @ts: The timestamp counter of this event.
3232 *
3233 * This will return the event that will be read next, but does
3234 * not increment the iterator.
3235 */
3236struct ring_buffer_event *
3237ring_buffer_iter_peek(struct ring_buffer_iter *iter, u64 *ts)
3238{
3239 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
3240 struct ring_buffer_event *event;
3241 unsigned long flags;
3242
Tom Zanussi2d622712009-03-22 03:30:49 -05003243 again:
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003244 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
3245 event = rb_iter_peek(iter, ts);
3246 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
3247
Steven Rostedt1b959e12009-09-03 10:12:13 -04003248 if (event && event->type_len == RINGBUF_TYPE_PADDING)
Tom Zanussi2d622712009-03-22 03:30:49 -05003249 goto again;
Tom Zanussi2d622712009-03-22 03:30:49 -05003250
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003251 return event;
3252}
3253
3254/**
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003255 * ring_buffer_consume - return an event and consume it
3256 * @buffer: The ring buffer to get the next event from
Steven Rostedt66a8cb92010-03-31 13:21:56 -04003257 * @cpu: the cpu to read the buffer from
3258 * @ts: a variable to store the timestamp (may be NULL)
3259 * @lost_events: a variable to store if events were lost (may be NULL)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003260 *
3261 * Returns the next event in the ring buffer, and that event is consumed.
3262 * Meaning, that sequential reads will keep returning a different event,
3263 * and eventually empty the ring buffer if the producer is slower.
3264 */
3265struct ring_buffer_event *
Steven Rostedt66a8cb92010-03-31 13:21:56 -04003266ring_buffer_consume(struct ring_buffer *buffer, int cpu, u64 *ts,
3267 unsigned long *lost_events)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003268{
Steven Rostedt554f7862009-03-11 22:00:13 -04003269 struct ring_buffer_per_cpu *cpu_buffer;
3270 struct ring_buffer_event *event = NULL;
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003271 unsigned long flags;
Steven Rostedt8d707e82009-06-16 21:22:48 -04003272 int dolock;
3273
3274 dolock = rb_ok_to_lock();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003275
Tom Zanussi2d622712009-03-22 03:30:49 -05003276 again:
Steven Rostedt554f7862009-03-11 22:00:13 -04003277 /* might be called in atomic */
3278 preempt_disable();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003279
Steven Rostedt554f7862009-03-11 22:00:13 -04003280 if (!cpumask_test_cpu(cpu, buffer->cpumask))
3281 goto out;
3282
3283 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt8d707e82009-06-16 21:22:48 -04003284 local_irq_save(flags);
3285 if (dolock)
3286 spin_lock(&cpu_buffer->reader_lock);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003287
Steven Rostedt66a8cb92010-03-31 13:21:56 -04003288 event = rb_buffer_peek(cpu_buffer, ts, lost_events);
3289 if (event) {
3290 cpu_buffer->lost_events = 0;
Robert Richter469535a2009-07-30 19:19:18 +02003291 rb_advance_reader(cpu_buffer);
Steven Rostedt66a8cb92010-03-31 13:21:56 -04003292 }
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003293
Steven Rostedt8d707e82009-06-16 21:22:48 -04003294 if (dolock)
3295 spin_unlock(&cpu_buffer->reader_lock);
3296 local_irq_restore(flags);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003297
Steven Rostedt554f7862009-03-11 22:00:13 -04003298 out:
3299 preempt_enable();
3300
Steven Rostedt1b959e12009-09-03 10:12:13 -04003301 if (event && event->type_len == RINGBUF_TYPE_PADDING)
Tom Zanussi2d622712009-03-22 03:30:49 -05003302 goto again;
Tom Zanussi2d622712009-03-22 03:30:49 -05003303
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003304 return event;
3305}
Robert Richterc4f50182008-12-11 16:49:22 +01003306EXPORT_SYMBOL_GPL(ring_buffer_consume);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003307
3308/**
David Miller72c9ddf2010-04-20 15:47:11 -07003309 * ring_buffer_read_prepare - Prepare for a non consuming read of the buffer
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003310 * @buffer: The ring buffer to read from
3311 * @cpu: The cpu buffer to iterate over
3312 *
David Miller72c9ddf2010-04-20 15:47:11 -07003313 * This performs the initial preparations necessary to iterate
3314 * through the buffer. Memory is allocated, buffer recording
3315 * is disabled, and the iterator pointer is returned to the caller.
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003316 *
David Miller72c9ddf2010-04-20 15:47:11 -07003317 * Disabling buffer recordng prevents the reading from being
3318 * corrupted. This is not a consuming read, so a producer is not
3319 * expected.
3320 *
3321 * After a sequence of ring_buffer_read_prepare calls, the user is
3322 * expected to make at least one call to ring_buffer_prepare_sync.
3323 * Afterwards, ring_buffer_read_start is invoked to get things going
3324 * for real.
3325 *
3326 * This overall must be paired with ring_buffer_finish.
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003327 */
3328struct ring_buffer_iter *
David Miller72c9ddf2010-04-20 15:47:11 -07003329ring_buffer_read_prepare(struct ring_buffer *buffer, int cpu)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003330{
3331 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedt8aabee52009-03-12 13:13:49 -04003332 struct ring_buffer_iter *iter;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003333
Rusty Russell9e01c1b2009-01-01 10:12:22 +10303334 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04003335 return NULL;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003336
3337 iter = kmalloc(sizeof(*iter), GFP_KERNEL);
3338 if (!iter)
Steven Rostedt8aabee52009-03-12 13:13:49 -04003339 return NULL;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003340
3341 cpu_buffer = buffer->buffers[cpu];
3342
3343 iter->cpu_buffer = cpu_buffer;
3344
3345 atomic_inc(&cpu_buffer->record_disabled);
David Miller72c9ddf2010-04-20 15:47:11 -07003346
3347 return iter;
3348}
3349EXPORT_SYMBOL_GPL(ring_buffer_read_prepare);
3350
3351/**
3352 * ring_buffer_read_prepare_sync - Synchronize a set of prepare calls
3353 *
3354 * All previously invoked ring_buffer_read_prepare calls to prepare
3355 * iterators will be synchronized. Afterwards, read_buffer_read_start
3356 * calls on those iterators are allowed.
3357 */
3358void
3359ring_buffer_read_prepare_sync(void)
3360{
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003361 synchronize_sched();
David Miller72c9ddf2010-04-20 15:47:11 -07003362}
3363EXPORT_SYMBOL_GPL(ring_buffer_read_prepare_sync);
3364
3365/**
3366 * ring_buffer_read_start - start a non consuming read of the buffer
3367 * @iter: The iterator returned by ring_buffer_read_prepare
3368 *
3369 * This finalizes the startup of an iteration through the buffer.
3370 * The iterator comes from a call to ring_buffer_read_prepare and
3371 * an intervening ring_buffer_read_prepare_sync must have been
3372 * performed.
3373 *
3374 * Must be paired with ring_buffer_finish.
3375 */
3376void
3377ring_buffer_read_start(struct ring_buffer_iter *iter)
3378{
3379 struct ring_buffer_per_cpu *cpu_buffer;
3380 unsigned long flags;
3381
3382 if (!iter)
3383 return;
3384
3385 cpu_buffer = iter->cpu_buffer;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003386
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003387 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01003388 arch_spin_lock(&cpu_buffer->lock);
Steven Rostedt642edba2008-11-12 00:01:26 -05003389 rb_iter_reset(iter);
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01003390 arch_spin_unlock(&cpu_buffer->lock);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003391 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003392}
Robert Richterc4f50182008-12-11 16:49:22 +01003393EXPORT_SYMBOL_GPL(ring_buffer_read_start);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003394
3395/**
3396 * ring_buffer_finish - finish reading the iterator of the buffer
3397 * @iter: The iterator retrieved by ring_buffer_start
3398 *
3399 * This re-enables the recording to the buffer, and frees the
3400 * iterator.
3401 */
3402void
3403ring_buffer_read_finish(struct ring_buffer_iter *iter)
3404{
3405 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
3406
3407 atomic_dec(&cpu_buffer->record_disabled);
3408 kfree(iter);
3409}
Robert Richterc4f50182008-12-11 16:49:22 +01003410EXPORT_SYMBOL_GPL(ring_buffer_read_finish);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003411
3412/**
3413 * ring_buffer_read - read the next item in the ring buffer by the iterator
3414 * @iter: The ring buffer iterator
3415 * @ts: The time stamp of the event read.
3416 *
3417 * This reads the next event in the ring buffer and increments the iterator.
3418 */
3419struct ring_buffer_event *
3420ring_buffer_read(struct ring_buffer_iter *iter, u64 *ts)
3421{
3422 struct ring_buffer_event *event;
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003423 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
3424 unsigned long flags;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003425
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003426 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
Steven Rostedt7e9391c2009-09-03 10:02:09 -04003427 again:
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003428 event = rb_iter_peek(iter, ts);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003429 if (!event)
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003430 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003431
Steven Rostedt7e9391c2009-09-03 10:02:09 -04003432 if (event->type_len == RINGBUF_TYPE_PADDING)
3433 goto again;
3434
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003435 rb_advance_iter(iter);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003436 out:
3437 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003438
3439 return event;
3440}
Robert Richterc4f50182008-12-11 16:49:22 +01003441EXPORT_SYMBOL_GPL(ring_buffer_read);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003442
3443/**
3444 * ring_buffer_size - return the size of the ring buffer (in bytes)
3445 * @buffer: The ring buffer.
3446 */
3447unsigned long ring_buffer_size(struct ring_buffer *buffer)
3448{
3449 return BUF_PAGE_SIZE * buffer->pages;
3450}
Robert Richterc4f50182008-12-11 16:49:22 +01003451EXPORT_SYMBOL_GPL(ring_buffer_size);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003452
3453static void
3454rb_reset_cpu(struct ring_buffer_per_cpu *cpu_buffer)
3455{
Steven Rostedt77ae3652009-03-27 11:00:29 -04003456 rb_head_page_deactivate(cpu_buffer);
3457
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003458 cpu_buffer->head_page
Steven Rostedt3adc54f2009-03-30 15:32:01 -04003459 = list_entry(cpu_buffer->pages, struct buffer_page, list);
Steven Rostedtbf41a152008-10-04 02:00:59 -04003460 local_set(&cpu_buffer->head_page->write, 0);
Steven Rostedt778c55d2009-05-01 18:44:45 -04003461 local_set(&cpu_buffer->head_page->entries, 0);
Steven Rostedtabc9b562008-12-02 15:34:06 -05003462 local_set(&cpu_buffer->head_page->page->commit, 0);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003463
Steven Rostedt6f807ac2008-10-04 02:00:58 -04003464 cpu_buffer->head_page->read = 0;
Steven Rostedtbf41a152008-10-04 02:00:59 -04003465
3466 cpu_buffer->tail_page = cpu_buffer->head_page;
3467 cpu_buffer->commit_page = cpu_buffer->head_page;
3468
3469 INIT_LIST_HEAD(&cpu_buffer->reader_page->list);
3470 local_set(&cpu_buffer->reader_page->write, 0);
Steven Rostedt778c55d2009-05-01 18:44:45 -04003471 local_set(&cpu_buffer->reader_page->entries, 0);
Steven Rostedtabc9b562008-12-02 15:34:06 -05003472 local_set(&cpu_buffer->reader_page->page->commit, 0);
Steven Rostedt6f807ac2008-10-04 02:00:58 -04003473 cpu_buffer->reader_page->read = 0;
Steven Rostedtd7690412008-10-01 00:29:53 -04003474
Steven Rostedt77ae3652009-03-27 11:00:29 -04003475 local_set(&cpu_buffer->commit_overrun, 0);
3476 local_set(&cpu_buffer->overrun, 0);
Steven Rostedte4906ef2009-04-30 20:49:44 -04003477 local_set(&cpu_buffer->entries, 0);
Steven Rostedtfa743952009-06-16 12:37:57 -04003478 local_set(&cpu_buffer->committing, 0);
3479 local_set(&cpu_buffer->commits, 0);
Steven Rostedt77ae3652009-03-27 11:00:29 -04003480 cpu_buffer->read = 0;
Steven Rostedt69507c02009-01-21 18:45:57 -05003481
3482 cpu_buffer->write_stamp = 0;
3483 cpu_buffer->read_stamp = 0;
Steven Rostedt77ae3652009-03-27 11:00:29 -04003484
Steven Rostedt66a8cb92010-03-31 13:21:56 -04003485 cpu_buffer->lost_events = 0;
3486 cpu_buffer->last_overrun = 0;
3487
Steven Rostedt77ae3652009-03-27 11:00:29 -04003488 rb_head_page_activate(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003489}
3490
3491/**
3492 * ring_buffer_reset_cpu - reset a ring buffer per CPU buffer
3493 * @buffer: The ring buffer to reset a per cpu buffer of
3494 * @cpu: The CPU buffer to be reset
3495 */
3496void ring_buffer_reset_cpu(struct ring_buffer *buffer, int cpu)
3497{
3498 struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
3499 unsigned long flags;
3500
Rusty Russell9e01c1b2009-01-01 10:12:22 +10303501 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04003502 return;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003503
Steven Rostedt41ede232009-05-01 20:26:54 -04003504 atomic_inc(&cpu_buffer->record_disabled);
3505
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003506 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
3507
Steven Rostedt41b6a952009-09-02 09:59:48 -04003508 if (RB_WARN_ON(cpu_buffer, local_read(&cpu_buffer->committing)))
3509 goto out;
3510
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01003511 arch_spin_lock(&cpu_buffer->lock);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003512
3513 rb_reset_cpu(cpu_buffer);
3514
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01003515 arch_spin_unlock(&cpu_buffer->lock);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003516
Steven Rostedt41b6a952009-09-02 09:59:48 -04003517 out:
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003518 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
Steven Rostedt41ede232009-05-01 20:26:54 -04003519
3520 atomic_dec(&cpu_buffer->record_disabled);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003521}
Robert Richterc4f50182008-12-11 16:49:22 +01003522EXPORT_SYMBOL_GPL(ring_buffer_reset_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003523
3524/**
3525 * ring_buffer_reset - reset a ring buffer
3526 * @buffer: The ring buffer to reset all cpu buffers
3527 */
3528void ring_buffer_reset(struct ring_buffer *buffer)
3529{
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003530 int cpu;
3531
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003532 for_each_buffer_cpu(buffer, cpu)
Steven Rostedtd7690412008-10-01 00:29:53 -04003533 ring_buffer_reset_cpu(buffer, cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003534}
Robert Richterc4f50182008-12-11 16:49:22 +01003535EXPORT_SYMBOL_GPL(ring_buffer_reset);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003536
3537/**
3538 * rind_buffer_empty - is the ring buffer empty?
3539 * @buffer: The ring buffer to test
3540 */
3541int ring_buffer_empty(struct ring_buffer *buffer)
3542{
3543 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedtd4788202009-06-17 00:39:43 -04003544 unsigned long flags;
Steven Rostedt8d707e82009-06-16 21:22:48 -04003545 int dolock;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003546 int cpu;
Steven Rostedtd4788202009-06-17 00:39:43 -04003547 int ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003548
Steven Rostedt8d707e82009-06-16 21:22:48 -04003549 dolock = rb_ok_to_lock();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003550
3551 /* yes this is racy, but if you don't like the race, lock the buffer */
3552 for_each_buffer_cpu(buffer, cpu) {
3553 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt8d707e82009-06-16 21:22:48 -04003554 local_irq_save(flags);
3555 if (dolock)
3556 spin_lock(&cpu_buffer->reader_lock);
Steven Rostedtd4788202009-06-17 00:39:43 -04003557 ret = rb_per_cpu_empty(cpu_buffer);
Steven Rostedt8d707e82009-06-16 21:22:48 -04003558 if (dolock)
3559 spin_unlock(&cpu_buffer->reader_lock);
3560 local_irq_restore(flags);
3561
Steven Rostedtd4788202009-06-17 00:39:43 -04003562 if (!ret)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003563 return 0;
3564 }
Steven Rostedt554f7862009-03-11 22:00:13 -04003565
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003566 return 1;
3567}
Robert Richterc4f50182008-12-11 16:49:22 +01003568EXPORT_SYMBOL_GPL(ring_buffer_empty);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003569
3570/**
3571 * ring_buffer_empty_cpu - is a cpu buffer of a ring buffer empty?
3572 * @buffer: The ring buffer
3573 * @cpu: The CPU buffer to test
3574 */
3575int ring_buffer_empty_cpu(struct ring_buffer *buffer, int cpu)
3576{
3577 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedtd4788202009-06-17 00:39:43 -04003578 unsigned long flags;
Steven Rostedt8d707e82009-06-16 21:22:48 -04003579 int dolock;
Steven Rostedt8aabee52009-03-12 13:13:49 -04003580 int ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003581
Rusty Russell9e01c1b2009-01-01 10:12:22 +10303582 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04003583 return 1;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003584
Steven Rostedt8d707e82009-06-16 21:22:48 -04003585 dolock = rb_ok_to_lock();
Steven Rostedt554f7862009-03-11 22:00:13 -04003586
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003587 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt8d707e82009-06-16 21:22:48 -04003588 local_irq_save(flags);
3589 if (dolock)
3590 spin_lock(&cpu_buffer->reader_lock);
Steven Rostedt554f7862009-03-11 22:00:13 -04003591 ret = rb_per_cpu_empty(cpu_buffer);
Steven Rostedt8d707e82009-06-16 21:22:48 -04003592 if (dolock)
3593 spin_unlock(&cpu_buffer->reader_lock);
3594 local_irq_restore(flags);
Steven Rostedt554f7862009-03-11 22:00:13 -04003595
3596 return ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003597}
Robert Richterc4f50182008-12-11 16:49:22 +01003598EXPORT_SYMBOL_GPL(ring_buffer_empty_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003599
Steven Rostedt85bac322009-09-04 14:24:40 -04003600#ifdef CONFIG_RING_BUFFER_ALLOW_SWAP
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003601/**
3602 * ring_buffer_swap_cpu - swap a CPU buffer between two ring buffers
3603 * @buffer_a: One buffer to swap with
3604 * @buffer_b: The other buffer to swap with
3605 *
3606 * This function is useful for tracers that want to take a "snapshot"
3607 * of a CPU buffer and has another back up buffer lying around.
3608 * it is expected that the tracer handles the cpu buffer not being
3609 * used at the moment.
3610 */
3611int ring_buffer_swap_cpu(struct ring_buffer *buffer_a,
3612 struct ring_buffer *buffer_b, int cpu)
3613{
3614 struct ring_buffer_per_cpu *cpu_buffer_a;
3615 struct ring_buffer_per_cpu *cpu_buffer_b;
Steven Rostedt554f7862009-03-11 22:00:13 -04003616 int ret = -EINVAL;
3617
Rusty Russell9e01c1b2009-01-01 10:12:22 +10303618 if (!cpumask_test_cpu(cpu, buffer_a->cpumask) ||
3619 !cpumask_test_cpu(cpu, buffer_b->cpumask))
Steven Rostedt554f7862009-03-11 22:00:13 -04003620 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003621
3622 /* At least make sure the two buffers are somewhat the same */
Lai Jiangshan6d102bc2008-12-17 17:48:23 +08003623 if (buffer_a->pages != buffer_b->pages)
Steven Rostedt554f7862009-03-11 22:00:13 -04003624 goto out;
3625
3626 ret = -EAGAIN;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003627
Steven Rostedt97b17ef2009-01-21 15:24:56 -05003628 if (ring_buffer_flags != RB_BUFFERS_ON)
Steven Rostedt554f7862009-03-11 22:00:13 -04003629 goto out;
Steven Rostedt97b17ef2009-01-21 15:24:56 -05003630
3631 if (atomic_read(&buffer_a->record_disabled))
Steven Rostedt554f7862009-03-11 22:00:13 -04003632 goto out;
Steven Rostedt97b17ef2009-01-21 15:24:56 -05003633
3634 if (atomic_read(&buffer_b->record_disabled))
Steven Rostedt554f7862009-03-11 22:00:13 -04003635 goto out;
Steven Rostedt97b17ef2009-01-21 15:24:56 -05003636
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003637 cpu_buffer_a = buffer_a->buffers[cpu];
3638 cpu_buffer_b = buffer_b->buffers[cpu];
3639
Steven Rostedt97b17ef2009-01-21 15:24:56 -05003640 if (atomic_read(&cpu_buffer_a->record_disabled))
Steven Rostedt554f7862009-03-11 22:00:13 -04003641 goto out;
Steven Rostedt97b17ef2009-01-21 15:24:56 -05003642
3643 if (atomic_read(&cpu_buffer_b->record_disabled))
Steven Rostedt554f7862009-03-11 22:00:13 -04003644 goto out;
Steven Rostedt97b17ef2009-01-21 15:24:56 -05003645
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003646 /*
3647 * We can't do a synchronize_sched here because this
3648 * function can be called in atomic context.
3649 * Normally this will be called from the same CPU as cpu.
3650 * If not it's up to the caller to protect this.
3651 */
3652 atomic_inc(&cpu_buffer_a->record_disabled);
3653 atomic_inc(&cpu_buffer_b->record_disabled);
3654
Steven Rostedt98277992009-09-02 10:56:15 -04003655 ret = -EBUSY;
3656 if (local_read(&cpu_buffer_a->committing))
3657 goto out_dec;
3658 if (local_read(&cpu_buffer_b->committing))
3659 goto out_dec;
3660
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003661 buffer_a->buffers[cpu] = cpu_buffer_b;
3662 buffer_b->buffers[cpu] = cpu_buffer_a;
3663
3664 cpu_buffer_b->buffer = buffer_a;
3665 cpu_buffer_a->buffer = buffer_b;
3666
Steven Rostedt98277992009-09-02 10:56:15 -04003667 ret = 0;
3668
3669out_dec:
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003670 atomic_dec(&cpu_buffer_a->record_disabled);
3671 atomic_dec(&cpu_buffer_b->record_disabled);
Steven Rostedt554f7862009-03-11 22:00:13 -04003672out:
Steven Rostedt554f7862009-03-11 22:00:13 -04003673 return ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003674}
Robert Richterc4f50182008-12-11 16:49:22 +01003675EXPORT_SYMBOL_GPL(ring_buffer_swap_cpu);
Steven Rostedt85bac322009-09-04 14:24:40 -04003676#endif /* CONFIG_RING_BUFFER_ALLOW_SWAP */
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003677
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003678/**
3679 * ring_buffer_alloc_read_page - allocate a page to read from buffer
3680 * @buffer: the buffer to allocate for.
3681 *
3682 * This function is used in conjunction with ring_buffer_read_page.
3683 * When reading a full page from the ring buffer, these functions
3684 * can be used to speed up the process. The calling function should
3685 * allocate a few pages first with this function. Then when it
3686 * needs to get pages from the ring buffer, it passes the result
3687 * of this function into ring_buffer_read_page, which will swap
3688 * the page that was allocated, with the read page of the buffer.
3689 *
3690 * Returns:
3691 * The page allocated, or NULL on error.
3692 */
3693void *ring_buffer_alloc_read_page(struct ring_buffer *buffer)
3694{
Steven Rostedt044fa782008-12-02 23:50:03 -05003695 struct buffer_data_page *bpage;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003696 unsigned long addr;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003697
3698 addr = __get_free_page(GFP_KERNEL);
3699 if (!addr)
3700 return NULL;
3701
Steven Rostedt044fa782008-12-02 23:50:03 -05003702 bpage = (void *)addr;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003703
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003704 rb_init_page(bpage);
3705
Steven Rostedt044fa782008-12-02 23:50:03 -05003706 return bpage;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003707}
Steven Rostedtd6ce96d2009-05-05 01:15:24 -04003708EXPORT_SYMBOL_GPL(ring_buffer_alloc_read_page);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003709
3710/**
3711 * ring_buffer_free_read_page - free an allocated read page
3712 * @buffer: the buffer the page was allocate for
3713 * @data: the page to free
3714 *
3715 * Free a page allocated from ring_buffer_alloc_read_page.
3716 */
3717void ring_buffer_free_read_page(struct ring_buffer *buffer, void *data)
3718{
3719 free_page((unsigned long)data);
3720}
Steven Rostedtd6ce96d2009-05-05 01:15:24 -04003721EXPORT_SYMBOL_GPL(ring_buffer_free_read_page);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003722
3723/**
3724 * ring_buffer_read_page - extract a page from the ring buffer
3725 * @buffer: buffer to extract from
3726 * @data_page: the page to use allocated from ring_buffer_alloc_read_page
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003727 * @len: amount to extract
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003728 * @cpu: the cpu of the buffer to extract
3729 * @full: should the extraction only happen when the page is full.
3730 *
3731 * This function will pull out a page from the ring buffer and consume it.
3732 * @data_page must be the address of the variable that was returned
3733 * from ring_buffer_alloc_read_page. This is because the page might be used
3734 * to swap with a page in the ring buffer.
3735 *
3736 * for example:
Lai Jiangshanb85fa012009-02-09 14:21:14 +08003737 * rpage = ring_buffer_alloc_read_page(buffer);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003738 * if (!rpage)
3739 * return error;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003740 * ret = ring_buffer_read_page(buffer, &rpage, len, cpu, 0);
Lai Jiangshan667d2412009-02-09 14:21:17 +08003741 * if (ret >= 0)
3742 * process_page(rpage, ret);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003743 *
3744 * When @full is set, the function will not return true unless
3745 * the writer is off the reader page.
3746 *
3747 * Note: it is up to the calling functions to handle sleeps and wakeups.
3748 * The ring buffer can be used anywhere in the kernel and can not
3749 * blindly call wake_up. The layer that uses the ring buffer must be
3750 * responsible for that.
3751 *
3752 * Returns:
Lai Jiangshan667d2412009-02-09 14:21:17 +08003753 * >=0 if data has been transferred, returns the offset of consumed data.
3754 * <0 if no data has been transferred.
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003755 */
3756int ring_buffer_read_page(struct ring_buffer *buffer,
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003757 void **data_page, size_t len, int cpu, int full)
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003758{
3759 struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
3760 struct ring_buffer_event *event;
Steven Rostedt044fa782008-12-02 23:50:03 -05003761 struct buffer_data_page *bpage;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003762 struct buffer_page *reader;
Steven Rostedtff0ff842010-03-31 22:11:42 -04003763 unsigned long missed_events;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003764 unsigned long flags;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003765 unsigned int commit;
Lai Jiangshan667d2412009-02-09 14:21:17 +08003766 unsigned int read;
Steven Rostedt4f3640f2009-03-03 23:52:42 -05003767 u64 save_timestamp;
Lai Jiangshan667d2412009-02-09 14:21:17 +08003768 int ret = -1;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003769
Steven Rostedt554f7862009-03-11 22:00:13 -04003770 if (!cpumask_test_cpu(cpu, buffer->cpumask))
3771 goto out;
3772
Steven Rostedt474d32b2009-03-03 19:51:40 -05003773 /*
3774 * If len is not big enough to hold the page header, then
3775 * we can not copy anything.
3776 */
3777 if (len <= BUF_PAGE_HDR_SIZE)
Steven Rostedt554f7862009-03-11 22:00:13 -04003778 goto out;
Steven Rostedt474d32b2009-03-03 19:51:40 -05003779
3780 len -= BUF_PAGE_HDR_SIZE;
3781
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003782 if (!data_page)
Steven Rostedt554f7862009-03-11 22:00:13 -04003783 goto out;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003784
Steven Rostedt044fa782008-12-02 23:50:03 -05003785 bpage = *data_page;
3786 if (!bpage)
Steven Rostedt554f7862009-03-11 22:00:13 -04003787 goto out;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003788
3789 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
3790
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003791 reader = rb_get_reader_page(cpu_buffer);
3792 if (!reader)
Steven Rostedt554f7862009-03-11 22:00:13 -04003793 goto out_unlock;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003794
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003795 event = rb_reader_event(cpu_buffer);
Lai Jiangshan667d2412009-02-09 14:21:17 +08003796
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003797 read = reader->read;
3798 commit = rb_page_commit(reader);
3799
Steven Rostedt66a8cb92010-03-31 13:21:56 -04003800 /* Check if any events were dropped */
Steven Rostedtff0ff842010-03-31 22:11:42 -04003801 missed_events = cpu_buffer->lost_events;
Steven Rostedt66a8cb92010-03-31 13:21:56 -04003802
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003803 /*
Steven Rostedt474d32b2009-03-03 19:51:40 -05003804 * If this page has been partially read or
3805 * if len is not big enough to read the rest of the page or
3806 * a writer is still on the page, then
3807 * we must copy the data from the page to the buffer.
3808 * Otherwise, we can simply swap the page with the one passed in.
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003809 */
Steven Rostedt474d32b2009-03-03 19:51:40 -05003810 if (read || (len < (commit - read)) ||
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003811 cpu_buffer->reader_page == cpu_buffer->commit_page) {
Lai Jiangshan667d2412009-02-09 14:21:17 +08003812 struct buffer_data_page *rpage = cpu_buffer->reader_page->page;
Steven Rostedt474d32b2009-03-03 19:51:40 -05003813 unsigned int rpos = read;
3814 unsigned int pos = 0;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003815 unsigned int size;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003816
3817 if (full)
Steven Rostedt554f7862009-03-11 22:00:13 -04003818 goto out_unlock;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003819
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003820 if (len > (commit - read))
3821 len = (commit - read);
3822
3823 size = rb_event_length(event);
3824
3825 if (len < size)
Steven Rostedt554f7862009-03-11 22:00:13 -04003826 goto out_unlock;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003827
Steven Rostedt4f3640f2009-03-03 23:52:42 -05003828 /* save the current timestamp, since the user will need it */
3829 save_timestamp = cpu_buffer->read_stamp;
3830
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003831 /* Need to copy one event at a time */
3832 do {
Steven Rostedt474d32b2009-03-03 19:51:40 -05003833 memcpy(bpage->data + pos, rpage->data + rpos, size);
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003834
3835 len -= size;
3836
3837 rb_advance_reader(cpu_buffer);
Steven Rostedt474d32b2009-03-03 19:51:40 -05003838 rpos = reader->read;
3839 pos += size;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003840
3841 event = rb_reader_event(cpu_buffer);
3842 size = rb_event_length(event);
3843 } while (len > size);
Lai Jiangshan667d2412009-02-09 14:21:17 +08003844
3845 /* update bpage */
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003846 local_set(&bpage->commit, pos);
Steven Rostedt4f3640f2009-03-03 23:52:42 -05003847 bpage->time_stamp = save_timestamp;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003848
Steven Rostedt474d32b2009-03-03 19:51:40 -05003849 /* we copied everything to the beginning */
3850 read = 0;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003851 } else {
Steven Rostedtafbab762009-05-01 19:40:05 -04003852 /* update the entry counter */
Steven Rostedt77ae3652009-03-27 11:00:29 -04003853 cpu_buffer->read += rb_page_entries(reader);
Steven Rostedtafbab762009-05-01 19:40:05 -04003854
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003855 /* swap the pages */
Steven Rostedt044fa782008-12-02 23:50:03 -05003856 rb_init_page(bpage);
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003857 bpage = reader->page;
3858 reader->page = *data_page;
3859 local_set(&reader->write, 0);
Steven Rostedt778c55d2009-05-01 18:44:45 -04003860 local_set(&reader->entries, 0);
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003861 reader->read = 0;
Steven Rostedt044fa782008-12-02 23:50:03 -05003862 *data_page = bpage;
Steven Rostedtff0ff842010-03-31 22:11:42 -04003863
3864 /*
3865 * Use the real_end for the data size,
3866 * This gives us a chance to store the lost events
3867 * on the page.
3868 */
3869 if (reader->real_end)
3870 local_set(&bpage->commit, reader->real_end);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003871 }
Lai Jiangshan667d2412009-02-09 14:21:17 +08003872 ret = read;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003873
Steven Rostedt66a8cb92010-03-31 13:21:56 -04003874 cpu_buffer->lost_events = 0;
3875 /*
3876 * Set a flag in the commit field if we lost events
3877 */
Steven Rostedtff0ff842010-03-31 22:11:42 -04003878 if (missed_events) {
3879 commit = local_read(&bpage->commit);
3880
3881 /* If there is room at the end of the page to save the
3882 * missed events, then record it there.
3883 */
3884 if (BUF_PAGE_SIZE - commit >= sizeof(missed_events)) {
3885 memcpy(&bpage->data[commit], &missed_events,
3886 sizeof(missed_events));
3887 local_add(RB_MISSED_STORED, &bpage->commit);
3888 }
Steven Rostedt66a8cb92010-03-31 13:21:56 -04003889 local_add(RB_MISSED_EVENTS, &bpage->commit);
Steven Rostedtff0ff842010-03-31 22:11:42 -04003890 }
Steven Rostedt66a8cb92010-03-31 13:21:56 -04003891
Steven Rostedt554f7862009-03-11 22:00:13 -04003892 out_unlock:
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003893 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
3894
Steven Rostedt554f7862009-03-11 22:00:13 -04003895 out:
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003896 return ret;
3897}
Steven Rostedtd6ce96d2009-05-05 01:15:24 -04003898EXPORT_SYMBOL_GPL(ring_buffer_read_page);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003899
Paul Mundt1155de42009-06-25 14:30:12 +09003900#ifdef CONFIG_TRACING
Steven Rostedta3583242008-11-11 15:01:42 -05003901static ssize_t
3902rb_simple_read(struct file *filp, char __user *ubuf,
3903 size_t cnt, loff_t *ppos)
3904{
Hannes Eder5e398412009-02-10 19:44:34 +01003905 unsigned long *p = filp->private_data;
Steven Rostedta3583242008-11-11 15:01:42 -05003906 char buf[64];
3907 int r;
3908
Steven Rostedt033601a2008-11-21 12:41:55 -05003909 if (test_bit(RB_BUFFERS_DISABLED_BIT, p))
3910 r = sprintf(buf, "permanently disabled\n");
3911 else
3912 r = sprintf(buf, "%d\n", test_bit(RB_BUFFERS_ON_BIT, p));
Steven Rostedta3583242008-11-11 15:01:42 -05003913
3914 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
3915}
3916
3917static ssize_t
3918rb_simple_write(struct file *filp, const char __user *ubuf,
3919 size_t cnt, loff_t *ppos)
3920{
Hannes Eder5e398412009-02-10 19:44:34 +01003921 unsigned long *p = filp->private_data;
Steven Rostedta3583242008-11-11 15:01:42 -05003922 char buf[64];
Hannes Eder5e398412009-02-10 19:44:34 +01003923 unsigned long val;
Steven Rostedta3583242008-11-11 15:01:42 -05003924 int ret;
3925
3926 if (cnt >= sizeof(buf))
3927 return -EINVAL;
3928
3929 if (copy_from_user(&buf, ubuf, cnt))
3930 return -EFAULT;
3931
3932 buf[cnt] = 0;
3933
3934 ret = strict_strtoul(buf, 10, &val);
3935 if (ret < 0)
3936 return ret;
3937
Steven Rostedt033601a2008-11-21 12:41:55 -05003938 if (val)
3939 set_bit(RB_BUFFERS_ON_BIT, p);
3940 else
3941 clear_bit(RB_BUFFERS_ON_BIT, p);
Steven Rostedta3583242008-11-11 15:01:42 -05003942
3943 (*ppos)++;
3944
3945 return cnt;
3946}
3947
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003948static const struct file_operations rb_simple_fops = {
Steven Rostedta3583242008-11-11 15:01:42 -05003949 .open = tracing_open_generic,
3950 .read = rb_simple_read,
3951 .write = rb_simple_write,
3952};
3953
3954
3955static __init int rb_init_debugfs(void)
3956{
3957 struct dentry *d_tracer;
Steven Rostedta3583242008-11-11 15:01:42 -05003958
3959 d_tracer = tracing_init_dentry();
3960
Frederic Weisbecker5452af62009-03-27 00:25:38 +01003961 trace_create_file("tracing_on", 0644, d_tracer,
3962 &ring_buffer_flags, &rb_simple_fops);
Steven Rostedta3583242008-11-11 15:01:42 -05003963
3964 return 0;
3965}
3966
3967fs_initcall(rb_init_debugfs);
Paul Mundt1155de42009-06-25 14:30:12 +09003968#endif
Steven Rostedt554f7862009-03-11 22:00:13 -04003969
Steven Rostedt59222ef2009-03-12 11:46:03 -04003970#ifdef CONFIG_HOTPLUG_CPU
Frederic Weisbecker09c9e842009-03-21 04:33:36 +01003971static int rb_cpu_notify(struct notifier_block *self,
3972 unsigned long action, void *hcpu)
Steven Rostedt554f7862009-03-11 22:00:13 -04003973{
3974 struct ring_buffer *buffer =
3975 container_of(self, struct ring_buffer, cpu_notify);
3976 long cpu = (long)hcpu;
3977
3978 switch (action) {
3979 case CPU_UP_PREPARE:
3980 case CPU_UP_PREPARE_FROZEN:
Rusty Russell3f237a72009-06-12 21:15:30 +09303981 if (cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt554f7862009-03-11 22:00:13 -04003982 return NOTIFY_OK;
3983
3984 buffer->buffers[cpu] =
3985 rb_allocate_cpu_buffer(buffer, cpu);
3986 if (!buffer->buffers[cpu]) {
3987 WARN(1, "failed to allocate ring buffer on CPU %ld\n",
3988 cpu);
3989 return NOTIFY_OK;
3990 }
3991 smp_wmb();
Rusty Russell3f237a72009-06-12 21:15:30 +09303992 cpumask_set_cpu(cpu, buffer->cpumask);
Steven Rostedt554f7862009-03-11 22:00:13 -04003993 break;
3994 case CPU_DOWN_PREPARE:
3995 case CPU_DOWN_PREPARE_FROZEN:
3996 /*
3997 * Do nothing.
3998 * If we were to free the buffer, then the user would
3999 * lose any trace that was in the buffer.
4000 */
4001 break;
4002 default:
4003 break;
4004 }
4005 return NOTIFY_OK;
4006}
4007#endif