blob: eb6c8988c31a835e28ef4b5de9c25296bab3bb0f [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
210/* define RINGBUF_TYPE_DATA for 'case RINGBUF_TYPE_DATA:' */
211#define RINGBUF_TYPE_DATA 0 ... RINGBUF_TYPE_DATA_TYPE_LEN_MAX
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400212
213enum {
214 RB_LEN_TIME_EXTEND = 8,
215 RB_LEN_TIME_STAMP = 16,
216};
217
Tom Zanussi2d622712009-03-22 03:30:49 -0500218static inline int rb_null_event(struct ring_buffer_event *event)
219{
Steven Rostedta1863c22009-09-03 10:23:58 -0400220 return event->type_len == RINGBUF_TYPE_PADDING && !event->time_delta;
Tom Zanussi2d622712009-03-22 03:30:49 -0500221}
222
223static void rb_event_set_padding(struct ring_buffer_event *event)
224{
Steven Rostedta1863c22009-09-03 10:23:58 -0400225 /* padding has a NULL time_delta */
Lai Jiangshan334d4162009-04-24 11:27:05 +0800226 event->type_len = RINGBUF_TYPE_PADDING;
Tom Zanussi2d622712009-03-22 03:30:49 -0500227 event->time_delta = 0;
228}
229
Tom Zanussi2d622712009-03-22 03:30:49 -0500230static unsigned
231rb_event_data_length(struct ring_buffer_event *event)
232{
233 unsigned length;
234
Lai Jiangshan334d4162009-04-24 11:27:05 +0800235 if (event->type_len)
236 length = event->type_len * RB_ALIGNMENT;
Tom Zanussi2d622712009-03-22 03:30:49 -0500237 else
238 length = event->array[0];
239 return length + RB_EVNT_HDR_SIZE;
240}
241
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400242/* inline for ring buffer fast paths */
Andrew Morton34a148b2009-01-09 12:27:09 -0800243static unsigned
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400244rb_event_length(struct ring_buffer_event *event)
245{
Lai Jiangshan334d4162009-04-24 11:27:05 +0800246 switch (event->type_len) {
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400247 case RINGBUF_TYPE_PADDING:
Tom Zanussi2d622712009-03-22 03:30:49 -0500248 if (rb_null_event(event))
249 /* undefined */
250 return -1;
Lai Jiangshan334d4162009-04-24 11:27:05 +0800251 return event->array[0] + RB_EVNT_HDR_SIZE;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400252
253 case RINGBUF_TYPE_TIME_EXTEND:
254 return RB_LEN_TIME_EXTEND;
255
256 case RINGBUF_TYPE_TIME_STAMP:
257 return RB_LEN_TIME_STAMP;
258
259 case RINGBUF_TYPE_DATA:
Tom Zanussi2d622712009-03-22 03:30:49 -0500260 return rb_event_data_length(event);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400261 default:
262 BUG();
263 }
264 /* not hit */
265 return 0;
266}
267
268/**
269 * ring_buffer_event_length - return the length of the event
270 * @event: the event to get the length of
271 */
272unsigned ring_buffer_event_length(struct ring_buffer_event *event)
273{
Robert Richter465634a2009-01-07 15:32:11 +0100274 unsigned length = rb_event_length(event);
Lai Jiangshan334d4162009-04-24 11:27:05 +0800275 if (event->type_len > RINGBUF_TYPE_DATA_TYPE_LEN_MAX)
Robert Richter465634a2009-01-07 15:32:11 +0100276 return length;
277 length -= RB_EVNT_HDR_SIZE;
278 if (length > RB_MAX_SMALL_DATA + sizeof(event->array[0]))
279 length -= sizeof(event->array[0]);
280 return length;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400281}
Robert Richterc4f50182008-12-11 16:49:22 +0100282EXPORT_SYMBOL_GPL(ring_buffer_event_length);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400283
284/* inline for ring buffer fast paths */
Andrew Morton34a148b2009-01-09 12:27:09 -0800285static void *
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400286rb_event_data(struct ring_buffer_event *event)
287{
Lai Jiangshan334d4162009-04-24 11:27:05 +0800288 BUG_ON(event->type_len > RINGBUF_TYPE_DATA_TYPE_LEN_MAX);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400289 /* If length is in len field, then array[0] has the data */
Lai Jiangshan334d4162009-04-24 11:27:05 +0800290 if (event->type_len)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400291 return (void *)&event->array[0];
292 /* Otherwise length is in array[0] and array[1] has the data */
293 return (void *)&event->array[1];
294}
295
296/**
297 * ring_buffer_event_data - return the data of the event
298 * @event: the event to get the data from
299 */
300void *ring_buffer_event_data(struct ring_buffer_event *event)
301{
302 return rb_event_data(event);
303}
Robert Richterc4f50182008-12-11 16:49:22 +0100304EXPORT_SYMBOL_GPL(ring_buffer_event_data);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400305
306#define for_each_buffer_cpu(buffer, cpu) \
Rusty Russell9e01c1b2009-01-01 10:12:22 +1030307 for_each_cpu(cpu, buffer->cpumask)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400308
309#define TS_SHIFT 27
310#define TS_MASK ((1ULL << TS_SHIFT) - 1)
311#define TS_DELTA_TEST (~TS_MASK)
312
Steven Rostedtabc9b562008-12-02 15:34:06 -0500313struct buffer_data_page {
Steven Rostedte4c2ce82008-10-01 11:14:54 -0400314 u64 time_stamp; /* page time stamp */
Wenji Huangc3706f02009-02-10 01:03:18 -0500315 local_t commit; /* write committed index */
Steven Rostedtabc9b562008-12-02 15:34:06 -0500316 unsigned char data[]; /* data of buffer page */
317};
318
Steven Rostedt77ae3652009-03-27 11:00:29 -0400319/*
320 * Note, the buffer_page list must be first. The buffer pages
321 * are allocated in cache lines, which means that each buffer
322 * page will be at the beginning of a cache line, and thus
323 * the least significant bits will be zero. We use this to
324 * add flags in the list struct pointers, to make the ring buffer
325 * lockless.
326 */
Steven Rostedtabc9b562008-12-02 15:34:06 -0500327struct buffer_page {
Steven Rostedt778c55d2009-05-01 18:44:45 -0400328 struct list_head list; /* list of buffer pages */
Steven Rostedtabc9b562008-12-02 15:34:06 -0500329 local_t write; /* index for next write */
Steven Rostedt6f807ac2008-10-04 02:00:58 -0400330 unsigned read; /* index for next read */
Steven Rostedt778c55d2009-05-01 18:44:45 -0400331 local_t entries; /* entries on this page */
Steven Rostedtabc9b562008-12-02 15:34:06 -0500332 struct buffer_data_page *page; /* Actual data page */
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400333};
334
Steven Rostedt77ae3652009-03-27 11:00:29 -0400335/*
336 * The buffer page counters, write and entries, must be reset
337 * atomically when crossing page boundaries. To synchronize this
338 * update, two counters are inserted into the number. One is
339 * the actual counter for the write position or count on the page.
340 *
341 * The other is a counter of updaters. Before an update happens
342 * the update partition of the counter is incremented. This will
343 * allow the updater to update the counter atomically.
344 *
345 * The counter is 20 bits, and the state data is 12.
346 */
347#define RB_WRITE_MASK 0xfffff
348#define RB_WRITE_INTCNT (1 << 20)
349
Steven Rostedt044fa782008-12-02 23:50:03 -0500350static void rb_init_page(struct buffer_data_page *bpage)
Steven Rostedtabc9b562008-12-02 15:34:06 -0500351{
Steven Rostedt044fa782008-12-02 23:50:03 -0500352 local_set(&bpage->commit, 0);
Steven Rostedtabc9b562008-12-02 15:34:06 -0500353}
354
Steven Rostedt474d32b2009-03-03 19:51:40 -0500355/**
356 * ring_buffer_page_len - the size of data on the page.
357 * @page: The page to read
358 *
359 * Returns the amount of data on the page, including buffer page header.
360 */
Steven Rostedtef7a4a12009-03-03 00:27:49 -0500361size_t ring_buffer_page_len(void *page)
362{
Steven Rostedt474d32b2009-03-03 19:51:40 -0500363 return local_read(&((struct buffer_data_page *)page)->commit)
364 + BUF_PAGE_HDR_SIZE;
Steven Rostedtef7a4a12009-03-03 00:27:49 -0500365}
366
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400367/*
Steven Rostedted568292008-09-29 23:02:40 -0400368 * Also stolen from mm/slob.c. Thanks to Mathieu Desnoyers for pointing
369 * this issue out.
370 */
Andrew Morton34a148b2009-01-09 12:27:09 -0800371static void free_buffer_page(struct buffer_page *bpage)
Steven Rostedted568292008-09-29 23:02:40 -0400372{
Andrew Morton34a148b2009-01-09 12:27:09 -0800373 free_page((unsigned long)bpage->page);
Steven Rostedte4c2ce82008-10-01 11:14:54 -0400374 kfree(bpage);
Steven Rostedted568292008-09-29 23:02:40 -0400375}
376
377/*
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400378 * We need to fit the time_stamp delta into 27 bits.
379 */
380static inline int test_time_stamp(u64 delta)
381{
382 if (delta & TS_DELTA_TEST)
383 return 1;
384 return 0;
385}
386
Steven Rostedt474d32b2009-03-03 19:51:40 -0500387#define BUF_PAGE_SIZE (PAGE_SIZE - BUF_PAGE_HDR_SIZE)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400388
Steven Rostedtbe957c42009-05-11 14:42:53 -0400389/* Max payload is BUF_PAGE_SIZE - header (8bytes) */
390#define BUF_MAX_DATA_SIZE (BUF_PAGE_SIZE - (sizeof(u32) * 2))
391
Steven Rostedtea05b572009-06-03 09:30:10 -0400392/* Max number of timestamps that can fit on a page */
393#define RB_TIMESTAMPS_PER_PAGE (BUF_PAGE_SIZE / RB_LEN_TIME_STAMP)
394
Steven Rostedtd1b182a2009-04-15 16:53:47 -0400395int ring_buffer_print_page_header(struct trace_seq *s)
396{
397 struct buffer_data_page field;
398 int ret;
399
400 ret = trace_seq_printf(s, "\tfield: u64 timestamp;\t"
Tom Zanussi26a50742009-10-06 01:09:50 -0500401 "offset:0;\tsize:%u;\tsigned:%u;\n",
402 (unsigned int)sizeof(field.time_stamp),
403 (unsigned int)is_signed_type(u64));
Steven Rostedtd1b182a2009-04-15 16:53:47 -0400404
405 ret = trace_seq_printf(s, "\tfield: local_t commit;\t"
Tom Zanussi26a50742009-10-06 01:09:50 -0500406 "offset:%u;\tsize:%u;\tsigned:%u;\n",
Steven Rostedtd1b182a2009-04-15 16:53:47 -0400407 (unsigned int)offsetof(typeof(field), commit),
Tom Zanussi26a50742009-10-06 01:09:50 -0500408 (unsigned int)sizeof(field.commit),
409 (unsigned int)is_signed_type(long));
Steven Rostedtd1b182a2009-04-15 16:53:47 -0400410
411 ret = trace_seq_printf(s, "\tfield: char data;\t"
Tom Zanussi26a50742009-10-06 01:09:50 -0500412 "offset:%u;\tsize:%u;\tsigned:%u;\n",
Steven Rostedtd1b182a2009-04-15 16:53:47 -0400413 (unsigned int)offsetof(typeof(field), data),
Tom Zanussi26a50742009-10-06 01:09:50 -0500414 (unsigned int)BUF_PAGE_SIZE,
415 (unsigned int)is_signed_type(char));
Steven Rostedtd1b182a2009-04-15 16:53:47 -0400416
417 return ret;
418}
419
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400420/*
421 * head_page == tail_page && head == tail then buffer is empty.
422 */
423struct ring_buffer_per_cpu {
424 int cpu;
425 struct ring_buffer *buffer;
Steven Rostedt77ae3652009-03-27 11:00:29 -0400426 spinlock_t reader_lock; /* serialize readers */
Thomas Gleixner445c8952009-12-02 19:49:50 +0100427 arch_spinlock_t lock;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400428 struct lock_class_key lock_key;
Steven Rostedt3adc54f2009-03-30 15:32:01 -0400429 struct list_head *pages;
Steven Rostedt6f807ac2008-10-04 02:00:58 -0400430 struct buffer_page *head_page; /* read from head */
431 struct buffer_page *tail_page; /* write to tail */
Wenji Huangc3706f02009-02-10 01:03:18 -0500432 struct buffer_page *commit_page; /* committed pages */
Steven Rostedtd7690412008-10-01 00:29:53 -0400433 struct buffer_page *reader_page;
Steven Rostedt77ae3652009-03-27 11:00:29 -0400434 local_t commit_overrun;
435 local_t overrun;
Steven Rostedte4906ef2009-04-30 20:49:44 -0400436 local_t entries;
Steven Rostedtfa743952009-06-16 12:37:57 -0400437 local_t committing;
438 local_t commits;
Steven Rostedt77ae3652009-03-27 11:00:29 -0400439 unsigned long read;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400440 u64 write_stamp;
441 u64 read_stamp;
442 atomic_t record_disabled;
443};
444
445struct ring_buffer {
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400446 unsigned pages;
447 unsigned flags;
448 int cpus;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400449 atomic_t record_disabled;
Arnaldo Carvalho de Melo00f62f62009-02-09 17:04:06 -0200450 cpumask_var_t cpumask;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400451
Peter Zijlstra1f8a6a12009-06-08 18:18:39 +0200452 struct lock_class_key *reader_lock_key;
453
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400454 struct mutex mutex;
455
456 struct ring_buffer_per_cpu **buffers;
Steven Rostedt554f7862009-03-11 22:00:13 -0400457
Steven Rostedt59222ef2009-03-12 11:46:03 -0400458#ifdef CONFIG_HOTPLUG_CPU
Steven Rostedt554f7862009-03-11 22:00:13 -0400459 struct notifier_block cpu_notify;
460#endif
Steven Rostedt37886f62009-03-17 17:22:06 -0400461 u64 (*clock)(void);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400462};
463
464struct ring_buffer_iter {
465 struct ring_buffer_per_cpu *cpu_buffer;
466 unsigned long head;
467 struct buffer_page *head_page;
468 u64 read_stamp;
469};
470
Steven Rostedtf536aaf2008-11-10 23:07:30 -0500471/* buffer may be either ring_buffer or ring_buffer_per_cpu */
Steven Rostedt077c5402009-09-03 19:53:46 -0400472#define RB_WARN_ON(b, cond) \
473 ({ \
474 int _____ret = unlikely(cond); \
475 if (_____ret) { \
476 if (__same_type(*(b), struct ring_buffer_per_cpu)) { \
477 struct ring_buffer_per_cpu *__b = \
478 (void *)b; \
479 atomic_inc(&__b->buffer->record_disabled); \
480 } else \
481 atomic_inc(&b->record_disabled); \
482 WARN_ON(1); \
483 } \
484 _____ret; \
Steven Rostedt3e89c7b2008-11-11 15:28:41 -0500485 })
Steven Rostedtf536aaf2008-11-10 23:07:30 -0500486
Steven Rostedt37886f62009-03-17 17:22:06 -0400487/* Up this if you want to test the TIME_EXTENTS and normalization */
488#define DEBUG_SHIFT 0
489
Jiri Olsa6d3f1e12009-10-23 19:36:19 -0400490static inline u64 rb_time_stamp(struct ring_buffer *buffer)
Steven Rostedt88eb0122009-05-11 16:28:23 -0400491{
492 /* shift to debug/test normalization and TIME_EXTENTS */
493 return buffer->clock() << DEBUG_SHIFT;
494}
495
Steven Rostedt37886f62009-03-17 17:22:06 -0400496u64 ring_buffer_time_stamp(struct ring_buffer *buffer, int cpu)
497{
498 u64 time;
499
500 preempt_disable_notrace();
Jiri Olsa6d3f1e12009-10-23 19:36:19 -0400501 time = rb_time_stamp(buffer);
Steven Rostedt37886f62009-03-17 17:22:06 -0400502 preempt_enable_no_resched_notrace();
503
504 return time;
505}
506EXPORT_SYMBOL_GPL(ring_buffer_time_stamp);
507
508void ring_buffer_normalize_time_stamp(struct ring_buffer *buffer,
509 int cpu, u64 *ts)
510{
511 /* Just stupid testing the normalize function and deltas */
512 *ts >>= DEBUG_SHIFT;
513}
514EXPORT_SYMBOL_GPL(ring_buffer_normalize_time_stamp);
515
Steven Rostedt77ae3652009-03-27 11:00:29 -0400516/*
517 * Making the ring buffer lockless makes things tricky.
518 * Although writes only happen on the CPU that they are on,
519 * and they only need to worry about interrupts. Reads can
520 * happen on any CPU.
521 *
522 * The reader page is always off the ring buffer, but when the
523 * reader finishes with a page, it needs to swap its page with
524 * a new one from the buffer. The reader needs to take from
525 * the head (writes go to the tail). But if a writer is in overwrite
526 * mode and wraps, it must push the head page forward.
527 *
528 * Here lies the problem.
529 *
530 * The reader must be careful to replace only the head page, and
531 * not another one. As described at the top of the file in the
532 * ASCII art, the reader sets its old page to point to the next
533 * page after head. It then sets the page after head to point to
534 * the old reader page. But if the writer moves the head page
535 * during this operation, the reader could end up with the tail.
536 *
537 * We use cmpxchg to help prevent this race. We also do something
538 * special with the page before head. We set the LSB to 1.
539 *
540 * When the writer must push the page forward, it will clear the
541 * bit that points to the head page, move the head, and then set
542 * the bit that points to the new head page.
543 *
544 * We also don't want an interrupt coming in and moving the head
545 * page on another writer. Thus we use the second LSB to catch
546 * that too. Thus:
547 *
548 * head->list->prev->next bit 1 bit 0
549 * ------- -------
550 * Normal page 0 0
551 * Points to head page 0 1
552 * New head page 1 0
553 *
554 * Note we can not trust the prev pointer of the head page, because:
555 *
556 * +----+ +-----+ +-----+
557 * | |------>| T |---X--->| N |
558 * | |<------| | | |
559 * +----+ +-----+ +-----+
560 * ^ ^ |
561 * | +-----+ | |
562 * +----------| R |----------+ |
563 * | |<-----------+
564 * +-----+
565 *
566 * Key: ---X--> HEAD flag set in pointer
567 * T Tail page
568 * R Reader page
569 * N Next page
570 *
571 * (see __rb_reserve_next() to see where this happens)
572 *
573 * What the above shows is that the reader just swapped out
574 * the reader page with a page in the buffer, but before it
575 * could make the new header point back to the new page added
576 * it was preempted by a writer. The writer moved forward onto
577 * the new page added by the reader and is about to move forward
578 * again.
579 *
580 * You can see, it is legitimate for the previous pointer of
581 * the head (or any page) not to point back to itself. But only
582 * temporarially.
583 */
584
585#define RB_PAGE_NORMAL 0UL
586#define RB_PAGE_HEAD 1UL
587#define RB_PAGE_UPDATE 2UL
588
589
590#define RB_FLAG_MASK 3UL
591
592/* PAGE_MOVED is not part of the mask */
593#define RB_PAGE_MOVED 4UL
594
595/*
596 * rb_list_head - remove any bit
597 */
598static struct list_head *rb_list_head(struct list_head *list)
599{
600 unsigned long val = (unsigned long)list;
601
602 return (struct list_head *)(val & ~RB_FLAG_MASK);
603}
604
605/*
Jiri Olsa6d3f1e12009-10-23 19:36:19 -0400606 * rb_is_head_page - test if the given page is the head page
Steven Rostedt77ae3652009-03-27 11:00:29 -0400607 *
608 * Because the reader may move the head_page pointer, we can
609 * not trust what the head page is (it may be pointing to
610 * the reader page). But if the next page is a header page,
611 * its flags will be non zero.
612 */
613static int inline
614rb_is_head_page(struct ring_buffer_per_cpu *cpu_buffer,
615 struct buffer_page *page, struct list_head *list)
616{
617 unsigned long val;
618
619 val = (unsigned long)list->next;
620
621 if ((val & ~RB_FLAG_MASK) != (unsigned long)&page->list)
622 return RB_PAGE_MOVED;
623
624 return val & RB_FLAG_MASK;
625}
626
627/*
628 * rb_is_reader_page
629 *
630 * The unique thing about the reader page, is that, if the
631 * writer is ever on it, the previous pointer never points
632 * back to the reader page.
633 */
634static int rb_is_reader_page(struct buffer_page *page)
635{
636 struct list_head *list = page->list.prev;
637
638 return rb_list_head(list->next) != &page->list;
639}
640
641/*
642 * rb_set_list_to_head - set a list_head to be pointing to head.
643 */
644static void rb_set_list_to_head(struct ring_buffer_per_cpu *cpu_buffer,
645 struct list_head *list)
646{
647 unsigned long *ptr;
648
649 ptr = (unsigned long *)&list->next;
650 *ptr |= RB_PAGE_HEAD;
651 *ptr &= ~RB_PAGE_UPDATE;
652}
653
654/*
655 * rb_head_page_activate - sets up head page
656 */
657static void rb_head_page_activate(struct ring_buffer_per_cpu *cpu_buffer)
658{
659 struct buffer_page *head;
660
661 head = cpu_buffer->head_page;
662 if (!head)
663 return;
664
665 /*
666 * Set the previous list pointer to have the HEAD flag.
667 */
668 rb_set_list_to_head(cpu_buffer, head->list.prev);
669}
670
671static void rb_list_head_clear(struct list_head *list)
672{
673 unsigned long *ptr = (unsigned long *)&list->next;
674
675 *ptr &= ~RB_FLAG_MASK;
676}
677
678/*
679 * rb_head_page_dactivate - clears head page ptr (for free list)
680 */
681static void
682rb_head_page_deactivate(struct ring_buffer_per_cpu *cpu_buffer)
683{
684 struct list_head *hd;
685
686 /* Go through the whole list and clear any pointers found. */
687 rb_list_head_clear(cpu_buffer->pages);
688
689 list_for_each(hd, cpu_buffer->pages)
690 rb_list_head_clear(hd);
691}
692
693static int rb_head_page_set(struct ring_buffer_per_cpu *cpu_buffer,
694 struct buffer_page *head,
695 struct buffer_page *prev,
696 int old_flag, int new_flag)
697{
698 struct list_head *list;
699 unsigned long val = (unsigned long)&head->list;
700 unsigned long ret;
701
702 list = &prev->list;
703
704 val &= ~RB_FLAG_MASK;
705
Steven Rostedt08a40812009-09-14 09:31:35 -0400706 ret = cmpxchg((unsigned long *)&list->next,
707 val | old_flag, val | new_flag);
Steven Rostedt77ae3652009-03-27 11:00:29 -0400708
709 /* check if the reader took the page */
710 if ((ret & ~RB_FLAG_MASK) != val)
711 return RB_PAGE_MOVED;
712
713 return ret & RB_FLAG_MASK;
714}
715
716static int rb_head_page_set_update(struct ring_buffer_per_cpu *cpu_buffer,
717 struct buffer_page *head,
718 struct buffer_page *prev,
719 int old_flag)
720{
721 return rb_head_page_set(cpu_buffer, head, prev,
722 old_flag, RB_PAGE_UPDATE);
723}
724
725static int rb_head_page_set_head(struct ring_buffer_per_cpu *cpu_buffer,
726 struct buffer_page *head,
727 struct buffer_page *prev,
728 int old_flag)
729{
730 return rb_head_page_set(cpu_buffer, head, prev,
731 old_flag, RB_PAGE_HEAD);
732}
733
734static int rb_head_page_set_normal(struct ring_buffer_per_cpu *cpu_buffer,
735 struct buffer_page *head,
736 struct buffer_page *prev,
737 int old_flag)
738{
739 return rb_head_page_set(cpu_buffer, head, prev,
740 old_flag, RB_PAGE_NORMAL);
741}
742
743static inline void rb_inc_page(struct ring_buffer_per_cpu *cpu_buffer,
744 struct buffer_page **bpage)
745{
746 struct list_head *p = rb_list_head((*bpage)->list.next);
747
748 *bpage = list_entry(p, struct buffer_page, list);
749}
750
751static struct buffer_page *
752rb_set_head_page(struct ring_buffer_per_cpu *cpu_buffer)
753{
754 struct buffer_page *head;
755 struct buffer_page *page;
756 struct list_head *list;
757 int i;
758
759 if (RB_WARN_ON(cpu_buffer, !cpu_buffer->head_page))
760 return NULL;
761
762 /* sanity check */
763 list = cpu_buffer->pages;
764 if (RB_WARN_ON(cpu_buffer, rb_list_head(list->prev->next) != list))
765 return NULL;
766
767 page = head = cpu_buffer->head_page;
768 /*
769 * It is possible that the writer moves the header behind
770 * where we started, and we miss in one loop.
771 * A second loop should grab the header, but we'll do
772 * three loops just because I'm paranoid.
773 */
774 for (i = 0; i < 3; i++) {
775 do {
776 if (rb_is_head_page(cpu_buffer, page, page->list.prev)) {
777 cpu_buffer->head_page = page;
778 return page;
779 }
780 rb_inc_page(cpu_buffer, &page);
781 } while (page != head);
782 }
783
784 RB_WARN_ON(cpu_buffer, 1);
785
786 return NULL;
787}
788
789static int rb_head_page_replace(struct buffer_page *old,
790 struct buffer_page *new)
791{
792 unsigned long *ptr = (unsigned long *)&old->list.prev->next;
793 unsigned long val;
794 unsigned long ret;
795
796 val = *ptr & ~RB_FLAG_MASK;
797 val |= RB_PAGE_HEAD;
798
Steven Rostedt08a40812009-09-14 09:31:35 -0400799 ret = cmpxchg(ptr, val, (unsigned long)&new->list);
Steven Rostedt77ae3652009-03-27 11:00:29 -0400800
801 return ret == val;
802}
803
804/*
805 * rb_tail_page_update - move the tail page forward
806 *
807 * Returns 1 if moved tail page, 0 if someone else did.
808 */
809static int rb_tail_page_update(struct ring_buffer_per_cpu *cpu_buffer,
810 struct buffer_page *tail_page,
811 struct buffer_page *next_page)
812{
813 struct buffer_page *old_tail;
814 unsigned long old_entries;
815 unsigned long old_write;
816 int ret = 0;
817
818 /*
819 * The tail page now needs to be moved forward.
820 *
821 * We need to reset the tail page, but without messing
822 * with possible erasing of data brought in by interrupts
823 * that have moved the tail page and are currently on it.
824 *
825 * We add a counter to the write field to denote this.
826 */
827 old_write = local_add_return(RB_WRITE_INTCNT, &next_page->write);
828 old_entries = local_add_return(RB_WRITE_INTCNT, &next_page->entries);
829
830 /*
831 * Just make sure we have seen our old_write and synchronize
832 * with any interrupts that come in.
833 */
834 barrier();
835
836 /*
837 * If the tail page is still the same as what we think
838 * it is, then it is up to us to update the tail
839 * pointer.
840 */
841 if (tail_page == cpu_buffer->tail_page) {
842 /* Zero the write counter */
843 unsigned long val = old_write & ~RB_WRITE_MASK;
844 unsigned long eval = old_entries & ~RB_WRITE_MASK;
845
846 /*
847 * This will only succeed if an interrupt did
848 * not come in and change it. In which case, we
849 * do not want to modify it.
Lai Jiangshanda706d82009-07-15 16:27:30 +0800850 *
851 * We add (void) to let the compiler know that we do not care
852 * about the return value of these functions. We use the
853 * cmpxchg to only update if an interrupt did not already
854 * do it for us. If the cmpxchg fails, we don't care.
Steven Rostedt77ae3652009-03-27 11:00:29 -0400855 */
Lai Jiangshanda706d82009-07-15 16:27:30 +0800856 (void)local_cmpxchg(&next_page->write, old_write, val);
857 (void)local_cmpxchg(&next_page->entries, old_entries, eval);
Steven Rostedt77ae3652009-03-27 11:00:29 -0400858
859 /*
860 * No need to worry about races with clearing out the commit.
861 * it only can increment when a commit takes place. But that
862 * only happens in the outer most nested commit.
863 */
864 local_set(&next_page->page->commit, 0);
865
866 old_tail = cmpxchg(&cpu_buffer->tail_page,
867 tail_page, next_page);
868
869 if (old_tail == tail_page)
870 ret = 1;
871 }
872
873 return ret;
874}
875
876static int rb_check_bpage(struct ring_buffer_per_cpu *cpu_buffer,
877 struct buffer_page *bpage)
878{
879 unsigned long val = (unsigned long)bpage;
880
881 if (RB_WARN_ON(cpu_buffer, val & RB_FLAG_MASK))
882 return 1;
883
884 return 0;
885}
886
887/**
888 * rb_check_list - make sure a pointer to a list has the last bits zero
889 */
890static int rb_check_list(struct ring_buffer_per_cpu *cpu_buffer,
891 struct list_head *list)
892{
893 if (RB_WARN_ON(cpu_buffer, rb_list_head(list->prev) != list->prev))
894 return 1;
895 if (RB_WARN_ON(cpu_buffer, rb_list_head(list->next) != list->next))
896 return 1;
897 return 0;
898}
899
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400900/**
901 * check_pages - integrity check of buffer pages
902 * @cpu_buffer: CPU buffer with pages to test
903 *
Wenji Huangc3706f02009-02-10 01:03:18 -0500904 * As a safety measure we check to make sure the data pages have not
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400905 * been corrupted.
906 */
907static int rb_check_pages(struct ring_buffer_per_cpu *cpu_buffer)
908{
Steven Rostedt3adc54f2009-03-30 15:32:01 -0400909 struct list_head *head = cpu_buffer->pages;
Steven Rostedt044fa782008-12-02 23:50:03 -0500910 struct buffer_page *bpage, *tmp;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400911
Steven Rostedt77ae3652009-03-27 11:00:29 -0400912 rb_head_page_deactivate(cpu_buffer);
913
Steven Rostedt3e89c7b2008-11-11 15:28:41 -0500914 if (RB_WARN_ON(cpu_buffer, head->next->prev != head))
915 return -1;
916 if (RB_WARN_ON(cpu_buffer, head->prev->next != head))
917 return -1;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400918
Steven Rostedt77ae3652009-03-27 11:00:29 -0400919 if (rb_check_list(cpu_buffer, head))
920 return -1;
921
Steven Rostedt044fa782008-12-02 23:50:03 -0500922 list_for_each_entry_safe(bpage, tmp, head, list) {
Steven Rostedt3e89c7b2008-11-11 15:28:41 -0500923 if (RB_WARN_ON(cpu_buffer,
Steven Rostedt044fa782008-12-02 23:50:03 -0500924 bpage->list.next->prev != &bpage->list))
Steven Rostedt3e89c7b2008-11-11 15:28:41 -0500925 return -1;
926 if (RB_WARN_ON(cpu_buffer,
Steven Rostedt044fa782008-12-02 23:50:03 -0500927 bpage->list.prev->next != &bpage->list))
Steven Rostedt3e89c7b2008-11-11 15:28:41 -0500928 return -1;
Steven Rostedt77ae3652009-03-27 11:00:29 -0400929 if (rb_check_list(cpu_buffer, &bpage->list))
930 return -1;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400931 }
932
Steven Rostedt77ae3652009-03-27 11:00:29 -0400933 rb_head_page_activate(cpu_buffer);
934
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400935 return 0;
936}
937
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400938static int rb_allocate_pages(struct ring_buffer_per_cpu *cpu_buffer,
939 unsigned nr_pages)
940{
Steven Rostedt044fa782008-12-02 23:50:03 -0500941 struct buffer_page *bpage, *tmp;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400942 unsigned long addr;
943 LIST_HEAD(pages);
944 unsigned i;
945
Steven Rostedt3adc54f2009-03-30 15:32:01 -0400946 WARN_ON(!nr_pages);
947
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400948 for (i = 0; i < nr_pages; i++) {
Steven Rostedt044fa782008-12-02 23:50:03 -0500949 bpage = kzalloc_node(ALIGN(sizeof(*bpage), cache_line_size()),
Steven Rostedtaa1e0e32008-10-02 19:18:09 -0400950 GFP_KERNEL, cpu_to_node(cpu_buffer->cpu));
Steven Rostedt044fa782008-12-02 23:50:03 -0500951 if (!bpage)
Steven Rostedte4c2ce82008-10-01 11:14:54 -0400952 goto free_pages;
Steven Rostedt77ae3652009-03-27 11:00:29 -0400953
954 rb_check_bpage(cpu_buffer, bpage);
955
Steven Rostedt044fa782008-12-02 23:50:03 -0500956 list_add(&bpage->list, &pages);
Steven Rostedte4c2ce82008-10-01 11:14:54 -0400957
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400958 addr = __get_free_page(GFP_KERNEL);
959 if (!addr)
960 goto free_pages;
Steven Rostedt044fa782008-12-02 23:50:03 -0500961 bpage->page = (void *)addr;
962 rb_init_page(bpage->page);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400963 }
964
Steven Rostedt3adc54f2009-03-30 15:32:01 -0400965 /*
966 * The ring buffer page list is a circular list that does not
967 * start and end with a list head. All page list items point to
968 * other pages.
969 */
970 cpu_buffer->pages = pages.next;
971 list_del(&pages);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400972
973 rb_check_pages(cpu_buffer);
974
975 return 0;
976
977 free_pages:
Steven Rostedt044fa782008-12-02 23:50:03 -0500978 list_for_each_entry_safe(bpage, tmp, &pages, list) {
979 list_del_init(&bpage->list);
980 free_buffer_page(bpage);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400981 }
982 return -ENOMEM;
983}
984
985static struct ring_buffer_per_cpu *
986rb_allocate_cpu_buffer(struct ring_buffer *buffer, int cpu)
987{
988 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedt044fa782008-12-02 23:50:03 -0500989 struct buffer_page *bpage;
Steven Rostedtd7690412008-10-01 00:29:53 -0400990 unsigned long addr;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400991 int ret;
992
993 cpu_buffer = kzalloc_node(ALIGN(sizeof(*cpu_buffer), cache_line_size()),
994 GFP_KERNEL, cpu_to_node(cpu));
995 if (!cpu_buffer)
996 return NULL;
997
998 cpu_buffer->cpu = cpu;
999 cpu_buffer->buffer = buffer;
Steven Rostedtf83c9d02008-11-11 18:47:44 +01001000 spin_lock_init(&cpu_buffer->reader_lock);
Peter Zijlstra1f8a6a12009-06-08 18:18:39 +02001001 lockdep_set_class(&cpu_buffer->reader_lock, buffer->reader_lock_key);
Thomas Gleixneredc35bd2009-12-03 12:38:57 +01001002 cpu_buffer->lock = (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001003
Steven Rostedt044fa782008-12-02 23:50:03 -05001004 bpage = kzalloc_node(ALIGN(sizeof(*bpage), cache_line_size()),
Steven Rostedte4c2ce82008-10-01 11:14:54 -04001005 GFP_KERNEL, cpu_to_node(cpu));
Steven Rostedt044fa782008-12-02 23:50:03 -05001006 if (!bpage)
Steven Rostedte4c2ce82008-10-01 11:14:54 -04001007 goto fail_free_buffer;
1008
Steven Rostedt77ae3652009-03-27 11:00:29 -04001009 rb_check_bpage(cpu_buffer, bpage);
1010
Steven Rostedt044fa782008-12-02 23:50:03 -05001011 cpu_buffer->reader_page = bpage;
Steven Rostedtd7690412008-10-01 00:29:53 -04001012 addr = __get_free_page(GFP_KERNEL);
1013 if (!addr)
Steven Rostedte4c2ce82008-10-01 11:14:54 -04001014 goto fail_free_reader;
Steven Rostedt044fa782008-12-02 23:50:03 -05001015 bpage->page = (void *)addr;
1016 rb_init_page(bpage->page);
Steven Rostedte4c2ce82008-10-01 11:14:54 -04001017
Steven Rostedtd7690412008-10-01 00:29:53 -04001018 INIT_LIST_HEAD(&cpu_buffer->reader_page->list);
Steven Rostedtd7690412008-10-01 00:29:53 -04001019
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001020 ret = rb_allocate_pages(cpu_buffer, buffer->pages);
1021 if (ret < 0)
Steven Rostedtd7690412008-10-01 00:29:53 -04001022 goto fail_free_reader;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001023
1024 cpu_buffer->head_page
Steven Rostedt3adc54f2009-03-30 15:32:01 -04001025 = list_entry(cpu_buffer->pages, struct buffer_page, list);
Steven Rostedtbf41a152008-10-04 02:00:59 -04001026 cpu_buffer->tail_page = cpu_buffer->commit_page = cpu_buffer->head_page;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001027
Steven Rostedt77ae3652009-03-27 11:00:29 -04001028 rb_head_page_activate(cpu_buffer);
1029
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001030 return cpu_buffer;
1031
Steven Rostedtd7690412008-10-01 00:29:53 -04001032 fail_free_reader:
1033 free_buffer_page(cpu_buffer->reader_page);
1034
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001035 fail_free_buffer:
1036 kfree(cpu_buffer);
1037 return NULL;
1038}
1039
1040static void rb_free_cpu_buffer(struct ring_buffer_per_cpu *cpu_buffer)
1041{
Steven Rostedt3adc54f2009-03-30 15:32:01 -04001042 struct list_head *head = cpu_buffer->pages;
Steven Rostedt044fa782008-12-02 23:50:03 -05001043 struct buffer_page *bpage, *tmp;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001044
Steven Rostedtd7690412008-10-01 00:29:53 -04001045 free_buffer_page(cpu_buffer->reader_page);
1046
Steven Rostedt77ae3652009-03-27 11:00:29 -04001047 rb_head_page_deactivate(cpu_buffer);
1048
Steven Rostedt3adc54f2009-03-30 15:32:01 -04001049 if (head) {
1050 list_for_each_entry_safe(bpage, tmp, head, list) {
1051 list_del_init(&bpage->list);
1052 free_buffer_page(bpage);
1053 }
1054 bpage = list_entry(head, struct buffer_page, list);
Steven Rostedt044fa782008-12-02 23:50:03 -05001055 free_buffer_page(bpage);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001056 }
Steven Rostedt3adc54f2009-03-30 15:32:01 -04001057
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001058 kfree(cpu_buffer);
1059}
1060
Steven Rostedt59222ef2009-03-12 11:46:03 -04001061#ifdef CONFIG_HOTPLUG_CPU
Frederic Weisbecker09c9e842009-03-21 04:33:36 +01001062static int rb_cpu_notify(struct notifier_block *self,
1063 unsigned long action, void *hcpu);
Steven Rostedt554f7862009-03-11 22:00:13 -04001064#endif
1065
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001066/**
1067 * ring_buffer_alloc - allocate a new ring_buffer
Robert Richter68814b52008-11-24 12:24:12 +01001068 * @size: the size in bytes per cpu that is needed.
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001069 * @flags: attributes to set for the ring buffer.
1070 *
1071 * Currently the only flag that is available is the RB_FL_OVERWRITE
1072 * flag. This flag means that the buffer will overwrite old data
1073 * when the buffer wraps. If this flag is not set, the buffer will
1074 * drop data when the tail hits the head.
1075 */
Peter Zijlstra1f8a6a12009-06-08 18:18:39 +02001076struct ring_buffer *__ring_buffer_alloc(unsigned long size, unsigned flags,
1077 struct lock_class_key *key)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001078{
1079 struct ring_buffer *buffer;
1080 int bsize;
1081 int cpu;
1082
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001083 /* keep it in its own cache line */
1084 buffer = kzalloc(ALIGN(sizeof(*buffer), cache_line_size()),
1085 GFP_KERNEL);
1086 if (!buffer)
1087 return NULL;
1088
Rusty Russell9e01c1b2009-01-01 10:12:22 +10301089 if (!alloc_cpumask_var(&buffer->cpumask, GFP_KERNEL))
1090 goto fail_free_buffer;
1091
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001092 buffer->pages = DIV_ROUND_UP(size, BUF_PAGE_SIZE);
1093 buffer->flags = flags;
Steven Rostedt37886f62009-03-17 17:22:06 -04001094 buffer->clock = trace_clock_local;
Peter Zijlstra1f8a6a12009-06-08 18:18:39 +02001095 buffer->reader_lock_key = key;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001096
1097 /* need at least two pages */
Steven Rostedt5f78abe2009-06-17 14:11:10 -04001098 if (buffer->pages < 2)
1099 buffer->pages = 2;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001100
Frederic Weisbecker3bf832c2009-03-19 14:47:33 +01001101 /*
1102 * In case of non-hotplug cpu, if the ring-buffer is allocated
1103 * in early initcall, it will not be notified of secondary cpus.
1104 * In that off case, we need to allocate for all possible cpus.
1105 */
1106#ifdef CONFIG_HOTPLUG_CPU
Steven Rostedt554f7862009-03-11 22:00:13 -04001107 get_online_cpus();
1108 cpumask_copy(buffer->cpumask, cpu_online_mask);
Frederic Weisbecker3bf832c2009-03-19 14:47:33 +01001109#else
1110 cpumask_copy(buffer->cpumask, cpu_possible_mask);
1111#endif
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001112 buffer->cpus = nr_cpu_ids;
1113
1114 bsize = sizeof(void *) * nr_cpu_ids;
1115 buffer->buffers = kzalloc(ALIGN(bsize, cache_line_size()),
1116 GFP_KERNEL);
1117 if (!buffer->buffers)
Rusty Russell9e01c1b2009-01-01 10:12:22 +10301118 goto fail_free_cpumask;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001119
1120 for_each_buffer_cpu(buffer, cpu) {
1121 buffer->buffers[cpu] =
1122 rb_allocate_cpu_buffer(buffer, cpu);
1123 if (!buffer->buffers[cpu])
1124 goto fail_free_buffers;
1125 }
1126
Steven Rostedt59222ef2009-03-12 11:46:03 -04001127#ifdef CONFIG_HOTPLUG_CPU
Steven Rostedt554f7862009-03-11 22:00:13 -04001128 buffer->cpu_notify.notifier_call = rb_cpu_notify;
1129 buffer->cpu_notify.priority = 0;
1130 register_cpu_notifier(&buffer->cpu_notify);
1131#endif
1132
1133 put_online_cpus();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001134 mutex_init(&buffer->mutex);
1135
1136 return buffer;
1137
1138 fail_free_buffers:
1139 for_each_buffer_cpu(buffer, cpu) {
1140 if (buffer->buffers[cpu])
1141 rb_free_cpu_buffer(buffer->buffers[cpu]);
1142 }
1143 kfree(buffer->buffers);
1144
Rusty Russell9e01c1b2009-01-01 10:12:22 +10301145 fail_free_cpumask:
1146 free_cpumask_var(buffer->cpumask);
Steven Rostedt554f7862009-03-11 22:00:13 -04001147 put_online_cpus();
Rusty Russell9e01c1b2009-01-01 10:12:22 +10301148
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001149 fail_free_buffer:
1150 kfree(buffer);
1151 return NULL;
1152}
Peter Zijlstra1f8a6a12009-06-08 18:18:39 +02001153EXPORT_SYMBOL_GPL(__ring_buffer_alloc);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001154
1155/**
1156 * ring_buffer_free - free a ring buffer.
1157 * @buffer: the buffer to free.
1158 */
1159void
1160ring_buffer_free(struct ring_buffer *buffer)
1161{
1162 int cpu;
1163
Steven Rostedt554f7862009-03-11 22:00:13 -04001164 get_online_cpus();
1165
Steven Rostedt59222ef2009-03-12 11:46:03 -04001166#ifdef CONFIG_HOTPLUG_CPU
Steven Rostedt554f7862009-03-11 22:00:13 -04001167 unregister_cpu_notifier(&buffer->cpu_notify);
1168#endif
1169
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001170 for_each_buffer_cpu(buffer, cpu)
1171 rb_free_cpu_buffer(buffer->buffers[cpu]);
1172
Steven Rostedt554f7862009-03-11 22:00:13 -04001173 put_online_cpus();
1174
Eric Dumazetbd3f0222009-08-07 12:49:29 +02001175 kfree(buffer->buffers);
Rusty Russell9e01c1b2009-01-01 10:12:22 +10301176 free_cpumask_var(buffer->cpumask);
1177
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001178 kfree(buffer);
1179}
Robert Richterc4f50182008-12-11 16:49:22 +01001180EXPORT_SYMBOL_GPL(ring_buffer_free);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001181
Steven Rostedt37886f62009-03-17 17:22:06 -04001182void ring_buffer_set_clock(struct ring_buffer *buffer,
1183 u64 (*clock)(void))
1184{
1185 buffer->clock = clock;
1186}
1187
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001188static void rb_reset_cpu(struct ring_buffer_per_cpu *cpu_buffer);
1189
1190static void
1191rb_remove_pages(struct ring_buffer_per_cpu *cpu_buffer, unsigned nr_pages)
1192{
Steven Rostedt044fa782008-12-02 23:50:03 -05001193 struct buffer_page *bpage;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001194 struct list_head *p;
1195 unsigned i;
1196
Lai Jiangshanf7112942009-11-03 19:42:45 +08001197 spin_lock_irq(&cpu_buffer->reader_lock);
Steven Rostedt77ae3652009-03-27 11:00:29 -04001198 rb_head_page_deactivate(cpu_buffer);
1199
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001200 for (i = 0; i < nr_pages; i++) {
Steven Rostedt3adc54f2009-03-30 15:32:01 -04001201 if (RB_WARN_ON(cpu_buffer, list_empty(cpu_buffer->pages)))
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05001202 return;
Steven Rostedt3adc54f2009-03-30 15:32:01 -04001203 p = cpu_buffer->pages->next;
Steven Rostedt044fa782008-12-02 23:50:03 -05001204 bpage = list_entry(p, struct buffer_page, list);
1205 list_del_init(&bpage->list);
1206 free_buffer_page(bpage);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001207 }
Steven Rostedt3adc54f2009-03-30 15:32:01 -04001208 if (RB_WARN_ON(cpu_buffer, list_empty(cpu_buffer->pages)))
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05001209 return;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001210
1211 rb_reset_cpu(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001212 rb_check_pages(cpu_buffer);
1213
Steven Rostedtdd7f5942009-12-10 23:20:52 -05001214 spin_unlock_irq(&cpu_buffer->reader_lock);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001215}
1216
1217static void
1218rb_insert_pages(struct ring_buffer_per_cpu *cpu_buffer,
1219 struct list_head *pages, unsigned nr_pages)
1220{
Steven Rostedt044fa782008-12-02 23:50:03 -05001221 struct buffer_page *bpage;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001222 struct list_head *p;
1223 unsigned i;
1224
Steven Rostedt77ae3652009-03-27 11:00:29 -04001225 spin_lock_irq(&cpu_buffer->reader_lock);
1226 rb_head_page_deactivate(cpu_buffer);
1227
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001228 for (i = 0; i < nr_pages; i++) {
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05001229 if (RB_WARN_ON(cpu_buffer, list_empty(pages)))
1230 return;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001231 p = pages->next;
Steven Rostedt044fa782008-12-02 23:50:03 -05001232 bpage = list_entry(p, struct buffer_page, list);
1233 list_del_init(&bpage->list);
Steven Rostedt3adc54f2009-03-30 15:32:01 -04001234 list_add_tail(&bpage->list, cpu_buffer->pages);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001235 }
1236 rb_reset_cpu(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001237 rb_check_pages(cpu_buffer);
1238
Steven Rostedtdd7f5942009-12-10 23:20:52 -05001239 spin_unlock_irq(&cpu_buffer->reader_lock);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001240}
1241
1242/**
1243 * ring_buffer_resize - resize the ring buffer
1244 * @buffer: the buffer to resize.
1245 * @size: the new size.
1246 *
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001247 * Minimum size is 2 * BUF_PAGE_SIZE.
1248 *
1249 * Returns -1 on failure.
1250 */
1251int ring_buffer_resize(struct ring_buffer *buffer, unsigned long size)
1252{
1253 struct ring_buffer_per_cpu *cpu_buffer;
1254 unsigned nr_pages, rm_pages, new_pages;
Steven Rostedt044fa782008-12-02 23:50:03 -05001255 struct buffer_page *bpage, *tmp;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001256 unsigned long buffer_size;
1257 unsigned long addr;
1258 LIST_HEAD(pages);
1259 int i, cpu;
1260
Ingo Molnaree51a1d2008-11-13 14:58:31 +01001261 /*
1262 * Always succeed at resizing a non-existent buffer:
1263 */
1264 if (!buffer)
1265 return size;
1266
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001267 size = DIV_ROUND_UP(size, BUF_PAGE_SIZE);
1268 size *= BUF_PAGE_SIZE;
1269 buffer_size = buffer->pages * BUF_PAGE_SIZE;
1270
1271 /* we need a minimum of two pages */
1272 if (size < BUF_PAGE_SIZE * 2)
1273 size = BUF_PAGE_SIZE * 2;
1274
1275 if (size == buffer_size)
1276 return size;
1277
Steven Rostedt18421012009-12-10 22:54:27 -05001278 atomic_inc(&buffer->record_disabled);
1279
1280 /* Make sure all writers are done with this buffer. */
1281 synchronize_sched();
1282
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001283 mutex_lock(&buffer->mutex);
Steven Rostedt554f7862009-03-11 22:00:13 -04001284 get_online_cpus();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001285
1286 nr_pages = DIV_ROUND_UP(size, BUF_PAGE_SIZE);
1287
1288 if (size < buffer_size) {
1289
1290 /* easy case, just free pages */
Steven Rostedt554f7862009-03-11 22:00:13 -04001291 if (RB_WARN_ON(buffer, nr_pages >= buffer->pages))
1292 goto out_fail;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001293
1294 rm_pages = buffer->pages - nr_pages;
1295
1296 for_each_buffer_cpu(buffer, cpu) {
1297 cpu_buffer = buffer->buffers[cpu];
1298 rb_remove_pages(cpu_buffer, rm_pages);
1299 }
1300 goto out;
1301 }
1302
1303 /*
1304 * This is a bit more difficult. We only want to add pages
1305 * when we can allocate enough for all CPUs. We do this
1306 * by allocating all the pages and storing them on a local
1307 * link list. If we succeed in our allocation, then we
1308 * add these pages to the cpu_buffers. Otherwise we just free
1309 * them all and return -ENOMEM;
1310 */
Steven Rostedt554f7862009-03-11 22:00:13 -04001311 if (RB_WARN_ON(buffer, nr_pages <= buffer->pages))
1312 goto out_fail;
Steven Rostedtf536aaf2008-11-10 23:07:30 -05001313
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001314 new_pages = nr_pages - buffer->pages;
1315
1316 for_each_buffer_cpu(buffer, cpu) {
1317 for (i = 0; i < new_pages; i++) {
Steven Rostedt044fa782008-12-02 23:50:03 -05001318 bpage = kzalloc_node(ALIGN(sizeof(*bpage),
Steven Rostedte4c2ce82008-10-01 11:14:54 -04001319 cache_line_size()),
1320 GFP_KERNEL, cpu_to_node(cpu));
Steven Rostedt044fa782008-12-02 23:50:03 -05001321 if (!bpage)
Steven Rostedte4c2ce82008-10-01 11:14:54 -04001322 goto free_pages;
Steven Rostedt044fa782008-12-02 23:50:03 -05001323 list_add(&bpage->list, &pages);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001324 addr = __get_free_page(GFP_KERNEL);
1325 if (!addr)
1326 goto free_pages;
Steven Rostedt044fa782008-12-02 23:50:03 -05001327 bpage->page = (void *)addr;
1328 rb_init_page(bpage->page);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001329 }
1330 }
1331
1332 for_each_buffer_cpu(buffer, cpu) {
1333 cpu_buffer = buffer->buffers[cpu];
1334 rb_insert_pages(cpu_buffer, &pages, new_pages);
1335 }
1336
Steven Rostedt554f7862009-03-11 22:00:13 -04001337 if (RB_WARN_ON(buffer, !list_empty(&pages)))
1338 goto out_fail;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001339
1340 out:
1341 buffer->pages = nr_pages;
Steven Rostedt554f7862009-03-11 22:00:13 -04001342 put_online_cpus();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001343 mutex_unlock(&buffer->mutex);
1344
Steven Rostedt18421012009-12-10 22:54:27 -05001345 atomic_dec(&buffer->record_disabled);
1346
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001347 return size;
1348
1349 free_pages:
Steven Rostedt044fa782008-12-02 23:50:03 -05001350 list_for_each_entry_safe(bpage, tmp, &pages, list) {
1351 list_del_init(&bpage->list);
1352 free_buffer_page(bpage);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001353 }
Steven Rostedt554f7862009-03-11 22:00:13 -04001354 put_online_cpus();
Vegard Nossum641d2f62008-11-18 19:22:13 +01001355 mutex_unlock(&buffer->mutex);
Steven Rostedt18421012009-12-10 22:54:27 -05001356 atomic_dec(&buffer->record_disabled);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001357 return -ENOMEM;
Steven Rostedt554f7862009-03-11 22:00:13 -04001358
1359 /*
1360 * Something went totally wrong, and we are too paranoid
1361 * to even clean up the mess.
1362 */
1363 out_fail:
1364 put_online_cpus();
1365 mutex_unlock(&buffer->mutex);
Steven Rostedt18421012009-12-10 22:54:27 -05001366 atomic_dec(&buffer->record_disabled);
Steven Rostedt554f7862009-03-11 22:00:13 -04001367 return -1;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001368}
Robert Richterc4f50182008-12-11 16:49:22 +01001369EXPORT_SYMBOL_GPL(ring_buffer_resize);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001370
Steven Rostedt8789a9e2008-12-02 15:34:07 -05001371static inline void *
Steven Rostedt044fa782008-12-02 23:50:03 -05001372__rb_data_page_index(struct buffer_data_page *bpage, unsigned index)
Steven Rostedt8789a9e2008-12-02 15:34:07 -05001373{
Steven Rostedt044fa782008-12-02 23:50:03 -05001374 return bpage->data + index;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05001375}
1376
Steven Rostedt044fa782008-12-02 23:50:03 -05001377static inline void *__rb_page_index(struct buffer_page *bpage, unsigned index)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001378{
Steven Rostedt044fa782008-12-02 23:50:03 -05001379 return bpage->page->data + index;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001380}
1381
1382static inline struct ring_buffer_event *
Steven Rostedtd7690412008-10-01 00:29:53 -04001383rb_reader_event(struct ring_buffer_per_cpu *cpu_buffer)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001384{
Steven Rostedt6f807ac2008-10-04 02:00:58 -04001385 return __rb_page_index(cpu_buffer->reader_page,
1386 cpu_buffer->reader_page->read);
1387}
1388
1389static inline struct ring_buffer_event *
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001390rb_iter_head_event(struct ring_buffer_iter *iter)
1391{
Steven Rostedt6f807ac2008-10-04 02:00:58 -04001392 return __rb_page_index(iter->head_page, iter->head);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001393}
1394
Steven Rostedt77ae3652009-03-27 11:00:29 -04001395static inline unsigned long rb_page_write(struct buffer_page *bpage)
Steven Rostedtbf41a152008-10-04 02:00:59 -04001396{
Steven Rostedt77ae3652009-03-27 11:00:29 -04001397 return local_read(&bpage->write) & RB_WRITE_MASK;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001398}
1399
1400static inline unsigned rb_page_commit(struct buffer_page *bpage)
1401{
Steven Rostedtabc9b562008-12-02 15:34:06 -05001402 return local_read(&bpage->page->commit);
Steven Rostedtbf41a152008-10-04 02:00:59 -04001403}
1404
Steven Rostedt77ae3652009-03-27 11:00:29 -04001405static inline unsigned long rb_page_entries(struct buffer_page *bpage)
1406{
1407 return local_read(&bpage->entries) & RB_WRITE_MASK;
1408}
1409
Steven Rostedtbf41a152008-10-04 02:00:59 -04001410/* Size is determined by what has been commited */
1411static inline unsigned rb_page_size(struct buffer_page *bpage)
1412{
1413 return rb_page_commit(bpage);
1414}
1415
1416static inline unsigned
1417rb_commit_index(struct ring_buffer_per_cpu *cpu_buffer)
1418{
1419 return rb_page_commit(cpu_buffer->commit_page);
1420}
1421
Steven Rostedtbf41a152008-10-04 02:00:59 -04001422static inline unsigned
1423rb_event_index(struct ring_buffer_event *event)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001424{
Steven Rostedtbf41a152008-10-04 02:00:59 -04001425 unsigned long addr = (unsigned long)event;
1426
Steven Rostedt22f470f2009-06-11 09:29:58 -04001427 return (addr & ~PAGE_MASK) - BUF_PAGE_HDR_SIZE;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001428}
1429
Steven Rostedt0f0c85f2009-05-11 16:08:00 -04001430static inline int
Steven Rostedtfa743952009-06-16 12:37:57 -04001431rb_event_is_commit(struct ring_buffer_per_cpu *cpu_buffer,
1432 struct ring_buffer_event *event)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001433{
Steven Rostedtbf41a152008-10-04 02:00:59 -04001434 unsigned long addr = (unsigned long)event;
1435 unsigned long index;
1436
1437 index = rb_event_index(event);
1438 addr &= PAGE_MASK;
1439
1440 return cpu_buffer->commit_page->page == (void *)addr &&
1441 rb_commit_index(cpu_buffer) == index;
1442}
1443
Andrew Morton34a148b2009-01-09 12:27:09 -08001444static void
Steven Rostedtbf41a152008-10-04 02:00:59 -04001445rb_set_commit_to_write(struct ring_buffer_per_cpu *cpu_buffer)
1446{
Steven Rostedt77ae3652009-03-27 11:00:29 -04001447 unsigned long max_count;
1448
Steven Rostedtbf41a152008-10-04 02:00:59 -04001449 /*
1450 * We only race with interrupts and NMIs on this CPU.
1451 * If we own the commit event, then we can commit
1452 * all others that interrupted us, since the interruptions
1453 * are in stack format (they finish before they come
1454 * back to us). This allows us to do a simple loop to
1455 * assign the commit to the tail.
1456 */
Steven Rostedta8ccf1d2008-12-23 11:32:24 -05001457 again:
Steven Rostedt77ae3652009-03-27 11:00:29 -04001458 max_count = cpu_buffer->buffer->pages * 100;
1459
Steven Rostedtbf41a152008-10-04 02:00:59 -04001460 while (cpu_buffer->commit_page != cpu_buffer->tail_page) {
Steven Rostedt77ae3652009-03-27 11:00:29 -04001461 if (RB_WARN_ON(cpu_buffer, !(--max_count)))
1462 return;
1463 if (RB_WARN_ON(cpu_buffer,
1464 rb_is_reader_page(cpu_buffer->tail_page)))
1465 return;
1466 local_set(&cpu_buffer->commit_page->page->commit,
1467 rb_page_write(cpu_buffer->commit_page));
Steven Rostedtbf41a152008-10-04 02:00:59 -04001468 rb_inc_page(cpu_buffer, &cpu_buffer->commit_page);
Steven Rostedtabc9b562008-12-02 15:34:06 -05001469 cpu_buffer->write_stamp =
1470 cpu_buffer->commit_page->page->time_stamp;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001471 /* add barrier to keep gcc from optimizing too much */
1472 barrier();
1473 }
1474 while (rb_commit_index(cpu_buffer) !=
1475 rb_page_write(cpu_buffer->commit_page)) {
Steven Rostedt77ae3652009-03-27 11:00:29 -04001476
1477 local_set(&cpu_buffer->commit_page->page->commit,
1478 rb_page_write(cpu_buffer->commit_page));
1479 RB_WARN_ON(cpu_buffer,
1480 local_read(&cpu_buffer->commit_page->page->commit) &
1481 ~RB_WRITE_MASK);
Steven Rostedtbf41a152008-10-04 02:00:59 -04001482 barrier();
1483 }
Steven Rostedta8ccf1d2008-12-23 11:32:24 -05001484
1485 /* again, keep gcc from optimizing */
1486 barrier();
1487
1488 /*
1489 * If an interrupt came in just after the first while loop
1490 * and pushed the tail page forward, we will be left with
1491 * a dangling commit that will never go forward.
1492 */
1493 if (unlikely(cpu_buffer->commit_page != cpu_buffer->tail_page))
1494 goto again;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001495}
1496
Steven Rostedtd7690412008-10-01 00:29:53 -04001497static void rb_reset_reader_page(struct ring_buffer_per_cpu *cpu_buffer)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001498{
Steven Rostedtabc9b562008-12-02 15:34:06 -05001499 cpu_buffer->read_stamp = cpu_buffer->reader_page->page->time_stamp;
Steven Rostedt6f807ac2008-10-04 02:00:58 -04001500 cpu_buffer->reader_page->read = 0;
Steven Rostedtd7690412008-10-01 00:29:53 -04001501}
1502
Andrew Morton34a148b2009-01-09 12:27:09 -08001503static void rb_inc_iter(struct ring_buffer_iter *iter)
Steven Rostedtd7690412008-10-01 00:29:53 -04001504{
1505 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
1506
1507 /*
1508 * The iterator could be on the reader page (it starts there).
1509 * But the head could have moved, since the reader was
1510 * found. Check for this case and assign the iterator
1511 * to the head page instead of next.
1512 */
1513 if (iter->head_page == cpu_buffer->reader_page)
Steven Rostedt77ae3652009-03-27 11:00:29 -04001514 iter->head_page = rb_set_head_page(cpu_buffer);
Steven Rostedtd7690412008-10-01 00:29:53 -04001515 else
1516 rb_inc_page(cpu_buffer, &iter->head_page);
1517
Steven Rostedtabc9b562008-12-02 15:34:06 -05001518 iter->read_stamp = iter->head_page->page->time_stamp;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001519 iter->head = 0;
1520}
1521
1522/**
1523 * ring_buffer_update_event - update event type and data
1524 * @event: the even to update
1525 * @type: the type of event
1526 * @length: the size of the event field in the ring buffer
1527 *
1528 * Update the type and data fields of the event. The length
1529 * is the actual size that is written to the ring buffer,
1530 * and with this, we can determine what to place into the
1531 * data field.
1532 */
Andrew Morton34a148b2009-01-09 12:27:09 -08001533static void
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001534rb_update_event(struct ring_buffer_event *event,
1535 unsigned type, unsigned length)
1536{
Lai Jiangshan334d4162009-04-24 11:27:05 +08001537 event->type_len = type;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001538
1539 switch (type) {
1540
1541 case RINGBUF_TYPE_PADDING:
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001542 case RINGBUF_TYPE_TIME_EXTEND:
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001543 case RINGBUF_TYPE_TIME_STAMP:
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001544 break;
1545
Lai Jiangshan334d4162009-04-24 11:27:05 +08001546 case 0:
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001547 length -= RB_EVNT_HDR_SIZE;
Lai Jiangshan334d4162009-04-24 11:27:05 +08001548 if (length > RB_MAX_SMALL_DATA)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001549 event->array[0] = length;
Lai Jiangshan334d4162009-04-24 11:27:05 +08001550 else
1551 event->type_len = DIV_ROUND_UP(length, RB_ALIGNMENT);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001552 break;
1553 default:
1554 BUG();
1555 }
1556}
1557
Steven Rostedt77ae3652009-03-27 11:00:29 -04001558/*
1559 * rb_handle_head_page - writer hit the head page
1560 *
1561 * Returns: +1 to retry page
1562 * 0 to continue
1563 * -1 on error
1564 */
1565static int
1566rb_handle_head_page(struct ring_buffer_per_cpu *cpu_buffer,
1567 struct buffer_page *tail_page,
1568 struct buffer_page *next_page)
1569{
1570 struct buffer_page *new_head;
1571 int entries;
1572 int type;
1573 int ret;
1574
1575 entries = rb_page_entries(next_page);
1576
1577 /*
1578 * The hard part is here. We need to move the head
1579 * forward, and protect against both readers on
1580 * other CPUs and writers coming in via interrupts.
1581 */
1582 type = rb_head_page_set_update(cpu_buffer, next_page, tail_page,
1583 RB_PAGE_HEAD);
1584
1585 /*
1586 * type can be one of four:
1587 * NORMAL - an interrupt already moved it for us
1588 * HEAD - we are the first to get here.
1589 * UPDATE - we are the interrupt interrupting
1590 * a current move.
1591 * MOVED - a reader on another CPU moved the next
1592 * pointer to its reader page. Give up
1593 * and try again.
1594 */
1595
1596 switch (type) {
1597 case RB_PAGE_HEAD:
1598 /*
1599 * We changed the head to UPDATE, thus
1600 * it is our responsibility to update
1601 * the counters.
1602 */
1603 local_add(entries, &cpu_buffer->overrun);
1604
1605 /*
1606 * The entries will be zeroed out when we move the
1607 * tail page.
1608 */
1609
1610 /* still more to do */
1611 break;
1612
1613 case RB_PAGE_UPDATE:
1614 /*
1615 * This is an interrupt that interrupt the
1616 * previous update. Still more to do.
1617 */
1618 break;
1619 case RB_PAGE_NORMAL:
1620 /*
1621 * An interrupt came in before the update
1622 * and processed this for us.
1623 * Nothing left to do.
1624 */
1625 return 1;
1626 case RB_PAGE_MOVED:
1627 /*
1628 * The reader is on another CPU and just did
1629 * a swap with our next_page.
1630 * Try again.
1631 */
1632 return 1;
1633 default:
1634 RB_WARN_ON(cpu_buffer, 1); /* WTF??? */
1635 return -1;
1636 }
1637
1638 /*
1639 * Now that we are here, the old head pointer is
1640 * set to UPDATE. This will keep the reader from
1641 * swapping the head page with the reader page.
1642 * The reader (on another CPU) will spin till
1643 * we are finished.
1644 *
1645 * We just need to protect against interrupts
1646 * doing the job. We will set the next pointer
1647 * to HEAD. After that, we set the old pointer
1648 * to NORMAL, but only if it was HEAD before.
1649 * otherwise we are an interrupt, and only
1650 * want the outer most commit to reset it.
1651 */
1652 new_head = next_page;
1653 rb_inc_page(cpu_buffer, &new_head);
1654
1655 ret = rb_head_page_set_head(cpu_buffer, new_head, next_page,
1656 RB_PAGE_NORMAL);
1657
1658 /*
1659 * Valid returns are:
1660 * HEAD - an interrupt came in and already set it.
1661 * NORMAL - One of two things:
1662 * 1) We really set it.
1663 * 2) A bunch of interrupts came in and moved
1664 * the page forward again.
1665 */
1666 switch (ret) {
1667 case RB_PAGE_HEAD:
1668 case RB_PAGE_NORMAL:
1669 /* OK */
1670 break;
1671 default:
1672 RB_WARN_ON(cpu_buffer, 1);
1673 return -1;
1674 }
1675
1676 /*
1677 * It is possible that an interrupt came in,
1678 * set the head up, then more interrupts came in
1679 * and moved it again. When we get back here,
1680 * the page would have been set to NORMAL but we
1681 * just set it back to HEAD.
1682 *
1683 * How do you detect this? Well, if that happened
1684 * the tail page would have moved.
1685 */
1686 if (ret == RB_PAGE_NORMAL) {
1687 /*
1688 * If the tail had moved passed next, then we need
1689 * to reset the pointer.
1690 */
1691 if (cpu_buffer->tail_page != tail_page &&
1692 cpu_buffer->tail_page != next_page)
1693 rb_head_page_set_normal(cpu_buffer, new_head,
1694 next_page,
1695 RB_PAGE_HEAD);
1696 }
1697
1698 /*
1699 * If this was the outer most commit (the one that
1700 * changed the original pointer from HEAD to UPDATE),
1701 * then it is up to us to reset it to NORMAL.
1702 */
1703 if (type == RB_PAGE_HEAD) {
1704 ret = rb_head_page_set_normal(cpu_buffer, next_page,
1705 tail_page,
1706 RB_PAGE_UPDATE);
1707 if (RB_WARN_ON(cpu_buffer,
1708 ret != RB_PAGE_UPDATE))
1709 return -1;
1710 }
1711
1712 return 0;
1713}
1714
Andrew Morton34a148b2009-01-09 12:27:09 -08001715static unsigned rb_calculate_event_length(unsigned length)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001716{
1717 struct ring_buffer_event event; /* Used only for sizeof array */
1718
1719 /* zero length can cause confusions */
1720 if (!length)
1721 length = 1;
1722
1723 if (length > RB_MAX_SMALL_DATA)
1724 length += sizeof(event.array[0]);
1725
1726 length += RB_EVNT_HDR_SIZE;
1727 length = ALIGN(length, RB_ALIGNMENT);
1728
1729 return length;
1730}
1731
Steven Rostedtc7b09302009-06-11 11:12:00 -04001732static inline void
1733rb_reset_tail(struct ring_buffer_per_cpu *cpu_buffer,
1734 struct buffer_page *tail_page,
1735 unsigned long tail, unsigned long length)
1736{
1737 struct ring_buffer_event *event;
1738
1739 /*
1740 * Only the event that crossed the page boundary
1741 * must fill the old tail_page with padding.
1742 */
1743 if (tail >= BUF_PAGE_SIZE) {
1744 local_sub(length, &tail_page->write);
1745 return;
1746 }
1747
1748 event = __rb_page_index(tail_page, tail);
Linus Torvaldsb0b70652009-06-20 10:56:46 -07001749 kmemcheck_annotate_bitfield(event, bitfield);
Steven Rostedtc7b09302009-06-11 11:12:00 -04001750
1751 /*
1752 * If this event is bigger than the minimum size, then
1753 * we need to be careful that we don't subtract the
1754 * write counter enough to allow another writer to slip
1755 * in on this page.
1756 * We put in a discarded commit instead, to make sure
1757 * that this space is not used again.
1758 *
1759 * If we are less than the minimum size, we don't need to
1760 * worry about it.
1761 */
1762 if (tail > (BUF_PAGE_SIZE - RB_EVNT_MIN_SIZE)) {
1763 /* No room for any events */
1764
1765 /* Mark the rest of the page with padding */
1766 rb_event_set_padding(event);
1767
1768 /* Set the write back to the previous setting */
1769 local_sub(length, &tail_page->write);
1770 return;
1771 }
1772
1773 /* Put in a discarded event */
1774 event->array[0] = (BUF_PAGE_SIZE - tail) - RB_EVNT_HDR_SIZE;
1775 event->type_len = RINGBUF_TYPE_PADDING;
1776 /* time delta must be non zero */
1777 event->time_delta = 1;
Steven Rostedtc7b09302009-06-11 11:12:00 -04001778
1779 /* Set write to end of buffer */
1780 length = (tail + length) - BUF_PAGE_SIZE;
1781 local_sub(length, &tail_page->write);
1782}
Steven Rostedt6634ff22009-05-06 15:30:07 -04001783
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001784static struct ring_buffer_event *
Steven Rostedt6634ff22009-05-06 15:30:07 -04001785rb_move_tail(struct ring_buffer_per_cpu *cpu_buffer,
1786 unsigned long length, unsigned long tail,
Steven Rostedt6634ff22009-05-06 15:30:07 -04001787 struct buffer_page *tail_page, u64 *ts)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001788{
Steven Rostedt5a50e332009-11-17 08:43:01 -05001789 struct buffer_page *commit_page = cpu_buffer->commit_page;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001790 struct ring_buffer *buffer = cpu_buffer->buffer;
Steven Rostedt77ae3652009-03-27 11:00:29 -04001791 struct buffer_page *next_page;
1792 int ret;
Steven Rostedtaa20ae82009-05-05 21:16:11 -04001793
1794 next_page = tail_page;
1795
Steven Rostedtaa20ae82009-05-05 21:16:11 -04001796 rb_inc_page(cpu_buffer, &next_page);
1797
Steven Rostedtaa20ae82009-05-05 21:16:11 -04001798 /*
1799 * If for some reason, we had an interrupt storm that made
1800 * it all the way around the buffer, bail, and warn
1801 * about it.
1802 */
1803 if (unlikely(next_page == commit_page)) {
Steven Rostedt77ae3652009-03-27 11:00:29 -04001804 local_inc(&cpu_buffer->commit_overrun);
Steven Rostedtaa20ae82009-05-05 21:16:11 -04001805 goto out_reset;
1806 }
1807
Steven Rostedt77ae3652009-03-27 11:00:29 -04001808 /*
1809 * This is where the fun begins!
1810 *
1811 * We are fighting against races between a reader that
1812 * could be on another CPU trying to swap its reader
1813 * page with the buffer head.
1814 *
1815 * We are also fighting against interrupts coming in and
1816 * moving the head or tail on us as well.
1817 *
1818 * If the next page is the head page then we have filled
1819 * the buffer, unless the commit page is still on the
1820 * reader page.
1821 */
1822 if (rb_is_head_page(cpu_buffer, next_page, &tail_page->list)) {
Steven Rostedtaa20ae82009-05-05 21:16:11 -04001823
Steven Rostedt77ae3652009-03-27 11:00:29 -04001824 /*
1825 * If the commit is not on the reader page, then
1826 * move the header page.
1827 */
1828 if (!rb_is_reader_page(cpu_buffer->commit_page)) {
1829 /*
1830 * If we are not in overwrite mode,
1831 * this is easy, just stop here.
1832 */
1833 if (!(buffer->flags & RB_FL_OVERWRITE))
1834 goto out_reset;
Steven Rostedtaa20ae82009-05-05 21:16:11 -04001835
Steven Rostedt77ae3652009-03-27 11:00:29 -04001836 ret = rb_handle_head_page(cpu_buffer,
1837 tail_page,
1838 next_page);
1839 if (ret < 0)
1840 goto out_reset;
1841 if (ret)
1842 goto out_again;
1843 } else {
1844 /*
1845 * We need to be careful here too. The
1846 * commit page could still be on the reader
1847 * page. We could have a small buffer, and
1848 * have filled up the buffer with events
1849 * from interrupts and such, and wrapped.
1850 *
1851 * Note, if the tail page is also the on the
1852 * reader_page, we let it move out.
1853 */
1854 if (unlikely((cpu_buffer->commit_page !=
1855 cpu_buffer->tail_page) &&
1856 (cpu_buffer->commit_page ==
1857 cpu_buffer->reader_page))) {
1858 local_inc(&cpu_buffer->commit_overrun);
1859 goto out_reset;
1860 }
Steven Rostedtaa20ae82009-05-05 21:16:11 -04001861 }
1862 }
1863
Steven Rostedt77ae3652009-03-27 11:00:29 -04001864 ret = rb_tail_page_update(cpu_buffer, tail_page, next_page);
1865 if (ret) {
1866 /*
1867 * Nested commits always have zero deltas, so
1868 * just reread the time stamp
1869 */
Jiri Olsa6d3f1e12009-10-23 19:36:19 -04001870 *ts = rb_time_stamp(buffer);
Steven Rostedt77ae3652009-03-27 11:00:29 -04001871 next_page->page->time_stamp = *ts;
Steven Rostedtaa20ae82009-05-05 21:16:11 -04001872 }
1873
Steven Rostedt77ae3652009-03-27 11:00:29 -04001874 out_again:
Steven Rostedtaa20ae82009-05-05 21:16:11 -04001875
Steven Rostedt77ae3652009-03-27 11:00:29 -04001876 rb_reset_tail(cpu_buffer, tail_page, tail, length);
Steven Rostedtaa20ae82009-05-05 21:16:11 -04001877
1878 /* fail and let the caller try again */
1879 return ERR_PTR(-EAGAIN);
1880
Steven Rostedt45141d42009-02-12 13:19:48 -05001881 out_reset:
Lai Jiangshan6f3b3442009-01-12 11:06:18 +08001882 /* reset write */
Steven Rostedtc7b09302009-06-11 11:12:00 -04001883 rb_reset_tail(cpu_buffer, tail_page, tail, length);
Lai Jiangshan6f3b3442009-01-12 11:06:18 +08001884
Steven Rostedtbf41a152008-10-04 02:00:59 -04001885 return NULL;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001886}
1887
Steven Rostedt6634ff22009-05-06 15:30:07 -04001888static struct ring_buffer_event *
1889__rb_reserve_next(struct ring_buffer_per_cpu *cpu_buffer,
1890 unsigned type, unsigned long length, u64 *ts)
1891{
Steven Rostedt5a50e332009-11-17 08:43:01 -05001892 struct buffer_page *tail_page;
Steven Rostedt6634ff22009-05-06 15:30:07 -04001893 struct ring_buffer_event *event;
1894 unsigned long tail, write;
1895
Steven Rostedt6634ff22009-05-06 15:30:07 -04001896 tail_page = cpu_buffer->tail_page;
1897 write = local_add_return(length, &tail_page->write);
Steven Rostedt77ae3652009-03-27 11:00:29 -04001898
1899 /* set write to only the index of the write */
1900 write &= RB_WRITE_MASK;
Steven Rostedt6634ff22009-05-06 15:30:07 -04001901 tail = write - length;
1902
1903 /* See if we shot pass the end of this buffer page */
1904 if (write > BUF_PAGE_SIZE)
1905 return rb_move_tail(cpu_buffer, length, tail,
Steven Rostedt5a50e332009-11-17 08:43:01 -05001906 tail_page, ts);
Steven Rostedt6634ff22009-05-06 15:30:07 -04001907
1908 /* We reserved something on the buffer */
1909
Steven Rostedt6634ff22009-05-06 15:30:07 -04001910 event = __rb_page_index(tail_page, tail);
Vegard Nossum1744a212009-02-28 08:29:44 +01001911 kmemcheck_annotate_bitfield(event, bitfield);
Steven Rostedt6634ff22009-05-06 15:30:07 -04001912 rb_update_event(event, type, length);
1913
1914 /* The passed in type is zero for DATA */
1915 if (likely(!type))
1916 local_inc(&tail_page->entries);
1917
1918 /*
Steven Rostedtfa743952009-06-16 12:37:57 -04001919 * If this is the first commit on the page, then update
1920 * its timestamp.
Steven Rostedt6634ff22009-05-06 15:30:07 -04001921 */
Steven Rostedtfa743952009-06-16 12:37:57 -04001922 if (!tail)
1923 tail_page->page->time_stamp = *ts;
Steven Rostedt6634ff22009-05-06 15:30:07 -04001924
1925 return event;
1926}
1927
Steven Rostedtedd813bf2009-06-02 23:00:53 -04001928static inline int
1929rb_try_to_discard(struct ring_buffer_per_cpu *cpu_buffer,
1930 struct ring_buffer_event *event)
1931{
1932 unsigned long new_index, old_index;
1933 struct buffer_page *bpage;
1934 unsigned long index;
1935 unsigned long addr;
1936
1937 new_index = rb_event_index(event);
1938 old_index = new_index + rb_event_length(event);
1939 addr = (unsigned long)event;
1940 addr &= PAGE_MASK;
1941
1942 bpage = cpu_buffer->tail_page;
1943
1944 if (bpage->page == (void *)addr && rb_page_write(bpage) == old_index) {
Steven Rostedt77ae3652009-03-27 11:00:29 -04001945 unsigned long write_mask =
1946 local_read(&bpage->write) & ~RB_WRITE_MASK;
Steven Rostedtedd813bf2009-06-02 23:00:53 -04001947 /*
1948 * This is on the tail page. It is possible that
1949 * a write could come in and move the tail page
1950 * and write to the next page. That is fine
1951 * because we just shorten what is on this page.
1952 */
Steven Rostedt77ae3652009-03-27 11:00:29 -04001953 old_index += write_mask;
1954 new_index += write_mask;
Steven Rostedtedd813bf2009-06-02 23:00:53 -04001955 index = local_cmpxchg(&bpage->write, old_index, new_index);
1956 if (index == old_index)
1957 return 1;
1958 }
1959
1960 /* could not discard */
1961 return 0;
1962}
1963
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001964static int
1965rb_add_time_stamp(struct ring_buffer_per_cpu *cpu_buffer,
1966 u64 *ts, u64 *delta)
1967{
1968 struct ring_buffer_event *event;
1969 static int once;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001970 int ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001971
1972 if (unlikely(*delta > (1ULL << 59) && !once++)) {
1973 printk(KERN_WARNING "Delta way too big! %llu"
1974 " ts=%llu write stamp = %llu\n",
Stephen Rothwelle2862c92008-10-27 17:43:28 +11001975 (unsigned long long)*delta,
1976 (unsigned long long)*ts,
1977 (unsigned long long)cpu_buffer->write_stamp);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001978 WARN_ON(1);
1979 }
1980
1981 /*
1982 * The delta is too big, we to add a
1983 * new timestamp.
1984 */
1985 event = __rb_reserve_next(cpu_buffer,
1986 RINGBUF_TYPE_TIME_EXTEND,
1987 RB_LEN_TIME_EXTEND,
1988 ts);
1989 if (!event)
Steven Rostedtbf41a152008-10-04 02:00:59 -04001990 return -EBUSY;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001991
Steven Rostedtbf41a152008-10-04 02:00:59 -04001992 if (PTR_ERR(event) == -EAGAIN)
1993 return -EAGAIN;
1994
1995 /* Only a commited time event can update the write stamp */
Steven Rostedtfa743952009-06-16 12:37:57 -04001996 if (rb_event_is_commit(cpu_buffer, event)) {
Steven Rostedtbf41a152008-10-04 02:00:59 -04001997 /*
Steven Rostedtfa743952009-06-16 12:37:57 -04001998 * If this is the first on the page, then it was
1999 * updated with the page itself. Try to discard it
2000 * and if we can't just make it zero.
Steven Rostedtbf41a152008-10-04 02:00:59 -04002001 */
2002 if (rb_event_index(event)) {
2003 event->time_delta = *delta & TS_MASK;
2004 event->array[0] = *delta >> TS_SHIFT;
2005 } else {
Steven Rostedtea05b572009-06-03 09:30:10 -04002006 /* try to discard, since we do not need this */
2007 if (!rb_try_to_discard(cpu_buffer, event)) {
2008 /* nope, just zero it */
2009 event->time_delta = 0;
2010 event->array[0] = 0;
2011 }
Steven Rostedtbf41a152008-10-04 02:00:59 -04002012 }
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002013 cpu_buffer->write_stamp = *ts;
Steven Rostedtbf41a152008-10-04 02:00:59 -04002014 /* let the caller know this was the commit */
2015 ret = 1;
2016 } else {
Steven Rostedtedd813bf2009-06-02 23:00:53 -04002017 /* Try to discard the event */
2018 if (!rb_try_to_discard(cpu_buffer, event)) {
2019 /* Darn, this is just wasted space */
2020 event->time_delta = 0;
2021 event->array[0] = 0;
Steven Rostedtedd813bf2009-06-02 23:00:53 -04002022 }
Steven Rostedtf57a8a12009-06-05 14:11:30 -04002023 ret = 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002024 }
2025
Steven Rostedtbf41a152008-10-04 02:00:59 -04002026 *delta = 0;
2027
2028 return ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002029}
2030
Steven Rostedtfa743952009-06-16 12:37:57 -04002031static void rb_start_commit(struct ring_buffer_per_cpu *cpu_buffer)
2032{
2033 local_inc(&cpu_buffer->committing);
2034 local_inc(&cpu_buffer->commits);
2035}
2036
2037static void rb_end_commit(struct ring_buffer_per_cpu *cpu_buffer)
2038{
2039 unsigned long commits;
2040
2041 if (RB_WARN_ON(cpu_buffer,
2042 !local_read(&cpu_buffer->committing)))
2043 return;
2044
2045 again:
2046 commits = local_read(&cpu_buffer->commits);
2047 /* synchronize with interrupts */
2048 barrier();
2049 if (local_read(&cpu_buffer->committing) == 1)
2050 rb_set_commit_to_write(cpu_buffer);
2051
2052 local_dec(&cpu_buffer->committing);
2053
2054 /* synchronize with interrupts */
2055 barrier();
2056
2057 /*
2058 * Need to account for interrupts coming in between the
2059 * updating of the commit page and the clearing of the
2060 * committing counter.
2061 */
2062 if (unlikely(local_read(&cpu_buffer->commits) != commits) &&
2063 !local_read(&cpu_buffer->committing)) {
2064 local_inc(&cpu_buffer->committing);
2065 goto again;
2066 }
2067}
2068
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002069static struct ring_buffer_event *
Steven Rostedt62f0b3e2009-09-04 14:11:34 -04002070rb_reserve_next_event(struct ring_buffer *buffer,
2071 struct ring_buffer_per_cpu *cpu_buffer,
Steven Rostedt1cd8d732009-05-11 14:08:09 -04002072 unsigned long length)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002073{
2074 struct ring_buffer_event *event;
Steven Rostedt168b6b12009-05-11 22:11:05 -04002075 u64 ts, delta = 0;
Steven Rostedtbf41a152008-10-04 02:00:59 -04002076 int commit = 0;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002077 int nr_loops = 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002078
Steven Rostedtfa743952009-06-16 12:37:57 -04002079 rb_start_commit(cpu_buffer);
2080
Steven Rostedt85bac322009-09-04 14:24:40 -04002081#ifdef CONFIG_RING_BUFFER_ALLOW_SWAP
Steven Rostedt62f0b3e2009-09-04 14:11:34 -04002082 /*
2083 * Due to the ability to swap a cpu buffer from a buffer
2084 * it is possible it was swapped before we committed.
2085 * (committing stops a swap). We check for it here and
2086 * if it happened, we have to fail the write.
2087 */
2088 barrier();
2089 if (unlikely(ACCESS_ONCE(cpu_buffer->buffer) != buffer)) {
2090 local_dec(&cpu_buffer->committing);
2091 local_dec(&cpu_buffer->commits);
2092 return NULL;
2093 }
Steven Rostedt85bac322009-09-04 14:24:40 -04002094#endif
Steven Rostedt62f0b3e2009-09-04 14:11:34 -04002095
Steven Rostedtbe957c42009-05-11 14:42:53 -04002096 length = rb_calculate_event_length(length);
Steven Rostedtbf41a152008-10-04 02:00:59 -04002097 again:
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002098 /*
2099 * We allow for interrupts to reenter here and do a trace.
2100 * If one does, it will cause this original code to loop
2101 * back here. Even with heavy interrupts happening, this
2102 * should only happen a few times in a row. If this happens
2103 * 1000 times in a row, there must be either an interrupt
2104 * storm or we have something buggy.
2105 * Bail!
2106 */
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05002107 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 1000))
Steven Rostedtfa743952009-06-16 12:37:57 -04002108 goto out_fail;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002109
Jiri Olsa6d3f1e12009-10-23 19:36:19 -04002110 ts = rb_time_stamp(cpu_buffer->buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002111
Steven Rostedtbf41a152008-10-04 02:00:59 -04002112 /*
2113 * Only the first commit can update the timestamp.
2114 * Yes there is a race here. If an interrupt comes in
2115 * just after the conditional and it traces too, then it
2116 * will also check the deltas. More than one timestamp may
2117 * also be made. But only the entry that did the actual
2118 * commit will be something other than zero.
2119 */
Steven Rostedt0f0c85f2009-05-11 16:08:00 -04002120 if (likely(cpu_buffer->tail_page == cpu_buffer->commit_page &&
2121 rb_page_write(cpu_buffer->tail_page) ==
2122 rb_commit_index(cpu_buffer))) {
Steven Rostedt168b6b12009-05-11 22:11:05 -04002123 u64 diff;
Steven Rostedtbf41a152008-10-04 02:00:59 -04002124
Steven Rostedt168b6b12009-05-11 22:11:05 -04002125 diff = ts - cpu_buffer->write_stamp;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002126
Steven Rostedt168b6b12009-05-11 22:11:05 -04002127 /* make sure this diff is calculated here */
Steven Rostedtbf41a152008-10-04 02:00:59 -04002128 barrier();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002129
Steven Rostedtbf41a152008-10-04 02:00:59 -04002130 /* Did the write stamp get updated already? */
2131 if (unlikely(ts < cpu_buffer->write_stamp))
Steven Rostedt168b6b12009-05-11 22:11:05 -04002132 goto get_event;
Steven Rostedtbf41a152008-10-04 02:00:59 -04002133
Steven Rostedt168b6b12009-05-11 22:11:05 -04002134 delta = diff;
2135 if (unlikely(test_time_stamp(delta))) {
Steven Rostedtbf41a152008-10-04 02:00:59 -04002136
2137 commit = rb_add_time_stamp(cpu_buffer, &ts, &delta);
Steven Rostedtbf41a152008-10-04 02:00:59 -04002138 if (commit == -EBUSY)
Steven Rostedtfa743952009-06-16 12:37:57 -04002139 goto out_fail;
Steven Rostedtbf41a152008-10-04 02:00:59 -04002140
2141 if (commit == -EAGAIN)
2142 goto again;
2143
2144 RB_WARN_ON(cpu_buffer, commit < 0);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002145 }
Steven Rostedt168b6b12009-05-11 22:11:05 -04002146 }
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002147
Steven Rostedt168b6b12009-05-11 22:11:05 -04002148 get_event:
Steven Rostedt1cd8d732009-05-11 14:08:09 -04002149 event = __rb_reserve_next(cpu_buffer, 0, length, &ts);
Steven Rostedt168b6b12009-05-11 22:11:05 -04002150 if (unlikely(PTR_ERR(event) == -EAGAIN))
Steven Rostedtbf41a152008-10-04 02:00:59 -04002151 goto again;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002152
Steven Rostedtfa743952009-06-16 12:37:57 -04002153 if (!event)
2154 goto out_fail;
Steven Rostedtbf41a152008-10-04 02:00:59 -04002155
Steven Rostedtfa743952009-06-16 12:37:57 -04002156 if (!rb_event_is_commit(cpu_buffer, event))
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002157 delta = 0;
2158
2159 event->time_delta = delta;
2160
2161 return event;
Steven Rostedtfa743952009-06-16 12:37:57 -04002162
2163 out_fail:
2164 rb_end_commit(cpu_buffer);
2165 return NULL;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002166}
2167
Paul Mundt1155de42009-06-25 14:30:12 +09002168#ifdef CONFIG_TRACING
2169
Steven Rostedtaa18efb2009-04-20 16:16:11 -04002170#define TRACE_RECURSIVE_DEPTH 16
Steven Rostedt261842b2009-04-16 21:41:52 -04002171
2172static int trace_recursive_lock(void)
2173{
Steven Rostedtaa18efb2009-04-20 16:16:11 -04002174 current->trace_recursion++;
Steven Rostedt261842b2009-04-16 21:41:52 -04002175
Steven Rostedtaa18efb2009-04-20 16:16:11 -04002176 if (likely(current->trace_recursion < TRACE_RECURSIVE_DEPTH))
2177 return 0;
Steven Rostedt261842b2009-04-16 21:41:52 -04002178
Steven Rostedtaa18efb2009-04-20 16:16:11 -04002179 /* Disable all tracing before we do anything else */
2180 tracing_off_permanent();
Frederic Weisbeckere057a5e2009-04-19 23:38:12 +02002181
Steven Rostedt7d7d2b82009-04-27 12:37:49 -04002182 printk_once(KERN_WARNING "Tracing recursion: depth[%ld]:"
Steven Rostedtaa18efb2009-04-20 16:16:11 -04002183 "HC[%lu]:SC[%lu]:NMI[%lu]\n",
2184 current->trace_recursion,
2185 hardirq_count() >> HARDIRQ_SHIFT,
2186 softirq_count() >> SOFTIRQ_SHIFT,
2187 in_nmi());
Frederic Weisbeckere057a5e2009-04-19 23:38:12 +02002188
Steven Rostedtaa18efb2009-04-20 16:16:11 -04002189 WARN_ON_ONCE(1);
2190 return -1;
Steven Rostedt261842b2009-04-16 21:41:52 -04002191}
2192
2193static void trace_recursive_unlock(void)
2194{
Steven Rostedtaa18efb2009-04-20 16:16:11 -04002195 WARN_ON_ONCE(!current->trace_recursion);
Steven Rostedt261842b2009-04-16 21:41:52 -04002196
Steven Rostedtaa18efb2009-04-20 16:16:11 -04002197 current->trace_recursion--;
Steven Rostedt261842b2009-04-16 21:41:52 -04002198}
2199
Paul Mundt1155de42009-06-25 14:30:12 +09002200#else
2201
2202#define trace_recursive_lock() (0)
2203#define trace_recursive_unlock() do { } while (0)
2204
2205#endif
2206
Steven Rostedtbf41a152008-10-04 02:00:59 -04002207static DEFINE_PER_CPU(int, rb_need_resched);
2208
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002209/**
2210 * ring_buffer_lock_reserve - reserve a part of the buffer
2211 * @buffer: the ring buffer to reserve from
2212 * @length: the length of the data to reserve (excluding event header)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002213 *
2214 * Returns a reseverd event on the ring buffer to copy directly to.
2215 * The user of this interface will need to get the body to write into
2216 * and can use the ring_buffer_event_data() interface.
2217 *
2218 * The length is the length of the data needed, not the event length
2219 * which also includes the event header.
2220 *
2221 * Must be paired with ring_buffer_unlock_commit, unless NULL is returned.
2222 * If NULL is returned, then nothing has been allocated or locked.
2223 */
2224struct ring_buffer_event *
Arnaldo Carvalho de Melo0a987752009-02-05 16:12:56 -02002225ring_buffer_lock_reserve(struct ring_buffer *buffer, unsigned long length)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002226{
2227 struct ring_buffer_per_cpu *cpu_buffer;
2228 struct ring_buffer_event *event;
Steven Rostedtbf41a152008-10-04 02:00:59 -04002229 int cpu, resched;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002230
Steven Rostedt033601a2008-11-21 12:41:55 -05002231 if (ring_buffer_flags != RB_BUFFERS_ON)
Steven Rostedta3583242008-11-11 15:01:42 -05002232 return NULL;
2233
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002234 if (atomic_read(&buffer->record_disabled))
2235 return NULL;
2236
Steven Rostedtbf41a152008-10-04 02:00:59 -04002237 /* If we are tracing schedule, we don't want to recurse */
Steven Rostedt182e9f52008-11-03 23:15:56 -05002238 resched = ftrace_preempt_disable();
Steven Rostedtbf41a152008-10-04 02:00:59 -04002239
Steven Rostedt261842b2009-04-16 21:41:52 -04002240 if (trace_recursive_lock())
2241 goto out_nocheck;
2242
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002243 cpu = raw_smp_processor_id();
2244
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302245 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedtd7690412008-10-01 00:29:53 -04002246 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002247
2248 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002249
2250 if (atomic_read(&cpu_buffer->record_disabled))
Steven Rostedtd7690412008-10-01 00:29:53 -04002251 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002252
Steven Rostedtbe957c42009-05-11 14:42:53 -04002253 if (length > BUF_MAX_DATA_SIZE)
Steven Rostedtbf41a152008-10-04 02:00:59 -04002254 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002255
Steven Rostedt62f0b3e2009-09-04 14:11:34 -04002256 event = rb_reserve_next_event(buffer, cpu_buffer, length);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002257 if (!event)
Steven Rostedtd7690412008-10-01 00:29:53 -04002258 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002259
Steven Rostedtbf41a152008-10-04 02:00:59 -04002260 /*
2261 * Need to store resched state on this cpu.
2262 * Only the first needs to.
2263 */
2264
2265 if (preempt_count() == 1)
2266 per_cpu(rb_need_resched, cpu) = resched;
2267
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002268 return event;
2269
Steven Rostedtd7690412008-10-01 00:29:53 -04002270 out:
Steven Rostedt261842b2009-04-16 21:41:52 -04002271 trace_recursive_unlock();
2272
2273 out_nocheck:
Steven Rostedt182e9f52008-11-03 23:15:56 -05002274 ftrace_preempt_enable(resched);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002275 return NULL;
2276}
Robert Richterc4f50182008-12-11 16:49:22 +01002277EXPORT_SYMBOL_GPL(ring_buffer_lock_reserve);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002278
Steven Rostedta1863c22009-09-03 10:23:58 -04002279static void
2280rb_update_write_stamp(struct ring_buffer_per_cpu *cpu_buffer,
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002281 struct ring_buffer_event *event)
2282{
Steven Rostedtfa743952009-06-16 12:37:57 -04002283 /*
2284 * The event first in the commit queue updates the
2285 * time stamp.
2286 */
2287 if (rb_event_is_commit(cpu_buffer, event))
2288 cpu_buffer->write_stamp += event->time_delta;
Steven Rostedta1863c22009-09-03 10:23:58 -04002289}
Steven Rostedtbf41a152008-10-04 02:00:59 -04002290
Steven Rostedta1863c22009-09-03 10:23:58 -04002291static void rb_commit(struct ring_buffer_per_cpu *cpu_buffer,
2292 struct ring_buffer_event *event)
2293{
2294 local_inc(&cpu_buffer->entries);
2295 rb_update_write_stamp(cpu_buffer, event);
Steven Rostedtfa743952009-06-16 12:37:57 -04002296 rb_end_commit(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002297}
2298
2299/**
2300 * ring_buffer_unlock_commit - commit a reserved
2301 * @buffer: The buffer to commit to
2302 * @event: The event pointer to commit.
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002303 *
2304 * This commits the data to the ring buffer, and releases any locks held.
2305 *
2306 * Must be paired with ring_buffer_lock_reserve.
2307 */
2308int ring_buffer_unlock_commit(struct ring_buffer *buffer,
Arnaldo Carvalho de Melo0a987752009-02-05 16:12:56 -02002309 struct ring_buffer_event *event)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002310{
2311 struct ring_buffer_per_cpu *cpu_buffer;
2312 int cpu = raw_smp_processor_id();
2313
2314 cpu_buffer = buffer->buffers[cpu];
2315
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002316 rb_commit(cpu_buffer, event);
2317
Steven Rostedt261842b2009-04-16 21:41:52 -04002318 trace_recursive_unlock();
2319
Steven Rostedtbf41a152008-10-04 02:00:59 -04002320 /*
2321 * Only the last preempt count needs to restore preemption.
2322 */
Steven Rostedt182e9f52008-11-03 23:15:56 -05002323 if (preempt_count() == 1)
2324 ftrace_preempt_enable(per_cpu(rb_need_resched, cpu));
2325 else
Steven Rostedtbf41a152008-10-04 02:00:59 -04002326 preempt_enable_no_resched_notrace();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002327
2328 return 0;
2329}
Robert Richterc4f50182008-12-11 16:49:22 +01002330EXPORT_SYMBOL_GPL(ring_buffer_unlock_commit);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002331
Frederic Weisbeckerf3b9aae2009-04-19 23:39:33 +02002332static inline void rb_event_discard(struct ring_buffer_event *event)
2333{
Lai Jiangshan334d4162009-04-24 11:27:05 +08002334 /* array[0] holds the actual length for the discarded event */
2335 event->array[0] = rb_event_data_length(event) - RB_EVNT_HDR_SIZE;
2336 event->type_len = RINGBUF_TYPE_PADDING;
Frederic Weisbeckerf3b9aae2009-04-19 23:39:33 +02002337 /* time delta must be non zero */
2338 if (!event->time_delta)
2339 event->time_delta = 1;
2340}
2341
Steven Rostedta1863c22009-09-03 10:23:58 -04002342/*
2343 * Decrement the entries to the page that an event is on.
2344 * The event does not even need to exist, only the pointer
2345 * to the page it is on. This may only be called before the commit
2346 * takes place.
2347 */
2348static inline void
2349rb_decrement_entry(struct ring_buffer_per_cpu *cpu_buffer,
2350 struct ring_buffer_event *event)
2351{
2352 unsigned long addr = (unsigned long)event;
2353 struct buffer_page *bpage = cpu_buffer->commit_page;
2354 struct buffer_page *start;
2355
2356 addr &= PAGE_MASK;
2357
2358 /* Do the likely case first */
2359 if (likely(bpage->page == (void *)addr)) {
2360 local_dec(&bpage->entries);
2361 return;
2362 }
2363
2364 /*
2365 * Because the commit page may be on the reader page we
2366 * start with the next page and check the end loop there.
2367 */
2368 rb_inc_page(cpu_buffer, &bpage);
2369 start = bpage;
2370 do {
2371 if (bpage->page == (void *)addr) {
2372 local_dec(&bpage->entries);
2373 return;
2374 }
2375 rb_inc_page(cpu_buffer, &bpage);
2376 } while (bpage != start);
2377
2378 /* commit not part of this buffer?? */
2379 RB_WARN_ON(cpu_buffer, 1);
2380}
2381
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002382/**
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002383 * ring_buffer_commit_discard - discard an event that has not been committed
2384 * @buffer: the ring buffer
2385 * @event: non committed event to discard
2386 *
Steven Rostedtdc892f72009-09-03 15:33:41 -04002387 * Sometimes an event that is in the ring buffer needs to be ignored.
2388 * This function lets the user discard an event in the ring buffer
2389 * and then that event will not be read later.
2390 *
2391 * This function only works if it is called before the the item has been
2392 * committed. It will try to free the event from the ring buffer
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002393 * if another event has not been added behind it.
2394 *
2395 * If another event has been added behind it, it will set the event
2396 * up as discarded, and perform the commit.
2397 *
2398 * If this function is called, do not call ring_buffer_unlock_commit on
2399 * the event.
2400 */
2401void ring_buffer_discard_commit(struct ring_buffer *buffer,
2402 struct ring_buffer_event *event)
2403{
2404 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002405 int cpu;
2406
2407 /* The event is discarded regardless */
Frederic Weisbeckerf3b9aae2009-04-19 23:39:33 +02002408 rb_event_discard(event);
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002409
Steven Rostedtfa743952009-06-16 12:37:57 -04002410 cpu = smp_processor_id();
2411 cpu_buffer = buffer->buffers[cpu];
2412
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002413 /*
2414 * This must only be called if the event has not been
2415 * committed yet. Thus we can assume that preemption
2416 * is still disabled.
2417 */
Steven Rostedtfa743952009-06-16 12:37:57 -04002418 RB_WARN_ON(buffer, !local_read(&cpu_buffer->committing));
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002419
Steven Rostedta1863c22009-09-03 10:23:58 -04002420 rb_decrement_entry(cpu_buffer, event);
Steven Rostedt0f2541d2009-08-05 12:02:48 -04002421 if (rb_try_to_discard(cpu_buffer, event))
Steven Rostedtedd813bf2009-06-02 23:00:53 -04002422 goto out;
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002423
2424 /*
2425 * The commit is still visible by the reader, so we
Steven Rostedta1863c22009-09-03 10:23:58 -04002426 * must still update the timestamp.
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002427 */
Steven Rostedta1863c22009-09-03 10:23:58 -04002428 rb_update_write_stamp(cpu_buffer, event);
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002429 out:
Steven Rostedtfa743952009-06-16 12:37:57 -04002430 rb_end_commit(cpu_buffer);
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002431
Frederic Weisbeckerf3b9aae2009-04-19 23:39:33 +02002432 trace_recursive_unlock();
2433
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002434 /*
2435 * Only the last preempt count needs to restore preemption.
2436 */
2437 if (preempt_count() == 1)
2438 ftrace_preempt_enable(per_cpu(rb_need_resched, cpu));
2439 else
2440 preempt_enable_no_resched_notrace();
2441
2442}
2443EXPORT_SYMBOL_GPL(ring_buffer_discard_commit);
2444
2445/**
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002446 * ring_buffer_write - write data to the buffer without reserving
2447 * @buffer: The ring buffer to write to.
2448 * @length: The length of the data being written (excluding the event header)
2449 * @data: The data to write to the buffer.
2450 *
2451 * This is like ring_buffer_lock_reserve and ring_buffer_unlock_commit as
2452 * one function. If you already have the data to write to the buffer, it
2453 * may be easier to simply call this function.
2454 *
2455 * Note, like ring_buffer_lock_reserve, the length is the length of the data
2456 * and not the length of the event which would hold the header.
2457 */
2458int ring_buffer_write(struct ring_buffer *buffer,
2459 unsigned long length,
2460 void *data)
2461{
2462 struct ring_buffer_per_cpu *cpu_buffer;
2463 struct ring_buffer_event *event;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002464 void *body;
2465 int ret = -EBUSY;
Steven Rostedtbf41a152008-10-04 02:00:59 -04002466 int cpu, resched;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002467
Steven Rostedt033601a2008-11-21 12:41:55 -05002468 if (ring_buffer_flags != RB_BUFFERS_ON)
Steven Rostedta3583242008-11-11 15:01:42 -05002469 return -EBUSY;
2470
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002471 if (atomic_read(&buffer->record_disabled))
2472 return -EBUSY;
2473
Steven Rostedt182e9f52008-11-03 23:15:56 -05002474 resched = ftrace_preempt_disable();
Steven Rostedtbf41a152008-10-04 02:00:59 -04002475
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002476 cpu = raw_smp_processor_id();
2477
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302478 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedtd7690412008-10-01 00:29:53 -04002479 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002480
2481 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002482
2483 if (atomic_read(&cpu_buffer->record_disabled))
2484 goto out;
2485
Steven Rostedtbe957c42009-05-11 14:42:53 -04002486 if (length > BUF_MAX_DATA_SIZE)
2487 goto out;
2488
Steven Rostedt62f0b3e2009-09-04 14:11:34 -04002489 event = rb_reserve_next_event(buffer, cpu_buffer, length);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002490 if (!event)
2491 goto out;
2492
2493 body = rb_event_data(event);
2494
2495 memcpy(body, data, length);
2496
2497 rb_commit(cpu_buffer, event);
2498
2499 ret = 0;
2500 out:
Steven Rostedt182e9f52008-11-03 23:15:56 -05002501 ftrace_preempt_enable(resched);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002502
2503 return ret;
2504}
Robert Richterc4f50182008-12-11 16:49:22 +01002505EXPORT_SYMBOL_GPL(ring_buffer_write);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002506
Andrew Morton34a148b2009-01-09 12:27:09 -08002507static int rb_per_cpu_empty(struct ring_buffer_per_cpu *cpu_buffer)
Steven Rostedtbf41a152008-10-04 02:00:59 -04002508{
2509 struct buffer_page *reader = cpu_buffer->reader_page;
Steven Rostedt77ae3652009-03-27 11:00:29 -04002510 struct buffer_page *head = rb_set_head_page(cpu_buffer);
Steven Rostedtbf41a152008-10-04 02:00:59 -04002511 struct buffer_page *commit = cpu_buffer->commit_page;
2512
Steven Rostedt77ae3652009-03-27 11:00:29 -04002513 /* In case of error, head will be NULL */
2514 if (unlikely(!head))
2515 return 1;
2516
Steven Rostedtbf41a152008-10-04 02:00:59 -04002517 return reader->read == rb_page_commit(reader) &&
2518 (commit == reader ||
2519 (commit == head &&
2520 head->read == rb_page_commit(commit)));
2521}
2522
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002523/**
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002524 * ring_buffer_record_disable - stop all writes into the buffer
2525 * @buffer: The ring buffer to stop writes to.
2526 *
2527 * This prevents all writes to the buffer. Any attempt to write
2528 * to the buffer after this will fail and return NULL.
2529 *
2530 * The caller should call synchronize_sched() after this.
2531 */
2532void ring_buffer_record_disable(struct ring_buffer *buffer)
2533{
2534 atomic_inc(&buffer->record_disabled);
2535}
Robert Richterc4f50182008-12-11 16:49:22 +01002536EXPORT_SYMBOL_GPL(ring_buffer_record_disable);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002537
2538/**
2539 * ring_buffer_record_enable - enable writes to the buffer
2540 * @buffer: The ring buffer to enable writes
2541 *
2542 * Note, multiple disables will need the same number of enables
2543 * to truely enable the writing (much like preempt_disable).
2544 */
2545void ring_buffer_record_enable(struct ring_buffer *buffer)
2546{
2547 atomic_dec(&buffer->record_disabled);
2548}
Robert Richterc4f50182008-12-11 16:49:22 +01002549EXPORT_SYMBOL_GPL(ring_buffer_record_enable);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002550
2551/**
2552 * ring_buffer_record_disable_cpu - stop all writes into the cpu_buffer
2553 * @buffer: The ring buffer to stop writes to.
2554 * @cpu: The CPU buffer to stop
2555 *
2556 * This prevents all writes to the buffer. Any attempt to write
2557 * to the buffer after this will fail and return NULL.
2558 *
2559 * The caller should call synchronize_sched() after this.
2560 */
2561void ring_buffer_record_disable_cpu(struct ring_buffer *buffer, int cpu)
2562{
2563 struct ring_buffer_per_cpu *cpu_buffer;
2564
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302565 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04002566 return;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002567
2568 cpu_buffer = buffer->buffers[cpu];
2569 atomic_inc(&cpu_buffer->record_disabled);
2570}
Robert Richterc4f50182008-12-11 16:49:22 +01002571EXPORT_SYMBOL_GPL(ring_buffer_record_disable_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002572
2573/**
2574 * ring_buffer_record_enable_cpu - enable writes to the buffer
2575 * @buffer: The ring buffer to enable writes
2576 * @cpu: The CPU to enable.
2577 *
2578 * Note, multiple disables will need the same number of enables
2579 * to truely enable the writing (much like preempt_disable).
2580 */
2581void ring_buffer_record_enable_cpu(struct ring_buffer *buffer, int cpu)
2582{
2583 struct ring_buffer_per_cpu *cpu_buffer;
2584
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302585 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04002586 return;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002587
2588 cpu_buffer = buffer->buffers[cpu];
2589 atomic_dec(&cpu_buffer->record_disabled);
2590}
Robert Richterc4f50182008-12-11 16:49:22 +01002591EXPORT_SYMBOL_GPL(ring_buffer_record_enable_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002592
2593/**
2594 * ring_buffer_entries_cpu - get the number of entries in a cpu buffer
2595 * @buffer: The ring buffer
2596 * @cpu: The per CPU buffer to get the entries from.
2597 */
2598unsigned long ring_buffer_entries_cpu(struct ring_buffer *buffer, int cpu)
2599{
2600 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedt8aabee52009-03-12 13:13:49 -04002601 unsigned long ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002602
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302603 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04002604 return 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002605
2606 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt77ae3652009-03-27 11:00:29 -04002607 ret = (local_read(&cpu_buffer->entries) - local_read(&cpu_buffer->overrun))
Steven Rostedte4906ef2009-04-30 20:49:44 -04002608 - cpu_buffer->read;
Steven Rostedt554f7862009-03-11 22:00:13 -04002609
2610 return ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002611}
Robert Richterc4f50182008-12-11 16:49:22 +01002612EXPORT_SYMBOL_GPL(ring_buffer_entries_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002613
2614/**
2615 * ring_buffer_overrun_cpu - get the number of overruns in a cpu_buffer
2616 * @buffer: The ring buffer
2617 * @cpu: The per CPU buffer to get the number of overruns from
2618 */
2619unsigned long ring_buffer_overrun_cpu(struct ring_buffer *buffer, int cpu)
2620{
2621 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedt8aabee52009-03-12 13:13:49 -04002622 unsigned long ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002623
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302624 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04002625 return 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002626
2627 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt77ae3652009-03-27 11:00:29 -04002628 ret = local_read(&cpu_buffer->overrun);
Steven Rostedt554f7862009-03-11 22:00:13 -04002629
2630 return ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002631}
Robert Richterc4f50182008-12-11 16:49:22 +01002632EXPORT_SYMBOL_GPL(ring_buffer_overrun_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002633
2634/**
Steven Rostedtf0d2c682009-04-29 13:43:37 -04002635 * ring_buffer_commit_overrun_cpu - get the number of overruns caused by commits
2636 * @buffer: The ring buffer
2637 * @cpu: The per CPU buffer to get the number of overruns from
2638 */
2639unsigned long
2640ring_buffer_commit_overrun_cpu(struct ring_buffer *buffer, int cpu)
2641{
2642 struct ring_buffer_per_cpu *cpu_buffer;
2643 unsigned long ret;
2644
2645 if (!cpumask_test_cpu(cpu, buffer->cpumask))
2646 return 0;
2647
2648 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt77ae3652009-03-27 11:00:29 -04002649 ret = local_read(&cpu_buffer->commit_overrun);
Steven Rostedtf0d2c682009-04-29 13:43:37 -04002650
2651 return ret;
2652}
2653EXPORT_SYMBOL_GPL(ring_buffer_commit_overrun_cpu);
2654
2655/**
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002656 * ring_buffer_entries - get the number of entries in a buffer
2657 * @buffer: The ring buffer
2658 *
2659 * Returns the total number of entries in the ring buffer
2660 * (all CPU entries)
2661 */
2662unsigned long ring_buffer_entries(struct ring_buffer *buffer)
2663{
2664 struct ring_buffer_per_cpu *cpu_buffer;
2665 unsigned long entries = 0;
2666 int cpu;
2667
2668 /* if you care about this being correct, lock the buffer */
2669 for_each_buffer_cpu(buffer, cpu) {
2670 cpu_buffer = buffer->buffers[cpu];
Steven Rostedte4906ef2009-04-30 20:49:44 -04002671 entries += (local_read(&cpu_buffer->entries) -
Steven Rostedt77ae3652009-03-27 11:00:29 -04002672 local_read(&cpu_buffer->overrun)) - cpu_buffer->read;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002673 }
2674
2675 return entries;
2676}
Robert Richterc4f50182008-12-11 16:49:22 +01002677EXPORT_SYMBOL_GPL(ring_buffer_entries);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002678
2679/**
Jiri Olsa67b394f2009-10-23 19:36:18 -04002680 * ring_buffer_overruns - get the number of overruns in buffer
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002681 * @buffer: The ring buffer
2682 *
2683 * Returns the total number of overruns in the ring buffer
2684 * (all CPU entries)
2685 */
2686unsigned long ring_buffer_overruns(struct ring_buffer *buffer)
2687{
2688 struct ring_buffer_per_cpu *cpu_buffer;
2689 unsigned long overruns = 0;
2690 int cpu;
2691
2692 /* if you care about this being correct, lock the buffer */
2693 for_each_buffer_cpu(buffer, cpu) {
2694 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt77ae3652009-03-27 11:00:29 -04002695 overruns += local_read(&cpu_buffer->overrun);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002696 }
2697
2698 return overruns;
2699}
Robert Richterc4f50182008-12-11 16:49:22 +01002700EXPORT_SYMBOL_GPL(ring_buffer_overruns);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002701
Steven Rostedt642edba2008-11-12 00:01:26 -05002702static void rb_iter_reset(struct ring_buffer_iter *iter)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002703{
2704 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
2705
Steven Rostedtd7690412008-10-01 00:29:53 -04002706 /* Iterator usage is expected to have record disabled */
2707 if (list_empty(&cpu_buffer->reader_page->list)) {
Steven Rostedt77ae3652009-03-27 11:00:29 -04002708 iter->head_page = rb_set_head_page(cpu_buffer);
2709 if (unlikely(!iter->head_page))
2710 return;
2711 iter->head = iter->head_page->read;
Steven Rostedtd7690412008-10-01 00:29:53 -04002712 } else {
2713 iter->head_page = cpu_buffer->reader_page;
Steven Rostedt6f807ac2008-10-04 02:00:58 -04002714 iter->head = cpu_buffer->reader_page->read;
Steven Rostedtd7690412008-10-01 00:29:53 -04002715 }
2716 if (iter->head)
2717 iter->read_stamp = cpu_buffer->read_stamp;
2718 else
Steven Rostedtabc9b562008-12-02 15:34:06 -05002719 iter->read_stamp = iter->head_page->page->time_stamp;
Steven Rostedt642edba2008-11-12 00:01:26 -05002720}
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002721
Steven Rostedt642edba2008-11-12 00:01:26 -05002722/**
2723 * ring_buffer_iter_reset - reset an iterator
2724 * @iter: The iterator to reset
2725 *
2726 * Resets the iterator, so that it will start from the beginning
2727 * again.
2728 */
2729void ring_buffer_iter_reset(struct ring_buffer_iter *iter)
2730{
Steven Rostedt554f7862009-03-11 22:00:13 -04002731 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedt642edba2008-11-12 00:01:26 -05002732 unsigned long flags;
2733
Steven Rostedt554f7862009-03-11 22:00:13 -04002734 if (!iter)
2735 return;
2736
2737 cpu_buffer = iter->cpu_buffer;
2738
Steven Rostedt642edba2008-11-12 00:01:26 -05002739 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
2740 rb_iter_reset(iter);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002741 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002742}
Robert Richterc4f50182008-12-11 16:49:22 +01002743EXPORT_SYMBOL_GPL(ring_buffer_iter_reset);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002744
2745/**
2746 * ring_buffer_iter_empty - check if an iterator has no more to read
2747 * @iter: The iterator to check
2748 */
2749int ring_buffer_iter_empty(struct ring_buffer_iter *iter)
2750{
2751 struct ring_buffer_per_cpu *cpu_buffer;
2752
2753 cpu_buffer = iter->cpu_buffer;
2754
Steven Rostedtbf41a152008-10-04 02:00:59 -04002755 return iter->head_page == cpu_buffer->commit_page &&
2756 iter->head == rb_commit_index(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002757}
Robert Richterc4f50182008-12-11 16:49:22 +01002758EXPORT_SYMBOL_GPL(ring_buffer_iter_empty);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002759
2760static void
2761rb_update_read_stamp(struct ring_buffer_per_cpu *cpu_buffer,
2762 struct ring_buffer_event *event)
2763{
2764 u64 delta;
2765
Lai Jiangshan334d4162009-04-24 11:27:05 +08002766 switch (event->type_len) {
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002767 case RINGBUF_TYPE_PADDING:
2768 return;
2769
2770 case RINGBUF_TYPE_TIME_EXTEND:
2771 delta = event->array[0];
2772 delta <<= TS_SHIFT;
2773 delta += event->time_delta;
2774 cpu_buffer->read_stamp += delta;
2775 return;
2776
2777 case RINGBUF_TYPE_TIME_STAMP:
2778 /* FIXME: not implemented */
2779 return;
2780
2781 case RINGBUF_TYPE_DATA:
2782 cpu_buffer->read_stamp += event->time_delta;
2783 return;
2784
2785 default:
2786 BUG();
2787 }
2788 return;
2789}
2790
2791static void
2792rb_update_iter_read_stamp(struct ring_buffer_iter *iter,
2793 struct ring_buffer_event *event)
2794{
2795 u64 delta;
2796
Lai Jiangshan334d4162009-04-24 11:27:05 +08002797 switch (event->type_len) {
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002798 case RINGBUF_TYPE_PADDING:
2799 return;
2800
2801 case RINGBUF_TYPE_TIME_EXTEND:
2802 delta = event->array[0];
2803 delta <<= TS_SHIFT;
2804 delta += event->time_delta;
2805 iter->read_stamp += delta;
2806 return;
2807
2808 case RINGBUF_TYPE_TIME_STAMP:
2809 /* FIXME: not implemented */
2810 return;
2811
2812 case RINGBUF_TYPE_DATA:
2813 iter->read_stamp += event->time_delta;
2814 return;
2815
2816 default:
2817 BUG();
2818 }
2819 return;
2820}
2821
Steven Rostedtd7690412008-10-01 00:29:53 -04002822static struct buffer_page *
2823rb_get_reader_page(struct ring_buffer_per_cpu *cpu_buffer)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002824{
Steven Rostedtd7690412008-10-01 00:29:53 -04002825 struct buffer_page *reader = NULL;
2826 unsigned long flags;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002827 int nr_loops = 0;
Steven Rostedt77ae3652009-03-27 11:00:29 -04002828 int ret;
Steven Rostedtd7690412008-10-01 00:29:53 -04002829
Steven Rostedt3e03fb72008-11-06 00:09:43 -05002830 local_irq_save(flags);
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01002831 arch_spin_lock(&cpu_buffer->lock);
Steven Rostedtd7690412008-10-01 00:29:53 -04002832
2833 again:
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002834 /*
2835 * This should normally only loop twice. But because the
2836 * start of the reader inserts an empty page, it causes
2837 * a case where we will loop three times. There should be no
2838 * reason to loop four times (that I know of).
2839 */
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05002840 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 3)) {
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002841 reader = NULL;
2842 goto out;
2843 }
2844
Steven Rostedtd7690412008-10-01 00:29:53 -04002845 reader = cpu_buffer->reader_page;
2846
2847 /* If there's more to read, return this page */
Steven Rostedtbf41a152008-10-04 02:00:59 -04002848 if (cpu_buffer->reader_page->read < rb_page_size(reader))
Steven Rostedtd7690412008-10-01 00:29:53 -04002849 goto out;
2850
2851 /* Never should we have an index greater than the size */
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05002852 if (RB_WARN_ON(cpu_buffer,
2853 cpu_buffer->reader_page->read > rb_page_size(reader)))
2854 goto out;
Steven Rostedtd7690412008-10-01 00:29:53 -04002855
2856 /* check if we caught up to the tail */
2857 reader = NULL;
Steven Rostedtbf41a152008-10-04 02:00:59 -04002858 if (cpu_buffer->commit_page == cpu_buffer->reader_page)
Steven Rostedtd7690412008-10-01 00:29:53 -04002859 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002860
2861 /*
Steven Rostedtd7690412008-10-01 00:29:53 -04002862 * Reset the reader page to size zero.
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002863 */
Steven Rostedt77ae3652009-03-27 11:00:29 -04002864 local_set(&cpu_buffer->reader_page->write, 0);
2865 local_set(&cpu_buffer->reader_page->entries, 0);
2866 local_set(&cpu_buffer->reader_page->page->commit, 0);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002867
Steven Rostedt77ae3652009-03-27 11:00:29 -04002868 spin:
2869 /*
2870 * Splice the empty reader page into the list around the head.
2871 */
2872 reader = rb_set_head_page(cpu_buffer);
Steven Rostedtd7690412008-10-01 00:29:53 -04002873 cpu_buffer->reader_page->list.next = reader->list.next;
2874 cpu_buffer->reader_page->list.prev = reader->list.prev;
Steven Rostedtbf41a152008-10-04 02:00:59 -04002875
Steven Rostedt3adc54f2009-03-30 15:32:01 -04002876 /*
2877 * cpu_buffer->pages just needs to point to the buffer, it
2878 * has no specific buffer page to point to. Lets move it out
2879 * of our way so we don't accidently swap it.
2880 */
2881 cpu_buffer->pages = reader->list.prev;
2882
Steven Rostedt77ae3652009-03-27 11:00:29 -04002883 /* The reader page will be pointing to the new head */
2884 rb_set_list_to_head(cpu_buffer, &cpu_buffer->reader_page->list);
Steven Rostedtd7690412008-10-01 00:29:53 -04002885
2886 /*
Steven Rostedt77ae3652009-03-27 11:00:29 -04002887 * Here's the tricky part.
2888 *
2889 * We need to move the pointer past the header page.
2890 * But we can only do that if a writer is not currently
2891 * moving it. The page before the header page has the
2892 * flag bit '1' set if it is pointing to the page we want.
2893 * but if the writer is in the process of moving it
2894 * than it will be '2' or already moved '0'.
Steven Rostedtd7690412008-10-01 00:29:53 -04002895 */
Steven Rostedtd7690412008-10-01 00:29:53 -04002896
Steven Rostedt77ae3652009-03-27 11:00:29 -04002897 ret = rb_head_page_replace(reader, cpu_buffer->reader_page);
2898
2899 /*
2900 * If we did not convert it, then we must try again.
2901 */
2902 if (!ret)
2903 goto spin;
2904
2905 /*
2906 * Yeah! We succeeded in replacing the page.
2907 *
2908 * Now make the new head point back to the reader page.
2909 */
2910 reader->list.next->prev = &cpu_buffer->reader_page->list;
2911 rb_inc_page(cpu_buffer, &cpu_buffer->head_page);
Steven Rostedtd7690412008-10-01 00:29:53 -04002912
2913 /* Finally update the reader page to the new head */
2914 cpu_buffer->reader_page = reader;
2915 rb_reset_reader_page(cpu_buffer);
2916
2917 goto again;
2918
2919 out:
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01002920 arch_spin_unlock(&cpu_buffer->lock);
Steven Rostedt3e03fb72008-11-06 00:09:43 -05002921 local_irq_restore(flags);
Steven Rostedtd7690412008-10-01 00:29:53 -04002922
2923 return reader;
2924}
2925
2926static void rb_advance_reader(struct ring_buffer_per_cpu *cpu_buffer)
2927{
2928 struct ring_buffer_event *event;
2929 struct buffer_page *reader;
2930 unsigned length;
2931
2932 reader = rb_get_reader_page(cpu_buffer);
2933
2934 /* This function should not be called when buffer is empty */
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05002935 if (RB_WARN_ON(cpu_buffer, !reader))
2936 return;
Steven Rostedtd7690412008-10-01 00:29:53 -04002937
2938 event = rb_reader_event(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002939
Steven Rostedta1863c22009-09-03 10:23:58 -04002940 if (event->type_len <= RINGBUF_TYPE_DATA_TYPE_LEN_MAX)
Steven Rostedte4906ef2009-04-30 20:49:44 -04002941 cpu_buffer->read++;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002942
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002943 rb_update_read_stamp(cpu_buffer, event);
2944
Steven Rostedtd7690412008-10-01 00:29:53 -04002945 length = rb_event_length(event);
Steven Rostedt6f807ac2008-10-04 02:00:58 -04002946 cpu_buffer->reader_page->read += length;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002947}
2948
2949static void rb_advance_iter(struct ring_buffer_iter *iter)
2950{
2951 struct ring_buffer *buffer;
2952 struct ring_buffer_per_cpu *cpu_buffer;
2953 struct ring_buffer_event *event;
2954 unsigned length;
2955
2956 cpu_buffer = iter->cpu_buffer;
2957 buffer = cpu_buffer->buffer;
2958
2959 /*
2960 * Check if we are at the end of the buffer.
2961 */
Steven Rostedtbf41a152008-10-04 02:00:59 -04002962 if (iter->head >= rb_page_size(iter->head_page)) {
Steven Rostedtea05b572009-06-03 09:30:10 -04002963 /* discarded commits can make the page empty */
2964 if (iter->head_page == cpu_buffer->commit_page)
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05002965 return;
Steven Rostedtd7690412008-10-01 00:29:53 -04002966 rb_inc_iter(iter);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002967 return;
2968 }
2969
2970 event = rb_iter_head_event(iter);
2971
2972 length = rb_event_length(event);
2973
2974 /*
2975 * This should not be called to advance the header if we are
2976 * at the tail of the buffer.
2977 */
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05002978 if (RB_WARN_ON(cpu_buffer,
Steven Rostedtf536aaf2008-11-10 23:07:30 -05002979 (iter->head_page == cpu_buffer->commit_page) &&
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05002980 (iter->head + length > rb_commit_index(cpu_buffer))))
2981 return;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002982
2983 rb_update_iter_read_stamp(iter, event);
2984
2985 iter->head += length;
2986
2987 /* check for end of page padding */
Steven Rostedtbf41a152008-10-04 02:00:59 -04002988 if ((iter->head >= rb_page_size(iter->head_page)) &&
2989 (iter->head_page != cpu_buffer->commit_page))
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002990 rb_advance_iter(iter);
2991}
2992
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002993static struct ring_buffer_event *
Robert Richterd8eeb2d2009-07-31 14:58:04 +02002994rb_buffer_peek(struct ring_buffer_per_cpu *cpu_buffer, u64 *ts)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002995{
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002996 struct ring_buffer_event *event;
Steven Rostedtd7690412008-10-01 00:29:53 -04002997 struct buffer_page *reader;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002998 int nr_loops = 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002999
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003000 again:
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003001 /*
3002 * We repeat when a timestamp is encountered. It is possible
3003 * to get multiple timestamps from an interrupt entering just
Steven Rostedtea05b572009-06-03 09:30:10 -04003004 * as one timestamp is about to be written, or from discarded
3005 * commits. The most that we can have is the number on a single page.
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003006 */
Steven Rostedtea05b572009-06-03 09:30:10 -04003007 if (RB_WARN_ON(cpu_buffer, ++nr_loops > RB_TIMESTAMPS_PER_PAGE))
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003008 return NULL;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003009
Steven Rostedtd7690412008-10-01 00:29:53 -04003010 reader = rb_get_reader_page(cpu_buffer);
3011 if (!reader)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003012 return NULL;
3013
Steven Rostedtd7690412008-10-01 00:29:53 -04003014 event = rb_reader_event(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003015
Lai Jiangshan334d4162009-04-24 11:27:05 +08003016 switch (event->type_len) {
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003017 case RINGBUF_TYPE_PADDING:
Tom Zanussi2d622712009-03-22 03:30:49 -05003018 if (rb_null_event(event))
3019 RB_WARN_ON(cpu_buffer, 1);
3020 /*
3021 * Because the writer could be discarding every
3022 * event it creates (which would probably be bad)
3023 * if we were to go back to "again" then we may never
3024 * catch up, and will trigger the warn on, or lock
3025 * the box. Return the padding, and we will release
3026 * the current locks, and try again.
3027 */
Tom Zanussi2d622712009-03-22 03:30:49 -05003028 return event;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003029
3030 case RINGBUF_TYPE_TIME_EXTEND:
3031 /* Internal data, OK to advance */
Steven Rostedtd7690412008-10-01 00:29:53 -04003032 rb_advance_reader(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003033 goto again;
3034
3035 case RINGBUF_TYPE_TIME_STAMP:
3036 /* FIXME: not implemented */
Steven Rostedtd7690412008-10-01 00:29:53 -04003037 rb_advance_reader(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003038 goto again;
3039
3040 case RINGBUF_TYPE_DATA:
3041 if (ts) {
3042 *ts = cpu_buffer->read_stamp + event->time_delta;
Robert Richterd8eeb2d2009-07-31 14:58:04 +02003043 ring_buffer_normalize_time_stamp(cpu_buffer->buffer,
Steven Rostedt37886f62009-03-17 17:22:06 -04003044 cpu_buffer->cpu, ts);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003045 }
3046 return event;
3047
3048 default:
3049 BUG();
3050 }
3051
3052 return NULL;
3053}
Robert Richterc4f50182008-12-11 16:49:22 +01003054EXPORT_SYMBOL_GPL(ring_buffer_peek);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003055
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003056static struct ring_buffer_event *
3057rb_iter_peek(struct ring_buffer_iter *iter, u64 *ts)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003058{
3059 struct ring_buffer *buffer;
3060 struct ring_buffer_per_cpu *cpu_buffer;
3061 struct ring_buffer_event *event;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003062 int nr_loops = 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003063
3064 if (ring_buffer_iter_empty(iter))
3065 return NULL;
3066
3067 cpu_buffer = iter->cpu_buffer;
3068 buffer = cpu_buffer->buffer;
3069
3070 again:
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003071 /*
Steven Rostedtea05b572009-06-03 09:30:10 -04003072 * We repeat when a timestamp is encountered.
3073 * We can get multiple timestamps by nested interrupts or also
3074 * if filtering is on (discarding commits). Since discarding
3075 * commits can be frequent we can get a lot of timestamps.
3076 * But we limit them by not adding timestamps if they begin
3077 * at the start of a page.
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003078 */
Steven Rostedtea05b572009-06-03 09:30:10 -04003079 if (RB_WARN_ON(cpu_buffer, ++nr_loops > RB_TIMESTAMPS_PER_PAGE))
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003080 return NULL;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003081
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003082 if (rb_per_cpu_empty(cpu_buffer))
3083 return NULL;
3084
3085 event = rb_iter_head_event(iter);
3086
Lai Jiangshan334d4162009-04-24 11:27:05 +08003087 switch (event->type_len) {
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003088 case RINGBUF_TYPE_PADDING:
Tom Zanussi2d622712009-03-22 03:30:49 -05003089 if (rb_null_event(event)) {
3090 rb_inc_iter(iter);
3091 goto again;
3092 }
3093 rb_advance_iter(iter);
3094 return event;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003095
3096 case RINGBUF_TYPE_TIME_EXTEND:
3097 /* Internal data, OK to advance */
3098 rb_advance_iter(iter);
3099 goto again;
3100
3101 case RINGBUF_TYPE_TIME_STAMP:
3102 /* FIXME: not implemented */
3103 rb_advance_iter(iter);
3104 goto again;
3105
3106 case RINGBUF_TYPE_DATA:
3107 if (ts) {
3108 *ts = iter->read_stamp + event->time_delta;
Steven Rostedt37886f62009-03-17 17:22:06 -04003109 ring_buffer_normalize_time_stamp(buffer,
3110 cpu_buffer->cpu, ts);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003111 }
3112 return event;
3113
3114 default:
3115 BUG();
3116 }
3117
3118 return NULL;
3119}
Robert Richterc4f50182008-12-11 16:49:22 +01003120EXPORT_SYMBOL_GPL(ring_buffer_iter_peek);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003121
Steven Rostedt8d707e82009-06-16 21:22:48 -04003122static inline int rb_ok_to_lock(void)
3123{
3124 /*
3125 * If an NMI die dumps out the content of the ring buffer
3126 * do not grab locks. We also permanently disable the ring
3127 * buffer too. A one time deal is all you get from reading
3128 * the ring buffer from an NMI.
3129 */
Steven Rostedt464e85e2009-08-05 15:26:37 -04003130 if (likely(!in_nmi()))
Steven Rostedt8d707e82009-06-16 21:22:48 -04003131 return 1;
3132
3133 tracing_off_permanent();
3134 return 0;
3135}
3136
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003137/**
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003138 * ring_buffer_peek - peek at the next event to be read
3139 * @buffer: The ring buffer to read
3140 * @cpu: The cpu to peak at
3141 * @ts: The timestamp counter of this event.
3142 *
3143 * This will return the event that will be read next, but does
3144 * not consume the data.
3145 */
3146struct ring_buffer_event *
3147ring_buffer_peek(struct ring_buffer *buffer, int cpu, u64 *ts)
3148{
3149 struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
Steven Rostedt8aabee52009-03-12 13:13:49 -04003150 struct ring_buffer_event *event;
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003151 unsigned long flags;
Steven Rostedt8d707e82009-06-16 21:22:48 -04003152 int dolock;
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003153
Steven Rostedt554f7862009-03-11 22:00:13 -04003154 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04003155 return NULL;
Steven Rostedt554f7862009-03-11 22:00:13 -04003156
Steven Rostedt8d707e82009-06-16 21:22:48 -04003157 dolock = rb_ok_to_lock();
Tom Zanussi2d622712009-03-22 03:30:49 -05003158 again:
Steven Rostedt8d707e82009-06-16 21:22:48 -04003159 local_irq_save(flags);
3160 if (dolock)
3161 spin_lock(&cpu_buffer->reader_lock);
Robert Richterd8eeb2d2009-07-31 14:58:04 +02003162 event = rb_buffer_peek(cpu_buffer, ts);
Robert Richter469535a2009-07-30 19:19:18 +02003163 if (event && event->type_len == RINGBUF_TYPE_PADDING)
3164 rb_advance_reader(cpu_buffer);
Steven Rostedt8d707e82009-06-16 21:22:48 -04003165 if (dolock)
3166 spin_unlock(&cpu_buffer->reader_lock);
3167 local_irq_restore(flags);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003168
Steven Rostedt1b959e12009-09-03 10:12:13 -04003169 if (event && event->type_len == RINGBUF_TYPE_PADDING)
Tom Zanussi2d622712009-03-22 03:30:49 -05003170 goto again;
Tom Zanussi2d622712009-03-22 03:30:49 -05003171
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003172 return event;
3173}
3174
3175/**
3176 * ring_buffer_iter_peek - peek at the next event to be read
3177 * @iter: The ring buffer iterator
3178 * @ts: The timestamp counter of this event.
3179 *
3180 * This will return the event that will be read next, but does
3181 * not increment the iterator.
3182 */
3183struct ring_buffer_event *
3184ring_buffer_iter_peek(struct ring_buffer_iter *iter, u64 *ts)
3185{
3186 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
3187 struct ring_buffer_event *event;
3188 unsigned long flags;
3189
Tom Zanussi2d622712009-03-22 03:30:49 -05003190 again:
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003191 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
3192 event = rb_iter_peek(iter, ts);
3193 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
3194
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/**
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003202 * ring_buffer_consume - return an event and consume it
3203 * @buffer: The ring buffer to get the next event from
3204 *
3205 * Returns the next event in the ring buffer, and that event is consumed.
3206 * Meaning, that sequential reads will keep returning a different event,
3207 * and eventually empty the ring buffer if the producer is slower.
3208 */
3209struct ring_buffer_event *
3210ring_buffer_consume(struct ring_buffer *buffer, int cpu, u64 *ts)
3211{
Steven Rostedt554f7862009-03-11 22:00:13 -04003212 struct ring_buffer_per_cpu *cpu_buffer;
3213 struct ring_buffer_event *event = NULL;
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003214 unsigned long flags;
Steven Rostedt8d707e82009-06-16 21:22:48 -04003215 int dolock;
3216
3217 dolock = rb_ok_to_lock();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003218
Tom Zanussi2d622712009-03-22 03:30:49 -05003219 again:
Steven Rostedt554f7862009-03-11 22:00:13 -04003220 /* might be called in atomic */
3221 preempt_disable();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003222
Steven Rostedt554f7862009-03-11 22:00:13 -04003223 if (!cpumask_test_cpu(cpu, buffer->cpumask))
3224 goto out;
3225
3226 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt8d707e82009-06-16 21:22:48 -04003227 local_irq_save(flags);
3228 if (dolock)
3229 spin_lock(&cpu_buffer->reader_lock);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003230
Robert Richterd8eeb2d2009-07-31 14:58:04 +02003231 event = rb_buffer_peek(cpu_buffer, ts);
Robert Richter469535a2009-07-30 19:19:18 +02003232 if (event)
3233 rb_advance_reader(cpu_buffer);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003234
Steven Rostedt8d707e82009-06-16 21:22:48 -04003235 if (dolock)
3236 spin_unlock(&cpu_buffer->reader_lock);
3237 local_irq_restore(flags);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003238
Steven Rostedt554f7862009-03-11 22:00:13 -04003239 out:
3240 preempt_enable();
3241
Steven Rostedt1b959e12009-09-03 10:12:13 -04003242 if (event && event->type_len == RINGBUF_TYPE_PADDING)
Tom Zanussi2d622712009-03-22 03:30:49 -05003243 goto again;
Tom Zanussi2d622712009-03-22 03:30:49 -05003244
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003245 return event;
3246}
Robert Richterc4f50182008-12-11 16:49:22 +01003247EXPORT_SYMBOL_GPL(ring_buffer_consume);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003248
3249/**
3250 * ring_buffer_read_start - start a non consuming read of the buffer
3251 * @buffer: The ring buffer to read from
3252 * @cpu: The cpu buffer to iterate over
3253 *
3254 * This starts up an iteration through the buffer. It also disables
3255 * the recording to the buffer until the reading is finished.
3256 * This prevents the reading from being corrupted. This is not
3257 * a consuming read, so a producer is not expected.
3258 *
3259 * Must be paired with ring_buffer_finish.
3260 */
3261struct ring_buffer_iter *
3262ring_buffer_read_start(struct ring_buffer *buffer, int cpu)
3263{
3264 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedt8aabee52009-03-12 13:13:49 -04003265 struct ring_buffer_iter *iter;
Steven Rostedtd7690412008-10-01 00:29:53 -04003266 unsigned long flags;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003267
Rusty Russell9e01c1b2009-01-01 10:12:22 +10303268 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04003269 return NULL;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003270
3271 iter = kmalloc(sizeof(*iter), GFP_KERNEL);
3272 if (!iter)
Steven Rostedt8aabee52009-03-12 13:13:49 -04003273 return NULL;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003274
3275 cpu_buffer = buffer->buffers[cpu];
3276
3277 iter->cpu_buffer = cpu_buffer;
3278
3279 atomic_inc(&cpu_buffer->record_disabled);
3280 synchronize_sched();
3281
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003282 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01003283 arch_spin_lock(&cpu_buffer->lock);
Steven Rostedt642edba2008-11-12 00:01:26 -05003284 rb_iter_reset(iter);
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01003285 arch_spin_unlock(&cpu_buffer->lock);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003286 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003287
3288 return iter;
3289}
Robert Richterc4f50182008-12-11 16:49:22 +01003290EXPORT_SYMBOL_GPL(ring_buffer_read_start);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003291
3292/**
3293 * ring_buffer_finish - finish reading the iterator of the buffer
3294 * @iter: The iterator retrieved by ring_buffer_start
3295 *
3296 * This re-enables the recording to the buffer, and frees the
3297 * iterator.
3298 */
3299void
3300ring_buffer_read_finish(struct ring_buffer_iter *iter)
3301{
3302 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
3303
3304 atomic_dec(&cpu_buffer->record_disabled);
3305 kfree(iter);
3306}
Robert Richterc4f50182008-12-11 16:49:22 +01003307EXPORT_SYMBOL_GPL(ring_buffer_read_finish);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003308
3309/**
3310 * ring_buffer_read - read the next item in the ring buffer by the iterator
3311 * @iter: The ring buffer iterator
3312 * @ts: The time stamp of the event read.
3313 *
3314 * This reads the next event in the ring buffer and increments the iterator.
3315 */
3316struct ring_buffer_event *
3317ring_buffer_read(struct ring_buffer_iter *iter, u64 *ts)
3318{
3319 struct ring_buffer_event *event;
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003320 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
3321 unsigned long flags;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003322
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003323 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
Steven Rostedt7e9391c2009-09-03 10:02:09 -04003324 again:
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003325 event = rb_iter_peek(iter, ts);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003326 if (!event)
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003327 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003328
Steven Rostedt7e9391c2009-09-03 10:02:09 -04003329 if (event->type_len == RINGBUF_TYPE_PADDING)
3330 goto again;
3331
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003332 rb_advance_iter(iter);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003333 out:
3334 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003335
3336 return event;
3337}
Robert Richterc4f50182008-12-11 16:49:22 +01003338EXPORT_SYMBOL_GPL(ring_buffer_read);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003339
3340/**
3341 * ring_buffer_size - return the size of the ring buffer (in bytes)
3342 * @buffer: The ring buffer.
3343 */
3344unsigned long ring_buffer_size(struct ring_buffer *buffer)
3345{
3346 return BUF_PAGE_SIZE * buffer->pages;
3347}
Robert Richterc4f50182008-12-11 16:49:22 +01003348EXPORT_SYMBOL_GPL(ring_buffer_size);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003349
3350static void
3351rb_reset_cpu(struct ring_buffer_per_cpu *cpu_buffer)
3352{
Steven Rostedt77ae3652009-03-27 11:00:29 -04003353 rb_head_page_deactivate(cpu_buffer);
3354
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003355 cpu_buffer->head_page
Steven Rostedt3adc54f2009-03-30 15:32:01 -04003356 = list_entry(cpu_buffer->pages, struct buffer_page, list);
Steven Rostedtbf41a152008-10-04 02:00:59 -04003357 local_set(&cpu_buffer->head_page->write, 0);
Steven Rostedt778c55d2009-05-01 18:44:45 -04003358 local_set(&cpu_buffer->head_page->entries, 0);
Steven Rostedtabc9b562008-12-02 15:34:06 -05003359 local_set(&cpu_buffer->head_page->page->commit, 0);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003360
Steven Rostedt6f807ac2008-10-04 02:00:58 -04003361 cpu_buffer->head_page->read = 0;
Steven Rostedtbf41a152008-10-04 02:00:59 -04003362
3363 cpu_buffer->tail_page = cpu_buffer->head_page;
3364 cpu_buffer->commit_page = cpu_buffer->head_page;
3365
3366 INIT_LIST_HEAD(&cpu_buffer->reader_page->list);
3367 local_set(&cpu_buffer->reader_page->write, 0);
Steven Rostedt778c55d2009-05-01 18:44:45 -04003368 local_set(&cpu_buffer->reader_page->entries, 0);
Steven Rostedtabc9b562008-12-02 15:34:06 -05003369 local_set(&cpu_buffer->reader_page->page->commit, 0);
Steven Rostedt6f807ac2008-10-04 02:00:58 -04003370 cpu_buffer->reader_page->read = 0;
Steven Rostedtd7690412008-10-01 00:29:53 -04003371
Steven Rostedt77ae3652009-03-27 11:00:29 -04003372 local_set(&cpu_buffer->commit_overrun, 0);
3373 local_set(&cpu_buffer->overrun, 0);
Steven Rostedte4906ef2009-04-30 20:49:44 -04003374 local_set(&cpu_buffer->entries, 0);
Steven Rostedtfa743952009-06-16 12:37:57 -04003375 local_set(&cpu_buffer->committing, 0);
3376 local_set(&cpu_buffer->commits, 0);
Steven Rostedt77ae3652009-03-27 11:00:29 -04003377 cpu_buffer->read = 0;
Steven Rostedt69507c02009-01-21 18:45:57 -05003378
3379 cpu_buffer->write_stamp = 0;
3380 cpu_buffer->read_stamp = 0;
Steven Rostedt77ae3652009-03-27 11:00:29 -04003381
3382 rb_head_page_activate(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003383}
3384
3385/**
3386 * ring_buffer_reset_cpu - reset a ring buffer per CPU buffer
3387 * @buffer: The ring buffer to reset a per cpu buffer of
3388 * @cpu: The CPU buffer to be reset
3389 */
3390void ring_buffer_reset_cpu(struct ring_buffer *buffer, int cpu)
3391{
3392 struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
3393 unsigned long flags;
3394
Rusty Russell9e01c1b2009-01-01 10:12:22 +10303395 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04003396 return;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003397
Steven Rostedt41ede232009-05-01 20:26:54 -04003398 atomic_inc(&cpu_buffer->record_disabled);
3399
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003400 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
3401
Steven Rostedt41b6a952009-09-02 09:59:48 -04003402 if (RB_WARN_ON(cpu_buffer, local_read(&cpu_buffer->committing)))
3403 goto out;
3404
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01003405 arch_spin_lock(&cpu_buffer->lock);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003406
3407 rb_reset_cpu(cpu_buffer);
3408
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01003409 arch_spin_unlock(&cpu_buffer->lock);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003410
Steven Rostedt41b6a952009-09-02 09:59:48 -04003411 out:
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003412 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
Steven Rostedt41ede232009-05-01 20:26:54 -04003413
3414 atomic_dec(&cpu_buffer->record_disabled);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003415}
Robert Richterc4f50182008-12-11 16:49:22 +01003416EXPORT_SYMBOL_GPL(ring_buffer_reset_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003417
3418/**
3419 * ring_buffer_reset - reset a ring buffer
3420 * @buffer: The ring buffer to reset all cpu buffers
3421 */
3422void ring_buffer_reset(struct ring_buffer *buffer)
3423{
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003424 int cpu;
3425
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003426 for_each_buffer_cpu(buffer, cpu)
Steven Rostedtd7690412008-10-01 00:29:53 -04003427 ring_buffer_reset_cpu(buffer, cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003428}
Robert Richterc4f50182008-12-11 16:49:22 +01003429EXPORT_SYMBOL_GPL(ring_buffer_reset);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003430
3431/**
3432 * rind_buffer_empty - is the ring buffer empty?
3433 * @buffer: The ring buffer to test
3434 */
3435int ring_buffer_empty(struct ring_buffer *buffer)
3436{
3437 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedtd4788202009-06-17 00:39:43 -04003438 unsigned long flags;
Steven Rostedt8d707e82009-06-16 21:22:48 -04003439 int dolock;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003440 int cpu;
Steven Rostedtd4788202009-06-17 00:39:43 -04003441 int ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003442
Steven Rostedt8d707e82009-06-16 21:22:48 -04003443 dolock = rb_ok_to_lock();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003444
3445 /* yes this is racy, but if you don't like the race, lock the buffer */
3446 for_each_buffer_cpu(buffer, cpu) {
3447 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt8d707e82009-06-16 21:22:48 -04003448 local_irq_save(flags);
3449 if (dolock)
3450 spin_lock(&cpu_buffer->reader_lock);
Steven Rostedtd4788202009-06-17 00:39:43 -04003451 ret = rb_per_cpu_empty(cpu_buffer);
Steven Rostedt8d707e82009-06-16 21:22:48 -04003452 if (dolock)
3453 spin_unlock(&cpu_buffer->reader_lock);
3454 local_irq_restore(flags);
3455
Steven Rostedtd4788202009-06-17 00:39:43 -04003456 if (!ret)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003457 return 0;
3458 }
Steven Rostedt554f7862009-03-11 22:00:13 -04003459
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003460 return 1;
3461}
Robert Richterc4f50182008-12-11 16:49:22 +01003462EXPORT_SYMBOL_GPL(ring_buffer_empty);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003463
3464/**
3465 * ring_buffer_empty_cpu - is a cpu buffer of a ring buffer empty?
3466 * @buffer: The ring buffer
3467 * @cpu: The CPU buffer to test
3468 */
3469int ring_buffer_empty_cpu(struct ring_buffer *buffer, int cpu)
3470{
3471 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedtd4788202009-06-17 00:39:43 -04003472 unsigned long flags;
Steven Rostedt8d707e82009-06-16 21:22:48 -04003473 int dolock;
Steven Rostedt8aabee52009-03-12 13:13:49 -04003474 int ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003475
Rusty Russell9e01c1b2009-01-01 10:12:22 +10303476 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04003477 return 1;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003478
Steven Rostedt8d707e82009-06-16 21:22:48 -04003479 dolock = rb_ok_to_lock();
Steven Rostedt554f7862009-03-11 22:00:13 -04003480
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003481 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt8d707e82009-06-16 21:22:48 -04003482 local_irq_save(flags);
3483 if (dolock)
3484 spin_lock(&cpu_buffer->reader_lock);
Steven Rostedt554f7862009-03-11 22:00:13 -04003485 ret = rb_per_cpu_empty(cpu_buffer);
Steven Rostedt8d707e82009-06-16 21:22:48 -04003486 if (dolock)
3487 spin_unlock(&cpu_buffer->reader_lock);
3488 local_irq_restore(flags);
Steven Rostedt554f7862009-03-11 22:00:13 -04003489
3490 return ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003491}
Robert Richterc4f50182008-12-11 16:49:22 +01003492EXPORT_SYMBOL_GPL(ring_buffer_empty_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003493
Steven Rostedt85bac322009-09-04 14:24:40 -04003494#ifdef CONFIG_RING_BUFFER_ALLOW_SWAP
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003495/**
3496 * ring_buffer_swap_cpu - swap a CPU buffer between two ring buffers
3497 * @buffer_a: One buffer to swap with
3498 * @buffer_b: The other buffer to swap with
3499 *
3500 * This function is useful for tracers that want to take a "snapshot"
3501 * of a CPU buffer and has another back up buffer lying around.
3502 * it is expected that the tracer handles the cpu buffer not being
3503 * used at the moment.
3504 */
3505int ring_buffer_swap_cpu(struct ring_buffer *buffer_a,
3506 struct ring_buffer *buffer_b, int cpu)
3507{
3508 struct ring_buffer_per_cpu *cpu_buffer_a;
3509 struct ring_buffer_per_cpu *cpu_buffer_b;
Steven Rostedt554f7862009-03-11 22:00:13 -04003510 int ret = -EINVAL;
3511
Rusty Russell9e01c1b2009-01-01 10:12:22 +10303512 if (!cpumask_test_cpu(cpu, buffer_a->cpumask) ||
3513 !cpumask_test_cpu(cpu, buffer_b->cpumask))
Steven Rostedt554f7862009-03-11 22:00:13 -04003514 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003515
3516 /* At least make sure the two buffers are somewhat the same */
Lai Jiangshan6d102bc2008-12-17 17:48:23 +08003517 if (buffer_a->pages != buffer_b->pages)
Steven Rostedt554f7862009-03-11 22:00:13 -04003518 goto out;
3519
3520 ret = -EAGAIN;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003521
Steven Rostedt97b17ef2009-01-21 15:24:56 -05003522 if (ring_buffer_flags != RB_BUFFERS_ON)
Steven Rostedt554f7862009-03-11 22:00:13 -04003523 goto out;
Steven Rostedt97b17ef2009-01-21 15:24:56 -05003524
3525 if (atomic_read(&buffer_a->record_disabled))
Steven Rostedt554f7862009-03-11 22:00:13 -04003526 goto out;
Steven Rostedt97b17ef2009-01-21 15:24:56 -05003527
3528 if (atomic_read(&buffer_b->record_disabled))
Steven Rostedt554f7862009-03-11 22:00:13 -04003529 goto out;
Steven Rostedt97b17ef2009-01-21 15:24:56 -05003530
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003531 cpu_buffer_a = buffer_a->buffers[cpu];
3532 cpu_buffer_b = buffer_b->buffers[cpu];
3533
Steven Rostedt97b17ef2009-01-21 15:24:56 -05003534 if (atomic_read(&cpu_buffer_a->record_disabled))
Steven Rostedt554f7862009-03-11 22:00:13 -04003535 goto out;
Steven Rostedt97b17ef2009-01-21 15:24:56 -05003536
3537 if (atomic_read(&cpu_buffer_b->record_disabled))
Steven Rostedt554f7862009-03-11 22:00:13 -04003538 goto out;
Steven Rostedt97b17ef2009-01-21 15:24:56 -05003539
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003540 /*
3541 * We can't do a synchronize_sched here because this
3542 * function can be called in atomic context.
3543 * Normally this will be called from the same CPU as cpu.
3544 * If not it's up to the caller to protect this.
3545 */
3546 atomic_inc(&cpu_buffer_a->record_disabled);
3547 atomic_inc(&cpu_buffer_b->record_disabled);
3548
Steven Rostedt98277992009-09-02 10:56:15 -04003549 ret = -EBUSY;
3550 if (local_read(&cpu_buffer_a->committing))
3551 goto out_dec;
3552 if (local_read(&cpu_buffer_b->committing))
3553 goto out_dec;
3554
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003555 buffer_a->buffers[cpu] = cpu_buffer_b;
3556 buffer_b->buffers[cpu] = cpu_buffer_a;
3557
3558 cpu_buffer_b->buffer = buffer_a;
3559 cpu_buffer_a->buffer = buffer_b;
3560
Steven Rostedt98277992009-09-02 10:56:15 -04003561 ret = 0;
3562
3563out_dec:
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003564 atomic_dec(&cpu_buffer_a->record_disabled);
3565 atomic_dec(&cpu_buffer_b->record_disabled);
Steven Rostedt554f7862009-03-11 22:00:13 -04003566out:
Steven Rostedt554f7862009-03-11 22:00:13 -04003567 return ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003568}
Robert Richterc4f50182008-12-11 16:49:22 +01003569EXPORT_SYMBOL_GPL(ring_buffer_swap_cpu);
Steven Rostedt85bac322009-09-04 14:24:40 -04003570#endif /* CONFIG_RING_BUFFER_ALLOW_SWAP */
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003571
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003572/**
3573 * ring_buffer_alloc_read_page - allocate a page to read from buffer
3574 * @buffer: the buffer to allocate for.
3575 *
3576 * This function is used in conjunction with ring_buffer_read_page.
3577 * When reading a full page from the ring buffer, these functions
3578 * can be used to speed up the process. The calling function should
3579 * allocate a few pages first with this function. Then when it
3580 * needs to get pages from the ring buffer, it passes the result
3581 * of this function into ring_buffer_read_page, which will swap
3582 * the page that was allocated, with the read page of the buffer.
3583 *
3584 * Returns:
3585 * The page allocated, or NULL on error.
3586 */
3587void *ring_buffer_alloc_read_page(struct ring_buffer *buffer)
3588{
Steven Rostedt044fa782008-12-02 23:50:03 -05003589 struct buffer_data_page *bpage;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003590 unsigned long addr;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003591
3592 addr = __get_free_page(GFP_KERNEL);
3593 if (!addr)
3594 return NULL;
3595
Steven Rostedt044fa782008-12-02 23:50:03 -05003596 bpage = (void *)addr;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003597
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003598 rb_init_page(bpage);
3599
Steven Rostedt044fa782008-12-02 23:50:03 -05003600 return bpage;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003601}
Steven Rostedtd6ce96d2009-05-05 01:15:24 -04003602EXPORT_SYMBOL_GPL(ring_buffer_alloc_read_page);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003603
3604/**
3605 * ring_buffer_free_read_page - free an allocated read page
3606 * @buffer: the buffer the page was allocate for
3607 * @data: the page to free
3608 *
3609 * Free a page allocated from ring_buffer_alloc_read_page.
3610 */
3611void ring_buffer_free_read_page(struct ring_buffer *buffer, void *data)
3612{
3613 free_page((unsigned long)data);
3614}
Steven Rostedtd6ce96d2009-05-05 01:15:24 -04003615EXPORT_SYMBOL_GPL(ring_buffer_free_read_page);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003616
3617/**
3618 * ring_buffer_read_page - extract a page from the ring buffer
3619 * @buffer: buffer to extract from
3620 * @data_page: the page to use allocated from ring_buffer_alloc_read_page
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003621 * @len: amount to extract
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003622 * @cpu: the cpu of the buffer to extract
3623 * @full: should the extraction only happen when the page is full.
3624 *
3625 * This function will pull out a page from the ring buffer and consume it.
3626 * @data_page must be the address of the variable that was returned
3627 * from ring_buffer_alloc_read_page. This is because the page might be used
3628 * to swap with a page in the ring buffer.
3629 *
3630 * for example:
Lai Jiangshanb85fa012009-02-09 14:21:14 +08003631 * rpage = ring_buffer_alloc_read_page(buffer);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003632 * if (!rpage)
3633 * return error;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003634 * ret = ring_buffer_read_page(buffer, &rpage, len, cpu, 0);
Lai Jiangshan667d2412009-02-09 14:21:17 +08003635 * if (ret >= 0)
3636 * process_page(rpage, ret);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003637 *
3638 * When @full is set, the function will not return true unless
3639 * the writer is off the reader page.
3640 *
3641 * Note: it is up to the calling functions to handle sleeps and wakeups.
3642 * The ring buffer can be used anywhere in the kernel and can not
3643 * blindly call wake_up. The layer that uses the ring buffer must be
3644 * responsible for that.
3645 *
3646 * Returns:
Lai Jiangshan667d2412009-02-09 14:21:17 +08003647 * >=0 if data has been transferred, returns the offset of consumed data.
3648 * <0 if no data has been transferred.
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003649 */
3650int ring_buffer_read_page(struct ring_buffer *buffer,
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003651 void **data_page, size_t len, int cpu, int full)
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003652{
3653 struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
3654 struct ring_buffer_event *event;
Steven Rostedt044fa782008-12-02 23:50:03 -05003655 struct buffer_data_page *bpage;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003656 struct buffer_page *reader;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003657 unsigned long flags;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003658 unsigned int commit;
Lai Jiangshan667d2412009-02-09 14:21:17 +08003659 unsigned int read;
Steven Rostedt4f3640f2009-03-03 23:52:42 -05003660 u64 save_timestamp;
Lai Jiangshan667d2412009-02-09 14:21:17 +08003661 int ret = -1;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003662
Steven Rostedt554f7862009-03-11 22:00:13 -04003663 if (!cpumask_test_cpu(cpu, buffer->cpumask))
3664 goto out;
3665
Steven Rostedt474d32b2009-03-03 19:51:40 -05003666 /*
3667 * If len is not big enough to hold the page header, then
3668 * we can not copy anything.
3669 */
3670 if (len <= BUF_PAGE_HDR_SIZE)
Steven Rostedt554f7862009-03-11 22:00:13 -04003671 goto out;
Steven Rostedt474d32b2009-03-03 19:51:40 -05003672
3673 len -= BUF_PAGE_HDR_SIZE;
3674
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003675 if (!data_page)
Steven Rostedt554f7862009-03-11 22:00:13 -04003676 goto out;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003677
Steven Rostedt044fa782008-12-02 23:50:03 -05003678 bpage = *data_page;
3679 if (!bpage)
Steven Rostedt554f7862009-03-11 22:00:13 -04003680 goto out;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003681
3682 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
3683
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003684 reader = rb_get_reader_page(cpu_buffer);
3685 if (!reader)
Steven Rostedt554f7862009-03-11 22:00:13 -04003686 goto out_unlock;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003687
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003688 event = rb_reader_event(cpu_buffer);
Lai Jiangshan667d2412009-02-09 14:21:17 +08003689
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003690 read = reader->read;
3691 commit = rb_page_commit(reader);
3692
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003693 /*
Steven Rostedt474d32b2009-03-03 19:51:40 -05003694 * If this page has been partially read or
3695 * if len is not big enough to read the rest of the page or
3696 * a writer is still on the page, then
3697 * we must copy the data from the page to the buffer.
3698 * Otherwise, we can simply swap the page with the one passed in.
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003699 */
Steven Rostedt474d32b2009-03-03 19:51:40 -05003700 if (read || (len < (commit - read)) ||
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003701 cpu_buffer->reader_page == cpu_buffer->commit_page) {
Lai Jiangshan667d2412009-02-09 14:21:17 +08003702 struct buffer_data_page *rpage = cpu_buffer->reader_page->page;
Steven Rostedt474d32b2009-03-03 19:51:40 -05003703 unsigned int rpos = read;
3704 unsigned int pos = 0;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003705 unsigned int size;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003706
3707 if (full)
Steven Rostedt554f7862009-03-11 22:00:13 -04003708 goto out_unlock;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003709
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003710 if (len > (commit - read))
3711 len = (commit - read);
3712
3713 size = rb_event_length(event);
3714
3715 if (len < size)
Steven Rostedt554f7862009-03-11 22:00:13 -04003716 goto out_unlock;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003717
Steven Rostedt4f3640f2009-03-03 23:52:42 -05003718 /* save the current timestamp, since the user will need it */
3719 save_timestamp = cpu_buffer->read_stamp;
3720
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003721 /* Need to copy one event at a time */
3722 do {
Steven Rostedt474d32b2009-03-03 19:51:40 -05003723 memcpy(bpage->data + pos, rpage->data + rpos, size);
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003724
3725 len -= size;
3726
3727 rb_advance_reader(cpu_buffer);
Steven Rostedt474d32b2009-03-03 19:51:40 -05003728 rpos = reader->read;
3729 pos += size;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003730
3731 event = rb_reader_event(cpu_buffer);
3732 size = rb_event_length(event);
3733 } while (len > size);
Lai Jiangshan667d2412009-02-09 14:21:17 +08003734
3735 /* update bpage */
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003736 local_set(&bpage->commit, pos);
Steven Rostedt4f3640f2009-03-03 23:52:42 -05003737 bpage->time_stamp = save_timestamp;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003738
Steven Rostedt474d32b2009-03-03 19:51:40 -05003739 /* we copied everything to the beginning */
3740 read = 0;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003741 } else {
Steven Rostedtafbab762009-05-01 19:40:05 -04003742 /* update the entry counter */
Steven Rostedt77ae3652009-03-27 11:00:29 -04003743 cpu_buffer->read += rb_page_entries(reader);
Steven Rostedtafbab762009-05-01 19:40:05 -04003744
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003745 /* swap the pages */
Steven Rostedt044fa782008-12-02 23:50:03 -05003746 rb_init_page(bpage);
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003747 bpage = reader->page;
3748 reader->page = *data_page;
3749 local_set(&reader->write, 0);
Steven Rostedt778c55d2009-05-01 18:44:45 -04003750 local_set(&reader->entries, 0);
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003751 reader->read = 0;
Steven Rostedt044fa782008-12-02 23:50:03 -05003752 *data_page = bpage;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003753 }
Lai Jiangshan667d2412009-02-09 14:21:17 +08003754 ret = read;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003755
Steven Rostedt554f7862009-03-11 22:00:13 -04003756 out_unlock:
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003757 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
3758
Steven Rostedt554f7862009-03-11 22:00:13 -04003759 out:
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003760 return ret;
3761}
Steven Rostedtd6ce96d2009-05-05 01:15:24 -04003762EXPORT_SYMBOL_GPL(ring_buffer_read_page);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003763
Paul Mundt1155de42009-06-25 14:30:12 +09003764#ifdef CONFIG_TRACING
Steven Rostedta3583242008-11-11 15:01:42 -05003765static ssize_t
3766rb_simple_read(struct file *filp, char __user *ubuf,
3767 size_t cnt, loff_t *ppos)
3768{
Hannes Eder5e398412009-02-10 19:44:34 +01003769 unsigned long *p = filp->private_data;
Steven Rostedta3583242008-11-11 15:01:42 -05003770 char buf[64];
3771 int r;
3772
Steven Rostedt033601a2008-11-21 12:41:55 -05003773 if (test_bit(RB_BUFFERS_DISABLED_BIT, p))
3774 r = sprintf(buf, "permanently disabled\n");
3775 else
3776 r = sprintf(buf, "%d\n", test_bit(RB_BUFFERS_ON_BIT, p));
Steven Rostedta3583242008-11-11 15:01:42 -05003777
3778 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
3779}
3780
3781static ssize_t
3782rb_simple_write(struct file *filp, const char __user *ubuf,
3783 size_t cnt, loff_t *ppos)
3784{
Hannes Eder5e398412009-02-10 19:44:34 +01003785 unsigned long *p = filp->private_data;
Steven Rostedta3583242008-11-11 15:01:42 -05003786 char buf[64];
Hannes Eder5e398412009-02-10 19:44:34 +01003787 unsigned long val;
Steven Rostedta3583242008-11-11 15:01:42 -05003788 int ret;
3789
3790 if (cnt >= sizeof(buf))
3791 return -EINVAL;
3792
3793 if (copy_from_user(&buf, ubuf, cnt))
3794 return -EFAULT;
3795
3796 buf[cnt] = 0;
3797
3798 ret = strict_strtoul(buf, 10, &val);
3799 if (ret < 0)
3800 return ret;
3801
Steven Rostedt033601a2008-11-21 12:41:55 -05003802 if (val)
3803 set_bit(RB_BUFFERS_ON_BIT, p);
3804 else
3805 clear_bit(RB_BUFFERS_ON_BIT, p);
Steven Rostedta3583242008-11-11 15:01:42 -05003806
3807 (*ppos)++;
3808
3809 return cnt;
3810}
3811
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003812static const struct file_operations rb_simple_fops = {
Steven Rostedta3583242008-11-11 15:01:42 -05003813 .open = tracing_open_generic,
3814 .read = rb_simple_read,
3815 .write = rb_simple_write,
3816};
3817
3818
3819static __init int rb_init_debugfs(void)
3820{
3821 struct dentry *d_tracer;
Steven Rostedta3583242008-11-11 15:01:42 -05003822
3823 d_tracer = tracing_init_dentry();
3824
Frederic Weisbecker5452af62009-03-27 00:25:38 +01003825 trace_create_file("tracing_on", 0644, d_tracer,
3826 &ring_buffer_flags, &rb_simple_fops);
Steven Rostedta3583242008-11-11 15:01:42 -05003827
3828 return 0;
3829}
3830
3831fs_initcall(rb_init_debugfs);
Paul Mundt1155de42009-06-25 14:30:12 +09003832#endif
Steven Rostedt554f7862009-03-11 22:00:13 -04003833
Steven Rostedt59222ef2009-03-12 11:46:03 -04003834#ifdef CONFIG_HOTPLUG_CPU
Frederic Weisbecker09c9e842009-03-21 04:33:36 +01003835static int rb_cpu_notify(struct notifier_block *self,
3836 unsigned long action, void *hcpu)
Steven Rostedt554f7862009-03-11 22:00:13 -04003837{
3838 struct ring_buffer *buffer =
3839 container_of(self, struct ring_buffer, cpu_notify);
3840 long cpu = (long)hcpu;
3841
3842 switch (action) {
3843 case CPU_UP_PREPARE:
3844 case CPU_UP_PREPARE_FROZEN:
Rusty Russell3f237a72009-06-12 21:15:30 +09303845 if (cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt554f7862009-03-11 22:00:13 -04003846 return NOTIFY_OK;
3847
3848 buffer->buffers[cpu] =
3849 rb_allocate_cpu_buffer(buffer, cpu);
3850 if (!buffer->buffers[cpu]) {
3851 WARN(1, "failed to allocate ring buffer on CPU %ld\n",
3852 cpu);
3853 return NOTIFY_OK;
3854 }
3855 smp_wmb();
Rusty Russell3f237a72009-06-12 21:15:30 +09303856 cpumask_set_cpu(cpu, buffer->cpumask);
Steven Rostedt554f7862009-03-11 22:00:13 -04003857 break;
3858 case CPU_DOWN_PREPARE:
3859 case CPU_DOWN_PREPARE_FROZEN:
3860 /*
3861 * Do nothing.
3862 * If we were to free the buffer, then the user would
3863 * lose any trace that was in the buffer.
3864 */
3865 break;
3866 default:
3867 break;
3868 }
3869 return NOTIFY_OK;
3870}
3871#endif