blob: 51633d74a21eaae376771390f0b562455552e21a [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
Ingo Molnard06bbd62008-11-12 10:11:37 +0100204#include "trace.h"
205
Steven Rostedte3d6bf02009-03-03 13:53:07 -0500206#define RB_EVNT_HDR_SIZE (offsetof(struct ring_buffer_event, array))
Andrew Morton67d34722009-01-09 12:27:09 -0800207#define RB_ALIGNMENT 4U
Lai Jiangshan334d4162009-04-24 11:27:05 +0800208#define RB_MAX_SMALL_DATA (RB_ALIGNMENT * RINGBUF_TYPE_DATA_TYPE_LEN_MAX)
Steven Rostedtc7b09302009-06-11 11:12:00 -0400209#define RB_EVNT_MIN_SIZE 8U /* two 32bit words */
Lai Jiangshan334d4162009-04-24 11:27:05 +0800210
211/* define RINGBUF_TYPE_DATA for 'case RINGBUF_TYPE_DATA:' */
212#define RINGBUF_TYPE_DATA 0 ... RINGBUF_TYPE_DATA_TYPE_LEN_MAX
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400213
214enum {
215 RB_LEN_TIME_EXTEND = 8,
216 RB_LEN_TIME_STAMP = 16,
217};
218
Tom Zanussi2d622712009-03-22 03:30:49 -0500219static inline int rb_null_event(struct ring_buffer_event *event)
220{
Lai Jiangshan334d4162009-04-24 11:27:05 +0800221 return event->type_len == RINGBUF_TYPE_PADDING
222 && event->time_delta == 0;
Tom Zanussi2d622712009-03-22 03:30:49 -0500223}
224
225static inline int rb_discarded_event(struct ring_buffer_event *event)
226{
Lai Jiangshan334d4162009-04-24 11:27:05 +0800227 return event->type_len == RINGBUF_TYPE_PADDING && event->time_delta;
Tom Zanussi2d622712009-03-22 03:30:49 -0500228}
229
230static void rb_event_set_padding(struct ring_buffer_event *event)
231{
Lai Jiangshan334d4162009-04-24 11:27:05 +0800232 event->type_len = RINGBUF_TYPE_PADDING;
Tom Zanussi2d622712009-03-22 03:30:49 -0500233 event->time_delta = 0;
234}
235
Tom Zanussi2d622712009-03-22 03:30:49 -0500236static unsigned
237rb_event_data_length(struct ring_buffer_event *event)
238{
239 unsigned length;
240
Lai Jiangshan334d4162009-04-24 11:27:05 +0800241 if (event->type_len)
242 length = event->type_len * RB_ALIGNMENT;
Tom Zanussi2d622712009-03-22 03:30:49 -0500243 else
244 length = event->array[0];
245 return length + RB_EVNT_HDR_SIZE;
246}
247
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400248/* inline for ring buffer fast paths */
Andrew Morton34a148b2009-01-09 12:27:09 -0800249static unsigned
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400250rb_event_length(struct ring_buffer_event *event)
251{
Lai Jiangshan334d4162009-04-24 11:27:05 +0800252 switch (event->type_len) {
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400253 case RINGBUF_TYPE_PADDING:
Tom Zanussi2d622712009-03-22 03:30:49 -0500254 if (rb_null_event(event))
255 /* undefined */
256 return -1;
Lai Jiangshan334d4162009-04-24 11:27:05 +0800257 return event->array[0] + RB_EVNT_HDR_SIZE;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400258
259 case RINGBUF_TYPE_TIME_EXTEND:
260 return RB_LEN_TIME_EXTEND;
261
262 case RINGBUF_TYPE_TIME_STAMP:
263 return RB_LEN_TIME_STAMP;
264
265 case RINGBUF_TYPE_DATA:
Tom Zanussi2d622712009-03-22 03:30:49 -0500266 return rb_event_data_length(event);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400267 default:
268 BUG();
269 }
270 /* not hit */
271 return 0;
272}
273
274/**
275 * ring_buffer_event_length - return the length of the event
276 * @event: the event to get the length of
277 */
278unsigned ring_buffer_event_length(struct ring_buffer_event *event)
279{
Robert Richter465634a2009-01-07 15:32:11 +0100280 unsigned length = rb_event_length(event);
Lai Jiangshan334d4162009-04-24 11:27:05 +0800281 if (event->type_len > RINGBUF_TYPE_DATA_TYPE_LEN_MAX)
Robert Richter465634a2009-01-07 15:32:11 +0100282 return length;
283 length -= RB_EVNT_HDR_SIZE;
284 if (length > RB_MAX_SMALL_DATA + sizeof(event->array[0]))
285 length -= sizeof(event->array[0]);
286 return length;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400287}
Robert Richterc4f50182008-12-11 16:49:22 +0100288EXPORT_SYMBOL_GPL(ring_buffer_event_length);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400289
290/* inline for ring buffer fast paths */
Andrew Morton34a148b2009-01-09 12:27:09 -0800291static void *
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400292rb_event_data(struct ring_buffer_event *event)
293{
Lai Jiangshan334d4162009-04-24 11:27:05 +0800294 BUG_ON(event->type_len > RINGBUF_TYPE_DATA_TYPE_LEN_MAX);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400295 /* If length is in len field, then array[0] has the data */
Lai Jiangshan334d4162009-04-24 11:27:05 +0800296 if (event->type_len)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400297 return (void *)&event->array[0];
298 /* Otherwise length is in array[0] and array[1] has the data */
299 return (void *)&event->array[1];
300}
301
302/**
303 * ring_buffer_event_data - return the data of the event
304 * @event: the event to get the data from
305 */
306void *ring_buffer_event_data(struct ring_buffer_event *event)
307{
308 return rb_event_data(event);
309}
Robert Richterc4f50182008-12-11 16:49:22 +0100310EXPORT_SYMBOL_GPL(ring_buffer_event_data);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400311
312#define for_each_buffer_cpu(buffer, cpu) \
Rusty Russell9e01c1b2009-01-01 10:12:22 +1030313 for_each_cpu(cpu, buffer->cpumask)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400314
315#define TS_SHIFT 27
316#define TS_MASK ((1ULL << TS_SHIFT) - 1)
317#define TS_DELTA_TEST (~TS_MASK)
318
Steven Rostedtabc9b562008-12-02 15:34:06 -0500319struct buffer_data_page {
Steven Rostedte4c2ce82008-10-01 11:14:54 -0400320 u64 time_stamp; /* page time stamp */
Wenji Huangc3706f02009-02-10 01:03:18 -0500321 local_t commit; /* write committed index */
Steven Rostedtabc9b562008-12-02 15:34:06 -0500322 unsigned char data[]; /* data of buffer page */
323};
324
Steven Rostedt77ae3652009-03-27 11:00:29 -0400325/*
326 * Note, the buffer_page list must be first. The buffer pages
327 * are allocated in cache lines, which means that each buffer
328 * page will be at the beginning of a cache line, and thus
329 * the least significant bits will be zero. We use this to
330 * add flags in the list struct pointers, to make the ring buffer
331 * lockless.
332 */
Steven Rostedtabc9b562008-12-02 15:34:06 -0500333struct buffer_page {
Steven Rostedt778c55d2009-05-01 18:44:45 -0400334 struct list_head list; /* list of buffer pages */
Steven Rostedtabc9b562008-12-02 15:34:06 -0500335 local_t write; /* index for next write */
Steven Rostedt6f807ac2008-10-04 02:00:58 -0400336 unsigned read; /* index for next read */
Steven Rostedt778c55d2009-05-01 18:44:45 -0400337 local_t entries; /* entries on this page */
Steven Rostedtabc9b562008-12-02 15:34:06 -0500338 struct buffer_data_page *page; /* Actual data page */
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400339};
340
Steven Rostedt77ae3652009-03-27 11:00:29 -0400341/*
342 * The buffer page counters, write and entries, must be reset
343 * atomically when crossing page boundaries. To synchronize this
344 * update, two counters are inserted into the number. One is
345 * the actual counter for the write position or count on the page.
346 *
347 * The other is a counter of updaters. Before an update happens
348 * the update partition of the counter is incremented. This will
349 * allow the updater to update the counter atomically.
350 *
351 * The counter is 20 bits, and the state data is 12.
352 */
353#define RB_WRITE_MASK 0xfffff
354#define RB_WRITE_INTCNT (1 << 20)
355
Steven Rostedt044fa782008-12-02 23:50:03 -0500356static void rb_init_page(struct buffer_data_page *bpage)
Steven Rostedtabc9b562008-12-02 15:34:06 -0500357{
Steven Rostedt044fa782008-12-02 23:50:03 -0500358 local_set(&bpage->commit, 0);
Steven Rostedtabc9b562008-12-02 15:34:06 -0500359}
360
Steven Rostedt474d32b2009-03-03 19:51:40 -0500361/**
362 * ring_buffer_page_len - the size of data on the page.
363 * @page: The page to read
364 *
365 * Returns the amount of data on the page, including buffer page header.
366 */
Steven Rostedtef7a4a12009-03-03 00:27:49 -0500367size_t ring_buffer_page_len(void *page)
368{
Steven Rostedt474d32b2009-03-03 19:51:40 -0500369 return local_read(&((struct buffer_data_page *)page)->commit)
370 + BUF_PAGE_HDR_SIZE;
Steven Rostedtef7a4a12009-03-03 00:27:49 -0500371}
372
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400373/*
Steven Rostedted568292008-09-29 23:02:40 -0400374 * Also stolen from mm/slob.c. Thanks to Mathieu Desnoyers for pointing
375 * this issue out.
376 */
Andrew Morton34a148b2009-01-09 12:27:09 -0800377static void free_buffer_page(struct buffer_page *bpage)
Steven Rostedted568292008-09-29 23:02:40 -0400378{
Andrew Morton34a148b2009-01-09 12:27:09 -0800379 free_page((unsigned long)bpage->page);
Steven Rostedte4c2ce82008-10-01 11:14:54 -0400380 kfree(bpage);
Steven Rostedted568292008-09-29 23:02:40 -0400381}
382
383/*
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400384 * We need to fit the time_stamp delta into 27 bits.
385 */
386static inline int test_time_stamp(u64 delta)
387{
388 if (delta & TS_DELTA_TEST)
389 return 1;
390 return 0;
391}
392
Steven Rostedt474d32b2009-03-03 19:51:40 -0500393#define BUF_PAGE_SIZE (PAGE_SIZE - BUF_PAGE_HDR_SIZE)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400394
Steven Rostedtbe957c42009-05-11 14:42:53 -0400395/* Max payload is BUF_PAGE_SIZE - header (8bytes) */
396#define BUF_MAX_DATA_SIZE (BUF_PAGE_SIZE - (sizeof(u32) * 2))
397
Steven Rostedtea05b572009-06-03 09:30:10 -0400398/* Max number of timestamps that can fit on a page */
399#define RB_TIMESTAMPS_PER_PAGE (BUF_PAGE_SIZE / RB_LEN_TIME_STAMP)
400
Steven Rostedtd1b182a2009-04-15 16:53:47 -0400401int ring_buffer_print_page_header(struct trace_seq *s)
402{
403 struct buffer_data_page field;
404 int ret;
405
406 ret = trace_seq_printf(s, "\tfield: u64 timestamp;\t"
407 "offset:0;\tsize:%u;\n",
408 (unsigned int)sizeof(field.time_stamp));
409
410 ret = trace_seq_printf(s, "\tfield: local_t commit;\t"
411 "offset:%u;\tsize:%u;\n",
412 (unsigned int)offsetof(typeof(field), commit),
413 (unsigned int)sizeof(field.commit));
414
415 ret = trace_seq_printf(s, "\tfield: char data;\t"
416 "offset:%u;\tsize:%u;\n",
417 (unsigned int)offsetof(typeof(field), data),
418 (unsigned int)BUF_PAGE_SIZE);
419
420 return ret;
421}
422
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400423/*
424 * head_page == tail_page && head == tail then buffer is empty.
425 */
426struct ring_buffer_per_cpu {
427 int cpu;
428 struct ring_buffer *buffer;
Steven Rostedt77ae3652009-03-27 11:00:29 -0400429 spinlock_t reader_lock; /* serialize readers */
Steven Rostedt3e03fb72008-11-06 00:09:43 -0500430 raw_spinlock_t lock;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400431 struct lock_class_key lock_key;
Steven Rostedt3adc54f2009-03-30 15:32:01 -0400432 struct list_head *pages;
Steven Rostedt6f807ac2008-10-04 02:00:58 -0400433 struct buffer_page *head_page; /* read from head */
434 struct buffer_page *tail_page; /* write to tail */
Wenji Huangc3706f02009-02-10 01:03:18 -0500435 struct buffer_page *commit_page; /* committed pages */
Steven Rostedtd7690412008-10-01 00:29:53 -0400436 struct buffer_page *reader_page;
Steven Rostedt77ae3652009-03-27 11:00:29 -0400437 local_t commit_overrun;
438 local_t overrun;
Steven Rostedte4906ef2009-04-30 20:49:44 -0400439 local_t entries;
Steven Rostedtfa743952009-06-16 12:37:57 -0400440 local_t committing;
441 local_t commits;
Steven Rostedt77ae3652009-03-27 11:00:29 -0400442 unsigned long read;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400443 u64 write_stamp;
444 u64 read_stamp;
445 atomic_t record_disabled;
446};
447
448struct ring_buffer {
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400449 unsigned pages;
450 unsigned flags;
451 int cpus;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400452 atomic_t record_disabled;
Arnaldo Carvalho de Melo00f62f62009-02-09 17:04:06 -0200453 cpumask_var_t cpumask;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400454
Peter Zijlstra1f8a6a12009-06-08 18:18:39 +0200455 struct lock_class_key *reader_lock_key;
456
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400457 struct mutex mutex;
458
459 struct ring_buffer_per_cpu **buffers;
Steven Rostedt554f7862009-03-11 22:00:13 -0400460
Steven Rostedt59222ef2009-03-12 11:46:03 -0400461#ifdef CONFIG_HOTPLUG_CPU
Steven Rostedt554f7862009-03-11 22:00:13 -0400462 struct notifier_block cpu_notify;
463#endif
Steven Rostedt37886f62009-03-17 17:22:06 -0400464 u64 (*clock)(void);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400465};
466
467struct ring_buffer_iter {
468 struct ring_buffer_per_cpu *cpu_buffer;
469 unsigned long head;
470 struct buffer_page *head_page;
471 u64 read_stamp;
472};
473
Steven Rostedtf536aaf2008-11-10 23:07:30 -0500474/* buffer may be either ring_buffer or ring_buffer_per_cpu */
Steven Rostedtbf41a152008-10-04 02:00:59 -0400475#define RB_WARN_ON(buffer, cond) \
Steven Rostedt3e89c7b2008-11-11 15:28:41 -0500476 ({ \
477 int _____ret = unlikely(cond); \
478 if (_____ret) { \
Steven Rostedtbf41a152008-10-04 02:00:59 -0400479 atomic_inc(&buffer->record_disabled); \
480 WARN_ON(1); \
481 } \
Steven Rostedt3e89c7b2008-11-11 15:28:41 -0500482 _____ret; \
483 })
Steven Rostedtf536aaf2008-11-10 23:07:30 -0500484
Steven Rostedt37886f62009-03-17 17:22:06 -0400485/* Up this if you want to test the TIME_EXTENTS and normalization */
486#define DEBUG_SHIFT 0
487
Steven Rostedt88eb0122009-05-11 16:28:23 -0400488static inline u64 rb_time_stamp(struct ring_buffer *buffer, int cpu)
489{
490 /* shift to debug/test normalization and TIME_EXTENTS */
491 return buffer->clock() << DEBUG_SHIFT;
492}
493
Steven Rostedt37886f62009-03-17 17:22:06 -0400494u64 ring_buffer_time_stamp(struct ring_buffer *buffer, int cpu)
495{
496 u64 time;
497
498 preempt_disable_notrace();
Steven Rostedt88eb0122009-05-11 16:28:23 -0400499 time = rb_time_stamp(buffer, cpu);
Steven Rostedt37886f62009-03-17 17:22:06 -0400500 preempt_enable_no_resched_notrace();
501
502 return time;
503}
504EXPORT_SYMBOL_GPL(ring_buffer_time_stamp);
505
506void ring_buffer_normalize_time_stamp(struct ring_buffer *buffer,
507 int cpu, u64 *ts)
508{
509 /* Just stupid testing the normalize function and deltas */
510 *ts >>= DEBUG_SHIFT;
511}
512EXPORT_SYMBOL_GPL(ring_buffer_normalize_time_stamp);
513
Steven Rostedt77ae3652009-03-27 11:00:29 -0400514/*
515 * Making the ring buffer lockless makes things tricky.
516 * Although writes only happen on the CPU that they are on,
517 * and they only need to worry about interrupts. Reads can
518 * happen on any CPU.
519 *
520 * The reader page is always off the ring buffer, but when the
521 * reader finishes with a page, it needs to swap its page with
522 * a new one from the buffer. The reader needs to take from
523 * the head (writes go to the tail). But if a writer is in overwrite
524 * mode and wraps, it must push the head page forward.
525 *
526 * Here lies the problem.
527 *
528 * The reader must be careful to replace only the head page, and
529 * not another one. As described at the top of the file in the
530 * ASCII art, the reader sets its old page to point to the next
531 * page after head. It then sets the page after head to point to
532 * the old reader page. But if the writer moves the head page
533 * during this operation, the reader could end up with the tail.
534 *
535 * We use cmpxchg to help prevent this race. We also do something
536 * special with the page before head. We set the LSB to 1.
537 *
538 * When the writer must push the page forward, it will clear the
539 * bit that points to the head page, move the head, and then set
540 * the bit that points to the new head page.
541 *
542 * We also don't want an interrupt coming in and moving the head
543 * page on another writer. Thus we use the second LSB to catch
544 * that too. Thus:
545 *
546 * head->list->prev->next bit 1 bit 0
547 * ------- -------
548 * Normal page 0 0
549 * Points to head page 0 1
550 * New head page 1 0
551 *
552 * Note we can not trust the prev pointer of the head page, because:
553 *
554 * +----+ +-----+ +-----+
555 * | |------>| T |---X--->| N |
556 * | |<------| | | |
557 * +----+ +-----+ +-----+
558 * ^ ^ |
559 * | +-----+ | |
560 * +----------| R |----------+ |
561 * | |<-----------+
562 * +-----+
563 *
564 * Key: ---X--> HEAD flag set in pointer
565 * T Tail page
566 * R Reader page
567 * N Next page
568 *
569 * (see __rb_reserve_next() to see where this happens)
570 *
571 * What the above shows is that the reader just swapped out
572 * the reader page with a page in the buffer, but before it
573 * could make the new header point back to the new page added
574 * it was preempted by a writer. The writer moved forward onto
575 * the new page added by the reader and is about to move forward
576 * again.
577 *
578 * You can see, it is legitimate for the previous pointer of
579 * the head (or any page) not to point back to itself. But only
580 * temporarially.
581 */
582
583#define RB_PAGE_NORMAL 0UL
584#define RB_PAGE_HEAD 1UL
585#define RB_PAGE_UPDATE 2UL
586
587
588#define RB_FLAG_MASK 3UL
589
590/* PAGE_MOVED is not part of the mask */
591#define RB_PAGE_MOVED 4UL
592
593/*
594 * rb_list_head - remove any bit
595 */
596static struct list_head *rb_list_head(struct list_head *list)
597{
598 unsigned long val = (unsigned long)list;
599
600 return (struct list_head *)(val & ~RB_FLAG_MASK);
601}
602
603/*
604 * rb_is_head_page - test if the give page is the head page
605 *
606 * Because the reader may move the head_page pointer, we can
607 * not trust what the head page is (it may be pointing to
608 * the reader page). But if the next page is a header page,
609 * its flags will be non zero.
610 */
611static int inline
612rb_is_head_page(struct ring_buffer_per_cpu *cpu_buffer,
613 struct buffer_page *page, struct list_head *list)
614{
615 unsigned long val;
616
617 val = (unsigned long)list->next;
618
619 if ((val & ~RB_FLAG_MASK) != (unsigned long)&page->list)
620 return RB_PAGE_MOVED;
621
622 return val & RB_FLAG_MASK;
623}
624
625/*
626 * rb_is_reader_page
627 *
628 * The unique thing about the reader page, is that, if the
629 * writer is ever on it, the previous pointer never points
630 * back to the reader page.
631 */
632static int rb_is_reader_page(struct buffer_page *page)
633{
634 struct list_head *list = page->list.prev;
635
636 return rb_list_head(list->next) != &page->list;
637}
638
639/*
640 * rb_set_list_to_head - set a list_head to be pointing to head.
641 */
642static void rb_set_list_to_head(struct ring_buffer_per_cpu *cpu_buffer,
643 struct list_head *list)
644{
645 unsigned long *ptr;
646
647 ptr = (unsigned long *)&list->next;
648 *ptr |= RB_PAGE_HEAD;
649 *ptr &= ~RB_PAGE_UPDATE;
650}
651
652/*
653 * rb_head_page_activate - sets up head page
654 */
655static void rb_head_page_activate(struct ring_buffer_per_cpu *cpu_buffer)
656{
657 struct buffer_page *head;
658
659 head = cpu_buffer->head_page;
660 if (!head)
661 return;
662
663 /*
664 * Set the previous list pointer to have the HEAD flag.
665 */
666 rb_set_list_to_head(cpu_buffer, head->list.prev);
667}
668
669static void rb_list_head_clear(struct list_head *list)
670{
671 unsigned long *ptr = (unsigned long *)&list->next;
672
673 *ptr &= ~RB_FLAG_MASK;
674}
675
676/*
677 * rb_head_page_dactivate - clears head page ptr (for free list)
678 */
679static void
680rb_head_page_deactivate(struct ring_buffer_per_cpu *cpu_buffer)
681{
682 struct list_head *hd;
683
684 /* Go through the whole list and clear any pointers found. */
685 rb_list_head_clear(cpu_buffer->pages);
686
687 list_for_each(hd, cpu_buffer->pages)
688 rb_list_head_clear(hd);
689}
690
691static int rb_head_page_set(struct ring_buffer_per_cpu *cpu_buffer,
692 struct buffer_page *head,
693 struct buffer_page *prev,
694 int old_flag, int new_flag)
695{
696 struct list_head *list;
697 unsigned long val = (unsigned long)&head->list;
698 unsigned long ret;
699
700 list = &prev->list;
701
702 val &= ~RB_FLAG_MASK;
703
704 ret = (unsigned long)cmpxchg(&list->next,
705 val | old_flag, val | new_flag);
706
707 /* check if the reader took the page */
708 if ((ret & ~RB_FLAG_MASK) != val)
709 return RB_PAGE_MOVED;
710
711 return ret & RB_FLAG_MASK;
712}
713
714static int rb_head_page_set_update(struct ring_buffer_per_cpu *cpu_buffer,
715 struct buffer_page *head,
716 struct buffer_page *prev,
717 int old_flag)
718{
719 return rb_head_page_set(cpu_buffer, head, prev,
720 old_flag, RB_PAGE_UPDATE);
721}
722
723static int rb_head_page_set_head(struct ring_buffer_per_cpu *cpu_buffer,
724 struct buffer_page *head,
725 struct buffer_page *prev,
726 int old_flag)
727{
728 return rb_head_page_set(cpu_buffer, head, prev,
729 old_flag, RB_PAGE_HEAD);
730}
731
732static int rb_head_page_set_normal(struct ring_buffer_per_cpu *cpu_buffer,
733 struct buffer_page *head,
734 struct buffer_page *prev,
735 int old_flag)
736{
737 return rb_head_page_set(cpu_buffer, head, prev,
738 old_flag, RB_PAGE_NORMAL);
739}
740
741static inline void rb_inc_page(struct ring_buffer_per_cpu *cpu_buffer,
742 struct buffer_page **bpage)
743{
744 struct list_head *p = rb_list_head((*bpage)->list.next);
745
746 *bpage = list_entry(p, struct buffer_page, list);
747}
748
749static struct buffer_page *
750rb_set_head_page(struct ring_buffer_per_cpu *cpu_buffer)
751{
752 struct buffer_page *head;
753 struct buffer_page *page;
754 struct list_head *list;
755 int i;
756
757 if (RB_WARN_ON(cpu_buffer, !cpu_buffer->head_page))
758 return NULL;
759
760 /* sanity check */
761 list = cpu_buffer->pages;
762 if (RB_WARN_ON(cpu_buffer, rb_list_head(list->prev->next) != list))
763 return NULL;
764
765 page = head = cpu_buffer->head_page;
766 /*
767 * It is possible that the writer moves the header behind
768 * where we started, and we miss in one loop.
769 * A second loop should grab the header, but we'll do
770 * three loops just because I'm paranoid.
771 */
772 for (i = 0; i < 3; i++) {
773 do {
774 if (rb_is_head_page(cpu_buffer, page, page->list.prev)) {
775 cpu_buffer->head_page = page;
776 return page;
777 }
778 rb_inc_page(cpu_buffer, &page);
779 } while (page != head);
780 }
781
782 RB_WARN_ON(cpu_buffer, 1);
783
784 return NULL;
785}
786
787static int rb_head_page_replace(struct buffer_page *old,
788 struct buffer_page *new)
789{
790 unsigned long *ptr = (unsigned long *)&old->list.prev->next;
791 unsigned long val;
792 unsigned long ret;
793
794 val = *ptr & ~RB_FLAG_MASK;
795 val |= RB_PAGE_HEAD;
796
797 ret = cmpxchg(ptr, val, &new->list);
798
799 return ret == val;
800}
801
802/*
803 * rb_tail_page_update - move the tail page forward
804 *
805 * Returns 1 if moved tail page, 0 if someone else did.
806 */
807static int rb_tail_page_update(struct ring_buffer_per_cpu *cpu_buffer,
808 struct buffer_page *tail_page,
809 struct buffer_page *next_page)
810{
811 struct buffer_page *old_tail;
812 unsigned long old_entries;
813 unsigned long old_write;
814 int ret = 0;
815
816 /*
817 * The tail page now needs to be moved forward.
818 *
819 * We need to reset the tail page, but without messing
820 * with possible erasing of data brought in by interrupts
821 * that have moved the tail page and are currently on it.
822 *
823 * We add a counter to the write field to denote this.
824 */
825 old_write = local_add_return(RB_WRITE_INTCNT, &next_page->write);
826 old_entries = local_add_return(RB_WRITE_INTCNT, &next_page->entries);
827
828 /*
829 * Just make sure we have seen our old_write and synchronize
830 * with any interrupts that come in.
831 */
832 barrier();
833
834 /*
835 * If the tail page is still the same as what we think
836 * it is, then it is up to us to update the tail
837 * pointer.
838 */
839 if (tail_page == cpu_buffer->tail_page) {
840 /* Zero the write counter */
841 unsigned long val = old_write & ~RB_WRITE_MASK;
842 unsigned long eval = old_entries & ~RB_WRITE_MASK;
843
844 /*
845 * This will only succeed if an interrupt did
846 * not come in and change it. In which case, we
847 * do not want to modify it.
Lai Jiangshanda706d82009-07-15 16:27:30 +0800848 *
849 * We add (void) to let the compiler know that we do not care
850 * about the return value of these functions. We use the
851 * cmpxchg to only update if an interrupt did not already
852 * do it for us. If the cmpxchg fails, we don't care.
Steven Rostedt77ae3652009-03-27 11:00:29 -0400853 */
Lai Jiangshanda706d82009-07-15 16:27:30 +0800854 (void)local_cmpxchg(&next_page->write, old_write, val);
855 (void)local_cmpxchg(&next_page->entries, old_entries, eval);
Steven Rostedt77ae3652009-03-27 11:00:29 -0400856
857 /*
858 * No need to worry about races with clearing out the commit.
859 * it only can increment when a commit takes place. But that
860 * only happens in the outer most nested commit.
861 */
862 local_set(&next_page->page->commit, 0);
863
864 old_tail = cmpxchg(&cpu_buffer->tail_page,
865 tail_page, next_page);
866
867 if (old_tail == tail_page)
868 ret = 1;
869 }
870
871 return ret;
872}
873
874static int rb_check_bpage(struct ring_buffer_per_cpu *cpu_buffer,
875 struct buffer_page *bpage)
876{
877 unsigned long val = (unsigned long)bpage;
878
879 if (RB_WARN_ON(cpu_buffer, val & RB_FLAG_MASK))
880 return 1;
881
882 return 0;
883}
884
885/**
886 * rb_check_list - make sure a pointer to a list has the last bits zero
887 */
888static int rb_check_list(struct ring_buffer_per_cpu *cpu_buffer,
889 struct list_head *list)
890{
891 if (RB_WARN_ON(cpu_buffer, rb_list_head(list->prev) != list->prev))
892 return 1;
893 if (RB_WARN_ON(cpu_buffer, rb_list_head(list->next) != list->next))
894 return 1;
895 return 0;
896}
897
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400898/**
899 * check_pages - integrity check of buffer pages
900 * @cpu_buffer: CPU buffer with pages to test
901 *
Wenji Huangc3706f02009-02-10 01:03:18 -0500902 * As a safety measure we check to make sure the data pages have not
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400903 * been corrupted.
904 */
905static int rb_check_pages(struct ring_buffer_per_cpu *cpu_buffer)
906{
Steven Rostedt3adc54f2009-03-30 15:32:01 -0400907 struct list_head *head = cpu_buffer->pages;
Steven Rostedt044fa782008-12-02 23:50:03 -0500908 struct buffer_page *bpage, *tmp;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400909
Steven Rostedt77ae3652009-03-27 11:00:29 -0400910 rb_head_page_deactivate(cpu_buffer);
911
Steven Rostedt3e89c7b2008-11-11 15:28:41 -0500912 if (RB_WARN_ON(cpu_buffer, head->next->prev != head))
913 return -1;
914 if (RB_WARN_ON(cpu_buffer, head->prev->next != head))
915 return -1;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400916
Steven Rostedt77ae3652009-03-27 11:00:29 -0400917 if (rb_check_list(cpu_buffer, head))
918 return -1;
919
Steven Rostedt044fa782008-12-02 23:50:03 -0500920 list_for_each_entry_safe(bpage, tmp, head, list) {
Steven Rostedt3e89c7b2008-11-11 15:28:41 -0500921 if (RB_WARN_ON(cpu_buffer,
Steven Rostedt044fa782008-12-02 23:50:03 -0500922 bpage->list.next->prev != &bpage->list))
Steven Rostedt3e89c7b2008-11-11 15:28:41 -0500923 return -1;
924 if (RB_WARN_ON(cpu_buffer,
Steven Rostedt044fa782008-12-02 23:50:03 -0500925 bpage->list.prev->next != &bpage->list))
Steven Rostedt3e89c7b2008-11-11 15:28:41 -0500926 return -1;
Steven Rostedt77ae3652009-03-27 11:00:29 -0400927 if (rb_check_list(cpu_buffer, &bpage->list))
928 return -1;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400929 }
930
Steven Rostedt77ae3652009-03-27 11:00:29 -0400931 rb_head_page_activate(cpu_buffer);
932
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400933 return 0;
934}
935
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400936static int rb_allocate_pages(struct ring_buffer_per_cpu *cpu_buffer,
937 unsigned nr_pages)
938{
Steven Rostedt044fa782008-12-02 23:50:03 -0500939 struct buffer_page *bpage, *tmp;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400940 unsigned long addr;
941 LIST_HEAD(pages);
942 unsigned i;
943
Steven Rostedt3adc54f2009-03-30 15:32:01 -0400944 WARN_ON(!nr_pages);
945
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400946 for (i = 0; i < nr_pages; i++) {
Steven Rostedt044fa782008-12-02 23:50:03 -0500947 bpage = kzalloc_node(ALIGN(sizeof(*bpage), cache_line_size()),
Steven Rostedtaa1e0e3b2008-10-02 19:18:09 -0400948 GFP_KERNEL, cpu_to_node(cpu_buffer->cpu));
Steven Rostedt044fa782008-12-02 23:50:03 -0500949 if (!bpage)
Steven Rostedte4c2ce82008-10-01 11:14:54 -0400950 goto free_pages;
Steven Rostedt77ae3652009-03-27 11:00:29 -0400951
952 rb_check_bpage(cpu_buffer, bpage);
953
Steven Rostedt044fa782008-12-02 23:50:03 -0500954 list_add(&bpage->list, &pages);
Steven Rostedte4c2ce82008-10-01 11:14:54 -0400955
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400956 addr = __get_free_page(GFP_KERNEL);
957 if (!addr)
958 goto free_pages;
Steven Rostedt044fa782008-12-02 23:50:03 -0500959 bpage->page = (void *)addr;
960 rb_init_page(bpage->page);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400961 }
962
Steven Rostedt3adc54f2009-03-30 15:32:01 -0400963 /*
964 * The ring buffer page list is a circular list that does not
965 * start and end with a list head. All page list items point to
966 * other pages.
967 */
968 cpu_buffer->pages = pages.next;
969 list_del(&pages);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400970
971 rb_check_pages(cpu_buffer);
972
973 return 0;
974
975 free_pages:
Steven Rostedt044fa782008-12-02 23:50:03 -0500976 list_for_each_entry_safe(bpage, tmp, &pages, list) {
977 list_del_init(&bpage->list);
978 free_buffer_page(bpage);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400979 }
980 return -ENOMEM;
981}
982
983static struct ring_buffer_per_cpu *
984rb_allocate_cpu_buffer(struct ring_buffer *buffer, int cpu)
985{
986 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedt044fa782008-12-02 23:50:03 -0500987 struct buffer_page *bpage;
Steven Rostedtd7690412008-10-01 00:29:53 -0400988 unsigned long addr;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400989 int ret;
990
991 cpu_buffer = kzalloc_node(ALIGN(sizeof(*cpu_buffer), cache_line_size()),
992 GFP_KERNEL, cpu_to_node(cpu));
993 if (!cpu_buffer)
994 return NULL;
995
996 cpu_buffer->cpu = cpu;
997 cpu_buffer->buffer = buffer;
Steven Rostedtf83c9d02008-11-11 18:47:44 +0100998 spin_lock_init(&cpu_buffer->reader_lock);
Peter Zijlstra1f8a6a12009-06-08 18:18:39 +0200999 lockdep_set_class(&cpu_buffer->reader_lock, buffer->reader_lock_key);
Steven Rostedt3e03fb72008-11-06 00:09:43 -05001000 cpu_buffer->lock = (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001001
Steven Rostedt044fa782008-12-02 23:50:03 -05001002 bpage = kzalloc_node(ALIGN(sizeof(*bpage), cache_line_size()),
Steven Rostedte4c2ce82008-10-01 11:14:54 -04001003 GFP_KERNEL, cpu_to_node(cpu));
Steven Rostedt044fa782008-12-02 23:50:03 -05001004 if (!bpage)
Steven Rostedte4c2ce82008-10-01 11:14:54 -04001005 goto fail_free_buffer;
1006
Steven Rostedt77ae3652009-03-27 11:00:29 -04001007 rb_check_bpage(cpu_buffer, bpage);
1008
Steven Rostedt044fa782008-12-02 23:50:03 -05001009 cpu_buffer->reader_page = bpage;
Steven Rostedtd7690412008-10-01 00:29:53 -04001010 addr = __get_free_page(GFP_KERNEL);
1011 if (!addr)
Steven Rostedte4c2ce82008-10-01 11:14:54 -04001012 goto fail_free_reader;
Steven Rostedt044fa782008-12-02 23:50:03 -05001013 bpage->page = (void *)addr;
1014 rb_init_page(bpage->page);
Steven Rostedte4c2ce82008-10-01 11:14:54 -04001015
Steven Rostedtd7690412008-10-01 00:29:53 -04001016 INIT_LIST_HEAD(&cpu_buffer->reader_page->list);
Steven Rostedtd7690412008-10-01 00:29:53 -04001017
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001018 ret = rb_allocate_pages(cpu_buffer, buffer->pages);
1019 if (ret < 0)
Steven Rostedtd7690412008-10-01 00:29:53 -04001020 goto fail_free_reader;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001021
1022 cpu_buffer->head_page
Steven Rostedt3adc54f2009-03-30 15:32:01 -04001023 = list_entry(cpu_buffer->pages, struct buffer_page, list);
Steven Rostedtbf41a152008-10-04 02:00:59 -04001024 cpu_buffer->tail_page = cpu_buffer->commit_page = cpu_buffer->head_page;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001025
Steven Rostedt77ae3652009-03-27 11:00:29 -04001026 rb_head_page_activate(cpu_buffer);
1027
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001028 return cpu_buffer;
1029
Steven Rostedtd7690412008-10-01 00:29:53 -04001030 fail_free_reader:
1031 free_buffer_page(cpu_buffer->reader_page);
1032
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001033 fail_free_buffer:
1034 kfree(cpu_buffer);
1035 return NULL;
1036}
1037
1038static void rb_free_cpu_buffer(struct ring_buffer_per_cpu *cpu_buffer)
1039{
Steven Rostedt3adc54f2009-03-30 15:32:01 -04001040 struct list_head *head = cpu_buffer->pages;
Steven Rostedt044fa782008-12-02 23:50:03 -05001041 struct buffer_page *bpage, *tmp;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001042
Steven Rostedtd7690412008-10-01 00:29:53 -04001043 free_buffer_page(cpu_buffer->reader_page);
1044
Steven Rostedt77ae3652009-03-27 11:00:29 -04001045 rb_head_page_deactivate(cpu_buffer);
1046
Steven Rostedt3adc54f2009-03-30 15:32:01 -04001047 if (head) {
1048 list_for_each_entry_safe(bpage, tmp, head, list) {
1049 list_del_init(&bpage->list);
1050 free_buffer_page(bpage);
1051 }
1052 bpage = list_entry(head, struct buffer_page, list);
Steven Rostedt044fa782008-12-02 23:50:03 -05001053 free_buffer_page(bpage);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001054 }
Steven Rostedt3adc54f2009-03-30 15:32:01 -04001055
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001056 kfree(cpu_buffer);
1057}
1058
Steven Rostedt59222ef2009-03-12 11:46:03 -04001059#ifdef CONFIG_HOTPLUG_CPU
Frederic Weisbecker09c9e842009-03-21 04:33:36 +01001060static int rb_cpu_notify(struct notifier_block *self,
1061 unsigned long action, void *hcpu);
Steven Rostedt554f7862009-03-11 22:00:13 -04001062#endif
1063
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001064/**
1065 * ring_buffer_alloc - allocate a new ring_buffer
Robert Richter68814b52008-11-24 12:24:12 +01001066 * @size: the size in bytes per cpu that is needed.
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001067 * @flags: attributes to set for the ring buffer.
1068 *
1069 * Currently the only flag that is available is the RB_FL_OVERWRITE
1070 * flag. This flag means that the buffer will overwrite old data
1071 * when the buffer wraps. If this flag is not set, the buffer will
1072 * drop data when the tail hits the head.
1073 */
Peter Zijlstra1f8a6a12009-06-08 18:18:39 +02001074struct ring_buffer *__ring_buffer_alloc(unsigned long size, unsigned flags,
1075 struct lock_class_key *key)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001076{
1077 struct ring_buffer *buffer;
1078 int bsize;
1079 int cpu;
1080
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001081 /* keep it in its own cache line */
1082 buffer = kzalloc(ALIGN(sizeof(*buffer), cache_line_size()),
1083 GFP_KERNEL);
1084 if (!buffer)
1085 return NULL;
1086
Rusty Russell9e01c1b2009-01-01 10:12:22 +10301087 if (!alloc_cpumask_var(&buffer->cpumask, GFP_KERNEL))
1088 goto fail_free_buffer;
1089
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001090 buffer->pages = DIV_ROUND_UP(size, BUF_PAGE_SIZE);
1091 buffer->flags = flags;
Steven Rostedt37886f62009-03-17 17:22:06 -04001092 buffer->clock = trace_clock_local;
Peter Zijlstra1f8a6a12009-06-08 18:18:39 +02001093 buffer->reader_lock_key = key;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001094
1095 /* need at least two pages */
Steven Rostedt5f78abe2009-06-17 14:11:10 -04001096 if (buffer->pages < 2)
1097 buffer->pages = 2;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001098
Frederic Weisbecker3bf832c2009-03-19 14:47:33 +01001099 /*
1100 * In case of non-hotplug cpu, if the ring-buffer is allocated
1101 * in early initcall, it will not be notified of secondary cpus.
1102 * In that off case, we need to allocate for all possible cpus.
1103 */
1104#ifdef CONFIG_HOTPLUG_CPU
Steven Rostedt554f7862009-03-11 22:00:13 -04001105 get_online_cpus();
1106 cpumask_copy(buffer->cpumask, cpu_online_mask);
Frederic Weisbecker3bf832c2009-03-19 14:47:33 +01001107#else
1108 cpumask_copy(buffer->cpumask, cpu_possible_mask);
1109#endif
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001110 buffer->cpus = nr_cpu_ids;
1111
1112 bsize = sizeof(void *) * nr_cpu_ids;
1113 buffer->buffers = kzalloc(ALIGN(bsize, cache_line_size()),
1114 GFP_KERNEL);
1115 if (!buffer->buffers)
Rusty Russell9e01c1b2009-01-01 10:12:22 +10301116 goto fail_free_cpumask;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001117
1118 for_each_buffer_cpu(buffer, cpu) {
1119 buffer->buffers[cpu] =
1120 rb_allocate_cpu_buffer(buffer, cpu);
1121 if (!buffer->buffers[cpu])
1122 goto fail_free_buffers;
1123 }
1124
Steven Rostedt59222ef2009-03-12 11:46:03 -04001125#ifdef CONFIG_HOTPLUG_CPU
Steven Rostedt554f7862009-03-11 22:00:13 -04001126 buffer->cpu_notify.notifier_call = rb_cpu_notify;
1127 buffer->cpu_notify.priority = 0;
1128 register_cpu_notifier(&buffer->cpu_notify);
1129#endif
1130
1131 put_online_cpus();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001132 mutex_init(&buffer->mutex);
1133
1134 return buffer;
1135
1136 fail_free_buffers:
1137 for_each_buffer_cpu(buffer, cpu) {
1138 if (buffer->buffers[cpu])
1139 rb_free_cpu_buffer(buffer->buffers[cpu]);
1140 }
1141 kfree(buffer->buffers);
1142
Rusty Russell9e01c1b2009-01-01 10:12:22 +10301143 fail_free_cpumask:
1144 free_cpumask_var(buffer->cpumask);
Steven Rostedt554f7862009-03-11 22:00:13 -04001145 put_online_cpus();
Rusty Russell9e01c1b2009-01-01 10:12:22 +10301146
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001147 fail_free_buffer:
1148 kfree(buffer);
1149 return NULL;
1150}
Peter Zijlstra1f8a6a12009-06-08 18:18:39 +02001151EXPORT_SYMBOL_GPL(__ring_buffer_alloc);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001152
1153/**
1154 * ring_buffer_free - free a ring buffer.
1155 * @buffer: the buffer to free.
1156 */
1157void
1158ring_buffer_free(struct ring_buffer *buffer)
1159{
1160 int cpu;
1161
Steven Rostedt554f7862009-03-11 22:00:13 -04001162 get_online_cpus();
1163
Steven Rostedt59222ef2009-03-12 11:46:03 -04001164#ifdef CONFIG_HOTPLUG_CPU
Steven Rostedt554f7862009-03-11 22:00:13 -04001165 unregister_cpu_notifier(&buffer->cpu_notify);
1166#endif
1167
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001168 for_each_buffer_cpu(buffer, cpu)
1169 rb_free_cpu_buffer(buffer->buffers[cpu]);
1170
Steven Rostedt554f7862009-03-11 22:00:13 -04001171 put_online_cpus();
1172
Rusty Russell9e01c1b2009-01-01 10:12:22 +10301173 free_cpumask_var(buffer->cpumask);
1174
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001175 kfree(buffer);
1176}
Robert Richterc4f50182008-12-11 16:49:22 +01001177EXPORT_SYMBOL_GPL(ring_buffer_free);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001178
Steven Rostedt37886f62009-03-17 17:22:06 -04001179void ring_buffer_set_clock(struct ring_buffer *buffer,
1180 u64 (*clock)(void))
1181{
1182 buffer->clock = clock;
1183}
1184
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001185static void rb_reset_cpu(struct ring_buffer_per_cpu *cpu_buffer);
1186
1187static void
1188rb_remove_pages(struct ring_buffer_per_cpu *cpu_buffer, unsigned nr_pages)
1189{
Steven Rostedt044fa782008-12-02 23:50:03 -05001190 struct buffer_page *bpage;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001191 struct list_head *p;
1192 unsigned i;
1193
1194 atomic_inc(&cpu_buffer->record_disabled);
1195 synchronize_sched();
1196
Steven Rostedt77ae3652009-03-27 11:00:29 -04001197 rb_head_page_deactivate(cpu_buffer);
1198
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001199 for (i = 0; i < nr_pages; i++) {
Steven Rostedt3adc54f2009-03-30 15:32:01 -04001200 if (RB_WARN_ON(cpu_buffer, list_empty(cpu_buffer->pages)))
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05001201 return;
Steven Rostedt3adc54f2009-03-30 15:32:01 -04001202 p = cpu_buffer->pages->next;
Steven Rostedt044fa782008-12-02 23:50:03 -05001203 bpage = list_entry(p, struct buffer_page, list);
1204 list_del_init(&bpage->list);
1205 free_buffer_page(bpage);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001206 }
Steven Rostedt3adc54f2009-03-30 15:32:01 -04001207 if (RB_WARN_ON(cpu_buffer, list_empty(cpu_buffer->pages)))
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05001208 return;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001209
1210 rb_reset_cpu(cpu_buffer);
1211
1212 rb_check_pages(cpu_buffer);
1213
1214 atomic_dec(&cpu_buffer->record_disabled);
1215
1216}
1217
1218static void
1219rb_insert_pages(struct ring_buffer_per_cpu *cpu_buffer,
1220 struct list_head *pages, unsigned nr_pages)
1221{
Steven Rostedt044fa782008-12-02 23:50:03 -05001222 struct buffer_page *bpage;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001223 struct list_head *p;
1224 unsigned i;
1225
1226 atomic_inc(&cpu_buffer->record_disabled);
1227 synchronize_sched();
1228
Steven Rostedt77ae3652009-03-27 11:00:29 -04001229 spin_lock_irq(&cpu_buffer->reader_lock);
1230 rb_head_page_deactivate(cpu_buffer);
1231
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001232 for (i = 0; i < nr_pages; i++) {
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05001233 if (RB_WARN_ON(cpu_buffer, list_empty(pages)))
1234 return;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001235 p = pages->next;
Steven Rostedt044fa782008-12-02 23:50:03 -05001236 bpage = list_entry(p, struct buffer_page, list);
1237 list_del_init(&bpage->list);
Steven Rostedt3adc54f2009-03-30 15:32:01 -04001238 list_add_tail(&bpage->list, cpu_buffer->pages);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001239 }
1240 rb_reset_cpu(cpu_buffer);
Steven Rostedt77ae3652009-03-27 11:00:29 -04001241 spin_unlock_irq(&cpu_buffer->reader_lock);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001242
1243 rb_check_pages(cpu_buffer);
1244
1245 atomic_dec(&cpu_buffer->record_disabled);
1246}
1247
1248/**
1249 * ring_buffer_resize - resize the ring buffer
1250 * @buffer: the buffer to resize.
1251 * @size: the new size.
1252 *
1253 * The tracer is responsible for making sure that the buffer is
1254 * not being used while changing the size.
1255 * Note: We may be able to change the above requirement by using
1256 * RCU synchronizations.
1257 *
1258 * Minimum size is 2 * BUF_PAGE_SIZE.
1259 *
1260 * Returns -1 on failure.
1261 */
1262int ring_buffer_resize(struct ring_buffer *buffer, unsigned long size)
1263{
1264 struct ring_buffer_per_cpu *cpu_buffer;
1265 unsigned nr_pages, rm_pages, new_pages;
Steven Rostedt044fa782008-12-02 23:50:03 -05001266 struct buffer_page *bpage, *tmp;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001267 unsigned long buffer_size;
1268 unsigned long addr;
1269 LIST_HEAD(pages);
1270 int i, cpu;
1271
Ingo Molnaree51a1d2008-11-13 14:58:31 +01001272 /*
1273 * Always succeed at resizing a non-existent buffer:
1274 */
1275 if (!buffer)
1276 return size;
1277
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001278 size = DIV_ROUND_UP(size, BUF_PAGE_SIZE);
1279 size *= BUF_PAGE_SIZE;
1280 buffer_size = buffer->pages * BUF_PAGE_SIZE;
1281
1282 /* we need a minimum of two pages */
1283 if (size < BUF_PAGE_SIZE * 2)
1284 size = BUF_PAGE_SIZE * 2;
1285
1286 if (size == buffer_size)
1287 return size;
1288
1289 mutex_lock(&buffer->mutex);
Steven Rostedt554f7862009-03-11 22:00:13 -04001290 get_online_cpus();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001291
1292 nr_pages = DIV_ROUND_UP(size, BUF_PAGE_SIZE);
1293
1294 if (size < buffer_size) {
1295
1296 /* easy case, just free pages */
Steven Rostedt554f7862009-03-11 22:00:13 -04001297 if (RB_WARN_ON(buffer, nr_pages >= buffer->pages))
1298 goto out_fail;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001299
1300 rm_pages = buffer->pages - nr_pages;
1301
1302 for_each_buffer_cpu(buffer, cpu) {
1303 cpu_buffer = buffer->buffers[cpu];
1304 rb_remove_pages(cpu_buffer, rm_pages);
1305 }
1306 goto out;
1307 }
1308
1309 /*
1310 * This is a bit more difficult. We only want to add pages
1311 * when we can allocate enough for all CPUs. We do this
1312 * by allocating all the pages and storing them on a local
1313 * link list. If we succeed in our allocation, then we
1314 * add these pages to the cpu_buffers. Otherwise we just free
1315 * them all and return -ENOMEM;
1316 */
Steven Rostedt554f7862009-03-11 22:00:13 -04001317 if (RB_WARN_ON(buffer, nr_pages <= buffer->pages))
1318 goto out_fail;
Steven Rostedtf536aaf2008-11-10 23:07:30 -05001319
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001320 new_pages = nr_pages - buffer->pages;
1321
1322 for_each_buffer_cpu(buffer, cpu) {
1323 for (i = 0; i < new_pages; i++) {
Steven Rostedt044fa782008-12-02 23:50:03 -05001324 bpage = kzalloc_node(ALIGN(sizeof(*bpage),
Steven Rostedte4c2ce82008-10-01 11:14:54 -04001325 cache_line_size()),
1326 GFP_KERNEL, cpu_to_node(cpu));
Steven Rostedt044fa782008-12-02 23:50:03 -05001327 if (!bpage)
Steven Rostedte4c2ce82008-10-01 11:14:54 -04001328 goto free_pages;
Steven Rostedt044fa782008-12-02 23:50:03 -05001329 list_add(&bpage->list, &pages);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001330 addr = __get_free_page(GFP_KERNEL);
1331 if (!addr)
1332 goto free_pages;
Steven Rostedt044fa782008-12-02 23:50:03 -05001333 bpage->page = (void *)addr;
1334 rb_init_page(bpage->page);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001335 }
1336 }
1337
1338 for_each_buffer_cpu(buffer, cpu) {
1339 cpu_buffer = buffer->buffers[cpu];
1340 rb_insert_pages(cpu_buffer, &pages, new_pages);
1341 }
1342
Steven Rostedt554f7862009-03-11 22:00:13 -04001343 if (RB_WARN_ON(buffer, !list_empty(&pages)))
1344 goto out_fail;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001345
1346 out:
1347 buffer->pages = nr_pages;
Steven Rostedt554f7862009-03-11 22:00:13 -04001348 put_online_cpus();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001349 mutex_unlock(&buffer->mutex);
1350
1351 return size;
1352
1353 free_pages:
Steven Rostedt044fa782008-12-02 23:50:03 -05001354 list_for_each_entry_safe(bpage, tmp, &pages, list) {
1355 list_del_init(&bpage->list);
1356 free_buffer_page(bpage);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001357 }
Steven Rostedt554f7862009-03-11 22:00:13 -04001358 put_online_cpus();
Vegard Nossum641d2f62008-11-18 19:22:13 +01001359 mutex_unlock(&buffer->mutex);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001360 return -ENOMEM;
Steven Rostedt554f7862009-03-11 22:00:13 -04001361
1362 /*
1363 * Something went totally wrong, and we are too paranoid
1364 * to even clean up the mess.
1365 */
1366 out_fail:
1367 put_online_cpus();
1368 mutex_unlock(&buffer->mutex);
1369 return -1;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001370}
Robert Richterc4f50182008-12-11 16:49:22 +01001371EXPORT_SYMBOL_GPL(ring_buffer_resize);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001372
Steven Rostedt8789a9e2008-12-02 15:34:07 -05001373static inline void *
Steven Rostedt044fa782008-12-02 23:50:03 -05001374__rb_data_page_index(struct buffer_data_page *bpage, unsigned index)
Steven Rostedt8789a9e2008-12-02 15:34:07 -05001375{
Steven Rostedt044fa782008-12-02 23:50:03 -05001376 return bpage->data + index;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05001377}
1378
Steven Rostedt044fa782008-12-02 23:50:03 -05001379static inline void *__rb_page_index(struct buffer_page *bpage, unsigned index)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001380{
Steven Rostedt044fa782008-12-02 23:50:03 -05001381 return bpage->page->data + index;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001382}
1383
1384static inline struct ring_buffer_event *
Steven Rostedtd7690412008-10-01 00:29:53 -04001385rb_reader_event(struct ring_buffer_per_cpu *cpu_buffer)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001386{
Steven Rostedt6f807ac2008-10-04 02:00:58 -04001387 return __rb_page_index(cpu_buffer->reader_page,
1388 cpu_buffer->reader_page->read);
1389}
1390
1391static inline struct ring_buffer_event *
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001392rb_iter_head_event(struct ring_buffer_iter *iter)
1393{
Steven Rostedt6f807ac2008-10-04 02:00:58 -04001394 return __rb_page_index(iter->head_page, iter->head);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001395}
1396
Steven Rostedt77ae3652009-03-27 11:00:29 -04001397static inline unsigned long rb_page_write(struct buffer_page *bpage)
Steven Rostedtbf41a152008-10-04 02:00:59 -04001398{
Steven Rostedt77ae3652009-03-27 11:00:29 -04001399 return local_read(&bpage->write) & RB_WRITE_MASK;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001400}
1401
1402static inline unsigned rb_page_commit(struct buffer_page *bpage)
1403{
Steven Rostedtabc9b562008-12-02 15:34:06 -05001404 return local_read(&bpage->page->commit);
Steven Rostedtbf41a152008-10-04 02:00:59 -04001405}
1406
Steven Rostedt77ae3652009-03-27 11:00:29 -04001407static inline unsigned long rb_page_entries(struct buffer_page *bpage)
1408{
1409 return local_read(&bpage->entries) & RB_WRITE_MASK;
1410}
1411
Steven Rostedtbf41a152008-10-04 02:00:59 -04001412/* Size is determined by what has been commited */
1413static inline unsigned rb_page_size(struct buffer_page *bpage)
1414{
1415 return rb_page_commit(bpage);
1416}
1417
1418static inline unsigned
1419rb_commit_index(struct ring_buffer_per_cpu *cpu_buffer)
1420{
1421 return rb_page_commit(cpu_buffer->commit_page);
1422}
1423
Steven Rostedtbf41a152008-10-04 02:00:59 -04001424static inline unsigned
1425rb_event_index(struct ring_buffer_event *event)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001426{
Steven Rostedtbf41a152008-10-04 02:00:59 -04001427 unsigned long addr = (unsigned long)event;
1428
Steven Rostedt22f470f2009-06-11 09:29:58 -04001429 return (addr & ~PAGE_MASK) - BUF_PAGE_HDR_SIZE;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001430}
1431
Steven Rostedt0f0c85f2009-05-11 16:08:00 -04001432static inline int
Steven Rostedtfa743952009-06-16 12:37:57 -04001433rb_event_is_commit(struct ring_buffer_per_cpu *cpu_buffer,
1434 struct ring_buffer_event *event)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001435{
Steven Rostedtbf41a152008-10-04 02:00:59 -04001436 unsigned long addr = (unsigned long)event;
1437 unsigned long index;
1438
1439 index = rb_event_index(event);
1440 addr &= PAGE_MASK;
1441
1442 return cpu_buffer->commit_page->page == (void *)addr &&
1443 rb_commit_index(cpu_buffer) == index;
1444}
1445
Andrew Morton34a148b2009-01-09 12:27:09 -08001446static void
Steven Rostedtbf41a152008-10-04 02:00:59 -04001447rb_set_commit_to_write(struct ring_buffer_per_cpu *cpu_buffer)
1448{
Steven Rostedt77ae3652009-03-27 11:00:29 -04001449 unsigned long max_count;
1450
Steven Rostedtbf41a152008-10-04 02:00:59 -04001451 /*
1452 * We only race with interrupts and NMIs on this CPU.
1453 * If we own the commit event, then we can commit
1454 * all others that interrupted us, since the interruptions
1455 * are in stack format (they finish before they come
1456 * back to us). This allows us to do a simple loop to
1457 * assign the commit to the tail.
1458 */
Steven Rostedta8ccf1d2008-12-23 11:32:24 -05001459 again:
Steven Rostedt77ae3652009-03-27 11:00:29 -04001460 max_count = cpu_buffer->buffer->pages * 100;
1461
Steven Rostedtbf41a152008-10-04 02:00:59 -04001462 while (cpu_buffer->commit_page != cpu_buffer->tail_page) {
Steven Rostedt77ae3652009-03-27 11:00:29 -04001463 if (RB_WARN_ON(cpu_buffer, !(--max_count)))
1464 return;
1465 if (RB_WARN_ON(cpu_buffer,
1466 rb_is_reader_page(cpu_buffer->tail_page)))
1467 return;
1468 local_set(&cpu_buffer->commit_page->page->commit,
1469 rb_page_write(cpu_buffer->commit_page));
Steven Rostedtbf41a152008-10-04 02:00:59 -04001470 rb_inc_page(cpu_buffer, &cpu_buffer->commit_page);
Steven Rostedtabc9b562008-12-02 15:34:06 -05001471 cpu_buffer->write_stamp =
1472 cpu_buffer->commit_page->page->time_stamp;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001473 /* add barrier to keep gcc from optimizing too much */
1474 barrier();
1475 }
1476 while (rb_commit_index(cpu_buffer) !=
1477 rb_page_write(cpu_buffer->commit_page)) {
Steven Rostedt77ae3652009-03-27 11:00:29 -04001478
1479 local_set(&cpu_buffer->commit_page->page->commit,
1480 rb_page_write(cpu_buffer->commit_page));
1481 RB_WARN_ON(cpu_buffer,
1482 local_read(&cpu_buffer->commit_page->page->commit) &
1483 ~RB_WRITE_MASK);
Steven Rostedtbf41a152008-10-04 02:00:59 -04001484 barrier();
1485 }
Steven Rostedta8ccf1d2008-12-23 11:32:24 -05001486
1487 /* again, keep gcc from optimizing */
1488 barrier();
1489
1490 /*
1491 * If an interrupt came in just after the first while loop
1492 * and pushed the tail page forward, we will be left with
1493 * a dangling commit that will never go forward.
1494 */
1495 if (unlikely(cpu_buffer->commit_page != cpu_buffer->tail_page))
1496 goto again;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001497}
1498
Steven Rostedtd7690412008-10-01 00:29:53 -04001499static void rb_reset_reader_page(struct ring_buffer_per_cpu *cpu_buffer)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001500{
Steven Rostedtabc9b562008-12-02 15:34:06 -05001501 cpu_buffer->read_stamp = cpu_buffer->reader_page->page->time_stamp;
Steven Rostedt6f807ac2008-10-04 02:00:58 -04001502 cpu_buffer->reader_page->read = 0;
Steven Rostedtd7690412008-10-01 00:29:53 -04001503}
1504
Andrew Morton34a148b2009-01-09 12:27:09 -08001505static void rb_inc_iter(struct ring_buffer_iter *iter)
Steven Rostedtd7690412008-10-01 00:29:53 -04001506{
1507 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
1508
1509 /*
1510 * The iterator could be on the reader page (it starts there).
1511 * But the head could have moved, since the reader was
1512 * found. Check for this case and assign the iterator
1513 * to the head page instead of next.
1514 */
1515 if (iter->head_page == cpu_buffer->reader_page)
Steven Rostedt77ae3652009-03-27 11:00:29 -04001516 iter->head_page = rb_set_head_page(cpu_buffer);
Steven Rostedtd7690412008-10-01 00:29:53 -04001517 else
1518 rb_inc_page(cpu_buffer, &iter->head_page);
1519
Steven Rostedtabc9b562008-12-02 15:34:06 -05001520 iter->read_stamp = iter->head_page->page->time_stamp;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001521 iter->head = 0;
1522}
1523
1524/**
1525 * ring_buffer_update_event - update event type and data
1526 * @event: the even to update
1527 * @type: the type of event
1528 * @length: the size of the event field in the ring buffer
1529 *
1530 * Update the type and data fields of the event. The length
1531 * is the actual size that is written to the ring buffer,
1532 * and with this, we can determine what to place into the
1533 * data field.
1534 */
Andrew Morton34a148b2009-01-09 12:27:09 -08001535static void
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001536rb_update_event(struct ring_buffer_event *event,
1537 unsigned type, unsigned length)
1538{
Lai Jiangshan334d4162009-04-24 11:27:05 +08001539 event->type_len = type;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001540
1541 switch (type) {
1542
1543 case RINGBUF_TYPE_PADDING:
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001544 case RINGBUF_TYPE_TIME_EXTEND:
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001545 case RINGBUF_TYPE_TIME_STAMP:
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001546 break;
1547
Lai Jiangshan334d4162009-04-24 11:27:05 +08001548 case 0:
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001549 length -= RB_EVNT_HDR_SIZE;
Lai Jiangshan334d4162009-04-24 11:27:05 +08001550 if (length > RB_MAX_SMALL_DATA)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001551 event->array[0] = length;
Lai Jiangshan334d4162009-04-24 11:27:05 +08001552 else
1553 event->type_len = DIV_ROUND_UP(length, RB_ALIGNMENT);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001554 break;
1555 default:
1556 BUG();
1557 }
1558}
1559
Steven Rostedt77ae3652009-03-27 11:00:29 -04001560/*
1561 * rb_handle_head_page - writer hit the head page
1562 *
1563 * Returns: +1 to retry page
1564 * 0 to continue
1565 * -1 on error
1566 */
1567static int
1568rb_handle_head_page(struct ring_buffer_per_cpu *cpu_buffer,
1569 struct buffer_page *tail_page,
1570 struct buffer_page *next_page)
1571{
1572 struct buffer_page *new_head;
1573 int entries;
1574 int type;
1575 int ret;
1576
1577 entries = rb_page_entries(next_page);
1578
1579 /*
1580 * The hard part is here. We need to move the head
1581 * forward, and protect against both readers on
1582 * other CPUs and writers coming in via interrupts.
1583 */
1584 type = rb_head_page_set_update(cpu_buffer, next_page, tail_page,
1585 RB_PAGE_HEAD);
1586
1587 /*
1588 * type can be one of four:
1589 * NORMAL - an interrupt already moved it for us
1590 * HEAD - we are the first to get here.
1591 * UPDATE - we are the interrupt interrupting
1592 * a current move.
1593 * MOVED - a reader on another CPU moved the next
1594 * pointer to its reader page. Give up
1595 * and try again.
1596 */
1597
1598 switch (type) {
1599 case RB_PAGE_HEAD:
1600 /*
1601 * We changed the head to UPDATE, thus
1602 * it is our responsibility to update
1603 * the counters.
1604 */
1605 local_add(entries, &cpu_buffer->overrun);
1606
1607 /*
1608 * The entries will be zeroed out when we move the
1609 * tail page.
1610 */
1611
1612 /* still more to do */
1613 break;
1614
1615 case RB_PAGE_UPDATE:
1616 /*
1617 * This is an interrupt that interrupt the
1618 * previous update. Still more to do.
1619 */
1620 break;
1621 case RB_PAGE_NORMAL:
1622 /*
1623 * An interrupt came in before the update
1624 * and processed this for us.
1625 * Nothing left to do.
1626 */
1627 return 1;
1628 case RB_PAGE_MOVED:
1629 /*
1630 * The reader is on another CPU and just did
1631 * a swap with our next_page.
1632 * Try again.
1633 */
1634 return 1;
1635 default:
1636 RB_WARN_ON(cpu_buffer, 1); /* WTF??? */
1637 return -1;
1638 }
1639
1640 /*
1641 * Now that we are here, the old head pointer is
1642 * set to UPDATE. This will keep the reader from
1643 * swapping the head page with the reader page.
1644 * The reader (on another CPU) will spin till
1645 * we are finished.
1646 *
1647 * We just need to protect against interrupts
1648 * doing the job. We will set the next pointer
1649 * to HEAD. After that, we set the old pointer
1650 * to NORMAL, but only if it was HEAD before.
1651 * otherwise we are an interrupt, and only
1652 * want the outer most commit to reset it.
1653 */
1654 new_head = next_page;
1655 rb_inc_page(cpu_buffer, &new_head);
1656
1657 ret = rb_head_page_set_head(cpu_buffer, new_head, next_page,
1658 RB_PAGE_NORMAL);
1659
1660 /*
1661 * Valid returns are:
1662 * HEAD - an interrupt came in and already set it.
1663 * NORMAL - One of two things:
1664 * 1) We really set it.
1665 * 2) A bunch of interrupts came in and moved
1666 * the page forward again.
1667 */
1668 switch (ret) {
1669 case RB_PAGE_HEAD:
1670 case RB_PAGE_NORMAL:
1671 /* OK */
1672 break;
1673 default:
1674 RB_WARN_ON(cpu_buffer, 1);
1675 return -1;
1676 }
1677
1678 /*
1679 * It is possible that an interrupt came in,
1680 * set the head up, then more interrupts came in
1681 * and moved it again. When we get back here,
1682 * the page would have been set to NORMAL but we
1683 * just set it back to HEAD.
1684 *
1685 * How do you detect this? Well, if that happened
1686 * the tail page would have moved.
1687 */
1688 if (ret == RB_PAGE_NORMAL) {
1689 /*
1690 * If the tail had moved passed next, then we need
1691 * to reset the pointer.
1692 */
1693 if (cpu_buffer->tail_page != tail_page &&
1694 cpu_buffer->tail_page != next_page)
1695 rb_head_page_set_normal(cpu_buffer, new_head,
1696 next_page,
1697 RB_PAGE_HEAD);
1698 }
1699
1700 /*
1701 * If this was the outer most commit (the one that
1702 * changed the original pointer from HEAD to UPDATE),
1703 * then it is up to us to reset it to NORMAL.
1704 */
1705 if (type == RB_PAGE_HEAD) {
1706 ret = rb_head_page_set_normal(cpu_buffer, next_page,
1707 tail_page,
1708 RB_PAGE_UPDATE);
1709 if (RB_WARN_ON(cpu_buffer,
1710 ret != RB_PAGE_UPDATE))
1711 return -1;
1712 }
1713
1714 return 0;
1715}
1716
Andrew Morton34a148b2009-01-09 12:27:09 -08001717static unsigned rb_calculate_event_length(unsigned length)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001718{
1719 struct ring_buffer_event event; /* Used only for sizeof array */
1720
1721 /* zero length can cause confusions */
1722 if (!length)
1723 length = 1;
1724
1725 if (length > RB_MAX_SMALL_DATA)
1726 length += sizeof(event.array[0]);
1727
1728 length += RB_EVNT_HDR_SIZE;
1729 length = ALIGN(length, RB_ALIGNMENT);
1730
1731 return length;
1732}
1733
Steven Rostedtc7b09302009-06-11 11:12:00 -04001734static inline void
1735rb_reset_tail(struct ring_buffer_per_cpu *cpu_buffer,
1736 struct buffer_page *tail_page,
1737 unsigned long tail, unsigned long length)
1738{
1739 struct ring_buffer_event *event;
1740
1741 /*
1742 * Only the event that crossed the page boundary
1743 * must fill the old tail_page with padding.
1744 */
1745 if (tail >= BUF_PAGE_SIZE) {
1746 local_sub(length, &tail_page->write);
1747 return;
1748 }
1749
1750 event = __rb_page_index(tail_page, tail);
Linus Torvaldsb0b70652009-06-20 10:56:46 -07001751 kmemcheck_annotate_bitfield(event, bitfield);
Steven Rostedtc7b09302009-06-11 11:12:00 -04001752
1753 /*
1754 * If this event is bigger than the minimum size, then
1755 * we need to be careful that we don't subtract the
1756 * write counter enough to allow another writer to slip
1757 * in on this page.
1758 * We put in a discarded commit instead, to make sure
1759 * that this space is not used again.
1760 *
1761 * If we are less than the minimum size, we don't need to
1762 * worry about it.
1763 */
1764 if (tail > (BUF_PAGE_SIZE - RB_EVNT_MIN_SIZE)) {
1765 /* No room for any events */
1766
1767 /* Mark the rest of the page with padding */
1768 rb_event_set_padding(event);
1769
1770 /* Set the write back to the previous setting */
1771 local_sub(length, &tail_page->write);
1772 return;
1773 }
1774
1775 /* Put in a discarded event */
1776 event->array[0] = (BUF_PAGE_SIZE - tail) - RB_EVNT_HDR_SIZE;
1777 event->type_len = RINGBUF_TYPE_PADDING;
1778 /* time delta must be non zero */
1779 event->time_delta = 1;
1780 /* Account for this as an entry */
1781 local_inc(&tail_page->entries);
1782 local_inc(&cpu_buffer->entries);
1783
1784 /* Set write to end of buffer */
1785 length = (tail + length) - BUF_PAGE_SIZE;
1786 local_sub(length, &tail_page->write);
1787}
Steven Rostedt6634ff22009-05-06 15:30:07 -04001788
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001789static struct ring_buffer_event *
Steven Rostedt6634ff22009-05-06 15:30:07 -04001790rb_move_tail(struct ring_buffer_per_cpu *cpu_buffer,
1791 unsigned long length, unsigned long tail,
1792 struct buffer_page *commit_page,
1793 struct buffer_page *tail_page, u64 *ts)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001794{
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001795 struct ring_buffer *buffer = cpu_buffer->buffer;
Steven Rostedt77ae3652009-03-27 11:00:29 -04001796 struct buffer_page *next_page;
1797 int ret;
Steven Rostedtaa20ae82009-05-05 21:16:11 -04001798
1799 next_page = tail_page;
1800
Steven Rostedtaa20ae82009-05-05 21:16:11 -04001801 rb_inc_page(cpu_buffer, &next_page);
1802
Steven Rostedtaa20ae82009-05-05 21:16:11 -04001803 /*
1804 * If for some reason, we had an interrupt storm that made
1805 * it all the way around the buffer, bail, and warn
1806 * about it.
1807 */
1808 if (unlikely(next_page == commit_page)) {
Steven Rostedt77ae3652009-03-27 11:00:29 -04001809 local_inc(&cpu_buffer->commit_overrun);
Steven Rostedtaa20ae82009-05-05 21:16:11 -04001810 goto out_reset;
1811 }
1812
Steven Rostedt77ae3652009-03-27 11:00:29 -04001813 /*
1814 * This is where the fun begins!
1815 *
1816 * We are fighting against races between a reader that
1817 * could be on another CPU trying to swap its reader
1818 * page with the buffer head.
1819 *
1820 * We are also fighting against interrupts coming in and
1821 * moving the head or tail on us as well.
1822 *
1823 * If the next page is the head page then we have filled
1824 * the buffer, unless the commit page is still on the
1825 * reader page.
1826 */
1827 if (rb_is_head_page(cpu_buffer, next_page, &tail_page->list)) {
Steven Rostedtaa20ae82009-05-05 21:16:11 -04001828
Steven Rostedt77ae3652009-03-27 11:00:29 -04001829 /*
1830 * If the commit is not on the reader page, then
1831 * move the header page.
1832 */
1833 if (!rb_is_reader_page(cpu_buffer->commit_page)) {
1834 /*
1835 * If we are not in overwrite mode,
1836 * this is easy, just stop here.
1837 */
1838 if (!(buffer->flags & RB_FL_OVERWRITE))
1839 goto out_reset;
Steven Rostedtaa20ae82009-05-05 21:16:11 -04001840
Steven Rostedt77ae3652009-03-27 11:00:29 -04001841 ret = rb_handle_head_page(cpu_buffer,
1842 tail_page,
1843 next_page);
1844 if (ret < 0)
1845 goto out_reset;
1846 if (ret)
1847 goto out_again;
1848 } else {
1849 /*
1850 * We need to be careful here too. The
1851 * commit page could still be on the reader
1852 * page. We could have a small buffer, and
1853 * have filled up the buffer with events
1854 * from interrupts and such, and wrapped.
1855 *
1856 * Note, if the tail page is also the on the
1857 * reader_page, we let it move out.
1858 */
1859 if (unlikely((cpu_buffer->commit_page !=
1860 cpu_buffer->tail_page) &&
1861 (cpu_buffer->commit_page ==
1862 cpu_buffer->reader_page))) {
1863 local_inc(&cpu_buffer->commit_overrun);
1864 goto out_reset;
1865 }
Steven Rostedtaa20ae82009-05-05 21:16:11 -04001866 }
1867 }
1868
Steven Rostedt77ae3652009-03-27 11:00:29 -04001869 ret = rb_tail_page_update(cpu_buffer, tail_page, next_page);
1870 if (ret) {
1871 /*
1872 * Nested commits always have zero deltas, so
1873 * just reread the time stamp
1874 */
Steven Rostedt88eb0122009-05-11 16:28:23 -04001875 *ts = rb_time_stamp(buffer, cpu_buffer->cpu);
Steven Rostedt77ae3652009-03-27 11:00:29 -04001876 next_page->page->time_stamp = *ts;
Steven Rostedtaa20ae82009-05-05 21:16:11 -04001877 }
1878
Steven Rostedt77ae3652009-03-27 11:00:29 -04001879 out_again:
Steven Rostedtaa20ae82009-05-05 21:16:11 -04001880
Steven Rostedt77ae3652009-03-27 11:00:29 -04001881 rb_reset_tail(cpu_buffer, tail_page, tail, length);
Steven Rostedtaa20ae82009-05-05 21:16:11 -04001882
1883 /* fail and let the caller try again */
1884 return ERR_PTR(-EAGAIN);
1885
Steven Rostedt45141d42009-02-12 13:19:48 -05001886 out_reset:
Lai Jiangshan6f3b3442009-01-12 11:06:18 +08001887 /* reset write */
Steven Rostedtc7b09302009-06-11 11:12:00 -04001888 rb_reset_tail(cpu_buffer, tail_page, tail, length);
Lai Jiangshan6f3b3442009-01-12 11:06:18 +08001889
Steven Rostedtbf41a152008-10-04 02:00:59 -04001890 return NULL;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001891}
1892
Steven Rostedt6634ff22009-05-06 15:30:07 -04001893static struct ring_buffer_event *
1894__rb_reserve_next(struct ring_buffer_per_cpu *cpu_buffer,
1895 unsigned type, unsigned long length, u64 *ts)
1896{
1897 struct buffer_page *tail_page, *commit_page;
1898 struct ring_buffer_event *event;
1899 unsigned long tail, write;
1900
1901 commit_page = cpu_buffer->commit_page;
1902 /* we just need to protect against interrupts */
1903 barrier();
1904 tail_page = cpu_buffer->tail_page;
1905 write = local_add_return(length, &tail_page->write);
Steven Rostedt77ae3652009-03-27 11:00:29 -04001906
1907 /* set write to only the index of the write */
1908 write &= RB_WRITE_MASK;
Steven Rostedt6634ff22009-05-06 15:30:07 -04001909 tail = write - length;
1910
1911 /* See if we shot pass the end of this buffer page */
1912 if (write > BUF_PAGE_SIZE)
1913 return rb_move_tail(cpu_buffer, length, tail,
1914 commit_page, tail_page, ts);
1915
1916 /* We reserved something on the buffer */
1917
Steven Rostedt6634ff22009-05-06 15:30:07 -04001918 event = __rb_page_index(tail_page, tail);
Vegard Nossum1744a212009-02-28 08:29:44 +01001919 kmemcheck_annotate_bitfield(event, bitfield);
Steven Rostedt6634ff22009-05-06 15:30:07 -04001920 rb_update_event(event, type, length);
1921
1922 /* The passed in type is zero for DATA */
1923 if (likely(!type))
1924 local_inc(&tail_page->entries);
1925
1926 /*
Steven Rostedtfa743952009-06-16 12:37:57 -04001927 * If this is the first commit on the page, then update
1928 * its timestamp.
Steven Rostedt6634ff22009-05-06 15:30:07 -04001929 */
Steven Rostedtfa743952009-06-16 12:37:57 -04001930 if (!tail)
1931 tail_page->page->time_stamp = *ts;
Steven Rostedt6634ff22009-05-06 15:30:07 -04001932
1933 return event;
1934}
1935
Steven Rostedtedd813bf2009-06-02 23:00:53 -04001936static inline int
1937rb_try_to_discard(struct ring_buffer_per_cpu *cpu_buffer,
1938 struct ring_buffer_event *event)
1939{
1940 unsigned long new_index, old_index;
1941 struct buffer_page *bpage;
1942 unsigned long index;
1943 unsigned long addr;
1944
1945 new_index = rb_event_index(event);
1946 old_index = new_index + rb_event_length(event);
1947 addr = (unsigned long)event;
1948 addr &= PAGE_MASK;
1949
1950 bpage = cpu_buffer->tail_page;
1951
1952 if (bpage->page == (void *)addr && rb_page_write(bpage) == old_index) {
Steven Rostedt77ae3652009-03-27 11:00:29 -04001953 unsigned long write_mask =
1954 local_read(&bpage->write) & ~RB_WRITE_MASK;
Steven Rostedtedd813bf2009-06-02 23:00:53 -04001955 /*
1956 * This is on the tail page. It is possible that
1957 * a write could come in and move the tail page
1958 * and write to the next page. That is fine
1959 * because we just shorten what is on this page.
1960 */
Steven Rostedt77ae3652009-03-27 11:00:29 -04001961 old_index += write_mask;
1962 new_index += write_mask;
Steven Rostedtedd813bf2009-06-02 23:00:53 -04001963 index = local_cmpxchg(&bpage->write, old_index, new_index);
1964 if (index == old_index)
1965 return 1;
1966 }
1967
1968 /* could not discard */
1969 return 0;
1970}
1971
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001972static int
1973rb_add_time_stamp(struct ring_buffer_per_cpu *cpu_buffer,
1974 u64 *ts, u64 *delta)
1975{
1976 struct ring_buffer_event *event;
1977 static int once;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001978 int ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001979
1980 if (unlikely(*delta > (1ULL << 59) && !once++)) {
1981 printk(KERN_WARNING "Delta way too big! %llu"
1982 " ts=%llu write stamp = %llu\n",
Stephen Rothwelle2862c92008-10-27 17:43:28 +11001983 (unsigned long long)*delta,
1984 (unsigned long long)*ts,
1985 (unsigned long long)cpu_buffer->write_stamp);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001986 WARN_ON(1);
1987 }
1988
1989 /*
1990 * The delta is too big, we to add a
1991 * new timestamp.
1992 */
1993 event = __rb_reserve_next(cpu_buffer,
1994 RINGBUF_TYPE_TIME_EXTEND,
1995 RB_LEN_TIME_EXTEND,
1996 ts);
1997 if (!event)
Steven Rostedtbf41a152008-10-04 02:00:59 -04001998 return -EBUSY;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001999
Steven Rostedtbf41a152008-10-04 02:00:59 -04002000 if (PTR_ERR(event) == -EAGAIN)
2001 return -EAGAIN;
2002
2003 /* Only a commited time event can update the write stamp */
Steven Rostedtfa743952009-06-16 12:37:57 -04002004 if (rb_event_is_commit(cpu_buffer, event)) {
Steven Rostedtbf41a152008-10-04 02:00:59 -04002005 /*
Steven Rostedtfa743952009-06-16 12:37:57 -04002006 * If this is the first on the page, then it was
2007 * updated with the page itself. Try to discard it
2008 * and if we can't just make it zero.
Steven Rostedtbf41a152008-10-04 02:00:59 -04002009 */
2010 if (rb_event_index(event)) {
2011 event->time_delta = *delta & TS_MASK;
2012 event->array[0] = *delta >> TS_SHIFT;
2013 } else {
Steven Rostedtea05b572009-06-03 09:30:10 -04002014 /* try to discard, since we do not need this */
2015 if (!rb_try_to_discard(cpu_buffer, event)) {
2016 /* nope, just zero it */
2017 event->time_delta = 0;
2018 event->array[0] = 0;
2019 }
Steven Rostedtbf41a152008-10-04 02:00:59 -04002020 }
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002021 cpu_buffer->write_stamp = *ts;
Steven Rostedtbf41a152008-10-04 02:00:59 -04002022 /* let the caller know this was the commit */
2023 ret = 1;
2024 } else {
Steven Rostedtedd813bf2009-06-02 23:00:53 -04002025 /* Try to discard the event */
2026 if (!rb_try_to_discard(cpu_buffer, event)) {
2027 /* Darn, this is just wasted space */
2028 event->time_delta = 0;
2029 event->array[0] = 0;
Steven Rostedtedd813bf2009-06-02 23:00:53 -04002030 }
Steven Rostedtf57a8a12009-06-05 14:11:30 -04002031 ret = 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002032 }
2033
Steven Rostedtbf41a152008-10-04 02:00:59 -04002034 *delta = 0;
2035
2036 return ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002037}
2038
Steven Rostedtfa743952009-06-16 12:37:57 -04002039static void rb_start_commit(struct ring_buffer_per_cpu *cpu_buffer)
2040{
2041 local_inc(&cpu_buffer->committing);
2042 local_inc(&cpu_buffer->commits);
2043}
2044
2045static void rb_end_commit(struct ring_buffer_per_cpu *cpu_buffer)
2046{
2047 unsigned long commits;
2048
2049 if (RB_WARN_ON(cpu_buffer,
2050 !local_read(&cpu_buffer->committing)))
2051 return;
2052
2053 again:
2054 commits = local_read(&cpu_buffer->commits);
2055 /* synchronize with interrupts */
2056 barrier();
2057 if (local_read(&cpu_buffer->committing) == 1)
2058 rb_set_commit_to_write(cpu_buffer);
2059
2060 local_dec(&cpu_buffer->committing);
2061
2062 /* synchronize with interrupts */
2063 barrier();
2064
2065 /*
2066 * Need to account for interrupts coming in between the
2067 * updating of the commit page and the clearing of the
2068 * committing counter.
2069 */
2070 if (unlikely(local_read(&cpu_buffer->commits) != commits) &&
2071 !local_read(&cpu_buffer->committing)) {
2072 local_inc(&cpu_buffer->committing);
2073 goto again;
2074 }
2075}
2076
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002077static struct ring_buffer_event *
2078rb_reserve_next_event(struct ring_buffer_per_cpu *cpu_buffer,
Steven Rostedt1cd8d732009-05-11 14:08:09 -04002079 unsigned long length)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002080{
2081 struct ring_buffer_event *event;
Steven Rostedt168b6b12009-05-11 22:11:05 -04002082 u64 ts, delta = 0;
Steven Rostedtbf41a152008-10-04 02:00:59 -04002083 int commit = 0;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002084 int nr_loops = 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002085
Steven Rostedtfa743952009-06-16 12:37:57 -04002086 rb_start_commit(cpu_buffer);
2087
Steven Rostedtbe957c42009-05-11 14:42:53 -04002088 length = rb_calculate_event_length(length);
Steven Rostedtbf41a152008-10-04 02:00:59 -04002089 again:
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002090 /*
2091 * We allow for interrupts to reenter here and do a trace.
2092 * If one does, it will cause this original code to loop
2093 * back here. Even with heavy interrupts happening, this
2094 * should only happen a few times in a row. If this happens
2095 * 1000 times in a row, there must be either an interrupt
2096 * storm or we have something buggy.
2097 * Bail!
2098 */
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05002099 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 1000))
Steven Rostedtfa743952009-06-16 12:37:57 -04002100 goto out_fail;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002101
Steven Rostedt88eb0122009-05-11 16:28:23 -04002102 ts = rb_time_stamp(cpu_buffer->buffer, cpu_buffer->cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002103
Steven Rostedtbf41a152008-10-04 02:00:59 -04002104 /*
2105 * Only the first commit can update the timestamp.
2106 * Yes there is a race here. If an interrupt comes in
2107 * just after the conditional and it traces too, then it
2108 * will also check the deltas. More than one timestamp may
2109 * also be made. But only the entry that did the actual
2110 * commit will be something other than zero.
2111 */
Steven Rostedt0f0c85f2009-05-11 16:08:00 -04002112 if (likely(cpu_buffer->tail_page == cpu_buffer->commit_page &&
2113 rb_page_write(cpu_buffer->tail_page) ==
2114 rb_commit_index(cpu_buffer))) {
Steven Rostedt168b6b12009-05-11 22:11:05 -04002115 u64 diff;
Steven Rostedtbf41a152008-10-04 02:00:59 -04002116
Steven Rostedt168b6b12009-05-11 22:11:05 -04002117 diff = ts - cpu_buffer->write_stamp;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002118
Steven Rostedt168b6b12009-05-11 22:11:05 -04002119 /* make sure this diff is calculated here */
Steven Rostedtbf41a152008-10-04 02:00:59 -04002120 barrier();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002121
Steven Rostedtbf41a152008-10-04 02:00:59 -04002122 /* Did the write stamp get updated already? */
2123 if (unlikely(ts < cpu_buffer->write_stamp))
Steven Rostedt168b6b12009-05-11 22:11:05 -04002124 goto get_event;
Steven Rostedtbf41a152008-10-04 02:00:59 -04002125
Steven Rostedt168b6b12009-05-11 22:11:05 -04002126 delta = diff;
2127 if (unlikely(test_time_stamp(delta))) {
Steven Rostedtbf41a152008-10-04 02:00:59 -04002128
2129 commit = rb_add_time_stamp(cpu_buffer, &ts, &delta);
Steven Rostedtbf41a152008-10-04 02:00:59 -04002130 if (commit == -EBUSY)
Steven Rostedtfa743952009-06-16 12:37:57 -04002131 goto out_fail;
Steven Rostedtbf41a152008-10-04 02:00:59 -04002132
2133 if (commit == -EAGAIN)
2134 goto again;
2135
2136 RB_WARN_ON(cpu_buffer, commit < 0);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002137 }
Steven Rostedt168b6b12009-05-11 22:11:05 -04002138 }
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002139
Steven Rostedt168b6b12009-05-11 22:11:05 -04002140 get_event:
Steven Rostedt1cd8d732009-05-11 14:08:09 -04002141 event = __rb_reserve_next(cpu_buffer, 0, length, &ts);
Steven Rostedt168b6b12009-05-11 22:11:05 -04002142 if (unlikely(PTR_ERR(event) == -EAGAIN))
Steven Rostedtbf41a152008-10-04 02:00:59 -04002143 goto again;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002144
Steven Rostedtfa743952009-06-16 12:37:57 -04002145 if (!event)
2146 goto out_fail;
Steven Rostedtbf41a152008-10-04 02:00:59 -04002147
Steven Rostedtfa743952009-06-16 12:37:57 -04002148 if (!rb_event_is_commit(cpu_buffer, event))
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002149 delta = 0;
2150
2151 event->time_delta = delta;
2152
2153 return event;
Steven Rostedtfa743952009-06-16 12:37:57 -04002154
2155 out_fail:
2156 rb_end_commit(cpu_buffer);
2157 return NULL;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002158}
2159
Paul Mundt1155de42009-06-25 14:30:12 +09002160#ifdef CONFIG_TRACING
2161
Steven Rostedtaa18efb2009-04-20 16:16:11 -04002162#define TRACE_RECURSIVE_DEPTH 16
Steven Rostedt261842b2009-04-16 21:41:52 -04002163
2164static int trace_recursive_lock(void)
2165{
Steven Rostedtaa18efb2009-04-20 16:16:11 -04002166 current->trace_recursion++;
Steven Rostedt261842b2009-04-16 21:41:52 -04002167
Steven Rostedtaa18efb2009-04-20 16:16:11 -04002168 if (likely(current->trace_recursion < TRACE_RECURSIVE_DEPTH))
2169 return 0;
Steven Rostedt261842b2009-04-16 21:41:52 -04002170
Steven Rostedtaa18efb2009-04-20 16:16:11 -04002171 /* Disable all tracing before we do anything else */
2172 tracing_off_permanent();
Frederic Weisbeckere057a5e2009-04-19 23:38:12 +02002173
Steven Rostedt7d7d2b82009-04-27 12:37:49 -04002174 printk_once(KERN_WARNING "Tracing recursion: depth[%ld]:"
Steven Rostedtaa18efb2009-04-20 16:16:11 -04002175 "HC[%lu]:SC[%lu]:NMI[%lu]\n",
2176 current->trace_recursion,
2177 hardirq_count() >> HARDIRQ_SHIFT,
2178 softirq_count() >> SOFTIRQ_SHIFT,
2179 in_nmi());
Frederic Weisbeckere057a5e2009-04-19 23:38:12 +02002180
Steven Rostedtaa18efb2009-04-20 16:16:11 -04002181 WARN_ON_ONCE(1);
2182 return -1;
Steven Rostedt261842b2009-04-16 21:41:52 -04002183}
2184
2185static void trace_recursive_unlock(void)
2186{
Steven Rostedtaa18efb2009-04-20 16:16:11 -04002187 WARN_ON_ONCE(!current->trace_recursion);
Steven Rostedt261842b2009-04-16 21:41:52 -04002188
Steven Rostedtaa18efb2009-04-20 16:16:11 -04002189 current->trace_recursion--;
Steven Rostedt261842b2009-04-16 21:41:52 -04002190}
2191
Paul Mundt1155de42009-06-25 14:30:12 +09002192#else
2193
2194#define trace_recursive_lock() (0)
2195#define trace_recursive_unlock() do { } while (0)
2196
2197#endif
2198
Steven Rostedtbf41a152008-10-04 02:00:59 -04002199static DEFINE_PER_CPU(int, rb_need_resched);
2200
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002201/**
2202 * ring_buffer_lock_reserve - reserve a part of the buffer
2203 * @buffer: the ring buffer to reserve from
2204 * @length: the length of the data to reserve (excluding event header)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002205 *
2206 * Returns a reseverd event on the ring buffer to copy directly to.
2207 * The user of this interface will need to get the body to write into
2208 * and can use the ring_buffer_event_data() interface.
2209 *
2210 * The length is the length of the data needed, not the event length
2211 * which also includes the event header.
2212 *
2213 * Must be paired with ring_buffer_unlock_commit, unless NULL is returned.
2214 * If NULL is returned, then nothing has been allocated or locked.
2215 */
2216struct ring_buffer_event *
Arnaldo Carvalho de Melo0a987752009-02-05 16:12:56 -02002217ring_buffer_lock_reserve(struct ring_buffer *buffer, unsigned long length)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002218{
2219 struct ring_buffer_per_cpu *cpu_buffer;
2220 struct ring_buffer_event *event;
Steven Rostedtbf41a152008-10-04 02:00:59 -04002221 int cpu, resched;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002222
Steven Rostedt033601a2008-11-21 12:41:55 -05002223 if (ring_buffer_flags != RB_BUFFERS_ON)
Steven Rostedta3583242008-11-11 15:01:42 -05002224 return NULL;
2225
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002226 if (atomic_read(&buffer->record_disabled))
2227 return NULL;
2228
Steven Rostedtbf41a152008-10-04 02:00:59 -04002229 /* If we are tracing schedule, we don't want to recurse */
Steven Rostedt182e9f52008-11-03 23:15:56 -05002230 resched = ftrace_preempt_disable();
Steven Rostedtbf41a152008-10-04 02:00:59 -04002231
Steven Rostedt261842b2009-04-16 21:41:52 -04002232 if (trace_recursive_lock())
2233 goto out_nocheck;
2234
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002235 cpu = raw_smp_processor_id();
2236
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302237 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedtd7690412008-10-01 00:29:53 -04002238 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002239
2240 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002241
2242 if (atomic_read(&cpu_buffer->record_disabled))
Steven Rostedtd7690412008-10-01 00:29:53 -04002243 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002244
Steven Rostedtbe957c42009-05-11 14:42:53 -04002245 if (length > BUF_MAX_DATA_SIZE)
Steven Rostedtbf41a152008-10-04 02:00:59 -04002246 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002247
Steven Rostedt1cd8d732009-05-11 14:08:09 -04002248 event = rb_reserve_next_event(cpu_buffer, length);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002249 if (!event)
Steven Rostedtd7690412008-10-01 00:29:53 -04002250 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002251
Steven Rostedtbf41a152008-10-04 02:00:59 -04002252 /*
2253 * Need to store resched state on this cpu.
2254 * Only the first needs to.
2255 */
2256
2257 if (preempt_count() == 1)
2258 per_cpu(rb_need_resched, cpu) = resched;
2259
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002260 return event;
2261
Steven Rostedtd7690412008-10-01 00:29:53 -04002262 out:
Steven Rostedt261842b2009-04-16 21:41:52 -04002263 trace_recursive_unlock();
2264
2265 out_nocheck:
Steven Rostedt182e9f52008-11-03 23:15:56 -05002266 ftrace_preempt_enable(resched);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002267 return NULL;
2268}
Robert Richterc4f50182008-12-11 16:49:22 +01002269EXPORT_SYMBOL_GPL(ring_buffer_lock_reserve);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002270
2271static void rb_commit(struct ring_buffer_per_cpu *cpu_buffer,
2272 struct ring_buffer_event *event)
2273{
Steven Rostedte4906ef2009-04-30 20:49:44 -04002274 local_inc(&cpu_buffer->entries);
Steven Rostedtbf41a152008-10-04 02:00:59 -04002275
Steven Rostedtfa743952009-06-16 12:37:57 -04002276 /*
2277 * The event first in the commit queue updates the
2278 * time stamp.
2279 */
2280 if (rb_event_is_commit(cpu_buffer, event))
2281 cpu_buffer->write_stamp += event->time_delta;
Steven Rostedtbf41a152008-10-04 02:00:59 -04002282
Steven Rostedtfa743952009-06-16 12:37:57 -04002283 rb_end_commit(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002284}
2285
2286/**
2287 * ring_buffer_unlock_commit - commit a reserved
2288 * @buffer: The buffer to commit to
2289 * @event: The event pointer to commit.
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002290 *
2291 * This commits the data to the ring buffer, and releases any locks held.
2292 *
2293 * Must be paired with ring_buffer_lock_reserve.
2294 */
2295int ring_buffer_unlock_commit(struct ring_buffer *buffer,
Arnaldo Carvalho de Melo0a987752009-02-05 16:12:56 -02002296 struct ring_buffer_event *event)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002297{
2298 struct ring_buffer_per_cpu *cpu_buffer;
2299 int cpu = raw_smp_processor_id();
2300
2301 cpu_buffer = buffer->buffers[cpu];
2302
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002303 rb_commit(cpu_buffer, event);
2304
Steven Rostedt261842b2009-04-16 21:41:52 -04002305 trace_recursive_unlock();
2306
Steven Rostedtbf41a152008-10-04 02:00:59 -04002307 /*
2308 * Only the last preempt count needs to restore preemption.
2309 */
Steven Rostedt182e9f52008-11-03 23:15:56 -05002310 if (preempt_count() == 1)
2311 ftrace_preempt_enable(per_cpu(rb_need_resched, cpu));
2312 else
Steven Rostedtbf41a152008-10-04 02:00:59 -04002313 preempt_enable_no_resched_notrace();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002314
2315 return 0;
2316}
Robert Richterc4f50182008-12-11 16:49:22 +01002317EXPORT_SYMBOL_GPL(ring_buffer_unlock_commit);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002318
Frederic Weisbeckerf3b9aae2009-04-19 23:39:33 +02002319static inline void rb_event_discard(struct ring_buffer_event *event)
2320{
Lai Jiangshan334d4162009-04-24 11:27:05 +08002321 /* array[0] holds the actual length for the discarded event */
2322 event->array[0] = rb_event_data_length(event) - RB_EVNT_HDR_SIZE;
2323 event->type_len = RINGBUF_TYPE_PADDING;
Frederic Weisbeckerf3b9aae2009-04-19 23:39:33 +02002324 /* time delta must be non zero */
2325 if (!event->time_delta)
2326 event->time_delta = 1;
2327}
2328
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002329/**
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002330 * ring_buffer_event_discard - discard any event in the ring buffer
2331 * @event: the event to discard
2332 *
2333 * Sometimes a event that is in the ring buffer needs to be ignored.
2334 * This function lets the user discard an event in the ring buffer
2335 * and then that event will not be read later.
2336 *
2337 * Note, it is up to the user to be careful with this, and protect
2338 * against races. If the user discards an event that has been consumed
2339 * it is possible that it could corrupt the ring buffer.
2340 */
2341void ring_buffer_event_discard(struct ring_buffer_event *event)
2342{
Frederic Weisbeckerf3b9aae2009-04-19 23:39:33 +02002343 rb_event_discard(event);
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002344}
2345EXPORT_SYMBOL_GPL(ring_buffer_event_discard);
2346
2347/**
2348 * ring_buffer_commit_discard - discard an event that has not been committed
2349 * @buffer: the ring buffer
2350 * @event: non committed event to discard
2351 *
2352 * This is similar to ring_buffer_event_discard but must only be
2353 * performed on an event that has not been committed yet. The difference
2354 * is that this will also try to free the event from the ring buffer
2355 * if another event has not been added behind it.
2356 *
2357 * If another event has been added behind it, it will set the event
2358 * up as discarded, and perform the commit.
2359 *
2360 * If this function is called, do not call ring_buffer_unlock_commit on
2361 * the event.
2362 */
2363void ring_buffer_discard_commit(struct ring_buffer *buffer,
2364 struct ring_buffer_event *event)
2365{
2366 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002367 int cpu;
2368
2369 /* The event is discarded regardless */
Frederic Weisbeckerf3b9aae2009-04-19 23:39:33 +02002370 rb_event_discard(event);
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002371
Steven Rostedtfa743952009-06-16 12:37:57 -04002372 cpu = smp_processor_id();
2373 cpu_buffer = buffer->buffers[cpu];
2374
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002375 /*
2376 * This must only be called if the event has not been
2377 * committed yet. Thus we can assume that preemption
2378 * is still disabled.
2379 */
Steven Rostedtfa743952009-06-16 12:37:57 -04002380 RB_WARN_ON(buffer, !local_read(&cpu_buffer->committing));
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002381
Steven Rostedtedd813bf2009-06-02 23:00:53 -04002382 if (!rb_try_to_discard(cpu_buffer, event))
2383 goto out;
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002384
2385 /*
2386 * The commit is still visible by the reader, so we
2387 * must increment entries.
2388 */
Steven Rostedte4906ef2009-04-30 20:49:44 -04002389 local_inc(&cpu_buffer->entries);
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002390 out:
Steven Rostedtfa743952009-06-16 12:37:57 -04002391 rb_end_commit(cpu_buffer);
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002392
Frederic Weisbeckerf3b9aae2009-04-19 23:39:33 +02002393 trace_recursive_unlock();
2394
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002395 /*
2396 * Only the last preempt count needs to restore preemption.
2397 */
2398 if (preempt_count() == 1)
2399 ftrace_preempt_enable(per_cpu(rb_need_resched, cpu));
2400 else
2401 preempt_enable_no_resched_notrace();
2402
2403}
2404EXPORT_SYMBOL_GPL(ring_buffer_discard_commit);
2405
2406/**
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002407 * ring_buffer_write - write data to the buffer without reserving
2408 * @buffer: The ring buffer to write to.
2409 * @length: The length of the data being written (excluding the event header)
2410 * @data: The data to write to the buffer.
2411 *
2412 * This is like ring_buffer_lock_reserve and ring_buffer_unlock_commit as
2413 * one function. If you already have the data to write to the buffer, it
2414 * may be easier to simply call this function.
2415 *
2416 * Note, like ring_buffer_lock_reserve, the length is the length of the data
2417 * and not the length of the event which would hold the header.
2418 */
2419int ring_buffer_write(struct ring_buffer *buffer,
2420 unsigned long length,
2421 void *data)
2422{
2423 struct ring_buffer_per_cpu *cpu_buffer;
2424 struct ring_buffer_event *event;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002425 void *body;
2426 int ret = -EBUSY;
Steven Rostedtbf41a152008-10-04 02:00:59 -04002427 int cpu, resched;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002428
Steven Rostedt033601a2008-11-21 12:41:55 -05002429 if (ring_buffer_flags != RB_BUFFERS_ON)
Steven Rostedta3583242008-11-11 15:01:42 -05002430 return -EBUSY;
2431
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002432 if (atomic_read(&buffer->record_disabled))
2433 return -EBUSY;
2434
Steven Rostedt182e9f52008-11-03 23:15:56 -05002435 resched = ftrace_preempt_disable();
Steven Rostedtbf41a152008-10-04 02:00:59 -04002436
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002437 cpu = raw_smp_processor_id();
2438
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302439 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedtd7690412008-10-01 00:29:53 -04002440 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002441
2442 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002443
2444 if (atomic_read(&cpu_buffer->record_disabled))
2445 goto out;
2446
Steven Rostedtbe957c42009-05-11 14:42:53 -04002447 if (length > BUF_MAX_DATA_SIZE)
2448 goto out;
2449
2450 event = rb_reserve_next_event(cpu_buffer, length);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002451 if (!event)
2452 goto out;
2453
2454 body = rb_event_data(event);
2455
2456 memcpy(body, data, length);
2457
2458 rb_commit(cpu_buffer, event);
2459
2460 ret = 0;
2461 out:
Steven Rostedt182e9f52008-11-03 23:15:56 -05002462 ftrace_preempt_enable(resched);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002463
2464 return ret;
2465}
Robert Richterc4f50182008-12-11 16:49:22 +01002466EXPORT_SYMBOL_GPL(ring_buffer_write);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002467
Andrew Morton34a148b2009-01-09 12:27:09 -08002468static int rb_per_cpu_empty(struct ring_buffer_per_cpu *cpu_buffer)
Steven Rostedtbf41a152008-10-04 02:00:59 -04002469{
2470 struct buffer_page *reader = cpu_buffer->reader_page;
Steven Rostedt77ae3652009-03-27 11:00:29 -04002471 struct buffer_page *head = rb_set_head_page(cpu_buffer);
Steven Rostedtbf41a152008-10-04 02:00:59 -04002472 struct buffer_page *commit = cpu_buffer->commit_page;
2473
Steven Rostedt77ae3652009-03-27 11:00:29 -04002474 /* In case of error, head will be NULL */
2475 if (unlikely(!head))
2476 return 1;
2477
Steven Rostedtbf41a152008-10-04 02:00:59 -04002478 return reader->read == rb_page_commit(reader) &&
2479 (commit == reader ||
2480 (commit == head &&
2481 head->read == rb_page_commit(commit)));
2482}
2483
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002484/**
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002485 * ring_buffer_record_disable - stop all writes into the buffer
2486 * @buffer: The ring buffer to stop writes to.
2487 *
2488 * This prevents all writes to the buffer. Any attempt to write
2489 * to the buffer after this will fail and return NULL.
2490 *
2491 * The caller should call synchronize_sched() after this.
2492 */
2493void ring_buffer_record_disable(struct ring_buffer *buffer)
2494{
2495 atomic_inc(&buffer->record_disabled);
2496}
Robert Richterc4f50182008-12-11 16:49:22 +01002497EXPORT_SYMBOL_GPL(ring_buffer_record_disable);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002498
2499/**
2500 * ring_buffer_record_enable - enable writes to the buffer
2501 * @buffer: The ring buffer to enable writes
2502 *
2503 * Note, multiple disables will need the same number of enables
2504 * to truely enable the writing (much like preempt_disable).
2505 */
2506void ring_buffer_record_enable(struct ring_buffer *buffer)
2507{
2508 atomic_dec(&buffer->record_disabled);
2509}
Robert Richterc4f50182008-12-11 16:49:22 +01002510EXPORT_SYMBOL_GPL(ring_buffer_record_enable);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002511
2512/**
2513 * ring_buffer_record_disable_cpu - stop all writes into the cpu_buffer
2514 * @buffer: The ring buffer to stop writes to.
2515 * @cpu: The CPU buffer to stop
2516 *
2517 * This prevents all writes to the buffer. Any attempt to write
2518 * to the buffer after this will fail and return NULL.
2519 *
2520 * The caller should call synchronize_sched() after this.
2521 */
2522void ring_buffer_record_disable_cpu(struct ring_buffer *buffer, int cpu)
2523{
2524 struct ring_buffer_per_cpu *cpu_buffer;
2525
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302526 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04002527 return;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002528
2529 cpu_buffer = buffer->buffers[cpu];
2530 atomic_inc(&cpu_buffer->record_disabled);
2531}
Robert Richterc4f50182008-12-11 16:49:22 +01002532EXPORT_SYMBOL_GPL(ring_buffer_record_disable_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002533
2534/**
2535 * ring_buffer_record_enable_cpu - enable writes to the buffer
2536 * @buffer: The ring buffer to enable writes
2537 * @cpu: The CPU to enable.
2538 *
2539 * Note, multiple disables will need the same number of enables
2540 * to truely enable the writing (much like preempt_disable).
2541 */
2542void ring_buffer_record_enable_cpu(struct ring_buffer *buffer, int cpu)
2543{
2544 struct ring_buffer_per_cpu *cpu_buffer;
2545
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302546 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04002547 return;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002548
2549 cpu_buffer = buffer->buffers[cpu];
2550 atomic_dec(&cpu_buffer->record_disabled);
2551}
Robert Richterc4f50182008-12-11 16:49:22 +01002552EXPORT_SYMBOL_GPL(ring_buffer_record_enable_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002553
2554/**
2555 * ring_buffer_entries_cpu - get the number of entries in a cpu buffer
2556 * @buffer: The ring buffer
2557 * @cpu: The per CPU buffer to get the entries from.
2558 */
2559unsigned long ring_buffer_entries_cpu(struct ring_buffer *buffer, int cpu)
2560{
2561 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedt8aabee52009-03-12 13:13:49 -04002562 unsigned long ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002563
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302564 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04002565 return 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002566
2567 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt77ae3652009-03-27 11:00:29 -04002568 ret = (local_read(&cpu_buffer->entries) - local_read(&cpu_buffer->overrun))
Steven Rostedte4906ef2009-04-30 20:49:44 -04002569 - cpu_buffer->read;
Steven Rostedt554f7862009-03-11 22:00:13 -04002570
2571 return ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002572}
Robert Richterc4f50182008-12-11 16:49:22 +01002573EXPORT_SYMBOL_GPL(ring_buffer_entries_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002574
2575/**
2576 * ring_buffer_overrun_cpu - get the number of overruns in a cpu_buffer
2577 * @buffer: The ring buffer
2578 * @cpu: The per CPU buffer to get the number of overruns from
2579 */
2580unsigned long ring_buffer_overrun_cpu(struct ring_buffer *buffer, int cpu)
2581{
2582 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedt8aabee52009-03-12 13:13:49 -04002583 unsigned long ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002584
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302585 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04002586 return 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002587
2588 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt77ae3652009-03-27 11:00:29 -04002589 ret = local_read(&cpu_buffer->overrun);
Steven Rostedt554f7862009-03-11 22:00:13 -04002590
2591 return ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002592}
Robert Richterc4f50182008-12-11 16:49:22 +01002593EXPORT_SYMBOL_GPL(ring_buffer_overrun_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002594
2595/**
Steven Rostedtf0d2c682009-04-29 13:43:37 -04002596 * ring_buffer_commit_overrun_cpu - get the number of overruns caused by commits
2597 * @buffer: The ring buffer
2598 * @cpu: The per CPU buffer to get the number of overruns from
2599 */
2600unsigned long
2601ring_buffer_commit_overrun_cpu(struct ring_buffer *buffer, int cpu)
2602{
2603 struct ring_buffer_per_cpu *cpu_buffer;
2604 unsigned long ret;
2605
2606 if (!cpumask_test_cpu(cpu, buffer->cpumask))
2607 return 0;
2608
2609 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt77ae3652009-03-27 11:00:29 -04002610 ret = local_read(&cpu_buffer->commit_overrun);
Steven Rostedtf0d2c682009-04-29 13:43:37 -04002611
2612 return ret;
2613}
2614EXPORT_SYMBOL_GPL(ring_buffer_commit_overrun_cpu);
2615
2616/**
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002617 * ring_buffer_entries - get the number of entries in a buffer
2618 * @buffer: The ring buffer
2619 *
2620 * Returns the total number of entries in the ring buffer
2621 * (all CPU entries)
2622 */
2623unsigned long ring_buffer_entries(struct ring_buffer *buffer)
2624{
2625 struct ring_buffer_per_cpu *cpu_buffer;
2626 unsigned long entries = 0;
2627 int cpu;
2628
2629 /* if you care about this being correct, lock the buffer */
2630 for_each_buffer_cpu(buffer, cpu) {
2631 cpu_buffer = buffer->buffers[cpu];
Steven Rostedte4906ef2009-04-30 20:49:44 -04002632 entries += (local_read(&cpu_buffer->entries) -
Steven Rostedt77ae3652009-03-27 11:00:29 -04002633 local_read(&cpu_buffer->overrun)) - cpu_buffer->read;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002634 }
2635
2636 return entries;
2637}
Robert Richterc4f50182008-12-11 16:49:22 +01002638EXPORT_SYMBOL_GPL(ring_buffer_entries);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002639
2640/**
2641 * ring_buffer_overrun_cpu - get the number of overruns in buffer
2642 * @buffer: The ring buffer
2643 *
2644 * Returns the total number of overruns in the ring buffer
2645 * (all CPU entries)
2646 */
2647unsigned long ring_buffer_overruns(struct ring_buffer *buffer)
2648{
2649 struct ring_buffer_per_cpu *cpu_buffer;
2650 unsigned long overruns = 0;
2651 int cpu;
2652
2653 /* if you care about this being correct, lock the buffer */
2654 for_each_buffer_cpu(buffer, cpu) {
2655 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt77ae3652009-03-27 11:00:29 -04002656 overruns += local_read(&cpu_buffer->overrun);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002657 }
2658
2659 return overruns;
2660}
Robert Richterc4f50182008-12-11 16:49:22 +01002661EXPORT_SYMBOL_GPL(ring_buffer_overruns);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002662
Steven Rostedt642edba2008-11-12 00:01:26 -05002663static void rb_iter_reset(struct ring_buffer_iter *iter)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002664{
2665 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
2666
Steven Rostedtd7690412008-10-01 00:29:53 -04002667 /* Iterator usage is expected to have record disabled */
2668 if (list_empty(&cpu_buffer->reader_page->list)) {
Steven Rostedt77ae3652009-03-27 11:00:29 -04002669 iter->head_page = rb_set_head_page(cpu_buffer);
2670 if (unlikely(!iter->head_page))
2671 return;
2672 iter->head = iter->head_page->read;
Steven Rostedtd7690412008-10-01 00:29:53 -04002673 } else {
2674 iter->head_page = cpu_buffer->reader_page;
Steven Rostedt6f807ac2008-10-04 02:00:58 -04002675 iter->head = cpu_buffer->reader_page->read;
Steven Rostedtd7690412008-10-01 00:29:53 -04002676 }
2677 if (iter->head)
2678 iter->read_stamp = cpu_buffer->read_stamp;
2679 else
Steven Rostedtabc9b562008-12-02 15:34:06 -05002680 iter->read_stamp = iter->head_page->page->time_stamp;
Steven Rostedt642edba2008-11-12 00:01:26 -05002681}
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002682
Steven Rostedt642edba2008-11-12 00:01:26 -05002683/**
2684 * ring_buffer_iter_reset - reset an iterator
2685 * @iter: The iterator to reset
2686 *
2687 * Resets the iterator, so that it will start from the beginning
2688 * again.
2689 */
2690void ring_buffer_iter_reset(struct ring_buffer_iter *iter)
2691{
Steven Rostedt554f7862009-03-11 22:00:13 -04002692 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedt642edba2008-11-12 00:01:26 -05002693 unsigned long flags;
2694
Steven Rostedt554f7862009-03-11 22:00:13 -04002695 if (!iter)
2696 return;
2697
2698 cpu_buffer = iter->cpu_buffer;
2699
Steven Rostedt642edba2008-11-12 00:01:26 -05002700 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
2701 rb_iter_reset(iter);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002702 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002703}
Robert Richterc4f50182008-12-11 16:49:22 +01002704EXPORT_SYMBOL_GPL(ring_buffer_iter_reset);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002705
2706/**
2707 * ring_buffer_iter_empty - check if an iterator has no more to read
2708 * @iter: The iterator to check
2709 */
2710int ring_buffer_iter_empty(struct ring_buffer_iter *iter)
2711{
2712 struct ring_buffer_per_cpu *cpu_buffer;
2713
2714 cpu_buffer = iter->cpu_buffer;
2715
Steven Rostedtbf41a152008-10-04 02:00:59 -04002716 return iter->head_page == cpu_buffer->commit_page &&
2717 iter->head == rb_commit_index(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002718}
Robert Richterc4f50182008-12-11 16:49:22 +01002719EXPORT_SYMBOL_GPL(ring_buffer_iter_empty);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002720
2721static void
2722rb_update_read_stamp(struct ring_buffer_per_cpu *cpu_buffer,
2723 struct ring_buffer_event *event)
2724{
2725 u64 delta;
2726
Lai Jiangshan334d4162009-04-24 11:27:05 +08002727 switch (event->type_len) {
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002728 case RINGBUF_TYPE_PADDING:
2729 return;
2730
2731 case RINGBUF_TYPE_TIME_EXTEND:
2732 delta = event->array[0];
2733 delta <<= TS_SHIFT;
2734 delta += event->time_delta;
2735 cpu_buffer->read_stamp += delta;
2736 return;
2737
2738 case RINGBUF_TYPE_TIME_STAMP:
2739 /* FIXME: not implemented */
2740 return;
2741
2742 case RINGBUF_TYPE_DATA:
2743 cpu_buffer->read_stamp += event->time_delta;
2744 return;
2745
2746 default:
2747 BUG();
2748 }
2749 return;
2750}
2751
2752static void
2753rb_update_iter_read_stamp(struct ring_buffer_iter *iter,
2754 struct ring_buffer_event *event)
2755{
2756 u64 delta;
2757
Lai Jiangshan334d4162009-04-24 11:27:05 +08002758 switch (event->type_len) {
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002759 case RINGBUF_TYPE_PADDING:
2760 return;
2761
2762 case RINGBUF_TYPE_TIME_EXTEND:
2763 delta = event->array[0];
2764 delta <<= TS_SHIFT;
2765 delta += event->time_delta;
2766 iter->read_stamp += delta;
2767 return;
2768
2769 case RINGBUF_TYPE_TIME_STAMP:
2770 /* FIXME: not implemented */
2771 return;
2772
2773 case RINGBUF_TYPE_DATA:
2774 iter->read_stamp += event->time_delta;
2775 return;
2776
2777 default:
2778 BUG();
2779 }
2780 return;
2781}
2782
Steven Rostedtd7690412008-10-01 00:29:53 -04002783static struct buffer_page *
2784rb_get_reader_page(struct ring_buffer_per_cpu *cpu_buffer)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002785{
Steven Rostedtd7690412008-10-01 00:29:53 -04002786 struct buffer_page *reader = NULL;
2787 unsigned long flags;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002788 int nr_loops = 0;
Steven Rostedt77ae3652009-03-27 11:00:29 -04002789 int ret;
Steven Rostedtd7690412008-10-01 00:29:53 -04002790
Steven Rostedt3e03fb72008-11-06 00:09:43 -05002791 local_irq_save(flags);
2792 __raw_spin_lock(&cpu_buffer->lock);
Steven Rostedtd7690412008-10-01 00:29:53 -04002793
2794 again:
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002795 /*
2796 * This should normally only loop twice. But because the
2797 * start of the reader inserts an empty page, it causes
2798 * a case where we will loop three times. There should be no
2799 * reason to loop four times (that I know of).
2800 */
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05002801 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 3)) {
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002802 reader = NULL;
2803 goto out;
2804 }
2805
Steven Rostedtd7690412008-10-01 00:29:53 -04002806 reader = cpu_buffer->reader_page;
2807
2808 /* If there's more to read, return this page */
Steven Rostedtbf41a152008-10-04 02:00:59 -04002809 if (cpu_buffer->reader_page->read < rb_page_size(reader))
Steven Rostedtd7690412008-10-01 00:29:53 -04002810 goto out;
2811
2812 /* Never should we have an index greater than the size */
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05002813 if (RB_WARN_ON(cpu_buffer,
2814 cpu_buffer->reader_page->read > rb_page_size(reader)))
2815 goto out;
Steven Rostedtd7690412008-10-01 00:29:53 -04002816
2817 /* check if we caught up to the tail */
2818 reader = NULL;
Steven Rostedtbf41a152008-10-04 02:00:59 -04002819 if (cpu_buffer->commit_page == cpu_buffer->reader_page)
Steven Rostedtd7690412008-10-01 00:29:53 -04002820 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002821
2822 /*
Steven Rostedtd7690412008-10-01 00:29:53 -04002823 * Reset the reader page to size zero.
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002824 */
Steven Rostedt77ae3652009-03-27 11:00:29 -04002825 local_set(&cpu_buffer->reader_page->write, 0);
2826 local_set(&cpu_buffer->reader_page->entries, 0);
2827 local_set(&cpu_buffer->reader_page->page->commit, 0);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002828
Steven Rostedt77ae3652009-03-27 11:00:29 -04002829 spin:
2830 /*
2831 * Splice the empty reader page into the list around the head.
2832 */
2833 reader = rb_set_head_page(cpu_buffer);
Steven Rostedtd7690412008-10-01 00:29:53 -04002834 cpu_buffer->reader_page->list.next = reader->list.next;
2835 cpu_buffer->reader_page->list.prev = reader->list.prev;
Steven Rostedtbf41a152008-10-04 02:00:59 -04002836
Steven Rostedt3adc54f2009-03-30 15:32:01 -04002837 /*
2838 * cpu_buffer->pages just needs to point to the buffer, it
2839 * has no specific buffer page to point to. Lets move it out
2840 * of our way so we don't accidently swap it.
2841 */
2842 cpu_buffer->pages = reader->list.prev;
2843
Steven Rostedt77ae3652009-03-27 11:00:29 -04002844 /* The reader page will be pointing to the new head */
2845 rb_set_list_to_head(cpu_buffer, &cpu_buffer->reader_page->list);
Steven Rostedtd7690412008-10-01 00:29:53 -04002846
2847 /*
Steven Rostedt77ae3652009-03-27 11:00:29 -04002848 * Here's the tricky part.
2849 *
2850 * We need to move the pointer past the header page.
2851 * But we can only do that if a writer is not currently
2852 * moving it. The page before the header page has the
2853 * flag bit '1' set if it is pointing to the page we want.
2854 * but if the writer is in the process of moving it
2855 * than it will be '2' or already moved '0'.
Steven Rostedtd7690412008-10-01 00:29:53 -04002856 */
Steven Rostedtd7690412008-10-01 00:29:53 -04002857
Steven Rostedt77ae3652009-03-27 11:00:29 -04002858 ret = rb_head_page_replace(reader, cpu_buffer->reader_page);
2859
2860 /*
2861 * If we did not convert it, then we must try again.
2862 */
2863 if (!ret)
2864 goto spin;
2865
2866 /*
2867 * Yeah! We succeeded in replacing the page.
2868 *
2869 * Now make the new head point back to the reader page.
2870 */
2871 reader->list.next->prev = &cpu_buffer->reader_page->list;
2872 rb_inc_page(cpu_buffer, &cpu_buffer->head_page);
Steven Rostedtd7690412008-10-01 00:29:53 -04002873
2874 /* Finally update the reader page to the new head */
2875 cpu_buffer->reader_page = reader;
2876 rb_reset_reader_page(cpu_buffer);
2877
2878 goto again;
2879
2880 out:
Steven Rostedt3e03fb72008-11-06 00:09:43 -05002881 __raw_spin_unlock(&cpu_buffer->lock);
2882 local_irq_restore(flags);
Steven Rostedtd7690412008-10-01 00:29:53 -04002883
2884 return reader;
2885}
2886
2887static void rb_advance_reader(struct ring_buffer_per_cpu *cpu_buffer)
2888{
2889 struct ring_buffer_event *event;
2890 struct buffer_page *reader;
2891 unsigned length;
2892
2893 reader = rb_get_reader_page(cpu_buffer);
2894
2895 /* This function should not be called when buffer is empty */
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05002896 if (RB_WARN_ON(cpu_buffer, !reader))
2897 return;
Steven Rostedtd7690412008-10-01 00:29:53 -04002898
2899 event = rb_reader_event(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002900
Lai Jiangshan334d4162009-04-24 11:27:05 +08002901 if (event->type_len <= RINGBUF_TYPE_DATA_TYPE_LEN_MAX
2902 || rb_discarded_event(event))
Steven Rostedte4906ef2009-04-30 20:49:44 -04002903 cpu_buffer->read++;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002904
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002905 rb_update_read_stamp(cpu_buffer, event);
2906
Steven Rostedtd7690412008-10-01 00:29:53 -04002907 length = rb_event_length(event);
Steven Rostedt6f807ac2008-10-04 02:00:58 -04002908 cpu_buffer->reader_page->read += length;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002909}
2910
2911static void rb_advance_iter(struct ring_buffer_iter *iter)
2912{
2913 struct ring_buffer *buffer;
2914 struct ring_buffer_per_cpu *cpu_buffer;
2915 struct ring_buffer_event *event;
2916 unsigned length;
2917
2918 cpu_buffer = iter->cpu_buffer;
2919 buffer = cpu_buffer->buffer;
2920
2921 /*
2922 * Check if we are at the end of the buffer.
2923 */
Steven Rostedtbf41a152008-10-04 02:00:59 -04002924 if (iter->head >= rb_page_size(iter->head_page)) {
Steven Rostedtea05b572009-06-03 09:30:10 -04002925 /* discarded commits can make the page empty */
2926 if (iter->head_page == cpu_buffer->commit_page)
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05002927 return;
Steven Rostedtd7690412008-10-01 00:29:53 -04002928 rb_inc_iter(iter);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002929 return;
2930 }
2931
2932 event = rb_iter_head_event(iter);
2933
2934 length = rb_event_length(event);
2935
2936 /*
2937 * This should not be called to advance the header if we are
2938 * at the tail of the buffer.
2939 */
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05002940 if (RB_WARN_ON(cpu_buffer,
Steven Rostedtf536aaf2008-11-10 23:07:30 -05002941 (iter->head_page == cpu_buffer->commit_page) &&
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05002942 (iter->head + length > rb_commit_index(cpu_buffer))))
2943 return;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002944
2945 rb_update_iter_read_stamp(iter, event);
2946
2947 iter->head += length;
2948
2949 /* check for end of page padding */
Steven Rostedtbf41a152008-10-04 02:00:59 -04002950 if ((iter->head >= rb_page_size(iter->head_page)) &&
2951 (iter->head_page != cpu_buffer->commit_page))
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002952 rb_advance_iter(iter);
2953}
2954
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002955static struct ring_buffer_event *
2956rb_buffer_peek(struct ring_buffer *buffer, int cpu, u64 *ts)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002957{
2958 struct ring_buffer_per_cpu *cpu_buffer;
2959 struct ring_buffer_event *event;
Steven Rostedtd7690412008-10-01 00:29:53 -04002960 struct buffer_page *reader;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002961 int nr_loops = 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002962
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002963 cpu_buffer = buffer->buffers[cpu];
2964
2965 again:
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002966 /*
2967 * We repeat when a timestamp is encountered. It is possible
2968 * to get multiple timestamps from an interrupt entering just
Steven Rostedtea05b572009-06-03 09:30:10 -04002969 * as one timestamp is about to be written, or from discarded
2970 * commits. The most that we can have is the number on a single page.
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002971 */
Steven Rostedtea05b572009-06-03 09:30:10 -04002972 if (RB_WARN_ON(cpu_buffer, ++nr_loops > RB_TIMESTAMPS_PER_PAGE))
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002973 return NULL;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002974
Steven Rostedtd7690412008-10-01 00:29:53 -04002975 reader = rb_get_reader_page(cpu_buffer);
2976 if (!reader)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002977 return NULL;
2978
Steven Rostedtd7690412008-10-01 00:29:53 -04002979 event = rb_reader_event(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002980
Lai Jiangshan334d4162009-04-24 11:27:05 +08002981 switch (event->type_len) {
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002982 case RINGBUF_TYPE_PADDING:
Tom Zanussi2d622712009-03-22 03:30:49 -05002983 if (rb_null_event(event))
2984 RB_WARN_ON(cpu_buffer, 1);
2985 /*
2986 * Because the writer could be discarding every
2987 * event it creates (which would probably be bad)
2988 * if we were to go back to "again" then we may never
2989 * catch up, and will trigger the warn on, or lock
2990 * the box. Return the padding, and we will release
2991 * the current locks, and try again.
2992 */
Steven Rostedtd7690412008-10-01 00:29:53 -04002993 rb_advance_reader(cpu_buffer);
Tom Zanussi2d622712009-03-22 03:30:49 -05002994 return event;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002995
2996 case RINGBUF_TYPE_TIME_EXTEND:
2997 /* Internal data, OK to advance */
Steven Rostedtd7690412008-10-01 00:29:53 -04002998 rb_advance_reader(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002999 goto again;
3000
3001 case RINGBUF_TYPE_TIME_STAMP:
3002 /* FIXME: not implemented */
Steven Rostedtd7690412008-10-01 00:29:53 -04003003 rb_advance_reader(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003004 goto again;
3005
3006 case RINGBUF_TYPE_DATA:
3007 if (ts) {
3008 *ts = cpu_buffer->read_stamp + event->time_delta;
Steven Rostedt37886f62009-03-17 17:22:06 -04003009 ring_buffer_normalize_time_stamp(buffer,
3010 cpu_buffer->cpu, ts);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003011 }
3012 return event;
3013
3014 default:
3015 BUG();
3016 }
3017
3018 return NULL;
3019}
Robert Richterc4f50182008-12-11 16:49:22 +01003020EXPORT_SYMBOL_GPL(ring_buffer_peek);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003021
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003022static struct ring_buffer_event *
3023rb_iter_peek(struct ring_buffer_iter *iter, u64 *ts)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003024{
3025 struct ring_buffer *buffer;
3026 struct ring_buffer_per_cpu *cpu_buffer;
3027 struct ring_buffer_event *event;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003028 int nr_loops = 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003029
3030 if (ring_buffer_iter_empty(iter))
3031 return NULL;
3032
3033 cpu_buffer = iter->cpu_buffer;
3034 buffer = cpu_buffer->buffer;
3035
3036 again:
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003037 /*
Steven Rostedtea05b572009-06-03 09:30:10 -04003038 * We repeat when a timestamp is encountered.
3039 * We can get multiple timestamps by nested interrupts or also
3040 * if filtering is on (discarding commits). Since discarding
3041 * commits can be frequent we can get a lot of timestamps.
3042 * But we limit them by not adding timestamps if they begin
3043 * at the start of a page.
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003044 */
Steven Rostedtea05b572009-06-03 09:30:10 -04003045 if (RB_WARN_ON(cpu_buffer, ++nr_loops > RB_TIMESTAMPS_PER_PAGE))
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003046 return NULL;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003047
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003048 if (rb_per_cpu_empty(cpu_buffer))
3049 return NULL;
3050
3051 event = rb_iter_head_event(iter);
3052
Lai Jiangshan334d4162009-04-24 11:27:05 +08003053 switch (event->type_len) {
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003054 case RINGBUF_TYPE_PADDING:
Tom Zanussi2d622712009-03-22 03:30:49 -05003055 if (rb_null_event(event)) {
3056 rb_inc_iter(iter);
3057 goto again;
3058 }
3059 rb_advance_iter(iter);
3060 return event;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003061
3062 case RINGBUF_TYPE_TIME_EXTEND:
3063 /* Internal data, OK to advance */
3064 rb_advance_iter(iter);
3065 goto again;
3066
3067 case RINGBUF_TYPE_TIME_STAMP:
3068 /* FIXME: not implemented */
3069 rb_advance_iter(iter);
3070 goto again;
3071
3072 case RINGBUF_TYPE_DATA:
3073 if (ts) {
3074 *ts = iter->read_stamp + event->time_delta;
Steven Rostedt37886f62009-03-17 17:22:06 -04003075 ring_buffer_normalize_time_stamp(buffer,
3076 cpu_buffer->cpu, ts);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003077 }
3078 return event;
3079
3080 default:
3081 BUG();
3082 }
3083
3084 return NULL;
3085}
Robert Richterc4f50182008-12-11 16:49:22 +01003086EXPORT_SYMBOL_GPL(ring_buffer_iter_peek);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003087
Steven Rostedt8d707e82009-06-16 21:22:48 -04003088static inline int rb_ok_to_lock(void)
3089{
3090 /*
3091 * If an NMI die dumps out the content of the ring buffer
3092 * do not grab locks. We also permanently disable the ring
3093 * buffer too. A one time deal is all you get from reading
3094 * the ring buffer from an NMI.
3095 */
3096 if (likely(!in_nmi() && !oops_in_progress))
3097 return 1;
3098
3099 tracing_off_permanent();
3100 return 0;
3101}
3102
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003103/**
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003104 * ring_buffer_peek - peek at the next event to be read
3105 * @buffer: The ring buffer to read
3106 * @cpu: The cpu to peak at
3107 * @ts: The timestamp counter of this event.
3108 *
3109 * This will return the event that will be read next, but does
3110 * not consume the data.
3111 */
3112struct ring_buffer_event *
3113ring_buffer_peek(struct ring_buffer *buffer, int cpu, u64 *ts)
3114{
3115 struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
Steven Rostedt8aabee52009-03-12 13:13:49 -04003116 struct ring_buffer_event *event;
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003117 unsigned long flags;
Steven Rostedt8d707e82009-06-16 21:22:48 -04003118 int dolock;
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003119
Steven Rostedt554f7862009-03-11 22:00:13 -04003120 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04003121 return NULL;
Steven Rostedt554f7862009-03-11 22:00:13 -04003122
Steven Rostedt8d707e82009-06-16 21:22:48 -04003123 dolock = rb_ok_to_lock();
Tom Zanussi2d622712009-03-22 03:30:49 -05003124 again:
Steven Rostedt8d707e82009-06-16 21:22:48 -04003125 local_irq_save(flags);
3126 if (dolock)
3127 spin_lock(&cpu_buffer->reader_lock);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003128 event = rb_buffer_peek(buffer, cpu, ts);
Steven Rostedt8d707e82009-06-16 21:22:48 -04003129 if (dolock)
3130 spin_unlock(&cpu_buffer->reader_lock);
3131 local_irq_restore(flags);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003132
Lai Jiangshan334d4162009-04-24 11:27:05 +08003133 if (event && event->type_len == RINGBUF_TYPE_PADDING) {
Tom Zanussi2d622712009-03-22 03:30:49 -05003134 cpu_relax();
3135 goto again;
3136 }
3137
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003138 return event;
3139}
3140
3141/**
3142 * ring_buffer_iter_peek - peek at the next event to be read
3143 * @iter: The ring buffer iterator
3144 * @ts: The timestamp counter of this event.
3145 *
3146 * This will return the event that will be read next, but does
3147 * not increment the iterator.
3148 */
3149struct ring_buffer_event *
3150ring_buffer_iter_peek(struct ring_buffer_iter *iter, u64 *ts)
3151{
3152 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
3153 struct ring_buffer_event *event;
3154 unsigned long flags;
3155
Tom Zanussi2d622712009-03-22 03:30:49 -05003156 again:
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003157 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
3158 event = rb_iter_peek(iter, ts);
3159 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
3160
Lai Jiangshan334d4162009-04-24 11:27:05 +08003161 if (event && event->type_len == RINGBUF_TYPE_PADDING) {
Tom Zanussi2d622712009-03-22 03:30:49 -05003162 cpu_relax();
3163 goto again;
3164 }
3165
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003166 return event;
3167}
3168
3169/**
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003170 * ring_buffer_consume - return an event and consume it
3171 * @buffer: The ring buffer to get the next event from
3172 *
3173 * Returns the next event in the ring buffer, and that event is consumed.
3174 * Meaning, that sequential reads will keep returning a different event,
3175 * and eventually empty the ring buffer if the producer is slower.
3176 */
3177struct ring_buffer_event *
3178ring_buffer_consume(struct ring_buffer *buffer, int cpu, u64 *ts)
3179{
Steven Rostedt554f7862009-03-11 22:00:13 -04003180 struct ring_buffer_per_cpu *cpu_buffer;
3181 struct ring_buffer_event *event = NULL;
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003182 unsigned long flags;
Steven Rostedt8d707e82009-06-16 21:22:48 -04003183 int dolock;
3184
3185 dolock = rb_ok_to_lock();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003186
Tom Zanussi2d622712009-03-22 03:30:49 -05003187 again:
Steven Rostedt554f7862009-03-11 22:00:13 -04003188 /* might be called in atomic */
3189 preempt_disable();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003190
Steven Rostedt554f7862009-03-11 22:00:13 -04003191 if (!cpumask_test_cpu(cpu, buffer->cpumask))
3192 goto out;
3193
3194 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt8d707e82009-06-16 21:22:48 -04003195 local_irq_save(flags);
3196 if (dolock)
3197 spin_lock(&cpu_buffer->reader_lock);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003198
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003199 event = rb_buffer_peek(buffer, cpu, ts);
3200 if (!event)
Steven Rostedt554f7862009-03-11 22:00:13 -04003201 goto out_unlock;
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003202
Steven Rostedtd7690412008-10-01 00:29:53 -04003203 rb_advance_reader(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003204
Steven Rostedt554f7862009-03-11 22:00:13 -04003205 out_unlock:
Steven Rostedt8d707e82009-06-16 21:22:48 -04003206 if (dolock)
3207 spin_unlock(&cpu_buffer->reader_lock);
3208 local_irq_restore(flags);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003209
Steven Rostedt554f7862009-03-11 22:00:13 -04003210 out:
3211 preempt_enable();
3212
Lai Jiangshan334d4162009-04-24 11:27:05 +08003213 if (event && event->type_len == RINGBUF_TYPE_PADDING) {
Tom Zanussi2d622712009-03-22 03:30:49 -05003214 cpu_relax();
3215 goto again;
3216 }
3217
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003218 return event;
3219}
Robert Richterc4f50182008-12-11 16:49:22 +01003220EXPORT_SYMBOL_GPL(ring_buffer_consume);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003221
3222/**
3223 * ring_buffer_read_start - start a non consuming read of the buffer
3224 * @buffer: The ring buffer to read from
3225 * @cpu: The cpu buffer to iterate over
3226 *
3227 * This starts up an iteration through the buffer. It also disables
3228 * the recording to the buffer until the reading is finished.
3229 * This prevents the reading from being corrupted. This is not
3230 * a consuming read, so a producer is not expected.
3231 *
3232 * Must be paired with ring_buffer_finish.
3233 */
3234struct ring_buffer_iter *
3235ring_buffer_read_start(struct ring_buffer *buffer, int cpu)
3236{
3237 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedt8aabee52009-03-12 13:13:49 -04003238 struct ring_buffer_iter *iter;
Steven Rostedtd7690412008-10-01 00:29:53 -04003239 unsigned long flags;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003240
Rusty Russell9e01c1b2009-01-01 10:12:22 +10303241 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04003242 return NULL;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003243
3244 iter = kmalloc(sizeof(*iter), GFP_KERNEL);
3245 if (!iter)
Steven Rostedt8aabee52009-03-12 13:13:49 -04003246 return NULL;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003247
3248 cpu_buffer = buffer->buffers[cpu];
3249
3250 iter->cpu_buffer = cpu_buffer;
3251
3252 atomic_inc(&cpu_buffer->record_disabled);
3253 synchronize_sched();
3254
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003255 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
Steven Rostedt3e03fb72008-11-06 00:09:43 -05003256 __raw_spin_lock(&cpu_buffer->lock);
Steven Rostedt642edba2008-11-12 00:01:26 -05003257 rb_iter_reset(iter);
Steven Rostedt3e03fb72008-11-06 00:09:43 -05003258 __raw_spin_unlock(&cpu_buffer->lock);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003259 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003260
3261 return iter;
3262}
Robert Richterc4f50182008-12-11 16:49:22 +01003263EXPORT_SYMBOL_GPL(ring_buffer_read_start);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003264
3265/**
3266 * ring_buffer_finish - finish reading the iterator of the buffer
3267 * @iter: The iterator retrieved by ring_buffer_start
3268 *
3269 * This re-enables the recording to the buffer, and frees the
3270 * iterator.
3271 */
3272void
3273ring_buffer_read_finish(struct ring_buffer_iter *iter)
3274{
3275 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
3276
3277 atomic_dec(&cpu_buffer->record_disabled);
3278 kfree(iter);
3279}
Robert Richterc4f50182008-12-11 16:49:22 +01003280EXPORT_SYMBOL_GPL(ring_buffer_read_finish);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003281
3282/**
3283 * ring_buffer_read - read the next item in the ring buffer by the iterator
3284 * @iter: The ring buffer iterator
3285 * @ts: The time stamp of the event read.
3286 *
3287 * This reads the next event in the ring buffer and increments the iterator.
3288 */
3289struct ring_buffer_event *
3290ring_buffer_read(struct ring_buffer_iter *iter, u64 *ts)
3291{
3292 struct ring_buffer_event *event;
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003293 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
3294 unsigned long flags;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003295
Tom Zanussi2d622712009-03-22 03:30:49 -05003296 again:
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003297 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
3298 event = rb_iter_peek(iter, ts);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003299 if (!event)
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003300 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003301
3302 rb_advance_iter(iter);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003303 out:
3304 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003305
Lai Jiangshan334d4162009-04-24 11:27:05 +08003306 if (event && event->type_len == RINGBUF_TYPE_PADDING) {
Tom Zanussi2d622712009-03-22 03:30:49 -05003307 cpu_relax();
3308 goto again;
3309 }
3310
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003311 return event;
3312}
Robert Richterc4f50182008-12-11 16:49:22 +01003313EXPORT_SYMBOL_GPL(ring_buffer_read);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003314
3315/**
3316 * ring_buffer_size - return the size of the ring buffer (in bytes)
3317 * @buffer: The ring buffer.
3318 */
3319unsigned long ring_buffer_size(struct ring_buffer *buffer)
3320{
3321 return BUF_PAGE_SIZE * buffer->pages;
3322}
Robert Richterc4f50182008-12-11 16:49:22 +01003323EXPORT_SYMBOL_GPL(ring_buffer_size);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003324
3325static void
3326rb_reset_cpu(struct ring_buffer_per_cpu *cpu_buffer)
3327{
Steven Rostedt77ae3652009-03-27 11:00:29 -04003328 rb_head_page_deactivate(cpu_buffer);
3329
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003330 cpu_buffer->head_page
Steven Rostedt3adc54f2009-03-30 15:32:01 -04003331 = list_entry(cpu_buffer->pages, struct buffer_page, list);
Steven Rostedtbf41a152008-10-04 02:00:59 -04003332 local_set(&cpu_buffer->head_page->write, 0);
Steven Rostedt778c55d2009-05-01 18:44:45 -04003333 local_set(&cpu_buffer->head_page->entries, 0);
Steven Rostedtabc9b562008-12-02 15:34:06 -05003334 local_set(&cpu_buffer->head_page->page->commit, 0);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003335
Steven Rostedt6f807ac2008-10-04 02:00:58 -04003336 cpu_buffer->head_page->read = 0;
Steven Rostedtbf41a152008-10-04 02:00:59 -04003337
3338 cpu_buffer->tail_page = cpu_buffer->head_page;
3339 cpu_buffer->commit_page = cpu_buffer->head_page;
3340
3341 INIT_LIST_HEAD(&cpu_buffer->reader_page->list);
3342 local_set(&cpu_buffer->reader_page->write, 0);
Steven Rostedt778c55d2009-05-01 18:44:45 -04003343 local_set(&cpu_buffer->reader_page->entries, 0);
Steven Rostedtabc9b562008-12-02 15:34:06 -05003344 local_set(&cpu_buffer->reader_page->page->commit, 0);
Steven Rostedt6f807ac2008-10-04 02:00:58 -04003345 cpu_buffer->reader_page->read = 0;
Steven Rostedtd7690412008-10-01 00:29:53 -04003346
Steven Rostedt77ae3652009-03-27 11:00:29 -04003347 local_set(&cpu_buffer->commit_overrun, 0);
3348 local_set(&cpu_buffer->overrun, 0);
Steven Rostedte4906ef2009-04-30 20:49:44 -04003349 local_set(&cpu_buffer->entries, 0);
Steven Rostedtfa743952009-06-16 12:37:57 -04003350 local_set(&cpu_buffer->committing, 0);
3351 local_set(&cpu_buffer->commits, 0);
Steven Rostedt77ae3652009-03-27 11:00:29 -04003352 cpu_buffer->read = 0;
Steven Rostedt69507c02009-01-21 18:45:57 -05003353
3354 cpu_buffer->write_stamp = 0;
3355 cpu_buffer->read_stamp = 0;
Steven Rostedt77ae3652009-03-27 11:00:29 -04003356
3357 rb_head_page_activate(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003358}
3359
3360/**
3361 * ring_buffer_reset_cpu - reset a ring buffer per CPU buffer
3362 * @buffer: The ring buffer to reset a per cpu buffer of
3363 * @cpu: The CPU buffer to be reset
3364 */
3365void ring_buffer_reset_cpu(struct ring_buffer *buffer, int cpu)
3366{
3367 struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
3368 unsigned long flags;
3369
Rusty Russell9e01c1b2009-01-01 10:12:22 +10303370 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04003371 return;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003372
Steven Rostedt41ede232009-05-01 20:26:54 -04003373 atomic_inc(&cpu_buffer->record_disabled);
3374
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003375 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
3376
Steven Rostedt3e03fb72008-11-06 00:09:43 -05003377 __raw_spin_lock(&cpu_buffer->lock);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003378
3379 rb_reset_cpu(cpu_buffer);
3380
Steven Rostedt3e03fb72008-11-06 00:09:43 -05003381 __raw_spin_unlock(&cpu_buffer->lock);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003382
3383 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
Steven Rostedt41ede232009-05-01 20:26:54 -04003384
3385 atomic_dec(&cpu_buffer->record_disabled);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003386}
Robert Richterc4f50182008-12-11 16:49:22 +01003387EXPORT_SYMBOL_GPL(ring_buffer_reset_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003388
3389/**
3390 * ring_buffer_reset - reset a ring buffer
3391 * @buffer: The ring buffer to reset all cpu buffers
3392 */
3393void ring_buffer_reset(struct ring_buffer *buffer)
3394{
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003395 int cpu;
3396
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003397 for_each_buffer_cpu(buffer, cpu)
Steven Rostedtd7690412008-10-01 00:29:53 -04003398 ring_buffer_reset_cpu(buffer, cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003399}
Robert Richterc4f50182008-12-11 16:49:22 +01003400EXPORT_SYMBOL_GPL(ring_buffer_reset);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003401
3402/**
3403 * rind_buffer_empty - is the ring buffer empty?
3404 * @buffer: The ring buffer to test
3405 */
3406int ring_buffer_empty(struct ring_buffer *buffer)
3407{
3408 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedtd4788202009-06-17 00:39:43 -04003409 unsigned long flags;
Steven Rostedt8d707e82009-06-16 21:22:48 -04003410 int dolock;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003411 int cpu;
Steven Rostedtd4788202009-06-17 00:39:43 -04003412 int ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003413
Steven Rostedt8d707e82009-06-16 21:22:48 -04003414 dolock = rb_ok_to_lock();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003415
3416 /* yes this is racy, but if you don't like the race, lock the buffer */
3417 for_each_buffer_cpu(buffer, cpu) {
3418 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt8d707e82009-06-16 21:22:48 -04003419 local_irq_save(flags);
3420 if (dolock)
3421 spin_lock(&cpu_buffer->reader_lock);
Steven Rostedtd4788202009-06-17 00:39:43 -04003422 ret = rb_per_cpu_empty(cpu_buffer);
Steven Rostedt8d707e82009-06-16 21:22:48 -04003423 if (dolock)
3424 spin_unlock(&cpu_buffer->reader_lock);
3425 local_irq_restore(flags);
3426
Steven Rostedtd4788202009-06-17 00:39:43 -04003427 if (!ret)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003428 return 0;
3429 }
Steven Rostedt554f7862009-03-11 22:00:13 -04003430
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003431 return 1;
3432}
Robert Richterc4f50182008-12-11 16:49:22 +01003433EXPORT_SYMBOL_GPL(ring_buffer_empty);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003434
3435/**
3436 * ring_buffer_empty_cpu - is a cpu buffer of a ring buffer empty?
3437 * @buffer: The ring buffer
3438 * @cpu: The CPU buffer to test
3439 */
3440int ring_buffer_empty_cpu(struct ring_buffer *buffer, int cpu)
3441{
3442 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedtd4788202009-06-17 00:39:43 -04003443 unsigned long flags;
Steven Rostedt8d707e82009-06-16 21:22:48 -04003444 int dolock;
Steven Rostedt8aabee52009-03-12 13:13:49 -04003445 int ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003446
Rusty Russell9e01c1b2009-01-01 10:12:22 +10303447 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04003448 return 1;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003449
Steven Rostedt8d707e82009-06-16 21:22:48 -04003450 dolock = rb_ok_to_lock();
Steven Rostedt554f7862009-03-11 22:00:13 -04003451
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003452 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt8d707e82009-06-16 21:22:48 -04003453 local_irq_save(flags);
3454 if (dolock)
3455 spin_lock(&cpu_buffer->reader_lock);
Steven Rostedt554f7862009-03-11 22:00:13 -04003456 ret = rb_per_cpu_empty(cpu_buffer);
Steven Rostedt8d707e82009-06-16 21:22:48 -04003457 if (dolock)
3458 spin_unlock(&cpu_buffer->reader_lock);
3459 local_irq_restore(flags);
Steven Rostedt554f7862009-03-11 22:00:13 -04003460
3461 return ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003462}
Robert Richterc4f50182008-12-11 16:49:22 +01003463EXPORT_SYMBOL_GPL(ring_buffer_empty_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003464
3465/**
3466 * ring_buffer_swap_cpu - swap a CPU buffer between two ring buffers
3467 * @buffer_a: One buffer to swap with
3468 * @buffer_b: The other buffer to swap with
3469 *
3470 * This function is useful for tracers that want to take a "snapshot"
3471 * of a CPU buffer and has another back up buffer lying around.
3472 * it is expected that the tracer handles the cpu buffer not being
3473 * used at the moment.
3474 */
3475int ring_buffer_swap_cpu(struct ring_buffer *buffer_a,
3476 struct ring_buffer *buffer_b, int cpu)
3477{
3478 struct ring_buffer_per_cpu *cpu_buffer_a;
3479 struct ring_buffer_per_cpu *cpu_buffer_b;
Steven Rostedt554f7862009-03-11 22:00:13 -04003480 int ret = -EINVAL;
3481
Rusty Russell9e01c1b2009-01-01 10:12:22 +10303482 if (!cpumask_test_cpu(cpu, buffer_a->cpumask) ||
3483 !cpumask_test_cpu(cpu, buffer_b->cpumask))
Steven Rostedt554f7862009-03-11 22:00:13 -04003484 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003485
3486 /* At least make sure the two buffers are somewhat the same */
Lai Jiangshan6d102bc2008-12-17 17:48:23 +08003487 if (buffer_a->pages != buffer_b->pages)
Steven Rostedt554f7862009-03-11 22:00:13 -04003488 goto out;
3489
3490 ret = -EAGAIN;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003491
Steven Rostedt97b17ef2009-01-21 15:24:56 -05003492 if (ring_buffer_flags != RB_BUFFERS_ON)
Steven Rostedt554f7862009-03-11 22:00:13 -04003493 goto out;
Steven Rostedt97b17ef2009-01-21 15:24:56 -05003494
3495 if (atomic_read(&buffer_a->record_disabled))
Steven Rostedt554f7862009-03-11 22:00:13 -04003496 goto out;
Steven Rostedt97b17ef2009-01-21 15:24:56 -05003497
3498 if (atomic_read(&buffer_b->record_disabled))
Steven Rostedt554f7862009-03-11 22:00:13 -04003499 goto out;
Steven Rostedt97b17ef2009-01-21 15:24:56 -05003500
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003501 cpu_buffer_a = buffer_a->buffers[cpu];
3502 cpu_buffer_b = buffer_b->buffers[cpu];
3503
Steven Rostedt97b17ef2009-01-21 15:24:56 -05003504 if (atomic_read(&cpu_buffer_a->record_disabled))
Steven Rostedt554f7862009-03-11 22:00:13 -04003505 goto out;
Steven Rostedt97b17ef2009-01-21 15:24:56 -05003506
3507 if (atomic_read(&cpu_buffer_b->record_disabled))
Steven Rostedt554f7862009-03-11 22:00:13 -04003508 goto out;
Steven Rostedt97b17ef2009-01-21 15:24:56 -05003509
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003510 /*
3511 * We can't do a synchronize_sched here because this
3512 * function can be called in atomic context.
3513 * Normally this will be called from the same CPU as cpu.
3514 * If not it's up to the caller to protect this.
3515 */
3516 atomic_inc(&cpu_buffer_a->record_disabled);
3517 atomic_inc(&cpu_buffer_b->record_disabled);
3518
3519 buffer_a->buffers[cpu] = cpu_buffer_b;
3520 buffer_b->buffers[cpu] = cpu_buffer_a;
3521
3522 cpu_buffer_b->buffer = buffer_a;
3523 cpu_buffer_a->buffer = buffer_b;
3524
3525 atomic_dec(&cpu_buffer_a->record_disabled);
3526 atomic_dec(&cpu_buffer_b->record_disabled);
3527
Steven Rostedt554f7862009-03-11 22:00:13 -04003528 ret = 0;
3529out:
Steven Rostedt554f7862009-03-11 22:00:13 -04003530 return ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003531}
Robert Richterc4f50182008-12-11 16:49:22 +01003532EXPORT_SYMBOL_GPL(ring_buffer_swap_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003533
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003534/**
3535 * ring_buffer_alloc_read_page - allocate a page to read from buffer
3536 * @buffer: the buffer to allocate for.
3537 *
3538 * This function is used in conjunction with ring_buffer_read_page.
3539 * When reading a full page from the ring buffer, these functions
3540 * can be used to speed up the process. The calling function should
3541 * allocate a few pages first with this function. Then when it
3542 * needs to get pages from the ring buffer, it passes the result
3543 * of this function into ring_buffer_read_page, which will swap
3544 * the page that was allocated, with the read page of the buffer.
3545 *
3546 * Returns:
3547 * The page allocated, or NULL on error.
3548 */
3549void *ring_buffer_alloc_read_page(struct ring_buffer *buffer)
3550{
Steven Rostedt044fa782008-12-02 23:50:03 -05003551 struct buffer_data_page *bpage;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003552 unsigned long addr;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003553
3554 addr = __get_free_page(GFP_KERNEL);
3555 if (!addr)
3556 return NULL;
3557
Steven Rostedt044fa782008-12-02 23:50:03 -05003558 bpage = (void *)addr;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003559
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003560 rb_init_page(bpage);
3561
Steven Rostedt044fa782008-12-02 23:50:03 -05003562 return bpage;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003563}
Steven Rostedtd6ce96d2009-05-05 01:15:24 -04003564EXPORT_SYMBOL_GPL(ring_buffer_alloc_read_page);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003565
3566/**
3567 * ring_buffer_free_read_page - free an allocated read page
3568 * @buffer: the buffer the page was allocate for
3569 * @data: the page to free
3570 *
3571 * Free a page allocated from ring_buffer_alloc_read_page.
3572 */
3573void ring_buffer_free_read_page(struct ring_buffer *buffer, void *data)
3574{
3575 free_page((unsigned long)data);
3576}
Steven Rostedtd6ce96d2009-05-05 01:15:24 -04003577EXPORT_SYMBOL_GPL(ring_buffer_free_read_page);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003578
3579/**
3580 * ring_buffer_read_page - extract a page from the ring buffer
3581 * @buffer: buffer to extract from
3582 * @data_page: the page to use allocated from ring_buffer_alloc_read_page
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003583 * @len: amount to extract
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003584 * @cpu: the cpu of the buffer to extract
3585 * @full: should the extraction only happen when the page is full.
3586 *
3587 * This function will pull out a page from the ring buffer and consume it.
3588 * @data_page must be the address of the variable that was returned
3589 * from ring_buffer_alloc_read_page. This is because the page might be used
3590 * to swap with a page in the ring buffer.
3591 *
3592 * for example:
Lai Jiangshanb85fa012009-02-09 14:21:14 +08003593 * rpage = ring_buffer_alloc_read_page(buffer);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003594 * if (!rpage)
3595 * return error;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003596 * ret = ring_buffer_read_page(buffer, &rpage, len, cpu, 0);
Lai Jiangshan667d2412009-02-09 14:21:17 +08003597 * if (ret >= 0)
3598 * process_page(rpage, ret);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003599 *
3600 * When @full is set, the function will not return true unless
3601 * the writer is off the reader page.
3602 *
3603 * Note: it is up to the calling functions to handle sleeps and wakeups.
3604 * The ring buffer can be used anywhere in the kernel and can not
3605 * blindly call wake_up. The layer that uses the ring buffer must be
3606 * responsible for that.
3607 *
3608 * Returns:
Lai Jiangshan667d2412009-02-09 14:21:17 +08003609 * >=0 if data has been transferred, returns the offset of consumed data.
3610 * <0 if no data has been transferred.
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003611 */
3612int ring_buffer_read_page(struct ring_buffer *buffer,
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003613 void **data_page, size_t len, int cpu, int full)
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003614{
3615 struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
3616 struct ring_buffer_event *event;
Steven Rostedt044fa782008-12-02 23:50:03 -05003617 struct buffer_data_page *bpage;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003618 struct buffer_page *reader;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003619 unsigned long flags;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003620 unsigned int commit;
Lai Jiangshan667d2412009-02-09 14:21:17 +08003621 unsigned int read;
Steven Rostedt4f3640f2009-03-03 23:52:42 -05003622 u64 save_timestamp;
Lai Jiangshan667d2412009-02-09 14:21:17 +08003623 int ret = -1;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003624
Steven Rostedt554f7862009-03-11 22:00:13 -04003625 if (!cpumask_test_cpu(cpu, buffer->cpumask))
3626 goto out;
3627
Steven Rostedt474d32b2009-03-03 19:51:40 -05003628 /*
3629 * If len is not big enough to hold the page header, then
3630 * we can not copy anything.
3631 */
3632 if (len <= BUF_PAGE_HDR_SIZE)
Steven Rostedt554f7862009-03-11 22:00:13 -04003633 goto out;
Steven Rostedt474d32b2009-03-03 19:51:40 -05003634
3635 len -= BUF_PAGE_HDR_SIZE;
3636
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003637 if (!data_page)
Steven Rostedt554f7862009-03-11 22:00:13 -04003638 goto out;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003639
Steven Rostedt044fa782008-12-02 23:50:03 -05003640 bpage = *data_page;
3641 if (!bpage)
Steven Rostedt554f7862009-03-11 22:00:13 -04003642 goto out;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003643
3644 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
3645
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003646 reader = rb_get_reader_page(cpu_buffer);
3647 if (!reader)
Steven Rostedt554f7862009-03-11 22:00:13 -04003648 goto out_unlock;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003649
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003650 event = rb_reader_event(cpu_buffer);
Lai Jiangshan667d2412009-02-09 14:21:17 +08003651
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003652 read = reader->read;
3653 commit = rb_page_commit(reader);
3654
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003655 /*
Steven Rostedt474d32b2009-03-03 19:51:40 -05003656 * If this page has been partially read or
3657 * if len is not big enough to read the rest of the page or
3658 * a writer is still on the page, then
3659 * we must copy the data from the page to the buffer.
3660 * Otherwise, we can simply swap the page with the one passed in.
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003661 */
Steven Rostedt474d32b2009-03-03 19:51:40 -05003662 if (read || (len < (commit - read)) ||
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003663 cpu_buffer->reader_page == cpu_buffer->commit_page) {
Lai Jiangshan667d2412009-02-09 14:21:17 +08003664 struct buffer_data_page *rpage = cpu_buffer->reader_page->page;
Steven Rostedt474d32b2009-03-03 19:51:40 -05003665 unsigned int rpos = read;
3666 unsigned int pos = 0;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003667 unsigned int size;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003668
3669 if (full)
Steven Rostedt554f7862009-03-11 22:00:13 -04003670 goto out_unlock;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003671
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003672 if (len > (commit - read))
3673 len = (commit - read);
3674
3675 size = rb_event_length(event);
3676
3677 if (len < size)
Steven Rostedt554f7862009-03-11 22:00:13 -04003678 goto out_unlock;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003679
Steven Rostedt4f3640f2009-03-03 23:52:42 -05003680 /* save the current timestamp, since the user will need it */
3681 save_timestamp = cpu_buffer->read_stamp;
3682
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003683 /* Need to copy one event at a time */
3684 do {
Steven Rostedt474d32b2009-03-03 19:51:40 -05003685 memcpy(bpage->data + pos, rpage->data + rpos, size);
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003686
3687 len -= size;
3688
3689 rb_advance_reader(cpu_buffer);
Steven Rostedt474d32b2009-03-03 19:51:40 -05003690 rpos = reader->read;
3691 pos += size;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003692
3693 event = rb_reader_event(cpu_buffer);
3694 size = rb_event_length(event);
3695 } while (len > size);
Lai Jiangshan667d2412009-02-09 14:21:17 +08003696
3697 /* update bpage */
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003698 local_set(&bpage->commit, pos);
Steven Rostedt4f3640f2009-03-03 23:52:42 -05003699 bpage->time_stamp = save_timestamp;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003700
Steven Rostedt474d32b2009-03-03 19:51:40 -05003701 /* we copied everything to the beginning */
3702 read = 0;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003703 } else {
Steven Rostedtafbab762009-05-01 19:40:05 -04003704 /* update the entry counter */
Steven Rostedt77ae3652009-03-27 11:00:29 -04003705 cpu_buffer->read += rb_page_entries(reader);
Steven Rostedtafbab762009-05-01 19:40:05 -04003706
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003707 /* swap the pages */
Steven Rostedt044fa782008-12-02 23:50:03 -05003708 rb_init_page(bpage);
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003709 bpage = reader->page;
3710 reader->page = *data_page;
3711 local_set(&reader->write, 0);
Steven Rostedt778c55d2009-05-01 18:44:45 -04003712 local_set(&reader->entries, 0);
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003713 reader->read = 0;
Steven Rostedt044fa782008-12-02 23:50:03 -05003714 *data_page = bpage;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003715 }
Lai Jiangshan667d2412009-02-09 14:21:17 +08003716 ret = read;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003717
Steven Rostedt554f7862009-03-11 22:00:13 -04003718 out_unlock:
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003719 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
3720
Steven Rostedt554f7862009-03-11 22:00:13 -04003721 out:
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003722 return ret;
3723}
Steven Rostedtd6ce96d2009-05-05 01:15:24 -04003724EXPORT_SYMBOL_GPL(ring_buffer_read_page);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003725
Paul Mundt1155de42009-06-25 14:30:12 +09003726#ifdef CONFIG_TRACING
Steven Rostedta3583242008-11-11 15:01:42 -05003727static ssize_t
3728rb_simple_read(struct file *filp, char __user *ubuf,
3729 size_t cnt, loff_t *ppos)
3730{
Hannes Eder5e398412009-02-10 19:44:34 +01003731 unsigned long *p = filp->private_data;
Steven Rostedta3583242008-11-11 15:01:42 -05003732 char buf[64];
3733 int r;
3734
Steven Rostedt033601a2008-11-21 12:41:55 -05003735 if (test_bit(RB_BUFFERS_DISABLED_BIT, p))
3736 r = sprintf(buf, "permanently disabled\n");
3737 else
3738 r = sprintf(buf, "%d\n", test_bit(RB_BUFFERS_ON_BIT, p));
Steven Rostedta3583242008-11-11 15:01:42 -05003739
3740 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
3741}
3742
3743static ssize_t
3744rb_simple_write(struct file *filp, const char __user *ubuf,
3745 size_t cnt, loff_t *ppos)
3746{
Hannes Eder5e398412009-02-10 19:44:34 +01003747 unsigned long *p = filp->private_data;
Steven Rostedta3583242008-11-11 15:01:42 -05003748 char buf[64];
Hannes Eder5e398412009-02-10 19:44:34 +01003749 unsigned long val;
Steven Rostedta3583242008-11-11 15:01:42 -05003750 int ret;
3751
3752 if (cnt >= sizeof(buf))
3753 return -EINVAL;
3754
3755 if (copy_from_user(&buf, ubuf, cnt))
3756 return -EFAULT;
3757
3758 buf[cnt] = 0;
3759
3760 ret = strict_strtoul(buf, 10, &val);
3761 if (ret < 0)
3762 return ret;
3763
Steven Rostedt033601a2008-11-21 12:41:55 -05003764 if (val)
3765 set_bit(RB_BUFFERS_ON_BIT, p);
3766 else
3767 clear_bit(RB_BUFFERS_ON_BIT, p);
Steven Rostedta3583242008-11-11 15:01:42 -05003768
3769 (*ppos)++;
3770
3771 return cnt;
3772}
3773
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003774static const struct file_operations rb_simple_fops = {
Steven Rostedta3583242008-11-11 15:01:42 -05003775 .open = tracing_open_generic,
3776 .read = rb_simple_read,
3777 .write = rb_simple_write,
3778};
3779
3780
3781static __init int rb_init_debugfs(void)
3782{
3783 struct dentry *d_tracer;
Steven Rostedta3583242008-11-11 15:01:42 -05003784
3785 d_tracer = tracing_init_dentry();
3786
Frederic Weisbecker5452af62009-03-27 00:25:38 +01003787 trace_create_file("tracing_on", 0644, d_tracer,
3788 &ring_buffer_flags, &rb_simple_fops);
Steven Rostedta3583242008-11-11 15:01:42 -05003789
3790 return 0;
3791}
3792
3793fs_initcall(rb_init_debugfs);
Paul Mundt1155de42009-06-25 14:30:12 +09003794#endif
Steven Rostedt554f7862009-03-11 22:00:13 -04003795
Steven Rostedt59222ef2009-03-12 11:46:03 -04003796#ifdef CONFIG_HOTPLUG_CPU
Frederic Weisbecker09c9e842009-03-21 04:33:36 +01003797static int rb_cpu_notify(struct notifier_block *self,
3798 unsigned long action, void *hcpu)
Steven Rostedt554f7862009-03-11 22:00:13 -04003799{
3800 struct ring_buffer *buffer =
3801 container_of(self, struct ring_buffer, cpu_notify);
3802 long cpu = (long)hcpu;
3803
3804 switch (action) {
3805 case CPU_UP_PREPARE:
3806 case CPU_UP_PREPARE_FROZEN:
Rusty Russell3f237a72009-06-12 21:15:30 +09303807 if (cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt554f7862009-03-11 22:00:13 -04003808 return NOTIFY_OK;
3809
3810 buffer->buffers[cpu] =
3811 rb_allocate_cpu_buffer(buffer, cpu);
3812 if (!buffer->buffers[cpu]) {
3813 WARN(1, "failed to allocate ring buffer on CPU %ld\n",
3814 cpu);
3815 return NOTIFY_OK;
3816 }
3817 smp_wmb();
Rusty Russell3f237a72009-06-12 21:15:30 +09303818 cpumask_set_cpu(cpu, buffer->cpumask);
Steven Rostedt554f7862009-03-11 22:00:13 -04003819 break;
3820 case CPU_DOWN_PREPARE:
3821 case CPU_DOWN_PREPARE_FROZEN:
3822 /*
3823 * Do nothing.
3824 * If we were to free the buffer, then the user would
3825 * lose any trace that was in the buffer.
3826 */
3827 break;
3828 default:
3829 break;
3830 }
3831 return NOTIFY_OK;
3832}
3833#endif