blob: dc6d563a6d22d95a7f6da91eedfdb152201e9e2a [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>
Steven Rostedt7a8e76a2008-09-29 23:02:38 -040017#include <linux/init.h>
18#include <linux/hash.h>
19#include <linux/list.h>
Steven Rostedt554f7862009-03-11 22:00:13 -040020#include <linux/cpu.h>
Steven Rostedt7a8e76a2008-09-29 23:02:38 -040021#include <linux/fs.h>
22
Christoph Lameter79615762010-01-05 15:34:50 +090023#include <asm/local.h>
Steven Rostedt182e9f52008-11-03 23:15:56 -050024#include "trace.h"
25
Steven Rostedt033601a2008-11-21 12:41:55 -050026/*
Steven Rostedtd1b182a2009-04-15 16:53:47 -040027 * The ring buffer header is special. We must manually up keep it.
28 */
29int ring_buffer_print_entry_header(struct trace_seq *s)
30{
31 int ret;
32
Lai Jiangshan334d4162009-04-24 11:27:05 +080033 ret = trace_seq_printf(s, "# compressed entry header\n");
34 ret = trace_seq_printf(s, "\ttype_len : 5 bits\n");
Steven Rostedtd1b182a2009-04-15 16:53:47 -040035 ret = trace_seq_printf(s, "\ttime_delta : 27 bits\n");
36 ret = trace_seq_printf(s, "\tarray : 32 bits\n");
37 ret = trace_seq_printf(s, "\n");
38 ret = trace_seq_printf(s, "\tpadding : type == %d\n",
39 RINGBUF_TYPE_PADDING);
40 ret = trace_seq_printf(s, "\ttime_extend : type == %d\n",
41 RINGBUF_TYPE_TIME_EXTEND);
Lai Jiangshan334d4162009-04-24 11:27:05 +080042 ret = trace_seq_printf(s, "\tdata max type_len == %d\n",
43 RINGBUF_TYPE_DATA_TYPE_LEN_MAX);
Steven Rostedtd1b182a2009-04-15 16:53:47 -040044
45 return ret;
46}
47
48/*
Steven Rostedt5cc98542009-03-12 22:24:17 -040049 * The ring buffer is made up of a list of pages. A separate list of pages is
50 * allocated for each CPU. A writer may only write to a buffer that is
51 * associated with the CPU it is currently executing on. A reader may read
52 * from any per cpu buffer.
53 *
54 * The reader is special. For each per cpu buffer, the reader has its own
55 * reader page. When a reader has read the entire reader page, this reader
56 * page is swapped with another page in the ring buffer.
57 *
58 * Now, as long as the writer is off the reader page, the reader can do what
59 * ever it wants with that page. The writer will never write to that page
60 * again (as long as it is out of the ring buffer).
61 *
62 * Here's some silly ASCII art.
63 *
64 * +------+
65 * |reader| RING BUFFER
66 * |page |
67 * +------+ +---+ +---+ +---+
68 * | |-->| |-->| |
69 * +---+ +---+ +---+
70 * ^ |
71 * | |
72 * +---------------+
73 *
74 *
75 * +------+
76 * |reader| RING BUFFER
77 * |page |------------------v
78 * +------+ +---+ +---+ +---+
79 * | |-->| |-->| |
80 * +---+ +---+ +---+
81 * ^ |
82 * | |
83 * +---------------+
84 *
85 *
86 * +------+
87 * |reader| RING BUFFER
88 * |page |------------------v
89 * +------+ +---+ +---+ +---+
90 * ^ | |-->| |-->| |
91 * | +---+ +---+ +---+
92 * | |
93 * | |
94 * +------------------------------+
95 *
96 *
97 * +------+
98 * |buffer| RING BUFFER
99 * |page |------------------v
100 * +------+ +---+ +---+ +---+
101 * ^ | | | |-->| |
102 * | New +---+ +---+ +---+
103 * | Reader------^ |
104 * | page |
105 * +------------------------------+
106 *
107 *
108 * After we make this swap, the reader can hand this page off to the splice
109 * code and be done with it. It can even allocate a new page if it needs to
110 * and swap that into the ring buffer.
111 *
112 * We will be using cmpxchg soon to make all this lockless.
113 *
114 */
115
116/*
Steven Rostedt033601a2008-11-21 12:41:55 -0500117 * A fast way to enable or disable all ring buffers is to
118 * call tracing_on or tracing_off. Turning off the ring buffers
119 * prevents all ring buffers from being recorded to.
120 * Turning this switch on, makes it OK to write to the
121 * ring buffer, if the ring buffer is enabled itself.
122 *
123 * There's three layers that must be on in order to write
124 * to the ring buffer.
125 *
126 * 1) This global flag must be set.
127 * 2) The ring buffer must be enabled for recording.
128 * 3) The per cpu buffer must be enabled for recording.
129 *
130 * In case of an anomaly, this global flag has a bit set that
131 * will permantly disable all ring buffers.
132 */
133
134/*
135 * Global flag to disable all recording to ring buffers
136 * This has two bits: ON, DISABLED
137 *
138 * ON DISABLED
139 * ---- ----------
140 * 0 0 : ring buffers are off
141 * 1 0 : ring buffers are on
142 * X 1 : ring buffers are permanently disabled
143 */
144
145enum {
146 RB_BUFFERS_ON_BIT = 0,
147 RB_BUFFERS_DISABLED_BIT = 1,
148};
149
150enum {
151 RB_BUFFERS_ON = 1 << RB_BUFFERS_ON_BIT,
152 RB_BUFFERS_DISABLED = 1 << RB_BUFFERS_DISABLED_BIT,
153};
154
Hannes Eder5e398412009-02-10 19:44:34 +0100155static unsigned long ring_buffer_flags __read_mostly = RB_BUFFERS_ON;
Steven Rostedta3583242008-11-11 15:01:42 -0500156
Steven Rostedt474d32b2009-03-03 19:51:40 -0500157#define BUF_PAGE_HDR_SIZE offsetof(struct buffer_data_page, data)
158
Steven Rostedta3583242008-11-11 15:01:42 -0500159/**
160 * tracing_on - enable all tracing buffers
161 *
162 * This function enables all tracing buffers that may have been
163 * disabled with tracing_off.
164 */
165void tracing_on(void)
166{
Steven Rostedt033601a2008-11-21 12:41:55 -0500167 set_bit(RB_BUFFERS_ON_BIT, &ring_buffer_flags);
Steven Rostedta3583242008-11-11 15:01:42 -0500168}
Robert Richterc4f50182008-12-11 16:49:22 +0100169EXPORT_SYMBOL_GPL(tracing_on);
Steven Rostedta3583242008-11-11 15:01:42 -0500170
171/**
172 * tracing_off - turn off all tracing buffers
173 *
174 * This function stops all tracing buffers from recording data.
175 * It does not disable any overhead the tracers themselves may
176 * be causing. This function simply causes all recording to
177 * the ring buffers to fail.
178 */
179void tracing_off(void)
180{
Steven Rostedt033601a2008-11-21 12:41:55 -0500181 clear_bit(RB_BUFFERS_ON_BIT, &ring_buffer_flags);
182}
Robert Richterc4f50182008-12-11 16:49:22 +0100183EXPORT_SYMBOL_GPL(tracing_off);
Steven Rostedt033601a2008-11-21 12:41:55 -0500184
185/**
186 * tracing_off_permanent - permanently disable ring buffers
187 *
188 * This function, once called, will disable all ring buffers
Wenji Huangc3706f02009-02-10 01:03:18 -0500189 * permanently.
Steven Rostedt033601a2008-11-21 12:41:55 -0500190 */
191void tracing_off_permanent(void)
192{
193 set_bit(RB_BUFFERS_DISABLED_BIT, &ring_buffer_flags);
Steven Rostedta3583242008-11-11 15:01:42 -0500194}
195
Steven Rostedt988ae9d2009-02-14 19:17:02 -0500196/**
197 * tracing_is_on - show state of ring buffers enabled
198 */
199int tracing_is_on(void)
200{
201 return ring_buffer_flags == RB_BUFFERS_ON;
202}
203EXPORT_SYMBOL_GPL(tracing_is_on);
204
Steven Rostedte3d6bf02009-03-03 13:53:07 -0500205#define RB_EVNT_HDR_SIZE (offsetof(struct ring_buffer_event, array))
Andrew Morton67d34722009-01-09 12:27:09 -0800206#define RB_ALIGNMENT 4U
Lai Jiangshan334d4162009-04-24 11:27:05 +0800207#define RB_MAX_SMALL_DATA (RB_ALIGNMENT * RINGBUF_TYPE_DATA_TYPE_LEN_MAX)
Steven Rostedtc7b09302009-06-11 11:12:00 -0400208#define RB_EVNT_MIN_SIZE 8U /* two 32bit words */
Lai Jiangshan334d4162009-04-24 11:27:05 +0800209
Steven Rostedt22710482010-03-18 17:54:19 -0400210#if !defined(CONFIG_64BIT) || defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)
211# define RB_FORCE_8BYTE_ALIGNMENT 0
212# define RB_ARCH_ALIGNMENT RB_ALIGNMENT
213#else
214# define RB_FORCE_8BYTE_ALIGNMENT 1
215# define RB_ARCH_ALIGNMENT 8U
216#endif
217
Lai Jiangshan334d4162009-04-24 11:27:05 +0800218/* define RINGBUF_TYPE_DATA for 'case RINGBUF_TYPE_DATA:' */
219#define RINGBUF_TYPE_DATA 0 ... RINGBUF_TYPE_DATA_TYPE_LEN_MAX
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400220
221enum {
222 RB_LEN_TIME_EXTEND = 8,
223 RB_LEN_TIME_STAMP = 16,
224};
225
Tom Zanussi2d622712009-03-22 03:30:49 -0500226static inline int rb_null_event(struct ring_buffer_event *event)
227{
Steven Rostedta1863c22009-09-03 10:23:58 -0400228 return event->type_len == RINGBUF_TYPE_PADDING && !event->time_delta;
Tom Zanussi2d622712009-03-22 03:30:49 -0500229}
230
231static void rb_event_set_padding(struct ring_buffer_event *event)
232{
Steven Rostedta1863c22009-09-03 10:23:58 -0400233 /* padding has a NULL time_delta */
Lai Jiangshan334d4162009-04-24 11:27:05 +0800234 event->type_len = RINGBUF_TYPE_PADDING;
Tom Zanussi2d622712009-03-22 03:30:49 -0500235 event->time_delta = 0;
236}
237
Tom Zanussi2d622712009-03-22 03:30:49 -0500238static unsigned
239rb_event_data_length(struct ring_buffer_event *event)
240{
241 unsigned length;
242
Lai Jiangshan334d4162009-04-24 11:27:05 +0800243 if (event->type_len)
244 length = event->type_len * RB_ALIGNMENT;
Tom Zanussi2d622712009-03-22 03:30:49 -0500245 else
246 length = event->array[0];
247 return length + RB_EVNT_HDR_SIZE;
248}
249
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400250/* inline for ring buffer fast paths */
Andrew Morton34a148b2009-01-09 12:27:09 -0800251static unsigned
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400252rb_event_length(struct ring_buffer_event *event)
253{
Lai Jiangshan334d4162009-04-24 11:27:05 +0800254 switch (event->type_len) {
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400255 case RINGBUF_TYPE_PADDING:
Tom Zanussi2d622712009-03-22 03:30:49 -0500256 if (rb_null_event(event))
257 /* undefined */
258 return -1;
Lai Jiangshan334d4162009-04-24 11:27:05 +0800259 return event->array[0] + RB_EVNT_HDR_SIZE;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400260
261 case RINGBUF_TYPE_TIME_EXTEND:
262 return RB_LEN_TIME_EXTEND;
263
264 case RINGBUF_TYPE_TIME_STAMP:
265 return RB_LEN_TIME_STAMP;
266
267 case RINGBUF_TYPE_DATA:
Tom Zanussi2d622712009-03-22 03:30:49 -0500268 return rb_event_data_length(event);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400269 default:
270 BUG();
271 }
272 /* not hit */
273 return 0;
274}
275
276/**
277 * ring_buffer_event_length - return the length of the event
278 * @event: the event to get the length of
279 */
280unsigned ring_buffer_event_length(struct ring_buffer_event *event)
281{
Robert Richter465634a2009-01-07 15:32:11 +0100282 unsigned length = rb_event_length(event);
Lai Jiangshan334d4162009-04-24 11:27:05 +0800283 if (event->type_len > RINGBUF_TYPE_DATA_TYPE_LEN_MAX)
Robert Richter465634a2009-01-07 15:32:11 +0100284 return length;
285 length -= RB_EVNT_HDR_SIZE;
286 if (length > RB_MAX_SMALL_DATA + sizeof(event->array[0]))
287 length -= sizeof(event->array[0]);
288 return length;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400289}
Robert Richterc4f50182008-12-11 16:49:22 +0100290EXPORT_SYMBOL_GPL(ring_buffer_event_length);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400291
292/* inline for ring buffer fast paths */
Andrew Morton34a148b2009-01-09 12:27:09 -0800293static void *
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400294rb_event_data(struct ring_buffer_event *event)
295{
Lai Jiangshan334d4162009-04-24 11:27:05 +0800296 BUG_ON(event->type_len > RINGBUF_TYPE_DATA_TYPE_LEN_MAX);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400297 /* If length is in len field, then array[0] has the data */
Lai Jiangshan334d4162009-04-24 11:27:05 +0800298 if (event->type_len)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400299 return (void *)&event->array[0];
300 /* Otherwise length is in array[0] and array[1] has the data */
301 return (void *)&event->array[1];
302}
303
304/**
305 * ring_buffer_event_data - return the data of the event
306 * @event: the event to get the data from
307 */
308void *ring_buffer_event_data(struct ring_buffer_event *event)
309{
310 return rb_event_data(event);
311}
Robert Richterc4f50182008-12-11 16:49:22 +0100312EXPORT_SYMBOL_GPL(ring_buffer_event_data);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400313
314#define for_each_buffer_cpu(buffer, cpu) \
Rusty Russell9e01c1b2009-01-01 10:12:22 +1030315 for_each_cpu(cpu, buffer->cpumask)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400316
317#define TS_SHIFT 27
318#define TS_MASK ((1ULL << TS_SHIFT) - 1)
319#define TS_DELTA_TEST (~TS_MASK)
320
Steven Rostedt66a8cb92010-03-31 13:21:56 -0400321/* Flag when events were overwritten */
322#define RB_MISSED_EVENTS (1 << 31)
Steven Rostedtff0ff842010-03-31 22:11:42 -0400323/* Missed count stored at end */
324#define RB_MISSED_STORED (1 << 30)
Steven Rostedt66a8cb92010-03-31 13:21:56 -0400325
Steven Rostedtabc9b562008-12-02 15:34:06 -0500326struct buffer_data_page {
Steven Rostedte4c2ce82008-10-01 11:14:54 -0400327 u64 time_stamp; /* page time stamp */
Wenji Huangc3706f02009-02-10 01:03:18 -0500328 local_t commit; /* write committed index */
Steven Rostedtabc9b562008-12-02 15:34:06 -0500329 unsigned char data[]; /* data of buffer page */
330};
331
Steven Rostedt77ae3652009-03-27 11:00:29 -0400332/*
333 * Note, the buffer_page list must be first. The buffer pages
334 * are allocated in cache lines, which means that each buffer
335 * page will be at the beginning of a cache line, and thus
336 * the least significant bits will be zero. We use this to
337 * add flags in the list struct pointers, to make the ring buffer
338 * lockless.
339 */
Steven Rostedtabc9b562008-12-02 15:34:06 -0500340struct buffer_page {
Steven Rostedt778c55d2009-05-01 18:44:45 -0400341 struct list_head list; /* list of buffer pages */
Steven Rostedtabc9b562008-12-02 15:34:06 -0500342 local_t write; /* index for next write */
Steven Rostedt6f807ac2008-10-04 02:00:58 -0400343 unsigned read; /* index for next read */
Steven Rostedt778c55d2009-05-01 18:44:45 -0400344 local_t entries; /* entries on this page */
Steven Rostedtff0ff842010-03-31 22:11:42 -0400345 unsigned long real_end; /* real end of data */
Steven Rostedtabc9b562008-12-02 15:34:06 -0500346 struct buffer_data_page *page; /* Actual data page */
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400347};
348
Steven Rostedt77ae3652009-03-27 11:00:29 -0400349/*
350 * The buffer page counters, write and entries, must be reset
351 * atomically when crossing page boundaries. To synchronize this
352 * update, two counters are inserted into the number. One is
353 * the actual counter for the write position or count on the page.
354 *
355 * The other is a counter of updaters. Before an update happens
356 * the update partition of the counter is incremented. This will
357 * allow the updater to update the counter atomically.
358 *
359 * The counter is 20 bits, and the state data is 12.
360 */
361#define RB_WRITE_MASK 0xfffff
362#define RB_WRITE_INTCNT (1 << 20)
363
Steven Rostedt044fa782008-12-02 23:50:03 -0500364static void rb_init_page(struct buffer_data_page *bpage)
Steven Rostedtabc9b562008-12-02 15:34:06 -0500365{
Steven Rostedt044fa782008-12-02 23:50:03 -0500366 local_set(&bpage->commit, 0);
Steven Rostedtabc9b562008-12-02 15:34:06 -0500367}
368
Steven Rostedt474d32b2009-03-03 19:51:40 -0500369/**
370 * ring_buffer_page_len - the size of data on the page.
371 * @page: The page to read
372 *
373 * Returns the amount of data on the page, including buffer page header.
374 */
Steven Rostedtef7a4a12009-03-03 00:27:49 -0500375size_t ring_buffer_page_len(void *page)
376{
Steven Rostedt474d32b2009-03-03 19:51:40 -0500377 return local_read(&((struct buffer_data_page *)page)->commit)
378 + BUF_PAGE_HDR_SIZE;
Steven Rostedtef7a4a12009-03-03 00:27:49 -0500379}
380
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400381/*
Steven Rostedted568292008-09-29 23:02:40 -0400382 * Also stolen from mm/slob.c. Thanks to Mathieu Desnoyers for pointing
383 * this issue out.
384 */
Andrew Morton34a148b2009-01-09 12:27:09 -0800385static void free_buffer_page(struct buffer_page *bpage)
Steven Rostedted568292008-09-29 23:02:40 -0400386{
Andrew Morton34a148b2009-01-09 12:27:09 -0800387 free_page((unsigned long)bpage->page);
Steven Rostedte4c2ce82008-10-01 11:14:54 -0400388 kfree(bpage);
Steven Rostedted568292008-09-29 23:02:40 -0400389}
390
391/*
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400392 * We need to fit the time_stamp delta into 27 bits.
393 */
394static inline int test_time_stamp(u64 delta)
395{
396 if (delta & TS_DELTA_TEST)
397 return 1;
398 return 0;
399}
400
Steven Rostedt474d32b2009-03-03 19:51:40 -0500401#define BUF_PAGE_SIZE (PAGE_SIZE - BUF_PAGE_HDR_SIZE)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400402
Steven Rostedtbe957c42009-05-11 14:42:53 -0400403/* Max payload is BUF_PAGE_SIZE - header (8bytes) */
404#define BUF_MAX_DATA_SIZE (BUF_PAGE_SIZE - (sizeof(u32) * 2))
405
Steven Rostedtea05b572009-06-03 09:30:10 -0400406/* Max number of timestamps that can fit on a page */
407#define RB_TIMESTAMPS_PER_PAGE (BUF_PAGE_SIZE / RB_LEN_TIME_STAMP)
408
Steven Rostedtd1b182a2009-04-15 16:53:47 -0400409int ring_buffer_print_page_header(struct trace_seq *s)
410{
411 struct buffer_data_page field;
412 int ret;
413
414 ret = trace_seq_printf(s, "\tfield: u64 timestamp;\t"
Tom Zanussi26a50742009-10-06 01:09:50 -0500415 "offset:0;\tsize:%u;\tsigned:%u;\n",
416 (unsigned int)sizeof(field.time_stamp),
417 (unsigned int)is_signed_type(u64));
Steven Rostedtd1b182a2009-04-15 16:53:47 -0400418
419 ret = trace_seq_printf(s, "\tfield: local_t commit;\t"
Tom Zanussi26a50742009-10-06 01:09:50 -0500420 "offset:%u;\tsize:%u;\tsigned:%u;\n",
Steven Rostedtd1b182a2009-04-15 16:53:47 -0400421 (unsigned int)offsetof(typeof(field), commit),
Tom Zanussi26a50742009-10-06 01:09:50 -0500422 (unsigned int)sizeof(field.commit),
423 (unsigned int)is_signed_type(long));
Steven Rostedtd1b182a2009-04-15 16:53:47 -0400424
Steven Rostedt66a8cb92010-03-31 13:21:56 -0400425 ret = trace_seq_printf(s, "\tfield: int overwrite;\t"
426 "offset:%u;\tsize:%u;\tsigned:%u;\n",
427 (unsigned int)offsetof(typeof(field), commit),
428 1,
429 (unsigned int)is_signed_type(long));
430
Steven Rostedtd1b182a2009-04-15 16:53:47 -0400431 ret = trace_seq_printf(s, "\tfield: char data;\t"
Tom Zanussi26a50742009-10-06 01:09:50 -0500432 "offset:%u;\tsize:%u;\tsigned:%u;\n",
Steven Rostedtd1b182a2009-04-15 16:53:47 -0400433 (unsigned int)offsetof(typeof(field), data),
Tom Zanussi26a50742009-10-06 01:09:50 -0500434 (unsigned int)BUF_PAGE_SIZE,
435 (unsigned int)is_signed_type(char));
Steven Rostedtd1b182a2009-04-15 16:53:47 -0400436
437 return ret;
438}
439
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400440/*
441 * head_page == tail_page && head == tail then buffer is empty.
442 */
443struct ring_buffer_per_cpu {
444 int cpu;
445 struct ring_buffer *buffer;
Steven Rostedt77ae3652009-03-27 11:00:29 -0400446 spinlock_t reader_lock; /* serialize readers */
Thomas Gleixner445c8952009-12-02 19:49:50 +0100447 arch_spinlock_t lock;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400448 struct lock_class_key lock_key;
Steven Rostedt3adc54f2009-03-30 15:32:01 -0400449 struct list_head *pages;
Steven Rostedt6f807ac2008-10-04 02:00:58 -0400450 struct buffer_page *head_page; /* read from head */
451 struct buffer_page *tail_page; /* write to tail */
Wenji Huangc3706f02009-02-10 01:03:18 -0500452 struct buffer_page *commit_page; /* committed pages */
Steven Rostedtd7690412008-10-01 00:29:53 -0400453 struct buffer_page *reader_page;
Steven Rostedt66a8cb92010-03-31 13:21:56 -0400454 unsigned long lost_events;
455 unsigned long last_overrun;
Steven Rostedt77ae3652009-03-27 11:00:29 -0400456 local_t commit_overrun;
457 local_t overrun;
Steven Rostedte4906ef2009-04-30 20:49:44 -0400458 local_t entries;
Steven Rostedtfa743952009-06-16 12:37:57 -0400459 local_t committing;
460 local_t commits;
Steven Rostedt77ae3652009-03-27 11:00:29 -0400461 unsigned long read;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400462 u64 write_stamp;
463 u64 read_stamp;
464 atomic_t record_disabled;
465};
466
467struct ring_buffer {
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400468 unsigned pages;
469 unsigned flags;
470 int cpus;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400471 atomic_t record_disabled;
Arnaldo Carvalho de Melo00f62f62009-02-09 17:04:06 -0200472 cpumask_var_t cpumask;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400473
Peter Zijlstra1f8a6a12009-06-08 18:18:39 +0200474 struct lock_class_key *reader_lock_key;
475
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400476 struct mutex mutex;
477
478 struct ring_buffer_per_cpu **buffers;
Steven Rostedt554f7862009-03-11 22:00:13 -0400479
Steven Rostedt59222ef2009-03-12 11:46:03 -0400480#ifdef CONFIG_HOTPLUG_CPU
Steven Rostedt554f7862009-03-11 22:00:13 -0400481 struct notifier_block cpu_notify;
482#endif
Steven Rostedt37886f62009-03-17 17:22:06 -0400483 u64 (*clock)(void);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400484};
485
486struct ring_buffer_iter {
487 struct ring_buffer_per_cpu *cpu_buffer;
488 unsigned long head;
489 struct buffer_page *head_page;
Steven Rostedt492a74f2010-01-25 15:17:47 -0500490 struct buffer_page *cache_reader_page;
491 unsigned long cache_read;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400492 u64 read_stamp;
493};
494
Steven Rostedtf536aaf2008-11-10 23:07:30 -0500495/* buffer may be either ring_buffer or ring_buffer_per_cpu */
Steven Rostedt077c5402009-09-03 19:53:46 -0400496#define RB_WARN_ON(b, cond) \
497 ({ \
498 int _____ret = unlikely(cond); \
499 if (_____ret) { \
500 if (__same_type(*(b), struct ring_buffer_per_cpu)) { \
501 struct ring_buffer_per_cpu *__b = \
502 (void *)b; \
503 atomic_inc(&__b->buffer->record_disabled); \
504 } else \
505 atomic_inc(&b->record_disabled); \
506 WARN_ON(1); \
507 } \
508 _____ret; \
Steven Rostedt3e89c7b2008-11-11 15:28:41 -0500509 })
Steven Rostedtf536aaf2008-11-10 23:07:30 -0500510
Steven Rostedt37886f62009-03-17 17:22:06 -0400511/* Up this if you want to test the TIME_EXTENTS and normalization */
512#define DEBUG_SHIFT 0
513
Jiri Olsa6d3f1e12009-10-23 19:36:19 -0400514static inline u64 rb_time_stamp(struct ring_buffer *buffer)
Steven Rostedt88eb0122009-05-11 16:28:23 -0400515{
516 /* shift to debug/test normalization and TIME_EXTENTS */
517 return buffer->clock() << DEBUG_SHIFT;
518}
519
Steven Rostedt37886f62009-03-17 17:22:06 -0400520u64 ring_buffer_time_stamp(struct ring_buffer *buffer, int cpu)
521{
522 u64 time;
523
524 preempt_disable_notrace();
Jiri Olsa6d3f1e12009-10-23 19:36:19 -0400525 time = rb_time_stamp(buffer);
Steven Rostedt37886f62009-03-17 17:22:06 -0400526 preempt_enable_no_resched_notrace();
527
528 return time;
529}
530EXPORT_SYMBOL_GPL(ring_buffer_time_stamp);
531
532void ring_buffer_normalize_time_stamp(struct ring_buffer *buffer,
533 int cpu, u64 *ts)
534{
535 /* Just stupid testing the normalize function and deltas */
536 *ts >>= DEBUG_SHIFT;
537}
538EXPORT_SYMBOL_GPL(ring_buffer_normalize_time_stamp);
539
Steven Rostedt77ae3652009-03-27 11:00:29 -0400540/*
541 * Making the ring buffer lockless makes things tricky.
542 * Although writes only happen on the CPU that they are on,
543 * and they only need to worry about interrupts. Reads can
544 * happen on any CPU.
545 *
546 * The reader page is always off the ring buffer, but when the
547 * reader finishes with a page, it needs to swap its page with
548 * a new one from the buffer. The reader needs to take from
549 * the head (writes go to the tail). But if a writer is in overwrite
550 * mode and wraps, it must push the head page forward.
551 *
552 * Here lies the problem.
553 *
554 * The reader must be careful to replace only the head page, and
555 * not another one. As described at the top of the file in the
556 * ASCII art, the reader sets its old page to point to the next
557 * page after head. It then sets the page after head to point to
558 * the old reader page. But if the writer moves the head page
559 * during this operation, the reader could end up with the tail.
560 *
561 * We use cmpxchg to help prevent this race. We also do something
562 * special with the page before head. We set the LSB to 1.
563 *
564 * When the writer must push the page forward, it will clear the
565 * bit that points to the head page, move the head, and then set
566 * the bit that points to the new head page.
567 *
568 * We also don't want an interrupt coming in and moving the head
569 * page on another writer. Thus we use the second LSB to catch
570 * that too. Thus:
571 *
572 * head->list->prev->next bit 1 bit 0
573 * ------- -------
574 * Normal page 0 0
575 * Points to head page 0 1
576 * New head page 1 0
577 *
578 * Note we can not trust the prev pointer of the head page, because:
579 *
580 * +----+ +-----+ +-----+
581 * | |------>| T |---X--->| N |
582 * | |<------| | | |
583 * +----+ +-----+ +-----+
584 * ^ ^ |
585 * | +-----+ | |
586 * +----------| R |----------+ |
587 * | |<-----------+
588 * +-----+
589 *
590 * Key: ---X--> HEAD flag set in pointer
591 * T Tail page
592 * R Reader page
593 * N Next page
594 *
595 * (see __rb_reserve_next() to see where this happens)
596 *
597 * What the above shows is that the reader just swapped out
598 * the reader page with a page in the buffer, but before it
599 * could make the new header point back to the new page added
600 * it was preempted by a writer. The writer moved forward onto
601 * the new page added by the reader and is about to move forward
602 * again.
603 *
604 * You can see, it is legitimate for the previous pointer of
605 * the head (or any page) not to point back to itself. But only
606 * temporarially.
607 */
608
609#define RB_PAGE_NORMAL 0UL
610#define RB_PAGE_HEAD 1UL
611#define RB_PAGE_UPDATE 2UL
612
613
614#define RB_FLAG_MASK 3UL
615
616/* PAGE_MOVED is not part of the mask */
617#define RB_PAGE_MOVED 4UL
618
619/*
620 * rb_list_head - remove any bit
621 */
622static struct list_head *rb_list_head(struct list_head *list)
623{
624 unsigned long val = (unsigned long)list;
625
626 return (struct list_head *)(val & ~RB_FLAG_MASK);
627}
628
629/*
Jiri Olsa6d3f1e12009-10-23 19:36:19 -0400630 * rb_is_head_page - test if the given page is the head page
Steven Rostedt77ae3652009-03-27 11:00:29 -0400631 *
632 * Because the reader may move the head_page pointer, we can
633 * not trust what the head page is (it may be pointing to
634 * the reader page). But if the next page is a header page,
635 * its flags will be non zero.
636 */
637static int inline
638rb_is_head_page(struct ring_buffer_per_cpu *cpu_buffer,
639 struct buffer_page *page, struct list_head *list)
640{
641 unsigned long val;
642
643 val = (unsigned long)list->next;
644
645 if ((val & ~RB_FLAG_MASK) != (unsigned long)&page->list)
646 return RB_PAGE_MOVED;
647
648 return val & RB_FLAG_MASK;
649}
650
651/*
652 * rb_is_reader_page
653 *
654 * The unique thing about the reader page, is that, if the
655 * writer is ever on it, the previous pointer never points
656 * back to the reader page.
657 */
658static int rb_is_reader_page(struct buffer_page *page)
659{
660 struct list_head *list = page->list.prev;
661
662 return rb_list_head(list->next) != &page->list;
663}
664
665/*
666 * rb_set_list_to_head - set a list_head to be pointing to head.
667 */
668static void rb_set_list_to_head(struct ring_buffer_per_cpu *cpu_buffer,
669 struct list_head *list)
670{
671 unsigned long *ptr;
672
673 ptr = (unsigned long *)&list->next;
674 *ptr |= RB_PAGE_HEAD;
675 *ptr &= ~RB_PAGE_UPDATE;
676}
677
678/*
679 * rb_head_page_activate - sets up head page
680 */
681static void rb_head_page_activate(struct ring_buffer_per_cpu *cpu_buffer)
682{
683 struct buffer_page *head;
684
685 head = cpu_buffer->head_page;
686 if (!head)
687 return;
688
689 /*
690 * Set the previous list pointer to have the HEAD flag.
691 */
692 rb_set_list_to_head(cpu_buffer, head->list.prev);
693}
694
695static void rb_list_head_clear(struct list_head *list)
696{
697 unsigned long *ptr = (unsigned long *)&list->next;
698
699 *ptr &= ~RB_FLAG_MASK;
700}
701
702/*
703 * rb_head_page_dactivate - clears head page ptr (for free list)
704 */
705static void
706rb_head_page_deactivate(struct ring_buffer_per_cpu *cpu_buffer)
707{
708 struct list_head *hd;
709
710 /* Go through the whole list and clear any pointers found. */
711 rb_list_head_clear(cpu_buffer->pages);
712
713 list_for_each(hd, cpu_buffer->pages)
714 rb_list_head_clear(hd);
715}
716
717static int rb_head_page_set(struct ring_buffer_per_cpu *cpu_buffer,
718 struct buffer_page *head,
719 struct buffer_page *prev,
720 int old_flag, int new_flag)
721{
722 struct list_head *list;
723 unsigned long val = (unsigned long)&head->list;
724 unsigned long ret;
725
726 list = &prev->list;
727
728 val &= ~RB_FLAG_MASK;
729
Steven Rostedt08a40812009-09-14 09:31:35 -0400730 ret = cmpxchg((unsigned long *)&list->next,
731 val | old_flag, val | new_flag);
Steven Rostedt77ae3652009-03-27 11:00:29 -0400732
733 /* check if the reader took the page */
734 if ((ret & ~RB_FLAG_MASK) != val)
735 return RB_PAGE_MOVED;
736
737 return ret & RB_FLAG_MASK;
738}
739
740static int rb_head_page_set_update(struct ring_buffer_per_cpu *cpu_buffer,
741 struct buffer_page *head,
742 struct buffer_page *prev,
743 int old_flag)
744{
745 return rb_head_page_set(cpu_buffer, head, prev,
746 old_flag, RB_PAGE_UPDATE);
747}
748
749static int rb_head_page_set_head(struct ring_buffer_per_cpu *cpu_buffer,
750 struct buffer_page *head,
751 struct buffer_page *prev,
752 int old_flag)
753{
754 return rb_head_page_set(cpu_buffer, head, prev,
755 old_flag, RB_PAGE_HEAD);
756}
757
758static int rb_head_page_set_normal(struct ring_buffer_per_cpu *cpu_buffer,
759 struct buffer_page *head,
760 struct buffer_page *prev,
761 int old_flag)
762{
763 return rb_head_page_set(cpu_buffer, head, prev,
764 old_flag, RB_PAGE_NORMAL);
765}
766
767static inline void rb_inc_page(struct ring_buffer_per_cpu *cpu_buffer,
768 struct buffer_page **bpage)
769{
770 struct list_head *p = rb_list_head((*bpage)->list.next);
771
772 *bpage = list_entry(p, struct buffer_page, list);
773}
774
775static struct buffer_page *
776rb_set_head_page(struct ring_buffer_per_cpu *cpu_buffer)
777{
778 struct buffer_page *head;
779 struct buffer_page *page;
780 struct list_head *list;
781 int i;
782
783 if (RB_WARN_ON(cpu_buffer, !cpu_buffer->head_page))
784 return NULL;
785
786 /* sanity check */
787 list = cpu_buffer->pages;
788 if (RB_WARN_ON(cpu_buffer, rb_list_head(list->prev->next) != list))
789 return NULL;
790
791 page = head = cpu_buffer->head_page;
792 /*
793 * It is possible that the writer moves the header behind
794 * where we started, and we miss in one loop.
795 * A second loop should grab the header, but we'll do
796 * three loops just because I'm paranoid.
797 */
798 for (i = 0; i < 3; i++) {
799 do {
800 if (rb_is_head_page(cpu_buffer, page, page->list.prev)) {
801 cpu_buffer->head_page = page;
802 return page;
803 }
804 rb_inc_page(cpu_buffer, &page);
805 } while (page != head);
806 }
807
808 RB_WARN_ON(cpu_buffer, 1);
809
810 return NULL;
811}
812
813static int rb_head_page_replace(struct buffer_page *old,
814 struct buffer_page *new)
815{
816 unsigned long *ptr = (unsigned long *)&old->list.prev->next;
817 unsigned long val;
818 unsigned long ret;
819
820 val = *ptr & ~RB_FLAG_MASK;
821 val |= RB_PAGE_HEAD;
822
Steven Rostedt08a40812009-09-14 09:31:35 -0400823 ret = cmpxchg(ptr, val, (unsigned long)&new->list);
Steven Rostedt77ae3652009-03-27 11:00:29 -0400824
825 return ret == val;
826}
827
828/*
829 * rb_tail_page_update - move the tail page forward
830 *
831 * Returns 1 if moved tail page, 0 if someone else did.
832 */
833static int rb_tail_page_update(struct ring_buffer_per_cpu *cpu_buffer,
834 struct buffer_page *tail_page,
835 struct buffer_page *next_page)
836{
837 struct buffer_page *old_tail;
838 unsigned long old_entries;
839 unsigned long old_write;
840 int ret = 0;
841
842 /*
843 * The tail page now needs to be moved forward.
844 *
845 * We need to reset the tail page, but without messing
846 * with possible erasing of data brought in by interrupts
847 * that have moved the tail page and are currently on it.
848 *
849 * We add a counter to the write field to denote this.
850 */
851 old_write = local_add_return(RB_WRITE_INTCNT, &next_page->write);
852 old_entries = local_add_return(RB_WRITE_INTCNT, &next_page->entries);
853
854 /*
855 * Just make sure we have seen our old_write and synchronize
856 * with any interrupts that come in.
857 */
858 barrier();
859
860 /*
861 * If the tail page is still the same as what we think
862 * it is, then it is up to us to update the tail
863 * pointer.
864 */
865 if (tail_page == cpu_buffer->tail_page) {
866 /* Zero the write counter */
867 unsigned long val = old_write & ~RB_WRITE_MASK;
868 unsigned long eval = old_entries & ~RB_WRITE_MASK;
869
870 /*
871 * This will only succeed if an interrupt did
872 * not come in and change it. In which case, we
873 * do not want to modify it.
Lai Jiangshanda706d82009-07-15 16:27:30 +0800874 *
875 * We add (void) to let the compiler know that we do not care
876 * about the return value of these functions. We use the
877 * cmpxchg to only update if an interrupt did not already
878 * do it for us. If the cmpxchg fails, we don't care.
Steven Rostedt77ae3652009-03-27 11:00:29 -0400879 */
Lai Jiangshanda706d82009-07-15 16:27:30 +0800880 (void)local_cmpxchg(&next_page->write, old_write, val);
881 (void)local_cmpxchg(&next_page->entries, old_entries, eval);
Steven Rostedt77ae3652009-03-27 11:00:29 -0400882
883 /*
884 * No need to worry about races with clearing out the commit.
885 * it only can increment when a commit takes place. But that
886 * only happens in the outer most nested commit.
887 */
888 local_set(&next_page->page->commit, 0);
889
890 old_tail = cmpxchg(&cpu_buffer->tail_page,
891 tail_page, next_page);
892
893 if (old_tail == tail_page)
894 ret = 1;
895 }
896
897 return ret;
898}
899
900static int rb_check_bpage(struct ring_buffer_per_cpu *cpu_buffer,
901 struct buffer_page *bpage)
902{
903 unsigned long val = (unsigned long)bpage;
904
905 if (RB_WARN_ON(cpu_buffer, val & RB_FLAG_MASK))
906 return 1;
907
908 return 0;
909}
910
911/**
912 * rb_check_list - make sure a pointer to a list has the last bits zero
913 */
914static int rb_check_list(struct ring_buffer_per_cpu *cpu_buffer,
915 struct list_head *list)
916{
917 if (RB_WARN_ON(cpu_buffer, rb_list_head(list->prev) != list->prev))
918 return 1;
919 if (RB_WARN_ON(cpu_buffer, rb_list_head(list->next) != list->next))
920 return 1;
921 return 0;
922}
923
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400924/**
925 * check_pages - integrity check of buffer pages
926 * @cpu_buffer: CPU buffer with pages to test
927 *
Wenji Huangc3706f02009-02-10 01:03:18 -0500928 * As a safety measure we check to make sure the data pages have not
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400929 * been corrupted.
930 */
931static int rb_check_pages(struct ring_buffer_per_cpu *cpu_buffer)
932{
Steven Rostedt3adc54f2009-03-30 15:32:01 -0400933 struct list_head *head = cpu_buffer->pages;
Steven Rostedt044fa782008-12-02 23:50:03 -0500934 struct buffer_page *bpage, *tmp;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400935
Steven Rostedt77ae3652009-03-27 11:00:29 -0400936 rb_head_page_deactivate(cpu_buffer);
937
Steven Rostedt3e89c7b2008-11-11 15:28:41 -0500938 if (RB_WARN_ON(cpu_buffer, head->next->prev != head))
939 return -1;
940 if (RB_WARN_ON(cpu_buffer, head->prev->next != head))
941 return -1;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400942
Steven Rostedt77ae3652009-03-27 11:00:29 -0400943 if (rb_check_list(cpu_buffer, head))
944 return -1;
945
Steven Rostedt044fa782008-12-02 23:50:03 -0500946 list_for_each_entry_safe(bpage, tmp, head, list) {
Steven Rostedt3e89c7b2008-11-11 15:28:41 -0500947 if (RB_WARN_ON(cpu_buffer,
Steven Rostedt044fa782008-12-02 23:50:03 -0500948 bpage->list.next->prev != &bpage->list))
Steven Rostedt3e89c7b2008-11-11 15:28:41 -0500949 return -1;
950 if (RB_WARN_ON(cpu_buffer,
Steven Rostedt044fa782008-12-02 23:50:03 -0500951 bpage->list.prev->next != &bpage->list))
Steven Rostedt3e89c7b2008-11-11 15:28:41 -0500952 return -1;
Steven Rostedt77ae3652009-03-27 11:00:29 -0400953 if (rb_check_list(cpu_buffer, &bpage->list))
954 return -1;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400955 }
956
Steven Rostedt77ae3652009-03-27 11:00:29 -0400957 rb_head_page_activate(cpu_buffer);
958
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400959 return 0;
960}
961
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400962static int rb_allocate_pages(struct ring_buffer_per_cpu *cpu_buffer,
963 unsigned nr_pages)
964{
Steven Rostedt044fa782008-12-02 23:50:03 -0500965 struct buffer_page *bpage, *tmp;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400966 unsigned long addr;
967 LIST_HEAD(pages);
968 unsigned i;
969
Steven Rostedt3adc54f2009-03-30 15:32:01 -0400970 WARN_ON(!nr_pages);
971
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400972 for (i = 0; i < nr_pages; i++) {
Steven Rostedt044fa782008-12-02 23:50:03 -0500973 bpage = kzalloc_node(ALIGN(sizeof(*bpage), cache_line_size()),
Steven Rostedtaa1e0e32008-10-02 19:18:09 -0400974 GFP_KERNEL, cpu_to_node(cpu_buffer->cpu));
Steven Rostedt044fa782008-12-02 23:50:03 -0500975 if (!bpage)
Steven Rostedte4c2ce82008-10-01 11:14:54 -0400976 goto free_pages;
Steven Rostedt77ae3652009-03-27 11:00:29 -0400977
978 rb_check_bpage(cpu_buffer, bpage);
979
Steven Rostedt044fa782008-12-02 23:50:03 -0500980 list_add(&bpage->list, &pages);
Steven Rostedte4c2ce82008-10-01 11:14:54 -0400981
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400982 addr = __get_free_page(GFP_KERNEL);
983 if (!addr)
984 goto free_pages;
Steven Rostedt044fa782008-12-02 23:50:03 -0500985 bpage->page = (void *)addr;
986 rb_init_page(bpage->page);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400987 }
988
Steven Rostedt3adc54f2009-03-30 15:32:01 -0400989 /*
990 * The ring buffer page list is a circular list that does not
991 * start and end with a list head. All page list items point to
992 * other pages.
993 */
994 cpu_buffer->pages = pages.next;
995 list_del(&pages);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400996
997 rb_check_pages(cpu_buffer);
998
999 return 0;
1000
1001 free_pages:
Steven Rostedt044fa782008-12-02 23:50:03 -05001002 list_for_each_entry_safe(bpage, tmp, &pages, list) {
1003 list_del_init(&bpage->list);
1004 free_buffer_page(bpage);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001005 }
1006 return -ENOMEM;
1007}
1008
1009static struct ring_buffer_per_cpu *
1010rb_allocate_cpu_buffer(struct ring_buffer *buffer, int cpu)
1011{
1012 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedt044fa782008-12-02 23:50:03 -05001013 struct buffer_page *bpage;
Steven Rostedtd7690412008-10-01 00:29:53 -04001014 unsigned long addr;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001015 int ret;
1016
1017 cpu_buffer = kzalloc_node(ALIGN(sizeof(*cpu_buffer), cache_line_size()),
1018 GFP_KERNEL, cpu_to_node(cpu));
1019 if (!cpu_buffer)
1020 return NULL;
1021
1022 cpu_buffer->cpu = cpu;
1023 cpu_buffer->buffer = buffer;
Steven Rostedtf83c9d02008-11-11 18:47:44 +01001024 spin_lock_init(&cpu_buffer->reader_lock);
Peter Zijlstra1f8a6a12009-06-08 18:18:39 +02001025 lockdep_set_class(&cpu_buffer->reader_lock, buffer->reader_lock_key);
Thomas Gleixneredc35bd2009-12-03 12:38:57 +01001026 cpu_buffer->lock = (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001027
Steven Rostedt044fa782008-12-02 23:50:03 -05001028 bpage = kzalloc_node(ALIGN(sizeof(*bpage), cache_line_size()),
Steven Rostedte4c2ce82008-10-01 11:14:54 -04001029 GFP_KERNEL, cpu_to_node(cpu));
Steven Rostedt044fa782008-12-02 23:50:03 -05001030 if (!bpage)
Steven Rostedte4c2ce82008-10-01 11:14:54 -04001031 goto fail_free_buffer;
1032
Steven Rostedt77ae3652009-03-27 11:00:29 -04001033 rb_check_bpage(cpu_buffer, bpage);
1034
Steven Rostedt044fa782008-12-02 23:50:03 -05001035 cpu_buffer->reader_page = bpage;
Steven Rostedtd7690412008-10-01 00:29:53 -04001036 addr = __get_free_page(GFP_KERNEL);
1037 if (!addr)
Steven Rostedte4c2ce82008-10-01 11:14:54 -04001038 goto fail_free_reader;
Steven Rostedt044fa782008-12-02 23:50:03 -05001039 bpage->page = (void *)addr;
1040 rb_init_page(bpage->page);
Steven Rostedte4c2ce82008-10-01 11:14:54 -04001041
Steven Rostedtd7690412008-10-01 00:29:53 -04001042 INIT_LIST_HEAD(&cpu_buffer->reader_page->list);
Steven Rostedtd7690412008-10-01 00:29:53 -04001043
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001044 ret = rb_allocate_pages(cpu_buffer, buffer->pages);
1045 if (ret < 0)
Steven Rostedtd7690412008-10-01 00:29:53 -04001046 goto fail_free_reader;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001047
1048 cpu_buffer->head_page
Steven Rostedt3adc54f2009-03-30 15:32:01 -04001049 = list_entry(cpu_buffer->pages, struct buffer_page, list);
Steven Rostedtbf41a152008-10-04 02:00:59 -04001050 cpu_buffer->tail_page = cpu_buffer->commit_page = cpu_buffer->head_page;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001051
Steven Rostedt77ae3652009-03-27 11:00:29 -04001052 rb_head_page_activate(cpu_buffer);
1053
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001054 return cpu_buffer;
1055
Steven Rostedtd7690412008-10-01 00:29:53 -04001056 fail_free_reader:
1057 free_buffer_page(cpu_buffer->reader_page);
1058
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001059 fail_free_buffer:
1060 kfree(cpu_buffer);
1061 return NULL;
1062}
1063
1064static void rb_free_cpu_buffer(struct ring_buffer_per_cpu *cpu_buffer)
1065{
Steven Rostedt3adc54f2009-03-30 15:32:01 -04001066 struct list_head *head = cpu_buffer->pages;
Steven Rostedt044fa782008-12-02 23:50:03 -05001067 struct buffer_page *bpage, *tmp;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001068
Steven Rostedtd7690412008-10-01 00:29:53 -04001069 free_buffer_page(cpu_buffer->reader_page);
1070
Steven Rostedt77ae3652009-03-27 11:00:29 -04001071 rb_head_page_deactivate(cpu_buffer);
1072
Steven Rostedt3adc54f2009-03-30 15:32:01 -04001073 if (head) {
1074 list_for_each_entry_safe(bpage, tmp, head, list) {
1075 list_del_init(&bpage->list);
1076 free_buffer_page(bpage);
1077 }
1078 bpage = list_entry(head, struct buffer_page, list);
Steven Rostedt044fa782008-12-02 23:50:03 -05001079 free_buffer_page(bpage);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001080 }
Steven Rostedt3adc54f2009-03-30 15:32:01 -04001081
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001082 kfree(cpu_buffer);
1083}
1084
Steven Rostedt59222ef2009-03-12 11:46:03 -04001085#ifdef CONFIG_HOTPLUG_CPU
Frederic Weisbecker09c9e842009-03-21 04:33:36 +01001086static int rb_cpu_notify(struct notifier_block *self,
1087 unsigned long action, void *hcpu);
Steven Rostedt554f7862009-03-11 22:00:13 -04001088#endif
1089
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001090/**
1091 * ring_buffer_alloc - allocate a new ring_buffer
Robert Richter68814b52008-11-24 12:24:12 +01001092 * @size: the size in bytes per cpu that is needed.
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001093 * @flags: attributes to set for the ring buffer.
1094 *
1095 * Currently the only flag that is available is the RB_FL_OVERWRITE
1096 * flag. This flag means that the buffer will overwrite old data
1097 * when the buffer wraps. If this flag is not set, the buffer will
1098 * drop data when the tail hits the head.
1099 */
Peter Zijlstra1f8a6a12009-06-08 18:18:39 +02001100struct ring_buffer *__ring_buffer_alloc(unsigned long size, unsigned flags,
1101 struct lock_class_key *key)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001102{
1103 struct ring_buffer *buffer;
1104 int bsize;
1105 int cpu;
1106
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001107 /* keep it in its own cache line */
1108 buffer = kzalloc(ALIGN(sizeof(*buffer), cache_line_size()),
1109 GFP_KERNEL);
1110 if (!buffer)
1111 return NULL;
1112
Rusty Russell9e01c1b2009-01-01 10:12:22 +10301113 if (!alloc_cpumask_var(&buffer->cpumask, GFP_KERNEL))
1114 goto fail_free_buffer;
1115
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001116 buffer->pages = DIV_ROUND_UP(size, BUF_PAGE_SIZE);
1117 buffer->flags = flags;
Steven Rostedt37886f62009-03-17 17:22:06 -04001118 buffer->clock = trace_clock_local;
Peter Zijlstra1f8a6a12009-06-08 18:18:39 +02001119 buffer->reader_lock_key = key;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001120
1121 /* need at least two pages */
Steven Rostedt5f78abe2009-06-17 14:11:10 -04001122 if (buffer->pages < 2)
1123 buffer->pages = 2;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001124
Frederic Weisbecker3bf832c2009-03-19 14:47:33 +01001125 /*
1126 * In case of non-hotplug cpu, if the ring-buffer is allocated
1127 * in early initcall, it will not be notified of secondary cpus.
1128 * In that off case, we need to allocate for all possible cpus.
1129 */
1130#ifdef CONFIG_HOTPLUG_CPU
Steven Rostedt554f7862009-03-11 22:00:13 -04001131 get_online_cpus();
1132 cpumask_copy(buffer->cpumask, cpu_online_mask);
Frederic Weisbecker3bf832c2009-03-19 14:47:33 +01001133#else
1134 cpumask_copy(buffer->cpumask, cpu_possible_mask);
1135#endif
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001136 buffer->cpus = nr_cpu_ids;
1137
1138 bsize = sizeof(void *) * nr_cpu_ids;
1139 buffer->buffers = kzalloc(ALIGN(bsize, cache_line_size()),
1140 GFP_KERNEL);
1141 if (!buffer->buffers)
Rusty Russell9e01c1b2009-01-01 10:12:22 +10301142 goto fail_free_cpumask;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001143
1144 for_each_buffer_cpu(buffer, cpu) {
1145 buffer->buffers[cpu] =
1146 rb_allocate_cpu_buffer(buffer, cpu);
1147 if (!buffer->buffers[cpu])
1148 goto fail_free_buffers;
1149 }
1150
Steven Rostedt59222ef2009-03-12 11:46:03 -04001151#ifdef CONFIG_HOTPLUG_CPU
Steven Rostedt554f7862009-03-11 22:00:13 -04001152 buffer->cpu_notify.notifier_call = rb_cpu_notify;
1153 buffer->cpu_notify.priority = 0;
1154 register_cpu_notifier(&buffer->cpu_notify);
1155#endif
1156
1157 put_online_cpus();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001158 mutex_init(&buffer->mutex);
1159
1160 return buffer;
1161
1162 fail_free_buffers:
1163 for_each_buffer_cpu(buffer, cpu) {
1164 if (buffer->buffers[cpu])
1165 rb_free_cpu_buffer(buffer->buffers[cpu]);
1166 }
1167 kfree(buffer->buffers);
1168
Rusty Russell9e01c1b2009-01-01 10:12:22 +10301169 fail_free_cpumask:
1170 free_cpumask_var(buffer->cpumask);
Steven Rostedt554f7862009-03-11 22:00:13 -04001171 put_online_cpus();
Rusty Russell9e01c1b2009-01-01 10:12:22 +10301172
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001173 fail_free_buffer:
1174 kfree(buffer);
1175 return NULL;
1176}
Peter Zijlstra1f8a6a12009-06-08 18:18:39 +02001177EXPORT_SYMBOL_GPL(__ring_buffer_alloc);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001178
1179/**
1180 * ring_buffer_free - free a ring buffer.
1181 * @buffer: the buffer to free.
1182 */
1183void
1184ring_buffer_free(struct ring_buffer *buffer)
1185{
1186 int cpu;
1187
Steven Rostedt554f7862009-03-11 22:00:13 -04001188 get_online_cpus();
1189
Steven Rostedt59222ef2009-03-12 11:46:03 -04001190#ifdef CONFIG_HOTPLUG_CPU
Steven Rostedt554f7862009-03-11 22:00:13 -04001191 unregister_cpu_notifier(&buffer->cpu_notify);
1192#endif
1193
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001194 for_each_buffer_cpu(buffer, cpu)
1195 rb_free_cpu_buffer(buffer->buffers[cpu]);
1196
Steven Rostedt554f7862009-03-11 22:00:13 -04001197 put_online_cpus();
1198
Eric Dumazetbd3f0222009-08-07 12:49:29 +02001199 kfree(buffer->buffers);
Rusty Russell9e01c1b2009-01-01 10:12:22 +10301200 free_cpumask_var(buffer->cpumask);
1201
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001202 kfree(buffer);
1203}
Robert Richterc4f50182008-12-11 16:49:22 +01001204EXPORT_SYMBOL_GPL(ring_buffer_free);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001205
Steven Rostedt37886f62009-03-17 17:22:06 -04001206void ring_buffer_set_clock(struct ring_buffer *buffer,
1207 u64 (*clock)(void))
1208{
1209 buffer->clock = clock;
1210}
1211
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001212static void rb_reset_cpu(struct ring_buffer_per_cpu *cpu_buffer);
1213
1214static void
1215rb_remove_pages(struct ring_buffer_per_cpu *cpu_buffer, unsigned nr_pages)
1216{
Steven Rostedt044fa782008-12-02 23:50:03 -05001217 struct buffer_page *bpage;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001218 struct list_head *p;
1219 unsigned i;
1220
Lai Jiangshanf7112942009-11-03 19:42:45 +08001221 spin_lock_irq(&cpu_buffer->reader_lock);
Steven Rostedt77ae3652009-03-27 11:00:29 -04001222 rb_head_page_deactivate(cpu_buffer);
1223
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001224 for (i = 0; i < nr_pages; i++) {
Steven Rostedt3adc54f2009-03-30 15:32:01 -04001225 if (RB_WARN_ON(cpu_buffer, list_empty(cpu_buffer->pages)))
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05001226 return;
Steven Rostedt3adc54f2009-03-30 15:32:01 -04001227 p = cpu_buffer->pages->next;
Steven Rostedt044fa782008-12-02 23:50:03 -05001228 bpage = list_entry(p, struct buffer_page, list);
1229 list_del_init(&bpage->list);
1230 free_buffer_page(bpage);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001231 }
Steven Rostedt3adc54f2009-03-30 15:32:01 -04001232 if (RB_WARN_ON(cpu_buffer, list_empty(cpu_buffer->pages)))
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05001233 return;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001234
1235 rb_reset_cpu(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001236 rb_check_pages(cpu_buffer);
1237
Steven Rostedtdd7f5942009-12-10 23:20:52 -05001238 spin_unlock_irq(&cpu_buffer->reader_lock);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001239}
1240
1241static void
1242rb_insert_pages(struct ring_buffer_per_cpu *cpu_buffer,
1243 struct list_head *pages, unsigned nr_pages)
1244{
Steven Rostedt044fa782008-12-02 23:50:03 -05001245 struct buffer_page *bpage;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001246 struct list_head *p;
1247 unsigned i;
1248
Steven Rostedt77ae3652009-03-27 11:00:29 -04001249 spin_lock_irq(&cpu_buffer->reader_lock);
1250 rb_head_page_deactivate(cpu_buffer);
1251
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001252 for (i = 0; i < nr_pages; i++) {
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05001253 if (RB_WARN_ON(cpu_buffer, list_empty(pages)))
1254 return;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001255 p = pages->next;
Steven Rostedt044fa782008-12-02 23:50:03 -05001256 bpage = list_entry(p, struct buffer_page, list);
1257 list_del_init(&bpage->list);
Steven Rostedt3adc54f2009-03-30 15:32:01 -04001258 list_add_tail(&bpage->list, cpu_buffer->pages);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001259 }
1260 rb_reset_cpu(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001261 rb_check_pages(cpu_buffer);
1262
Steven Rostedtdd7f5942009-12-10 23:20:52 -05001263 spin_unlock_irq(&cpu_buffer->reader_lock);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001264}
1265
1266/**
1267 * ring_buffer_resize - resize the ring buffer
1268 * @buffer: the buffer to resize.
1269 * @size: the new size.
1270 *
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001271 * Minimum size is 2 * BUF_PAGE_SIZE.
1272 *
1273 * Returns -1 on failure.
1274 */
1275int ring_buffer_resize(struct ring_buffer *buffer, unsigned long size)
1276{
1277 struct ring_buffer_per_cpu *cpu_buffer;
1278 unsigned nr_pages, rm_pages, new_pages;
Steven Rostedt044fa782008-12-02 23:50:03 -05001279 struct buffer_page *bpage, *tmp;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001280 unsigned long buffer_size;
1281 unsigned long addr;
1282 LIST_HEAD(pages);
1283 int i, cpu;
1284
Ingo Molnaree51a1d2008-11-13 14:58:31 +01001285 /*
1286 * Always succeed at resizing a non-existent buffer:
1287 */
1288 if (!buffer)
1289 return size;
1290
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001291 size = DIV_ROUND_UP(size, BUF_PAGE_SIZE);
1292 size *= BUF_PAGE_SIZE;
1293 buffer_size = buffer->pages * BUF_PAGE_SIZE;
1294
1295 /* we need a minimum of two pages */
1296 if (size < BUF_PAGE_SIZE * 2)
1297 size = BUF_PAGE_SIZE * 2;
1298
1299 if (size == buffer_size)
1300 return size;
1301
Steven Rostedt18421012009-12-10 22:54:27 -05001302 atomic_inc(&buffer->record_disabled);
1303
1304 /* Make sure all writers are done with this buffer. */
1305 synchronize_sched();
1306
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001307 mutex_lock(&buffer->mutex);
Steven Rostedt554f7862009-03-11 22:00:13 -04001308 get_online_cpus();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001309
1310 nr_pages = DIV_ROUND_UP(size, BUF_PAGE_SIZE);
1311
1312 if (size < buffer_size) {
1313
1314 /* easy case, just free pages */
Steven Rostedt554f7862009-03-11 22:00:13 -04001315 if (RB_WARN_ON(buffer, nr_pages >= buffer->pages))
1316 goto out_fail;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001317
1318 rm_pages = buffer->pages - nr_pages;
1319
1320 for_each_buffer_cpu(buffer, cpu) {
1321 cpu_buffer = buffer->buffers[cpu];
1322 rb_remove_pages(cpu_buffer, rm_pages);
1323 }
1324 goto out;
1325 }
1326
1327 /*
1328 * This is a bit more difficult. We only want to add pages
1329 * when we can allocate enough for all CPUs. We do this
1330 * by allocating all the pages and storing them on a local
1331 * link list. If we succeed in our allocation, then we
1332 * add these pages to the cpu_buffers. Otherwise we just free
1333 * them all and return -ENOMEM;
1334 */
Steven Rostedt554f7862009-03-11 22:00:13 -04001335 if (RB_WARN_ON(buffer, nr_pages <= buffer->pages))
1336 goto out_fail;
Steven Rostedtf536aaf2008-11-10 23:07:30 -05001337
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001338 new_pages = nr_pages - buffer->pages;
1339
1340 for_each_buffer_cpu(buffer, cpu) {
1341 for (i = 0; i < new_pages; i++) {
Steven Rostedt044fa782008-12-02 23:50:03 -05001342 bpage = kzalloc_node(ALIGN(sizeof(*bpage),
Steven Rostedte4c2ce82008-10-01 11:14:54 -04001343 cache_line_size()),
1344 GFP_KERNEL, cpu_to_node(cpu));
Steven Rostedt044fa782008-12-02 23:50:03 -05001345 if (!bpage)
Steven Rostedte4c2ce82008-10-01 11:14:54 -04001346 goto free_pages;
Steven Rostedt044fa782008-12-02 23:50:03 -05001347 list_add(&bpage->list, &pages);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001348 addr = __get_free_page(GFP_KERNEL);
1349 if (!addr)
1350 goto free_pages;
Steven Rostedt044fa782008-12-02 23:50:03 -05001351 bpage->page = (void *)addr;
1352 rb_init_page(bpage->page);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001353 }
1354 }
1355
1356 for_each_buffer_cpu(buffer, cpu) {
1357 cpu_buffer = buffer->buffers[cpu];
1358 rb_insert_pages(cpu_buffer, &pages, new_pages);
1359 }
1360
Steven Rostedt554f7862009-03-11 22:00:13 -04001361 if (RB_WARN_ON(buffer, !list_empty(&pages)))
1362 goto out_fail;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001363
1364 out:
1365 buffer->pages = nr_pages;
Steven Rostedt554f7862009-03-11 22:00:13 -04001366 put_online_cpus();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001367 mutex_unlock(&buffer->mutex);
1368
Steven Rostedt18421012009-12-10 22:54:27 -05001369 atomic_dec(&buffer->record_disabled);
1370
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001371 return size;
1372
1373 free_pages:
Steven Rostedt044fa782008-12-02 23:50:03 -05001374 list_for_each_entry_safe(bpage, tmp, &pages, list) {
1375 list_del_init(&bpage->list);
1376 free_buffer_page(bpage);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001377 }
Steven Rostedt554f7862009-03-11 22:00:13 -04001378 put_online_cpus();
Vegard Nossum641d2f62008-11-18 19:22:13 +01001379 mutex_unlock(&buffer->mutex);
Steven Rostedt18421012009-12-10 22:54:27 -05001380 atomic_dec(&buffer->record_disabled);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001381 return -ENOMEM;
Steven Rostedt554f7862009-03-11 22:00:13 -04001382
1383 /*
1384 * Something went totally wrong, and we are too paranoid
1385 * to even clean up the mess.
1386 */
1387 out_fail:
1388 put_online_cpus();
1389 mutex_unlock(&buffer->mutex);
Steven Rostedt18421012009-12-10 22:54:27 -05001390 atomic_dec(&buffer->record_disabled);
Steven Rostedt554f7862009-03-11 22:00:13 -04001391 return -1;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001392}
Robert Richterc4f50182008-12-11 16:49:22 +01001393EXPORT_SYMBOL_GPL(ring_buffer_resize);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001394
Steven Rostedt8789a9e2008-12-02 15:34:07 -05001395static inline void *
Steven Rostedt044fa782008-12-02 23:50:03 -05001396__rb_data_page_index(struct buffer_data_page *bpage, unsigned index)
Steven Rostedt8789a9e2008-12-02 15:34:07 -05001397{
Steven Rostedt044fa782008-12-02 23:50:03 -05001398 return bpage->data + index;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05001399}
1400
Steven Rostedt044fa782008-12-02 23:50:03 -05001401static inline void *__rb_page_index(struct buffer_page *bpage, unsigned index)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001402{
Steven Rostedt044fa782008-12-02 23:50:03 -05001403 return bpage->page->data + index;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001404}
1405
1406static inline struct ring_buffer_event *
Steven Rostedtd7690412008-10-01 00:29:53 -04001407rb_reader_event(struct ring_buffer_per_cpu *cpu_buffer)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001408{
Steven Rostedt6f807ac2008-10-04 02:00:58 -04001409 return __rb_page_index(cpu_buffer->reader_page,
1410 cpu_buffer->reader_page->read);
1411}
1412
1413static inline struct ring_buffer_event *
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001414rb_iter_head_event(struct ring_buffer_iter *iter)
1415{
Steven Rostedt6f807ac2008-10-04 02:00:58 -04001416 return __rb_page_index(iter->head_page, iter->head);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001417}
1418
Steven Rostedt77ae3652009-03-27 11:00:29 -04001419static inline unsigned long rb_page_write(struct buffer_page *bpage)
Steven Rostedtbf41a152008-10-04 02:00:59 -04001420{
Steven Rostedt77ae3652009-03-27 11:00:29 -04001421 return local_read(&bpage->write) & RB_WRITE_MASK;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001422}
1423
1424static inline unsigned rb_page_commit(struct buffer_page *bpage)
1425{
Steven Rostedtabc9b562008-12-02 15:34:06 -05001426 return local_read(&bpage->page->commit);
Steven Rostedtbf41a152008-10-04 02:00:59 -04001427}
1428
Steven Rostedt77ae3652009-03-27 11:00:29 -04001429static inline unsigned long rb_page_entries(struct buffer_page *bpage)
1430{
1431 return local_read(&bpage->entries) & RB_WRITE_MASK;
1432}
1433
Steven Rostedtbf41a152008-10-04 02:00:59 -04001434/* Size is determined by what has been commited */
1435static inline unsigned rb_page_size(struct buffer_page *bpage)
1436{
1437 return rb_page_commit(bpage);
1438}
1439
1440static inline unsigned
1441rb_commit_index(struct ring_buffer_per_cpu *cpu_buffer)
1442{
1443 return rb_page_commit(cpu_buffer->commit_page);
1444}
1445
Steven Rostedtbf41a152008-10-04 02:00:59 -04001446static inline unsigned
1447rb_event_index(struct ring_buffer_event *event)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001448{
Steven Rostedtbf41a152008-10-04 02:00:59 -04001449 unsigned long addr = (unsigned long)event;
1450
Steven Rostedt22f470f2009-06-11 09:29:58 -04001451 return (addr & ~PAGE_MASK) - BUF_PAGE_HDR_SIZE;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001452}
1453
Steven Rostedt0f0c85f2009-05-11 16:08:00 -04001454static inline int
Steven Rostedtfa743952009-06-16 12:37:57 -04001455rb_event_is_commit(struct ring_buffer_per_cpu *cpu_buffer,
1456 struct ring_buffer_event *event)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001457{
Steven Rostedtbf41a152008-10-04 02:00:59 -04001458 unsigned long addr = (unsigned long)event;
1459 unsigned long index;
1460
1461 index = rb_event_index(event);
1462 addr &= PAGE_MASK;
1463
1464 return cpu_buffer->commit_page->page == (void *)addr &&
1465 rb_commit_index(cpu_buffer) == index;
1466}
1467
Andrew Morton34a148b2009-01-09 12:27:09 -08001468static void
Steven Rostedtbf41a152008-10-04 02:00:59 -04001469rb_set_commit_to_write(struct ring_buffer_per_cpu *cpu_buffer)
1470{
Steven Rostedt77ae3652009-03-27 11:00:29 -04001471 unsigned long max_count;
1472
Steven Rostedtbf41a152008-10-04 02:00:59 -04001473 /*
1474 * We only race with interrupts and NMIs on this CPU.
1475 * If we own the commit event, then we can commit
1476 * all others that interrupted us, since the interruptions
1477 * are in stack format (they finish before they come
1478 * back to us). This allows us to do a simple loop to
1479 * assign the commit to the tail.
1480 */
Steven Rostedta8ccf1d2008-12-23 11:32:24 -05001481 again:
Steven Rostedt77ae3652009-03-27 11:00:29 -04001482 max_count = cpu_buffer->buffer->pages * 100;
1483
Steven Rostedtbf41a152008-10-04 02:00:59 -04001484 while (cpu_buffer->commit_page != cpu_buffer->tail_page) {
Steven Rostedt77ae3652009-03-27 11:00:29 -04001485 if (RB_WARN_ON(cpu_buffer, !(--max_count)))
1486 return;
1487 if (RB_WARN_ON(cpu_buffer,
1488 rb_is_reader_page(cpu_buffer->tail_page)))
1489 return;
1490 local_set(&cpu_buffer->commit_page->page->commit,
1491 rb_page_write(cpu_buffer->commit_page));
Steven Rostedtbf41a152008-10-04 02:00:59 -04001492 rb_inc_page(cpu_buffer, &cpu_buffer->commit_page);
Steven Rostedtabc9b562008-12-02 15:34:06 -05001493 cpu_buffer->write_stamp =
1494 cpu_buffer->commit_page->page->time_stamp;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001495 /* add barrier to keep gcc from optimizing too much */
1496 barrier();
1497 }
1498 while (rb_commit_index(cpu_buffer) !=
1499 rb_page_write(cpu_buffer->commit_page)) {
Steven Rostedt77ae3652009-03-27 11:00:29 -04001500
1501 local_set(&cpu_buffer->commit_page->page->commit,
1502 rb_page_write(cpu_buffer->commit_page));
1503 RB_WARN_ON(cpu_buffer,
1504 local_read(&cpu_buffer->commit_page->page->commit) &
1505 ~RB_WRITE_MASK);
Steven Rostedtbf41a152008-10-04 02:00:59 -04001506 barrier();
1507 }
Steven Rostedta8ccf1d2008-12-23 11:32:24 -05001508
1509 /* again, keep gcc from optimizing */
1510 barrier();
1511
1512 /*
1513 * If an interrupt came in just after the first while loop
1514 * and pushed the tail page forward, we will be left with
1515 * a dangling commit that will never go forward.
1516 */
1517 if (unlikely(cpu_buffer->commit_page != cpu_buffer->tail_page))
1518 goto again;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001519}
1520
Steven Rostedtd7690412008-10-01 00:29:53 -04001521static void rb_reset_reader_page(struct ring_buffer_per_cpu *cpu_buffer)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001522{
Steven Rostedtabc9b562008-12-02 15:34:06 -05001523 cpu_buffer->read_stamp = cpu_buffer->reader_page->page->time_stamp;
Steven Rostedt6f807ac2008-10-04 02:00:58 -04001524 cpu_buffer->reader_page->read = 0;
Steven Rostedtd7690412008-10-01 00:29:53 -04001525}
1526
Andrew Morton34a148b2009-01-09 12:27:09 -08001527static void rb_inc_iter(struct ring_buffer_iter *iter)
Steven Rostedtd7690412008-10-01 00:29:53 -04001528{
1529 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
1530
1531 /*
1532 * The iterator could be on the reader page (it starts there).
1533 * But the head could have moved, since the reader was
1534 * found. Check for this case and assign the iterator
1535 * to the head page instead of next.
1536 */
1537 if (iter->head_page == cpu_buffer->reader_page)
Steven Rostedt77ae3652009-03-27 11:00:29 -04001538 iter->head_page = rb_set_head_page(cpu_buffer);
Steven Rostedtd7690412008-10-01 00:29:53 -04001539 else
1540 rb_inc_page(cpu_buffer, &iter->head_page);
1541
Steven Rostedtabc9b562008-12-02 15:34:06 -05001542 iter->read_stamp = iter->head_page->page->time_stamp;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001543 iter->head = 0;
1544}
1545
1546/**
1547 * ring_buffer_update_event - update event type and data
1548 * @event: the even to update
1549 * @type: the type of event
1550 * @length: the size of the event field in the ring buffer
1551 *
1552 * Update the type and data fields of the event. The length
1553 * is the actual size that is written to the ring buffer,
1554 * and with this, we can determine what to place into the
1555 * data field.
1556 */
Andrew Morton34a148b2009-01-09 12:27:09 -08001557static void
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001558rb_update_event(struct ring_buffer_event *event,
1559 unsigned type, unsigned length)
1560{
Lai Jiangshan334d4162009-04-24 11:27:05 +08001561 event->type_len = type;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001562
1563 switch (type) {
1564
1565 case RINGBUF_TYPE_PADDING:
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001566 case RINGBUF_TYPE_TIME_EXTEND:
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001567 case RINGBUF_TYPE_TIME_STAMP:
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001568 break;
1569
Lai Jiangshan334d4162009-04-24 11:27:05 +08001570 case 0:
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001571 length -= RB_EVNT_HDR_SIZE;
Steven Rostedt22710482010-03-18 17:54:19 -04001572 if (length > RB_MAX_SMALL_DATA || RB_FORCE_8BYTE_ALIGNMENT)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001573 event->array[0] = length;
Lai Jiangshan334d4162009-04-24 11:27:05 +08001574 else
1575 event->type_len = DIV_ROUND_UP(length, RB_ALIGNMENT);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001576 break;
1577 default:
1578 BUG();
1579 }
1580}
1581
Steven Rostedt77ae3652009-03-27 11:00:29 -04001582/*
1583 * rb_handle_head_page - writer hit the head page
1584 *
1585 * Returns: +1 to retry page
1586 * 0 to continue
1587 * -1 on error
1588 */
1589static int
1590rb_handle_head_page(struct ring_buffer_per_cpu *cpu_buffer,
1591 struct buffer_page *tail_page,
1592 struct buffer_page *next_page)
1593{
1594 struct buffer_page *new_head;
1595 int entries;
1596 int type;
1597 int ret;
1598
1599 entries = rb_page_entries(next_page);
1600
1601 /*
1602 * The hard part is here. We need to move the head
1603 * forward, and protect against both readers on
1604 * other CPUs and writers coming in via interrupts.
1605 */
1606 type = rb_head_page_set_update(cpu_buffer, next_page, tail_page,
1607 RB_PAGE_HEAD);
1608
1609 /*
1610 * type can be one of four:
1611 * NORMAL - an interrupt already moved it for us
1612 * HEAD - we are the first to get here.
1613 * UPDATE - we are the interrupt interrupting
1614 * a current move.
1615 * MOVED - a reader on another CPU moved the next
1616 * pointer to its reader page. Give up
1617 * and try again.
1618 */
1619
1620 switch (type) {
1621 case RB_PAGE_HEAD:
1622 /*
1623 * We changed the head to UPDATE, thus
1624 * it is our responsibility to update
1625 * the counters.
1626 */
1627 local_add(entries, &cpu_buffer->overrun);
1628
1629 /*
1630 * The entries will be zeroed out when we move the
1631 * tail page.
1632 */
1633
1634 /* still more to do */
1635 break;
1636
1637 case RB_PAGE_UPDATE:
1638 /*
1639 * This is an interrupt that interrupt the
1640 * previous update. Still more to do.
1641 */
1642 break;
1643 case RB_PAGE_NORMAL:
1644 /*
1645 * An interrupt came in before the update
1646 * and processed this for us.
1647 * Nothing left to do.
1648 */
1649 return 1;
1650 case RB_PAGE_MOVED:
1651 /*
1652 * The reader is on another CPU and just did
1653 * a swap with our next_page.
1654 * Try again.
1655 */
1656 return 1;
1657 default:
1658 RB_WARN_ON(cpu_buffer, 1); /* WTF??? */
1659 return -1;
1660 }
1661
1662 /*
1663 * Now that we are here, the old head pointer is
1664 * set to UPDATE. This will keep the reader from
1665 * swapping the head page with the reader page.
1666 * The reader (on another CPU) will spin till
1667 * we are finished.
1668 *
1669 * We just need to protect against interrupts
1670 * doing the job. We will set the next pointer
1671 * to HEAD. After that, we set the old pointer
1672 * to NORMAL, but only if it was HEAD before.
1673 * otherwise we are an interrupt, and only
1674 * want the outer most commit to reset it.
1675 */
1676 new_head = next_page;
1677 rb_inc_page(cpu_buffer, &new_head);
1678
1679 ret = rb_head_page_set_head(cpu_buffer, new_head, next_page,
1680 RB_PAGE_NORMAL);
1681
1682 /*
1683 * Valid returns are:
1684 * HEAD - an interrupt came in and already set it.
1685 * NORMAL - One of two things:
1686 * 1) We really set it.
1687 * 2) A bunch of interrupts came in and moved
1688 * the page forward again.
1689 */
1690 switch (ret) {
1691 case RB_PAGE_HEAD:
1692 case RB_PAGE_NORMAL:
1693 /* OK */
1694 break;
1695 default:
1696 RB_WARN_ON(cpu_buffer, 1);
1697 return -1;
1698 }
1699
1700 /*
1701 * It is possible that an interrupt came in,
1702 * set the head up, then more interrupts came in
1703 * and moved it again. When we get back here,
1704 * the page would have been set to NORMAL but we
1705 * just set it back to HEAD.
1706 *
1707 * How do you detect this? Well, if that happened
1708 * the tail page would have moved.
1709 */
1710 if (ret == RB_PAGE_NORMAL) {
1711 /*
1712 * If the tail had moved passed next, then we need
1713 * to reset the pointer.
1714 */
1715 if (cpu_buffer->tail_page != tail_page &&
1716 cpu_buffer->tail_page != next_page)
1717 rb_head_page_set_normal(cpu_buffer, new_head,
1718 next_page,
1719 RB_PAGE_HEAD);
1720 }
1721
1722 /*
1723 * If this was the outer most commit (the one that
1724 * changed the original pointer from HEAD to UPDATE),
1725 * then it is up to us to reset it to NORMAL.
1726 */
1727 if (type == RB_PAGE_HEAD) {
1728 ret = rb_head_page_set_normal(cpu_buffer, next_page,
1729 tail_page,
1730 RB_PAGE_UPDATE);
1731 if (RB_WARN_ON(cpu_buffer,
1732 ret != RB_PAGE_UPDATE))
1733 return -1;
1734 }
1735
1736 return 0;
1737}
1738
Andrew Morton34a148b2009-01-09 12:27:09 -08001739static unsigned rb_calculate_event_length(unsigned length)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001740{
1741 struct ring_buffer_event event; /* Used only for sizeof array */
1742
1743 /* zero length can cause confusions */
1744 if (!length)
1745 length = 1;
1746
Steven Rostedt22710482010-03-18 17:54:19 -04001747 if (length > RB_MAX_SMALL_DATA || RB_FORCE_8BYTE_ALIGNMENT)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001748 length += sizeof(event.array[0]);
1749
1750 length += RB_EVNT_HDR_SIZE;
Steven Rostedt22710482010-03-18 17:54:19 -04001751 length = ALIGN(length, RB_ARCH_ALIGNMENT);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001752
1753 return length;
1754}
1755
Steven Rostedtc7b09302009-06-11 11:12:00 -04001756static inline void
1757rb_reset_tail(struct ring_buffer_per_cpu *cpu_buffer,
1758 struct buffer_page *tail_page,
1759 unsigned long tail, unsigned long length)
1760{
1761 struct ring_buffer_event *event;
1762
1763 /*
1764 * Only the event that crossed the page boundary
1765 * must fill the old tail_page with padding.
1766 */
1767 if (tail >= BUF_PAGE_SIZE) {
1768 local_sub(length, &tail_page->write);
1769 return;
1770 }
1771
1772 event = __rb_page_index(tail_page, tail);
Linus Torvaldsb0b70652009-06-20 10:56:46 -07001773 kmemcheck_annotate_bitfield(event, bitfield);
Steven Rostedtc7b09302009-06-11 11:12:00 -04001774
1775 /*
Steven Rostedtff0ff842010-03-31 22:11:42 -04001776 * Save the original length to the meta data.
1777 * This will be used by the reader to add lost event
1778 * counter.
1779 */
1780 tail_page->real_end = tail;
1781
1782 /*
Steven Rostedtc7b09302009-06-11 11:12:00 -04001783 * If this event is bigger than the minimum size, then
1784 * we need to be careful that we don't subtract the
1785 * write counter enough to allow another writer to slip
1786 * in on this page.
1787 * We put in a discarded commit instead, to make sure
1788 * that this space is not used again.
1789 *
1790 * If we are less than the minimum size, we don't need to
1791 * worry about it.
1792 */
1793 if (tail > (BUF_PAGE_SIZE - RB_EVNT_MIN_SIZE)) {
1794 /* No room for any events */
1795
1796 /* Mark the rest of the page with padding */
1797 rb_event_set_padding(event);
1798
1799 /* Set the write back to the previous setting */
1800 local_sub(length, &tail_page->write);
1801 return;
1802 }
1803
1804 /* Put in a discarded event */
1805 event->array[0] = (BUF_PAGE_SIZE - tail) - RB_EVNT_HDR_SIZE;
1806 event->type_len = RINGBUF_TYPE_PADDING;
1807 /* time delta must be non zero */
1808 event->time_delta = 1;
Steven Rostedtc7b09302009-06-11 11:12:00 -04001809
1810 /* Set write to end of buffer */
1811 length = (tail + length) - BUF_PAGE_SIZE;
1812 local_sub(length, &tail_page->write);
1813}
Steven Rostedt6634ff22009-05-06 15:30:07 -04001814
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001815static struct ring_buffer_event *
Steven Rostedt6634ff22009-05-06 15:30:07 -04001816rb_move_tail(struct ring_buffer_per_cpu *cpu_buffer,
1817 unsigned long length, unsigned long tail,
Steven Rostedt6634ff22009-05-06 15:30:07 -04001818 struct buffer_page *tail_page, u64 *ts)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001819{
Steven Rostedt5a50e332009-11-17 08:43:01 -05001820 struct buffer_page *commit_page = cpu_buffer->commit_page;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001821 struct ring_buffer *buffer = cpu_buffer->buffer;
Steven Rostedt77ae3652009-03-27 11:00:29 -04001822 struct buffer_page *next_page;
1823 int ret;
Steven Rostedtaa20ae82009-05-05 21:16:11 -04001824
1825 next_page = tail_page;
1826
Steven Rostedtaa20ae82009-05-05 21:16:11 -04001827 rb_inc_page(cpu_buffer, &next_page);
1828
Steven Rostedtaa20ae82009-05-05 21:16:11 -04001829 /*
1830 * If for some reason, we had an interrupt storm that made
1831 * it all the way around the buffer, bail, and warn
1832 * about it.
1833 */
1834 if (unlikely(next_page == commit_page)) {
Steven Rostedt77ae3652009-03-27 11:00:29 -04001835 local_inc(&cpu_buffer->commit_overrun);
Steven Rostedtaa20ae82009-05-05 21:16:11 -04001836 goto out_reset;
1837 }
1838
Steven Rostedt77ae3652009-03-27 11:00:29 -04001839 /*
1840 * This is where the fun begins!
1841 *
1842 * We are fighting against races between a reader that
1843 * could be on another CPU trying to swap its reader
1844 * page with the buffer head.
1845 *
1846 * We are also fighting against interrupts coming in and
1847 * moving the head or tail on us as well.
1848 *
1849 * If the next page is the head page then we have filled
1850 * the buffer, unless the commit page is still on the
1851 * reader page.
1852 */
1853 if (rb_is_head_page(cpu_buffer, next_page, &tail_page->list)) {
Steven Rostedtaa20ae82009-05-05 21:16:11 -04001854
Steven Rostedt77ae3652009-03-27 11:00:29 -04001855 /*
1856 * If the commit is not on the reader page, then
1857 * move the header page.
1858 */
1859 if (!rb_is_reader_page(cpu_buffer->commit_page)) {
1860 /*
1861 * If we are not in overwrite mode,
1862 * this is easy, just stop here.
1863 */
1864 if (!(buffer->flags & RB_FL_OVERWRITE))
1865 goto out_reset;
Steven Rostedtaa20ae82009-05-05 21:16:11 -04001866
Steven Rostedt77ae3652009-03-27 11:00:29 -04001867 ret = rb_handle_head_page(cpu_buffer,
1868 tail_page,
1869 next_page);
1870 if (ret < 0)
1871 goto out_reset;
1872 if (ret)
1873 goto out_again;
1874 } else {
1875 /*
1876 * We need to be careful here too. The
1877 * commit page could still be on the reader
1878 * page. We could have a small buffer, and
1879 * have filled up the buffer with events
1880 * from interrupts and such, and wrapped.
1881 *
1882 * Note, if the tail page is also the on the
1883 * reader_page, we let it move out.
1884 */
1885 if (unlikely((cpu_buffer->commit_page !=
1886 cpu_buffer->tail_page) &&
1887 (cpu_buffer->commit_page ==
1888 cpu_buffer->reader_page))) {
1889 local_inc(&cpu_buffer->commit_overrun);
1890 goto out_reset;
1891 }
Steven Rostedtaa20ae82009-05-05 21:16:11 -04001892 }
1893 }
1894
Steven Rostedt77ae3652009-03-27 11:00:29 -04001895 ret = rb_tail_page_update(cpu_buffer, tail_page, next_page);
1896 if (ret) {
1897 /*
1898 * Nested commits always have zero deltas, so
1899 * just reread the time stamp
1900 */
Jiri Olsa6d3f1e12009-10-23 19:36:19 -04001901 *ts = rb_time_stamp(buffer);
Steven Rostedt77ae3652009-03-27 11:00:29 -04001902 next_page->page->time_stamp = *ts;
Steven Rostedtaa20ae82009-05-05 21:16:11 -04001903 }
1904
Steven Rostedt77ae3652009-03-27 11:00:29 -04001905 out_again:
Steven Rostedtaa20ae82009-05-05 21:16:11 -04001906
Steven Rostedt77ae3652009-03-27 11:00:29 -04001907 rb_reset_tail(cpu_buffer, tail_page, tail, length);
Steven Rostedtaa20ae82009-05-05 21:16:11 -04001908
1909 /* fail and let the caller try again */
1910 return ERR_PTR(-EAGAIN);
1911
Steven Rostedt45141d42009-02-12 13:19:48 -05001912 out_reset:
Lai Jiangshan6f3b3442009-01-12 11:06:18 +08001913 /* reset write */
Steven Rostedtc7b09302009-06-11 11:12:00 -04001914 rb_reset_tail(cpu_buffer, tail_page, tail, length);
Lai Jiangshan6f3b3442009-01-12 11:06:18 +08001915
Steven Rostedtbf41a152008-10-04 02:00:59 -04001916 return NULL;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001917}
1918
Steven Rostedt6634ff22009-05-06 15:30:07 -04001919static struct ring_buffer_event *
1920__rb_reserve_next(struct ring_buffer_per_cpu *cpu_buffer,
1921 unsigned type, unsigned long length, u64 *ts)
1922{
Steven Rostedt5a50e332009-11-17 08:43:01 -05001923 struct buffer_page *tail_page;
Steven Rostedt6634ff22009-05-06 15:30:07 -04001924 struct ring_buffer_event *event;
1925 unsigned long tail, write;
1926
Steven Rostedt6634ff22009-05-06 15:30:07 -04001927 tail_page = cpu_buffer->tail_page;
1928 write = local_add_return(length, &tail_page->write);
Steven Rostedt77ae3652009-03-27 11:00:29 -04001929
1930 /* set write to only the index of the write */
1931 write &= RB_WRITE_MASK;
Steven Rostedt6634ff22009-05-06 15:30:07 -04001932 tail = write - length;
1933
1934 /* See if we shot pass the end of this buffer page */
1935 if (write > BUF_PAGE_SIZE)
1936 return rb_move_tail(cpu_buffer, length, tail,
Steven Rostedt5a50e332009-11-17 08:43:01 -05001937 tail_page, ts);
Steven Rostedt6634ff22009-05-06 15:30:07 -04001938
1939 /* We reserved something on the buffer */
1940
Steven Rostedt6634ff22009-05-06 15:30:07 -04001941 event = __rb_page_index(tail_page, tail);
Vegard Nossum1744a212009-02-28 08:29:44 +01001942 kmemcheck_annotate_bitfield(event, bitfield);
Steven Rostedt6634ff22009-05-06 15:30:07 -04001943 rb_update_event(event, type, length);
1944
1945 /* The passed in type is zero for DATA */
1946 if (likely(!type))
1947 local_inc(&tail_page->entries);
1948
1949 /*
Steven Rostedtfa743952009-06-16 12:37:57 -04001950 * If this is the first commit on the page, then update
1951 * its timestamp.
Steven Rostedt6634ff22009-05-06 15:30:07 -04001952 */
Steven Rostedtfa743952009-06-16 12:37:57 -04001953 if (!tail)
1954 tail_page->page->time_stamp = *ts;
Steven Rostedt6634ff22009-05-06 15:30:07 -04001955
1956 return event;
1957}
1958
Steven Rostedtedd813bf2009-06-02 23:00:53 -04001959static inline int
1960rb_try_to_discard(struct ring_buffer_per_cpu *cpu_buffer,
1961 struct ring_buffer_event *event)
1962{
1963 unsigned long new_index, old_index;
1964 struct buffer_page *bpage;
1965 unsigned long index;
1966 unsigned long addr;
1967
1968 new_index = rb_event_index(event);
1969 old_index = new_index + rb_event_length(event);
1970 addr = (unsigned long)event;
1971 addr &= PAGE_MASK;
1972
1973 bpage = cpu_buffer->tail_page;
1974
1975 if (bpage->page == (void *)addr && rb_page_write(bpage) == old_index) {
Steven Rostedt77ae3652009-03-27 11:00:29 -04001976 unsigned long write_mask =
1977 local_read(&bpage->write) & ~RB_WRITE_MASK;
Steven Rostedtedd813bf2009-06-02 23:00:53 -04001978 /*
1979 * This is on the tail page. It is possible that
1980 * a write could come in and move the tail page
1981 * and write to the next page. That is fine
1982 * because we just shorten what is on this page.
1983 */
Steven Rostedt77ae3652009-03-27 11:00:29 -04001984 old_index += write_mask;
1985 new_index += write_mask;
Steven Rostedtedd813bf2009-06-02 23:00:53 -04001986 index = local_cmpxchg(&bpage->write, old_index, new_index);
1987 if (index == old_index)
1988 return 1;
1989 }
1990
1991 /* could not discard */
1992 return 0;
1993}
1994
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001995static int
1996rb_add_time_stamp(struct ring_buffer_per_cpu *cpu_buffer,
1997 u64 *ts, u64 *delta)
1998{
1999 struct ring_buffer_event *event;
2000 static int once;
Steven Rostedtbf41a152008-10-04 02:00:59 -04002001 int ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002002
2003 if (unlikely(*delta > (1ULL << 59) && !once++)) {
2004 printk(KERN_WARNING "Delta way too big! %llu"
2005 " ts=%llu write stamp = %llu\n",
Stephen Rothwelle2862c92008-10-27 17:43:28 +11002006 (unsigned long long)*delta,
2007 (unsigned long long)*ts,
2008 (unsigned long long)cpu_buffer->write_stamp);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002009 WARN_ON(1);
2010 }
2011
2012 /*
2013 * The delta is too big, we to add a
2014 * new timestamp.
2015 */
2016 event = __rb_reserve_next(cpu_buffer,
2017 RINGBUF_TYPE_TIME_EXTEND,
2018 RB_LEN_TIME_EXTEND,
2019 ts);
2020 if (!event)
Steven Rostedtbf41a152008-10-04 02:00:59 -04002021 return -EBUSY;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002022
Steven Rostedtbf41a152008-10-04 02:00:59 -04002023 if (PTR_ERR(event) == -EAGAIN)
2024 return -EAGAIN;
2025
2026 /* Only a commited time event can update the write stamp */
Steven Rostedtfa743952009-06-16 12:37:57 -04002027 if (rb_event_is_commit(cpu_buffer, event)) {
Steven Rostedtbf41a152008-10-04 02:00:59 -04002028 /*
Steven Rostedtfa743952009-06-16 12:37:57 -04002029 * If this is the first on the page, then it was
2030 * updated with the page itself. Try to discard it
2031 * and if we can't just make it zero.
Steven Rostedtbf41a152008-10-04 02:00:59 -04002032 */
2033 if (rb_event_index(event)) {
2034 event->time_delta = *delta & TS_MASK;
2035 event->array[0] = *delta >> TS_SHIFT;
2036 } else {
Steven Rostedtea05b572009-06-03 09:30:10 -04002037 /* try to discard, since we do not need this */
2038 if (!rb_try_to_discard(cpu_buffer, event)) {
2039 /* nope, just zero it */
2040 event->time_delta = 0;
2041 event->array[0] = 0;
2042 }
Steven Rostedtbf41a152008-10-04 02:00:59 -04002043 }
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002044 cpu_buffer->write_stamp = *ts;
Steven Rostedtbf41a152008-10-04 02:00:59 -04002045 /* let the caller know this was the commit */
2046 ret = 1;
2047 } else {
Steven Rostedtedd813bf2009-06-02 23:00:53 -04002048 /* Try to discard the event */
2049 if (!rb_try_to_discard(cpu_buffer, event)) {
2050 /* Darn, this is just wasted space */
2051 event->time_delta = 0;
2052 event->array[0] = 0;
Steven Rostedtedd813bf2009-06-02 23:00:53 -04002053 }
Steven Rostedtf57a8a12009-06-05 14:11:30 -04002054 ret = 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002055 }
2056
Steven Rostedtbf41a152008-10-04 02:00:59 -04002057 *delta = 0;
2058
2059 return ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002060}
2061
Steven Rostedtfa743952009-06-16 12:37:57 -04002062static void rb_start_commit(struct ring_buffer_per_cpu *cpu_buffer)
2063{
2064 local_inc(&cpu_buffer->committing);
2065 local_inc(&cpu_buffer->commits);
2066}
2067
2068static void rb_end_commit(struct ring_buffer_per_cpu *cpu_buffer)
2069{
2070 unsigned long commits;
2071
2072 if (RB_WARN_ON(cpu_buffer,
2073 !local_read(&cpu_buffer->committing)))
2074 return;
2075
2076 again:
2077 commits = local_read(&cpu_buffer->commits);
2078 /* synchronize with interrupts */
2079 barrier();
2080 if (local_read(&cpu_buffer->committing) == 1)
2081 rb_set_commit_to_write(cpu_buffer);
2082
2083 local_dec(&cpu_buffer->committing);
2084
2085 /* synchronize with interrupts */
2086 barrier();
2087
2088 /*
2089 * Need to account for interrupts coming in between the
2090 * updating of the commit page and the clearing of the
2091 * committing counter.
2092 */
2093 if (unlikely(local_read(&cpu_buffer->commits) != commits) &&
2094 !local_read(&cpu_buffer->committing)) {
2095 local_inc(&cpu_buffer->committing);
2096 goto again;
2097 }
2098}
2099
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002100static struct ring_buffer_event *
Steven Rostedt62f0b3e2009-09-04 14:11:34 -04002101rb_reserve_next_event(struct ring_buffer *buffer,
2102 struct ring_buffer_per_cpu *cpu_buffer,
Steven Rostedt1cd8d732009-05-11 14:08:09 -04002103 unsigned long length)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002104{
2105 struct ring_buffer_event *event;
Steven Rostedt168b6b12009-05-11 22:11:05 -04002106 u64 ts, delta = 0;
Steven Rostedtbf41a152008-10-04 02:00:59 -04002107 int commit = 0;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002108 int nr_loops = 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002109
Steven Rostedtfa743952009-06-16 12:37:57 -04002110 rb_start_commit(cpu_buffer);
2111
Steven Rostedt85bac322009-09-04 14:24:40 -04002112#ifdef CONFIG_RING_BUFFER_ALLOW_SWAP
Steven Rostedt62f0b3e2009-09-04 14:11:34 -04002113 /*
2114 * Due to the ability to swap a cpu buffer from a buffer
2115 * it is possible it was swapped before we committed.
2116 * (committing stops a swap). We check for it here and
2117 * if it happened, we have to fail the write.
2118 */
2119 barrier();
2120 if (unlikely(ACCESS_ONCE(cpu_buffer->buffer) != buffer)) {
2121 local_dec(&cpu_buffer->committing);
2122 local_dec(&cpu_buffer->commits);
2123 return NULL;
2124 }
Steven Rostedt85bac322009-09-04 14:24:40 -04002125#endif
Steven Rostedt62f0b3e2009-09-04 14:11:34 -04002126
Steven Rostedtbe957c42009-05-11 14:42:53 -04002127 length = rb_calculate_event_length(length);
Steven Rostedtbf41a152008-10-04 02:00:59 -04002128 again:
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002129 /*
2130 * We allow for interrupts to reenter here and do a trace.
2131 * If one does, it will cause this original code to loop
2132 * back here. Even with heavy interrupts happening, this
2133 * should only happen a few times in a row. If this happens
2134 * 1000 times in a row, there must be either an interrupt
2135 * storm or we have something buggy.
2136 * Bail!
2137 */
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05002138 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 1000))
Steven Rostedtfa743952009-06-16 12:37:57 -04002139 goto out_fail;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002140
Jiri Olsa6d3f1e12009-10-23 19:36:19 -04002141 ts = rb_time_stamp(cpu_buffer->buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002142
Steven Rostedtbf41a152008-10-04 02:00:59 -04002143 /*
2144 * Only the first commit can update the timestamp.
2145 * Yes there is a race here. If an interrupt comes in
2146 * just after the conditional and it traces too, then it
2147 * will also check the deltas. More than one timestamp may
2148 * also be made. But only the entry that did the actual
2149 * commit will be something other than zero.
2150 */
Steven Rostedt0f0c85f2009-05-11 16:08:00 -04002151 if (likely(cpu_buffer->tail_page == cpu_buffer->commit_page &&
2152 rb_page_write(cpu_buffer->tail_page) ==
2153 rb_commit_index(cpu_buffer))) {
Steven Rostedt168b6b12009-05-11 22:11:05 -04002154 u64 diff;
Steven Rostedtbf41a152008-10-04 02:00:59 -04002155
Steven Rostedt168b6b12009-05-11 22:11:05 -04002156 diff = ts - cpu_buffer->write_stamp;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002157
Steven Rostedt168b6b12009-05-11 22:11:05 -04002158 /* make sure this diff is calculated here */
Steven Rostedtbf41a152008-10-04 02:00:59 -04002159 barrier();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002160
Steven Rostedtbf41a152008-10-04 02:00:59 -04002161 /* Did the write stamp get updated already? */
2162 if (unlikely(ts < cpu_buffer->write_stamp))
Steven Rostedt168b6b12009-05-11 22:11:05 -04002163 goto get_event;
Steven Rostedtbf41a152008-10-04 02:00:59 -04002164
Steven Rostedt168b6b12009-05-11 22:11:05 -04002165 delta = diff;
2166 if (unlikely(test_time_stamp(delta))) {
Steven Rostedtbf41a152008-10-04 02:00:59 -04002167
2168 commit = rb_add_time_stamp(cpu_buffer, &ts, &delta);
Steven Rostedtbf41a152008-10-04 02:00:59 -04002169 if (commit == -EBUSY)
Steven Rostedtfa743952009-06-16 12:37:57 -04002170 goto out_fail;
Steven Rostedtbf41a152008-10-04 02:00:59 -04002171
2172 if (commit == -EAGAIN)
2173 goto again;
2174
2175 RB_WARN_ON(cpu_buffer, commit < 0);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002176 }
Steven Rostedt168b6b12009-05-11 22:11:05 -04002177 }
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002178
Steven Rostedt168b6b12009-05-11 22:11:05 -04002179 get_event:
Steven Rostedt1cd8d732009-05-11 14:08:09 -04002180 event = __rb_reserve_next(cpu_buffer, 0, length, &ts);
Steven Rostedt168b6b12009-05-11 22:11:05 -04002181 if (unlikely(PTR_ERR(event) == -EAGAIN))
Steven Rostedtbf41a152008-10-04 02:00:59 -04002182 goto again;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002183
Steven Rostedtfa743952009-06-16 12:37:57 -04002184 if (!event)
2185 goto out_fail;
Steven Rostedtbf41a152008-10-04 02:00:59 -04002186
Steven Rostedtfa743952009-06-16 12:37:57 -04002187 if (!rb_event_is_commit(cpu_buffer, event))
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002188 delta = 0;
2189
2190 event->time_delta = delta;
2191
2192 return event;
Steven Rostedtfa743952009-06-16 12:37:57 -04002193
2194 out_fail:
2195 rb_end_commit(cpu_buffer);
2196 return NULL;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002197}
2198
Paul Mundt1155de42009-06-25 14:30:12 +09002199#ifdef CONFIG_TRACING
2200
Steven Rostedtaa18efb2009-04-20 16:16:11 -04002201#define TRACE_RECURSIVE_DEPTH 16
Steven Rostedt261842b2009-04-16 21:41:52 -04002202
2203static int trace_recursive_lock(void)
2204{
Steven Rostedtaa18efb2009-04-20 16:16:11 -04002205 current->trace_recursion++;
Steven Rostedt261842b2009-04-16 21:41:52 -04002206
Steven Rostedtaa18efb2009-04-20 16:16:11 -04002207 if (likely(current->trace_recursion < TRACE_RECURSIVE_DEPTH))
2208 return 0;
Steven Rostedt261842b2009-04-16 21:41:52 -04002209
Steven Rostedtaa18efb2009-04-20 16:16:11 -04002210 /* Disable all tracing before we do anything else */
2211 tracing_off_permanent();
Frederic Weisbeckere057a5e2009-04-19 23:38:12 +02002212
Steven Rostedt7d7d2b82009-04-27 12:37:49 -04002213 printk_once(KERN_WARNING "Tracing recursion: depth[%ld]:"
Steven Rostedtaa18efb2009-04-20 16:16:11 -04002214 "HC[%lu]:SC[%lu]:NMI[%lu]\n",
2215 current->trace_recursion,
2216 hardirq_count() >> HARDIRQ_SHIFT,
2217 softirq_count() >> SOFTIRQ_SHIFT,
2218 in_nmi());
Frederic Weisbeckere057a5e2009-04-19 23:38:12 +02002219
Steven Rostedtaa18efb2009-04-20 16:16:11 -04002220 WARN_ON_ONCE(1);
2221 return -1;
Steven Rostedt261842b2009-04-16 21:41:52 -04002222}
2223
2224static void trace_recursive_unlock(void)
2225{
Steven Rostedtaa18efb2009-04-20 16:16:11 -04002226 WARN_ON_ONCE(!current->trace_recursion);
Steven Rostedt261842b2009-04-16 21:41:52 -04002227
Steven Rostedtaa18efb2009-04-20 16:16:11 -04002228 current->trace_recursion--;
Steven Rostedt261842b2009-04-16 21:41:52 -04002229}
2230
Paul Mundt1155de42009-06-25 14:30:12 +09002231#else
2232
2233#define trace_recursive_lock() (0)
2234#define trace_recursive_unlock() do { } while (0)
2235
2236#endif
2237
Steven Rostedtbf41a152008-10-04 02:00:59 -04002238static DEFINE_PER_CPU(int, rb_need_resched);
2239
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002240/**
2241 * ring_buffer_lock_reserve - reserve a part of the buffer
2242 * @buffer: the ring buffer to reserve from
2243 * @length: the length of the data to reserve (excluding event header)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002244 *
2245 * Returns a reseverd event on the ring buffer to copy directly to.
2246 * The user of this interface will need to get the body to write into
2247 * and can use the ring_buffer_event_data() interface.
2248 *
2249 * The length is the length of the data needed, not the event length
2250 * which also includes the event header.
2251 *
2252 * Must be paired with ring_buffer_unlock_commit, unless NULL is returned.
2253 * If NULL is returned, then nothing has been allocated or locked.
2254 */
2255struct ring_buffer_event *
Arnaldo Carvalho de Melo0a987752009-02-05 16:12:56 -02002256ring_buffer_lock_reserve(struct ring_buffer *buffer, unsigned long length)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002257{
2258 struct ring_buffer_per_cpu *cpu_buffer;
2259 struct ring_buffer_event *event;
Steven Rostedtbf41a152008-10-04 02:00:59 -04002260 int cpu, resched;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002261
Steven Rostedt033601a2008-11-21 12:41:55 -05002262 if (ring_buffer_flags != RB_BUFFERS_ON)
Steven Rostedta3583242008-11-11 15:01:42 -05002263 return NULL;
2264
Steven Rostedtbf41a152008-10-04 02:00:59 -04002265 /* If we are tracing schedule, we don't want to recurse */
Steven Rostedt182e9f52008-11-03 23:15:56 -05002266 resched = ftrace_preempt_disable();
Steven Rostedtbf41a152008-10-04 02:00:59 -04002267
Lai Jiangshan52fbe9c2010-03-08 14:50:43 +08002268 if (atomic_read(&buffer->record_disabled))
2269 goto out_nocheck;
2270
Steven Rostedt261842b2009-04-16 21:41:52 -04002271 if (trace_recursive_lock())
2272 goto out_nocheck;
2273
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002274 cpu = raw_smp_processor_id();
2275
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302276 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedtd7690412008-10-01 00:29:53 -04002277 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002278
2279 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002280
2281 if (atomic_read(&cpu_buffer->record_disabled))
Steven Rostedtd7690412008-10-01 00:29:53 -04002282 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002283
Steven Rostedtbe957c42009-05-11 14:42:53 -04002284 if (length > BUF_MAX_DATA_SIZE)
Steven Rostedtbf41a152008-10-04 02:00:59 -04002285 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002286
Steven Rostedt62f0b3e2009-09-04 14:11:34 -04002287 event = rb_reserve_next_event(buffer, cpu_buffer, length);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002288 if (!event)
Steven Rostedtd7690412008-10-01 00:29:53 -04002289 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002290
Steven Rostedtbf41a152008-10-04 02:00:59 -04002291 /*
2292 * Need to store resched state on this cpu.
2293 * Only the first needs to.
2294 */
2295
2296 if (preempt_count() == 1)
2297 per_cpu(rb_need_resched, cpu) = resched;
2298
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002299 return event;
2300
Steven Rostedtd7690412008-10-01 00:29:53 -04002301 out:
Steven Rostedt261842b2009-04-16 21:41:52 -04002302 trace_recursive_unlock();
2303
2304 out_nocheck:
Steven Rostedt182e9f52008-11-03 23:15:56 -05002305 ftrace_preempt_enable(resched);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002306 return NULL;
2307}
Robert Richterc4f50182008-12-11 16:49:22 +01002308EXPORT_SYMBOL_GPL(ring_buffer_lock_reserve);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002309
Steven Rostedta1863c22009-09-03 10:23:58 -04002310static void
2311rb_update_write_stamp(struct ring_buffer_per_cpu *cpu_buffer,
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002312 struct ring_buffer_event *event)
2313{
Steven Rostedtfa743952009-06-16 12:37:57 -04002314 /*
2315 * The event first in the commit queue updates the
2316 * time stamp.
2317 */
2318 if (rb_event_is_commit(cpu_buffer, event))
2319 cpu_buffer->write_stamp += event->time_delta;
Steven Rostedta1863c22009-09-03 10:23:58 -04002320}
Steven Rostedtbf41a152008-10-04 02:00:59 -04002321
Steven Rostedta1863c22009-09-03 10:23:58 -04002322static void rb_commit(struct ring_buffer_per_cpu *cpu_buffer,
2323 struct ring_buffer_event *event)
2324{
2325 local_inc(&cpu_buffer->entries);
2326 rb_update_write_stamp(cpu_buffer, event);
Steven Rostedtfa743952009-06-16 12:37:57 -04002327 rb_end_commit(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002328}
2329
2330/**
2331 * ring_buffer_unlock_commit - commit a reserved
2332 * @buffer: The buffer to commit to
2333 * @event: The event pointer to commit.
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002334 *
2335 * This commits the data to the ring buffer, and releases any locks held.
2336 *
2337 * Must be paired with ring_buffer_lock_reserve.
2338 */
2339int ring_buffer_unlock_commit(struct ring_buffer *buffer,
Arnaldo Carvalho de Melo0a987752009-02-05 16:12:56 -02002340 struct ring_buffer_event *event)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002341{
2342 struct ring_buffer_per_cpu *cpu_buffer;
2343 int cpu = raw_smp_processor_id();
2344
2345 cpu_buffer = buffer->buffers[cpu];
2346
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002347 rb_commit(cpu_buffer, event);
2348
Steven Rostedt261842b2009-04-16 21:41:52 -04002349 trace_recursive_unlock();
2350
Steven Rostedtbf41a152008-10-04 02:00:59 -04002351 /*
2352 * Only the last preempt count needs to restore preemption.
2353 */
Steven Rostedt182e9f52008-11-03 23:15:56 -05002354 if (preempt_count() == 1)
2355 ftrace_preempt_enable(per_cpu(rb_need_resched, cpu));
2356 else
Steven Rostedtbf41a152008-10-04 02:00:59 -04002357 preempt_enable_no_resched_notrace();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002358
2359 return 0;
2360}
Robert Richterc4f50182008-12-11 16:49:22 +01002361EXPORT_SYMBOL_GPL(ring_buffer_unlock_commit);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002362
Frederic Weisbeckerf3b9aae2009-04-19 23:39:33 +02002363static inline void rb_event_discard(struct ring_buffer_event *event)
2364{
Lai Jiangshan334d4162009-04-24 11:27:05 +08002365 /* array[0] holds the actual length for the discarded event */
2366 event->array[0] = rb_event_data_length(event) - RB_EVNT_HDR_SIZE;
2367 event->type_len = RINGBUF_TYPE_PADDING;
Frederic Weisbeckerf3b9aae2009-04-19 23:39:33 +02002368 /* time delta must be non zero */
2369 if (!event->time_delta)
2370 event->time_delta = 1;
2371}
2372
Steven Rostedta1863c22009-09-03 10:23:58 -04002373/*
2374 * Decrement the entries to the page that an event is on.
2375 * The event does not even need to exist, only the pointer
2376 * to the page it is on. This may only be called before the commit
2377 * takes place.
2378 */
2379static inline void
2380rb_decrement_entry(struct ring_buffer_per_cpu *cpu_buffer,
2381 struct ring_buffer_event *event)
2382{
2383 unsigned long addr = (unsigned long)event;
2384 struct buffer_page *bpage = cpu_buffer->commit_page;
2385 struct buffer_page *start;
2386
2387 addr &= PAGE_MASK;
2388
2389 /* Do the likely case first */
2390 if (likely(bpage->page == (void *)addr)) {
2391 local_dec(&bpage->entries);
2392 return;
2393 }
2394
2395 /*
2396 * Because the commit page may be on the reader page we
2397 * start with the next page and check the end loop there.
2398 */
2399 rb_inc_page(cpu_buffer, &bpage);
2400 start = bpage;
2401 do {
2402 if (bpage->page == (void *)addr) {
2403 local_dec(&bpage->entries);
2404 return;
2405 }
2406 rb_inc_page(cpu_buffer, &bpage);
2407 } while (bpage != start);
2408
2409 /* commit not part of this buffer?? */
2410 RB_WARN_ON(cpu_buffer, 1);
2411}
2412
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002413/**
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002414 * ring_buffer_commit_discard - discard an event that has not been committed
2415 * @buffer: the ring buffer
2416 * @event: non committed event to discard
2417 *
Steven Rostedtdc892f72009-09-03 15:33:41 -04002418 * Sometimes an event that is in the ring buffer needs to be ignored.
2419 * This function lets the user discard an event in the ring buffer
2420 * and then that event will not be read later.
2421 *
2422 * This function only works if it is called before the the item has been
2423 * committed. It will try to free the event from the ring buffer
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002424 * if another event has not been added behind it.
2425 *
2426 * If another event has been added behind it, it will set the event
2427 * up as discarded, and perform the commit.
2428 *
2429 * If this function is called, do not call ring_buffer_unlock_commit on
2430 * the event.
2431 */
2432void ring_buffer_discard_commit(struct ring_buffer *buffer,
2433 struct ring_buffer_event *event)
2434{
2435 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002436 int cpu;
2437
2438 /* The event is discarded regardless */
Frederic Weisbeckerf3b9aae2009-04-19 23:39:33 +02002439 rb_event_discard(event);
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002440
Steven Rostedtfa743952009-06-16 12:37:57 -04002441 cpu = smp_processor_id();
2442 cpu_buffer = buffer->buffers[cpu];
2443
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002444 /*
2445 * This must only be called if the event has not been
2446 * committed yet. Thus we can assume that preemption
2447 * is still disabled.
2448 */
Steven Rostedtfa743952009-06-16 12:37:57 -04002449 RB_WARN_ON(buffer, !local_read(&cpu_buffer->committing));
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002450
Steven Rostedta1863c22009-09-03 10:23:58 -04002451 rb_decrement_entry(cpu_buffer, event);
Steven Rostedt0f2541d2009-08-05 12:02:48 -04002452 if (rb_try_to_discard(cpu_buffer, event))
Steven Rostedtedd813bf2009-06-02 23:00:53 -04002453 goto out;
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002454
2455 /*
2456 * The commit is still visible by the reader, so we
Steven Rostedta1863c22009-09-03 10:23:58 -04002457 * must still update the timestamp.
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002458 */
Steven Rostedta1863c22009-09-03 10:23:58 -04002459 rb_update_write_stamp(cpu_buffer, event);
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002460 out:
Steven Rostedtfa743952009-06-16 12:37:57 -04002461 rb_end_commit(cpu_buffer);
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002462
Frederic Weisbeckerf3b9aae2009-04-19 23:39:33 +02002463 trace_recursive_unlock();
2464
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002465 /*
2466 * Only the last preempt count needs to restore preemption.
2467 */
2468 if (preempt_count() == 1)
2469 ftrace_preempt_enable(per_cpu(rb_need_resched, cpu));
2470 else
2471 preempt_enable_no_resched_notrace();
2472
2473}
2474EXPORT_SYMBOL_GPL(ring_buffer_discard_commit);
2475
2476/**
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002477 * ring_buffer_write - write data to the buffer without reserving
2478 * @buffer: The ring buffer to write to.
2479 * @length: The length of the data being written (excluding the event header)
2480 * @data: The data to write to the buffer.
2481 *
2482 * This is like ring_buffer_lock_reserve and ring_buffer_unlock_commit as
2483 * one function. If you already have the data to write to the buffer, it
2484 * may be easier to simply call this function.
2485 *
2486 * Note, like ring_buffer_lock_reserve, the length is the length of the data
2487 * and not the length of the event which would hold the header.
2488 */
2489int ring_buffer_write(struct ring_buffer *buffer,
2490 unsigned long length,
2491 void *data)
2492{
2493 struct ring_buffer_per_cpu *cpu_buffer;
2494 struct ring_buffer_event *event;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002495 void *body;
2496 int ret = -EBUSY;
Steven Rostedtbf41a152008-10-04 02:00:59 -04002497 int cpu, resched;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002498
Steven Rostedt033601a2008-11-21 12:41:55 -05002499 if (ring_buffer_flags != RB_BUFFERS_ON)
Steven Rostedta3583242008-11-11 15:01:42 -05002500 return -EBUSY;
2501
Steven Rostedt182e9f52008-11-03 23:15:56 -05002502 resched = ftrace_preempt_disable();
Steven Rostedtbf41a152008-10-04 02:00:59 -04002503
Lai Jiangshan52fbe9c2010-03-08 14:50:43 +08002504 if (atomic_read(&buffer->record_disabled))
2505 goto out;
2506
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002507 cpu = raw_smp_processor_id();
2508
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302509 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedtd7690412008-10-01 00:29:53 -04002510 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002511
2512 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002513
2514 if (atomic_read(&cpu_buffer->record_disabled))
2515 goto out;
2516
Steven Rostedtbe957c42009-05-11 14:42:53 -04002517 if (length > BUF_MAX_DATA_SIZE)
2518 goto out;
2519
Steven Rostedt62f0b3e2009-09-04 14:11:34 -04002520 event = rb_reserve_next_event(buffer, cpu_buffer, length);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002521 if (!event)
2522 goto out;
2523
2524 body = rb_event_data(event);
2525
2526 memcpy(body, data, length);
2527
2528 rb_commit(cpu_buffer, event);
2529
2530 ret = 0;
2531 out:
Steven Rostedt182e9f52008-11-03 23:15:56 -05002532 ftrace_preempt_enable(resched);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002533
2534 return ret;
2535}
Robert Richterc4f50182008-12-11 16:49:22 +01002536EXPORT_SYMBOL_GPL(ring_buffer_write);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002537
Andrew Morton34a148b2009-01-09 12:27:09 -08002538static int rb_per_cpu_empty(struct ring_buffer_per_cpu *cpu_buffer)
Steven Rostedtbf41a152008-10-04 02:00:59 -04002539{
2540 struct buffer_page *reader = cpu_buffer->reader_page;
Steven Rostedt77ae3652009-03-27 11:00:29 -04002541 struct buffer_page *head = rb_set_head_page(cpu_buffer);
Steven Rostedtbf41a152008-10-04 02:00:59 -04002542 struct buffer_page *commit = cpu_buffer->commit_page;
2543
Steven Rostedt77ae3652009-03-27 11:00:29 -04002544 /* In case of error, head will be NULL */
2545 if (unlikely(!head))
2546 return 1;
2547
Steven Rostedtbf41a152008-10-04 02:00:59 -04002548 return reader->read == rb_page_commit(reader) &&
2549 (commit == reader ||
2550 (commit == head &&
2551 head->read == rb_page_commit(commit)));
2552}
2553
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002554/**
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002555 * ring_buffer_record_disable - stop all writes into the buffer
2556 * @buffer: The ring buffer to stop writes to.
2557 *
2558 * This prevents all writes to the buffer. Any attempt to write
2559 * to the buffer after this will fail and return NULL.
2560 *
2561 * The caller should call synchronize_sched() after this.
2562 */
2563void ring_buffer_record_disable(struct ring_buffer *buffer)
2564{
2565 atomic_inc(&buffer->record_disabled);
2566}
Robert Richterc4f50182008-12-11 16:49:22 +01002567EXPORT_SYMBOL_GPL(ring_buffer_record_disable);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002568
2569/**
2570 * ring_buffer_record_enable - enable writes to the buffer
2571 * @buffer: The ring buffer to enable writes
2572 *
2573 * Note, multiple disables will need the same number of enables
Adam Buchbinderc41b20e2009-12-11 16:35:39 -05002574 * to truly enable the writing (much like preempt_disable).
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002575 */
2576void ring_buffer_record_enable(struct ring_buffer *buffer)
2577{
2578 atomic_dec(&buffer->record_disabled);
2579}
Robert Richterc4f50182008-12-11 16:49:22 +01002580EXPORT_SYMBOL_GPL(ring_buffer_record_enable);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002581
2582/**
2583 * ring_buffer_record_disable_cpu - stop all writes into the cpu_buffer
2584 * @buffer: The ring buffer to stop writes to.
2585 * @cpu: The CPU buffer to stop
2586 *
2587 * This prevents all writes to the buffer. Any attempt to write
2588 * to the buffer after this will fail and return NULL.
2589 *
2590 * The caller should call synchronize_sched() after this.
2591 */
2592void ring_buffer_record_disable_cpu(struct ring_buffer *buffer, int cpu)
2593{
2594 struct ring_buffer_per_cpu *cpu_buffer;
2595
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302596 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04002597 return;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002598
2599 cpu_buffer = buffer->buffers[cpu];
2600 atomic_inc(&cpu_buffer->record_disabled);
2601}
Robert Richterc4f50182008-12-11 16:49:22 +01002602EXPORT_SYMBOL_GPL(ring_buffer_record_disable_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002603
2604/**
2605 * ring_buffer_record_enable_cpu - enable writes to the buffer
2606 * @buffer: The ring buffer to enable writes
2607 * @cpu: The CPU to enable.
2608 *
2609 * Note, multiple disables will need the same number of enables
Adam Buchbinderc41b20e2009-12-11 16:35:39 -05002610 * to truly enable the writing (much like preempt_disable).
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002611 */
2612void ring_buffer_record_enable_cpu(struct ring_buffer *buffer, int cpu)
2613{
2614 struct ring_buffer_per_cpu *cpu_buffer;
2615
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302616 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04002617 return;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002618
2619 cpu_buffer = buffer->buffers[cpu];
2620 atomic_dec(&cpu_buffer->record_disabled);
2621}
Robert Richterc4f50182008-12-11 16:49:22 +01002622EXPORT_SYMBOL_GPL(ring_buffer_record_enable_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002623
2624/**
2625 * ring_buffer_entries_cpu - get the number of entries in a cpu buffer
2626 * @buffer: The ring buffer
2627 * @cpu: The per CPU buffer to get the entries from.
2628 */
2629unsigned long ring_buffer_entries_cpu(struct ring_buffer *buffer, int cpu)
2630{
2631 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedt8aabee52009-03-12 13:13:49 -04002632 unsigned long ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002633
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302634 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04002635 return 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002636
2637 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt77ae3652009-03-27 11:00:29 -04002638 ret = (local_read(&cpu_buffer->entries) - local_read(&cpu_buffer->overrun))
Steven Rostedte4906ef2009-04-30 20:49:44 -04002639 - cpu_buffer->read;
Steven Rostedt554f7862009-03-11 22:00:13 -04002640
2641 return ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002642}
Robert Richterc4f50182008-12-11 16:49:22 +01002643EXPORT_SYMBOL_GPL(ring_buffer_entries_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002644
2645/**
2646 * ring_buffer_overrun_cpu - get the number of overruns in a cpu_buffer
2647 * @buffer: The ring buffer
2648 * @cpu: The per CPU buffer to get the number of overruns from
2649 */
2650unsigned long ring_buffer_overrun_cpu(struct ring_buffer *buffer, int cpu)
2651{
2652 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedt8aabee52009-03-12 13:13:49 -04002653 unsigned long ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002654
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302655 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04002656 return 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002657
2658 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt77ae3652009-03-27 11:00:29 -04002659 ret = local_read(&cpu_buffer->overrun);
Steven Rostedt554f7862009-03-11 22:00:13 -04002660
2661 return ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002662}
Robert Richterc4f50182008-12-11 16:49:22 +01002663EXPORT_SYMBOL_GPL(ring_buffer_overrun_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002664
2665/**
Steven Rostedtf0d2c682009-04-29 13:43:37 -04002666 * ring_buffer_commit_overrun_cpu - get the number of overruns caused by commits
2667 * @buffer: The ring buffer
2668 * @cpu: The per CPU buffer to get the number of overruns from
2669 */
2670unsigned long
2671ring_buffer_commit_overrun_cpu(struct ring_buffer *buffer, int cpu)
2672{
2673 struct ring_buffer_per_cpu *cpu_buffer;
2674 unsigned long ret;
2675
2676 if (!cpumask_test_cpu(cpu, buffer->cpumask))
2677 return 0;
2678
2679 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt77ae3652009-03-27 11:00:29 -04002680 ret = local_read(&cpu_buffer->commit_overrun);
Steven Rostedtf0d2c682009-04-29 13:43:37 -04002681
2682 return ret;
2683}
2684EXPORT_SYMBOL_GPL(ring_buffer_commit_overrun_cpu);
2685
2686/**
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002687 * ring_buffer_entries - get the number of entries in a buffer
2688 * @buffer: The ring buffer
2689 *
2690 * Returns the total number of entries in the ring buffer
2691 * (all CPU entries)
2692 */
2693unsigned long ring_buffer_entries(struct ring_buffer *buffer)
2694{
2695 struct ring_buffer_per_cpu *cpu_buffer;
2696 unsigned long entries = 0;
2697 int cpu;
2698
2699 /* if you care about this being correct, lock the buffer */
2700 for_each_buffer_cpu(buffer, cpu) {
2701 cpu_buffer = buffer->buffers[cpu];
Steven Rostedte4906ef2009-04-30 20:49:44 -04002702 entries += (local_read(&cpu_buffer->entries) -
Steven Rostedt77ae3652009-03-27 11:00:29 -04002703 local_read(&cpu_buffer->overrun)) - cpu_buffer->read;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002704 }
2705
2706 return entries;
2707}
Robert Richterc4f50182008-12-11 16:49:22 +01002708EXPORT_SYMBOL_GPL(ring_buffer_entries);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002709
2710/**
Jiri Olsa67b394f2009-10-23 19:36:18 -04002711 * ring_buffer_overruns - get the number of overruns in buffer
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002712 * @buffer: The ring buffer
2713 *
2714 * Returns the total number of overruns in the ring buffer
2715 * (all CPU entries)
2716 */
2717unsigned long ring_buffer_overruns(struct ring_buffer *buffer)
2718{
2719 struct ring_buffer_per_cpu *cpu_buffer;
2720 unsigned long overruns = 0;
2721 int cpu;
2722
2723 /* if you care about this being correct, lock the buffer */
2724 for_each_buffer_cpu(buffer, cpu) {
2725 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt77ae3652009-03-27 11:00:29 -04002726 overruns += local_read(&cpu_buffer->overrun);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002727 }
2728
2729 return overruns;
2730}
Robert Richterc4f50182008-12-11 16:49:22 +01002731EXPORT_SYMBOL_GPL(ring_buffer_overruns);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002732
Steven Rostedt642edba2008-11-12 00:01:26 -05002733static void rb_iter_reset(struct ring_buffer_iter *iter)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002734{
2735 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
2736
Steven Rostedtd7690412008-10-01 00:29:53 -04002737 /* Iterator usage is expected to have record disabled */
2738 if (list_empty(&cpu_buffer->reader_page->list)) {
Steven Rostedt77ae3652009-03-27 11:00:29 -04002739 iter->head_page = rb_set_head_page(cpu_buffer);
2740 if (unlikely(!iter->head_page))
2741 return;
2742 iter->head = iter->head_page->read;
Steven Rostedtd7690412008-10-01 00:29:53 -04002743 } else {
2744 iter->head_page = cpu_buffer->reader_page;
Steven Rostedt6f807ac2008-10-04 02:00:58 -04002745 iter->head = cpu_buffer->reader_page->read;
Steven Rostedtd7690412008-10-01 00:29:53 -04002746 }
2747 if (iter->head)
2748 iter->read_stamp = cpu_buffer->read_stamp;
2749 else
Steven Rostedtabc9b562008-12-02 15:34:06 -05002750 iter->read_stamp = iter->head_page->page->time_stamp;
Steven Rostedt492a74f2010-01-25 15:17:47 -05002751 iter->cache_reader_page = cpu_buffer->reader_page;
2752 iter->cache_read = cpu_buffer->read;
Steven Rostedt642edba2008-11-12 00:01:26 -05002753}
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002754
Steven Rostedt642edba2008-11-12 00:01:26 -05002755/**
2756 * ring_buffer_iter_reset - reset an iterator
2757 * @iter: The iterator to reset
2758 *
2759 * Resets the iterator, so that it will start from the beginning
2760 * again.
2761 */
2762void ring_buffer_iter_reset(struct ring_buffer_iter *iter)
2763{
Steven Rostedt554f7862009-03-11 22:00:13 -04002764 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedt642edba2008-11-12 00:01:26 -05002765 unsigned long flags;
2766
Steven Rostedt554f7862009-03-11 22:00:13 -04002767 if (!iter)
2768 return;
2769
2770 cpu_buffer = iter->cpu_buffer;
2771
Steven Rostedt642edba2008-11-12 00:01:26 -05002772 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
2773 rb_iter_reset(iter);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002774 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002775}
Robert Richterc4f50182008-12-11 16:49:22 +01002776EXPORT_SYMBOL_GPL(ring_buffer_iter_reset);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002777
2778/**
2779 * ring_buffer_iter_empty - check if an iterator has no more to read
2780 * @iter: The iterator to check
2781 */
2782int ring_buffer_iter_empty(struct ring_buffer_iter *iter)
2783{
2784 struct ring_buffer_per_cpu *cpu_buffer;
2785
2786 cpu_buffer = iter->cpu_buffer;
2787
Steven Rostedtbf41a152008-10-04 02:00:59 -04002788 return iter->head_page == cpu_buffer->commit_page &&
2789 iter->head == rb_commit_index(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002790}
Robert Richterc4f50182008-12-11 16:49:22 +01002791EXPORT_SYMBOL_GPL(ring_buffer_iter_empty);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002792
2793static void
2794rb_update_read_stamp(struct ring_buffer_per_cpu *cpu_buffer,
2795 struct ring_buffer_event *event)
2796{
2797 u64 delta;
2798
Lai Jiangshan334d4162009-04-24 11:27:05 +08002799 switch (event->type_len) {
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002800 case RINGBUF_TYPE_PADDING:
2801 return;
2802
2803 case RINGBUF_TYPE_TIME_EXTEND:
2804 delta = event->array[0];
2805 delta <<= TS_SHIFT;
2806 delta += event->time_delta;
2807 cpu_buffer->read_stamp += delta;
2808 return;
2809
2810 case RINGBUF_TYPE_TIME_STAMP:
2811 /* FIXME: not implemented */
2812 return;
2813
2814 case RINGBUF_TYPE_DATA:
2815 cpu_buffer->read_stamp += event->time_delta;
2816 return;
2817
2818 default:
2819 BUG();
2820 }
2821 return;
2822}
2823
2824static void
2825rb_update_iter_read_stamp(struct ring_buffer_iter *iter,
2826 struct ring_buffer_event *event)
2827{
2828 u64 delta;
2829
Lai Jiangshan334d4162009-04-24 11:27:05 +08002830 switch (event->type_len) {
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002831 case RINGBUF_TYPE_PADDING:
2832 return;
2833
2834 case RINGBUF_TYPE_TIME_EXTEND:
2835 delta = event->array[0];
2836 delta <<= TS_SHIFT;
2837 delta += event->time_delta;
2838 iter->read_stamp += delta;
2839 return;
2840
2841 case RINGBUF_TYPE_TIME_STAMP:
2842 /* FIXME: not implemented */
2843 return;
2844
2845 case RINGBUF_TYPE_DATA:
2846 iter->read_stamp += event->time_delta;
2847 return;
2848
2849 default:
2850 BUG();
2851 }
2852 return;
2853}
2854
Steven Rostedtd7690412008-10-01 00:29:53 -04002855static struct buffer_page *
2856rb_get_reader_page(struct ring_buffer_per_cpu *cpu_buffer)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002857{
Steven Rostedtd7690412008-10-01 00:29:53 -04002858 struct buffer_page *reader = NULL;
Steven Rostedt66a8cb92010-03-31 13:21:56 -04002859 unsigned long overwrite;
Steven Rostedtd7690412008-10-01 00:29:53 -04002860 unsigned long flags;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002861 int nr_loops = 0;
Steven Rostedt77ae3652009-03-27 11:00:29 -04002862 int ret;
Steven Rostedtd7690412008-10-01 00:29:53 -04002863
Steven Rostedt3e03fb72008-11-06 00:09:43 -05002864 local_irq_save(flags);
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01002865 arch_spin_lock(&cpu_buffer->lock);
Steven Rostedtd7690412008-10-01 00:29:53 -04002866
2867 again:
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002868 /*
2869 * This should normally only loop twice. But because the
2870 * start of the reader inserts an empty page, it causes
2871 * a case where we will loop three times. There should be no
2872 * reason to loop four times (that I know of).
2873 */
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05002874 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 3)) {
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002875 reader = NULL;
2876 goto out;
2877 }
2878
Steven Rostedtd7690412008-10-01 00:29:53 -04002879 reader = cpu_buffer->reader_page;
2880
2881 /* If there's more to read, return this page */
Steven Rostedtbf41a152008-10-04 02:00:59 -04002882 if (cpu_buffer->reader_page->read < rb_page_size(reader))
Steven Rostedtd7690412008-10-01 00:29:53 -04002883 goto out;
2884
2885 /* Never should we have an index greater than the size */
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05002886 if (RB_WARN_ON(cpu_buffer,
2887 cpu_buffer->reader_page->read > rb_page_size(reader)))
2888 goto out;
Steven Rostedtd7690412008-10-01 00:29:53 -04002889
2890 /* check if we caught up to the tail */
2891 reader = NULL;
Steven Rostedtbf41a152008-10-04 02:00:59 -04002892 if (cpu_buffer->commit_page == cpu_buffer->reader_page)
Steven Rostedtd7690412008-10-01 00:29:53 -04002893 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002894
2895 /*
Steven Rostedtd7690412008-10-01 00:29:53 -04002896 * Reset the reader page to size zero.
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002897 */
Steven Rostedt77ae3652009-03-27 11:00:29 -04002898 local_set(&cpu_buffer->reader_page->write, 0);
2899 local_set(&cpu_buffer->reader_page->entries, 0);
2900 local_set(&cpu_buffer->reader_page->page->commit, 0);
Steven Rostedtff0ff842010-03-31 22:11:42 -04002901 cpu_buffer->reader_page->real_end = 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002902
Steven Rostedt77ae3652009-03-27 11:00:29 -04002903 spin:
2904 /*
2905 * Splice the empty reader page into the list around the head.
2906 */
2907 reader = rb_set_head_page(cpu_buffer);
Steven Rostedt0e1ff5d2010-01-06 20:40:44 -05002908 cpu_buffer->reader_page->list.next = rb_list_head(reader->list.next);
Steven Rostedtd7690412008-10-01 00:29:53 -04002909 cpu_buffer->reader_page->list.prev = reader->list.prev;
Steven Rostedtbf41a152008-10-04 02:00:59 -04002910
Steven Rostedt3adc54f2009-03-30 15:32:01 -04002911 /*
2912 * cpu_buffer->pages just needs to point to the buffer, it
2913 * has no specific buffer page to point to. Lets move it out
2914 * of our way so we don't accidently swap it.
2915 */
2916 cpu_buffer->pages = reader->list.prev;
2917
Steven Rostedt77ae3652009-03-27 11:00:29 -04002918 /* The reader page will be pointing to the new head */
2919 rb_set_list_to_head(cpu_buffer, &cpu_buffer->reader_page->list);
Steven Rostedtd7690412008-10-01 00:29:53 -04002920
2921 /*
Steven Rostedt66a8cb92010-03-31 13:21:56 -04002922 * We want to make sure we read the overruns after we set up our
2923 * pointers to the next object. The writer side does a
2924 * cmpxchg to cross pages which acts as the mb on the writer
2925 * side. Note, the reader will constantly fail the swap
2926 * while the writer is updating the pointers, so this
2927 * guarantees that the overwrite recorded here is the one we
2928 * want to compare with the last_overrun.
2929 */
2930 smp_mb();
2931 overwrite = local_read(&(cpu_buffer->overrun));
2932
2933 /*
Steven Rostedt77ae3652009-03-27 11:00:29 -04002934 * Here's the tricky part.
2935 *
2936 * We need to move the pointer past the header page.
2937 * But we can only do that if a writer is not currently
2938 * moving it. The page before the header page has the
2939 * flag bit '1' set if it is pointing to the page we want.
2940 * but if the writer is in the process of moving it
2941 * than it will be '2' or already moved '0'.
Steven Rostedtd7690412008-10-01 00:29:53 -04002942 */
Steven Rostedtd7690412008-10-01 00:29:53 -04002943
Steven Rostedt77ae3652009-03-27 11:00:29 -04002944 ret = rb_head_page_replace(reader, cpu_buffer->reader_page);
2945
2946 /*
2947 * If we did not convert it, then we must try again.
2948 */
2949 if (!ret)
2950 goto spin;
2951
2952 /*
2953 * Yeah! We succeeded in replacing the page.
2954 *
2955 * Now make the new head point back to the reader page.
2956 */
David Sharp5ded3dc2010-01-06 17:12:07 -08002957 rb_list_head(reader->list.next)->prev = &cpu_buffer->reader_page->list;
Steven Rostedt77ae3652009-03-27 11:00:29 -04002958 rb_inc_page(cpu_buffer, &cpu_buffer->head_page);
Steven Rostedtd7690412008-10-01 00:29:53 -04002959
2960 /* Finally update the reader page to the new head */
2961 cpu_buffer->reader_page = reader;
2962 rb_reset_reader_page(cpu_buffer);
2963
Steven Rostedt66a8cb92010-03-31 13:21:56 -04002964 if (overwrite != cpu_buffer->last_overrun) {
2965 cpu_buffer->lost_events = overwrite - cpu_buffer->last_overrun;
2966 cpu_buffer->last_overrun = overwrite;
2967 }
2968
Steven Rostedtd7690412008-10-01 00:29:53 -04002969 goto again;
2970
2971 out:
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01002972 arch_spin_unlock(&cpu_buffer->lock);
Steven Rostedt3e03fb72008-11-06 00:09:43 -05002973 local_irq_restore(flags);
Steven Rostedtd7690412008-10-01 00:29:53 -04002974
2975 return reader;
2976}
2977
2978static void rb_advance_reader(struct ring_buffer_per_cpu *cpu_buffer)
2979{
2980 struct ring_buffer_event *event;
2981 struct buffer_page *reader;
2982 unsigned length;
2983
2984 reader = rb_get_reader_page(cpu_buffer);
2985
2986 /* This function should not be called when buffer is empty */
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05002987 if (RB_WARN_ON(cpu_buffer, !reader))
2988 return;
Steven Rostedtd7690412008-10-01 00:29:53 -04002989
2990 event = rb_reader_event(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002991
Steven Rostedta1863c22009-09-03 10:23:58 -04002992 if (event->type_len <= RINGBUF_TYPE_DATA_TYPE_LEN_MAX)
Steven Rostedte4906ef2009-04-30 20:49:44 -04002993 cpu_buffer->read++;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002994
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002995 rb_update_read_stamp(cpu_buffer, event);
2996
Steven Rostedtd7690412008-10-01 00:29:53 -04002997 length = rb_event_length(event);
Steven Rostedt6f807ac2008-10-04 02:00:58 -04002998 cpu_buffer->reader_page->read += length;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002999}
3000
3001static void rb_advance_iter(struct ring_buffer_iter *iter)
3002{
3003 struct ring_buffer *buffer;
3004 struct ring_buffer_per_cpu *cpu_buffer;
3005 struct ring_buffer_event *event;
3006 unsigned length;
3007
3008 cpu_buffer = iter->cpu_buffer;
3009 buffer = cpu_buffer->buffer;
3010
3011 /*
3012 * Check if we are at the end of the buffer.
3013 */
Steven Rostedtbf41a152008-10-04 02:00:59 -04003014 if (iter->head >= rb_page_size(iter->head_page)) {
Steven Rostedtea05b572009-06-03 09:30:10 -04003015 /* discarded commits can make the page empty */
3016 if (iter->head_page == cpu_buffer->commit_page)
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05003017 return;
Steven Rostedtd7690412008-10-01 00:29:53 -04003018 rb_inc_iter(iter);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003019 return;
3020 }
3021
3022 event = rb_iter_head_event(iter);
3023
3024 length = rb_event_length(event);
3025
3026 /*
3027 * This should not be called to advance the header if we are
3028 * at the tail of the buffer.
3029 */
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05003030 if (RB_WARN_ON(cpu_buffer,
Steven Rostedtf536aaf2008-11-10 23:07:30 -05003031 (iter->head_page == cpu_buffer->commit_page) &&
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05003032 (iter->head + length > rb_commit_index(cpu_buffer))))
3033 return;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003034
3035 rb_update_iter_read_stamp(iter, event);
3036
3037 iter->head += length;
3038
3039 /* check for end of page padding */
Steven Rostedtbf41a152008-10-04 02:00:59 -04003040 if ((iter->head >= rb_page_size(iter->head_page)) &&
3041 (iter->head_page != cpu_buffer->commit_page))
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003042 rb_advance_iter(iter);
3043}
3044
Steven Rostedt66a8cb92010-03-31 13:21:56 -04003045static int rb_lost_events(struct ring_buffer_per_cpu *cpu_buffer)
3046{
3047 return cpu_buffer->lost_events;
3048}
3049
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003050static struct ring_buffer_event *
Steven Rostedt66a8cb92010-03-31 13:21:56 -04003051rb_buffer_peek(struct ring_buffer_per_cpu *cpu_buffer, u64 *ts,
3052 unsigned long *lost_events)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003053{
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003054 struct ring_buffer_event *event;
Steven Rostedtd7690412008-10-01 00:29:53 -04003055 struct buffer_page *reader;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003056 int nr_loops = 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003057
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003058 again:
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003059 /*
3060 * We repeat when a timestamp is encountered. It is possible
3061 * to get multiple timestamps from an interrupt entering just
Steven Rostedtea05b572009-06-03 09:30:10 -04003062 * as one timestamp is about to be written, or from discarded
3063 * commits. The most that we can have is the number on a single page.
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003064 */
Steven Rostedtea05b572009-06-03 09:30:10 -04003065 if (RB_WARN_ON(cpu_buffer, ++nr_loops > RB_TIMESTAMPS_PER_PAGE))
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003066 return NULL;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003067
Steven Rostedtd7690412008-10-01 00:29:53 -04003068 reader = rb_get_reader_page(cpu_buffer);
3069 if (!reader)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003070 return NULL;
3071
Steven Rostedtd7690412008-10-01 00:29:53 -04003072 event = rb_reader_event(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003073
Lai Jiangshan334d4162009-04-24 11:27:05 +08003074 switch (event->type_len) {
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003075 case RINGBUF_TYPE_PADDING:
Tom Zanussi2d622712009-03-22 03:30:49 -05003076 if (rb_null_event(event))
3077 RB_WARN_ON(cpu_buffer, 1);
3078 /*
3079 * Because the writer could be discarding every
3080 * event it creates (which would probably be bad)
3081 * if we were to go back to "again" then we may never
3082 * catch up, and will trigger the warn on, or lock
3083 * the box. Return the padding, and we will release
3084 * the current locks, and try again.
3085 */
Tom Zanussi2d622712009-03-22 03:30:49 -05003086 return event;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003087
3088 case RINGBUF_TYPE_TIME_EXTEND:
3089 /* Internal data, OK to advance */
Steven Rostedtd7690412008-10-01 00:29:53 -04003090 rb_advance_reader(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003091 goto again;
3092
3093 case RINGBUF_TYPE_TIME_STAMP:
3094 /* FIXME: not implemented */
Steven Rostedtd7690412008-10-01 00:29:53 -04003095 rb_advance_reader(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003096 goto again;
3097
3098 case RINGBUF_TYPE_DATA:
3099 if (ts) {
3100 *ts = cpu_buffer->read_stamp + event->time_delta;
Robert Richterd8eeb2d2009-07-31 14:58:04 +02003101 ring_buffer_normalize_time_stamp(cpu_buffer->buffer,
Steven Rostedt37886f62009-03-17 17:22:06 -04003102 cpu_buffer->cpu, ts);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003103 }
Steven Rostedt66a8cb92010-03-31 13:21:56 -04003104 if (lost_events)
3105 *lost_events = rb_lost_events(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003106 return event;
3107
3108 default:
3109 BUG();
3110 }
3111
3112 return NULL;
3113}
Robert Richterc4f50182008-12-11 16:49:22 +01003114EXPORT_SYMBOL_GPL(ring_buffer_peek);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003115
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003116static struct ring_buffer_event *
3117rb_iter_peek(struct ring_buffer_iter *iter, u64 *ts)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003118{
3119 struct ring_buffer *buffer;
3120 struct ring_buffer_per_cpu *cpu_buffer;
3121 struct ring_buffer_event *event;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003122 int nr_loops = 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003123
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003124 cpu_buffer = iter->cpu_buffer;
3125 buffer = cpu_buffer->buffer;
3126
Steven Rostedt492a74f2010-01-25 15:17:47 -05003127 /*
3128 * Check if someone performed a consuming read to
3129 * the buffer. A consuming read invalidates the iterator
3130 * and we need to reset the iterator in this case.
3131 */
3132 if (unlikely(iter->cache_read != cpu_buffer->read ||
3133 iter->cache_reader_page != cpu_buffer->reader_page))
3134 rb_iter_reset(iter);
3135
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003136 again:
Steven Rostedt3c05d742010-01-26 16:14:08 -05003137 if (ring_buffer_iter_empty(iter))
3138 return NULL;
3139
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003140 /*
Steven Rostedtea05b572009-06-03 09:30:10 -04003141 * We repeat when a timestamp is encountered.
3142 * We can get multiple timestamps by nested interrupts or also
3143 * if filtering is on (discarding commits). Since discarding
3144 * commits can be frequent we can get a lot of timestamps.
3145 * But we limit them by not adding timestamps if they begin
3146 * at the start of a page.
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003147 */
Steven Rostedtea05b572009-06-03 09:30:10 -04003148 if (RB_WARN_ON(cpu_buffer, ++nr_loops > RB_TIMESTAMPS_PER_PAGE))
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003149 return NULL;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003150
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003151 if (rb_per_cpu_empty(cpu_buffer))
3152 return NULL;
3153
Steven Rostedt3c05d742010-01-26 16:14:08 -05003154 if (iter->head >= local_read(&iter->head_page->page->commit)) {
3155 rb_inc_iter(iter);
3156 goto again;
3157 }
3158
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003159 event = rb_iter_head_event(iter);
3160
Lai Jiangshan334d4162009-04-24 11:27:05 +08003161 switch (event->type_len) {
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003162 case RINGBUF_TYPE_PADDING:
Tom Zanussi2d622712009-03-22 03:30:49 -05003163 if (rb_null_event(event)) {
3164 rb_inc_iter(iter);
3165 goto again;
3166 }
3167 rb_advance_iter(iter);
3168 return event;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003169
3170 case RINGBUF_TYPE_TIME_EXTEND:
3171 /* Internal data, OK to advance */
3172 rb_advance_iter(iter);
3173 goto again;
3174
3175 case RINGBUF_TYPE_TIME_STAMP:
3176 /* FIXME: not implemented */
3177 rb_advance_iter(iter);
3178 goto again;
3179
3180 case RINGBUF_TYPE_DATA:
3181 if (ts) {
3182 *ts = iter->read_stamp + event->time_delta;
Steven Rostedt37886f62009-03-17 17:22:06 -04003183 ring_buffer_normalize_time_stamp(buffer,
3184 cpu_buffer->cpu, ts);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003185 }
3186 return event;
3187
3188 default:
3189 BUG();
3190 }
3191
3192 return NULL;
3193}
Robert Richterc4f50182008-12-11 16:49:22 +01003194EXPORT_SYMBOL_GPL(ring_buffer_iter_peek);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003195
Steven Rostedt8d707e82009-06-16 21:22:48 -04003196static inline int rb_ok_to_lock(void)
3197{
3198 /*
3199 * If an NMI die dumps out the content of the ring buffer
3200 * do not grab locks. We also permanently disable the ring
3201 * buffer too. A one time deal is all you get from reading
3202 * the ring buffer from an NMI.
3203 */
Steven Rostedt464e85e2009-08-05 15:26:37 -04003204 if (likely(!in_nmi()))
Steven Rostedt8d707e82009-06-16 21:22:48 -04003205 return 1;
3206
3207 tracing_off_permanent();
3208 return 0;
3209}
3210
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003211/**
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003212 * ring_buffer_peek - peek at the next event to be read
3213 * @buffer: The ring buffer to read
3214 * @cpu: The cpu to peak at
3215 * @ts: The timestamp counter of this event.
Steven Rostedt66a8cb92010-03-31 13:21:56 -04003216 * @lost_events: a variable to store if events were lost (may be NULL)
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003217 *
3218 * This will return the event that will be read next, but does
3219 * not consume the data.
3220 */
3221struct ring_buffer_event *
Steven Rostedt66a8cb92010-03-31 13:21:56 -04003222ring_buffer_peek(struct ring_buffer *buffer, int cpu, u64 *ts,
3223 unsigned long *lost_events)
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003224{
3225 struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
Steven Rostedt8aabee52009-03-12 13:13:49 -04003226 struct ring_buffer_event *event;
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003227 unsigned long flags;
Steven Rostedt8d707e82009-06-16 21:22:48 -04003228 int dolock;
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003229
Steven Rostedt554f7862009-03-11 22:00:13 -04003230 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04003231 return NULL;
Steven Rostedt554f7862009-03-11 22:00:13 -04003232
Steven Rostedt8d707e82009-06-16 21:22:48 -04003233 dolock = rb_ok_to_lock();
Tom Zanussi2d622712009-03-22 03:30:49 -05003234 again:
Steven Rostedt8d707e82009-06-16 21:22:48 -04003235 local_irq_save(flags);
3236 if (dolock)
3237 spin_lock(&cpu_buffer->reader_lock);
Steven Rostedt66a8cb92010-03-31 13:21:56 -04003238 event = rb_buffer_peek(cpu_buffer, ts, lost_events);
Robert Richter469535a2009-07-30 19:19:18 +02003239 if (event && event->type_len == RINGBUF_TYPE_PADDING)
3240 rb_advance_reader(cpu_buffer);
Steven Rostedt8d707e82009-06-16 21:22:48 -04003241 if (dolock)
3242 spin_unlock(&cpu_buffer->reader_lock);
3243 local_irq_restore(flags);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003244
Steven Rostedt1b959e12009-09-03 10:12:13 -04003245 if (event && event->type_len == RINGBUF_TYPE_PADDING)
Tom Zanussi2d622712009-03-22 03:30:49 -05003246 goto again;
Tom Zanussi2d622712009-03-22 03:30:49 -05003247
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003248 return event;
3249}
3250
3251/**
3252 * ring_buffer_iter_peek - peek at the next event to be read
3253 * @iter: The ring buffer iterator
3254 * @ts: The timestamp counter of this event.
3255 *
3256 * This will return the event that will be read next, but does
3257 * not increment the iterator.
3258 */
3259struct ring_buffer_event *
3260ring_buffer_iter_peek(struct ring_buffer_iter *iter, u64 *ts)
3261{
3262 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
3263 struct ring_buffer_event *event;
3264 unsigned long flags;
3265
Tom Zanussi2d622712009-03-22 03:30:49 -05003266 again:
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003267 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
3268 event = rb_iter_peek(iter, ts);
3269 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
3270
Steven Rostedt1b959e12009-09-03 10:12:13 -04003271 if (event && event->type_len == RINGBUF_TYPE_PADDING)
Tom Zanussi2d622712009-03-22 03:30:49 -05003272 goto again;
Tom Zanussi2d622712009-03-22 03:30:49 -05003273
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003274 return event;
3275}
3276
3277/**
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003278 * ring_buffer_consume - return an event and consume it
3279 * @buffer: The ring buffer to get the next event from
Steven Rostedt66a8cb92010-03-31 13:21:56 -04003280 * @cpu: the cpu to read the buffer from
3281 * @ts: a variable to store the timestamp (may be NULL)
3282 * @lost_events: a variable to store if events were lost (may be NULL)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003283 *
3284 * Returns the next event in the ring buffer, and that event is consumed.
3285 * Meaning, that sequential reads will keep returning a different event,
3286 * and eventually empty the ring buffer if the producer is slower.
3287 */
3288struct ring_buffer_event *
Steven Rostedt66a8cb92010-03-31 13:21:56 -04003289ring_buffer_consume(struct ring_buffer *buffer, int cpu, u64 *ts,
3290 unsigned long *lost_events)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003291{
Steven Rostedt554f7862009-03-11 22:00:13 -04003292 struct ring_buffer_per_cpu *cpu_buffer;
3293 struct ring_buffer_event *event = NULL;
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003294 unsigned long flags;
Steven Rostedt8d707e82009-06-16 21:22:48 -04003295 int dolock;
3296
3297 dolock = rb_ok_to_lock();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003298
Tom Zanussi2d622712009-03-22 03:30:49 -05003299 again:
Steven Rostedt554f7862009-03-11 22:00:13 -04003300 /* might be called in atomic */
3301 preempt_disable();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003302
Steven Rostedt554f7862009-03-11 22:00:13 -04003303 if (!cpumask_test_cpu(cpu, buffer->cpumask))
3304 goto out;
3305
3306 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt8d707e82009-06-16 21:22:48 -04003307 local_irq_save(flags);
3308 if (dolock)
3309 spin_lock(&cpu_buffer->reader_lock);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003310
Steven Rostedt66a8cb92010-03-31 13:21:56 -04003311 event = rb_buffer_peek(cpu_buffer, ts, lost_events);
3312 if (event) {
3313 cpu_buffer->lost_events = 0;
Robert Richter469535a2009-07-30 19:19:18 +02003314 rb_advance_reader(cpu_buffer);
Steven Rostedt66a8cb92010-03-31 13:21:56 -04003315 }
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003316
Steven Rostedt8d707e82009-06-16 21:22:48 -04003317 if (dolock)
3318 spin_unlock(&cpu_buffer->reader_lock);
3319 local_irq_restore(flags);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003320
Steven Rostedt554f7862009-03-11 22:00:13 -04003321 out:
3322 preempt_enable();
3323
Steven Rostedt1b959e12009-09-03 10:12:13 -04003324 if (event && event->type_len == RINGBUF_TYPE_PADDING)
Tom Zanussi2d622712009-03-22 03:30:49 -05003325 goto again;
Tom Zanussi2d622712009-03-22 03:30:49 -05003326
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003327 return event;
3328}
Robert Richterc4f50182008-12-11 16:49:22 +01003329EXPORT_SYMBOL_GPL(ring_buffer_consume);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003330
3331/**
3332 * ring_buffer_read_start - start a non consuming read of the buffer
3333 * @buffer: The ring buffer to read from
3334 * @cpu: The cpu buffer to iterate over
3335 *
3336 * This starts up an iteration through the buffer. It also disables
3337 * the recording to the buffer until the reading is finished.
3338 * This prevents the reading from being corrupted. This is not
3339 * a consuming read, so a producer is not expected.
3340 *
3341 * Must be paired with ring_buffer_finish.
3342 */
3343struct ring_buffer_iter *
3344ring_buffer_read_start(struct ring_buffer *buffer, int cpu)
3345{
3346 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedt8aabee52009-03-12 13:13:49 -04003347 struct ring_buffer_iter *iter;
Steven Rostedtd7690412008-10-01 00:29:53 -04003348 unsigned long flags;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003349
Rusty Russell9e01c1b2009-01-01 10:12:22 +10303350 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04003351 return NULL;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003352
3353 iter = kmalloc(sizeof(*iter), GFP_KERNEL);
3354 if (!iter)
Steven Rostedt8aabee52009-03-12 13:13:49 -04003355 return NULL;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003356
3357 cpu_buffer = buffer->buffers[cpu];
3358
3359 iter->cpu_buffer = cpu_buffer;
3360
3361 atomic_inc(&cpu_buffer->record_disabled);
3362 synchronize_sched();
3363
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003364 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01003365 arch_spin_lock(&cpu_buffer->lock);
Steven Rostedt642edba2008-11-12 00:01:26 -05003366 rb_iter_reset(iter);
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01003367 arch_spin_unlock(&cpu_buffer->lock);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003368 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003369
3370 return iter;
3371}
Robert Richterc4f50182008-12-11 16:49:22 +01003372EXPORT_SYMBOL_GPL(ring_buffer_read_start);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003373
3374/**
3375 * ring_buffer_finish - finish reading the iterator of the buffer
3376 * @iter: The iterator retrieved by ring_buffer_start
3377 *
3378 * This re-enables the recording to the buffer, and frees the
3379 * iterator.
3380 */
3381void
3382ring_buffer_read_finish(struct ring_buffer_iter *iter)
3383{
3384 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
3385
3386 atomic_dec(&cpu_buffer->record_disabled);
3387 kfree(iter);
3388}
Robert Richterc4f50182008-12-11 16:49:22 +01003389EXPORT_SYMBOL_GPL(ring_buffer_read_finish);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003390
3391/**
3392 * ring_buffer_read - read the next item in the ring buffer by the iterator
3393 * @iter: The ring buffer iterator
3394 * @ts: The time stamp of the event read.
3395 *
3396 * This reads the next event in the ring buffer and increments the iterator.
3397 */
3398struct ring_buffer_event *
3399ring_buffer_read(struct ring_buffer_iter *iter, u64 *ts)
3400{
3401 struct ring_buffer_event *event;
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003402 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
3403 unsigned long flags;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003404
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003405 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
Steven Rostedt7e9391c2009-09-03 10:02:09 -04003406 again:
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003407 event = rb_iter_peek(iter, ts);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003408 if (!event)
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003409 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003410
Steven Rostedt7e9391c2009-09-03 10:02:09 -04003411 if (event->type_len == RINGBUF_TYPE_PADDING)
3412 goto again;
3413
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003414 rb_advance_iter(iter);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003415 out:
3416 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003417
3418 return event;
3419}
Robert Richterc4f50182008-12-11 16:49:22 +01003420EXPORT_SYMBOL_GPL(ring_buffer_read);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003421
3422/**
3423 * ring_buffer_size - return the size of the ring buffer (in bytes)
3424 * @buffer: The ring buffer.
3425 */
3426unsigned long ring_buffer_size(struct ring_buffer *buffer)
3427{
3428 return BUF_PAGE_SIZE * buffer->pages;
3429}
Robert Richterc4f50182008-12-11 16:49:22 +01003430EXPORT_SYMBOL_GPL(ring_buffer_size);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003431
3432static void
3433rb_reset_cpu(struct ring_buffer_per_cpu *cpu_buffer)
3434{
Steven Rostedt77ae3652009-03-27 11:00:29 -04003435 rb_head_page_deactivate(cpu_buffer);
3436
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003437 cpu_buffer->head_page
Steven Rostedt3adc54f2009-03-30 15:32:01 -04003438 = list_entry(cpu_buffer->pages, struct buffer_page, list);
Steven Rostedtbf41a152008-10-04 02:00:59 -04003439 local_set(&cpu_buffer->head_page->write, 0);
Steven Rostedt778c55d2009-05-01 18:44:45 -04003440 local_set(&cpu_buffer->head_page->entries, 0);
Steven Rostedtabc9b562008-12-02 15:34:06 -05003441 local_set(&cpu_buffer->head_page->page->commit, 0);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003442
Steven Rostedt6f807ac2008-10-04 02:00:58 -04003443 cpu_buffer->head_page->read = 0;
Steven Rostedtbf41a152008-10-04 02:00:59 -04003444
3445 cpu_buffer->tail_page = cpu_buffer->head_page;
3446 cpu_buffer->commit_page = cpu_buffer->head_page;
3447
3448 INIT_LIST_HEAD(&cpu_buffer->reader_page->list);
3449 local_set(&cpu_buffer->reader_page->write, 0);
Steven Rostedt778c55d2009-05-01 18:44:45 -04003450 local_set(&cpu_buffer->reader_page->entries, 0);
Steven Rostedtabc9b562008-12-02 15:34:06 -05003451 local_set(&cpu_buffer->reader_page->page->commit, 0);
Steven Rostedt6f807ac2008-10-04 02:00:58 -04003452 cpu_buffer->reader_page->read = 0;
Steven Rostedtd7690412008-10-01 00:29:53 -04003453
Steven Rostedt77ae3652009-03-27 11:00:29 -04003454 local_set(&cpu_buffer->commit_overrun, 0);
3455 local_set(&cpu_buffer->overrun, 0);
Steven Rostedte4906ef2009-04-30 20:49:44 -04003456 local_set(&cpu_buffer->entries, 0);
Steven Rostedtfa743952009-06-16 12:37:57 -04003457 local_set(&cpu_buffer->committing, 0);
3458 local_set(&cpu_buffer->commits, 0);
Steven Rostedt77ae3652009-03-27 11:00:29 -04003459 cpu_buffer->read = 0;
Steven Rostedt69507c02009-01-21 18:45:57 -05003460
3461 cpu_buffer->write_stamp = 0;
3462 cpu_buffer->read_stamp = 0;
Steven Rostedt77ae3652009-03-27 11:00:29 -04003463
Steven Rostedt66a8cb92010-03-31 13:21:56 -04003464 cpu_buffer->lost_events = 0;
3465 cpu_buffer->last_overrun = 0;
3466
Steven Rostedt77ae3652009-03-27 11:00:29 -04003467 rb_head_page_activate(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003468}
3469
3470/**
3471 * ring_buffer_reset_cpu - reset a ring buffer per CPU buffer
3472 * @buffer: The ring buffer to reset a per cpu buffer of
3473 * @cpu: The CPU buffer to be reset
3474 */
3475void ring_buffer_reset_cpu(struct ring_buffer *buffer, int cpu)
3476{
3477 struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
3478 unsigned long flags;
3479
Rusty Russell9e01c1b2009-01-01 10:12:22 +10303480 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04003481 return;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003482
Steven Rostedt41ede232009-05-01 20:26:54 -04003483 atomic_inc(&cpu_buffer->record_disabled);
3484
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003485 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
3486
Steven Rostedt41b6a952009-09-02 09:59:48 -04003487 if (RB_WARN_ON(cpu_buffer, local_read(&cpu_buffer->committing)))
3488 goto out;
3489
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01003490 arch_spin_lock(&cpu_buffer->lock);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003491
3492 rb_reset_cpu(cpu_buffer);
3493
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01003494 arch_spin_unlock(&cpu_buffer->lock);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003495
Steven Rostedt41b6a952009-09-02 09:59:48 -04003496 out:
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003497 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
Steven Rostedt41ede232009-05-01 20:26:54 -04003498
3499 atomic_dec(&cpu_buffer->record_disabled);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003500}
Robert Richterc4f50182008-12-11 16:49:22 +01003501EXPORT_SYMBOL_GPL(ring_buffer_reset_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003502
3503/**
3504 * ring_buffer_reset - reset a ring buffer
3505 * @buffer: The ring buffer to reset all cpu buffers
3506 */
3507void ring_buffer_reset(struct ring_buffer *buffer)
3508{
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003509 int cpu;
3510
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003511 for_each_buffer_cpu(buffer, cpu)
Steven Rostedtd7690412008-10-01 00:29:53 -04003512 ring_buffer_reset_cpu(buffer, cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003513}
Robert Richterc4f50182008-12-11 16:49:22 +01003514EXPORT_SYMBOL_GPL(ring_buffer_reset);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003515
3516/**
3517 * rind_buffer_empty - is the ring buffer empty?
3518 * @buffer: The ring buffer to test
3519 */
3520int ring_buffer_empty(struct ring_buffer *buffer)
3521{
3522 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedtd4788202009-06-17 00:39:43 -04003523 unsigned long flags;
Steven Rostedt8d707e82009-06-16 21:22:48 -04003524 int dolock;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003525 int cpu;
Steven Rostedtd4788202009-06-17 00:39:43 -04003526 int ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003527
Steven Rostedt8d707e82009-06-16 21:22:48 -04003528 dolock = rb_ok_to_lock();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003529
3530 /* yes this is racy, but if you don't like the race, lock the buffer */
3531 for_each_buffer_cpu(buffer, cpu) {
3532 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt8d707e82009-06-16 21:22:48 -04003533 local_irq_save(flags);
3534 if (dolock)
3535 spin_lock(&cpu_buffer->reader_lock);
Steven Rostedtd4788202009-06-17 00:39:43 -04003536 ret = rb_per_cpu_empty(cpu_buffer);
Steven Rostedt8d707e82009-06-16 21:22:48 -04003537 if (dolock)
3538 spin_unlock(&cpu_buffer->reader_lock);
3539 local_irq_restore(flags);
3540
Steven Rostedtd4788202009-06-17 00:39:43 -04003541 if (!ret)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003542 return 0;
3543 }
Steven Rostedt554f7862009-03-11 22:00:13 -04003544
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003545 return 1;
3546}
Robert Richterc4f50182008-12-11 16:49:22 +01003547EXPORT_SYMBOL_GPL(ring_buffer_empty);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003548
3549/**
3550 * ring_buffer_empty_cpu - is a cpu buffer of a ring buffer empty?
3551 * @buffer: The ring buffer
3552 * @cpu: The CPU buffer to test
3553 */
3554int ring_buffer_empty_cpu(struct ring_buffer *buffer, int cpu)
3555{
3556 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedtd4788202009-06-17 00:39:43 -04003557 unsigned long flags;
Steven Rostedt8d707e82009-06-16 21:22:48 -04003558 int dolock;
Steven Rostedt8aabee52009-03-12 13:13:49 -04003559 int ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003560
Rusty Russell9e01c1b2009-01-01 10:12:22 +10303561 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04003562 return 1;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003563
Steven Rostedt8d707e82009-06-16 21:22:48 -04003564 dolock = rb_ok_to_lock();
Steven Rostedt554f7862009-03-11 22:00:13 -04003565
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003566 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt8d707e82009-06-16 21:22:48 -04003567 local_irq_save(flags);
3568 if (dolock)
3569 spin_lock(&cpu_buffer->reader_lock);
Steven Rostedt554f7862009-03-11 22:00:13 -04003570 ret = rb_per_cpu_empty(cpu_buffer);
Steven Rostedt8d707e82009-06-16 21:22:48 -04003571 if (dolock)
3572 spin_unlock(&cpu_buffer->reader_lock);
3573 local_irq_restore(flags);
Steven Rostedt554f7862009-03-11 22:00:13 -04003574
3575 return ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003576}
Robert Richterc4f50182008-12-11 16:49:22 +01003577EXPORT_SYMBOL_GPL(ring_buffer_empty_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003578
Steven Rostedt85bac322009-09-04 14:24:40 -04003579#ifdef CONFIG_RING_BUFFER_ALLOW_SWAP
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003580/**
3581 * ring_buffer_swap_cpu - swap a CPU buffer between two ring buffers
3582 * @buffer_a: One buffer to swap with
3583 * @buffer_b: The other buffer to swap with
3584 *
3585 * This function is useful for tracers that want to take a "snapshot"
3586 * of a CPU buffer and has another back up buffer lying around.
3587 * it is expected that the tracer handles the cpu buffer not being
3588 * used at the moment.
3589 */
3590int ring_buffer_swap_cpu(struct ring_buffer *buffer_a,
3591 struct ring_buffer *buffer_b, int cpu)
3592{
3593 struct ring_buffer_per_cpu *cpu_buffer_a;
3594 struct ring_buffer_per_cpu *cpu_buffer_b;
Steven Rostedt554f7862009-03-11 22:00:13 -04003595 int ret = -EINVAL;
3596
Rusty Russell9e01c1b2009-01-01 10:12:22 +10303597 if (!cpumask_test_cpu(cpu, buffer_a->cpumask) ||
3598 !cpumask_test_cpu(cpu, buffer_b->cpumask))
Steven Rostedt554f7862009-03-11 22:00:13 -04003599 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003600
3601 /* At least make sure the two buffers are somewhat the same */
Lai Jiangshan6d102bc2008-12-17 17:48:23 +08003602 if (buffer_a->pages != buffer_b->pages)
Steven Rostedt554f7862009-03-11 22:00:13 -04003603 goto out;
3604
3605 ret = -EAGAIN;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003606
Steven Rostedt97b17ef2009-01-21 15:24:56 -05003607 if (ring_buffer_flags != RB_BUFFERS_ON)
Steven Rostedt554f7862009-03-11 22:00:13 -04003608 goto out;
Steven Rostedt97b17ef2009-01-21 15:24:56 -05003609
3610 if (atomic_read(&buffer_a->record_disabled))
Steven Rostedt554f7862009-03-11 22:00:13 -04003611 goto out;
Steven Rostedt97b17ef2009-01-21 15:24:56 -05003612
3613 if (atomic_read(&buffer_b->record_disabled))
Steven Rostedt554f7862009-03-11 22:00:13 -04003614 goto out;
Steven Rostedt97b17ef2009-01-21 15:24:56 -05003615
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003616 cpu_buffer_a = buffer_a->buffers[cpu];
3617 cpu_buffer_b = buffer_b->buffers[cpu];
3618
Steven Rostedt97b17ef2009-01-21 15:24:56 -05003619 if (atomic_read(&cpu_buffer_a->record_disabled))
Steven Rostedt554f7862009-03-11 22:00:13 -04003620 goto out;
Steven Rostedt97b17ef2009-01-21 15:24:56 -05003621
3622 if (atomic_read(&cpu_buffer_b->record_disabled))
Steven Rostedt554f7862009-03-11 22:00:13 -04003623 goto out;
Steven Rostedt97b17ef2009-01-21 15:24:56 -05003624
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003625 /*
3626 * We can't do a synchronize_sched here because this
3627 * function can be called in atomic context.
3628 * Normally this will be called from the same CPU as cpu.
3629 * If not it's up to the caller to protect this.
3630 */
3631 atomic_inc(&cpu_buffer_a->record_disabled);
3632 atomic_inc(&cpu_buffer_b->record_disabled);
3633
Steven Rostedt98277992009-09-02 10:56:15 -04003634 ret = -EBUSY;
3635 if (local_read(&cpu_buffer_a->committing))
3636 goto out_dec;
3637 if (local_read(&cpu_buffer_b->committing))
3638 goto out_dec;
3639
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003640 buffer_a->buffers[cpu] = cpu_buffer_b;
3641 buffer_b->buffers[cpu] = cpu_buffer_a;
3642
3643 cpu_buffer_b->buffer = buffer_a;
3644 cpu_buffer_a->buffer = buffer_b;
3645
Steven Rostedt98277992009-09-02 10:56:15 -04003646 ret = 0;
3647
3648out_dec:
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003649 atomic_dec(&cpu_buffer_a->record_disabled);
3650 atomic_dec(&cpu_buffer_b->record_disabled);
Steven Rostedt554f7862009-03-11 22:00:13 -04003651out:
Steven Rostedt554f7862009-03-11 22:00:13 -04003652 return ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003653}
Robert Richterc4f50182008-12-11 16:49:22 +01003654EXPORT_SYMBOL_GPL(ring_buffer_swap_cpu);
Steven Rostedt85bac322009-09-04 14:24:40 -04003655#endif /* CONFIG_RING_BUFFER_ALLOW_SWAP */
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003656
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003657/**
3658 * ring_buffer_alloc_read_page - allocate a page to read from buffer
3659 * @buffer: the buffer to allocate for.
3660 *
3661 * This function is used in conjunction with ring_buffer_read_page.
3662 * When reading a full page from the ring buffer, these functions
3663 * can be used to speed up the process. The calling function should
3664 * allocate a few pages first with this function. Then when it
3665 * needs to get pages from the ring buffer, it passes the result
3666 * of this function into ring_buffer_read_page, which will swap
3667 * the page that was allocated, with the read page of the buffer.
3668 *
3669 * Returns:
3670 * The page allocated, or NULL on error.
3671 */
3672void *ring_buffer_alloc_read_page(struct ring_buffer *buffer)
3673{
Steven Rostedt044fa782008-12-02 23:50:03 -05003674 struct buffer_data_page *bpage;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003675 unsigned long addr;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003676
3677 addr = __get_free_page(GFP_KERNEL);
3678 if (!addr)
3679 return NULL;
3680
Steven Rostedt044fa782008-12-02 23:50:03 -05003681 bpage = (void *)addr;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003682
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003683 rb_init_page(bpage);
3684
Steven Rostedt044fa782008-12-02 23:50:03 -05003685 return bpage;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003686}
Steven Rostedtd6ce96d2009-05-05 01:15:24 -04003687EXPORT_SYMBOL_GPL(ring_buffer_alloc_read_page);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003688
3689/**
3690 * ring_buffer_free_read_page - free an allocated read page
3691 * @buffer: the buffer the page was allocate for
3692 * @data: the page to free
3693 *
3694 * Free a page allocated from ring_buffer_alloc_read_page.
3695 */
3696void ring_buffer_free_read_page(struct ring_buffer *buffer, void *data)
3697{
3698 free_page((unsigned long)data);
3699}
Steven Rostedtd6ce96d2009-05-05 01:15:24 -04003700EXPORT_SYMBOL_GPL(ring_buffer_free_read_page);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003701
3702/**
3703 * ring_buffer_read_page - extract a page from the ring buffer
3704 * @buffer: buffer to extract from
3705 * @data_page: the page to use allocated from ring_buffer_alloc_read_page
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003706 * @len: amount to extract
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003707 * @cpu: the cpu of the buffer to extract
3708 * @full: should the extraction only happen when the page is full.
3709 *
3710 * This function will pull out a page from the ring buffer and consume it.
3711 * @data_page must be the address of the variable that was returned
3712 * from ring_buffer_alloc_read_page. This is because the page might be used
3713 * to swap with a page in the ring buffer.
3714 *
3715 * for example:
Lai Jiangshanb85fa012009-02-09 14:21:14 +08003716 * rpage = ring_buffer_alloc_read_page(buffer);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003717 * if (!rpage)
3718 * return error;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003719 * ret = ring_buffer_read_page(buffer, &rpage, len, cpu, 0);
Lai Jiangshan667d2412009-02-09 14:21:17 +08003720 * if (ret >= 0)
3721 * process_page(rpage, ret);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003722 *
3723 * When @full is set, the function will not return true unless
3724 * the writer is off the reader page.
3725 *
3726 * Note: it is up to the calling functions to handle sleeps and wakeups.
3727 * The ring buffer can be used anywhere in the kernel and can not
3728 * blindly call wake_up. The layer that uses the ring buffer must be
3729 * responsible for that.
3730 *
3731 * Returns:
Lai Jiangshan667d2412009-02-09 14:21:17 +08003732 * >=0 if data has been transferred, returns the offset of consumed data.
3733 * <0 if no data has been transferred.
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003734 */
3735int ring_buffer_read_page(struct ring_buffer *buffer,
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003736 void **data_page, size_t len, int cpu, int full)
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003737{
3738 struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
3739 struct ring_buffer_event *event;
Steven Rostedt044fa782008-12-02 23:50:03 -05003740 struct buffer_data_page *bpage;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003741 struct buffer_page *reader;
Steven Rostedtff0ff842010-03-31 22:11:42 -04003742 unsigned long missed_events;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003743 unsigned long flags;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003744 unsigned int commit;
Lai Jiangshan667d2412009-02-09 14:21:17 +08003745 unsigned int read;
Steven Rostedt4f3640f2009-03-03 23:52:42 -05003746 u64 save_timestamp;
Lai Jiangshan667d2412009-02-09 14:21:17 +08003747 int ret = -1;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003748
Steven Rostedt554f7862009-03-11 22:00:13 -04003749 if (!cpumask_test_cpu(cpu, buffer->cpumask))
3750 goto out;
3751
Steven Rostedt474d32b2009-03-03 19:51:40 -05003752 /*
3753 * If len is not big enough to hold the page header, then
3754 * we can not copy anything.
3755 */
3756 if (len <= BUF_PAGE_HDR_SIZE)
Steven Rostedt554f7862009-03-11 22:00:13 -04003757 goto out;
Steven Rostedt474d32b2009-03-03 19:51:40 -05003758
3759 len -= BUF_PAGE_HDR_SIZE;
3760
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003761 if (!data_page)
Steven Rostedt554f7862009-03-11 22:00:13 -04003762 goto out;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003763
Steven Rostedt044fa782008-12-02 23:50:03 -05003764 bpage = *data_page;
3765 if (!bpage)
Steven Rostedt554f7862009-03-11 22:00:13 -04003766 goto out;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003767
3768 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
3769
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003770 reader = rb_get_reader_page(cpu_buffer);
3771 if (!reader)
Steven Rostedt554f7862009-03-11 22:00:13 -04003772 goto out_unlock;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003773
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003774 event = rb_reader_event(cpu_buffer);
Lai Jiangshan667d2412009-02-09 14:21:17 +08003775
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003776 read = reader->read;
3777 commit = rb_page_commit(reader);
3778
Steven Rostedt66a8cb92010-03-31 13:21:56 -04003779 /* Check if any events were dropped */
Steven Rostedtff0ff842010-03-31 22:11:42 -04003780 missed_events = cpu_buffer->lost_events;
Steven Rostedt66a8cb92010-03-31 13:21:56 -04003781
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003782 /*
Steven Rostedt474d32b2009-03-03 19:51:40 -05003783 * If this page has been partially read or
3784 * if len is not big enough to read the rest of the page or
3785 * a writer is still on the page, then
3786 * we must copy the data from the page to the buffer.
3787 * Otherwise, we can simply swap the page with the one passed in.
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003788 */
Steven Rostedt474d32b2009-03-03 19:51:40 -05003789 if (read || (len < (commit - read)) ||
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003790 cpu_buffer->reader_page == cpu_buffer->commit_page) {
Lai Jiangshan667d2412009-02-09 14:21:17 +08003791 struct buffer_data_page *rpage = cpu_buffer->reader_page->page;
Steven Rostedt474d32b2009-03-03 19:51:40 -05003792 unsigned int rpos = read;
3793 unsigned int pos = 0;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003794 unsigned int size;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003795
3796 if (full)
Steven Rostedt554f7862009-03-11 22:00:13 -04003797 goto out_unlock;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003798
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003799 if (len > (commit - read))
3800 len = (commit - read);
3801
3802 size = rb_event_length(event);
3803
3804 if (len < size)
Steven Rostedt554f7862009-03-11 22:00:13 -04003805 goto out_unlock;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003806
Steven Rostedt4f3640f2009-03-03 23:52:42 -05003807 /* save the current timestamp, since the user will need it */
3808 save_timestamp = cpu_buffer->read_stamp;
3809
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003810 /* Need to copy one event at a time */
3811 do {
Steven Rostedt474d32b2009-03-03 19:51:40 -05003812 memcpy(bpage->data + pos, rpage->data + rpos, size);
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003813
3814 len -= size;
3815
3816 rb_advance_reader(cpu_buffer);
Steven Rostedt474d32b2009-03-03 19:51:40 -05003817 rpos = reader->read;
3818 pos += size;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003819
3820 event = rb_reader_event(cpu_buffer);
3821 size = rb_event_length(event);
3822 } while (len > size);
Lai Jiangshan667d2412009-02-09 14:21:17 +08003823
3824 /* update bpage */
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003825 local_set(&bpage->commit, pos);
Steven Rostedt4f3640f2009-03-03 23:52:42 -05003826 bpage->time_stamp = save_timestamp;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003827
Steven Rostedt474d32b2009-03-03 19:51:40 -05003828 /* we copied everything to the beginning */
3829 read = 0;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003830 } else {
Steven Rostedtafbab762009-05-01 19:40:05 -04003831 /* update the entry counter */
Steven Rostedt77ae3652009-03-27 11:00:29 -04003832 cpu_buffer->read += rb_page_entries(reader);
Steven Rostedtafbab762009-05-01 19:40:05 -04003833
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003834 /* swap the pages */
Steven Rostedt044fa782008-12-02 23:50:03 -05003835 rb_init_page(bpage);
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003836 bpage = reader->page;
3837 reader->page = *data_page;
3838 local_set(&reader->write, 0);
Steven Rostedt778c55d2009-05-01 18:44:45 -04003839 local_set(&reader->entries, 0);
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003840 reader->read = 0;
Steven Rostedt044fa782008-12-02 23:50:03 -05003841 *data_page = bpage;
Steven Rostedtff0ff842010-03-31 22:11:42 -04003842
3843 /*
3844 * Use the real_end for the data size,
3845 * This gives us a chance to store the lost events
3846 * on the page.
3847 */
3848 if (reader->real_end)
3849 local_set(&bpage->commit, reader->real_end);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003850 }
Lai Jiangshan667d2412009-02-09 14:21:17 +08003851 ret = read;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003852
Steven Rostedt66a8cb92010-03-31 13:21:56 -04003853 cpu_buffer->lost_events = 0;
3854 /*
3855 * Set a flag in the commit field if we lost events
3856 */
Steven Rostedtff0ff842010-03-31 22:11:42 -04003857 if (missed_events) {
3858 commit = local_read(&bpage->commit);
3859
3860 /* If there is room at the end of the page to save the
3861 * missed events, then record it there.
3862 */
3863 if (BUF_PAGE_SIZE - commit >= sizeof(missed_events)) {
3864 memcpy(&bpage->data[commit], &missed_events,
3865 sizeof(missed_events));
3866 local_add(RB_MISSED_STORED, &bpage->commit);
3867 }
Steven Rostedt66a8cb92010-03-31 13:21:56 -04003868 local_add(RB_MISSED_EVENTS, &bpage->commit);
Steven Rostedtff0ff842010-03-31 22:11:42 -04003869 }
Steven Rostedt66a8cb92010-03-31 13:21:56 -04003870
Steven Rostedt554f7862009-03-11 22:00:13 -04003871 out_unlock:
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003872 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
3873
Steven Rostedt554f7862009-03-11 22:00:13 -04003874 out:
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003875 return ret;
3876}
Steven Rostedtd6ce96d2009-05-05 01:15:24 -04003877EXPORT_SYMBOL_GPL(ring_buffer_read_page);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003878
Paul Mundt1155de42009-06-25 14:30:12 +09003879#ifdef CONFIG_TRACING
Steven Rostedta3583242008-11-11 15:01:42 -05003880static ssize_t
3881rb_simple_read(struct file *filp, char __user *ubuf,
3882 size_t cnt, loff_t *ppos)
3883{
Hannes Eder5e398412009-02-10 19:44:34 +01003884 unsigned long *p = filp->private_data;
Steven Rostedta3583242008-11-11 15:01:42 -05003885 char buf[64];
3886 int r;
3887
Steven Rostedt033601a2008-11-21 12:41:55 -05003888 if (test_bit(RB_BUFFERS_DISABLED_BIT, p))
3889 r = sprintf(buf, "permanently disabled\n");
3890 else
3891 r = sprintf(buf, "%d\n", test_bit(RB_BUFFERS_ON_BIT, p));
Steven Rostedta3583242008-11-11 15:01:42 -05003892
3893 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
3894}
3895
3896static ssize_t
3897rb_simple_write(struct file *filp, const char __user *ubuf,
3898 size_t cnt, loff_t *ppos)
3899{
Hannes Eder5e398412009-02-10 19:44:34 +01003900 unsigned long *p = filp->private_data;
Steven Rostedta3583242008-11-11 15:01:42 -05003901 char buf[64];
Hannes Eder5e398412009-02-10 19:44:34 +01003902 unsigned long val;
Steven Rostedta3583242008-11-11 15:01:42 -05003903 int ret;
3904
3905 if (cnt >= sizeof(buf))
3906 return -EINVAL;
3907
3908 if (copy_from_user(&buf, ubuf, cnt))
3909 return -EFAULT;
3910
3911 buf[cnt] = 0;
3912
3913 ret = strict_strtoul(buf, 10, &val);
3914 if (ret < 0)
3915 return ret;
3916
Steven Rostedt033601a2008-11-21 12:41:55 -05003917 if (val)
3918 set_bit(RB_BUFFERS_ON_BIT, p);
3919 else
3920 clear_bit(RB_BUFFERS_ON_BIT, p);
Steven Rostedta3583242008-11-11 15:01:42 -05003921
3922 (*ppos)++;
3923
3924 return cnt;
3925}
3926
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003927static const struct file_operations rb_simple_fops = {
Steven Rostedta3583242008-11-11 15:01:42 -05003928 .open = tracing_open_generic,
3929 .read = rb_simple_read,
3930 .write = rb_simple_write,
3931};
3932
3933
3934static __init int rb_init_debugfs(void)
3935{
3936 struct dentry *d_tracer;
Steven Rostedta3583242008-11-11 15:01:42 -05003937
3938 d_tracer = tracing_init_dentry();
3939
Frederic Weisbecker5452af62009-03-27 00:25:38 +01003940 trace_create_file("tracing_on", 0644, d_tracer,
3941 &ring_buffer_flags, &rb_simple_fops);
Steven Rostedta3583242008-11-11 15:01:42 -05003942
3943 return 0;
3944}
3945
3946fs_initcall(rb_init_debugfs);
Paul Mundt1155de42009-06-25 14:30:12 +09003947#endif
Steven Rostedt554f7862009-03-11 22:00:13 -04003948
Steven Rostedt59222ef2009-03-12 11:46:03 -04003949#ifdef CONFIG_HOTPLUG_CPU
Frederic Weisbecker09c9e842009-03-21 04:33:36 +01003950static int rb_cpu_notify(struct notifier_block *self,
3951 unsigned long action, void *hcpu)
Steven Rostedt554f7862009-03-11 22:00:13 -04003952{
3953 struct ring_buffer *buffer =
3954 container_of(self, struct ring_buffer, cpu_notify);
3955 long cpu = (long)hcpu;
3956
3957 switch (action) {
3958 case CPU_UP_PREPARE:
3959 case CPU_UP_PREPARE_FROZEN:
Rusty Russell3f237a72009-06-12 21:15:30 +09303960 if (cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt554f7862009-03-11 22:00:13 -04003961 return NOTIFY_OK;
3962
3963 buffer->buffers[cpu] =
3964 rb_allocate_cpu_buffer(buffer, cpu);
3965 if (!buffer->buffers[cpu]) {
3966 WARN(1, "failed to allocate ring buffer on CPU %ld\n",
3967 cpu);
3968 return NOTIFY_OK;
3969 }
3970 smp_wmb();
Rusty Russell3f237a72009-06-12 21:15:30 +09303971 cpumask_set_cpu(cpu, buffer->cpumask);
Steven Rostedt554f7862009-03-11 22:00:13 -04003972 break;
3973 case CPU_DOWN_PREPARE:
3974 case CPU_DOWN_PREPARE_FROZEN:
3975 /*
3976 * Do nothing.
3977 * If we were to free the buffer, then the user would
3978 * lose any trace that was in the buffer.
3979 */
3980 break;
3981 default:
3982 break;
3983 }
3984 return NOTIFY_OK;
3985}
3986#endif