blob: bd1c35a4fbccf31c0531f0667545011f704c561f [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 Rostedtd1b182a2009-04-15 16:53:47 -0400444int ring_buffer_print_page_header(struct trace_seq *s)
445{
446 struct buffer_data_page field;
447 int ret;
448
449 ret = trace_seq_printf(s, "\tfield: u64 timestamp;\t"
Tom Zanussi26a50742009-10-06 01:09:50 -0500450 "offset:0;\tsize:%u;\tsigned:%u;\n",
451 (unsigned int)sizeof(field.time_stamp),
452 (unsigned int)is_signed_type(u64));
Steven Rostedtd1b182a2009-04-15 16:53:47 -0400453
454 ret = trace_seq_printf(s, "\tfield: local_t commit;\t"
Tom Zanussi26a50742009-10-06 01:09:50 -0500455 "offset:%u;\tsize:%u;\tsigned:%u;\n",
Steven Rostedtd1b182a2009-04-15 16:53:47 -0400456 (unsigned int)offsetof(typeof(field), commit),
Tom Zanussi26a50742009-10-06 01:09:50 -0500457 (unsigned int)sizeof(field.commit),
458 (unsigned int)is_signed_type(long));
Steven Rostedtd1b182a2009-04-15 16:53:47 -0400459
Steven Rostedt66a8cb92010-03-31 13:21:56 -0400460 ret = trace_seq_printf(s, "\tfield: int overwrite;\t"
461 "offset:%u;\tsize:%u;\tsigned:%u;\n",
462 (unsigned int)offsetof(typeof(field), commit),
463 1,
464 (unsigned int)is_signed_type(long));
465
Steven Rostedtd1b182a2009-04-15 16:53:47 -0400466 ret = trace_seq_printf(s, "\tfield: char data;\t"
Tom Zanussi26a50742009-10-06 01:09:50 -0500467 "offset:%u;\tsize:%u;\tsigned:%u;\n",
Steven Rostedtd1b182a2009-04-15 16:53:47 -0400468 (unsigned int)offsetof(typeof(field), data),
Tom Zanussi26a50742009-10-06 01:09:50 -0500469 (unsigned int)BUF_PAGE_SIZE,
470 (unsigned int)is_signed_type(char));
Steven Rostedtd1b182a2009-04-15 16:53:47 -0400471
472 return ret;
473}
474
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400475/*
476 * head_page == tail_page && head == tail then buffer is empty.
477 */
478struct ring_buffer_per_cpu {
479 int cpu;
Richard Kennedy985023d2010-03-25 11:27:36 +0000480 atomic_t record_disabled;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400481 struct ring_buffer *buffer;
Steven Rostedt77ae3652009-03-27 11:00:29 -0400482 spinlock_t reader_lock; /* serialize readers */
Thomas Gleixner445c8952009-12-02 19:49:50 +0100483 arch_spinlock_t lock;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400484 struct lock_class_key lock_key;
Steven Rostedt3adc54f2009-03-30 15:32:01 -0400485 struct list_head *pages;
Steven Rostedt6f807ac2008-10-04 02:00:58 -0400486 struct buffer_page *head_page; /* read from head */
487 struct buffer_page *tail_page; /* write to tail */
Wenji Huangc3706f02009-02-10 01:03:18 -0500488 struct buffer_page *commit_page; /* committed pages */
Steven Rostedtd7690412008-10-01 00:29:53 -0400489 struct buffer_page *reader_page;
Steven Rostedt66a8cb92010-03-31 13:21:56 -0400490 unsigned long lost_events;
491 unsigned long last_overrun;
Steven Rostedt77ae3652009-03-27 11:00:29 -0400492 local_t commit_overrun;
493 local_t overrun;
Steven Rostedte4906ef2009-04-30 20:49:44 -0400494 local_t entries;
Steven Rostedtfa743952009-06-16 12:37:57 -0400495 local_t committing;
496 local_t commits;
Steven Rostedt77ae3652009-03-27 11:00:29 -0400497 unsigned long read;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400498 u64 write_stamp;
499 u64 read_stamp;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400500};
501
502struct ring_buffer {
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400503 unsigned pages;
504 unsigned flags;
505 int cpus;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400506 atomic_t record_disabled;
Arnaldo Carvalho de Melo00f62f62009-02-09 17:04:06 -0200507 cpumask_var_t cpumask;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400508
Peter Zijlstra1f8a6a12009-06-08 18:18:39 +0200509 struct lock_class_key *reader_lock_key;
510
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400511 struct mutex mutex;
512
513 struct ring_buffer_per_cpu **buffers;
Steven Rostedt554f7862009-03-11 22:00:13 -0400514
Steven Rostedt59222ef2009-03-12 11:46:03 -0400515#ifdef CONFIG_HOTPLUG_CPU
Steven Rostedt554f7862009-03-11 22:00:13 -0400516 struct notifier_block cpu_notify;
517#endif
Steven Rostedt37886f62009-03-17 17:22:06 -0400518 u64 (*clock)(void);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400519};
520
521struct ring_buffer_iter {
522 struct ring_buffer_per_cpu *cpu_buffer;
523 unsigned long head;
524 struct buffer_page *head_page;
Steven Rostedt492a74f2010-01-25 15:17:47 -0500525 struct buffer_page *cache_reader_page;
526 unsigned long cache_read;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400527 u64 read_stamp;
528};
529
Steven Rostedtf536aaf2008-11-10 23:07:30 -0500530/* buffer may be either ring_buffer or ring_buffer_per_cpu */
Steven Rostedt077c5402009-09-03 19:53:46 -0400531#define RB_WARN_ON(b, cond) \
532 ({ \
533 int _____ret = unlikely(cond); \
534 if (_____ret) { \
535 if (__same_type(*(b), struct ring_buffer_per_cpu)) { \
536 struct ring_buffer_per_cpu *__b = \
537 (void *)b; \
538 atomic_inc(&__b->buffer->record_disabled); \
539 } else \
540 atomic_inc(&b->record_disabled); \
541 WARN_ON(1); \
542 } \
543 _____ret; \
Steven Rostedt3e89c7bb2008-11-11 15:28:41 -0500544 })
Steven Rostedtf536aaf2008-11-10 23:07:30 -0500545
Steven Rostedt37886f62009-03-17 17:22:06 -0400546/* Up this if you want to test the TIME_EXTENTS and normalization */
547#define DEBUG_SHIFT 0
548
Jiri Olsa6d3f1e12009-10-23 19:36:19 -0400549static inline u64 rb_time_stamp(struct ring_buffer *buffer)
Steven Rostedt88eb0122009-05-11 16:28:23 -0400550{
551 /* shift to debug/test normalization and TIME_EXTENTS */
552 return buffer->clock() << DEBUG_SHIFT;
553}
554
Steven Rostedt37886f62009-03-17 17:22:06 -0400555u64 ring_buffer_time_stamp(struct ring_buffer *buffer, int cpu)
556{
557 u64 time;
558
559 preempt_disable_notrace();
Jiri Olsa6d3f1e12009-10-23 19:36:19 -0400560 time = rb_time_stamp(buffer);
Steven Rostedt37886f62009-03-17 17:22:06 -0400561 preempt_enable_no_resched_notrace();
562
563 return time;
564}
565EXPORT_SYMBOL_GPL(ring_buffer_time_stamp);
566
567void ring_buffer_normalize_time_stamp(struct ring_buffer *buffer,
568 int cpu, u64 *ts)
569{
570 /* Just stupid testing the normalize function and deltas */
571 *ts >>= DEBUG_SHIFT;
572}
573EXPORT_SYMBOL_GPL(ring_buffer_normalize_time_stamp);
574
Steven Rostedt77ae3652009-03-27 11:00:29 -0400575/*
576 * Making the ring buffer lockless makes things tricky.
577 * Although writes only happen on the CPU that they are on,
578 * and they only need to worry about interrupts. Reads can
579 * happen on any CPU.
580 *
581 * The reader page is always off the ring buffer, but when the
582 * reader finishes with a page, it needs to swap its page with
583 * a new one from the buffer. The reader needs to take from
584 * the head (writes go to the tail). But if a writer is in overwrite
585 * mode and wraps, it must push the head page forward.
586 *
587 * Here lies the problem.
588 *
589 * The reader must be careful to replace only the head page, and
590 * not another one. As described at the top of the file in the
591 * ASCII art, the reader sets its old page to point to the next
592 * page after head. It then sets the page after head to point to
593 * the old reader page. But if the writer moves the head page
594 * during this operation, the reader could end up with the tail.
595 *
596 * We use cmpxchg to help prevent this race. We also do something
597 * special with the page before head. We set the LSB to 1.
598 *
599 * When the writer must push the page forward, it will clear the
600 * bit that points to the head page, move the head, and then set
601 * the bit that points to the new head page.
602 *
603 * We also don't want an interrupt coming in and moving the head
604 * page on another writer. Thus we use the second LSB to catch
605 * that too. Thus:
606 *
607 * head->list->prev->next bit 1 bit 0
608 * ------- -------
609 * Normal page 0 0
610 * Points to head page 0 1
611 * New head page 1 0
612 *
613 * Note we can not trust the prev pointer of the head page, because:
614 *
615 * +----+ +-----+ +-----+
616 * | |------>| T |---X--->| N |
617 * | |<------| | | |
618 * +----+ +-----+ +-----+
619 * ^ ^ |
620 * | +-----+ | |
621 * +----------| R |----------+ |
622 * | |<-----------+
623 * +-----+
624 *
625 * Key: ---X--> HEAD flag set in pointer
626 * T Tail page
627 * R Reader page
628 * N Next page
629 *
630 * (see __rb_reserve_next() to see where this happens)
631 *
632 * What the above shows is that the reader just swapped out
633 * the reader page with a page in the buffer, but before it
634 * could make the new header point back to the new page added
635 * it was preempted by a writer. The writer moved forward onto
636 * the new page added by the reader and is about to move forward
637 * again.
638 *
639 * You can see, it is legitimate for the previous pointer of
640 * the head (or any page) not to point back to itself. But only
641 * temporarially.
642 */
643
644#define RB_PAGE_NORMAL 0UL
645#define RB_PAGE_HEAD 1UL
646#define RB_PAGE_UPDATE 2UL
647
648
649#define RB_FLAG_MASK 3UL
650
651/* PAGE_MOVED is not part of the mask */
652#define RB_PAGE_MOVED 4UL
653
654/*
655 * rb_list_head - remove any bit
656 */
657static struct list_head *rb_list_head(struct list_head *list)
658{
659 unsigned long val = (unsigned long)list;
660
661 return (struct list_head *)(val & ~RB_FLAG_MASK);
662}
663
664/*
Jiri Olsa6d3f1e12009-10-23 19:36:19 -0400665 * rb_is_head_page - test if the given page is the head page
Steven Rostedt77ae3652009-03-27 11:00:29 -0400666 *
667 * Because the reader may move the head_page pointer, we can
668 * not trust what the head page is (it may be pointing to
669 * the reader page). But if the next page is a header page,
670 * its flags will be non zero.
671 */
672static int inline
673rb_is_head_page(struct ring_buffer_per_cpu *cpu_buffer,
674 struct buffer_page *page, struct list_head *list)
675{
676 unsigned long val;
677
678 val = (unsigned long)list->next;
679
680 if ((val & ~RB_FLAG_MASK) != (unsigned long)&page->list)
681 return RB_PAGE_MOVED;
682
683 return val & RB_FLAG_MASK;
684}
685
686/*
687 * rb_is_reader_page
688 *
689 * The unique thing about the reader page, is that, if the
690 * writer is ever on it, the previous pointer never points
691 * back to the reader page.
692 */
693static int rb_is_reader_page(struct buffer_page *page)
694{
695 struct list_head *list = page->list.prev;
696
697 return rb_list_head(list->next) != &page->list;
698}
699
700/*
701 * rb_set_list_to_head - set a list_head to be pointing to head.
702 */
703static void rb_set_list_to_head(struct ring_buffer_per_cpu *cpu_buffer,
704 struct list_head *list)
705{
706 unsigned long *ptr;
707
708 ptr = (unsigned long *)&list->next;
709 *ptr |= RB_PAGE_HEAD;
710 *ptr &= ~RB_PAGE_UPDATE;
711}
712
713/*
714 * rb_head_page_activate - sets up head page
715 */
716static void rb_head_page_activate(struct ring_buffer_per_cpu *cpu_buffer)
717{
718 struct buffer_page *head;
719
720 head = cpu_buffer->head_page;
721 if (!head)
722 return;
723
724 /*
725 * Set the previous list pointer to have the HEAD flag.
726 */
727 rb_set_list_to_head(cpu_buffer, head->list.prev);
728}
729
730static void rb_list_head_clear(struct list_head *list)
731{
732 unsigned long *ptr = (unsigned long *)&list->next;
733
734 *ptr &= ~RB_FLAG_MASK;
735}
736
737/*
738 * rb_head_page_dactivate - clears head page ptr (for free list)
739 */
740static void
741rb_head_page_deactivate(struct ring_buffer_per_cpu *cpu_buffer)
742{
743 struct list_head *hd;
744
745 /* Go through the whole list and clear any pointers found. */
746 rb_list_head_clear(cpu_buffer->pages);
747
748 list_for_each(hd, cpu_buffer->pages)
749 rb_list_head_clear(hd);
750}
751
752static int rb_head_page_set(struct ring_buffer_per_cpu *cpu_buffer,
753 struct buffer_page *head,
754 struct buffer_page *prev,
755 int old_flag, int new_flag)
756{
757 struct list_head *list;
758 unsigned long val = (unsigned long)&head->list;
759 unsigned long ret;
760
761 list = &prev->list;
762
763 val &= ~RB_FLAG_MASK;
764
Steven Rostedt08a40812009-09-14 09:31:35 -0400765 ret = cmpxchg((unsigned long *)&list->next,
766 val | old_flag, val | new_flag);
Steven Rostedt77ae3652009-03-27 11:00:29 -0400767
768 /* check if the reader took the page */
769 if ((ret & ~RB_FLAG_MASK) != val)
770 return RB_PAGE_MOVED;
771
772 return ret & RB_FLAG_MASK;
773}
774
775static int rb_head_page_set_update(struct ring_buffer_per_cpu *cpu_buffer,
776 struct buffer_page *head,
777 struct buffer_page *prev,
778 int old_flag)
779{
780 return rb_head_page_set(cpu_buffer, head, prev,
781 old_flag, RB_PAGE_UPDATE);
782}
783
784static int rb_head_page_set_head(struct ring_buffer_per_cpu *cpu_buffer,
785 struct buffer_page *head,
786 struct buffer_page *prev,
787 int old_flag)
788{
789 return rb_head_page_set(cpu_buffer, head, prev,
790 old_flag, RB_PAGE_HEAD);
791}
792
793static int rb_head_page_set_normal(struct ring_buffer_per_cpu *cpu_buffer,
794 struct buffer_page *head,
795 struct buffer_page *prev,
796 int old_flag)
797{
798 return rb_head_page_set(cpu_buffer, head, prev,
799 old_flag, RB_PAGE_NORMAL);
800}
801
802static inline void rb_inc_page(struct ring_buffer_per_cpu *cpu_buffer,
803 struct buffer_page **bpage)
804{
805 struct list_head *p = rb_list_head((*bpage)->list.next);
806
807 *bpage = list_entry(p, struct buffer_page, list);
808}
809
810static struct buffer_page *
811rb_set_head_page(struct ring_buffer_per_cpu *cpu_buffer)
812{
813 struct buffer_page *head;
814 struct buffer_page *page;
815 struct list_head *list;
816 int i;
817
818 if (RB_WARN_ON(cpu_buffer, !cpu_buffer->head_page))
819 return NULL;
820
821 /* sanity check */
822 list = cpu_buffer->pages;
823 if (RB_WARN_ON(cpu_buffer, rb_list_head(list->prev->next) != list))
824 return NULL;
825
826 page = head = cpu_buffer->head_page;
827 /*
828 * It is possible that the writer moves the header behind
829 * where we started, and we miss in one loop.
830 * A second loop should grab the header, but we'll do
831 * three loops just because I'm paranoid.
832 */
833 for (i = 0; i < 3; i++) {
834 do {
835 if (rb_is_head_page(cpu_buffer, page, page->list.prev)) {
836 cpu_buffer->head_page = page;
837 return page;
838 }
839 rb_inc_page(cpu_buffer, &page);
840 } while (page != head);
841 }
842
843 RB_WARN_ON(cpu_buffer, 1);
844
845 return NULL;
846}
847
848static int rb_head_page_replace(struct buffer_page *old,
849 struct buffer_page *new)
850{
851 unsigned long *ptr = (unsigned long *)&old->list.prev->next;
852 unsigned long val;
853 unsigned long ret;
854
855 val = *ptr & ~RB_FLAG_MASK;
856 val |= RB_PAGE_HEAD;
857
Steven Rostedt08a40812009-09-14 09:31:35 -0400858 ret = cmpxchg(ptr, val, (unsigned long)&new->list);
Steven Rostedt77ae3652009-03-27 11:00:29 -0400859
860 return ret == val;
861}
862
863/*
864 * rb_tail_page_update - move the tail page forward
865 *
866 * Returns 1 if moved tail page, 0 if someone else did.
867 */
868static int rb_tail_page_update(struct ring_buffer_per_cpu *cpu_buffer,
869 struct buffer_page *tail_page,
870 struct buffer_page *next_page)
871{
872 struct buffer_page *old_tail;
873 unsigned long old_entries;
874 unsigned long old_write;
875 int ret = 0;
876
877 /*
878 * The tail page now needs to be moved forward.
879 *
880 * We need to reset the tail page, but without messing
881 * with possible erasing of data brought in by interrupts
882 * that have moved the tail page and are currently on it.
883 *
884 * We add a counter to the write field to denote this.
885 */
886 old_write = local_add_return(RB_WRITE_INTCNT, &next_page->write);
887 old_entries = local_add_return(RB_WRITE_INTCNT, &next_page->entries);
888
889 /*
890 * Just make sure we have seen our old_write and synchronize
891 * with any interrupts that come in.
892 */
893 barrier();
894
895 /*
896 * If the tail page is still the same as what we think
897 * it is, then it is up to us to update the tail
898 * pointer.
899 */
900 if (tail_page == cpu_buffer->tail_page) {
901 /* Zero the write counter */
902 unsigned long val = old_write & ~RB_WRITE_MASK;
903 unsigned long eval = old_entries & ~RB_WRITE_MASK;
904
905 /*
906 * This will only succeed if an interrupt did
907 * not come in and change it. In which case, we
908 * do not want to modify it.
Lai Jiangshanda706d82009-07-15 16:27:30 +0800909 *
910 * We add (void) to let the compiler know that we do not care
911 * about the return value of these functions. We use the
912 * cmpxchg to only update if an interrupt did not already
913 * do it for us. If the cmpxchg fails, we don't care.
Steven Rostedt77ae3652009-03-27 11:00:29 -0400914 */
Lai Jiangshanda706d82009-07-15 16:27:30 +0800915 (void)local_cmpxchg(&next_page->write, old_write, val);
916 (void)local_cmpxchg(&next_page->entries, old_entries, eval);
Steven Rostedt77ae3652009-03-27 11:00:29 -0400917
918 /*
919 * No need to worry about races with clearing out the commit.
920 * it only can increment when a commit takes place. But that
921 * only happens in the outer most nested commit.
922 */
923 local_set(&next_page->page->commit, 0);
924
925 old_tail = cmpxchg(&cpu_buffer->tail_page,
926 tail_page, next_page);
927
928 if (old_tail == tail_page)
929 ret = 1;
930 }
931
932 return ret;
933}
934
935static int rb_check_bpage(struct ring_buffer_per_cpu *cpu_buffer,
936 struct buffer_page *bpage)
937{
938 unsigned long val = (unsigned long)bpage;
939
940 if (RB_WARN_ON(cpu_buffer, val & RB_FLAG_MASK))
941 return 1;
942
943 return 0;
944}
945
946/**
947 * rb_check_list - make sure a pointer to a list has the last bits zero
948 */
949static int rb_check_list(struct ring_buffer_per_cpu *cpu_buffer,
950 struct list_head *list)
951{
952 if (RB_WARN_ON(cpu_buffer, rb_list_head(list->prev) != list->prev))
953 return 1;
954 if (RB_WARN_ON(cpu_buffer, rb_list_head(list->next) != list->next))
955 return 1;
956 return 0;
957}
958
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400959/**
960 * check_pages - integrity check of buffer pages
961 * @cpu_buffer: CPU buffer with pages to test
962 *
Wenji Huangc3706f02009-02-10 01:03:18 -0500963 * As a safety measure we check to make sure the data pages have not
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400964 * been corrupted.
965 */
966static int rb_check_pages(struct ring_buffer_per_cpu *cpu_buffer)
967{
Steven Rostedt3adc54f2009-03-30 15:32:01 -0400968 struct list_head *head = cpu_buffer->pages;
Steven Rostedt044fa782008-12-02 23:50:03 -0500969 struct buffer_page *bpage, *tmp;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400970
Steven Rostedt77ae3652009-03-27 11:00:29 -0400971 rb_head_page_deactivate(cpu_buffer);
972
Steven Rostedt3e89c7bb2008-11-11 15:28:41 -0500973 if (RB_WARN_ON(cpu_buffer, head->next->prev != head))
974 return -1;
975 if (RB_WARN_ON(cpu_buffer, head->prev->next != head))
976 return -1;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400977
Steven Rostedt77ae3652009-03-27 11:00:29 -0400978 if (rb_check_list(cpu_buffer, head))
979 return -1;
980
Steven Rostedt044fa782008-12-02 23:50:03 -0500981 list_for_each_entry_safe(bpage, tmp, head, list) {
Steven Rostedt3e89c7bb2008-11-11 15:28:41 -0500982 if (RB_WARN_ON(cpu_buffer,
Steven Rostedt044fa782008-12-02 23:50:03 -0500983 bpage->list.next->prev != &bpage->list))
Steven Rostedt3e89c7bb2008-11-11 15:28:41 -0500984 return -1;
985 if (RB_WARN_ON(cpu_buffer,
Steven Rostedt044fa782008-12-02 23:50:03 -0500986 bpage->list.prev->next != &bpage->list))
Steven Rostedt3e89c7bb2008-11-11 15:28:41 -0500987 return -1;
Steven Rostedt77ae3652009-03-27 11:00:29 -0400988 if (rb_check_list(cpu_buffer, &bpage->list))
989 return -1;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400990 }
991
Steven Rostedt77ae3652009-03-27 11:00:29 -0400992 rb_head_page_activate(cpu_buffer);
993
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400994 return 0;
995}
996
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400997static int rb_allocate_pages(struct ring_buffer_per_cpu *cpu_buffer,
998 unsigned nr_pages)
999{
Steven Rostedt044fa782008-12-02 23:50:03 -05001000 struct buffer_page *bpage, *tmp;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001001 unsigned long addr;
1002 LIST_HEAD(pages);
1003 unsigned i;
1004
Steven Rostedt3adc54f2009-03-30 15:32:01 -04001005 WARN_ON(!nr_pages);
1006
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001007 for (i = 0; i < nr_pages; i++) {
Steven Rostedt044fa782008-12-02 23:50:03 -05001008 bpage = kzalloc_node(ALIGN(sizeof(*bpage), cache_line_size()),
Steven Rostedtaa1e0e3b2008-10-02 19:18:09 -04001009 GFP_KERNEL, cpu_to_node(cpu_buffer->cpu));
Steven Rostedt044fa782008-12-02 23:50:03 -05001010 if (!bpage)
Steven Rostedte4c2ce82008-10-01 11:14:54 -04001011 goto free_pages;
Steven Rostedt77ae3652009-03-27 11:00:29 -04001012
1013 rb_check_bpage(cpu_buffer, bpage);
1014
Steven Rostedt044fa782008-12-02 23:50:03 -05001015 list_add(&bpage->list, &pages);
Steven Rostedte4c2ce82008-10-01 11:14:54 -04001016
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001017 addr = __get_free_page(GFP_KERNEL);
1018 if (!addr)
1019 goto free_pages;
Steven Rostedt044fa782008-12-02 23:50:03 -05001020 bpage->page = (void *)addr;
1021 rb_init_page(bpage->page);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001022 }
1023
Steven Rostedt3adc54f2009-03-30 15:32:01 -04001024 /*
1025 * The ring buffer page list is a circular list that does not
1026 * start and end with a list head. All page list items point to
1027 * other pages.
1028 */
1029 cpu_buffer->pages = pages.next;
1030 list_del(&pages);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001031
1032 rb_check_pages(cpu_buffer);
1033
1034 return 0;
1035
1036 free_pages:
Steven Rostedt044fa782008-12-02 23:50:03 -05001037 list_for_each_entry_safe(bpage, tmp, &pages, list) {
1038 list_del_init(&bpage->list);
1039 free_buffer_page(bpage);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001040 }
1041 return -ENOMEM;
1042}
1043
1044static struct ring_buffer_per_cpu *
1045rb_allocate_cpu_buffer(struct ring_buffer *buffer, int cpu)
1046{
1047 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedt044fa782008-12-02 23:50:03 -05001048 struct buffer_page *bpage;
Steven Rostedtd7690412008-10-01 00:29:53 -04001049 unsigned long addr;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001050 int ret;
1051
1052 cpu_buffer = kzalloc_node(ALIGN(sizeof(*cpu_buffer), cache_line_size()),
1053 GFP_KERNEL, cpu_to_node(cpu));
1054 if (!cpu_buffer)
1055 return NULL;
1056
1057 cpu_buffer->cpu = cpu;
1058 cpu_buffer->buffer = buffer;
Steven Rostedtf83c9d02008-11-11 18:47:44 +01001059 spin_lock_init(&cpu_buffer->reader_lock);
Peter Zijlstra1f8a6a12009-06-08 18:18:39 +02001060 lockdep_set_class(&cpu_buffer->reader_lock, buffer->reader_lock_key);
Thomas Gleixneredc35bd2009-12-03 12:38:57 +01001061 cpu_buffer->lock = (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001062
Steven Rostedt044fa782008-12-02 23:50:03 -05001063 bpage = kzalloc_node(ALIGN(sizeof(*bpage), cache_line_size()),
Steven Rostedte4c2ce82008-10-01 11:14:54 -04001064 GFP_KERNEL, cpu_to_node(cpu));
Steven Rostedt044fa782008-12-02 23:50:03 -05001065 if (!bpage)
Steven Rostedte4c2ce82008-10-01 11:14:54 -04001066 goto fail_free_buffer;
1067
Steven Rostedt77ae3652009-03-27 11:00:29 -04001068 rb_check_bpage(cpu_buffer, bpage);
1069
Steven Rostedt044fa782008-12-02 23:50:03 -05001070 cpu_buffer->reader_page = bpage;
Steven Rostedtd7690412008-10-01 00:29:53 -04001071 addr = __get_free_page(GFP_KERNEL);
1072 if (!addr)
Steven Rostedte4c2ce82008-10-01 11:14:54 -04001073 goto fail_free_reader;
Steven Rostedt044fa782008-12-02 23:50:03 -05001074 bpage->page = (void *)addr;
1075 rb_init_page(bpage->page);
Steven Rostedte4c2ce82008-10-01 11:14:54 -04001076
Steven Rostedtd7690412008-10-01 00:29:53 -04001077 INIT_LIST_HEAD(&cpu_buffer->reader_page->list);
Steven Rostedtd7690412008-10-01 00:29:53 -04001078
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001079 ret = rb_allocate_pages(cpu_buffer, buffer->pages);
1080 if (ret < 0)
Steven Rostedtd7690412008-10-01 00:29:53 -04001081 goto fail_free_reader;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001082
1083 cpu_buffer->head_page
Steven Rostedt3adc54f2009-03-30 15:32:01 -04001084 = list_entry(cpu_buffer->pages, struct buffer_page, list);
Steven Rostedtbf41a152008-10-04 02:00:59 -04001085 cpu_buffer->tail_page = cpu_buffer->commit_page = cpu_buffer->head_page;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001086
Steven Rostedt77ae3652009-03-27 11:00:29 -04001087 rb_head_page_activate(cpu_buffer);
1088
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001089 return cpu_buffer;
1090
Steven Rostedtd7690412008-10-01 00:29:53 -04001091 fail_free_reader:
1092 free_buffer_page(cpu_buffer->reader_page);
1093
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001094 fail_free_buffer:
1095 kfree(cpu_buffer);
1096 return NULL;
1097}
1098
1099static void rb_free_cpu_buffer(struct ring_buffer_per_cpu *cpu_buffer)
1100{
Steven Rostedt3adc54f2009-03-30 15:32:01 -04001101 struct list_head *head = cpu_buffer->pages;
Steven Rostedt044fa782008-12-02 23:50:03 -05001102 struct buffer_page *bpage, *tmp;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001103
Steven Rostedtd7690412008-10-01 00:29:53 -04001104 free_buffer_page(cpu_buffer->reader_page);
1105
Steven Rostedt77ae3652009-03-27 11:00:29 -04001106 rb_head_page_deactivate(cpu_buffer);
1107
Steven Rostedt3adc54f2009-03-30 15:32:01 -04001108 if (head) {
1109 list_for_each_entry_safe(bpage, tmp, head, list) {
1110 list_del_init(&bpage->list);
1111 free_buffer_page(bpage);
1112 }
1113 bpage = list_entry(head, struct buffer_page, list);
Steven Rostedt044fa782008-12-02 23:50:03 -05001114 free_buffer_page(bpage);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001115 }
Steven Rostedt3adc54f2009-03-30 15:32:01 -04001116
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001117 kfree(cpu_buffer);
1118}
1119
Steven Rostedt59222ef2009-03-12 11:46:03 -04001120#ifdef CONFIG_HOTPLUG_CPU
Frederic Weisbecker09c9e842009-03-21 04:33:36 +01001121static int rb_cpu_notify(struct notifier_block *self,
1122 unsigned long action, void *hcpu);
Steven Rostedt554f7862009-03-11 22:00:13 -04001123#endif
1124
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001125/**
1126 * ring_buffer_alloc - allocate a new ring_buffer
Robert Richter68814b52008-11-24 12:24:12 +01001127 * @size: the size in bytes per cpu that is needed.
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001128 * @flags: attributes to set for the ring buffer.
1129 *
1130 * Currently the only flag that is available is the RB_FL_OVERWRITE
1131 * flag. This flag means that the buffer will overwrite old data
1132 * when the buffer wraps. If this flag is not set, the buffer will
1133 * drop data when the tail hits the head.
1134 */
Peter Zijlstra1f8a6a12009-06-08 18:18:39 +02001135struct ring_buffer *__ring_buffer_alloc(unsigned long size, unsigned flags,
1136 struct lock_class_key *key)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001137{
1138 struct ring_buffer *buffer;
1139 int bsize;
1140 int cpu;
1141
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001142 /* keep it in its own cache line */
1143 buffer = kzalloc(ALIGN(sizeof(*buffer), cache_line_size()),
1144 GFP_KERNEL);
1145 if (!buffer)
1146 return NULL;
1147
Rusty Russell9e01c1b2009-01-01 10:12:22 +10301148 if (!alloc_cpumask_var(&buffer->cpumask, GFP_KERNEL))
1149 goto fail_free_buffer;
1150
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001151 buffer->pages = DIV_ROUND_UP(size, BUF_PAGE_SIZE);
1152 buffer->flags = flags;
Steven Rostedt37886f62009-03-17 17:22:06 -04001153 buffer->clock = trace_clock_local;
Peter Zijlstra1f8a6a12009-06-08 18:18:39 +02001154 buffer->reader_lock_key = key;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001155
1156 /* need at least two pages */
Steven Rostedt5f78abe2009-06-17 14:11:10 -04001157 if (buffer->pages < 2)
1158 buffer->pages = 2;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001159
Frederic Weisbecker3bf832c2009-03-19 14:47:33 +01001160 /*
1161 * In case of non-hotplug cpu, if the ring-buffer is allocated
1162 * in early initcall, it will not be notified of secondary cpus.
1163 * In that off case, we need to allocate for all possible cpus.
1164 */
1165#ifdef CONFIG_HOTPLUG_CPU
Steven Rostedt554f7862009-03-11 22:00:13 -04001166 get_online_cpus();
1167 cpumask_copy(buffer->cpumask, cpu_online_mask);
Frederic Weisbecker3bf832c2009-03-19 14:47:33 +01001168#else
1169 cpumask_copy(buffer->cpumask, cpu_possible_mask);
1170#endif
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001171 buffer->cpus = nr_cpu_ids;
1172
1173 bsize = sizeof(void *) * nr_cpu_ids;
1174 buffer->buffers = kzalloc(ALIGN(bsize, cache_line_size()),
1175 GFP_KERNEL);
1176 if (!buffer->buffers)
Rusty Russell9e01c1b2009-01-01 10:12:22 +10301177 goto fail_free_cpumask;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001178
1179 for_each_buffer_cpu(buffer, cpu) {
1180 buffer->buffers[cpu] =
1181 rb_allocate_cpu_buffer(buffer, cpu);
1182 if (!buffer->buffers[cpu])
1183 goto fail_free_buffers;
1184 }
1185
Steven Rostedt59222ef2009-03-12 11:46:03 -04001186#ifdef CONFIG_HOTPLUG_CPU
Steven Rostedt554f7862009-03-11 22:00:13 -04001187 buffer->cpu_notify.notifier_call = rb_cpu_notify;
1188 buffer->cpu_notify.priority = 0;
1189 register_cpu_notifier(&buffer->cpu_notify);
1190#endif
1191
1192 put_online_cpus();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001193 mutex_init(&buffer->mutex);
1194
1195 return buffer;
1196
1197 fail_free_buffers:
1198 for_each_buffer_cpu(buffer, cpu) {
1199 if (buffer->buffers[cpu])
1200 rb_free_cpu_buffer(buffer->buffers[cpu]);
1201 }
1202 kfree(buffer->buffers);
1203
Rusty Russell9e01c1b2009-01-01 10:12:22 +10301204 fail_free_cpumask:
1205 free_cpumask_var(buffer->cpumask);
Steven Rostedt554f7862009-03-11 22:00:13 -04001206 put_online_cpus();
Rusty Russell9e01c1b2009-01-01 10:12:22 +10301207
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001208 fail_free_buffer:
1209 kfree(buffer);
1210 return NULL;
1211}
Peter Zijlstra1f8a6a12009-06-08 18:18:39 +02001212EXPORT_SYMBOL_GPL(__ring_buffer_alloc);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001213
1214/**
1215 * ring_buffer_free - free a ring buffer.
1216 * @buffer: the buffer to free.
1217 */
1218void
1219ring_buffer_free(struct ring_buffer *buffer)
1220{
1221 int cpu;
1222
Steven Rostedt554f7862009-03-11 22:00:13 -04001223 get_online_cpus();
1224
Steven Rostedt59222ef2009-03-12 11:46:03 -04001225#ifdef CONFIG_HOTPLUG_CPU
Steven Rostedt554f7862009-03-11 22:00:13 -04001226 unregister_cpu_notifier(&buffer->cpu_notify);
1227#endif
1228
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001229 for_each_buffer_cpu(buffer, cpu)
1230 rb_free_cpu_buffer(buffer->buffers[cpu]);
1231
Steven Rostedt554f7862009-03-11 22:00:13 -04001232 put_online_cpus();
1233
Eric Dumazetbd3f0222009-08-07 12:49:29 +02001234 kfree(buffer->buffers);
Rusty Russell9e01c1b2009-01-01 10:12:22 +10301235 free_cpumask_var(buffer->cpumask);
1236
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001237 kfree(buffer);
1238}
Robert Richterc4f50182008-12-11 16:49:22 +01001239EXPORT_SYMBOL_GPL(ring_buffer_free);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001240
Steven Rostedt37886f62009-03-17 17:22:06 -04001241void ring_buffer_set_clock(struct ring_buffer *buffer,
1242 u64 (*clock)(void))
1243{
1244 buffer->clock = clock;
1245}
1246
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001247static void rb_reset_cpu(struct ring_buffer_per_cpu *cpu_buffer);
1248
1249static void
1250rb_remove_pages(struct ring_buffer_per_cpu *cpu_buffer, unsigned nr_pages)
1251{
Steven Rostedt044fa782008-12-02 23:50:03 -05001252 struct buffer_page *bpage;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001253 struct list_head *p;
1254 unsigned i;
1255
Lai Jiangshanf7112942009-11-03 19:42:45 +08001256 spin_lock_irq(&cpu_buffer->reader_lock);
Steven Rostedt77ae3652009-03-27 11:00:29 -04001257 rb_head_page_deactivate(cpu_buffer);
1258
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001259 for (i = 0; i < nr_pages; i++) {
Steven Rostedt3adc54f2009-03-30 15:32:01 -04001260 if (RB_WARN_ON(cpu_buffer, list_empty(cpu_buffer->pages)))
Julia Lawall292f60c2010-03-29 17:37:02 +02001261 goto out;
Steven Rostedt3adc54f2009-03-30 15:32:01 -04001262 p = cpu_buffer->pages->next;
Steven Rostedt044fa782008-12-02 23:50:03 -05001263 bpage = list_entry(p, struct buffer_page, list);
1264 list_del_init(&bpage->list);
1265 free_buffer_page(bpage);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001266 }
Steven Rostedt3adc54f2009-03-30 15:32:01 -04001267 if (RB_WARN_ON(cpu_buffer, list_empty(cpu_buffer->pages)))
Julia Lawall292f60c2010-03-29 17:37:02 +02001268 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001269
1270 rb_reset_cpu(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001271 rb_check_pages(cpu_buffer);
1272
Julia Lawall292f60c2010-03-29 17:37:02 +02001273out:
Steven Rostedtdd7f5942009-12-10 23:20:52 -05001274 spin_unlock_irq(&cpu_buffer->reader_lock);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001275}
1276
1277static void
1278rb_insert_pages(struct ring_buffer_per_cpu *cpu_buffer,
1279 struct list_head *pages, unsigned nr_pages)
1280{
Steven Rostedt044fa782008-12-02 23:50:03 -05001281 struct buffer_page *bpage;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001282 struct list_head *p;
1283 unsigned i;
1284
Steven Rostedt77ae3652009-03-27 11:00:29 -04001285 spin_lock_irq(&cpu_buffer->reader_lock);
1286 rb_head_page_deactivate(cpu_buffer);
1287
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001288 for (i = 0; i < nr_pages; i++) {
Steven Rostedt3e89c7bb2008-11-11 15:28:41 -05001289 if (RB_WARN_ON(cpu_buffer, list_empty(pages)))
Julia Lawall292f60c2010-03-29 17:37:02 +02001290 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001291 p = pages->next;
Steven Rostedt044fa782008-12-02 23:50:03 -05001292 bpage = list_entry(p, struct buffer_page, list);
1293 list_del_init(&bpage->list);
Steven Rostedt3adc54f2009-03-30 15:32:01 -04001294 list_add_tail(&bpage->list, cpu_buffer->pages);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001295 }
1296 rb_reset_cpu(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001297 rb_check_pages(cpu_buffer);
1298
Julia Lawall292f60c2010-03-29 17:37:02 +02001299out:
Steven Rostedtdd7f5942009-12-10 23:20:52 -05001300 spin_unlock_irq(&cpu_buffer->reader_lock);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001301}
1302
1303/**
1304 * ring_buffer_resize - resize the ring buffer
1305 * @buffer: the buffer to resize.
1306 * @size: the new size.
1307 *
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001308 * Minimum size is 2 * BUF_PAGE_SIZE.
1309 *
1310 * Returns -1 on failure.
1311 */
1312int ring_buffer_resize(struct ring_buffer *buffer, unsigned long size)
1313{
1314 struct ring_buffer_per_cpu *cpu_buffer;
1315 unsigned nr_pages, rm_pages, new_pages;
Steven Rostedt044fa782008-12-02 23:50:03 -05001316 struct buffer_page *bpage, *tmp;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001317 unsigned long buffer_size;
1318 unsigned long addr;
1319 LIST_HEAD(pages);
1320 int i, cpu;
1321
Ingo Molnaree51a1d2008-11-13 14:58:31 +01001322 /*
1323 * Always succeed at resizing a non-existent buffer:
1324 */
1325 if (!buffer)
1326 return size;
1327
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001328 size = DIV_ROUND_UP(size, BUF_PAGE_SIZE);
1329 size *= BUF_PAGE_SIZE;
1330 buffer_size = buffer->pages * BUF_PAGE_SIZE;
1331
1332 /* we need a minimum of two pages */
1333 if (size < BUF_PAGE_SIZE * 2)
1334 size = BUF_PAGE_SIZE * 2;
1335
1336 if (size == buffer_size)
1337 return size;
1338
Steven Rostedt18421012009-12-10 22:54:27 -05001339 atomic_inc(&buffer->record_disabled);
1340
1341 /* Make sure all writers are done with this buffer. */
1342 synchronize_sched();
1343
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001344 mutex_lock(&buffer->mutex);
Steven Rostedt554f7862009-03-11 22:00:13 -04001345 get_online_cpus();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001346
1347 nr_pages = DIV_ROUND_UP(size, BUF_PAGE_SIZE);
1348
1349 if (size < buffer_size) {
1350
1351 /* easy case, just free pages */
Steven Rostedt554f7862009-03-11 22:00:13 -04001352 if (RB_WARN_ON(buffer, nr_pages >= buffer->pages))
1353 goto out_fail;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001354
1355 rm_pages = buffer->pages - nr_pages;
1356
1357 for_each_buffer_cpu(buffer, cpu) {
1358 cpu_buffer = buffer->buffers[cpu];
1359 rb_remove_pages(cpu_buffer, rm_pages);
1360 }
1361 goto out;
1362 }
1363
1364 /*
1365 * This is a bit more difficult. We only want to add pages
1366 * when we can allocate enough for all CPUs. We do this
1367 * by allocating all the pages and storing them on a local
1368 * link list. If we succeed in our allocation, then we
1369 * add these pages to the cpu_buffers. Otherwise we just free
1370 * them all and return -ENOMEM;
1371 */
Steven Rostedt554f7862009-03-11 22:00:13 -04001372 if (RB_WARN_ON(buffer, nr_pages <= buffer->pages))
1373 goto out_fail;
Steven Rostedtf536aaf2008-11-10 23:07:30 -05001374
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001375 new_pages = nr_pages - buffer->pages;
1376
1377 for_each_buffer_cpu(buffer, cpu) {
1378 for (i = 0; i < new_pages; i++) {
Steven Rostedt044fa782008-12-02 23:50:03 -05001379 bpage = kzalloc_node(ALIGN(sizeof(*bpage),
Steven Rostedte4c2ce82008-10-01 11:14:54 -04001380 cache_line_size()),
1381 GFP_KERNEL, cpu_to_node(cpu));
Steven Rostedt044fa782008-12-02 23:50:03 -05001382 if (!bpage)
Steven Rostedte4c2ce82008-10-01 11:14:54 -04001383 goto free_pages;
Steven Rostedt044fa782008-12-02 23:50:03 -05001384 list_add(&bpage->list, &pages);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001385 addr = __get_free_page(GFP_KERNEL);
1386 if (!addr)
1387 goto free_pages;
Steven Rostedt044fa782008-12-02 23:50:03 -05001388 bpage->page = (void *)addr;
1389 rb_init_page(bpage->page);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001390 }
1391 }
1392
1393 for_each_buffer_cpu(buffer, cpu) {
1394 cpu_buffer = buffer->buffers[cpu];
1395 rb_insert_pages(cpu_buffer, &pages, new_pages);
1396 }
1397
Steven Rostedt554f7862009-03-11 22:00:13 -04001398 if (RB_WARN_ON(buffer, !list_empty(&pages)))
1399 goto out_fail;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001400
1401 out:
1402 buffer->pages = nr_pages;
Steven Rostedt554f7862009-03-11 22:00:13 -04001403 put_online_cpus();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001404 mutex_unlock(&buffer->mutex);
1405
Steven Rostedt18421012009-12-10 22:54:27 -05001406 atomic_dec(&buffer->record_disabled);
1407
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001408 return size;
1409
1410 free_pages:
Steven Rostedt044fa782008-12-02 23:50:03 -05001411 list_for_each_entry_safe(bpage, tmp, &pages, list) {
1412 list_del_init(&bpage->list);
1413 free_buffer_page(bpage);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001414 }
Steven Rostedt554f7862009-03-11 22:00:13 -04001415 put_online_cpus();
Vegard Nossum641d2f62008-11-18 19:22:13 +01001416 mutex_unlock(&buffer->mutex);
Steven Rostedt18421012009-12-10 22:54:27 -05001417 atomic_dec(&buffer->record_disabled);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001418 return -ENOMEM;
Steven Rostedt554f7862009-03-11 22:00:13 -04001419
1420 /*
1421 * Something went totally wrong, and we are too paranoid
1422 * to even clean up the mess.
1423 */
1424 out_fail:
1425 put_online_cpus();
1426 mutex_unlock(&buffer->mutex);
Steven Rostedt18421012009-12-10 22:54:27 -05001427 atomic_dec(&buffer->record_disabled);
Steven Rostedt554f7862009-03-11 22:00:13 -04001428 return -1;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001429}
Robert Richterc4f50182008-12-11 16:49:22 +01001430EXPORT_SYMBOL_GPL(ring_buffer_resize);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001431
Steven Rostedt8789a9e2008-12-02 15:34:07 -05001432static inline void *
Steven Rostedt044fa782008-12-02 23:50:03 -05001433__rb_data_page_index(struct buffer_data_page *bpage, unsigned index)
Steven Rostedt8789a9e2008-12-02 15:34:07 -05001434{
Steven Rostedt044fa782008-12-02 23:50:03 -05001435 return bpage->data + index;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05001436}
1437
Steven Rostedt044fa782008-12-02 23:50:03 -05001438static inline void *__rb_page_index(struct buffer_page *bpage, unsigned index)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001439{
Steven Rostedt044fa782008-12-02 23:50:03 -05001440 return bpage->page->data + index;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001441}
1442
1443static inline struct ring_buffer_event *
Steven Rostedtd7690412008-10-01 00:29:53 -04001444rb_reader_event(struct ring_buffer_per_cpu *cpu_buffer)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001445{
Steven Rostedt6f807ac2008-10-04 02:00:58 -04001446 return __rb_page_index(cpu_buffer->reader_page,
1447 cpu_buffer->reader_page->read);
1448}
1449
1450static inline struct ring_buffer_event *
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001451rb_iter_head_event(struct ring_buffer_iter *iter)
1452{
Steven Rostedt6f807ac2008-10-04 02:00:58 -04001453 return __rb_page_index(iter->head_page, iter->head);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001454}
1455
Steven Rostedt77ae3652009-03-27 11:00:29 -04001456static inline unsigned long rb_page_write(struct buffer_page *bpage)
Steven Rostedtbf41a152008-10-04 02:00:59 -04001457{
Steven Rostedt77ae3652009-03-27 11:00:29 -04001458 return local_read(&bpage->write) & RB_WRITE_MASK;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001459}
1460
1461static inline unsigned rb_page_commit(struct buffer_page *bpage)
1462{
Steven Rostedtabc9b562008-12-02 15:34:06 -05001463 return local_read(&bpage->page->commit);
Steven Rostedtbf41a152008-10-04 02:00:59 -04001464}
1465
Steven Rostedt77ae3652009-03-27 11:00:29 -04001466static inline unsigned long rb_page_entries(struct buffer_page *bpage)
1467{
1468 return local_read(&bpage->entries) & RB_WRITE_MASK;
1469}
1470
Steven Rostedtbf41a152008-10-04 02:00:59 -04001471/* Size is determined by what has been commited */
1472static inline unsigned rb_page_size(struct buffer_page *bpage)
1473{
1474 return rb_page_commit(bpage);
1475}
1476
1477static inline unsigned
1478rb_commit_index(struct ring_buffer_per_cpu *cpu_buffer)
1479{
1480 return rb_page_commit(cpu_buffer->commit_page);
1481}
1482
Steven Rostedtbf41a152008-10-04 02:00:59 -04001483static inline unsigned
1484rb_event_index(struct ring_buffer_event *event)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001485{
Steven Rostedtbf41a152008-10-04 02:00:59 -04001486 unsigned long addr = (unsigned long)event;
1487
Steven Rostedt22f470f2009-06-11 09:29:58 -04001488 return (addr & ~PAGE_MASK) - BUF_PAGE_HDR_SIZE;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001489}
1490
Steven Rostedt0f0c85f2009-05-11 16:08:00 -04001491static inline int
Steven Rostedtfa743952009-06-16 12:37:57 -04001492rb_event_is_commit(struct ring_buffer_per_cpu *cpu_buffer,
1493 struct ring_buffer_event *event)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001494{
Steven Rostedtbf41a152008-10-04 02:00:59 -04001495 unsigned long addr = (unsigned long)event;
1496 unsigned long index;
1497
1498 index = rb_event_index(event);
1499 addr &= PAGE_MASK;
1500
1501 return cpu_buffer->commit_page->page == (void *)addr &&
1502 rb_commit_index(cpu_buffer) == index;
1503}
1504
Andrew Morton34a148b2009-01-09 12:27:09 -08001505static void
Steven Rostedtbf41a152008-10-04 02:00:59 -04001506rb_set_commit_to_write(struct ring_buffer_per_cpu *cpu_buffer)
1507{
Steven Rostedt77ae3652009-03-27 11:00:29 -04001508 unsigned long max_count;
1509
Steven Rostedtbf41a152008-10-04 02:00:59 -04001510 /*
1511 * We only race with interrupts and NMIs on this CPU.
1512 * If we own the commit event, then we can commit
1513 * all others that interrupted us, since the interruptions
1514 * are in stack format (they finish before they come
1515 * back to us). This allows us to do a simple loop to
1516 * assign the commit to the tail.
1517 */
Steven Rostedta8ccf1d2008-12-23 11:32:24 -05001518 again:
Steven Rostedt77ae3652009-03-27 11:00:29 -04001519 max_count = cpu_buffer->buffer->pages * 100;
1520
Steven Rostedtbf41a152008-10-04 02:00:59 -04001521 while (cpu_buffer->commit_page != cpu_buffer->tail_page) {
Steven Rostedt77ae3652009-03-27 11:00:29 -04001522 if (RB_WARN_ON(cpu_buffer, !(--max_count)))
1523 return;
1524 if (RB_WARN_ON(cpu_buffer,
1525 rb_is_reader_page(cpu_buffer->tail_page)))
1526 return;
1527 local_set(&cpu_buffer->commit_page->page->commit,
1528 rb_page_write(cpu_buffer->commit_page));
Steven Rostedtbf41a152008-10-04 02:00:59 -04001529 rb_inc_page(cpu_buffer, &cpu_buffer->commit_page);
Steven Rostedtabc9b562008-12-02 15:34:06 -05001530 cpu_buffer->write_stamp =
1531 cpu_buffer->commit_page->page->time_stamp;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001532 /* add barrier to keep gcc from optimizing too much */
1533 barrier();
1534 }
1535 while (rb_commit_index(cpu_buffer) !=
1536 rb_page_write(cpu_buffer->commit_page)) {
Steven Rostedt77ae3652009-03-27 11:00:29 -04001537
1538 local_set(&cpu_buffer->commit_page->page->commit,
1539 rb_page_write(cpu_buffer->commit_page));
1540 RB_WARN_ON(cpu_buffer,
1541 local_read(&cpu_buffer->commit_page->page->commit) &
1542 ~RB_WRITE_MASK);
Steven Rostedtbf41a152008-10-04 02:00:59 -04001543 barrier();
1544 }
Steven Rostedta8ccf1d2008-12-23 11:32:24 -05001545
1546 /* again, keep gcc from optimizing */
1547 barrier();
1548
1549 /*
1550 * If an interrupt came in just after the first while loop
1551 * and pushed the tail page forward, we will be left with
1552 * a dangling commit that will never go forward.
1553 */
1554 if (unlikely(cpu_buffer->commit_page != cpu_buffer->tail_page))
1555 goto again;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001556}
1557
Steven Rostedtd7690412008-10-01 00:29:53 -04001558static void rb_reset_reader_page(struct ring_buffer_per_cpu *cpu_buffer)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001559{
Steven Rostedtabc9b562008-12-02 15:34:06 -05001560 cpu_buffer->read_stamp = cpu_buffer->reader_page->page->time_stamp;
Steven Rostedt6f807ac2008-10-04 02:00:58 -04001561 cpu_buffer->reader_page->read = 0;
Steven Rostedtd7690412008-10-01 00:29:53 -04001562}
1563
Andrew Morton34a148b2009-01-09 12:27:09 -08001564static void rb_inc_iter(struct ring_buffer_iter *iter)
Steven Rostedtd7690412008-10-01 00:29:53 -04001565{
1566 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
1567
1568 /*
1569 * The iterator could be on the reader page (it starts there).
1570 * But the head could have moved, since the reader was
1571 * found. Check for this case and assign the iterator
1572 * to the head page instead of next.
1573 */
1574 if (iter->head_page == cpu_buffer->reader_page)
Steven Rostedt77ae3652009-03-27 11:00:29 -04001575 iter->head_page = rb_set_head_page(cpu_buffer);
Steven Rostedtd7690412008-10-01 00:29:53 -04001576 else
1577 rb_inc_page(cpu_buffer, &iter->head_page);
1578
Steven Rostedtabc9b562008-12-02 15:34:06 -05001579 iter->read_stamp = iter->head_page->page->time_stamp;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001580 iter->head = 0;
1581}
1582
Steven Rostedt69d1b832010-10-07 18:18:05 -04001583/* Slow path, do not inline */
1584static noinline struct ring_buffer_event *
1585rb_add_time_stamp(struct ring_buffer_event *event, u64 delta)
1586{
1587 event->type_len = RINGBUF_TYPE_TIME_EXTEND;
1588
1589 /* Not the first event on the page? */
1590 if (rb_event_index(event)) {
1591 event->time_delta = delta & TS_MASK;
1592 event->array[0] = delta >> TS_SHIFT;
1593 } else {
1594 /* nope, just zero it */
1595 event->time_delta = 0;
1596 event->array[0] = 0;
1597 }
1598
1599 return skip_time_extend(event);
1600}
1601
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001602/**
1603 * ring_buffer_update_event - update event type and data
1604 * @event: the even to update
1605 * @type: the type of event
1606 * @length: the size of the event field in the ring buffer
1607 *
1608 * Update the type and data fields of the event. The length
1609 * is the actual size that is written to the ring buffer,
1610 * and with this, we can determine what to place into the
1611 * data field.
1612 */
Andrew Morton34a148b2009-01-09 12:27:09 -08001613static void
Steven Rostedt69d1b832010-10-07 18:18:05 -04001614rb_update_event(struct ring_buffer_per_cpu *cpu_buffer,
1615 struct ring_buffer_event *event, unsigned length,
1616 int add_timestamp, u64 delta)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001617{
Steven Rostedt69d1b832010-10-07 18:18:05 -04001618 /* Only a commit updates the timestamp */
1619 if (unlikely(!rb_event_is_commit(cpu_buffer, event)))
1620 delta = 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001621
Steven Rostedt69d1b832010-10-07 18:18:05 -04001622 /*
1623 * If we need to add a timestamp, then we
1624 * add it to the start of the resevered space.
1625 */
1626 if (unlikely(add_timestamp)) {
1627 event = rb_add_time_stamp(event, delta);
1628 length -= RB_LEN_TIME_EXTEND;
1629 delta = 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001630 }
Steven Rostedt69d1b832010-10-07 18:18:05 -04001631
1632 event->time_delta = delta;
1633 length -= RB_EVNT_HDR_SIZE;
1634 if (length > RB_MAX_SMALL_DATA || RB_FORCE_8BYTE_ALIGNMENT) {
1635 event->type_len = 0;
1636 event->array[0] = length;
1637 } else
1638 event->type_len = DIV_ROUND_UP(length, RB_ALIGNMENT);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001639}
1640
Steven Rostedt77ae3652009-03-27 11:00:29 -04001641/*
1642 * rb_handle_head_page - writer hit the head page
1643 *
1644 * Returns: +1 to retry page
1645 * 0 to continue
1646 * -1 on error
1647 */
1648static int
1649rb_handle_head_page(struct ring_buffer_per_cpu *cpu_buffer,
1650 struct buffer_page *tail_page,
1651 struct buffer_page *next_page)
1652{
1653 struct buffer_page *new_head;
1654 int entries;
1655 int type;
1656 int ret;
1657
1658 entries = rb_page_entries(next_page);
1659
1660 /*
1661 * The hard part is here. We need to move the head
1662 * forward, and protect against both readers on
1663 * other CPUs and writers coming in via interrupts.
1664 */
1665 type = rb_head_page_set_update(cpu_buffer, next_page, tail_page,
1666 RB_PAGE_HEAD);
1667
1668 /*
1669 * type can be one of four:
1670 * NORMAL - an interrupt already moved it for us
1671 * HEAD - we are the first to get here.
1672 * UPDATE - we are the interrupt interrupting
1673 * a current move.
1674 * MOVED - a reader on another CPU moved the next
1675 * pointer to its reader page. Give up
1676 * and try again.
1677 */
1678
1679 switch (type) {
1680 case RB_PAGE_HEAD:
1681 /*
1682 * We changed the head to UPDATE, thus
1683 * it is our responsibility to update
1684 * the counters.
1685 */
1686 local_add(entries, &cpu_buffer->overrun);
1687
1688 /*
1689 * The entries will be zeroed out when we move the
1690 * tail page.
1691 */
1692
1693 /* still more to do */
1694 break;
1695
1696 case RB_PAGE_UPDATE:
1697 /*
1698 * This is an interrupt that interrupt the
1699 * previous update. Still more to do.
1700 */
1701 break;
1702 case RB_PAGE_NORMAL:
1703 /*
1704 * An interrupt came in before the update
1705 * and processed this for us.
1706 * Nothing left to do.
1707 */
1708 return 1;
1709 case RB_PAGE_MOVED:
1710 /*
1711 * The reader is on another CPU and just did
1712 * a swap with our next_page.
1713 * Try again.
1714 */
1715 return 1;
1716 default:
1717 RB_WARN_ON(cpu_buffer, 1); /* WTF??? */
1718 return -1;
1719 }
1720
1721 /*
1722 * Now that we are here, the old head pointer is
1723 * set to UPDATE. This will keep the reader from
1724 * swapping the head page with the reader page.
1725 * The reader (on another CPU) will spin till
1726 * we are finished.
1727 *
1728 * We just need to protect against interrupts
1729 * doing the job. We will set the next pointer
1730 * to HEAD. After that, we set the old pointer
1731 * to NORMAL, but only if it was HEAD before.
1732 * otherwise we are an interrupt, and only
1733 * want the outer most commit to reset it.
1734 */
1735 new_head = next_page;
1736 rb_inc_page(cpu_buffer, &new_head);
1737
1738 ret = rb_head_page_set_head(cpu_buffer, new_head, next_page,
1739 RB_PAGE_NORMAL);
1740
1741 /*
1742 * Valid returns are:
1743 * HEAD - an interrupt came in and already set it.
1744 * NORMAL - One of two things:
1745 * 1) We really set it.
1746 * 2) A bunch of interrupts came in and moved
1747 * the page forward again.
1748 */
1749 switch (ret) {
1750 case RB_PAGE_HEAD:
1751 case RB_PAGE_NORMAL:
1752 /* OK */
1753 break;
1754 default:
1755 RB_WARN_ON(cpu_buffer, 1);
1756 return -1;
1757 }
1758
1759 /*
1760 * It is possible that an interrupt came in,
1761 * set the head up, then more interrupts came in
1762 * and moved it again. When we get back here,
1763 * the page would have been set to NORMAL but we
1764 * just set it back to HEAD.
1765 *
1766 * How do you detect this? Well, if that happened
1767 * the tail page would have moved.
1768 */
1769 if (ret == RB_PAGE_NORMAL) {
1770 /*
1771 * If the tail had moved passed next, then we need
1772 * to reset the pointer.
1773 */
1774 if (cpu_buffer->tail_page != tail_page &&
1775 cpu_buffer->tail_page != next_page)
1776 rb_head_page_set_normal(cpu_buffer, new_head,
1777 next_page,
1778 RB_PAGE_HEAD);
1779 }
1780
1781 /*
1782 * If this was the outer most commit (the one that
1783 * changed the original pointer from HEAD to UPDATE),
1784 * then it is up to us to reset it to NORMAL.
1785 */
1786 if (type == RB_PAGE_HEAD) {
1787 ret = rb_head_page_set_normal(cpu_buffer, next_page,
1788 tail_page,
1789 RB_PAGE_UPDATE);
1790 if (RB_WARN_ON(cpu_buffer,
1791 ret != RB_PAGE_UPDATE))
1792 return -1;
1793 }
1794
1795 return 0;
1796}
1797
Andrew Morton34a148b2009-01-09 12:27:09 -08001798static unsigned rb_calculate_event_length(unsigned length)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001799{
1800 struct ring_buffer_event event; /* Used only for sizeof array */
1801
1802 /* zero length can cause confusions */
1803 if (!length)
1804 length = 1;
1805
Steven Rostedt22710482010-03-18 17:54:19 -04001806 if (length > RB_MAX_SMALL_DATA || RB_FORCE_8BYTE_ALIGNMENT)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001807 length += sizeof(event.array[0]);
1808
1809 length += RB_EVNT_HDR_SIZE;
Steven Rostedt22710482010-03-18 17:54:19 -04001810 length = ALIGN(length, RB_ARCH_ALIGNMENT);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001811
1812 return length;
1813}
1814
Steven Rostedtc7b09302009-06-11 11:12:00 -04001815static inline void
1816rb_reset_tail(struct ring_buffer_per_cpu *cpu_buffer,
1817 struct buffer_page *tail_page,
1818 unsigned long tail, unsigned long length)
1819{
1820 struct ring_buffer_event *event;
1821
1822 /*
1823 * Only the event that crossed the page boundary
1824 * must fill the old tail_page with padding.
1825 */
1826 if (tail >= BUF_PAGE_SIZE) {
Steven Rostedtb3230c82010-05-21 11:55:21 -04001827 /*
1828 * If the page was filled, then we still need
1829 * to update the real_end. Reset it to zero
1830 * and the reader will ignore it.
1831 */
1832 if (tail == BUF_PAGE_SIZE)
1833 tail_page->real_end = 0;
1834
Steven Rostedtc7b09302009-06-11 11:12:00 -04001835 local_sub(length, &tail_page->write);
1836 return;
1837 }
1838
1839 event = __rb_page_index(tail_page, tail);
Linus Torvaldsb0b70652009-06-20 10:56:46 -07001840 kmemcheck_annotate_bitfield(event, bitfield);
Steven Rostedtc7b09302009-06-11 11:12:00 -04001841
1842 /*
Steven Rostedtff0ff842010-03-31 22:11:42 -04001843 * Save the original length to the meta data.
1844 * This will be used by the reader to add lost event
1845 * counter.
1846 */
1847 tail_page->real_end = tail;
1848
1849 /*
Steven Rostedtc7b09302009-06-11 11:12:00 -04001850 * If this event is bigger than the minimum size, then
1851 * we need to be careful that we don't subtract the
1852 * write counter enough to allow another writer to slip
1853 * in on this page.
1854 * We put in a discarded commit instead, to make sure
1855 * that this space is not used again.
1856 *
1857 * If we are less than the minimum size, we don't need to
1858 * worry about it.
1859 */
1860 if (tail > (BUF_PAGE_SIZE - RB_EVNT_MIN_SIZE)) {
1861 /* No room for any events */
1862
1863 /* Mark the rest of the page with padding */
1864 rb_event_set_padding(event);
1865
1866 /* Set the write back to the previous setting */
1867 local_sub(length, &tail_page->write);
1868 return;
1869 }
1870
1871 /* Put in a discarded event */
1872 event->array[0] = (BUF_PAGE_SIZE - tail) - RB_EVNT_HDR_SIZE;
1873 event->type_len = RINGBUF_TYPE_PADDING;
1874 /* time delta must be non zero */
1875 event->time_delta = 1;
Steven Rostedtc7b09302009-06-11 11:12:00 -04001876
1877 /* Set write to end of buffer */
1878 length = (tail + length) - BUF_PAGE_SIZE;
1879 local_sub(length, &tail_page->write);
1880}
Steven Rostedt6634ff22009-05-06 15:30:07 -04001881
Steven Rostedt747e94a2010-10-08 13:51:48 -04001882/*
1883 * This is the slow path, force gcc not to inline it.
1884 */
1885static noinline struct ring_buffer_event *
Steven Rostedt6634ff22009-05-06 15:30:07 -04001886rb_move_tail(struct ring_buffer_per_cpu *cpu_buffer,
1887 unsigned long length, unsigned long tail,
Steven Rostedte8bc43e2010-10-20 10:58:02 -04001888 struct buffer_page *tail_page, u64 ts)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001889{
Steven Rostedt5a50e332009-11-17 08:43:01 -05001890 struct buffer_page *commit_page = cpu_buffer->commit_page;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001891 struct ring_buffer *buffer = cpu_buffer->buffer;
Steven Rostedt77ae3652009-03-27 11:00:29 -04001892 struct buffer_page *next_page;
1893 int ret;
Steven Rostedtaa20ae82009-05-05 21:16:11 -04001894
1895 next_page = tail_page;
1896
Steven Rostedtaa20ae82009-05-05 21:16:11 -04001897 rb_inc_page(cpu_buffer, &next_page);
1898
Steven Rostedtaa20ae82009-05-05 21:16:11 -04001899 /*
1900 * If for some reason, we had an interrupt storm that made
1901 * it all the way around the buffer, bail, and warn
1902 * about it.
1903 */
1904 if (unlikely(next_page == commit_page)) {
Steven Rostedt77ae3652009-03-27 11:00:29 -04001905 local_inc(&cpu_buffer->commit_overrun);
Steven Rostedtaa20ae82009-05-05 21:16:11 -04001906 goto out_reset;
1907 }
1908
Steven Rostedt77ae3652009-03-27 11:00:29 -04001909 /*
1910 * This is where the fun begins!
1911 *
1912 * We are fighting against races between a reader that
1913 * could be on another CPU trying to swap its reader
1914 * page with the buffer head.
1915 *
1916 * We are also fighting against interrupts coming in and
1917 * moving the head or tail on us as well.
1918 *
1919 * If the next page is the head page then we have filled
1920 * the buffer, unless the commit page is still on the
1921 * reader page.
1922 */
1923 if (rb_is_head_page(cpu_buffer, next_page, &tail_page->list)) {
Steven Rostedtaa20ae82009-05-05 21:16:11 -04001924
Steven Rostedt77ae3652009-03-27 11:00:29 -04001925 /*
1926 * If the commit is not on the reader page, then
1927 * move the header page.
1928 */
1929 if (!rb_is_reader_page(cpu_buffer->commit_page)) {
1930 /*
1931 * If we are not in overwrite mode,
1932 * this is easy, just stop here.
1933 */
1934 if (!(buffer->flags & RB_FL_OVERWRITE))
1935 goto out_reset;
Steven Rostedtaa20ae82009-05-05 21:16:11 -04001936
Steven Rostedt77ae3652009-03-27 11:00:29 -04001937 ret = rb_handle_head_page(cpu_buffer,
1938 tail_page,
1939 next_page);
1940 if (ret < 0)
1941 goto out_reset;
1942 if (ret)
1943 goto out_again;
1944 } else {
1945 /*
1946 * We need to be careful here too. The
1947 * commit page could still be on the reader
1948 * page. We could have a small buffer, and
1949 * have filled up the buffer with events
1950 * from interrupts and such, and wrapped.
1951 *
1952 * Note, if the tail page is also the on the
1953 * reader_page, we let it move out.
1954 */
1955 if (unlikely((cpu_buffer->commit_page !=
1956 cpu_buffer->tail_page) &&
1957 (cpu_buffer->commit_page ==
1958 cpu_buffer->reader_page))) {
1959 local_inc(&cpu_buffer->commit_overrun);
1960 goto out_reset;
1961 }
Steven Rostedtaa20ae82009-05-05 21:16:11 -04001962 }
1963 }
1964
Steven Rostedt77ae3652009-03-27 11:00:29 -04001965 ret = rb_tail_page_update(cpu_buffer, tail_page, next_page);
1966 if (ret) {
1967 /*
1968 * Nested commits always have zero deltas, so
1969 * just reread the time stamp
1970 */
Steven Rostedte8bc43e2010-10-20 10:58:02 -04001971 ts = rb_time_stamp(buffer);
1972 next_page->page->time_stamp = ts;
Steven Rostedtaa20ae82009-05-05 21:16:11 -04001973 }
1974
Steven Rostedt77ae3652009-03-27 11:00:29 -04001975 out_again:
Steven Rostedtaa20ae82009-05-05 21:16:11 -04001976
Steven Rostedt77ae3652009-03-27 11:00:29 -04001977 rb_reset_tail(cpu_buffer, tail_page, tail, length);
Steven Rostedtaa20ae82009-05-05 21:16:11 -04001978
1979 /* fail and let the caller try again */
1980 return ERR_PTR(-EAGAIN);
1981
Steven Rostedt45141d42009-02-12 13:19:48 -05001982 out_reset:
Lai Jiangshan6f3b3442009-01-12 11:06:18 +08001983 /* reset write */
Steven Rostedtc7b09302009-06-11 11:12:00 -04001984 rb_reset_tail(cpu_buffer, tail_page, tail, length);
Lai Jiangshan6f3b3442009-01-12 11:06:18 +08001985
Steven Rostedtbf41a152008-10-04 02:00:59 -04001986 return NULL;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001987}
1988
Steven Rostedt6634ff22009-05-06 15:30:07 -04001989static struct ring_buffer_event *
1990__rb_reserve_next(struct ring_buffer_per_cpu *cpu_buffer,
Steven Rostedt69d1b832010-10-07 18:18:05 -04001991 unsigned long length, u64 ts,
1992 u64 delta, int add_timestamp)
Steven Rostedt6634ff22009-05-06 15:30:07 -04001993{
Steven Rostedt5a50e332009-11-17 08:43:01 -05001994 struct buffer_page *tail_page;
Steven Rostedt6634ff22009-05-06 15:30:07 -04001995 struct ring_buffer_event *event;
1996 unsigned long tail, write;
1997
Steven Rostedt69d1b832010-10-07 18:18:05 -04001998 /*
1999 * If the time delta since the last event is too big to
2000 * hold in the time field of the event, then we append a
2001 * TIME EXTEND event ahead of the data event.
2002 */
2003 if (unlikely(add_timestamp))
2004 length += RB_LEN_TIME_EXTEND;
2005
Steven Rostedt6634ff22009-05-06 15:30:07 -04002006 tail_page = cpu_buffer->tail_page;
2007 write = local_add_return(length, &tail_page->write);
Steven Rostedt77ae3652009-03-27 11:00:29 -04002008
2009 /* set write to only the index of the write */
2010 write &= RB_WRITE_MASK;
Steven Rostedt6634ff22009-05-06 15:30:07 -04002011 tail = write - length;
2012
2013 /* See if we shot pass the end of this buffer page */
Steven Rostedt747e94a2010-10-08 13:51:48 -04002014 if (unlikely(write > BUF_PAGE_SIZE))
Steven Rostedt6634ff22009-05-06 15:30:07 -04002015 return rb_move_tail(cpu_buffer, length, tail,
Steven Rostedt5a50e332009-11-17 08:43:01 -05002016 tail_page, ts);
Steven Rostedt6634ff22009-05-06 15:30:07 -04002017
2018 /* We reserved something on the buffer */
2019
Steven Rostedt6634ff22009-05-06 15:30:07 -04002020 event = __rb_page_index(tail_page, tail);
Vegard Nossum1744a212009-02-28 08:29:44 +01002021 kmemcheck_annotate_bitfield(event, bitfield);
Steven Rostedt69d1b832010-10-07 18:18:05 -04002022 rb_update_event(cpu_buffer, event, length, add_timestamp, delta);
Steven Rostedt6634ff22009-05-06 15:30:07 -04002023
Steven Rostedt69d1b832010-10-07 18:18:05 -04002024 local_inc(&tail_page->entries);
Steven Rostedt6634ff22009-05-06 15:30:07 -04002025
2026 /*
Steven Rostedtfa743952009-06-16 12:37:57 -04002027 * If this is the first commit on the page, then update
2028 * its timestamp.
Steven Rostedt6634ff22009-05-06 15:30:07 -04002029 */
Steven Rostedtfa743952009-06-16 12:37:57 -04002030 if (!tail)
Steven Rostedte8bc43e2010-10-20 10:58:02 -04002031 tail_page->page->time_stamp = ts;
Steven Rostedt6634ff22009-05-06 15:30:07 -04002032
2033 return event;
2034}
2035
Steven Rostedtedd813bf2009-06-02 23:00:53 -04002036static inline int
2037rb_try_to_discard(struct ring_buffer_per_cpu *cpu_buffer,
2038 struct ring_buffer_event *event)
2039{
2040 unsigned long new_index, old_index;
2041 struct buffer_page *bpage;
2042 unsigned long index;
2043 unsigned long addr;
2044
2045 new_index = rb_event_index(event);
Steven Rostedt69d1b832010-10-07 18:18:05 -04002046 old_index = new_index + rb_event_ts_length(event);
Steven Rostedtedd813bf2009-06-02 23:00:53 -04002047 addr = (unsigned long)event;
2048 addr &= PAGE_MASK;
2049
2050 bpage = cpu_buffer->tail_page;
2051
2052 if (bpage->page == (void *)addr && rb_page_write(bpage) == old_index) {
Steven Rostedt77ae3652009-03-27 11:00:29 -04002053 unsigned long write_mask =
2054 local_read(&bpage->write) & ~RB_WRITE_MASK;
Steven Rostedtedd813bf2009-06-02 23:00:53 -04002055 /*
2056 * This is on the tail page. It is possible that
2057 * a write could come in and move the tail page
2058 * and write to the next page. That is fine
2059 * because we just shorten what is on this page.
2060 */
Steven Rostedt77ae3652009-03-27 11:00:29 -04002061 old_index += write_mask;
2062 new_index += write_mask;
Steven Rostedtedd813bf2009-06-02 23:00:53 -04002063 index = local_cmpxchg(&bpage->write, old_index, new_index);
2064 if (index == old_index)
2065 return 1;
2066 }
2067
2068 /* could not discard */
2069 return 0;
2070}
2071
Steven Rostedtfa743952009-06-16 12:37:57 -04002072static void rb_start_commit(struct ring_buffer_per_cpu *cpu_buffer)
2073{
2074 local_inc(&cpu_buffer->committing);
2075 local_inc(&cpu_buffer->commits);
2076}
2077
Steven Rostedtd9abde22010-10-19 13:17:08 -04002078static inline void rb_end_commit(struct ring_buffer_per_cpu *cpu_buffer)
Steven Rostedtfa743952009-06-16 12:37:57 -04002079{
2080 unsigned long commits;
2081
2082 if (RB_WARN_ON(cpu_buffer,
2083 !local_read(&cpu_buffer->committing)))
2084 return;
2085
2086 again:
2087 commits = local_read(&cpu_buffer->commits);
2088 /* synchronize with interrupts */
2089 barrier();
2090 if (local_read(&cpu_buffer->committing) == 1)
2091 rb_set_commit_to_write(cpu_buffer);
2092
2093 local_dec(&cpu_buffer->committing);
2094
2095 /* synchronize with interrupts */
2096 barrier();
2097
2098 /*
2099 * Need to account for interrupts coming in between the
2100 * updating of the commit page and the clearing of the
2101 * committing counter.
2102 */
2103 if (unlikely(local_read(&cpu_buffer->commits) != commits) &&
2104 !local_read(&cpu_buffer->committing)) {
2105 local_inc(&cpu_buffer->committing);
2106 goto again;
2107 }
2108}
2109
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002110static struct ring_buffer_event *
Steven Rostedt62f0b3e2009-09-04 14:11:34 -04002111rb_reserve_next_event(struct ring_buffer *buffer,
2112 struct ring_buffer_per_cpu *cpu_buffer,
Steven Rostedt1cd8d732009-05-11 14:08:09 -04002113 unsigned long length)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002114{
2115 struct ring_buffer_event *event;
Steven Rostedt69d1b832010-10-07 18:18:05 -04002116 u64 ts, delta;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002117 int nr_loops = 0;
Steven Rostedt69d1b832010-10-07 18:18:05 -04002118 int add_timestamp;
Steven Rostedt140ff892010-10-08 10:50:30 -04002119 u64 diff;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002120
Steven Rostedtfa743952009-06-16 12:37:57 -04002121 rb_start_commit(cpu_buffer);
2122
Steven Rostedt85bac322009-09-04 14:24:40 -04002123#ifdef CONFIG_RING_BUFFER_ALLOW_SWAP
Steven Rostedt62f0b3e2009-09-04 14:11:34 -04002124 /*
2125 * Due to the ability to swap a cpu buffer from a buffer
2126 * it is possible it was swapped before we committed.
2127 * (committing stops a swap). We check for it here and
2128 * if it happened, we have to fail the write.
2129 */
2130 barrier();
2131 if (unlikely(ACCESS_ONCE(cpu_buffer->buffer) != buffer)) {
2132 local_dec(&cpu_buffer->committing);
2133 local_dec(&cpu_buffer->commits);
2134 return NULL;
2135 }
Steven Rostedt85bac322009-09-04 14:24:40 -04002136#endif
Steven Rostedt62f0b3e2009-09-04 14:11:34 -04002137
Steven Rostedtbe957c42009-05-11 14:42:53 -04002138 length = rb_calculate_event_length(length);
Steven Rostedtbf41a152008-10-04 02:00:59 -04002139 again:
Steven Rostedt69d1b832010-10-07 18:18:05 -04002140 add_timestamp = 0;
2141 delta = 0;
2142
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002143 /*
2144 * We allow for interrupts to reenter here and do a trace.
2145 * If one does, it will cause this original code to loop
2146 * back here. Even with heavy interrupts happening, this
2147 * should only happen a few times in a row. If this happens
2148 * 1000 times in a row, there must be either an interrupt
2149 * storm or we have something buggy.
2150 * Bail!
2151 */
Steven Rostedt3e89c7bb2008-11-11 15:28:41 -05002152 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 1000))
Steven Rostedtfa743952009-06-16 12:37:57 -04002153 goto out_fail;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002154
Jiri Olsa6d3f1e12009-10-23 19:36:19 -04002155 ts = rb_time_stamp(cpu_buffer->buffer);
Steven Rostedt140ff892010-10-08 10:50:30 -04002156 diff = ts - cpu_buffer->write_stamp;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002157
Steven Rostedt140ff892010-10-08 10:50:30 -04002158 /* make sure this diff is calculated here */
2159 barrier();
Steven Rostedtbf41a152008-10-04 02:00:59 -04002160
Steven Rostedt140ff892010-10-08 10:50:30 -04002161 /* Did the write stamp get updated already? */
2162 if (likely(ts >= cpu_buffer->write_stamp)) {
Steven Rostedt168b6b12009-05-11 22:11:05 -04002163 delta = diff;
2164 if (unlikely(test_time_stamp(delta))) {
Steven Rostedt69d1b832010-10-07 18:18:05 -04002165 WARN_ONCE(delta > (1ULL << 59),
Ingo Molnare9345aa2011-02-18 08:09:49 +01002166 KERN_WARNING "Delta way too big! %llu ts=%llu write stamp = %llu\n",
Steven Rostedt69d1b832010-10-07 18:18:05 -04002167 (unsigned long long)delta,
2168 (unsigned long long)ts,
Ingo Molnare9345aa2011-02-18 08:09:49 +01002169 (unsigned long long)cpu_buffer->write_stamp);
Steven Rostedt69d1b832010-10-07 18:18:05 -04002170 add_timestamp = 1;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002171 }
Steven Rostedt168b6b12009-05-11 22:11:05 -04002172 }
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002173
Steven Rostedt69d1b832010-10-07 18:18:05 -04002174 event = __rb_reserve_next(cpu_buffer, length, ts,
2175 delta, add_timestamp);
Steven Rostedt168b6b12009-05-11 22:11:05 -04002176 if (unlikely(PTR_ERR(event) == -EAGAIN))
Steven Rostedtbf41a152008-10-04 02:00:59 -04002177 goto again;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002178
Steven Rostedtfa743952009-06-16 12:37:57 -04002179 if (!event)
2180 goto out_fail;
Steven Rostedtbf41a152008-10-04 02:00:59 -04002181
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002182 return event;
Steven Rostedtfa743952009-06-16 12:37:57 -04002183
2184 out_fail:
2185 rb_end_commit(cpu_buffer);
2186 return NULL;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002187}
2188
Paul Mundt1155de42009-06-25 14:30:12 +09002189#ifdef CONFIG_TRACING
2190
Steven Rostedtaa18efb2009-04-20 16:16:11 -04002191#define TRACE_RECURSIVE_DEPTH 16
Steven Rostedt261842b2009-04-16 21:41:52 -04002192
Steven Rostedtd9abde22010-10-19 13:17:08 -04002193/* Keep this code out of the fast path cache */
2194static noinline void trace_recursive_fail(void)
Steven Rostedt261842b2009-04-16 21:41:52 -04002195{
Steven Rostedtaa18efb2009-04-20 16:16:11 -04002196 /* Disable all tracing before we do anything else */
2197 tracing_off_permanent();
Frederic Weisbeckere057a5e2009-04-19 23:38:12 +02002198
Steven Rostedt7d7d2b82009-04-27 12:37:49 -04002199 printk_once(KERN_WARNING "Tracing recursion: depth[%ld]:"
Steven Rostedtaa18efb2009-04-20 16:16:11 -04002200 "HC[%lu]:SC[%lu]:NMI[%lu]\n",
2201 current->trace_recursion,
2202 hardirq_count() >> HARDIRQ_SHIFT,
2203 softirq_count() >> SOFTIRQ_SHIFT,
2204 in_nmi());
Frederic Weisbeckere057a5e2009-04-19 23:38:12 +02002205
Steven Rostedtaa18efb2009-04-20 16:16:11 -04002206 WARN_ON_ONCE(1);
Steven Rostedtd9abde22010-10-19 13:17:08 -04002207}
2208
2209static inline int trace_recursive_lock(void)
2210{
2211 current->trace_recursion++;
2212
2213 if (likely(current->trace_recursion < TRACE_RECURSIVE_DEPTH))
2214 return 0;
2215
2216 trace_recursive_fail();
2217
Steven Rostedtaa18efb2009-04-20 16:16:11 -04002218 return -1;
Steven Rostedt261842b2009-04-16 21:41:52 -04002219}
2220
Steven Rostedtd9abde22010-10-19 13:17:08 -04002221static inline void trace_recursive_unlock(void)
Steven Rostedt261842b2009-04-16 21:41:52 -04002222{
Steven Rostedtaa18efb2009-04-20 16:16:11 -04002223 WARN_ON_ONCE(!current->trace_recursion);
Steven Rostedt261842b2009-04-16 21:41:52 -04002224
Steven Rostedtaa18efb2009-04-20 16:16:11 -04002225 current->trace_recursion--;
Steven Rostedt261842b2009-04-16 21:41:52 -04002226}
2227
Paul Mundt1155de42009-06-25 14:30:12 +09002228#else
2229
2230#define trace_recursive_lock() (0)
2231#define trace_recursive_unlock() do { } while (0)
2232
2233#endif
2234
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002235/**
2236 * ring_buffer_lock_reserve - reserve a part of the buffer
2237 * @buffer: the ring buffer to reserve from
2238 * @length: the length of the data to reserve (excluding event header)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002239 *
2240 * Returns a reseverd event on the ring buffer to copy directly to.
2241 * The user of this interface will need to get the body to write into
2242 * and can use the ring_buffer_event_data() interface.
2243 *
2244 * The length is the length of the data needed, not the event length
2245 * which also includes the event header.
2246 *
2247 * Must be paired with ring_buffer_unlock_commit, unless NULL is returned.
2248 * If NULL is returned, then nothing has been allocated or locked.
2249 */
2250struct ring_buffer_event *
Arnaldo Carvalho de Melo0a987752009-02-05 16:12:56 -02002251ring_buffer_lock_reserve(struct ring_buffer *buffer, unsigned long length)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002252{
2253 struct ring_buffer_per_cpu *cpu_buffer;
2254 struct ring_buffer_event *event;
Steven Rostedt5168ae52010-06-03 09:36:50 -04002255 int cpu;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002256
Steven Rostedt033601a2008-11-21 12:41:55 -05002257 if (ring_buffer_flags != RB_BUFFERS_ON)
Steven Rostedta3583242008-11-11 15:01:42 -05002258 return NULL;
2259
Steven Rostedtbf41a152008-10-04 02:00:59 -04002260 /* If we are tracing schedule, we don't want to recurse */
Steven Rostedt5168ae52010-06-03 09:36:50 -04002261 preempt_disable_notrace();
Steven Rostedtbf41a152008-10-04 02:00:59 -04002262
Lai Jiangshan52fbe9c2010-03-08 14:50:43 +08002263 if (atomic_read(&buffer->record_disabled))
2264 goto out_nocheck;
2265
Steven Rostedt261842b2009-04-16 21:41:52 -04002266 if (trace_recursive_lock())
2267 goto out_nocheck;
2268
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002269 cpu = raw_smp_processor_id();
2270
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302271 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedtd7690412008-10-01 00:29:53 -04002272 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002273
2274 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002275
2276 if (atomic_read(&cpu_buffer->record_disabled))
Steven Rostedtd7690412008-10-01 00:29:53 -04002277 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002278
Steven Rostedtbe957c42009-05-11 14:42:53 -04002279 if (length > BUF_MAX_DATA_SIZE)
Steven Rostedtbf41a152008-10-04 02:00:59 -04002280 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002281
Steven Rostedt62f0b3e2009-09-04 14:11:34 -04002282 event = rb_reserve_next_event(buffer, cpu_buffer, length);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002283 if (!event)
Steven Rostedtd7690412008-10-01 00:29:53 -04002284 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002285
2286 return event;
2287
Steven Rostedtd7690412008-10-01 00:29:53 -04002288 out:
Steven Rostedt261842b2009-04-16 21:41:52 -04002289 trace_recursive_unlock();
2290
2291 out_nocheck:
Steven Rostedt5168ae52010-06-03 09:36:50 -04002292 preempt_enable_notrace();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002293 return NULL;
2294}
Robert Richterc4f50182008-12-11 16:49:22 +01002295EXPORT_SYMBOL_GPL(ring_buffer_lock_reserve);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002296
Steven Rostedta1863c22009-09-03 10:23:58 -04002297static void
2298rb_update_write_stamp(struct ring_buffer_per_cpu *cpu_buffer,
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002299 struct ring_buffer_event *event)
2300{
Steven Rostedt69d1b832010-10-07 18:18:05 -04002301 u64 delta;
2302
Steven Rostedtfa743952009-06-16 12:37:57 -04002303 /*
2304 * The event first in the commit queue updates the
2305 * time stamp.
2306 */
Steven Rostedt69d1b832010-10-07 18:18:05 -04002307 if (rb_event_is_commit(cpu_buffer, event)) {
2308 /*
2309 * A commit event that is first on a page
2310 * updates the write timestamp with the page stamp
2311 */
2312 if (!rb_event_index(event))
2313 cpu_buffer->write_stamp =
2314 cpu_buffer->commit_page->page->time_stamp;
2315 else if (event->type_len == RINGBUF_TYPE_TIME_EXTEND) {
2316 delta = event->array[0];
2317 delta <<= TS_SHIFT;
2318 delta += event->time_delta;
2319 cpu_buffer->write_stamp += delta;
2320 } else
2321 cpu_buffer->write_stamp += event->time_delta;
2322 }
Steven Rostedta1863c22009-09-03 10:23:58 -04002323}
Steven Rostedtbf41a152008-10-04 02:00:59 -04002324
Steven Rostedta1863c22009-09-03 10:23:58 -04002325static void rb_commit(struct ring_buffer_per_cpu *cpu_buffer,
2326 struct ring_buffer_event *event)
2327{
2328 local_inc(&cpu_buffer->entries);
2329 rb_update_write_stamp(cpu_buffer, event);
Steven Rostedtfa743952009-06-16 12:37:57 -04002330 rb_end_commit(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002331}
2332
2333/**
2334 * ring_buffer_unlock_commit - commit a reserved
2335 * @buffer: The buffer to commit to
2336 * @event: The event pointer to commit.
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002337 *
2338 * This commits the data to the ring buffer, and releases any locks held.
2339 *
2340 * Must be paired with ring_buffer_lock_reserve.
2341 */
2342int ring_buffer_unlock_commit(struct ring_buffer *buffer,
Arnaldo Carvalho de Melo0a987752009-02-05 16:12:56 -02002343 struct ring_buffer_event *event)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002344{
2345 struct ring_buffer_per_cpu *cpu_buffer;
2346 int cpu = raw_smp_processor_id();
2347
2348 cpu_buffer = buffer->buffers[cpu];
2349
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002350 rb_commit(cpu_buffer, event);
2351
Steven Rostedt261842b2009-04-16 21:41:52 -04002352 trace_recursive_unlock();
2353
Steven Rostedt5168ae52010-06-03 09:36:50 -04002354 preempt_enable_notrace();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002355
2356 return 0;
2357}
Robert Richterc4f50182008-12-11 16:49:22 +01002358EXPORT_SYMBOL_GPL(ring_buffer_unlock_commit);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002359
Frederic Weisbeckerf3b9aae2009-04-19 23:39:33 +02002360static inline void rb_event_discard(struct ring_buffer_event *event)
2361{
Steven Rostedt69d1b832010-10-07 18:18:05 -04002362 if (event->type_len == RINGBUF_TYPE_TIME_EXTEND)
2363 event = skip_time_extend(event);
2364
Lai Jiangshan334d4162009-04-24 11:27:05 +08002365 /* array[0] holds the actual length for the discarded event */
2366 event->array[0] = rb_event_data_length(event) - RB_EVNT_HDR_SIZE;
2367 event->type_len = RINGBUF_TYPE_PADDING;
Frederic Weisbeckerf3b9aae2009-04-19 23:39:33 +02002368 /* time delta must be non zero */
2369 if (!event->time_delta)
2370 event->time_delta = 1;
2371}
2372
Steven Rostedta1863c22009-09-03 10:23:58 -04002373/*
2374 * Decrement the entries to the page that an event is on.
2375 * The event does not even need to exist, only the pointer
2376 * to the page it is on. This may only be called before the commit
2377 * takes place.
2378 */
2379static inline void
2380rb_decrement_entry(struct ring_buffer_per_cpu *cpu_buffer,
2381 struct ring_buffer_event *event)
2382{
2383 unsigned long addr = (unsigned long)event;
2384 struct buffer_page *bpage = cpu_buffer->commit_page;
2385 struct buffer_page *start;
2386
2387 addr &= PAGE_MASK;
2388
2389 /* Do the likely case first */
2390 if (likely(bpage->page == (void *)addr)) {
2391 local_dec(&bpage->entries);
2392 return;
2393 }
2394
2395 /*
2396 * Because the commit page may be on the reader page we
2397 * start with the next page and check the end loop there.
2398 */
2399 rb_inc_page(cpu_buffer, &bpage);
2400 start = bpage;
2401 do {
2402 if (bpage->page == (void *)addr) {
2403 local_dec(&bpage->entries);
2404 return;
2405 }
2406 rb_inc_page(cpu_buffer, &bpage);
2407 } while (bpage != start);
2408
2409 /* commit not part of this buffer?? */
2410 RB_WARN_ON(cpu_buffer, 1);
2411}
2412
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002413/**
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002414 * ring_buffer_commit_discard - discard an event that has not been committed
2415 * @buffer: the ring buffer
2416 * @event: non committed event to discard
2417 *
Steven Rostedtdc892f72009-09-03 15:33:41 -04002418 * Sometimes an event that is in the ring buffer needs to be ignored.
2419 * This function lets the user discard an event in the ring buffer
2420 * and then that event will not be read later.
2421 *
2422 * This function only works if it is called before the the item has been
2423 * committed. It will try to free the event from the ring buffer
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002424 * if another event has not been added behind it.
2425 *
2426 * If another event has been added behind it, it will set the event
2427 * up as discarded, and perform the commit.
2428 *
2429 * If this function is called, do not call ring_buffer_unlock_commit on
2430 * the event.
2431 */
2432void ring_buffer_discard_commit(struct ring_buffer *buffer,
2433 struct ring_buffer_event *event)
2434{
2435 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002436 int cpu;
2437
2438 /* The event is discarded regardless */
Frederic Weisbeckerf3b9aae2009-04-19 23:39:33 +02002439 rb_event_discard(event);
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002440
Steven Rostedtfa743952009-06-16 12:37:57 -04002441 cpu = smp_processor_id();
2442 cpu_buffer = buffer->buffers[cpu];
2443
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002444 /*
2445 * This must only be called if the event has not been
2446 * committed yet. Thus we can assume that preemption
2447 * is still disabled.
2448 */
Steven Rostedtfa743952009-06-16 12:37:57 -04002449 RB_WARN_ON(buffer, !local_read(&cpu_buffer->committing));
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002450
Steven Rostedta1863c22009-09-03 10:23:58 -04002451 rb_decrement_entry(cpu_buffer, event);
Steven Rostedt0f2541d2009-08-05 12:02:48 -04002452 if (rb_try_to_discard(cpu_buffer, event))
Steven Rostedtedd813bf2009-06-02 23:00:53 -04002453 goto out;
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002454
2455 /*
2456 * The commit is still visible by the reader, so we
Steven Rostedta1863c22009-09-03 10:23:58 -04002457 * must still update the timestamp.
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002458 */
Steven Rostedta1863c22009-09-03 10:23:58 -04002459 rb_update_write_stamp(cpu_buffer, event);
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002460 out:
Steven Rostedtfa743952009-06-16 12:37:57 -04002461 rb_end_commit(cpu_buffer);
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002462
Frederic Weisbeckerf3b9aae2009-04-19 23:39:33 +02002463 trace_recursive_unlock();
2464
Steven Rostedt5168ae52010-06-03 09:36:50 -04002465 preempt_enable_notrace();
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002466
2467}
2468EXPORT_SYMBOL_GPL(ring_buffer_discard_commit);
2469
2470/**
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002471 * ring_buffer_write - write data to the buffer without reserving
2472 * @buffer: The ring buffer to write to.
2473 * @length: The length of the data being written (excluding the event header)
2474 * @data: The data to write to the buffer.
2475 *
2476 * This is like ring_buffer_lock_reserve and ring_buffer_unlock_commit as
2477 * one function. If you already have the data to write to the buffer, it
2478 * may be easier to simply call this function.
2479 *
2480 * Note, like ring_buffer_lock_reserve, the length is the length of the data
2481 * and not the length of the event which would hold the header.
2482 */
2483int ring_buffer_write(struct ring_buffer *buffer,
2484 unsigned long length,
2485 void *data)
2486{
2487 struct ring_buffer_per_cpu *cpu_buffer;
2488 struct ring_buffer_event *event;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002489 void *body;
2490 int ret = -EBUSY;
Steven Rostedt5168ae52010-06-03 09:36:50 -04002491 int cpu;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002492
Steven Rostedt033601a2008-11-21 12:41:55 -05002493 if (ring_buffer_flags != RB_BUFFERS_ON)
Steven Rostedta3583242008-11-11 15:01:42 -05002494 return -EBUSY;
2495
Steven Rostedt5168ae52010-06-03 09:36:50 -04002496 preempt_disable_notrace();
Steven Rostedtbf41a152008-10-04 02:00:59 -04002497
Lai Jiangshan52fbe9c2010-03-08 14:50:43 +08002498 if (atomic_read(&buffer->record_disabled))
2499 goto out;
2500
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002501 cpu = raw_smp_processor_id();
2502
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302503 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedtd7690412008-10-01 00:29:53 -04002504 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002505
2506 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002507
2508 if (atomic_read(&cpu_buffer->record_disabled))
2509 goto out;
2510
Steven Rostedtbe957c42009-05-11 14:42:53 -04002511 if (length > BUF_MAX_DATA_SIZE)
2512 goto out;
2513
Steven Rostedt62f0b3e2009-09-04 14:11:34 -04002514 event = rb_reserve_next_event(buffer, cpu_buffer, length);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002515 if (!event)
2516 goto out;
2517
2518 body = rb_event_data(event);
2519
2520 memcpy(body, data, length);
2521
2522 rb_commit(cpu_buffer, event);
2523
2524 ret = 0;
2525 out:
Steven Rostedt5168ae52010-06-03 09:36:50 -04002526 preempt_enable_notrace();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002527
2528 return ret;
2529}
Robert Richterc4f50182008-12-11 16:49:22 +01002530EXPORT_SYMBOL_GPL(ring_buffer_write);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002531
Andrew Morton34a148b2009-01-09 12:27:09 -08002532static int rb_per_cpu_empty(struct ring_buffer_per_cpu *cpu_buffer)
Steven Rostedtbf41a152008-10-04 02:00:59 -04002533{
2534 struct buffer_page *reader = cpu_buffer->reader_page;
Steven Rostedt77ae3652009-03-27 11:00:29 -04002535 struct buffer_page *head = rb_set_head_page(cpu_buffer);
Steven Rostedtbf41a152008-10-04 02:00:59 -04002536 struct buffer_page *commit = cpu_buffer->commit_page;
2537
Steven Rostedt77ae3652009-03-27 11:00:29 -04002538 /* In case of error, head will be NULL */
2539 if (unlikely(!head))
2540 return 1;
2541
Steven Rostedtbf41a152008-10-04 02:00:59 -04002542 return reader->read == rb_page_commit(reader) &&
2543 (commit == reader ||
2544 (commit == head &&
2545 head->read == rb_page_commit(commit)));
2546}
2547
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002548/**
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002549 * ring_buffer_record_disable - stop all writes into the buffer
2550 * @buffer: The ring buffer to stop writes to.
2551 *
2552 * This prevents all writes to the buffer. Any attempt to write
2553 * to the buffer after this will fail and return NULL.
2554 *
2555 * The caller should call synchronize_sched() after this.
2556 */
2557void ring_buffer_record_disable(struct ring_buffer *buffer)
2558{
2559 atomic_inc(&buffer->record_disabled);
2560}
Robert Richterc4f50182008-12-11 16:49:22 +01002561EXPORT_SYMBOL_GPL(ring_buffer_record_disable);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002562
2563/**
2564 * ring_buffer_record_enable - enable writes to the buffer
2565 * @buffer: The ring buffer to enable writes
2566 *
2567 * Note, multiple disables will need the same number of enables
Adam Buchbinderc41b20e2009-12-11 16:35:39 -05002568 * to truly enable the writing (much like preempt_disable).
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002569 */
2570void ring_buffer_record_enable(struct ring_buffer *buffer)
2571{
2572 atomic_dec(&buffer->record_disabled);
2573}
Robert Richterc4f50182008-12-11 16:49:22 +01002574EXPORT_SYMBOL_GPL(ring_buffer_record_enable);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002575
2576/**
2577 * ring_buffer_record_disable_cpu - stop all writes into the cpu_buffer
2578 * @buffer: The ring buffer to stop writes to.
2579 * @cpu: The CPU buffer to stop
2580 *
2581 * This prevents all writes to the buffer. Any attempt to write
2582 * to the buffer after this will fail and return NULL.
2583 *
2584 * The caller should call synchronize_sched() after this.
2585 */
2586void ring_buffer_record_disable_cpu(struct ring_buffer *buffer, int cpu)
2587{
2588 struct ring_buffer_per_cpu *cpu_buffer;
2589
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302590 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04002591 return;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002592
2593 cpu_buffer = buffer->buffers[cpu];
2594 atomic_inc(&cpu_buffer->record_disabled);
2595}
Robert Richterc4f50182008-12-11 16:49:22 +01002596EXPORT_SYMBOL_GPL(ring_buffer_record_disable_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002597
2598/**
2599 * ring_buffer_record_enable_cpu - enable writes to the buffer
2600 * @buffer: The ring buffer to enable writes
2601 * @cpu: The CPU to enable.
2602 *
2603 * Note, multiple disables will need the same number of enables
Adam Buchbinderc41b20e2009-12-11 16:35:39 -05002604 * to truly enable the writing (much like preempt_disable).
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002605 */
2606void ring_buffer_record_enable_cpu(struct ring_buffer *buffer, int cpu)
2607{
2608 struct ring_buffer_per_cpu *cpu_buffer;
2609
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302610 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04002611 return;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002612
2613 cpu_buffer = buffer->buffers[cpu];
2614 atomic_dec(&cpu_buffer->record_disabled);
2615}
Robert Richterc4f50182008-12-11 16:49:22 +01002616EXPORT_SYMBOL_GPL(ring_buffer_record_enable_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002617
Steven Rostedtf6195aa2010-09-01 12:23:12 -04002618/*
2619 * The total entries in the ring buffer is the running counter
2620 * of entries entered into the ring buffer, minus the sum of
2621 * the entries read from the ring buffer and the number of
2622 * entries that were overwritten.
2623 */
2624static inline unsigned long
2625rb_num_of_entries(struct ring_buffer_per_cpu *cpu_buffer)
2626{
2627 return local_read(&cpu_buffer->entries) -
2628 (local_read(&cpu_buffer->overrun) + cpu_buffer->read);
2629}
2630
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002631/**
2632 * ring_buffer_entries_cpu - get the number of entries in a cpu buffer
2633 * @buffer: The ring buffer
2634 * @cpu: The per CPU buffer to get the entries from.
2635 */
2636unsigned long ring_buffer_entries_cpu(struct ring_buffer *buffer, int cpu)
2637{
2638 struct ring_buffer_per_cpu *cpu_buffer;
2639
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 Rostedt554f7862009-03-11 22:00:13 -04002644
Steven Rostedtf6195aa2010-09-01 12:23:12 -04002645 return rb_num_of_entries(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002646}
Robert Richterc4f50182008-12-11 16:49:22 +01002647EXPORT_SYMBOL_GPL(ring_buffer_entries_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002648
2649/**
2650 * ring_buffer_overrun_cpu - get the number of overruns in a cpu_buffer
2651 * @buffer: The ring buffer
2652 * @cpu: The per CPU buffer to get the number of overruns from
2653 */
2654unsigned long ring_buffer_overrun_cpu(struct ring_buffer *buffer, int cpu)
2655{
2656 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedt8aabee52009-03-12 13:13:49 -04002657 unsigned long ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002658
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302659 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04002660 return 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002661
2662 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt77ae3652009-03-27 11:00:29 -04002663 ret = local_read(&cpu_buffer->overrun);
Steven Rostedt554f7862009-03-11 22:00:13 -04002664
2665 return ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002666}
Robert Richterc4f50182008-12-11 16:49:22 +01002667EXPORT_SYMBOL_GPL(ring_buffer_overrun_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002668
2669/**
Steven Rostedtf0d2c682009-04-29 13:43:37 -04002670 * ring_buffer_commit_overrun_cpu - get the number of overruns caused by commits
2671 * @buffer: The ring buffer
2672 * @cpu: The per CPU buffer to get the number of overruns from
2673 */
2674unsigned long
2675ring_buffer_commit_overrun_cpu(struct ring_buffer *buffer, int cpu)
2676{
2677 struct ring_buffer_per_cpu *cpu_buffer;
2678 unsigned long ret;
2679
2680 if (!cpumask_test_cpu(cpu, buffer->cpumask))
2681 return 0;
2682
2683 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt77ae3652009-03-27 11:00:29 -04002684 ret = local_read(&cpu_buffer->commit_overrun);
Steven Rostedtf0d2c682009-04-29 13:43:37 -04002685
2686 return ret;
2687}
2688EXPORT_SYMBOL_GPL(ring_buffer_commit_overrun_cpu);
2689
2690/**
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002691 * ring_buffer_entries - get the number of entries in a buffer
2692 * @buffer: The ring buffer
2693 *
2694 * Returns the total number of entries in the ring buffer
2695 * (all CPU entries)
2696 */
2697unsigned long ring_buffer_entries(struct ring_buffer *buffer)
2698{
2699 struct ring_buffer_per_cpu *cpu_buffer;
2700 unsigned long entries = 0;
2701 int cpu;
2702
2703 /* if you care about this being correct, lock the buffer */
2704 for_each_buffer_cpu(buffer, cpu) {
2705 cpu_buffer = buffer->buffers[cpu];
Steven Rostedtf6195aa2010-09-01 12:23:12 -04002706 entries += rb_num_of_entries(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002707 }
2708
2709 return entries;
2710}
Robert Richterc4f50182008-12-11 16:49:22 +01002711EXPORT_SYMBOL_GPL(ring_buffer_entries);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002712
2713/**
Jiri Olsa67b394f2009-10-23 19:36:18 -04002714 * ring_buffer_overruns - get the number of overruns in buffer
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002715 * @buffer: The ring buffer
2716 *
2717 * Returns the total number of overruns in the ring buffer
2718 * (all CPU entries)
2719 */
2720unsigned long ring_buffer_overruns(struct ring_buffer *buffer)
2721{
2722 struct ring_buffer_per_cpu *cpu_buffer;
2723 unsigned long overruns = 0;
2724 int cpu;
2725
2726 /* if you care about this being correct, lock the buffer */
2727 for_each_buffer_cpu(buffer, cpu) {
2728 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt77ae3652009-03-27 11:00:29 -04002729 overruns += local_read(&cpu_buffer->overrun);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002730 }
2731
2732 return overruns;
2733}
Robert Richterc4f50182008-12-11 16:49:22 +01002734EXPORT_SYMBOL_GPL(ring_buffer_overruns);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002735
Steven Rostedt642edba2008-11-12 00:01:26 -05002736static void rb_iter_reset(struct ring_buffer_iter *iter)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002737{
2738 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
2739
Steven Rostedtd7690412008-10-01 00:29:53 -04002740 /* Iterator usage is expected to have record disabled */
2741 if (list_empty(&cpu_buffer->reader_page->list)) {
Steven Rostedt77ae3652009-03-27 11:00:29 -04002742 iter->head_page = rb_set_head_page(cpu_buffer);
2743 if (unlikely(!iter->head_page))
2744 return;
2745 iter->head = iter->head_page->read;
Steven Rostedtd7690412008-10-01 00:29:53 -04002746 } else {
2747 iter->head_page = cpu_buffer->reader_page;
Steven Rostedt6f807ac2008-10-04 02:00:58 -04002748 iter->head = cpu_buffer->reader_page->read;
Steven Rostedtd7690412008-10-01 00:29:53 -04002749 }
2750 if (iter->head)
2751 iter->read_stamp = cpu_buffer->read_stamp;
2752 else
Steven Rostedtabc9b562008-12-02 15:34:06 -05002753 iter->read_stamp = iter->head_page->page->time_stamp;
Steven Rostedt492a74f2010-01-25 15:17:47 -05002754 iter->cache_reader_page = cpu_buffer->reader_page;
2755 iter->cache_read = cpu_buffer->read;
Steven Rostedt642edba2008-11-12 00:01:26 -05002756}
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002757
Steven Rostedt642edba2008-11-12 00:01:26 -05002758/**
2759 * ring_buffer_iter_reset - reset an iterator
2760 * @iter: The iterator to reset
2761 *
2762 * Resets the iterator, so that it will start from the beginning
2763 * again.
2764 */
2765void ring_buffer_iter_reset(struct ring_buffer_iter *iter)
2766{
Steven Rostedt554f7862009-03-11 22:00:13 -04002767 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedt642edba2008-11-12 00:01:26 -05002768 unsigned long flags;
2769
Steven Rostedt554f7862009-03-11 22:00:13 -04002770 if (!iter)
2771 return;
2772
2773 cpu_buffer = iter->cpu_buffer;
2774
Steven Rostedt642edba2008-11-12 00:01:26 -05002775 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
2776 rb_iter_reset(iter);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002777 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002778}
Robert Richterc4f50182008-12-11 16:49:22 +01002779EXPORT_SYMBOL_GPL(ring_buffer_iter_reset);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002780
2781/**
2782 * ring_buffer_iter_empty - check if an iterator has no more to read
2783 * @iter: The iterator to check
2784 */
2785int ring_buffer_iter_empty(struct ring_buffer_iter *iter)
2786{
2787 struct ring_buffer_per_cpu *cpu_buffer;
2788
2789 cpu_buffer = iter->cpu_buffer;
2790
Steven Rostedtbf41a152008-10-04 02:00:59 -04002791 return iter->head_page == cpu_buffer->commit_page &&
2792 iter->head == rb_commit_index(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002793}
Robert Richterc4f50182008-12-11 16:49:22 +01002794EXPORT_SYMBOL_GPL(ring_buffer_iter_empty);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002795
2796static void
2797rb_update_read_stamp(struct ring_buffer_per_cpu *cpu_buffer,
2798 struct ring_buffer_event *event)
2799{
2800 u64 delta;
2801
Lai Jiangshan334d4162009-04-24 11:27:05 +08002802 switch (event->type_len) {
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002803 case RINGBUF_TYPE_PADDING:
2804 return;
2805
2806 case RINGBUF_TYPE_TIME_EXTEND:
2807 delta = event->array[0];
2808 delta <<= TS_SHIFT;
2809 delta += event->time_delta;
2810 cpu_buffer->read_stamp += delta;
2811 return;
2812
2813 case RINGBUF_TYPE_TIME_STAMP:
2814 /* FIXME: not implemented */
2815 return;
2816
2817 case RINGBUF_TYPE_DATA:
2818 cpu_buffer->read_stamp += event->time_delta;
2819 return;
2820
2821 default:
2822 BUG();
2823 }
2824 return;
2825}
2826
2827static void
2828rb_update_iter_read_stamp(struct ring_buffer_iter *iter,
2829 struct ring_buffer_event *event)
2830{
2831 u64 delta;
2832
Lai Jiangshan334d4162009-04-24 11:27:05 +08002833 switch (event->type_len) {
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002834 case RINGBUF_TYPE_PADDING:
2835 return;
2836
2837 case RINGBUF_TYPE_TIME_EXTEND:
2838 delta = event->array[0];
2839 delta <<= TS_SHIFT;
2840 delta += event->time_delta;
2841 iter->read_stamp += delta;
2842 return;
2843
2844 case RINGBUF_TYPE_TIME_STAMP:
2845 /* FIXME: not implemented */
2846 return;
2847
2848 case RINGBUF_TYPE_DATA:
2849 iter->read_stamp += event->time_delta;
2850 return;
2851
2852 default:
2853 BUG();
2854 }
2855 return;
2856}
2857
Steven Rostedtd7690412008-10-01 00:29:53 -04002858static struct buffer_page *
2859rb_get_reader_page(struct ring_buffer_per_cpu *cpu_buffer)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002860{
Steven Rostedtd7690412008-10-01 00:29:53 -04002861 struct buffer_page *reader = NULL;
Steven Rostedt66a8cb92010-03-31 13:21:56 -04002862 unsigned long overwrite;
Steven Rostedtd7690412008-10-01 00:29:53 -04002863 unsigned long flags;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002864 int nr_loops = 0;
Steven Rostedt77ae3652009-03-27 11:00:29 -04002865 int ret;
Steven Rostedtd7690412008-10-01 00:29:53 -04002866
Steven Rostedt3e03fb72008-11-06 00:09:43 -05002867 local_irq_save(flags);
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01002868 arch_spin_lock(&cpu_buffer->lock);
Steven Rostedtd7690412008-10-01 00:29:53 -04002869
2870 again:
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002871 /*
2872 * This should normally only loop twice. But because the
2873 * start of the reader inserts an empty page, it causes
2874 * a case where we will loop three times. There should be no
2875 * reason to loop four times (that I know of).
2876 */
Steven Rostedt3e89c7bb2008-11-11 15:28:41 -05002877 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 3)) {
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002878 reader = NULL;
2879 goto out;
2880 }
2881
Steven Rostedtd7690412008-10-01 00:29:53 -04002882 reader = cpu_buffer->reader_page;
2883
2884 /* If there's more to read, return this page */
Steven Rostedtbf41a152008-10-04 02:00:59 -04002885 if (cpu_buffer->reader_page->read < rb_page_size(reader))
Steven Rostedtd7690412008-10-01 00:29:53 -04002886 goto out;
2887
2888 /* Never should we have an index greater than the size */
Steven Rostedt3e89c7bb2008-11-11 15:28:41 -05002889 if (RB_WARN_ON(cpu_buffer,
2890 cpu_buffer->reader_page->read > rb_page_size(reader)))
2891 goto out;
Steven Rostedtd7690412008-10-01 00:29:53 -04002892
2893 /* check if we caught up to the tail */
2894 reader = NULL;
Steven Rostedtbf41a152008-10-04 02:00:59 -04002895 if (cpu_buffer->commit_page == cpu_buffer->reader_page)
Steven Rostedtd7690412008-10-01 00:29:53 -04002896 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002897
2898 /*
Steven Rostedtd7690412008-10-01 00:29:53 -04002899 * Reset the reader page to size zero.
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002900 */
Steven Rostedt77ae3652009-03-27 11:00:29 -04002901 local_set(&cpu_buffer->reader_page->write, 0);
2902 local_set(&cpu_buffer->reader_page->entries, 0);
2903 local_set(&cpu_buffer->reader_page->page->commit, 0);
Steven Rostedtff0ff842010-03-31 22:11:42 -04002904 cpu_buffer->reader_page->real_end = 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002905
Steven Rostedt77ae3652009-03-27 11:00:29 -04002906 spin:
2907 /*
2908 * Splice the empty reader page into the list around the head.
2909 */
2910 reader = rb_set_head_page(cpu_buffer);
Steven Rostedt0e1ff5d2010-01-06 20:40:44 -05002911 cpu_buffer->reader_page->list.next = rb_list_head(reader->list.next);
Steven Rostedtd7690412008-10-01 00:29:53 -04002912 cpu_buffer->reader_page->list.prev = reader->list.prev;
Steven Rostedtbf41a152008-10-04 02:00:59 -04002913
Steven Rostedt3adc54f2009-03-30 15:32:01 -04002914 /*
2915 * cpu_buffer->pages just needs to point to the buffer, it
2916 * has no specific buffer page to point to. Lets move it out
2917 * of our way so we don't accidently swap it.
2918 */
2919 cpu_buffer->pages = reader->list.prev;
2920
Steven Rostedt77ae3652009-03-27 11:00:29 -04002921 /* The reader page will be pointing to the new head */
2922 rb_set_list_to_head(cpu_buffer, &cpu_buffer->reader_page->list);
Steven Rostedtd7690412008-10-01 00:29:53 -04002923
2924 /*
Steven Rostedt66a8cb92010-03-31 13:21:56 -04002925 * We want to make sure we read the overruns after we set up our
2926 * pointers to the next object. The writer side does a
2927 * cmpxchg to cross pages which acts as the mb on the writer
2928 * side. Note, the reader will constantly fail the swap
2929 * while the writer is updating the pointers, so this
2930 * guarantees that the overwrite recorded here is the one we
2931 * want to compare with the last_overrun.
2932 */
2933 smp_mb();
2934 overwrite = local_read(&(cpu_buffer->overrun));
2935
2936 /*
Steven Rostedt77ae3652009-03-27 11:00:29 -04002937 * Here's the tricky part.
2938 *
2939 * We need to move the pointer past the header page.
2940 * But we can only do that if a writer is not currently
2941 * moving it. The page before the header page has the
2942 * flag bit '1' set if it is pointing to the page we want.
2943 * but if the writer is in the process of moving it
2944 * than it will be '2' or already moved '0'.
Steven Rostedtd7690412008-10-01 00:29:53 -04002945 */
Steven Rostedtd7690412008-10-01 00:29:53 -04002946
Steven Rostedt77ae3652009-03-27 11:00:29 -04002947 ret = rb_head_page_replace(reader, cpu_buffer->reader_page);
2948
2949 /*
2950 * If we did not convert it, then we must try again.
2951 */
2952 if (!ret)
2953 goto spin;
2954
2955 /*
2956 * Yeah! We succeeded in replacing the page.
2957 *
2958 * Now make the new head point back to the reader page.
2959 */
David Sharp5ded3dc62010-01-06 17:12:07 -08002960 rb_list_head(reader->list.next)->prev = &cpu_buffer->reader_page->list;
Steven Rostedt77ae3652009-03-27 11:00:29 -04002961 rb_inc_page(cpu_buffer, &cpu_buffer->head_page);
Steven Rostedtd7690412008-10-01 00:29:53 -04002962
2963 /* Finally update the reader page to the new head */
2964 cpu_buffer->reader_page = reader;
2965 rb_reset_reader_page(cpu_buffer);
2966
Steven Rostedt66a8cb92010-03-31 13:21:56 -04002967 if (overwrite != cpu_buffer->last_overrun) {
2968 cpu_buffer->lost_events = overwrite - cpu_buffer->last_overrun;
2969 cpu_buffer->last_overrun = overwrite;
2970 }
2971
Steven Rostedtd7690412008-10-01 00:29:53 -04002972 goto again;
2973
2974 out:
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01002975 arch_spin_unlock(&cpu_buffer->lock);
Steven Rostedt3e03fb72008-11-06 00:09:43 -05002976 local_irq_restore(flags);
Steven Rostedtd7690412008-10-01 00:29:53 -04002977
2978 return reader;
2979}
2980
2981static void rb_advance_reader(struct ring_buffer_per_cpu *cpu_buffer)
2982{
2983 struct ring_buffer_event *event;
2984 struct buffer_page *reader;
2985 unsigned length;
2986
2987 reader = rb_get_reader_page(cpu_buffer);
2988
2989 /* This function should not be called when buffer is empty */
Steven Rostedt3e89c7bb2008-11-11 15:28:41 -05002990 if (RB_WARN_ON(cpu_buffer, !reader))
2991 return;
Steven Rostedtd7690412008-10-01 00:29:53 -04002992
2993 event = rb_reader_event(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002994
Steven Rostedta1863c22009-09-03 10:23:58 -04002995 if (event->type_len <= RINGBUF_TYPE_DATA_TYPE_LEN_MAX)
Steven Rostedte4906ef2009-04-30 20:49:44 -04002996 cpu_buffer->read++;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002997
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002998 rb_update_read_stamp(cpu_buffer, event);
2999
Steven Rostedtd7690412008-10-01 00:29:53 -04003000 length = rb_event_length(event);
Steven Rostedt6f807ac2008-10-04 02:00:58 -04003001 cpu_buffer->reader_page->read += length;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003002}
3003
3004static void rb_advance_iter(struct ring_buffer_iter *iter)
3005{
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003006 struct ring_buffer_per_cpu *cpu_buffer;
3007 struct ring_buffer_event *event;
3008 unsigned length;
3009
3010 cpu_buffer = iter->cpu_buffer;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003011
3012 /*
3013 * Check if we are at the end of the buffer.
3014 */
Steven Rostedtbf41a152008-10-04 02:00:59 -04003015 if (iter->head >= rb_page_size(iter->head_page)) {
Steven Rostedtea05b572009-06-03 09:30:10 -04003016 /* discarded commits can make the page empty */
3017 if (iter->head_page == cpu_buffer->commit_page)
Steven Rostedt3e89c7bb2008-11-11 15:28:41 -05003018 return;
Steven Rostedtd7690412008-10-01 00:29:53 -04003019 rb_inc_iter(iter);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003020 return;
3021 }
3022
3023 event = rb_iter_head_event(iter);
3024
3025 length = rb_event_length(event);
3026
3027 /*
3028 * This should not be called to advance the header if we are
3029 * at the tail of the buffer.
3030 */
Steven Rostedt3e89c7bb2008-11-11 15:28:41 -05003031 if (RB_WARN_ON(cpu_buffer,
Steven Rostedtf536aaf2008-11-10 23:07:30 -05003032 (iter->head_page == cpu_buffer->commit_page) &&
Steven Rostedt3e89c7bb2008-11-11 15:28:41 -05003033 (iter->head + length > rb_commit_index(cpu_buffer))))
3034 return;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003035
3036 rb_update_iter_read_stamp(iter, event);
3037
3038 iter->head += length;
3039
3040 /* check for end of page padding */
Steven Rostedtbf41a152008-10-04 02:00:59 -04003041 if ((iter->head >= rb_page_size(iter->head_page)) &&
3042 (iter->head_page != cpu_buffer->commit_page))
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003043 rb_advance_iter(iter);
3044}
3045
Steven Rostedt66a8cb92010-03-31 13:21:56 -04003046static int rb_lost_events(struct ring_buffer_per_cpu *cpu_buffer)
3047{
3048 return cpu_buffer->lost_events;
3049}
3050
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003051static struct ring_buffer_event *
Steven Rostedt66a8cb92010-03-31 13:21:56 -04003052rb_buffer_peek(struct ring_buffer_per_cpu *cpu_buffer, u64 *ts,
3053 unsigned long *lost_events)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003054{
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003055 struct ring_buffer_event *event;
Steven Rostedtd7690412008-10-01 00:29:53 -04003056 struct buffer_page *reader;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003057 int nr_loops = 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003058
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003059 again:
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003060 /*
Steven Rostedt69d1b832010-10-07 18:18:05 -04003061 * We repeat when a time extend is encountered.
3062 * Since the time extend is always attached to a data event,
3063 * we should never loop more than once.
3064 * (We never hit the following condition more than twice).
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003065 */
Steven Rostedt69d1b832010-10-07 18:18:05 -04003066 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 2))
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003067 return NULL;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003068
Steven Rostedtd7690412008-10-01 00:29:53 -04003069 reader = rb_get_reader_page(cpu_buffer);
3070 if (!reader)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003071 return NULL;
3072
Steven Rostedtd7690412008-10-01 00:29:53 -04003073 event = rb_reader_event(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003074
Lai Jiangshan334d4162009-04-24 11:27:05 +08003075 switch (event->type_len) {
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003076 case RINGBUF_TYPE_PADDING:
Tom Zanussi2d622712009-03-22 03:30:49 -05003077 if (rb_null_event(event))
3078 RB_WARN_ON(cpu_buffer, 1);
3079 /*
3080 * Because the writer could be discarding every
3081 * event it creates (which would probably be bad)
3082 * if we were to go back to "again" then we may never
3083 * catch up, and will trigger the warn on, or lock
3084 * the box. Return the padding, and we will release
3085 * the current locks, and try again.
3086 */
Tom Zanussi2d622712009-03-22 03:30:49 -05003087 return event;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003088
3089 case RINGBUF_TYPE_TIME_EXTEND:
3090 /* Internal data, OK to advance */
Steven Rostedtd7690412008-10-01 00:29:53 -04003091 rb_advance_reader(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003092 goto again;
3093
3094 case RINGBUF_TYPE_TIME_STAMP:
3095 /* FIXME: not implemented */
Steven Rostedtd7690412008-10-01 00:29:53 -04003096 rb_advance_reader(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003097 goto again;
3098
3099 case RINGBUF_TYPE_DATA:
3100 if (ts) {
3101 *ts = cpu_buffer->read_stamp + event->time_delta;
Robert Richterd8eeb2d2009-07-31 14:58:04 +02003102 ring_buffer_normalize_time_stamp(cpu_buffer->buffer,
Steven Rostedt37886f62009-03-17 17:22:06 -04003103 cpu_buffer->cpu, ts);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003104 }
Steven Rostedt66a8cb92010-03-31 13:21:56 -04003105 if (lost_events)
3106 *lost_events = rb_lost_events(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003107 return event;
3108
3109 default:
3110 BUG();
3111 }
3112
3113 return NULL;
3114}
Robert Richterc4f50182008-12-11 16:49:22 +01003115EXPORT_SYMBOL_GPL(ring_buffer_peek);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003116
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003117static struct ring_buffer_event *
3118rb_iter_peek(struct ring_buffer_iter *iter, u64 *ts)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003119{
3120 struct ring_buffer *buffer;
3121 struct ring_buffer_per_cpu *cpu_buffer;
3122 struct ring_buffer_event *event;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003123 int nr_loops = 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003124
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003125 cpu_buffer = iter->cpu_buffer;
3126 buffer = cpu_buffer->buffer;
3127
Steven Rostedt492a74f2010-01-25 15:17:47 -05003128 /*
3129 * Check if someone performed a consuming read to
3130 * the buffer. A consuming read invalidates the iterator
3131 * and we need to reset the iterator in this case.
3132 */
3133 if (unlikely(iter->cache_read != cpu_buffer->read ||
3134 iter->cache_reader_page != cpu_buffer->reader_page))
3135 rb_iter_reset(iter);
3136
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003137 again:
Steven Rostedt3c05d742010-01-26 16:14:08 -05003138 if (ring_buffer_iter_empty(iter))
3139 return NULL;
3140
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003141 /*
Steven Rostedt69d1b832010-10-07 18:18:05 -04003142 * We repeat when a time extend is encountered.
3143 * Since the time extend is always attached to a data event,
3144 * we should never loop more than once.
3145 * (We never hit the following condition more than twice).
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003146 */
Steven Rostedt69d1b832010-10-07 18:18:05 -04003147 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 2))
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003148 return NULL;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003149
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003150 if (rb_per_cpu_empty(cpu_buffer))
3151 return NULL;
3152
Steven Rostedt3c05d742010-01-26 16:14:08 -05003153 if (iter->head >= local_read(&iter->head_page->page->commit)) {
3154 rb_inc_iter(iter);
3155 goto again;
3156 }
3157
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003158 event = rb_iter_head_event(iter);
3159
Lai Jiangshan334d4162009-04-24 11:27:05 +08003160 switch (event->type_len) {
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003161 case RINGBUF_TYPE_PADDING:
Tom Zanussi2d622712009-03-22 03:30:49 -05003162 if (rb_null_event(event)) {
3163 rb_inc_iter(iter);
3164 goto again;
3165 }
3166 rb_advance_iter(iter);
3167 return event;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003168
3169 case RINGBUF_TYPE_TIME_EXTEND:
3170 /* Internal data, OK to advance */
3171 rb_advance_iter(iter);
3172 goto again;
3173
3174 case RINGBUF_TYPE_TIME_STAMP:
3175 /* FIXME: not implemented */
3176 rb_advance_iter(iter);
3177 goto again;
3178
3179 case RINGBUF_TYPE_DATA:
3180 if (ts) {
3181 *ts = iter->read_stamp + event->time_delta;
Steven Rostedt37886f62009-03-17 17:22:06 -04003182 ring_buffer_normalize_time_stamp(buffer,
3183 cpu_buffer->cpu, ts);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003184 }
3185 return event;
3186
3187 default:
3188 BUG();
3189 }
3190
3191 return NULL;
3192}
Robert Richterc4f50182008-12-11 16:49:22 +01003193EXPORT_SYMBOL_GPL(ring_buffer_iter_peek);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003194
Steven Rostedt8d707e82009-06-16 21:22:48 -04003195static inline int rb_ok_to_lock(void)
3196{
3197 /*
3198 * If an NMI die dumps out the content of the ring buffer
3199 * do not grab locks. We also permanently disable the ring
3200 * buffer too. A one time deal is all you get from reading
3201 * the ring buffer from an NMI.
3202 */
Steven Rostedt464e85e2009-08-05 15:26:37 -04003203 if (likely(!in_nmi()))
Steven Rostedt8d707e82009-06-16 21:22:48 -04003204 return 1;
3205
3206 tracing_off_permanent();
3207 return 0;
3208}
3209
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003210/**
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003211 * ring_buffer_peek - peek at the next event to be read
3212 * @buffer: The ring buffer to read
3213 * @cpu: The cpu to peak at
3214 * @ts: The timestamp counter of this event.
Steven Rostedt66a8cb92010-03-31 13:21:56 -04003215 * @lost_events: a variable to store if events were lost (may be NULL)
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003216 *
3217 * This will return the event that will be read next, but does
3218 * not consume the data.
3219 */
3220struct ring_buffer_event *
Steven Rostedt66a8cb92010-03-31 13:21:56 -04003221ring_buffer_peek(struct ring_buffer *buffer, int cpu, u64 *ts,
3222 unsigned long *lost_events)
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003223{
3224 struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
Steven Rostedt8aabee52009-03-12 13:13:49 -04003225 struct ring_buffer_event *event;
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003226 unsigned long flags;
Steven Rostedt8d707e82009-06-16 21:22:48 -04003227 int dolock;
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003228
Steven Rostedt554f7862009-03-11 22:00:13 -04003229 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04003230 return NULL;
Steven Rostedt554f7862009-03-11 22:00:13 -04003231
Steven Rostedt8d707e82009-06-16 21:22:48 -04003232 dolock = rb_ok_to_lock();
Tom Zanussi2d622712009-03-22 03:30:49 -05003233 again:
Steven Rostedt8d707e82009-06-16 21:22:48 -04003234 local_irq_save(flags);
3235 if (dolock)
3236 spin_lock(&cpu_buffer->reader_lock);
Steven Rostedt66a8cb92010-03-31 13:21:56 -04003237 event = rb_buffer_peek(cpu_buffer, ts, lost_events);
Robert Richter469535a2009-07-30 19:19:18 +02003238 if (event && event->type_len == RINGBUF_TYPE_PADDING)
3239 rb_advance_reader(cpu_buffer);
Steven Rostedt8d707e82009-06-16 21:22:48 -04003240 if (dolock)
3241 spin_unlock(&cpu_buffer->reader_lock);
3242 local_irq_restore(flags);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003243
Steven Rostedt1b959e12009-09-03 10:12:13 -04003244 if (event && event->type_len == RINGBUF_TYPE_PADDING)
Tom Zanussi2d622712009-03-22 03:30:49 -05003245 goto again;
Tom Zanussi2d622712009-03-22 03:30:49 -05003246
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003247 return event;
3248}
3249
3250/**
3251 * ring_buffer_iter_peek - peek at the next event to be read
3252 * @iter: The ring buffer iterator
3253 * @ts: The timestamp counter of this event.
3254 *
3255 * This will return the event that will be read next, but does
3256 * not increment the iterator.
3257 */
3258struct ring_buffer_event *
3259ring_buffer_iter_peek(struct ring_buffer_iter *iter, u64 *ts)
3260{
3261 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
3262 struct ring_buffer_event *event;
3263 unsigned long flags;
3264
Tom Zanussi2d622712009-03-22 03:30:49 -05003265 again:
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003266 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
3267 event = rb_iter_peek(iter, ts);
3268 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
3269
Steven Rostedt1b959e12009-09-03 10:12:13 -04003270 if (event && event->type_len == RINGBUF_TYPE_PADDING)
Tom Zanussi2d622712009-03-22 03:30:49 -05003271 goto again;
Tom Zanussi2d622712009-03-22 03:30:49 -05003272
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003273 return event;
3274}
3275
3276/**
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003277 * ring_buffer_consume - return an event and consume it
3278 * @buffer: The ring buffer to get the next event from
Steven Rostedt66a8cb92010-03-31 13:21:56 -04003279 * @cpu: the cpu to read the buffer from
3280 * @ts: a variable to store the timestamp (may be NULL)
3281 * @lost_events: a variable to store if events were lost (may be NULL)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003282 *
3283 * Returns the next event in the ring buffer, and that event is consumed.
3284 * Meaning, that sequential reads will keep returning a different event,
3285 * and eventually empty the ring buffer if the producer is slower.
3286 */
3287struct ring_buffer_event *
Steven Rostedt66a8cb92010-03-31 13:21:56 -04003288ring_buffer_consume(struct ring_buffer *buffer, int cpu, u64 *ts,
3289 unsigned long *lost_events)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003290{
Steven Rostedt554f7862009-03-11 22:00:13 -04003291 struct ring_buffer_per_cpu *cpu_buffer;
3292 struct ring_buffer_event *event = NULL;
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003293 unsigned long flags;
Steven Rostedt8d707e82009-06-16 21:22:48 -04003294 int dolock;
3295
3296 dolock = rb_ok_to_lock();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003297
Tom Zanussi2d622712009-03-22 03:30:49 -05003298 again:
Steven Rostedt554f7862009-03-11 22:00:13 -04003299 /* might be called in atomic */
3300 preempt_disable();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003301
Steven Rostedt554f7862009-03-11 22:00:13 -04003302 if (!cpumask_test_cpu(cpu, buffer->cpumask))
3303 goto out;
3304
3305 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt8d707e82009-06-16 21:22:48 -04003306 local_irq_save(flags);
3307 if (dolock)
3308 spin_lock(&cpu_buffer->reader_lock);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003309
Steven Rostedt66a8cb92010-03-31 13:21:56 -04003310 event = rb_buffer_peek(cpu_buffer, ts, lost_events);
3311 if (event) {
3312 cpu_buffer->lost_events = 0;
Robert Richter469535a2009-07-30 19:19:18 +02003313 rb_advance_reader(cpu_buffer);
Steven Rostedt66a8cb92010-03-31 13:21:56 -04003314 }
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003315
Steven Rostedt8d707e82009-06-16 21:22:48 -04003316 if (dolock)
3317 spin_unlock(&cpu_buffer->reader_lock);
3318 local_irq_restore(flags);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003319
Steven Rostedt554f7862009-03-11 22:00:13 -04003320 out:
3321 preempt_enable();
3322
Steven Rostedt1b959e12009-09-03 10:12:13 -04003323 if (event && event->type_len == RINGBUF_TYPE_PADDING)
Tom Zanussi2d622712009-03-22 03:30:49 -05003324 goto again;
Tom Zanussi2d622712009-03-22 03:30:49 -05003325
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003326 return event;
3327}
Robert Richterc4f50182008-12-11 16:49:22 +01003328EXPORT_SYMBOL_GPL(ring_buffer_consume);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003329
3330/**
David Miller72c9ddf2010-04-20 15:47:11 -07003331 * ring_buffer_read_prepare - Prepare for a non consuming read of the buffer
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003332 * @buffer: The ring buffer to read from
3333 * @cpu: The cpu buffer to iterate over
3334 *
David Miller72c9ddf2010-04-20 15:47:11 -07003335 * This performs the initial preparations necessary to iterate
3336 * through the buffer. Memory is allocated, buffer recording
3337 * is disabled, and the iterator pointer is returned to the caller.
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003338 *
David Miller72c9ddf2010-04-20 15:47:11 -07003339 * Disabling buffer recordng prevents the reading from being
3340 * corrupted. This is not a consuming read, so a producer is not
3341 * expected.
3342 *
3343 * After a sequence of ring_buffer_read_prepare calls, the user is
3344 * expected to make at least one call to ring_buffer_prepare_sync.
3345 * Afterwards, ring_buffer_read_start is invoked to get things going
3346 * for real.
3347 *
3348 * This overall must be paired with ring_buffer_finish.
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003349 */
3350struct ring_buffer_iter *
David Miller72c9ddf2010-04-20 15:47:11 -07003351ring_buffer_read_prepare(struct ring_buffer *buffer, int cpu)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003352{
3353 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedt8aabee52009-03-12 13:13:49 -04003354 struct ring_buffer_iter *iter;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003355
Rusty Russell9e01c1b2009-01-01 10:12:22 +10303356 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04003357 return NULL;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003358
3359 iter = kmalloc(sizeof(*iter), GFP_KERNEL);
3360 if (!iter)
Steven Rostedt8aabee52009-03-12 13:13:49 -04003361 return NULL;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003362
3363 cpu_buffer = buffer->buffers[cpu];
3364
3365 iter->cpu_buffer = cpu_buffer;
3366
3367 atomic_inc(&cpu_buffer->record_disabled);
David Miller72c9ddf2010-04-20 15:47:11 -07003368
3369 return iter;
3370}
3371EXPORT_SYMBOL_GPL(ring_buffer_read_prepare);
3372
3373/**
3374 * ring_buffer_read_prepare_sync - Synchronize a set of prepare calls
3375 *
3376 * All previously invoked ring_buffer_read_prepare calls to prepare
3377 * iterators will be synchronized. Afterwards, read_buffer_read_start
3378 * calls on those iterators are allowed.
3379 */
3380void
3381ring_buffer_read_prepare_sync(void)
3382{
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003383 synchronize_sched();
David Miller72c9ddf2010-04-20 15:47:11 -07003384}
3385EXPORT_SYMBOL_GPL(ring_buffer_read_prepare_sync);
3386
3387/**
3388 * ring_buffer_read_start - start a non consuming read of the buffer
3389 * @iter: The iterator returned by ring_buffer_read_prepare
3390 *
3391 * This finalizes the startup of an iteration through the buffer.
3392 * The iterator comes from a call to ring_buffer_read_prepare and
3393 * an intervening ring_buffer_read_prepare_sync must have been
3394 * performed.
3395 *
3396 * Must be paired with ring_buffer_finish.
3397 */
3398void
3399ring_buffer_read_start(struct ring_buffer_iter *iter)
3400{
3401 struct ring_buffer_per_cpu *cpu_buffer;
3402 unsigned long flags;
3403
3404 if (!iter)
3405 return;
3406
3407 cpu_buffer = iter->cpu_buffer;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003408
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003409 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01003410 arch_spin_lock(&cpu_buffer->lock);
Steven Rostedt642edba2008-11-12 00:01:26 -05003411 rb_iter_reset(iter);
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01003412 arch_spin_unlock(&cpu_buffer->lock);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003413 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003414}
Robert Richterc4f50182008-12-11 16:49:22 +01003415EXPORT_SYMBOL_GPL(ring_buffer_read_start);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003416
3417/**
3418 * ring_buffer_finish - finish reading the iterator of the buffer
3419 * @iter: The iterator retrieved by ring_buffer_start
3420 *
3421 * This re-enables the recording to the buffer, and frees the
3422 * iterator.
3423 */
3424void
3425ring_buffer_read_finish(struct ring_buffer_iter *iter)
3426{
3427 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
3428
3429 atomic_dec(&cpu_buffer->record_disabled);
3430 kfree(iter);
3431}
Robert Richterc4f50182008-12-11 16:49:22 +01003432EXPORT_SYMBOL_GPL(ring_buffer_read_finish);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003433
3434/**
3435 * ring_buffer_read - read the next item in the ring buffer by the iterator
3436 * @iter: The ring buffer iterator
3437 * @ts: The time stamp of the event read.
3438 *
3439 * This reads the next event in the ring buffer and increments the iterator.
3440 */
3441struct ring_buffer_event *
3442ring_buffer_read(struct ring_buffer_iter *iter, u64 *ts)
3443{
3444 struct ring_buffer_event *event;
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003445 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
3446 unsigned long flags;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003447
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003448 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
Steven Rostedt7e9391c2009-09-03 10:02:09 -04003449 again:
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003450 event = rb_iter_peek(iter, ts);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003451 if (!event)
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003452 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003453
Steven Rostedt7e9391c2009-09-03 10:02:09 -04003454 if (event->type_len == RINGBUF_TYPE_PADDING)
3455 goto again;
3456
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003457 rb_advance_iter(iter);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003458 out:
3459 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003460
3461 return event;
3462}
Robert Richterc4f50182008-12-11 16:49:22 +01003463EXPORT_SYMBOL_GPL(ring_buffer_read);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003464
3465/**
3466 * ring_buffer_size - return the size of the ring buffer (in bytes)
3467 * @buffer: The ring buffer.
3468 */
3469unsigned long ring_buffer_size(struct ring_buffer *buffer)
3470{
3471 return BUF_PAGE_SIZE * buffer->pages;
3472}
Robert Richterc4f50182008-12-11 16:49:22 +01003473EXPORT_SYMBOL_GPL(ring_buffer_size);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003474
3475static void
3476rb_reset_cpu(struct ring_buffer_per_cpu *cpu_buffer)
3477{
Steven Rostedt77ae3652009-03-27 11:00:29 -04003478 rb_head_page_deactivate(cpu_buffer);
3479
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003480 cpu_buffer->head_page
Steven Rostedt3adc54f2009-03-30 15:32:01 -04003481 = list_entry(cpu_buffer->pages, struct buffer_page, list);
Steven Rostedtbf41a152008-10-04 02:00:59 -04003482 local_set(&cpu_buffer->head_page->write, 0);
Steven Rostedt778c55d2009-05-01 18:44:45 -04003483 local_set(&cpu_buffer->head_page->entries, 0);
Steven Rostedtabc9b562008-12-02 15:34:06 -05003484 local_set(&cpu_buffer->head_page->page->commit, 0);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003485
Steven Rostedt6f807ac2008-10-04 02:00:58 -04003486 cpu_buffer->head_page->read = 0;
Steven Rostedtbf41a152008-10-04 02:00:59 -04003487
3488 cpu_buffer->tail_page = cpu_buffer->head_page;
3489 cpu_buffer->commit_page = cpu_buffer->head_page;
3490
3491 INIT_LIST_HEAD(&cpu_buffer->reader_page->list);
3492 local_set(&cpu_buffer->reader_page->write, 0);
Steven Rostedt778c55d2009-05-01 18:44:45 -04003493 local_set(&cpu_buffer->reader_page->entries, 0);
Steven Rostedtabc9b562008-12-02 15:34:06 -05003494 local_set(&cpu_buffer->reader_page->page->commit, 0);
Steven Rostedt6f807ac2008-10-04 02:00:58 -04003495 cpu_buffer->reader_page->read = 0;
Steven Rostedtd7690412008-10-01 00:29:53 -04003496
Steven Rostedt77ae3652009-03-27 11:00:29 -04003497 local_set(&cpu_buffer->commit_overrun, 0);
3498 local_set(&cpu_buffer->overrun, 0);
Steven Rostedte4906ef2009-04-30 20:49:44 -04003499 local_set(&cpu_buffer->entries, 0);
Steven Rostedtfa743952009-06-16 12:37:57 -04003500 local_set(&cpu_buffer->committing, 0);
3501 local_set(&cpu_buffer->commits, 0);
Steven Rostedt77ae3652009-03-27 11:00:29 -04003502 cpu_buffer->read = 0;
Steven Rostedt69507c02009-01-21 18:45:57 -05003503
3504 cpu_buffer->write_stamp = 0;
3505 cpu_buffer->read_stamp = 0;
Steven Rostedt77ae3652009-03-27 11:00:29 -04003506
Steven Rostedt66a8cb92010-03-31 13:21:56 -04003507 cpu_buffer->lost_events = 0;
3508 cpu_buffer->last_overrun = 0;
3509
Steven Rostedt77ae3652009-03-27 11:00:29 -04003510 rb_head_page_activate(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003511}
3512
3513/**
3514 * ring_buffer_reset_cpu - reset a ring buffer per CPU buffer
3515 * @buffer: The ring buffer to reset a per cpu buffer of
3516 * @cpu: The CPU buffer to be reset
3517 */
3518void ring_buffer_reset_cpu(struct ring_buffer *buffer, int cpu)
3519{
3520 struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
3521 unsigned long flags;
3522
Rusty Russell9e01c1b2009-01-01 10:12:22 +10303523 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04003524 return;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003525
Steven Rostedt41ede232009-05-01 20:26:54 -04003526 atomic_inc(&cpu_buffer->record_disabled);
3527
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003528 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
3529
Steven Rostedt41b6a952009-09-02 09:59:48 -04003530 if (RB_WARN_ON(cpu_buffer, local_read(&cpu_buffer->committing)))
3531 goto out;
3532
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01003533 arch_spin_lock(&cpu_buffer->lock);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003534
3535 rb_reset_cpu(cpu_buffer);
3536
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01003537 arch_spin_unlock(&cpu_buffer->lock);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003538
Steven Rostedt41b6a952009-09-02 09:59:48 -04003539 out:
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003540 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
Steven Rostedt41ede232009-05-01 20:26:54 -04003541
3542 atomic_dec(&cpu_buffer->record_disabled);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003543}
Robert Richterc4f50182008-12-11 16:49:22 +01003544EXPORT_SYMBOL_GPL(ring_buffer_reset_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003545
3546/**
3547 * ring_buffer_reset - reset a ring buffer
3548 * @buffer: The ring buffer to reset all cpu buffers
3549 */
3550void ring_buffer_reset(struct ring_buffer *buffer)
3551{
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003552 int cpu;
3553
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003554 for_each_buffer_cpu(buffer, cpu)
Steven Rostedtd7690412008-10-01 00:29:53 -04003555 ring_buffer_reset_cpu(buffer, cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003556}
Robert Richterc4f50182008-12-11 16:49:22 +01003557EXPORT_SYMBOL_GPL(ring_buffer_reset);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003558
3559/**
3560 * rind_buffer_empty - is the ring buffer empty?
3561 * @buffer: The ring buffer to test
3562 */
3563int ring_buffer_empty(struct ring_buffer *buffer)
3564{
3565 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedtd4788202009-06-17 00:39:43 -04003566 unsigned long flags;
Steven Rostedt8d707e82009-06-16 21:22:48 -04003567 int dolock;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003568 int cpu;
Steven Rostedtd4788202009-06-17 00:39:43 -04003569 int ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003570
Steven Rostedt8d707e82009-06-16 21:22:48 -04003571 dolock = rb_ok_to_lock();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003572
3573 /* yes this is racy, but if you don't like the race, lock the buffer */
3574 for_each_buffer_cpu(buffer, cpu) {
3575 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt8d707e82009-06-16 21:22:48 -04003576 local_irq_save(flags);
3577 if (dolock)
3578 spin_lock(&cpu_buffer->reader_lock);
Steven Rostedtd4788202009-06-17 00:39:43 -04003579 ret = rb_per_cpu_empty(cpu_buffer);
Steven Rostedt8d707e82009-06-16 21:22:48 -04003580 if (dolock)
3581 spin_unlock(&cpu_buffer->reader_lock);
3582 local_irq_restore(flags);
3583
Steven Rostedtd4788202009-06-17 00:39:43 -04003584 if (!ret)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003585 return 0;
3586 }
Steven Rostedt554f7862009-03-11 22:00:13 -04003587
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003588 return 1;
3589}
Robert Richterc4f50182008-12-11 16:49:22 +01003590EXPORT_SYMBOL_GPL(ring_buffer_empty);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003591
3592/**
3593 * ring_buffer_empty_cpu - is a cpu buffer of a ring buffer empty?
3594 * @buffer: The ring buffer
3595 * @cpu: The CPU buffer to test
3596 */
3597int ring_buffer_empty_cpu(struct ring_buffer *buffer, int cpu)
3598{
3599 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedtd4788202009-06-17 00:39:43 -04003600 unsigned long flags;
Steven Rostedt8d707e82009-06-16 21:22:48 -04003601 int dolock;
Steven Rostedt8aabee52009-03-12 13:13:49 -04003602 int ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003603
Rusty Russell9e01c1b2009-01-01 10:12:22 +10303604 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04003605 return 1;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003606
Steven Rostedt8d707e82009-06-16 21:22:48 -04003607 dolock = rb_ok_to_lock();
Steven Rostedt554f7862009-03-11 22:00:13 -04003608
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003609 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt8d707e82009-06-16 21:22:48 -04003610 local_irq_save(flags);
3611 if (dolock)
3612 spin_lock(&cpu_buffer->reader_lock);
Steven Rostedt554f7862009-03-11 22:00:13 -04003613 ret = rb_per_cpu_empty(cpu_buffer);
Steven Rostedt8d707e82009-06-16 21:22:48 -04003614 if (dolock)
3615 spin_unlock(&cpu_buffer->reader_lock);
3616 local_irq_restore(flags);
Steven Rostedt554f7862009-03-11 22:00:13 -04003617
3618 return ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003619}
Robert Richterc4f50182008-12-11 16:49:22 +01003620EXPORT_SYMBOL_GPL(ring_buffer_empty_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003621
Steven Rostedt85bac322009-09-04 14:24:40 -04003622#ifdef CONFIG_RING_BUFFER_ALLOW_SWAP
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003623/**
3624 * ring_buffer_swap_cpu - swap a CPU buffer between two ring buffers
3625 * @buffer_a: One buffer to swap with
3626 * @buffer_b: The other buffer to swap with
3627 *
3628 * This function is useful for tracers that want to take a "snapshot"
3629 * of a CPU buffer and has another back up buffer lying around.
3630 * it is expected that the tracer handles the cpu buffer not being
3631 * used at the moment.
3632 */
3633int ring_buffer_swap_cpu(struct ring_buffer *buffer_a,
3634 struct ring_buffer *buffer_b, int cpu)
3635{
3636 struct ring_buffer_per_cpu *cpu_buffer_a;
3637 struct ring_buffer_per_cpu *cpu_buffer_b;
Steven Rostedt554f7862009-03-11 22:00:13 -04003638 int ret = -EINVAL;
3639
Rusty Russell9e01c1b2009-01-01 10:12:22 +10303640 if (!cpumask_test_cpu(cpu, buffer_a->cpumask) ||
3641 !cpumask_test_cpu(cpu, buffer_b->cpumask))
Steven Rostedt554f7862009-03-11 22:00:13 -04003642 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003643
3644 /* At least make sure the two buffers are somewhat the same */
Lai Jiangshan6d102bc2008-12-17 17:48:23 +08003645 if (buffer_a->pages != buffer_b->pages)
Steven Rostedt554f7862009-03-11 22:00:13 -04003646 goto out;
3647
3648 ret = -EAGAIN;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003649
Steven Rostedt97b17ef2009-01-21 15:24:56 -05003650 if (ring_buffer_flags != RB_BUFFERS_ON)
Steven Rostedt554f7862009-03-11 22:00:13 -04003651 goto out;
Steven Rostedt97b17ef2009-01-21 15:24:56 -05003652
3653 if (atomic_read(&buffer_a->record_disabled))
Steven Rostedt554f7862009-03-11 22:00:13 -04003654 goto out;
Steven Rostedt97b17ef2009-01-21 15:24:56 -05003655
3656 if (atomic_read(&buffer_b->record_disabled))
Steven Rostedt554f7862009-03-11 22:00:13 -04003657 goto out;
Steven Rostedt97b17ef2009-01-21 15:24:56 -05003658
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003659 cpu_buffer_a = buffer_a->buffers[cpu];
3660 cpu_buffer_b = buffer_b->buffers[cpu];
3661
Steven Rostedt97b17ef2009-01-21 15:24:56 -05003662 if (atomic_read(&cpu_buffer_a->record_disabled))
Steven Rostedt554f7862009-03-11 22:00:13 -04003663 goto out;
Steven Rostedt97b17ef2009-01-21 15:24:56 -05003664
3665 if (atomic_read(&cpu_buffer_b->record_disabled))
Steven Rostedt554f7862009-03-11 22:00:13 -04003666 goto out;
Steven Rostedt97b17ef2009-01-21 15:24:56 -05003667
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003668 /*
3669 * We can't do a synchronize_sched here because this
3670 * function can be called in atomic context.
3671 * Normally this will be called from the same CPU as cpu.
3672 * If not it's up to the caller to protect this.
3673 */
3674 atomic_inc(&cpu_buffer_a->record_disabled);
3675 atomic_inc(&cpu_buffer_b->record_disabled);
3676
Steven Rostedt98277992009-09-02 10:56:15 -04003677 ret = -EBUSY;
3678 if (local_read(&cpu_buffer_a->committing))
3679 goto out_dec;
3680 if (local_read(&cpu_buffer_b->committing))
3681 goto out_dec;
3682
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003683 buffer_a->buffers[cpu] = cpu_buffer_b;
3684 buffer_b->buffers[cpu] = cpu_buffer_a;
3685
3686 cpu_buffer_b->buffer = buffer_a;
3687 cpu_buffer_a->buffer = buffer_b;
3688
Steven Rostedt98277992009-09-02 10:56:15 -04003689 ret = 0;
3690
3691out_dec:
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003692 atomic_dec(&cpu_buffer_a->record_disabled);
3693 atomic_dec(&cpu_buffer_b->record_disabled);
Steven Rostedt554f7862009-03-11 22:00:13 -04003694out:
Steven Rostedt554f7862009-03-11 22:00:13 -04003695 return ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003696}
Robert Richterc4f50182008-12-11 16:49:22 +01003697EXPORT_SYMBOL_GPL(ring_buffer_swap_cpu);
Steven Rostedt85bac322009-09-04 14:24:40 -04003698#endif /* CONFIG_RING_BUFFER_ALLOW_SWAP */
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003699
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003700/**
3701 * ring_buffer_alloc_read_page - allocate a page to read from buffer
3702 * @buffer: the buffer to allocate for.
3703 *
3704 * This function is used in conjunction with ring_buffer_read_page.
3705 * When reading a full page from the ring buffer, these functions
3706 * can be used to speed up the process. The calling function should
3707 * allocate a few pages first with this function. Then when it
3708 * needs to get pages from the ring buffer, it passes the result
3709 * of this function into ring_buffer_read_page, which will swap
3710 * the page that was allocated, with the read page of the buffer.
3711 *
3712 * Returns:
3713 * The page allocated, or NULL on error.
3714 */
3715void *ring_buffer_alloc_read_page(struct ring_buffer *buffer)
3716{
Steven Rostedt044fa782008-12-02 23:50:03 -05003717 struct buffer_data_page *bpage;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003718 unsigned long addr;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003719
3720 addr = __get_free_page(GFP_KERNEL);
3721 if (!addr)
3722 return NULL;
3723
Steven Rostedt044fa782008-12-02 23:50:03 -05003724 bpage = (void *)addr;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003725
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003726 rb_init_page(bpage);
3727
Steven Rostedt044fa782008-12-02 23:50:03 -05003728 return bpage;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003729}
Steven Rostedtd6ce96d2009-05-05 01:15:24 -04003730EXPORT_SYMBOL_GPL(ring_buffer_alloc_read_page);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003731
3732/**
3733 * ring_buffer_free_read_page - free an allocated read page
3734 * @buffer: the buffer the page was allocate for
3735 * @data: the page to free
3736 *
3737 * Free a page allocated from ring_buffer_alloc_read_page.
3738 */
3739void ring_buffer_free_read_page(struct ring_buffer *buffer, void *data)
3740{
3741 free_page((unsigned long)data);
3742}
Steven Rostedtd6ce96d2009-05-05 01:15:24 -04003743EXPORT_SYMBOL_GPL(ring_buffer_free_read_page);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003744
3745/**
3746 * ring_buffer_read_page - extract a page from the ring buffer
3747 * @buffer: buffer to extract from
3748 * @data_page: the page to use allocated from ring_buffer_alloc_read_page
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003749 * @len: amount to extract
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003750 * @cpu: the cpu of the buffer to extract
3751 * @full: should the extraction only happen when the page is full.
3752 *
3753 * This function will pull out a page from the ring buffer and consume it.
3754 * @data_page must be the address of the variable that was returned
3755 * from ring_buffer_alloc_read_page. This is because the page might be used
3756 * to swap with a page in the ring buffer.
3757 *
3758 * for example:
Lai Jiangshanb85fa012009-02-09 14:21:14 +08003759 * rpage = ring_buffer_alloc_read_page(buffer);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003760 * if (!rpage)
3761 * return error;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003762 * ret = ring_buffer_read_page(buffer, &rpage, len, cpu, 0);
Lai Jiangshan667d2412009-02-09 14:21:17 +08003763 * if (ret >= 0)
3764 * process_page(rpage, ret);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003765 *
3766 * When @full is set, the function will not return true unless
3767 * the writer is off the reader page.
3768 *
3769 * Note: it is up to the calling functions to handle sleeps and wakeups.
3770 * The ring buffer can be used anywhere in the kernel and can not
3771 * blindly call wake_up. The layer that uses the ring buffer must be
3772 * responsible for that.
3773 *
3774 * Returns:
Lai Jiangshan667d2412009-02-09 14:21:17 +08003775 * >=0 if data has been transferred, returns the offset of consumed data.
3776 * <0 if no data has been transferred.
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003777 */
3778int ring_buffer_read_page(struct ring_buffer *buffer,
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003779 void **data_page, size_t len, int cpu, int full)
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003780{
3781 struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
3782 struct ring_buffer_event *event;
Steven Rostedt044fa782008-12-02 23:50:03 -05003783 struct buffer_data_page *bpage;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003784 struct buffer_page *reader;
Steven Rostedtff0ff842010-03-31 22:11:42 -04003785 unsigned long missed_events;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003786 unsigned long flags;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003787 unsigned int commit;
Lai Jiangshan667d2412009-02-09 14:21:17 +08003788 unsigned int read;
Steven Rostedt4f3640f2009-03-03 23:52:42 -05003789 u64 save_timestamp;
Lai Jiangshan667d2412009-02-09 14:21:17 +08003790 int ret = -1;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003791
Steven Rostedt554f7862009-03-11 22:00:13 -04003792 if (!cpumask_test_cpu(cpu, buffer->cpumask))
3793 goto out;
3794
Steven Rostedt474d32b2009-03-03 19:51:40 -05003795 /*
3796 * If len is not big enough to hold the page header, then
3797 * we can not copy anything.
3798 */
3799 if (len <= BUF_PAGE_HDR_SIZE)
Steven Rostedt554f7862009-03-11 22:00:13 -04003800 goto out;
Steven Rostedt474d32b2009-03-03 19:51:40 -05003801
3802 len -= BUF_PAGE_HDR_SIZE;
3803
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003804 if (!data_page)
Steven Rostedt554f7862009-03-11 22:00:13 -04003805 goto out;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003806
Steven Rostedt044fa782008-12-02 23:50:03 -05003807 bpage = *data_page;
3808 if (!bpage)
Steven Rostedt554f7862009-03-11 22:00:13 -04003809 goto out;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003810
3811 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
3812
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003813 reader = rb_get_reader_page(cpu_buffer);
3814 if (!reader)
Steven Rostedt554f7862009-03-11 22:00:13 -04003815 goto out_unlock;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003816
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003817 event = rb_reader_event(cpu_buffer);
Lai Jiangshan667d2412009-02-09 14:21:17 +08003818
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003819 read = reader->read;
3820 commit = rb_page_commit(reader);
3821
Steven Rostedt66a8cb92010-03-31 13:21:56 -04003822 /* Check if any events were dropped */
Steven Rostedtff0ff842010-03-31 22:11:42 -04003823 missed_events = cpu_buffer->lost_events;
Steven Rostedt66a8cb92010-03-31 13:21:56 -04003824
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003825 /*
Steven Rostedt474d32b2009-03-03 19:51:40 -05003826 * If this page has been partially read or
3827 * if len is not big enough to read the rest of the page or
3828 * a writer is still on the page, then
3829 * we must copy the data from the page to the buffer.
3830 * Otherwise, we can simply swap the page with the one passed in.
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003831 */
Steven Rostedt474d32b2009-03-03 19:51:40 -05003832 if (read || (len < (commit - read)) ||
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003833 cpu_buffer->reader_page == cpu_buffer->commit_page) {
Lai Jiangshan667d2412009-02-09 14:21:17 +08003834 struct buffer_data_page *rpage = cpu_buffer->reader_page->page;
Steven Rostedt474d32b2009-03-03 19:51:40 -05003835 unsigned int rpos = read;
3836 unsigned int pos = 0;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003837 unsigned int size;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003838
3839 if (full)
Steven Rostedt554f7862009-03-11 22:00:13 -04003840 goto out_unlock;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003841
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003842 if (len > (commit - read))
3843 len = (commit - read);
3844
Steven Rostedt69d1b832010-10-07 18:18:05 -04003845 /* Always keep the time extend and data together */
3846 size = rb_event_ts_length(event);
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003847
3848 if (len < size)
Steven Rostedt554f7862009-03-11 22:00:13 -04003849 goto out_unlock;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003850
Steven Rostedt4f3640f2009-03-03 23:52:42 -05003851 /* save the current timestamp, since the user will need it */
3852 save_timestamp = cpu_buffer->read_stamp;
3853
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003854 /* Need to copy one event at a time */
3855 do {
David Sharpe1e35922010-12-22 16:38:24 -08003856 /* We need the size of one event, because
3857 * rb_advance_reader only advances by one event,
3858 * whereas rb_event_ts_length may include the size of
3859 * one or two events.
3860 * We have already ensured there's enough space if this
3861 * is a time extend. */
3862 size = rb_event_length(event);
Steven Rostedt474d32b2009-03-03 19:51:40 -05003863 memcpy(bpage->data + pos, rpage->data + rpos, size);
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003864
3865 len -= size;
3866
3867 rb_advance_reader(cpu_buffer);
Steven Rostedt474d32b2009-03-03 19:51:40 -05003868 rpos = reader->read;
3869 pos += size;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003870
Huang Ying18fab912010-07-28 14:14:01 +08003871 if (rpos >= commit)
3872 break;
3873
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003874 event = rb_reader_event(cpu_buffer);
Steven Rostedt69d1b832010-10-07 18:18:05 -04003875 /* Always keep the time extend and data together */
3876 size = rb_event_ts_length(event);
David Sharpe1e35922010-12-22 16:38:24 -08003877 } while (len >= size);
Lai Jiangshan667d2412009-02-09 14:21:17 +08003878
3879 /* update bpage */
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003880 local_set(&bpage->commit, pos);
Steven Rostedt4f3640f2009-03-03 23:52:42 -05003881 bpage->time_stamp = save_timestamp;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003882
Steven Rostedt474d32b2009-03-03 19:51:40 -05003883 /* we copied everything to the beginning */
3884 read = 0;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003885 } else {
Steven Rostedtafbab762009-05-01 19:40:05 -04003886 /* update the entry counter */
Steven Rostedt77ae3652009-03-27 11:00:29 -04003887 cpu_buffer->read += rb_page_entries(reader);
Steven Rostedtafbab762009-05-01 19:40:05 -04003888
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003889 /* swap the pages */
Steven Rostedt044fa782008-12-02 23:50:03 -05003890 rb_init_page(bpage);
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003891 bpage = reader->page;
3892 reader->page = *data_page;
3893 local_set(&reader->write, 0);
Steven Rostedt778c55d2009-05-01 18:44:45 -04003894 local_set(&reader->entries, 0);
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003895 reader->read = 0;
Steven Rostedt044fa782008-12-02 23:50:03 -05003896 *data_page = bpage;
Steven Rostedtff0ff842010-03-31 22:11:42 -04003897
3898 /*
3899 * Use the real_end for the data size,
3900 * This gives us a chance to store the lost events
3901 * on the page.
3902 */
3903 if (reader->real_end)
3904 local_set(&bpage->commit, reader->real_end);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003905 }
Lai Jiangshan667d2412009-02-09 14:21:17 +08003906 ret = read;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003907
Steven Rostedt66a8cb92010-03-31 13:21:56 -04003908 cpu_buffer->lost_events = 0;
Steven Rostedt2711ca22010-05-21 13:32:26 -04003909
3910 commit = local_read(&bpage->commit);
Steven Rostedt66a8cb92010-03-31 13:21:56 -04003911 /*
3912 * Set a flag in the commit field if we lost events
3913 */
Steven Rostedtff0ff842010-03-31 22:11:42 -04003914 if (missed_events) {
Steven Rostedtff0ff842010-03-31 22:11:42 -04003915 /* If there is room at the end of the page to save the
3916 * missed events, then record it there.
3917 */
3918 if (BUF_PAGE_SIZE - commit >= sizeof(missed_events)) {
3919 memcpy(&bpage->data[commit], &missed_events,
3920 sizeof(missed_events));
3921 local_add(RB_MISSED_STORED, &bpage->commit);
Steven Rostedt2711ca22010-05-21 13:32:26 -04003922 commit += sizeof(missed_events);
Steven Rostedtff0ff842010-03-31 22:11:42 -04003923 }
Steven Rostedt66a8cb92010-03-31 13:21:56 -04003924 local_add(RB_MISSED_EVENTS, &bpage->commit);
Steven Rostedtff0ff842010-03-31 22:11:42 -04003925 }
Steven Rostedt66a8cb92010-03-31 13:21:56 -04003926
Steven Rostedt2711ca22010-05-21 13:32:26 -04003927 /*
3928 * This page may be off to user land. Zero it out here.
3929 */
3930 if (commit < BUF_PAGE_SIZE)
3931 memset(&bpage->data[commit], 0, BUF_PAGE_SIZE - commit);
3932
Steven Rostedt554f7862009-03-11 22:00:13 -04003933 out_unlock:
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003934 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
3935
Steven Rostedt554f7862009-03-11 22:00:13 -04003936 out:
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003937 return ret;
3938}
Steven Rostedtd6ce96d2009-05-05 01:15:24 -04003939EXPORT_SYMBOL_GPL(ring_buffer_read_page);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003940
Paul Mundt1155de42009-06-25 14:30:12 +09003941#ifdef CONFIG_TRACING
Steven Rostedta3583242008-11-11 15:01:42 -05003942static ssize_t
3943rb_simple_read(struct file *filp, char __user *ubuf,
3944 size_t cnt, loff_t *ppos)
3945{
Hannes Eder5e398412009-02-10 19:44:34 +01003946 unsigned long *p = filp->private_data;
Steven Rostedta3583242008-11-11 15:01:42 -05003947 char buf[64];
3948 int r;
3949
Steven Rostedt033601a2008-11-21 12:41:55 -05003950 if (test_bit(RB_BUFFERS_DISABLED_BIT, p))
3951 r = sprintf(buf, "permanently disabled\n");
3952 else
3953 r = sprintf(buf, "%d\n", test_bit(RB_BUFFERS_ON_BIT, p));
Steven Rostedta3583242008-11-11 15:01:42 -05003954
3955 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
3956}
3957
3958static ssize_t
3959rb_simple_write(struct file *filp, const char __user *ubuf,
3960 size_t cnt, loff_t *ppos)
3961{
Hannes Eder5e398412009-02-10 19:44:34 +01003962 unsigned long *p = filp->private_data;
Steven Rostedta3583242008-11-11 15:01:42 -05003963 char buf[64];
Hannes Eder5e398412009-02-10 19:44:34 +01003964 unsigned long val;
Steven Rostedta3583242008-11-11 15:01:42 -05003965 int ret;
3966
3967 if (cnt >= sizeof(buf))
3968 return -EINVAL;
3969
3970 if (copy_from_user(&buf, ubuf, cnt))
3971 return -EFAULT;
3972
3973 buf[cnt] = 0;
3974
3975 ret = strict_strtoul(buf, 10, &val);
3976 if (ret < 0)
3977 return ret;
3978
Steven Rostedt033601a2008-11-21 12:41:55 -05003979 if (val)
3980 set_bit(RB_BUFFERS_ON_BIT, p);
3981 else
3982 clear_bit(RB_BUFFERS_ON_BIT, p);
Steven Rostedta3583242008-11-11 15:01:42 -05003983
3984 (*ppos)++;
3985
3986 return cnt;
3987}
3988
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003989static const struct file_operations rb_simple_fops = {
Steven Rostedta3583242008-11-11 15:01:42 -05003990 .open = tracing_open_generic,
3991 .read = rb_simple_read,
3992 .write = rb_simple_write,
Arnd Bergmann6038f372010-08-15 18:52:59 +02003993 .llseek = default_llseek,
Steven Rostedta3583242008-11-11 15:01:42 -05003994};
3995
3996
3997static __init int rb_init_debugfs(void)
3998{
3999 struct dentry *d_tracer;
Steven Rostedta3583242008-11-11 15:01:42 -05004000
4001 d_tracer = tracing_init_dentry();
4002
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004003 trace_create_file("tracing_on", 0644, d_tracer,
4004 &ring_buffer_flags, &rb_simple_fops);
Steven Rostedta3583242008-11-11 15:01:42 -05004005
4006 return 0;
4007}
4008
4009fs_initcall(rb_init_debugfs);
Paul Mundt1155de42009-06-25 14:30:12 +09004010#endif
Steven Rostedt554f7862009-03-11 22:00:13 -04004011
Steven Rostedt59222ef2009-03-12 11:46:03 -04004012#ifdef CONFIG_HOTPLUG_CPU
Frederic Weisbecker09c9e842009-03-21 04:33:36 +01004013static int rb_cpu_notify(struct notifier_block *self,
4014 unsigned long action, void *hcpu)
Steven Rostedt554f7862009-03-11 22:00:13 -04004015{
4016 struct ring_buffer *buffer =
4017 container_of(self, struct ring_buffer, cpu_notify);
4018 long cpu = (long)hcpu;
4019
4020 switch (action) {
4021 case CPU_UP_PREPARE:
4022 case CPU_UP_PREPARE_FROZEN:
Rusty Russell3f237a72009-06-12 21:15:30 +09304023 if (cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt554f7862009-03-11 22:00:13 -04004024 return NOTIFY_OK;
4025
4026 buffer->buffers[cpu] =
4027 rb_allocate_cpu_buffer(buffer, cpu);
4028 if (!buffer->buffers[cpu]) {
4029 WARN(1, "failed to allocate ring buffer on CPU %ld\n",
4030 cpu);
4031 return NOTIFY_OK;
4032 }
4033 smp_wmb();
Rusty Russell3f237a72009-06-12 21:15:30 +09304034 cpumask_set_cpu(cpu, buffer->cpumask);
Steven Rostedt554f7862009-03-11 22:00:13 -04004035 break;
4036 case CPU_DOWN_PREPARE:
4037 case CPU_DOWN_PREPARE_FROZEN:
4038 /*
4039 * Do nothing.
4040 * If we were to free the buffer, then the user would
4041 * lose any trace that was in the buffer.
4042 */
4043 break;
4044 default:
4045 break;
4046 }
4047 return NOTIFY_OK;
4048}
4049#endif