blob: d1187ef20caf914048fb897dc5c63f1b302edbf6 [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)))
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05001212 return;
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)))
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05001219 return;
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
Steven Rostedtdd7f5942009-12-10 23:20:52 -05001224 spin_unlock_irq(&cpu_buffer->reader_lock);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001225}
1226
1227static void
1228rb_insert_pages(struct ring_buffer_per_cpu *cpu_buffer,
1229 struct list_head *pages, unsigned nr_pages)
1230{
Steven Rostedt044fa782008-12-02 23:50:03 -05001231 struct buffer_page *bpage;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001232 struct list_head *p;
1233 unsigned i;
1234
Steven Rostedt77ae3652009-03-27 11:00:29 -04001235 spin_lock_irq(&cpu_buffer->reader_lock);
1236 rb_head_page_deactivate(cpu_buffer);
1237
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001238 for (i = 0; i < nr_pages; i++) {
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05001239 if (RB_WARN_ON(cpu_buffer, list_empty(pages)))
1240 return;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001241 p = pages->next;
Steven Rostedt044fa782008-12-02 23:50:03 -05001242 bpage = list_entry(p, struct buffer_page, list);
1243 list_del_init(&bpage->list);
Steven Rostedt3adc54f2009-03-30 15:32:01 -04001244 list_add_tail(&bpage->list, cpu_buffer->pages);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001245 }
1246 rb_reset_cpu(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001247 rb_check_pages(cpu_buffer);
1248
Steven Rostedtdd7f5942009-12-10 23:20:52 -05001249 spin_unlock_irq(&cpu_buffer->reader_lock);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001250}
1251
1252/**
1253 * ring_buffer_resize - resize the ring buffer
1254 * @buffer: the buffer to resize.
1255 * @size: the new size.
1256 *
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001257 * Minimum size is 2 * BUF_PAGE_SIZE.
1258 *
1259 * Returns -1 on failure.
1260 */
1261int ring_buffer_resize(struct ring_buffer *buffer, unsigned long size)
1262{
1263 struct ring_buffer_per_cpu *cpu_buffer;
1264 unsigned nr_pages, rm_pages, new_pages;
Steven Rostedt044fa782008-12-02 23:50:03 -05001265 struct buffer_page *bpage, *tmp;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001266 unsigned long buffer_size;
1267 unsigned long addr;
1268 LIST_HEAD(pages);
1269 int i, cpu;
1270
Ingo Molnaree51a1d2008-11-13 14:58:31 +01001271 /*
1272 * Always succeed at resizing a non-existent buffer:
1273 */
1274 if (!buffer)
1275 return size;
1276
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001277 size = DIV_ROUND_UP(size, BUF_PAGE_SIZE);
1278 size *= BUF_PAGE_SIZE;
1279 buffer_size = buffer->pages * BUF_PAGE_SIZE;
1280
1281 /* we need a minimum of two pages */
1282 if (size < BUF_PAGE_SIZE * 2)
1283 size = BUF_PAGE_SIZE * 2;
1284
1285 if (size == buffer_size)
1286 return size;
1287
Steven Rostedt18421012009-12-10 22:54:27 -05001288 atomic_inc(&buffer->record_disabled);
1289
1290 /* Make sure all writers are done with this buffer. */
1291 synchronize_sched();
1292
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001293 mutex_lock(&buffer->mutex);
Steven Rostedt554f7862009-03-11 22:00:13 -04001294 get_online_cpus();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001295
1296 nr_pages = DIV_ROUND_UP(size, BUF_PAGE_SIZE);
1297
1298 if (size < buffer_size) {
1299
1300 /* easy case, just free pages */
Steven Rostedt554f7862009-03-11 22:00:13 -04001301 if (RB_WARN_ON(buffer, nr_pages >= buffer->pages))
1302 goto out_fail;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001303
1304 rm_pages = buffer->pages - nr_pages;
1305
1306 for_each_buffer_cpu(buffer, cpu) {
1307 cpu_buffer = buffer->buffers[cpu];
1308 rb_remove_pages(cpu_buffer, rm_pages);
1309 }
1310 goto out;
1311 }
1312
1313 /*
1314 * This is a bit more difficult. We only want to add pages
1315 * when we can allocate enough for all CPUs. We do this
1316 * by allocating all the pages and storing them on a local
1317 * link list. If we succeed in our allocation, then we
1318 * add these pages to the cpu_buffers. Otherwise we just free
1319 * them all and return -ENOMEM;
1320 */
Steven Rostedt554f7862009-03-11 22:00:13 -04001321 if (RB_WARN_ON(buffer, nr_pages <= buffer->pages))
1322 goto out_fail;
Steven Rostedtf536aaf2008-11-10 23:07:30 -05001323
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001324 new_pages = nr_pages - buffer->pages;
1325
1326 for_each_buffer_cpu(buffer, cpu) {
1327 for (i = 0; i < new_pages; i++) {
Steven Rostedt044fa782008-12-02 23:50:03 -05001328 bpage = kzalloc_node(ALIGN(sizeof(*bpage),
Steven Rostedte4c2ce82008-10-01 11:14:54 -04001329 cache_line_size()),
1330 GFP_KERNEL, cpu_to_node(cpu));
Steven Rostedt044fa782008-12-02 23:50:03 -05001331 if (!bpage)
Steven Rostedte4c2ce82008-10-01 11:14:54 -04001332 goto free_pages;
Steven Rostedt044fa782008-12-02 23:50:03 -05001333 list_add(&bpage->list, &pages);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001334 addr = __get_free_page(GFP_KERNEL);
1335 if (!addr)
1336 goto free_pages;
Steven Rostedt044fa782008-12-02 23:50:03 -05001337 bpage->page = (void *)addr;
1338 rb_init_page(bpage->page);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001339 }
1340 }
1341
1342 for_each_buffer_cpu(buffer, cpu) {
1343 cpu_buffer = buffer->buffers[cpu];
1344 rb_insert_pages(cpu_buffer, &pages, new_pages);
1345 }
1346
Steven Rostedt554f7862009-03-11 22:00:13 -04001347 if (RB_WARN_ON(buffer, !list_empty(&pages)))
1348 goto out_fail;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001349
1350 out:
1351 buffer->pages = nr_pages;
Steven Rostedt554f7862009-03-11 22:00:13 -04001352 put_online_cpus();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001353 mutex_unlock(&buffer->mutex);
1354
Steven Rostedt18421012009-12-10 22:54:27 -05001355 atomic_dec(&buffer->record_disabled);
1356
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001357 return size;
1358
1359 free_pages:
Steven Rostedt044fa782008-12-02 23:50:03 -05001360 list_for_each_entry_safe(bpage, tmp, &pages, list) {
1361 list_del_init(&bpage->list);
1362 free_buffer_page(bpage);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001363 }
Steven Rostedt554f7862009-03-11 22:00:13 -04001364 put_online_cpus();
Vegard Nossum641d2f62008-11-18 19:22:13 +01001365 mutex_unlock(&buffer->mutex);
Steven Rostedt18421012009-12-10 22:54:27 -05001366 atomic_dec(&buffer->record_disabled);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001367 return -ENOMEM;
Steven Rostedt554f7862009-03-11 22:00:13 -04001368
1369 /*
1370 * Something went totally wrong, and we are too paranoid
1371 * to even clean up the mess.
1372 */
1373 out_fail:
1374 put_online_cpus();
1375 mutex_unlock(&buffer->mutex);
Steven Rostedt18421012009-12-10 22:54:27 -05001376 atomic_dec(&buffer->record_disabled);
Steven Rostedt554f7862009-03-11 22:00:13 -04001377 return -1;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001378}
Robert Richterc4f50182008-12-11 16:49:22 +01001379EXPORT_SYMBOL_GPL(ring_buffer_resize);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001380
Steven Rostedt8789a9e2008-12-02 15:34:07 -05001381static inline void *
Steven Rostedt044fa782008-12-02 23:50:03 -05001382__rb_data_page_index(struct buffer_data_page *bpage, unsigned index)
Steven Rostedt8789a9e2008-12-02 15:34:07 -05001383{
Steven Rostedt044fa782008-12-02 23:50:03 -05001384 return bpage->data + index;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05001385}
1386
Steven Rostedt044fa782008-12-02 23:50:03 -05001387static inline void *__rb_page_index(struct buffer_page *bpage, unsigned index)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001388{
Steven Rostedt044fa782008-12-02 23:50:03 -05001389 return bpage->page->data + index;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001390}
1391
1392static inline struct ring_buffer_event *
Steven Rostedtd7690412008-10-01 00:29:53 -04001393rb_reader_event(struct ring_buffer_per_cpu *cpu_buffer)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001394{
Steven Rostedt6f807ac2008-10-04 02:00:58 -04001395 return __rb_page_index(cpu_buffer->reader_page,
1396 cpu_buffer->reader_page->read);
1397}
1398
1399static inline struct ring_buffer_event *
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001400rb_iter_head_event(struct ring_buffer_iter *iter)
1401{
Steven Rostedt6f807ac2008-10-04 02:00:58 -04001402 return __rb_page_index(iter->head_page, iter->head);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001403}
1404
Steven Rostedt77ae3652009-03-27 11:00:29 -04001405static inline unsigned long rb_page_write(struct buffer_page *bpage)
Steven Rostedtbf41a152008-10-04 02:00:59 -04001406{
Steven Rostedt77ae3652009-03-27 11:00:29 -04001407 return local_read(&bpage->write) & RB_WRITE_MASK;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001408}
1409
1410static inline unsigned rb_page_commit(struct buffer_page *bpage)
1411{
Steven Rostedtabc9b562008-12-02 15:34:06 -05001412 return local_read(&bpage->page->commit);
Steven Rostedtbf41a152008-10-04 02:00:59 -04001413}
1414
Steven Rostedt77ae3652009-03-27 11:00:29 -04001415static inline unsigned long rb_page_entries(struct buffer_page *bpage)
1416{
1417 return local_read(&bpage->entries) & RB_WRITE_MASK;
1418}
1419
Steven Rostedtbf41a152008-10-04 02:00:59 -04001420/* Size is determined by what has been commited */
1421static inline unsigned rb_page_size(struct buffer_page *bpage)
1422{
1423 return rb_page_commit(bpage);
1424}
1425
1426static inline unsigned
1427rb_commit_index(struct ring_buffer_per_cpu *cpu_buffer)
1428{
1429 return rb_page_commit(cpu_buffer->commit_page);
1430}
1431
Steven Rostedtbf41a152008-10-04 02:00:59 -04001432static inline unsigned
1433rb_event_index(struct ring_buffer_event *event)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001434{
Steven Rostedtbf41a152008-10-04 02:00:59 -04001435 unsigned long addr = (unsigned long)event;
1436
Steven Rostedt22f470f2009-06-11 09:29:58 -04001437 return (addr & ~PAGE_MASK) - BUF_PAGE_HDR_SIZE;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001438}
1439
Steven Rostedt0f0c85f2009-05-11 16:08:00 -04001440static inline int
Steven Rostedtfa743952009-06-16 12:37:57 -04001441rb_event_is_commit(struct ring_buffer_per_cpu *cpu_buffer,
1442 struct ring_buffer_event *event)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001443{
Steven Rostedtbf41a152008-10-04 02:00:59 -04001444 unsigned long addr = (unsigned long)event;
1445 unsigned long index;
1446
1447 index = rb_event_index(event);
1448 addr &= PAGE_MASK;
1449
1450 return cpu_buffer->commit_page->page == (void *)addr &&
1451 rb_commit_index(cpu_buffer) == index;
1452}
1453
Andrew Morton34a148b2009-01-09 12:27:09 -08001454static void
Steven Rostedtbf41a152008-10-04 02:00:59 -04001455rb_set_commit_to_write(struct ring_buffer_per_cpu *cpu_buffer)
1456{
Steven Rostedt77ae3652009-03-27 11:00:29 -04001457 unsigned long max_count;
1458
Steven Rostedtbf41a152008-10-04 02:00:59 -04001459 /*
1460 * We only race with interrupts and NMIs on this CPU.
1461 * If we own the commit event, then we can commit
1462 * all others that interrupted us, since the interruptions
1463 * are in stack format (they finish before they come
1464 * back to us). This allows us to do a simple loop to
1465 * assign the commit to the tail.
1466 */
Steven Rostedta8ccf1d2008-12-23 11:32:24 -05001467 again:
Steven Rostedt77ae3652009-03-27 11:00:29 -04001468 max_count = cpu_buffer->buffer->pages * 100;
1469
Steven Rostedtbf41a152008-10-04 02:00:59 -04001470 while (cpu_buffer->commit_page != cpu_buffer->tail_page) {
Steven Rostedt77ae3652009-03-27 11:00:29 -04001471 if (RB_WARN_ON(cpu_buffer, !(--max_count)))
1472 return;
1473 if (RB_WARN_ON(cpu_buffer,
1474 rb_is_reader_page(cpu_buffer->tail_page)))
1475 return;
1476 local_set(&cpu_buffer->commit_page->page->commit,
1477 rb_page_write(cpu_buffer->commit_page));
Steven Rostedtbf41a152008-10-04 02:00:59 -04001478 rb_inc_page(cpu_buffer, &cpu_buffer->commit_page);
Steven Rostedtabc9b562008-12-02 15:34:06 -05001479 cpu_buffer->write_stamp =
1480 cpu_buffer->commit_page->page->time_stamp;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001481 /* add barrier to keep gcc from optimizing too much */
1482 barrier();
1483 }
1484 while (rb_commit_index(cpu_buffer) !=
1485 rb_page_write(cpu_buffer->commit_page)) {
Steven Rostedt77ae3652009-03-27 11:00:29 -04001486
1487 local_set(&cpu_buffer->commit_page->page->commit,
1488 rb_page_write(cpu_buffer->commit_page));
1489 RB_WARN_ON(cpu_buffer,
1490 local_read(&cpu_buffer->commit_page->page->commit) &
1491 ~RB_WRITE_MASK);
Steven Rostedtbf41a152008-10-04 02:00:59 -04001492 barrier();
1493 }
Steven Rostedta8ccf1d2008-12-23 11:32:24 -05001494
1495 /* again, keep gcc from optimizing */
1496 barrier();
1497
1498 /*
1499 * If an interrupt came in just after the first while loop
1500 * and pushed the tail page forward, we will be left with
1501 * a dangling commit that will never go forward.
1502 */
1503 if (unlikely(cpu_buffer->commit_page != cpu_buffer->tail_page))
1504 goto again;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001505}
1506
Steven Rostedtd7690412008-10-01 00:29:53 -04001507static void rb_reset_reader_page(struct ring_buffer_per_cpu *cpu_buffer)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001508{
Steven Rostedtabc9b562008-12-02 15:34:06 -05001509 cpu_buffer->read_stamp = cpu_buffer->reader_page->page->time_stamp;
Steven Rostedt6f807ac2008-10-04 02:00:58 -04001510 cpu_buffer->reader_page->read = 0;
Steven Rostedtd7690412008-10-01 00:29:53 -04001511}
1512
Andrew Morton34a148b2009-01-09 12:27:09 -08001513static void rb_inc_iter(struct ring_buffer_iter *iter)
Steven Rostedtd7690412008-10-01 00:29:53 -04001514{
1515 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
1516
1517 /*
1518 * The iterator could be on the reader page (it starts there).
1519 * But the head could have moved, since the reader was
1520 * found. Check for this case and assign the iterator
1521 * to the head page instead of next.
1522 */
1523 if (iter->head_page == cpu_buffer->reader_page)
Steven Rostedt77ae3652009-03-27 11:00:29 -04001524 iter->head_page = rb_set_head_page(cpu_buffer);
Steven Rostedtd7690412008-10-01 00:29:53 -04001525 else
1526 rb_inc_page(cpu_buffer, &iter->head_page);
1527
Steven Rostedtabc9b562008-12-02 15:34:06 -05001528 iter->read_stamp = iter->head_page->page->time_stamp;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001529 iter->head = 0;
1530}
1531
1532/**
1533 * ring_buffer_update_event - update event type and data
1534 * @event: the even to update
1535 * @type: the type of event
1536 * @length: the size of the event field in the ring buffer
1537 *
1538 * Update the type and data fields of the event. The length
1539 * is the actual size that is written to the ring buffer,
1540 * and with this, we can determine what to place into the
1541 * data field.
1542 */
Andrew Morton34a148b2009-01-09 12:27:09 -08001543static void
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001544rb_update_event(struct ring_buffer_event *event,
1545 unsigned type, unsigned length)
1546{
Lai Jiangshan334d4162009-04-24 11:27:05 +08001547 event->type_len = type;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001548
1549 switch (type) {
1550
1551 case RINGBUF_TYPE_PADDING:
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001552 case RINGBUF_TYPE_TIME_EXTEND:
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001553 case RINGBUF_TYPE_TIME_STAMP:
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001554 break;
1555
Lai Jiangshan334d4162009-04-24 11:27:05 +08001556 case 0:
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001557 length -= RB_EVNT_HDR_SIZE;
Steven Rostedt22710482010-03-18 17:54:19 -04001558 if (length > RB_MAX_SMALL_DATA || RB_FORCE_8BYTE_ALIGNMENT)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001559 event->array[0] = length;
Lai Jiangshan334d4162009-04-24 11:27:05 +08001560 else
1561 event->type_len = DIV_ROUND_UP(length, RB_ALIGNMENT);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001562 break;
1563 default:
1564 BUG();
1565 }
1566}
1567
Steven Rostedt77ae3652009-03-27 11:00:29 -04001568/*
1569 * rb_handle_head_page - writer hit the head page
1570 *
1571 * Returns: +1 to retry page
1572 * 0 to continue
1573 * -1 on error
1574 */
1575static int
1576rb_handle_head_page(struct ring_buffer_per_cpu *cpu_buffer,
1577 struct buffer_page *tail_page,
1578 struct buffer_page *next_page)
1579{
1580 struct buffer_page *new_head;
1581 int entries;
1582 int type;
1583 int ret;
1584
1585 entries = rb_page_entries(next_page);
1586
1587 /*
1588 * The hard part is here. We need to move the head
1589 * forward, and protect against both readers on
1590 * other CPUs and writers coming in via interrupts.
1591 */
1592 type = rb_head_page_set_update(cpu_buffer, next_page, tail_page,
1593 RB_PAGE_HEAD);
1594
1595 /*
1596 * type can be one of four:
1597 * NORMAL - an interrupt already moved it for us
1598 * HEAD - we are the first to get here.
1599 * UPDATE - we are the interrupt interrupting
1600 * a current move.
1601 * MOVED - a reader on another CPU moved the next
1602 * pointer to its reader page. Give up
1603 * and try again.
1604 */
1605
1606 switch (type) {
1607 case RB_PAGE_HEAD:
1608 /*
1609 * We changed the head to UPDATE, thus
1610 * it is our responsibility to update
1611 * the counters.
1612 */
1613 local_add(entries, &cpu_buffer->overrun);
1614
1615 /*
1616 * The entries will be zeroed out when we move the
1617 * tail page.
1618 */
1619
1620 /* still more to do */
1621 break;
1622
1623 case RB_PAGE_UPDATE:
1624 /*
1625 * This is an interrupt that interrupt the
1626 * previous update. Still more to do.
1627 */
1628 break;
1629 case RB_PAGE_NORMAL:
1630 /*
1631 * An interrupt came in before the update
1632 * and processed this for us.
1633 * Nothing left to do.
1634 */
1635 return 1;
1636 case RB_PAGE_MOVED:
1637 /*
1638 * The reader is on another CPU and just did
1639 * a swap with our next_page.
1640 * Try again.
1641 */
1642 return 1;
1643 default:
1644 RB_WARN_ON(cpu_buffer, 1); /* WTF??? */
1645 return -1;
1646 }
1647
1648 /*
1649 * Now that we are here, the old head pointer is
1650 * set to UPDATE. This will keep the reader from
1651 * swapping the head page with the reader page.
1652 * The reader (on another CPU) will spin till
1653 * we are finished.
1654 *
1655 * We just need to protect against interrupts
1656 * doing the job. We will set the next pointer
1657 * to HEAD. After that, we set the old pointer
1658 * to NORMAL, but only if it was HEAD before.
1659 * otherwise we are an interrupt, and only
1660 * want the outer most commit to reset it.
1661 */
1662 new_head = next_page;
1663 rb_inc_page(cpu_buffer, &new_head);
1664
1665 ret = rb_head_page_set_head(cpu_buffer, new_head, next_page,
1666 RB_PAGE_NORMAL);
1667
1668 /*
1669 * Valid returns are:
1670 * HEAD - an interrupt came in and already set it.
1671 * NORMAL - One of two things:
1672 * 1) We really set it.
1673 * 2) A bunch of interrupts came in and moved
1674 * the page forward again.
1675 */
1676 switch (ret) {
1677 case RB_PAGE_HEAD:
1678 case RB_PAGE_NORMAL:
1679 /* OK */
1680 break;
1681 default:
1682 RB_WARN_ON(cpu_buffer, 1);
1683 return -1;
1684 }
1685
1686 /*
1687 * It is possible that an interrupt came in,
1688 * set the head up, then more interrupts came in
1689 * and moved it again. When we get back here,
1690 * the page would have been set to NORMAL but we
1691 * just set it back to HEAD.
1692 *
1693 * How do you detect this? Well, if that happened
1694 * the tail page would have moved.
1695 */
1696 if (ret == RB_PAGE_NORMAL) {
1697 /*
1698 * If the tail had moved passed next, then we need
1699 * to reset the pointer.
1700 */
1701 if (cpu_buffer->tail_page != tail_page &&
1702 cpu_buffer->tail_page != next_page)
1703 rb_head_page_set_normal(cpu_buffer, new_head,
1704 next_page,
1705 RB_PAGE_HEAD);
1706 }
1707
1708 /*
1709 * If this was the outer most commit (the one that
1710 * changed the original pointer from HEAD to UPDATE),
1711 * then it is up to us to reset it to NORMAL.
1712 */
1713 if (type == RB_PAGE_HEAD) {
1714 ret = rb_head_page_set_normal(cpu_buffer, next_page,
1715 tail_page,
1716 RB_PAGE_UPDATE);
1717 if (RB_WARN_ON(cpu_buffer,
1718 ret != RB_PAGE_UPDATE))
1719 return -1;
1720 }
1721
1722 return 0;
1723}
1724
Andrew Morton34a148b2009-01-09 12:27:09 -08001725static unsigned rb_calculate_event_length(unsigned length)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001726{
1727 struct ring_buffer_event event; /* Used only for sizeof array */
1728
1729 /* zero length can cause confusions */
1730 if (!length)
1731 length = 1;
1732
Steven Rostedt22710482010-03-18 17:54:19 -04001733 if (length > RB_MAX_SMALL_DATA || RB_FORCE_8BYTE_ALIGNMENT)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001734 length += sizeof(event.array[0]);
1735
1736 length += RB_EVNT_HDR_SIZE;
Steven Rostedt22710482010-03-18 17:54:19 -04001737 length = ALIGN(length, RB_ARCH_ALIGNMENT);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001738
1739 return length;
1740}
1741
Steven Rostedtc7b09302009-06-11 11:12:00 -04001742static inline void
1743rb_reset_tail(struct ring_buffer_per_cpu *cpu_buffer,
1744 struct buffer_page *tail_page,
1745 unsigned long tail, unsigned long length)
1746{
1747 struct ring_buffer_event *event;
1748
1749 /*
1750 * Only the event that crossed the page boundary
1751 * must fill the old tail_page with padding.
1752 */
1753 if (tail >= BUF_PAGE_SIZE) {
1754 local_sub(length, &tail_page->write);
1755 return;
1756 }
1757
1758 event = __rb_page_index(tail_page, tail);
Linus Torvaldsb0b70652009-06-20 10:56:46 -07001759 kmemcheck_annotate_bitfield(event, bitfield);
Steven Rostedtc7b09302009-06-11 11:12:00 -04001760
1761 /*
1762 * If this event is bigger than the minimum size, then
1763 * we need to be careful that we don't subtract the
1764 * write counter enough to allow another writer to slip
1765 * in on this page.
1766 * We put in a discarded commit instead, to make sure
1767 * that this space is not used again.
1768 *
1769 * If we are less than the minimum size, we don't need to
1770 * worry about it.
1771 */
1772 if (tail > (BUF_PAGE_SIZE - RB_EVNT_MIN_SIZE)) {
1773 /* No room for any events */
1774
1775 /* Mark the rest of the page with padding */
1776 rb_event_set_padding(event);
1777
1778 /* Set the write back to the previous setting */
1779 local_sub(length, &tail_page->write);
1780 return;
1781 }
1782
1783 /* Put in a discarded event */
1784 event->array[0] = (BUF_PAGE_SIZE - tail) - RB_EVNT_HDR_SIZE;
1785 event->type_len = RINGBUF_TYPE_PADDING;
1786 /* time delta must be non zero */
1787 event->time_delta = 1;
Steven Rostedtc7b09302009-06-11 11:12:00 -04001788
1789 /* Set write to end of buffer */
1790 length = (tail + length) - BUF_PAGE_SIZE;
1791 local_sub(length, &tail_page->write);
1792}
Steven Rostedt6634ff22009-05-06 15:30:07 -04001793
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001794static struct ring_buffer_event *
Steven Rostedt6634ff22009-05-06 15:30:07 -04001795rb_move_tail(struct ring_buffer_per_cpu *cpu_buffer,
1796 unsigned long length, unsigned long tail,
Steven Rostedt6634ff22009-05-06 15:30:07 -04001797 struct buffer_page *tail_page, u64 *ts)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001798{
Steven Rostedt5a50e332009-11-17 08:43:01 -05001799 struct buffer_page *commit_page = cpu_buffer->commit_page;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001800 struct ring_buffer *buffer = cpu_buffer->buffer;
Steven Rostedt77ae3652009-03-27 11:00:29 -04001801 struct buffer_page *next_page;
1802 int ret;
Steven Rostedtaa20ae82009-05-05 21:16:11 -04001803
1804 next_page = tail_page;
1805
Steven Rostedtaa20ae82009-05-05 21:16:11 -04001806 rb_inc_page(cpu_buffer, &next_page);
1807
Steven Rostedtaa20ae82009-05-05 21:16:11 -04001808 /*
1809 * If for some reason, we had an interrupt storm that made
1810 * it all the way around the buffer, bail, and warn
1811 * about it.
1812 */
1813 if (unlikely(next_page == commit_page)) {
Steven Rostedt77ae3652009-03-27 11:00:29 -04001814 local_inc(&cpu_buffer->commit_overrun);
Steven Rostedtaa20ae82009-05-05 21:16:11 -04001815 goto out_reset;
1816 }
1817
Steven Rostedt77ae3652009-03-27 11:00:29 -04001818 /*
1819 * This is where the fun begins!
1820 *
1821 * We are fighting against races between a reader that
1822 * could be on another CPU trying to swap its reader
1823 * page with the buffer head.
1824 *
1825 * We are also fighting against interrupts coming in and
1826 * moving the head or tail on us as well.
1827 *
1828 * If the next page is the head page then we have filled
1829 * the buffer, unless the commit page is still on the
1830 * reader page.
1831 */
1832 if (rb_is_head_page(cpu_buffer, next_page, &tail_page->list)) {
Steven Rostedtaa20ae82009-05-05 21:16:11 -04001833
Steven Rostedt77ae3652009-03-27 11:00:29 -04001834 /*
1835 * If the commit is not on the reader page, then
1836 * move the header page.
1837 */
1838 if (!rb_is_reader_page(cpu_buffer->commit_page)) {
1839 /*
1840 * If we are not in overwrite mode,
1841 * this is easy, just stop here.
1842 */
1843 if (!(buffer->flags & RB_FL_OVERWRITE))
1844 goto out_reset;
Steven Rostedtaa20ae82009-05-05 21:16:11 -04001845
Steven Rostedt77ae3652009-03-27 11:00:29 -04001846 ret = rb_handle_head_page(cpu_buffer,
1847 tail_page,
1848 next_page);
1849 if (ret < 0)
1850 goto out_reset;
1851 if (ret)
1852 goto out_again;
1853 } else {
1854 /*
1855 * We need to be careful here too. The
1856 * commit page could still be on the reader
1857 * page. We could have a small buffer, and
1858 * have filled up the buffer with events
1859 * from interrupts and such, and wrapped.
1860 *
1861 * Note, if the tail page is also the on the
1862 * reader_page, we let it move out.
1863 */
1864 if (unlikely((cpu_buffer->commit_page !=
1865 cpu_buffer->tail_page) &&
1866 (cpu_buffer->commit_page ==
1867 cpu_buffer->reader_page))) {
1868 local_inc(&cpu_buffer->commit_overrun);
1869 goto out_reset;
1870 }
Steven Rostedtaa20ae82009-05-05 21:16:11 -04001871 }
1872 }
1873
Steven Rostedt77ae3652009-03-27 11:00:29 -04001874 ret = rb_tail_page_update(cpu_buffer, tail_page, next_page);
1875 if (ret) {
1876 /*
1877 * Nested commits always have zero deltas, so
1878 * just reread the time stamp
1879 */
Jiri Olsa6d3f1e12009-10-23 19:36:19 -04001880 *ts = rb_time_stamp(buffer);
Steven Rostedt77ae3652009-03-27 11:00:29 -04001881 next_page->page->time_stamp = *ts;
Steven Rostedtaa20ae82009-05-05 21:16:11 -04001882 }
1883
Steven Rostedt77ae3652009-03-27 11:00:29 -04001884 out_again:
Steven Rostedtaa20ae82009-05-05 21:16:11 -04001885
Steven Rostedt77ae3652009-03-27 11:00:29 -04001886 rb_reset_tail(cpu_buffer, tail_page, tail, length);
Steven Rostedtaa20ae82009-05-05 21:16:11 -04001887
1888 /* fail and let the caller try again */
1889 return ERR_PTR(-EAGAIN);
1890
Steven Rostedt45141d42009-02-12 13:19:48 -05001891 out_reset:
Lai Jiangshan6f3b3442009-01-12 11:06:18 +08001892 /* reset write */
Steven Rostedtc7b09302009-06-11 11:12:00 -04001893 rb_reset_tail(cpu_buffer, tail_page, tail, length);
Lai Jiangshan6f3b3442009-01-12 11:06:18 +08001894
Steven Rostedtbf41a152008-10-04 02:00:59 -04001895 return NULL;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001896}
1897
Steven Rostedt6634ff22009-05-06 15:30:07 -04001898static struct ring_buffer_event *
1899__rb_reserve_next(struct ring_buffer_per_cpu *cpu_buffer,
1900 unsigned type, unsigned long length, u64 *ts)
1901{
Steven Rostedt5a50e332009-11-17 08:43:01 -05001902 struct buffer_page *tail_page;
Steven Rostedt6634ff22009-05-06 15:30:07 -04001903 struct ring_buffer_event *event;
1904 unsigned long tail, write;
1905
Steven Rostedt6634ff22009-05-06 15:30:07 -04001906 tail_page = cpu_buffer->tail_page;
1907 write = local_add_return(length, &tail_page->write);
Steven Rostedt77ae3652009-03-27 11:00:29 -04001908
1909 /* set write to only the index of the write */
1910 write &= RB_WRITE_MASK;
Steven Rostedt6634ff22009-05-06 15:30:07 -04001911 tail = write - length;
1912
1913 /* See if we shot pass the end of this buffer page */
1914 if (write > BUF_PAGE_SIZE)
1915 return rb_move_tail(cpu_buffer, length, tail,
Steven Rostedt5a50e332009-11-17 08:43:01 -05001916 tail_page, ts);
Steven Rostedt6634ff22009-05-06 15:30:07 -04001917
1918 /* We reserved something on the buffer */
1919
Steven Rostedt6634ff22009-05-06 15:30:07 -04001920 event = __rb_page_index(tail_page, tail);
Vegard Nossum1744a212009-02-28 08:29:44 +01001921 kmemcheck_annotate_bitfield(event, bitfield);
Steven Rostedt6634ff22009-05-06 15:30:07 -04001922 rb_update_event(event, type, length);
1923
1924 /* The passed in type is zero for DATA */
1925 if (likely(!type))
1926 local_inc(&tail_page->entries);
1927
1928 /*
Steven Rostedtfa743952009-06-16 12:37:57 -04001929 * If this is the first commit on the page, then update
1930 * its timestamp.
Steven Rostedt6634ff22009-05-06 15:30:07 -04001931 */
Steven Rostedtfa743952009-06-16 12:37:57 -04001932 if (!tail)
1933 tail_page->page->time_stamp = *ts;
Steven Rostedt6634ff22009-05-06 15:30:07 -04001934
1935 return event;
1936}
1937
Steven Rostedtedd813bf2009-06-02 23:00:53 -04001938static inline int
1939rb_try_to_discard(struct ring_buffer_per_cpu *cpu_buffer,
1940 struct ring_buffer_event *event)
1941{
1942 unsigned long new_index, old_index;
1943 struct buffer_page *bpage;
1944 unsigned long index;
1945 unsigned long addr;
1946
1947 new_index = rb_event_index(event);
1948 old_index = new_index + rb_event_length(event);
1949 addr = (unsigned long)event;
1950 addr &= PAGE_MASK;
1951
1952 bpage = cpu_buffer->tail_page;
1953
1954 if (bpage->page == (void *)addr && rb_page_write(bpage) == old_index) {
Steven Rostedt77ae3652009-03-27 11:00:29 -04001955 unsigned long write_mask =
1956 local_read(&bpage->write) & ~RB_WRITE_MASK;
Steven Rostedtedd813bf2009-06-02 23:00:53 -04001957 /*
1958 * This is on the tail page. It is possible that
1959 * a write could come in and move the tail page
1960 * and write to the next page. That is fine
1961 * because we just shorten what is on this page.
1962 */
Steven Rostedt77ae3652009-03-27 11:00:29 -04001963 old_index += write_mask;
1964 new_index += write_mask;
Steven Rostedtedd813bf2009-06-02 23:00:53 -04001965 index = local_cmpxchg(&bpage->write, old_index, new_index);
1966 if (index == old_index)
1967 return 1;
1968 }
1969
1970 /* could not discard */
1971 return 0;
1972}
1973
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001974static int
1975rb_add_time_stamp(struct ring_buffer_per_cpu *cpu_buffer,
1976 u64 *ts, u64 *delta)
1977{
1978 struct ring_buffer_event *event;
1979 static int once;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001980 int ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001981
1982 if (unlikely(*delta > (1ULL << 59) && !once++)) {
1983 printk(KERN_WARNING "Delta way too big! %llu"
1984 " ts=%llu write stamp = %llu\n",
Stephen Rothwelle2862c92008-10-27 17:43:28 +11001985 (unsigned long long)*delta,
1986 (unsigned long long)*ts,
1987 (unsigned long long)cpu_buffer->write_stamp);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001988 WARN_ON(1);
1989 }
1990
1991 /*
1992 * The delta is too big, we to add a
1993 * new timestamp.
1994 */
1995 event = __rb_reserve_next(cpu_buffer,
1996 RINGBUF_TYPE_TIME_EXTEND,
1997 RB_LEN_TIME_EXTEND,
1998 ts);
1999 if (!event)
Steven Rostedtbf41a152008-10-04 02:00:59 -04002000 return -EBUSY;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002001
Steven Rostedtbf41a152008-10-04 02:00:59 -04002002 if (PTR_ERR(event) == -EAGAIN)
2003 return -EAGAIN;
2004
2005 /* Only a commited time event can update the write stamp */
Steven Rostedtfa743952009-06-16 12:37:57 -04002006 if (rb_event_is_commit(cpu_buffer, event)) {
Steven Rostedtbf41a152008-10-04 02:00:59 -04002007 /*
Steven Rostedtfa743952009-06-16 12:37:57 -04002008 * If this is the first on the page, then it was
2009 * updated with the page itself. Try to discard it
2010 * and if we can't just make it zero.
Steven Rostedtbf41a152008-10-04 02:00:59 -04002011 */
2012 if (rb_event_index(event)) {
2013 event->time_delta = *delta & TS_MASK;
2014 event->array[0] = *delta >> TS_SHIFT;
2015 } else {
Steven Rostedtea05b572009-06-03 09:30:10 -04002016 /* try to discard, since we do not need this */
2017 if (!rb_try_to_discard(cpu_buffer, event)) {
2018 /* nope, just zero it */
2019 event->time_delta = 0;
2020 event->array[0] = 0;
2021 }
Steven Rostedtbf41a152008-10-04 02:00:59 -04002022 }
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002023 cpu_buffer->write_stamp = *ts;
Steven Rostedtbf41a152008-10-04 02:00:59 -04002024 /* let the caller know this was the commit */
2025 ret = 1;
2026 } else {
Steven Rostedtedd813bf2009-06-02 23:00:53 -04002027 /* Try to discard the event */
2028 if (!rb_try_to_discard(cpu_buffer, event)) {
2029 /* Darn, this is just wasted space */
2030 event->time_delta = 0;
2031 event->array[0] = 0;
Steven Rostedtedd813bf2009-06-02 23:00:53 -04002032 }
Steven Rostedtf57a8a12009-06-05 14:11:30 -04002033 ret = 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002034 }
2035
Steven Rostedtbf41a152008-10-04 02:00:59 -04002036 *delta = 0;
2037
2038 return ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002039}
2040
Steven Rostedtfa743952009-06-16 12:37:57 -04002041static void rb_start_commit(struct ring_buffer_per_cpu *cpu_buffer)
2042{
2043 local_inc(&cpu_buffer->committing);
2044 local_inc(&cpu_buffer->commits);
2045}
2046
2047static void rb_end_commit(struct ring_buffer_per_cpu *cpu_buffer)
2048{
2049 unsigned long commits;
2050
2051 if (RB_WARN_ON(cpu_buffer,
2052 !local_read(&cpu_buffer->committing)))
2053 return;
2054
2055 again:
2056 commits = local_read(&cpu_buffer->commits);
2057 /* synchronize with interrupts */
2058 barrier();
2059 if (local_read(&cpu_buffer->committing) == 1)
2060 rb_set_commit_to_write(cpu_buffer);
2061
2062 local_dec(&cpu_buffer->committing);
2063
2064 /* synchronize with interrupts */
2065 barrier();
2066
2067 /*
2068 * Need to account for interrupts coming in between the
2069 * updating of the commit page and the clearing of the
2070 * committing counter.
2071 */
2072 if (unlikely(local_read(&cpu_buffer->commits) != commits) &&
2073 !local_read(&cpu_buffer->committing)) {
2074 local_inc(&cpu_buffer->committing);
2075 goto again;
2076 }
2077}
2078
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002079static struct ring_buffer_event *
Steven Rostedt62f0b3e2009-09-04 14:11:34 -04002080rb_reserve_next_event(struct ring_buffer *buffer,
2081 struct ring_buffer_per_cpu *cpu_buffer,
Steven Rostedt1cd8d732009-05-11 14:08:09 -04002082 unsigned long length)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002083{
2084 struct ring_buffer_event *event;
Steven Rostedt168b6b12009-05-11 22:11:05 -04002085 u64 ts, delta = 0;
Steven Rostedtbf41a152008-10-04 02:00:59 -04002086 int commit = 0;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002087 int nr_loops = 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002088
Steven Rostedtfa743952009-06-16 12:37:57 -04002089 rb_start_commit(cpu_buffer);
2090
Steven Rostedt85bac322009-09-04 14:24:40 -04002091#ifdef CONFIG_RING_BUFFER_ALLOW_SWAP
Steven Rostedt62f0b3e2009-09-04 14:11:34 -04002092 /*
2093 * Due to the ability to swap a cpu buffer from a buffer
2094 * it is possible it was swapped before we committed.
2095 * (committing stops a swap). We check for it here and
2096 * if it happened, we have to fail the write.
2097 */
2098 barrier();
2099 if (unlikely(ACCESS_ONCE(cpu_buffer->buffer) != buffer)) {
2100 local_dec(&cpu_buffer->committing);
2101 local_dec(&cpu_buffer->commits);
2102 return NULL;
2103 }
Steven Rostedt85bac322009-09-04 14:24:40 -04002104#endif
Steven Rostedt62f0b3e2009-09-04 14:11:34 -04002105
Steven Rostedtbe957c42009-05-11 14:42:53 -04002106 length = rb_calculate_event_length(length);
Steven Rostedtbf41a152008-10-04 02:00:59 -04002107 again:
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002108 /*
2109 * We allow for interrupts to reenter here and do a trace.
2110 * If one does, it will cause this original code to loop
2111 * back here. Even with heavy interrupts happening, this
2112 * should only happen a few times in a row. If this happens
2113 * 1000 times in a row, there must be either an interrupt
2114 * storm or we have something buggy.
2115 * Bail!
2116 */
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05002117 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 1000))
Steven Rostedtfa743952009-06-16 12:37:57 -04002118 goto out_fail;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002119
Jiri Olsa6d3f1e12009-10-23 19:36:19 -04002120 ts = rb_time_stamp(cpu_buffer->buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002121
Steven Rostedtbf41a152008-10-04 02:00:59 -04002122 /*
2123 * Only the first commit can update the timestamp.
2124 * Yes there is a race here. If an interrupt comes in
2125 * just after the conditional and it traces too, then it
2126 * will also check the deltas. More than one timestamp may
2127 * also be made. But only the entry that did the actual
2128 * commit will be something other than zero.
2129 */
Steven Rostedt0f0c85f2009-05-11 16:08:00 -04002130 if (likely(cpu_buffer->tail_page == cpu_buffer->commit_page &&
2131 rb_page_write(cpu_buffer->tail_page) ==
2132 rb_commit_index(cpu_buffer))) {
Steven Rostedt168b6b12009-05-11 22:11:05 -04002133 u64 diff;
Steven Rostedtbf41a152008-10-04 02:00:59 -04002134
Steven Rostedt168b6b12009-05-11 22:11:05 -04002135 diff = ts - cpu_buffer->write_stamp;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002136
Steven Rostedt168b6b12009-05-11 22:11:05 -04002137 /* make sure this diff is calculated here */
Steven Rostedtbf41a152008-10-04 02:00:59 -04002138 barrier();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002139
Steven Rostedtbf41a152008-10-04 02:00:59 -04002140 /* Did the write stamp get updated already? */
2141 if (unlikely(ts < cpu_buffer->write_stamp))
Steven Rostedt168b6b12009-05-11 22:11:05 -04002142 goto get_event;
Steven Rostedtbf41a152008-10-04 02:00:59 -04002143
Steven Rostedt168b6b12009-05-11 22:11:05 -04002144 delta = diff;
2145 if (unlikely(test_time_stamp(delta))) {
Steven Rostedtbf41a152008-10-04 02:00:59 -04002146
2147 commit = rb_add_time_stamp(cpu_buffer, &ts, &delta);
Steven Rostedtbf41a152008-10-04 02:00:59 -04002148 if (commit == -EBUSY)
Steven Rostedtfa743952009-06-16 12:37:57 -04002149 goto out_fail;
Steven Rostedtbf41a152008-10-04 02:00:59 -04002150
2151 if (commit == -EAGAIN)
2152 goto again;
2153
2154 RB_WARN_ON(cpu_buffer, commit < 0);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002155 }
Steven Rostedt168b6b12009-05-11 22:11:05 -04002156 }
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002157
Steven Rostedt168b6b12009-05-11 22:11:05 -04002158 get_event:
Steven Rostedt1cd8d732009-05-11 14:08:09 -04002159 event = __rb_reserve_next(cpu_buffer, 0, length, &ts);
Steven Rostedt168b6b12009-05-11 22:11:05 -04002160 if (unlikely(PTR_ERR(event) == -EAGAIN))
Steven Rostedtbf41a152008-10-04 02:00:59 -04002161 goto again;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002162
Steven Rostedtfa743952009-06-16 12:37:57 -04002163 if (!event)
2164 goto out_fail;
Steven Rostedtbf41a152008-10-04 02:00:59 -04002165
Steven Rostedtfa743952009-06-16 12:37:57 -04002166 if (!rb_event_is_commit(cpu_buffer, event))
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002167 delta = 0;
2168
2169 event->time_delta = delta;
2170
2171 return event;
Steven Rostedtfa743952009-06-16 12:37:57 -04002172
2173 out_fail:
2174 rb_end_commit(cpu_buffer);
2175 return NULL;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002176}
2177
Paul Mundt1155de42009-06-25 14:30:12 +09002178#ifdef CONFIG_TRACING
2179
Steven Rostedtaa18efb2009-04-20 16:16:11 -04002180#define TRACE_RECURSIVE_DEPTH 16
Steven Rostedt261842b2009-04-16 21:41:52 -04002181
2182static int trace_recursive_lock(void)
2183{
Steven Rostedtaa18efb2009-04-20 16:16:11 -04002184 current->trace_recursion++;
Steven Rostedt261842b2009-04-16 21:41:52 -04002185
Steven Rostedtaa18efb2009-04-20 16:16:11 -04002186 if (likely(current->trace_recursion < TRACE_RECURSIVE_DEPTH))
2187 return 0;
Steven Rostedt261842b2009-04-16 21:41:52 -04002188
Steven Rostedtaa18efb2009-04-20 16:16:11 -04002189 /* Disable all tracing before we do anything else */
2190 tracing_off_permanent();
Frederic Weisbeckere057a5e2009-04-19 23:38:12 +02002191
Steven Rostedt7d7d2b82009-04-27 12:37:49 -04002192 printk_once(KERN_WARNING "Tracing recursion: depth[%ld]:"
Steven Rostedtaa18efb2009-04-20 16:16:11 -04002193 "HC[%lu]:SC[%lu]:NMI[%lu]\n",
2194 current->trace_recursion,
2195 hardirq_count() >> HARDIRQ_SHIFT,
2196 softirq_count() >> SOFTIRQ_SHIFT,
2197 in_nmi());
Frederic Weisbeckere057a5e2009-04-19 23:38:12 +02002198
Steven Rostedtaa18efb2009-04-20 16:16:11 -04002199 WARN_ON_ONCE(1);
2200 return -1;
Steven Rostedt261842b2009-04-16 21:41:52 -04002201}
2202
2203static void trace_recursive_unlock(void)
2204{
Steven Rostedtaa18efb2009-04-20 16:16:11 -04002205 WARN_ON_ONCE(!current->trace_recursion);
Steven Rostedt261842b2009-04-16 21:41:52 -04002206
Steven Rostedtaa18efb2009-04-20 16:16:11 -04002207 current->trace_recursion--;
Steven Rostedt261842b2009-04-16 21:41:52 -04002208}
2209
Paul Mundt1155de42009-06-25 14:30:12 +09002210#else
2211
2212#define trace_recursive_lock() (0)
2213#define trace_recursive_unlock() do { } while (0)
2214
2215#endif
2216
Steven Rostedtbf41a152008-10-04 02:00:59 -04002217static DEFINE_PER_CPU(int, rb_need_resched);
2218
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002219/**
2220 * ring_buffer_lock_reserve - reserve a part of the buffer
2221 * @buffer: the ring buffer to reserve from
2222 * @length: the length of the data to reserve (excluding event header)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002223 *
2224 * Returns a reseverd event on the ring buffer to copy directly to.
2225 * The user of this interface will need to get the body to write into
2226 * and can use the ring_buffer_event_data() interface.
2227 *
2228 * The length is the length of the data needed, not the event length
2229 * which also includes the event header.
2230 *
2231 * Must be paired with ring_buffer_unlock_commit, unless NULL is returned.
2232 * If NULL is returned, then nothing has been allocated or locked.
2233 */
2234struct ring_buffer_event *
Arnaldo Carvalho de Melo0a987752009-02-05 16:12:56 -02002235ring_buffer_lock_reserve(struct ring_buffer *buffer, unsigned long length)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002236{
2237 struct ring_buffer_per_cpu *cpu_buffer;
2238 struct ring_buffer_event *event;
Steven Rostedtbf41a152008-10-04 02:00:59 -04002239 int cpu, resched;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002240
Steven Rostedt033601a2008-11-21 12:41:55 -05002241 if (ring_buffer_flags != RB_BUFFERS_ON)
Steven Rostedta3583242008-11-11 15:01:42 -05002242 return NULL;
2243
Steven Rostedtbf41a152008-10-04 02:00:59 -04002244 /* If we are tracing schedule, we don't want to recurse */
Steven Rostedt182e9f52008-11-03 23:15:56 -05002245 resched = ftrace_preempt_disable();
Steven Rostedtbf41a152008-10-04 02:00:59 -04002246
Lai Jiangshan52fbe9c2010-03-08 14:50:43 +08002247 if (atomic_read(&buffer->record_disabled))
2248 goto out_nocheck;
2249
Steven Rostedt261842b2009-04-16 21:41:52 -04002250 if (trace_recursive_lock())
2251 goto out_nocheck;
2252
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002253 cpu = raw_smp_processor_id();
2254
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302255 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedtd7690412008-10-01 00:29:53 -04002256 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002257
2258 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002259
2260 if (atomic_read(&cpu_buffer->record_disabled))
Steven Rostedtd7690412008-10-01 00:29:53 -04002261 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002262
Steven Rostedtbe957c42009-05-11 14:42:53 -04002263 if (length > BUF_MAX_DATA_SIZE)
Steven Rostedtbf41a152008-10-04 02:00:59 -04002264 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002265
Steven Rostedt62f0b3e2009-09-04 14:11:34 -04002266 event = rb_reserve_next_event(buffer, cpu_buffer, length);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002267 if (!event)
Steven Rostedtd7690412008-10-01 00:29:53 -04002268 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002269
Steven Rostedtbf41a152008-10-04 02:00:59 -04002270 /*
2271 * Need to store resched state on this cpu.
2272 * Only the first needs to.
2273 */
2274
2275 if (preempt_count() == 1)
2276 per_cpu(rb_need_resched, cpu) = resched;
2277
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002278 return event;
2279
Steven Rostedtd7690412008-10-01 00:29:53 -04002280 out:
Steven Rostedt261842b2009-04-16 21:41:52 -04002281 trace_recursive_unlock();
2282
2283 out_nocheck:
Steven Rostedt182e9f52008-11-03 23:15:56 -05002284 ftrace_preempt_enable(resched);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002285 return NULL;
2286}
Robert Richterc4f50182008-12-11 16:49:22 +01002287EXPORT_SYMBOL_GPL(ring_buffer_lock_reserve);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002288
Steven Rostedta1863c22009-09-03 10:23:58 -04002289static void
2290rb_update_write_stamp(struct ring_buffer_per_cpu *cpu_buffer,
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002291 struct ring_buffer_event *event)
2292{
Steven Rostedtfa743952009-06-16 12:37:57 -04002293 /*
2294 * The event first in the commit queue updates the
2295 * time stamp.
2296 */
2297 if (rb_event_is_commit(cpu_buffer, event))
2298 cpu_buffer->write_stamp += event->time_delta;
Steven Rostedta1863c22009-09-03 10:23:58 -04002299}
Steven Rostedtbf41a152008-10-04 02:00:59 -04002300
Steven Rostedta1863c22009-09-03 10:23:58 -04002301static void rb_commit(struct ring_buffer_per_cpu *cpu_buffer,
2302 struct ring_buffer_event *event)
2303{
2304 local_inc(&cpu_buffer->entries);
2305 rb_update_write_stamp(cpu_buffer, event);
Steven Rostedtfa743952009-06-16 12:37:57 -04002306 rb_end_commit(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002307}
2308
2309/**
2310 * ring_buffer_unlock_commit - commit a reserved
2311 * @buffer: The buffer to commit to
2312 * @event: The event pointer to commit.
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002313 *
2314 * This commits the data to the ring buffer, and releases any locks held.
2315 *
2316 * Must be paired with ring_buffer_lock_reserve.
2317 */
2318int ring_buffer_unlock_commit(struct ring_buffer *buffer,
Arnaldo Carvalho de Melo0a987752009-02-05 16:12:56 -02002319 struct ring_buffer_event *event)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002320{
2321 struct ring_buffer_per_cpu *cpu_buffer;
2322 int cpu = raw_smp_processor_id();
2323
2324 cpu_buffer = buffer->buffers[cpu];
2325
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002326 rb_commit(cpu_buffer, event);
2327
Steven Rostedt261842b2009-04-16 21:41:52 -04002328 trace_recursive_unlock();
2329
Steven Rostedtbf41a152008-10-04 02:00:59 -04002330 /*
2331 * Only the last preempt count needs to restore preemption.
2332 */
Steven Rostedt182e9f52008-11-03 23:15:56 -05002333 if (preempt_count() == 1)
2334 ftrace_preempt_enable(per_cpu(rb_need_resched, cpu));
2335 else
Steven Rostedtbf41a152008-10-04 02:00:59 -04002336 preempt_enable_no_resched_notrace();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002337
2338 return 0;
2339}
Robert Richterc4f50182008-12-11 16:49:22 +01002340EXPORT_SYMBOL_GPL(ring_buffer_unlock_commit);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002341
Frederic Weisbeckerf3b9aae2009-04-19 23:39:33 +02002342static inline void rb_event_discard(struct ring_buffer_event *event)
2343{
Lai Jiangshan334d4162009-04-24 11:27:05 +08002344 /* array[0] holds the actual length for the discarded event */
2345 event->array[0] = rb_event_data_length(event) - RB_EVNT_HDR_SIZE;
2346 event->type_len = RINGBUF_TYPE_PADDING;
Frederic Weisbeckerf3b9aae2009-04-19 23:39:33 +02002347 /* time delta must be non zero */
2348 if (!event->time_delta)
2349 event->time_delta = 1;
2350}
2351
Steven Rostedta1863c22009-09-03 10:23:58 -04002352/*
2353 * Decrement the entries to the page that an event is on.
2354 * The event does not even need to exist, only the pointer
2355 * to the page it is on. This may only be called before the commit
2356 * takes place.
2357 */
2358static inline void
2359rb_decrement_entry(struct ring_buffer_per_cpu *cpu_buffer,
2360 struct ring_buffer_event *event)
2361{
2362 unsigned long addr = (unsigned long)event;
2363 struct buffer_page *bpage = cpu_buffer->commit_page;
2364 struct buffer_page *start;
2365
2366 addr &= PAGE_MASK;
2367
2368 /* Do the likely case first */
2369 if (likely(bpage->page == (void *)addr)) {
2370 local_dec(&bpage->entries);
2371 return;
2372 }
2373
2374 /*
2375 * Because the commit page may be on the reader page we
2376 * start with the next page and check the end loop there.
2377 */
2378 rb_inc_page(cpu_buffer, &bpage);
2379 start = bpage;
2380 do {
2381 if (bpage->page == (void *)addr) {
2382 local_dec(&bpage->entries);
2383 return;
2384 }
2385 rb_inc_page(cpu_buffer, &bpage);
2386 } while (bpage != start);
2387
2388 /* commit not part of this buffer?? */
2389 RB_WARN_ON(cpu_buffer, 1);
2390}
2391
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002392/**
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002393 * ring_buffer_commit_discard - discard an event that has not been committed
2394 * @buffer: the ring buffer
2395 * @event: non committed event to discard
2396 *
Steven Rostedtdc892f72009-09-03 15:33:41 -04002397 * Sometimes an event that is in the ring buffer needs to be ignored.
2398 * This function lets the user discard an event in the ring buffer
2399 * and then that event will not be read later.
2400 *
2401 * This function only works if it is called before the the item has been
2402 * committed. It will try to free the event from the ring buffer
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002403 * if another event has not been added behind it.
2404 *
2405 * If another event has been added behind it, it will set the event
2406 * up as discarded, and perform the commit.
2407 *
2408 * If this function is called, do not call ring_buffer_unlock_commit on
2409 * the event.
2410 */
2411void ring_buffer_discard_commit(struct ring_buffer *buffer,
2412 struct ring_buffer_event *event)
2413{
2414 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002415 int cpu;
2416
2417 /* The event is discarded regardless */
Frederic Weisbeckerf3b9aae2009-04-19 23:39:33 +02002418 rb_event_discard(event);
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002419
Steven Rostedtfa743952009-06-16 12:37:57 -04002420 cpu = smp_processor_id();
2421 cpu_buffer = buffer->buffers[cpu];
2422
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002423 /*
2424 * This must only be called if the event has not been
2425 * committed yet. Thus we can assume that preemption
2426 * is still disabled.
2427 */
Steven Rostedtfa743952009-06-16 12:37:57 -04002428 RB_WARN_ON(buffer, !local_read(&cpu_buffer->committing));
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002429
Steven Rostedta1863c22009-09-03 10:23:58 -04002430 rb_decrement_entry(cpu_buffer, event);
Steven Rostedt0f2541d2009-08-05 12:02:48 -04002431 if (rb_try_to_discard(cpu_buffer, event))
Steven Rostedtedd813bf2009-06-02 23:00:53 -04002432 goto out;
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002433
2434 /*
2435 * The commit is still visible by the reader, so we
Steven Rostedta1863c22009-09-03 10:23:58 -04002436 * must still update the timestamp.
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002437 */
Steven Rostedta1863c22009-09-03 10:23:58 -04002438 rb_update_write_stamp(cpu_buffer, event);
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002439 out:
Steven Rostedtfa743952009-06-16 12:37:57 -04002440 rb_end_commit(cpu_buffer);
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002441
Frederic Weisbeckerf3b9aae2009-04-19 23:39:33 +02002442 trace_recursive_unlock();
2443
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002444 /*
2445 * Only the last preempt count needs to restore preemption.
2446 */
2447 if (preempt_count() == 1)
2448 ftrace_preempt_enable(per_cpu(rb_need_resched, cpu));
2449 else
2450 preempt_enable_no_resched_notrace();
2451
2452}
2453EXPORT_SYMBOL_GPL(ring_buffer_discard_commit);
2454
2455/**
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002456 * ring_buffer_write - write data to the buffer without reserving
2457 * @buffer: The ring buffer to write to.
2458 * @length: The length of the data being written (excluding the event header)
2459 * @data: The data to write to the buffer.
2460 *
2461 * This is like ring_buffer_lock_reserve and ring_buffer_unlock_commit as
2462 * one function. If you already have the data to write to the buffer, it
2463 * may be easier to simply call this function.
2464 *
2465 * Note, like ring_buffer_lock_reserve, the length is the length of the data
2466 * and not the length of the event which would hold the header.
2467 */
2468int ring_buffer_write(struct ring_buffer *buffer,
2469 unsigned long length,
2470 void *data)
2471{
2472 struct ring_buffer_per_cpu *cpu_buffer;
2473 struct ring_buffer_event *event;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002474 void *body;
2475 int ret = -EBUSY;
Steven Rostedtbf41a152008-10-04 02:00:59 -04002476 int cpu, resched;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002477
Steven Rostedt033601a2008-11-21 12:41:55 -05002478 if (ring_buffer_flags != RB_BUFFERS_ON)
Steven Rostedta3583242008-11-11 15:01:42 -05002479 return -EBUSY;
2480
Steven Rostedt182e9f52008-11-03 23:15:56 -05002481 resched = ftrace_preempt_disable();
Steven Rostedtbf41a152008-10-04 02:00:59 -04002482
Lai Jiangshan52fbe9c2010-03-08 14:50:43 +08002483 if (atomic_read(&buffer->record_disabled))
2484 goto out;
2485
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002486 cpu = raw_smp_processor_id();
2487
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302488 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedtd7690412008-10-01 00:29:53 -04002489 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002490
2491 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002492
2493 if (atomic_read(&cpu_buffer->record_disabled))
2494 goto out;
2495
Steven Rostedtbe957c42009-05-11 14:42:53 -04002496 if (length > BUF_MAX_DATA_SIZE)
2497 goto out;
2498
Steven Rostedt62f0b3e2009-09-04 14:11:34 -04002499 event = rb_reserve_next_event(buffer, cpu_buffer, length);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002500 if (!event)
2501 goto out;
2502
2503 body = rb_event_data(event);
2504
2505 memcpy(body, data, length);
2506
2507 rb_commit(cpu_buffer, event);
2508
2509 ret = 0;
2510 out:
Steven Rostedt182e9f52008-11-03 23:15:56 -05002511 ftrace_preempt_enable(resched);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002512
2513 return ret;
2514}
Robert Richterc4f50182008-12-11 16:49:22 +01002515EXPORT_SYMBOL_GPL(ring_buffer_write);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002516
Andrew Morton34a148b2009-01-09 12:27:09 -08002517static int rb_per_cpu_empty(struct ring_buffer_per_cpu *cpu_buffer)
Steven Rostedtbf41a152008-10-04 02:00:59 -04002518{
2519 struct buffer_page *reader = cpu_buffer->reader_page;
Steven Rostedt77ae3652009-03-27 11:00:29 -04002520 struct buffer_page *head = rb_set_head_page(cpu_buffer);
Steven Rostedtbf41a152008-10-04 02:00:59 -04002521 struct buffer_page *commit = cpu_buffer->commit_page;
2522
Steven Rostedt77ae3652009-03-27 11:00:29 -04002523 /* In case of error, head will be NULL */
2524 if (unlikely(!head))
2525 return 1;
2526
Steven Rostedtbf41a152008-10-04 02:00:59 -04002527 return reader->read == rb_page_commit(reader) &&
2528 (commit == reader ||
2529 (commit == head &&
2530 head->read == rb_page_commit(commit)));
2531}
2532
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002533/**
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002534 * ring_buffer_record_disable - stop all writes into the buffer
2535 * @buffer: The ring buffer to stop writes to.
2536 *
2537 * This prevents all writes to the buffer. Any attempt to write
2538 * to the buffer after this will fail and return NULL.
2539 *
2540 * The caller should call synchronize_sched() after this.
2541 */
2542void ring_buffer_record_disable(struct ring_buffer *buffer)
2543{
2544 atomic_inc(&buffer->record_disabled);
2545}
Robert Richterc4f50182008-12-11 16:49:22 +01002546EXPORT_SYMBOL_GPL(ring_buffer_record_disable);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002547
2548/**
2549 * ring_buffer_record_enable - enable writes to the buffer
2550 * @buffer: The ring buffer to enable writes
2551 *
2552 * Note, multiple disables will need the same number of enables
Adam Buchbinderc41b20e2009-12-11 16:35:39 -05002553 * to truly enable the writing (much like preempt_disable).
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002554 */
2555void ring_buffer_record_enable(struct ring_buffer *buffer)
2556{
2557 atomic_dec(&buffer->record_disabled);
2558}
Robert Richterc4f50182008-12-11 16:49:22 +01002559EXPORT_SYMBOL_GPL(ring_buffer_record_enable);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002560
2561/**
2562 * ring_buffer_record_disable_cpu - stop all writes into the cpu_buffer
2563 * @buffer: The ring buffer to stop writes to.
2564 * @cpu: The CPU buffer to stop
2565 *
2566 * This prevents all writes to the buffer. Any attempt to write
2567 * to the buffer after this will fail and return NULL.
2568 *
2569 * The caller should call synchronize_sched() after this.
2570 */
2571void ring_buffer_record_disable_cpu(struct ring_buffer *buffer, int cpu)
2572{
2573 struct ring_buffer_per_cpu *cpu_buffer;
2574
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302575 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04002576 return;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002577
2578 cpu_buffer = buffer->buffers[cpu];
2579 atomic_inc(&cpu_buffer->record_disabled);
2580}
Robert Richterc4f50182008-12-11 16:49:22 +01002581EXPORT_SYMBOL_GPL(ring_buffer_record_disable_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002582
2583/**
2584 * ring_buffer_record_enable_cpu - enable writes to the buffer
2585 * @buffer: The ring buffer to enable writes
2586 * @cpu: The CPU to enable.
2587 *
2588 * Note, multiple disables will need the same number of enables
Adam Buchbinderc41b20e2009-12-11 16:35:39 -05002589 * to truly enable the writing (much like preempt_disable).
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002590 */
2591void ring_buffer_record_enable_cpu(struct ring_buffer *buffer, int cpu)
2592{
2593 struct ring_buffer_per_cpu *cpu_buffer;
2594
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302595 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04002596 return;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002597
2598 cpu_buffer = buffer->buffers[cpu];
2599 atomic_dec(&cpu_buffer->record_disabled);
2600}
Robert Richterc4f50182008-12-11 16:49:22 +01002601EXPORT_SYMBOL_GPL(ring_buffer_record_enable_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002602
2603/**
2604 * ring_buffer_entries_cpu - get the number of entries in a cpu buffer
2605 * @buffer: The ring buffer
2606 * @cpu: The per CPU buffer to get the entries from.
2607 */
2608unsigned long ring_buffer_entries_cpu(struct ring_buffer *buffer, int cpu)
2609{
2610 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedt8aabee52009-03-12 13:13:49 -04002611 unsigned long ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002612
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302613 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04002614 return 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002615
2616 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt77ae3652009-03-27 11:00:29 -04002617 ret = (local_read(&cpu_buffer->entries) - local_read(&cpu_buffer->overrun))
Steven Rostedte4906ef2009-04-30 20:49:44 -04002618 - cpu_buffer->read;
Steven Rostedt554f7862009-03-11 22:00:13 -04002619
2620 return ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002621}
Robert Richterc4f50182008-12-11 16:49:22 +01002622EXPORT_SYMBOL_GPL(ring_buffer_entries_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002623
2624/**
2625 * ring_buffer_overrun_cpu - get the number of overruns in a cpu_buffer
2626 * @buffer: The ring buffer
2627 * @cpu: The per CPU buffer to get the number of overruns from
2628 */
2629unsigned long ring_buffer_overrun_cpu(struct ring_buffer *buffer, int cpu)
2630{
2631 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedt8aabee52009-03-12 13:13:49 -04002632 unsigned long ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002633
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302634 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04002635 return 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002636
2637 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt77ae3652009-03-27 11:00:29 -04002638 ret = local_read(&cpu_buffer->overrun);
Steven Rostedt554f7862009-03-11 22:00:13 -04002639
2640 return ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002641}
Robert Richterc4f50182008-12-11 16:49:22 +01002642EXPORT_SYMBOL_GPL(ring_buffer_overrun_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002643
2644/**
Steven Rostedtf0d2c682009-04-29 13:43:37 -04002645 * ring_buffer_commit_overrun_cpu - get the number of overruns caused by commits
2646 * @buffer: The ring buffer
2647 * @cpu: The per CPU buffer to get the number of overruns from
2648 */
2649unsigned long
2650ring_buffer_commit_overrun_cpu(struct ring_buffer *buffer, int cpu)
2651{
2652 struct ring_buffer_per_cpu *cpu_buffer;
2653 unsigned long ret;
2654
2655 if (!cpumask_test_cpu(cpu, buffer->cpumask))
2656 return 0;
2657
2658 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt77ae3652009-03-27 11:00:29 -04002659 ret = local_read(&cpu_buffer->commit_overrun);
Steven Rostedtf0d2c682009-04-29 13:43:37 -04002660
2661 return ret;
2662}
2663EXPORT_SYMBOL_GPL(ring_buffer_commit_overrun_cpu);
2664
2665/**
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002666 * ring_buffer_entries - get the number of entries in a buffer
2667 * @buffer: The ring buffer
2668 *
2669 * Returns the total number of entries in the ring buffer
2670 * (all CPU entries)
2671 */
2672unsigned long ring_buffer_entries(struct ring_buffer *buffer)
2673{
2674 struct ring_buffer_per_cpu *cpu_buffer;
2675 unsigned long entries = 0;
2676 int cpu;
2677
2678 /* if you care about this being correct, lock the buffer */
2679 for_each_buffer_cpu(buffer, cpu) {
2680 cpu_buffer = buffer->buffers[cpu];
Steven Rostedte4906ef2009-04-30 20:49:44 -04002681 entries += (local_read(&cpu_buffer->entries) -
Steven Rostedt77ae3652009-03-27 11:00:29 -04002682 local_read(&cpu_buffer->overrun)) - cpu_buffer->read;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002683 }
2684
2685 return entries;
2686}
Robert Richterc4f50182008-12-11 16:49:22 +01002687EXPORT_SYMBOL_GPL(ring_buffer_entries);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002688
2689/**
Jiri Olsa67b394f2009-10-23 19:36:18 -04002690 * ring_buffer_overruns - get the number of overruns in buffer
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002691 * @buffer: The ring buffer
2692 *
2693 * Returns the total number of overruns in the ring buffer
2694 * (all CPU entries)
2695 */
2696unsigned long ring_buffer_overruns(struct ring_buffer *buffer)
2697{
2698 struct ring_buffer_per_cpu *cpu_buffer;
2699 unsigned long overruns = 0;
2700 int cpu;
2701
2702 /* if you care about this being correct, lock the buffer */
2703 for_each_buffer_cpu(buffer, cpu) {
2704 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt77ae3652009-03-27 11:00:29 -04002705 overruns += local_read(&cpu_buffer->overrun);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002706 }
2707
2708 return overruns;
2709}
Robert Richterc4f50182008-12-11 16:49:22 +01002710EXPORT_SYMBOL_GPL(ring_buffer_overruns);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002711
Steven Rostedt642edba2008-11-12 00:01:26 -05002712static void rb_iter_reset(struct ring_buffer_iter *iter)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002713{
2714 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
2715
Steven Rostedtd7690412008-10-01 00:29:53 -04002716 /* Iterator usage is expected to have record disabled */
2717 if (list_empty(&cpu_buffer->reader_page->list)) {
Steven Rostedt77ae3652009-03-27 11:00:29 -04002718 iter->head_page = rb_set_head_page(cpu_buffer);
2719 if (unlikely(!iter->head_page))
2720 return;
2721 iter->head = iter->head_page->read;
Steven Rostedtd7690412008-10-01 00:29:53 -04002722 } else {
2723 iter->head_page = cpu_buffer->reader_page;
Steven Rostedt6f807ac2008-10-04 02:00:58 -04002724 iter->head = cpu_buffer->reader_page->read;
Steven Rostedtd7690412008-10-01 00:29:53 -04002725 }
2726 if (iter->head)
2727 iter->read_stamp = cpu_buffer->read_stamp;
2728 else
Steven Rostedtabc9b562008-12-02 15:34:06 -05002729 iter->read_stamp = iter->head_page->page->time_stamp;
Steven Rostedt492a74f2010-01-25 15:17:47 -05002730 iter->cache_reader_page = cpu_buffer->reader_page;
2731 iter->cache_read = cpu_buffer->read;
Steven Rostedt642edba2008-11-12 00:01:26 -05002732}
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002733
Steven Rostedt642edba2008-11-12 00:01:26 -05002734/**
2735 * ring_buffer_iter_reset - reset an iterator
2736 * @iter: The iterator to reset
2737 *
2738 * Resets the iterator, so that it will start from the beginning
2739 * again.
2740 */
2741void ring_buffer_iter_reset(struct ring_buffer_iter *iter)
2742{
Steven Rostedt554f7862009-03-11 22:00:13 -04002743 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedt642edba2008-11-12 00:01:26 -05002744 unsigned long flags;
2745
Steven Rostedt554f7862009-03-11 22:00:13 -04002746 if (!iter)
2747 return;
2748
2749 cpu_buffer = iter->cpu_buffer;
2750
Steven Rostedt642edba2008-11-12 00:01:26 -05002751 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
2752 rb_iter_reset(iter);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002753 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002754}
Robert Richterc4f50182008-12-11 16:49:22 +01002755EXPORT_SYMBOL_GPL(ring_buffer_iter_reset);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002756
2757/**
2758 * ring_buffer_iter_empty - check if an iterator has no more to read
2759 * @iter: The iterator to check
2760 */
2761int ring_buffer_iter_empty(struct ring_buffer_iter *iter)
2762{
2763 struct ring_buffer_per_cpu *cpu_buffer;
2764
2765 cpu_buffer = iter->cpu_buffer;
2766
Steven Rostedtbf41a152008-10-04 02:00:59 -04002767 return iter->head_page == cpu_buffer->commit_page &&
2768 iter->head == rb_commit_index(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002769}
Robert Richterc4f50182008-12-11 16:49:22 +01002770EXPORT_SYMBOL_GPL(ring_buffer_iter_empty);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002771
2772static void
2773rb_update_read_stamp(struct ring_buffer_per_cpu *cpu_buffer,
2774 struct ring_buffer_event *event)
2775{
2776 u64 delta;
2777
Lai Jiangshan334d4162009-04-24 11:27:05 +08002778 switch (event->type_len) {
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002779 case RINGBUF_TYPE_PADDING:
2780 return;
2781
2782 case RINGBUF_TYPE_TIME_EXTEND:
2783 delta = event->array[0];
2784 delta <<= TS_SHIFT;
2785 delta += event->time_delta;
2786 cpu_buffer->read_stamp += delta;
2787 return;
2788
2789 case RINGBUF_TYPE_TIME_STAMP:
2790 /* FIXME: not implemented */
2791 return;
2792
2793 case RINGBUF_TYPE_DATA:
2794 cpu_buffer->read_stamp += event->time_delta;
2795 return;
2796
2797 default:
2798 BUG();
2799 }
2800 return;
2801}
2802
2803static void
2804rb_update_iter_read_stamp(struct ring_buffer_iter *iter,
2805 struct ring_buffer_event *event)
2806{
2807 u64 delta;
2808
Lai Jiangshan334d4162009-04-24 11:27:05 +08002809 switch (event->type_len) {
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002810 case RINGBUF_TYPE_PADDING:
2811 return;
2812
2813 case RINGBUF_TYPE_TIME_EXTEND:
2814 delta = event->array[0];
2815 delta <<= TS_SHIFT;
2816 delta += event->time_delta;
2817 iter->read_stamp += delta;
2818 return;
2819
2820 case RINGBUF_TYPE_TIME_STAMP:
2821 /* FIXME: not implemented */
2822 return;
2823
2824 case RINGBUF_TYPE_DATA:
2825 iter->read_stamp += event->time_delta;
2826 return;
2827
2828 default:
2829 BUG();
2830 }
2831 return;
2832}
2833
Steven Rostedtd7690412008-10-01 00:29:53 -04002834static struct buffer_page *
2835rb_get_reader_page(struct ring_buffer_per_cpu *cpu_buffer)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002836{
Steven Rostedtd7690412008-10-01 00:29:53 -04002837 struct buffer_page *reader = NULL;
2838 unsigned long flags;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002839 int nr_loops = 0;
Steven Rostedt77ae3652009-03-27 11:00:29 -04002840 int ret;
Steven Rostedtd7690412008-10-01 00:29:53 -04002841
Steven Rostedt3e03fb72008-11-06 00:09:43 -05002842 local_irq_save(flags);
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01002843 arch_spin_lock(&cpu_buffer->lock);
Steven Rostedtd7690412008-10-01 00:29:53 -04002844
2845 again:
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002846 /*
2847 * This should normally only loop twice. But because the
2848 * start of the reader inserts an empty page, it causes
2849 * a case where we will loop three times. There should be no
2850 * reason to loop four times (that I know of).
2851 */
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05002852 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 3)) {
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002853 reader = NULL;
2854 goto out;
2855 }
2856
Steven Rostedtd7690412008-10-01 00:29:53 -04002857 reader = cpu_buffer->reader_page;
2858
2859 /* If there's more to read, return this page */
Steven Rostedtbf41a152008-10-04 02:00:59 -04002860 if (cpu_buffer->reader_page->read < rb_page_size(reader))
Steven Rostedtd7690412008-10-01 00:29:53 -04002861 goto out;
2862
2863 /* Never should we have an index greater than the size */
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05002864 if (RB_WARN_ON(cpu_buffer,
2865 cpu_buffer->reader_page->read > rb_page_size(reader)))
2866 goto out;
Steven Rostedtd7690412008-10-01 00:29:53 -04002867
2868 /* check if we caught up to the tail */
2869 reader = NULL;
Steven Rostedtbf41a152008-10-04 02:00:59 -04002870 if (cpu_buffer->commit_page == cpu_buffer->reader_page)
Steven Rostedtd7690412008-10-01 00:29:53 -04002871 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002872
2873 /*
Steven Rostedtd7690412008-10-01 00:29:53 -04002874 * Reset the reader page to size zero.
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002875 */
Steven Rostedt77ae3652009-03-27 11:00:29 -04002876 local_set(&cpu_buffer->reader_page->write, 0);
2877 local_set(&cpu_buffer->reader_page->entries, 0);
2878 local_set(&cpu_buffer->reader_page->page->commit, 0);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002879
Steven Rostedt77ae3652009-03-27 11:00:29 -04002880 spin:
2881 /*
2882 * Splice the empty reader page into the list around the head.
2883 */
2884 reader = rb_set_head_page(cpu_buffer);
Steven Rostedt0e1ff5d2010-01-06 20:40:44 -05002885 cpu_buffer->reader_page->list.next = rb_list_head(reader->list.next);
Steven Rostedtd7690412008-10-01 00:29:53 -04002886 cpu_buffer->reader_page->list.prev = reader->list.prev;
Steven Rostedtbf41a152008-10-04 02:00:59 -04002887
Steven Rostedt3adc54f2009-03-30 15:32:01 -04002888 /*
2889 * cpu_buffer->pages just needs to point to the buffer, it
2890 * has no specific buffer page to point to. Lets move it out
2891 * of our way so we don't accidently swap it.
2892 */
2893 cpu_buffer->pages = reader->list.prev;
2894
Steven Rostedt77ae3652009-03-27 11:00:29 -04002895 /* The reader page will be pointing to the new head */
2896 rb_set_list_to_head(cpu_buffer, &cpu_buffer->reader_page->list);
Steven Rostedtd7690412008-10-01 00:29:53 -04002897
2898 /*
Steven Rostedt77ae3652009-03-27 11:00:29 -04002899 * Here's the tricky part.
2900 *
2901 * We need to move the pointer past the header page.
2902 * But we can only do that if a writer is not currently
2903 * moving it. The page before the header page has the
2904 * flag bit '1' set if it is pointing to the page we want.
2905 * but if the writer is in the process of moving it
2906 * than it will be '2' or already moved '0'.
Steven Rostedtd7690412008-10-01 00:29:53 -04002907 */
Steven Rostedtd7690412008-10-01 00:29:53 -04002908
Steven Rostedt77ae3652009-03-27 11:00:29 -04002909 ret = rb_head_page_replace(reader, cpu_buffer->reader_page);
2910
2911 /*
2912 * If we did not convert it, then we must try again.
2913 */
2914 if (!ret)
2915 goto spin;
2916
2917 /*
2918 * Yeah! We succeeded in replacing the page.
2919 *
2920 * Now make the new head point back to the reader page.
2921 */
David Sharp5ded3dc2010-01-06 17:12:07 -08002922 rb_list_head(reader->list.next)->prev = &cpu_buffer->reader_page->list;
Steven Rostedt77ae3652009-03-27 11:00:29 -04002923 rb_inc_page(cpu_buffer, &cpu_buffer->head_page);
Steven Rostedtd7690412008-10-01 00:29:53 -04002924
2925 /* Finally update the reader page to the new head */
2926 cpu_buffer->reader_page = reader;
2927 rb_reset_reader_page(cpu_buffer);
2928
2929 goto again;
2930
2931 out:
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01002932 arch_spin_unlock(&cpu_buffer->lock);
Steven Rostedt3e03fb72008-11-06 00:09:43 -05002933 local_irq_restore(flags);
Steven Rostedtd7690412008-10-01 00:29:53 -04002934
2935 return reader;
2936}
2937
2938static void rb_advance_reader(struct ring_buffer_per_cpu *cpu_buffer)
2939{
2940 struct ring_buffer_event *event;
2941 struct buffer_page *reader;
2942 unsigned length;
2943
2944 reader = rb_get_reader_page(cpu_buffer);
2945
2946 /* This function should not be called when buffer is empty */
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05002947 if (RB_WARN_ON(cpu_buffer, !reader))
2948 return;
Steven Rostedtd7690412008-10-01 00:29:53 -04002949
2950 event = rb_reader_event(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002951
Steven Rostedta1863c22009-09-03 10:23:58 -04002952 if (event->type_len <= RINGBUF_TYPE_DATA_TYPE_LEN_MAX)
Steven Rostedte4906ef2009-04-30 20:49:44 -04002953 cpu_buffer->read++;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002954
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002955 rb_update_read_stamp(cpu_buffer, event);
2956
Steven Rostedtd7690412008-10-01 00:29:53 -04002957 length = rb_event_length(event);
Steven Rostedt6f807ac2008-10-04 02:00:58 -04002958 cpu_buffer->reader_page->read += length;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002959}
2960
2961static void rb_advance_iter(struct ring_buffer_iter *iter)
2962{
2963 struct ring_buffer *buffer;
2964 struct ring_buffer_per_cpu *cpu_buffer;
2965 struct ring_buffer_event *event;
2966 unsigned length;
2967
2968 cpu_buffer = iter->cpu_buffer;
2969 buffer = cpu_buffer->buffer;
2970
2971 /*
2972 * Check if we are at the end of the buffer.
2973 */
Steven Rostedtbf41a152008-10-04 02:00:59 -04002974 if (iter->head >= rb_page_size(iter->head_page)) {
Steven Rostedtea05b572009-06-03 09:30:10 -04002975 /* discarded commits can make the page empty */
2976 if (iter->head_page == cpu_buffer->commit_page)
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05002977 return;
Steven Rostedtd7690412008-10-01 00:29:53 -04002978 rb_inc_iter(iter);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002979 return;
2980 }
2981
2982 event = rb_iter_head_event(iter);
2983
2984 length = rb_event_length(event);
2985
2986 /*
2987 * This should not be called to advance the header if we are
2988 * at the tail of the buffer.
2989 */
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05002990 if (RB_WARN_ON(cpu_buffer,
Steven Rostedtf536aaf2008-11-10 23:07:30 -05002991 (iter->head_page == cpu_buffer->commit_page) &&
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05002992 (iter->head + length > rb_commit_index(cpu_buffer))))
2993 return;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002994
2995 rb_update_iter_read_stamp(iter, event);
2996
2997 iter->head += length;
2998
2999 /* check for end of page padding */
Steven Rostedtbf41a152008-10-04 02:00:59 -04003000 if ((iter->head >= rb_page_size(iter->head_page)) &&
3001 (iter->head_page != cpu_buffer->commit_page))
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003002 rb_advance_iter(iter);
3003}
3004
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003005static struct ring_buffer_event *
Robert Richterd8eeb2d2009-07-31 14:58:04 +02003006rb_buffer_peek(struct ring_buffer_per_cpu *cpu_buffer, u64 *ts)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003007{
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003008 struct ring_buffer_event *event;
Steven Rostedtd7690412008-10-01 00:29:53 -04003009 struct buffer_page *reader;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003010 int nr_loops = 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003011
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003012 again:
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003013 /*
3014 * We repeat when a timestamp is encountered. It is possible
3015 * to get multiple timestamps from an interrupt entering just
Steven Rostedtea05b572009-06-03 09:30:10 -04003016 * as one timestamp is about to be written, or from discarded
3017 * commits. The most that we can have is the number on a single page.
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003018 */
Steven Rostedtea05b572009-06-03 09:30:10 -04003019 if (RB_WARN_ON(cpu_buffer, ++nr_loops > RB_TIMESTAMPS_PER_PAGE))
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003020 return NULL;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003021
Steven Rostedtd7690412008-10-01 00:29:53 -04003022 reader = rb_get_reader_page(cpu_buffer);
3023 if (!reader)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003024 return NULL;
3025
Steven Rostedtd7690412008-10-01 00:29:53 -04003026 event = rb_reader_event(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003027
Lai Jiangshan334d4162009-04-24 11:27:05 +08003028 switch (event->type_len) {
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003029 case RINGBUF_TYPE_PADDING:
Tom Zanussi2d622712009-03-22 03:30:49 -05003030 if (rb_null_event(event))
3031 RB_WARN_ON(cpu_buffer, 1);
3032 /*
3033 * Because the writer could be discarding every
3034 * event it creates (which would probably be bad)
3035 * if we were to go back to "again" then we may never
3036 * catch up, and will trigger the warn on, or lock
3037 * the box. Return the padding, and we will release
3038 * the current locks, and try again.
3039 */
Tom Zanussi2d622712009-03-22 03:30:49 -05003040 return event;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003041
3042 case RINGBUF_TYPE_TIME_EXTEND:
3043 /* Internal data, OK to advance */
Steven Rostedtd7690412008-10-01 00:29:53 -04003044 rb_advance_reader(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003045 goto again;
3046
3047 case RINGBUF_TYPE_TIME_STAMP:
3048 /* FIXME: not implemented */
Steven Rostedtd7690412008-10-01 00:29:53 -04003049 rb_advance_reader(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003050 goto again;
3051
3052 case RINGBUF_TYPE_DATA:
3053 if (ts) {
3054 *ts = cpu_buffer->read_stamp + event->time_delta;
Robert Richterd8eeb2d2009-07-31 14:58:04 +02003055 ring_buffer_normalize_time_stamp(cpu_buffer->buffer,
Steven Rostedt37886f62009-03-17 17:22:06 -04003056 cpu_buffer->cpu, ts);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003057 }
3058 return event;
3059
3060 default:
3061 BUG();
3062 }
3063
3064 return NULL;
3065}
Robert Richterc4f50182008-12-11 16:49:22 +01003066EXPORT_SYMBOL_GPL(ring_buffer_peek);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003067
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003068static struct ring_buffer_event *
3069rb_iter_peek(struct ring_buffer_iter *iter, u64 *ts)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003070{
3071 struct ring_buffer *buffer;
3072 struct ring_buffer_per_cpu *cpu_buffer;
3073 struct ring_buffer_event *event;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003074 int nr_loops = 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003075
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003076 cpu_buffer = iter->cpu_buffer;
3077 buffer = cpu_buffer->buffer;
3078
Steven Rostedt492a74f2010-01-25 15:17:47 -05003079 /*
3080 * Check if someone performed a consuming read to
3081 * the buffer. A consuming read invalidates the iterator
3082 * and we need to reset the iterator in this case.
3083 */
3084 if (unlikely(iter->cache_read != cpu_buffer->read ||
3085 iter->cache_reader_page != cpu_buffer->reader_page))
3086 rb_iter_reset(iter);
3087
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003088 again:
Steven Rostedt3c05d742010-01-26 16:14:08 -05003089 if (ring_buffer_iter_empty(iter))
3090 return NULL;
3091
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003092 /*
Steven Rostedtea05b572009-06-03 09:30:10 -04003093 * We repeat when a timestamp is encountered.
3094 * We can get multiple timestamps by nested interrupts or also
3095 * if filtering is on (discarding commits). Since discarding
3096 * commits can be frequent we can get a lot of timestamps.
3097 * But we limit them by not adding timestamps if they begin
3098 * at the start of a page.
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003099 */
Steven Rostedtea05b572009-06-03 09:30:10 -04003100 if (RB_WARN_ON(cpu_buffer, ++nr_loops > RB_TIMESTAMPS_PER_PAGE))
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003101 return NULL;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003102
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003103 if (rb_per_cpu_empty(cpu_buffer))
3104 return NULL;
3105
Steven Rostedt3c05d742010-01-26 16:14:08 -05003106 if (iter->head >= local_read(&iter->head_page->page->commit)) {
3107 rb_inc_iter(iter);
3108 goto again;
3109 }
3110
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003111 event = rb_iter_head_event(iter);
3112
Lai Jiangshan334d4162009-04-24 11:27:05 +08003113 switch (event->type_len) {
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003114 case RINGBUF_TYPE_PADDING:
Tom Zanussi2d622712009-03-22 03:30:49 -05003115 if (rb_null_event(event)) {
3116 rb_inc_iter(iter);
3117 goto again;
3118 }
3119 rb_advance_iter(iter);
3120 return event;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003121
3122 case RINGBUF_TYPE_TIME_EXTEND:
3123 /* Internal data, OK to advance */
3124 rb_advance_iter(iter);
3125 goto again;
3126
3127 case RINGBUF_TYPE_TIME_STAMP:
3128 /* FIXME: not implemented */
3129 rb_advance_iter(iter);
3130 goto again;
3131
3132 case RINGBUF_TYPE_DATA:
3133 if (ts) {
3134 *ts = iter->read_stamp + event->time_delta;
Steven Rostedt37886f62009-03-17 17:22:06 -04003135 ring_buffer_normalize_time_stamp(buffer,
3136 cpu_buffer->cpu, ts);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003137 }
3138 return event;
3139
3140 default:
3141 BUG();
3142 }
3143
3144 return NULL;
3145}
Robert Richterc4f50182008-12-11 16:49:22 +01003146EXPORT_SYMBOL_GPL(ring_buffer_iter_peek);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003147
Steven Rostedt8d707e82009-06-16 21:22:48 -04003148static inline int rb_ok_to_lock(void)
3149{
3150 /*
3151 * If an NMI die dumps out the content of the ring buffer
3152 * do not grab locks. We also permanently disable the ring
3153 * buffer too. A one time deal is all you get from reading
3154 * the ring buffer from an NMI.
3155 */
Steven Rostedt464e85e2009-08-05 15:26:37 -04003156 if (likely(!in_nmi()))
Steven Rostedt8d707e82009-06-16 21:22:48 -04003157 return 1;
3158
3159 tracing_off_permanent();
3160 return 0;
3161}
3162
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003163/**
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003164 * ring_buffer_peek - peek at the next event to be read
3165 * @buffer: The ring buffer to read
3166 * @cpu: The cpu to peak at
3167 * @ts: The timestamp counter of this event.
3168 *
3169 * This will return the event that will be read next, but does
3170 * not consume the data.
3171 */
3172struct ring_buffer_event *
3173ring_buffer_peek(struct ring_buffer *buffer, int cpu, u64 *ts)
3174{
3175 struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
Steven Rostedt8aabee52009-03-12 13:13:49 -04003176 struct ring_buffer_event *event;
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003177 unsigned long flags;
Steven Rostedt8d707e82009-06-16 21:22:48 -04003178 int dolock;
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003179
Steven Rostedt554f7862009-03-11 22:00:13 -04003180 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04003181 return NULL;
Steven Rostedt554f7862009-03-11 22:00:13 -04003182
Steven Rostedt8d707e82009-06-16 21:22:48 -04003183 dolock = rb_ok_to_lock();
Tom Zanussi2d622712009-03-22 03:30:49 -05003184 again:
Steven Rostedt8d707e82009-06-16 21:22:48 -04003185 local_irq_save(flags);
3186 if (dolock)
3187 spin_lock(&cpu_buffer->reader_lock);
Robert Richterd8eeb2d2009-07-31 14:58:04 +02003188 event = rb_buffer_peek(cpu_buffer, ts);
Robert Richter469535a2009-07-30 19:19:18 +02003189 if (event && event->type_len == RINGBUF_TYPE_PADDING)
3190 rb_advance_reader(cpu_buffer);
Steven Rostedt8d707e82009-06-16 21:22:48 -04003191 if (dolock)
3192 spin_unlock(&cpu_buffer->reader_lock);
3193 local_irq_restore(flags);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003194
Steven Rostedt1b959e12009-09-03 10:12:13 -04003195 if (event && event->type_len == RINGBUF_TYPE_PADDING)
Tom Zanussi2d622712009-03-22 03:30:49 -05003196 goto again;
Tom Zanussi2d622712009-03-22 03:30:49 -05003197
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003198 return event;
3199}
3200
3201/**
3202 * ring_buffer_iter_peek - peek at the next event to be read
3203 * @iter: The ring buffer iterator
3204 * @ts: The timestamp counter of this event.
3205 *
3206 * This will return the event that will be read next, but does
3207 * not increment the iterator.
3208 */
3209struct ring_buffer_event *
3210ring_buffer_iter_peek(struct ring_buffer_iter *iter, u64 *ts)
3211{
3212 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
3213 struct ring_buffer_event *event;
3214 unsigned long flags;
3215
Tom Zanussi2d622712009-03-22 03:30:49 -05003216 again:
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003217 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
3218 event = rb_iter_peek(iter, ts);
3219 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
3220
Steven Rostedt1b959e12009-09-03 10:12:13 -04003221 if (event && event->type_len == RINGBUF_TYPE_PADDING)
Tom Zanussi2d622712009-03-22 03:30:49 -05003222 goto again;
Tom Zanussi2d622712009-03-22 03:30:49 -05003223
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003224 return event;
3225}
3226
3227/**
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003228 * ring_buffer_consume - return an event and consume it
3229 * @buffer: The ring buffer to get the next event from
3230 *
3231 * Returns the next event in the ring buffer, and that event is consumed.
3232 * Meaning, that sequential reads will keep returning a different event,
3233 * and eventually empty the ring buffer if the producer is slower.
3234 */
3235struct ring_buffer_event *
3236ring_buffer_consume(struct ring_buffer *buffer, int cpu, u64 *ts)
3237{
Steven Rostedt554f7862009-03-11 22:00:13 -04003238 struct ring_buffer_per_cpu *cpu_buffer;
3239 struct ring_buffer_event *event = NULL;
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003240 unsigned long flags;
Steven Rostedt8d707e82009-06-16 21:22:48 -04003241 int dolock;
3242
3243 dolock = rb_ok_to_lock();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003244
Tom Zanussi2d622712009-03-22 03:30:49 -05003245 again:
Steven Rostedt554f7862009-03-11 22:00:13 -04003246 /* might be called in atomic */
3247 preempt_disable();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003248
Steven Rostedt554f7862009-03-11 22:00:13 -04003249 if (!cpumask_test_cpu(cpu, buffer->cpumask))
3250 goto out;
3251
3252 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt8d707e82009-06-16 21:22:48 -04003253 local_irq_save(flags);
3254 if (dolock)
3255 spin_lock(&cpu_buffer->reader_lock);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003256
Robert Richterd8eeb2d2009-07-31 14:58:04 +02003257 event = rb_buffer_peek(cpu_buffer, ts);
Robert Richter469535a2009-07-30 19:19:18 +02003258 if (event)
3259 rb_advance_reader(cpu_buffer);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003260
Steven Rostedt8d707e82009-06-16 21:22:48 -04003261 if (dolock)
3262 spin_unlock(&cpu_buffer->reader_lock);
3263 local_irq_restore(flags);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003264
Steven Rostedt554f7862009-03-11 22:00:13 -04003265 out:
3266 preempt_enable();
3267
Steven Rostedt1b959e12009-09-03 10:12:13 -04003268 if (event && event->type_len == RINGBUF_TYPE_PADDING)
Tom Zanussi2d622712009-03-22 03:30:49 -05003269 goto again;
Tom Zanussi2d622712009-03-22 03:30:49 -05003270
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003271 return event;
3272}
Robert Richterc4f50182008-12-11 16:49:22 +01003273EXPORT_SYMBOL_GPL(ring_buffer_consume);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003274
3275/**
3276 * ring_buffer_read_start - start a non consuming read of the buffer
3277 * @buffer: The ring buffer to read from
3278 * @cpu: The cpu buffer to iterate over
3279 *
3280 * This starts up an iteration through the buffer. It also disables
3281 * the recording to the buffer until the reading is finished.
3282 * This prevents the reading from being corrupted. This is not
3283 * a consuming read, so a producer is not expected.
3284 *
3285 * Must be paired with ring_buffer_finish.
3286 */
3287struct ring_buffer_iter *
3288ring_buffer_read_start(struct ring_buffer *buffer, int cpu)
3289{
3290 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedt8aabee52009-03-12 13:13:49 -04003291 struct ring_buffer_iter *iter;
Steven Rostedtd7690412008-10-01 00:29:53 -04003292 unsigned long flags;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003293
Rusty Russell9e01c1b2009-01-01 10:12:22 +10303294 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04003295 return NULL;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003296
3297 iter = kmalloc(sizeof(*iter), GFP_KERNEL);
3298 if (!iter)
Steven Rostedt8aabee52009-03-12 13:13:49 -04003299 return NULL;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003300
3301 cpu_buffer = buffer->buffers[cpu];
3302
3303 iter->cpu_buffer = cpu_buffer;
3304
3305 atomic_inc(&cpu_buffer->record_disabled);
3306 synchronize_sched();
3307
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003308 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01003309 arch_spin_lock(&cpu_buffer->lock);
Steven Rostedt642edba2008-11-12 00:01:26 -05003310 rb_iter_reset(iter);
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01003311 arch_spin_unlock(&cpu_buffer->lock);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003312 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003313
3314 return iter;
3315}
Robert Richterc4f50182008-12-11 16:49:22 +01003316EXPORT_SYMBOL_GPL(ring_buffer_read_start);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003317
3318/**
3319 * ring_buffer_finish - finish reading the iterator of the buffer
3320 * @iter: The iterator retrieved by ring_buffer_start
3321 *
3322 * This re-enables the recording to the buffer, and frees the
3323 * iterator.
3324 */
3325void
3326ring_buffer_read_finish(struct ring_buffer_iter *iter)
3327{
3328 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
3329
3330 atomic_dec(&cpu_buffer->record_disabled);
3331 kfree(iter);
3332}
Robert Richterc4f50182008-12-11 16:49:22 +01003333EXPORT_SYMBOL_GPL(ring_buffer_read_finish);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003334
3335/**
3336 * ring_buffer_read - read the next item in the ring buffer by the iterator
3337 * @iter: The ring buffer iterator
3338 * @ts: The time stamp of the event read.
3339 *
3340 * This reads the next event in the ring buffer and increments the iterator.
3341 */
3342struct ring_buffer_event *
3343ring_buffer_read(struct ring_buffer_iter *iter, u64 *ts)
3344{
3345 struct ring_buffer_event *event;
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003346 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
3347 unsigned long flags;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003348
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003349 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
Steven Rostedt7e9391c2009-09-03 10:02:09 -04003350 again:
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003351 event = rb_iter_peek(iter, ts);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003352 if (!event)
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003353 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003354
Steven Rostedt7e9391c2009-09-03 10:02:09 -04003355 if (event->type_len == RINGBUF_TYPE_PADDING)
3356 goto again;
3357
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003358 rb_advance_iter(iter);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003359 out:
3360 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003361
3362 return event;
3363}
Robert Richterc4f50182008-12-11 16:49:22 +01003364EXPORT_SYMBOL_GPL(ring_buffer_read);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003365
3366/**
3367 * ring_buffer_size - return the size of the ring buffer (in bytes)
3368 * @buffer: The ring buffer.
3369 */
3370unsigned long ring_buffer_size(struct ring_buffer *buffer)
3371{
3372 return BUF_PAGE_SIZE * buffer->pages;
3373}
Robert Richterc4f50182008-12-11 16:49:22 +01003374EXPORT_SYMBOL_GPL(ring_buffer_size);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003375
3376static void
3377rb_reset_cpu(struct ring_buffer_per_cpu *cpu_buffer)
3378{
Steven Rostedt77ae3652009-03-27 11:00:29 -04003379 rb_head_page_deactivate(cpu_buffer);
3380
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003381 cpu_buffer->head_page
Steven Rostedt3adc54f2009-03-30 15:32:01 -04003382 = list_entry(cpu_buffer->pages, struct buffer_page, list);
Steven Rostedtbf41a152008-10-04 02:00:59 -04003383 local_set(&cpu_buffer->head_page->write, 0);
Steven Rostedt778c55d2009-05-01 18:44:45 -04003384 local_set(&cpu_buffer->head_page->entries, 0);
Steven Rostedtabc9b562008-12-02 15:34:06 -05003385 local_set(&cpu_buffer->head_page->page->commit, 0);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003386
Steven Rostedt6f807ac2008-10-04 02:00:58 -04003387 cpu_buffer->head_page->read = 0;
Steven Rostedtbf41a152008-10-04 02:00:59 -04003388
3389 cpu_buffer->tail_page = cpu_buffer->head_page;
3390 cpu_buffer->commit_page = cpu_buffer->head_page;
3391
3392 INIT_LIST_HEAD(&cpu_buffer->reader_page->list);
3393 local_set(&cpu_buffer->reader_page->write, 0);
Steven Rostedt778c55d2009-05-01 18:44:45 -04003394 local_set(&cpu_buffer->reader_page->entries, 0);
Steven Rostedtabc9b562008-12-02 15:34:06 -05003395 local_set(&cpu_buffer->reader_page->page->commit, 0);
Steven Rostedt6f807ac2008-10-04 02:00:58 -04003396 cpu_buffer->reader_page->read = 0;
Steven Rostedtd7690412008-10-01 00:29:53 -04003397
Steven Rostedt77ae3652009-03-27 11:00:29 -04003398 local_set(&cpu_buffer->commit_overrun, 0);
3399 local_set(&cpu_buffer->overrun, 0);
Steven Rostedte4906ef2009-04-30 20:49:44 -04003400 local_set(&cpu_buffer->entries, 0);
Steven Rostedtfa743952009-06-16 12:37:57 -04003401 local_set(&cpu_buffer->committing, 0);
3402 local_set(&cpu_buffer->commits, 0);
Steven Rostedt77ae3652009-03-27 11:00:29 -04003403 cpu_buffer->read = 0;
Steven Rostedt69507c02009-01-21 18:45:57 -05003404
3405 cpu_buffer->write_stamp = 0;
3406 cpu_buffer->read_stamp = 0;
Steven Rostedt77ae3652009-03-27 11:00:29 -04003407
3408 rb_head_page_activate(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003409}
3410
3411/**
3412 * ring_buffer_reset_cpu - reset a ring buffer per CPU buffer
3413 * @buffer: The ring buffer to reset a per cpu buffer of
3414 * @cpu: The CPU buffer to be reset
3415 */
3416void ring_buffer_reset_cpu(struct ring_buffer *buffer, int cpu)
3417{
3418 struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
3419 unsigned long flags;
3420
Rusty Russell9e01c1b2009-01-01 10:12:22 +10303421 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04003422 return;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003423
Steven Rostedt41ede232009-05-01 20:26:54 -04003424 atomic_inc(&cpu_buffer->record_disabled);
3425
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003426 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
3427
Steven Rostedt41b6a952009-09-02 09:59:48 -04003428 if (RB_WARN_ON(cpu_buffer, local_read(&cpu_buffer->committing)))
3429 goto out;
3430
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01003431 arch_spin_lock(&cpu_buffer->lock);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003432
3433 rb_reset_cpu(cpu_buffer);
3434
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01003435 arch_spin_unlock(&cpu_buffer->lock);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003436
Steven Rostedt41b6a952009-09-02 09:59:48 -04003437 out:
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003438 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
Steven Rostedt41ede232009-05-01 20:26:54 -04003439
3440 atomic_dec(&cpu_buffer->record_disabled);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003441}
Robert Richterc4f50182008-12-11 16:49:22 +01003442EXPORT_SYMBOL_GPL(ring_buffer_reset_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003443
3444/**
3445 * ring_buffer_reset - reset a ring buffer
3446 * @buffer: The ring buffer to reset all cpu buffers
3447 */
3448void ring_buffer_reset(struct ring_buffer *buffer)
3449{
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003450 int cpu;
3451
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003452 for_each_buffer_cpu(buffer, cpu)
Steven Rostedtd7690412008-10-01 00:29:53 -04003453 ring_buffer_reset_cpu(buffer, cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003454}
Robert Richterc4f50182008-12-11 16:49:22 +01003455EXPORT_SYMBOL_GPL(ring_buffer_reset);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003456
3457/**
3458 * rind_buffer_empty - is the ring buffer empty?
3459 * @buffer: The ring buffer to test
3460 */
3461int ring_buffer_empty(struct ring_buffer *buffer)
3462{
3463 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedtd4788202009-06-17 00:39:43 -04003464 unsigned long flags;
Steven Rostedt8d707e82009-06-16 21:22:48 -04003465 int dolock;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003466 int cpu;
Steven Rostedtd4788202009-06-17 00:39:43 -04003467 int ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003468
Steven Rostedt8d707e82009-06-16 21:22:48 -04003469 dolock = rb_ok_to_lock();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003470
3471 /* yes this is racy, but if you don't like the race, lock the buffer */
3472 for_each_buffer_cpu(buffer, cpu) {
3473 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt8d707e82009-06-16 21:22:48 -04003474 local_irq_save(flags);
3475 if (dolock)
3476 spin_lock(&cpu_buffer->reader_lock);
Steven Rostedtd4788202009-06-17 00:39:43 -04003477 ret = rb_per_cpu_empty(cpu_buffer);
Steven Rostedt8d707e82009-06-16 21:22:48 -04003478 if (dolock)
3479 spin_unlock(&cpu_buffer->reader_lock);
3480 local_irq_restore(flags);
3481
Steven Rostedtd4788202009-06-17 00:39:43 -04003482 if (!ret)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003483 return 0;
3484 }
Steven Rostedt554f7862009-03-11 22:00:13 -04003485
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003486 return 1;
3487}
Robert Richterc4f50182008-12-11 16:49:22 +01003488EXPORT_SYMBOL_GPL(ring_buffer_empty);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003489
3490/**
3491 * ring_buffer_empty_cpu - is a cpu buffer of a ring buffer empty?
3492 * @buffer: The ring buffer
3493 * @cpu: The CPU buffer to test
3494 */
3495int ring_buffer_empty_cpu(struct ring_buffer *buffer, int cpu)
3496{
3497 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedtd4788202009-06-17 00:39:43 -04003498 unsigned long flags;
Steven Rostedt8d707e82009-06-16 21:22:48 -04003499 int dolock;
Steven Rostedt8aabee52009-03-12 13:13:49 -04003500 int ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003501
Rusty Russell9e01c1b2009-01-01 10:12:22 +10303502 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04003503 return 1;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003504
Steven Rostedt8d707e82009-06-16 21:22:48 -04003505 dolock = rb_ok_to_lock();
Steven Rostedt554f7862009-03-11 22:00:13 -04003506
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003507 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt8d707e82009-06-16 21:22:48 -04003508 local_irq_save(flags);
3509 if (dolock)
3510 spin_lock(&cpu_buffer->reader_lock);
Steven Rostedt554f7862009-03-11 22:00:13 -04003511 ret = rb_per_cpu_empty(cpu_buffer);
Steven Rostedt8d707e82009-06-16 21:22:48 -04003512 if (dolock)
3513 spin_unlock(&cpu_buffer->reader_lock);
3514 local_irq_restore(flags);
Steven Rostedt554f7862009-03-11 22:00:13 -04003515
3516 return ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003517}
Robert Richterc4f50182008-12-11 16:49:22 +01003518EXPORT_SYMBOL_GPL(ring_buffer_empty_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003519
Steven Rostedt85bac322009-09-04 14:24:40 -04003520#ifdef CONFIG_RING_BUFFER_ALLOW_SWAP
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003521/**
3522 * ring_buffer_swap_cpu - swap a CPU buffer between two ring buffers
3523 * @buffer_a: One buffer to swap with
3524 * @buffer_b: The other buffer to swap with
3525 *
3526 * This function is useful for tracers that want to take a "snapshot"
3527 * of a CPU buffer and has another back up buffer lying around.
3528 * it is expected that the tracer handles the cpu buffer not being
3529 * used at the moment.
3530 */
3531int ring_buffer_swap_cpu(struct ring_buffer *buffer_a,
3532 struct ring_buffer *buffer_b, int cpu)
3533{
3534 struct ring_buffer_per_cpu *cpu_buffer_a;
3535 struct ring_buffer_per_cpu *cpu_buffer_b;
Steven Rostedt554f7862009-03-11 22:00:13 -04003536 int ret = -EINVAL;
3537
Rusty Russell9e01c1b2009-01-01 10:12:22 +10303538 if (!cpumask_test_cpu(cpu, buffer_a->cpumask) ||
3539 !cpumask_test_cpu(cpu, buffer_b->cpumask))
Steven Rostedt554f7862009-03-11 22:00:13 -04003540 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003541
3542 /* At least make sure the two buffers are somewhat the same */
Lai Jiangshan6d102bc2008-12-17 17:48:23 +08003543 if (buffer_a->pages != buffer_b->pages)
Steven Rostedt554f7862009-03-11 22:00:13 -04003544 goto out;
3545
3546 ret = -EAGAIN;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003547
Steven Rostedt97b17ef2009-01-21 15:24:56 -05003548 if (ring_buffer_flags != RB_BUFFERS_ON)
Steven Rostedt554f7862009-03-11 22:00:13 -04003549 goto out;
Steven Rostedt97b17ef2009-01-21 15:24:56 -05003550
3551 if (atomic_read(&buffer_a->record_disabled))
Steven Rostedt554f7862009-03-11 22:00:13 -04003552 goto out;
Steven Rostedt97b17ef2009-01-21 15:24:56 -05003553
3554 if (atomic_read(&buffer_b->record_disabled))
Steven Rostedt554f7862009-03-11 22:00:13 -04003555 goto out;
Steven Rostedt97b17ef2009-01-21 15:24:56 -05003556
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003557 cpu_buffer_a = buffer_a->buffers[cpu];
3558 cpu_buffer_b = buffer_b->buffers[cpu];
3559
Steven Rostedt97b17ef2009-01-21 15:24:56 -05003560 if (atomic_read(&cpu_buffer_a->record_disabled))
Steven Rostedt554f7862009-03-11 22:00:13 -04003561 goto out;
Steven Rostedt97b17ef2009-01-21 15:24:56 -05003562
3563 if (atomic_read(&cpu_buffer_b->record_disabled))
Steven Rostedt554f7862009-03-11 22:00:13 -04003564 goto out;
Steven Rostedt97b17ef2009-01-21 15:24:56 -05003565
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003566 /*
3567 * We can't do a synchronize_sched here because this
3568 * function can be called in atomic context.
3569 * Normally this will be called from the same CPU as cpu.
3570 * If not it's up to the caller to protect this.
3571 */
3572 atomic_inc(&cpu_buffer_a->record_disabled);
3573 atomic_inc(&cpu_buffer_b->record_disabled);
3574
Steven Rostedt98277992009-09-02 10:56:15 -04003575 ret = -EBUSY;
3576 if (local_read(&cpu_buffer_a->committing))
3577 goto out_dec;
3578 if (local_read(&cpu_buffer_b->committing))
3579 goto out_dec;
3580
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003581 buffer_a->buffers[cpu] = cpu_buffer_b;
3582 buffer_b->buffers[cpu] = cpu_buffer_a;
3583
3584 cpu_buffer_b->buffer = buffer_a;
3585 cpu_buffer_a->buffer = buffer_b;
3586
Steven Rostedt98277992009-09-02 10:56:15 -04003587 ret = 0;
3588
3589out_dec:
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003590 atomic_dec(&cpu_buffer_a->record_disabled);
3591 atomic_dec(&cpu_buffer_b->record_disabled);
Steven Rostedt554f7862009-03-11 22:00:13 -04003592out:
Steven Rostedt554f7862009-03-11 22:00:13 -04003593 return ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003594}
Robert Richterc4f50182008-12-11 16:49:22 +01003595EXPORT_SYMBOL_GPL(ring_buffer_swap_cpu);
Steven Rostedt85bac322009-09-04 14:24:40 -04003596#endif /* CONFIG_RING_BUFFER_ALLOW_SWAP */
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003597
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003598/**
3599 * ring_buffer_alloc_read_page - allocate a page to read from buffer
3600 * @buffer: the buffer to allocate for.
3601 *
3602 * This function is used in conjunction with ring_buffer_read_page.
3603 * When reading a full page from the ring buffer, these functions
3604 * can be used to speed up the process. The calling function should
3605 * allocate a few pages first with this function. Then when it
3606 * needs to get pages from the ring buffer, it passes the result
3607 * of this function into ring_buffer_read_page, which will swap
3608 * the page that was allocated, with the read page of the buffer.
3609 *
3610 * Returns:
3611 * The page allocated, or NULL on error.
3612 */
3613void *ring_buffer_alloc_read_page(struct ring_buffer *buffer)
3614{
Steven Rostedt044fa782008-12-02 23:50:03 -05003615 struct buffer_data_page *bpage;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003616 unsigned long addr;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003617
3618 addr = __get_free_page(GFP_KERNEL);
3619 if (!addr)
3620 return NULL;
3621
Steven Rostedt044fa782008-12-02 23:50:03 -05003622 bpage = (void *)addr;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003623
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003624 rb_init_page(bpage);
3625
Steven Rostedt044fa782008-12-02 23:50:03 -05003626 return bpage;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003627}
Steven Rostedtd6ce96d2009-05-05 01:15:24 -04003628EXPORT_SYMBOL_GPL(ring_buffer_alloc_read_page);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003629
3630/**
3631 * ring_buffer_free_read_page - free an allocated read page
3632 * @buffer: the buffer the page was allocate for
3633 * @data: the page to free
3634 *
3635 * Free a page allocated from ring_buffer_alloc_read_page.
3636 */
3637void ring_buffer_free_read_page(struct ring_buffer *buffer, void *data)
3638{
3639 free_page((unsigned long)data);
3640}
Steven Rostedtd6ce96d2009-05-05 01:15:24 -04003641EXPORT_SYMBOL_GPL(ring_buffer_free_read_page);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003642
3643/**
3644 * ring_buffer_read_page - extract a page from the ring buffer
3645 * @buffer: buffer to extract from
3646 * @data_page: the page to use allocated from ring_buffer_alloc_read_page
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003647 * @len: amount to extract
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003648 * @cpu: the cpu of the buffer to extract
3649 * @full: should the extraction only happen when the page is full.
3650 *
3651 * This function will pull out a page from the ring buffer and consume it.
3652 * @data_page must be the address of the variable that was returned
3653 * from ring_buffer_alloc_read_page. This is because the page might be used
3654 * to swap with a page in the ring buffer.
3655 *
3656 * for example:
Lai Jiangshanb85fa012009-02-09 14:21:14 +08003657 * rpage = ring_buffer_alloc_read_page(buffer);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003658 * if (!rpage)
3659 * return error;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003660 * ret = ring_buffer_read_page(buffer, &rpage, len, cpu, 0);
Lai Jiangshan667d2412009-02-09 14:21:17 +08003661 * if (ret >= 0)
3662 * process_page(rpage, ret);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003663 *
3664 * When @full is set, the function will not return true unless
3665 * the writer is off the reader page.
3666 *
3667 * Note: it is up to the calling functions to handle sleeps and wakeups.
3668 * The ring buffer can be used anywhere in the kernel and can not
3669 * blindly call wake_up. The layer that uses the ring buffer must be
3670 * responsible for that.
3671 *
3672 * Returns:
Lai Jiangshan667d2412009-02-09 14:21:17 +08003673 * >=0 if data has been transferred, returns the offset of consumed data.
3674 * <0 if no data has been transferred.
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003675 */
3676int ring_buffer_read_page(struct ring_buffer *buffer,
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003677 void **data_page, size_t len, int cpu, int full)
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003678{
3679 struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
3680 struct ring_buffer_event *event;
Steven Rostedt044fa782008-12-02 23:50:03 -05003681 struct buffer_data_page *bpage;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003682 struct buffer_page *reader;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003683 unsigned long flags;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003684 unsigned int commit;
Lai Jiangshan667d2412009-02-09 14:21:17 +08003685 unsigned int read;
Steven Rostedt4f3640f2009-03-03 23:52:42 -05003686 u64 save_timestamp;
Lai Jiangshan667d2412009-02-09 14:21:17 +08003687 int ret = -1;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003688
Steven Rostedt554f7862009-03-11 22:00:13 -04003689 if (!cpumask_test_cpu(cpu, buffer->cpumask))
3690 goto out;
3691
Steven Rostedt474d32b2009-03-03 19:51:40 -05003692 /*
3693 * If len is not big enough to hold the page header, then
3694 * we can not copy anything.
3695 */
3696 if (len <= BUF_PAGE_HDR_SIZE)
Steven Rostedt554f7862009-03-11 22:00:13 -04003697 goto out;
Steven Rostedt474d32b2009-03-03 19:51:40 -05003698
3699 len -= BUF_PAGE_HDR_SIZE;
3700
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003701 if (!data_page)
Steven Rostedt554f7862009-03-11 22:00:13 -04003702 goto out;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003703
Steven Rostedt044fa782008-12-02 23:50:03 -05003704 bpage = *data_page;
3705 if (!bpage)
Steven Rostedt554f7862009-03-11 22:00:13 -04003706 goto out;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003707
3708 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
3709
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003710 reader = rb_get_reader_page(cpu_buffer);
3711 if (!reader)
Steven Rostedt554f7862009-03-11 22:00:13 -04003712 goto out_unlock;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003713
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003714 event = rb_reader_event(cpu_buffer);
Lai Jiangshan667d2412009-02-09 14:21:17 +08003715
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003716 read = reader->read;
3717 commit = rb_page_commit(reader);
3718
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003719 /*
Steven Rostedt474d32b2009-03-03 19:51:40 -05003720 * If this page has been partially read or
3721 * if len is not big enough to read the rest of the page or
3722 * a writer is still on the page, then
3723 * we must copy the data from the page to the buffer.
3724 * Otherwise, we can simply swap the page with the one passed in.
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003725 */
Steven Rostedt474d32b2009-03-03 19:51:40 -05003726 if (read || (len < (commit - read)) ||
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003727 cpu_buffer->reader_page == cpu_buffer->commit_page) {
Lai Jiangshan667d2412009-02-09 14:21:17 +08003728 struct buffer_data_page *rpage = cpu_buffer->reader_page->page;
Steven Rostedt474d32b2009-03-03 19:51:40 -05003729 unsigned int rpos = read;
3730 unsigned int pos = 0;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003731 unsigned int size;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003732
3733 if (full)
Steven Rostedt554f7862009-03-11 22:00:13 -04003734 goto out_unlock;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003735
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003736 if (len > (commit - read))
3737 len = (commit - read);
3738
3739 size = rb_event_length(event);
3740
3741 if (len < size)
Steven Rostedt554f7862009-03-11 22:00:13 -04003742 goto out_unlock;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003743
Steven Rostedt4f3640f2009-03-03 23:52:42 -05003744 /* save the current timestamp, since the user will need it */
3745 save_timestamp = cpu_buffer->read_stamp;
3746
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003747 /* Need to copy one event at a time */
3748 do {
Steven Rostedt474d32b2009-03-03 19:51:40 -05003749 memcpy(bpage->data + pos, rpage->data + rpos, size);
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003750
3751 len -= size;
3752
3753 rb_advance_reader(cpu_buffer);
Steven Rostedt474d32b2009-03-03 19:51:40 -05003754 rpos = reader->read;
3755 pos += size;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003756
3757 event = rb_reader_event(cpu_buffer);
3758 size = rb_event_length(event);
3759 } while (len > size);
Lai Jiangshan667d2412009-02-09 14:21:17 +08003760
3761 /* update bpage */
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003762 local_set(&bpage->commit, pos);
Steven Rostedt4f3640f2009-03-03 23:52:42 -05003763 bpage->time_stamp = save_timestamp;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003764
Steven Rostedt474d32b2009-03-03 19:51:40 -05003765 /* we copied everything to the beginning */
3766 read = 0;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003767 } else {
Steven Rostedtafbab762009-05-01 19:40:05 -04003768 /* update the entry counter */
Steven Rostedt77ae3652009-03-27 11:00:29 -04003769 cpu_buffer->read += rb_page_entries(reader);
Steven Rostedtafbab762009-05-01 19:40:05 -04003770
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003771 /* swap the pages */
Steven Rostedt044fa782008-12-02 23:50:03 -05003772 rb_init_page(bpage);
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003773 bpage = reader->page;
3774 reader->page = *data_page;
3775 local_set(&reader->write, 0);
Steven Rostedt778c55d2009-05-01 18:44:45 -04003776 local_set(&reader->entries, 0);
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003777 reader->read = 0;
Steven Rostedt044fa782008-12-02 23:50:03 -05003778 *data_page = bpage;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003779 }
Lai Jiangshan667d2412009-02-09 14:21:17 +08003780 ret = read;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003781
Steven Rostedt554f7862009-03-11 22:00:13 -04003782 out_unlock:
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003783 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
3784
Steven Rostedt554f7862009-03-11 22:00:13 -04003785 out:
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003786 return ret;
3787}
Steven Rostedtd6ce96d2009-05-05 01:15:24 -04003788EXPORT_SYMBOL_GPL(ring_buffer_read_page);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003789
Paul Mundt1155de42009-06-25 14:30:12 +09003790#ifdef CONFIG_TRACING
Steven Rostedta3583242008-11-11 15:01:42 -05003791static ssize_t
3792rb_simple_read(struct file *filp, char __user *ubuf,
3793 size_t cnt, loff_t *ppos)
3794{
Hannes Eder5e398412009-02-10 19:44:34 +01003795 unsigned long *p = filp->private_data;
Steven Rostedta3583242008-11-11 15:01:42 -05003796 char buf[64];
3797 int r;
3798
Steven Rostedt033601a2008-11-21 12:41:55 -05003799 if (test_bit(RB_BUFFERS_DISABLED_BIT, p))
3800 r = sprintf(buf, "permanently disabled\n");
3801 else
3802 r = sprintf(buf, "%d\n", test_bit(RB_BUFFERS_ON_BIT, p));
Steven Rostedta3583242008-11-11 15:01:42 -05003803
3804 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
3805}
3806
3807static ssize_t
3808rb_simple_write(struct file *filp, const char __user *ubuf,
3809 size_t cnt, loff_t *ppos)
3810{
Hannes Eder5e398412009-02-10 19:44:34 +01003811 unsigned long *p = filp->private_data;
Steven Rostedta3583242008-11-11 15:01:42 -05003812 char buf[64];
Hannes Eder5e398412009-02-10 19:44:34 +01003813 unsigned long val;
Steven Rostedta3583242008-11-11 15:01:42 -05003814 int ret;
3815
3816 if (cnt >= sizeof(buf))
3817 return -EINVAL;
3818
3819 if (copy_from_user(&buf, ubuf, cnt))
3820 return -EFAULT;
3821
3822 buf[cnt] = 0;
3823
3824 ret = strict_strtoul(buf, 10, &val);
3825 if (ret < 0)
3826 return ret;
3827
Steven Rostedt033601a2008-11-21 12:41:55 -05003828 if (val)
3829 set_bit(RB_BUFFERS_ON_BIT, p);
3830 else
3831 clear_bit(RB_BUFFERS_ON_BIT, p);
Steven Rostedta3583242008-11-11 15:01:42 -05003832
3833 (*ppos)++;
3834
3835 return cnt;
3836}
3837
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003838static const struct file_operations rb_simple_fops = {
Steven Rostedta3583242008-11-11 15:01:42 -05003839 .open = tracing_open_generic,
3840 .read = rb_simple_read,
3841 .write = rb_simple_write,
3842};
3843
3844
3845static __init int rb_init_debugfs(void)
3846{
3847 struct dentry *d_tracer;
Steven Rostedta3583242008-11-11 15:01:42 -05003848
3849 d_tracer = tracing_init_dentry();
3850
Frederic Weisbecker5452af62009-03-27 00:25:38 +01003851 trace_create_file("tracing_on", 0644, d_tracer,
3852 &ring_buffer_flags, &rb_simple_fops);
Steven Rostedta3583242008-11-11 15:01:42 -05003853
3854 return 0;
3855}
3856
3857fs_initcall(rb_init_debugfs);
Paul Mundt1155de42009-06-25 14:30:12 +09003858#endif
Steven Rostedt554f7862009-03-11 22:00:13 -04003859
Steven Rostedt59222ef2009-03-12 11:46:03 -04003860#ifdef CONFIG_HOTPLUG_CPU
Frederic Weisbecker09c9e842009-03-21 04:33:36 +01003861static int rb_cpu_notify(struct notifier_block *self,
3862 unsigned long action, void *hcpu)
Steven Rostedt554f7862009-03-11 22:00:13 -04003863{
3864 struct ring_buffer *buffer =
3865 container_of(self, struct ring_buffer, cpu_notify);
3866 long cpu = (long)hcpu;
3867
3868 switch (action) {
3869 case CPU_UP_PREPARE:
3870 case CPU_UP_PREPARE_FROZEN:
Rusty Russell3f237a72009-06-12 21:15:30 +09303871 if (cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt554f7862009-03-11 22:00:13 -04003872 return NOTIFY_OK;
3873
3874 buffer->buffers[cpu] =
3875 rb_allocate_cpu_buffer(buffer, cpu);
3876 if (!buffer->buffers[cpu]) {
3877 WARN(1, "failed to allocate ring buffer on CPU %ld\n",
3878 cpu);
3879 return NOTIFY_OK;
3880 }
3881 smp_wmb();
Rusty Russell3f237a72009-06-12 21:15:30 +09303882 cpumask_set_cpu(cpu, buffer->cpumask);
Steven Rostedt554f7862009-03-11 22:00:13 -04003883 break;
3884 case CPU_DOWN_PREPARE:
3885 case CPU_DOWN_PREPARE_FROZEN:
3886 /*
3887 * Do nothing.
3888 * If we were to free the buffer, then the user would
3889 * lose any trace that was in the buffer.
3890 */
3891 break;
3892 default:
3893 break;
3894 }
3895 return NOTIFY_OK;
3896}
3897#endif