blob: f50f43107e938d235964653b9bf35f55ded206cb [file] [log] [blame]
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001/*
2 * Generic ring buffer
3 *
4 * Copyright (C) 2008 Steven Rostedt <srostedt@redhat.com>
5 */
6#include <linux/ring_buffer.h>
Ingo Molnar14131f22009-02-26 18:47:11 +01007#include <linux/trace_clock.h>
Steven Rostedt78d904b2009-02-05 18:43:07 -05008#include <linux/ftrace_irq.h>
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04009#include <linux/spinlock.h>
10#include <linux/debugfs.h>
11#include <linux/uaccess.h>
Steven Rostedta81bd802009-02-06 01:45:16 -050012#include <linux/hardirq.h>
Vegard Nossum1744a212009-02-28 08:29:44 +010013#include <linux/kmemcheck.h>
Steven Rostedt7a8e76a2008-09-29 23:02:38 -040014#include <linux/module.h>
15#include <linux/percpu.h>
16#include <linux/mutex.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090017#include <linux/slab.h>
Steven Rostedt7a8e76a2008-09-29 23:02:38 -040018#include <linux/init.h>
19#include <linux/hash.h>
20#include <linux/list.h>
Steven Rostedt554f7862009-03-11 22:00:13 -040021#include <linux/cpu.h>
Steven Rostedt7a8e76a2008-09-29 23:02:38 -040022#include <linux/fs.h>
23
Christoph Lameter79615762010-01-05 15:34:50 +090024#include <asm/local.h>
Steven Rostedt182e9f52008-11-03 23:15:56 -050025#include "trace.h"
26
Steven Rostedt033601a2008-11-21 12:41:55 -050027/*
Steven Rostedtd1b182a2009-04-15 16:53:47 -040028 * The ring buffer header is special. We must manually up keep it.
29 */
30int ring_buffer_print_entry_header(struct trace_seq *s)
31{
32 int ret;
33
Lai Jiangshan334d4162009-04-24 11:27:05 +080034 ret = trace_seq_printf(s, "# compressed entry header\n");
35 ret = trace_seq_printf(s, "\ttype_len : 5 bits\n");
Steven Rostedtd1b182a2009-04-15 16:53:47 -040036 ret = trace_seq_printf(s, "\ttime_delta : 27 bits\n");
37 ret = trace_seq_printf(s, "\tarray : 32 bits\n");
38 ret = trace_seq_printf(s, "\n");
39 ret = trace_seq_printf(s, "\tpadding : type == %d\n",
40 RINGBUF_TYPE_PADDING);
41 ret = trace_seq_printf(s, "\ttime_extend : type == %d\n",
42 RINGBUF_TYPE_TIME_EXTEND);
Lai Jiangshan334d4162009-04-24 11:27:05 +080043 ret = trace_seq_printf(s, "\tdata max type_len == %d\n",
44 RINGBUF_TYPE_DATA_TYPE_LEN_MAX);
Steven Rostedtd1b182a2009-04-15 16:53:47 -040045
46 return ret;
47}
48
49/*
Steven Rostedt5cc98542009-03-12 22:24:17 -040050 * The ring buffer is made up of a list of pages. A separate list of pages is
51 * allocated for each CPU. A writer may only write to a buffer that is
52 * associated with the CPU it is currently executing on. A reader may read
53 * from any per cpu buffer.
54 *
55 * The reader is special. For each per cpu buffer, the reader has its own
56 * reader page. When a reader has read the entire reader page, this reader
57 * page is swapped with another page in the ring buffer.
58 *
59 * Now, as long as the writer is off the reader page, the reader can do what
60 * ever it wants with that page. The writer will never write to that page
61 * again (as long as it is out of the ring buffer).
62 *
63 * Here's some silly ASCII art.
64 *
65 * +------+
66 * |reader| RING BUFFER
67 * |page |
68 * +------+ +---+ +---+ +---+
69 * | |-->| |-->| |
70 * +---+ +---+ +---+
71 * ^ |
72 * | |
73 * +---------------+
74 *
75 *
76 * +------+
77 * |reader| RING BUFFER
78 * |page |------------------v
79 * +------+ +---+ +---+ +---+
80 * | |-->| |-->| |
81 * +---+ +---+ +---+
82 * ^ |
83 * | |
84 * +---------------+
85 *
86 *
87 * +------+
88 * |reader| RING BUFFER
89 * |page |------------------v
90 * +------+ +---+ +---+ +---+
91 * ^ | |-->| |-->| |
92 * | +---+ +---+ +---+
93 * | |
94 * | |
95 * +------------------------------+
96 *
97 *
98 * +------+
99 * |buffer| RING BUFFER
100 * |page |------------------v
101 * +------+ +---+ +---+ +---+
102 * ^ | | | |-->| |
103 * | New +---+ +---+ +---+
104 * | Reader------^ |
105 * | page |
106 * +------------------------------+
107 *
108 *
109 * After we make this swap, the reader can hand this page off to the splice
110 * code and be done with it. It can even allocate a new page if it needs to
111 * and swap that into the ring buffer.
112 *
113 * We will be using cmpxchg soon to make all this lockless.
114 *
115 */
116
117/*
Steven Rostedt033601a2008-11-21 12:41:55 -0500118 * A fast way to enable or disable all ring buffers is to
119 * call tracing_on or tracing_off. Turning off the ring buffers
120 * prevents all ring buffers from being recorded to.
121 * Turning this switch on, makes it OK to write to the
122 * ring buffer, if the ring buffer is enabled itself.
123 *
124 * There's three layers that must be on in order to write
125 * to the ring buffer.
126 *
127 * 1) This global flag must be set.
128 * 2) The ring buffer must be enabled for recording.
129 * 3) The per cpu buffer must be enabled for recording.
130 *
131 * In case of an anomaly, this global flag has a bit set that
132 * will permantly disable all ring buffers.
133 */
134
135/*
136 * Global flag to disable all recording to ring buffers
137 * This has two bits: ON, DISABLED
138 *
139 * ON DISABLED
140 * ---- ----------
141 * 0 0 : ring buffers are off
142 * 1 0 : ring buffers are on
143 * X 1 : ring buffers are permanently disabled
144 */
145
146enum {
147 RB_BUFFERS_ON_BIT = 0,
148 RB_BUFFERS_DISABLED_BIT = 1,
149};
150
151enum {
152 RB_BUFFERS_ON = 1 << RB_BUFFERS_ON_BIT,
153 RB_BUFFERS_DISABLED = 1 << RB_BUFFERS_DISABLED_BIT,
154};
155
Hannes Eder5e398412009-02-10 19:44:34 +0100156static unsigned long ring_buffer_flags __read_mostly = RB_BUFFERS_ON;
Steven Rostedta3583242008-11-11 15:01:42 -0500157
Steven Rostedt474d32b2009-03-03 19:51:40 -0500158#define BUF_PAGE_HDR_SIZE offsetof(struct buffer_data_page, data)
159
Steven Rostedta3583242008-11-11 15:01:42 -0500160/**
161 * tracing_on - enable all tracing buffers
162 *
163 * This function enables all tracing buffers that may have been
164 * disabled with tracing_off.
165 */
166void tracing_on(void)
167{
Steven Rostedt033601a2008-11-21 12:41:55 -0500168 set_bit(RB_BUFFERS_ON_BIT, &ring_buffer_flags);
Steven Rostedta3583242008-11-11 15:01:42 -0500169}
Robert Richterc4f50182008-12-11 16:49:22 +0100170EXPORT_SYMBOL_GPL(tracing_on);
Steven Rostedta3583242008-11-11 15:01:42 -0500171
172/**
173 * tracing_off - turn off all tracing buffers
174 *
175 * This function stops all tracing buffers from recording data.
176 * It does not disable any overhead the tracers themselves may
177 * be causing. This function simply causes all recording to
178 * the ring buffers to fail.
179 */
180void tracing_off(void)
181{
Steven Rostedt033601a2008-11-21 12:41:55 -0500182 clear_bit(RB_BUFFERS_ON_BIT, &ring_buffer_flags);
183}
Robert Richterc4f50182008-12-11 16:49:22 +0100184EXPORT_SYMBOL_GPL(tracing_off);
Steven Rostedt033601a2008-11-21 12:41:55 -0500185
186/**
187 * tracing_off_permanent - permanently disable ring buffers
188 *
189 * This function, once called, will disable all ring buffers
Wenji Huangc3706f02009-02-10 01:03:18 -0500190 * permanently.
Steven Rostedt033601a2008-11-21 12:41:55 -0500191 */
192void tracing_off_permanent(void)
193{
194 set_bit(RB_BUFFERS_DISABLED_BIT, &ring_buffer_flags);
Steven Rostedta3583242008-11-11 15:01:42 -0500195}
196
Steven Rostedt988ae9d2009-02-14 19:17:02 -0500197/**
198 * tracing_is_on - show state of ring buffers enabled
199 */
200int tracing_is_on(void)
201{
202 return ring_buffer_flags == RB_BUFFERS_ON;
203}
204EXPORT_SYMBOL_GPL(tracing_is_on);
205
Steven Rostedte3d6bf02009-03-03 13:53:07 -0500206#define RB_EVNT_HDR_SIZE (offsetof(struct ring_buffer_event, array))
Andrew Morton67d34722009-01-09 12:27:09 -0800207#define RB_ALIGNMENT 4U
Lai Jiangshan334d4162009-04-24 11:27:05 +0800208#define RB_MAX_SMALL_DATA (RB_ALIGNMENT * RINGBUF_TYPE_DATA_TYPE_LEN_MAX)
Steven Rostedtc7b09302009-06-11 11:12:00 -0400209#define RB_EVNT_MIN_SIZE 8U /* two 32bit words */
Lai Jiangshan334d4162009-04-24 11:27:05 +0800210
Steven Rostedt22710482010-03-18 17:54:19 -0400211#if !defined(CONFIG_64BIT) || defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)
212# define RB_FORCE_8BYTE_ALIGNMENT 0
213# define RB_ARCH_ALIGNMENT RB_ALIGNMENT
214#else
215# define RB_FORCE_8BYTE_ALIGNMENT 1
216# define RB_ARCH_ALIGNMENT 8U
217#endif
218
Lai Jiangshan334d4162009-04-24 11:27:05 +0800219/* define RINGBUF_TYPE_DATA for 'case RINGBUF_TYPE_DATA:' */
220#define RINGBUF_TYPE_DATA 0 ... RINGBUF_TYPE_DATA_TYPE_LEN_MAX
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400221
222enum {
223 RB_LEN_TIME_EXTEND = 8,
224 RB_LEN_TIME_STAMP = 16,
225};
226
Steven Rostedt69d1b832010-10-07 18:18:05 -0400227#define skip_time_extend(event) \
228 ((struct ring_buffer_event *)((char *)event + RB_LEN_TIME_EXTEND))
229
Tom Zanussi2d622712009-03-22 03:30:49 -0500230static inline int rb_null_event(struct ring_buffer_event *event)
231{
Steven Rostedta1863c22009-09-03 10:23:58 -0400232 return event->type_len == RINGBUF_TYPE_PADDING && !event->time_delta;
Tom Zanussi2d622712009-03-22 03:30:49 -0500233}
234
235static void rb_event_set_padding(struct ring_buffer_event *event)
236{
Steven Rostedta1863c22009-09-03 10:23:58 -0400237 /* padding has a NULL time_delta */
Lai Jiangshan334d4162009-04-24 11:27:05 +0800238 event->type_len = RINGBUF_TYPE_PADDING;
Tom Zanussi2d622712009-03-22 03:30:49 -0500239 event->time_delta = 0;
240}
241
Tom Zanussi2d622712009-03-22 03:30:49 -0500242static unsigned
243rb_event_data_length(struct ring_buffer_event *event)
244{
245 unsigned length;
246
Lai Jiangshan334d4162009-04-24 11:27:05 +0800247 if (event->type_len)
248 length = event->type_len * RB_ALIGNMENT;
Tom Zanussi2d622712009-03-22 03:30:49 -0500249 else
250 length = event->array[0];
251 return length + RB_EVNT_HDR_SIZE;
252}
253
Steven Rostedt69d1b832010-10-07 18:18:05 -0400254/*
255 * Return the length of the given event. Will return
256 * the length of the time extend if the event is a
257 * time extend.
258 */
259static inline unsigned
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400260rb_event_length(struct ring_buffer_event *event)
261{
Lai Jiangshan334d4162009-04-24 11:27:05 +0800262 switch (event->type_len) {
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400263 case RINGBUF_TYPE_PADDING:
Tom Zanussi2d622712009-03-22 03:30:49 -0500264 if (rb_null_event(event))
265 /* undefined */
266 return -1;
Lai Jiangshan334d4162009-04-24 11:27:05 +0800267 return event->array[0] + RB_EVNT_HDR_SIZE;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400268
269 case RINGBUF_TYPE_TIME_EXTEND:
270 return RB_LEN_TIME_EXTEND;
271
272 case RINGBUF_TYPE_TIME_STAMP:
273 return RB_LEN_TIME_STAMP;
274
275 case RINGBUF_TYPE_DATA:
Tom Zanussi2d622712009-03-22 03:30:49 -0500276 return rb_event_data_length(event);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400277 default:
278 BUG();
279 }
280 /* not hit */
281 return 0;
282}
283
Steven Rostedt69d1b832010-10-07 18:18:05 -0400284/*
285 * Return total length of time extend and data,
286 * or just the event length for all other events.
287 */
288static inline unsigned
289rb_event_ts_length(struct ring_buffer_event *event)
290{
291 unsigned len = 0;
292
293 if (event->type_len == RINGBUF_TYPE_TIME_EXTEND) {
294 /* time extends include the data event after it */
295 len = RB_LEN_TIME_EXTEND;
296 event = skip_time_extend(event);
297 }
298 return len + rb_event_length(event);
299}
300
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400301/**
302 * ring_buffer_event_length - return the length of the event
303 * @event: the event to get the length of
Steven Rostedt69d1b832010-10-07 18:18:05 -0400304 *
305 * Returns the size of the data load of a data event.
306 * If the event is something other than a data event, it
307 * returns the size of the event itself. With the exception
308 * of a TIME EXTEND, where it still returns the size of the
309 * data load of the data event after it.
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400310 */
311unsigned ring_buffer_event_length(struct ring_buffer_event *event)
312{
Steven Rostedt69d1b832010-10-07 18:18:05 -0400313 unsigned length;
314
315 if (event->type_len == RINGBUF_TYPE_TIME_EXTEND)
316 event = skip_time_extend(event);
317
318 length = rb_event_length(event);
Lai Jiangshan334d4162009-04-24 11:27:05 +0800319 if (event->type_len > RINGBUF_TYPE_DATA_TYPE_LEN_MAX)
Robert Richter465634a2009-01-07 15:32:11 +0100320 return length;
321 length -= RB_EVNT_HDR_SIZE;
322 if (length > RB_MAX_SMALL_DATA + sizeof(event->array[0]))
323 length -= sizeof(event->array[0]);
324 return length;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400325}
Robert Richterc4f50182008-12-11 16:49:22 +0100326EXPORT_SYMBOL_GPL(ring_buffer_event_length);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400327
328/* inline for ring buffer fast paths */
Andrew Morton34a148b2009-01-09 12:27:09 -0800329static void *
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400330rb_event_data(struct ring_buffer_event *event)
331{
Steven Rostedt69d1b832010-10-07 18:18:05 -0400332 if (event->type_len == RINGBUF_TYPE_TIME_EXTEND)
333 event = skip_time_extend(event);
Lai Jiangshan334d4162009-04-24 11:27:05 +0800334 BUG_ON(event->type_len > RINGBUF_TYPE_DATA_TYPE_LEN_MAX);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400335 /* If length is in len field, then array[0] has the data */
Lai Jiangshan334d4162009-04-24 11:27:05 +0800336 if (event->type_len)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400337 return (void *)&event->array[0];
338 /* Otherwise length is in array[0] and array[1] has the data */
339 return (void *)&event->array[1];
340}
341
342/**
343 * ring_buffer_event_data - return the data of the event
344 * @event: the event to get the data from
345 */
346void *ring_buffer_event_data(struct ring_buffer_event *event)
347{
348 return rb_event_data(event);
349}
Robert Richterc4f50182008-12-11 16:49:22 +0100350EXPORT_SYMBOL_GPL(ring_buffer_event_data);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400351
352#define for_each_buffer_cpu(buffer, cpu) \
Rusty Russell9e01c1b2009-01-01 10:12:22 +1030353 for_each_cpu(cpu, buffer->cpumask)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400354
355#define TS_SHIFT 27
356#define TS_MASK ((1ULL << TS_SHIFT) - 1)
357#define TS_DELTA_TEST (~TS_MASK)
358
Steven Rostedt66a8cb92010-03-31 13:21:56 -0400359/* Flag when events were overwritten */
360#define RB_MISSED_EVENTS (1 << 31)
Steven Rostedtff0ff842010-03-31 22:11:42 -0400361/* Missed count stored at end */
362#define RB_MISSED_STORED (1 << 30)
Steven Rostedt66a8cb92010-03-31 13:21:56 -0400363
Steven Rostedtabc9b562008-12-02 15:34:06 -0500364struct buffer_data_page {
Steven Rostedte4c2ce82008-10-01 11:14:54 -0400365 u64 time_stamp; /* page time stamp */
Wenji Huangc3706f02009-02-10 01:03:18 -0500366 local_t commit; /* write committed index */
Steven Rostedtabc9b562008-12-02 15:34:06 -0500367 unsigned char data[]; /* data of buffer page */
368};
369
Steven Rostedt77ae3652009-03-27 11:00:29 -0400370/*
371 * Note, the buffer_page list must be first. The buffer pages
372 * are allocated in cache lines, which means that each buffer
373 * page will be at the beginning of a cache line, and thus
374 * the least significant bits will be zero. We use this to
375 * add flags in the list struct pointers, to make the ring buffer
376 * lockless.
377 */
Steven Rostedtabc9b562008-12-02 15:34:06 -0500378struct buffer_page {
Steven Rostedt778c55d2009-05-01 18:44:45 -0400379 struct list_head list; /* list of buffer pages */
Steven Rostedtabc9b562008-12-02 15:34:06 -0500380 local_t write; /* index for next write */
Steven Rostedt6f807ac2008-10-04 02:00:58 -0400381 unsigned read; /* index for next read */
Steven Rostedt778c55d2009-05-01 18:44:45 -0400382 local_t entries; /* entries on this page */
Steven Rostedtff0ff842010-03-31 22:11:42 -0400383 unsigned long real_end; /* real end of data */
Steven Rostedtabc9b562008-12-02 15:34:06 -0500384 struct buffer_data_page *page; /* Actual data page */
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400385};
386
Steven Rostedt77ae3652009-03-27 11:00:29 -0400387/*
388 * The buffer page counters, write and entries, must be reset
389 * atomically when crossing page boundaries. To synchronize this
390 * update, two counters are inserted into the number. One is
391 * the actual counter for the write position or count on the page.
392 *
393 * The other is a counter of updaters. Before an update happens
394 * the update partition of the counter is incremented. This will
395 * allow the updater to update the counter atomically.
396 *
397 * The counter is 20 bits, and the state data is 12.
398 */
399#define RB_WRITE_MASK 0xfffff
400#define RB_WRITE_INTCNT (1 << 20)
401
Steven Rostedt044fa782008-12-02 23:50:03 -0500402static void rb_init_page(struct buffer_data_page *bpage)
Steven Rostedtabc9b562008-12-02 15:34:06 -0500403{
Steven Rostedt044fa782008-12-02 23:50:03 -0500404 local_set(&bpage->commit, 0);
Steven Rostedtabc9b562008-12-02 15:34:06 -0500405}
406
Steven Rostedt474d32b2009-03-03 19:51:40 -0500407/**
408 * ring_buffer_page_len - the size of data on the page.
409 * @page: The page to read
410 *
411 * Returns the amount of data on the page, including buffer page header.
412 */
Steven Rostedtef7a4a12009-03-03 00:27:49 -0500413size_t ring_buffer_page_len(void *page)
414{
Steven Rostedt474d32b2009-03-03 19:51:40 -0500415 return local_read(&((struct buffer_data_page *)page)->commit)
416 + BUF_PAGE_HDR_SIZE;
Steven Rostedtef7a4a12009-03-03 00:27:49 -0500417}
418
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400419/*
Steven Rostedted568292008-09-29 23:02:40 -0400420 * Also stolen from mm/slob.c. Thanks to Mathieu Desnoyers for pointing
421 * this issue out.
422 */
Andrew Morton34a148b2009-01-09 12:27:09 -0800423static void free_buffer_page(struct buffer_page *bpage)
Steven Rostedted568292008-09-29 23:02:40 -0400424{
Andrew Morton34a148b2009-01-09 12:27:09 -0800425 free_page((unsigned long)bpage->page);
Steven Rostedte4c2ce82008-10-01 11:14:54 -0400426 kfree(bpage);
Steven Rostedted568292008-09-29 23:02:40 -0400427}
428
429/*
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400430 * We need to fit the time_stamp delta into 27 bits.
431 */
432static inline int test_time_stamp(u64 delta)
433{
434 if (delta & TS_DELTA_TEST)
435 return 1;
436 return 0;
437}
438
Steven Rostedt474d32b2009-03-03 19:51:40 -0500439#define BUF_PAGE_SIZE (PAGE_SIZE - BUF_PAGE_HDR_SIZE)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400440
Steven Rostedtbe957c42009-05-11 14:42:53 -0400441/* Max payload is BUF_PAGE_SIZE - header (8bytes) */
442#define BUF_MAX_DATA_SIZE (BUF_PAGE_SIZE - (sizeof(u32) * 2))
443
Steven Rostedtea05b572009-06-03 09:30:10 -0400444/* Max number of timestamps that can fit on a page */
Steven Rostedtd0134322010-10-12 12:06:43 -0400445#define RB_TIMESTAMPS_PER_PAGE (BUF_PAGE_SIZE / RB_LEN_TIME_EXTEND)
Steven Rostedtea05b572009-06-03 09:30:10 -0400446
Steven Rostedtd1b182a2009-04-15 16:53:47 -0400447int ring_buffer_print_page_header(struct trace_seq *s)
448{
449 struct buffer_data_page field;
450 int ret;
451
452 ret = trace_seq_printf(s, "\tfield: u64 timestamp;\t"
Tom Zanussi26a50742009-10-06 01:09:50 -0500453 "offset:0;\tsize:%u;\tsigned:%u;\n",
454 (unsigned int)sizeof(field.time_stamp),
455 (unsigned int)is_signed_type(u64));
Steven Rostedtd1b182a2009-04-15 16:53:47 -0400456
457 ret = trace_seq_printf(s, "\tfield: local_t commit;\t"
Tom Zanussi26a50742009-10-06 01:09:50 -0500458 "offset:%u;\tsize:%u;\tsigned:%u;\n",
Steven Rostedtd1b182a2009-04-15 16:53:47 -0400459 (unsigned int)offsetof(typeof(field), commit),
Tom Zanussi26a50742009-10-06 01:09:50 -0500460 (unsigned int)sizeof(field.commit),
461 (unsigned int)is_signed_type(long));
Steven Rostedtd1b182a2009-04-15 16:53:47 -0400462
Steven Rostedt66a8cb92010-03-31 13:21:56 -0400463 ret = trace_seq_printf(s, "\tfield: int overwrite;\t"
464 "offset:%u;\tsize:%u;\tsigned:%u;\n",
465 (unsigned int)offsetof(typeof(field), commit),
466 1,
467 (unsigned int)is_signed_type(long));
468
Steven Rostedtd1b182a2009-04-15 16:53:47 -0400469 ret = trace_seq_printf(s, "\tfield: char data;\t"
Tom Zanussi26a50742009-10-06 01:09:50 -0500470 "offset:%u;\tsize:%u;\tsigned:%u;\n",
Steven Rostedtd1b182a2009-04-15 16:53:47 -0400471 (unsigned int)offsetof(typeof(field), data),
Tom Zanussi26a50742009-10-06 01:09:50 -0500472 (unsigned int)BUF_PAGE_SIZE,
473 (unsigned int)is_signed_type(char));
Steven Rostedtd1b182a2009-04-15 16:53:47 -0400474
475 return ret;
476}
477
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400478/*
479 * head_page == tail_page && head == tail then buffer is empty.
480 */
481struct ring_buffer_per_cpu {
482 int cpu;
Richard Kennedy985023d2010-03-25 11:27:36 +0000483 atomic_t record_disabled;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400484 struct ring_buffer *buffer;
Steven Rostedt77ae3652009-03-27 11:00:29 -0400485 spinlock_t reader_lock; /* serialize readers */
Thomas Gleixner445c8952009-12-02 19:49:50 +0100486 arch_spinlock_t lock;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400487 struct lock_class_key lock_key;
Steven Rostedt3adc54f2009-03-30 15:32:01 -0400488 struct list_head *pages;
Steven Rostedt6f807ac2008-10-04 02:00:58 -0400489 struct buffer_page *head_page; /* read from head */
490 struct buffer_page *tail_page; /* write to tail */
Wenji Huangc3706f02009-02-10 01:03:18 -0500491 struct buffer_page *commit_page; /* committed pages */
Steven Rostedtd7690412008-10-01 00:29:53 -0400492 struct buffer_page *reader_page;
Steven Rostedt66a8cb92010-03-31 13:21:56 -0400493 unsigned long lost_events;
494 unsigned long last_overrun;
Steven Rostedt77ae3652009-03-27 11:00:29 -0400495 local_t commit_overrun;
496 local_t overrun;
Steven Rostedte4906ef2009-04-30 20:49:44 -0400497 local_t entries;
Steven Rostedtfa743952009-06-16 12:37:57 -0400498 local_t committing;
499 local_t commits;
Steven Rostedt77ae3652009-03-27 11:00:29 -0400500 unsigned long read;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400501 u64 write_stamp;
502 u64 read_stamp;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400503};
504
505struct ring_buffer {
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400506 unsigned pages;
507 unsigned flags;
508 int cpus;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400509 atomic_t record_disabled;
Arnaldo Carvalho de Melo00f62f62009-02-09 17:04:06 -0200510 cpumask_var_t cpumask;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400511
Peter Zijlstra1f8a6a12009-06-08 18:18:39 +0200512 struct lock_class_key *reader_lock_key;
513
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400514 struct mutex mutex;
515
516 struct ring_buffer_per_cpu **buffers;
Steven Rostedt554f7862009-03-11 22:00:13 -0400517
Steven Rostedt59222ef2009-03-12 11:46:03 -0400518#ifdef CONFIG_HOTPLUG_CPU
Steven Rostedt554f7862009-03-11 22:00:13 -0400519 struct notifier_block cpu_notify;
520#endif
Steven Rostedt37886f62009-03-17 17:22:06 -0400521 u64 (*clock)(void);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400522};
523
524struct ring_buffer_iter {
525 struct ring_buffer_per_cpu *cpu_buffer;
526 unsigned long head;
527 struct buffer_page *head_page;
Steven Rostedt492a74f2010-01-25 15:17:47 -0500528 struct buffer_page *cache_reader_page;
529 unsigned long cache_read;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400530 u64 read_stamp;
531};
532
Steven Rostedtf536aaf2008-11-10 23:07:30 -0500533/* buffer may be either ring_buffer or ring_buffer_per_cpu */
Steven Rostedt077c5402009-09-03 19:53:46 -0400534#define RB_WARN_ON(b, cond) \
535 ({ \
536 int _____ret = unlikely(cond); \
537 if (_____ret) { \
538 if (__same_type(*(b), struct ring_buffer_per_cpu)) { \
539 struct ring_buffer_per_cpu *__b = \
540 (void *)b; \
541 atomic_inc(&__b->buffer->record_disabled); \
542 } else \
543 atomic_inc(&b->record_disabled); \
544 WARN_ON(1); \
545 } \
546 _____ret; \
Steven Rostedt3e89c7b2008-11-11 15:28:41 -0500547 })
Steven Rostedtf536aaf2008-11-10 23:07:30 -0500548
Steven Rostedt37886f62009-03-17 17:22:06 -0400549/* Up this if you want to test the TIME_EXTENTS and normalization */
550#define DEBUG_SHIFT 0
551
Jiri Olsa6d3f1e12009-10-23 19:36:19 -0400552static inline u64 rb_time_stamp(struct ring_buffer *buffer)
Steven Rostedt88eb0122009-05-11 16:28:23 -0400553{
554 /* shift to debug/test normalization and TIME_EXTENTS */
555 return buffer->clock() << DEBUG_SHIFT;
556}
557
Steven Rostedt37886f62009-03-17 17:22:06 -0400558u64 ring_buffer_time_stamp(struct ring_buffer *buffer, int cpu)
559{
560 u64 time;
561
562 preempt_disable_notrace();
Jiri Olsa6d3f1e12009-10-23 19:36:19 -0400563 time = rb_time_stamp(buffer);
Steven Rostedt37886f62009-03-17 17:22:06 -0400564 preempt_enable_no_resched_notrace();
565
566 return time;
567}
568EXPORT_SYMBOL_GPL(ring_buffer_time_stamp);
569
570void ring_buffer_normalize_time_stamp(struct ring_buffer *buffer,
571 int cpu, u64 *ts)
572{
573 /* Just stupid testing the normalize function and deltas */
574 *ts >>= DEBUG_SHIFT;
575}
576EXPORT_SYMBOL_GPL(ring_buffer_normalize_time_stamp);
577
Steven Rostedt77ae3652009-03-27 11:00:29 -0400578/*
579 * Making the ring buffer lockless makes things tricky.
580 * Although writes only happen on the CPU that they are on,
581 * and they only need to worry about interrupts. Reads can
582 * happen on any CPU.
583 *
584 * The reader page is always off the ring buffer, but when the
585 * reader finishes with a page, it needs to swap its page with
586 * a new one from the buffer. The reader needs to take from
587 * the head (writes go to the tail). But if a writer is in overwrite
588 * mode and wraps, it must push the head page forward.
589 *
590 * Here lies the problem.
591 *
592 * The reader must be careful to replace only the head page, and
593 * not another one. As described at the top of the file in the
594 * ASCII art, the reader sets its old page to point to the next
595 * page after head. It then sets the page after head to point to
596 * the old reader page. But if the writer moves the head page
597 * during this operation, the reader could end up with the tail.
598 *
599 * We use cmpxchg to help prevent this race. We also do something
600 * special with the page before head. We set the LSB to 1.
601 *
602 * When the writer must push the page forward, it will clear the
603 * bit that points to the head page, move the head, and then set
604 * the bit that points to the new head page.
605 *
606 * We also don't want an interrupt coming in and moving the head
607 * page on another writer. Thus we use the second LSB to catch
608 * that too. Thus:
609 *
610 * head->list->prev->next bit 1 bit 0
611 * ------- -------
612 * Normal page 0 0
613 * Points to head page 0 1
614 * New head page 1 0
615 *
616 * Note we can not trust the prev pointer of the head page, because:
617 *
618 * +----+ +-----+ +-----+
619 * | |------>| T |---X--->| N |
620 * | |<------| | | |
621 * +----+ +-----+ +-----+
622 * ^ ^ |
623 * | +-----+ | |
624 * +----------| R |----------+ |
625 * | |<-----------+
626 * +-----+
627 *
628 * Key: ---X--> HEAD flag set in pointer
629 * T Tail page
630 * R Reader page
631 * N Next page
632 *
633 * (see __rb_reserve_next() to see where this happens)
634 *
635 * What the above shows is that the reader just swapped out
636 * the reader page with a page in the buffer, but before it
637 * could make the new header point back to the new page added
638 * it was preempted by a writer. The writer moved forward onto
639 * the new page added by the reader and is about to move forward
640 * again.
641 *
642 * You can see, it is legitimate for the previous pointer of
643 * the head (or any page) not to point back to itself. But only
644 * temporarially.
645 */
646
647#define RB_PAGE_NORMAL 0UL
648#define RB_PAGE_HEAD 1UL
649#define RB_PAGE_UPDATE 2UL
650
651
652#define RB_FLAG_MASK 3UL
653
654/* PAGE_MOVED is not part of the mask */
655#define RB_PAGE_MOVED 4UL
656
657/*
658 * rb_list_head - remove any bit
659 */
660static struct list_head *rb_list_head(struct list_head *list)
661{
662 unsigned long val = (unsigned long)list;
663
664 return (struct list_head *)(val & ~RB_FLAG_MASK);
665}
666
667/*
Jiri Olsa6d3f1e12009-10-23 19:36:19 -0400668 * rb_is_head_page - test if the given page is the head page
Steven Rostedt77ae3652009-03-27 11:00:29 -0400669 *
670 * Because the reader may move the head_page pointer, we can
671 * not trust what the head page is (it may be pointing to
672 * the reader page). But if the next page is a header page,
673 * its flags will be non zero.
674 */
675static int inline
676rb_is_head_page(struct ring_buffer_per_cpu *cpu_buffer,
677 struct buffer_page *page, struct list_head *list)
678{
679 unsigned long val;
680
681 val = (unsigned long)list->next;
682
683 if ((val & ~RB_FLAG_MASK) != (unsigned long)&page->list)
684 return RB_PAGE_MOVED;
685
686 return val & RB_FLAG_MASK;
687}
688
689/*
690 * rb_is_reader_page
691 *
692 * The unique thing about the reader page, is that, if the
693 * writer is ever on it, the previous pointer never points
694 * back to the reader page.
695 */
696static int rb_is_reader_page(struct buffer_page *page)
697{
698 struct list_head *list = page->list.prev;
699
700 return rb_list_head(list->next) != &page->list;
701}
702
703/*
704 * rb_set_list_to_head - set a list_head to be pointing to head.
705 */
706static void rb_set_list_to_head(struct ring_buffer_per_cpu *cpu_buffer,
707 struct list_head *list)
708{
709 unsigned long *ptr;
710
711 ptr = (unsigned long *)&list->next;
712 *ptr |= RB_PAGE_HEAD;
713 *ptr &= ~RB_PAGE_UPDATE;
714}
715
716/*
717 * rb_head_page_activate - sets up head page
718 */
719static void rb_head_page_activate(struct ring_buffer_per_cpu *cpu_buffer)
720{
721 struct buffer_page *head;
722
723 head = cpu_buffer->head_page;
724 if (!head)
725 return;
726
727 /*
728 * Set the previous list pointer to have the HEAD flag.
729 */
730 rb_set_list_to_head(cpu_buffer, head->list.prev);
731}
732
733static void rb_list_head_clear(struct list_head *list)
734{
735 unsigned long *ptr = (unsigned long *)&list->next;
736
737 *ptr &= ~RB_FLAG_MASK;
738}
739
740/*
741 * rb_head_page_dactivate - clears head page ptr (for free list)
742 */
743static void
744rb_head_page_deactivate(struct ring_buffer_per_cpu *cpu_buffer)
745{
746 struct list_head *hd;
747
748 /* Go through the whole list and clear any pointers found. */
749 rb_list_head_clear(cpu_buffer->pages);
750
751 list_for_each(hd, cpu_buffer->pages)
752 rb_list_head_clear(hd);
753}
754
755static int rb_head_page_set(struct ring_buffer_per_cpu *cpu_buffer,
756 struct buffer_page *head,
757 struct buffer_page *prev,
758 int old_flag, int new_flag)
759{
760 struct list_head *list;
761 unsigned long val = (unsigned long)&head->list;
762 unsigned long ret;
763
764 list = &prev->list;
765
766 val &= ~RB_FLAG_MASK;
767
Steven Rostedt08a40812009-09-14 09:31:35 -0400768 ret = cmpxchg((unsigned long *)&list->next,
769 val | old_flag, val | new_flag);
Steven Rostedt77ae3652009-03-27 11:00:29 -0400770
771 /* check if the reader took the page */
772 if ((ret & ~RB_FLAG_MASK) != val)
773 return RB_PAGE_MOVED;
774
775 return ret & RB_FLAG_MASK;
776}
777
778static int rb_head_page_set_update(struct ring_buffer_per_cpu *cpu_buffer,
779 struct buffer_page *head,
780 struct buffer_page *prev,
781 int old_flag)
782{
783 return rb_head_page_set(cpu_buffer, head, prev,
784 old_flag, RB_PAGE_UPDATE);
785}
786
787static int rb_head_page_set_head(struct ring_buffer_per_cpu *cpu_buffer,
788 struct buffer_page *head,
789 struct buffer_page *prev,
790 int old_flag)
791{
792 return rb_head_page_set(cpu_buffer, head, prev,
793 old_flag, RB_PAGE_HEAD);
794}
795
796static int rb_head_page_set_normal(struct ring_buffer_per_cpu *cpu_buffer,
797 struct buffer_page *head,
798 struct buffer_page *prev,
799 int old_flag)
800{
801 return rb_head_page_set(cpu_buffer, head, prev,
802 old_flag, RB_PAGE_NORMAL);
803}
804
805static inline void rb_inc_page(struct ring_buffer_per_cpu *cpu_buffer,
806 struct buffer_page **bpage)
807{
808 struct list_head *p = rb_list_head((*bpage)->list.next);
809
810 *bpage = list_entry(p, struct buffer_page, list);
811}
812
813static struct buffer_page *
814rb_set_head_page(struct ring_buffer_per_cpu *cpu_buffer)
815{
816 struct buffer_page *head;
817 struct buffer_page *page;
818 struct list_head *list;
819 int i;
820
821 if (RB_WARN_ON(cpu_buffer, !cpu_buffer->head_page))
822 return NULL;
823
824 /* sanity check */
825 list = cpu_buffer->pages;
826 if (RB_WARN_ON(cpu_buffer, rb_list_head(list->prev->next) != list))
827 return NULL;
828
829 page = head = cpu_buffer->head_page;
830 /*
831 * It is possible that the writer moves the header behind
832 * where we started, and we miss in one loop.
833 * A second loop should grab the header, but we'll do
834 * three loops just because I'm paranoid.
835 */
836 for (i = 0; i < 3; i++) {
837 do {
838 if (rb_is_head_page(cpu_buffer, page, page->list.prev)) {
839 cpu_buffer->head_page = page;
840 return page;
841 }
842 rb_inc_page(cpu_buffer, &page);
843 } while (page != head);
844 }
845
846 RB_WARN_ON(cpu_buffer, 1);
847
848 return NULL;
849}
850
851static int rb_head_page_replace(struct buffer_page *old,
852 struct buffer_page *new)
853{
854 unsigned long *ptr = (unsigned long *)&old->list.prev->next;
855 unsigned long val;
856 unsigned long ret;
857
858 val = *ptr & ~RB_FLAG_MASK;
859 val |= RB_PAGE_HEAD;
860
Steven Rostedt08a40812009-09-14 09:31:35 -0400861 ret = cmpxchg(ptr, val, (unsigned long)&new->list);
Steven Rostedt77ae3652009-03-27 11:00:29 -0400862
863 return ret == val;
864}
865
866/*
867 * rb_tail_page_update - move the tail page forward
868 *
869 * Returns 1 if moved tail page, 0 if someone else did.
870 */
871static int rb_tail_page_update(struct ring_buffer_per_cpu *cpu_buffer,
872 struct buffer_page *tail_page,
873 struct buffer_page *next_page)
874{
875 struct buffer_page *old_tail;
876 unsigned long old_entries;
877 unsigned long old_write;
878 int ret = 0;
879
880 /*
881 * The tail page now needs to be moved forward.
882 *
883 * We need to reset the tail page, but without messing
884 * with possible erasing of data brought in by interrupts
885 * that have moved the tail page and are currently on it.
886 *
887 * We add a counter to the write field to denote this.
888 */
889 old_write = local_add_return(RB_WRITE_INTCNT, &next_page->write);
890 old_entries = local_add_return(RB_WRITE_INTCNT, &next_page->entries);
891
892 /*
893 * Just make sure we have seen our old_write and synchronize
894 * with any interrupts that come in.
895 */
896 barrier();
897
898 /*
899 * If the tail page is still the same as what we think
900 * it is, then it is up to us to update the tail
901 * pointer.
902 */
903 if (tail_page == cpu_buffer->tail_page) {
904 /* Zero the write counter */
905 unsigned long val = old_write & ~RB_WRITE_MASK;
906 unsigned long eval = old_entries & ~RB_WRITE_MASK;
907
908 /*
909 * This will only succeed if an interrupt did
910 * not come in and change it. In which case, we
911 * do not want to modify it.
Lai Jiangshanda706d82009-07-15 16:27:30 +0800912 *
913 * We add (void) to let the compiler know that we do not care
914 * about the return value of these functions. We use the
915 * cmpxchg to only update if an interrupt did not already
916 * do it for us. If the cmpxchg fails, we don't care.
Steven Rostedt77ae3652009-03-27 11:00:29 -0400917 */
Lai Jiangshanda706d82009-07-15 16:27:30 +0800918 (void)local_cmpxchg(&next_page->write, old_write, val);
919 (void)local_cmpxchg(&next_page->entries, old_entries, eval);
Steven Rostedt77ae3652009-03-27 11:00:29 -0400920
921 /*
922 * No need to worry about races with clearing out the commit.
923 * it only can increment when a commit takes place. But that
924 * only happens in the outer most nested commit.
925 */
926 local_set(&next_page->page->commit, 0);
927
928 old_tail = cmpxchg(&cpu_buffer->tail_page,
929 tail_page, next_page);
930
931 if (old_tail == tail_page)
932 ret = 1;
933 }
934
935 return ret;
936}
937
938static int rb_check_bpage(struct ring_buffer_per_cpu *cpu_buffer,
939 struct buffer_page *bpage)
940{
941 unsigned long val = (unsigned long)bpage;
942
943 if (RB_WARN_ON(cpu_buffer, val & RB_FLAG_MASK))
944 return 1;
945
946 return 0;
947}
948
949/**
950 * rb_check_list - make sure a pointer to a list has the last bits zero
951 */
952static int rb_check_list(struct ring_buffer_per_cpu *cpu_buffer,
953 struct list_head *list)
954{
955 if (RB_WARN_ON(cpu_buffer, rb_list_head(list->prev) != list->prev))
956 return 1;
957 if (RB_WARN_ON(cpu_buffer, rb_list_head(list->next) != list->next))
958 return 1;
959 return 0;
960}
961
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400962/**
963 * check_pages - integrity check of buffer pages
964 * @cpu_buffer: CPU buffer with pages to test
965 *
Wenji Huangc3706f02009-02-10 01:03:18 -0500966 * As a safety measure we check to make sure the data pages have not
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400967 * been corrupted.
968 */
969static int rb_check_pages(struct ring_buffer_per_cpu *cpu_buffer)
970{
Steven Rostedt3adc54f2009-03-30 15:32:01 -0400971 struct list_head *head = cpu_buffer->pages;
Steven Rostedt044fa782008-12-02 23:50:03 -0500972 struct buffer_page *bpage, *tmp;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400973
Steven Rostedt77ae3652009-03-27 11:00:29 -0400974 rb_head_page_deactivate(cpu_buffer);
975
Steven Rostedt3e89c7b2008-11-11 15:28:41 -0500976 if (RB_WARN_ON(cpu_buffer, head->next->prev != head))
977 return -1;
978 if (RB_WARN_ON(cpu_buffer, head->prev->next != head))
979 return -1;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400980
Steven Rostedt77ae3652009-03-27 11:00:29 -0400981 if (rb_check_list(cpu_buffer, head))
982 return -1;
983
Steven Rostedt044fa782008-12-02 23:50:03 -0500984 list_for_each_entry_safe(bpage, tmp, head, list) {
Steven Rostedt3e89c7b2008-11-11 15:28:41 -0500985 if (RB_WARN_ON(cpu_buffer,
Steven Rostedt044fa782008-12-02 23:50:03 -0500986 bpage->list.next->prev != &bpage->list))
Steven Rostedt3e89c7b2008-11-11 15:28:41 -0500987 return -1;
988 if (RB_WARN_ON(cpu_buffer,
Steven Rostedt044fa782008-12-02 23:50:03 -0500989 bpage->list.prev->next != &bpage->list))
Steven Rostedt3e89c7b2008-11-11 15:28:41 -0500990 return -1;
Steven Rostedt77ae3652009-03-27 11:00:29 -0400991 if (rb_check_list(cpu_buffer, &bpage->list))
992 return -1;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400993 }
994
Steven Rostedt77ae3652009-03-27 11:00:29 -0400995 rb_head_page_activate(cpu_buffer);
996
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400997 return 0;
998}
999
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001000static int rb_allocate_pages(struct ring_buffer_per_cpu *cpu_buffer,
1001 unsigned nr_pages)
1002{
Steven Rostedt044fa782008-12-02 23:50:03 -05001003 struct buffer_page *bpage, *tmp;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001004 unsigned long addr;
1005 LIST_HEAD(pages);
1006 unsigned i;
1007
Steven Rostedt3adc54f2009-03-30 15:32:01 -04001008 WARN_ON(!nr_pages);
1009
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001010 for (i = 0; i < nr_pages; i++) {
Steven Rostedt044fa782008-12-02 23:50:03 -05001011 bpage = kzalloc_node(ALIGN(sizeof(*bpage), cache_line_size()),
Steven Rostedtaa1e0e32008-10-02 19:18:09 -04001012 GFP_KERNEL, cpu_to_node(cpu_buffer->cpu));
Steven Rostedt044fa782008-12-02 23:50:03 -05001013 if (!bpage)
Steven Rostedte4c2ce82008-10-01 11:14:54 -04001014 goto free_pages;
Steven Rostedt77ae3652009-03-27 11:00:29 -04001015
1016 rb_check_bpage(cpu_buffer, bpage);
1017
Steven Rostedt044fa782008-12-02 23:50:03 -05001018 list_add(&bpage->list, &pages);
Steven Rostedte4c2ce82008-10-01 11:14:54 -04001019
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001020 addr = __get_free_page(GFP_KERNEL);
1021 if (!addr)
1022 goto free_pages;
Steven Rostedt044fa782008-12-02 23:50:03 -05001023 bpage->page = (void *)addr;
1024 rb_init_page(bpage->page);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001025 }
1026
Steven Rostedt3adc54f2009-03-30 15:32:01 -04001027 /*
1028 * The ring buffer page list is a circular list that does not
1029 * start and end with a list head. All page list items point to
1030 * other pages.
1031 */
1032 cpu_buffer->pages = pages.next;
1033 list_del(&pages);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001034
1035 rb_check_pages(cpu_buffer);
1036
1037 return 0;
1038
1039 free_pages:
Steven Rostedt044fa782008-12-02 23:50:03 -05001040 list_for_each_entry_safe(bpage, tmp, &pages, list) {
1041 list_del_init(&bpage->list);
1042 free_buffer_page(bpage);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001043 }
1044 return -ENOMEM;
1045}
1046
1047static struct ring_buffer_per_cpu *
1048rb_allocate_cpu_buffer(struct ring_buffer *buffer, int cpu)
1049{
1050 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedt044fa782008-12-02 23:50:03 -05001051 struct buffer_page *bpage;
Steven Rostedtd7690412008-10-01 00:29:53 -04001052 unsigned long addr;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001053 int ret;
1054
1055 cpu_buffer = kzalloc_node(ALIGN(sizeof(*cpu_buffer), cache_line_size()),
1056 GFP_KERNEL, cpu_to_node(cpu));
1057 if (!cpu_buffer)
1058 return NULL;
1059
1060 cpu_buffer->cpu = cpu;
1061 cpu_buffer->buffer = buffer;
Steven Rostedtf83c9d02008-11-11 18:47:44 +01001062 spin_lock_init(&cpu_buffer->reader_lock);
Peter Zijlstra1f8a6a12009-06-08 18:18:39 +02001063 lockdep_set_class(&cpu_buffer->reader_lock, buffer->reader_lock_key);
Thomas Gleixneredc35bd2009-12-03 12:38:57 +01001064 cpu_buffer->lock = (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001065
Steven Rostedt044fa782008-12-02 23:50:03 -05001066 bpage = kzalloc_node(ALIGN(sizeof(*bpage), cache_line_size()),
Steven Rostedte4c2ce82008-10-01 11:14:54 -04001067 GFP_KERNEL, cpu_to_node(cpu));
Steven Rostedt044fa782008-12-02 23:50:03 -05001068 if (!bpage)
Steven Rostedte4c2ce82008-10-01 11:14:54 -04001069 goto fail_free_buffer;
1070
Steven Rostedt77ae3652009-03-27 11:00:29 -04001071 rb_check_bpage(cpu_buffer, bpage);
1072
Steven Rostedt044fa782008-12-02 23:50:03 -05001073 cpu_buffer->reader_page = bpage;
Steven Rostedtd7690412008-10-01 00:29:53 -04001074 addr = __get_free_page(GFP_KERNEL);
1075 if (!addr)
Steven Rostedte4c2ce82008-10-01 11:14:54 -04001076 goto fail_free_reader;
Steven Rostedt044fa782008-12-02 23:50:03 -05001077 bpage->page = (void *)addr;
1078 rb_init_page(bpage->page);
Steven Rostedte4c2ce82008-10-01 11:14:54 -04001079
Steven Rostedtd7690412008-10-01 00:29:53 -04001080 INIT_LIST_HEAD(&cpu_buffer->reader_page->list);
Steven Rostedtd7690412008-10-01 00:29:53 -04001081
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001082 ret = rb_allocate_pages(cpu_buffer, buffer->pages);
1083 if (ret < 0)
Steven Rostedtd7690412008-10-01 00:29:53 -04001084 goto fail_free_reader;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001085
1086 cpu_buffer->head_page
Steven Rostedt3adc54f2009-03-30 15:32:01 -04001087 = list_entry(cpu_buffer->pages, struct buffer_page, list);
Steven Rostedtbf41a152008-10-04 02:00:59 -04001088 cpu_buffer->tail_page = cpu_buffer->commit_page = cpu_buffer->head_page;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001089
Steven Rostedt77ae3652009-03-27 11:00:29 -04001090 rb_head_page_activate(cpu_buffer);
1091
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001092 return cpu_buffer;
1093
Steven Rostedtd7690412008-10-01 00:29:53 -04001094 fail_free_reader:
1095 free_buffer_page(cpu_buffer->reader_page);
1096
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001097 fail_free_buffer:
1098 kfree(cpu_buffer);
1099 return NULL;
1100}
1101
1102static void rb_free_cpu_buffer(struct ring_buffer_per_cpu *cpu_buffer)
1103{
Steven Rostedt3adc54f2009-03-30 15:32:01 -04001104 struct list_head *head = cpu_buffer->pages;
Steven Rostedt044fa782008-12-02 23:50:03 -05001105 struct buffer_page *bpage, *tmp;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001106
Steven Rostedtd7690412008-10-01 00:29:53 -04001107 free_buffer_page(cpu_buffer->reader_page);
1108
Steven Rostedt77ae3652009-03-27 11:00:29 -04001109 rb_head_page_deactivate(cpu_buffer);
1110
Steven Rostedt3adc54f2009-03-30 15:32:01 -04001111 if (head) {
1112 list_for_each_entry_safe(bpage, tmp, head, list) {
1113 list_del_init(&bpage->list);
1114 free_buffer_page(bpage);
1115 }
1116 bpage = list_entry(head, struct buffer_page, list);
Steven Rostedt044fa782008-12-02 23:50:03 -05001117 free_buffer_page(bpage);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001118 }
Steven Rostedt3adc54f2009-03-30 15:32:01 -04001119
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001120 kfree(cpu_buffer);
1121}
1122
Steven Rostedt59222ef2009-03-12 11:46:03 -04001123#ifdef CONFIG_HOTPLUG_CPU
Frederic Weisbecker09c9e842009-03-21 04:33:36 +01001124static int rb_cpu_notify(struct notifier_block *self,
1125 unsigned long action, void *hcpu);
Steven Rostedt554f7862009-03-11 22:00:13 -04001126#endif
1127
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001128/**
1129 * ring_buffer_alloc - allocate a new ring_buffer
Robert Richter68814b52008-11-24 12:24:12 +01001130 * @size: the size in bytes per cpu that is needed.
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001131 * @flags: attributes to set for the ring buffer.
1132 *
1133 * Currently the only flag that is available is the RB_FL_OVERWRITE
1134 * flag. This flag means that the buffer will overwrite old data
1135 * when the buffer wraps. If this flag is not set, the buffer will
1136 * drop data when the tail hits the head.
1137 */
Peter Zijlstra1f8a6a12009-06-08 18:18:39 +02001138struct ring_buffer *__ring_buffer_alloc(unsigned long size, unsigned flags,
1139 struct lock_class_key *key)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001140{
1141 struct ring_buffer *buffer;
1142 int bsize;
1143 int cpu;
1144
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001145 /* keep it in its own cache line */
1146 buffer = kzalloc(ALIGN(sizeof(*buffer), cache_line_size()),
1147 GFP_KERNEL);
1148 if (!buffer)
1149 return NULL;
1150
Rusty Russell9e01c1b2009-01-01 10:12:22 +10301151 if (!alloc_cpumask_var(&buffer->cpumask, GFP_KERNEL))
1152 goto fail_free_buffer;
1153
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001154 buffer->pages = DIV_ROUND_UP(size, BUF_PAGE_SIZE);
1155 buffer->flags = flags;
Steven Rostedt37886f62009-03-17 17:22:06 -04001156 buffer->clock = trace_clock_local;
Peter Zijlstra1f8a6a12009-06-08 18:18:39 +02001157 buffer->reader_lock_key = key;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001158
1159 /* need at least two pages */
Steven Rostedt5f78abe2009-06-17 14:11:10 -04001160 if (buffer->pages < 2)
1161 buffer->pages = 2;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001162
Frederic Weisbecker3bf832c2009-03-19 14:47:33 +01001163 /*
1164 * In case of non-hotplug cpu, if the ring-buffer is allocated
1165 * in early initcall, it will not be notified of secondary cpus.
1166 * In that off case, we need to allocate for all possible cpus.
1167 */
1168#ifdef CONFIG_HOTPLUG_CPU
Steven Rostedt554f7862009-03-11 22:00:13 -04001169 get_online_cpus();
1170 cpumask_copy(buffer->cpumask, cpu_online_mask);
Frederic Weisbecker3bf832c2009-03-19 14:47:33 +01001171#else
1172 cpumask_copy(buffer->cpumask, cpu_possible_mask);
1173#endif
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001174 buffer->cpus = nr_cpu_ids;
1175
1176 bsize = sizeof(void *) * nr_cpu_ids;
1177 buffer->buffers = kzalloc(ALIGN(bsize, cache_line_size()),
1178 GFP_KERNEL);
1179 if (!buffer->buffers)
Rusty Russell9e01c1b2009-01-01 10:12:22 +10301180 goto fail_free_cpumask;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001181
1182 for_each_buffer_cpu(buffer, cpu) {
1183 buffer->buffers[cpu] =
1184 rb_allocate_cpu_buffer(buffer, cpu);
1185 if (!buffer->buffers[cpu])
1186 goto fail_free_buffers;
1187 }
1188
Steven Rostedt59222ef2009-03-12 11:46:03 -04001189#ifdef CONFIG_HOTPLUG_CPU
Steven Rostedt554f7862009-03-11 22:00:13 -04001190 buffer->cpu_notify.notifier_call = rb_cpu_notify;
1191 buffer->cpu_notify.priority = 0;
1192 register_cpu_notifier(&buffer->cpu_notify);
1193#endif
1194
1195 put_online_cpus();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001196 mutex_init(&buffer->mutex);
1197
1198 return buffer;
1199
1200 fail_free_buffers:
1201 for_each_buffer_cpu(buffer, cpu) {
1202 if (buffer->buffers[cpu])
1203 rb_free_cpu_buffer(buffer->buffers[cpu]);
1204 }
1205 kfree(buffer->buffers);
1206
Rusty Russell9e01c1b2009-01-01 10:12:22 +10301207 fail_free_cpumask:
1208 free_cpumask_var(buffer->cpumask);
Steven Rostedt554f7862009-03-11 22:00:13 -04001209 put_online_cpus();
Rusty Russell9e01c1b2009-01-01 10:12:22 +10301210
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001211 fail_free_buffer:
1212 kfree(buffer);
1213 return NULL;
1214}
Peter Zijlstra1f8a6a12009-06-08 18:18:39 +02001215EXPORT_SYMBOL_GPL(__ring_buffer_alloc);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001216
1217/**
1218 * ring_buffer_free - free a ring buffer.
1219 * @buffer: the buffer to free.
1220 */
1221void
1222ring_buffer_free(struct ring_buffer *buffer)
1223{
1224 int cpu;
1225
Steven Rostedt554f7862009-03-11 22:00:13 -04001226 get_online_cpus();
1227
Steven Rostedt59222ef2009-03-12 11:46:03 -04001228#ifdef CONFIG_HOTPLUG_CPU
Steven Rostedt554f7862009-03-11 22:00:13 -04001229 unregister_cpu_notifier(&buffer->cpu_notify);
1230#endif
1231
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001232 for_each_buffer_cpu(buffer, cpu)
1233 rb_free_cpu_buffer(buffer->buffers[cpu]);
1234
Steven Rostedt554f7862009-03-11 22:00:13 -04001235 put_online_cpus();
1236
Eric Dumazetbd3f0222009-08-07 12:49:29 +02001237 kfree(buffer->buffers);
Rusty Russell9e01c1b2009-01-01 10:12:22 +10301238 free_cpumask_var(buffer->cpumask);
1239
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001240 kfree(buffer);
1241}
Robert Richterc4f50182008-12-11 16:49:22 +01001242EXPORT_SYMBOL_GPL(ring_buffer_free);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001243
Steven Rostedt37886f62009-03-17 17:22:06 -04001244void ring_buffer_set_clock(struct ring_buffer *buffer,
1245 u64 (*clock)(void))
1246{
1247 buffer->clock = clock;
1248}
1249
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001250static void rb_reset_cpu(struct ring_buffer_per_cpu *cpu_buffer);
1251
1252static void
1253rb_remove_pages(struct ring_buffer_per_cpu *cpu_buffer, unsigned nr_pages)
1254{
Steven Rostedt044fa782008-12-02 23:50:03 -05001255 struct buffer_page *bpage;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001256 struct list_head *p;
1257 unsigned i;
1258
Lai Jiangshanf7112942009-11-03 19:42:45 +08001259 spin_lock_irq(&cpu_buffer->reader_lock);
Steven Rostedt77ae3652009-03-27 11:00:29 -04001260 rb_head_page_deactivate(cpu_buffer);
1261
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001262 for (i = 0; i < nr_pages; i++) {
Steven Rostedt3adc54f2009-03-30 15:32:01 -04001263 if (RB_WARN_ON(cpu_buffer, list_empty(cpu_buffer->pages)))
Julia Lawall292f60c2010-03-29 17:37:02 +02001264 goto out;
Steven Rostedt3adc54f2009-03-30 15:32:01 -04001265 p = cpu_buffer->pages->next;
Steven Rostedt044fa782008-12-02 23:50:03 -05001266 bpage = list_entry(p, struct buffer_page, list);
1267 list_del_init(&bpage->list);
1268 free_buffer_page(bpage);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001269 }
Steven Rostedt3adc54f2009-03-30 15:32:01 -04001270 if (RB_WARN_ON(cpu_buffer, list_empty(cpu_buffer->pages)))
Julia Lawall292f60c2010-03-29 17:37:02 +02001271 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001272
1273 rb_reset_cpu(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001274 rb_check_pages(cpu_buffer);
1275
Julia Lawall292f60c2010-03-29 17:37:02 +02001276out:
Steven Rostedtdd7f5942009-12-10 23:20:52 -05001277 spin_unlock_irq(&cpu_buffer->reader_lock);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001278}
1279
1280static void
1281rb_insert_pages(struct ring_buffer_per_cpu *cpu_buffer,
1282 struct list_head *pages, unsigned nr_pages)
1283{
Steven Rostedt044fa782008-12-02 23:50:03 -05001284 struct buffer_page *bpage;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001285 struct list_head *p;
1286 unsigned i;
1287
Steven Rostedt77ae3652009-03-27 11:00:29 -04001288 spin_lock_irq(&cpu_buffer->reader_lock);
1289 rb_head_page_deactivate(cpu_buffer);
1290
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001291 for (i = 0; i < nr_pages; i++) {
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05001292 if (RB_WARN_ON(cpu_buffer, list_empty(pages)))
Julia Lawall292f60c2010-03-29 17:37:02 +02001293 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001294 p = pages->next;
Steven Rostedt044fa782008-12-02 23:50:03 -05001295 bpage = list_entry(p, struct buffer_page, list);
1296 list_del_init(&bpage->list);
Steven Rostedt3adc54f2009-03-30 15:32:01 -04001297 list_add_tail(&bpage->list, cpu_buffer->pages);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001298 }
1299 rb_reset_cpu(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001300 rb_check_pages(cpu_buffer);
1301
Julia Lawall292f60c2010-03-29 17:37:02 +02001302out:
Steven Rostedtdd7f5942009-12-10 23:20:52 -05001303 spin_unlock_irq(&cpu_buffer->reader_lock);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001304}
1305
1306/**
1307 * ring_buffer_resize - resize the ring buffer
1308 * @buffer: the buffer to resize.
1309 * @size: the new size.
1310 *
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001311 * Minimum size is 2 * BUF_PAGE_SIZE.
1312 *
1313 * Returns -1 on failure.
1314 */
1315int ring_buffer_resize(struct ring_buffer *buffer, unsigned long size)
1316{
1317 struct ring_buffer_per_cpu *cpu_buffer;
1318 unsigned nr_pages, rm_pages, new_pages;
Steven Rostedt044fa782008-12-02 23:50:03 -05001319 struct buffer_page *bpage, *tmp;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001320 unsigned long buffer_size;
1321 unsigned long addr;
1322 LIST_HEAD(pages);
1323 int i, cpu;
1324
Ingo Molnaree51a1d2008-11-13 14:58:31 +01001325 /*
1326 * Always succeed at resizing a non-existent buffer:
1327 */
1328 if (!buffer)
1329 return size;
1330
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001331 size = DIV_ROUND_UP(size, BUF_PAGE_SIZE);
1332 size *= BUF_PAGE_SIZE;
1333 buffer_size = buffer->pages * BUF_PAGE_SIZE;
1334
1335 /* we need a minimum of two pages */
1336 if (size < BUF_PAGE_SIZE * 2)
1337 size = BUF_PAGE_SIZE * 2;
1338
1339 if (size == buffer_size)
1340 return size;
1341
Steven Rostedt18421012009-12-10 22:54:27 -05001342 atomic_inc(&buffer->record_disabled);
1343
1344 /* Make sure all writers are done with this buffer. */
1345 synchronize_sched();
1346
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001347 mutex_lock(&buffer->mutex);
Steven Rostedt554f7862009-03-11 22:00:13 -04001348 get_online_cpus();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001349
1350 nr_pages = DIV_ROUND_UP(size, BUF_PAGE_SIZE);
1351
1352 if (size < buffer_size) {
1353
1354 /* easy case, just free pages */
Steven Rostedt554f7862009-03-11 22:00:13 -04001355 if (RB_WARN_ON(buffer, nr_pages >= buffer->pages))
1356 goto out_fail;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001357
1358 rm_pages = buffer->pages - nr_pages;
1359
1360 for_each_buffer_cpu(buffer, cpu) {
1361 cpu_buffer = buffer->buffers[cpu];
1362 rb_remove_pages(cpu_buffer, rm_pages);
1363 }
1364 goto out;
1365 }
1366
1367 /*
1368 * This is a bit more difficult. We only want to add pages
1369 * when we can allocate enough for all CPUs. We do this
1370 * by allocating all the pages and storing them on a local
1371 * link list. If we succeed in our allocation, then we
1372 * add these pages to the cpu_buffers. Otherwise we just free
1373 * them all and return -ENOMEM;
1374 */
Steven Rostedt554f7862009-03-11 22:00:13 -04001375 if (RB_WARN_ON(buffer, nr_pages <= buffer->pages))
1376 goto out_fail;
Steven Rostedtf536aaf2008-11-10 23:07:30 -05001377
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001378 new_pages = nr_pages - buffer->pages;
1379
1380 for_each_buffer_cpu(buffer, cpu) {
1381 for (i = 0; i < new_pages; i++) {
Steven Rostedt044fa782008-12-02 23:50:03 -05001382 bpage = kzalloc_node(ALIGN(sizeof(*bpage),
Steven Rostedte4c2ce82008-10-01 11:14:54 -04001383 cache_line_size()),
1384 GFP_KERNEL, cpu_to_node(cpu));
Steven Rostedt044fa782008-12-02 23:50:03 -05001385 if (!bpage)
Steven Rostedte4c2ce82008-10-01 11:14:54 -04001386 goto free_pages;
Steven Rostedt044fa782008-12-02 23:50:03 -05001387 list_add(&bpage->list, &pages);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001388 addr = __get_free_page(GFP_KERNEL);
1389 if (!addr)
1390 goto free_pages;
Steven Rostedt044fa782008-12-02 23:50:03 -05001391 bpage->page = (void *)addr;
1392 rb_init_page(bpage->page);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001393 }
1394 }
1395
1396 for_each_buffer_cpu(buffer, cpu) {
1397 cpu_buffer = buffer->buffers[cpu];
1398 rb_insert_pages(cpu_buffer, &pages, new_pages);
1399 }
1400
Steven Rostedt554f7862009-03-11 22:00:13 -04001401 if (RB_WARN_ON(buffer, !list_empty(&pages)))
1402 goto out_fail;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001403
1404 out:
1405 buffer->pages = nr_pages;
Steven Rostedt554f7862009-03-11 22:00:13 -04001406 put_online_cpus();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001407 mutex_unlock(&buffer->mutex);
1408
Steven Rostedt18421012009-12-10 22:54:27 -05001409 atomic_dec(&buffer->record_disabled);
1410
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001411 return size;
1412
1413 free_pages:
Steven Rostedt044fa782008-12-02 23:50:03 -05001414 list_for_each_entry_safe(bpage, tmp, &pages, list) {
1415 list_del_init(&bpage->list);
1416 free_buffer_page(bpage);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001417 }
Steven Rostedt554f7862009-03-11 22:00:13 -04001418 put_online_cpus();
Vegard Nossum641d2f62008-11-18 19:22:13 +01001419 mutex_unlock(&buffer->mutex);
Steven Rostedt18421012009-12-10 22:54:27 -05001420 atomic_dec(&buffer->record_disabled);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001421 return -ENOMEM;
Steven Rostedt554f7862009-03-11 22:00:13 -04001422
1423 /*
1424 * Something went totally wrong, and we are too paranoid
1425 * to even clean up the mess.
1426 */
1427 out_fail:
1428 put_online_cpus();
1429 mutex_unlock(&buffer->mutex);
Steven Rostedt18421012009-12-10 22:54:27 -05001430 atomic_dec(&buffer->record_disabled);
Steven Rostedt554f7862009-03-11 22:00:13 -04001431 return -1;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001432}
Robert Richterc4f50182008-12-11 16:49:22 +01001433EXPORT_SYMBOL_GPL(ring_buffer_resize);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001434
Steven Rostedt8789a9e2008-12-02 15:34:07 -05001435static inline void *
Steven Rostedt044fa782008-12-02 23:50:03 -05001436__rb_data_page_index(struct buffer_data_page *bpage, unsigned index)
Steven Rostedt8789a9e2008-12-02 15:34:07 -05001437{
Steven Rostedt044fa782008-12-02 23:50:03 -05001438 return bpage->data + index;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05001439}
1440
Steven Rostedt044fa782008-12-02 23:50:03 -05001441static inline void *__rb_page_index(struct buffer_page *bpage, unsigned index)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001442{
Steven Rostedt044fa782008-12-02 23:50:03 -05001443 return bpage->page->data + index;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001444}
1445
1446static inline struct ring_buffer_event *
Steven Rostedtd7690412008-10-01 00:29:53 -04001447rb_reader_event(struct ring_buffer_per_cpu *cpu_buffer)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001448{
Steven Rostedt6f807ac2008-10-04 02:00:58 -04001449 return __rb_page_index(cpu_buffer->reader_page,
1450 cpu_buffer->reader_page->read);
1451}
1452
1453static inline struct ring_buffer_event *
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001454rb_iter_head_event(struct ring_buffer_iter *iter)
1455{
Steven Rostedt6f807ac2008-10-04 02:00:58 -04001456 return __rb_page_index(iter->head_page, iter->head);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001457}
1458
Steven Rostedt77ae3652009-03-27 11:00:29 -04001459static inline unsigned long rb_page_write(struct buffer_page *bpage)
Steven Rostedtbf41a152008-10-04 02:00:59 -04001460{
Steven Rostedt77ae3652009-03-27 11:00:29 -04001461 return local_read(&bpage->write) & RB_WRITE_MASK;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001462}
1463
1464static inline unsigned rb_page_commit(struct buffer_page *bpage)
1465{
Steven Rostedtabc9b562008-12-02 15:34:06 -05001466 return local_read(&bpage->page->commit);
Steven Rostedtbf41a152008-10-04 02:00:59 -04001467}
1468
Steven Rostedt77ae3652009-03-27 11:00:29 -04001469static inline unsigned long rb_page_entries(struct buffer_page *bpage)
1470{
1471 return local_read(&bpage->entries) & RB_WRITE_MASK;
1472}
1473
Steven Rostedtbf41a152008-10-04 02:00:59 -04001474/* Size is determined by what has been commited */
1475static inline unsigned rb_page_size(struct buffer_page *bpage)
1476{
1477 return rb_page_commit(bpage);
1478}
1479
1480static inline unsigned
1481rb_commit_index(struct ring_buffer_per_cpu *cpu_buffer)
1482{
1483 return rb_page_commit(cpu_buffer->commit_page);
1484}
1485
Steven Rostedtbf41a152008-10-04 02:00:59 -04001486static inline unsigned
1487rb_event_index(struct ring_buffer_event *event)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001488{
Steven Rostedtbf41a152008-10-04 02:00:59 -04001489 unsigned long addr = (unsigned long)event;
1490
Steven Rostedt22f470f2009-06-11 09:29:58 -04001491 return (addr & ~PAGE_MASK) - BUF_PAGE_HDR_SIZE;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001492}
1493
Steven Rostedt0f0c85f2009-05-11 16:08:00 -04001494static inline int
Steven Rostedtfa743952009-06-16 12:37:57 -04001495rb_event_is_commit(struct ring_buffer_per_cpu *cpu_buffer,
1496 struct ring_buffer_event *event)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001497{
Steven Rostedtbf41a152008-10-04 02:00:59 -04001498 unsigned long addr = (unsigned long)event;
1499 unsigned long index;
1500
1501 index = rb_event_index(event);
1502 addr &= PAGE_MASK;
1503
1504 return cpu_buffer->commit_page->page == (void *)addr &&
1505 rb_commit_index(cpu_buffer) == index;
1506}
1507
Andrew Morton34a148b2009-01-09 12:27:09 -08001508static void
Steven Rostedtbf41a152008-10-04 02:00:59 -04001509rb_set_commit_to_write(struct ring_buffer_per_cpu *cpu_buffer)
1510{
Steven Rostedt77ae3652009-03-27 11:00:29 -04001511 unsigned long max_count;
1512
Steven Rostedtbf41a152008-10-04 02:00:59 -04001513 /*
1514 * We only race with interrupts and NMIs on this CPU.
1515 * If we own the commit event, then we can commit
1516 * all others that interrupted us, since the interruptions
1517 * are in stack format (they finish before they come
1518 * back to us). This allows us to do a simple loop to
1519 * assign the commit to the tail.
1520 */
Steven Rostedta8ccf1d2008-12-23 11:32:24 -05001521 again:
Steven Rostedt77ae3652009-03-27 11:00:29 -04001522 max_count = cpu_buffer->buffer->pages * 100;
1523
Steven Rostedtbf41a152008-10-04 02:00:59 -04001524 while (cpu_buffer->commit_page != cpu_buffer->tail_page) {
Steven Rostedt77ae3652009-03-27 11:00:29 -04001525 if (RB_WARN_ON(cpu_buffer, !(--max_count)))
1526 return;
1527 if (RB_WARN_ON(cpu_buffer,
1528 rb_is_reader_page(cpu_buffer->tail_page)))
1529 return;
1530 local_set(&cpu_buffer->commit_page->page->commit,
1531 rb_page_write(cpu_buffer->commit_page));
Steven Rostedtbf41a152008-10-04 02:00:59 -04001532 rb_inc_page(cpu_buffer, &cpu_buffer->commit_page);
Steven Rostedtabc9b562008-12-02 15:34:06 -05001533 cpu_buffer->write_stamp =
1534 cpu_buffer->commit_page->page->time_stamp;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001535 /* add barrier to keep gcc from optimizing too much */
1536 barrier();
1537 }
1538 while (rb_commit_index(cpu_buffer) !=
1539 rb_page_write(cpu_buffer->commit_page)) {
Steven Rostedt77ae3652009-03-27 11:00:29 -04001540
1541 local_set(&cpu_buffer->commit_page->page->commit,
1542 rb_page_write(cpu_buffer->commit_page));
1543 RB_WARN_ON(cpu_buffer,
1544 local_read(&cpu_buffer->commit_page->page->commit) &
1545 ~RB_WRITE_MASK);
Steven Rostedtbf41a152008-10-04 02:00:59 -04001546 barrier();
1547 }
Steven Rostedta8ccf1d2008-12-23 11:32:24 -05001548
1549 /* again, keep gcc from optimizing */
1550 barrier();
1551
1552 /*
1553 * If an interrupt came in just after the first while loop
1554 * and pushed the tail page forward, we will be left with
1555 * a dangling commit that will never go forward.
1556 */
1557 if (unlikely(cpu_buffer->commit_page != cpu_buffer->tail_page))
1558 goto again;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001559}
1560
Steven Rostedtd7690412008-10-01 00:29:53 -04001561static void rb_reset_reader_page(struct ring_buffer_per_cpu *cpu_buffer)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001562{
Steven Rostedtabc9b562008-12-02 15:34:06 -05001563 cpu_buffer->read_stamp = cpu_buffer->reader_page->page->time_stamp;
Steven Rostedt6f807ac2008-10-04 02:00:58 -04001564 cpu_buffer->reader_page->read = 0;
Steven Rostedtd7690412008-10-01 00:29:53 -04001565}
1566
Andrew Morton34a148b2009-01-09 12:27:09 -08001567static void rb_inc_iter(struct ring_buffer_iter *iter)
Steven Rostedtd7690412008-10-01 00:29:53 -04001568{
1569 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
1570
1571 /*
1572 * The iterator could be on the reader page (it starts there).
1573 * But the head could have moved, since the reader was
1574 * found. Check for this case and assign the iterator
1575 * to the head page instead of next.
1576 */
1577 if (iter->head_page == cpu_buffer->reader_page)
Steven Rostedt77ae3652009-03-27 11:00:29 -04001578 iter->head_page = rb_set_head_page(cpu_buffer);
Steven Rostedtd7690412008-10-01 00:29:53 -04001579 else
1580 rb_inc_page(cpu_buffer, &iter->head_page);
1581
Steven Rostedtabc9b562008-12-02 15:34:06 -05001582 iter->read_stamp = iter->head_page->page->time_stamp;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001583 iter->head = 0;
1584}
1585
Steven Rostedt69d1b832010-10-07 18:18:05 -04001586/* Slow path, do not inline */
1587static noinline struct ring_buffer_event *
1588rb_add_time_stamp(struct ring_buffer_event *event, u64 delta)
1589{
1590 event->type_len = RINGBUF_TYPE_TIME_EXTEND;
1591
1592 /* Not the first event on the page? */
1593 if (rb_event_index(event)) {
1594 event->time_delta = delta & TS_MASK;
1595 event->array[0] = delta >> TS_SHIFT;
1596 } else {
1597 /* nope, just zero it */
1598 event->time_delta = 0;
1599 event->array[0] = 0;
1600 }
1601
1602 return skip_time_extend(event);
1603}
1604
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001605/**
1606 * ring_buffer_update_event - update event type and data
1607 * @event: the even to update
1608 * @type: the type of event
1609 * @length: the size of the event field in the ring buffer
1610 *
1611 * Update the type and data fields of the event. The length
1612 * is the actual size that is written to the ring buffer,
1613 * and with this, we can determine what to place into the
1614 * data field.
1615 */
Andrew Morton34a148b2009-01-09 12:27:09 -08001616static void
Steven Rostedt69d1b832010-10-07 18:18:05 -04001617rb_update_event(struct ring_buffer_per_cpu *cpu_buffer,
1618 struct ring_buffer_event *event, unsigned length,
1619 int add_timestamp, u64 delta)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001620{
Steven Rostedt69d1b832010-10-07 18:18:05 -04001621 /* Only a commit updates the timestamp */
1622 if (unlikely(!rb_event_is_commit(cpu_buffer, event)))
1623 delta = 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001624
Steven Rostedt69d1b832010-10-07 18:18:05 -04001625 /*
1626 * If we need to add a timestamp, then we
1627 * add it to the start of the resevered space.
1628 */
1629 if (unlikely(add_timestamp)) {
1630 event = rb_add_time_stamp(event, delta);
1631 length -= RB_LEN_TIME_EXTEND;
1632 delta = 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001633 }
Steven Rostedt69d1b832010-10-07 18:18:05 -04001634
1635 event->time_delta = delta;
1636 length -= RB_EVNT_HDR_SIZE;
1637 if (length > RB_MAX_SMALL_DATA || RB_FORCE_8BYTE_ALIGNMENT) {
1638 event->type_len = 0;
1639 event->array[0] = length;
1640 } else
1641 event->type_len = DIV_ROUND_UP(length, RB_ALIGNMENT);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001642}
1643
Steven Rostedt77ae3652009-03-27 11:00:29 -04001644/*
1645 * rb_handle_head_page - writer hit the head page
1646 *
1647 * Returns: +1 to retry page
1648 * 0 to continue
1649 * -1 on error
1650 */
1651static int
1652rb_handle_head_page(struct ring_buffer_per_cpu *cpu_buffer,
1653 struct buffer_page *tail_page,
1654 struct buffer_page *next_page)
1655{
1656 struct buffer_page *new_head;
1657 int entries;
1658 int type;
1659 int ret;
1660
1661 entries = rb_page_entries(next_page);
1662
1663 /*
1664 * The hard part is here. We need to move the head
1665 * forward, and protect against both readers on
1666 * other CPUs and writers coming in via interrupts.
1667 */
1668 type = rb_head_page_set_update(cpu_buffer, next_page, tail_page,
1669 RB_PAGE_HEAD);
1670
1671 /*
1672 * type can be one of four:
1673 * NORMAL - an interrupt already moved it for us
1674 * HEAD - we are the first to get here.
1675 * UPDATE - we are the interrupt interrupting
1676 * a current move.
1677 * MOVED - a reader on another CPU moved the next
1678 * pointer to its reader page. Give up
1679 * and try again.
1680 */
1681
1682 switch (type) {
1683 case RB_PAGE_HEAD:
1684 /*
1685 * We changed the head to UPDATE, thus
1686 * it is our responsibility to update
1687 * the counters.
1688 */
1689 local_add(entries, &cpu_buffer->overrun);
1690
1691 /*
1692 * The entries will be zeroed out when we move the
1693 * tail page.
1694 */
1695
1696 /* still more to do */
1697 break;
1698
1699 case RB_PAGE_UPDATE:
1700 /*
1701 * This is an interrupt that interrupt the
1702 * previous update. Still more to do.
1703 */
1704 break;
1705 case RB_PAGE_NORMAL:
1706 /*
1707 * An interrupt came in before the update
1708 * and processed this for us.
1709 * Nothing left to do.
1710 */
1711 return 1;
1712 case RB_PAGE_MOVED:
1713 /*
1714 * The reader is on another CPU and just did
1715 * a swap with our next_page.
1716 * Try again.
1717 */
1718 return 1;
1719 default:
1720 RB_WARN_ON(cpu_buffer, 1); /* WTF??? */
1721 return -1;
1722 }
1723
1724 /*
1725 * Now that we are here, the old head pointer is
1726 * set to UPDATE. This will keep the reader from
1727 * swapping the head page with the reader page.
1728 * The reader (on another CPU) will spin till
1729 * we are finished.
1730 *
1731 * We just need to protect against interrupts
1732 * doing the job. We will set the next pointer
1733 * to HEAD. After that, we set the old pointer
1734 * to NORMAL, but only if it was HEAD before.
1735 * otherwise we are an interrupt, and only
1736 * want the outer most commit to reset it.
1737 */
1738 new_head = next_page;
1739 rb_inc_page(cpu_buffer, &new_head);
1740
1741 ret = rb_head_page_set_head(cpu_buffer, new_head, next_page,
1742 RB_PAGE_NORMAL);
1743
1744 /*
1745 * Valid returns are:
1746 * HEAD - an interrupt came in and already set it.
1747 * NORMAL - One of two things:
1748 * 1) We really set it.
1749 * 2) A bunch of interrupts came in and moved
1750 * the page forward again.
1751 */
1752 switch (ret) {
1753 case RB_PAGE_HEAD:
1754 case RB_PAGE_NORMAL:
1755 /* OK */
1756 break;
1757 default:
1758 RB_WARN_ON(cpu_buffer, 1);
1759 return -1;
1760 }
1761
1762 /*
1763 * It is possible that an interrupt came in,
1764 * set the head up, then more interrupts came in
1765 * and moved it again. When we get back here,
1766 * the page would have been set to NORMAL but we
1767 * just set it back to HEAD.
1768 *
1769 * How do you detect this? Well, if that happened
1770 * the tail page would have moved.
1771 */
1772 if (ret == RB_PAGE_NORMAL) {
1773 /*
1774 * If the tail had moved passed next, then we need
1775 * to reset the pointer.
1776 */
1777 if (cpu_buffer->tail_page != tail_page &&
1778 cpu_buffer->tail_page != next_page)
1779 rb_head_page_set_normal(cpu_buffer, new_head,
1780 next_page,
1781 RB_PAGE_HEAD);
1782 }
1783
1784 /*
1785 * If this was the outer most commit (the one that
1786 * changed the original pointer from HEAD to UPDATE),
1787 * then it is up to us to reset it to NORMAL.
1788 */
1789 if (type == RB_PAGE_HEAD) {
1790 ret = rb_head_page_set_normal(cpu_buffer, next_page,
1791 tail_page,
1792 RB_PAGE_UPDATE);
1793 if (RB_WARN_ON(cpu_buffer,
1794 ret != RB_PAGE_UPDATE))
1795 return -1;
1796 }
1797
1798 return 0;
1799}
1800
Andrew Morton34a148b2009-01-09 12:27:09 -08001801static unsigned rb_calculate_event_length(unsigned length)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001802{
1803 struct ring_buffer_event event; /* Used only for sizeof array */
1804
1805 /* zero length can cause confusions */
1806 if (!length)
1807 length = 1;
1808
Steven Rostedt22710482010-03-18 17:54:19 -04001809 if (length > RB_MAX_SMALL_DATA || RB_FORCE_8BYTE_ALIGNMENT)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001810 length += sizeof(event.array[0]);
1811
1812 length += RB_EVNT_HDR_SIZE;
Steven Rostedt22710482010-03-18 17:54:19 -04001813 length = ALIGN(length, RB_ARCH_ALIGNMENT);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001814
1815 return length;
1816}
1817
Steven Rostedtc7b09302009-06-11 11:12:00 -04001818static inline void
1819rb_reset_tail(struct ring_buffer_per_cpu *cpu_buffer,
1820 struct buffer_page *tail_page,
1821 unsigned long tail, unsigned long length)
1822{
1823 struct ring_buffer_event *event;
1824
1825 /*
1826 * Only the event that crossed the page boundary
1827 * must fill the old tail_page with padding.
1828 */
1829 if (tail >= BUF_PAGE_SIZE) {
Steven Rostedtb3230c82010-05-21 11:55:21 -04001830 /*
1831 * If the page was filled, then we still need
1832 * to update the real_end. Reset it to zero
1833 * and the reader will ignore it.
1834 */
1835 if (tail == BUF_PAGE_SIZE)
1836 tail_page->real_end = 0;
1837
Steven Rostedtc7b09302009-06-11 11:12:00 -04001838 local_sub(length, &tail_page->write);
1839 return;
1840 }
1841
1842 event = __rb_page_index(tail_page, tail);
Linus Torvaldsb0b70652009-06-20 10:56:46 -07001843 kmemcheck_annotate_bitfield(event, bitfield);
Steven Rostedtc7b09302009-06-11 11:12:00 -04001844
1845 /*
Steven Rostedtff0ff842010-03-31 22:11:42 -04001846 * Save the original length to the meta data.
1847 * This will be used by the reader to add lost event
1848 * counter.
1849 */
1850 tail_page->real_end = tail;
1851
1852 /*
Steven Rostedtc7b09302009-06-11 11:12:00 -04001853 * If this event is bigger than the minimum size, then
1854 * we need to be careful that we don't subtract the
1855 * write counter enough to allow another writer to slip
1856 * in on this page.
1857 * We put in a discarded commit instead, to make sure
1858 * that this space is not used again.
1859 *
1860 * If we are less than the minimum size, we don't need to
1861 * worry about it.
1862 */
1863 if (tail > (BUF_PAGE_SIZE - RB_EVNT_MIN_SIZE)) {
1864 /* No room for any events */
1865
1866 /* Mark the rest of the page with padding */
1867 rb_event_set_padding(event);
1868
1869 /* Set the write back to the previous setting */
1870 local_sub(length, &tail_page->write);
1871 return;
1872 }
1873
1874 /* Put in a discarded event */
1875 event->array[0] = (BUF_PAGE_SIZE - tail) - RB_EVNT_HDR_SIZE;
1876 event->type_len = RINGBUF_TYPE_PADDING;
1877 /* time delta must be non zero */
1878 event->time_delta = 1;
Steven Rostedtc7b09302009-06-11 11:12:00 -04001879
1880 /* Set write to end of buffer */
1881 length = (tail + length) - BUF_PAGE_SIZE;
1882 local_sub(length, &tail_page->write);
1883}
Steven Rostedt6634ff22009-05-06 15:30:07 -04001884
Steven Rostedt747e94a2010-10-08 13:51:48 -04001885/*
1886 * This is the slow path, force gcc not to inline it.
1887 */
1888static noinline struct ring_buffer_event *
Steven Rostedt6634ff22009-05-06 15:30:07 -04001889rb_move_tail(struct ring_buffer_per_cpu *cpu_buffer,
1890 unsigned long length, unsigned long tail,
Steven Rostedte8bc43e2010-10-20 10:58:02 -04001891 struct buffer_page *tail_page, u64 ts)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001892{
Steven Rostedt5a50e332009-11-17 08:43:01 -05001893 struct buffer_page *commit_page = cpu_buffer->commit_page;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001894 struct ring_buffer *buffer = cpu_buffer->buffer;
Steven Rostedt77ae3652009-03-27 11:00:29 -04001895 struct buffer_page *next_page;
1896 int ret;
Steven Rostedtaa20ae82009-05-05 21:16:11 -04001897
1898 next_page = tail_page;
1899
Steven Rostedtaa20ae82009-05-05 21:16:11 -04001900 rb_inc_page(cpu_buffer, &next_page);
1901
Steven Rostedtaa20ae82009-05-05 21:16:11 -04001902 /*
1903 * If for some reason, we had an interrupt storm that made
1904 * it all the way around the buffer, bail, and warn
1905 * about it.
1906 */
1907 if (unlikely(next_page == commit_page)) {
Steven Rostedt77ae3652009-03-27 11:00:29 -04001908 local_inc(&cpu_buffer->commit_overrun);
Steven Rostedtaa20ae82009-05-05 21:16:11 -04001909 goto out_reset;
1910 }
1911
Steven Rostedt77ae3652009-03-27 11:00:29 -04001912 /*
1913 * This is where the fun begins!
1914 *
1915 * We are fighting against races between a reader that
1916 * could be on another CPU trying to swap its reader
1917 * page with the buffer head.
1918 *
1919 * We are also fighting against interrupts coming in and
1920 * moving the head or tail on us as well.
1921 *
1922 * If the next page is the head page then we have filled
1923 * the buffer, unless the commit page is still on the
1924 * reader page.
1925 */
1926 if (rb_is_head_page(cpu_buffer, next_page, &tail_page->list)) {
Steven Rostedtaa20ae82009-05-05 21:16:11 -04001927
Steven Rostedt77ae3652009-03-27 11:00:29 -04001928 /*
1929 * If the commit is not on the reader page, then
1930 * move the header page.
1931 */
1932 if (!rb_is_reader_page(cpu_buffer->commit_page)) {
1933 /*
1934 * If we are not in overwrite mode,
1935 * this is easy, just stop here.
1936 */
1937 if (!(buffer->flags & RB_FL_OVERWRITE))
1938 goto out_reset;
Steven Rostedtaa20ae82009-05-05 21:16:11 -04001939
Steven Rostedt77ae3652009-03-27 11:00:29 -04001940 ret = rb_handle_head_page(cpu_buffer,
1941 tail_page,
1942 next_page);
1943 if (ret < 0)
1944 goto out_reset;
1945 if (ret)
1946 goto out_again;
1947 } else {
1948 /*
1949 * We need to be careful here too. The
1950 * commit page could still be on the reader
1951 * page. We could have a small buffer, and
1952 * have filled up the buffer with events
1953 * from interrupts and such, and wrapped.
1954 *
1955 * Note, if the tail page is also the on the
1956 * reader_page, we let it move out.
1957 */
1958 if (unlikely((cpu_buffer->commit_page !=
1959 cpu_buffer->tail_page) &&
1960 (cpu_buffer->commit_page ==
1961 cpu_buffer->reader_page))) {
1962 local_inc(&cpu_buffer->commit_overrun);
1963 goto out_reset;
1964 }
Steven Rostedtaa20ae82009-05-05 21:16:11 -04001965 }
1966 }
1967
Steven Rostedt77ae3652009-03-27 11:00:29 -04001968 ret = rb_tail_page_update(cpu_buffer, tail_page, next_page);
1969 if (ret) {
1970 /*
1971 * Nested commits always have zero deltas, so
1972 * just reread the time stamp
1973 */
Steven Rostedte8bc43e2010-10-20 10:58:02 -04001974 ts = rb_time_stamp(buffer);
1975 next_page->page->time_stamp = ts;
Steven Rostedtaa20ae82009-05-05 21:16:11 -04001976 }
1977
Steven Rostedt77ae3652009-03-27 11:00:29 -04001978 out_again:
Steven Rostedtaa20ae82009-05-05 21:16:11 -04001979
Steven Rostedt77ae3652009-03-27 11:00:29 -04001980 rb_reset_tail(cpu_buffer, tail_page, tail, length);
Steven Rostedtaa20ae82009-05-05 21:16:11 -04001981
1982 /* fail and let the caller try again */
1983 return ERR_PTR(-EAGAIN);
1984
Steven Rostedt45141d42009-02-12 13:19:48 -05001985 out_reset:
Lai Jiangshan6f3b3442009-01-12 11:06:18 +08001986 /* reset write */
Steven Rostedtc7b09302009-06-11 11:12:00 -04001987 rb_reset_tail(cpu_buffer, tail_page, tail, length);
Lai Jiangshan6f3b3442009-01-12 11:06:18 +08001988
Steven Rostedtbf41a152008-10-04 02:00:59 -04001989 return NULL;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001990}
1991
Steven Rostedt6634ff22009-05-06 15:30:07 -04001992static struct ring_buffer_event *
1993__rb_reserve_next(struct ring_buffer_per_cpu *cpu_buffer,
Steven Rostedt69d1b832010-10-07 18:18:05 -04001994 unsigned long length, u64 ts,
1995 u64 delta, int add_timestamp)
Steven Rostedt6634ff22009-05-06 15:30:07 -04001996{
Steven Rostedt5a50e332009-11-17 08:43:01 -05001997 struct buffer_page *tail_page;
Steven Rostedt6634ff22009-05-06 15:30:07 -04001998 struct ring_buffer_event *event;
1999 unsigned long tail, write;
2000
Steven Rostedt69d1b832010-10-07 18:18:05 -04002001 /*
2002 * If the time delta since the last event is too big to
2003 * hold in the time field of the event, then we append a
2004 * TIME EXTEND event ahead of the data event.
2005 */
2006 if (unlikely(add_timestamp))
2007 length += RB_LEN_TIME_EXTEND;
2008
Steven Rostedt6634ff22009-05-06 15:30:07 -04002009 tail_page = cpu_buffer->tail_page;
2010 write = local_add_return(length, &tail_page->write);
Steven Rostedt77ae3652009-03-27 11:00:29 -04002011
2012 /* set write to only the index of the write */
2013 write &= RB_WRITE_MASK;
Steven Rostedt6634ff22009-05-06 15:30:07 -04002014 tail = write - length;
2015
2016 /* See if we shot pass the end of this buffer page */
Steven Rostedt747e94a2010-10-08 13:51:48 -04002017 if (unlikely(write > BUF_PAGE_SIZE))
Steven Rostedt6634ff22009-05-06 15:30:07 -04002018 return rb_move_tail(cpu_buffer, length, tail,
Steven Rostedt5a50e332009-11-17 08:43:01 -05002019 tail_page, ts);
Steven Rostedt6634ff22009-05-06 15:30:07 -04002020
2021 /* We reserved something on the buffer */
2022
Steven Rostedt6634ff22009-05-06 15:30:07 -04002023 event = __rb_page_index(tail_page, tail);
Vegard Nossum1744a212009-02-28 08:29:44 +01002024 kmemcheck_annotate_bitfield(event, bitfield);
Steven Rostedt69d1b832010-10-07 18:18:05 -04002025 rb_update_event(cpu_buffer, event, length, add_timestamp, delta);
Steven Rostedt6634ff22009-05-06 15:30:07 -04002026
Steven Rostedt69d1b832010-10-07 18:18:05 -04002027 local_inc(&tail_page->entries);
Steven Rostedt6634ff22009-05-06 15:30:07 -04002028
2029 /*
Steven Rostedtfa743952009-06-16 12:37:57 -04002030 * If this is the first commit on the page, then update
2031 * its timestamp.
Steven Rostedt6634ff22009-05-06 15:30:07 -04002032 */
Steven Rostedtfa743952009-06-16 12:37:57 -04002033 if (!tail)
Steven Rostedte8bc43e2010-10-20 10:58:02 -04002034 tail_page->page->time_stamp = ts;
Steven Rostedt6634ff22009-05-06 15:30:07 -04002035
2036 return event;
2037}
2038
Steven Rostedtedd813bf2009-06-02 23:00:53 -04002039static inline int
2040rb_try_to_discard(struct ring_buffer_per_cpu *cpu_buffer,
2041 struct ring_buffer_event *event)
2042{
2043 unsigned long new_index, old_index;
2044 struct buffer_page *bpage;
2045 unsigned long index;
2046 unsigned long addr;
2047
2048 new_index = rb_event_index(event);
Steven Rostedt69d1b832010-10-07 18:18:05 -04002049 old_index = new_index + rb_event_ts_length(event);
Steven Rostedtedd813bf2009-06-02 23:00:53 -04002050 addr = (unsigned long)event;
2051 addr &= PAGE_MASK;
2052
2053 bpage = cpu_buffer->tail_page;
2054
2055 if (bpage->page == (void *)addr && rb_page_write(bpage) == old_index) {
Steven Rostedt77ae3652009-03-27 11:00:29 -04002056 unsigned long write_mask =
2057 local_read(&bpage->write) & ~RB_WRITE_MASK;
Steven Rostedtedd813bf2009-06-02 23:00:53 -04002058 /*
2059 * This is on the tail page. It is possible that
2060 * a write could come in and move the tail page
2061 * and write to the next page. That is fine
2062 * because we just shorten what is on this page.
2063 */
Steven Rostedt77ae3652009-03-27 11:00:29 -04002064 old_index += write_mask;
2065 new_index += write_mask;
Steven Rostedtedd813bf2009-06-02 23:00:53 -04002066 index = local_cmpxchg(&bpage->write, old_index, new_index);
2067 if (index == old_index)
2068 return 1;
2069 }
2070
2071 /* could not discard */
2072 return 0;
2073}
2074
Steven Rostedtfa743952009-06-16 12:37:57 -04002075static void rb_start_commit(struct ring_buffer_per_cpu *cpu_buffer)
2076{
2077 local_inc(&cpu_buffer->committing);
2078 local_inc(&cpu_buffer->commits);
2079}
2080
2081static void rb_end_commit(struct ring_buffer_per_cpu *cpu_buffer)
2082{
2083 unsigned long commits;
2084
2085 if (RB_WARN_ON(cpu_buffer,
2086 !local_read(&cpu_buffer->committing)))
2087 return;
2088
2089 again:
2090 commits = local_read(&cpu_buffer->commits);
2091 /* synchronize with interrupts */
2092 barrier();
2093 if (local_read(&cpu_buffer->committing) == 1)
2094 rb_set_commit_to_write(cpu_buffer);
2095
2096 local_dec(&cpu_buffer->committing);
2097
2098 /* synchronize with interrupts */
2099 barrier();
2100
2101 /*
2102 * Need to account for interrupts coming in between the
2103 * updating of the commit page and the clearing of the
2104 * committing counter.
2105 */
2106 if (unlikely(local_read(&cpu_buffer->commits) != commits) &&
2107 !local_read(&cpu_buffer->committing)) {
2108 local_inc(&cpu_buffer->committing);
2109 goto again;
2110 }
2111}
2112
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002113static struct ring_buffer_event *
Steven Rostedt62f0b3e2009-09-04 14:11:34 -04002114rb_reserve_next_event(struct ring_buffer *buffer,
2115 struct ring_buffer_per_cpu *cpu_buffer,
Steven Rostedt1cd8d732009-05-11 14:08:09 -04002116 unsigned long length)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002117{
2118 struct ring_buffer_event *event;
Steven Rostedt69d1b832010-10-07 18:18:05 -04002119 u64 ts, delta;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002120 int nr_loops = 0;
Steven Rostedt69d1b832010-10-07 18:18:05 -04002121 int add_timestamp;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002122
Steven Rostedtfa743952009-06-16 12:37:57 -04002123 rb_start_commit(cpu_buffer);
2124
Steven Rostedt85bac322009-09-04 14:24:40 -04002125#ifdef CONFIG_RING_BUFFER_ALLOW_SWAP
Steven Rostedt62f0b3e2009-09-04 14:11:34 -04002126 /*
2127 * Due to the ability to swap a cpu buffer from a buffer
2128 * it is possible it was swapped before we committed.
2129 * (committing stops a swap). We check for it here and
2130 * if it happened, we have to fail the write.
2131 */
2132 barrier();
2133 if (unlikely(ACCESS_ONCE(cpu_buffer->buffer) != buffer)) {
2134 local_dec(&cpu_buffer->committing);
2135 local_dec(&cpu_buffer->commits);
2136 return NULL;
2137 }
Steven Rostedt85bac322009-09-04 14:24:40 -04002138#endif
Steven Rostedt62f0b3e2009-09-04 14:11:34 -04002139
Steven Rostedtbe957c42009-05-11 14:42:53 -04002140 length = rb_calculate_event_length(length);
Steven Rostedtbf41a152008-10-04 02:00:59 -04002141 again:
Steven Rostedt69d1b832010-10-07 18:18:05 -04002142 add_timestamp = 0;
2143 delta = 0;
2144
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002145 /*
2146 * We allow for interrupts to reenter here and do a trace.
2147 * If one does, it will cause this original code to loop
2148 * back here. Even with heavy interrupts happening, this
2149 * should only happen a few times in a row. If this happens
2150 * 1000 times in a row, there must be either an interrupt
2151 * storm or we have something buggy.
2152 * Bail!
2153 */
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05002154 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 1000))
Steven Rostedtfa743952009-06-16 12:37:57 -04002155 goto out_fail;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002156
Jiri Olsa6d3f1e12009-10-23 19:36:19 -04002157 ts = rb_time_stamp(cpu_buffer->buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002158
Steven Rostedtbf41a152008-10-04 02:00:59 -04002159 /*
2160 * Only the first commit can update the timestamp.
2161 * Yes there is a race here. If an interrupt comes in
2162 * just after the conditional and it traces too, then it
2163 * will also check the deltas. More than one timestamp may
2164 * also be made. But only the entry that did the actual
2165 * commit will be something other than zero.
2166 */
Steven Rostedt0f0c85f2009-05-11 16:08:00 -04002167 if (likely(cpu_buffer->tail_page == cpu_buffer->commit_page &&
2168 rb_page_write(cpu_buffer->tail_page) ==
2169 rb_commit_index(cpu_buffer))) {
Steven Rostedt168b6b12009-05-11 22:11:05 -04002170 u64 diff;
Steven Rostedtbf41a152008-10-04 02:00:59 -04002171
Steven Rostedt168b6b12009-05-11 22:11:05 -04002172 diff = ts - cpu_buffer->write_stamp;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002173
Steven Rostedt168b6b12009-05-11 22:11:05 -04002174 /* make sure this diff is calculated here */
Steven Rostedtbf41a152008-10-04 02:00:59 -04002175 barrier();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002176
Steven Rostedtbf41a152008-10-04 02:00:59 -04002177 /* Did the write stamp get updated already? */
2178 if (unlikely(ts < cpu_buffer->write_stamp))
Steven Rostedt168b6b12009-05-11 22:11:05 -04002179 goto get_event;
Steven Rostedtbf41a152008-10-04 02:00:59 -04002180
Steven Rostedt168b6b12009-05-11 22:11:05 -04002181 delta = diff;
2182 if (unlikely(test_time_stamp(delta))) {
Steven Rostedt69d1b832010-10-07 18:18:05 -04002183 WARN_ONCE(delta > (1ULL << 59),
2184 KERN_WARNING "Delta way too big! %llu ts=%llu write stamp = %llu\n",
2185 (unsigned long long)delta,
2186 (unsigned long long)ts,
2187 (unsigned long long)cpu_buffer->write_stamp);
2188 add_timestamp = 1;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002189 }
Steven Rostedt168b6b12009-05-11 22:11:05 -04002190 }
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002191
Steven Rostedt168b6b12009-05-11 22:11:05 -04002192 get_event:
Steven Rostedt69d1b832010-10-07 18:18:05 -04002193 event = __rb_reserve_next(cpu_buffer, length, ts,
2194 delta, add_timestamp);
Steven Rostedt168b6b12009-05-11 22:11:05 -04002195 if (unlikely(PTR_ERR(event) == -EAGAIN))
Steven Rostedtbf41a152008-10-04 02:00:59 -04002196 goto again;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002197
Steven Rostedtfa743952009-06-16 12:37:57 -04002198 if (!event)
2199 goto out_fail;
Steven Rostedtbf41a152008-10-04 02:00:59 -04002200
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002201 return event;
Steven Rostedtfa743952009-06-16 12:37:57 -04002202
2203 out_fail:
2204 rb_end_commit(cpu_buffer);
2205 return NULL;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002206}
2207
Paul Mundt1155de42009-06-25 14:30:12 +09002208#ifdef CONFIG_TRACING
2209
Steven Rostedtaa18efb2009-04-20 16:16:11 -04002210#define TRACE_RECURSIVE_DEPTH 16
Steven Rostedt261842b2009-04-16 21:41:52 -04002211
2212static int trace_recursive_lock(void)
2213{
Steven Rostedtaa18efb2009-04-20 16:16:11 -04002214 current->trace_recursion++;
Steven Rostedt261842b2009-04-16 21:41:52 -04002215
Steven Rostedtaa18efb2009-04-20 16:16:11 -04002216 if (likely(current->trace_recursion < TRACE_RECURSIVE_DEPTH))
2217 return 0;
Steven Rostedt261842b2009-04-16 21:41:52 -04002218
Steven Rostedtaa18efb2009-04-20 16:16:11 -04002219 /* Disable all tracing before we do anything else */
2220 tracing_off_permanent();
Frederic Weisbeckere057a5e2009-04-19 23:38:12 +02002221
Steven Rostedt7d7d2b82009-04-27 12:37:49 -04002222 printk_once(KERN_WARNING "Tracing recursion: depth[%ld]:"
Steven Rostedtaa18efb2009-04-20 16:16:11 -04002223 "HC[%lu]:SC[%lu]:NMI[%lu]\n",
2224 current->trace_recursion,
2225 hardirq_count() >> HARDIRQ_SHIFT,
2226 softirq_count() >> SOFTIRQ_SHIFT,
2227 in_nmi());
Frederic Weisbeckere057a5e2009-04-19 23:38:12 +02002228
Steven Rostedtaa18efb2009-04-20 16:16:11 -04002229 WARN_ON_ONCE(1);
2230 return -1;
Steven Rostedt261842b2009-04-16 21:41:52 -04002231}
2232
2233static void trace_recursive_unlock(void)
2234{
Steven Rostedtaa18efb2009-04-20 16:16:11 -04002235 WARN_ON_ONCE(!current->trace_recursion);
Steven Rostedt261842b2009-04-16 21:41:52 -04002236
Steven Rostedtaa18efb2009-04-20 16:16:11 -04002237 current->trace_recursion--;
Steven Rostedt261842b2009-04-16 21:41:52 -04002238}
2239
Paul Mundt1155de42009-06-25 14:30:12 +09002240#else
2241
2242#define trace_recursive_lock() (0)
2243#define trace_recursive_unlock() do { } while (0)
2244
2245#endif
2246
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002247/**
2248 * ring_buffer_lock_reserve - reserve a part of the buffer
2249 * @buffer: the ring buffer to reserve from
2250 * @length: the length of the data to reserve (excluding event header)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002251 *
2252 * Returns a reseverd event on the ring buffer to copy directly to.
2253 * The user of this interface will need to get the body to write into
2254 * and can use the ring_buffer_event_data() interface.
2255 *
2256 * The length is the length of the data needed, not the event length
2257 * which also includes the event header.
2258 *
2259 * Must be paired with ring_buffer_unlock_commit, unless NULL is returned.
2260 * If NULL is returned, then nothing has been allocated or locked.
2261 */
2262struct ring_buffer_event *
Arnaldo Carvalho de Melo0a987752009-02-05 16:12:56 -02002263ring_buffer_lock_reserve(struct ring_buffer *buffer, unsigned long length)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002264{
2265 struct ring_buffer_per_cpu *cpu_buffer;
2266 struct ring_buffer_event *event;
Steven Rostedt5168ae52010-06-03 09:36:50 -04002267 int cpu;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002268
Steven Rostedt033601a2008-11-21 12:41:55 -05002269 if (ring_buffer_flags != RB_BUFFERS_ON)
Steven Rostedta3583242008-11-11 15:01:42 -05002270 return NULL;
2271
Steven Rostedtbf41a152008-10-04 02:00:59 -04002272 /* If we are tracing schedule, we don't want to recurse */
Steven Rostedt5168ae52010-06-03 09:36:50 -04002273 preempt_disable_notrace();
Steven Rostedtbf41a152008-10-04 02:00:59 -04002274
Lai Jiangshan52fbe9c2010-03-08 14:50:43 +08002275 if (atomic_read(&buffer->record_disabled))
2276 goto out_nocheck;
2277
Steven Rostedt261842b2009-04-16 21:41:52 -04002278 if (trace_recursive_lock())
2279 goto out_nocheck;
2280
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002281 cpu = raw_smp_processor_id();
2282
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302283 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedtd7690412008-10-01 00:29:53 -04002284 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002285
2286 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002287
2288 if (atomic_read(&cpu_buffer->record_disabled))
Steven Rostedtd7690412008-10-01 00:29:53 -04002289 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002290
Steven Rostedtbe957c42009-05-11 14:42:53 -04002291 if (length > BUF_MAX_DATA_SIZE)
Steven Rostedtbf41a152008-10-04 02:00:59 -04002292 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002293
Steven Rostedt62f0b3e2009-09-04 14:11:34 -04002294 event = rb_reserve_next_event(buffer, cpu_buffer, length);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002295 if (!event)
Steven Rostedtd7690412008-10-01 00:29:53 -04002296 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002297
2298 return event;
2299
Steven Rostedtd7690412008-10-01 00:29:53 -04002300 out:
Steven Rostedt261842b2009-04-16 21:41:52 -04002301 trace_recursive_unlock();
2302
2303 out_nocheck:
Steven Rostedt5168ae52010-06-03 09:36:50 -04002304 preempt_enable_notrace();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002305 return NULL;
2306}
Robert Richterc4f50182008-12-11 16:49:22 +01002307EXPORT_SYMBOL_GPL(ring_buffer_lock_reserve);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002308
Steven Rostedta1863c22009-09-03 10:23:58 -04002309static void
2310rb_update_write_stamp(struct ring_buffer_per_cpu *cpu_buffer,
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002311 struct ring_buffer_event *event)
2312{
Steven Rostedt69d1b832010-10-07 18:18:05 -04002313 u64 delta;
2314
Steven Rostedtfa743952009-06-16 12:37:57 -04002315 /*
2316 * The event first in the commit queue updates the
2317 * time stamp.
2318 */
Steven Rostedt69d1b832010-10-07 18:18:05 -04002319 if (rb_event_is_commit(cpu_buffer, event)) {
2320 /*
2321 * A commit event that is first on a page
2322 * updates the write timestamp with the page stamp
2323 */
2324 if (!rb_event_index(event))
2325 cpu_buffer->write_stamp =
2326 cpu_buffer->commit_page->page->time_stamp;
2327 else if (event->type_len == RINGBUF_TYPE_TIME_EXTEND) {
2328 delta = event->array[0];
2329 delta <<= TS_SHIFT;
2330 delta += event->time_delta;
2331 cpu_buffer->write_stamp += delta;
2332 } else
2333 cpu_buffer->write_stamp += event->time_delta;
2334 }
Steven Rostedta1863c22009-09-03 10:23:58 -04002335}
Steven Rostedtbf41a152008-10-04 02:00:59 -04002336
Steven Rostedta1863c22009-09-03 10:23:58 -04002337static void rb_commit(struct ring_buffer_per_cpu *cpu_buffer,
2338 struct ring_buffer_event *event)
2339{
2340 local_inc(&cpu_buffer->entries);
2341 rb_update_write_stamp(cpu_buffer, event);
Steven Rostedtfa743952009-06-16 12:37:57 -04002342 rb_end_commit(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002343}
2344
2345/**
2346 * ring_buffer_unlock_commit - commit a reserved
2347 * @buffer: The buffer to commit to
2348 * @event: The event pointer to commit.
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002349 *
2350 * This commits the data to the ring buffer, and releases any locks held.
2351 *
2352 * Must be paired with ring_buffer_lock_reserve.
2353 */
2354int ring_buffer_unlock_commit(struct ring_buffer *buffer,
Arnaldo Carvalho de Melo0a987752009-02-05 16:12:56 -02002355 struct ring_buffer_event *event)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002356{
2357 struct ring_buffer_per_cpu *cpu_buffer;
2358 int cpu = raw_smp_processor_id();
2359
2360 cpu_buffer = buffer->buffers[cpu];
2361
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002362 rb_commit(cpu_buffer, event);
2363
Steven Rostedt261842b2009-04-16 21:41:52 -04002364 trace_recursive_unlock();
2365
Steven Rostedt5168ae52010-06-03 09:36:50 -04002366 preempt_enable_notrace();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002367
2368 return 0;
2369}
Robert Richterc4f50182008-12-11 16:49:22 +01002370EXPORT_SYMBOL_GPL(ring_buffer_unlock_commit);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002371
Frederic Weisbeckerf3b9aae2009-04-19 23:39:33 +02002372static inline void rb_event_discard(struct ring_buffer_event *event)
2373{
Steven Rostedt69d1b832010-10-07 18:18:05 -04002374 if (event->type_len == RINGBUF_TYPE_TIME_EXTEND)
2375 event = skip_time_extend(event);
2376
Lai Jiangshan334d4162009-04-24 11:27:05 +08002377 /* array[0] holds the actual length for the discarded event */
2378 event->array[0] = rb_event_data_length(event) - RB_EVNT_HDR_SIZE;
2379 event->type_len = RINGBUF_TYPE_PADDING;
Frederic Weisbeckerf3b9aae2009-04-19 23:39:33 +02002380 /* time delta must be non zero */
2381 if (!event->time_delta)
2382 event->time_delta = 1;
2383}
2384
Steven Rostedta1863c22009-09-03 10:23:58 -04002385/*
2386 * Decrement the entries to the page that an event is on.
2387 * The event does not even need to exist, only the pointer
2388 * to the page it is on. This may only be called before the commit
2389 * takes place.
2390 */
2391static inline void
2392rb_decrement_entry(struct ring_buffer_per_cpu *cpu_buffer,
2393 struct ring_buffer_event *event)
2394{
2395 unsigned long addr = (unsigned long)event;
2396 struct buffer_page *bpage = cpu_buffer->commit_page;
2397 struct buffer_page *start;
2398
2399 addr &= PAGE_MASK;
2400
2401 /* Do the likely case first */
2402 if (likely(bpage->page == (void *)addr)) {
2403 local_dec(&bpage->entries);
2404 return;
2405 }
2406
2407 /*
2408 * Because the commit page may be on the reader page we
2409 * start with the next page and check the end loop there.
2410 */
2411 rb_inc_page(cpu_buffer, &bpage);
2412 start = bpage;
2413 do {
2414 if (bpage->page == (void *)addr) {
2415 local_dec(&bpage->entries);
2416 return;
2417 }
2418 rb_inc_page(cpu_buffer, &bpage);
2419 } while (bpage != start);
2420
2421 /* commit not part of this buffer?? */
2422 RB_WARN_ON(cpu_buffer, 1);
2423}
2424
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002425/**
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002426 * ring_buffer_commit_discard - discard an event that has not been committed
2427 * @buffer: the ring buffer
2428 * @event: non committed event to discard
2429 *
Steven Rostedtdc892f72009-09-03 15:33:41 -04002430 * Sometimes an event that is in the ring buffer needs to be ignored.
2431 * This function lets the user discard an event in the ring buffer
2432 * and then that event will not be read later.
2433 *
2434 * This function only works if it is called before the the item has been
2435 * committed. It will try to free the event from the ring buffer
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002436 * if another event has not been added behind it.
2437 *
2438 * If another event has been added behind it, it will set the event
2439 * up as discarded, and perform the commit.
2440 *
2441 * If this function is called, do not call ring_buffer_unlock_commit on
2442 * the event.
2443 */
2444void ring_buffer_discard_commit(struct ring_buffer *buffer,
2445 struct ring_buffer_event *event)
2446{
2447 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002448 int cpu;
2449
2450 /* The event is discarded regardless */
Frederic Weisbeckerf3b9aae2009-04-19 23:39:33 +02002451 rb_event_discard(event);
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002452
Steven Rostedtfa743952009-06-16 12:37:57 -04002453 cpu = smp_processor_id();
2454 cpu_buffer = buffer->buffers[cpu];
2455
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002456 /*
2457 * This must only be called if the event has not been
2458 * committed yet. Thus we can assume that preemption
2459 * is still disabled.
2460 */
Steven Rostedtfa743952009-06-16 12:37:57 -04002461 RB_WARN_ON(buffer, !local_read(&cpu_buffer->committing));
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002462
Steven Rostedta1863c22009-09-03 10:23:58 -04002463 rb_decrement_entry(cpu_buffer, event);
Steven Rostedt0f2541d2009-08-05 12:02:48 -04002464 if (rb_try_to_discard(cpu_buffer, event))
Steven Rostedtedd813bf2009-06-02 23:00:53 -04002465 goto out;
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002466
2467 /*
2468 * The commit is still visible by the reader, so we
Steven Rostedta1863c22009-09-03 10:23:58 -04002469 * must still update the timestamp.
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002470 */
Steven Rostedta1863c22009-09-03 10:23:58 -04002471 rb_update_write_stamp(cpu_buffer, event);
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002472 out:
Steven Rostedtfa743952009-06-16 12:37:57 -04002473 rb_end_commit(cpu_buffer);
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002474
Frederic Weisbeckerf3b9aae2009-04-19 23:39:33 +02002475 trace_recursive_unlock();
2476
Steven Rostedt5168ae52010-06-03 09:36:50 -04002477 preempt_enable_notrace();
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002478
2479}
2480EXPORT_SYMBOL_GPL(ring_buffer_discard_commit);
2481
2482/**
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002483 * ring_buffer_write - write data to the buffer without reserving
2484 * @buffer: The ring buffer to write to.
2485 * @length: The length of the data being written (excluding the event header)
2486 * @data: The data to write to the buffer.
2487 *
2488 * This is like ring_buffer_lock_reserve and ring_buffer_unlock_commit as
2489 * one function. If you already have the data to write to the buffer, it
2490 * may be easier to simply call this function.
2491 *
2492 * Note, like ring_buffer_lock_reserve, the length is the length of the data
2493 * and not the length of the event which would hold the header.
2494 */
2495int ring_buffer_write(struct ring_buffer *buffer,
2496 unsigned long length,
2497 void *data)
2498{
2499 struct ring_buffer_per_cpu *cpu_buffer;
2500 struct ring_buffer_event *event;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002501 void *body;
2502 int ret = -EBUSY;
Steven Rostedt5168ae52010-06-03 09:36:50 -04002503 int cpu;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002504
Steven Rostedt033601a2008-11-21 12:41:55 -05002505 if (ring_buffer_flags != RB_BUFFERS_ON)
Steven Rostedta3583242008-11-11 15:01:42 -05002506 return -EBUSY;
2507
Steven Rostedt5168ae52010-06-03 09:36:50 -04002508 preempt_disable_notrace();
Steven Rostedtbf41a152008-10-04 02:00:59 -04002509
Lai Jiangshan52fbe9c2010-03-08 14:50:43 +08002510 if (atomic_read(&buffer->record_disabled))
2511 goto out;
2512
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002513 cpu = raw_smp_processor_id();
2514
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302515 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedtd7690412008-10-01 00:29:53 -04002516 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002517
2518 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002519
2520 if (atomic_read(&cpu_buffer->record_disabled))
2521 goto out;
2522
Steven Rostedtbe957c42009-05-11 14:42:53 -04002523 if (length > BUF_MAX_DATA_SIZE)
2524 goto out;
2525
Steven Rostedt62f0b3e2009-09-04 14:11:34 -04002526 event = rb_reserve_next_event(buffer, cpu_buffer, length);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002527 if (!event)
2528 goto out;
2529
2530 body = rb_event_data(event);
2531
2532 memcpy(body, data, length);
2533
2534 rb_commit(cpu_buffer, event);
2535
2536 ret = 0;
2537 out:
Steven Rostedt5168ae52010-06-03 09:36:50 -04002538 preempt_enable_notrace();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002539
2540 return ret;
2541}
Robert Richterc4f50182008-12-11 16:49:22 +01002542EXPORT_SYMBOL_GPL(ring_buffer_write);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002543
Andrew Morton34a148b2009-01-09 12:27:09 -08002544static int rb_per_cpu_empty(struct ring_buffer_per_cpu *cpu_buffer)
Steven Rostedtbf41a152008-10-04 02:00:59 -04002545{
2546 struct buffer_page *reader = cpu_buffer->reader_page;
Steven Rostedt77ae3652009-03-27 11:00:29 -04002547 struct buffer_page *head = rb_set_head_page(cpu_buffer);
Steven Rostedtbf41a152008-10-04 02:00:59 -04002548 struct buffer_page *commit = cpu_buffer->commit_page;
2549
Steven Rostedt77ae3652009-03-27 11:00:29 -04002550 /* In case of error, head will be NULL */
2551 if (unlikely(!head))
2552 return 1;
2553
Steven Rostedtbf41a152008-10-04 02:00:59 -04002554 return reader->read == rb_page_commit(reader) &&
2555 (commit == reader ||
2556 (commit == head &&
2557 head->read == rb_page_commit(commit)));
2558}
2559
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002560/**
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002561 * ring_buffer_record_disable - stop all writes into the buffer
2562 * @buffer: The ring buffer to stop writes to.
2563 *
2564 * This prevents all writes to the buffer. Any attempt to write
2565 * to the buffer after this will fail and return NULL.
2566 *
2567 * The caller should call synchronize_sched() after this.
2568 */
2569void ring_buffer_record_disable(struct ring_buffer *buffer)
2570{
2571 atomic_inc(&buffer->record_disabled);
2572}
Robert Richterc4f50182008-12-11 16:49:22 +01002573EXPORT_SYMBOL_GPL(ring_buffer_record_disable);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002574
2575/**
2576 * ring_buffer_record_enable - enable writes to the buffer
2577 * @buffer: The ring buffer to enable writes
2578 *
2579 * Note, multiple disables will need the same number of enables
Adam Buchbinderc41b20e2009-12-11 16:35:39 -05002580 * to truly enable the writing (much like preempt_disable).
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002581 */
2582void ring_buffer_record_enable(struct ring_buffer *buffer)
2583{
2584 atomic_dec(&buffer->record_disabled);
2585}
Robert Richterc4f50182008-12-11 16:49:22 +01002586EXPORT_SYMBOL_GPL(ring_buffer_record_enable);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002587
2588/**
2589 * ring_buffer_record_disable_cpu - stop all writes into the cpu_buffer
2590 * @buffer: The ring buffer to stop writes to.
2591 * @cpu: The CPU buffer to stop
2592 *
2593 * This prevents all writes to the buffer. Any attempt to write
2594 * to the buffer after this will fail and return NULL.
2595 *
2596 * The caller should call synchronize_sched() after this.
2597 */
2598void ring_buffer_record_disable_cpu(struct ring_buffer *buffer, int cpu)
2599{
2600 struct ring_buffer_per_cpu *cpu_buffer;
2601
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302602 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04002603 return;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002604
2605 cpu_buffer = buffer->buffers[cpu];
2606 atomic_inc(&cpu_buffer->record_disabled);
2607}
Robert Richterc4f50182008-12-11 16:49:22 +01002608EXPORT_SYMBOL_GPL(ring_buffer_record_disable_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002609
2610/**
2611 * ring_buffer_record_enable_cpu - enable writes to the buffer
2612 * @buffer: The ring buffer to enable writes
2613 * @cpu: The CPU to enable.
2614 *
2615 * Note, multiple disables will need the same number of enables
Adam Buchbinderc41b20e2009-12-11 16:35:39 -05002616 * to truly enable the writing (much like preempt_disable).
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002617 */
2618void ring_buffer_record_enable_cpu(struct ring_buffer *buffer, int cpu)
2619{
2620 struct ring_buffer_per_cpu *cpu_buffer;
2621
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302622 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04002623 return;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002624
2625 cpu_buffer = buffer->buffers[cpu];
2626 atomic_dec(&cpu_buffer->record_disabled);
2627}
Robert Richterc4f50182008-12-11 16:49:22 +01002628EXPORT_SYMBOL_GPL(ring_buffer_record_enable_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002629
2630/**
2631 * ring_buffer_entries_cpu - get the number of entries in a cpu buffer
2632 * @buffer: The ring buffer
2633 * @cpu: The per CPU buffer to get the entries from.
2634 */
2635unsigned long ring_buffer_entries_cpu(struct ring_buffer *buffer, int cpu)
2636{
2637 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedt8aabee52009-03-12 13:13:49 -04002638 unsigned long ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002639
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302640 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04002641 return 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002642
2643 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt77ae3652009-03-27 11:00:29 -04002644 ret = (local_read(&cpu_buffer->entries) - local_read(&cpu_buffer->overrun))
Steven Rostedte4906ef2009-04-30 20:49:44 -04002645 - cpu_buffer->read;
Steven Rostedt554f7862009-03-11 22:00:13 -04002646
2647 return ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002648}
Robert Richterc4f50182008-12-11 16:49:22 +01002649EXPORT_SYMBOL_GPL(ring_buffer_entries_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002650
2651/**
2652 * ring_buffer_overrun_cpu - get the number of overruns in a cpu_buffer
2653 * @buffer: The ring buffer
2654 * @cpu: The per CPU buffer to get the number of overruns from
2655 */
2656unsigned long ring_buffer_overrun_cpu(struct ring_buffer *buffer, int cpu)
2657{
2658 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedt8aabee52009-03-12 13:13:49 -04002659 unsigned long ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002660
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302661 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04002662 return 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002663
2664 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt77ae3652009-03-27 11:00:29 -04002665 ret = local_read(&cpu_buffer->overrun);
Steven Rostedt554f7862009-03-11 22:00:13 -04002666
2667 return ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002668}
Robert Richterc4f50182008-12-11 16:49:22 +01002669EXPORT_SYMBOL_GPL(ring_buffer_overrun_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002670
2671/**
Steven Rostedtf0d2c682009-04-29 13:43:37 -04002672 * ring_buffer_commit_overrun_cpu - get the number of overruns caused by commits
2673 * @buffer: The ring buffer
2674 * @cpu: The per CPU buffer to get the number of overruns from
2675 */
2676unsigned long
2677ring_buffer_commit_overrun_cpu(struct ring_buffer *buffer, int cpu)
2678{
2679 struct ring_buffer_per_cpu *cpu_buffer;
2680 unsigned long ret;
2681
2682 if (!cpumask_test_cpu(cpu, buffer->cpumask))
2683 return 0;
2684
2685 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt77ae3652009-03-27 11:00:29 -04002686 ret = local_read(&cpu_buffer->commit_overrun);
Steven Rostedtf0d2c682009-04-29 13:43:37 -04002687
2688 return ret;
2689}
2690EXPORT_SYMBOL_GPL(ring_buffer_commit_overrun_cpu);
2691
2692/**
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002693 * ring_buffer_entries - get the number of entries in a buffer
2694 * @buffer: The ring buffer
2695 *
2696 * Returns the total number of entries in the ring buffer
2697 * (all CPU entries)
2698 */
2699unsigned long ring_buffer_entries(struct ring_buffer *buffer)
2700{
2701 struct ring_buffer_per_cpu *cpu_buffer;
2702 unsigned long entries = 0;
2703 int cpu;
2704
2705 /* if you care about this being correct, lock the buffer */
2706 for_each_buffer_cpu(buffer, cpu) {
2707 cpu_buffer = buffer->buffers[cpu];
Steven Rostedte4906ef2009-04-30 20:49:44 -04002708 entries += (local_read(&cpu_buffer->entries) -
Steven Rostedt77ae3652009-03-27 11:00:29 -04002709 local_read(&cpu_buffer->overrun)) - cpu_buffer->read;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002710 }
2711
2712 return entries;
2713}
Robert Richterc4f50182008-12-11 16:49:22 +01002714EXPORT_SYMBOL_GPL(ring_buffer_entries);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002715
2716/**
Jiri Olsa67b394f2009-10-23 19:36:18 -04002717 * ring_buffer_overruns - get the number of overruns in buffer
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002718 * @buffer: The ring buffer
2719 *
2720 * Returns the total number of overruns in the ring buffer
2721 * (all CPU entries)
2722 */
2723unsigned long ring_buffer_overruns(struct ring_buffer *buffer)
2724{
2725 struct ring_buffer_per_cpu *cpu_buffer;
2726 unsigned long overruns = 0;
2727 int cpu;
2728
2729 /* if you care about this being correct, lock the buffer */
2730 for_each_buffer_cpu(buffer, cpu) {
2731 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt77ae3652009-03-27 11:00:29 -04002732 overruns += local_read(&cpu_buffer->overrun);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002733 }
2734
2735 return overruns;
2736}
Robert Richterc4f50182008-12-11 16:49:22 +01002737EXPORT_SYMBOL_GPL(ring_buffer_overruns);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002738
Steven Rostedt642edba2008-11-12 00:01:26 -05002739static void rb_iter_reset(struct ring_buffer_iter *iter)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002740{
2741 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
2742
Steven Rostedtd7690412008-10-01 00:29:53 -04002743 /* Iterator usage is expected to have record disabled */
2744 if (list_empty(&cpu_buffer->reader_page->list)) {
Steven Rostedt77ae3652009-03-27 11:00:29 -04002745 iter->head_page = rb_set_head_page(cpu_buffer);
2746 if (unlikely(!iter->head_page))
2747 return;
2748 iter->head = iter->head_page->read;
Steven Rostedtd7690412008-10-01 00:29:53 -04002749 } else {
2750 iter->head_page = cpu_buffer->reader_page;
Steven Rostedt6f807ac2008-10-04 02:00:58 -04002751 iter->head = cpu_buffer->reader_page->read;
Steven Rostedtd7690412008-10-01 00:29:53 -04002752 }
2753 if (iter->head)
2754 iter->read_stamp = cpu_buffer->read_stamp;
2755 else
Steven Rostedtabc9b562008-12-02 15:34:06 -05002756 iter->read_stamp = iter->head_page->page->time_stamp;
Steven Rostedt492a74f2010-01-25 15:17:47 -05002757 iter->cache_reader_page = cpu_buffer->reader_page;
2758 iter->cache_read = cpu_buffer->read;
Steven Rostedt642edba2008-11-12 00:01:26 -05002759}
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002760
Steven Rostedt642edba2008-11-12 00:01:26 -05002761/**
2762 * ring_buffer_iter_reset - reset an iterator
2763 * @iter: The iterator to reset
2764 *
2765 * Resets the iterator, so that it will start from the beginning
2766 * again.
2767 */
2768void ring_buffer_iter_reset(struct ring_buffer_iter *iter)
2769{
Steven Rostedt554f7862009-03-11 22:00:13 -04002770 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedt642edba2008-11-12 00:01:26 -05002771 unsigned long flags;
2772
Steven Rostedt554f7862009-03-11 22:00:13 -04002773 if (!iter)
2774 return;
2775
2776 cpu_buffer = iter->cpu_buffer;
2777
Steven Rostedt642edba2008-11-12 00:01:26 -05002778 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
2779 rb_iter_reset(iter);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002780 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002781}
Robert Richterc4f50182008-12-11 16:49:22 +01002782EXPORT_SYMBOL_GPL(ring_buffer_iter_reset);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002783
2784/**
2785 * ring_buffer_iter_empty - check if an iterator has no more to read
2786 * @iter: The iterator to check
2787 */
2788int ring_buffer_iter_empty(struct ring_buffer_iter *iter)
2789{
2790 struct ring_buffer_per_cpu *cpu_buffer;
2791
2792 cpu_buffer = iter->cpu_buffer;
2793
Steven Rostedtbf41a152008-10-04 02:00:59 -04002794 return iter->head_page == cpu_buffer->commit_page &&
2795 iter->head == rb_commit_index(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002796}
Robert Richterc4f50182008-12-11 16:49:22 +01002797EXPORT_SYMBOL_GPL(ring_buffer_iter_empty);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002798
2799static void
2800rb_update_read_stamp(struct ring_buffer_per_cpu *cpu_buffer,
2801 struct ring_buffer_event *event)
2802{
2803 u64 delta;
2804
Lai Jiangshan334d4162009-04-24 11:27:05 +08002805 switch (event->type_len) {
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002806 case RINGBUF_TYPE_PADDING:
2807 return;
2808
2809 case RINGBUF_TYPE_TIME_EXTEND:
2810 delta = event->array[0];
2811 delta <<= TS_SHIFT;
2812 delta += event->time_delta;
2813 cpu_buffer->read_stamp += delta;
2814 return;
2815
2816 case RINGBUF_TYPE_TIME_STAMP:
2817 /* FIXME: not implemented */
2818 return;
2819
2820 case RINGBUF_TYPE_DATA:
2821 cpu_buffer->read_stamp += event->time_delta;
2822 return;
2823
2824 default:
2825 BUG();
2826 }
2827 return;
2828}
2829
2830static void
2831rb_update_iter_read_stamp(struct ring_buffer_iter *iter,
2832 struct ring_buffer_event *event)
2833{
2834 u64 delta;
2835
Lai Jiangshan334d4162009-04-24 11:27:05 +08002836 switch (event->type_len) {
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002837 case RINGBUF_TYPE_PADDING:
2838 return;
2839
2840 case RINGBUF_TYPE_TIME_EXTEND:
2841 delta = event->array[0];
2842 delta <<= TS_SHIFT;
2843 delta += event->time_delta;
2844 iter->read_stamp += delta;
2845 return;
2846
2847 case RINGBUF_TYPE_TIME_STAMP:
2848 /* FIXME: not implemented */
2849 return;
2850
2851 case RINGBUF_TYPE_DATA:
2852 iter->read_stamp += event->time_delta;
2853 return;
2854
2855 default:
2856 BUG();
2857 }
2858 return;
2859}
2860
Steven Rostedtd7690412008-10-01 00:29:53 -04002861static struct buffer_page *
2862rb_get_reader_page(struct ring_buffer_per_cpu *cpu_buffer)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002863{
Steven Rostedtd7690412008-10-01 00:29:53 -04002864 struct buffer_page *reader = NULL;
Steven Rostedt66a8cb92010-03-31 13:21:56 -04002865 unsigned long overwrite;
Steven Rostedtd7690412008-10-01 00:29:53 -04002866 unsigned long flags;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002867 int nr_loops = 0;
Steven Rostedt77ae3652009-03-27 11:00:29 -04002868 int ret;
Steven Rostedtd7690412008-10-01 00:29:53 -04002869
Steven Rostedt3e03fb72008-11-06 00:09:43 -05002870 local_irq_save(flags);
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01002871 arch_spin_lock(&cpu_buffer->lock);
Steven Rostedtd7690412008-10-01 00:29:53 -04002872
2873 again:
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002874 /*
2875 * This should normally only loop twice. But because the
2876 * start of the reader inserts an empty page, it causes
2877 * a case where we will loop three times. There should be no
2878 * reason to loop four times (that I know of).
2879 */
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05002880 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 3)) {
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002881 reader = NULL;
2882 goto out;
2883 }
2884
Steven Rostedtd7690412008-10-01 00:29:53 -04002885 reader = cpu_buffer->reader_page;
2886
2887 /* If there's more to read, return this page */
Steven Rostedtbf41a152008-10-04 02:00:59 -04002888 if (cpu_buffer->reader_page->read < rb_page_size(reader))
Steven Rostedtd7690412008-10-01 00:29:53 -04002889 goto out;
2890
2891 /* Never should we have an index greater than the size */
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05002892 if (RB_WARN_ON(cpu_buffer,
2893 cpu_buffer->reader_page->read > rb_page_size(reader)))
2894 goto out;
Steven Rostedtd7690412008-10-01 00:29:53 -04002895
2896 /* check if we caught up to the tail */
2897 reader = NULL;
Steven Rostedtbf41a152008-10-04 02:00:59 -04002898 if (cpu_buffer->commit_page == cpu_buffer->reader_page)
Steven Rostedtd7690412008-10-01 00:29:53 -04002899 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002900
2901 /*
Steven Rostedtd7690412008-10-01 00:29:53 -04002902 * Reset the reader page to size zero.
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002903 */
Steven Rostedt77ae3652009-03-27 11:00:29 -04002904 local_set(&cpu_buffer->reader_page->write, 0);
2905 local_set(&cpu_buffer->reader_page->entries, 0);
2906 local_set(&cpu_buffer->reader_page->page->commit, 0);
Steven Rostedtff0ff842010-03-31 22:11:42 -04002907 cpu_buffer->reader_page->real_end = 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002908
Steven Rostedt77ae3652009-03-27 11:00:29 -04002909 spin:
2910 /*
2911 * Splice the empty reader page into the list around the head.
2912 */
2913 reader = rb_set_head_page(cpu_buffer);
Steven Rostedt0e1ff5d2010-01-06 20:40:44 -05002914 cpu_buffer->reader_page->list.next = rb_list_head(reader->list.next);
Steven Rostedtd7690412008-10-01 00:29:53 -04002915 cpu_buffer->reader_page->list.prev = reader->list.prev;
Steven Rostedtbf41a152008-10-04 02:00:59 -04002916
Steven Rostedt3adc54f2009-03-30 15:32:01 -04002917 /*
2918 * cpu_buffer->pages just needs to point to the buffer, it
2919 * has no specific buffer page to point to. Lets move it out
2920 * of our way so we don't accidently swap it.
2921 */
2922 cpu_buffer->pages = reader->list.prev;
2923
Steven Rostedt77ae3652009-03-27 11:00:29 -04002924 /* The reader page will be pointing to the new head */
2925 rb_set_list_to_head(cpu_buffer, &cpu_buffer->reader_page->list);
Steven Rostedtd7690412008-10-01 00:29:53 -04002926
2927 /*
Steven Rostedt66a8cb92010-03-31 13:21:56 -04002928 * We want to make sure we read the overruns after we set up our
2929 * pointers to the next object. The writer side does a
2930 * cmpxchg to cross pages which acts as the mb on the writer
2931 * side. Note, the reader will constantly fail the swap
2932 * while the writer is updating the pointers, so this
2933 * guarantees that the overwrite recorded here is the one we
2934 * want to compare with the last_overrun.
2935 */
2936 smp_mb();
2937 overwrite = local_read(&(cpu_buffer->overrun));
2938
2939 /*
Steven Rostedt77ae3652009-03-27 11:00:29 -04002940 * Here's the tricky part.
2941 *
2942 * We need to move the pointer past the header page.
2943 * But we can only do that if a writer is not currently
2944 * moving it. The page before the header page has the
2945 * flag bit '1' set if it is pointing to the page we want.
2946 * but if the writer is in the process of moving it
2947 * than it will be '2' or already moved '0'.
Steven Rostedtd7690412008-10-01 00:29:53 -04002948 */
Steven Rostedtd7690412008-10-01 00:29:53 -04002949
Steven Rostedt77ae3652009-03-27 11:00:29 -04002950 ret = rb_head_page_replace(reader, cpu_buffer->reader_page);
2951
2952 /*
2953 * If we did not convert it, then we must try again.
2954 */
2955 if (!ret)
2956 goto spin;
2957
2958 /*
2959 * Yeah! We succeeded in replacing the page.
2960 *
2961 * Now make the new head point back to the reader page.
2962 */
David Sharp5ded3dc2010-01-06 17:12:07 -08002963 rb_list_head(reader->list.next)->prev = &cpu_buffer->reader_page->list;
Steven Rostedt77ae3652009-03-27 11:00:29 -04002964 rb_inc_page(cpu_buffer, &cpu_buffer->head_page);
Steven Rostedtd7690412008-10-01 00:29:53 -04002965
2966 /* Finally update the reader page to the new head */
2967 cpu_buffer->reader_page = reader;
2968 rb_reset_reader_page(cpu_buffer);
2969
Steven Rostedt66a8cb92010-03-31 13:21:56 -04002970 if (overwrite != cpu_buffer->last_overrun) {
2971 cpu_buffer->lost_events = overwrite - cpu_buffer->last_overrun;
2972 cpu_buffer->last_overrun = overwrite;
2973 }
2974
Steven Rostedtd7690412008-10-01 00:29:53 -04002975 goto again;
2976
2977 out:
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01002978 arch_spin_unlock(&cpu_buffer->lock);
Steven Rostedt3e03fb72008-11-06 00:09:43 -05002979 local_irq_restore(flags);
Steven Rostedtd7690412008-10-01 00:29:53 -04002980
2981 return reader;
2982}
2983
2984static void rb_advance_reader(struct ring_buffer_per_cpu *cpu_buffer)
2985{
2986 struct ring_buffer_event *event;
2987 struct buffer_page *reader;
2988 unsigned length;
2989
2990 reader = rb_get_reader_page(cpu_buffer);
2991
2992 /* This function should not be called when buffer is empty */
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05002993 if (RB_WARN_ON(cpu_buffer, !reader))
2994 return;
Steven Rostedtd7690412008-10-01 00:29:53 -04002995
2996 event = rb_reader_event(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002997
Steven Rostedta1863c22009-09-03 10:23:58 -04002998 if (event->type_len <= RINGBUF_TYPE_DATA_TYPE_LEN_MAX)
Steven Rostedte4906ef2009-04-30 20:49:44 -04002999 cpu_buffer->read++;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003000
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003001 rb_update_read_stamp(cpu_buffer, event);
3002
Steven Rostedtd7690412008-10-01 00:29:53 -04003003 length = rb_event_length(event);
Steven Rostedt6f807ac2008-10-04 02:00:58 -04003004 cpu_buffer->reader_page->read += length;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003005}
3006
3007static void rb_advance_iter(struct ring_buffer_iter *iter)
3008{
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003009 struct ring_buffer_per_cpu *cpu_buffer;
3010 struct ring_buffer_event *event;
3011 unsigned length;
3012
3013 cpu_buffer = iter->cpu_buffer;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003014
3015 /*
3016 * Check if we are at the end of the buffer.
3017 */
Steven Rostedtbf41a152008-10-04 02:00:59 -04003018 if (iter->head >= rb_page_size(iter->head_page)) {
Steven Rostedtea05b572009-06-03 09:30:10 -04003019 /* discarded commits can make the page empty */
3020 if (iter->head_page == cpu_buffer->commit_page)
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05003021 return;
Steven Rostedtd7690412008-10-01 00:29:53 -04003022 rb_inc_iter(iter);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003023 return;
3024 }
3025
3026 event = rb_iter_head_event(iter);
3027
3028 length = rb_event_length(event);
3029
3030 /*
3031 * This should not be called to advance the header if we are
3032 * at the tail of the buffer.
3033 */
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05003034 if (RB_WARN_ON(cpu_buffer,
Steven Rostedtf536aaf2008-11-10 23:07:30 -05003035 (iter->head_page == cpu_buffer->commit_page) &&
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05003036 (iter->head + length > rb_commit_index(cpu_buffer))))
3037 return;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003038
3039 rb_update_iter_read_stamp(iter, event);
3040
3041 iter->head += length;
3042
3043 /* check for end of page padding */
Steven Rostedtbf41a152008-10-04 02:00:59 -04003044 if ((iter->head >= rb_page_size(iter->head_page)) &&
3045 (iter->head_page != cpu_buffer->commit_page))
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003046 rb_advance_iter(iter);
3047}
3048
Steven Rostedt66a8cb92010-03-31 13:21:56 -04003049static int rb_lost_events(struct ring_buffer_per_cpu *cpu_buffer)
3050{
3051 return cpu_buffer->lost_events;
3052}
3053
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003054static struct ring_buffer_event *
Steven Rostedt66a8cb92010-03-31 13:21:56 -04003055rb_buffer_peek(struct ring_buffer_per_cpu *cpu_buffer, u64 *ts,
3056 unsigned long *lost_events)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003057{
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003058 struct ring_buffer_event *event;
Steven Rostedtd7690412008-10-01 00:29:53 -04003059 struct buffer_page *reader;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003060 int nr_loops = 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003061
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003062 again:
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003063 /*
Steven Rostedt69d1b832010-10-07 18:18:05 -04003064 * We repeat when a time extend is encountered.
3065 * Since the time extend is always attached to a data event,
3066 * we should never loop more than once.
3067 * (We never hit the following condition more than twice).
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003068 */
Steven Rostedt69d1b832010-10-07 18:18:05 -04003069 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 2))
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003070 return NULL;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003071
Steven Rostedtd7690412008-10-01 00:29:53 -04003072 reader = rb_get_reader_page(cpu_buffer);
3073 if (!reader)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003074 return NULL;
3075
Steven Rostedtd7690412008-10-01 00:29:53 -04003076 event = rb_reader_event(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003077
Lai Jiangshan334d4162009-04-24 11:27:05 +08003078 switch (event->type_len) {
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003079 case RINGBUF_TYPE_PADDING:
Tom Zanussi2d622712009-03-22 03:30:49 -05003080 if (rb_null_event(event))
3081 RB_WARN_ON(cpu_buffer, 1);
3082 /*
3083 * Because the writer could be discarding every
3084 * event it creates (which would probably be bad)
3085 * if we were to go back to "again" then we may never
3086 * catch up, and will trigger the warn on, or lock
3087 * the box. Return the padding, and we will release
3088 * the current locks, and try again.
3089 */
Tom Zanussi2d622712009-03-22 03:30:49 -05003090 return event;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003091
3092 case RINGBUF_TYPE_TIME_EXTEND:
3093 /* Internal data, OK to advance */
Steven Rostedtd7690412008-10-01 00:29:53 -04003094 rb_advance_reader(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003095 goto again;
3096
3097 case RINGBUF_TYPE_TIME_STAMP:
3098 /* FIXME: not implemented */
Steven Rostedtd7690412008-10-01 00:29:53 -04003099 rb_advance_reader(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003100 goto again;
3101
3102 case RINGBUF_TYPE_DATA:
3103 if (ts) {
3104 *ts = cpu_buffer->read_stamp + event->time_delta;
Robert Richterd8eeb2d2009-07-31 14:58:04 +02003105 ring_buffer_normalize_time_stamp(cpu_buffer->buffer,
Steven Rostedt37886f62009-03-17 17:22:06 -04003106 cpu_buffer->cpu, ts);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003107 }
Steven Rostedt66a8cb92010-03-31 13:21:56 -04003108 if (lost_events)
3109 *lost_events = rb_lost_events(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003110 return event;
3111
3112 default:
3113 BUG();
3114 }
3115
3116 return NULL;
3117}
Robert Richterc4f50182008-12-11 16:49:22 +01003118EXPORT_SYMBOL_GPL(ring_buffer_peek);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003119
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003120static struct ring_buffer_event *
3121rb_iter_peek(struct ring_buffer_iter *iter, u64 *ts)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003122{
3123 struct ring_buffer *buffer;
3124 struct ring_buffer_per_cpu *cpu_buffer;
3125 struct ring_buffer_event *event;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003126 int nr_loops = 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003127
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003128 cpu_buffer = iter->cpu_buffer;
3129 buffer = cpu_buffer->buffer;
3130
Steven Rostedt492a74f2010-01-25 15:17:47 -05003131 /*
3132 * Check if someone performed a consuming read to
3133 * the buffer. A consuming read invalidates the iterator
3134 * and we need to reset the iterator in this case.
3135 */
3136 if (unlikely(iter->cache_read != cpu_buffer->read ||
3137 iter->cache_reader_page != cpu_buffer->reader_page))
3138 rb_iter_reset(iter);
3139
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003140 again:
Steven Rostedt3c05d742010-01-26 16:14:08 -05003141 if (ring_buffer_iter_empty(iter))
3142 return NULL;
3143
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003144 /*
Steven Rostedt69d1b832010-10-07 18:18:05 -04003145 * We repeat when a time extend is encountered.
3146 * Since the time extend is always attached to a data event,
3147 * we should never loop more than once.
3148 * (We never hit the following condition more than twice).
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003149 */
Steven Rostedt69d1b832010-10-07 18:18:05 -04003150 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 2))
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003151 return NULL;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003152
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003153 if (rb_per_cpu_empty(cpu_buffer))
3154 return NULL;
3155
Steven Rostedt3c05d742010-01-26 16:14:08 -05003156 if (iter->head >= local_read(&iter->head_page->page->commit)) {
3157 rb_inc_iter(iter);
3158 goto again;
3159 }
3160
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003161 event = rb_iter_head_event(iter);
3162
Lai Jiangshan334d4162009-04-24 11:27:05 +08003163 switch (event->type_len) {
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003164 case RINGBUF_TYPE_PADDING:
Tom Zanussi2d622712009-03-22 03:30:49 -05003165 if (rb_null_event(event)) {
3166 rb_inc_iter(iter);
3167 goto again;
3168 }
3169 rb_advance_iter(iter);
3170 return event;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003171
3172 case RINGBUF_TYPE_TIME_EXTEND:
3173 /* Internal data, OK to advance */
3174 rb_advance_iter(iter);
3175 goto again;
3176
3177 case RINGBUF_TYPE_TIME_STAMP:
3178 /* FIXME: not implemented */
3179 rb_advance_iter(iter);
3180 goto again;
3181
3182 case RINGBUF_TYPE_DATA:
3183 if (ts) {
3184 *ts = iter->read_stamp + event->time_delta;
Steven Rostedt37886f62009-03-17 17:22:06 -04003185 ring_buffer_normalize_time_stamp(buffer,
3186 cpu_buffer->cpu, ts);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003187 }
3188 return event;
3189
3190 default:
3191 BUG();
3192 }
3193
3194 return NULL;
3195}
Robert Richterc4f50182008-12-11 16:49:22 +01003196EXPORT_SYMBOL_GPL(ring_buffer_iter_peek);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003197
Steven Rostedt8d707e82009-06-16 21:22:48 -04003198static inline int rb_ok_to_lock(void)
3199{
3200 /*
3201 * If an NMI die dumps out the content of the ring buffer
3202 * do not grab locks. We also permanently disable the ring
3203 * buffer too. A one time deal is all you get from reading
3204 * the ring buffer from an NMI.
3205 */
Steven Rostedt464e85e2009-08-05 15:26:37 -04003206 if (likely(!in_nmi()))
Steven Rostedt8d707e82009-06-16 21:22:48 -04003207 return 1;
3208
3209 tracing_off_permanent();
3210 return 0;
3211}
3212
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003213/**
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003214 * ring_buffer_peek - peek at the next event to be read
3215 * @buffer: The ring buffer to read
3216 * @cpu: The cpu to peak at
3217 * @ts: The timestamp counter of this event.
Steven Rostedt66a8cb92010-03-31 13:21:56 -04003218 * @lost_events: a variable to store if events were lost (may be NULL)
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003219 *
3220 * This will return the event that will be read next, but does
3221 * not consume the data.
3222 */
3223struct ring_buffer_event *
Steven Rostedt66a8cb92010-03-31 13:21:56 -04003224ring_buffer_peek(struct ring_buffer *buffer, int cpu, u64 *ts,
3225 unsigned long *lost_events)
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003226{
3227 struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
Steven Rostedt8aabee52009-03-12 13:13:49 -04003228 struct ring_buffer_event *event;
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003229 unsigned long flags;
Steven Rostedt8d707e82009-06-16 21:22:48 -04003230 int dolock;
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003231
Steven Rostedt554f7862009-03-11 22:00:13 -04003232 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04003233 return NULL;
Steven Rostedt554f7862009-03-11 22:00:13 -04003234
Steven Rostedt8d707e82009-06-16 21:22:48 -04003235 dolock = rb_ok_to_lock();
Tom Zanussi2d622712009-03-22 03:30:49 -05003236 again:
Steven Rostedt8d707e82009-06-16 21:22:48 -04003237 local_irq_save(flags);
3238 if (dolock)
3239 spin_lock(&cpu_buffer->reader_lock);
Steven Rostedt66a8cb92010-03-31 13:21:56 -04003240 event = rb_buffer_peek(cpu_buffer, ts, lost_events);
Robert Richter469535a2009-07-30 19:19:18 +02003241 if (event && event->type_len == RINGBUF_TYPE_PADDING)
3242 rb_advance_reader(cpu_buffer);
Steven Rostedt8d707e82009-06-16 21:22:48 -04003243 if (dolock)
3244 spin_unlock(&cpu_buffer->reader_lock);
3245 local_irq_restore(flags);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003246
Steven Rostedt1b959e12009-09-03 10:12:13 -04003247 if (event && event->type_len == RINGBUF_TYPE_PADDING)
Tom Zanussi2d622712009-03-22 03:30:49 -05003248 goto again;
Tom Zanussi2d622712009-03-22 03:30:49 -05003249
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003250 return event;
3251}
3252
3253/**
3254 * ring_buffer_iter_peek - peek at the next event to be read
3255 * @iter: The ring buffer iterator
3256 * @ts: The timestamp counter of this event.
3257 *
3258 * This will return the event that will be read next, but does
3259 * not increment the iterator.
3260 */
3261struct ring_buffer_event *
3262ring_buffer_iter_peek(struct ring_buffer_iter *iter, u64 *ts)
3263{
3264 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
3265 struct ring_buffer_event *event;
3266 unsigned long flags;
3267
Tom Zanussi2d622712009-03-22 03:30:49 -05003268 again:
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003269 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
3270 event = rb_iter_peek(iter, ts);
3271 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
3272
Steven Rostedt1b959e12009-09-03 10:12:13 -04003273 if (event && event->type_len == RINGBUF_TYPE_PADDING)
Tom Zanussi2d622712009-03-22 03:30:49 -05003274 goto again;
Tom Zanussi2d622712009-03-22 03:30:49 -05003275
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003276 return event;
3277}
3278
3279/**
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003280 * ring_buffer_consume - return an event and consume it
3281 * @buffer: The ring buffer to get the next event from
Steven Rostedt66a8cb92010-03-31 13:21:56 -04003282 * @cpu: the cpu to read the buffer from
3283 * @ts: a variable to store the timestamp (may be NULL)
3284 * @lost_events: a variable to store if events were lost (may be NULL)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003285 *
3286 * Returns the next event in the ring buffer, and that event is consumed.
3287 * Meaning, that sequential reads will keep returning a different event,
3288 * and eventually empty the ring buffer if the producer is slower.
3289 */
3290struct ring_buffer_event *
Steven Rostedt66a8cb92010-03-31 13:21:56 -04003291ring_buffer_consume(struct ring_buffer *buffer, int cpu, u64 *ts,
3292 unsigned long *lost_events)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003293{
Steven Rostedt554f7862009-03-11 22:00:13 -04003294 struct ring_buffer_per_cpu *cpu_buffer;
3295 struct ring_buffer_event *event = NULL;
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003296 unsigned long flags;
Steven Rostedt8d707e82009-06-16 21:22:48 -04003297 int dolock;
3298
3299 dolock = rb_ok_to_lock();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003300
Tom Zanussi2d622712009-03-22 03:30:49 -05003301 again:
Steven Rostedt554f7862009-03-11 22:00:13 -04003302 /* might be called in atomic */
3303 preempt_disable();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003304
Steven Rostedt554f7862009-03-11 22:00:13 -04003305 if (!cpumask_test_cpu(cpu, buffer->cpumask))
3306 goto out;
3307
3308 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt8d707e82009-06-16 21:22:48 -04003309 local_irq_save(flags);
3310 if (dolock)
3311 spin_lock(&cpu_buffer->reader_lock);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003312
Steven Rostedt66a8cb92010-03-31 13:21:56 -04003313 event = rb_buffer_peek(cpu_buffer, ts, lost_events);
3314 if (event) {
3315 cpu_buffer->lost_events = 0;
Robert Richter469535a2009-07-30 19:19:18 +02003316 rb_advance_reader(cpu_buffer);
Steven Rostedt66a8cb92010-03-31 13:21:56 -04003317 }
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003318
Steven Rostedt8d707e82009-06-16 21:22:48 -04003319 if (dolock)
3320 spin_unlock(&cpu_buffer->reader_lock);
3321 local_irq_restore(flags);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003322
Steven Rostedt554f7862009-03-11 22:00:13 -04003323 out:
3324 preempt_enable();
3325
Steven Rostedt1b959e12009-09-03 10:12:13 -04003326 if (event && event->type_len == RINGBUF_TYPE_PADDING)
Tom Zanussi2d622712009-03-22 03:30:49 -05003327 goto again;
Tom Zanussi2d622712009-03-22 03:30:49 -05003328
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003329 return event;
3330}
Robert Richterc4f50182008-12-11 16:49:22 +01003331EXPORT_SYMBOL_GPL(ring_buffer_consume);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003332
3333/**
David Miller72c9ddf2010-04-20 15:47:11 -07003334 * ring_buffer_read_prepare - Prepare for a non consuming read of the buffer
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003335 * @buffer: The ring buffer to read from
3336 * @cpu: The cpu buffer to iterate over
3337 *
David Miller72c9ddf2010-04-20 15:47:11 -07003338 * This performs the initial preparations necessary to iterate
3339 * through the buffer. Memory is allocated, buffer recording
3340 * is disabled, and the iterator pointer is returned to the caller.
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003341 *
David Miller72c9ddf2010-04-20 15:47:11 -07003342 * Disabling buffer recordng prevents the reading from being
3343 * corrupted. This is not a consuming read, so a producer is not
3344 * expected.
3345 *
3346 * After a sequence of ring_buffer_read_prepare calls, the user is
3347 * expected to make at least one call to ring_buffer_prepare_sync.
3348 * Afterwards, ring_buffer_read_start is invoked to get things going
3349 * for real.
3350 *
3351 * This overall must be paired with ring_buffer_finish.
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003352 */
3353struct ring_buffer_iter *
David Miller72c9ddf2010-04-20 15:47:11 -07003354ring_buffer_read_prepare(struct ring_buffer *buffer, int cpu)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003355{
3356 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedt8aabee52009-03-12 13:13:49 -04003357 struct ring_buffer_iter *iter;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003358
Rusty Russell9e01c1b2009-01-01 10:12:22 +10303359 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04003360 return NULL;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003361
3362 iter = kmalloc(sizeof(*iter), GFP_KERNEL);
3363 if (!iter)
Steven Rostedt8aabee52009-03-12 13:13:49 -04003364 return NULL;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003365
3366 cpu_buffer = buffer->buffers[cpu];
3367
3368 iter->cpu_buffer = cpu_buffer;
3369
3370 atomic_inc(&cpu_buffer->record_disabled);
David Miller72c9ddf2010-04-20 15:47:11 -07003371
3372 return iter;
3373}
3374EXPORT_SYMBOL_GPL(ring_buffer_read_prepare);
3375
3376/**
3377 * ring_buffer_read_prepare_sync - Synchronize a set of prepare calls
3378 *
3379 * All previously invoked ring_buffer_read_prepare calls to prepare
3380 * iterators will be synchronized. Afterwards, read_buffer_read_start
3381 * calls on those iterators are allowed.
3382 */
3383void
3384ring_buffer_read_prepare_sync(void)
3385{
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003386 synchronize_sched();
David Miller72c9ddf2010-04-20 15:47:11 -07003387}
3388EXPORT_SYMBOL_GPL(ring_buffer_read_prepare_sync);
3389
3390/**
3391 * ring_buffer_read_start - start a non consuming read of the buffer
3392 * @iter: The iterator returned by ring_buffer_read_prepare
3393 *
3394 * This finalizes the startup of an iteration through the buffer.
3395 * The iterator comes from a call to ring_buffer_read_prepare and
3396 * an intervening ring_buffer_read_prepare_sync must have been
3397 * performed.
3398 *
3399 * Must be paired with ring_buffer_finish.
3400 */
3401void
3402ring_buffer_read_start(struct ring_buffer_iter *iter)
3403{
3404 struct ring_buffer_per_cpu *cpu_buffer;
3405 unsigned long flags;
3406
3407 if (!iter)
3408 return;
3409
3410 cpu_buffer = iter->cpu_buffer;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003411
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003412 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01003413 arch_spin_lock(&cpu_buffer->lock);
Steven Rostedt642edba2008-11-12 00:01:26 -05003414 rb_iter_reset(iter);
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01003415 arch_spin_unlock(&cpu_buffer->lock);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003416 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003417}
Robert Richterc4f50182008-12-11 16:49:22 +01003418EXPORT_SYMBOL_GPL(ring_buffer_read_start);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003419
3420/**
3421 * ring_buffer_finish - finish reading the iterator of the buffer
3422 * @iter: The iterator retrieved by ring_buffer_start
3423 *
3424 * This re-enables the recording to the buffer, and frees the
3425 * iterator.
3426 */
3427void
3428ring_buffer_read_finish(struct ring_buffer_iter *iter)
3429{
3430 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
3431
3432 atomic_dec(&cpu_buffer->record_disabled);
3433 kfree(iter);
3434}
Robert Richterc4f50182008-12-11 16:49:22 +01003435EXPORT_SYMBOL_GPL(ring_buffer_read_finish);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003436
3437/**
3438 * ring_buffer_read - read the next item in the ring buffer by the iterator
3439 * @iter: The ring buffer iterator
3440 * @ts: The time stamp of the event read.
3441 *
3442 * This reads the next event in the ring buffer and increments the iterator.
3443 */
3444struct ring_buffer_event *
3445ring_buffer_read(struct ring_buffer_iter *iter, u64 *ts)
3446{
3447 struct ring_buffer_event *event;
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003448 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
3449 unsigned long flags;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003450
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003451 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
Steven Rostedt7e9391c2009-09-03 10:02:09 -04003452 again:
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003453 event = rb_iter_peek(iter, ts);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003454 if (!event)
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003455 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003456
Steven Rostedt7e9391c2009-09-03 10:02:09 -04003457 if (event->type_len == RINGBUF_TYPE_PADDING)
3458 goto again;
3459
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003460 rb_advance_iter(iter);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003461 out:
3462 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003463
3464 return event;
3465}
Robert Richterc4f50182008-12-11 16:49:22 +01003466EXPORT_SYMBOL_GPL(ring_buffer_read);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003467
3468/**
3469 * ring_buffer_size - return the size of the ring buffer (in bytes)
3470 * @buffer: The ring buffer.
3471 */
3472unsigned long ring_buffer_size(struct ring_buffer *buffer)
3473{
3474 return BUF_PAGE_SIZE * buffer->pages;
3475}
Robert Richterc4f50182008-12-11 16:49:22 +01003476EXPORT_SYMBOL_GPL(ring_buffer_size);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003477
3478static void
3479rb_reset_cpu(struct ring_buffer_per_cpu *cpu_buffer)
3480{
Steven Rostedt77ae3652009-03-27 11:00:29 -04003481 rb_head_page_deactivate(cpu_buffer);
3482
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003483 cpu_buffer->head_page
Steven Rostedt3adc54f2009-03-30 15:32:01 -04003484 = list_entry(cpu_buffer->pages, struct buffer_page, list);
Steven Rostedtbf41a152008-10-04 02:00:59 -04003485 local_set(&cpu_buffer->head_page->write, 0);
Steven Rostedt778c55d2009-05-01 18:44:45 -04003486 local_set(&cpu_buffer->head_page->entries, 0);
Steven Rostedtabc9b562008-12-02 15:34:06 -05003487 local_set(&cpu_buffer->head_page->page->commit, 0);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003488
Steven Rostedt6f807ac2008-10-04 02:00:58 -04003489 cpu_buffer->head_page->read = 0;
Steven Rostedtbf41a152008-10-04 02:00:59 -04003490
3491 cpu_buffer->tail_page = cpu_buffer->head_page;
3492 cpu_buffer->commit_page = cpu_buffer->head_page;
3493
3494 INIT_LIST_HEAD(&cpu_buffer->reader_page->list);
3495 local_set(&cpu_buffer->reader_page->write, 0);
Steven Rostedt778c55d2009-05-01 18:44:45 -04003496 local_set(&cpu_buffer->reader_page->entries, 0);
Steven Rostedtabc9b562008-12-02 15:34:06 -05003497 local_set(&cpu_buffer->reader_page->page->commit, 0);
Steven Rostedt6f807ac2008-10-04 02:00:58 -04003498 cpu_buffer->reader_page->read = 0;
Steven Rostedtd7690412008-10-01 00:29:53 -04003499
Steven Rostedt77ae3652009-03-27 11:00:29 -04003500 local_set(&cpu_buffer->commit_overrun, 0);
3501 local_set(&cpu_buffer->overrun, 0);
Steven Rostedte4906ef2009-04-30 20:49:44 -04003502 local_set(&cpu_buffer->entries, 0);
Steven Rostedtfa743952009-06-16 12:37:57 -04003503 local_set(&cpu_buffer->committing, 0);
3504 local_set(&cpu_buffer->commits, 0);
Steven Rostedt77ae3652009-03-27 11:00:29 -04003505 cpu_buffer->read = 0;
Steven Rostedt69507c02009-01-21 18:45:57 -05003506
3507 cpu_buffer->write_stamp = 0;
3508 cpu_buffer->read_stamp = 0;
Steven Rostedt77ae3652009-03-27 11:00:29 -04003509
Steven Rostedt66a8cb92010-03-31 13:21:56 -04003510 cpu_buffer->lost_events = 0;
3511 cpu_buffer->last_overrun = 0;
3512
Steven Rostedt77ae3652009-03-27 11:00:29 -04003513 rb_head_page_activate(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003514}
3515
3516/**
3517 * ring_buffer_reset_cpu - reset a ring buffer per CPU buffer
3518 * @buffer: The ring buffer to reset a per cpu buffer of
3519 * @cpu: The CPU buffer to be reset
3520 */
3521void ring_buffer_reset_cpu(struct ring_buffer *buffer, int cpu)
3522{
3523 struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
3524 unsigned long flags;
3525
Rusty Russell9e01c1b2009-01-01 10:12:22 +10303526 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04003527 return;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003528
Steven Rostedt41ede232009-05-01 20:26:54 -04003529 atomic_inc(&cpu_buffer->record_disabled);
3530
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003531 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
3532
Steven Rostedt41b6a952009-09-02 09:59:48 -04003533 if (RB_WARN_ON(cpu_buffer, local_read(&cpu_buffer->committing)))
3534 goto out;
3535
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01003536 arch_spin_lock(&cpu_buffer->lock);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003537
3538 rb_reset_cpu(cpu_buffer);
3539
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01003540 arch_spin_unlock(&cpu_buffer->lock);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003541
Steven Rostedt41b6a952009-09-02 09:59:48 -04003542 out:
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003543 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
Steven Rostedt41ede232009-05-01 20:26:54 -04003544
3545 atomic_dec(&cpu_buffer->record_disabled);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003546}
Robert Richterc4f50182008-12-11 16:49:22 +01003547EXPORT_SYMBOL_GPL(ring_buffer_reset_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003548
3549/**
3550 * ring_buffer_reset - reset a ring buffer
3551 * @buffer: The ring buffer to reset all cpu buffers
3552 */
3553void ring_buffer_reset(struct ring_buffer *buffer)
3554{
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003555 int cpu;
3556
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003557 for_each_buffer_cpu(buffer, cpu)
Steven Rostedtd7690412008-10-01 00:29:53 -04003558 ring_buffer_reset_cpu(buffer, cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003559}
Robert Richterc4f50182008-12-11 16:49:22 +01003560EXPORT_SYMBOL_GPL(ring_buffer_reset);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003561
3562/**
3563 * rind_buffer_empty - is the ring buffer empty?
3564 * @buffer: The ring buffer to test
3565 */
3566int ring_buffer_empty(struct ring_buffer *buffer)
3567{
3568 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedtd4788202009-06-17 00:39:43 -04003569 unsigned long flags;
Steven Rostedt8d707e82009-06-16 21:22:48 -04003570 int dolock;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003571 int cpu;
Steven Rostedtd4788202009-06-17 00:39:43 -04003572 int ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003573
Steven Rostedt8d707e82009-06-16 21:22:48 -04003574 dolock = rb_ok_to_lock();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003575
3576 /* yes this is racy, but if you don't like the race, lock the buffer */
3577 for_each_buffer_cpu(buffer, cpu) {
3578 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt8d707e82009-06-16 21:22:48 -04003579 local_irq_save(flags);
3580 if (dolock)
3581 spin_lock(&cpu_buffer->reader_lock);
Steven Rostedtd4788202009-06-17 00:39:43 -04003582 ret = rb_per_cpu_empty(cpu_buffer);
Steven Rostedt8d707e82009-06-16 21:22:48 -04003583 if (dolock)
3584 spin_unlock(&cpu_buffer->reader_lock);
3585 local_irq_restore(flags);
3586
Steven Rostedtd4788202009-06-17 00:39:43 -04003587 if (!ret)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003588 return 0;
3589 }
Steven Rostedt554f7862009-03-11 22:00:13 -04003590
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003591 return 1;
3592}
Robert Richterc4f50182008-12-11 16:49:22 +01003593EXPORT_SYMBOL_GPL(ring_buffer_empty);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003594
3595/**
3596 * ring_buffer_empty_cpu - is a cpu buffer of a ring buffer empty?
3597 * @buffer: The ring buffer
3598 * @cpu: The CPU buffer to test
3599 */
3600int ring_buffer_empty_cpu(struct ring_buffer *buffer, int cpu)
3601{
3602 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedtd4788202009-06-17 00:39:43 -04003603 unsigned long flags;
Steven Rostedt8d707e82009-06-16 21:22:48 -04003604 int dolock;
Steven Rostedt8aabee52009-03-12 13:13:49 -04003605 int ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003606
Rusty Russell9e01c1b2009-01-01 10:12:22 +10303607 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04003608 return 1;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003609
Steven Rostedt8d707e82009-06-16 21:22:48 -04003610 dolock = rb_ok_to_lock();
Steven Rostedt554f7862009-03-11 22:00:13 -04003611
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003612 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt8d707e82009-06-16 21:22:48 -04003613 local_irq_save(flags);
3614 if (dolock)
3615 spin_lock(&cpu_buffer->reader_lock);
Steven Rostedt554f7862009-03-11 22:00:13 -04003616 ret = rb_per_cpu_empty(cpu_buffer);
Steven Rostedt8d707e82009-06-16 21:22:48 -04003617 if (dolock)
3618 spin_unlock(&cpu_buffer->reader_lock);
3619 local_irq_restore(flags);
Steven Rostedt554f7862009-03-11 22:00:13 -04003620
3621 return ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003622}
Robert Richterc4f50182008-12-11 16:49:22 +01003623EXPORT_SYMBOL_GPL(ring_buffer_empty_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003624
Steven Rostedt85bac322009-09-04 14:24:40 -04003625#ifdef CONFIG_RING_BUFFER_ALLOW_SWAP
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003626/**
3627 * ring_buffer_swap_cpu - swap a CPU buffer between two ring buffers
3628 * @buffer_a: One buffer to swap with
3629 * @buffer_b: The other buffer to swap with
3630 *
3631 * This function is useful for tracers that want to take a "snapshot"
3632 * of a CPU buffer and has another back up buffer lying around.
3633 * it is expected that the tracer handles the cpu buffer not being
3634 * used at the moment.
3635 */
3636int ring_buffer_swap_cpu(struct ring_buffer *buffer_a,
3637 struct ring_buffer *buffer_b, int cpu)
3638{
3639 struct ring_buffer_per_cpu *cpu_buffer_a;
3640 struct ring_buffer_per_cpu *cpu_buffer_b;
Steven Rostedt554f7862009-03-11 22:00:13 -04003641 int ret = -EINVAL;
3642
Rusty Russell9e01c1b2009-01-01 10:12:22 +10303643 if (!cpumask_test_cpu(cpu, buffer_a->cpumask) ||
3644 !cpumask_test_cpu(cpu, buffer_b->cpumask))
Steven Rostedt554f7862009-03-11 22:00:13 -04003645 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003646
3647 /* At least make sure the two buffers are somewhat the same */
Lai Jiangshan6d102bc2008-12-17 17:48:23 +08003648 if (buffer_a->pages != buffer_b->pages)
Steven Rostedt554f7862009-03-11 22:00:13 -04003649 goto out;
3650
3651 ret = -EAGAIN;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003652
Steven Rostedt97b17ef2009-01-21 15:24:56 -05003653 if (ring_buffer_flags != RB_BUFFERS_ON)
Steven Rostedt554f7862009-03-11 22:00:13 -04003654 goto out;
Steven Rostedt97b17ef2009-01-21 15:24:56 -05003655
3656 if (atomic_read(&buffer_a->record_disabled))
Steven Rostedt554f7862009-03-11 22:00:13 -04003657 goto out;
Steven Rostedt97b17ef2009-01-21 15:24:56 -05003658
3659 if (atomic_read(&buffer_b->record_disabled))
Steven Rostedt554f7862009-03-11 22:00:13 -04003660 goto out;
Steven Rostedt97b17ef2009-01-21 15:24:56 -05003661
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003662 cpu_buffer_a = buffer_a->buffers[cpu];
3663 cpu_buffer_b = buffer_b->buffers[cpu];
3664
Steven Rostedt97b17ef2009-01-21 15:24:56 -05003665 if (atomic_read(&cpu_buffer_a->record_disabled))
Steven Rostedt554f7862009-03-11 22:00:13 -04003666 goto out;
Steven Rostedt97b17ef2009-01-21 15:24:56 -05003667
3668 if (atomic_read(&cpu_buffer_b->record_disabled))
Steven Rostedt554f7862009-03-11 22:00:13 -04003669 goto out;
Steven Rostedt97b17ef2009-01-21 15:24:56 -05003670
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003671 /*
3672 * We can't do a synchronize_sched here because this
3673 * function can be called in atomic context.
3674 * Normally this will be called from the same CPU as cpu.
3675 * If not it's up to the caller to protect this.
3676 */
3677 atomic_inc(&cpu_buffer_a->record_disabled);
3678 atomic_inc(&cpu_buffer_b->record_disabled);
3679
Steven Rostedt98277992009-09-02 10:56:15 -04003680 ret = -EBUSY;
3681 if (local_read(&cpu_buffer_a->committing))
3682 goto out_dec;
3683 if (local_read(&cpu_buffer_b->committing))
3684 goto out_dec;
3685
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003686 buffer_a->buffers[cpu] = cpu_buffer_b;
3687 buffer_b->buffers[cpu] = cpu_buffer_a;
3688
3689 cpu_buffer_b->buffer = buffer_a;
3690 cpu_buffer_a->buffer = buffer_b;
3691
Steven Rostedt98277992009-09-02 10:56:15 -04003692 ret = 0;
3693
3694out_dec:
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003695 atomic_dec(&cpu_buffer_a->record_disabled);
3696 atomic_dec(&cpu_buffer_b->record_disabled);
Steven Rostedt554f7862009-03-11 22:00:13 -04003697out:
Steven Rostedt554f7862009-03-11 22:00:13 -04003698 return ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003699}
Robert Richterc4f50182008-12-11 16:49:22 +01003700EXPORT_SYMBOL_GPL(ring_buffer_swap_cpu);
Steven Rostedt85bac322009-09-04 14:24:40 -04003701#endif /* CONFIG_RING_BUFFER_ALLOW_SWAP */
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003702
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003703/**
3704 * ring_buffer_alloc_read_page - allocate a page to read from buffer
3705 * @buffer: the buffer to allocate for.
3706 *
3707 * This function is used in conjunction with ring_buffer_read_page.
3708 * When reading a full page from the ring buffer, these functions
3709 * can be used to speed up the process. The calling function should
3710 * allocate a few pages first with this function. Then when it
3711 * needs to get pages from the ring buffer, it passes the result
3712 * of this function into ring_buffer_read_page, which will swap
3713 * the page that was allocated, with the read page of the buffer.
3714 *
3715 * Returns:
3716 * The page allocated, or NULL on error.
3717 */
3718void *ring_buffer_alloc_read_page(struct ring_buffer *buffer)
3719{
Steven Rostedt044fa782008-12-02 23:50:03 -05003720 struct buffer_data_page *bpage;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003721 unsigned long addr;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003722
3723 addr = __get_free_page(GFP_KERNEL);
3724 if (!addr)
3725 return NULL;
3726
Steven Rostedt044fa782008-12-02 23:50:03 -05003727 bpage = (void *)addr;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003728
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003729 rb_init_page(bpage);
3730
Steven Rostedt044fa782008-12-02 23:50:03 -05003731 return bpage;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003732}
Steven Rostedtd6ce96d2009-05-05 01:15:24 -04003733EXPORT_SYMBOL_GPL(ring_buffer_alloc_read_page);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003734
3735/**
3736 * ring_buffer_free_read_page - free an allocated read page
3737 * @buffer: the buffer the page was allocate for
3738 * @data: the page to free
3739 *
3740 * Free a page allocated from ring_buffer_alloc_read_page.
3741 */
3742void ring_buffer_free_read_page(struct ring_buffer *buffer, void *data)
3743{
3744 free_page((unsigned long)data);
3745}
Steven Rostedtd6ce96d2009-05-05 01:15:24 -04003746EXPORT_SYMBOL_GPL(ring_buffer_free_read_page);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003747
3748/**
3749 * ring_buffer_read_page - extract a page from the ring buffer
3750 * @buffer: buffer to extract from
3751 * @data_page: the page to use allocated from ring_buffer_alloc_read_page
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003752 * @len: amount to extract
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003753 * @cpu: the cpu of the buffer to extract
3754 * @full: should the extraction only happen when the page is full.
3755 *
3756 * This function will pull out a page from the ring buffer and consume it.
3757 * @data_page must be the address of the variable that was returned
3758 * from ring_buffer_alloc_read_page. This is because the page might be used
3759 * to swap with a page in the ring buffer.
3760 *
3761 * for example:
Lai Jiangshanb85fa012009-02-09 14:21:14 +08003762 * rpage = ring_buffer_alloc_read_page(buffer);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003763 * if (!rpage)
3764 * return error;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003765 * ret = ring_buffer_read_page(buffer, &rpage, len, cpu, 0);
Lai Jiangshan667d2412009-02-09 14:21:17 +08003766 * if (ret >= 0)
3767 * process_page(rpage, ret);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003768 *
3769 * When @full is set, the function will not return true unless
3770 * the writer is off the reader page.
3771 *
3772 * Note: it is up to the calling functions to handle sleeps and wakeups.
3773 * The ring buffer can be used anywhere in the kernel and can not
3774 * blindly call wake_up. The layer that uses the ring buffer must be
3775 * responsible for that.
3776 *
3777 * Returns:
Lai Jiangshan667d2412009-02-09 14:21:17 +08003778 * >=0 if data has been transferred, returns the offset of consumed data.
3779 * <0 if no data has been transferred.
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003780 */
3781int ring_buffer_read_page(struct ring_buffer *buffer,
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003782 void **data_page, size_t len, int cpu, int full)
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003783{
3784 struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
3785 struct ring_buffer_event *event;
Steven Rostedt044fa782008-12-02 23:50:03 -05003786 struct buffer_data_page *bpage;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003787 struct buffer_page *reader;
Steven Rostedtff0ff842010-03-31 22:11:42 -04003788 unsigned long missed_events;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003789 unsigned long flags;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003790 unsigned int commit;
Lai Jiangshan667d2412009-02-09 14:21:17 +08003791 unsigned int read;
Steven Rostedt4f3640f2009-03-03 23:52:42 -05003792 u64 save_timestamp;
Lai Jiangshan667d2412009-02-09 14:21:17 +08003793 int ret = -1;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003794
Steven Rostedt554f7862009-03-11 22:00:13 -04003795 if (!cpumask_test_cpu(cpu, buffer->cpumask))
3796 goto out;
3797
Steven Rostedt474d32b2009-03-03 19:51:40 -05003798 /*
3799 * If len is not big enough to hold the page header, then
3800 * we can not copy anything.
3801 */
3802 if (len <= BUF_PAGE_HDR_SIZE)
Steven Rostedt554f7862009-03-11 22:00:13 -04003803 goto out;
Steven Rostedt474d32b2009-03-03 19:51:40 -05003804
3805 len -= BUF_PAGE_HDR_SIZE;
3806
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003807 if (!data_page)
Steven Rostedt554f7862009-03-11 22:00:13 -04003808 goto out;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003809
Steven Rostedt044fa782008-12-02 23:50:03 -05003810 bpage = *data_page;
3811 if (!bpage)
Steven Rostedt554f7862009-03-11 22:00:13 -04003812 goto out;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003813
3814 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
3815
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003816 reader = rb_get_reader_page(cpu_buffer);
3817 if (!reader)
Steven Rostedt554f7862009-03-11 22:00:13 -04003818 goto out_unlock;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003819
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003820 event = rb_reader_event(cpu_buffer);
Lai Jiangshan667d2412009-02-09 14:21:17 +08003821
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003822 read = reader->read;
3823 commit = rb_page_commit(reader);
3824
Steven Rostedt66a8cb92010-03-31 13:21:56 -04003825 /* Check if any events were dropped */
Steven Rostedtff0ff842010-03-31 22:11:42 -04003826 missed_events = cpu_buffer->lost_events;
Steven Rostedt66a8cb92010-03-31 13:21:56 -04003827
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003828 /*
Steven Rostedt474d32b2009-03-03 19:51:40 -05003829 * If this page has been partially read or
3830 * if len is not big enough to read the rest of the page or
3831 * a writer is still on the page, then
3832 * we must copy the data from the page to the buffer.
3833 * Otherwise, we can simply swap the page with the one passed in.
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003834 */
Steven Rostedt474d32b2009-03-03 19:51:40 -05003835 if (read || (len < (commit - read)) ||
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003836 cpu_buffer->reader_page == cpu_buffer->commit_page) {
Lai Jiangshan667d2412009-02-09 14:21:17 +08003837 struct buffer_data_page *rpage = cpu_buffer->reader_page->page;
Steven Rostedt474d32b2009-03-03 19:51:40 -05003838 unsigned int rpos = read;
3839 unsigned int pos = 0;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003840 unsigned int size;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003841
3842 if (full)
Steven Rostedt554f7862009-03-11 22:00:13 -04003843 goto out_unlock;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003844
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003845 if (len > (commit - read))
3846 len = (commit - read);
3847
Steven Rostedt69d1b832010-10-07 18:18:05 -04003848 /* Always keep the time extend and data together */
3849 size = rb_event_ts_length(event);
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003850
3851 if (len < size)
Steven Rostedt554f7862009-03-11 22:00:13 -04003852 goto out_unlock;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003853
Steven Rostedt4f3640f2009-03-03 23:52:42 -05003854 /* save the current timestamp, since the user will need it */
3855 save_timestamp = cpu_buffer->read_stamp;
3856
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003857 /* Need to copy one event at a time */
3858 do {
Steven Rostedt474d32b2009-03-03 19:51:40 -05003859 memcpy(bpage->data + pos, rpage->data + rpos, size);
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003860
3861 len -= size;
3862
3863 rb_advance_reader(cpu_buffer);
Steven Rostedt474d32b2009-03-03 19:51:40 -05003864 rpos = reader->read;
3865 pos += size;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003866
Huang Ying18fab912010-07-28 14:14:01 +08003867 if (rpos >= commit)
3868 break;
3869
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003870 event = rb_reader_event(cpu_buffer);
Steven Rostedt69d1b832010-10-07 18:18:05 -04003871 /* Always keep the time extend and data together */
3872 size = rb_event_ts_length(event);
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003873 } while (len > size);
Lai Jiangshan667d2412009-02-09 14:21:17 +08003874
3875 /* update bpage */
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003876 local_set(&bpage->commit, pos);
Steven Rostedt4f3640f2009-03-03 23:52:42 -05003877 bpage->time_stamp = save_timestamp;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003878
Steven Rostedt474d32b2009-03-03 19:51:40 -05003879 /* we copied everything to the beginning */
3880 read = 0;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003881 } else {
Steven Rostedtafbab762009-05-01 19:40:05 -04003882 /* update the entry counter */
Steven Rostedt77ae3652009-03-27 11:00:29 -04003883 cpu_buffer->read += rb_page_entries(reader);
Steven Rostedtafbab762009-05-01 19:40:05 -04003884
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003885 /* swap the pages */
Steven Rostedt044fa782008-12-02 23:50:03 -05003886 rb_init_page(bpage);
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003887 bpage = reader->page;
3888 reader->page = *data_page;
3889 local_set(&reader->write, 0);
Steven Rostedt778c55d2009-05-01 18:44:45 -04003890 local_set(&reader->entries, 0);
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003891 reader->read = 0;
Steven Rostedt044fa782008-12-02 23:50:03 -05003892 *data_page = bpage;
Steven Rostedtff0ff842010-03-31 22:11:42 -04003893
3894 /*
3895 * Use the real_end for the data size,
3896 * This gives us a chance to store the lost events
3897 * on the page.
3898 */
3899 if (reader->real_end)
3900 local_set(&bpage->commit, reader->real_end);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003901 }
Lai Jiangshan667d2412009-02-09 14:21:17 +08003902 ret = read;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003903
Steven Rostedt66a8cb92010-03-31 13:21:56 -04003904 cpu_buffer->lost_events = 0;
Steven Rostedt2711ca22010-05-21 13:32:26 -04003905
3906 commit = local_read(&bpage->commit);
Steven Rostedt66a8cb92010-03-31 13:21:56 -04003907 /*
3908 * Set a flag in the commit field if we lost events
3909 */
Steven Rostedtff0ff842010-03-31 22:11:42 -04003910 if (missed_events) {
Steven Rostedtff0ff842010-03-31 22:11:42 -04003911 /* If there is room at the end of the page to save the
3912 * missed events, then record it there.
3913 */
3914 if (BUF_PAGE_SIZE - commit >= sizeof(missed_events)) {
3915 memcpy(&bpage->data[commit], &missed_events,
3916 sizeof(missed_events));
3917 local_add(RB_MISSED_STORED, &bpage->commit);
Steven Rostedt2711ca22010-05-21 13:32:26 -04003918 commit += sizeof(missed_events);
Steven Rostedtff0ff842010-03-31 22:11:42 -04003919 }
Steven Rostedt66a8cb92010-03-31 13:21:56 -04003920 local_add(RB_MISSED_EVENTS, &bpage->commit);
Steven Rostedtff0ff842010-03-31 22:11:42 -04003921 }
Steven Rostedt66a8cb92010-03-31 13:21:56 -04003922
Steven Rostedt2711ca22010-05-21 13:32:26 -04003923 /*
3924 * This page may be off to user land. Zero it out here.
3925 */
3926 if (commit < BUF_PAGE_SIZE)
3927 memset(&bpage->data[commit], 0, BUF_PAGE_SIZE - commit);
3928
Steven Rostedt554f7862009-03-11 22:00:13 -04003929 out_unlock:
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003930 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
3931
Steven Rostedt554f7862009-03-11 22:00:13 -04003932 out:
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003933 return ret;
3934}
Steven Rostedtd6ce96d2009-05-05 01:15:24 -04003935EXPORT_SYMBOL_GPL(ring_buffer_read_page);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003936
Paul Mundt1155de42009-06-25 14:30:12 +09003937#ifdef CONFIG_TRACING
Steven Rostedta3583242008-11-11 15:01:42 -05003938static ssize_t
3939rb_simple_read(struct file *filp, char __user *ubuf,
3940 size_t cnt, loff_t *ppos)
3941{
Hannes Eder5e398412009-02-10 19:44:34 +01003942 unsigned long *p = filp->private_data;
Steven Rostedta3583242008-11-11 15:01:42 -05003943 char buf[64];
3944 int r;
3945
Steven Rostedt033601a2008-11-21 12:41:55 -05003946 if (test_bit(RB_BUFFERS_DISABLED_BIT, p))
3947 r = sprintf(buf, "permanently disabled\n");
3948 else
3949 r = sprintf(buf, "%d\n", test_bit(RB_BUFFERS_ON_BIT, p));
Steven Rostedta3583242008-11-11 15:01:42 -05003950
3951 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
3952}
3953
3954static ssize_t
3955rb_simple_write(struct file *filp, const char __user *ubuf,
3956 size_t cnt, loff_t *ppos)
3957{
Hannes Eder5e398412009-02-10 19:44:34 +01003958 unsigned long *p = filp->private_data;
Steven Rostedta3583242008-11-11 15:01:42 -05003959 char buf[64];
Hannes Eder5e398412009-02-10 19:44:34 +01003960 unsigned long val;
Steven Rostedta3583242008-11-11 15:01:42 -05003961 int ret;
3962
3963 if (cnt >= sizeof(buf))
3964 return -EINVAL;
3965
3966 if (copy_from_user(&buf, ubuf, cnt))
3967 return -EFAULT;
3968
3969 buf[cnt] = 0;
3970
3971 ret = strict_strtoul(buf, 10, &val);
3972 if (ret < 0)
3973 return ret;
3974
Steven Rostedt033601a2008-11-21 12:41:55 -05003975 if (val)
3976 set_bit(RB_BUFFERS_ON_BIT, p);
3977 else
3978 clear_bit(RB_BUFFERS_ON_BIT, p);
Steven Rostedta3583242008-11-11 15:01:42 -05003979
3980 (*ppos)++;
3981
3982 return cnt;
3983}
3984
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003985static const struct file_operations rb_simple_fops = {
Steven Rostedta3583242008-11-11 15:01:42 -05003986 .open = tracing_open_generic,
3987 .read = rb_simple_read,
3988 .write = rb_simple_write,
3989};
3990
3991
3992static __init int rb_init_debugfs(void)
3993{
3994 struct dentry *d_tracer;
Steven Rostedta3583242008-11-11 15:01:42 -05003995
3996 d_tracer = tracing_init_dentry();
3997
Frederic Weisbecker5452af62009-03-27 00:25:38 +01003998 trace_create_file("tracing_on", 0644, d_tracer,
3999 &ring_buffer_flags, &rb_simple_fops);
Steven Rostedta3583242008-11-11 15:01:42 -05004000
4001 return 0;
4002}
4003
4004fs_initcall(rb_init_debugfs);
Paul Mundt1155de42009-06-25 14:30:12 +09004005#endif
Steven Rostedt554f7862009-03-11 22:00:13 -04004006
Steven Rostedt59222ef2009-03-12 11:46:03 -04004007#ifdef CONFIG_HOTPLUG_CPU
Frederic Weisbecker09c9e842009-03-21 04:33:36 +01004008static int rb_cpu_notify(struct notifier_block *self,
4009 unsigned long action, void *hcpu)
Steven Rostedt554f7862009-03-11 22:00:13 -04004010{
4011 struct ring_buffer *buffer =
4012 container_of(self, struct ring_buffer, cpu_notify);
4013 long cpu = (long)hcpu;
4014
4015 switch (action) {
4016 case CPU_UP_PREPARE:
4017 case CPU_UP_PREPARE_FROZEN:
Rusty Russell3f237a72009-06-12 21:15:30 +09304018 if (cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt554f7862009-03-11 22:00:13 -04004019 return NOTIFY_OK;
4020
4021 buffer->buffers[cpu] =
4022 rb_allocate_cpu_buffer(buffer, cpu);
4023 if (!buffer->buffers[cpu]) {
4024 WARN(1, "failed to allocate ring buffer on CPU %ld\n",
4025 cpu);
4026 return NOTIFY_OK;
4027 }
4028 smp_wmb();
Rusty Russell3f237a72009-06-12 21:15:30 +09304029 cpumask_set_cpu(cpu, buffer->cpumask);
Steven Rostedt554f7862009-03-11 22:00:13 -04004030 break;
4031 case CPU_DOWN_PREPARE:
4032 case CPU_DOWN_PREPARE_FROZEN:
4033 /*
4034 * Do nothing.
4035 * If we were to free the buffer, then the user would
4036 * lose any trace that was in the buffer.
4037 */
4038 break;
4039 default:
4040 break;
4041 }
4042 return NOTIFY_OK;
4043}
4044#endif