blob: 9a0f9bf6a37b37295a5e4bf6a52d03b3472905fa [file] [log] [blame]
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001/*
2 * Generic ring buffer
3 *
4 * Copyright (C) 2008 Steven Rostedt <srostedt@redhat.com>
5 */
6#include <linux/ring_buffer.h>
Ingo Molnar14131f22009-02-26 18:47:11 +01007#include <linux/trace_clock.h>
Steven Rostedt78d904b2009-02-05 18:43:07 -05008#include <linux/ftrace_irq.h>
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04009#include <linux/spinlock.h>
10#include <linux/debugfs.h>
11#include <linux/uaccess.h>
Steven Rostedta81bd802009-02-06 01:45:16 -050012#include <linux/hardirq.h>
Vegard Nossum1744a212009-02-28 08:29:44 +010013#include <linux/kmemcheck.h>
Steven Rostedt7a8e76a2008-09-29 23:02:38 -040014#include <linux/module.h>
15#include <linux/percpu.h>
16#include <linux/mutex.h>
Steven Rostedt7a8e76a2008-09-29 23:02:38 -040017#include <linux/init.h>
18#include <linux/hash.h>
19#include <linux/list.h>
Steven Rostedt554f7862009-03-11 22:00:13 -040020#include <linux/cpu.h>
Steven Rostedt7a8e76a2008-09-29 23:02:38 -040021#include <linux/fs.h>
22
Christoph Lameter79615762010-01-05 15:34:50 +090023#include <asm/local.h>
Steven Rostedt182e9f52008-11-03 23:15:56 -050024#include "trace.h"
25
Steven Rostedt033601a2008-11-21 12:41:55 -050026/*
Steven Rostedtd1b182a2009-04-15 16:53:47 -040027 * The ring buffer header is special. We must manually up keep it.
28 */
29int ring_buffer_print_entry_header(struct trace_seq *s)
30{
31 int ret;
32
Lai Jiangshan334d4162009-04-24 11:27:05 +080033 ret = trace_seq_printf(s, "# compressed entry header\n");
34 ret = trace_seq_printf(s, "\ttype_len : 5 bits\n");
Steven Rostedtd1b182a2009-04-15 16:53:47 -040035 ret = trace_seq_printf(s, "\ttime_delta : 27 bits\n");
36 ret = trace_seq_printf(s, "\tarray : 32 bits\n");
37 ret = trace_seq_printf(s, "\n");
38 ret = trace_seq_printf(s, "\tpadding : type == %d\n",
39 RINGBUF_TYPE_PADDING);
40 ret = trace_seq_printf(s, "\ttime_extend : type == %d\n",
41 RINGBUF_TYPE_TIME_EXTEND);
Lai Jiangshan334d4162009-04-24 11:27:05 +080042 ret = trace_seq_printf(s, "\tdata max type_len == %d\n",
43 RINGBUF_TYPE_DATA_TYPE_LEN_MAX);
Steven Rostedtd1b182a2009-04-15 16:53:47 -040044
45 return ret;
46}
47
48/*
Steven Rostedt5cc98542009-03-12 22:24:17 -040049 * The ring buffer is made up of a list of pages. A separate list of pages is
50 * allocated for each CPU. A writer may only write to a buffer that is
51 * associated with the CPU it is currently executing on. A reader may read
52 * from any per cpu buffer.
53 *
54 * The reader is special. For each per cpu buffer, the reader has its own
55 * reader page. When a reader has read the entire reader page, this reader
56 * page is swapped with another page in the ring buffer.
57 *
58 * Now, as long as the writer is off the reader page, the reader can do what
59 * ever it wants with that page. The writer will never write to that page
60 * again (as long as it is out of the ring buffer).
61 *
62 * Here's some silly ASCII art.
63 *
64 * +------+
65 * |reader| RING BUFFER
66 * |page |
67 * +------+ +---+ +---+ +---+
68 * | |-->| |-->| |
69 * +---+ +---+ +---+
70 * ^ |
71 * | |
72 * +---------------+
73 *
74 *
75 * +------+
76 * |reader| RING BUFFER
77 * |page |------------------v
78 * +------+ +---+ +---+ +---+
79 * | |-->| |-->| |
80 * +---+ +---+ +---+
81 * ^ |
82 * | |
83 * +---------------+
84 *
85 *
86 * +------+
87 * |reader| RING BUFFER
88 * |page |------------------v
89 * +------+ +---+ +---+ +---+
90 * ^ | |-->| |-->| |
91 * | +---+ +---+ +---+
92 * | |
93 * | |
94 * +------------------------------+
95 *
96 *
97 * +------+
98 * |buffer| RING BUFFER
99 * |page |------------------v
100 * +------+ +---+ +---+ +---+
101 * ^ | | | |-->| |
102 * | New +---+ +---+ +---+
103 * | Reader------^ |
104 * | page |
105 * +------------------------------+
106 *
107 *
108 * After we make this swap, the reader can hand this page off to the splice
109 * code and be done with it. It can even allocate a new page if it needs to
110 * and swap that into the ring buffer.
111 *
112 * We will be using cmpxchg soon to make all this lockless.
113 *
114 */
115
116/*
Steven Rostedt033601a2008-11-21 12:41:55 -0500117 * A fast way to enable or disable all ring buffers is to
118 * call tracing_on or tracing_off. Turning off the ring buffers
119 * prevents all ring buffers from being recorded to.
120 * Turning this switch on, makes it OK to write to the
121 * ring buffer, if the ring buffer is enabled itself.
122 *
123 * There's three layers that must be on in order to write
124 * to the ring buffer.
125 *
126 * 1) This global flag must be set.
127 * 2) The ring buffer must be enabled for recording.
128 * 3) The per cpu buffer must be enabled for recording.
129 *
130 * In case of an anomaly, this global flag has a bit set that
131 * will permantly disable all ring buffers.
132 */
133
134/*
135 * Global flag to disable all recording to ring buffers
136 * This has two bits: ON, DISABLED
137 *
138 * ON DISABLED
139 * ---- ----------
140 * 0 0 : ring buffers are off
141 * 1 0 : ring buffers are on
142 * X 1 : ring buffers are permanently disabled
143 */
144
145enum {
146 RB_BUFFERS_ON_BIT = 0,
147 RB_BUFFERS_DISABLED_BIT = 1,
148};
149
150enum {
151 RB_BUFFERS_ON = 1 << RB_BUFFERS_ON_BIT,
152 RB_BUFFERS_DISABLED = 1 << RB_BUFFERS_DISABLED_BIT,
153};
154
Hannes Eder5e398412009-02-10 19:44:34 +0100155static unsigned long ring_buffer_flags __read_mostly = RB_BUFFERS_ON;
Steven Rostedta3583242008-11-11 15:01:42 -0500156
Steven Rostedt474d32b2009-03-03 19:51:40 -0500157#define BUF_PAGE_HDR_SIZE offsetof(struct buffer_data_page, data)
158
Steven Rostedta3583242008-11-11 15:01:42 -0500159/**
160 * tracing_on - enable all tracing buffers
161 *
162 * This function enables all tracing buffers that may have been
163 * disabled with tracing_off.
164 */
165void tracing_on(void)
166{
Steven Rostedt033601a2008-11-21 12:41:55 -0500167 set_bit(RB_BUFFERS_ON_BIT, &ring_buffer_flags);
Steven Rostedta3583242008-11-11 15:01:42 -0500168}
Robert Richterc4f50182008-12-11 16:49:22 +0100169EXPORT_SYMBOL_GPL(tracing_on);
Steven Rostedta3583242008-11-11 15:01:42 -0500170
171/**
172 * tracing_off - turn off all tracing buffers
173 *
174 * This function stops all tracing buffers from recording data.
175 * It does not disable any overhead the tracers themselves may
176 * be causing. This function simply causes all recording to
177 * the ring buffers to fail.
178 */
179void tracing_off(void)
180{
Steven Rostedt033601a2008-11-21 12:41:55 -0500181 clear_bit(RB_BUFFERS_ON_BIT, &ring_buffer_flags);
182}
Robert Richterc4f50182008-12-11 16:49:22 +0100183EXPORT_SYMBOL_GPL(tracing_off);
Steven Rostedt033601a2008-11-21 12:41:55 -0500184
185/**
186 * tracing_off_permanent - permanently disable ring buffers
187 *
188 * This function, once called, will disable all ring buffers
Wenji Huangc3706f02009-02-10 01:03:18 -0500189 * permanently.
Steven Rostedt033601a2008-11-21 12:41:55 -0500190 */
191void tracing_off_permanent(void)
192{
193 set_bit(RB_BUFFERS_DISABLED_BIT, &ring_buffer_flags);
Steven Rostedta3583242008-11-11 15:01:42 -0500194}
195
Steven Rostedt988ae9d2009-02-14 19:17:02 -0500196/**
197 * tracing_is_on - show state of ring buffers enabled
198 */
199int tracing_is_on(void)
200{
201 return ring_buffer_flags == RB_BUFFERS_ON;
202}
203EXPORT_SYMBOL_GPL(tracing_is_on);
204
Steven Rostedte3d6bf02009-03-03 13:53:07 -0500205#define RB_EVNT_HDR_SIZE (offsetof(struct ring_buffer_event, array))
Andrew Morton67d34722009-01-09 12:27:09 -0800206#define RB_ALIGNMENT 4U
Lai Jiangshan334d4162009-04-24 11:27:05 +0800207#define RB_MAX_SMALL_DATA (RB_ALIGNMENT * RINGBUF_TYPE_DATA_TYPE_LEN_MAX)
Steven Rostedtc7b09302009-06-11 11:12:00 -0400208#define RB_EVNT_MIN_SIZE 8U /* two 32bit words */
Lai Jiangshan334d4162009-04-24 11:27:05 +0800209
Steven Rostedt22710482010-03-18 17:54:19 -0400210#if !defined(CONFIG_64BIT) || defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)
211# define RB_FORCE_8BYTE_ALIGNMENT 0
212# define RB_ARCH_ALIGNMENT RB_ALIGNMENT
213#else
214# define RB_FORCE_8BYTE_ALIGNMENT 1
215# define RB_ARCH_ALIGNMENT 8U
216#endif
217
Lai Jiangshan334d4162009-04-24 11:27:05 +0800218/* define RINGBUF_TYPE_DATA for 'case RINGBUF_TYPE_DATA:' */
219#define RINGBUF_TYPE_DATA 0 ... RINGBUF_TYPE_DATA_TYPE_LEN_MAX
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400220
221enum {
222 RB_LEN_TIME_EXTEND = 8,
223 RB_LEN_TIME_STAMP = 16,
224};
225
Tom Zanussi2d622712009-03-22 03:30:49 -0500226static inline int rb_null_event(struct ring_buffer_event *event)
227{
Steven Rostedta1863c22009-09-03 10:23:58 -0400228 return event->type_len == RINGBUF_TYPE_PADDING && !event->time_delta;
Tom Zanussi2d622712009-03-22 03:30:49 -0500229}
230
231static void rb_event_set_padding(struct ring_buffer_event *event)
232{
Steven Rostedta1863c22009-09-03 10:23:58 -0400233 /* padding has a NULL time_delta */
Lai Jiangshan334d4162009-04-24 11:27:05 +0800234 event->type_len = RINGBUF_TYPE_PADDING;
Tom Zanussi2d622712009-03-22 03:30:49 -0500235 event->time_delta = 0;
236}
237
Tom Zanussi2d622712009-03-22 03:30:49 -0500238static unsigned
239rb_event_data_length(struct ring_buffer_event *event)
240{
241 unsigned length;
242
Lai Jiangshan334d4162009-04-24 11:27:05 +0800243 if (event->type_len)
244 length = event->type_len * RB_ALIGNMENT;
Tom Zanussi2d622712009-03-22 03:30:49 -0500245 else
246 length = event->array[0];
247 return length + RB_EVNT_HDR_SIZE;
248}
249
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400250/* inline for ring buffer fast paths */
Andrew Morton34a148b2009-01-09 12:27:09 -0800251static unsigned
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400252rb_event_length(struct ring_buffer_event *event)
253{
Lai Jiangshan334d4162009-04-24 11:27:05 +0800254 switch (event->type_len) {
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400255 case RINGBUF_TYPE_PADDING:
Tom Zanussi2d622712009-03-22 03:30:49 -0500256 if (rb_null_event(event))
257 /* undefined */
258 return -1;
Lai Jiangshan334d4162009-04-24 11:27:05 +0800259 return event->array[0] + RB_EVNT_HDR_SIZE;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400260
261 case RINGBUF_TYPE_TIME_EXTEND:
262 return RB_LEN_TIME_EXTEND;
263
264 case RINGBUF_TYPE_TIME_STAMP:
265 return RB_LEN_TIME_STAMP;
266
267 case RINGBUF_TYPE_DATA:
Tom Zanussi2d622712009-03-22 03:30:49 -0500268 return rb_event_data_length(event);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400269 default:
270 BUG();
271 }
272 /* not hit */
273 return 0;
274}
275
276/**
277 * ring_buffer_event_length - return the length of the event
278 * @event: the event to get the length of
279 */
280unsigned ring_buffer_event_length(struct ring_buffer_event *event)
281{
Robert Richter465634a2009-01-07 15:32:11 +0100282 unsigned length = rb_event_length(event);
Lai Jiangshan334d4162009-04-24 11:27:05 +0800283 if (event->type_len > RINGBUF_TYPE_DATA_TYPE_LEN_MAX)
Robert Richter465634a2009-01-07 15:32:11 +0100284 return length;
285 length -= RB_EVNT_HDR_SIZE;
286 if (length > RB_MAX_SMALL_DATA + sizeof(event->array[0]))
287 length -= sizeof(event->array[0]);
288 return length;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400289}
Robert Richterc4f50182008-12-11 16:49:22 +0100290EXPORT_SYMBOL_GPL(ring_buffer_event_length);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400291
292/* inline for ring buffer fast paths */
Andrew Morton34a148b2009-01-09 12:27:09 -0800293static void *
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400294rb_event_data(struct ring_buffer_event *event)
295{
Lai Jiangshan334d4162009-04-24 11:27:05 +0800296 BUG_ON(event->type_len > RINGBUF_TYPE_DATA_TYPE_LEN_MAX);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400297 /* If length is in len field, then array[0] has the data */
Lai Jiangshan334d4162009-04-24 11:27:05 +0800298 if (event->type_len)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400299 return (void *)&event->array[0];
300 /* Otherwise length is in array[0] and array[1] has the data */
301 return (void *)&event->array[1];
302}
303
304/**
305 * ring_buffer_event_data - return the data of the event
306 * @event: the event to get the data from
307 */
308void *ring_buffer_event_data(struct ring_buffer_event *event)
309{
310 return rb_event_data(event);
311}
Robert Richterc4f50182008-12-11 16:49:22 +0100312EXPORT_SYMBOL_GPL(ring_buffer_event_data);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400313
314#define for_each_buffer_cpu(buffer, cpu) \
Rusty Russell9e01c1b2009-01-01 10:12:22 +1030315 for_each_cpu(cpu, buffer->cpumask)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400316
317#define TS_SHIFT 27
318#define TS_MASK ((1ULL << TS_SHIFT) - 1)
319#define TS_DELTA_TEST (~TS_MASK)
320
Steven Rostedtabc9b562008-12-02 15:34:06 -0500321struct buffer_data_page {
Steven Rostedte4c2ce82008-10-01 11:14:54 -0400322 u64 time_stamp; /* page time stamp */
Wenji Huangc3706f02009-02-10 01:03:18 -0500323 local_t commit; /* write committed index */
Steven Rostedtabc9b562008-12-02 15:34:06 -0500324 unsigned char data[]; /* data of buffer page */
325};
326
Steven Rostedt77ae3652009-03-27 11:00:29 -0400327/*
328 * Note, the buffer_page list must be first. The buffer pages
329 * are allocated in cache lines, which means that each buffer
330 * page will be at the beginning of a cache line, and thus
331 * the least significant bits will be zero. We use this to
332 * add flags in the list struct pointers, to make the ring buffer
333 * lockless.
334 */
Steven Rostedtabc9b562008-12-02 15:34:06 -0500335struct buffer_page {
Steven Rostedt778c55d2009-05-01 18:44:45 -0400336 struct list_head list; /* list of buffer pages */
Steven Rostedtabc9b562008-12-02 15:34:06 -0500337 local_t write; /* index for next write */
Steven Rostedt6f807ac2008-10-04 02:00:58 -0400338 unsigned read; /* index for next read */
Steven Rostedt778c55d2009-05-01 18:44:45 -0400339 local_t entries; /* entries on this page */
Steven Rostedtabc9b562008-12-02 15:34:06 -0500340 struct buffer_data_page *page; /* Actual data page */
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400341};
342
Steven Rostedt77ae3652009-03-27 11:00:29 -0400343/*
344 * The buffer page counters, write and entries, must be reset
345 * atomically when crossing page boundaries. To synchronize this
346 * update, two counters are inserted into the number. One is
347 * the actual counter for the write position or count on the page.
348 *
349 * The other is a counter of updaters. Before an update happens
350 * the update partition of the counter is incremented. This will
351 * allow the updater to update the counter atomically.
352 *
353 * The counter is 20 bits, and the state data is 12.
354 */
355#define RB_WRITE_MASK 0xfffff
356#define RB_WRITE_INTCNT (1 << 20)
357
Steven Rostedt044fa782008-12-02 23:50:03 -0500358static void rb_init_page(struct buffer_data_page *bpage)
Steven Rostedtabc9b562008-12-02 15:34:06 -0500359{
Steven Rostedt044fa782008-12-02 23:50:03 -0500360 local_set(&bpage->commit, 0);
Steven Rostedtabc9b562008-12-02 15:34:06 -0500361}
362
Steven Rostedt474d32b2009-03-03 19:51:40 -0500363/**
364 * ring_buffer_page_len - the size of data on the page.
365 * @page: The page to read
366 *
367 * Returns the amount of data on the page, including buffer page header.
368 */
Steven Rostedtef7a4a12009-03-03 00:27:49 -0500369size_t ring_buffer_page_len(void *page)
370{
Steven Rostedt474d32b2009-03-03 19:51:40 -0500371 return local_read(&((struct buffer_data_page *)page)->commit)
372 + BUF_PAGE_HDR_SIZE;
Steven Rostedtef7a4a12009-03-03 00:27:49 -0500373}
374
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400375/*
Steven Rostedted568292008-09-29 23:02:40 -0400376 * Also stolen from mm/slob.c. Thanks to Mathieu Desnoyers for pointing
377 * this issue out.
378 */
Andrew Morton34a148b2009-01-09 12:27:09 -0800379static void free_buffer_page(struct buffer_page *bpage)
Steven Rostedted568292008-09-29 23:02:40 -0400380{
Andrew Morton34a148b2009-01-09 12:27:09 -0800381 free_page((unsigned long)bpage->page);
Steven Rostedte4c2ce82008-10-01 11:14:54 -0400382 kfree(bpage);
Steven Rostedted568292008-09-29 23:02:40 -0400383}
384
385/*
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400386 * We need to fit the time_stamp delta into 27 bits.
387 */
388static inline int test_time_stamp(u64 delta)
389{
390 if (delta & TS_DELTA_TEST)
391 return 1;
392 return 0;
393}
394
Steven Rostedt474d32b2009-03-03 19:51:40 -0500395#define BUF_PAGE_SIZE (PAGE_SIZE - BUF_PAGE_HDR_SIZE)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400396
Steven Rostedtbe957c42009-05-11 14:42:53 -0400397/* Max payload is BUF_PAGE_SIZE - header (8bytes) */
398#define BUF_MAX_DATA_SIZE (BUF_PAGE_SIZE - (sizeof(u32) * 2))
399
Steven Rostedtea05b572009-06-03 09:30:10 -0400400/* Max number of timestamps that can fit on a page */
401#define RB_TIMESTAMPS_PER_PAGE (BUF_PAGE_SIZE / RB_LEN_TIME_STAMP)
402
Steven Rostedtd1b182a2009-04-15 16:53:47 -0400403int ring_buffer_print_page_header(struct trace_seq *s)
404{
405 struct buffer_data_page field;
406 int ret;
407
408 ret = trace_seq_printf(s, "\tfield: u64 timestamp;\t"
Tom Zanussi26a50742009-10-06 01:09:50 -0500409 "offset:0;\tsize:%u;\tsigned:%u;\n",
410 (unsigned int)sizeof(field.time_stamp),
411 (unsigned int)is_signed_type(u64));
Steven Rostedtd1b182a2009-04-15 16:53:47 -0400412
413 ret = trace_seq_printf(s, "\tfield: local_t commit;\t"
Tom Zanussi26a50742009-10-06 01:09:50 -0500414 "offset:%u;\tsize:%u;\tsigned:%u;\n",
Steven Rostedtd1b182a2009-04-15 16:53:47 -0400415 (unsigned int)offsetof(typeof(field), commit),
Tom Zanussi26a50742009-10-06 01:09:50 -0500416 (unsigned int)sizeof(field.commit),
417 (unsigned int)is_signed_type(long));
Steven Rostedtd1b182a2009-04-15 16:53:47 -0400418
419 ret = trace_seq_printf(s, "\tfield: char data;\t"
Tom Zanussi26a50742009-10-06 01:09:50 -0500420 "offset:%u;\tsize:%u;\tsigned:%u;\n",
Steven Rostedtd1b182a2009-04-15 16:53:47 -0400421 (unsigned int)offsetof(typeof(field), data),
Tom Zanussi26a50742009-10-06 01:09:50 -0500422 (unsigned int)BUF_PAGE_SIZE,
423 (unsigned int)is_signed_type(char));
Steven Rostedtd1b182a2009-04-15 16:53:47 -0400424
425 return ret;
426}
427
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400428/*
429 * head_page == tail_page && head == tail then buffer is empty.
430 */
431struct ring_buffer_per_cpu {
432 int cpu;
433 struct ring_buffer *buffer;
Steven Rostedt77ae3652009-03-27 11:00:29 -0400434 spinlock_t reader_lock; /* serialize readers */
Thomas Gleixner445c8952009-12-02 19:49:50 +0100435 arch_spinlock_t lock;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400436 struct lock_class_key lock_key;
Steven Rostedt3adc54f2009-03-30 15:32:01 -0400437 struct list_head *pages;
Steven Rostedt6f807ac2008-10-04 02:00:58 -0400438 struct buffer_page *head_page; /* read from head */
439 struct buffer_page *tail_page; /* write to tail */
Wenji Huangc3706f02009-02-10 01:03:18 -0500440 struct buffer_page *commit_page; /* committed pages */
Steven Rostedtd7690412008-10-01 00:29:53 -0400441 struct buffer_page *reader_page;
Steven Rostedt77ae3652009-03-27 11:00:29 -0400442 local_t commit_overrun;
443 local_t overrun;
Steven Rostedte4906ef2009-04-30 20:49:44 -0400444 local_t entries;
Steven Rostedtfa743952009-06-16 12:37:57 -0400445 local_t committing;
446 local_t commits;
Steven Rostedt77ae3652009-03-27 11:00:29 -0400447 unsigned long read;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400448 u64 write_stamp;
449 u64 read_stamp;
450 atomic_t record_disabled;
451};
452
453struct ring_buffer {
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400454 unsigned pages;
455 unsigned flags;
456 int cpus;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400457 atomic_t record_disabled;
Arnaldo Carvalho de Melo00f62f62009-02-09 17:04:06 -0200458 cpumask_var_t cpumask;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400459
Peter Zijlstra1f8a6a12009-06-08 18:18:39 +0200460 struct lock_class_key *reader_lock_key;
461
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400462 struct mutex mutex;
463
464 struct ring_buffer_per_cpu **buffers;
Steven Rostedt554f7862009-03-11 22:00:13 -0400465
Steven Rostedt59222ef2009-03-12 11:46:03 -0400466#ifdef CONFIG_HOTPLUG_CPU
Steven Rostedt554f7862009-03-11 22:00:13 -0400467 struct notifier_block cpu_notify;
468#endif
Steven Rostedt37886f62009-03-17 17:22:06 -0400469 u64 (*clock)(void);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400470};
471
472struct ring_buffer_iter {
473 struct ring_buffer_per_cpu *cpu_buffer;
474 unsigned long head;
475 struct buffer_page *head_page;
Steven Rostedt492a74f2010-01-25 15:17:47 -0500476 struct buffer_page *cache_reader_page;
477 unsigned long cache_read;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400478 u64 read_stamp;
479};
480
Steven Rostedtf536aaf2008-11-10 23:07:30 -0500481/* buffer may be either ring_buffer or ring_buffer_per_cpu */
Steven Rostedt077c5402009-09-03 19:53:46 -0400482#define RB_WARN_ON(b, cond) \
483 ({ \
484 int _____ret = unlikely(cond); \
485 if (_____ret) { \
486 if (__same_type(*(b), struct ring_buffer_per_cpu)) { \
487 struct ring_buffer_per_cpu *__b = \
488 (void *)b; \
489 atomic_inc(&__b->buffer->record_disabled); \
490 } else \
491 atomic_inc(&b->record_disabled); \
492 WARN_ON(1); \
493 } \
494 _____ret; \
Steven Rostedt3e89c7b2008-11-11 15:28:41 -0500495 })
Steven Rostedtf536aaf2008-11-10 23:07:30 -0500496
Steven Rostedt37886f62009-03-17 17:22:06 -0400497/* Up this if you want to test the TIME_EXTENTS and normalization */
498#define DEBUG_SHIFT 0
499
Jiri Olsa6d3f1e12009-10-23 19:36:19 -0400500static inline u64 rb_time_stamp(struct ring_buffer *buffer)
Steven Rostedt88eb0122009-05-11 16:28:23 -0400501{
502 /* shift to debug/test normalization and TIME_EXTENTS */
503 return buffer->clock() << DEBUG_SHIFT;
504}
505
Steven Rostedt37886f62009-03-17 17:22:06 -0400506u64 ring_buffer_time_stamp(struct ring_buffer *buffer, int cpu)
507{
508 u64 time;
509
510 preempt_disable_notrace();
Jiri Olsa6d3f1e12009-10-23 19:36:19 -0400511 time = rb_time_stamp(buffer);
Steven Rostedt37886f62009-03-17 17:22:06 -0400512 preempt_enable_no_resched_notrace();
513
514 return time;
515}
516EXPORT_SYMBOL_GPL(ring_buffer_time_stamp);
517
518void ring_buffer_normalize_time_stamp(struct ring_buffer *buffer,
519 int cpu, u64 *ts)
520{
521 /* Just stupid testing the normalize function and deltas */
522 *ts >>= DEBUG_SHIFT;
523}
524EXPORT_SYMBOL_GPL(ring_buffer_normalize_time_stamp);
525
Steven Rostedt77ae3652009-03-27 11:00:29 -0400526/*
527 * Making the ring buffer lockless makes things tricky.
528 * Although writes only happen on the CPU that they are on,
529 * and they only need to worry about interrupts. Reads can
530 * happen on any CPU.
531 *
532 * The reader page is always off the ring buffer, but when the
533 * reader finishes with a page, it needs to swap its page with
534 * a new one from the buffer. The reader needs to take from
535 * the head (writes go to the tail). But if a writer is in overwrite
536 * mode and wraps, it must push the head page forward.
537 *
538 * Here lies the problem.
539 *
540 * The reader must be careful to replace only the head page, and
541 * not another one. As described at the top of the file in the
542 * ASCII art, the reader sets its old page to point to the next
543 * page after head. It then sets the page after head to point to
544 * the old reader page. But if the writer moves the head page
545 * during this operation, the reader could end up with the tail.
546 *
547 * We use cmpxchg to help prevent this race. We also do something
548 * special with the page before head. We set the LSB to 1.
549 *
550 * When the writer must push the page forward, it will clear the
551 * bit that points to the head page, move the head, and then set
552 * the bit that points to the new head page.
553 *
554 * We also don't want an interrupt coming in and moving the head
555 * page on another writer. Thus we use the second LSB to catch
556 * that too. Thus:
557 *
558 * head->list->prev->next bit 1 bit 0
559 * ------- -------
560 * Normal page 0 0
561 * Points to head page 0 1
562 * New head page 1 0
563 *
564 * Note we can not trust the prev pointer of the head page, because:
565 *
566 * +----+ +-----+ +-----+
567 * | |------>| T |---X--->| N |
568 * | |<------| | | |
569 * +----+ +-----+ +-----+
570 * ^ ^ |
571 * | +-----+ | |
572 * +----------| R |----------+ |
573 * | |<-----------+
574 * +-----+
575 *
576 * Key: ---X--> HEAD flag set in pointer
577 * T Tail page
578 * R Reader page
579 * N Next page
580 *
581 * (see __rb_reserve_next() to see where this happens)
582 *
583 * What the above shows is that the reader just swapped out
584 * the reader page with a page in the buffer, but before it
585 * could make the new header point back to the new page added
586 * it was preempted by a writer. The writer moved forward onto
587 * the new page added by the reader and is about to move forward
588 * again.
589 *
590 * You can see, it is legitimate for the previous pointer of
591 * the head (or any page) not to point back to itself. But only
592 * temporarially.
593 */
594
595#define RB_PAGE_NORMAL 0UL
596#define RB_PAGE_HEAD 1UL
597#define RB_PAGE_UPDATE 2UL
598
599
600#define RB_FLAG_MASK 3UL
601
602/* PAGE_MOVED is not part of the mask */
603#define RB_PAGE_MOVED 4UL
604
605/*
606 * rb_list_head - remove any bit
607 */
608static struct list_head *rb_list_head(struct list_head *list)
609{
610 unsigned long val = (unsigned long)list;
611
612 return (struct list_head *)(val & ~RB_FLAG_MASK);
613}
614
615/*
Jiri Olsa6d3f1e12009-10-23 19:36:19 -0400616 * rb_is_head_page - test if the given page is the head page
Steven Rostedt77ae3652009-03-27 11:00:29 -0400617 *
618 * Because the reader may move the head_page pointer, we can
619 * not trust what the head page is (it may be pointing to
620 * the reader page). But if the next page is a header page,
621 * its flags will be non zero.
622 */
623static int inline
624rb_is_head_page(struct ring_buffer_per_cpu *cpu_buffer,
625 struct buffer_page *page, struct list_head *list)
626{
627 unsigned long val;
628
629 val = (unsigned long)list->next;
630
631 if ((val & ~RB_FLAG_MASK) != (unsigned long)&page->list)
632 return RB_PAGE_MOVED;
633
634 return val & RB_FLAG_MASK;
635}
636
637/*
638 * rb_is_reader_page
639 *
640 * The unique thing about the reader page, is that, if the
641 * writer is ever on it, the previous pointer never points
642 * back to the reader page.
643 */
644static int rb_is_reader_page(struct buffer_page *page)
645{
646 struct list_head *list = page->list.prev;
647
648 return rb_list_head(list->next) != &page->list;
649}
650
651/*
652 * rb_set_list_to_head - set a list_head to be pointing to head.
653 */
654static void rb_set_list_to_head(struct ring_buffer_per_cpu *cpu_buffer,
655 struct list_head *list)
656{
657 unsigned long *ptr;
658
659 ptr = (unsigned long *)&list->next;
660 *ptr |= RB_PAGE_HEAD;
661 *ptr &= ~RB_PAGE_UPDATE;
662}
663
664/*
665 * rb_head_page_activate - sets up head page
666 */
667static void rb_head_page_activate(struct ring_buffer_per_cpu *cpu_buffer)
668{
669 struct buffer_page *head;
670
671 head = cpu_buffer->head_page;
672 if (!head)
673 return;
674
675 /*
676 * Set the previous list pointer to have the HEAD flag.
677 */
678 rb_set_list_to_head(cpu_buffer, head->list.prev);
679}
680
681static void rb_list_head_clear(struct list_head *list)
682{
683 unsigned long *ptr = (unsigned long *)&list->next;
684
685 *ptr &= ~RB_FLAG_MASK;
686}
687
688/*
689 * rb_head_page_dactivate - clears head page ptr (for free list)
690 */
691static void
692rb_head_page_deactivate(struct ring_buffer_per_cpu *cpu_buffer)
693{
694 struct list_head *hd;
695
696 /* Go through the whole list and clear any pointers found. */
697 rb_list_head_clear(cpu_buffer->pages);
698
699 list_for_each(hd, cpu_buffer->pages)
700 rb_list_head_clear(hd);
701}
702
703static int rb_head_page_set(struct ring_buffer_per_cpu *cpu_buffer,
704 struct buffer_page *head,
705 struct buffer_page *prev,
706 int old_flag, int new_flag)
707{
708 struct list_head *list;
709 unsigned long val = (unsigned long)&head->list;
710 unsigned long ret;
711
712 list = &prev->list;
713
714 val &= ~RB_FLAG_MASK;
715
Steven Rostedt08a40812009-09-14 09:31:35 -0400716 ret = cmpxchg((unsigned long *)&list->next,
717 val | old_flag, val | new_flag);
Steven Rostedt77ae3652009-03-27 11:00:29 -0400718
719 /* check if the reader took the page */
720 if ((ret & ~RB_FLAG_MASK) != val)
721 return RB_PAGE_MOVED;
722
723 return ret & RB_FLAG_MASK;
724}
725
726static int rb_head_page_set_update(struct ring_buffer_per_cpu *cpu_buffer,
727 struct buffer_page *head,
728 struct buffer_page *prev,
729 int old_flag)
730{
731 return rb_head_page_set(cpu_buffer, head, prev,
732 old_flag, RB_PAGE_UPDATE);
733}
734
735static int rb_head_page_set_head(struct ring_buffer_per_cpu *cpu_buffer,
736 struct buffer_page *head,
737 struct buffer_page *prev,
738 int old_flag)
739{
740 return rb_head_page_set(cpu_buffer, head, prev,
741 old_flag, RB_PAGE_HEAD);
742}
743
744static int rb_head_page_set_normal(struct ring_buffer_per_cpu *cpu_buffer,
745 struct buffer_page *head,
746 struct buffer_page *prev,
747 int old_flag)
748{
749 return rb_head_page_set(cpu_buffer, head, prev,
750 old_flag, RB_PAGE_NORMAL);
751}
752
753static inline void rb_inc_page(struct ring_buffer_per_cpu *cpu_buffer,
754 struct buffer_page **bpage)
755{
756 struct list_head *p = rb_list_head((*bpage)->list.next);
757
758 *bpage = list_entry(p, struct buffer_page, list);
759}
760
761static struct buffer_page *
762rb_set_head_page(struct ring_buffer_per_cpu *cpu_buffer)
763{
764 struct buffer_page *head;
765 struct buffer_page *page;
766 struct list_head *list;
767 int i;
768
769 if (RB_WARN_ON(cpu_buffer, !cpu_buffer->head_page))
770 return NULL;
771
772 /* sanity check */
773 list = cpu_buffer->pages;
774 if (RB_WARN_ON(cpu_buffer, rb_list_head(list->prev->next) != list))
775 return NULL;
776
777 page = head = cpu_buffer->head_page;
778 /*
779 * It is possible that the writer moves the header behind
780 * where we started, and we miss in one loop.
781 * A second loop should grab the header, but we'll do
782 * three loops just because I'm paranoid.
783 */
784 for (i = 0; i < 3; i++) {
785 do {
786 if (rb_is_head_page(cpu_buffer, page, page->list.prev)) {
787 cpu_buffer->head_page = page;
788 return page;
789 }
790 rb_inc_page(cpu_buffer, &page);
791 } while (page != head);
792 }
793
794 RB_WARN_ON(cpu_buffer, 1);
795
796 return NULL;
797}
798
799static int rb_head_page_replace(struct buffer_page *old,
800 struct buffer_page *new)
801{
802 unsigned long *ptr = (unsigned long *)&old->list.prev->next;
803 unsigned long val;
804 unsigned long ret;
805
806 val = *ptr & ~RB_FLAG_MASK;
807 val |= RB_PAGE_HEAD;
808
Steven Rostedt08a40812009-09-14 09:31:35 -0400809 ret = cmpxchg(ptr, val, (unsigned long)&new->list);
Steven Rostedt77ae3652009-03-27 11:00:29 -0400810
811 return ret == val;
812}
813
814/*
815 * rb_tail_page_update - move the tail page forward
816 *
817 * Returns 1 if moved tail page, 0 if someone else did.
818 */
819static int rb_tail_page_update(struct ring_buffer_per_cpu *cpu_buffer,
820 struct buffer_page *tail_page,
821 struct buffer_page *next_page)
822{
823 struct buffer_page *old_tail;
824 unsigned long old_entries;
825 unsigned long old_write;
826 int ret = 0;
827
828 /*
829 * The tail page now needs to be moved forward.
830 *
831 * We need to reset the tail page, but without messing
832 * with possible erasing of data brought in by interrupts
833 * that have moved the tail page and are currently on it.
834 *
835 * We add a counter to the write field to denote this.
836 */
837 old_write = local_add_return(RB_WRITE_INTCNT, &next_page->write);
838 old_entries = local_add_return(RB_WRITE_INTCNT, &next_page->entries);
839
840 /*
841 * Just make sure we have seen our old_write and synchronize
842 * with any interrupts that come in.
843 */
844 barrier();
845
846 /*
847 * If the tail page is still the same as what we think
848 * it is, then it is up to us to update the tail
849 * pointer.
850 */
851 if (tail_page == cpu_buffer->tail_page) {
852 /* Zero the write counter */
853 unsigned long val = old_write & ~RB_WRITE_MASK;
854 unsigned long eval = old_entries & ~RB_WRITE_MASK;
855
856 /*
857 * This will only succeed if an interrupt did
858 * not come in and change it. In which case, we
859 * do not want to modify it.
Lai Jiangshanda706d82009-07-15 16:27:30 +0800860 *
861 * We add (void) to let the compiler know that we do not care
862 * about the return value of these functions. We use the
863 * cmpxchg to only update if an interrupt did not already
864 * do it for us. If the cmpxchg fails, we don't care.
Steven Rostedt77ae3652009-03-27 11:00:29 -0400865 */
Lai Jiangshanda706d82009-07-15 16:27:30 +0800866 (void)local_cmpxchg(&next_page->write, old_write, val);
867 (void)local_cmpxchg(&next_page->entries, old_entries, eval);
Steven Rostedt77ae3652009-03-27 11:00:29 -0400868
869 /*
870 * No need to worry about races with clearing out the commit.
871 * it only can increment when a commit takes place. But that
872 * only happens in the outer most nested commit.
873 */
874 local_set(&next_page->page->commit, 0);
875
876 old_tail = cmpxchg(&cpu_buffer->tail_page,
877 tail_page, next_page);
878
879 if (old_tail == tail_page)
880 ret = 1;
881 }
882
883 return ret;
884}
885
886static int rb_check_bpage(struct ring_buffer_per_cpu *cpu_buffer,
887 struct buffer_page *bpage)
888{
889 unsigned long val = (unsigned long)bpage;
890
891 if (RB_WARN_ON(cpu_buffer, val & RB_FLAG_MASK))
892 return 1;
893
894 return 0;
895}
896
897/**
898 * rb_check_list - make sure a pointer to a list has the last bits zero
899 */
900static int rb_check_list(struct ring_buffer_per_cpu *cpu_buffer,
901 struct list_head *list)
902{
903 if (RB_WARN_ON(cpu_buffer, rb_list_head(list->prev) != list->prev))
904 return 1;
905 if (RB_WARN_ON(cpu_buffer, rb_list_head(list->next) != list->next))
906 return 1;
907 return 0;
908}
909
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400910/**
911 * check_pages - integrity check of buffer pages
912 * @cpu_buffer: CPU buffer with pages to test
913 *
Wenji Huangc3706f02009-02-10 01:03:18 -0500914 * As a safety measure we check to make sure the data pages have not
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400915 * been corrupted.
916 */
917static int rb_check_pages(struct ring_buffer_per_cpu *cpu_buffer)
918{
Steven Rostedt3adc54f2009-03-30 15:32:01 -0400919 struct list_head *head = cpu_buffer->pages;
Steven Rostedt044fa782008-12-02 23:50:03 -0500920 struct buffer_page *bpage, *tmp;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400921
Steven Rostedt77ae3652009-03-27 11:00:29 -0400922 rb_head_page_deactivate(cpu_buffer);
923
Steven Rostedt3e89c7b2008-11-11 15:28:41 -0500924 if (RB_WARN_ON(cpu_buffer, head->next->prev != head))
925 return -1;
926 if (RB_WARN_ON(cpu_buffer, head->prev->next != head))
927 return -1;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400928
Steven Rostedt77ae3652009-03-27 11:00:29 -0400929 if (rb_check_list(cpu_buffer, head))
930 return -1;
931
Steven Rostedt044fa782008-12-02 23:50:03 -0500932 list_for_each_entry_safe(bpage, tmp, head, list) {
Steven Rostedt3e89c7b2008-11-11 15:28:41 -0500933 if (RB_WARN_ON(cpu_buffer,
Steven Rostedt044fa782008-12-02 23:50:03 -0500934 bpage->list.next->prev != &bpage->list))
Steven Rostedt3e89c7b2008-11-11 15:28:41 -0500935 return -1;
936 if (RB_WARN_ON(cpu_buffer,
Steven Rostedt044fa782008-12-02 23:50:03 -0500937 bpage->list.prev->next != &bpage->list))
Steven Rostedt3e89c7b2008-11-11 15:28:41 -0500938 return -1;
Steven Rostedt77ae3652009-03-27 11:00:29 -0400939 if (rb_check_list(cpu_buffer, &bpage->list))
940 return -1;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400941 }
942
Steven Rostedt77ae3652009-03-27 11:00:29 -0400943 rb_head_page_activate(cpu_buffer);
944
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400945 return 0;
946}
947
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400948static int rb_allocate_pages(struct ring_buffer_per_cpu *cpu_buffer,
949 unsigned nr_pages)
950{
Steven Rostedt044fa782008-12-02 23:50:03 -0500951 struct buffer_page *bpage, *tmp;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400952 unsigned long addr;
953 LIST_HEAD(pages);
954 unsigned i;
955
Steven Rostedt3adc54f2009-03-30 15:32:01 -0400956 WARN_ON(!nr_pages);
957
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400958 for (i = 0; i < nr_pages; i++) {
Steven Rostedt044fa782008-12-02 23:50:03 -0500959 bpage = kzalloc_node(ALIGN(sizeof(*bpage), cache_line_size()),
Steven Rostedtaa1e0e32008-10-02 19:18:09 -0400960 GFP_KERNEL, cpu_to_node(cpu_buffer->cpu));
Steven Rostedt044fa782008-12-02 23:50:03 -0500961 if (!bpage)
Steven Rostedte4c2ce82008-10-01 11:14:54 -0400962 goto free_pages;
Steven Rostedt77ae3652009-03-27 11:00:29 -0400963
964 rb_check_bpage(cpu_buffer, bpage);
965
Steven Rostedt044fa782008-12-02 23:50:03 -0500966 list_add(&bpage->list, &pages);
Steven Rostedte4c2ce82008-10-01 11:14:54 -0400967
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400968 addr = __get_free_page(GFP_KERNEL);
969 if (!addr)
970 goto free_pages;
Steven Rostedt044fa782008-12-02 23:50:03 -0500971 bpage->page = (void *)addr;
972 rb_init_page(bpage->page);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400973 }
974
Steven Rostedt3adc54f2009-03-30 15:32:01 -0400975 /*
976 * The ring buffer page list is a circular list that does not
977 * start and end with a list head. All page list items point to
978 * other pages.
979 */
980 cpu_buffer->pages = pages.next;
981 list_del(&pages);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400982
983 rb_check_pages(cpu_buffer);
984
985 return 0;
986
987 free_pages:
Steven Rostedt044fa782008-12-02 23:50:03 -0500988 list_for_each_entry_safe(bpage, tmp, &pages, list) {
989 list_del_init(&bpage->list);
990 free_buffer_page(bpage);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400991 }
992 return -ENOMEM;
993}
994
995static struct ring_buffer_per_cpu *
996rb_allocate_cpu_buffer(struct ring_buffer *buffer, int cpu)
997{
998 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedt044fa782008-12-02 23:50:03 -0500999 struct buffer_page *bpage;
Steven Rostedtd7690412008-10-01 00:29:53 -04001000 unsigned long addr;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001001 int ret;
1002
1003 cpu_buffer = kzalloc_node(ALIGN(sizeof(*cpu_buffer), cache_line_size()),
1004 GFP_KERNEL, cpu_to_node(cpu));
1005 if (!cpu_buffer)
1006 return NULL;
1007
1008 cpu_buffer->cpu = cpu;
1009 cpu_buffer->buffer = buffer;
Steven Rostedtf83c9d02008-11-11 18:47:44 +01001010 spin_lock_init(&cpu_buffer->reader_lock);
Peter Zijlstra1f8a6a12009-06-08 18:18:39 +02001011 lockdep_set_class(&cpu_buffer->reader_lock, buffer->reader_lock_key);
Thomas Gleixneredc35bd2009-12-03 12:38:57 +01001012 cpu_buffer->lock = (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001013
Steven Rostedt044fa782008-12-02 23:50:03 -05001014 bpage = kzalloc_node(ALIGN(sizeof(*bpage), cache_line_size()),
Steven Rostedte4c2ce82008-10-01 11:14:54 -04001015 GFP_KERNEL, cpu_to_node(cpu));
Steven Rostedt044fa782008-12-02 23:50:03 -05001016 if (!bpage)
Steven Rostedte4c2ce82008-10-01 11:14:54 -04001017 goto fail_free_buffer;
1018
Steven Rostedt77ae3652009-03-27 11:00:29 -04001019 rb_check_bpage(cpu_buffer, bpage);
1020
Steven Rostedt044fa782008-12-02 23:50:03 -05001021 cpu_buffer->reader_page = bpage;
Steven Rostedtd7690412008-10-01 00:29:53 -04001022 addr = __get_free_page(GFP_KERNEL);
1023 if (!addr)
Steven Rostedte4c2ce82008-10-01 11:14:54 -04001024 goto fail_free_reader;
Steven Rostedt044fa782008-12-02 23:50:03 -05001025 bpage->page = (void *)addr;
1026 rb_init_page(bpage->page);
Steven Rostedte4c2ce82008-10-01 11:14:54 -04001027
Steven Rostedtd7690412008-10-01 00:29:53 -04001028 INIT_LIST_HEAD(&cpu_buffer->reader_page->list);
Steven Rostedtd7690412008-10-01 00:29:53 -04001029
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001030 ret = rb_allocate_pages(cpu_buffer, buffer->pages);
1031 if (ret < 0)
Steven Rostedtd7690412008-10-01 00:29:53 -04001032 goto fail_free_reader;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001033
1034 cpu_buffer->head_page
Steven Rostedt3adc54f2009-03-30 15:32:01 -04001035 = list_entry(cpu_buffer->pages, struct buffer_page, list);
Steven Rostedtbf41a152008-10-04 02:00:59 -04001036 cpu_buffer->tail_page = cpu_buffer->commit_page = cpu_buffer->head_page;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001037
Steven Rostedt77ae3652009-03-27 11:00:29 -04001038 rb_head_page_activate(cpu_buffer);
1039
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001040 return cpu_buffer;
1041
Steven Rostedtd7690412008-10-01 00:29:53 -04001042 fail_free_reader:
1043 free_buffer_page(cpu_buffer->reader_page);
1044
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001045 fail_free_buffer:
1046 kfree(cpu_buffer);
1047 return NULL;
1048}
1049
1050static void rb_free_cpu_buffer(struct ring_buffer_per_cpu *cpu_buffer)
1051{
Steven Rostedt3adc54f2009-03-30 15:32:01 -04001052 struct list_head *head = cpu_buffer->pages;
Steven Rostedt044fa782008-12-02 23:50:03 -05001053 struct buffer_page *bpage, *tmp;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001054
Steven Rostedtd7690412008-10-01 00:29:53 -04001055 free_buffer_page(cpu_buffer->reader_page);
1056
Steven Rostedt77ae3652009-03-27 11:00:29 -04001057 rb_head_page_deactivate(cpu_buffer);
1058
Steven Rostedt3adc54f2009-03-30 15:32:01 -04001059 if (head) {
1060 list_for_each_entry_safe(bpage, tmp, head, list) {
1061 list_del_init(&bpage->list);
1062 free_buffer_page(bpage);
1063 }
1064 bpage = list_entry(head, struct buffer_page, list);
Steven Rostedt044fa782008-12-02 23:50:03 -05001065 free_buffer_page(bpage);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001066 }
Steven Rostedt3adc54f2009-03-30 15:32:01 -04001067
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001068 kfree(cpu_buffer);
1069}
1070
Steven Rostedt59222ef2009-03-12 11:46:03 -04001071#ifdef CONFIG_HOTPLUG_CPU
Frederic Weisbecker09c9e842009-03-21 04:33:36 +01001072static int rb_cpu_notify(struct notifier_block *self,
1073 unsigned long action, void *hcpu);
Steven Rostedt554f7862009-03-11 22:00:13 -04001074#endif
1075
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001076/**
1077 * ring_buffer_alloc - allocate a new ring_buffer
Robert Richter68814b52008-11-24 12:24:12 +01001078 * @size: the size in bytes per cpu that is needed.
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001079 * @flags: attributes to set for the ring buffer.
1080 *
1081 * Currently the only flag that is available is the RB_FL_OVERWRITE
1082 * flag. This flag means that the buffer will overwrite old data
1083 * when the buffer wraps. If this flag is not set, the buffer will
1084 * drop data when the tail hits the head.
1085 */
Peter Zijlstra1f8a6a12009-06-08 18:18:39 +02001086struct ring_buffer *__ring_buffer_alloc(unsigned long size, unsigned flags,
1087 struct lock_class_key *key)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001088{
1089 struct ring_buffer *buffer;
1090 int bsize;
1091 int cpu;
1092
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001093 /* keep it in its own cache line */
1094 buffer = kzalloc(ALIGN(sizeof(*buffer), cache_line_size()),
1095 GFP_KERNEL);
1096 if (!buffer)
1097 return NULL;
1098
Rusty Russell9e01c1b2009-01-01 10:12:22 +10301099 if (!alloc_cpumask_var(&buffer->cpumask, GFP_KERNEL))
1100 goto fail_free_buffer;
1101
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001102 buffer->pages = DIV_ROUND_UP(size, BUF_PAGE_SIZE);
1103 buffer->flags = flags;
Steven Rostedt37886f62009-03-17 17:22:06 -04001104 buffer->clock = trace_clock_local;
Peter Zijlstra1f8a6a12009-06-08 18:18:39 +02001105 buffer->reader_lock_key = key;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001106
1107 /* need at least two pages */
Steven Rostedt5f78abe2009-06-17 14:11:10 -04001108 if (buffer->pages < 2)
1109 buffer->pages = 2;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001110
Frederic Weisbecker3bf832c2009-03-19 14:47:33 +01001111 /*
1112 * In case of non-hotplug cpu, if the ring-buffer is allocated
1113 * in early initcall, it will not be notified of secondary cpus.
1114 * In that off case, we need to allocate for all possible cpus.
1115 */
1116#ifdef CONFIG_HOTPLUG_CPU
Steven Rostedt554f7862009-03-11 22:00:13 -04001117 get_online_cpus();
1118 cpumask_copy(buffer->cpumask, cpu_online_mask);
Frederic Weisbecker3bf832c2009-03-19 14:47:33 +01001119#else
1120 cpumask_copy(buffer->cpumask, cpu_possible_mask);
1121#endif
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001122 buffer->cpus = nr_cpu_ids;
1123
1124 bsize = sizeof(void *) * nr_cpu_ids;
1125 buffer->buffers = kzalloc(ALIGN(bsize, cache_line_size()),
1126 GFP_KERNEL);
1127 if (!buffer->buffers)
Rusty Russell9e01c1b2009-01-01 10:12:22 +10301128 goto fail_free_cpumask;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001129
1130 for_each_buffer_cpu(buffer, cpu) {
1131 buffer->buffers[cpu] =
1132 rb_allocate_cpu_buffer(buffer, cpu);
1133 if (!buffer->buffers[cpu])
1134 goto fail_free_buffers;
1135 }
1136
Steven Rostedt59222ef2009-03-12 11:46:03 -04001137#ifdef CONFIG_HOTPLUG_CPU
Steven Rostedt554f7862009-03-11 22:00:13 -04001138 buffer->cpu_notify.notifier_call = rb_cpu_notify;
1139 buffer->cpu_notify.priority = 0;
1140 register_cpu_notifier(&buffer->cpu_notify);
1141#endif
1142
1143 put_online_cpus();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001144 mutex_init(&buffer->mutex);
1145
1146 return buffer;
1147
1148 fail_free_buffers:
1149 for_each_buffer_cpu(buffer, cpu) {
1150 if (buffer->buffers[cpu])
1151 rb_free_cpu_buffer(buffer->buffers[cpu]);
1152 }
1153 kfree(buffer->buffers);
1154
Rusty Russell9e01c1b2009-01-01 10:12:22 +10301155 fail_free_cpumask:
1156 free_cpumask_var(buffer->cpumask);
Steven Rostedt554f7862009-03-11 22:00:13 -04001157 put_online_cpus();
Rusty Russell9e01c1b2009-01-01 10:12:22 +10301158
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001159 fail_free_buffer:
1160 kfree(buffer);
1161 return NULL;
1162}
Peter Zijlstra1f8a6a12009-06-08 18:18:39 +02001163EXPORT_SYMBOL_GPL(__ring_buffer_alloc);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001164
1165/**
1166 * ring_buffer_free - free a ring buffer.
1167 * @buffer: the buffer to free.
1168 */
1169void
1170ring_buffer_free(struct ring_buffer *buffer)
1171{
1172 int cpu;
1173
Steven Rostedt554f7862009-03-11 22:00:13 -04001174 get_online_cpus();
1175
Steven Rostedt59222ef2009-03-12 11:46:03 -04001176#ifdef CONFIG_HOTPLUG_CPU
Steven Rostedt554f7862009-03-11 22:00:13 -04001177 unregister_cpu_notifier(&buffer->cpu_notify);
1178#endif
1179
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001180 for_each_buffer_cpu(buffer, cpu)
1181 rb_free_cpu_buffer(buffer->buffers[cpu]);
1182
Steven Rostedt554f7862009-03-11 22:00:13 -04001183 put_online_cpus();
1184
Eric Dumazetbd3f0222009-08-07 12:49:29 +02001185 kfree(buffer->buffers);
Rusty Russell9e01c1b2009-01-01 10:12:22 +10301186 free_cpumask_var(buffer->cpumask);
1187
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001188 kfree(buffer);
1189}
Robert Richterc4f50182008-12-11 16:49:22 +01001190EXPORT_SYMBOL_GPL(ring_buffer_free);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001191
Steven Rostedt37886f62009-03-17 17:22:06 -04001192void ring_buffer_set_clock(struct ring_buffer *buffer,
1193 u64 (*clock)(void))
1194{
1195 buffer->clock = clock;
1196}
1197
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001198static void rb_reset_cpu(struct ring_buffer_per_cpu *cpu_buffer);
1199
1200static void
1201rb_remove_pages(struct ring_buffer_per_cpu *cpu_buffer, unsigned nr_pages)
1202{
Steven Rostedt044fa782008-12-02 23:50:03 -05001203 struct buffer_page *bpage;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001204 struct list_head *p;
1205 unsigned i;
1206
Lai Jiangshanf7112942009-11-03 19:42:45 +08001207 spin_lock_irq(&cpu_buffer->reader_lock);
Steven Rostedt77ae3652009-03-27 11:00:29 -04001208 rb_head_page_deactivate(cpu_buffer);
1209
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001210 for (i = 0; i < nr_pages; i++) {
Steven Rostedt3adc54f2009-03-30 15:32:01 -04001211 if (RB_WARN_ON(cpu_buffer, list_empty(cpu_buffer->pages)))
Julia Lawall292f60c2010-03-29 17:37:02 +02001212 goto out;
Steven Rostedt3adc54f2009-03-30 15:32:01 -04001213 p = cpu_buffer->pages->next;
Steven Rostedt044fa782008-12-02 23:50:03 -05001214 bpage = list_entry(p, struct buffer_page, list);
1215 list_del_init(&bpage->list);
1216 free_buffer_page(bpage);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001217 }
Steven Rostedt3adc54f2009-03-30 15:32:01 -04001218 if (RB_WARN_ON(cpu_buffer, list_empty(cpu_buffer->pages)))
Julia Lawall292f60c2010-03-29 17:37:02 +02001219 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001220
1221 rb_reset_cpu(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001222 rb_check_pages(cpu_buffer);
1223
Julia Lawall292f60c2010-03-29 17:37:02 +02001224out:
Steven Rostedtdd7f5942009-12-10 23:20:52 -05001225 spin_unlock_irq(&cpu_buffer->reader_lock);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001226}
1227
1228static void
1229rb_insert_pages(struct ring_buffer_per_cpu *cpu_buffer,
1230 struct list_head *pages, unsigned nr_pages)
1231{
Steven Rostedt044fa782008-12-02 23:50:03 -05001232 struct buffer_page *bpage;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001233 struct list_head *p;
1234 unsigned i;
1235
Steven Rostedt77ae3652009-03-27 11:00:29 -04001236 spin_lock_irq(&cpu_buffer->reader_lock);
1237 rb_head_page_deactivate(cpu_buffer);
1238
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001239 for (i = 0; i < nr_pages; i++) {
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05001240 if (RB_WARN_ON(cpu_buffer, list_empty(pages)))
Julia Lawall292f60c2010-03-29 17:37:02 +02001241 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001242 p = pages->next;
Steven Rostedt044fa782008-12-02 23:50:03 -05001243 bpage = list_entry(p, struct buffer_page, list);
1244 list_del_init(&bpage->list);
Steven Rostedt3adc54f2009-03-30 15:32:01 -04001245 list_add_tail(&bpage->list, cpu_buffer->pages);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001246 }
1247 rb_reset_cpu(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001248 rb_check_pages(cpu_buffer);
1249
Julia Lawall292f60c2010-03-29 17:37:02 +02001250out:
Steven Rostedtdd7f5942009-12-10 23:20:52 -05001251 spin_unlock_irq(&cpu_buffer->reader_lock);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001252}
1253
1254/**
1255 * ring_buffer_resize - resize the ring buffer
1256 * @buffer: the buffer to resize.
1257 * @size: the new size.
1258 *
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001259 * Minimum size is 2 * BUF_PAGE_SIZE.
1260 *
1261 * Returns -1 on failure.
1262 */
1263int ring_buffer_resize(struct ring_buffer *buffer, unsigned long size)
1264{
1265 struct ring_buffer_per_cpu *cpu_buffer;
1266 unsigned nr_pages, rm_pages, new_pages;
Steven Rostedt044fa782008-12-02 23:50:03 -05001267 struct buffer_page *bpage, *tmp;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001268 unsigned long buffer_size;
1269 unsigned long addr;
1270 LIST_HEAD(pages);
1271 int i, cpu;
1272
Ingo Molnaree51a1d2008-11-13 14:58:31 +01001273 /*
1274 * Always succeed at resizing a non-existent buffer:
1275 */
1276 if (!buffer)
1277 return size;
1278
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001279 size = DIV_ROUND_UP(size, BUF_PAGE_SIZE);
1280 size *= BUF_PAGE_SIZE;
1281 buffer_size = buffer->pages * BUF_PAGE_SIZE;
1282
1283 /* we need a minimum of two pages */
1284 if (size < BUF_PAGE_SIZE * 2)
1285 size = BUF_PAGE_SIZE * 2;
1286
1287 if (size == buffer_size)
1288 return size;
1289
Steven Rostedt18421012009-12-10 22:54:27 -05001290 atomic_inc(&buffer->record_disabled);
1291
1292 /* Make sure all writers are done with this buffer. */
1293 synchronize_sched();
1294
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001295 mutex_lock(&buffer->mutex);
Steven Rostedt554f7862009-03-11 22:00:13 -04001296 get_online_cpus();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001297
1298 nr_pages = DIV_ROUND_UP(size, BUF_PAGE_SIZE);
1299
1300 if (size < buffer_size) {
1301
1302 /* easy case, just free pages */
Steven Rostedt554f7862009-03-11 22:00:13 -04001303 if (RB_WARN_ON(buffer, nr_pages >= buffer->pages))
1304 goto out_fail;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001305
1306 rm_pages = buffer->pages - nr_pages;
1307
1308 for_each_buffer_cpu(buffer, cpu) {
1309 cpu_buffer = buffer->buffers[cpu];
1310 rb_remove_pages(cpu_buffer, rm_pages);
1311 }
1312 goto out;
1313 }
1314
1315 /*
1316 * This is a bit more difficult. We only want to add pages
1317 * when we can allocate enough for all CPUs. We do this
1318 * by allocating all the pages and storing them on a local
1319 * link list. If we succeed in our allocation, then we
1320 * add these pages to the cpu_buffers. Otherwise we just free
1321 * them all and return -ENOMEM;
1322 */
Steven Rostedt554f7862009-03-11 22:00:13 -04001323 if (RB_WARN_ON(buffer, nr_pages <= buffer->pages))
1324 goto out_fail;
Steven Rostedtf536aaf2008-11-10 23:07:30 -05001325
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001326 new_pages = nr_pages - buffer->pages;
1327
1328 for_each_buffer_cpu(buffer, cpu) {
1329 for (i = 0; i < new_pages; i++) {
Steven Rostedt044fa782008-12-02 23:50:03 -05001330 bpage = kzalloc_node(ALIGN(sizeof(*bpage),
Steven Rostedte4c2ce82008-10-01 11:14:54 -04001331 cache_line_size()),
1332 GFP_KERNEL, cpu_to_node(cpu));
Steven Rostedt044fa782008-12-02 23:50:03 -05001333 if (!bpage)
Steven Rostedte4c2ce82008-10-01 11:14:54 -04001334 goto free_pages;
Steven Rostedt044fa782008-12-02 23:50:03 -05001335 list_add(&bpage->list, &pages);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001336 addr = __get_free_page(GFP_KERNEL);
1337 if (!addr)
1338 goto free_pages;
Steven Rostedt044fa782008-12-02 23:50:03 -05001339 bpage->page = (void *)addr;
1340 rb_init_page(bpage->page);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001341 }
1342 }
1343
1344 for_each_buffer_cpu(buffer, cpu) {
1345 cpu_buffer = buffer->buffers[cpu];
1346 rb_insert_pages(cpu_buffer, &pages, new_pages);
1347 }
1348
Steven Rostedt554f7862009-03-11 22:00:13 -04001349 if (RB_WARN_ON(buffer, !list_empty(&pages)))
1350 goto out_fail;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001351
1352 out:
1353 buffer->pages = nr_pages;
Steven Rostedt554f7862009-03-11 22:00:13 -04001354 put_online_cpus();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001355 mutex_unlock(&buffer->mutex);
1356
Steven Rostedt18421012009-12-10 22:54:27 -05001357 atomic_dec(&buffer->record_disabled);
1358
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001359 return size;
1360
1361 free_pages:
Steven Rostedt044fa782008-12-02 23:50:03 -05001362 list_for_each_entry_safe(bpage, tmp, &pages, list) {
1363 list_del_init(&bpage->list);
1364 free_buffer_page(bpage);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001365 }
Steven Rostedt554f7862009-03-11 22:00:13 -04001366 put_online_cpus();
Vegard Nossum641d2f62008-11-18 19:22:13 +01001367 mutex_unlock(&buffer->mutex);
Steven Rostedt18421012009-12-10 22:54:27 -05001368 atomic_dec(&buffer->record_disabled);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001369 return -ENOMEM;
Steven Rostedt554f7862009-03-11 22:00:13 -04001370
1371 /*
1372 * Something went totally wrong, and we are too paranoid
1373 * to even clean up the mess.
1374 */
1375 out_fail:
1376 put_online_cpus();
1377 mutex_unlock(&buffer->mutex);
Steven Rostedt18421012009-12-10 22:54:27 -05001378 atomic_dec(&buffer->record_disabled);
Steven Rostedt554f7862009-03-11 22:00:13 -04001379 return -1;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001380}
Robert Richterc4f50182008-12-11 16:49:22 +01001381EXPORT_SYMBOL_GPL(ring_buffer_resize);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001382
Steven Rostedt8789a9e2008-12-02 15:34:07 -05001383static inline void *
Steven Rostedt044fa782008-12-02 23:50:03 -05001384__rb_data_page_index(struct buffer_data_page *bpage, unsigned index)
Steven Rostedt8789a9e2008-12-02 15:34:07 -05001385{
Steven Rostedt044fa782008-12-02 23:50:03 -05001386 return bpage->data + index;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05001387}
1388
Steven Rostedt044fa782008-12-02 23:50:03 -05001389static inline void *__rb_page_index(struct buffer_page *bpage, unsigned index)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001390{
Steven Rostedt044fa782008-12-02 23:50:03 -05001391 return bpage->page->data + index;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001392}
1393
1394static inline struct ring_buffer_event *
Steven Rostedtd7690412008-10-01 00:29:53 -04001395rb_reader_event(struct ring_buffer_per_cpu *cpu_buffer)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001396{
Steven Rostedt6f807ac2008-10-04 02:00:58 -04001397 return __rb_page_index(cpu_buffer->reader_page,
1398 cpu_buffer->reader_page->read);
1399}
1400
1401static inline struct ring_buffer_event *
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001402rb_iter_head_event(struct ring_buffer_iter *iter)
1403{
Steven Rostedt6f807ac2008-10-04 02:00:58 -04001404 return __rb_page_index(iter->head_page, iter->head);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001405}
1406
Steven Rostedt77ae3652009-03-27 11:00:29 -04001407static inline unsigned long rb_page_write(struct buffer_page *bpage)
Steven Rostedtbf41a152008-10-04 02:00:59 -04001408{
Steven Rostedt77ae3652009-03-27 11:00:29 -04001409 return local_read(&bpage->write) & RB_WRITE_MASK;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001410}
1411
1412static inline unsigned rb_page_commit(struct buffer_page *bpage)
1413{
Steven Rostedtabc9b562008-12-02 15:34:06 -05001414 return local_read(&bpage->page->commit);
Steven Rostedtbf41a152008-10-04 02:00:59 -04001415}
1416
Steven Rostedt77ae3652009-03-27 11:00:29 -04001417static inline unsigned long rb_page_entries(struct buffer_page *bpage)
1418{
1419 return local_read(&bpage->entries) & RB_WRITE_MASK;
1420}
1421
Steven Rostedtbf41a152008-10-04 02:00:59 -04001422/* Size is determined by what has been commited */
1423static inline unsigned rb_page_size(struct buffer_page *bpage)
1424{
1425 return rb_page_commit(bpage);
1426}
1427
1428static inline unsigned
1429rb_commit_index(struct ring_buffer_per_cpu *cpu_buffer)
1430{
1431 return rb_page_commit(cpu_buffer->commit_page);
1432}
1433
Steven Rostedtbf41a152008-10-04 02:00:59 -04001434static inline unsigned
1435rb_event_index(struct ring_buffer_event *event)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001436{
Steven Rostedtbf41a152008-10-04 02:00:59 -04001437 unsigned long addr = (unsigned long)event;
1438
Steven Rostedt22f470f2009-06-11 09:29:58 -04001439 return (addr & ~PAGE_MASK) - BUF_PAGE_HDR_SIZE;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001440}
1441
Steven Rostedt0f0c85f2009-05-11 16:08:00 -04001442static inline int
Steven Rostedtfa743952009-06-16 12:37:57 -04001443rb_event_is_commit(struct ring_buffer_per_cpu *cpu_buffer,
1444 struct ring_buffer_event *event)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001445{
Steven Rostedtbf41a152008-10-04 02:00:59 -04001446 unsigned long addr = (unsigned long)event;
1447 unsigned long index;
1448
1449 index = rb_event_index(event);
1450 addr &= PAGE_MASK;
1451
1452 return cpu_buffer->commit_page->page == (void *)addr &&
1453 rb_commit_index(cpu_buffer) == index;
1454}
1455
Andrew Morton34a148b2009-01-09 12:27:09 -08001456static void
Steven Rostedtbf41a152008-10-04 02:00:59 -04001457rb_set_commit_to_write(struct ring_buffer_per_cpu *cpu_buffer)
1458{
Steven Rostedt77ae3652009-03-27 11:00:29 -04001459 unsigned long max_count;
1460
Steven Rostedtbf41a152008-10-04 02:00:59 -04001461 /*
1462 * We only race with interrupts and NMIs on this CPU.
1463 * If we own the commit event, then we can commit
1464 * all others that interrupted us, since the interruptions
1465 * are in stack format (they finish before they come
1466 * back to us). This allows us to do a simple loop to
1467 * assign the commit to the tail.
1468 */
Steven Rostedta8ccf1d2008-12-23 11:32:24 -05001469 again:
Steven Rostedt77ae3652009-03-27 11:00:29 -04001470 max_count = cpu_buffer->buffer->pages * 100;
1471
Steven Rostedtbf41a152008-10-04 02:00:59 -04001472 while (cpu_buffer->commit_page != cpu_buffer->tail_page) {
Steven Rostedt77ae3652009-03-27 11:00:29 -04001473 if (RB_WARN_ON(cpu_buffer, !(--max_count)))
1474 return;
1475 if (RB_WARN_ON(cpu_buffer,
1476 rb_is_reader_page(cpu_buffer->tail_page)))
1477 return;
1478 local_set(&cpu_buffer->commit_page->page->commit,
1479 rb_page_write(cpu_buffer->commit_page));
Steven Rostedtbf41a152008-10-04 02:00:59 -04001480 rb_inc_page(cpu_buffer, &cpu_buffer->commit_page);
Steven Rostedtabc9b562008-12-02 15:34:06 -05001481 cpu_buffer->write_stamp =
1482 cpu_buffer->commit_page->page->time_stamp;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001483 /* add barrier to keep gcc from optimizing too much */
1484 barrier();
1485 }
1486 while (rb_commit_index(cpu_buffer) !=
1487 rb_page_write(cpu_buffer->commit_page)) {
Steven Rostedt77ae3652009-03-27 11:00:29 -04001488
1489 local_set(&cpu_buffer->commit_page->page->commit,
1490 rb_page_write(cpu_buffer->commit_page));
1491 RB_WARN_ON(cpu_buffer,
1492 local_read(&cpu_buffer->commit_page->page->commit) &
1493 ~RB_WRITE_MASK);
Steven Rostedtbf41a152008-10-04 02:00:59 -04001494 barrier();
1495 }
Steven Rostedta8ccf1d2008-12-23 11:32:24 -05001496
1497 /* again, keep gcc from optimizing */
1498 barrier();
1499
1500 /*
1501 * If an interrupt came in just after the first while loop
1502 * and pushed the tail page forward, we will be left with
1503 * a dangling commit that will never go forward.
1504 */
1505 if (unlikely(cpu_buffer->commit_page != cpu_buffer->tail_page))
1506 goto again;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001507}
1508
Steven Rostedtd7690412008-10-01 00:29:53 -04001509static void rb_reset_reader_page(struct ring_buffer_per_cpu *cpu_buffer)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001510{
Steven Rostedtabc9b562008-12-02 15:34:06 -05001511 cpu_buffer->read_stamp = cpu_buffer->reader_page->page->time_stamp;
Steven Rostedt6f807ac2008-10-04 02:00:58 -04001512 cpu_buffer->reader_page->read = 0;
Steven Rostedtd7690412008-10-01 00:29:53 -04001513}
1514
Andrew Morton34a148b2009-01-09 12:27:09 -08001515static void rb_inc_iter(struct ring_buffer_iter *iter)
Steven Rostedtd7690412008-10-01 00:29:53 -04001516{
1517 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
1518
1519 /*
1520 * The iterator could be on the reader page (it starts there).
1521 * But the head could have moved, since the reader was
1522 * found. Check for this case and assign the iterator
1523 * to the head page instead of next.
1524 */
1525 if (iter->head_page == cpu_buffer->reader_page)
Steven Rostedt77ae3652009-03-27 11:00:29 -04001526 iter->head_page = rb_set_head_page(cpu_buffer);
Steven Rostedtd7690412008-10-01 00:29:53 -04001527 else
1528 rb_inc_page(cpu_buffer, &iter->head_page);
1529
Steven Rostedtabc9b562008-12-02 15:34:06 -05001530 iter->read_stamp = iter->head_page->page->time_stamp;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001531 iter->head = 0;
1532}
1533
1534/**
1535 * ring_buffer_update_event - update event type and data
1536 * @event: the even to update
1537 * @type: the type of event
1538 * @length: the size of the event field in the ring buffer
1539 *
1540 * Update the type and data fields of the event. The length
1541 * is the actual size that is written to the ring buffer,
1542 * and with this, we can determine what to place into the
1543 * data field.
1544 */
Andrew Morton34a148b2009-01-09 12:27:09 -08001545static void
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001546rb_update_event(struct ring_buffer_event *event,
1547 unsigned type, unsigned length)
1548{
Lai Jiangshan334d4162009-04-24 11:27:05 +08001549 event->type_len = type;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001550
1551 switch (type) {
1552
1553 case RINGBUF_TYPE_PADDING:
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001554 case RINGBUF_TYPE_TIME_EXTEND:
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001555 case RINGBUF_TYPE_TIME_STAMP:
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001556 break;
1557
Lai Jiangshan334d4162009-04-24 11:27:05 +08001558 case 0:
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001559 length -= RB_EVNT_HDR_SIZE;
Steven Rostedt22710482010-03-18 17:54:19 -04001560 if (length > RB_MAX_SMALL_DATA || RB_FORCE_8BYTE_ALIGNMENT)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001561 event->array[0] = length;
Lai Jiangshan334d4162009-04-24 11:27:05 +08001562 else
1563 event->type_len = DIV_ROUND_UP(length, RB_ALIGNMENT);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001564 break;
1565 default:
1566 BUG();
1567 }
1568}
1569
Steven Rostedt77ae3652009-03-27 11:00:29 -04001570/*
1571 * rb_handle_head_page - writer hit the head page
1572 *
1573 * Returns: +1 to retry page
1574 * 0 to continue
1575 * -1 on error
1576 */
1577static int
1578rb_handle_head_page(struct ring_buffer_per_cpu *cpu_buffer,
1579 struct buffer_page *tail_page,
1580 struct buffer_page *next_page)
1581{
1582 struct buffer_page *new_head;
1583 int entries;
1584 int type;
1585 int ret;
1586
1587 entries = rb_page_entries(next_page);
1588
1589 /*
1590 * The hard part is here. We need to move the head
1591 * forward, and protect against both readers on
1592 * other CPUs and writers coming in via interrupts.
1593 */
1594 type = rb_head_page_set_update(cpu_buffer, next_page, tail_page,
1595 RB_PAGE_HEAD);
1596
1597 /*
1598 * type can be one of four:
1599 * NORMAL - an interrupt already moved it for us
1600 * HEAD - we are the first to get here.
1601 * UPDATE - we are the interrupt interrupting
1602 * a current move.
1603 * MOVED - a reader on another CPU moved the next
1604 * pointer to its reader page. Give up
1605 * and try again.
1606 */
1607
1608 switch (type) {
1609 case RB_PAGE_HEAD:
1610 /*
1611 * We changed the head to UPDATE, thus
1612 * it is our responsibility to update
1613 * the counters.
1614 */
1615 local_add(entries, &cpu_buffer->overrun);
1616
1617 /*
1618 * The entries will be zeroed out when we move the
1619 * tail page.
1620 */
1621
1622 /* still more to do */
1623 break;
1624
1625 case RB_PAGE_UPDATE:
1626 /*
1627 * This is an interrupt that interrupt the
1628 * previous update. Still more to do.
1629 */
1630 break;
1631 case RB_PAGE_NORMAL:
1632 /*
1633 * An interrupt came in before the update
1634 * and processed this for us.
1635 * Nothing left to do.
1636 */
1637 return 1;
1638 case RB_PAGE_MOVED:
1639 /*
1640 * The reader is on another CPU and just did
1641 * a swap with our next_page.
1642 * Try again.
1643 */
1644 return 1;
1645 default:
1646 RB_WARN_ON(cpu_buffer, 1); /* WTF??? */
1647 return -1;
1648 }
1649
1650 /*
1651 * Now that we are here, the old head pointer is
1652 * set to UPDATE. This will keep the reader from
1653 * swapping the head page with the reader page.
1654 * The reader (on another CPU) will spin till
1655 * we are finished.
1656 *
1657 * We just need to protect against interrupts
1658 * doing the job. We will set the next pointer
1659 * to HEAD. After that, we set the old pointer
1660 * to NORMAL, but only if it was HEAD before.
1661 * otherwise we are an interrupt, and only
1662 * want the outer most commit to reset it.
1663 */
1664 new_head = next_page;
1665 rb_inc_page(cpu_buffer, &new_head);
1666
1667 ret = rb_head_page_set_head(cpu_buffer, new_head, next_page,
1668 RB_PAGE_NORMAL);
1669
1670 /*
1671 * Valid returns are:
1672 * HEAD - an interrupt came in and already set it.
1673 * NORMAL - One of two things:
1674 * 1) We really set it.
1675 * 2) A bunch of interrupts came in and moved
1676 * the page forward again.
1677 */
1678 switch (ret) {
1679 case RB_PAGE_HEAD:
1680 case RB_PAGE_NORMAL:
1681 /* OK */
1682 break;
1683 default:
1684 RB_WARN_ON(cpu_buffer, 1);
1685 return -1;
1686 }
1687
1688 /*
1689 * It is possible that an interrupt came in,
1690 * set the head up, then more interrupts came in
1691 * and moved it again. When we get back here,
1692 * the page would have been set to NORMAL but we
1693 * just set it back to HEAD.
1694 *
1695 * How do you detect this? Well, if that happened
1696 * the tail page would have moved.
1697 */
1698 if (ret == RB_PAGE_NORMAL) {
1699 /*
1700 * If the tail had moved passed next, then we need
1701 * to reset the pointer.
1702 */
1703 if (cpu_buffer->tail_page != tail_page &&
1704 cpu_buffer->tail_page != next_page)
1705 rb_head_page_set_normal(cpu_buffer, new_head,
1706 next_page,
1707 RB_PAGE_HEAD);
1708 }
1709
1710 /*
1711 * If this was the outer most commit (the one that
1712 * changed the original pointer from HEAD to UPDATE),
1713 * then it is up to us to reset it to NORMAL.
1714 */
1715 if (type == RB_PAGE_HEAD) {
1716 ret = rb_head_page_set_normal(cpu_buffer, next_page,
1717 tail_page,
1718 RB_PAGE_UPDATE);
1719 if (RB_WARN_ON(cpu_buffer,
1720 ret != RB_PAGE_UPDATE))
1721 return -1;
1722 }
1723
1724 return 0;
1725}
1726
Andrew Morton34a148b2009-01-09 12:27:09 -08001727static unsigned rb_calculate_event_length(unsigned length)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001728{
1729 struct ring_buffer_event event; /* Used only for sizeof array */
1730
1731 /* zero length can cause confusions */
1732 if (!length)
1733 length = 1;
1734
Steven Rostedt22710482010-03-18 17:54:19 -04001735 if (length > RB_MAX_SMALL_DATA || RB_FORCE_8BYTE_ALIGNMENT)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001736 length += sizeof(event.array[0]);
1737
1738 length += RB_EVNT_HDR_SIZE;
Steven Rostedt22710482010-03-18 17:54:19 -04001739 length = ALIGN(length, RB_ARCH_ALIGNMENT);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001740
1741 return length;
1742}
1743
Steven Rostedtc7b09302009-06-11 11:12:00 -04001744static inline void
1745rb_reset_tail(struct ring_buffer_per_cpu *cpu_buffer,
1746 struct buffer_page *tail_page,
1747 unsigned long tail, unsigned long length)
1748{
1749 struct ring_buffer_event *event;
1750
1751 /*
1752 * Only the event that crossed the page boundary
1753 * must fill the old tail_page with padding.
1754 */
1755 if (tail >= BUF_PAGE_SIZE) {
1756 local_sub(length, &tail_page->write);
1757 return;
1758 }
1759
1760 event = __rb_page_index(tail_page, tail);
Linus Torvaldsb0b70652009-06-20 10:56:46 -07001761 kmemcheck_annotate_bitfield(event, bitfield);
Steven Rostedtc7b09302009-06-11 11:12:00 -04001762
1763 /*
1764 * If this event is bigger than the minimum size, then
1765 * we need to be careful that we don't subtract the
1766 * write counter enough to allow another writer to slip
1767 * in on this page.
1768 * We put in a discarded commit instead, to make sure
1769 * that this space is not used again.
1770 *
1771 * If we are less than the minimum size, we don't need to
1772 * worry about it.
1773 */
1774 if (tail > (BUF_PAGE_SIZE - RB_EVNT_MIN_SIZE)) {
1775 /* No room for any events */
1776
1777 /* Mark the rest of the page with padding */
1778 rb_event_set_padding(event);
1779
1780 /* Set the write back to the previous setting */
1781 local_sub(length, &tail_page->write);
1782 return;
1783 }
1784
1785 /* Put in a discarded event */
1786 event->array[0] = (BUF_PAGE_SIZE - tail) - RB_EVNT_HDR_SIZE;
1787 event->type_len = RINGBUF_TYPE_PADDING;
1788 /* time delta must be non zero */
1789 event->time_delta = 1;
Steven Rostedtc7b09302009-06-11 11:12:00 -04001790
1791 /* Set write to end of buffer */
1792 length = (tail + length) - BUF_PAGE_SIZE;
1793 local_sub(length, &tail_page->write);
1794}
Steven Rostedt6634ff22009-05-06 15:30:07 -04001795
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001796static struct ring_buffer_event *
Steven Rostedt6634ff22009-05-06 15:30:07 -04001797rb_move_tail(struct ring_buffer_per_cpu *cpu_buffer,
1798 unsigned long length, unsigned long tail,
Steven Rostedt6634ff22009-05-06 15:30:07 -04001799 struct buffer_page *tail_page, u64 *ts)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001800{
Steven Rostedt5a50e332009-11-17 08:43:01 -05001801 struct buffer_page *commit_page = cpu_buffer->commit_page;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001802 struct ring_buffer *buffer = cpu_buffer->buffer;
Steven Rostedt77ae3652009-03-27 11:00:29 -04001803 struct buffer_page *next_page;
1804 int ret;
Steven Rostedtaa20ae82009-05-05 21:16:11 -04001805
1806 next_page = tail_page;
1807
Steven Rostedtaa20ae82009-05-05 21:16:11 -04001808 rb_inc_page(cpu_buffer, &next_page);
1809
Steven Rostedtaa20ae82009-05-05 21:16:11 -04001810 /*
1811 * If for some reason, we had an interrupt storm that made
1812 * it all the way around the buffer, bail, and warn
1813 * about it.
1814 */
1815 if (unlikely(next_page == commit_page)) {
Steven Rostedt77ae3652009-03-27 11:00:29 -04001816 local_inc(&cpu_buffer->commit_overrun);
Steven Rostedtaa20ae82009-05-05 21:16:11 -04001817 goto out_reset;
1818 }
1819
Steven Rostedt77ae3652009-03-27 11:00:29 -04001820 /*
1821 * This is where the fun begins!
1822 *
1823 * We are fighting against races between a reader that
1824 * could be on another CPU trying to swap its reader
1825 * page with the buffer head.
1826 *
1827 * We are also fighting against interrupts coming in and
1828 * moving the head or tail on us as well.
1829 *
1830 * If the next page is the head page then we have filled
1831 * the buffer, unless the commit page is still on the
1832 * reader page.
1833 */
1834 if (rb_is_head_page(cpu_buffer, next_page, &tail_page->list)) {
Steven Rostedtaa20ae82009-05-05 21:16:11 -04001835
Steven Rostedt77ae3652009-03-27 11:00:29 -04001836 /*
1837 * If the commit is not on the reader page, then
1838 * move the header page.
1839 */
1840 if (!rb_is_reader_page(cpu_buffer->commit_page)) {
1841 /*
1842 * If we are not in overwrite mode,
1843 * this is easy, just stop here.
1844 */
1845 if (!(buffer->flags & RB_FL_OVERWRITE))
1846 goto out_reset;
Steven Rostedtaa20ae82009-05-05 21:16:11 -04001847
Steven Rostedt77ae3652009-03-27 11:00:29 -04001848 ret = rb_handle_head_page(cpu_buffer,
1849 tail_page,
1850 next_page);
1851 if (ret < 0)
1852 goto out_reset;
1853 if (ret)
1854 goto out_again;
1855 } else {
1856 /*
1857 * We need to be careful here too. The
1858 * commit page could still be on the reader
1859 * page. We could have a small buffer, and
1860 * have filled up the buffer with events
1861 * from interrupts and such, and wrapped.
1862 *
1863 * Note, if the tail page is also the on the
1864 * reader_page, we let it move out.
1865 */
1866 if (unlikely((cpu_buffer->commit_page !=
1867 cpu_buffer->tail_page) &&
1868 (cpu_buffer->commit_page ==
1869 cpu_buffer->reader_page))) {
1870 local_inc(&cpu_buffer->commit_overrun);
1871 goto out_reset;
1872 }
Steven Rostedtaa20ae82009-05-05 21:16:11 -04001873 }
1874 }
1875
Steven Rostedt77ae3652009-03-27 11:00:29 -04001876 ret = rb_tail_page_update(cpu_buffer, tail_page, next_page);
1877 if (ret) {
1878 /*
1879 * Nested commits always have zero deltas, so
1880 * just reread the time stamp
1881 */
Jiri Olsa6d3f1e12009-10-23 19:36:19 -04001882 *ts = rb_time_stamp(buffer);
Steven Rostedt77ae3652009-03-27 11:00:29 -04001883 next_page->page->time_stamp = *ts;
Steven Rostedtaa20ae82009-05-05 21:16:11 -04001884 }
1885
Steven Rostedt77ae3652009-03-27 11:00:29 -04001886 out_again:
Steven Rostedtaa20ae82009-05-05 21:16:11 -04001887
Steven Rostedt77ae3652009-03-27 11:00:29 -04001888 rb_reset_tail(cpu_buffer, tail_page, tail, length);
Steven Rostedtaa20ae82009-05-05 21:16:11 -04001889
1890 /* fail and let the caller try again */
1891 return ERR_PTR(-EAGAIN);
1892
Steven Rostedt45141d42009-02-12 13:19:48 -05001893 out_reset:
Lai Jiangshan6f3b3442009-01-12 11:06:18 +08001894 /* reset write */
Steven Rostedtc7b09302009-06-11 11:12:00 -04001895 rb_reset_tail(cpu_buffer, tail_page, tail, length);
Lai Jiangshan6f3b3442009-01-12 11:06:18 +08001896
Steven Rostedtbf41a152008-10-04 02:00:59 -04001897 return NULL;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001898}
1899
Steven Rostedt6634ff22009-05-06 15:30:07 -04001900static struct ring_buffer_event *
1901__rb_reserve_next(struct ring_buffer_per_cpu *cpu_buffer,
1902 unsigned type, unsigned long length, u64 *ts)
1903{
Steven Rostedt5a50e332009-11-17 08:43:01 -05001904 struct buffer_page *tail_page;
Steven Rostedt6634ff22009-05-06 15:30:07 -04001905 struct ring_buffer_event *event;
1906 unsigned long tail, write;
1907
Steven Rostedt6634ff22009-05-06 15:30:07 -04001908 tail_page = cpu_buffer->tail_page;
1909 write = local_add_return(length, &tail_page->write);
Steven Rostedt77ae3652009-03-27 11:00:29 -04001910
1911 /* set write to only the index of the write */
1912 write &= RB_WRITE_MASK;
Steven Rostedt6634ff22009-05-06 15:30:07 -04001913 tail = write - length;
1914
1915 /* See if we shot pass the end of this buffer page */
1916 if (write > BUF_PAGE_SIZE)
1917 return rb_move_tail(cpu_buffer, length, tail,
Steven Rostedt5a50e332009-11-17 08:43:01 -05001918 tail_page, ts);
Steven Rostedt6634ff22009-05-06 15:30:07 -04001919
1920 /* We reserved something on the buffer */
1921
Steven Rostedt6634ff22009-05-06 15:30:07 -04001922 event = __rb_page_index(tail_page, tail);
Vegard Nossum1744a212009-02-28 08:29:44 +01001923 kmemcheck_annotate_bitfield(event, bitfield);
Steven Rostedt6634ff22009-05-06 15:30:07 -04001924 rb_update_event(event, type, length);
1925
1926 /* The passed in type is zero for DATA */
1927 if (likely(!type))
1928 local_inc(&tail_page->entries);
1929
1930 /*
Steven Rostedtfa743952009-06-16 12:37:57 -04001931 * If this is the first commit on the page, then update
1932 * its timestamp.
Steven Rostedt6634ff22009-05-06 15:30:07 -04001933 */
Steven Rostedtfa743952009-06-16 12:37:57 -04001934 if (!tail)
1935 tail_page->page->time_stamp = *ts;
Steven Rostedt6634ff22009-05-06 15:30:07 -04001936
1937 return event;
1938}
1939
Steven Rostedtedd813bf2009-06-02 23:00:53 -04001940static inline int
1941rb_try_to_discard(struct ring_buffer_per_cpu *cpu_buffer,
1942 struct ring_buffer_event *event)
1943{
1944 unsigned long new_index, old_index;
1945 struct buffer_page *bpage;
1946 unsigned long index;
1947 unsigned long addr;
1948
1949 new_index = rb_event_index(event);
1950 old_index = new_index + rb_event_length(event);
1951 addr = (unsigned long)event;
1952 addr &= PAGE_MASK;
1953
1954 bpage = cpu_buffer->tail_page;
1955
1956 if (bpage->page == (void *)addr && rb_page_write(bpage) == old_index) {
Steven Rostedt77ae3652009-03-27 11:00:29 -04001957 unsigned long write_mask =
1958 local_read(&bpage->write) & ~RB_WRITE_MASK;
Steven Rostedtedd813bf2009-06-02 23:00:53 -04001959 /*
1960 * This is on the tail page. It is possible that
1961 * a write could come in and move the tail page
1962 * and write to the next page. That is fine
1963 * because we just shorten what is on this page.
1964 */
Steven Rostedt77ae3652009-03-27 11:00:29 -04001965 old_index += write_mask;
1966 new_index += write_mask;
Steven Rostedtedd813bf2009-06-02 23:00:53 -04001967 index = local_cmpxchg(&bpage->write, old_index, new_index);
1968 if (index == old_index)
1969 return 1;
1970 }
1971
1972 /* could not discard */
1973 return 0;
1974}
1975
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001976static int
1977rb_add_time_stamp(struct ring_buffer_per_cpu *cpu_buffer,
1978 u64 *ts, u64 *delta)
1979{
1980 struct ring_buffer_event *event;
1981 static int once;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001982 int ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001983
1984 if (unlikely(*delta > (1ULL << 59) && !once++)) {
1985 printk(KERN_WARNING "Delta way too big! %llu"
1986 " ts=%llu write stamp = %llu\n",
Stephen Rothwelle2862c92008-10-27 17:43:28 +11001987 (unsigned long long)*delta,
1988 (unsigned long long)*ts,
1989 (unsigned long long)cpu_buffer->write_stamp);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001990 WARN_ON(1);
1991 }
1992
1993 /*
1994 * The delta is too big, we to add a
1995 * new timestamp.
1996 */
1997 event = __rb_reserve_next(cpu_buffer,
1998 RINGBUF_TYPE_TIME_EXTEND,
1999 RB_LEN_TIME_EXTEND,
2000 ts);
2001 if (!event)
Steven Rostedtbf41a152008-10-04 02:00:59 -04002002 return -EBUSY;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002003
Steven Rostedtbf41a152008-10-04 02:00:59 -04002004 if (PTR_ERR(event) == -EAGAIN)
2005 return -EAGAIN;
2006
2007 /* Only a commited time event can update the write stamp */
Steven Rostedtfa743952009-06-16 12:37:57 -04002008 if (rb_event_is_commit(cpu_buffer, event)) {
Steven Rostedtbf41a152008-10-04 02:00:59 -04002009 /*
Steven Rostedtfa743952009-06-16 12:37:57 -04002010 * If this is the first on the page, then it was
2011 * updated with the page itself. Try to discard it
2012 * and if we can't just make it zero.
Steven Rostedtbf41a152008-10-04 02:00:59 -04002013 */
2014 if (rb_event_index(event)) {
2015 event->time_delta = *delta & TS_MASK;
2016 event->array[0] = *delta >> TS_SHIFT;
2017 } else {
Steven Rostedtea05b572009-06-03 09:30:10 -04002018 /* try to discard, since we do not need this */
2019 if (!rb_try_to_discard(cpu_buffer, event)) {
2020 /* nope, just zero it */
2021 event->time_delta = 0;
2022 event->array[0] = 0;
2023 }
Steven Rostedtbf41a152008-10-04 02:00:59 -04002024 }
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002025 cpu_buffer->write_stamp = *ts;
Steven Rostedtbf41a152008-10-04 02:00:59 -04002026 /* let the caller know this was the commit */
2027 ret = 1;
2028 } else {
Steven Rostedtedd813bf2009-06-02 23:00:53 -04002029 /* Try to discard the event */
2030 if (!rb_try_to_discard(cpu_buffer, event)) {
2031 /* Darn, this is just wasted space */
2032 event->time_delta = 0;
2033 event->array[0] = 0;
Steven Rostedtedd813bf2009-06-02 23:00:53 -04002034 }
Steven Rostedtf57a8a12009-06-05 14:11:30 -04002035 ret = 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002036 }
2037
Steven Rostedtbf41a152008-10-04 02:00:59 -04002038 *delta = 0;
2039
2040 return ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002041}
2042
Steven Rostedtfa743952009-06-16 12:37:57 -04002043static void rb_start_commit(struct ring_buffer_per_cpu *cpu_buffer)
2044{
2045 local_inc(&cpu_buffer->committing);
2046 local_inc(&cpu_buffer->commits);
2047}
2048
2049static void rb_end_commit(struct ring_buffer_per_cpu *cpu_buffer)
2050{
2051 unsigned long commits;
2052
2053 if (RB_WARN_ON(cpu_buffer,
2054 !local_read(&cpu_buffer->committing)))
2055 return;
2056
2057 again:
2058 commits = local_read(&cpu_buffer->commits);
2059 /* synchronize with interrupts */
2060 barrier();
2061 if (local_read(&cpu_buffer->committing) == 1)
2062 rb_set_commit_to_write(cpu_buffer);
2063
2064 local_dec(&cpu_buffer->committing);
2065
2066 /* synchronize with interrupts */
2067 barrier();
2068
2069 /*
2070 * Need to account for interrupts coming in between the
2071 * updating of the commit page and the clearing of the
2072 * committing counter.
2073 */
2074 if (unlikely(local_read(&cpu_buffer->commits) != commits) &&
2075 !local_read(&cpu_buffer->committing)) {
2076 local_inc(&cpu_buffer->committing);
2077 goto again;
2078 }
2079}
2080
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002081static struct ring_buffer_event *
Steven Rostedt62f0b3e2009-09-04 14:11:34 -04002082rb_reserve_next_event(struct ring_buffer *buffer,
2083 struct ring_buffer_per_cpu *cpu_buffer,
Steven Rostedt1cd8d732009-05-11 14:08:09 -04002084 unsigned long length)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002085{
2086 struct ring_buffer_event *event;
Steven Rostedt168b6b12009-05-11 22:11:05 -04002087 u64 ts, delta = 0;
Steven Rostedtbf41a152008-10-04 02:00:59 -04002088 int commit = 0;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002089 int nr_loops = 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002090
Steven Rostedtfa743952009-06-16 12:37:57 -04002091 rb_start_commit(cpu_buffer);
2092
Steven Rostedt85bac322009-09-04 14:24:40 -04002093#ifdef CONFIG_RING_BUFFER_ALLOW_SWAP
Steven Rostedt62f0b3e2009-09-04 14:11:34 -04002094 /*
2095 * Due to the ability to swap a cpu buffer from a buffer
2096 * it is possible it was swapped before we committed.
2097 * (committing stops a swap). We check for it here and
2098 * if it happened, we have to fail the write.
2099 */
2100 barrier();
2101 if (unlikely(ACCESS_ONCE(cpu_buffer->buffer) != buffer)) {
2102 local_dec(&cpu_buffer->committing);
2103 local_dec(&cpu_buffer->commits);
2104 return NULL;
2105 }
Steven Rostedt85bac322009-09-04 14:24:40 -04002106#endif
Steven Rostedt62f0b3e2009-09-04 14:11:34 -04002107
Steven Rostedtbe957c42009-05-11 14:42:53 -04002108 length = rb_calculate_event_length(length);
Steven Rostedtbf41a152008-10-04 02:00:59 -04002109 again:
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002110 /*
2111 * We allow for interrupts to reenter here and do a trace.
2112 * If one does, it will cause this original code to loop
2113 * back here. Even with heavy interrupts happening, this
2114 * should only happen a few times in a row. If this happens
2115 * 1000 times in a row, there must be either an interrupt
2116 * storm or we have something buggy.
2117 * Bail!
2118 */
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05002119 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 1000))
Steven Rostedtfa743952009-06-16 12:37:57 -04002120 goto out_fail;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002121
Jiri Olsa6d3f1e12009-10-23 19:36:19 -04002122 ts = rb_time_stamp(cpu_buffer->buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002123
Steven Rostedtbf41a152008-10-04 02:00:59 -04002124 /*
2125 * Only the first commit can update the timestamp.
2126 * Yes there is a race here. If an interrupt comes in
2127 * just after the conditional and it traces too, then it
2128 * will also check the deltas. More than one timestamp may
2129 * also be made. But only the entry that did the actual
2130 * commit will be something other than zero.
2131 */
Steven Rostedt0f0c85f2009-05-11 16:08:00 -04002132 if (likely(cpu_buffer->tail_page == cpu_buffer->commit_page &&
2133 rb_page_write(cpu_buffer->tail_page) ==
2134 rb_commit_index(cpu_buffer))) {
Steven Rostedt168b6b12009-05-11 22:11:05 -04002135 u64 diff;
Steven Rostedtbf41a152008-10-04 02:00:59 -04002136
Steven Rostedt168b6b12009-05-11 22:11:05 -04002137 diff = ts - cpu_buffer->write_stamp;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002138
Steven Rostedt168b6b12009-05-11 22:11:05 -04002139 /* make sure this diff is calculated here */
Steven Rostedtbf41a152008-10-04 02:00:59 -04002140 barrier();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002141
Steven Rostedtbf41a152008-10-04 02:00:59 -04002142 /* Did the write stamp get updated already? */
2143 if (unlikely(ts < cpu_buffer->write_stamp))
Steven Rostedt168b6b12009-05-11 22:11:05 -04002144 goto get_event;
Steven Rostedtbf41a152008-10-04 02:00:59 -04002145
Steven Rostedt168b6b12009-05-11 22:11:05 -04002146 delta = diff;
2147 if (unlikely(test_time_stamp(delta))) {
Steven Rostedtbf41a152008-10-04 02:00:59 -04002148
2149 commit = rb_add_time_stamp(cpu_buffer, &ts, &delta);
Steven Rostedtbf41a152008-10-04 02:00:59 -04002150 if (commit == -EBUSY)
Steven Rostedtfa743952009-06-16 12:37:57 -04002151 goto out_fail;
Steven Rostedtbf41a152008-10-04 02:00:59 -04002152
2153 if (commit == -EAGAIN)
2154 goto again;
2155
2156 RB_WARN_ON(cpu_buffer, commit < 0);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002157 }
Steven Rostedt168b6b12009-05-11 22:11:05 -04002158 }
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002159
Steven Rostedt168b6b12009-05-11 22:11:05 -04002160 get_event:
Steven Rostedt1cd8d732009-05-11 14:08:09 -04002161 event = __rb_reserve_next(cpu_buffer, 0, length, &ts);
Steven Rostedt168b6b12009-05-11 22:11:05 -04002162 if (unlikely(PTR_ERR(event) == -EAGAIN))
Steven Rostedtbf41a152008-10-04 02:00:59 -04002163 goto again;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002164
Steven Rostedtfa743952009-06-16 12:37:57 -04002165 if (!event)
2166 goto out_fail;
Steven Rostedtbf41a152008-10-04 02:00:59 -04002167
Steven Rostedtfa743952009-06-16 12:37:57 -04002168 if (!rb_event_is_commit(cpu_buffer, event))
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002169 delta = 0;
2170
2171 event->time_delta = delta;
2172
2173 return event;
Steven Rostedtfa743952009-06-16 12:37:57 -04002174
2175 out_fail:
2176 rb_end_commit(cpu_buffer);
2177 return NULL;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002178}
2179
Paul Mundt1155de42009-06-25 14:30:12 +09002180#ifdef CONFIG_TRACING
2181
Steven Rostedtaa18efb2009-04-20 16:16:11 -04002182#define TRACE_RECURSIVE_DEPTH 16
Steven Rostedt261842b2009-04-16 21:41:52 -04002183
2184static int trace_recursive_lock(void)
2185{
Steven Rostedtaa18efb2009-04-20 16:16:11 -04002186 current->trace_recursion++;
Steven Rostedt261842b2009-04-16 21:41:52 -04002187
Steven Rostedtaa18efb2009-04-20 16:16:11 -04002188 if (likely(current->trace_recursion < TRACE_RECURSIVE_DEPTH))
2189 return 0;
Steven Rostedt261842b2009-04-16 21:41:52 -04002190
Steven Rostedtaa18efb2009-04-20 16:16:11 -04002191 /* Disable all tracing before we do anything else */
2192 tracing_off_permanent();
Frederic Weisbeckere057a5e2009-04-19 23:38:12 +02002193
Steven Rostedt7d7d2b82009-04-27 12:37:49 -04002194 printk_once(KERN_WARNING "Tracing recursion: depth[%ld]:"
Steven Rostedtaa18efb2009-04-20 16:16:11 -04002195 "HC[%lu]:SC[%lu]:NMI[%lu]\n",
2196 current->trace_recursion,
2197 hardirq_count() >> HARDIRQ_SHIFT,
2198 softirq_count() >> SOFTIRQ_SHIFT,
2199 in_nmi());
Frederic Weisbeckere057a5e2009-04-19 23:38:12 +02002200
Steven Rostedtaa18efb2009-04-20 16:16:11 -04002201 WARN_ON_ONCE(1);
2202 return -1;
Steven Rostedt261842b2009-04-16 21:41:52 -04002203}
2204
2205static void trace_recursive_unlock(void)
2206{
Steven Rostedtaa18efb2009-04-20 16:16:11 -04002207 WARN_ON_ONCE(!current->trace_recursion);
Steven Rostedt261842b2009-04-16 21:41:52 -04002208
Steven Rostedtaa18efb2009-04-20 16:16:11 -04002209 current->trace_recursion--;
Steven Rostedt261842b2009-04-16 21:41:52 -04002210}
2211
Paul Mundt1155de42009-06-25 14:30:12 +09002212#else
2213
2214#define trace_recursive_lock() (0)
2215#define trace_recursive_unlock() do { } while (0)
2216
2217#endif
2218
Steven Rostedtbf41a152008-10-04 02:00:59 -04002219static DEFINE_PER_CPU(int, rb_need_resched);
2220
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002221/**
2222 * ring_buffer_lock_reserve - reserve a part of the buffer
2223 * @buffer: the ring buffer to reserve from
2224 * @length: the length of the data to reserve (excluding event header)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002225 *
2226 * Returns a reseverd event on the ring buffer to copy directly to.
2227 * The user of this interface will need to get the body to write into
2228 * and can use the ring_buffer_event_data() interface.
2229 *
2230 * The length is the length of the data needed, not the event length
2231 * which also includes the event header.
2232 *
2233 * Must be paired with ring_buffer_unlock_commit, unless NULL is returned.
2234 * If NULL is returned, then nothing has been allocated or locked.
2235 */
2236struct ring_buffer_event *
Arnaldo Carvalho de Melo0a987752009-02-05 16:12:56 -02002237ring_buffer_lock_reserve(struct ring_buffer *buffer, unsigned long length)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002238{
2239 struct ring_buffer_per_cpu *cpu_buffer;
2240 struct ring_buffer_event *event;
Steven Rostedtbf41a152008-10-04 02:00:59 -04002241 int cpu, resched;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002242
Steven Rostedt033601a2008-11-21 12:41:55 -05002243 if (ring_buffer_flags != RB_BUFFERS_ON)
Steven Rostedta3583242008-11-11 15:01:42 -05002244 return NULL;
2245
Steven Rostedtbf41a152008-10-04 02:00:59 -04002246 /* If we are tracing schedule, we don't want to recurse */
Steven Rostedt182e9f52008-11-03 23:15:56 -05002247 resched = ftrace_preempt_disable();
Steven Rostedtbf41a152008-10-04 02:00:59 -04002248
Lai Jiangshan52fbe9c2010-03-08 14:50:43 +08002249 if (atomic_read(&buffer->record_disabled))
2250 goto out_nocheck;
2251
Steven Rostedt261842b2009-04-16 21:41:52 -04002252 if (trace_recursive_lock())
2253 goto out_nocheck;
2254
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002255 cpu = raw_smp_processor_id();
2256
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302257 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedtd7690412008-10-01 00:29:53 -04002258 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002259
2260 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002261
2262 if (atomic_read(&cpu_buffer->record_disabled))
Steven Rostedtd7690412008-10-01 00:29:53 -04002263 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002264
Steven Rostedtbe957c42009-05-11 14:42:53 -04002265 if (length > BUF_MAX_DATA_SIZE)
Steven Rostedtbf41a152008-10-04 02:00:59 -04002266 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002267
Steven Rostedt62f0b3e2009-09-04 14:11:34 -04002268 event = rb_reserve_next_event(buffer, cpu_buffer, length);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002269 if (!event)
Steven Rostedtd7690412008-10-01 00:29:53 -04002270 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002271
Steven Rostedtbf41a152008-10-04 02:00:59 -04002272 /*
2273 * Need to store resched state on this cpu.
2274 * Only the first needs to.
2275 */
2276
2277 if (preempt_count() == 1)
2278 per_cpu(rb_need_resched, cpu) = resched;
2279
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002280 return event;
2281
Steven Rostedtd7690412008-10-01 00:29:53 -04002282 out:
Steven Rostedt261842b2009-04-16 21:41:52 -04002283 trace_recursive_unlock();
2284
2285 out_nocheck:
Steven Rostedt182e9f52008-11-03 23:15:56 -05002286 ftrace_preempt_enable(resched);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002287 return NULL;
2288}
Robert Richterc4f50182008-12-11 16:49:22 +01002289EXPORT_SYMBOL_GPL(ring_buffer_lock_reserve);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002290
Steven Rostedta1863c22009-09-03 10:23:58 -04002291static void
2292rb_update_write_stamp(struct ring_buffer_per_cpu *cpu_buffer,
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002293 struct ring_buffer_event *event)
2294{
Steven Rostedtfa743952009-06-16 12:37:57 -04002295 /*
2296 * The event first in the commit queue updates the
2297 * time stamp.
2298 */
2299 if (rb_event_is_commit(cpu_buffer, event))
2300 cpu_buffer->write_stamp += event->time_delta;
Steven Rostedta1863c22009-09-03 10:23:58 -04002301}
Steven Rostedtbf41a152008-10-04 02:00:59 -04002302
Steven Rostedta1863c22009-09-03 10:23:58 -04002303static void rb_commit(struct ring_buffer_per_cpu *cpu_buffer,
2304 struct ring_buffer_event *event)
2305{
2306 local_inc(&cpu_buffer->entries);
2307 rb_update_write_stamp(cpu_buffer, event);
Steven Rostedtfa743952009-06-16 12:37:57 -04002308 rb_end_commit(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002309}
2310
2311/**
2312 * ring_buffer_unlock_commit - commit a reserved
2313 * @buffer: The buffer to commit to
2314 * @event: The event pointer to commit.
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002315 *
2316 * This commits the data to the ring buffer, and releases any locks held.
2317 *
2318 * Must be paired with ring_buffer_lock_reserve.
2319 */
2320int ring_buffer_unlock_commit(struct ring_buffer *buffer,
Arnaldo Carvalho de Melo0a987752009-02-05 16:12:56 -02002321 struct ring_buffer_event *event)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002322{
2323 struct ring_buffer_per_cpu *cpu_buffer;
2324 int cpu = raw_smp_processor_id();
2325
2326 cpu_buffer = buffer->buffers[cpu];
2327
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002328 rb_commit(cpu_buffer, event);
2329
Steven Rostedt261842b2009-04-16 21:41:52 -04002330 trace_recursive_unlock();
2331
Steven Rostedtbf41a152008-10-04 02:00:59 -04002332 /*
2333 * Only the last preempt count needs to restore preemption.
2334 */
Steven Rostedt182e9f52008-11-03 23:15:56 -05002335 if (preempt_count() == 1)
2336 ftrace_preempt_enable(per_cpu(rb_need_resched, cpu));
2337 else
Steven Rostedtbf41a152008-10-04 02:00:59 -04002338 preempt_enable_no_resched_notrace();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002339
2340 return 0;
2341}
Robert Richterc4f50182008-12-11 16:49:22 +01002342EXPORT_SYMBOL_GPL(ring_buffer_unlock_commit);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002343
Frederic Weisbeckerf3b9aae2009-04-19 23:39:33 +02002344static inline void rb_event_discard(struct ring_buffer_event *event)
2345{
Lai Jiangshan334d4162009-04-24 11:27:05 +08002346 /* array[0] holds the actual length for the discarded event */
2347 event->array[0] = rb_event_data_length(event) - RB_EVNT_HDR_SIZE;
2348 event->type_len = RINGBUF_TYPE_PADDING;
Frederic Weisbeckerf3b9aae2009-04-19 23:39:33 +02002349 /* time delta must be non zero */
2350 if (!event->time_delta)
2351 event->time_delta = 1;
2352}
2353
Steven Rostedta1863c22009-09-03 10:23:58 -04002354/*
2355 * Decrement the entries to the page that an event is on.
2356 * The event does not even need to exist, only the pointer
2357 * to the page it is on. This may only be called before the commit
2358 * takes place.
2359 */
2360static inline void
2361rb_decrement_entry(struct ring_buffer_per_cpu *cpu_buffer,
2362 struct ring_buffer_event *event)
2363{
2364 unsigned long addr = (unsigned long)event;
2365 struct buffer_page *bpage = cpu_buffer->commit_page;
2366 struct buffer_page *start;
2367
2368 addr &= PAGE_MASK;
2369
2370 /* Do the likely case first */
2371 if (likely(bpage->page == (void *)addr)) {
2372 local_dec(&bpage->entries);
2373 return;
2374 }
2375
2376 /*
2377 * Because the commit page may be on the reader page we
2378 * start with the next page and check the end loop there.
2379 */
2380 rb_inc_page(cpu_buffer, &bpage);
2381 start = bpage;
2382 do {
2383 if (bpage->page == (void *)addr) {
2384 local_dec(&bpage->entries);
2385 return;
2386 }
2387 rb_inc_page(cpu_buffer, &bpage);
2388 } while (bpage != start);
2389
2390 /* commit not part of this buffer?? */
2391 RB_WARN_ON(cpu_buffer, 1);
2392}
2393
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002394/**
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002395 * ring_buffer_commit_discard - discard an event that has not been committed
2396 * @buffer: the ring buffer
2397 * @event: non committed event to discard
2398 *
Steven Rostedtdc892f72009-09-03 15:33:41 -04002399 * Sometimes an event that is in the ring buffer needs to be ignored.
2400 * This function lets the user discard an event in the ring buffer
2401 * and then that event will not be read later.
2402 *
2403 * This function only works if it is called before the the item has been
2404 * committed. It will try to free the event from the ring buffer
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002405 * if another event has not been added behind it.
2406 *
2407 * If another event has been added behind it, it will set the event
2408 * up as discarded, and perform the commit.
2409 *
2410 * If this function is called, do not call ring_buffer_unlock_commit on
2411 * the event.
2412 */
2413void ring_buffer_discard_commit(struct ring_buffer *buffer,
2414 struct ring_buffer_event *event)
2415{
2416 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002417 int cpu;
2418
2419 /* The event is discarded regardless */
Frederic Weisbeckerf3b9aae2009-04-19 23:39:33 +02002420 rb_event_discard(event);
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002421
Steven Rostedtfa743952009-06-16 12:37:57 -04002422 cpu = smp_processor_id();
2423 cpu_buffer = buffer->buffers[cpu];
2424
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002425 /*
2426 * This must only be called if the event has not been
2427 * committed yet. Thus we can assume that preemption
2428 * is still disabled.
2429 */
Steven Rostedtfa743952009-06-16 12:37:57 -04002430 RB_WARN_ON(buffer, !local_read(&cpu_buffer->committing));
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002431
Steven Rostedta1863c22009-09-03 10:23:58 -04002432 rb_decrement_entry(cpu_buffer, event);
Steven Rostedt0f2541d2009-08-05 12:02:48 -04002433 if (rb_try_to_discard(cpu_buffer, event))
Steven Rostedtedd813bf2009-06-02 23:00:53 -04002434 goto out;
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002435
2436 /*
2437 * The commit is still visible by the reader, so we
Steven Rostedta1863c22009-09-03 10:23:58 -04002438 * must still update the timestamp.
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002439 */
Steven Rostedta1863c22009-09-03 10:23:58 -04002440 rb_update_write_stamp(cpu_buffer, event);
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002441 out:
Steven Rostedtfa743952009-06-16 12:37:57 -04002442 rb_end_commit(cpu_buffer);
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002443
Frederic Weisbeckerf3b9aae2009-04-19 23:39:33 +02002444 trace_recursive_unlock();
2445
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002446 /*
2447 * Only the last preempt count needs to restore preemption.
2448 */
2449 if (preempt_count() == 1)
2450 ftrace_preempt_enable(per_cpu(rb_need_resched, cpu));
2451 else
2452 preempt_enable_no_resched_notrace();
2453
2454}
2455EXPORT_SYMBOL_GPL(ring_buffer_discard_commit);
2456
2457/**
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002458 * ring_buffer_write - write data to the buffer without reserving
2459 * @buffer: The ring buffer to write to.
2460 * @length: The length of the data being written (excluding the event header)
2461 * @data: The data to write to the buffer.
2462 *
2463 * This is like ring_buffer_lock_reserve and ring_buffer_unlock_commit as
2464 * one function. If you already have the data to write to the buffer, it
2465 * may be easier to simply call this function.
2466 *
2467 * Note, like ring_buffer_lock_reserve, the length is the length of the data
2468 * and not the length of the event which would hold the header.
2469 */
2470int ring_buffer_write(struct ring_buffer *buffer,
2471 unsigned long length,
2472 void *data)
2473{
2474 struct ring_buffer_per_cpu *cpu_buffer;
2475 struct ring_buffer_event *event;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002476 void *body;
2477 int ret = -EBUSY;
Steven Rostedtbf41a152008-10-04 02:00:59 -04002478 int cpu, resched;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002479
Steven Rostedt033601a2008-11-21 12:41:55 -05002480 if (ring_buffer_flags != RB_BUFFERS_ON)
Steven Rostedta3583242008-11-11 15:01:42 -05002481 return -EBUSY;
2482
Steven Rostedt182e9f52008-11-03 23:15:56 -05002483 resched = ftrace_preempt_disable();
Steven Rostedtbf41a152008-10-04 02:00:59 -04002484
Lai Jiangshan52fbe9c2010-03-08 14:50:43 +08002485 if (atomic_read(&buffer->record_disabled))
2486 goto out;
2487
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002488 cpu = raw_smp_processor_id();
2489
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302490 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedtd7690412008-10-01 00:29:53 -04002491 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002492
2493 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002494
2495 if (atomic_read(&cpu_buffer->record_disabled))
2496 goto out;
2497
Steven Rostedtbe957c42009-05-11 14:42:53 -04002498 if (length > BUF_MAX_DATA_SIZE)
2499 goto out;
2500
Steven Rostedt62f0b3e2009-09-04 14:11:34 -04002501 event = rb_reserve_next_event(buffer, cpu_buffer, length);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002502 if (!event)
2503 goto out;
2504
2505 body = rb_event_data(event);
2506
2507 memcpy(body, data, length);
2508
2509 rb_commit(cpu_buffer, event);
2510
2511 ret = 0;
2512 out:
Steven Rostedt182e9f52008-11-03 23:15:56 -05002513 ftrace_preempt_enable(resched);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002514
2515 return ret;
2516}
Robert Richterc4f50182008-12-11 16:49:22 +01002517EXPORT_SYMBOL_GPL(ring_buffer_write);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002518
Andrew Morton34a148b2009-01-09 12:27:09 -08002519static int rb_per_cpu_empty(struct ring_buffer_per_cpu *cpu_buffer)
Steven Rostedtbf41a152008-10-04 02:00:59 -04002520{
2521 struct buffer_page *reader = cpu_buffer->reader_page;
Steven Rostedt77ae3652009-03-27 11:00:29 -04002522 struct buffer_page *head = rb_set_head_page(cpu_buffer);
Steven Rostedtbf41a152008-10-04 02:00:59 -04002523 struct buffer_page *commit = cpu_buffer->commit_page;
2524
Steven Rostedt77ae3652009-03-27 11:00:29 -04002525 /* In case of error, head will be NULL */
2526 if (unlikely(!head))
2527 return 1;
2528
Steven Rostedtbf41a152008-10-04 02:00:59 -04002529 return reader->read == rb_page_commit(reader) &&
2530 (commit == reader ||
2531 (commit == head &&
2532 head->read == rb_page_commit(commit)));
2533}
2534
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002535/**
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002536 * ring_buffer_record_disable - stop all writes into the buffer
2537 * @buffer: The ring buffer to stop writes to.
2538 *
2539 * This prevents all writes to the buffer. Any attempt to write
2540 * to the buffer after this will fail and return NULL.
2541 *
2542 * The caller should call synchronize_sched() after this.
2543 */
2544void ring_buffer_record_disable(struct ring_buffer *buffer)
2545{
2546 atomic_inc(&buffer->record_disabled);
2547}
Robert Richterc4f50182008-12-11 16:49:22 +01002548EXPORT_SYMBOL_GPL(ring_buffer_record_disable);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002549
2550/**
2551 * ring_buffer_record_enable - enable writes to the buffer
2552 * @buffer: The ring buffer to enable writes
2553 *
2554 * Note, multiple disables will need the same number of enables
Adam Buchbinderc41b20e2009-12-11 16:35:39 -05002555 * to truly enable the writing (much like preempt_disable).
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002556 */
2557void ring_buffer_record_enable(struct ring_buffer *buffer)
2558{
2559 atomic_dec(&buffer->record_disabled);
2560}
Robert Richterc4f50182008-12-11 16:49:22 +01002561EXPORT_SYMBOL_GPL(ring_buffer_record_enable);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002562
2563/**
2564 * ring_buffer_record_disable_cpu - stop all writes into the cpu_buffer
2565 * @buffer: The ring buffer to stop writes to.
2566 * @cpu: The CPU buffer to stop
2567 *
2568 * This prevents all writes to the buffer. Any attempt to write
2569 * to the buffer after this will fail and return NULL.
2570 *
2571 * The caller should call synchronize_sched() after this.
2572 */
2573void ring_buffer_record_disable_cpu(struct ring_buffer *buffer, int cpu)
2574{
2575 struct ring_buffer_per_cpu *cpu_buffer;
2576
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302577 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04002578 return;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002579
2580 cpu_buffer = buffer->buffers[cpu];
2581 atomic_inc(&cpu_buffer->record_disabled);
2582}
Robert Richterc4f50182008-12-11 16:49:22 +01002583EXPORT_SYMBOL_GPL(ring_buffer_record_disable_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002584
2585/**
2586 * ring_buffer_record_enable_cpu - enable writes to the buffer
2587 * @buffer: The ring buffer to enable writes
2588 * @cpu: The CPU to enable.
2589 *
2590 * Note, multiple disables will need the same number of enables
Adam Buchbinderc41b20e2009-12-11 16:35:39 -05002591 * to truly enable the writing (much like preempt_disable).
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002592 */
2593void ring_buffer_record_enable_cpu(struct ring_buffer *buffer, int cpu)
2594{
2595 struct ring_buffer_per_cpu *cpu_buffer;
2596
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302597 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04002598 return;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002599
2600 cpu_buffer = buffer->buffers[cpu];
2601 atomic_dec(&cpu_buffer->record_disabled);
2602}
Robert Richterc4f50182008-12-11 16:49:22 +01002603EXPORT_SYMBOL_GPL(ring_buffer_record_enable_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002604
2605/**
2606 * ring_buffer_entries_cpu - get the number of entries in a cpu buffer
2607 * @buffer: The ring buffer
2608 * @cpu: The per CPU buffer to get the entries from.
2609 */
2610unsigned long ring_buffer_entries_cpu(struct ring_buffer *buffer, int cpu)
2611{
2612 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedt8aabee52009-03-12 13:13:49 -04002613 unsigned long ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002614
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302615 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04002616 return 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002617
2618 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt77ae3652009-03-27 11:00:29 -04002619 ret = (local_read(&cpu_buffer->entries) - local_read(&cpu_buffer->overrun))
Steven Rostedte4906ef2009-04-30 20:49:44 -04002620 - cpu_buffer->read;
Steven Rostedt554f7862009-03-11 22:00:13 -04002621
2622 return ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002623}
Robert Richterc4f50182008-12-11 16:49:22 +01002624EXPORT_SYMBOL_GPL(ring_buffer_entries_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002625
2626/**
2627 * ring_buffer_overrun_cpu - get the number of overruns in a cpu_buffer
2628 * @buffer: The ring buffer
2629 * @cpu: The per CPU buffer to get the number of overruns from
2630 */
2631unsigned long ring_buffer_overrun_cpu(struct ring_buffer *buffer, int cpu)
2632{
2633 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedt8aabee52009-03-12 13:13:49 -04002634 unsigned long ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002635
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302636 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04002637 return 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002638
2639 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt77ae3652009-03-27 11:00:29 -04002640 ret = local_read(&cpu_buffer->overrun);
Steven Rostedt554f7862009-03-11 22:00:13 -04002641
2642 return ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002643}
Robert Richterc4f50182008-12-11 16:49:22 +01002644EXPORT_SYMBOL_GPL(ring_buffer_overrun_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002645
2646/**
Steven Rostedtf0d2c682009-04-29 13:43:37 -04002647 * ring_buffer_commit_overrun_cpu - get the number of overruns caused by commits
2648 * @buffer: The ring buffer
2649 * @cpu: The per CPU buffer to get the number of overruns from
2650 */
2651unsigned long
2652ring_buffer_commit_overrun_cpu(struct ring_buffer *buffer, int cpu)
2653{
2654 struct ring_buffer_per_cpu *cpu_buffer;
2655 unsigned long ret;
2656
2657 if (!cpumask_test_cpu(cpu, buffer->cpumask))
2658 return 0;
2659
2660 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt77ae3652009-03-27 11:00:29 -04002661 ret = local_read(&cpu_buffer->commit_overrun);
Steven Rostedtf0d2c682009-04-29 13:43:37 -04002662
2663 return ret;
2664}
2665EXPORT_SYMBOL_GPL(ring_buffer_commit_overrun_cpu);
2666
2667/**
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002668 * ring_buffer_entries - get the number of entries in a buffer
2669 * @buffer: The ring buffer
2670 *
2671 * Returns the total number of entries in the ring buffer
2672 * (all CPU entries)
2673 */
2674unsigned long ring_buffer_entries(struct ring_buffer *buffer)
2675{
2676 struct ring_buffer_per_cpu *cpu_buffer;
2677 unsigned long entries = 0;
2678 int cpu;
2679
2680 /* if you care about this being correct, lock the buffer */
2681 for_each_buffer_cpu(buffer, cpu) {
2682 cpu_buffer = buffer->buffers[cpu];
Steven Rostedte4906ef2009-04-30 20:49:44 -04002683 entries += (local_read(&cpu_buffer->entries) -
Steven Rostedt77ae3652009-03-27 11:00:29 -04002684 local_read(&cpu_buffer->overrun)) - cpu_buffer->read;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002685 }
2686
2687 return entries;
2688}
Robert Richterc4f50182008-12-11 16:49:22 +01002689EXPORT_SYMBOL_GPL(ring_buffer_entries);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002690
2691/**
Jiri Olsa67b394f2009-10-23 19:36:18 -04002692 * ring_buffer_overruns - get the number of overruns in buffer
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002693 * @buffer: The ring buffer
2694 *
2695 * Returns the total number of overruns in the ring buffer
2696 * (all CPU entries)
2697 */
2698unsigned long ring_buffer_overruns(struct ring_buffer *buffer)
2699{
2700 struct ring_buffer_per_cpu *cpu_buffer;
2701 unsigned long overruns = 0;
2702 int cpu;
2703
2704 /* if you care about this being correct, lock the buffer */
2705 for_each_buffer_cpu(buffer, cpu) {
2706 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt77ae3652009-03-27 11:00:29 -04002707 overruns += local_read(&cpu_buffer->overrun);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002708 }
2709
2710 return overruns;
2711}
Robert Richterc4f50182008-12-11 16:49:22 +01002712EXPORT_SYMBOL_GPL(ring_buffer_overruns);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002713
Steven Rostedt642edba2008-11-12 00:01:26 -05002714static void rb_iter_reset(struct ring_buffer_iter *iter)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002715{
2716 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
2717
Steven Rostedtd7690412008-10-01 00:29:53 -04002718 /* Iterator usage is expected to have record disabled */
2719 if (list_empty(&cpu_buffer->reader_page->list)) {
Steven Rostedt77ae3652009-03-27 11:00:29 -04002720 iter->head_page = rb_set_head_page(cpu_buffer);
2721 if (unlikely(!iter->head_page))
2722 return;
2723 iter->head = iter->head_page->read;
Steven Rostedtd7690412008-10-01 00:29:53 -04002724 } else {
2725 iter->head_page = cpu_buffer->reader_page;
Steven Rostedt6f807ac2008-10-04 02:00:58 -04002726 iter->head = cpu_buffer->reader_page->read;
Steven Rostedtd7690412008-10-01 00:29:53 -04002727 }
2728 if (iter->head)
2729 iter->read_stamp = cpu_buffer->read_stamp;
2730 else
Steven Rostedtabc9b562008-12-02 15:34:06 -05002731 iter->read_stamp = iter->head_page->page->time_stamp;
Steven Rostedt492a74f2010-01-25 15:17:47 -05002732 iter->cache_reader_page = cpu_buffer->reader_page;
2733 iter->cache_read = cpu_buffer->read;
Steven Rostedt642edba2008-11-12 00:01:26 -05002734}
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002735
Steven Rostedt642edba2008-11-12 00:01:26 -05002736/**
2737 * ring_buffer_iter_reset - reset an iterator
2738 * @iter: The iterator to reset
2739 *
2740 * Resets the iterator, so that it will start from the beginning
2741 * again.
2742 */
2743void ring_buffer_iter_reset(struct ring_buffer_iter *iter)
2744{
Steven Rostedt554f7862009-03-11 22:00:13 -04002745 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedt642edba2008-11-12 00:01:26 -05002746 unsigned long flags;
2747
Steven Rostedt554f7862009-03-11 22:00:13 -04002748 if (!iter)
2749 return;
2750
2751 cpu_buffer = iter->cpu_buffer;
2752
Steven Rostedt642edba2008-11-12 00:01:26 -05002753 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
2754 rb_iter_reset(iter);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002755 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002756}
Robert Richterc4f50182008-12-11 16:49:22 +01002757EXPORT_SYMBOL_GPL(ring_buffer_iter_reset);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002758
2759/**
2760 * ring_buffer_iter_empty - check if an iterator has no more to read
2761 * @iter: The iterator to check
2762 */
2763int ring_buffer_iter_empty(struct ring_buffer_iter *iter)
2764{
2765 struct ring_buffer_per_cpu *cpu_buffer;
2766
2767 cpu_buffer = iter->cpu_buffer;
2768
Steven Rostedtbf41a152008-10-04 02:00:59 -04002769 return iter->head_page == cpu_buffer->commit_page &&
2770 iter->head == rb_commit_index(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002771}
Robert Richterc4f50182008-12-11 16:49:22 +01002772EXPORT_SYMBOL_GPL(ring_buffer_iter_empty);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002773
2774static void
2775rb_update_read_stamp(struct ring_buffer_per_cpu *cpu_buffer,
2776 struct ring_buffer_event *event)
2777{
2778 u64 delta;
2779
Lai Jiangshan334d4162009-04-24 11:27:05 +08002780 switch (event->type_len) {
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002781 case RINGBUF_TYPE_PADDING:
2782 return;
2783
2784 case RINGBUF_TYPE_TIME_EXTEND:
2785 delta = event->array[0];
2786 delta <<= TS_SHIFT;
2787 delta += event->time_delta;
2788 cpu_buffer->read_stamp += delta;
2789 return;
2790
2791 case RINGBUF_TYPE_TIME_STAMP:
2792 /* FIXME: not implemented */
2793 return;
2794
2795 case RINGBUF_TYPE_DATA:
2796 cpu_buffer->read_stamp += event->time_delta;
2797 return;
2798
2799 default:
2800 BUG();
2801 }
2802 return;
2803}
2804
2805static void
2806rb_update_iter_read_stamp(struct ring_buffer_iter *iter,
2807 struct ring_buffer_event *event)
2808{
2809 u64 delta;
2810
Lai Jiangshan334d4162009-04-24 11:27:05 +08002811 switch (event->type_len) {
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002812 case RINGBUF_TYPE_PADDING:
2813 return;
2814
2815 case RINGBUF_TYPE_TIME_EXTEND:
2816 delta = event->array[0];
2817 delta <<= TS_SHIFT;
2818 delta += event->time_delta;
2819 iter->read_stamp += delta;
2820 return;
2821
2822 case RINGBUF_TYPE_TIME_STAMP:
2823 /* FIXME: not implemented */
2824 return;
2825
2826 case RINGBUF_TYPE_DATA:
2827 iter->read_stamp += event->time_delta;
2828 return;
2829
2830 default:
2831 BUG();
2832 }
2833 return;
2834}
2835
Steven Rostedtd7690412008-10-01 00:29:53 -04002836static struct buffer_page *
2837rb_get_reader_page(struct ring_buffer_per_cpu *cpu_buffer)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002838{
Steven Rostedtd7690412008-10-01 00:29:53 -04002839 struct buffer_page *reader = NULL;
2840 unsigned long flags;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002841 int nr_loops = 0;
Steven Rostedt77ae3652009-03-27 11:00:29 -04002842 int ret;
Steven Rostedtd7690412008-10-01 00:29:53 -04002843
Steven Rostedt3e03fb72008-11-06 00:09:43 -05002844 local_irq_save(flags);
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01002845 arch_spin_lock(&cpu_buffer->lock);
Steven Rostedtd7690412008-10-01 00:29:53 -04002846
2847 again:
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002848 /*
2849 * This should normally only loop twice. But because the
2850 * start of the reader inserts an empty page, it causes
2851 * a case where we will loop three times. There should be no
2852 * reason to loop four times (that I know of).
2853 */
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05002854 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 3)) {
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002855 reader = NULL;
2856 goto out;
2857 }
2858
Steven Rostedtd7690412008-10-01 00:29:53 -04002859 reader = cpu_buffer->reader_page;
2860
2861 /* If there's more to read, return this page */
Steven Rostedtbf41a152008-10-04 02:00:59 -04002862 if (cpu_buffer->reader_page->read < rb_page_size(reader))
Steven Rostedtd7690412008-10-01 00:29:53 -04002863 goto out;
2864
2865 /* Never should we have an index greater than the size */
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05002866 if (RB_WARN_ON(cpu_buffer,
2867 cpu_buffer->reader_page->read > rb_page_size(reader)))
2868 goto out;
Steven Rostedtd7690412008-10-01 00:29:53 -04002869
2870 /* check if we caught up to the tail */
2871 reader = NULL;
Steven Rostedtbf41a152008-10-04 02:00:59 -04002872 if (cpu_buffer->commit_page == cpu_buffer->reader_page)
Steven Rostedtd7690412008-10-01 00:29:53 -04002873 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002874
2875 /*
Steven Rostedtd7690412008-10-01 00:29:53 -04002876 * Reset the reader page to size zero.
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002877 */
Steven Rostedt77ae3652009-03-27 11:00:29 -04002878 local_set(&cpu_buffer->reader_page->write, 0);
2879 local_set(&cpu_buffer->reader_page->entries, 0);
2880 local_set(&cpu_buffer->reader_page->page->commit, 0);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002881
Steven Rostedt77ae3652009-03-27 11:00:29 -04002882 spin:
2883 /*
2884 * Splice the empty reader page into the list around the head.
2885 */
2886 reader = rb_set_head_page(cpu_buffer);
Steven Rostedt0e1ff5d2010-01-06 20:40:44 -05002887 cpu_buffer->reader_page->list.next = rb_list_head(reader->list.next);
Steven Rostedtd7690412008-10-01 00:29:53 -04002888 cpu_buffer->reader_page->list.prev = reader->list.prev;
Steven Rostedtbf41a152008-10-04 02:00:59 -04002889
Steven Rostedt3adc54f2009-03-30 15:32:01 -04002890 /*
2891 * cpu_buffer->pages just needs to point to the buffer, it
2892 * has no specific buffer page to point to. Lets move it out
2893 * of our way so we don't accidently swap it.
2894 */
2895 cpu_buffer->pages = reader->list.prev;
2896
Steven Rostedt77ae3652009-03-27 11:00:29 -04002897 /* The reader page will be pointing to the new head */
2898 rb_set_list_to_head(cpu_buffer, &cpu_buffer->reader_page->list);
Steven Rostedtd7690412008-10-01 00:29:53 -04002899
2900 /*
Steven Rostedt77ae3652009-03-27 11:00:29 -04002901 * Here's the tricky part.
2902 *
2903 * We need to move the pointer past the header page.
2904 * But we can only do that if a writer is not currently
2905 * moving it. The page before the header page has the
2906 * flag bit '1' set if it is pointing to the page we want.
2907 * but if the writer is in the process of moving it
2908 * than it will be '2' or already moved '0'.
Steven Rostedtd7690412008-10-01 00:29:53 -04002909 */
Steven Rostedtd7690412008-10-01 00:29:53 -04002910
Steven Rostedt77ae3652009-03-27 11:00:29 -04002911 ret = rb_head_page_replace(reader, cpu_buffer->reader_page);
2912
2913 /*
2914 * If we did not convert it, then we must try again.
2915 */
2916 if (!ret)
2917 goto spin;
2918
2919 /*
2920 * Yeah! We succeeded in replacing the page.
2921 *
2922 * Now make the new head point back to the reader page.
2923 */
David Sharp5ded3dc2010-01-06 17:12:07 -08002924 rb_list_head(reader->list.next)->prev = &cpu_buffer->reader_page->list;
Steven Rostedt77ae3652009-03-27 11:00:29 -04002925 rb_inc_page(cpu_buffer, &cpu_buffer->head_page);
Steven Rostedtd7690412008-10-01 00:29:53 -04002926
2927 /* Finally update the reader page to the new head */
2928 cpu_buffer->reader_page = reader;
2929 rb_reset_reader_page(cpu_buffer);
2930
2931 goto again;
2932
2933 out:
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01002934 arch_spin_unlock(&cpu_buffer->lock);
Steven Rostedt3e03fb72008-11-06 00:09:43 -05002935 local_irq_restore(flags);
Steven Rostedtd7690412008-10-01 00:29:53 -04002936
2937 return reader;
2938}
2939
2940static void rb_advance_reader(struct ring_buffer_per_cpu *cpu_buffer)
2941{
2942 struct ring_buffer_event *event;
2943 struct buffer_page *reader;
2944 unsigned length;
2945
2946 reader = rb_get_reader_page(cpu_buffer);
2947
2948 /* This function should not be called when buffer is empty */
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05002949 if (RB_WARN_ON(cpu_buffer, !reader))
2950 return;
Steven Rostedtd7690412008-10-01 00:29:53 -04002951
2952 event = rb_reader_event(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002953
Steven Rostedta1863c22009-09-03 10:23:58 -04002954 if (event->type_len <= RINGBUF_TYPE_DATA_TYPE_LEN_MAX)
Steven Rostedte4906ef2009-04-30 20:49:44 -04002955 cpu_buffer->read++;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002956
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002957 rb_update_read_stamp(cpu_buffer, event);
2958
Steven Rostedtd7690412008-10-01 00:29:53 -04002959 length = rb_event_length(event);
Steven Rostedt6f807ac2008-10-04 02:00:58 -04002960 cpu_buffer->reader_page->read += length;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002961}
2962
2963static void rb_advance_iter(struct ring_buffer_iter *iter)
2964{
2965 struct ring_buffer *buffer;
2966 struct ring_buffer_per_cpu *cpu_buffer;
2967 struct ring_buffer_event *event;
2968 unsigned length;
2969
2970 cpu_buffer = iter->cpu_buffer;
2971 buffer = cpu_buffer->buffer;
2972
2973 /*
2974 * Check if we are at the end of the buffer.
2975 */
Steven Rostedtbf41a152008-10-04 02:00:59 -04002976 if (iter->head >= rb_page_size(iter->head_page)) {
Steven Rostedtea05b572009-06-03 09:30:10 -04002977 /* discarded commits can make the page empty */
2978 if (iter->head_page == cpu_buffer->commit_page)
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05002979 return;
Steven Rostedtd7690412008-10-01 00:29:53 -04002980 rb_inc_iter(iter);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002981 return;
2982 }
2983
2984 event = rb_iter_head_event(iter);
2985
2986 length = rb_event_length(event);
2987
2988 /*
2989 * This should not be called to advance the header if we are
2990 * at the tail of the buffer.
2991 */
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05002992 if (RB_WARN_ON(cpu_buffer,
Steven Rostedtf536aaf2008-11-10 23:07:30 -05002993 (iter->head_page == cpu_buffer->commit_page) &&
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05002994 (iter->head + length > rb_commit_index(cpu_buffer))))
2995 return;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002996
2997 rb_update_iter_read_stamp(iter, event);
2998
2999 iter->head += length;
3000
3001 /* check for end of page padding */
Steven Rostedtbf41a152008-10-04 02:00:59 -04003002 if ((iter->head >= rb_page_size(iter->head_page)) &&
3003 (iter->head_page != cpu_buffer->commit_page))
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003004 rb_advance_iter(iter);
3005}
3006
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003007static struct ring_buffer_event *
Robert Richterd8eeb2d2009-07-31 14:58:04 +02003008rb_buffer_peek(struct ring_buffer_per_cpu *cpu_buffer, u64 *ts)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003009{
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003010 struct ring_buffer_event *event;
Steven Rostedtd7690412008-10-01 00:29:53 -04003011 struct buffer_page *reader;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003012 int nr_loops = 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003013
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003014 again:
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003015 /*
3016 * We repeat when a timestamp is encountered. It is possible
3017 * to get multiple timestamps from an interrupt entering just
Steven Rostedtea05b572009-06-03 09:30:10 -04003018 * as one timestamp is about to be written, or from discarded
3019 * commits. The most that we can have is the number on a single page.
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003020 */
Steven Rostedtea05b572009-06-03 09:30:10 -04003021 if (RB_WARN_ON(cpu_buffer, ++nr_loops > RB_TIMESTAMPS_PER_PAGE))
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003022 return NULL;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003023
Steven Rostedtd7690412008-10-01 00:29:53 -04003024 reader = rb_get_reader_page(cpu_buffer);
3025 if (!reader)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003026 return NULL;
3027
Steven Rostedtd7690412008-10-01 00:29:53 -04003028 event = rb_reader_event(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003029
Lai Jiangshan334d4162009-04-24 11:27:05 +08003030 switch (event->type_len) {
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003031 case RINGBUF_TYPE_PADDING:
Tom Zanussi2d622712009-03-22 03:30:49 -05003032 if (rb_null_event(event))
3033 RB_WARN_ON(cpu_buffer, 1);
3034 /*
3035 * Because the writer could be discarding every
3036 * event it creates (which would probably be bad)
3037 * if we were to go back to "again" then we may never
3038 * catch up, and will trigger the warn on, or lock
3039 * the box. Return the padding, and we will release
3040 * the current locks, and try again.
3041 */
Tom Zanussi2d622712009-03-22 03:30:49 -05003042 return event;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003043
3044 case RINGBUF_TYPE_TIME_EXTEND:
3045 /* Internal data, OK to advance */
Steven Rostedtd7690412008-10-01 00:29:53 -04003046 rb_advance_reader(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003047 goto again;
3048
3049 case RINGBUF_TYPE_TIME_STAMP:
3050 /* FIXME: not implemented */
Steven Rostedtd7690412008-10-01 00:29:53 -04003051 rb_advance_reader(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003052 goto again;
3053
3054 case RINGBUF_TYPE_DATA:
3055 if (ts) {
3056 *ts = cpu_buffer->read_stamp + event->time_delta;
Robert Richterd8eeb2d2009-07-31 14:58:04 +02003057 ring_buffer_normalize_time_stamp(cpu_buffer->buffer,
Steven Rostedt37886f62009-03-17 17:22:06 -04003058 cpu_buffer->cpu, ts);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003059 }
3060 return event;
3061
3062 default:
3063 BUG();
3064 }
3065
3066 return NULL;
3067}
Robert Richterc4f50182008-12-11 16:49:22 +01003068EXPORT_SYMBOL_GPL(ring_buffer_peek);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003069
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003070static struct ring_buffer_event *
3071rb_iter_peek(struct ring_buffer_iter *iter, u64 *ts)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003072{
3073 struct ring_buffer *buffer;
3074 struct ring_buffer_per_cpu *cpu_buffer;
3075 struct ring_buffer_event *event;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003076 int nr_loops = 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003077
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003078 cpu_buffer = iter->cpu_buffer;
3079 buffer = cpu_buffer->buffer;
3080
Steven Rostedt492a74f2010-01-25 15:17:47 -05003081 /*
3082 * Check if someone performed a consuming read to
3083 * the buffer. A consuming read invalidates the iterator
3084 * and we need to reset the iterator in this case.
3085 */
3086 if (unlikely(iter->cache_read != cpu_buffer->read ||
3087 iter->cache_reader_page != cpu_buffer->reader_page))
3088 rb_iter_reset(iter);
3089
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003090 again:
Steven Rostedt3c05d742010-01-26 16:14:08 -05003091 if (ring_buffer_iter_empty(iter))
3092 return NULL;
3093
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003094 /*
Steven Rostedtea05b572009-06-03 09:30:10 -04003095 * We repeat when a timestamp is encountered.
3096 * We can get multiple timestamps by nested interrupts or also
3097 * if filtering is on (discarding commits). Since discarding
3098 * commits can be frequent we can get a lot of timestamps.
3099 * But we limit them by not adding timestamps if they begin
3100 * at the start of a page.
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003101 */
Steven Rostedtea05b572009-06-03 09:30:10 -04003102 if (RB_WARN_ON(cpu_buffer, ++nr_loops > RB_TIMESTAMPS_PER_PAGE))
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003103 return NULL;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003104
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003105 if (rb_per_cpu_empty(cpu_buffer))
3106 return NULL;
3107
Steven Rostedt3c05d742010-01-26 16:14:08 -05003108 if (iter->head >= local_read(&iter->head_page->page->commit)) {
3109 rb_inc_iter(iter);
3110 goto again;
3111 }
3112
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003113 event = rb_iter_head_event(iter);
3114
Lai Jiangshan334d4162009-04-24 11:27:05 +08003115 switch (event->type_len) {
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003116 case RINGBUF_TYPE_PADDING:
Tom Zanussi2d622712009-03-22 03:30:49 -05003117 if (rb_null_event(event)) {
3118 rb_inc_iter(iter);
3119 goto again;
3120 }
3121 rb_advance_iter(iter);
3122 return event;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003123
3124 case RINGBUF_TYPE_TIME_EXTEND:
3125 /* Internal data, OK to advance */
3126 rb_advance_iter(iter);
3127 goto again;
3128
3129 case RINGBUF_TYPE_TIME_STAMP:
3130 /* FIXME: not implemented */
3131 rb_advance_iter(iter);
3132 goto again;
3133
3134 case RINGBUF_TYPE_DATA:
3135 if (ts) {
3136 *ts = iter->read_stamp + event->time_delta;
Steven Rostedt37886f62009-03-17 17:22:06 -04003137 ring_buffer_normalize_time_stamp(buffer,
3138 cpu_buffer->cpu, ts);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003139 }
3140 return event;
3141
3142 default:
3143 BUG();
3144 }
3145
3146 return NULL;
3147}
Robert Richterc4f50182008-12-11 16:49:22 +01003148EXPORT_SYMBOL_GPL(ring_buffer_iter_peek);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003149
Steven Rostedt8d707e82009-06-16 21:22:48 -04003150static inline int rb_ok_to_lock(void)
3151{
3152 /*
3153 * If an NMI die dumps out the content of the ring buffer
3154 * do not grab locks. We also permanently disable the ring
3155 * buffer too. A one time deal is all you get from reading
3156 * the ring buffer from an NMI.
3157 */
Steven Rostedt464e85e2009-08-05 15:26:37 -04003158 if (likely(!in_nmi()))
Steven Rostedt8d707e82009-06-16 21:22:48 -04003159 return 1;
3160
3161 tracing_off_permanent();
3162 return 0;
3163}
3164
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003165/**
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003166 * ring_buffer_peek - peek at the next event to be read
3167 * @buffer: The ring buffer to read
3168 * @cpu: The cpu to peak at
3169 * @ts: The timestamp counter of this event.
3170 *
3171 * This will return the event that will be read next, but does
3172 * not consume the data.
3173 */
3174struct ring_buffer_event *
3175ring_buffer_peek(struct ring_buffer *buffer, int cpu, u64 *ts)
3176{
3177 struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
Steven Rostedt8aabee52009-03-12 13:13:49 -04003178 struct ring_buffer_event *event;
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003179 unsigned long flags;
Steven Rostedt8d707e82009-06-16 21:22:48 -04003180 int dolock;
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003181
Steven Rostedt554f7862009-03-11 22:00:13 -04003182 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04003183 return NULL;
Steven Rostedt554f7862009-03-11 22:00:13 -04003184
Steven Rostedt8d707e82009-06-16 21:22:48 -04003185 dolock = rb_ok_to_lock();
Tom Zanussi2d622712009-03-22 03:30:49 -05003186 again:
Steven Rostedt8d707e82009-06-16 21:22:48 -04003187 local_irq_save(flags);
3188 if (dolock)
3189 spin_lock(&cpu_buffer->reader_lock);
Robert Richterd8eeb2d2009-07-31 14:58:04 +02003190 event = rb_buffer_peek(cpu_buffer, ts);
Robert Richter469535a2009-07-30 19:19:18 +02003191 if (event && event->type_len == RINGBUF_TYPE_PADDING)
3192 rb_advance_reader(cpu_buffer);
Steven Rostedt8d707e82009-06-16 21:22:48 -04003193 if (dolock)
3194 spin_unlock(&cpu_buffer->reader_lock);
3195 local_irq_restore(flags);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003196
Steven Rostedt1b959e12009-09-03 10:12:13 -04003197 if (event && event->type_len == RINGBUF_TYPE_PADDING)
Tom Zanussi2d622712009-03-22 03:30:49 -05003198 goto again;
Tom Zanussi2d622712009-03-22 03:30:49 -05003199
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003200 return event;
3201}
3202
3203/**
3204 * ring_buffer_iter_peek - peek at the next event to be read
3205 * @iter: The ring buffer iterator
3206 * @ts: The timestamp counter of this event.
3207 *
3208 * This will return the event that will be read next, but does
3209 * not increment the iterator.
3210 */
3211struct ring_buffer_event *
3212ring_buffer_iter_peek(struct ring_buffer_iter *iter, u64 *ts)
3213{
3214 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
3215 struct ring_buffer_event *event;
3216 unsigned long flags;
3217
Tom Zanussi2d622712009-03-22 03:30:49 -05003218 again:
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003219 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
3220 event = rb_iter_peek(iter, ts);
3221 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
3222
Steven Rostedt1b959e12009-09-03 10:12:13 -04003223 if (event && event->type_len == RINGBUF_TYPE_PADDING)
Tom Zanussi2d622712009-03-22 03:30:49 -05003224 goto again;
Tom Zanussi2d622712009-03-22 03:30:49 -05003225
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003226 return event;
3227}
3228
3229/**
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003230 * ring_buffer_consume - return an event and consume it
3231 * @buffer: The ring buffer to get the next event from
3232 *
3233 * Returns the next event in the ring buffer, and that event is consumed.
3234 * Meaning, that sequential reads will keep returning a different event,
3235 * and eventually empty the ring buffer if the producer is slower.
3236 */
3237struct ring_buffer_event *
3238ring_buffer_consume(struct ring_buffer *buffer, int cpu, u64 *ts)
3239{
Steven Rostedt554f7862009-03-11 22:00:13 -04003240 struct ring_buffer_per_cpu *cpu_buffer;
3241 struct ring_buffer_event *event = NULL;
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003242 unsigned long flags;
Steven Rostedt8d707e82009-06-16 21:22:48 -04003243 int dolock;
3244
3245 dolock = rb_ok_to_lock();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003246
Tom Zanussi2d622712009-03-22 03:30:49 -05003247 again:
Steven Rostedt554f7862009-03-11 22:00:13 -04003248 /* might be called in atomic */
3249 preempt_disable();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003250
Steven Rostedt554f7862009-03-11 22:00:13 -04003251 if (!cpumask_test_cpu(cpu, buffer->cpumask))
3252 goto out;
3253
3254 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt8d707e82009-06-16 21:22:48 -04003255 local_irq_save(flags);
3256 if (dolock)
3257 spin_lock(&cpu_buffer->reader_lock);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003258
Robert Richterd8eeb2d2009-07-31 14:58:04 +02003259 event = rb_buffer_peek(cpu_buffer, ts);
Robert Richter469535a2009-07-30 19:19:18 +02003260 if (event)
3261 rb_advance_reader(cpu_buffer);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003262
Steven Rostedt8d707e82009-06-16 21:22:48 -04003263 if (dolock)
3264 spin_unlock(&cpu_buffer->reader_lock);
3265 local_irq_restore(flags);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003266
Steven Rostedt554f7862009-03-11 22:00:13 -04003267 out:
3268 preempt_enable();
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 Rostedt7a8e76a2008-09-29 23:02:38 -04003273 return event;
3274}
Robert Richterc4f50182008-12-11 16:49:22 +01003275EXPORT_SYMBOL_GPL(ring_buffer_consume);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003276
3277/**
3278 * ring_buffer_read_start - start a non consuming read of the buffer
3279 * @buffer: The ring buffer to read from
3280 * @cpu: The cpu buffer to iterate over
3281 *
3282 * This starts up an iteration through the buffer. It also disables
3283 * the recording to the buffer until the reading is finished.
3284 * This prevents the reading from being corrupted. This is not
3285 * a consuming read, so a producer is not expected.
3286 *
3287 * Must be paired with ring_buffer_finish.
3288 */
3289struct ring_buffer_iter *
3290ring_buffer_read_start(struct ring_buffer *buffer, int cpu)
3291{
3292 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedt8aabee52009-03-12 13:13:49 -04003293 struct ring_buffer_iter *iter;
Steven Rostedtd7690412008-10-01 00:29:53 -04003294 unsigned long flags;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003295
Rusty Russell9e01c1b2009-01-01 10:12:22 +10303296 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04003297 return NULL;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003298
3299 iter = kmalloc(sizeof(*iter), GFP_KERNEL);
3300 if (!iter)
Steven Rostedt8aabee52009-03-12 13:13:49 -04003301 return NULL;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003302
3303 cpu_buffer = buffer->buffers[cpu];
3304
3305 iter->cpu_buffer = cpu_buffer;
3306
3307 atomic_inc(&cpu_buffer->record_disabled);
3308 synchronize_sched();
3309
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003310 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01003311 arch_spin_lock(&cpu_buffer->lock);
Steven Rostedt642edba2008-11-12 00:01:26 -05003312 rb_iter_reset(iter);
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01003313 arch_spin_unlock(&cpu_buffer->lock);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003314 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003315
3316 return iter;
3317}
Robert Richterc4f50182008-12-11 16:49:22 +01003318EXPORT_SYMBOL_GPL(ring_buffer_read_start);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003319
3320/**
3321 * ring_buffer_finish - finish reading the iterator of the buffer
3322 * @iter: The iterator retrieved by ring_buffer_start
3323 *
3324 * This re-enables the recording to the buffer, and frees the
3325 * iterator.
3326 */
3327void
3328ring_buffer_read_finish(struct ring_buffer_iter *iter)
3329{
3330 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
3331
3332 atomic_dec(&cpu_buffer->record_disabled);
3333 kfree(iter);
3334}
Robert Richterc4f50182008-12-11 16:49:22 +01003335EXPORT_SYMBOL_GPL(ring_buffer_read_finish);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003336
3337/**
3338 * ring_buffer_read - read the next item in the ring buffer by the iterator
3339 * @iter: The ring buffer iterator
3340 * @ts: The time stamp of the event read.
3341 *
3342 * This reads the next event in the ring buffer and increments the iterator.
3343 */
3344struct ring_buffer_event *
3345ring_buffer_read(struct ring_buffer_iter *iter, u64 *ts)
3346{
3347 struct ring_buffer_event *event;
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003348 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
3349 unsigned long flags;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003350
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003351 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
Steven Rostedt7e9391c2009-09-03 10:02:09 -04003352 again:
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003353 event = rb_iter_peek(iter, ts);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003354 if (!event)
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003355 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003356
Steven Rostedt7e9391c2009-09-03 10:02:09 -04003357 if (event->type_len == RINGBUF_TYPE_PADDING)
3358 goto again;
3359
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003360 rb_advance_iter(iter);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003361 out:
3362 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003363
3364 return event;
3365}
Robert Richterc4f50182008-12-11 16:49:22 +01003366EXPORT_SYMBOL_GPL(ring_buffer_read);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003367
3368/**
3369 * ring_buffer_size - return the size of the ring buffer (in bytes)
3370 * @buffer: The ring buffer.
3371 */
3372unsigned long ring_buffer_size(struct ring_buffer *buffer)
3373{
3374 return BUF_PAGE_SIZE * buffer->pages;
3375}
Robert Richterc4f50182008-12-11 16:49:22 +01003376EXPORT_SYMBOL_GPL(ring_buffer_size);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003377
3378static void
3379rb_reset_cpu(struct ring_buffer_per_cpu *cpu_buffer)
3380{
Steven Rostedt77ae3652009-03-27 11:00:29 -04003381 rb_head_page_deactivate(cpu_buffer);
3382
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003383 cpu_buffer->head_page
Steven Rostedt3adc54f2009-03-30 15:32:01 -04003384 = list_entry(cpu_buffer->pages, struct buffer_page, list);
Steven Rostedtbf41a152008-10-04 02:00:59 -04003385 local_set(&cpu_buffer->head_page->write, 0);
Steven Rostedt778c55d2009-05-01 18:44:45 -04003386 local_set(&cpu_buffer->head_page->entries, 0);
Steven Rostedtabc9b562008-12-02 15:34:06 -05003387 local_set(&cpu_buffer->head_page->page->commit, 0);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003388
Steven Rostedt6f807ac2008-10-04 02:00:58 -04003389 cpu_buffer->head_page->read = 0;
Steven Rostedtbf41a152008-10-04 02:00:59 -04003390
3391 cpu_buffer->tail_page = cpu_buffer->head_page;
3392 cpu_buffer->commit_page = cpu_buffer->head_page;
3393
3394 INIT_LIST_HEAD(&cpu_buffer->reader_page->list);
3395 local_set(&cpu_buffer->reader_page->write, 0);
Steven Rostedt778c55d2009-05-01 18:44:45 -04003396 local_set(&cpu_buffer->reader_page->entries, 0);
Steven Rostedtabc9b562008-12-02 15:34:06 -05003397 local_set(&cpu_buffer->reader_page->page->commit, 0);
Steven Rostedt6f807ac2008-10-04 02:00:58 -04003398 cpu_buffer->reader_page->read = 0;
Steven Rostedtd7690412008-10-01 00:29:53 -04003399
Steven Rostedt77ae3652009-03-27 11:00:29 -04003400 local_set(&cpu_buffer->commit_overrun, 0);
3401 local_set(&cpu_buffer->overrun, 0);
Steven Rostedte4906ef2009-04-30 20:49:44 -04003402 local_set(&cpu_buffer->entries, 0);
Steven Rostedtfa743952009-06-16 12:37:57 -04003403 local_set(&cpu_buffer->committing, 0);
3404 local_set(&cpu_buffer->commits, 0);
Steven Rostedt77ae3652009-03-27 11:00:29 -04003405 cpu_buffer->read = 0;
Steven Rostedt69507c02009-01-21 18:45:57 -05003406
3407 cpu_buffer->write_stamp = 0;
3408 cpu_buffer->read_stamp = 0;
Steven Rostedt77ae3652009-03-27 11:00:29 -04003409
3410 rb_head_page_activate(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003411}
3412
3413/**
3414 * ring_buffer_reset_cpu - reset a ring buffer per CPU buffer
3415 * @buffer: The ring buffer to reset a per cpu buffer of
3416 * @cpu: The CPU buffer to be reset
3417 */
3418void ring_buffer_reset_cpu(struct ring_buffer *buffer, int cpu)
3419{
3420 struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
3421 unsigned long flags;
3422
Rusty Russell9e01c1b2009-01-01 10:12:22 +10303423 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04003424 return;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003425
Steven Rostedt41ede232009-05-01 20:26:54 -04003426 atomic_inc(&cpu_buffer->record_disabled);
3427
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003428 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
3429
Steven Rostedt41b6a952009-09-02 09:59:48 -04003430 if (RB_WARN_ON(cpu_buffer, local_read(&cpu_buffer->committing)))
3431 goto out;
3432
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01003433 arch_spin_lock(&cpu_buffer->lock);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003434
3435 rb_reset_cpu(cpu_buffer);
3436
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01003437 arch_spin_unlock(&cpu_buffer->lock);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003438
Steven Rostedt41b6a952009-09-02 09:59:48 -04003439 out:
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003440 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
Steven Rostedt41ede232009-05-01 20:26:54 -04003441
3442 atomic_dec(&cpu_buffer->record_disabled);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003443}
Robert Richterc4f50182008-12-11 16:49:22 +01003444EXPORT_SYMBOL_GPL(ring_buffer_reset_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003445
3446/**
3447 * ring_buffer_reset - reset a ring buffer
3448 * @buffer: The ring buffer to reset all cpu buffers
3449 */
3450void ring_buffer_reset(struct ring_buffer *buffer)
3451{
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003452 int cpu;
3453
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003454 for_each_buffer_cpu(buffer, cpu)
Steven Rostedtd7690412008-10-01 00:29:53 -04003455 ring_buffer_reset_cpu(buffer, cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003456}
Robert Richterc4f50182008-12-11 16:49:22 +01003457EXPORT_SYMBOL_GPL(ring_buffer_reset);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003458
3459/**
3460 * rind_buffer_empty - is the ring buffer empty?
3461 * @buffer: The ring buffer to test
3462 */
3463int ring_buffer_empty(struct ring_buffer *buffer)
3464{
3465 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedtd4788202009-06-17 00:39:43 -04003466 unsigned long flags;
Steven Rostedt8d707e82009-06-16 21:22:48 -04003467 int dolock;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003468 int cpu;
Steven Rostedtd4788202009-06-17 00:39:43 -04003469 int ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003470
Steven Rostedt8d707e82009-06-16 21:22:48 -04003471 dolock = rb_ok_to_lock();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003472
3473 /* yes this is racy, but if you don't like the race, lock the buffer */
3474 for_each_buffer_cpu(buffer, cpu) {
3475 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt8d707e82009-06-16 21:22:48 -04003476 local_irq_save(flags);
3477 if (dolock)
3478 spin_lock(&cpu_buffer->reader_lock);
Steven Rostedtd4788202009-06-17 00:39:43 -04003479 ret = rb_per_cpu_empty(cpu_buffer);
Steven Rostedt8d707e82009-06-16 21:22:48 -04003480 if (dolock)
3481 spin_unlock(&cpu_buffer->reader_lock);
3482 local_irq_restore(flags);
3483
Steven Rostedtd4788202009-06-17 00:39:43 -04003484 if (!ret)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003485 return 0;
3486 }
Steven Rostedt554f7862009-03-11 22:00:13 -04003487
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003488 return 1;
3489}
Robert Richterc4f50182008-12-11 16:49:22 +01003490EXPORT_SYMBOL_GPL(ring_buffer_empty);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003491
3492/**
3493 * ring_buffer_empty_cpu - is a cpu buffer of a ring buffer empty?
3494 * @buffer: The ring buffer
3495 * @cpu: The CPU buffer to test
3496 */
3497int ring_buffer_empty_cpu(struct ring_buffer *buffer, int cpu)
3498{
3499 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedtd4788202009-06-17 00:39:43 -04003500 unsigned long flags;
Steven Rostedt8d707e82009-06-16 21:22:48 -04003501 int dolock;
Steven Rostedt8aabee52009-03-12 13:13:49 -04003502 int ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003503
Rusty Russell9e01c1b2009-01-01 10:12:22 +10303504 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04003505 return 1;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003506
Steven Rostedt8d707e82009-06-16 21:22:48 -04003507 dolock = rb_ok_to_lock();
Steven Rostedt554f7862009-03-11 22:00:13 -04003508
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003509 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt8d707e82009-06-16 21:22:48 -04003510 local_irq_save(flags);
3511 if (dolock)
3512 spin_lock(&cpu_buffer->reader_lock);
Steven Rostedt554f7862009-03-11 22:00:13 -04003513 ret = rb_per_cpu_empty(cpu_buffer);
Steven Rostedt8d707e82009-06-16 21:22:48 -04003514 if (dolock)
3515 spin_unlock(&cpu_buffer->reader_lock);
3516 local_irq_restore(flags);
Steven Rostedt554f7862009-03-11 22:00:13 -04003517
3518 return ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003519}
Robert Richterc4f50182008-12-11 16:49:22 +01003520EXPORT_SYMBOL_GPL(ring_buffer_empty_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003521
Steven Rostedt85bac322009-09-04 14:24:40 -04003522#ifdef CONFIG_RING_BUFFER_ALLOW_SWAP
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003523/**
3524 * ring_buffer_swap_cpu - swap a CPU buffer between two ring buffers
3525 * @buffer_a: One buffer to swap with
3526 * @buffer_b: The other buffer to swap with
3527 *
3528 * This function is useful for tracers that want to take a "snapshot"
3529 * of a CPU buffer and has another back up buffer lying around.
3530 * it is expected that the tracer handles the cpu buffer not being
3531 * used at the moment.
3532 */
3533int ring_buffer_swap_cpu(struct ring_buffer *buffer_a,
3534 struct ring_buffer *buffer_b, int cpu)
3535{
3536 struct ring_buffer_per_cpu *cpu_buffer_a;
3537 struct ring_buffer_per_cpu *cpu_buffer_b;
Steven Rostedt554f7862009-03-11 22:00:13 -04003538 int ret = -EINVAL;
3539
Rusty Russell9e01c1b2009-01-01 10:12:22 +10303540 if (!cpumask_test_cpu(cpu, buffer_a->cpumask) ||
3541 !cpumask_test_cpu(cpu, buffer_b->cpumask))
Steven Rostedt554f7862009-03-11 22:00:13 -04003542 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003543
3544 /* At least make sure the two buffers are somewhat the same */
Lai Jiangshan6d102bc2008-12-17 17:48:23 +08003545 if (buffer_a->pages != buffer_b->pages)
Steven Rostedt554f7862009-03-11 22:00:13 -04003546 goto out;
3547
3548 ret = -EAGAIN;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003549
Steven Rostedt97b17ef2009-01-21 15:24:56 -05003550 if (ring_buffer_flags != RB_BUFFERS_ON)
Steven Rostedt554f7862009-03-11 22:00:13 -04003551 goto out;
Steven Rostedt97b17ef2009-01-21 15:24:56 -05003552
3553 if (atomic_read(&buffer_a->record_disabled))
Steven Rostedt554f7862009-03-11 22:00:13 -04003554 goto out;
Steven Rostedt97b17ef2009-01-21 15:24:56 -05003555
3556 if (atomic_read(&buffer_b->record_disabled))
Steven Rostedt554f7862009-03-11 22:00:13 -04003557 goto out;
Steven Rostedt97b17ef2009-01-21 15:24:56 -05003558
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003559 cpu_buffer_a = buffer_a->buffers[cpu];
3560 cpu_buffer_b = buffer_b->buffers[cpu];
3561
Steven Rostedt97b17ef2009-01-21 15:24:56 -05003562 if (atomic_read(&cpu_buffer_a->record_disabled))
Steven Rostedt554f7862009-03-11 22:00:13 -04003563 goto out;
Steven Rostedt97b17ef2009-01-21 15:24:56 -05003564
3565 if (atomic_read(&cpu_buffer_b->record_disabled))
Steven Rostedt554f7862009-03-11 22:00:13 -04003566 goto out;
Steven Rostedt97b17ef2009-01-21 15:24:56 -05003567
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003568 /*
3569 * We can't do a synchronize_sched here because this
3570 * function can be called in atomic context.
3571 * Normally this will be called from the same CPU as cpu.
3572 * If not it's up to the caller to protect this.
3573 */
3574 atomic_inc(&cpu_buffer_a->record_disabled);
3575 atomic_inc(&cpu_buffer_b->record_disabled);
3576
Steven Rostedt98277992009-09-02 10:56:15 -04003577 ret = -EBUSY;
3578 if (local_read(&cpu_buffer_a->committing))
3579 goto out_dec;
3580 if (local_read(&cpu_buffer_b->committing))
3581 goto out_dec;
3582
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003583 buffer_a->buffers[cpu] = cpu_buffer_b;
3584 buffer_b->buffers[cpu] = cpu_buffer_a;
3585
3586 cpu_buffer_b->buffer = buffer_a;
3587 cpu_buffer_a->buffer = buffer_b;
3588
Steven Rostedt98277992009-09-02 10:56:15 -04003589 ret = 0;
3590
3591out_dec:
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003592 atomic_dec(&cpu_buffer_a->record_disabled);
3593 atomic_dec(&cpu_buffer_b->record_disabled);
Steven Rostedt554f7862009-03-11 22:00:13 -04003594out:
Steven Rostedt554f7862009-03-11 22:00:13 -04003595 return ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003596}
Robert Richterc4f50182008-12-11 16:49:22 +01003597EXPORT_SYMBOL_GPL(ring_buffer_swap_cpu);
Steven Rostedt85bac322009-09-04 14:24:40 -04003598#endif /* CONFIG_RING_BUFFER_ALLOW_SWAP */
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003599
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003600/**
3601 * ring_buffer_alloc_read_page - allocate a page to read from buffer
3602 * @buffer: the buffer to allocate for.
3603 *
3604 * This function is used in conjunction with ring_buffer_read_page.
3605 * When reading a full page from the ring buffer, these functions
3606 * can be used to speed up the process. The calling function should
3607 * allocate a few pages first with this function. Then when it
3608 * needs to get pages from the ring buffer, it passes the result
3609 * of this function into ring_buffer_read_page, which will swap
3610 * the page that was allocated, with the read page of the buffer.
3611 *
3612 * Returns:
3613 * The page allocated, or NULL on error.
3614 */
3615void *ring_buffer_alloc_read_page(struct ring_buffer *buffer)
3616{
Steven Rostedt044fa782008-12-02 23:50:03 -05003617 struct buffer_data_page *bpage;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003618 unsigned long addr;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003619
3620 addr = __get_free_page(GFP_KERNEL);
3621 if (!addr)
3622 return NULL;
3623
Steven Rostedt044fa782008-12-02 23:50:03 -05003624 bpage = (void *)addr;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003625
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003626 rb_init_page(bpage);
3627
Steven Rostedt044fa782008-12-02 23:50:03 -05003628 return bpage;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003629}
Steven Rostedtd6ce96d2009-05-05 01:15:24 -04003630EXPORT_SYMBOL_GPL(ring_buffer_alloc_read_page);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003631
3632/**
3633 * ring_buffer_free_read_page - free an allocated read page
3634 * @buffer: the buffer the page was allocate for
3635 * @data: the page to free
3636 *
3637 * Free a page allocated from ring_buffer_alloc_read_page.
3638 */
3639void ring_buffer_free_read_page(struct ring_buffer *buffer, void *data)
3640{
3641 free_page((unsigned long)data);
3642}
Steven Rostedtd6ce96d2009-05-05 01:15:24 -04003643EXPORT_SYMBOL_GPL(ring_buffer_free_read_page);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003644
3645/**
3646 * ring_buffer_read_page - extract a page from the ring buffer
3647 * @buffer: buffer to extract from
3648 * @data_page: the page to use allocated from ring_buffer_alloc_read_page
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003649 * @len: amount to extract
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003650 * @cpu: the cpu of the buffer to extract
3651 * @full: should the extraction only happen when the page is full.
3652 *
3653 * This function will pull out a page from the ring buffer and consume it.
3654 * @data_page must be the address of the variable that was returned
3655 * from ring_buffer_alloc_read_page. This is because the page might be used
3656 * to swap with a page in the ring buffer.
3657 *
3658 * for example:
Lai Jiangshanb85fa012009-02-09 14:21:14 +08003659 * rpage = ring_buffer_alloc_read_page(buffer);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003660 * if (!rpage)
3661 * return error;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003662 * ret = ring_buffer_read_page(buffer, &rpage, len, cpu, 0);
Lai Jiangshan667d2412009-02-09 14:21:17 +08003663 * if (ret >= 0)
3664 * process_page(rpage, ret);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003665 *
3666 * When @full is set, the function will not return true unless
3667 * the writer is off the reader page.
3668 *
3669 * Note: it is up to the calling functions to handle sleeps and wakeups.
3670 * The ring buffer can be used anywhere in the kernel and can not
3671 * blindly call wake_up. The layer that uses the ring buffer must be
3672 * responsible for that.
3673 *
3674 * Returns:
Lai Jiangshan667d2412009-02-09 14:21:17 +08003675 * >=0 if data has been transferred, returns the offset of consumed data.
3676 * <0 if no data has been transferred.
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003677 */
3678int ring_buffer_read_page(struct ring_buffer *buffer,
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003679 void **data_page, size_t len, int cpu, int full)
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003680{
3681 struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
3682 struct ring_buffer_event *event;
Steven Rostedt044fa782008-12-02 23:50:03 -05003683 struct buffer_data_page *bpage;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003684 struct buffer_page *reader;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003685 unsigned long flags;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003686 unsigned int commit;
Lai Jiangshan667d2412009-02-09 14:21:17 +08003687 unsigned int read;
Steven Rostedt4f3640f2009-03-03 23:52:42 -05003688 u64 save_timestamp;
Lai Jiangshan667d2412009-02-09 14:21:17 +08003689 int ret = -1;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003690
Steven Rostedt554f7862009-03-11 22:00:13 -04003691 if (!cpumask_test_cpu(cpu, buffer->cpumask))
3692 goto out;
3693
Steven Rostedt474d32b2009-03-03 19:51:40 -05003694 /*
3695 * If len is not big enough to hold the page header, then
3696 * we can not copy anything.
3697 */
3698 if (len <= BUF_PAGE_HDR_SIZE)
Steven Rostedt554f7862009-03-11 22:00:13 -04003699 goto out;
Steven Rostedt474d32b2009-03-03 19:51:40 -05003700
3701 len -= BUF_PAGE_HDR_SIZE;
3702
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003703 if (!data_page)
Steven Rostedt554f7862009-03-11 22:00:13 -04003704 goto out;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003705
Steven Rostedt044fa782008-12-02 23:50:03 -05003706 bpage = *data_page;
3707 if (!bpage)
Steven Rostedt554f7862009-03-11 22:00:13 -04003708 goto out;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003709
3710 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
3711
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003712 reader = rb_get_reader_page(cpu_buffer);
3713 if (!reader)
Steven Rostedt554f7862009-03-11 22:00:13 -04003714 goto out_unlock;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003715
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003716 event = rb_reader_event(cpu_buffer);
Lai Jiangshan667d2412009-02-09 14:21:17 +08003717
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003718 read = reader->read;
3719 commit = rb_page_commit(reader);
3720
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003721 /*
Steven Rostedt474d32b2009-03-03 19:51:40 -05003722 * If this page has been partially read or
3723 * if len is not big enough to read the rest of the page or
3724 * a writer is still on the page, then
3725 * we must copy the data from the page to the buffer.
3726 * Otherwise, we can simply swap the page with the one passed in.
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003727 */
Steven Rostedt474d32b2009-03-03 19:51:40 -05003728 if (read || (len < (commit - read)) ||
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003729 cpu_buffer->reader_page == cpu_buffer->commit_page) {
Lai Jiangshan667d2412009-02-09 14:21:17 +08003730 struct buffer_data_page *rpage = cpu_buffer->reader_page->page;
Steven Rostedt474d32b2009-03-03 19:51:40 -05003731 unsigned int rpos = read;
3732 unsigned int pos = 0;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003733 unsigned int size;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003734
3735 if (full)
Steven Rostedt554f7862009-03-11 22:00:13 -04003736 goto out_unlock;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003737
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003738 if (len > (commit - read))
3739 len = (commit - read);
3740
3741 size = rb_event_length(event);
3742
3743 if (len < size)
Steven Rostedt554f7862009-03-11 22:00:13 -04003744 goto out_unlock;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003745
Steven Rostedt4f3640f2009-03-03 23:52:42 -05003746 /* save the current timestamp, since the user will need it */
3747 save_timestamp = cpu_buffer->read_stamp;
3748
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003749 /* Need to copy one event at a time */
3750 do {
Steven Rostedt474d32b2009-03-03 19:51:40 -05003751 memcpy(bpage->data + pos, rpage->data + rpos, size);
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003752
3753 len -= size;
3754
3755 rb_advance_reader(cpu_buffer);
Steven Rostedt474d32b2009-03-03 19:51:40 -05003756 rpos = reader->read;
3757 pos += size;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003758
3759 event = rb_reader_event(cpu_buffer);
3760 size = rb_event_length(event);
3761 } while (len > size);
Lai Jiangshan667d2412009-02-09 14:21:17 +08003762
3763 /* update bpage */
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003764 local_set(&bpage->commit, pos);
Steven Rostedt4f3640f2009-03-03 23:52:42 -05003765 bpage->time_stamp = save_timestamp;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003766
Steven Rostedt474d32b2009-03-03 19:51:40 -05003767 /* we copied everything to the beginning */
3768 read = 0;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003769 } else {
Steven Rostedtafbab762009-05-01 19:40:05 -04003770 /* update the entry counter */
Steven Rostedt77ae3652009-03-27 11:00:29 -04003771 cpu_buffer->read += rb_page_entries(reader);
Steven Rostedtafbab762009-05-01 19:40:05 -04003772
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003773 /* swap the pages */
Steven Rostedt044fa782008-12-02 23:50:03 -05003774 rb_init_page(bpage);
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003775 bpage = reader->page;
3776 reader->page = *data_page;
3777 local_set(&reader->write, 0);
Steven Rostedt778c55d2009-05-01 18:44:45 -04003778 local_set(&reader->entries, 0);
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003779 reader->read = 0;
Steven Rostedt044fa782008-12-02 23:50:03 -05003780 *data_page = bpage;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003781 }
Lai Jiangshan667d2412009-02-09 14:21:17 +08003782 ret = read;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003783
Steven Rostedt554f7862009-03-11 22:00:13 -04003784 out_unlock:
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003785 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
3786
Steven Rostedt554f7862009-03-11 22:00:13 -04003787 out:
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003788 return ret;
3789}
Steven Rostedtd6ce96d2009-05-05 01:15:24 -04003790EXPORT_SYMBOL_GPL(ring_buffer_read_page);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003791
Paul Mundt1155de42009-06-25 14:30:12 +09003792#ifdef CONFIG_TRACING
Steven Rostedta3583242008-11-11 15:01:42 -05003793static ssize_t
3794rb_simple_read(struct file *filp, char __user *ubuf,
3795 size_t cnt, loff_t *ppos)
3796{
Hannes Eder5e398412009-02-10 19:44:34 +01003797 unsigned long *p = filp->private_data;
Steven Rostedta3583242008-11-11 15:01:42 -05003798 char buf[64];
3799 int r;
3800
Steven Rostedt033601a2008-11-21 12:41:55 -05003801 if (test_bit(RB_BUFFERS_DISABLED_BIT, p))
3802 r = sprintf(buf, "permanently disabled\n");
3803 else
3804 r = sprintf(buf, "%d\n", test_bit(RB_BUFFERS_ON_BIT, p));
Steven Rostedta3583242008-11-11 15:01:42 -05003805
3806 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
3807}
3808
3809static ssize_t
3810rb_simple_write(struct file *filp, const char __user *ubuf,
3811 size_t cnt, loff_t *ppos)
3812{
Hannes Eder5e398412009-02-10 19:44:34 +01003813 unsigned long *p = filp->private_data;
Steven Rostedta3583242008-11-11 15:01:42 -05003814 char buf[64];
Hannes Eder5e398412009-02-10 19:44:34 +01003815 unsigned long val;
Steven Rostedta3583242008-11-11 15:01:42 -05003816 int ret;
3817
3818 if (cnt >= sizeof(buf))
3819 return -EINVAL;
3820
3821 if (copy_from_user(&buf, ubuf, cnt))
3822 return -EFAULT;
3823
3824 buf[cnt] = 0;
3825
3826 ret = strict_strtoul(buf, 10, &val);
3827 if (ret < 0)
3828 return ret;
3829
Steven Rostedt033601a2008-11-21 12:41:55 -05003830 if (val)
3831 set_bit(RB_BUFFERS_ON_BIT, p);
3832 else
3833 clear_bit(RB_BUFFERS_ON_BIT, p);
Steven Rostedta3583242008-11-11 15:01:42 -05003834
3835 (*ppos)++;
3836
3837 return cnt;
3838}
3839
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003840static const struct file_operations rb_simple_fops = {
Steven Rostedta3583242008-11-11 15:01:42 -05003841 .open = tracing_open_generic,
3842 .read = rb_simple_read,
3843 .write = rb_simple_write,
3844};
3845
3846
3847static __init int rb_init_debugfs(void)
3848{
3849 struct dentry *d_tracer;
Steven Rostedta3583242008-11-11 15:01:42 -05003850
3851 d_tracer = tracing_init_dentry();
3852
Frederic Weisbecker5452af62009-03-27 00:25:38 +01003853 trace_create_file("tracing_on", 0644, d_tracer,
3854 &ring_buffer_flags, &rb_simple_fops);
Steven Rostedta3583242008-11-11 15:01:42 -05003855
3856 return 0;
3857}
3858
3859fs_initcall(rb_init_debugfs);
Paul Mundt1155de42009-06-25 14:30:12 +09003860#endif
Steven Rostedt554f7862009-03-11 22:00:13 -04003861
Steven Rostedt59222ef2009-03-12 11:46:03 -04003862#ifdef CONFIG_HOTPLUG_CPU
Frederic Weisbecker09c9e842009-03-21 04:33:36 +01003863static int rb_cpu_notify(struct notifier_block *self,
3864 unsigned long action, void *hcpu)
Steven Rostedt554f7862009-03-11 22:00:13 -04003865{
3866 struct ring_buffer *buffer =
3867 container_of(self, struct ring_buffer, cpu_notify);
3868 long cpu = (long)hcpu;
3869
3870 switch (action) {
3871 case CPU_UP_PREPARE:
3872 case CPU_UP_PREPARE_FROZEN:
Rusty Russell3f237a72009-06-12 21:15:30 +09303873 if (cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt554f7862009-03-11 22:00:13 -04003874 return NOTIFY_OK;
3875
3876 buffer->buffers[cpu] =
3877 rb_allocate_cpu_buffer(buffer, cpu);
3878 if (!buffer->buffers[cpu]) {
3879 WARN(1, "failed to allocate ring buffer on CPU %ld\n",
3880 cpu);
3881 return NOTIFY_OK;
3882 }
3883 smp_wmb();
Rusty Russell3f237a72009-06-12 21:15:30 +09303884 cpumask_set_cpu(cpu, buffer->cpumask);
Steven Rostedt554f7862009-03-11 22:00:13 -04003885 break;
3886 case CPU_DOWN_PREPARE:
3887 case CPU_DOWN_PREPARE_FROZEN:
3888 /*
3889 * Do nothing.
3890 * If we were to free the buffer, then the user would
3891 * lose any trace that was in the buffer.
3892 */
3893 break;
3894 default:
3895 break;
3896 }
3897 return NOTIFY_OK;
3898}
3899#endif