blob: db223fe8887fcac365391af9bbb98cb31ca41b01 [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
Steven Rostedt182e9f52008-11-03 23:15:56 -050023#include "trace.h"
24
Steven Rostedt033601a2008-11-21 12:41:55 -050025/*
Steven Rostedtd1b182a2009-04-15 16:53:47 -040026 * The ring buffer header is special. We must manually up keep it.
27 */
28int ring_buffer_print_entry_header(struct trace_seq *s)
29{
30 int ret;
31
Lai Jiangshan334d4162009-04-24 11:27:05 +080032 ret = trace_seq_printf(s, "# compressed entry header\n");
33 ret = trace_seq_printf(s, "\ttype_len : 5 bits\n");
Steven Rostedtd1b182a2009-04-15 16:53:47 -040034 ret = trace_seq_printf(s, "\ttime_delta : 27 bits\n");
35 ret = trace_seq_printf(s, "\tarray : 32 bits\n");
36 ret = trace_seq_printf(s, "\n");
37 ret = trace_seq_printf(s, "\tpadding : type == %d\n",
38 RINGBUF_TYPE_PADDING);
39 ret = trace_seq_printf(s, "\ttime_extend : type == %d\n",
40 RINGBUF_TYPE_TIME_EXTEND);
Lai Jiangshan334d4162009-04-24 11:27:05 +080041 ret = trace_seq_printf(s, "\tdata max type_len == %d\n",
42 RINGBUF_TYPE_DATA_TYPE_LEN_MAX);
Steven Rostedtd1b182a2009-04-15 16:53:47 -040043
44 return ret;
45}
46
47/*
Steven Rostedt5cc98542009-03-12 22:24:17 -040048 * The ring buffer is made up of a list of pages. A separate list of pages is
49 * allocated for each CPU. A writer may only write to a buffer that is
50 * associated with the CPU it is currently executing on. A reader may read
51 * from any per cpu buffer.
52 *
53 * The reader is special. For each per cpu buffer, the reader has its own
54 * reader page. When a reader has read the entire reader page, this reader
55 * page is swapped with another page in the ring buffer.
56 *
57 * Now, as long as the writer is off the reader page, the reader can do what
58 * ever it wants with that page. The writer will never write to that page
59 * again (as long as it is out of the ring buffer).
60 *
61 * Here's some silly ASCII art.
62 *
63 * +------+
64 * |reader| RING BUFFER
65 * |page |
66 * +------+ +---+ +---+ +---+
67 * | |-->| |-->| |
68 * +---+ +---+ +---+
69 * ^ |
70 * | |
71 * +---------------+
72 *
73 *
74 * +------+
75 * |reader| RING BUFFER
76 * |page |------------------v
77 * +------+ +---+ +---+ +---+
78 * | |-->| |-->| |
79 * +---+ +---+ +---+
80 * ^ |
81 * | |
82 * +---------------+
83 *
84 *
85 * +------+
86 * |reader| RING BUFFER
87 * |page |------------------v
88 * +------+ +---+ +---+ +---+
89 * ^ | |-->| |-->| |
90 * | +---+ +---+ +---+
91 * | |
92 * | |
93 * +------------------------------+
94 *
95 *
96 * +------+
97 * |buffer| RING BUFFER
98 * |page |------------------v
99 * +------+ +---+ +---+ +---+
100 * ^ | | | |-->| |
101 * | New +---+ +---+ +---+
102 * | Reader------^ |
103 * | page |
104 * +------------------------------+
105 *
106 *
107 * After we make this swap, the reader can hand this page off to the splice
108 * code and be done with it. It can even allocate a new page if it needs to
109 * and swap that into the ring buffer.
110 *
111 * We will be using cmpxchg soon to make all this lockless.
112 *
113 */
114
115/*
Steven Rostedt033601a2008-11-21 12:41:55 -0500116 * A fast way to enable or disable all ring buffers is to
117 * call tracing_on or tracing_off. Turning off the ring buffers
118 * prevents all ring buffers from being recorded to.
119 * Turning this switch on, makes it OK to write to the
120 * ring buffer, if the ring buffer is enabled itself.
121 *
122 * There's three layers that must be on in order to write
123 * to the ring buffer.
124 *
125 * 1) This global flag must be set.
126 * 2) The ring buffer must be enabled for recording.
127 * 3) The per cpu buffer must be enabled for recording.
128 *
129 * In case of an anomaly, this global flag has a bit set that
130 * will permantly disable all ring buffers.
131 */
132
133/*
134 * Global flag to disable all recording to ring buffers
135 * This has two bits: ON, DISABLED
136 *
137 * ON DISABLED
138 * ---- ----------
139 * 0 0 : ring buffers are off
140 * 1 0 : ring buffers are on
141 * X 1 : ring buffers are permanently disabled
142 */
143
144enum {
145 RB_BUFFERS_ON_BIT = 0,
146 RB_BUFFERS_DISABLED_BIT = 1,
147};
148
149enum {
150 RB_BUFFERS_ON = 1 << RB_BUFFERS_ON_BIT,
151 RB_BUFFERS_DISABLED = 1 << RB_BUFFERS_DISABLED_BIT,
152};
153
Hannes Eder5e398412009-02-10 19:44:34 +0100154static unsigned long ring_buffer_flags __read_mostly = RB_BUFFERS_ON;
Steven Rostedta3583242008-11-11 15:01:42 -0500155
Steven Rostedt474d32b2009-03-03 19:51:40 -0500156#define BUF_PAGE_HDR_SIZE offsetof(struct buffer_data_page, data)
157
Steven Rostedta3583242008-11-11 15:01:42 -0500158/**
159 * tracing_on - enable all tracing buffers
160 *
161 * This function enables all tracing buffers that may have been
162 * disabled with tracing_off.
163 */
164void tracing_on(void)
165{
Steven Rostedt033601a2008-11-21 12:41:55 -0500166 set_bit(RB_BUFFERS_ON_BIT, &ring_buffer_flags);
Steven Rostedta3583242008-11-11 15:01:42 -0500167}
Robert Richterc4f50182008-12-11 16:49:22 +0100168EXPORT_SYMBOL_GPL(tracing_on);
Steven Rostedta3583242008-11-11 15:01:42 -0500169
170/**
171 * tracing_off - turn off all tracing buffers
172 *
173 * This function stops all tracing buffers from recording data.
174 * It does not disable any overhead the tracers themselves may
175 * be causing. This function simply causes all recording to
176 * the ring buffers to fail.
177 */
178void tracing_off(void)
179{
Steven Rostedt033601a2008-11-21 12:41:55 -0500180 clear_bit(RB_BUFFERS_ON_BIT, &ring_buffer_flags);
181}
Robert Richterc4f50182008-12-11 16:49:22 +0100182EXPORT_SYMBOL_GPL(tracing_off);
Steven Rostedt033601a2008-11-21 12:41:55 -0500183
184/**
185 * tracing_off_permanent - permanently disable ring buffers
186 *
187 * This function, once called, will disable all ring buffers
Wenji Huangc3706f02009-02-10 01:03:18 -0500188 * permanently.
Steven Rostedt033601a2008-11-21 12:41:55 -0500189 */
190void tracing_off_permanent(void)
191{
192 set_bit(RB_BUFFERS_DISABLED_BIT, &ring_buffer_flags);
Steven Rostedta3583242008-11-11 15:01:42 -0500193}
194
Steven Rostedt988ae9d2009-02-14 19:17:02 -0500195/**
196 * tracing_is_on - show state of ring buffers enabled
197 */
198int tracing_is_on(void)
199{
200 return ring_buffer_flags == RB_BUFFERS_ON;
201}
202EXPORT_SYMBOL_GPL(tracing_is_on);
203
Steven Rostedte3d6bf02009-03-03 13:53:07 -0500204#define RB_EVNT_HDR_SIZE (offsetof(struct ring_buffer_event, array))
Andrew Morton67d34722009-01-09 12:27:09 -0800205#define RB_ALIGNMENT 4U
Lai Jiangshan334d4162009-04-24 11:27:05 +0800206#define RB_MAX_SMALL_DATA (RB_ALIGNMENT * RINGBUF_TYPE_DATA_TYPE_LEN_MAX)
Steven Rostedtc7b09302009-06-11 11:12:00 -0400207#define RB_EVNT_MIN_SIZE 8U /* two 32bit words */
Lai Jiangshan334d4162009-04-24 11:27:05 +0800208
209/* define RINGBUF_TYPE_DATA for 'case RINGBUF_TYPE_DATA:' */
210#define RINGBUF_TYPE_DATA 0 ... RINGBUF_TYPE_DATA_TYPE_LEN_MAX
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400211
212enum {
213 RB_LEN_TIME_EXTEND = 8,
214 RB_LEN_TIME_STAMP = 16,
215};
216
Tom Zanussi2d622712009-03-22 03:30:49 -0500217static inline int rb_null_event(struct ring_buffer_event *event)
218{
Steven Rostedta1863c22009-09-03 10:23:58 -0400219 return event->type_len == RINGBUF_TYPE_PADDING && !event->time_delta;
Tom Zanussi2d622712009-03-22 03:30:49 -0500220}
221
222static void rb_event_set_padding(struct ring_buffer_event *event)
223{
Steven Rostedta1863c22009-09-03 10:23:58 -0400224 /* padding has a NULL time_delta */
Lai Jiangshan334d4162009-04-24 11:27:05 +0800225 event->type_len = RINGBUF_TYPE_PADDING;
Tom Zanussi2d622712009-03-22 03:30:49 -0500226 event->time_delta = 0;
227}
228
Tom Zanussi2d622712009-03-22 03:30:49 -0500229static unsigned
230rb_event_data_length(struct ring_buffer_event *event)
231{
232 unsigned length;
233
Lai Jiangshan334d4162009-04-24 11:27:05 +0800234 if (event->type_len)
235 length = event->type_len * RB_ALIGNMENT;
Tom Zanussi2d622712009-03-22 03:30:49 -0500236 else
237 length = event->array[0];
238 return length + RB_EVNT_HDR_SIZE;
239}
240
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400241/* inline for ring buffer fast paths */
Andrew Morton34a148b2009-01-09 12:27:09 -0800242static unsigned
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400243rb_event_length(struct ring_buffer_event *event)
244{
Lai Jiangshan334d4162009-04-24 11:27:05 +0800245 switch (event->type_len) {
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400246 case RINGBUF_TYPE_PADDING:
Tom Zanussi2d622712009-03-22 03:30:49 -0500247 if (rb_null_event(event))
248 /* undefined */
249 return -1;
Lai Jiangshan334d4162009-04-24 11:27:05 +0800250 return event->array[0] + RB_EVNT_HDR_SIZE;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400251
252 case RINGBUF_TYPE_TIME_EXTEND:
253 return RB_LEN_TIME_EXTEND;
254
255 case RINGBUF_TYPE_TIME_STAMP:
256 return RB_LEN_TIME_STAMP;
257
258 case RINGBUF_TYPE_DATA:
Tom Zanussi2d622712009-03-22 03:30:49 -0500259 return rb_event_data_length(event);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400260 default:
261 BUG();
262 }
263 /* not hit */
264 return 0;
265}
266
267/**
268 * ring_buffer_event_length - return the length of the event
269 * @event: the event to get the length of
270 */
271unsigned ring_buffer_event_length(struct ring_buffer_event *event)
272{
Robert Richter465634a2009-01-07 15:32:11 +0100273 unsigned length = rb_event_length(event);
Lai Jiangshan334d4162009-04-24 11:27:05 +0800274 if (event->type_len > RINGBUF_TYPE_DATA_TYPE_LEN_MAX)
Robert Richter465634a2009-01-07 15:32:11 +0100275 return length;
276 length -= RB_EVNT_HDR_SIZE;
277 if (length > RB_MAX_SMALL_DATA + sizeof(event->array[0]))
278 length -= sizeof(event->array[0]);
279 return length;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400280}
Robert Richterc4f50182008-12-11 16:49:22 +0100281EXPORT_SYMBOL_GPL(ring_buffer_event_length);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400282
283/* inline for ring buffer fast paths */
Andrew Morton34a148b2009-01-09 12:27:09 -0800284static void *
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400285rb_event_data(struct ring_buffer_event *event)
286{
Lai Jiangshan334d4162009-04-24 11:27:05 +0800287 BUG_ON(event->type_len > RINGBUF_TYPE_DATA_TYPE_LEN_MAX);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400288 /* If length is in len field, then array[0] has the data */
Lai Jiangshan334d4162009-04-24 11:27:05 +0800289 if (event->type_len)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400290 return (void *)&event->array[0];
291 /* Otherwise length is in array[0] and array[1] has the data */
292 return (void *)&event->array[1];
293}
294
295/**
296 * ring_buffer_event_data - return the data of the event
297 * @event: the event to get the data from
298 */
299void *ring_buffer_event_data(struct ring_buffer_event *event)
300{
301 return rb_event_data(event);
302}
Robert Richterc4f50182008-12-11 16:49:22 +0100303EXPORT_SYMBOL_GPL(ring_buffer_event_data);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400304
305#define for_each_buffer_cpu(buffer, cpu) \
Rusty Russell9e01c1b2009-01-01 10:12:22 +1030306 for_each_cpu(cpu, buffer->cpumask)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400307
308#define TS_SHIFT 27
309#define TS_MASK ((1ULL << TS_SHIFT) - 1)
310#define TS_DELTA_TEST (~TS_MASK)
311
Steven Rostedtabc9b562008-12-02 15:34:06 -0500312struct buffer_data_page {
Steven Rostedte4c2ce82008-10-01 11:14:54 -0400313 u64 time_stamp; /* page time stamp */
Wenji Huangc3706f02009-02-10 01:03:18 -0500314 local_t commit; /* write committed index */
Steven Rostedtabc9b562008-12-02 15:34:06 -0500315 unsigned char data[]; /* data of buffer page */
316};
317
Steven Rostedt77ae3652009-03-27 11:00:29 -0400318/*
319 * Note, the buffer_page list must be first. The buffer pages
320 * are allocated in cache lines, which means that each buffer
321 * page will be at the beginning of a cache line, and thus
322 * the least significant bits will be zero. We use this to
323 * add flags in the list struct pointers, to make the ring buffer
324 * lockless.
325 */
Steven Rostedtabc9b562008-12-02 15:34:06 -0500326struct buffer_page {
Steven Rostedt778c55d2009-05-01 18:44:45 -0400327 struct list_head list; /* list of buffer pages */
Steven Rostedtabc9b562008-12-02 15:34:06 -0500328 local_t write; /* index for next write */
Steven Rostedt6f807ac2008-10-04 02:00:58 -0400329 unsigned read; /* index for next read */
Steven Rostedt778c55d2009-05-01 18:44:45 -0400330 local_t entries; /* entries on this page */
Steven Rostedtabc9b562008-12-02 15:34:06 -0500331 struct buffer_data_page *page; /* Actual data page */
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400332};
333
Steven Rostedt77ae3652009-03-27 11:00:29 -0400334/*
335 * The buffer page counters, write and entries, must be reset
336 * atomically when crossing page boundaries. To synchronize this
337 * update, two counters are inserted into the number. One is
338 * the actual counter for the write position or count on the page.
339 *
340 * The other is a counter of updaters. Before an update happens
341 * the update partition of the counter is incremented. This will
342 * allow the updater to update the counter atomically.
343 *
344 * The counter is 20 bits, and the state data is 12.
345 */
346#define RB_WRITE_MASK 0xfffff
347#define RB_WRITE_INTCNT (1 << 20)
348
Steven Rostedt044fa782008-12-02 23:50:03 -0500349static void rb_init_page(struct buffer_data_page *bpage)
Steven Rostedtabc9b562008-12-02 15:34:06 -0500350{
Steven Rostedt044fa782008-12-02 23:50:03 -0500351 local_set(&bpage->commit, 0);
Steven Rostedtabc9b562008-12-02 15:34:06 -0500352}
353
Steven Rostedt474d32b2009-03-03 19:51:40 -0500354/**
355 * ring_buffer_page_len - the size of data on the page.
356 * @page: The page to read
357 *
358 * Returns the amount of data on the page, including buffer page header.
359 */
Steven Rostedtef7a4a12009-03-03 00:27:49 -0500360size_t ring_buffer_page_len(void *page)
361{
Steven Rostedt474d32b2009-03-03 19:51:40 -0500362 return local_read(&((struct buffer_data_page *)page)->commit)
363 + BUF_PAGE_HDR_SIZE;
Steven Rostedtef7a4a12009-03-03 00:27:49 -0500364}
365
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400366/*
Steven Rostedted568292008-09-29 23:02:40 -0400367 * Also stolen from mm/slob.c. Thanks to Mathieu Desnoyers for pointing
368 * this issue out.
369 */
Andrew Morton34a148b2009-01-09 12:27:09 -0800370static void free_buffer_page(struct buffer_page *bpage)
Steven Rostedted568292008-09-29 23:02:40 -0400371{
Andrew Morton34a148b2009-01-09 12:27:09 -0800372 free_page((unsigned long)bpage->page);
Steven Rostedte4c2ce82008-10-01 11:14:54 -0400373 kfree(bpage);
Steven Rostedted568292008-09-29 23:02:40 -0400374}
375
376/*
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400377 * We need to fit the time_stamp delta into 27 bits.
378 */
379static inline int test_time_stamp(u64 delta)
380{
381 if (delta & TS_DELTA_TEST)
382 return 1;
383 return 0;
384}
385
Steven Rostedt474d32b2009-03-03 19:51:40 -0500386#define BUF_PAGE_SIZE (PAGE_SIZE - BUF_PAGE_HDR_SIZE)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400387
Steven Rostedtbe957c42009-05-11 14:42:53 -0400388/* Max payload is BUF_PAGE_SIZE - header (8bytes) */
389#define BUF_MAX_DATA_SIZE (BUF_PAGE_SIZE - (sizeof(u32) * 2))
390
Steven Rostedtea05b572009-06-03 09:30:10 -0400391/* Max number of timestamps that can fit on a page */
392#define RB_TIMESTAMPS_PER_PAGE (BUF_PAGE_SIZE / RB_LEN_TIME_STAMP)
393
Steven Rostedtd1b182a2009-04-15 16:53:47 -0400394int ring_buffer_print_page_header(struct trace_seq *s)
395{
396 struct buffer_data_page field;
397 int ret;
398
399 ret = trace_seq_printf(s, "\tfield: u64 timestamp;\t"
Tom Zanussi26a50742009-10-06 01:09:50 -0500400 "offset:0;\tsize:%u;\tsigned:%u;\n",
401 (unsigned int)sizeof(field.time_stamp),
402 (unsigned int)is_signed_type(u64));
Steven Rostedtd1b182a2009-04-15 16:53:47 -0400403
404 ret = trace_seq_printf(s, "\tfield: local_t commit;\t"
Tom Zanussi26a50742009-10-06 01:09:50 -0500405 "offset:%u;\tsize:%u;\tsigned:%u;\n",
Steven Rostedtd1b182a2009-04-15 16:53:47 -0400406 (unsigned int)offsetof(typeof(field), commit),
Tom Zanussi26a50742009-10-06 01:09:50 -0500407 (unsigned int)sizeof(field.commit),
408 (unsigned int)is_signed_type(long));
Steven Rostedtd1b182a2009-04-15 16:53:47 -0400409
410 ret = trace_seq_printf(s, "\tfield: char data;\t"
Tom Zanussi26a50742009-10-06 01:09:50 -0500411 "offset:%u;\tsize:%u;\tsigned:%u;\n",
Steven Rostedtd1b182a2009-04-15 16:53:47 -0400412 (unsigned int)offsetof(typeof(field), data),
Tom Zanussi26a50742009-10-06 01:09:50 -0500413 (unsigned int)BUF_PAGE_SIZE,
414 (unsigned int)is_signed_type(char));
Steven Rostedtd1b182a2009-04-15 16:53:47 -0400415
416 return ret;
417}
418
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400419/*
420 * head_page == tail_page && head == tail then buffer is empty.
421 */
422struct ring_buffer_per_cpu {
423 int cpu;
424 struct ring_buffer *buffer;
Steven Rostedt77ae3652009-03-27 11:00:29 -0400425 spinlock_t reader_lock; /* serialize readers */
Steven Rostedt3e03fb72008-11-06 00:09:43 -0500426 raw_spinlock_t lock;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400427 struct lock_class_key lock_key;
Steven Rostedt3adc54f2009-03-30 15:32:01 -0400428 struct list_head *pages;
Steven Rostedt6f807ac2008-10-04 02:00:58 -0400429 struct buffer_page *head_page; /* read from head */
430 struct buffer_page *tail_page; /* write to tail */
Wenji Huangc3706f02009-02-10 01:03:18 -0500431 struct buffer_page *commit_page; /* committed pages */
Steven Rostedtd7690412008-10-01 00:29:53 -0400432 struct buffer_page *reader_page;
Steven Rostedt77ae3652009-03-27 11:00:29 -0400433 local_t commit_overrun;
434 local_t overrun;
Steven Rostedte4906ef2009-04-30 20:49:44 -0400435 local_t entries;
Steven Rostedtfa743952009-06-16 12:37:57 -0400436 local_t committing;
437 local_t commits;
Steven Rostedt77ae3652009-03-27 11:00:29 -0400438 unsigned long read;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400439 u64 write_stamp;
440 u64 read_stamp;
441 atomic_t record_disabled;
442};
443
444struct ring_buffer {
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400445 unsigned pages;
446 unsigned flags;
447 int cpus;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400448 atomic_t record_disabled;
Arnaldo Carvalho de Melo00f62f62009-02-09 17:04:06 -0200449 cpumask_var_t cpumask;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400450
Peter Zijlstra1f8a6a12009-06-08 18:18:39 +0200451 struct lock_class_key *reader_lock_key;
452
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400453 struct mutex mutex;
454
455 struct ring_buffer_per_cpu **buffers;
Steven Rostedt554f7862009-03-11 22:00:13 -0400456
Steven Rostedt59222ef2009-03-12 11:46:03 -0400457#ifdef CONFIG_HOTPLUG_CPU
Steven Rostedt554f7862009-03-11 22:00:13 -0400458 struct notifier_block cpu_notify;
459#endif
Steven Rostedt37886f62009-03-17 17:22:06 -0400460 u64 (*clock)(void);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400461};
462
463struct ring_buffer_iter {
464 struct ring_buffer_per_cpu *cpu_buffer;
465 unsigned long head;
466 struct buffer_page *head_page;
467 u64 read_stamp;
468};
469
Steven Rostedtf536aaf2008-11-10 23:07:30 -0500470/* buffer may be either ring_buffer or ring_buffer_per_cpu */
Steven Rostedt077c5402009-09-03 19:53:46 -0400471#define RB_WARN_ON(b, cond) \
472 ({ \
473 int _____ret = unlikely(cond); \
474 if (_____ret) { \
475 if (__same_type(*(b), struct ring_buffer_per_cpu)) { \
476 struct ring_buffer_per_cpu *__b = \
477 (void *)b; \
478 atomic_inc(&__b->buffer->record_disabled); \
479 } else \
480 atomic_inc(&b->record_disabled); \
481 WARN_ON(1); \
482 } \
483 _____ret; \
Steven Rostedt3e89c7b2008-11-11 15:28:41 -0500484 })
Steven Rostedtf536aaf2008-11-10 23:07:30 -0500485
Steven Rostedt37886f62009-03-17 17:22:06 -0400486/* Up this if you want to test the TIME_EXTENTS and normalization */
487#define DEBUG_SHIFT 0
488
Jiri Olsa6d3f1e12009-10-23 19:36:19 -0400489static inline u64 rb_time_stamp(struct ring_buffer *buffer)
Steven Rostedt88eb0122009-05-11 16:28:23 -0400490{
491 /* shift to debug/test normalization and TIME_EXTENTS */
492 return buffer->clock() << DEBUG_SHIFT;
493}
494
Steven Rostedt37886f62009-03-17 17:22:06 -0400495u64 ring_buffer_time_stamp(struct ring_buffer *buffer, int cpu)
496{
497 u64 time;
498
499 preempt_disable_notrace();
Jiri Olsa6d3f1e12009-10-23 19:36:19 -0400500 time = rb_time_stamp(buffer);
Steven Rostedt37886f62009-03-17 17:22:06 -0400501 preempt_enable_no_resched_notrace();
502
503 return time;
504}
505EXPORT_SYMBOL_GPL(ring_buffer_time_stamp);
506
507void ring_buffer_normalize_time_stamp(struct ring_buffer *buffer,
508 int cpu, u64 *ts)
509{
510 /* Just stupid testing the normalize function and deltas */
511 *ts >>= DEBUG_SHIFT;
512}
513EXPORT_SYMBOL_GPL(ring_buffer_normalize_time_stamp);
514
Steven Rostedt77ae3652009-03-27 11:00:29 -0400515/*
516 * Making the ring buffer lockless makes things tricky.
517 * Although writes only happen on the CPU that they are on,
518 * and they only need to worry about interrupts. Reads can
519 * happen on any CPU.
520 *
521 * The reader page is always off the ring buffer, but when the
522 * reader finishes with a page, it needs to swap its page with
523 * a new one from the buffer. The reader needs to take from
524 * the head (writes go to the tail). But if a writer is in overwrite
525 * mode and wraps, it must push the head page forward.
526 *
527 * Here lies the problem.
528 *
529 * The reader must be careful to replace only the head page, and
530 * not another one. As described at the top of the file in the
531 * ASCII art, the reader sets its old page to point to the next
532 * page after head. It then sets the page after head to point to
533 * the old reader page. But if the writer moves the head page
534 * during this operation, the reader could end up with the tail.
535 *
536 * We use cmpxchg to help prevent this race. We also do something
537 * special with the page before head. We set the LSB to 1.
538 *
539 * When the writer must push the page forward, it will clear the
540 * bit that points to the head page, move the head, and then set
541 * the bit that points to the new head page.
542 *
543 * We also don't want an interrupt coming in and moving the head
544 * page on another writer. Thus we use the second LSB to catch
545 * that too. Thus:
546 *
547 * head->list->prev->next bit 1 bit 0
548 * ------- -------
549 * Normal page 0 0
550 * Points to head page 0 1
551 * New head page 1 0
552 *
553 * Note we can not trust the prev pointer of the head page, because:
554 *
555 * +----+ +-----+ +-----+
556 * | |------>| T |---X--->| N |
557 * | |<------| | | |
558 * +----+ +-----+ +-----+
559 * ^ ^ |
560 * | +-----+ | |
561 * +----------| R |----------+ |
562 * | |<-----------+
563 * +-----+
564 *
565 * Key: ---X--> HEAD flag set in pointer
566 * T Tail page
567 * R Reader page
568 * N Next page
569 *
570 * (see __rb_reserve_next() to see where this happens)
571 *
572 * What the above shows is that the reader just swapped out
573 * the reader page with a page in the buffer, but before it
574 * could make the new header point back to the new page added
575 * it was preempted by a writer. The writer moved forward onto
576 * the new page added by the reader and is about to move forward
577 * again.
578 *
579 * You can see, it is legitimate for the previous pointer of
580 * the head (or any page) not to point back to itself. But only
581 * temporarially.
582 */
583
584#define RB_PAGE_NORMAL 0UL
585#define RB_PAGE_HEAD 1UL
586#define RB_PAGE_UPDATE 2UL
587
588
589#define RB_FLAG_MASK 3UL
590
591/* PAGE_MOVED is not part of the mask */
592#define RB_PAGE_MOVED 4UL
593
594/*
595 * rb_list_head - remove any bit
596 */
597static struct list_head *rb_list_head(struct list_head *list)
598{
599 unsigned long val = (unsigned long)list;
600
601 return (struct list_head *)(val & ~RB_FLAG_MASK);
602}
603
604/*
Jiri Olsa6d3f1e12009-10-23 19:36:19 -0400605 * rb_is_head_page - test if the given page is the head page
Steven Rostedt77ae3652009-03-27 11:00:29 -0400606 *
607 * Because the reader may move the head_page pointer, we can
608 * not trust what the head page is (it may be pointing to
609 * the reader page). But if the next page is a header page,
610 * its flags will be non zero.
611 */
612static int inline
613rb_is_head_page(struct ring_buffer_per_cpu *cpu_buffer,
614 struct buffer_page *page, struct list_head *list)
615{
616 unsigned long val;
617
618 val = (unsigned long)list->next;
619
620 if ((val & ~RB_FLAG_MASK) != (unsigned long)&page->list)
621 return RB_PAGE_MOVED;
622
623 return val & RB_FLAG_MASK;
624}
625
626/*
627 * rb_is_reader_page
628 *
629 * The unique thing about the reader page, is that, if the
630 * writer is ever on it, the previous pointer never points
631 * back to the reader page.
632 */
633static int rb_is_reader_page(struct buffer_page *page)
634{
635 struct list_head *list = page->list.prev;
636
637 return rb_list_head(list->next) != &page->list;
638}
639
640/*
641 * rb_set_list_to_head - set a list_head to be pointing to head.
642 */
643static void rb_set_list_to_head(struct ring_buffer_per_cpu *cpu_buffer,
644 struct list_head *list)
645{
646 unsigned long *ptr;
647
648 ptr = (unsigned long *)&list->next;
649 *ptr |= RB_PAGE_HEAD;
650 *ptr &= ~RB_PAGE_UPDATE;
651}
652
653/*
654 * rb_head_page_activate - sets up head page
655 */
656static void rb_head_page_activate(struct ring_buffer_per_cpu *cpu_buffer)
657{
658 struct buffer_page *head;
659
660 head = cpu_buffer->head_page;
661 if (!head)
662 return;
663
664 /*
665 * Set the previous list pointer to have the HEAD flag.
666 */
667 rb_set_list_to_head(cpu_buffer, head->list.prev);
668}
669
670static void rb_list_head_clear(struct list_head *list)
671{
672 unsigned long *ptr = (unsigned long *)&list->next;
673
674 *ptr &= ~RB_FLAG_MASK;
675}
676
677/*
678 * rb_head_page_dactivate - clears head page ptr (for free list)
679 */
680static void
681rb_head_page_deactivate(struct ring_buffer_per_cpu *cpu_buffer)
682{
683 struct list_head *hd;
684
685 /* Go through the whole list and clear any pointers found. */
686 rb_list_head_clear(cpu_buffer->pages);
687
688 list_for_each(hd, cpu_buffer->pages)
689 rb_list_head_clear(hd);
690}
691
692static int rb_head_page_set(struct ring_buffer_per_cpu *cpu_buffer,
693 struct buffer_page *head,
694 struct buffer_page *prev,
695 int old_flag, int new_flag)
696{
697 struct list_head *list;
698 unsigned long val = (unsigned long)&head->list;
699 unsigned long ret;
700
701 list = &prev->list;
702
703 val &= ~RB_FLAG_MASK;
704
Steven Rostedt08a40812009-09-14 09:31:35 -0400705 ret = cmpxchg((unsigned long *)&list->next,
706 val | old_flag, val | new_flag);
Steven Rostedt77ae3652009-03-27 11:00:29 -0400707
708 /* check if the reader took the page */
709 if ((ret & ~RB_FLAG_MASK) != val)
710 return RB_PAGE_MOVED;
711
712 return ret & RB_FLAG_MASK;
713}
714
715static int rb_head_page_set_update(struct ring_buffer_per_cpu *cpu_buffer,
716 struct buffer_page *head,
717 struct buffer_page *prev,
718 int old_flag)
719{
720 return rb_head_page_set(cpu_buffer, head, prev,
721 old_flag, RB_PAGE_UPDATE);
722}
723
724static int rb_head_page_set_head(struct ring_buffer_per_cpu *cpu_buffer,
725 struct buffer_page *head,
726 struct buffer_page *prev,
727 int old_flag)
728{
729 return rb_head_page_set(cpu_buffer, head, prev,
730 old_flag, RB_PAGE_HEAD);
731}
732
733static int rb_head_page_set_normal(struct ring_buffer_per_cpu *cpu_buffer,
734 struct buffer_page *head,
735 struct buffer_page *prev,
736 int old_flag)
737{
738 return rb_head_page_set(cpu_buffer, head, prev,
739 old_flag, RB_PAGE_NORMAL);
740}
741
742static inline void rb_inc_page(struct ring_buffer_per_cpu *cpu_buffer,
743 struct buffer_page **bpage)
744{
745 struct list_head *p = rb_list_head((*bpage)->list.next);
746
747 *bpage = list_entry(p, struct buffer_page, list);
748}
749
750static struct buffer_page *
751rb_set_head_page(struct ring_buffer_per_cpu *cpu_buffer)
752{
753 struct buffer_page *head;
754 struct buffer_page *page;
755 struct list_head *list;
756 int i;
757
758 if (RB_WARN_ON(cpu_buffer, !cpu_buffer->head_page))
759 return NULL;
760
761 /* sanity check */
762 list = cpu_buffer->pages;
763 if (RB_WARN_ON(cpu_buffer, rb_list_head(list->prev->next) != list))
764 return NULL;
765
766 page = head = cpu_buffer->head_page;
767 /*
768 * It is possible that the writer moves the header behind
769 * where we started, and we miss in one loop.
770 * A second loop should grab the header, but we'll do
771 * three loops just because I'm paranoid.
772 */
773 for (i = 0; i < 3; i++) {
774 do {
775 if (rb_is_head_page(cpu_buffer, page, page->list.prev)) {
776 cpu_buffer->head_page = page;
777 return page;
778 }
779 rb_inc_page(cpu_buffer, &page);
780 } while (page != head);
781 }
782
783 RB_WARN_ON(cpu_buffer, 1);
784
785 return NULL;
786}
787
788static int rb_head_page_replace(struct buffer_page *old,
789 struct buffer_page *new)
790{
791 unsigned long *ptr = (unsigned long *)&old->list.prev->next;
792 unsigned long val;
793 unsigned long ret;
794
795 val = *ptr & ~RB_FLAG_MASK;
796 val |= RB_PAGE_HEAD;
797
Steven Rostedt08a40812009-09-14 09:31:35 -0400798 ret = cmpxchg(ptr, val, (unsigned long)&new->list);
Steven Rostedt77ae3652009-03-27 11:00:29 -0400799
800 return ret == val;
801}
802
803/*
804 * rb_tail_page_update - move the tail page forward
805 *
806 * Returns 1 if moved tail page, 0 if someone else did.
807 */
808static int rb_tail_page_update(struct ring_buffer_per_cpu *cpu_buffer,
809 struct buffer_page *tail_page,
810 struct buffer_page *next_page)
811{
812 struct buffer_page *old_tail;
813 unsigned long old_entries;
814 unsigned long old_write;
815 int ret = 0;
816
817 /*
818 * The tail page now needs to be moved forward.
819 *
820 * We need to reset the tail page, but without messing
821 * with possible erasing of data brought in by interrupts
822 * that have moved the tail page and are currently on it.
823 *
824 * We add a counter to the write field to denote this.
825 */
826 old_write = local_add_return(RB_WRITE_INTCNT, &next_page->write);
827 old_entries = local_add_return(RB_WRITE_INTCNT, &next_page->entries);
828
829 /*
830 * Just make sure we have seen our old_write and synchronize
831 * with any interrupts that come in.
832 */
833 barrier();
834
835 /*
836 * If the tail page is still the same as what we think
837 * it is, then it is up to us to update the tail
838 * pointer.
839 */
840 if (tail_page == cpu_buffer->tail_page) {
841 /* Zero the write counter */
842 unsigned long val = old_write & ~RB_WRITE_MASK;
843 unsigned long eval = old_entries & ~RB_WRITE_MASK;
844
845 /*
846 * This will only succeed if an interrupt did
847 * not come in and change it. In which case, we
848 * do not want to modify it.
Lai Jiangshanda706d82009-07-15 16:27:30 +0800849 *
850 * We add (void) to let the compiler know that we do not care
851 * about the return value of these functions. We use the
852 * cmpxchg to only update if an interrupt did not already
853 * do it for us. If the cmpxchg fails, we don't care.
Steven Rostedt77ae3652009-03-27 11:00:29 -0400854 */
Lai Jiangshanda706d82009-07-15 16:27:30 +0800855 (void)local_cmpxchg(&next_page->write, old_write, val);
856 (void)local_cmpxchg(&next_page->entries, old_entries, eval);
Steven Rostedt77ae3652009-03-27 11:00:29 -0400857
858 /*
859 * No need to worry about races with clearing out the commit.
860 * it only can increment when a commit takes place. But that
861 * only happens in the outer most nested commit.
862 */
863 local_set(&next_page->page->commit, 0);
864
865 old_tail = cmpxchg(&cpu_buffer->tail_page,
866 tail_page, next_page);
867
868 if (old_tail == tail_page)
869 ret = 1;
870 }
871
872 return ret;
873}
874
875static int rb_check_bpage(struct ring_buffer_per_cpu *cpu_buffer,
876 struct buffer_page *bpage)
877{
878 unsigned long val = (unsigned long)bpage;
879
880 if (RB_WARN_ON(cpu_buffer, val & RB_FLAG_MASK))
881 return 1;
882
883 return 0;
884}
885
886/**
887 * rb_check_list - make sure a pointer to a list has the last bits zero
888 */
889static int rb_check_list(struct ring_buffer_per_cpu *cpu_buffer,
890 struct list_head *list)
891{
892 if (RB_WARN_ON(cpu_buffer, rb_list_head(list->prev) != list->prev))
893 return 1;
894 if (RB_WARN_ON(cpu_buffer, rb_list_head(list->next) != list->next))
895 return 1;
896 return 0;
897}
898
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400899/**
900 * check_pages - integrity check of buffer pages
901 * @cpu_buffer: CPU buffer with pages to test
902 *
Wenji Huangc3706f02009-02-10 01:03:18 -0500903 * As a safety measure we check to make sure the data pages have not
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400904 * been corrupted.
905 */
906static int rb_check_pages(struct ring_buffer_per_cpu *cpu_buffer)
907{
Steven Rostedt3adc54f2009-03-30 15:32:01 -0400908 struct list_head *head = cpu_buffer->pages;
Steven Rostedt044fa782008-12-02 23:50:03 -0500909 struct buffer_page *bpage, *tmp;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400910
Steven Rostedt77ae3652009-03-27 11:00:29 -0400911 rb_head_page_deactivate(cpu_buffer);
912
Steven Rostedt3e89c7b2008-11-11 15:28:41 -0500913 if (RB_WARN_ON(cpu_buffer, head->next->prev != head))
914 return -1;
915 if (RB_WARN_ON(cpu_buffer, head->prev->next != head))
916 return -1;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400917
Steven Rostedt77ae3652009-03-27 11:00:29 -0400918 if (rb_check_list(cpu_buffer, head))
919 return -1;
920
Steven Rostedt044fa782008-12-02 23:50:03 -0500921 list_for_each_entry_safe(bpage, tmp, head, list) {
Steven Rostedt3e89c7b2008-11-11 15:28:41 -0500922 if (RB_WARN_ON(cpu_buffer,
Steven Rostedt044fa782008-12-02 23:50:03 -0500923 bpage->list.next->prev != &bpage->list))
Steven Rostedt3e89c7b2008-11-11 15:28:41 -0500924 return -1;
925 if (RB_WARN_ON(cpu_buffer,
Steven Rostedt044fa782008-12-02 23:50:03 -0500926 bpage->list.prev->next != &bpage->list))
Steven Rostedt3e89c7b2008-11-11 15:28:41 -0500927 return -1;
Steven Rostedt77ae3652009-03-27 11:00:29 -0400928 if (rb_check_list(cpu_buffer, &bpage->list))
929 return -1;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400930 }
931
Steven Rostedt77ae3652009-03-27 11:00:29 -0400932 rb_head_page_activate(cpu_buffer);
933
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400934 return 0;
935}
936
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400937static int rb_allocate_pages(struct ring_buffer_per_cpu *cpu_buffer,
938 unsigned nr_pages)
939{
Steven Rostedt044fa782008-12-02 23:50:03 -0500940 struct buffer_page *bpage, *tmp;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400941 unsigned long addr;
942 LIST_HEAD(pages);
943 unsigned i;
944
Steven Rostedt3adc54f2009-03-30 15:32:01 -0400945 WARN_ON(!nr_pages);
946
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400947 for (i = 0; i < nr_pages; i++) {
Steven Rostedt044fa782008-12-02 23:50:03 -0500948 bpage = kzalloc_node(ALIGN(sizeof(*bpage), cache_line_size()),
Steven Rostedtaa1e0e32008-10-02 19:18:09 -0400949 GFP_KERNEL, cpu_to_node(cpu_buffer->cpu));
Steven Rostedt044fa782008-12-02 23:50:03 -0500950 if (!bpage)
Steven Rostedte4c2ce82008-10-01 11:14:54 -0400951 goto free_pages;
Steven Rostedt77ae3652009-03-27 11:00:29 -0400952
953 rb_check_bpage(cpu_buffer, bpage);
954
Steven Rostedt044fa782008-12-02 23:50:03 -0500955 list_add(&bpage->list, &pages);
Steven Rostedte4c2ce82008-10-01 11:14:54 -0400956
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400957 addr = __get_free_page(GFP_KERNEL);
958 if (!addr)
959 goto free_pages;
Steven Rostedt044fa782008-12-02 23:50:03 -0500960 bpage->page = (void *)addr;
961 rb_init_page(bpage->page);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400962 }
963
Steven Rostedt3adc54f2009-03-30 15:32:01 -0400964 /*
965 * The ring buffer page list is a circular list that does not
966 * start and end with a list head. All page list items point to
967 * other pages.
968 */
969 cpu_buffer->pages = pages.next;
970 list_del(&pages);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400971
972 rb_check_pages(cpu_buffer);
973
974 return 0;
975
976 free_pages:
Steven Rostedt044fa782008-12-02 23:50:03 -0500977 list_for_each_entry_safe(bpage, tmp, &pages, list) {
978 list_del_init(&bpage->list);
979 free_buffer_page(bpage);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400980 }
981 return -ENOMEM;
982}
983
984static struct ring_buffer_per_cpu *
985rb_allocate_cpu_buffer(struct ring_buffer *buffer, int cpu)
986{
987 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedt044fa782008-12-02 23:50:03 -0500988 struct buffer_page *bpage;
Steven Rostedtd7690412008-10-01 00:29:53 -0400989 unsigned long addr;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400990 int ret;
991
992 cpu_buffer = kzalloc_node(ALIGN(sizeof(*cpu_buffer), cache_line_size()),
993 GFP_KERNEL, cpu_to_node(cpu));
994 if (!cpu_buffer)
995 return NULL;
996
997 cpu_buffer->cpu = cpu;
998 cpu_buffer->buffer = buffer;
Steven Rostedtf83c9d02008-11-11 18:47:44 +0100999 spin_lock_init(&cpu_buffer->reader_lock);
Peter Zijlstra1f8a6a12009-06-08 18:18:39 +02001000 lockdep_set_class(&cpu_buffer->reader_lock, buffer->reader_lock_key);
Steven Rostedt3e03fb72008-11-06 00:09:43 -05001001 cpu_buffer->lock = (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001002
Steven Rostedt044fa782008-12-02 23:50:03 -05001003 bpage = kzalloc_node(ALIGN(sizeof(*bpage), cache_line_size()),
Steven Rostedte4c2ce82008-10-01 11:14:54 -04001004 GFP_KERNEL, cpu_to_node(cpu));
Steven Rostedt044fa782008-12-02 23:50:03 -05001005 if (!bpage)
Steven Rostedte4c2ce82008-10-01 11:14:54 -04001006 goto fail_free_buffer;
1007
Steven Rostedt77ae3652009-03-27 11:00:29 -04001008 rb_check_bpage(cpu_buffer, bpage);
1009
Steven Rostedt044fa782008-12-02 23:50:03 -05001010 cpu_buffer->reader_page = bpage;
Steven Rostedtd7690412008-10-01 00:29:53 -04001011 addr = __get_free_page(GFP_KERNEL);
1012 if (!addr)
Steven Rostedte4c2ce82008-10-01 11:14:54 -04001013 goto fail_free_reader;
Steven Rostedt044fa782008-12-02 23:50:03 -05001014 bpage->page = (void *)addr;
1015 rb_init_page(bpage->page);
Steven Rostedte4c2ce82008-10-01 11:14:54 -04001016
Steven Rostedtd7690412008-10-01 00:29:53 -04001017 INIT_LIST_HEAD(&cpu_buffer->reader_page->list);
Steven Rostedtd7690412008-10-01 00:29:53 -04001018
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001019 ret = rb_allocate_pages(cpu_buffer, buffer->pages);
1020 if (ret < 0)
Steven Rostedtd7690412008-10-01 00:29:53 -04001021 goto fail_free_reader;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001022
1023 cpu_buffer->head_page
Steven Rostedt3adc54f2009-03-30 15:32:01 -04001024 = list_entry(cpu_buffer->pages, struct buffer_page, list);
Steven Rostedtbf41a152008-10-04 02:00:59 -04001025 cpu_buffer->tail_page = cpu_buffer->commit_page = cpu_buffer->head_page;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001026
Steven Rostedt77ae3652009-03-27 11:00:29 -04001027 rb_head_page_activate(cpu_buffer);
1028
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001029 return cpu_buffer;
1030
Steven Rostedtd7690412008-10-01 00:29:53 -04001031 fail_free_reader:
1032 free_buffer_page(cpu_buffer->reader_page);
1033
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001034 fail_free_buffer:
1035 kfree(cpu_buffer);
1036 return NULL;
1037}
1038
1039static void rb_free_cpu_buffer(struct ring_buffer_per_cpu *cpu_buffer)
1040{
Steven Rostedt3adc54f2009-03-30 15:32:01 -04001041 struct list_head *head = cpu_buffer->pages;
Steven Rostedt044fa782008-12-02 23:50:03 -05001042 struct buffer_page *bpage, *tmp;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001043
Steven Rostedtd7690412008-10-01 00:29:53 -04001044 free_buffer_page(cpu_buffer->reader_page);
1045
Steven Rostedt77ae3652009-03-27 11:00:29 -04001046 rb_head_page_deactivate(cpu_buffer);
1047
Steven Rostedt3adc54f2009-03-30 15:32:01 -04001048 if (head) {
1049 list_for_each_entry_safe(bpage, tmp, head, list) {
1050 list_del_init(&bpage->list);
1051 free_buffer_page(bpage);
1052 }
1053 bpage = list_entry(head, struct buffer_page, list);
Steven Rostedt044fa782008-12-02 23:50:03 -05001054 free_buffer_page(bpage);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001055 }
Steven Rostedt3adc54f2009-03-30 15:32:01 -04001056
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001057 kfree(cpu_buffer);
1058}
1059
Steven Rostedt59222ef2009-03-12 11:46:03 -04001060#ifdef CONFIG_HOTPLUG_CPU
Frederic Weisbecker09c9e842009-03-21 04:33:36 +01001061static int rb_cpu_notify(struct notifier_block *self,
1062 unsigned long action, void *hcpu);
Steven Rostedt554f7862009-03-11 22:00:13 -04001063#endif
1064
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001065/**
1066 * ring_buffer_alloc - allocate a new ring_buffer
Robert Richter68814b52008-11-24 12:24:12 +01001067 * @size: the size in bytes per cpu that is needed.
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001068 * @flags: attributes to set for the ring buffer.
1069 *
1070 * Currently the only flag that is available is the RB_FL_OVERWRITE
1071 * flag. This flag means that the buffer will overwrite old data
1072 * when the buffer wraps. If this flag is not set, the buffer will
1073 * drop data when the tail hits the head.
1074 */
Peter Zijlstra1f8a6a12009-06-08 18:18:39 +02001075struct ring_buffer *__ring_buffer_alloc(unsigned long size, unsigned flags,
1076 struct lock_class_key *key)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001077{
1078 struct ring_buffer *buffer;
1079 int bsize;
1080 int cpu;
1081
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001082 /* keep it in its own cache line */
1083 buffer = kzalloc(ALIGN(sizeof(*buffer), cache_line_size()),
1084 GFP_KERNEL);
1085 if (!buffer)
1086 return NULL;
1087
Rusty Russell9e01c1b2009-01-01 10:12:22 +10301088 if (!alloc_cpumask_var(&buffer->cpumask, GFP_KERNEL))
1089 goto fail_free_buffer;
1090
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001091 buffer->pages = DIV_ROUND_UP(size, BUF_PAGE_SIZE);
1092 buffer->flags = flags;
Steven Rostedt37886f62009-03-17 17:22:06 -04001093 buffer->clock = trace_clock_local;
Peter Zijlstra1f8a6a12009-06-08 18:18:39 +02001094 buffer->reader_lock_key = key;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001095
1096 /* need at least two pages */
Steven Rostedt5f78abe2009-06-17 14:11:10 -04001097 if (buffer->pages < 2)
1098 buffer->pages = 2;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001099
Frederic Weisbecker3bf832c2009-03-19 14:47:33 +01001100 /*
1101 * In case of non-hotplug cpu, if the ring-buffer is allocated
1102 * in early initcall, it will not be notified of secondary cpus.
1103 * In that off case, we need to allocate for all possible cpus.
1104 */
1105#ifdef CONFIG_HOTPLUG_CPU
Steven Rostedt554f7862009-03-11 22:00:13 -04001106 get_online_cpus();
1107 cpumask_copy(buffer->cpumask, cpu_online_mask);
Frederic Weisbecker3bf832c2009-03-19 14:47:33 +01001108#else
1109 cpumask_copy(buffer->cpumask, cpu_possible_mask);
1110#endif
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001111 buffer->cpus = nr_cpu_ids;
1112
1113 bsize = sizeof(void *) * nr_cpu_ids;
1114 buffer->buffers = kzalloc(ALIGN(bsize, cache_line_size()),
1115 GFP_KERNEL);
1116 if (!buffer->buffers)
Rusty Russell9e01c1b2009-01-01 10:12:22 +10301117 goto fail_free_cpumask;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001118
1119 for_each_buffer_cpu(buffer, cpu) {
1120 buffer->buffers[cpu] =
1121 rb_allocate_cpu_buffer(buffer, cpu);
1122 if (!buffer->buffers[cpu])
1123 goto fail_free_buffers;
1124 }
1125
Steven Rostedt59222ef2009-03-12 11:46:03 -04001126#ifdef CONFIG_HOTPLUG_CPU
Steven Rostedt554f7862009-03-11 22:00:13 -04001127 buffer->cpu_notify.notifier_call = rb_cpu_notify;
1128 buffer->cpu_notify.priority = 0;
1129 register_cpu_notifier(&buffer->cpu_notify);
1130#endif
1131
1132 put_online_cpus();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001133 mutex_init(&buffer->mutex);
1134
1135 return buffer;
1136
1137 fail_free_buffers:
1138 for_each_buffer_cpu(buffer, cpu) {
1139 if (buffer->buffers[cpu])
1140 rb_free_cpu_buffer(buffer->buffers[cpu]);
1141 }
1142 kfree(buffer->buffers);
1143
Rusty Russell9e01c1b2009-01-01 10:12:22 +10301144 fail_free_cpumask:
1145 free_cpumask_var(buffer->cpumask);
Steven Rostedt554f7862009-03-11 22:00:13 -04001146 put_online_cpus();
Rusty Russell9e01c1b2009-01-01 10:12:22 +10301147
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001148 fail_free_buffer:
1149 kfree(buffer);
1150 return NULL;
1151}
Peter Zijlstra1f8a6a12009-06-08 18:18:39 +02001152EXPORT_SYMBOL_GPL(__ring_buffer_alloc);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001153
1154/**
1155 * ring_buffer_free - free a ring buffer.
1156 * @buffer: the buffer to free.
1157 */
1158void
1159ring_buffer_free(struct ring_buffer *buffer)
1160{
1161 int cpu;
1162
Steven Rostedt554f7862009-03-11 22:00:13 -04001163 get_online_cpus();
1164
Steven Rostedt59222ef2009-03-12 11:46:03 -04001165#ifdef CONFIG_HOTPLUG_CPU
Steven Rostedt554f7862009-03-11 22:00:13 -04001166 unregister_cpu_notifier(&buffer->cpu_notify);
1167#endif
1168
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001169 for_each_buffer_cpu(buffer, cpu)
1170 rb_free_cpu_buffer(buffer->buffers[cpu]);
1171
Steven Rostedt554f7862009-03-11 22:00:13 -04001172 put_online_cpus();
1173
Eric Dumazetbd3f0222009-08-07 12:49:29 +02001174 kfree(buffer->buffers);
Rusty Russell9e01c1b2009-01-01 10:12:22 +10301175 free_cpumask_var(buffer->cpumask);
1176
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001177 kfree(buffer);
1178}
Robert Richterc4f50182008-12-11 16:49:22 +01001179EXPORT_SYMBOL_GPL(ring_buffer_free);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001180
Steven Rostedt37886f62009-03-17 17:22:06 -04001181void ring_buffer_set_clock(struct ring_buffer *buffer,
1182 u64 (*clock)(void))
1183{
1184 buffer->clock = clock;
1185}
1186
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001187static void rb_reset_cpu(struct ring_buffer_per_cpu *cpu_buffer);
1188
1189static void
1190rb_remove_pages(struct ring_buffer_per_cpu *cpu_buffer, unsigned nr_pages)
1191{
Steven Rostedt044fa782008-12-02 23:50:03 -05001192 struct buffer_page *bpage;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001193 struct list_head *p;
1194 unsigned i;
1195
1196 atomic_inc(&cpu_buffer->record_disabled);
1197 synchronize_sched();
1198
Lai Jiangshanf7112942009-11-03 19:42:45 +08001199 spin_lock_irq(&cpu_buffer->reader_lock);
Steven Rostedt77ae3652009-03-27 11:00:29 -04001200 rb_head_page_deactivate(cpu_buffer);
1201
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001202 for (i = 0; i < nr_pages; i++) {
Steven Rostedt3adc54f2009-03-30 15:32:01 -04001203 if (RB_WARN_ON(cpu_buffer, list_empty(cpu_buffer->pages)))
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05001204 return;
Steven Rostedt3adc54f2009-03-30 15:32:01 -04001205 p = cpu_buffer->pages->next;
Steven Rostedt044fa782008-12-02 23:50:03 -05001206 bpage = list_entry(p, struct buffer_page, list);
1207 list_del_init(&bpage->list);
1208 free_buffer_page(bpage);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001209 }
Steven Rostedt3adc54f2009-03-30 15:32:01 -04001210 if (RB_WARN_ON(cpu_buffer, list_empty(cpu_buffer->pages)))
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05001211 return;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001212
1213 rb_reset_cpu(cpu_buffer);
Lai Jiangshanf7112942009-11-03 19:42:45 +08001214 spin_unlock_irq(&cpu_buffer->reader_lock);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001215
1216 rb_check_pages(cpu_buffer);
1217
1218 atomic_dec(&cpu_buffer->record_disabled);
1219
1220}
1221
1222static void
1223rb_insert_pages(struct ring_buffer_per_cpu *cpu_buffer,
1224 struct list_head *pages, unsigned nr_pages)
1225{
Steven Rostedt044fa782008-12-02 23:50:03 -05001226 struct buffer_page *bpage;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001227 struct list_head *p;
1228 unsigned i;
1229
1230 atomic_inc(&cpu_buffer->record_disabled);
1231 synchronize_sched();
1232
Steven Rostedt77ae3652009-03-27 11:00:29 -04001233 spin_lock_irq(&cpu_buffer->reader_lock);
1234 rb_head_page_deactivate(cpu_buffer);
1235
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001236 for (i = 0; i < nr_pages; i++) {
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05001237 if (RB_WARN_ON(cpu_buffer, list_empty(pages)))
1238 return;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001239 p = pages->next;
Steven Rostedt044fa782008-12-02 23:50:03 -05001240 bpage = list_entry(p, struct buffer_page, list);
1241 list_del_init(&bpage->list);
Steven Rostedt3adc54f2009-03-30 15:32:01 -04001242 list_add_tail(&bpage->list, cpu_buffer->pages);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001243 }
1244 rb_reset_cpu(cpu_buffer);
Steven Rostedt77ae3652009-03-27 11:00:29 -04001245 spin_unlock_irq(&cpu_buffer->reader_lock);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001246
1247 rb_check_pages(cpu_buffer);
1248
1249 atomic_dec(&cpu_buffer->record_disabled);
1250}
1251
1252/**
1253 * ring_buffer_resize - resize the ring buffer
1254 * @buffer: the buffer to resize.
1255 * @size: the new size.
1256 *
1257 * The tracer is responsible for making sure that the buffer is
1258 * not being used while changing the size.
1259 * Note: We may be able to change the above requirement by using
1260 * RCU synchronizations.
1261 *
1262 * Minimum size is 2 * BUF_PAGE_SIZE.
1263 *
1264 * Returns -1 on failure.
1265 */
1266int ring_buffer_resize(struct ring_buffer *buffer, unsigned long size)
1267{
1268 struct ring_buffer_per_cpu *cpu_buffer;
1269 unsigned nr_pages, rm_pages, new_pages;
Steven Rostedt044fa782008-12-02 23:50:03 -05001270 struct buffer_page *bpage, *tmp;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001271 unsigned long buffer_size;
1272 unsigned long addr;
1273 LIST_HEAD(pages);
1274 int i, cpu;
1275
Ingo Molnaree51a1d2008-11-13 14:58:31 +01001276 /*
1277 * Always succeed at resizing a non-existent buffer:
1278 */
1279 if (!buffer)
1280 return size;
1281
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001282 size = DIV_ROUND_UP(size, BUF_PAGE_SIZE);
1283 size *= BUF_PAGE_SIZE;
1284 buffer_size = buffer->pages * BUF_PAGE_SIZE;
1285
1286 /* we need a minimum of two pages */
1287 if (size < BUF_PAGE_SIZE * 2)
1288 size = BUF_PAGE_SIZE * 2;
1289
1290 if (size == buffer_size)
1291 return size;
1292
1293 mutex_lock(&buffer->mutex);
Steven Rostedt554f7862009-03-11 22:00:13 -04001294 get_online_cpus();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001295
1296 nr_pages = DIV_ROUND_UP(size, BUF_PAGE_SIZE);
1297
1298 if (size < buffer_size) {
1299
1300 /* easy case, just free pages */
Steven Rostedt554f7862009-03-11 22:00:13 -04001301 if (RB_WARN_ON(buffer, nr_pages >= buffer->pages))
1302 goto out_fail;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001303
1304 rm_pages = buffer->pages - nr_pages;
1305
1306 for_each_buffer_cpu(buffer, cpu) {
1307 cpu_buffer = buffer->buffers[cpu];
1308 rb_remove_pages(cpu_buffer, rm_pages);
1309 }
1310 goto out;
1311 }
1312
1313 /*
1314 * This is a bit more difficult. We only want to add pages
1315 * when we can allocate enough for all CPUs. We do this
1316 * by allocating all the pages and storing them on a local
1317 * link list. If we succeed in our allocation, then we
1318 * add these pages to the cpu_buffers. Otherwise we just free
1319 * them all and return -ENOMEM;
1320 */
Steven Rostedt554f7862009-03-11 22:00:13 -04001321 if (RB_WARN_ON(buffer, nr_pages <= buffer->pages))
1322 goto out_fail;
Steven Rostedtf536aaf2008-11-10 23:07:30 -05001323
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001324 new_pages = nr_pages - buffer->pages;
1325
1326 for_each_buffer_cpu(buffer, cpu) {
1327 for (i = 0; i < new_pages; i++) {
Steven Rostedt044fa782008-12-02 23:50:03 -05001328 bpage = kzalloc_node(ALIGN(sizeof(*bpage),
Steven Rostedte4c2ce82008-10-01 11:14:54 -04001329 cache_line_size()),
1330 GFP_KERNEL, cpu_to_node(cpu));
Steven Rostedt044fa782008-12-02 23:50:03 -05001331 if (!bpage)
Steven Rostedte4c2ce82008-10-01 11:14:54 -04001332 goto free_pages;
Steven Rostedt044fa782008-12-02 23:50:03 -05001333 list_add(&bpage->list, &pages);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001334 addr = __get_free_page(GFP_KERNEL);
1335 if (!addr)
1336 goto free_pages;
Steven Rostedt044fa782008-12-02 23:50:03 -05001337 bpage->page = (void *)addr;
1338 rb_init_page(bpage->page);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001339 }
1340 }
1341
1342 for_each_buffer_cpu(buffer, cpu) {
1343 cpu_buffer = buffer->buffers[cpu];
1344 rb_insert_pages(cpu_buffer, &pages, new_pages);
1345 }
1346
Steven Rostedt554f7862009-03-11 22:00:13 -04001347 if (RB_WARN_ON(buffer, !list_empty(&pages)))
1348 goto out_fail;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001349
1350 out:
1351 buffer->pages = nr_pages;
Steven Rostedt554f7862009-03-11 22:00:13 -04001352 put_online_cpus();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001353 mutex_unlock(&buffer->mutex);
1354
1355 return size;
1356
1357 free_pages:
Steven Rostedt044fa782008-12-02 23:50:03 -05001358 list_for_each_entry_safe(bpage, tmp, &pages, list) {
1359 list_del_init(&bpage->list);
1360 free_buffer_page(bpage);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001361 }
Steven Rostedt554f7862009-03-11 22:00:13 -04001362 put_online_cpus();
Vegard Nossum641d2f62008-11-18 19:22:13 +01001363 mutex_unlock(&buffer->mutex);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001364 return -ENOMEM;
Steven Rostedt554f7862009-03-11 22:00:13 -04001365
1366 /*
1367 * Something went totally wrong, and we are too paranoid
1368 * to even clean up the mess.
1369 */
1370 out_fail:
1371 put_online_cpus();
1372 mutex_unlock(&buffer->mutex);
1373 return -1;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001374}
Robert Richterc4f50182008-12-11 16:49:22 +01001375EXPORT_SYMBOL_GPL(ring_buffer_resize);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001376
Steven Rostedt8789a9e2008-12-02 15:34:07 -05001377static inline void *
Steven Rostedt044fa782008-12-02 23:50:03 -05001378__rb_data_page_index(struct buffer_data_page *bpage, unsigned index)
Steven Rostedt8789a9e2008-12-02 15:34:07 -05001379{
Steven Rostedt044fa782008-12-02 23:50:03 -05001380 return bpage->data + index;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05001381}
1382
Steven Rostedt044fa782008-12-02 23:50:03 -05001383static inline void *__rb_page_index(struct buffer_page *bpage, unsigned index)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001384{
Steven Rostedt044fa782008-12-02 23:50:03 -05001385 return bpage->page->data + index;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001386}
1387
1388static inline struct ring_buffer_event *
Steven Rostedtd7690412008-10-01 00:29:53 -04001389rb_reader_event(struct ring_buffer_per_cpu *cpu_buffer)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001390{
Steven Rostedt6f807ac2008-10-04 02:00:58 -04001391 return __rb_page_index(cpu_buffer->reader_page,
1392 cpu_buffer->reader_page->read);
1393}
1394
1395static inline struct ring_buffer_event *
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001396rb_iter_head_event(struct ring_buffer_iter *iter)
1397{
Steven Rostedt6f807ac2008-10-04 02:00:58 -04001398 return __rb_page_index(iter->head_page, iter->head);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001399}
1400
Steven Rostedt77ae3652009-03-27 11:00:29 -04001401static inline unsigned long rb_page_write(struct buffer_page *bpage)
Steven Rostedtbf41a152008-10-04 02:00:59 -04001402{
Steven Rostedt77ae3652009-03-27 11:00:29 -04001403 return local_read(&bpage->write) & RB_WRITE_MASK;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001404}
1405
1406static inline unsigned rb_page_commit(struct buffer_page *bpage)
1407{
Steven Rostedtabc9b562008-12-02 15:34:06 -05001408 return local_read(&bpage->page->commit);
Steven Rostedtbf41a152008-10-04 02:00:59 -04001409}
1410
Steven Rostedt77ae3652009-03-27 11:00:29 -04001411static inline unsigned long rb_page_entries(struct buffer_page *bpage)
1412{
1413 return local_read(&bpage->entries) & RB_WRITE_MASK;
1414}
1415
Steven Rostedtbf41a152008-10-04 02:00:59 -04001416/* Size is determined by what has been commited */
1417static inline unsigned rb_page_size(struct buffer_page *bpage)
1418{
1419 return rb_page_commit(bpage);
1420}
1421
1422static inline unsigned
1423rb_commit_index(struct ring_buffer_per_cpu *cpu_buffer)
1424{
1425 return rb_page_commit(cpu_buffer->commit_page);
1426}
1427
Steven Rostedtbf41a152008-10-04 02:00:59 -04001428static inline unsigned
1429rb_event_index(struct ring_buffer_event *event)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001430{
Steven Rostedtbf41a152008-10-04 02:00:59 -04001431 unsigned long addr = (unsigned long)event;
1432
Steven Rostedt22f470f2009-06-11 09:29:58 -04001433 return (addr & ~PAGE_MASK) - BUF_PAGE_HDR_SIZE;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001434}
1435
Steven Rostedt0f0c85f2009-05-11 16:08:00 -04001436static inline int
Steven Rostedtfa743952009-06-16 12:37:57 -04001437rb_event_is_commit(struct ring_buffer_per_cpu *cpu_buffer,
1438 struct ring_buffer_event *event)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001439{
Steven Rostedtbf41a152008-10-04 02:00:59 -04001440 unsigned long addr = (unsigned long)event;
1441 unsigned long index;
1442
1443 index = rb_event_index(event);
1444 addr &= PAGE_MASK;
1445
1446 return cpu_buffer->commit_page->page == (void *)addr &&
1447 rb_commit_index(cpu_buffer) == index;
1448}
1449
Andrew Morton34a148b2009-01-09 12:27:09 -08001450static void
Steven Rostedtbf41a152008-10-04 02:00:59 -04001451rb_set_commit_to_write(struct ring_buffer_per_cpu *cpu_buffer)
1452{
Steven Rostedt77ae3652009-03-27 11:00:29 -04001453 unsigned long max_count;
1454
Steven Rostedtbf41a152008-10-04 02:00:59 -04001455 /*
1456 * We only race with interrupts and NMIs on this CPU.
1457 * If we own the commit event, then we can commit
1458 * all others that interrupted us, since the interruptions
1459 * are in stack format (they finish before they come
1460 * back to us). This allows us to do a simple loop to
1461 * assign the commit to the tail.
1462 */
Steven Rostedta8ccf1d2008-12-23 11:32:24 -05001463 again:
Steven Rostedt77ae3652009-03-27 11:00:29 -04001464 max_count = cpu_buffer->buffer->pages * 100;
1465
Steven Rostedtbf41a152008-10-04 02:00:59 -04001466 while (cpu_buffer->commit_page != cpu_buffer->tail_page) {
Steven Rostedt77ae3652009-03-27 11:00:29 -04001467 if (RB_WARN_ON(cpu_buffer, !(--max_count)))
1468 return;
1469 if (RB_WARN_ON(cpu_buffer,
1470 rb_is_reader_page(cpu_buffer->tail_page)))
1471 return;
1472 local_set(&cpu_buffer->commit_page->page->commit,
1473 rb_page_write(cpu_buffer->commit_page));
Steven Rostedtbf41a152008-10-04 02:00:59 -04001474 rb_inc_page(cpu_buffer, &cpu_buffer->commit_page);
Steven Rostedtabc9b562008-12-02 15:34:06 -05001475 cpu_buffer->write_stamp =
1476 cpu_buffer->commit_page->page->time_stamp;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001477 /* add barrier to keep gcc from optimizing too much */
1478 barrier();
1479 }
1480 while (rb_commit_index(cpu_buffer) !=
1481 rb_page_write(cpu_buffer->commit_page)) {
Steven Rostedt77ae3652009-03-27 11:00:29 -04001482
1483 local_set(&cpu_buffer->commit_page->page->commit,
1484 rb_page_write(cpu_buffer->commit_page));
1485 RB_WARN_ON(cpu_buffer,
1486 local_read(&cpu_buffer->commit_page->page->commit) &
1487 ~RB_WRITE_MASK);
Steven Rostedtbf41a152008-10-04 02:00:59 -04001488 barrier();
1489 }
Steven Rostedta8ccf1d2008-12-23 11:32:24 -05001490
1491 /* again, keep gcc from optimizing */
1492 barrier();
1493
1494 /*
1495 * If an interrupt came in just after the first while loop
1496 * and pushed the tail page forward, we will be left with
1497 * a dangling commit that will never go forward.
1498 */
1499 if (unlikely(cpu_buffer->commit_page != cpu_buffer->tail_page))
1500 goto again;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001501}
1502
Steven Rostedtd7690412008-10-01 00:29:53 -04001503static void rb_reset_reader_page(struct ring_buffer_per_cpu *cpu_buffer)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001504{
Steven Rostedtabc9b562008-12-02 15:34:06 -05001505 cpu_buffer->read_stamp = cpu_buffer->reader_page->page->time_stamp;
Steven Rostedt6f807ac2008-10-04 02:00:58 -04001506 cpu_buffer->reader_page->read = 0;
Steven Rostedtd7690412008-10-01 00:29:53 -04001507}
1508
Andrew Morton34a148b2009-01-09 12:27:09 -08001509static void rb_inc_iter(struct ring_buffer_iter *iter)
Steven Rostedtd7690412008-10-01 00:29:53 -04001510{
1511 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
1512
1513 /*
1514 * The iterator could be on the reader page (it starts there).
1515 * But the head could have moved, since the reader was
1516 * found. Check for this case and assign the iterator
1517 * to the head page instead of next.
1518 */
1519 if (iter->head_page == cpu_buffer->reader_page)
Steven Rostedt77ae3652009-03-27 11:00:29 -04001520 iter->head_page = rb_set_head_page(cpu_buffer);
Steven Rostedtd7690412008-10-01 00:29:53 -04001521 else
1522 rb_inc_page(cpu_buffer, &iter->head_page);
1523
Steven Rostedtabc9b562008-12-02 15:34:06 -05001524 iter->read_stamp = iter->head_page->page->time_stamp;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001525 iter->head = 0;
1526}
1527
1528/**
1529 * ring_buffer_update_event - update event type and data
1530 * @event: the even to update
1531 * @type: the type of event
1532 * @length: the size of the event field in the ring buffer
1533 *
1534 * Update the type and data fields of the event. The length
1535 * is the actual size that is written to the ring buffer,
1536 * and with this, we can determine what to place into the
1537 * data field.
1538 */
Andrew Morton34a148b2009-01-09 12:27:09 -08001539static void
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001540rb_update_event(struct ring_buffer_event *event,
1541 unsigned type, unsigned length)
1542{
Lai Jiangshan334d4162009-04-24 11:27:05 +08001543 event->type_len = type;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001544
1545 switch (type) {
1546
1547 case RINGBUF_TYPE_PADDING:
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001548 case RINGBUF_TYPE_TIME_EXTEND:
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001549 case RINGBUF_TYPE_TIME_STAMP:
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001550 break;
1551
Lai Jiangshan334d4162009-04-24 11:27:05 +08001552 case 0:
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001553 length -= RB_EVNT_HDR_SIZE;
Lai Jiangshan334d4162009-04-24 11:27:05 +08001554 if (length > RB_MAX_SMALL_DATA)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001555 event->array[0] = length;
Lai Jiangshan334d4162009-04-24 11:27:05 +08001556 else
1557 event->type_len = DIV_ROUND_UP(length, RB_ALIGNMENT);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001558 break;
1559 default:
1560 BUG();
1561 }
1562}
1563
Steven Rostedt77ae3652009-03-27 11:00:29 -04001564/*
1565 * rb_handle_head_page - writer hit the head page
1566 *
1567 * Returns: +1 to retry page
1568 * 0 to continue
1569 * -1 on error
1570 */
1571static int
1572rb_handle_head_page(struct ring_buffer_per_cpu *cpu_buffer,
1573 struct buffer_page *tail_page,
1574 struct buffer_page *next_page)
1575{
1576 struct buffer_page *new_head;
1577 int entries;
1578 int type;
1579 int ret;
1580
1581 entries = rb_page_entries(next_page);
1582
1583 /*
1584 * The hard part is here. We need to move the head
1585 * forward, and protect against both readers on
1586 * other CPUs and writers coming in via interrupts.
1587 */
1588 type = rb_head_page_set_update(cpu_buffer, next_page, tail_page,
1589 RB_PAGE_HEAD);
1590
1591 /*
1592 * type can be one of four:
1593 * NORMAL - an interrupt already moved it for us
1594 * HEAD - we are the first to get here.
1595 * UPDATE - we are the interrupt interrupting
1596 * a current move.
1597 * MOVED - a reader on another CPU moved the next
1598 * pointer to its reader page. Give up
1599 * and try again.
1600 */
1601
1602 switch (type) {
1603 case RB_PAGE_HEAD:
1604 /*
1605 * We changed the head to UPDATE, thus
1606 * it is our responsibility to update
1607 * the counters.
1608 */
1609 local_add(entries, &cpu_buffer->overrun);
1610
1611 /*
1612 * The entries will be zeroed out when we move the
1613 * tail page.
1614 */
1615
1616 /* still more to do */
1617 break;
1618
1619 case RB_PAGE_UPDATE:
1620 /*
1621 * This is an interrupt that interrupt the
1622 * previous update. Still more to do.
1623 */
1624 break;
1625 case RB_PAGE_NORMAL:
1626 /*
1627 * An interrupt came in before the update
1628 * and processed this for us.
1629 * Nothing left to do.
1630 */
1631 return 1;
1632 case RB_PAGE_MOVED:
1633 /*
1634 * The reader is on another CPU and just did
1635 * a swap with our next_page.
1636 * Try again.
1637 */
1638 return 1;
1639 default:
1640 RB_WARN_ON(cpu_buffer, 1); /* WTF??? */
1641 return -1;
1642 }
1643
1644 /*
1645 * Now that we are here, the old head pointer is
1646 * set to UPDATE. This will keep the reader from
1647 * swapping the head page with the reader page.
1648 * The reader (on another CPU) will spin till
1649 * we are finished.
1650 *
1651 * We just need to protect against interrupts
1652 * doing the job. We will set the next pointer
1653 * to HEAD. After that, we set the old pointer
1654 * to NORMAL, but only if it was HEAD before.
1655 * otherwise we are an interrupt, and only
1656 * want the outer most commit to reset it.
1657 */
1658 new_head = next_page;
1659 rb_inc_page(cpu_buffer, &new_head);
1660
1661 ret = rb_head_page_set_head(cpu_buffer, new_head, next_page,
1662 RB_PAGE_NORMAL);
1663
1664 /*
1665 * Valid returns are:
1666 * HEAD - an interrupt came in and already set it.
1667 * NORMAL - One of two things:
1668 * 1) We really set it.
1669 * 2) A bunch of interrupts came in and moved
1670 * the page forward again.
1671 */
1672 switch (ret) {
1673 case RB_PAGE_HEAD:
1674 case RB_PAGE_NORMAL:
1675 /* OK */
1676 break;
1677 default:
1678 RB_WARN_ON(cpu_buffer, 1);
1679 return -1;
1680 }
1681
1682 /*
1683 * It is possible that an interrupt came in,
1684 * set the head up, then more interrupts came in
1685 * and moved it again. When we get back here,
1686 * the page would have been set to NORMAL but we
1687 * just set it back to HEAD.
1688 *
1689 * How do you detect this? Well, if that happened
1690 * the tail page would have moved.
1691 */
1692 if (ret == RB_PAGE_NORMAL) {
1693 /*
1694 * If the tail had moved passed next, then we need
1695 * to reset the pointer.
1696 */
1697 if (cpu_buffer->tail_page != tail_page &&
1698 cpu_buffer->tail_page != next_page)
1699 rb_head_page_set_normal(cpu_buffer, new_head,
1700 next_page,
1701 RB_PAGE_HEAD);
1702 }
1703
1704 /*
1705 * If this was the outer most commit (the one that
1706 * changed the original pointer from HEAD to UPDATE),
1707 * then it is up to us to reset it to NORMAL.
1708 */
1709 if (type == RB_PAGE_HEAD) {
1710 ret = rb_head_page_set_normal(cpu_buffer, next_page,
1711 tail_page,
1712 RB_PAGE_UPDATE);
1713 if (RB_WARN_ON(cpu_buffer,
1714 ret != RB_PAGE_UPDATE))
1715 return -1;
1716 }
1717
1718 return 0;
1719}
1720
Andrew Morton34a148b2009-01-09 12:27:09 -08001721static unsigned rb_calculate_event_length(unsigned length)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001722{
1723 struct ring_buffer_event event; /* Used only for sizeof array */
1724
1725 /* zero length can cause confusions */
1726 if (!length)
1727 length = 1;
1728
1729 if (length > RB_MAX_SMALL_DATA)
1730 length += sizeof(event.array[0]);
1731
1732 length += RB_EVNT_HDR_SIZE;
1733 length = ALIGN(length, RB_ALIGNMENT);
1734
1735 return length;
1736}
1737
Steven Rostedtc7b09302009-06-11 11:12:00 -04001738static inline void
1739rb_reset_tail(struct ring_buffer_per_cpu *cpu_buffer,
1740 struct buffer_page *tail_page,
1741 unsigned long tail, unsigned long length)
1742{
1743 struct ring_buffer_event *event;
1744
1745 /*
1746 * Only the event that crossed the page boundary
1747 * must fill the old tail_page with padding.
1748 */
1749 if (tail >= BUF_PAGE_SIZE) {
1750 local_sub(length, &tail_page->write);
1751 return;
1752 }
1753
1754 event = __rb_page_index(tail_page, tail);
Linus Torvaldsb0b70652009-06-20 10:56:46 -07001755 kmemcheck_annotate_bitfield(event, bitfield);
Steven Rostedtc7b09302009-06-11 11:12:00 -04001756
1757 /*
1758 * If this event is bigger than the minimum size, then
1759 * we need to be careful that we don't subtract the
1760 * write counter enough to allow another writer to slip
1761 * in on this page.
1762 * We put in a discarded commit instead, to make sure
1763 * that this space is not used again.
1764 *
1765 * If we are less than the minimum size, we don't need to
1766 * worry about it.
1767 */
1768 if (tail > (BUF_PAGE_SIZE - RB_EVNT_MIN_SIZE)) {
1769 /* No room for any events */
1770
1771 /* Mark the rest of the page with padding */
1772 rb_event_set_padding(event);
1773
1774 /* Set the write back to the previous setting */
1775 local_sub(length, &tail_page->write);
1776 return;
1777 }
1778
1779 /* Put in a discarded event */
1780 event->array[0] = (BUF_PAGE_SIZE - tail) - RB_EVNT_HDR_SIZE;
1781 event->type_len = RINGBUF_TYPE_PADDING;
1782 /* time delta must be non zero */
1783 event->time_delta = 1;
Steven Rostedtc7b09302009-06-11 11:12:00 -04001784
1785 /* Set write to end of buffer */
1786 length = (tail + length) - BUF_PAGE_SIZE;
1787 local_sub(length, &tail_page->write);
1788}
Steven Rostedt6634ff22009-05-06 15:30:07 -04001789
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001790static struct ring_buffer_event *
Steven Rostedt6634ff22009-05-06 15:30:07 -04001791rb_move_tail(struct ring_buffer_per_cpu *cpu_buffer,
1792 unsigned long length, unsigned long tail,
1793 struct buffer_page *commit_page,
1794 struct buffer_page *tail_page, u64 *ts)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001795{
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001796 struct ring_buffer *buffer = cpu_buffer->buffer;
Steven Rostedt77ae3652009-03-27 11:00:29 -04001797 struct buffer_page *next_page;
1798 int ret;
Steven Rostedtaa20ae82009-05-05 21:16:11 -04001799
1800 next_page = tail_page;
1801
Steven Rostedtaa20ae82009-05-05 21:16:11 -04001802 rb_inc_page(cpu_buffer, &next_page);
1803
Steven Rostedtaa20ae82009-05-05 21:16:11 -04001804 /*
1805 * If for some reason, we had an interrupt storm that made
1806 * it all the way around the buffer, bail, and warn
1807 * about it.
1808 */
1809 if (unlikely(next_page == commit_page)) {
Steven Rostedt77ae3652009-03-27 11:00:29 -04001810 local_inc(&cpu_buffer->commit_overrun);
Steven Rostedtaa20ae82009-05-05 21:16:11 -04001811 goto out_reset;
1812 }
1813
Steven Rostedt77ae3652009-03-27 11:00:29 -04001814 /*
1815 * This is where the fun begins!
1816 *
1817 * We are fighting against races between a reader that
1818 * could be on another CPU trying to swap its reader
1819 * page with the buffer head.
1820 *
1821 * We are also fighting against interrupts coming in and
1822 * moving the head or tail on us as well.
1823 *
1824 * If the next page is the head page then we have filled
1825 * the buffer, unless the commit page is still on the
1826 * reader page.
1827 */
1828 if (rb_is_head_page(cpu_buffer, next_page, &tail_page->list)) {
Steven Rostedtaa20ae82009-05-05 21:16:11 -04001829
Steven Rostedt77ae3652009-03-27 11:00:29 -04001830 /*
1831 * If the commit is not on the reader page, then
1832 * move the header page.
1833 */
1834 if (!rb_is_reader_page(cpu_buffer->commit_page)) {
1835 /*
1836 * If we are not in overwrite mode,
1837 * this is easy, just stop here.
1838 */
1839 if (!(buffer->flags & RB_FL_OVERWRITE))
1840 goto out_reset;
Steven Rostedtaa20ae82009-05-05 21:16:11 -04001841
Steven Rostedt77ae3652009-03-27 11:00:29 -04001842 ret = rb_handle_head_page(cpu_buffer,
1843 tail_page,
1844 next_page);
1845 if (ret < 0)
1846 goto out_reset;
1847 if (ret)
1848 goto out_again;
1849 } else {
1850 /*
1851 * We need to be careful here too. The
1852 * commit page could still be on the reader
1853 * page. We could have a small buffer, and
1854 * have filled up the buffer with events
1855 * from interrupts and such, and wrapped.
1856 *
1857 * Note, if the tail page is also the on the
1858 * reader_page, we let it move out.
1859 */
1860 if (unlikely((cpu_buffer->commit_page !=
1861 cpu_buffer->tail_page) &&
1862 (cpu_buffer->commit_page ==
1863 cpu_buffer->reader_page))) {
1864 local_inc(&cpu_buffer->commit_overrun);
1865 goto out_reset;
1866 }
Steven Rostedtaa20ae82009-05-05 21:16:11 -04001867 }
1868 }
1869
Steven Rostedt77ae3652009-03-27 11:00:29 -04001870 ret = rb_tail_page_update(cpu_buffer, tail_page, next_page);
1871 if (ret) {
1872 /*
1873 * Nested commits always have zero deltas, so
1874 * just reread the time stamp
1875 */
Jiri Olsa6d3f1e12009-10-23 19:36:19 -04001876 *ts = rb_time_stamp(buffer);
Steven Rostedt77ae3652009-03-27 11:00:29 -04001877 next_page->page->time_stamp = *ts;
Steven Rostedtaa20ae82009-05-05 21:16:11 -04001878 }
1879
Steven Rostedt77ae3652009-03-27 11:00:29 -04001880 out_again:
Steven Rostedtaa20ae82009-05-05 21:16:11 -04001881
Steven Rostedt77ae3652009-03-27 11:00:29 -04001882 rb_reset_tail(cpu_buffer, tail_page, tail, length);
Steven Rostedtaa20ae82009-05-05 21:16:11 -04001883
1884 /* fail and let the caller try again */
1885 return ERR_PTR(-EAGAIN);
1886
Steven Rostedt45141d42009-02-12 13:19:48 -05001887 out_reset:
Lai Jiangshan6f3b3442009-01-12 11:06:18 +08001888 /* reset write */
Steven Rostedtc7b09302009-06-11 11:12:00 -04001889 rb_reset_tail(cpu_buffer, tail_page, tail, length);
Lai Jiangshan6f3b3442009-01-12 11:06:18 +08001890
Steven Rostedtbf41a152008-10-04 02:00:59 -04001891 return NULL;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001892}
1893
Steven Rostedt6634ff22009-05-06 15:30:07 -04001894static struct ring_buffer_event *
1895__rb_reserve_next(struct ring_buffer_per_cpu *cpu_buffer,
1896 unsigned type, unsigned long length, u64 *ts)
1897{
1898 struct buffer_page *tail_page, *commit_page;
1899 struct ring_buffer_event *event;
1900 unsigned long tail, write;
1901
1902 commit_page = cpu_buffer->commit_page;
1903 /* we just need to protect against interrupts */
1904 barrier();
1905 tail_page = cpu_buffer->tail_page;
1906 write = local_add_return(length, &tail_page->write);
Steven Rostedt77ae3652009-03-27 11:00:29 -04001907
1908 /* set write to only the index of the write */
1909 write &= RB_WRITE_MASK;
Steven Rostedt6634ff22009-05-06 15:30:07 -04001910 tail = write - length;
1911
1912 /* See if we shot pass the end of this buffer page */
1913 if (write > BUF_PAGE_SIZE)
1914 return rb_move_tail(cpu_buffer, length, tail,
1915 commit_page, tail_page, ts);
1916
1917 /* We reserved something on the buffer */
1918
Steven Rostedt6634ff22009-05-06 15:30:07 -04001919 event = __rb_page_index(tail_page, tail);
Vegard Nossum1744a212009-02-28 08:29:44 +01001920 kmemcheck_annotate_bitfield(event, bitfield);
Steven Rostedt6634ff22009-05-06 15:30:07 -04001921 rb_update_event(event, type, length);
1922
1923 /* The passed in type is zero for DATA */
1924 if (likely(!type))
1925 local_inc(&tail_page->entries);
1926
1927 /*
Steven Rostedtfa743952009-06-16 12:37:57 -04001928 * If this is the first commit on the page, then update
1929 * its timestamp.
Steven Rostedt6634ff22009-05-06 15:30:07 -04001930 */
Steven Rostedtfa743952009-06-16 12:37:57 -04001931 if (!tail)
1932 tail_page->page->time_stamp = *ts;
Steven Rostedt6634ff22009-05-06 15:30:07 -04001933
1934 return event;
1935}
1936
Steven Rostedtedd813b2009-06-02 23:00:53 -04001937static inline int
1938rb_try_to_discard(struct ring_buffer_per_cpu *cpu_buffer,
1939 struct ring_buffer_event *event)
1940{
1941 unsigned long new_index, old_index;
1942 struct buffer_page *bpage;
1943 unsigned long index;
1944 unsigned long addr;
1945
1946 new_index = rb_event_index(event);
1947 old_index = new_index + rb_event_length(event);
1948 addr = (unsigned long)event;
1949 addr &= PAGE_MASK;
1950
1951 bpage = cpu_buffer->tail_page;
1952
1953 if (bpage->page == (void *)addr && rb_page_write(bpage) == old_index) {
Steven Rostedt77ae3652009-03-27 11:00:29 -04001954 unsigned long write_mask =
1955 local_read(&bpage->write) & ~RB_WRITE_MASK;
Steven Rostedtedd813b2009-06-02 23:00:53 -04001956 /*
1957 * This is on the tail page. It is possible that
1958 * a write could come in and move the tail page
1959 * and write to the next page. That is fine
1960 * because we just shorten what is on this page.
1961 */
Steven Rostedt77ae3652009-03-27 11:00:29 -04001962 old_index += write_mask;
1963 new_index += write_mask;
Steven Rostedtedd813b2009-06-02 23:00:53 -04001964 index = local_cmpxchg(&bpage->write, old_index, new_index);
1965 if (index == old_index)
1966 return 1;
1967 }
1968
1969 /* could not discard */
1970 return 0;
1971}
1972
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001973static int
1974rb_add_time_stamp(struct ring_buffer_per_cpu *cpu_buffer,
1975 u64 *ts, u64 *delta)
1976{
1977 struct ring_buffer_event *event;
1978 static int once;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001979 int ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001980
1981 if (unlikely(*delta > (1ULL << 59) && !once++)) {
1982 printk(KERN_WARNING "Delta way too big! %llu"
1983 " ts=%llu write stamp = %llu\n",
Stephen Rothwelle2862c92008-10-27 17:43:28 +11001984 (unsigned long long)*delta,
1985 (unsigned long long)*ts,
1986 (unsigned long long)cpu_buffer->write_stamp);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001987 WARN_ON(1);
1988 }
1989
1990 /*
1991 * The delta is too big, we to add a
1992 * new timestamp.
1993 */
1994 event = __rb_reserve_next(cpu_buffer,
1995 RINGBUF_TYPE_TIME_EXTEND,
1996 RB_LEN_TIME_EXTEND,
1997 ts);
1998 if (!event)
Steven Rostedtbf41a152008-10-04 02:00:59 -04001999 return -EBUSY;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002000
Steven Rostedtbf41a152008-10-04 02:00:59 -04002001 if (PTR_ERR(event) == -EAGAIN)
2002 return -EAGAIN;
2003
2004 /* Only a commited time event can update the write stamp */
Steven Rostedtfa743952009-06-16 12:37:57 -04002005 if (rb_event_is_commit(cpu_buffer, event)) {
Steven Rostedtbf41a152008-10-04 02:00:59 -04002006 /*
Steven Rostedtfa743952009-06-16 12:37:57 -04002007 * If this is the first on the page, then it was
2008 * updated with the page itself. Try to discard it
2009 * and if we can't just make it zero.
Steven Rostedtbf41a152008-10-04 02:00:59 -04002010 */
2011 if (rb_event_index(event)) {
2012 event->time_delta = *delta & TS_MASK;
2013 event->array[0] = *delta >> TS_SHIFT;
2014 } else {
Steven Rostedtea05b572009-06-03 09:30:10 -04002015 /* try to discard, since we do not need this */
2016 if (!rb_try_to_discard(cpu_buffer, event)) {
2017 /* nope, just zero it */
2018 event->time_delta = 0;
2019 event->array[0] = 0;
2020 }
Steven Rostedtbf41a152008-10-04 02:00:59 -04002021 }
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002022 cpu_buffer->write_stamp = *ts;
Steven Rostedtbf41a152008-10-04 02:00:59 -04002023 /* let the caller know this was the commit */
2024 ret = 1;
2025 } else {
Steven Rostedtedd813b2009-06-02 23:00:53 -04002026 /* Try to discard the event */
2027 if (!rb_try_to_discard(cpu_buffer, event)) {
2028 /* Darn, this is just wasted space */
2029 event->time_delta = 0;
2030 event->array[0] = 0;
Steven Rostedtedd813b2009-06-02 23:00:53 -04002031 }
Steven Rostedtf57a8a12009-06-05 14:11:30 -04002032 ret = 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002033 }
2034
Steven Rostedtbf41a152008-10-04 02:00:59 -04002035 *delta = 0;
2036
2037 return ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002038}
2039
Steven Rostedtfa743952009-06-16 12:37:57 -04002040static void rb_start_commit(struct ring_buffer_per_cpu *cpu_buffer)
2041{
2042 local_inc(&cpu_buffer->committing);
2043 local_inc(&cpu_buffer->commits);
2044}
2045
2046static void rb_end_commit(struct ring_buffer_per_cpu *cpu_buffer)
2047{
2048 unsigned long commits;
2049
2050 if (RB_WARN_ON(cpu_buffer,
2051 !local_read(&cpu_buffer->committing)))
2052 return;
2053
2054 again:
2055 commits = local_read(&cpu_buffer->commits);
2056 /* synchronize with interrupts */
2057 barrier();
2058 if (local_read(&cpu_buffer->committing) == 1)
2059 rb_set_commit_to_write(cpu_buffer);
2060
2061 local_dec(&cpu_buffer->committing);
2062
2063 /* synchronize with interrupts */
2064 barrier();
2065
2066 /*
2067 * Need to account for interrupts coming in between the
2068 * updating of the commit page and the clearing of the
2069 * committing counter.
2070 */
2071 if (unlikely(local_read(&cpu_buffer->commits) != commits) &&
2072 !local_read(&cpu_buffer->committing)) {
2073 local_inc(&cpu_buffer->committing);
2074 goto again;
2075 }
2076}
2077
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002078static struct ring_buffer_event *
Steven Rostedt62f0b3e2009-09-04 14:11:34 -04002079rb_reserve_next_event(struct ring_buffer *buffer,
2080 struct ring_buffer_per_cpu *cpu_buffer,
Steven Rostedt1cd8d732009-05-11 14:08:09 -04002081 unsigned long length)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002082{
2083 struct ring_buffer_event *event;
Steven Rostedt168b6b12009-05-11 22:11:05 -04002084 u64 ts, delta = 0;
Steven Rostedtbf41a152008-10-04 02:00:59 -04002085 int commit = 0;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002086 int nr_loops = 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002087
Steven Rostedtfa743952009-06-16 12:37:57 -04002088 rb_start_commit(cpu_buffer);
2089
Steven Rostedt85bac322009-09-04 14:24:40 -04002090#ifdef CONFIG_RING_BUFFER_ALLOW_SWAP
Steven Rostedt62f0b3e2009-09-04 14:11:34 -04002091 /*
2092 * Due to the ability to swap a cpu buffer from a buffer
2093 * it is possible it was swapped before we committed.
2094 * (committing stops a swap). We check for it here and
2095 * if it happened, we have to fail the write.
2096 */
2097 barrier();
2098 if (unlikely(ACCESS_ONCE(cpu_buffer->buffer) != buffer)) {
2099 local_dec(&cpu_buffer->committing);
2100 local_dec(&cpu_buffer->commits);
2101 return NULL;
2102 }
Steven Rostedt85bac322009-09-04 14:24:40 -04002103#endif
Steven Rostedt62f0b3e2009-09-04 14:11:34 -04002104
Steven Rostedtbe957c42009-05-11 14:42:53 -04002105 length = rb_calculate_event_length(length);
Steven Rostedtbf41a152008-10-04 02:00:59 -04002106 again:
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002107 /*
2108 * We allow for interrupts to reenter here and do a trace.
2109 * If one does, it will cause this original code to loop
2110 * back here. Even with heavy interrupts happening, this
2111 * should only happen a few times in a row. If this happens
2112 * 1000 times in a row, there must be either an interrupt
2113 * storm or we have something buggy.
2114 * Bail!
2115 */
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05002116 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 1000))
Steven Rostedtfa743952009-06-16 12:37:57 -04002117 goto out_fail;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002118
Jiri Olsa6d3f1e12009-10-23 19:36:19 -04002119 ts = rb_time_stamp(cpu_buffer->buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002120
Steven Rostedtbf41a152008-10-04 02:00:59 -04002121 /*
2122 * Only the first commit can update the timestamp.
2123 * Yes there is a race here. If an interrupt comes in
2124 * just after the conditional and it traces too, then it
2125 * will also check the deltas. More than one timestamp may
2126 * also be made. But only the entry that did the actual
2127 * commit will be something other than zero.
2128 */
Steven Rostedt0f0c85f2009-05-11 16:08:00 -04002129 if (likely(cpu_buffer->tail_page == cpu_buffer->commit_page &&
2130 rb_page_write(cpu_buffer->tail_page) ==
2131 rb_commit_index(cpu_buffer))) {
Steven Rostedt168b6b12009-05-11 22:11:05 -04002132 u64 diff;
Steven Rostedtbf41a152008-10-04 02:00:59 -04002133
Steven Rostedt168b6b12009-05-11 22:11:05 -04002134 diff = ts - cpu_buffer->write_stamp;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002135
Steven Rostedt168b6b12009-05-11 22:11:05 -04002136 /* make sure this diff is calculated here */
Steven Rostedtbf41a152008-10-04 02:00:59 -04002137 barrier();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002138
Steven Rostedtbf41a152008-10-04 02:00:59 -04002139 /* Did the write stamp get updated already? */
2140 if (unlikely(ts < cpu_buffer->write_stamp))
Steven Rostedt168b6b12009-05-11 22:11:05 -04002141 goto get_event;
Steven Rostedtbf41a152008-10-04 02:00:59 -04002142
Steven Rostedt168b6b12009-05-11 22:11:05 -04002143 delta = diff;
2144 if (unlikely(test_time_stamp(delta))) {
Steven Rostedtbf41a152008-10-04 02:00:59 -04002145
2146 commit = rb_add_time_stamp(cpu_buffer, &ts, &delta);
Steven Rostedtbf41a152008-10-04 02:00:59 -04002147 if (commit == -EBUSY)
Steven Rostedtfa743952009-06-16 12:37:57 -04002148 goto out_fail;
Steven Rostedtbf41a152008-10-04 02:00:59 -04002149
2150 if (commit == -EAGAIN)
2151 goto again;
2152
2153 RB_WARN_ON(cpu_buffer, commit < 0);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002154 }
Steven Rostedt168b6b12009-05-11 22:11:05 -04002155 }
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002156
Steven Rostedt168b6b12009-05-11 22:11:05 -04002157 get_event:
Steven Rostedt1cd8d732009-05-11 14:08:09 -04002158 event = __rb_reserve_next(cpu_buffer, 0, length, &ts);
Steven Rostedt168b6b12009-05-11 22:11:05 -04002159 if (unlikely(PTR_ERR(event) == -EAGAIN))
Steven Rostedtbf41a152008-10-04 02:00:59 -04002160 goto again;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002161
Steven Rostedtfa743952009-06-16 12:37:57 -04002162 if (!event)
2163 goto out_fail;
Steven Rostedtbf41a152008-10-04 02:00:59 -04002164
Steven Rostedtfa743952009-06-16 12:37:57 -04002165 if (!rb_event_is_commit(cpu_buffer, event))
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002166 delta = 0;
2167
2168 event->time_delta = delta;
2169
2170 return event;
Steven Rostedtfa743952009-06-16 12:37:57 -04002171
2172 out_fail:
2173 rb_end_commit(cpu_buffer);
2174 return NULL;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002175}
2176
Paul Mundt1155de42009-06-25 14:30:12 +09002177#ifdef CONFIG_TRACING
2178
Steven Rostedtaa18efb2009-04-20 16:16:11 -04002179#define TRACE_RECURSIVE_DEPTH 16
Steven Rostedt261842b2009-04-16 21:41:52 -04002180
2181static int trace_recursive_lock(void)
2182{
Steven Rostedtaa18efb2009-04-20 16:16:11 -04002183 current->trace_recursion++;
Steven Rostedt261842b2009-04-16 21:41:52 -04002184
Steven Rostedtaa18efb2009-04-20 16:16:11 -04002185 if (likely(current->trace_recursion < TRACE_RECURSIVE_DEPTH))
2186 return 0;
Steven Rostedt261842b2009-04-16 21:41:52 -04002187
Steven Rostedtaa18efb2009-04-20 16:16:11 -04002188 /* Disable all tracing before we do anything else */
2189 tracing_off_permanent();
Frederic Weisbeckere057a5e2009-04-19 23:38:12 +02002190
Steven Rostedt7d7d2b82009-04-27 12:37:49 -04002191 printk_once(KERN_WARNING "Tracing recursion: depth[%ld]:"
Steven Rostedtaa18efb2009-04-20 16:16:11 -04002192 "HC[%lu]:SC[%lu]:NMI[%lu]\n",
2193 current->trace_recursion,
2194 hardirq_count() >> HARDIRQ_SHIFT,
2195 softirq_count() >> SOFTIRQ_SHIFT,
2196 in_nmi());
Frederic Weisbeckere057a5e2009-04-19 23:38:12 +02002197
Steven Rostedtaa18efb2009-04-20 16:16:11 -04002198 WARN_ON_ONCE(1);
2199 return -1;
Steven Rostedt261842b2009-04-16 21:41:52 -04002200}
2201
2202static void trace_recursive_unlock(void)
2203{
Steven Rostedtaa18efb2009-04-20 16:16:11 -04002204 WARN_ON_ONCE(!current->trace_recursion);
Steven Rostedt261842b2009-04-16 21:41:52 -04002205
Steven Rostedtaa18efb2009-04-20 16:16:11 -04002206 current->trace_recursion--;
Steven Rostedt261842b2009-04-16 21:41:52 -04002207}
2208
Paul Mundt1155de42009-06-25 14:30:12 +09002209#else
2210
2211#define trace_recursive_lock() (0)
2212#define trace_recursive_unlock() do { } while (0)
2213
2214#endif
2215
Steven Rostedtbf41a152008-10-04 02:00:59 -04002216static DEFINE_PER_CPU(int, rb_need_resched);
2217
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002218/**
2219 * ring_buffer_lock_reserve - reserve a part of the buffer
2220 * @buffer: the ring buffer to reserve from
2221 * @length: the length of the data to reserve (excluding event header)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002222 *
2223 * Returns a reseverd event on the ring buffer to copy directly to.
2224 * The user of this interface will need to get the body to write into
2225 * and can use the ring_buffer_event_data() interface.
2226 *
2227 * The length is the length of the data needed, not the event length
2228 * which also includes the event header.
2229 *
2230 * Must be paired with ring_buffer_unlock_commit, unless NULL is returned.
2231 * If NULL is returned, then nothing has been allocated or locked.
2232 */
2233struct ring_buffer_event *
Arnaldo Carvalho de Melo0a987752009-02-05 16:12:56 -02002234ring_buffer_lock_reserve(struct ring_buffer *buffer, unsigned long length)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002235{
2236 struct ring_buffer_per_cpu *cpu_buffer;
2237 struct ring_buffer_event *event;
Steven Rostedtbf41a152008-10-04 02:00:59 -04002238 int cpu, resched;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002239
Steven Rostedt033601a2008-11-21 12:41:55 -05002240 if (ring_buffer_flags != RB_BUFFERS_ON)
Steven Rostedta3583242008-11-11 15:01:42 -05002241 return NULL;
2242
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002243 if (atomic_read(&buffer->record_disabled))
2244 return NULL;
2245
Steven Rostedtbf41a152008-10-04 02:00:59 -04002246 /* If we are tracing schedule, we don't want to recurse */
Steven Rostedt182e9f52008-11-03 23:15:56 -05002247 resched = ftrace_preempt_disable();
Steven Rostedtbf41a152008-10-04 02:00:59 -04002248
Steven Rostedt261842b2009-04-16 21:41:52 -04002249 if (trace_recursive_lock())
2250 goto out_nocheck;
2251
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002252 cpu = raw_smp_processor_id();
2253
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302254 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedtd7690412008-10-01 00:29:53 -04002255 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002256
2257 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002258
2259 if (atomic_read(&cpu_buffer->record_disabled))
Steven Rostedtd7690412008-10-01 00:29:53 -04002260 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002261
Steven Rostedtbe957c42009-05-11 14:42:53 -04002262 if (length > BUF_MAX_DATA_SIZE)
Steven Rostedtbf41a152008-10-04 02:00:59 -04002263 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002264
Steven Rostedt62f0b3e2009-09-04 14:11:34 -04002265 event = rb_reserve_next_event(buffer, cpu_buffer, length);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002266 if (!event)
Steven Rostedtd7690412008-10-01 00:29:53 -04002267 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002268
Steven Rostedtbf41a152008-10-04 02:00:59 -04002269 /*
2270 * Need to store resched state on this cpu.
2271 * Only the first needs to.
2272 */
2273
2274 if (preempt_count() == 1)
2275 per_cpu(rb_need_resched, cpu) = resched;
2276
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002277 return event;
2278
Steven Rostedtd7690412008-10-01 00:29:53 -04002279 out:
Steven Rostedt261842b2009-04-16 21:41:52 -04002280 trace_recursive_unlock();
2281
2282 out_nocheck:
Steven Rostedt182e9f52008-11-03 23:15:56 -05002283 ftrace_preempt_enable(resched);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002284 return NULL;
2285}
Robert Richterc4f50182008-12-11 16:49:22 +01002286EXPORT_SYMBOL_GPL(ring_buffer_lock_reserve);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002287
Steven Rostedta1863c22009-09-03 10:23:58 -04002288static void
2289rb_update_write_stamp(struct ring_buffer_per_cpu *cpu_buffer,
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002290 struct ring_buffer_event *event)
2291{
Steven Rostedtfa743952009-06-16 12:37:57 -04002292 /*
2293 * The event first in the commit queue updates the
2294 * time stamp.
2295 */
2296 if (rb_event_is_commit(cpu_buffer, event))
2297 cpu_buffer->write_stamp += event->time_delta;
Steven Rostedta1863c22009-09-03 10:23:58 -04002298}
Steven Rostedtbf41a152008-10-04 02:00:59 -04002299
Steven Rostedta1863c22009-09-03 10:23:58 -04002300static void rb_commit(struct ring_buffer_per_cpu *cpu_buffer,
2301 struct ring_buffer_event *event)
2302{
2303 local_inc(&cpu_buffer->entries);
2304 rb_update_write_stamp(cpu_buffer, event);
Steven Rostedtfa743952009-06-16 12:37:57 -04002305 rb_end_commit(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002306}
2307
2308/**
2309 * ring_buffer_unlock_commit - commit a reserved
2310 * @buffer: The buffer to commit to
2311 * @event: The event pointer to commit.
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002312 *
2313 * This commits the data to the ring buffer, and releases any locks held.
2314 *
2315 * Must be paired with ring_buffer_lock_reserve.
2316 */
2317int ring_buffer_unlock_commit(struct ring_buffer *buffer,
Arnaldo Carvalho de Melo0a987752009-02-05 16:12:56 -02002318 struct ring_buffer_event *event)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002319{
2320 struct ring_buffer_per_cpu *cpu_buffer;
2321 int cpu = raw_smp_processor_id();
2322
2323 cpu_buffer = buffer->buffers[cpu];
2324
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002325 rb_commit(cpu_buffer, event);
2326
Steven Rostedt261842b2009-04-16 21:41:52 -04002327 trace_recursive_unlock();
2328
Steven Rostedtbf41a152008-10-04 02:00:59 -04002329 /*
2330 * Only the last preempt count needs to restore preemption.
2331 */
Steven Rostedt182e9f52008-11-03 23:15:56 -05002332 if (preempt_count() == 1)
2333 ftrace_preempt_enable(per_cpu(rb_need_resched, cpu));
2334 else
Steven Rostedtbf41a152008-10-04 02:00:59 -04002335 preempt_enable_no_resched_notrace();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002336
2337 return 0;
2338}
Robert Richterc4f50182008-12-11 16:49:22 +01002339EXPORT_SYMBOL_GPL(ring_buffer_unlock_commit);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002340
Frederic Weisbeckerf3b9aae2009-04-19 23:39:33 +02002341static inline void rb_event_discard(struct ring_buffer_event *event)
2342{
Lai Jiangshan334d4162009-04-24 11:27:05 +08002343 /* array[0] holds the actual length for the discarded event */
2344 event->array[0] = rb_event_data_length(event) - RB_EVNT_HDR_SIZE;
2345 event->type_len = RINGBUF_TYPE_PADDING;
Frederic Weisbeckerf3b9aae2009-04-19 23:39:33 +02002346 /* time delta must be non zero */
2347 if (!event->time_delta)
2348 event->time_delta = 1;
2349}
2350
Steven Rostedta1863c22009-09-03 10:23:58 -04002351/*
2352 * Decrement the entries to the page that an event is on.
2353 * The event does not even need to exist, only the pointer
2354 * to the page it is on. This may only be called before the commit
2355 * takes place.
2356 */
2357static inline void
2358rb_decrement_entry(struct ring_buffer_per_cpu *cpu_buffer,
2359 struct ring_buffer_event *event)
2360{
2361 unsigned long addr = (unsigned long)event;
2362 struct buffer_page *bpage = cpu_buffer->commit_page;
2363 struct buffer_page *start;
2364
2365 addr &= PAGE_MASK;
2366
2367 /* Do the likely case first */
2368 if (likely(bpage->page == (void *)addr)) {
2369 local_dec(&bpage->entries);
2370 return;
2371 }
2372
2373 /*
2374 * Because the commit page may be on the reader page we
2375 * start with the next page and check the end loop there.
2376 */
2377 rb_inc_page(cpu_buffer, &bpage);
2378 start = bpage;
2379 do {
2380 if (bpage->page == (void *)addr) {
2381 local_dec(&bpage->entries);
2382 return;
2383 }
2384 rb_inc_page(cpu_buffer, &bpage);
2385 } while (bpage != start);
2386
2387 /* commit not part of this buffer?? */
2388 RB_WARN_ON(cpu_buffer, 1);
2389}
2390
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002391/**
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002392 * ring_buffer_commit_discard - discard an event that has not been committed
2393 * @buffer: the ring buffer
2394 * @event: non committed event to discard
2395 *
Steven Rostedtdc892f72009-09-03 15:33:41 -04002396 * Sometimes an event that is in the ring buffer needs to be ignored.
2397 * This function lets the user discard an event in the ring buffer
2398 * and then that event will not be read later.
2399 *
2400 * This function only works if it is called before the the item has been
2401 * committed. It will try to free the event from the ring buffer
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002402 * if another event has not been added behind it.
2403 *
2404 * If another event has been added behind it, it will set the event
2405 * up as discarded, and perform the commit.
2406 *
2407 * If this function is called, do not call ring_buffer_unlock_commit on
2408 * the event.
2409 */
2410void ring_buffer_discard_commit(struct ring_buffer *buffer,
2411 struct ring_buffer_event *event)
2412{
2413 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002414 int cpu;
2415
2416 /* The event is discarded regardless */
Frederic Weisbeckerf3b9aae2009-04-19 23:39:33 +02002417 rb_event_discard(event);
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002418
Steven Rostedtfa743952009-06-16 12:37:57 -04002419 cpu = smp_processor_id();
2420 cpu_buffer = buffer->buffers[cpu];
2421
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002422 /*
2423 * This must only be called if the event has not been
2424 * committed yet. Thus we can assume that preemption
2425 * is still disabled.
2426 */
Steven Rostedtfa743952009-06-16 12:37:57 -04002427 RB_WARN_ON(buffer, !local_read(&cpu_buffer->committing));
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002428
Steven Rostedta1863c22009-09-03 10:23:58 -04002429 rb_decrement_entry(cpu_buffer, event);
Steven Rostedt0f2541d2009-08-05 12:02:48 -04002430 if (rb_try_to_discard(cpu_buffer, event))
Steven Rostedtedd813b2009-06-02 23:00:53 -04002431 goto out;
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002432
2433 /*
2434 * The commit is still visible by the reader, so we
Steven Rostedta1863c22009-09-03 10:23:58 -04002435 * must still update the timestamp.
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002436 */
Steven Rostedta1863c22009-09-03 10:23:58 -04002437 rb_update_write_stamp(cpu_buffer, event);
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002438 out:
Steven Rostedtfa743952009-06-16 12:37:57 -04002439 rb_end_commit(cpu_buffer);
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002440
Frederic Weisbeckerf3b9aae2009-04-19 23:39:33 +02002441 trace_recursive_unlock();
2442
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002443 /*
2444 * Only the last preempt count needs to restore preemption.
2445 */
2446 if (preempt_count() == 1)
2447 ftrace_preempt_enable(per_cpu(rb_need_resched, cpu));
2448 else
2449 preempt_enable_no_resched_notrace();
2450
2451}
2452EXPORT_SYMBOL_GPL(ring_buffer_discard_commit);
2453
2454/**
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002455 * ring_buffer_write - write data to the buffer without reserving
2456 * @buffer: The ring buffer to write to.
2457 * @length: The length of the data being written (excluding the event header)
2458 * @data: The data to write to the buffer.
2459 *
2460 * This is like ring_buffer_lock_reserve and ring_buffer_unlock_commit as
2461 * one function. If you already have the data to write to the buffer, it
2462 * may be easier to simply call this function.
2463 *
2464 * Note, like ring_buffer_lock_reserve, the length is the length of the data
2465 * and not the length of the event which would hold the header.
2466 */
2467int ring_buffer_write(struct ring_buffer *buffer,
2468 unsigned long length,
2469 void *data)
2470{
2471 struct ring_buffer_per_cpu *cpu_buffer;
2472 struct ring_buffer_event *event;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002473 void *body;
2474 int ret = -EBUSY;
Steven Rostedtbf41a152008-10-04 02:00:59 -04002475 int cpu, resched;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002476
Steven Rostedt033601a2008-11-21 12:41:55 -05002477 if (ring_buffer_flags != RB_BUFFERS_ON)
Steven Rostedta3583242008-11-11 15:01:42 -05002478 return -EBUSY;
2479
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002480 if (atomic_read(&buffer->record_disabled))
2481 return -EBUSY;
2482
Steven Rostedt182e9f52008-11-03 23:15:56 -05002483 resched = ftrace_preempt_disable();
Steven Rostedtbf41a152008-10-04 02:00:59 -04002484
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002485 cpu = raw_smp_processor_id();
2486
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302487 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedtd7690412008-10-01 00:29:53 -04002488 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002489
2490 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002491
2492 if (atomic_read(&cpu_buffer->record_disabled))
2493 goto out;
2494
Steven Rostedtbe957c42009-05-11 14:42:53 -04002495 if (length > BUF_MAX_DATA_SIZE)
2496 goto out;
2497
Steven Rostedt62f0b3e2009-09-04 14:11:34 -04002498 event = rb_reserve_next_event(buffer, cpu_buffer, length);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002499 if (!event)
2500 goto out;
2501
2502 body = rb_event_data(event);
2503
2504 memcpy(body, data, length);
2505
2506 rb_commit(cpu_buffer, event);
2507
2508 ret = 0;
2509 out:
Steven Rostedt182e9f52008-11-03 23:15:56 -05002510 ftrace_preempt_enable(resched);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002511
2512 return ret;
2513}
Robert Richterc4f50182008-12-11 16:49:22 +01002514EXPORT_SYMBOL_GPL(ring_buffer_write);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002515
Andrew Morton34a148b2009-01-09 12:27:09 -08002516static int rb_per_cpu_empty(struct ring_buffer_per_cpu *cpu_buffer)
Steven Rostedtbf41a152008-10-04 02:00:59 -04002517{
2518 struct buffer_page *reader = cpu_buffer->reader_page;
Steven Rostedt77ae3652009-03-27 11:00:29 -04002519 struct buffer_page *head = rb_set_head_page(cpu_buffer);
Steven Rostedtbf41a152008-10-04 02:00:59 -04002520 struct buffer_page *commit = cpu_buffer->commit_page;
2521
Steven Rostedt77ae3652009-03-27 11:00:29 -04002522 /* In case of error, head will be NULL */
2523 if (unlikely(!head))
2524 return 1;
2525
Steven Rostedtbf41a152008-10-04 02:00:59 -04002526 return reader->read == rb_page_commit(reader) &&
2527 (commit == reader ||
2528 (commit == head &&
2529 head->read == rb_page_commit(commit)));
2530}
2531
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002532/**
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002533 * ring_buffer_record_disable - stop all writes into the buffer
2534 * @buffer: The ring buffer to stop writes to.
2535 *
2536 * This prevents all writes to the buffer. Any attempt to write
2537 * to the buffer after this will fail and return NULL.
2538 *
2539 * The caller should call synchronize_sched() after this.
2540 */
2541void ring_buffer_record_disable(struct ring_buffer *buffer)
2542{
2543 atomic_inc(&buffer->record_disabled);
2544}
Robert Richterc4f50182008-12-11 16:49:22 +01002545EXPORT_SYMBOL_GPL(ring_buffer_record_disable);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002546
2547/**
2548 * ring_buffer_record_enable - enable writes to the buffer
2549 * @buffer: The ring buffer to enable writes
2550 *
2551 * Note, multiple disables will need the same number of enables
2552 * to truely enable the writing (much like preempt_disable).
2553 */
2554void ring_buffer_record_enable(struct ring_buffer *buffer)
2555{
2556 atomic_dec(&buffer->record_disabled);
2557}
Robert Richterc4f50182008-12-11 16:49:22 +01002558EXPORT_SYMBOL_GPL(ring_buffer_record_enable);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002559
2560/**
2561 * ring_buffer_record_disable_cpu - stop all writes into the cpu_buffer
2562 * @buffer: The ring buffer to stop writes to.
2563 * @cpu: The CPU buffer to stop
2564 *
2565 * This prevents all writes to the buffer. Any attempt to write
2566 * to the buffer after this will fail and return NULL.
2567 *
2568 * The caller should call synchronize_sched() after this.
2569 */
2570void ring_buffer_record_disable_cpu(struct ring_buffer *buffer, int cpu)
2571{
2572 struct ring_buffer_per_cpu *cpu_buffer;
2573
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302574 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04002575 return;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002576
2577 cpu_buffer = buffer->buffers[cpu];
2578 atomic_inc(&cpu_buffer->record_disabled);
2579}
Robert Richterc4f50182008-12-11 16:49:22 +01002580EXPORT_SYMBOL_GPL(ring_buffer_record_disable_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002581
2582/**
2583 * ring_buffer_record_enable_cpu - enable writes to the buffer
2584 * @buffer: The ring buffer to enable writes
2585 * @cpu: The CPU to enable.
2586 *
2587 * Note, multiple disables will need the same number of enables
2588 * to truely enable the writing (much like preempt_disable).
2589 */
2590void ring_buffer_record_enable_cpu(struct ring_buffer *buffer, int cpu)
2591{
2592 struct ring_buffer_per_cpu *cpu_buffer;
2593
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302594 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04002595 return;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002596
2597 cpu_buffer = buffer->buffers[cpu];
2598 atomic_dec(&cpu_buffer->record_disabled);
2599}
Robert Richterc4f50182008-12-11 16:49:22 +01002600EXPORT_SYMBOL_GPL(ring_buffer_record_enable_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002601
2602/**
2603 * ring_buffer_entries_cpu - get the number of entries in a cpu buffer
2604 * @buffer: The ring buffer
2605 * @cpu: The per CPU buffer to get the entries from.
2606 */
2607unsigned long ring_buffer_entries_cpu(struct ring_buffer *buffer, int cpu)
2608{
2609 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedt8aabee52009-03-12 13:13:49 -04002610 unsigned long ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002611
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302612 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04002613 return 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002614
2615 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt77ae3652009-03-27 11:00:29 -04002616 ret = (local_read(&cpu_buffer->entries) - local_read(&cpu_buffer->overrun))
Steven Rostedte4906ef2009-04-30 20:49:44 -04002617 - cpu_buffer->read;
Steven Rostedt554f7862009-03-11 22:00:13 -04002618
2619 return ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002620}
Robert Richterc4f50182008-12-11 16:49:22 +01002621EXPORT_SYMBOL_GPL(ring_buffer_entries_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002622
2623/**
2624 * ring_buffer_overrun_cpu - get the number of overruns in a cpu_buffer
2625 * @buffer: The ring buffer
2626 * @cpu: The per CPU buffer to get the number of overruns from
2627 */
2628unsigned long ring_buffer_overrun_cpu(struct ring_buffer *buffer, int cpu)
2629{
2630 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedt8aabee52009-03-12 13:13:49 -04002631 unsigned long ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002632
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302633 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04002634 return 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002635
2636 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt77ae3652009-03-27 11:00:29 -04002637 ret = local_read(&cpu_buffer->overrun);
Steven Rostedt554f7862009-03-11 22:00:13 -04002638
2639 return ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002640}
Robert Richterc4f50182008-12-11 16:49:22 +01002641EXPORT_SYMBOL_GPL(ring_buffer_overrun_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002642
2643/**
Steven Rostedtf0d2c682009-04-29 13:43:37 -04002644 * ring_buffer_commit_overrun_cpu - get the number of overruns caused by commits
2645 * @buffer: The ring buffer
2646 * @cpu: The per CPU buffer to get the number of overruns from
2647 */
2648unsigned long
2649ring_buffer_commit_overrun_cpu(struct ring_buffer *buffer, int cpu)
2650{
2651 struct ring_buffer_per_cpu *cpu_buffer;
2652 unsigned long ret;
2653
2654 if (!cpumask_test_cpu(cpu, buffer->cpumask))
2655 return 0;
2656
2657 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt77ae3652009-03-27 11:00:29 -04002658 ret = local_read(&cpu_buffer->commit_overrun);
Steven Rostedtf0d2c682009-04-29 13:43:37 -04002659
2660 return ret;
2661}
2662EXPORT_SYMBOL_GPL(ring_buffer_commit_overrun_cpu);
2663
2664/**
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002665 * ring_buffer_entries - get the number of entries in a buffer
2666 * @buffer: The ring buffer
2667 *
2668 * Returns the total number of entries in the ring buffer
2669 * (all CPU entries)
2670 */
2671unsigned long ring_buffer_entries(struct ring_buffer *buffer)
2672{
2673 struct ring_buffer_per_cpu *cpu_buffer;
2674 unsigned long entries = 0;
2675 int cpu;
2676
2677 /* if you care about this being correct, lock the buffer */
2678 for_each_buffer_cpu(buffer, cpu) {
2679 cpu_buffer = buffer->buffers[cpu];
Steven Rostedte4906ef2009-04-30 20:49:44 -04002680 entries += (local_read(&cpu_buffer->entries) -
Steven Rostedt77ae3652009-03-27 11:00:29 -04002681 local_read(&cpu_buffer->overrun)) - cpu_buffer->read;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002682 }
2683
2684 return entries;
2685}
Robert Richterc4f50182008-12-11 16:49:22 +01002686EXPORT_SYMBOL_GPL(ring_buffer_entries);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002687
2688/**
Jiri Olsa67b394f2009-10-23 19:36:18 -04002689 * ring_buffer_overruns - get the number of overruns in buffer
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002690 * @buffer: The ring buffer
2691 *
2692 * Returns the total number of overruns in the ring buffer
2693 * (all CPU entries)
2694 */
2695unsigned long ring_buffer_overruns(struct ring_buffer *buffer)
2696{
2697 struct ring_buffer_per_cpu *cpu_buffer;
2698 unsigned long overruns = 0;
2699 int cpu;
2700
2701 /* if you care about this being correct, lock the buffer */
2702 for_each_buffer_cpu(buffer, cpu) {
2703 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt77ae3652009-03-27 11:00:29 -04002704 overruns += local_read(&cpu_buffer->overrun);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002705 }
2706
2707 return overruns;
2708}
Robert Richterc4f50182008-12-11 16:49:22 +01002709EXPORT_SYMBOL_GPL(ring_buffer_overruns);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002710
Steven Rostedt642edba2008-11-12 00:01:26 -05002711static void rb_iter_reset(struct ring_buffer_iter *iter)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002712{
2713 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
2714
Steven Rostedtd7690412008-10-01 00:29:53 -04002715 /* Iterator usage is expected to have record disabled */
2716 if (list_empty(&cpu_buffer->reader_page->list)) {
Steven Rostedt77ae3652009-03-27 11:00:29 -04002717 iter->head_page = rb_set_head_page(cpu_buffer);
2718 if (unlikely(!iter->head_page))
2719 return;
2720 iter->head = iter->head_page->read;
Steven Rostedtd7690412008-10-01 00:29:53 -04002721 } else {
2722 iter->head_page = cpu_buffer->reader_page;
Steven Rostedt6f807ac2008-10-04 02:00:58 -04002723 iter->head = cpu_buffer->reader_page->read;
Steven Rostedtd7690412008-10-01 00:29:53 -04002724 }
2725 if (iter->head)
2726 iter->read_stamp = cpu_buffer->read_stamp;
2727 else
Steven Rostedtabc9b562008-12-02 15:34:06 -05002728 iter->read_stamp = iter->head_page->page->time_stamp;
Steven Rostedt642edba2008-11-12 00:01:26 -05002729}
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002730
Steven Rostedt642edba2008-11-12 00:01:26 -05002731/**
2732 * ring_buffer_iter_reset - reset an iterator
2733 * @iter: The iterator to reset
2734 *
2735 * Resets the iterator, so that it will start from the beginning
2736 * again.
2737 */
2738void ring_buffer_iter_reset(struct ring_buffer_iter *iter)
2739{
Steven Rostedt554f7862009-03-11 22:00:13 -04002740 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedt642edba2008-11-12 00:01:26 -05002741 unsigned long flags;
2742
Steven Rostedt554f7862009-03-11 22:00:13 -04002743 if (!iter)
2744 return;
2745
2746 cpu_buffer = iter->cpu_buffer;
2747
Steven Rostedt642edba2008-11-12 00:01:26 -05002748 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
2749 rb_iter_reset(iter);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002750 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002751}
Robert Richterc4f50182008-12-11 16:49:22 +01002752EXPORT_SYMBOL_GPL(ring_buffer_iter_reset);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002753
2754/**
2755 * ring_buffer_iter_empty - check if an iterator has no more to read
2756 * @iter: The iterator to check
2757 */
2758int ring_buffer_iter_empty(struct ring_buffer_iter *iter)
2759{
2760 struct ring_buffer_per_cpu *cpu_buffer;
2761
2762 cpu_buffer = iter->cpu_buffer;
2763
Steven Rostedtbf41a152008-10-04 02:00:59 -04002764 return iter->head_page == cpu_buffer->commit_page &&
2765 iter->head == rb_commit_index(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002766}
Robert Richterc4f50182008-12-11 16:49:22 +01002767EXPORT_SYMBOL_GPL(ring_buffer_iter_empty);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002768
2769static void
2770rb_update_read_stamp(struct ring_buffer_per_cpu *cpu_buffer,
2771 struct ring_buffer_event *event)
2772{
2773 u64 delta;
2774
Lai Jiangshan334d4162009-04-24 11:27:05 +08002775 switch (event->type_len) {
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002776 case RINGBUF_TYPE_PADDING:
2777 return;
2778
2779 case RINGBUF_TYPE_TIME_EXTEND:
2780 delta = event->array[0];
2781 delta <<= TS_SHIFT;
2782 delta += event->time_delta;
2783 cpu_buffer->read_stamp += delta;
2784 return;
2785
2786 case RINGBUF_TYPE_TIME_STAMP:
2787 /* FIXME: not implemented */
2788 return;
2789
2790 case RINGBUF_TYPE_DATA:
2791 cpu_buffer->read_stamp += event->time_delta;
2792 return;
2793
2794 default:
2795 BUG();
2796 }
2797 return;
2798}
2799
2800static void
2801rb_update_iter_read_stamp(struct ring_buffer_iter *iter,
2802 struct ring_buffer_event *event)
2803{
2804 u64 delta;
2805
Lai Jiangshan334d4162009-04-24 11:27:05 +08002806 switch (event->type_len) {
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002807 case RINGBUF_TYPE_PADDING:
2808 return;
2809
2810 case RINGBUF_TYPE_TIME_EXTEND:
2811 delta = event->array[0];
2812 delta <<= TS_SHIFT;
2813 delta += event->time_delta;
2814 iter->read_stamp += delta;
2815 return;
2816
2817 case RINGBUF_TYPE_TIME_STAMP:
2818 /* FIXME: not implemented */
2819 return;
2820
2821 case RINGBUF_TYPE_DATA:
2822 iter->read_stamp += event->time_delta;
2823 return;
2824
2825 default:
2826 BUG();
2827 }
2828 return;
2829}
2830
Steven Rostedtd7690412008-10-01 00:29:53 -04002831static struct buffer_page *
2832rb_get_reader_page(struct ring_buffer_per_cpu *cpu_buffer)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002833{
Steven Rostedtd7690412008-10-01 00:29:53 -04002834 struct buffer_page *reader = NULL;
2835 unsigned long flags;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002836 int nr_loops = 0;
Steven Rostedt77ae3652009-03-27 11:00:29 -04002837 int ret;
Steven Rostedtd7690412008-10-01 00:29:53 -04002838
Steven Rostedt3e03fb72008-11-06 00:09:43 -05002839 local_irq_save(flags);
2840 __raw_spin_lock(&cpu_buffer->lock);
Steven Rostedtd7690412008-10-01 00:29:53 -04002841
2842 again:
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002843 /*
2844 * This should normally only loop twice. But because the
2845 * start of the reader inserts an empty page, it causes
2846 * a case where we will loop three times. There should be no
2847 * reason to loop four times (that I know of).
2848 */
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05002849 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 3)) {
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002850 reader = NULL;
2851 goto out;
2852 }
2853
Steven Rostedtd7690412008-10-01 00:29:53 -04002854 reader = cpu_buffer->reader_page;
2855
2856 /* If there's more to read, return this page */
Steven Rostedtbf41a152008-10-04 02:00:59 -04002857 if (cpu_buffer->reader_page->read < rb_page_size(reader))
Steven Rostedtd7690412008-10-01 00:29:53 -04002858 goto out;
2859
2860 /* Never should we have an index greater than the size */
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05002861 if (RB_WARN_ON(cpu_buffer,
2862 cpu_buffer->reader_page->read > rb_page_size(reader)))
2863 goto out;
Steven Rostedtd7690412008-10-01 00:29:53 -04002864
2865 /* check if we caught up to the tail */
2866 reader = NULL;
Steven Rostedtbf41a152008-10-04 02:00:59 -04002867 if (cpu_buffer->commit_page == cpu_buffer->reader_page)
Steven Rostedtd7690412008-10-01 00:29:53 -04002868 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002869
2870 /*
Steven Rostedtd7690412008-10-01 00:29:53 -04002871 * Reset the reader page to size zero.
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002872 */
Steven Rostedt77ae3652009-03-27 11:00:29 -04002873 local_set(&cpu_buffer->reader_page->write, 0);
2874 local_set(&cpu_buffer->reader_page->entries, 0);
2875 local_set(&cpu_buffer->reader_page->page->commit, 0);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002876
Steven Rostedt77ae3652009-03-27 11:00:29 -04002877 spin:
2878 /*
2879 * Splice the empty reader page into the list around the head.
2880 */
2881 reader = rb_set_head_page(cpu_buffer);
Steven Rostedtd7690412008-10-01 00:29:53 -04002882 cpu_buffer->reader_page->list.next = reader->list.next;
2883 cpu_buffer->reader_page->list.prev = reader->list.prev;
Steven Rostedtbf41a152008-10-04 02:00:59 -04002884
Steven Rostedt3adc54f2009-03-30 15:32:01 -04002885 /*
2886 * cpu_buffer->pages just needs to point to the buffer, it
2887 * has no specific buffer page to point to. Lets move it out
2888 * of our way so we don't accidently swap it.
2889 */
2890 cpu_buffer->pages = reader->list.prev;
2891
Steven Rostedt77ae3652009-03-27 11:00:29 -04002892 /* The reader page will be pointing to the new head */
2893 rb_set_list_to_head(cpu_buffer, &cpu_buffer->reader_page->list);
Steven Rostedtd7690412008-10-01 00:29:53 -04002894
2895 /*
Steven Rostedt77ae3652009-03-27 11:00:29 -04002896 * Here's the tricky part.
2897 *
2898 * We need to move the pointer past the header page.
2899 * But we can only do that if a writer is not currently
2900 * moving it. The page before the header page has the
2901 * flag bit '1' set if it is pointing to the page we want.
2902 * but if the writer is in the process of moving it
2903 * than it will be '2' or already moved '0'.
Steven Rostedtd7690412008-10-01 00:29:53 -04002904 */
Steven Rostedtd7690412008-10-01 00:29:53 -04002905
Steven Rostedt77ae3652009-03-27 11:00:29 -04002906 ret = rb_head_page_replace(reader, cpu_buffer->reader_page);
2907
2908 /*
2909 * If we did not convert it, then we must try again.
2910 */
2911 if (!ret)
2912 goto spin;
2913
2914 /*
2915 * Yeah! We succeeded in replacing the page.
2916 *
2917 * Now make the new head point back to the reader page.
2918 */
2919 reader->list.next->prev = &cpu_buffer->reader_page->list;
2920 rb_inc_page(cpu_buffer, &cpu_buffer->head_page);
Steven Rostedtd7690412008-10-01 00:29:53 -04002921
2922 /* Finally update the reader page to the new head */
2923 cpu_buffer->reader_page = reader;
2924 rb_reset_reader_page(cpu_buffer);
2925
2926 goto again;
2927
2928 out:
Steven Rostedt3e03fb72008-11-06 00:09:43 -05002929 __raw_spin_unlock(&cpu_buffer->lock);
2930 local_irq_restore(flags);
Steven Rostedtd7690412008-10-01 00:29:53 -04002931
2932 return reader;
2933}
2934
2935static void rb_advance_reader(struct ring_buffer_per_cpu *cpu_buffer)
2936{
2937 struct ring_buffer_event *event;
2938 struct buffer_page *reader;
2939 unsigned length;
2940
2941 reader = rb_get_reader_page(cpu_buffer);
2942
2943 /* This function should not be called when buffer is empty */
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05002944 if (RB_WARN_ON(cpu_buffer, !reader))
2945 return;
Steven Rostedtd7690412008-10-01 00:29:53 -04002946
2947 event = rb_reader_event(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002948
Steven Rostedta1863c22009-09-03 10:23:58 -04002949 if (event->type_len <= RINGBUF_TYPE_DATA_TYPE_LEN_MAX)
Steven Rostedte4906ef2009-04-30 20:49:44 -04002950 cpu_buffer->read++;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002951
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002952 rb_update_read_stamp(cpu_buffer, event);
2953
Steven Rostedtd7690412008-10-01 00:29:53 -04002954 length = rb_event_length(event);
Steven Rostedt6f807ac2008-10-04 02:00:58 -04002955 cpu_buffer->reader_page->read += length;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002956}
2957
2958static void rb_advance_iter(struct ring_buffer_iter *iter)
2959{
2960 struct ring_buffer *buffer;
2961 struct ring_buffer_per_cpu *cpu_buffer;
2962 struct ring_buffer_event *event;
2963 unsigned length;
2964
2965 cpu_buffer = iter->cpu_buffer;
2966 buffer = cpu_buffer->buffer;
2967
2968 /*
2969 * Check if we are at the end of the buffer.
2970 */
Steven Rostedtbf41a152008-10-04 02:00:59 -04002971 if (iter->head >= rb_page_size(iter->head_page)) {
Steven Rostedtea05b572009-06-03 09:30:10 -04002972 /* discarded commits can make the page empty */
2973 if (iter->head_page == cpu_buffer->commit_page)
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05002974 return;
Steven Rostedtd7690412008-10-01 00:29:53 -04002975 rb_inc_iter(iter);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002976 return;
2977 }
2978
2979 event = rb_iter_head_event(iter);
2980
2981 length = rb_event_length(event);
2982
2983 /*
2984 * This should not be called to advance the header if we are
2985 * at the tail of the buffer.
2986 */
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05002987 if (RB_WARN_ON(cpu_buffer,
Steven Rostedtf536aaf2008-11-10 23:07:30 -05002988 (iter->head_page == cpu_buffer->commit_page) &&
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05002989 (iter->head + length > rb_commit_index(cpu_buffer))))
2990 return;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002991
2992 rb_update_iter_read_stamp(iter, event);
2993
2994 iter->head += length;
2995
2996 /* check for end of page padding */
Steven Rostedtbf41a152008-10-04 02:00:59 -04002997 if ((iter->head >= rb_page_size(iter->head_page)) &&
2998 (iter->head_page != cpu_buffer->commit_page))
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002999 rb_advance_iter(iter);
3000}
3001
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003002static struct ring_buffer_event *
Robert Richterd8eeb2d2009-07-31 14:58:04 +02003003rb_buffer_peek(struct ring_buffer_per_cpu *cpu_buffer, u64 *ts)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003004{
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003005 struct ring_buffer_event *event;
Steven Rostedtd7690412008-10-01 00:29:53 -04003006 struct buffer_page *reader;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003007 int nr_loops = 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003008
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003009 again:
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003010 /*
3011 * We repeat when a timestamp is encountered. It is possible
3012 * to get multiple timestamps from an interrupt entering just
Steven Rostedtea05b572009-06-03 09:30:10 -04003013 * as one timestamp is about to be written, or from discarded
3014 * commits. The most that we can have is the number on a single page.
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003015 */
Steven Rostedtea05b572009-06-03 09:30:10 -04003016 if (RB_WARN_ON(cpu_buffer, ++nr_loops > RB_TIMESTAMPS_PER_PAGE))
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003017 return NULL;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003018
Steven Rostedtd7690412008-10-01 00:29:53 -04003019 reader = rb_get_reader_page(cpu_buffer);
3020 if (!reader)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003021 return NULL;
3022
Steven Rostedtd7690412008-10-01 00:29:53 -04003023 event = rb_reader_event(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003024
Lai Jiangshan334d4162009-04-24 11:27:05 +08003025 switch (event->type_len) {
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003026 case RINGBUF_TYPE_PADDING:
Tom Zanussi2d622712009-03-22 03:30:49 -05003027 if (rb_null_event(event))
3028 RB_WARN_ON(cpu_buffer, 1);
3029 /*
3030 * Because the writer could be discarding every
3031 * event it creates (which would probably be bad)
3032 * if we were to go back to "again" then we may never
3033 * catch up, and will trigger the warn on, or lock
3034 * the box. Return the padding, and we will release
3035 * the current locks, and try again.
3036 */
Tom Zanussi2d622712009-03-22 03:30:49 -05003037 return event;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003038
3039 case RINGBUF_TYPE_TIME_EXTEND:
3040 /* Internal data, OK to advance */
Steven Rostedtd7690412008-10-01 00:29:53 -04003041 rb_advance_reader(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003042 goto again;
3043
3044 case RINGBUF_TYPE_TIME_STAMP:
3045 /* FIXME: not implemented */
Steven Rostedtd7690412008-10-01 00:29:53 -04003046 rb_advance_reader(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003047 goto again;
3048
3049 case RINGBUF_TYPE_DATA:
3050 if (ts) {
3051 *ts = cpu_buffer->read_stamp + event->time_delta;
Robert Richterd8eeb2d2009-07-31 14:58:04 +02003052 ring_buffer_normalize_time_stamp(cpu_buffer->buffer,
Steven Rostedt37886f62009-03-17 17:22:06 -04003053 cpu_buffer->cpu, ts);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003054 }
3055 return event;
3056
3057 default:
3058 BUG();
3059 }
3060
3061 return NULL;
3062}
Robert Richterc4f50182008-12-11 16:49:22 +01003063EXPORT_SYMBOL_GPL(ring_buffer_peek);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003064
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003065static struct ring_buffer_event *
3066rb_iter_peek(struct ring_buffer_iter *iter, u64 *ts)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003067{
3068 struct ring_buffer *buffer;
3069 struct ring_buffer_per_cpu *cpu_buffer;
3070 struct ring_buffer_event *event;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003071 int nr_loops = 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003072
3073 if (ring_buffer_iter_empty(iter))
3074 return NULL;
3075
3076 cpu_buffer = iter->cpu_buffer;
3077 buffer = cpu_buffer->buffer;
3078
3079 again:
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003080 /*
Steven Rostedtea05b572009-06-03 09:30:10 -04003081 * We repeat when a timestamp is encountered.
3082 * We can get multiple timestamps by nested interrupts or also
3083 * if filtering is on (discarding commits). Since discarding
3084 * commits can be frequent we can get a lot of timestamps.
3085 * But we limit them by not adding timestamps if they begin
3086 * at the start of a page.
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003087 */
Steven Rostedtea05b572009-06-03 09:30:10 -04003088 if (RB_WARN_ON(cpu_buffer, ++nr_loops > RB_TIMESTAMPS_PER_PAGE))
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003089 return NULL;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003090
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003091 if (rb_per_cpu_empty(cpu_buffer))
3092 return NULL;
3093
3094 event = rb_iter_head_event(iter);
3095
Lai Jiangshan334d4162009-04-24 11:27:05 +08003096 switch (event->type_len) {
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003097 case RINGBUF_TYPE_PADDING:
Tom Zanussi2d622712009-03-22 03:30:49 -05003098 if (rb_null_event(event)) {
3099 rb_inc_iter(iter);
3100 goto again;
3101 }
3102 rb_advance_iter(iter);
3103 return event;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003104
3105 case RINGBUF_TYPE_TIME_EXTEND:
3106 /* Internal data, OK to advance */
3107 rb_advance_iter(iter);
3108 goto again;
3109
3110 case RINGBUF_TYPE_TIME_STAMP:
3111 /* FIXME: not implemented */
3112 rb_advance_iter(iter);
3113 goto again;
3114
3115 case RINGBUF_TYPE_DATA:
3116 if (ts) {
3117 *ts = iter->read_stamp + event->time_delta;
Steven Rostedt37886f62009-03-17 17:22:06 -04003118 ring_buffer_normalize_time_stamp(buffer,
3119 cpu_buffer->cpu, ts);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003120 }
3121 return event;
3122
3123 default:
3124 BUG();
3125 }
3126
3127 return NULL;
3128}
Robert Richterc4f50182008-12-11 16:49:22 +01003129EXPORT_SYMBOL_GPL(ring_buffer_iter_peek);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003130
Steven Rostedt8d707e82009-06-16 21:22:48 -04003131static inline int rb_ok_to_lock(void)
3132{
3133 /*
3134 * If an NMI die dumps out the content of the ring buffer
3135 * do not grab locks. We also permanently disable the ring
3136 * buffer too. A one time deal is all you get from reading
3137 * the ring buffer from an NMI.
3138 */
Steven Rostedt464e85e2009-08-05 15:26:37 -04003139 if (likely(!in_nmi()))
Steven Rostedt8d707e82009-06-16 21:22:48 -04003140 return 1;
3141
3142 tracing_off_permanent();
3143 return 0;
3144}
3145
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003146/**
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003147 * ring_buffer_peek - peek at the next event to be read
3148 * @buffer: The ring buffer to read
3149 * @cpu: The cpu to peak at
3150 * @ts: The timestamp counter of this event.
3151 *
3152 * This will return the event that will be read next, but does
3153 * not consume the data.
3154 */
3155struct ring_buffer_event *
3156ring_buffer_peek(struct ring_buffer *buffer, int cpu, u64 *ts)
3157{
3158 struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
Steven Rostedt8aabee52009-03-12 13:13:49 -04003159 struct ring_buffer_event *event;
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003160 unsigned long flags;
Steven Rostedt8d707e82009-06-16 21:22:48 -04003161 int dolock;
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003162
Steven Rostedt554f7862009-03-11 22:00:13 -04003163 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04003164 return NULL;
Steven Rostedt554f7862009-03-11 22:00:13 -04003165
Steven Rostedt8d707e82009-06-16 21:22:48 -04003166 dolock = rb_ok_to_lock();
Tom Zanussi2d622712009-03-22 03:30:49 -05003167 again:
Steven Rostedt8d707e82009-06-16 21:22:48 -04003168 local_irq_save(flags);
3169 if (dolock)
3170 spin_lock(&cpu_buffer->reader_lock);
Robert Richterd8eeb2d2009-07-31 14:58:04 +02003171 event = rb_buffer_peek(cpu_buffer, ts);
Robert Richter469535a2009-07-30 19:19:18 +02003172 if (event && event->type_len == RINGBUF_TYPE_PADDING)
3173 rb_advance_reader(cpu_buffer);
Steven Rostedt8d707e82009-06-16 21:22:48 -04003174 if (dolock)
3175 spin_unlock(&cpu_buffer->reader_lock);
3176 local_irq_restore(flags);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003177
Steven Rostedt1b959e12009-09-03 10:12:13 -04003178 if (event && event->type_len == RINGBUF_TYPE_PADDING)
Tom Zanussi2d622712009-03-22 03:30:49 -05003179 goto again;
Tom Zanussi2d622712009-03-22 03:30:49 -05003180
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003181 return event;
3182}
3183
3184/**
3185 * ring_buffer_iter_peek - peek at the next event to be read
3186 * @iter: The ring buffer iterator
3187 * @ts: The timestamp counter of this event.
3188 *
3189 * This will return the event that will be read next, but does
3190 * not increment the iterator.
3191 */
3192struct ring_buffer_event *
3193ring_buffer_iter_peek(struct ring_buffer_iter *iter, u64 *ts)
3194{
3195 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
3196 struct ring_buffer_event *event;
3197 unsigned long flags;
3198
Tom Zanussi2d622712009-03-22 03:30:49 -05003199 again:
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003200 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
3201 event = rb_iter_peek(iter, ts);
3202 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
3203
Steven Rostedt1b959e12009-09-03 10:12:13 -04003204 if (event && event->type_len == RINGBUF_TYPE_PADDING)
Tom Zanussi2d622712009-03-22 03:30:49 -05003205 goto again;
Tom Zanussi2d622712009-03-22 03:30:49 -05003206
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003207 return event;
3208}
3209
3210/**
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003211 * ring_buffer_consume - return an event and consume it
3212 * @buffer: The ring buffer to get the next event from
3213 *
3214 * Returns the next event in the ring buffer, and that event is consumed.
3215 * Meaning, that sequential reads will keep returning a different event,
3216 * and eventually empty the ring buffer if the producer is slower.
3217 */
3218struct ring_buffer_event *
3219ring_buffer_consume(struct ring_buffer *buffer, int cpu, u64 *ts)
3220{
Steven Rostedt554f7862009-03-11 22:00:13 -04003221 struct ring_buffer_per_cpu *cpu_buffer;
3222 struct ring_buffer_event *event = NULL;
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003223 unsigned long flags;
Steven Rostedt8d707e82009-06-16 21:22:48 -04003224 int dolock;
3225
3226 dolock = rb_ok_to_lock();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003227
Tom Zanussi2d622712009-03-22 03:30:49 -05003228 again:
Steven Rostedt554f7862009-03-11 22:00:13 -04003229 /* might be called in atomic */
3230 preempt_disable();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003231
Steven Rostedt554f7862009-03-11 22:00:13 -04003232 if (!cpumask_test_cpu(cpu, buffer->cpumask))
3233 goto out;
3234
3235 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt8d707e82009-06-16 21:22:48 -04003236 local_irq_save(flags);
3237 if (dolock)
3238 spin_lock(&cpu_buffer->reader_lock);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003239
Robert Richterd8eeb2d2009-07-31 14:58:04 +02003240 event = rb_buffer_peek(cpu_buffer, ts);
Robert Richter469535a2009-07-30 19:19:18 +02003241 if (event)
3242 rb_advance_reader(cpu_buffer);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003243
Steven Rostedt8d707e82009-06-16 21:22:48 -04003244 if (dolock)
3245 spin_unlock(&cpu_buffer->reader_lock);
3246 local_irq_restore(flags);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003247
Steven Rostedt554f7862009-03-11 22:00:13 -04003248 out:
3249 preempt_enable();
3250
Steven Rostedt1b959e12009-09-03 10:12:13 -04003251 if (event && event->type_len == RINGBUF_TYPE_PADDING)
Tom Zanussi2d622712009-03-22 03:30:49 -05003252 goto again;
Tom Zanussi2d622712009-03-22 03:30:49 -05003253
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003254 return event;
3255}
Robert Richterc4f50182008-12-11 16:49:22 +01003256EXPORT_SYMBOL_GPL(ring_buffer_consume);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003257
3258/**
3259 * ring_buffer_read_start - start a non consuming read of the buffer
3260 * @buffer: The ring buffer to read from
3261 * @cpu: The cpu buffer to iterate over
3262 *
3263 * This starts up an iteration through the buffer. It also disables
3264 * the recording to the buffer until the reading is finished.
3265 * This prevents the reading from being corrupted. This is not
3266 * a consuming read, so a producer is not expected.
3267 *
3268 * Must be paired with ring_buffer_finish.
3269 */
3270struct ring_buffer_iter *
3271ring_buffer_read_start(struct ring_buffer *buffer, int cpu)
3272{
3273 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedt8aabee52009-03-12 13:13:49 -04003274 struct ring_buffer_iter *iter;
Steven Rostedtd7690412008-10-01 00:29:53 -04003275 unsigned long flags;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003276
Rusty Russell9e01c1b2009-01-01 10:12:22 +10303277 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04003278 return NULL;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003279
3280 iter = kmalloc(sizeof(*iter), GFP_KERNEL);
3281 if (!iter)
Steven Rostedt8aabee52009-03-12 13:13:49 -04003282 return NULL;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003283
3284 cpu_buffer = buffer->buffers[cpu];
3285
3286 iter->cpu_buffer = cpu_buffer;
3287
3288 atomic_inc(&cpu_buffer->record_disabled);
3289 synchronize_sched();
3290
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003291 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
Steven Rostedt3e03fb72008-11-06 00:09:43 -05003292 __raw_spin_lock(&cpu_buffer->lock);
Steven Rostedt642edba2008-11-12 00:01:26 -05003293 rb_iter_reset(iter);
Steven Rostedt3e03fb72008-11-06 00:09:43 -05003294 __raw_spin_unlock(&cpu_buffer->lock);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003295 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003296
3297 return iter;
3298}
Robert Richterc4f50182008-12-11 16:49:22 +01003299EXPORT_SYMBOL_GPL(ring_buffer_read_start);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003300
3301/**
3302 * ring_buffer_finish - finish reading the iterator of the buffer
3303 * @iter: The iterator retrieved by ring_buffer_start
3304 *
3305 * This re-enables the recording to the buffer, and frees the
3306 * iterator.
3307 */
3308void
3309ring_buffer_read_finish(struct ring_buffer_iter *iter)
3310{
3311 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
3312
3313 atomic_dec(&cpu_buffer->record_disabled);
3314 kfree(iter);
3315}
Robert Richterc4f50182008-12-11 16:49:22 +01003316EXPORT_SYMBOL_GPL(ring_buffer_read_finish);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003317
3318/**
3319 * ring_buffer_read - read the next item in the ring buffer by the iterator
3320 * @iter: The ring buffer iterator
3321 * @ts: The time stamp of the event read.
3322 *
3323 * This reads the next event in the ring buffer and increments the iterator.
3324 */
3325struct ring_buffer_event *
3326ring_buffer_read(struct ring_buffer_iter *iter, u64 *ts)
3327{
3328 struct ring_buffer_event *event;
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003329 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
3330 unsigned long flags;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003331
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003332 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
Steven Rostedt7e9391c2009-09-03 10:02:09 -04003333 again:
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003334 event = rb_iter_peek(iter, ts);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003335 if (!event)
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003336 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003337
Steven Rostedt7e9391c2009-09-03 10:02:09 -04003338 if (event->type_len == RINGBUF_TYPE_PADDING)
3339 goto again;
3340
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003341 rb_advance_iter(iter);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003342 out:
3343 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003344
3345 return event;
3346}
Robert Richterc4f50182008-12-11 16:49:22 +01003347EXPORT_SYMBOL_GPL(ring_buffer_read);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003348
3349/**
3350 * ring_buffer_size - return the size of the ring buffer (in bytes)
3351 * @buffer: The ring buffer.
3352 */
3353unsigned long ring_buffer_size(struct ring_buffer *buffer)
3354{
3355 return BUF_PAGE_SIZE * buffer->pages;
3356}
Robert Richterc4f50182008-12-11 16:49:22 +01003357EXPORT_SYMBOL_GPL(ring_buffer_size);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003358
3359static void
3360rb_reset_cpu(struct ring_buffer_per_cpu *cpu_buffer)
3361{
Steven Rostedt77ae3652009-03-27 11:00:29 -04003362 rb_head_page_deactivate(cpu_buffer);
3363
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003364 cpu_buffer->head_page
Steven Rostedt3adc54f2009-03-30 15:32:01 -04003365 = list_entry(cpu_buffer->pages, struct buffer_page, list);
Steven Rostedtbf41a152008-10-04 02:00:59 -04003366 local_set(&cpu_buffer->head_page->write, 0);
Steven Rostedt778c55d2009-05-01 18:44:45 -04003367 local_set(&cpu_buffer->head_page->entries, 0);
Steven Rostedtabc9b562008-12-02 15:34:06 -05003368 local_set(&cpu_buffer->head_page->page->commit, 0);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003369
Steven Rostedt6f807ac2008-10-04 02:00:58 -04003370 cpu_buffer->head_page->read = 0;
Steven Rostedtbf41a152008-10-04 02:00:59 -04003371
3372 cpu_buffer->tail_page = cpu_buffer->head_page;
3373 cpu_buffer->commit_page = cpu_buffer->head_page;
3374
3375 INIT_LIST_HEAD(&cpu_buffer->reader_page->list);
3376 local_set(&cpu_buffer->reader_page->write, 0);
Steven Rostedt778c55d2009-05-01 18:44:45 -04003377 local_set(&cpu_buffer->reader_page->entries, 0);
Steven Rostedtabc9b562008-12-02 15:34:06 -05003378 local_set(&cpu_buffer->reader_page->page->commit, 0);
Steven Rostedt6f807ac2008-10-04 02:00:58 -04003379 cpu_buffer->reader_page->read = 0;
Steven Rostedtd7690412008-10-01 00:29:53 -04003380
Steven Rostedt77ae3652009-03-27 11:00:29 -04003381 local_set(&cpu_buffer->commit_overrun, 0);
3382 local_set(&cpu_buffer->overrun, 0);
Steven Rostedte4906ef2009-04-30 20:49:44 -04003383 local_set(&cpu_buffer->entries, 0);
Steven Rostedtfa743952009-06-16 12:37:57 -04003384 local_set(&cpu_buffer->committing, 0);
3385 local_set(&cpu_buffer->commits, 0);
Steven Rostedt77ae3652009-03-27 11:00:29 -04003386 cpu_buffer->read = 0;
Steven Rostedt69507c02009-01-21 18:45:57 -05003387
3388 cpu_buffer->write_stamp = 0;
3389 cpu_buffer->read_stamp = 0;
Steven Rostedt77ae3652009-03-27 11:00:29 -04003390
3391 rb_head_page_activate(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003392}
3393
3394/**
3395 * ring_buffer_reset_cpu - reset a ring buffer per CPU buffer
3396 * @buffer: The ring buffer to reset a per cpu buffer of
3397 * @cpu: The CPU buffer to be reset
3398 */
3399void ring_buffer_reset_cpu(struct ring_buffer *buffer, int cpu)
3400{
3401 struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
3402 unsigned long flags;
3403
Rusty Russell9e01c1b2009-01-01 10:12:22 +10303404 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04003405 return;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003406
Steven Rostedt41ede232009-05-01 20:26:54 -04003407 atomic_inc(&cpu_buffer->record_disabled);
3408
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003409 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
3410
Steven Rostedt41b6a952009-09-02 09:59:48 -04003411 if (RB_WARN_ON(cpu_buffer, local_read(&cpu_buffer->committing)))
3412 goto out;
3413
Steven Rostedt3e03fb72008-11-06 00:09:43 -05003414 __raw_spin_lock(&cpu_buffer->lock);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003415
3416 rb_reset_cpu(cpu_buffer);
3417
Steven Rostedt3e03fb72008-11-06 00:09:43 -05003418 __raw_spin_unlock(&cpu_buffer->lock);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003419
Steven Rostedt41b6a952009-09-02 09:59:48 -04003420 out:
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003421 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
Steven Rostedt41ede232009-05-01 20:26:54 -04003422
3423 atomic_dec(&cpu_buffer->record_disabled);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003424}
Robert Richterc4f50182008-12-11 16:49:22 +01003425EXPORT_SYMBOL_GPL(ring_buffer_reset_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003426
3427/**
3428 * ring_buffer_reset - reset a ring buffer
3429 * @buffer: The ring buffer to reset all cpu buffers
3430 */
3431void ring_buffer_reset(struct ring_buffer *buffer)
3432{
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003433 int cpu;
3434
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003435 for_each_buffer_cpu(buffer, cpu)
Steven Rostedtd7690412008-10-01 00:29:53 -04003436 ring_buffer_reset_cpu(buffer, cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003437}
Robert Richterc4f50182008-12-11 16:49:22 +01003438EXPORT_SYMBOL_GPL(ring_buffer_reset);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003439
3440/**
3441 * rind_buffer_empty - is the ring buffer empty?
3442 * @buffer: The ring buffer to test
3443 */
3444int ring_buffer_empty(struct ring_buffer *buffer)
3445{
3446 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedtd4788202009-06-17 00:39:43 -04003447 unsigned long flags;
Steven Rostedt8d707e82009-06-16 21:22:48 -04003448 int dolock;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003449 int cpu;
Steven Rostedtd4788202009-06-17 00:39:43 -04003450 int ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003451
Steven Rostedt8d707e82009-06-16 21:22:48 -04003452 dolock = rb_ok_to_lock();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003453
3454 /* yes this is racy, but if you don't like the race, lock the buffer */
3455 for_each_buffer_cpu(buffer, cpu) {
3456 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt8d707e82009-06-16 21:22:48 -04003457 local_irq_save(flags);
3458 if (dolock)
3459 spin_lock(&cpu_buffer->reader_lock);
Steven Rostedtd4788202009-06-17 00:39:43 -04003460 ret = rb_per_cpu_empty(cpu_buffer);
Steven Rostedt8d707e82009-06-16 21:22:48 -04003461 if (dolock)
3462 spin_unlock(&cpu_buffer->reader_lock);
3463 local_irq_restore(flags);
3464
Steven Rostedtd4788202009-06-17 00:39:43 -04003465 if (!ret)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003466 return 0;
3467 }
Steven Rostedt554f7862009-03-11 22:00:13 -04003468
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003469 return 1;
3470}
Robert Richterc4f50182008-12-11 16:49:22 +01003471EXPORT_SYMBOL_GPL(ring_buffer_empty);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003472
3473/**
3474 * ring_buffer_empty_cpu - is a cpu buffer of a ring buffer empty?
3475 * @buffer: The ring buffer
3476 * @cpu: The CPU buffer to test
3477 */
3478int ring_buffer_empty_cpu(struct ring_buffer *buffer, int cpu)
3479{
3480 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedtd4788202009-06-17 00:39:43 -04003481 unsigned long flags;
Steven Rostedt8d707e82009-06-16 21:22:48 -04003482 int dolock;
Steven Rostedt8aabee52009-03-12 13:13:49 -04003483 int ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003484
Rusty Russell9e01c1b2009-01-01 10:12:22 +10303485 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04003486 return 1;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003487
Steven Rostedt8d707e82009-06-16 21:22:48 -04003488 dolock = rb_ok_to_lock();
Steven Rostedt554f7862009-03-11 22:00:13 -04003489
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003490 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt8d707e82009-06-16 21:22:48 -04003491 local_irq_save(flags);
3492 if (dolock)
3493 spin_lock(&cpu_buffer->reader_lock);
Steven Rostedt554f7862009-03-11 22:00:13 -04003494 ret = rb_per_cpu_empty(cpu_buffer);
Steven Rostedt8d707e82009-06-16 21:22:48 -04003495 if (dolock)
3496 spin_unlock(&cpu_buffer->reader_lock);
3497 local_irq_restore(flags);
Steven Rostedt554f7862009-03-11 22:00:13 -04003498
3499 return ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003500}
Robert Richterc4f50182008-12-11 16:49:22 +01003501EXPORT_SYMBOL_GPL(ring_buffer_empty_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003502
Steven Rostedt85bac322009-09-04 14:24:40 -04003503#ifdef CONFIG_RING_BUFFER_ALLOW_SWAP
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003504/**
3505 * ring_buffer_swap_cpu - swap a CPU buffer between two ring buffers
3506 * @buffer_a: One buffer to swap with
3507 * @buffer_b: The other buffer to swap with
3508 *
3509 * This function is useful for tracers that want to take a "snapshot"
3510 * of a CPU buffer and has another back up buffer lying around.
3511 * it is expected that the tracer handles the cpu buffer not being
3512 * used at the moment.
3513 */
3514int ring_buffer_swap_cpu(struct ring_buffer *buffer_a,
3515 struct ring_buffer *buffer_b, int cpu)
3516{
3517 struct ring_buffer_per_cpu *cpu_buffer_a;
3518 struct ring_buffer_per_cpu *cpu_buffer_b;
Steven Rostedt554f7862009-03-11 22:00:13 -04003519 int ret = -EINVAL;
3520
Rusty Russell9e01c1b2009-01-01 10:12:22 +10303521 if (!cpumask_test_cpu(cpu, buffer_a->cpumask) ||
3522 !cpumask_test_cpu(cpu, buffer_b->cpumask))
Steven Rostedt554f7862009-03-11 22:00:13 -04003523 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003524
3525 /* At least make sure the two buffers are somewhat the same */
Lai Jiangshan6d102bc2008-12-17 17:48:23 +08003526 if (buffer_a->pages != buffer_b->pages)
Steven Rostedt554f7862009-03-11 22:00:13 -04003527 goto out;
3528
3529 ret = -EAGAIN;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003530
Steven Rostedt97b17ef2009-01-21 15:24:56 -05003531 if (ring_buffer_flags != RB_BUFFERS_ON)
Steven Rostedt554f7862009-03-11 22:00:13 -04003532 goto out;
Steven Rostedt97b17ef2009-01-21 15:24:56 -05003533
3534 if (atomic_read(&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(&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 cpu_buffer_a = buffer_a->buffers[cpu];
3541 cpu_buffer_b = buffer_b->buffers[cpu];
3542
Steven Rostedt97b17ef2009-01-21 15:24:56 -05003543 if (atomic_read(&cpu_buffer_a->record_disabled))
Steven Rostedt554f7862009-03-11 22:00:13 -04003544 goto out;
Steven Rostedt97b17ef2009-01-21 15:24:56 -05003545
3546 if (atomic_read(&cpu_buffer_b->record_disabled))
Steven Rostedt554f7862009-03-11 22:00:13 -04003547 goto out;
Steven Rostedt97b17ef2009-01-21 15:24:56 -05003548
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003549 /*
3550 * We can't do a synchronize_sched here because this
3551 * function can be called in atomic context.
3552 * Normally this will be called from the same CPU as cpu.
3553 * If not it's up to the caller to protect this.
3554 */
3555 atomic_inc(&cpu_buffer_a->record_disabled);
3556 atomic_inc(&cpu_buffer_b->record_disabled);
3557
Steven Rostedt98277992009-09-02 10:56:15 -04003558 ret = -EBUSY;
3559 if (local_read(&cpu_buffer_a->committing))
3560 goto out_dec;
3561 if (local_read(&cpu_buffer_b->committing))
3562 goto out_dec;
3563
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003564 buffer_a->buffers[cpu] = cpu_buffer_b;
3565 buffer_b->buffers[cpu] = cpu_buffer_a;
3566
3567 cpu_buffer_b->buffer = buffer_a;
3568 cpu_buffer_a->buffer = buffer_b;
3569
Steven Rostedt98277992009-09-02 10:56:15 -04003570 ret = 0;
3571
3572out_dec:
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003573 atomic_dec(&cpu_buffer_a->record_disabled);
3574 atomic_dec(&cpu_buffer_b->record_disabled);
Steven Rostedt554f7862009-03-11 22:00:13 -04003575out:
Steven Rostedt554f7862009-03-11 22:00:13 -04003576 return ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003577}
Robert Richterc4f50182008-12-11 16:49:22 +01003578EXPORT_SYMBOL_GPL(ring_buffer_swap_cpu);
Steven Rostedt85bac322009-09-04 14:24:40 -04003579#endif /* CONFIG_RING_BUFFER_ALLOW_SWAP */
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003580
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003581/**
3582 * ring_buffer_alloc_read_page - allocate a page to read from buffer
3583 * @buffer: the buffer to allocate for.
3584 *
3585 * This function is used in conjunction with ring_buffer_read_page.
3586 * When reading a full page from the ring buffer, these functions
3587 * can be used to speed up the process. The calling function should
3588 * allocate a few pages first with this function. Then when it
3589 * needs to get pages from the ring buffer, it passes the result
3590 * of this function into ring_buffer_read_page, which will swap
3591 * the page that was allocated, with the read page of the buffer.
3592 *
3593 * Returns:
3594 * The page allocated, or NULL on error.
3595 */
3596void *ring_buffer_alloc_read_page(struct ring_buffer *buffer)
3597{
Steven Rostedt044fa782008-12-02 23:50:03 -05003598 struct buffer_data_page *bpage;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003599 unsigned long addr;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003600
3601 addr = __get_free_page(GFP_KERNEL);
3602 if (!addr)
3603 return NULL;
3604
Steven Rostedt044fa782008-12-02 23:50:03 -05003605 bpage = (void *)addr;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003606
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003607 rb_init_page(bpage);
3608
Steven Rostedt044fa782008-12-02 23:50:03 -05003609 return bpage;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003610}
Steven Rostedtd6ce96d2009-05-05 01:15:24 -04003611EXPORT_SYMBOL_GPL(ring_buffer_alloc_read_page);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003612
3613/**
3614 * ring_buffer_free_read_page - free an allocated read page
3615 * @buffer: the buffer the page was allocate for
3616 * @data: the page to free
3617 *
3618 * Free a page allocated from ring_buffer_alloc_read_page.
3619 */
3620void ring_buffer_free_read_page(struct ring_buffer *buffer, void *data)
3621{
3622 free_page((unsigned long)data);
3623}
Steven Rostedtd6ce96d2009-05-05 01:15:24 -04003624EXPORT_SYMBOL_GPL(ring_buffer_free_read_page);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003625
3626/**
3627 * ring_buffer_read_page - extract a page from the ring buffer
3628 * @buffer: buffer to extract from
3629 * @data_page: the page to use allocated from ring_buffer_alloc_read_page
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003630 * @len: amount to extract
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003631 * @cpu: the cpu of the buffer to extract
3632 * @full: should the extraction only happen when the page is full.
3633 *
3634 * This function will pull out a page from the ring buffer and consume it.
3635 * @data_page must be the address of the variable that was returned
3636 * from ring_buffer_alloc_read_page. This is because the page might be used
3637 * to swap with a page in the ring buffer.
3638 *
3639 * for example:
Lai Jiangshanb85fa012009-02-09 14:21:14 +08003640 * rpage = ring_buffer_alloc_read_page(buffer);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003641 * if (!rpage)
3642 * return error;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003643 * ret = ring_buffer_read_page(buffer, &rpage, len, cpu, 0);
Lai Jiangshan667d2412009-02-09 14:21:17 +08003644 * if (ret >= 0)
3645 * process_page(rpage, ret);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003646 *
3647 * When @full is set, the function will not return true unless
3648 * the writer is off the reader page.
3649 *
3650 * Note: it is up to the calling functions to handle sleeps and wakeups.
3651 * The ring buffer can be used anywhere in the kernel and can not
3652 * blindly call wake_up. The layer that uses the ring buffer must be
3653 * responsible for that.
3654 *
3655 * Returns:
Lai Jiangshan667d2412009-02-09 14:21:17 +08003656 * >=0 if data has been transferred, returns the offset of consumed data.
3657 * <0 if no data has been transferred.
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003658 */
3659int ring_buffer_read_page(struct ring_buffer *buffer,
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003660 void **data_page, size_t len, int cpu, int full)
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003661{
3662 struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
3663 struct ring_buffer_event *event;
Steven Rostedt044fa782008-12-02 23:50:03 -05003664 struct buffer_data_page *bpage;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003665 struct buffer_page *reader;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003666 unsigned long flags;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003667 unsigned int commit;
Lai Jiangshan667d2412009-02-09 14:21:17 +08003668 unsigned int read;
Steven Rostedt4f3640f2009-03-03 23:52:42 -05003669 u64 save_timestamp;
Lai Jiangshan667d2412009-02-09 14:21:17 +08003670 int ret = -1;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003671
Steven Rostedt554f7862009-03-11 22:00:13 -04003672 if (!cpumask_test_cpu(cpu, buffer->cpumask))
3673 goto out;
3674
Steven Rostedt474d32b2009-03-03 19:51:40 -05003675 /*
3676 * If len is not big enough to hold the page header, then
3677 * we can not copy anything.
3678 */
3679 if (len <= BUF_PAGE_HDR_SIZE)
Steven Rostedt554f7862009-03-11 22:00:13 -04003680 goto out;
Steven Rostedt474d32b2009-03-03 19:51:40 -05003681
3682 len -= BUF_PAGE_HDR_SIZE;
3683
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003684 if (!data_page)
Steven Rostedt554f7862009-03-11 22:00:13 -04003685 goto out;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003686
Steven Rostedt044fa782008-12-02 23:50:03 -05003687 bpage = *data_page;
3688 if (!bpage)
Steven Rostedt554f7862009-03-11 22:00:13 -04003689 goto out;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003690
3691 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
3692
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003693 reader = rb_get_reader_page(cpu_buffer);
3694 if (!reader)
Steven Rostedt554f7862009-03-11 22:00:13 -04003695 goto out_unlock;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003696
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003697 event = rb_reader_event(cpu_buffer);
Lai Jiangshan667d2412009-02-09 14:21:17 +08003698
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003699 read = reader->read;
3700 commit = rb_page_commit(reader);
3701
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003702 /*
Steven Rostedt474d32b2009-03-03 19:51:40 -05003703 * If this page has been partially read or
3704 * if len is not big enough to read the rest of the page or
3705 * a writer is still on the page, then
3706 * we must copy the data from the page to the buffer.
3707 * Otherwise, we can simply swap the page with the one passed in.
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003708 */
Steven Rostedt474d32b2009-03-03 19:51:40 -05003709 if (read || (len < (commit - read)) ||
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003710 cpu_buffer->reader_page == cpu_buffer->commit_page) {
Lai Jiangshan667d2412009-02-09 14:21:17 +08003711 struct buffer_data_page *rpage = cpu_buffer->reader_page->page;
Steven Rostedt474d32b2009-03-03 19:51:40 -05003712 unsigned int rpos = read;
3713 unsigned int pos = 0;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003714 unsigned int size;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003715
3716 if (full)
Steven Rostedt554f7862009-03-11 22:00:13 -04003717 goto out_unlock;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003718
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003719 if (len > (commit - read))
3720 len = (commit - read);
3721
3722 size = rb_event_length(event);
3723
3724 if (len < size)
Steven Rostedt554f7862009-03-11 22:00:13 -04003725 goto out_unlock;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003726
Steven Rostedt4f3640f2009-03-03 23:52:42 -05003727 /* save the current timestamp, since the user will need it */
3728 save_timestamp = cpu_buffer->read_stamp;
3729
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003730 /* Need to copy one event at a time */
3731 do {
Steven Rostedt474d32b2009-03-03 19:51:40 -05003732 memcpy(bpage->data + pos, rpage->data + rpos, size);
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003733
3734 len -= size;
3735
3736 rb_advance_reader(cpu_buffer);
Steven Rostedt474d32b2009-03-03 19:51:40 -05003737 rpos = reader->read;
3738 pos += size;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003739
3740 event = rb_reader_event(cpu_buffer);
3741 size = rb_event_length(event);
3742 } while (len > size);
Lai Jiangshan667d2412009-02-09 14:21:17 +08003743
3744 /* update bpage */
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003745 local_set(&bpage->commit, pos);
Steven Rostedt4f3640f2009-03-03 23:52:42 -05003746 bpage->time_stamp = save_timestamp;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003747
Steven Rostedt474d32b2009-03-03 19:51:40 -05003748 /* we copied everything to the beginning */
3749 read = 0;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003750 } else {
Steven Rostedtafbab762009-05-01 19:40:05 -04003751 /* update the entry counter */
Steven Rostedt77ae3652009-03-27 11:00:29 -04003752 cpu_buffer->read += rb_page_entries(reader);
Steven Rostedtafbab762009-05-01 19:40:05 -04003753
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003754 /* swap the pages */
Steven Rostedt044fa782008-12-02 23:50:03 -05003755 rb_init_page(bpage);
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003756 bpage = reader->page;
3757 reader->page = *data_page;
3758 local_set(&reader->write, 0);
Steven Rostedt778c55d2009-05-01 18:44:45 -04003759 local_set(&reader->entries, 0);
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003760 reader->read = 0;
Steven Rostedt044fa782008-12-02 23:50:03 -05003761 *data_page = bpage;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003762 }
Lai Jiangshan667d2412009-02-09 14:21:17 +08003763 ret = read;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003764
Steven Rostedt554f7862009-03-11 22:00:13 -04003765 out_unlock:
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003766 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
3767
Steven Rostedt554f7862009-03-11 22:00:13 -04003768 out:
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003769 return ret;
3770}
Steven Rostedtd6ce96d2009-05-05 01:15:24 -04003771EXPORT_SYMBOL_GPL(ring_buffer_read_page);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003772
Paul Mundt1155de42009-06-25 14:30:12 +09003773#ifdef CONFIG_TRACING
Steven Rostedta3583242008-11-11 15:01:42 -05003774static ssize_t
3775rb_simple_read(struct file *filp, char __user *ubuf,
3776 size_t cnt, loff_t *ppos)
3777{
Hannes Eder5e398412009-02-10 19:44:34 +01003778 unsigned long *p = filp->private_data;
Steven Rostedta3583242008-11-11 15:01:42 -05003779 char buf[64];
3780 int r;
3781
Steven Rostedt033601a2008-11-21 12:41:55 -05003782 if (test_bit(RB_BUFFERS_DISABLED_BIT, p))
3783 r = sprintf(buf, "permanently disabled\n");
3784 else
3785 r = sprintf(buf, "%d\n", test_bit(RB_BUFFERS_ON_BIT, p));
Steven Rostedta3583242008-11-11 15:01:42 -05003786
3787 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
3788}
3789
3790static ssize_t
3791rb_simple_write(struct file *filp, const char __user *ubuf,
3792 size_t cnt, loff_t *ppos)
3793{
Hannes Eder5e398412009-02-10 19:44:34 +01003794 unsigned long *p = filp->private_data;
Steven Rostedta3583242008-11-11 15:01:42 -05003795 char buf[64];
Hannes Eder5e398412009-02-10 19:44:34 +01003796 unsigned long val;
Steven Rostedta3583242008-11-11 15:01:42 -05003797 int ret;
3798
3799 if (cnt >= sizeof(buf))
3800 return -EINVAL;
3801
3802 if (copy_from_user(&buf, ubuf, cnt))
3803 return -EFAULT;
3804
3805 buf[cnt] = 0;
3806
3807 ret = strict_strtoul(buf, 10, &val);
3808 if (ret < 0)
3809 return ret;
3810
Steven Rostedt033601a2008-11-21 12:41:55 -05003811 if (val)
3812 set_bit(RB_BUFFERS_ON_BIT, p);
3813 else
3814 clear_bit(RB_BUFFERS_ON_BIT, p);
Steven Rostedta3583242008-11-11 15:01:42 -05003815
3816 (*ppos)++;
3817
3818 return cnt;
3819}
3820
Steven Rostedt5e2336a02009-03-05 21:44:55 -05003821static const struct file_operations rb_simple_fops = {
Steven Rostedta3583242008-11-11 15:01:42 -05003822 .open = tracing_open_generic,
3823 .read = rb_simple_read,
3824 .write = rb_simple_write,
3825};
3826
3827
3828static __init int rb_init_debugfs(void)
3829{
3830 struct dentry *d_tracer;
Steven Rostedta3583242008-11-11 15:01:42 -05003831
3832 d_tracer = tracing_init_dentry();
3833
Frederic Weisbecker5452af62009-03-27 00:25:38 +01003834 trace_create_file("tracing_on", 0644, d_tracer,
3835 &ring_buffer_flags, &rb_simple_fops);
Steven Rostedta3583242008-11-11 15:01:42 -05003836
3837 return 0;
3838}
3839
3840fs_initcall(rb_init_debugfs);
Paul Mundt1155de42009-06-25 14:30:12 +09003841#endif
Steven Rostedt554f7862009-03-11 22:00:13 -04003842
Steven Rostedt59222ef2009-03-12 11:46:03 -04003843#ifdef CONFIG_HOTPLUG_CPU
Frederic Weisbecker09c9e842009-03-21 04:33:36 +01003844static int rb_cpu_notify(struct notifier_block *self,
3845 unsigned long action, void *hcpu)
Steven Rostedt554f7862009-03-11 22:00:13 -04003846{
3847 struct ring_buffer *buffer =
3848 container_of(self, struct ring_buffer, cpu_notify);
3849 long cpu = (long)hcpu;
3850
3851 switch (action) {
3852 case CPU_UP_PREPARE:
3853 case CPU_UP_PREPARE_FROZEN:
Rusty Russell3f237a72009-06-12 21:15:30 +09303854 if (cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt554f7862009-03-11 22:00:13 -04003855 return NOTIFY_OK;
3856
3857 buffer->buffers[cpu] =
3858 rb_allocate_cpu_buffer(buffer, cpu);
3859 if (!buffer->buffers[cpu]) {
3860 WARN(1, "failed to allocate ring buffer on CPU %ld\n",
3861 cpu);
3862 return NOTIFY_OK;
3863 }
3864 smp_wmb();
Rusty Russell3f237a72009-06-12 21:15:30 +09303865 cpumask_set_cpu(cpu, buffer->cpumask);
Steven Rostedt554f7862009-03-11 22:00:13 -04003866 break;
3867 case CPU_DOWN_PREPARE:
3868 case CPU_DOWN_PREPARE_FROZEN:
3869 /*
3870 * Do nothing.
3871 * If we were to free the buffer, then the user would
3872 * lose any trace that was in the buffer.
3873 */
3874 break;
3875 default:
3876 break;
3877 }
3878 return NOTIFY_OK;
3879}
3880#endif