blob: e648ba4f70e0c15c528b8767b6b41f754450e7ab [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 Rostedt3e89c7bb2008-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 Rostedt3e89c7bb2008-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.
848 */
849 local_cmpxchg(&next_page->write, old_write, val);
850 local_cmpxchg(&next_page->entries, old_entries, eval);
851
852 /*
853 * No need to worry about races with clearing out the commit.
854 * it only can increment when a commit takes place. But that
855 * only happens in the outer most nested commit.
856 */
857 local_set(&next_page->page->commit, 0);
858
859 old_tail = cmpxchg(&cpu_buffer->tail_page,
860 tail_page, next_page);
861
862 if (old_tail == tail_page)
863 ret = 1;
864 }
865
866 return ret;
867}
868
869static int rb_check_bpage(struct ring_buffer_per_cpu *cpu_buffer,
870 struct buffer_page *bpage)
871{
872 unsigned long val = (unsigned long)bpage;
873
874 if (RB_WARN_ON(cpu_buffer, val & RB_FLAG_MASK))
875 return 1;
876
877 return 0;
878}
879
880/**
881 * rb_check_list - make sure a pointer to a list has the last bits zero
882 */
883static int rb_check_list(struct ring_buffer_per_cpu *cpu_buffer,
884 struct list_head *list)
885{
886 if (RB_WARN_ON(cpu_buffer, rb_list_head(list->prev) != list->prev))
887 return 1;
888 if (RB_WARN_ON(cpu_buffer, rb_list_head(list->next) != list->next))
889 return 1;
890 return 0;
891}
892
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400893/**
894 * check_pages - integrity check of buffer pages
895 * @cpu_buffer: CPU buffer with pages to test
896 *
Wenji Huangc3706f02009-02-10 01:03:18 -0500897 * As a safety measure we check to make sure the data pages have not
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400898 * been corrupted.
899 */
900static int rb_check_pages(struct ring_buffer_per_cpu *cpu_buffer)
901{
Steven Rostedt3adc54f2009-03-30 15:32:01 -0400902 struct list_head *head = cpu_buffer->pages;
Steven Rostedt044fa782008-12-02 23:50:03 -0500903 struct buffer_page *bpage, *tmp;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400904
Steven Rostedt77ae3652009-03-27 11:00:29 -0400905 rb_head_page_deactivate(cpu_buffer);
906
Steven Rostedt3e89c7bb2008-11-11 15:28:41 -0500907 if (RB_WARN_ON(cpu_buffer, head->next->prev != head))
908 return -1;
909 if (RB_WARN_ON(cpu_buffer, head->prev->next != head))
910 return -1;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400911
Steven Rostedt77ae3652009-03-27 11:00:29 -0400912 if (rb_check_list(cpu_buffer, head))
913 return -1;
914
Steven Rostedt044fa782008-12-02 23:50:03 -0500915 list_for_each_entry_safe(bpage, tmp, head, list) {
Steven Rostedt3e89c7bb2008-11-11 15:28:41 -0500916 if (RB_WARN_ON(cpu_buffer,
Steven Rostedt044fa782008-12-02 23:50:03 -0500917 bpage->list.next->prev != &bpage->list))
Steven Rostedt3e89c7bb2008-11-11 15:28:41 -0500918 return -1;
919 if (RB_WARN_ON(cpu_buffer,
Steven Rostedt044fa782008-12-02 23:50:03 -0500920 bpage->list.prev->next != &bpage->list))
Steven Rostedt3e89c7bb2008-11-11 15:28:41 -0500921 return -1;
Steven Rostedt77ae3652009-03-27 11:00:29 -0400922 if (rb_check_list(cpu_buffer, &bpage->list))
923 return -1;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400924 }
925
Steven Rostedt77ae3652009-03-27 11:00:29 -0400926 rb_head_page_activate(cpu_buffer);
927
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400928 return 0;
929}
930
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400931static int rb_allocate_pages(struct ring_buffer_per_cpu *cpu_buffer,
932 unsigned nr_pages)
933{
Steven Rostedt044fa782008-12-02 23:50:03 -0500934 struct buffer_page *bpage, *tmp;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400935 unsigned long addr;
936 LIST_HEAD(pages);
937 unsigned i;
938
Steven Rostedt3adc54f2009-03-30 15:32:01 -0400939 WARN_ON(!nr_pages);
940
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400941 for (i = 0; i < nr_pages; i++) {
Steven Rostedt044fa782008-12-02 23:50:03 -0500942 bpage = kzalloc_node(ALIGN(sizeof(*bpage), cache_line_size()),
Steven Rostedtaa1e0e3b2008-10-02 19:18:09 -0400943 GFP_KERNEL, cpu_to_node(cpu_buffer->cpu));
Steven Rostedt044fa782008-12-02 23:50:03 -0500944 if (!bpage)
Steven Rostedte4c2ce82008-10-01 11:14:54 -0400945 goto free_pages;
Steven Rostedt77ae3652009-03-27 11:00:29 -0400946
947 rb_check_bpage(cpu_buffer, bpage);
948
Steven Rostedt044fa782008-12-02 23:50:03 -0500949 list_add(&bpage->list, &pages);
Steven Rostedte4c2ce82008-10-01 11:14:54 -0400950
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400951 addr = __get_free_page(GFP_KERNEL);
952 if (!addr)
953 goto free_pages;
Steven Rostedt044fa782008-12-02 23:50:03 -0500954 bpage->page = (void *)addr;
955 rb_init_page(bpage->page);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400956 }
957
Steven Rostedt3adc54f2009-03-30 15:32:01 -0400958 /*
959 * The ring buffer page list is a circular list that does not
960 * start and end with a list head. All page list items point to
961 * other pages.
962 */
963 cpu_buffer->pages = pages.next;
964 list_del(&pages);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400965
966 rb_check_pages(cpu_buffer);
967
968 return 0;
969
970 free_pages:
Steven Rostedt044fa782008-12-02 23:50:03 -0500971 list_for_each_entry_safe(bpage, tmp, &pages, list) {
972 list_del_init(&bpage->list);
973 free_buffer_page(bpage);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400974 }
975 return -ENOMEM;
976}
977
978static struct ring_buffer_per_cpu *
979rb_allocate_cpu_buffer(struct ring_buffer *buffer, int cpu)
980{
981 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedt044fa782008-12-02 23:50:03 -0500982 struct buffer_page *bpage;
Steven Rostedtd7690412008-10-01 00:29:53 -0400983 unsigned long addr;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400984 int ret;
985
986 cpu_buffer = kzalloc_node(ALIGN(sizeof(*cpu_buffer), cache_line_size()),
987 GFP_KERNEL, cpu_to_node(cpu));
988 if (!cpu_buffer)
989 return NULL;
990
991 cpu_buffer->cpu = cpu;
992 cpu_buffer->buffer = buffer;
Steven Rostedtf83c9d02008-11-11 18:47:44 +0100993 spin_lock_init(&cpu_buffer->reader_lock);
Peter Zijlstra1f8a6a12009-06-08 18:18:39 +0200994 lockdep_set_class(&cpu_buffer->reader_lock, buffer->reader_lock_key);
Steven Rostedt3e03fb72008-11-06 00:09:43 -0500995 cpu_buffer->lock = (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400996
Steven Rostedt044fa782008-12-02 23:50:03 -0500997 bpage = kzalloc_node(ALIGN(sizeof(*bpage), cache_line_size()),
Steven Rostedte4c2ce82008-10-01 11:14:54 -0400998 GFP_KERNEL, cpu_to_node(cpu));
Steven Rostedt044fa782008-12-02 23:50:03 -0500999 if (!bpage)
Steven Rostedte4c2ce82008-10-01 11:14:54 -04001000 goto fail_free_buffer;
1001
Steven Rostedt77ae3652009-03-27 11:00:29 -04001002 rb_check_bpage(cpu_buffer, bpage);
1003
Steven Rostedt044fa782008-12-02 23:50:03 -05001004 cpu_buffer->reader_page = bpage;
Steven Rostedtd7690412008-10-01 00:29:53 -04001005 addr = __get_free_page(GFP_KERNEL);
1006 if (!addr)
Steven Rostedte4c2ce82008-10-01 11:14:54 -04001007 goto fail_free_reader;
Steven Rostedt044fa782008-12-02 23:50:03 -05001008 bpage->page = (void *)addr;
1009 rb_init_page(bpage->page);
Steven Rostedte4c2ce82008-10-01 11:14:54 -04001010
Steven Rostedtd7690412008-10-01 00:29:53 -04001011 INIT_LIST_HEAD(&cpu_buffer->reader_page->list);
Steven Rostedtd7690412008-10-01 00:29:53 -04001012
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001013 ret = rb_allocate_pages(cpu_buffer, buffer->pages);
1014 if (ret < 0)
Steven Rostedtd7690412008-10-01 00:29:53 -04001015 goto fail_free_reader;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001016
1017 cpu_buffer->head_page
Steven Rostedt3adc54f2009-03-30 15:32:01 -04001018 = list_entry(cpu_buffer->pages, struct buffer_page, list);
Steven Rostedtbf41a152008-10-04 02:00:59 -04001019 cpu_buffer->tail_page = cpu_buffer->commit_page = cpu_buffer->head_page;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001020
Steven Rostedt77ae3652009-03-27 11:00:29 -04001021 rb_head_page_activate(cpu_buffer);
1022
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001023 return cpu_buffer;
1024
Steven Rostedtd7690412008-10-01 00:29:53 -04001025 fail_free_reader:
1026 free_buffer_page(cpu_buffer->reader_page);
1027
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001028 fail_free_buffer:
1029 kfree(cpu_buffer);
1030 return NULL;
1031}
1032
1033static void rb_free_cpu_buffer(struct ring_buffer_per_cpu *cpu_buffer)
1034{
Steven Rostedt3adc54f2009-03-30 15:32:01 -04001035 struct list_head *head = cpu_buffer->pages;
Steven Rostedt044fa782008-12-02 23:50:03 -05001036 struct buffer_page *bpage, *tmp;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001037
Steven Rostedtd7690412008-10-01 00:29:53 -04001038 free_buffer_page(cpu_buffer->reader_page);
1039
Steven Rostedt77ae3652009-03-27 11:00:29 -04001040 rb_head_page_deactivate(cpu_buffer);
1041
Steven Rostedt3adc54f2009-03-30 15:32:01 -04001042 if (head) {
1043 list_for_each_entry_safe(bpage, tmp, head, list) {
1044 list_del_init(&bpage->list);
1045 free_buffer_page(bpage);
1046 }
1047 bpage = list_entry(head, struct buffer_page, list);
Steven Rostedt044fa782008-12-02 23:50:03 -05001048 free_buffer_page(bpage);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001049 }
Steven Rostedt3adc54f2009-03-30 15:32:01 -04001050
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001051 kfree(cpu_buffer);
1052}
1053
Steven Rostedt59222ef2009-03-12 11:46:03 -04001054#ifdef CONFIG_HOTPLUG_CPU
Frederic Weisbecker09c9e842009-03-21 04:33:36 +01001055static int rb_cpu_notify(struct notifier_block *self,
1056 unsigned long action, void *hcpu);
Steven Rostedt554f7862009-03-11 22:00:13 -04001057#endif
1058
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001059/**
1060 * ring_buffer_alloc - allocate a new ring_buffer
Robert Richter68814b52008-11-24 12:24:12 +01001061 * @size: the size in bytes per cpu that is needed.
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001062 * @flags: attributes to set for the ring buffer.
1063 *
1064 * Currently the only flag that is available is the RB_FL_OVERWRITE
1065 * flag. This flag means that the buffer will overwrite old data
1066 * when the buffer wraps. If this flag is not set, the buffer will
1067 * drop data when the tail hits the head.
1068 */
Peter Zijlstra1f8a6a12009-06-08 18:18:39 +02001069struct ring_buffer *__ring_buffer_alloc(unsigned long size, unsigned flags,
1070 struct lock_class_key *key)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001071{
1072 struct ring_buffer *buffer;
1073 int bsize;
1074 int cpu;
1075
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001076 /* keep it in its own cache line */
1077 buffer = kzalloc(ALIGN(sizeof(*buffer), cache_line_size()),
1078 GFP_KERNEL);
1079 if (!buffer)
1080 return NULL;
1081
Rusty Russell9e01c1b2009-01-01 10:12:22 +10301082 if (!alloc_cpumask_var(&buffer->cpumask, GFP_KERNEL))
1083 goto fail_free_buffer;
1084
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001085 buffer->pages = DIV_ROUND_UP(size, BUF_PAGE_SIZE);
1086 buffer->flags = flags;
Steven Rostedt37886f62009-03-17 17:22:06 -04001087 buffer->clock = trace_clock_local;
Peter Zijlstra1f8a6a12009-06-08 18:18:39 +02001088 buffer->reader_lock_key = key;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001089
1090 /* need at least two pages */
Steven Rostedt5f78abe2009-06-17 14:11:10 -04001091 if (buffer->pages < 2)
1092 buffer->pages = 2;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001093
Frederic Weisbecker3bf832c2009-03-19 14:47:33 +01001094 /*
1095 * In case of non-hotplug cpu, if the ring-buffer is allocated
1096 * in early initcall, it will not be notified of secondary cpus.
1097 * In that off case, we need to allocate for all possible cpus.
1098 */
1099#ifdef CONFIG_HOTPLUG_CPU
Steven Rostedt554f7862009-03-11 22:00:13 -04001100 get_online_cpus();
1101 cpumask_copy(buffer->cpumask, cpu_online_mask);
Frederic Weisbecker3bf832c2009-03-19 14:47:33 +01001102#else
1103 cpumask_copy(buffer->cpumask, cpu_possible_mask);
1104#endif
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001105 buffer->cpus = nr_cpu_ids;
1106
1107 bsize = sizeof(void *) * nr_cpu_ids;
1108 buffer->buffers = kzalloc(ALIGN(bsize, cache_line_size()),
1109 GFP_KERNEL);
1110 if (!buffer->buffers)
Rusty Russell9e01c1b2009-01-01 10:12:22 +10301111 goto fail_free_cpumask;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001112
1113 for_each_buffer_cpu(buffer, cpu) {
1114 buffer->buffers[cpu] =
1115 rb_allocate_cpu_buffer(buffer, cpu);
1116 if (!buffer->buffers[cpu])
1117 goto fail_free_buffers;
1118 }
1119
Steven Rostedt59222ef2009-03-12 11:46:03 -04001120#ifdef CONFIG_HOTPLUG_CPU
Steven Rostedt554f7862009-03-11 22:00:13 -04001121 buffer->cpu_notify.notifier_call = rb_cpu_notify;
1122 buffer->cpu_notify.priority = 0;
1123 register_cpu_notifier(&buffer->cpu_notify);
1124#endif
1125
1126 put_online_cpus();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001127 mutex_init(&buffer->mutex);
1128
1129 return buffer;
1130
1131 fail_free_buffers:
1132 for_each_buffer_cpu(buffer, cpu) {
1133 if (buffer->buffers[cpu])
1134 rb_free_cpu_buffer(buffer->buffers[cpu]);
1135 }
1136 kfree(buffer->buffers);
1137
Rusty Russell9e01c1b2009-01-01 10:12:22 +10301138 fail_free_cpumask:
1139 free_cpumask_var(buffer->cpumask);
Steven Rostedt554f7862009-03-11 22:00:13 -04001140 put_online_cpus();
Rusty Russell9e01c1b2009-01-01 10:12:22 +10301141
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001142 fail_free_buffer:
1143 kfree(buffer);
1144 return NULL;
1145}
Peter Zijlstra1f8a6a12009-06-08 18:18:39 +02001146EXPORT_SYMBOL_GPL(__ring_buffer_alloc);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001147
1148/**
1149 * ring_buffer_free - free a ring buffer.
1150 * @buffer: the buffer to free.
1151 */
1152void
1153ring_buffer_free(struct ring_buffer *buffer)
1154{
1155 int cpu;
1156
Steven Rostedt554f7862009-03-11 22:00:13 -04001157 get_online_cpus();
1158
Steven Rostedt59222ef2009-03-12 11:46:03 -04001159#ifdef CONFIG_HOTPLUG_CPU
Steven Rostedt554f7862009-03-11 22:00:13 -04001160 unregister_cpu_notifier(&buffer->cpu_notify);
1161#endif
1162
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001163 for_each_buffer_cpu(buffer, cpu)
1164 rb_free_cpu_buffer(buffer->buffers[cpu]);
1165
Steven Rostedt554f7862009-03-11 22:00:13 -04001166 put_online_cpus();
1167
Rusty Russell9e01c1b2009-01-01 10:12:22 +10301168 free_cpumask_var(buffer->cpumask);
1169
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001170 kfree(buffer);
1171}
Robert Richterc4f50182008-12-11 16:49:22 +01001172EXPORT_SYMBOL_GPL(ring_buffer_free);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001173
Steven Rostedt37886f62009-03-17 17:22:06 -04001174void ring_buffer_set_clock(struct ring_buffer *buffer,
1175 u64 (*clock)(void))
1176{
1177 buffer->clock = clock;
1178}
1179
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001180static void rb_reset_cpu(struct ring_buffer_per_cpu *cpu_buffer);
1181
1182static void
1183rb_remove_pages(struct ring_buffer_per_cpu *cpu_buffer, unsigned nr_pages)
1184{
Steven Rostedt044fa782008-12-02 23:50:03 -05001185 struct buffer_page *bpage;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001186 struct list_head *p;
1187 unsigned i;
1188
1189 atomic_inc(&cpu_buffer->record_disabled);
1190 synchronize_sched();
1191
Steven Rostedt77ae3652009-03-27 11:00:29 -04001192 rb_head_page_deactivate(cpu_buffer);
1193
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001194 for (i = 0; i < nr_pages; i++) {
Steven Rostedt3adc54f2009-03-30 15:32:01 -04001195 if (RB_WARN_ON(cpu_buffer, list_empty(cpu_buffer->pages)))
Steven Rostedt3e89c7bb2008-11-11 15:28:41 -05001196 return;
Steven Rostedt3adc54f2009-03-30 15:32:01 -04001197 p = cpu_buffer->pages->next;
Steven Rostedt044fa782008-12-02 23:50:03 -05001198 bpage = list_entry(p, struct buffer_page, list);
1199 list_del_init(&bpage->list);
1200 free_buffer_page(bpage);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001201 }
Steven Rostedt3adc54f2009-03-30 15:32:01 -04001202 if (RB_WARN_ON(cpu_buffer, list_empty(cpu_buffer->pages)))
Steven Rostedt3e89c7bb2008-11-11 15:28:41 -05001203 return;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001204
1205 rb_reset_cpu(cpu_buffer);
1206
1207 rb_check_pages(cpu_buffer);
1208
1209 atomic_dec(&cpu_buffer->record_disabled);
1210
1211}
1212
1213static void
1214rb_insert_pages(struct ring_buffer_per_cpu *cpu_buffer,
1215 struct list_head *pages, unsigned nr_pages)
1216{
Steven Rostedt044fa782008-12-02 23:50:03 -05001217 struct buffer_page *bpage;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001218 struct list_head *p;
1219 unsigned i;
1220
1221 atomic_inc(&cpu_buffer->record_disabled);
1222 synchronize_sched();
1223
Steven Rostedt77ae3652009-03-27 11:00:29 -04001224 spin_lock_irq(&cpu_buffer->reader_lock);
1225 rb_head_page_deactivate(cpu_buffer);
1226
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001227 for (i = 0; i < nr_pages; i++) {
Steven Rostedt3e89c7bb2008-11-11 15:28:41 -05001228 if (RB_WARN_ON(cpu_buffer, list_empty(pages)))
1229 return;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001230 p = pages->next;
Steven Rostedt044fa782008-12-02 23:50:03 -05001231 bpage = list_entry(p, struct buffer_page, list);
1232 list_del_init(&bpage->list);
Steven Rostedt3adc54f2009-03-30 15:32:01 -04001233 list_add_tail(&bpage->list, cpu_buffer->pages);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001234 }
1235 rb_reset_cpu(cpu_buffer);
Steven Rostedt77ae3652009-03-27 11:00:29 -04001236 spin_unlock_irq(&cpu_buffer->reader_lock);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001237
1238 rb_check_pages(cpu_buffer);
1239
1240 atomic_dec(&cpu_buffer->record_disabled);
1241}
1242
1243/**
1244 * ring_buffer_resize - resize the ring buffer
1245 * @buffer: the buffer to resize.
1246 * @size: the new size.
1247 *
1248 * The tracer is responsible for making sure that the buffer is
1249 * not being used while changing the size.
1250 * Note: We may be able to change the above requirement by using
1251 * RCU synchronizations.
1252 *
1253 * Minimum size is 2 * BUF_PAGE_SIZE.
1254 *
1255 * Returns -1 on failure.
1256 */
1257int ring_buffer_resize(struct ring_buffer *buffer, unsigned long size)
1258{
1259 struct ring_buffer_per_cpu *cpu_buffer;
1260 unsigned nr_pages, rm_pages, new_pages;
Steven Rostedt044fa782008-12-02 23:50:03 -05001261 struct buffer_page *bpage, *tmp;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001262 unsigned long buffer_size;
1263 unsigned long addr;
1264 LIST_HEAD(pages);
1265 int i, cpu;
1266
Ingo Molnaree51a1d2008-11-13 14:58:31 +01001267 /*
1268 * Always succeed at resizing a non-existent buffer:
1269 */
1270 if (!buffer)
1271 return size;
1272
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001273 size = DIV_ROUND_UP(size, BUF_PAGE_SIZE);
1274 size *= BUF_PAGE_SIZE;
1275 buffer_size = buffer->pages * BUF_PAGE_SIZE;
1276
1277 /* we need a minimum of two pages */
1278 if (size < BUF_PAGE_SIZE * 2)
1279 size = BUF_PAGE_SIZE * 2;
1280
1281 if (size == buffer_size)
1282 return size;
1283
1284 mutex_lock(&buffer->mutex);
Steven Rostedt554f7862009-03-11 22:00:13 -04001285 get_online_cpus();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001286
1287 nr_pages = DIV_ROUND_UP(size, BUF_PAGE_SIZE);
1288
1289 if (size < buffer_size) {
1290
1291 /* easy case, just free pages */
Steven Rostedt554f7862009-03-11 22:00:13 -04001292 if (RB_WARN_ON(buffer, nr_pages >= buffer->pages))
1293 goto out_fail;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001294
1295 rm_pages = buffer->pages - nr_pages;
1296
1297 for_each_buffer_cpu(buffer, cpu) {
1298 cpu_buffer = buffer->buffers[cpu];
1299 rb_remove_pages(cpu_buffer, rm_pages);
1300 }
1301 goto out;
1302 }
1303
1304 /*
1305 * This is a bit more difficult. We only want to add pages
1306 * when we can allocate enough for all CPUs. We do this
1307 * by allocating all the pages and storing them on a local
1308 * link list. If we succeed in our allocation, then we
1309 * add these pages to the cpu_buffers. Otherwise we just free
1310 * them all and return -ENOMEM;
1311 */
Steven Rostedt554f7862009-03-11 22:00:13 -04001312 if (RB_WARN_ON(buffer, nr_pages <= buffer->pages))
1313 goto out_fail;
Steven Rostedtf536aaf2008-11-10 23:07:30 -05001314
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001315 new_pages = nr_pages - buffer->pages;
1316
1317 for_each_buffer_cpu(buffer, cpu) {
1318 for (i = 0; i < new_pages; i++) {
Steven Rostedt044fa782008-12-02 23:50:03 -05001319 bpage = kzalloc_node(ALIGN(sizeof(*bpage),
Steven Rostedte4c2ce82008-10-01 11:14:54 -04001320 cache_line_size()),
1321 GFP_KERNEL, cpu_to_node(cpu));
Steven Rostedt044fa782008-12-02 23:50:03 -05001322 if (!bpage)
Steven Rostedte4c2ce82008-10-01 11:14:54 -04001323 goto free_pages;
Steven Rostedt044fa782008-12-02 23:50:03 -05001324 list_add(&bpage->list, &pages);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001325 addr = __get_free_page(GFP_KERNEL);
1326 if (!addr)
1327 goto free_pages;
Steven Rostedt044fa782008-12-02 23:50:03 -05001328 bpage->page = (void *)addr;
1329 rb_init_page(bpage->page);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001330 }
1331 }
1332
1333 for_each_buffer_cpu(buffer, cpu) {
1334 cpu_buffer = buffer->buffers[cpu];
1335 rb_insert_pages(cpu_buffer, &pages, new_pages);
1336 }
1337
Steven Rostedt554f7862009-03-11 22:00:13 -04001338 if (RB_WARN_ON(buffer, !list_empty(&pages)))
1339 goto out_fail;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001340
1341 out:
1342 buffer->pages = nr_pages;
Steven Rostedt554f7862009-03-11 22:00:13 -04001343 put_online_cpus();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001344 mutex_unlock(&buffer->mutex);
1345
1346 return size;
1347
1348 free_pages:
Steven Rostedt044fa782008-12-02 23:50:03 -05001349 list_for_each_entry_safe(bpage, tmp, &pages, list) {
1350 list_del_init(&bpage->list);
1351 free_buffer_page(bpage);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001352 }
Steven Rostedt554f7862009-03-11 22:00:13 -04001353 put_online_cpus();
Vegard Nossum641d2f62008-11-18 19:22:13 +01001354 mutex_unlock(&buffer->mutex);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001355 return -ENOMEM;
Steven Rostedt554f7862009-03-11 22:00:13 -04001356
1357 /*
1358 * Something went totally wrong, and we are too paranoid
1359 * to even clean up the mess.
1360 */
1361 out_fail:
1362 put_online_cpus();
1363 mutex_unlock(&buffer->mutex);
1364 return -1;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001365}
Robert Richterc4f50182008-12-11 16:49:22 +01001366EXPORT_SYMBOL_GPL(ring_buffer_resize);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001367
Steven Rostedt8789a9e2008-12-02 15:34:07 -05001368static inline void *
Steven Rostedt044fa782008-12-02 23:50:03 -05001369__rb_data_page_index(struct buffer_data_page *bpage, unsigned index)
Steven Rostedt8789a9e2008-12-02 15:34:07 -05001370{
Steven Rostedt044fa782008-12-02 23:50:03 -05001371 return bpage->data + index;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05001372}
1373
Steven Rostedt044fa782008-12-02 23:50:03 -05001374static inline void *__rb_page_index(struct buffer_page *bpage, unsigned index)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001375{
Steven Rostedt044fa782008-12-02 23:50:03 -05001376 return bpage->page->data + index;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001377}
1378
1379static inline struct ring_buffer_event *
Steven Rostedtd7690412008-10-01 00:29:53 -04001380rb_reader_event(struct ring_buffer_per_cpu *cpu_buffer)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001381{
Steven Rostedt6f807ac2008-10-04 02:00:58 -04001382 return __rb_page_index(cpu_buffer->reader_page,
1383 cpu_buffer->reader_page->read);
1384}
1385
1386static inline struct ring_buffer_event *
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001387rb_iter_head_event(struct ring_buffer_iter *iter)
1388{
Steven Rostedt6f807ac2008-10-04 02:00:58 -04001389 return __rb_page_index(iter->head_page, iter->head);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001390}
1391
Steven Rostedt77ae3652009-03-27 11:00:29 -04001392static inline unsigned long rb_page_write(struct buffer_page *bpage)
Steven Rostedtbf41a152008-10-04 02:00:59 -04001393{
Steven Rostedt77ae3652009-03-27 11:00:29 -04001394 return local_read(&bpage->write) & RB_WRITE_MASK;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001395}
1396
1397static inline unsigned rb_page_commit(struct buffer_page *bpage)
1398{
Steven Rostedtabc9b562008-12-02 15:34:06 -05001399 return local_read(&bpage->page->commit);
Steven Rostedtbf41a152008-10-04 02:00:59 -04001400}
1401
Steven Rostedt77ae3652009-03-27 11:00:29 -04001402static inline unsigned long rb_page_entries(struct buffer_page *bpage)
1403{
1404 return local_read(&bpage->entries) & RB_WRITE_MASK;
1405}
1406
Steven Rostedtbf41a152008-10-04 02:00:59 -04001407/* Size is determined by what has been commited */
1408static inline unsigned rb_page_size(struct buffer_page *bpage)
1409{
1410 return rb_page_commit(bpage);
1411}
1412
1413static inline unsigned
1414rb_commit_index(struct ring_buffer_per_cpu *cpu_buffer)
1415{
1416 return rb_page_commit(cpu_buffer->commit_page);
1417}
1418
Steven Rostedtbf41a152008-10-04 02:00:59 -04001419static inline unsigned
1420rb_event_index(struct ring_buffer_event *event)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001421{
Steven Rostedtbf41a152008-10-04 02:00:59 -04001422 unsigned long addr = (unsigned long)event;
1423
Steven Rostedt22f470f2009-06-11 09:29:58 -04001424 return (addr & ~PAGE_MASK) - BUF_PAGE_HDR_SIZE;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001425}
1426
Steven Rostedt0f0c85f2009-05-11 16:08:00 -04001427static inline int
Steven Rostedtfa743952009-06-16 12:37:57 -04001428rb_event_is_commit(struct ring_buffer_per_cpu *cpu_buffer,
1429 struct ring_buffer_event *event)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001430{
Steven Rostedtbf41a152008-10-04 02:00:59 -04001431 unsigned long addr = (unsigned long)event;
1432 unsigned long index;
1433
1434 index = rb_event_index(event);
1435 addr &= PAGE_MASK;
1436
1437 return cpu_buffer->commit_page->page == (void *)addr &&
1438 rb_commit_index(cpu_buffer) == index;
1439}
1440
Andrew Morton34a148b2009-01-09 12:27:09 -08001441static void
Steven Rostedtbf41a152008-10-04 02:00:59 -04001442rb_set_commit_to_write(struct ring_buffer_per_cpu *cpu_buffer)
1443{
Steven Rostedt77ae3652009-03-27 11:00:29 -04001444 unsigned long max_count;
1445
Steven Rostedtbf41a152008-10-04 02:00:59 -04001446 /*
1447 * We only race with interrupts and NMIs on this CPU.
1448 * If we own the commit event, then we can commit
1449 * all others that interrupted us, since the interruptions
1450 * are in stack format (they finish before they come
1451 * back to us). This allows us to do a simple loop to
1452 * assign the commit to the tail.
1453 */
Steven Rostedta8ccf1d2008-12-23 11:32:24 -05001454 again:
Steven Rostedt77ae3652009-03-27 11:00:29 -04001455 max_count = cpu_buffer->buffer->pages * 100;
1456
Steven Rostedtbf41a152008-10-04 02:00:59 -04001457 while (cpu_buffer->commit_page != cpu_buffer->tail_page) {
Steven Rostedt77ae3652009-03-27 11:00:29 -04001458 if (RB_WARN_ON(cpu_buffer, !(--max_count)))
1459 return;
1460 if (RB_WARN_ON(cpu_buffer,
1461 rb_is_reader_page(cpu_buffer->tail_page)))
1462 return;
1463 local_set(&cpu_buffer->commit_page->page->commit,
1464 rb_page_write(cpu_buffer->commit_page));
Steven Rostedtbf41a152008-10-04 02:00:59 -04001465 rb_inc_page(cpu_buffer, &cpu_buffer->commit_page);
Steven Rostedtabc9b562008-12-02 15:34:06 -05001466 cpu_buffer->write_stamp =
1467 cpu_buffer->commit_page->page->time_stamp;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001468 /* add barrier to keep gcc from optimizing too much */
1469 barrier();
1470 }
1471 while (rb_commit_index(cpu_buffer) !=
1472 rb_page_write(cpu_buffer->commit_page)) {
Steven Rostedt77ae3652009-03-27 11:00:29 -04001473
1474 local_set(&cpu_buffer->commit_page->page->commit,
1475 rb_page_write(cpu_buffer->commit_page));
1476 RB_WARN_ON(cpu_buffer,
1477 local_read(&cpu_buffer->commit_page->page->commit) &
1478 ~RB_WRITE_MASK);
Steven Rostedtbf41a152008-10-04 02:00:59 -04001479 barrier();
1480 }
Steven Rostedta8ccf1d2008-12-23 11:32:24 -05001481
1482 /* again, keep gcc from optimizing */
1483 barrier();
1484
1485 /*
1486 * If an interrupt came in just after the first while loop
1487 * and pushed the tail page forward, we will be left with
1488 * a dangling commit that will never go forward.
1489 */
1490 if (unlikely(cpu_buffer->commit_page != cpu_buffer->tail_page))
1491 goto again;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001492}
1493
Steven Rostedtd7690412008-10-01 00:29:53 -04001494static void rb_reset_reader_page(struct ring_buffer_per_cpu *cpu_buffer)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001495{
Steven Rostedtabc9b562008-12-02 15:34:06 -05001496 cpu_buffer->read_stamp = cpu_buffer->reader_page->page->time_stamp;
Steven Rostedt6f807ac2008-10-04 02:00:58 -04001497 cpu_buffer->reader_page->read = 0;
Steven Rostedtd7690412008-10-01 00:29:53 -04001498}
1499
Andrew Morton34a148b2009-01-09 12:27:09 -08001500static void rb_inc_iter(struct ring_buffer_iter *iter)
Steven Rostedtd7690412008-10-01 00:29:53 -04001501{
1502 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
1503
1504 /*
1505 * The iterator could be on the reader page (it starts there).
1506 * But the head could have moved, since the reader was
1507 * found. Check for this case and assign the iterator
1508 * to the head page instead of next.
1509 */
1510 if (iter->head_page == cpu_buffer->reader_page)
Steven Rostedt77ae3652009-03-27 11:00:29 -04001511 iter->head_page = rb_set_head_page(cpu_buffer);
Steven Rostedtd7690412008-10-01 00:29:53 -04001512 else
1513 rb_inc_page(cpu_buffer, &iter->head_page);
1514
Steven Rostedtabc9b562008-12-02 15:34:06 -05001515 iter->read_stamp = iter->head_page->page->time_stamp;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001516 iter->head = 0;
1517}
1518
1519/**
1520 * ring_buffer_update_event - update event type and data
1521 * @event: the even to update
1522 * @type: the type of event
1523 * @length: the size of the event field in the ring buffer
1524 *
1525 * Update the type and data fields of the event. The length
1526 * is the actual size that is written to the ring buffer,
1527 * and with this, we can determine what to place into the
1528 * data field.
1529 */
Andrew Morton34a148b2009-01-09 12:27:09 -08001530static void
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001531rb_update_event(struct ring_buffer_event *event,
1532 unsigned type, unsigned length)
1533{
Lai Jiangshan334d4162009-04-24 11:27:05 +08001534 event->type_len = type;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001535
1536 switch (type) {
1537
1538 case RINGBUF_TYPE_PADDING:
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001539 case RINGBUF_TYPE_TIME_EXTEND:
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001540 case RINGBUF_TYPE_TIME_STAMP:
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001541 break;
1542
Lai Jiangshan334d4162009-04-24 11:27:05 +08001543 case 0:
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001544 length -= RB_EVNT_HDR_SIZE;
Lai Jiangshan334d4162009-04-24 11:27:05 +08001545 if (length > RB_MAX_SMALL_DATA)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001546 event->array[0] = length;
Lai Jiangshan334d4162009-04-24 11:27:05 +08001547 else
1548 event->type_len = DIV_ROUND_UP(length, RB_ALIGNMENT);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001549 break;
1550 default:
1551 BUG();
1552 }
1553}
1554
Steven Rostedt77ae3652009-03-27 11:00:29 -04001555/*
1556 * rb_handle_head_page - writer hit the head page
1557 *
1558 * Returns: +1 to retry page
1559 * 0 to continue
1560 * -1 on error
1561 */
1562static int
1563rb_handle_head_page(struct ring_buffer_per_cpu *cpu_buffer,
1564 struct buffer_page *tail_page,
1565 struct buffer_page *next_page)
1566{
1567 struct buffer_page *new_head;
1568 int entries;
1569 int type;
1570 int ret;
1571
1572 entries = rb_page_entries(next_page);
1573
1574 /*
1575 * The hard part is here. We need to move the head
1576 * forward, and protect against both readers on
1577 * other CPUs and writers coming in via interrupts.
1578 */
1579 type = rb_head_page_set_update(cpu_buffer, next_page, tail_page,
1580 RB_PAGE_HEAD);
1581
1582 /*
1583 * type can be one of four:
1584 * NORMAL - an interrupt already moved it for us
1585 * HEAD - we are the first to get here.
1586 * UPDATE - we are the interrupt interrupting
1587 * a current move.
1588 * MOVED - a reader on another CPU moved the next
1589 * pointer to its reader page. Give up
1590 * and try again.
1591 */
1592
1593 switch (type) {
1594 case RB_PAGE_HEAD:
1595 /*
1596 * We changed the head to UPDATE, thus
1597 * it is our responsibility to update
1598 * the counters.
1599 */
1600 local_add(entries, &cpu_buffer->overrun);
1601
1602 /*
1603 * The entries will be zeroed out when we move the
1604 * tail page.
1605 */
1606
1607 /* still more to do */
1608 break;
1609
1610 case RB_PAGE_UPDATE:
1611 /*
1612 * This is an interrupt that interrupt the
1613 * previous update. Still more to do.
1614 */
1615 break;
1616 case RB_PAGE_NORMAL:
1617 /*
1618 * An interrupt came in before the update
1619 * and processed this for us.
1620 * Nothing left to do.
1621 */
1622 return 1;
1623 case RB_PAGE_MOVED:
1624 /*
1625 * The reader is on another CPU and just did
1626 * a swap with our next_page.
1627 * Try again.
1628 */
1629 return 1;
1630 default:
1631 RB_WARN_ON(cpu_buffer, 1); /* WTF??? */
1632 return -1;
1633 }
1634
1635 /*
1636 * Now that we are here, the old head pointer is
1637 * set to UPDATE. This will keep the reader from
1638 * swapping the head page with the reader page.
1639 * The reader (on another CPU) will spin till
1640 * we are finished.
1641 *
1642 * We just need to protect against interrupts
1643 * doing the job. We will set the next pointer
1644 * to HEAD. After that, we set the old pointer
1645 * to NORMAL, but only if it was HEAD before.
1646 * otherwise we are an interrupt, and only
1647 * want the outer most commit to reset it.
1648 */
1649 new_head = next_page;
1650 rb_inc_page(cpu_buffer, &new_head);
1651
1652 ret = rb_head_page_set_head(cpu_buffer, new_head, next_page,
1653 RB_PAGE_NORMAL);
1654
1655 /*
1656 * Valid returns are:
1657 * HEAD - an interrupt came in and already set it.
1658 * NORMAL - One of two things:
1659 * 1) We really set it.
1660 * 2) A bunch of interrupts came in and moved
1661 * the page forward again.
1662 */
1663 switch (ret) {
1664 case RB_PAGE_HEAD:
1665 case RB_PAGE_NORMAL:
1666 /* OK */
1667 break;
1668 default:
1669 RB_WARN_ON(cpu_buffer, 1);
1670 return -1;
1671 }
1672
1673 /*
1674 * It is possible that an interrupt came in,
1675 * set the head up, then more interrupts came in
1676 * and moved it again. When we get back here,
1677 * the page would have been set to NORMAL but we
1678 * just set it back to HEAD.
1679 *
1680 * How do you detect this? Well, if that happened
1681 * the tail page would have moved.
1682 */
1683 if (ret == RB_PAGE_NORMAL) {
1684 /*
1685 * If the tail had moved passed next, then we need
1686 * to reset the pointer.
1687 */
1688 if (cpu_buffer->tail_page != tail_page &&
1689 cpu_buffer->tail_page != next_page)
1690 rb_head_page_set_normal(cpu_buffer, new_head,
1691 next_page,
1692 RB_PAGE_HEAD);
1693 }
1694
1695 /*
1696 * If this was the outer most commit (the one that
1697 * changed the original pointer from HEAD to UPDATE),
1698 * then it is up to us to reset it to NORMAL.
1699 */
1700 if (type == RB_PAGE_HEAD) {
1701 ret = rb_head_page_set_normal(cpu_buffer, next_page,
1702 tail_page,
1703 RB_PAGE_UPDATE);
1704 if (RB_WARN_ON(cpu_buffer,
1705 ret != RB_PAGE_UPDATE))
1706 return -1;
1707 }
1708
1709 return 0;
1710}
1711
Andrew Morton34a148b2009-01-09 12:27:09 -08001712static unsigned rb_calculate_event_length(unsigned length)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001713{
1714 struct ring_buffer_event event; /* Used only for sizeof array */
1715
1716 /* zero length can cause confusions */
1717 if (!length)
1718 length = 1;
1719
1720 if (length > RB_MAX_SMALL_DATA)
1721 length += sizeof(event.array[0]);
1722
1723 length += RB_EVNT_HDR_SIZE;
1724 length = ALIGN(length, RB_ALIGNMENT);
1725
1726 return length;
1727}
1728
Steven Rostedtc7b09302009-06-11 11:12:00 -04001729static inline void
1730rb_reset_tail(struct ring_buffer_per_cpu *cpu_buffer,
1731 struct buffer_page *tail_page,
1732 unsigned long tail, unsigned long length)
1733{
1734 struct ring_buffer_event *event;
1735
1736 /*
1737 * Only the event that crossed the page boundary
1738 * must fill the old tail_page with padding.
1739 */
1740 if (tail >= BUF_PAGE_SIZE) {
1741 local_sub(length, &tail_page->write);
1742 return;
1743 }
1744
1745 event = __rb_page_index(tail_page, tail);
Linus Torvaldsb0b70652009-06-20 10:56:46 -07001746 kmemcheck_annotate_bitfield(event, bitfield);
Steven Rostedtc7b09302009-06-11 11:12:00 -04001747
1748 /*
1749 * If this event is bigger than the minimum size, then
1750 * we need to be careful that we don't subtract the
1751 * write counter enough to allow another writer to slip
1752 * in on this page.
1753 * We put in a discarded commit instead, to make sure
1754 * that this space is not used again.
1755 *
1756 * If we are less than the minimum size, we don't need to
1757 * worry about it.
1758 */
1759 if (tail > (BUF_PAGE_SIZE - RB_EVNT_MIN_SIZE)) {
1760 /* No room for any events */
1761
1762 /* Mark the rest of the page with padding */
1763 rb_event_set_padding(event);
1764
1765 /* Set the write back to the previous setting */
1766 local_sub(length, &tail_page->write);
1767 return;
1768 }
1769
1770 /* Put in a discarded event */
1771 event->array[0] = (BUF_PAGE_SIZE - tail) - RB_EVNT_HDR_SIZE;
1772 event->type_len = RINGBUF_TYPE_PADDING;
1773 /* time delta must be non zero */
1774 event->time_delta = 1;
1775 /* Account for this as an entry */
1776 local_inc(&tail_page->entries);
1777 local_inc(&cpu_buffer->entries);
1778
1779 /* Set write to end of buffer */
1780 length = (tail + length) - BUF_PAGE_SIZE;
1781 local_sub(length, &tail_page->write);
1782}
Steven Rostedt6634ff22009-05-06 15:30:07 -04001783
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001784static struct ring_buffer_event *
Steven Rostedt6634ff22009-05-06 15:30:07 -04001785rb_move_tail(struct ring_buffer_per_cpu *cpu_buffer,
1786 unsigned long length, unsigned long tail,
1787 struct buffer_page *commit_page,
1788 struct buffer_page *tail_page, u64 *ts)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001789{
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001790 struct ring_buffer *buffer = cpu_buffer->buffer;
Steven Rostedt77ae3652009-03-27 11:00:29 -04001791 struct buffer_page *next_page;
1792 int ret;
Steven Rostedtaa20ae82009-05-05 21:16:11 -04001793
1794 next_page = tail_page;
1795
Steven Rostedtaa20ae82009-05-05 21:16:11 -04001796 rb_inc_page(cpu_buffer, &next_page);
1797
Steven Rostedtaa20ae82009-05-05 21:16:11 -04001798 /*
1799 * If for some reason, we had an interrupt storm that made
1800 * it all the way around the buffer, bail, and warn
1801 * about it.
1802 */
1803 if (unlikely(next_page == commit_page)) {
Steven Rostedt77ae3652009-03-27 11:00:29 -04001804 local_inc(&cpu_buffer->commit_overrun);
Steven Rostedtaa20ae82009-05-05 21:16:11 -04001805 goto out_reset;
1806 }
1807
Steven Rostedt77ae3652009-03-27 11:00:29 -04001808 /*
1809 * This is where the fun begins!
1810 *
1811 * We are fighting against races between a reader that
1812 * could be on another CPU trying to swap its reader
1813 * page with the buffer head.
1814 *
1815 * We are also fighting against interrupts coming in and
1816 * moving the head or tail on us as well.
1817 *
1818 * If the next page is the head page then we have filled
1819 * the buffer, unless the commit page is still on the
1820 * reader page.
1821 */
1822 if (rb_is_head_page(cpu_buffer, next_page, &tail_page->list)) {
Steven Rostedtaa20ae82009-05-05 21:16:11 -04001823
Steven Rostedt77ae3652009-03-27 11:00:29 -04001824 /*
1825 * If the commit is not on the reader page, then
1826 * move the header page.
1827 */
1828 if (!rb_is_reader_page(cpu_buffer->commit_page)) {
1829 /*
1830 * If we are not in overwrite mode,
1831 * this is easy, just stop here.
1832 */
1833 if (!(buffer->flags & RB_FL_OVERWRITE))
1834 goto out_reset;
Steven Rostedtaa20ae82009-05-05 21:16:11 -04001835
Steven Rostedt77ae3652009-03-27 11:00:29 -04001836 ret = rb_handle_head_page(cpu_buffer,
1837 tail_page,
1838 next_page);
1839 if (ret < 0)
1840 goto out_reset;
1841 if (ret)
1842 goto out_again;
1843 } else {
1844 /*
1845 * We need to be careful here too. The
1846 * commit page could still be on the reader
1847 * page. We could have a small buffer, and
1848 * have filled up the buffer with events
1849 * from interrupts and such, and wrapped.
1850 *
1851 * Note, if the tail page is also the on the
1852 * reader_page, we let it move out.
1853 */
1854 if (unlikely((cpu_buffer->commit_page !=
1855 cpu_buffer->tail_page) &&
1856 (cpu_buffer->commit_page ==
1857 cpu_buffer->reader_page))) {
1858 local_inc(&cpu_buffer->commit_overrun);
1859 goto out_reset;
1860 }
Steven Rostedtaa20ae82009-05-05 21:16:11 -04001861 }
1862 }
1863
Steven Rostedt77ae3652009-03-27 11:00:29 -04001864 ret = rb_tail_page_update(cpu_buffer, tail_page, next_page);
1865 if (ret) {
1866 /*
1867 * Nested commits always have zero deltas, so
1868 * just reread the time stamp
1869 */
Steven Rostedt88eb0122009-05-11 16:28:23 -04001870 *ts = rb_time_stamp(buffer, cpu_buffer->cpu);
Steven Rostedt77ae3652009-03-27 11:00:29 -04001871 next_page->page->time_stamp = *ts;
Steven Rostedtaa20ae82009-05-05 21:16:11 -04001872 }
1873
Steven Rostedt77ae3652009-03-27 11:00:29 -04001874 out_again:
Steven Rostedtaa20ae82009-05-05 21:16:11 -04001875
Steven Rostedt77ae3652009-03-27 11:00:29 -04001876 rb_reset_tail(cpu_buffer, tail_page, tail, length);
Steven Rostedtaa20ae82009-05-05 21:16:11 -04001877
1878 /* fail and let the caller try again */
1879 return ERR_PTR(-EAGAIN);
1880
Steven Rostedt45141d42009-02-12 13:19:48 -05001881 out_reset:
Lai Jiangshan6f3b3442009-01-12 11:06:18 +08001882 /* reset write */
Steven Rostedtc7b09302009-06-11 11:12:00 -04001883 rb_reset_tail(cpu_buffer, tail_page, tail, length);
Lai Jiangshan6f3b3442009-01-12 11:06:18 +08001884
Steven Rostedtbf41a152008-10-04 02:00:59 -04001885 return NULL;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001886}
1887
Steven Rostedt6634ff22009-05-06 15:30:07 -04001888static struct ring_buffer_event *
1889__rb_reserve_next(struct ring_buffer_per_cpu *cpu_buffer,
1890 unsigned type, unsigned long length, u64 *ts)
1891{
1892 struct buffer_page *tail_page, *commit_page;
1893 struct ring_buffer_event *event;
1894 unsigned long tail, write;
1895
1896 commit_page = cpu_buffer->commit_page;
1897 /* we just need to protect against interrupts */
1898 barrier();
1899 tail_page = cpu_buffer->tail_page;
1900 write = local_add_return(length, &tail_page->write);
Steven Rostedt77ae3652009-03-27 11:00:29 -04001901
1902 /* set write to only the index of the write */
1903 write &= RB_WRITE_MASK;
Steven Rostedt6634ff22009-05-06 15:30:07 -04001904 tail = write - length;
1905
1906 /* See if we shot pass the end of this buffer page */
1907 if (write > BUF_PAGE_SIZE)
1908 return rb_move_tail(cpu_buffer, length, tail,
1909 commit_page, tail_page, ts);
1910
1911 /* We reserved something on the buffer */
1912
Steven Rostedt6634ff22009-05-06 15:30:07 -04001913 event = __rb_page_index(tail_page, tail);
Vegard Nossum1744a212009-02-28 08:29:44 +01001914 kmemcheck_annotate_bitfield(event, bitfield);
Steven Rostedt6634ff22009-05-06 15:30:07 -04001915 rb_update_event(event, type, length);
1916
1917 /* The passed in type is zero for DATA */
1918 if (likely(!type))
1919 local_inc(&tail_page->entries);
1920
1921 /*
Steven Rostedtfa743952009-06-16 12:37:57 -04001922 * If this is the first commit on the page, then update
1923 * its timestamp.
Steven Rostedt6634ff22009-05-06 15:30:07 -04001924 */
Steven Rostedtfa743952009-06-16 12:37:57 -04001925 if (!tail)
1926 tail_page->page->time_stamp = *ts;
Steven Rostedt6634ff22009-05-06 15:30:07 -04001927
1928 return event;
1929}
1930
Steven Rostedtedd813bf2009-06-02 23:00:53 -04001931static inline int
1932rb_try_to_discard(struct ring_buffer_per_cpu *cpu_buffer,
1933 struct ring_buffer_event *event)
1934{
1935 unsigned long new_index, old_index;
1936 struct buffer_page *bpage;
1937 unsigned long index;
1938 unsigned long addr;
1939
1940 new_index = rb_event_index(event);
1941 old_index = new_index + rb_event_length(event);
1942 addr = (unsigned long)event;
1943 addr &= PAGE_MASK;
1944
1945 bpage = cpu_buffer->tail_page;
1946
1947 if (bpage->page == (void *)addr && rb_page_write(bpage) == old_index) {
Steven Rostedt77ae3652009-03-27 11:00:29 -04001948 unsigned long write_mask =
1949 local_read(&bpage->write) & ~RB_WRITE_MASK;
Steven Rostedtedd813bf2009-06-02 23:00:53 -04001950 /*
1951 * This is on the tail page. It is possible that
1952 * a write could come in and move the tail page
1953 * and write to the next page. That is fine
1954 * because we just shorten what is on this page.
1955 */
Steven Rostedt77ae3652009-03-27 11:00:29 -04001956 old_index += write_mask;
1957 new_index += write_mask;
Steven Rostedtedd813bf2009-06-02 23:00:53 -04001958 index = local_cmpxchg(&bpage->write, old_index, new_index);
1959 if (index == old_index)
1960 return 1;
1961 }
1962
1963 /* could not discard */
1964 return 0;
1965}
1966
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001967static int
1968rb_add_time_stamp(struct ring_buffer_per_cpu *cpu_buffer,
1969 u64 *ts, u64 *delta)
1970{
1971 struct ring_buffer_event *event;
1972 static int once;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001973 int ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001974
1975 if (unlikely(*delta > (1ULL << 59) && !once++)) {
1976 printk(KERN_WARNING "Delta way too big! %llu"
1977 " ts=%llu write stamp = %llu\n",
Stephen Rothwelle2862c92008-10-27 17:43:28 +11001978 (unsigned long long)*delta,
1979 (unsigned long long)*ts,
1980 (unsigned long long)cpu_buffer->write_stamp);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001981 WARN_ON(1);
1982 }
1983
1984 /*
1985 * The delta is too big, we to add a
1986 * new timestamp.
1987 */
1988 event = __rb_reserve_next(cpu_buffer,
1989 RINGBUF_TYPE_TIME_EXTEND,
1990 RB_LEN_TIME_EXTEND,
1991 ts);
1992 if (!event)
Steven Rostedtbf41a152008-10-04 02:00:59 -04001993 return -EBUSY;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001994
Steven Rostedtbf41a152008-10-04 02:00:59 -04001995 if (PTR_ERR(event) == -EAGAIN)
1996 return -EAGAIN;
1997
1998 /* Only a commited time event can update the write stamp */
Steven Rostedtfa743952009-06-16 12:37:57 -04001999 if (rb_event_is_commit(cpu_buffer, event)) {
Steven Rostedtbf41a152008-10-04 02:00:59 -04002000 /*
Steven Rostedtfa743952009-06-16 12:37:57 -04002001 * If this is the first on the page, then it was
2002 * updated with the page itself. Try to discard it
2003 * and if we can't just make it zero.
Steven Rostedtbf41a152008-10-04 02:00:59 -04002004 */
2005 if (rb_event_index(event)) {
2006 event->time_delta = *delta & TS_MASK;
2007 event->array[0] = *delta >> TS_SHIFT;
2008 } else {
Steven Rostedtea05b572009-06-03 09:30:10 -04002009 /* try to discard, since we do not need this */
2010 if (!rb_try_to_discard(cpu_buffer, event)) {
2011 /* nope, just zero it */
2012 event->time_delta = 0;
2013 event->array[0] = 0;
2014 }
Steven Rostedtbf41a152008-10-04 02:00:59 -04002015 }
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002016 cpu_buffer->write_stamp = *ts;
Steven Rostedtbf41a152008-10-04 02:00:59 -04002017 /* let the caller know this was the commit */
2018 ret = 1;
2019 } else {
Steven Rostedtedd813bf2009-06-02 23:00:53 -04002020 /* Try to discard the event */
2021 if (!rb_try_to_discard(cpu_buffer, event)) {
2022 /* Darn, this is just wasted space */
2023 event->time_delta = 0;
2024 event->array[0] = 0;
Steven Rostedtedd813bf2009-06-02 23:00:53 -04002025 }
Steven Rostedtf57a8a12009-06-05 14:11:30 -04002026 ret = 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002027 }
2028
Steven Rostedtbf41a152008-10-04 02:00:59 -04002029 *delta = 0;
2030
2031 return ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002032}
2033
Steven Rostedtfa743952009-06-16 12:37:57 -04002034static void rb_start_commit(struct ring_buffer_per_cpu *cpu_buffer)
2035{
2036 local_inc(&cpu_buffer->committing);
2037 local_inc(&cpu_buffer->commits);
2038}
2039
2040static void rb_end_commit(struct ring_buffer_per_cpu *cpu_buffer)
2041{
2042 unsigned long commits;
2043
2044 if (RB_WARN_ON(cpu_buffer,
2045 !local_read(&cpu_buffer->committing)))
2046 return;
2047
2048 again:
2049 commits = local_read(&cpu_buffer->commits);
2050 /* synchronize with interrupts */
2051 barrier();
2052 if (local_read(&cpu_buffer->committing) == 1)
2053 rb_set_commit_to_write(cpu_buffer);
2054
2055 local_dec(&cpu_buffer->committing);
2056
2057 /* synchronize with interrupts */
2058 barrier();
2059
2060 /*
2061 * Need to account for interrupts coming in between the
2062 * updating of the commit page and the clearing of the
2063 * committing counter.
2064 */
2065 if (unlikely(local_read(&cpu_buffer->commits) != commits) &&
2066 !local_read(&cpu_buffer->committing)) {
2067 local_inc(&cpu_buffer->committing);
2068 goto again;
2069 }
2070}
2071
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002072static struct ring_buffer_event *
2073rb_reserve_next_event(struct ring_buffer_per_cpu *cpu_buffer,
Steven Rostedt1cd8d732009-05-11 14:08:09 -04002074 unsigned long length)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002075{
2076 struct ring_buffer_event *event;
Steven Rostedt168b6b12009-05-11 22:11:05 -04002077 u64 ts, delta = 0;
Steven Rostedtbf41a152008-10-04 02:00:59 -04002078 int commit = 0;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002079 int nr_loops = 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002080
Steven Rostedtfa743952009-06-16 12:37:57 -04002081 rb_start_commit(cpu_buffer);
2082
Steven Rostedtbe957c42009-05-11 14:42:53 -04002083 length = rb_calculate_event_length(length);
Steven Rostedtbf41a152008-10-04 02:00:59 -04002084 again:
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002085 /*
2086 * We allow for interrupts to reenter here and do a trace.
2087 * If one does, it will cause this original code to loop
2088 * back here. Even with heavy interrupts happening, this
2089 * should only happen a few times in a row. If this happens
2090 * 1000 times in a row, there must be either an interrupt
2091 * storm or we have something buggy.
2092 * Bail!
2093 */
Steven Rostedt3e89c7bb2008-11-11 15:28:41 -05002094 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 1000))
Steven Rostedtfa743952009-06-16 12:37:57 -04002095 goto out_fail;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002096
Steven Rostedt88eb0122009-05-11 16:28:23 -04002097 ts = rb_time_stamp(cpu_buffer->buffer, cpu_buffer->cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002098
Steven Rostedtbf41a152008-10-04 02:00:59 -04002099 /*
2100 * Only the first commit can update the timestamp.
2101 * Yes there is a race here. If an interrupt comes in
2102 * just after the conditional and it traces too, then it
2103 * will also check the deltas. More than one timestamp may
2104 * also be made. But only the entry that did the actual
2105 * commit will be something other than zero.
2106 */
Steven Rostedt0f0c85f2009-05-11 16:08:00 -04002107 if (likely(cpu_buffer->tail_page == cpu_buffer->commit_page &&
2108 rb_page_write(cpu_buffer->tail_page) ==
2109 rb_commit_index(cpu_buffer))) {
Steven Rostedt168b6b12009-05-11 22:11:05 -04002110 u64 diff;
Steven Rostedtbf41a152008-10-04 02:00:59 -04002111
Steven Rostedt168b6b12009-05-11 22:11:05 -04002112 diff = ts - cpu_buffer->write_stamp;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002113
Steven Rostedt168b6b12009-05-11 22:11:05 -04002114 /* make sure this diff is calculated here */
Steven Rostedtbf41a152008-10-04 02:00:59 -04002115 barrier();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002116
Steven Rostedtbf41a152008-10-04 02:00:59 -04002117 /* Did the write stamp get updated already? */
2118 if (unlikely(ts < cpu_buffer->write_stamp))
Steven Rostedt168b6b12009-05-11 22:11:05 -04002119 goto get_event;
Steven Rostedtbf41a152008-10-04 02:00:59 -04002120
Steven Rostedt168b6b12009-05-11 22:11:05 -04002121 delta = diff;
2122 if (unlikely(test_time_stamp(delta))) {
Steven Rostedtbf41a152008-10-04 02:00:59 -04002123
2124 commit = rb_add_time_stamp(cpu_buffer, &ts, &delta);
Steven Rostedtbf41a152008-10-04 02:00:59 -04002125 if (commit == -EBUSY)
Steven Rostedtfa743952009-06-16 12:37:57 -04002126 goto out_fail;
Steven Rostedtbf41a152008-10-04 02:00:59 -04002127
2128 if (commit == -EAGAIN)
2129 goto again;
2130
2131 RB_WARN_ON(cpu_buffer, commit < 0);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002132 }
Steven Rostedt168b6b12009-05-11 22:11:05 -04002133 }
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002134
Steven Rostedt168b6b12009-05-11 22:11:05 -04002135 get_event:
Steven Rostedt1cd8d732009-05-11 14:08:09 -04002136 event = __rb_reserve_next(cpu_buffer, 0, length, &ts);
Steven Rostedt168b6b12009-05-11 22:11:05 -04002137 if (unlikely(PTR_ERR(event) == -EAGAIN))
Steven Rostedtbf41a152008-10-04 02:00:59 -04002138 goto again;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002139
Steven Rostedtfa743952009-06-16 12:37:57 -04002140 if (!event)
2141 goto out_fail;
Steven Rostedtbf41a152008-10-04 02:00:59 -04002142
Steven Rostedtfa743952009-06-16 12:37:57 -04002143 if (!rb_event_is_commit(cpu_buffer, event))
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002144 delta = 0;
2145
2146 event->time_delta = delta;
2147
2148 return event;
Steven Rostedtfa743952009-06-16 12:37:57 -04002149
2150 out_fail:
2151 rb_end_commit(cpu_buffer);
2152 return NULL;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002153}
2154
Paul Mundt1155de42009-06-25 14:30:12 +09002155#ifdef CONFIG_TRACING
2156
Steven Rostedtaa18efb2009-04-20 16:16:11 -04002157#define TRACE_RECURSIVE_DEPTH 16
Steven Rostedt261842b2009-04-16 21:41:52 -04002158
2159static int trace_recursive_lock(void)
2160{
Steven Rostedtaa18efb2009-04-20 16:16:11 -04002161 current->trace_recursion++;
Steven Rostedt261842b2009-04-16 21:41:52 -04002162
Steven Rostedtaa18efb2009-04-20 16:16:11 -04002163 if (likely(current->trace_recursion < TRACE_RECURSIVE_DEPTH))
2164 return 0;
Steven Rostedt261842b2009-04-16 21:41:52 -04002165
Steven Rostedtaa18efb2009-04-20 16:16:11 -04002166 /* Disable all tracing before we do anything else */
2167 tracing_off_permanent();
Frederic Weisbeckere057a5e2009-04-19 23:38:12 +02002168
Steven Rostedt7d7d2b82009-04-27 12:37:49 -04002169 printk_once(KERN_WARNING "Tracing recursion: depth[%ld]:"
Steven Rostedtaa18efb2009-04-20 16:16:11 -04002170 "HC[%lu]:SC[%lu]:NMI[%lu]\n",
2171 current->trace_recursion,
2172 hardirq_count() >> HARDIRQ_SHIFT,
2173 softirq_count() >> SOFTIRQ_SHIFT,
2174 in_nmi());
Frederic Weisbeckere057a5e2009-04-19 23:38:12 +02002175
Steven Rostedtaa18efb2009-04-20 16:16:11 -04002176 WARN_ON_ONCE(1);
2177 return -1;
Steven Rostedt261842b2009-04-16 21:41:52 -04002178}
2179
2180static void trace_recursive_unlock(void)
2181{
Steven Rostedtaa18efb2009-04-20 16:16:11 -04002182 WARN_ON_ONCE(!current->trace_recursion);
Steven Rostedt261842b2009-04-16 21:41:52 -04002183
Steven Rostedtaa18efb2009-04-20 16:16:11 -04002184 current->trace_recursion--;
Steven Rostedt261842b2009-04-16 21:41:52 -04002185}
2186
Paul Mundt1155de42009-06-25 14:30:12 +09002187#else
2188
2189#define trace_recursive_lock() (0)
2190#define trace_recursive_unlock() do { } while (0)
2191
2192#endif
2193
Steven Rostedtbf41a152008-10-04 02:00:59 -04002194static DEFINE_PER_CPU(int, rb_need_resched);
2195
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002196/**
2197 * ring_buffer_lock_reserve - reserve a part of the buffer
2198 * @buffer: the ring buffer to reserve from
2199 * @length: the length of the data to reserve (excluding event header)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002200 *
2201 * Returns a reseverd event on the ring buffer to copy directly to.
2202 * The user of this interface will need to get the body to write into
2203 * and can use the ring_buffer_event_data() interface.
2204 *
2205 * The length is the length of the data needed, not the event length
2206 * which also includes the event header.
2207 *
2208 * Must be paired with ring_buffer_unlock_commit, unless NULL is returned.
2209 * If NULL is returned, then nothing has been allocated or locked.
2210 */
2211struct ring_buffer_event *
Arnaldo Carvalho de Melo0a987752009-02-05 16:12:56 -02002212ring_buffer_lock_reserve(struct ring_buffer *buffer, unsigned long length)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002213{
2214 struct ring_buffer_per_cpu *cpu_buffer;
2215 struct ring_buffer_event *event;
Steven Rostedtbf41a152008-10-04 02:00:59 -04002216 int cpu, resched;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002217
Steven Rostedt033601a2008-11-21 12:41:55 -05002218 if (ring_buffer_flags != RB_BUFFERS_ON)
Steven Rostedta3583242008-11-11 15:01:42 -05002219 return NULL;
2220
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002221 if (atomic_read(&buffer->record_disabled))
2222 return NULL;
2223
Steven Rostedtbf41a152008-10-04 02:00:59 -04002224 /* If we are tracing schedule, we don't want to recurse */
Steven Rostedt182e9f52008-11-03 23:15:56 -05002225 resched = ftrace_preempt_disable();
Steven Rostedtbf41a152008-10-04 02:00:59 -04002226
Steven Rostedt261842b2009-04-16 21:41:52 -04002227 if (trace_recursive_lock())
2228 goto out_nocheck;
2229
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002230 cpu = raw_smp_processor_id();
2231
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302232 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedtd7690412008-10-01 00:29:53 -04002233 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002234
2235 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002236
2237 if (atomic_read(&cpu_buffer->record_disabled))
Steven Rostedtd7690412008-10-01 00:29:53 -04002238 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002239
Steven Rostedtbe957c42009-05-11 14:42:53 -04002240 if (length > BUF_MAX_DATA_SIZE)
Steven Rostedtbf41a152008-10-04 02:00:59 -04002241 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002242
Steven Rostedt1cd8d732009-05-11 14:08:09 -04002243 event = rb_reserve_next_event(cpu_buffer, length);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002244 if (!event)
Steven Rostedtd7690412008-10-01 00:29:53 -04002245 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002246
Steven Rostedtbf41a152008-10-04 02:00:59 -04002247 /*
2248 * Need to store resched state on this cpu.
2249 * Only the first needs to.
2250 */
2251
2252 if (preempt_count() == 1)
2253 per_cpu(rb_need_resched, cpu) = resched;
2254
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002255 return event;
2256
Steven Rostedtd7690412008-10-01 00:29:53 -04002257 out:
Steven Rostedt261842b2009-04-16 21:41:52 -04002258 trace_recursive_unlock();
2259
2260 out_nocheck:
Steven Rostedt182e9f52008-11-03 23:15:56 -05002261 ftrace_preempt_enable(resched);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002262 return NULL;
2263}
Robert Richterc4f50182008-12-11 16:49:22 +01002264EXPORT_SYMBOL_GPL(ring_buffer_lock_reserve);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002265
2266static void rb_commit(struct ring_buffer_per_cpu *cpu_buffer,
2267 struct ring_buffer_event *event)
2268{
Steven Rostedte4906ef2009-04-30 20:49:44 -04002269 local_inc(&cpu_buffer->entries);
Steven Rostedtbf41a152008-10-04 02:00:59 -04002270
Steven Rostedtfa743952009-06-16 12:37:57 -04002271 /*
2272 * The event first in the commit queue updates the
2273 * time stamp.
2274 */
2275 if (rb_event_is_commit(cpu_buffer, event))
2276 cpu_buffer->write_stamp += event->time_delta;
Steven Rostedtbf41a152008-10-04 02:00:59 -04002277
Steven Rostedtfa743952009-06-16 12:37:57 -04002278 rb_end_commit(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002279}
2280
2281/**
2282 * ring_buffer_unlock_commit - commit a reserved
2283 * @buffer: The buffer to commit to
2284 * @event: The event pointer to commit.
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002285 *
2286 * This commits the data to the ring buffer, and releases any locks held.
2287 *
2288 * Must be paired with ring_buffer_lock_reserve.
2289 */
2290int ring_buffer_unlock_commit(struct ring_buffer *buffer,
Arnaldo Carvalho de Melo0a987752009-02-05 16:12:56 -02002291 struct ring_buffer_event *event)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002292{
2293 struct ring_buffer_per_cpu *cpu_buffer;
2294 int cpu = raw_smp_processor_id();
2295
2296 cpu_buffer = buffer->buffers[cpu];
2297
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002298 rb_commit(cpu_buffer, event);
2299
Steven Rostedt261842b2009-04-16 21:41:52 -04002300 trace_recursive_unlock();
2301
Steven Rostedtbf41a152008-10-04 02:00:59 -04002302 /*
2303 * Only the last preempt count needs to restore preemption.
2304 */
Steven Rostedt182e9f52008-11-03 23:15:56 -05002305 if (preempt_count() == 1)
2306 ftrace_preempt_enable(per_cpu(rb_need_resched, cpu));
2307 else
Steven Rostedtbf41a152008-10-04 02:00:59 -04002308 preempt_enable_no_resched_notrace();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002309
2310 return 0;
2311}
Robert Richterc4f50182008-12-11 16:49:22 +01002312EXPORT_SYMBOL_GPL(ring_buffer_unlock_commit);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002313
Frederic Weisbeckerf3b9aae2009-04-19 23:39:33 +02002314static inline void rb_event_discard(struct ring_buffer_event *event)
2315{
Lai Jiangshan334d4162009-04-24 11:27:05 +08002316 /* array[0] holds the actual length for the discarded event */
2317 event->array[0] = rb_event_data_length(event) - RB_EVNT_HDR_SIZE;
2318 event->type_len = RINGBUF_TYPE_PADDING;
Frederic Weisbeckerf3b9aae2009-04-19 23:39:33 +02002319 /* time delta must be non zero */
2320 if (!event->time_delta)
2321 event->time_delta = 1;
2322}
2323
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002324/**
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002325 * ring_buffer_event_discard - discard any event in the ring buffer
2326 * @event: the event to discard
2327 *
2328 * Sometimes a event that is in the ring buffer needs to be ignored.
2329 * This function lets the user discard an event in the ring buffer
2330 * and then that event will not be read later.
2331 *
2332 * Note, it is up to the user to be careful with this, and protect
2333 * against races. If the user discards an event that has been consumed
2334 * it is possible that it could corrupt the ring buffer.
2335 */
2336void ring_buffer_event_discard(struct ring_buffer_event *event)
2337{
Frederic Weisbeckerf3b9aae2009-04-19 23:39:33 +02002338 rb_event_discard(event);
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002339}
2340EXPORT_SYMBOL_GPL(ring_buffer_event_discard);
2341
2342/**
2343 * ring_buffer_commit_discard - discard an event that has not been committed
2344 * @buffer: the ring buffer
2345 * @event: non committed event to discard
2346 *
2347 * This is similar to ring_buffer_event_discard but must only be
2348 * performed on an event that has not been committed yet. The difference
2349 * is that this will also try to free the event from the ring buffer
2350 * if another event has not been added behind it.
2351 *
2352 * If another event has been added behind it, it will set the event
2353 * up as discarded, and perform the commit.
2354 *
2355 * If this function is called, do not call ring_buffer_unlock_commit on
2356 * the event.
2357 */
2358void ring_buffer_discard_commit(struct ring_buffer *buffer,
2359 struct ring_buffer_event *event)
2360{
2361 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002362 int cpu;
2363
2364 /* The event is discarded regardless */
Frederic Weisbeckerf3b9aae2009-04-19 23:39:33 +02002365 rb_event_discard(event);
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002366
Steven Rostedtfa743952009-06-16 12:37:57 -04002367 cpu = smp_processor_id();
2368 cpu_buffer = buffer->buffers[cpu];
2369
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002370 /*
2371 * This must only be called if the event has not been
2372 * committed yet. Thus we can assume that preemption
2373 * is still disabled.
2374 */
Steven Rostedtfa743952009-06-16 12:37:57 -04002375 RB_WARN_ON(buffer, !local_read(&cpu_buffer->committing));
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002376
Steven Rostedtedd813bf2009-06-02 23:00:53 -04002377 if (!rb_try_to_discard(cpu_buffer, event))
2378 goto out;
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002379
2380 /*
2381 * The commit is still visible by the reader, so we
2382 * must increment entries.
2383 */
Steven Rostedte4906ef2009-04-30 20:49:44 -04002384 local_inc(&cpu_buffer->entries);
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002385 out:
Steven Rostedtfa743952009-06-16 12:37:57 -04002386 rb_end_commit(cpu_buffer);
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002387
Frederic Weisbeckerf3b9aae2009-04-19 23:39:33 +02002388 trace_recursive_unlock();
2389
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002390 /*
2391 * Only the last preempt count needs to restore preemption.
2392 */
2393 if (preempt_count() == 1)
2394 ftrace_preempt_enable(per_cpu(rb_need_resched, cpu));
2395 else
2396 preempt_enable_no_resched_notrace();
2397
2398}
2399EXPORT_SYMBOL_GPL(ring_buffer_discard_commit);
2400
2401/**
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002402 * ring_buffer_write - write data to the buffer without reserving
2403 * @buffer: The ring buffer to write to.
2404 * @length: The length of the data being written (excluding the event header)
2405 * @data: The data to write to the buffer.
2406 *
2407 * This is like ring_buffer_lock_reserve and ring_buffer_unlock_commit as
2408 * one function. If you already have the data to write to the buffer, it
2409 * may be easier to simply call this function.
2410 *
2411 * Note, like ring_buffer_lock_reserve, the length is the length of the data
2412 * and not the length of the event which would hold the header.
2413 */
2414int ring_buffer_write(struct ring_buffer *buffer,
2415 unsigned long length,
2416 void *data)
2417{
2418 struct ring_buffer_per_cpu *cpu_buffer;
2419 struct ring_buffer_event *event;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002420 void *body;
2421 int ret = -EBUSY;
Steven Rostedtbf41a152008-10-04 02:00:59 -04002422 int cpu, resched;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002423
Steven Rostedt033601a2008-11-21 12:41:55 -05002424 if (ring_buffer_flags != RB_BUFFERS_ON)
Steven Rostedta3583242008-11-11 15:01:42 -05002425 return -EBUSY;
2426
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002427 if (atomic_read(&buffer->record_disabled))
2428 return -EBUSY;
2429
Steven Rostedt182e9f52008-11-03 23:15:56 -05002430 resched = ftrace_preempt_disable();
Steven Rostedtbf41a152008-10-04 02:00:59 -04002431
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002432 cpu = raw_smp_processor_id();
2433
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302434 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedtd7690412008-10-01 00:29:53 -04002435 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002436
2437 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002438
2439 if (atomic_read(&cpu_buffer->record_disabled))
2440 goto out;
2441
Steven Rostedtbe957c42009-05-11 14:42:53 -04002442 if (length > BUF_MAX_DATA_SIZE)
2443 goto out;
2444
2445 event = rb_reserve_next_event(cpu_buffer, length);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002446 if (!event)
2447 goto out;
2448
2449 body = rb_event_data(event);
2450
2451 memcpy(body, data, length);
2452
2453 rb_commit(cpu_buffer, event);
2454
2455 ret = 0;
2456 out:
Steven Rostedt182e9f52008-11-03 23:15:56 -05002457 ftrace_preempt_enable(resched);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002458
2459 return ret;
2460}
Robert Richterc4f50182008-12-11 16:49:22 +01002461EXPORT_SYMBOL_GPL(ring_buffer_write);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002462
Andrew Morton34a148b2009-01-09 12:27:09 -08002463static int rb_per_cpu_empty(struct ring_buffer_per_cpu *cpu_buffer)
Steven Rostedtbf41a152008-10-04 02:00:59 -04002464{
2465 struct buffer_page *reader = cpu_buffer->reader_page;
Steven Rostedt77ae3652009-03-27 11:00:29 -04002466 struct buffer_page *head = rb_set_head_page(cpu_buffer);
Steven Rostedtbf41a152008-10-04 02:00:59 -04002467 struct buffer_page *commit = cpu_buffer->commit_page;
2468
Steven Rostedt77ae3652009-03-27 11:00:29 -04002469 /* In case of error, head will be NULL */
2470 if (unlikely(!head))
2471 return 1;
2472
Steven Rostedtbf41a152008-10-04 02:00:59 -04002473 return reader->read == rb_page_commit(reader) &&
2474 (commit == reader ||
2475 (commit == head &&
2476 head->read == rb_page_commit(commit)));
2477}
2478
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002479/**
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002480 * ring_buffer_record_disable - stop all writes into the buffer
2481 * @buffer: The ring buffer to stop writes to.
2482 *
2483 * This prevents all writes to the buffer. Any attempt to write
2484 * to the buffer after this will fail and return NULL.
2485 *
2486 * The caller should call synchronize_sched() after this.
2487 */
2488void ring_buffer_record_disable(struct ring_buffer *buffer)
2489{
2490 atomic_inc(&buffer->record_disabled);
2491}
Robert Richterc4f50182008-12-11 16:49:22 +01002492EXPORT_SYMBOL_GPL(ring_buffer_record_disable);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002493
2494/**
2495 * ring_buffer_record_enable - enable writes to the buffer
2496 * @buffer: The ring buffer to enable writes
2497 *
2498 * Note, multiple disables will need the same number of enables
2499 * to truely enable the writing (much like preempt_disable).
2500 */
2501void ring_buffer_record_enable(struct ring_buffer *buffer)
2502{
2503 atomic_dec(&buffer->record_disabled);
2504}
Robert Richterc4f50182008-12-11 16:49:22 +01002505EXPORT_SYMBOL_GPL(ring_buffer_record_enable);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002506
2507/**
2508 * ring_buffer_record_disable_cpu - stop all writes into the cpu_buffer
2509 * @buffer: The ring buffer to stop writes to.
2510 * @cpu: The CPU buffer to stop
2511 *
2512 * This prevents all writes to the buffer. Any attempt to write
2513 * to the buffer after this will fail and return NULL.
2514 *
2515 * The caller should call synchronize_sched() after this.
2516 */
2517void ring_buffer_record_disable_cpu(struct ring_buffer *buffer, int cpu)
2518{
2519 struct ring_buffer_per_cpu *cpu_buffer;
2520
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302521 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04002522 return;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002523
2524 cpu_buffer = buffer->buffers[cpu];
2525 atomic_inc(&cpu_buffer->record_disabled);
2526}
Robert Richterc4f50182008-12-11 16:49:22 +01002527EXPORT_SYMBOL_GPL(ring_buffer_record_disable_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002528
2529/**
2530 * ring_buffer_record_enable_cpu - enable writes to the buffer
2531 * @buffer: The ring buffer to enable writes
2532 * @cpu: The CPU to enable.
2533 *
2534 * Note, multiple disables will need the same number of enables
2535 * to truely enable the writing (much like preempt_disable).
2536 */
2537void ring_buffer_record_enable_cpu(struct ring_buffer *buffer, int cpu)
2538{
2539 struct ring_buffer_per_cpu *cpu_buffer;
2540
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302541 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04002542 return;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002543
2544 cpu_buffer = buffer->buffers[cpu];
2545 atomic_dec(&cpu_buffer->record_disabled);
2546}
Robert Richterc4f50182008-12-11 16:49:22 +01002547EXPORT_SYMBOL_GPL(ring_buffer_record_enable_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002548
2549/**
2550 * ring_buffer_entries_cpu - get the number of entries in a cpu buffer
2551 * @buffer: The ring buffer
2552 * @cpu: The per CPU buffer to get the entries from.
2553 */
2554unsigned long ring_buffer_entries_cpu(struct ring_buffer *buffer, int cpu)
2555{
2556 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedt8aabee52009-03-12 13:13:49 -04002557 unsigned long ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002558
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302559 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04002560 return 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002561
2562 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt77ae3652009-03-27 11:00:29 -04002563 ret = (local_read(&cpu_buffer->entries) - local_read(&cpu_buffer->overrun))
Steven Rostedte4906ef2009-04-30 20:49:44 -04002564 - cpu_buffer->read;
Steven Rostedt554f7862009-03-11 22:00:13 -04002565
2566 return ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002567}
Robert Richterc4f50182008-12-11 16:49:22 +01002568EXPORT_SYMBOL_GPL(ring_buffer_entries_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002569
2570/**
2571 * ring_buffer_overrun_cpu - get the number of overruns in a cpu_buffer
2572 * @buffer: The ring buffer
2573 * @cpu: The per CPU buffer to get the number of overruns from
2574 */
2575unsigned long ring_buffer_overrun_cpu(struct ring_buffer *buffer, int cpu)
2576{
2577 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedt8aabee52009-03-12 13:13:49 -04002578 unsigned long ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002579
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302580 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04002581 return 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002582
2583 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt77ae3652009-03-27 11:00:29 -04002584 ret = local_read(&cpu_buffer->overrun);
Steven Rostedt554f7862009-03-11 22:00:13 -04002585
2586 return ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002587}
Robert Richterc4f50182008-12-11 16:49:22 +01002588EXPORT_SYMBOL_GPL(ring_buffer_overrun_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002589
2590/**
Steven Rostedtf0d2c682009-04-29 13:43:37 -04002591 * ring_buffer_commit_overrun_cpu - get the number of overruns caused by commits
2592 * @buffer: The ring buffer
2593 * @cpu: The per CPU buffer to get the number of overruns from
2594 */
2595unsigned long
2596ring_buffer_commit_overrun_cpu(struct ring_buffer *buffer, int cpu)
2597{
2598 struct ring_buffer_per_cpu *cpu_buffer;
2599 unsigned long ret;
2600
2601 if (!cpumask_test_cpu(cpu, buffer->cpumask))
2602 return 0;
2603
2604 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt77ae3652009-03-27 11:00:29 -04002605 ret = local_read(&cpu_buffer->commit_overrun);
Steven Rostedtf0d2c682009-04-29 13:43:37 -04002606
2607 return ret;
2608}
2609EXPORT_SYMBOL_GPL(ring_buffer_commit_overrun_cpu);
2610
2611/**
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002612 * ring_buffer_entries - get the number of entries in a buffer
2613 * @buffer: The ring buffer
2614 *
2615 * Returns the total number of entries in the ring buffer
2616 * (all CPU entries)
2617 */
2618unsigned long ring_buffer_entries(struct ring_buffer *buffer)
2619{
2620 struct ring_buffer_per_cpu *cpu_buffer;
2621 unsigned long entries = 0;
2622 int cpu;
2623
2624 /* if you care about this being correct, lock the buffer */
2625 for_each_buffer_cpu(buffer, cpu) {
2626 cpu_buffer = buffer->buffers[cpu];
Steven Rostedte4906ef2009-04-30 20:49:44 -04002627 entries += (local_read(&cpu_buffer->entries) -
Steven Rostedt77ae3652009-03-27 11:00:29 -04002628 local_read(&cpu_buffer->overrun)) - cpu_buffer->read;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002629 }
2630
2631 return entries;
2632}
Robert Richterc4f50182008-12-11 16:49:22 +01002633EXPORT_SYMBOL_GPL(ring_buffer_entries);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002634
2635/**
2636 * ring_buffer_overrun_cpu - get the number of overruns in buffer
2637 * @buffer: The ring buffer
2638 *
2639 * Returns the total number of overruns in the ring buffer
2640 * (all CPU entries)
2641 */
2642unsigned long ring_buffer_overruns(struct ring_buffer *buffer)
2643{
2644 struct ring_buffer_per_cpu *cpu_buffer;
2645 unsigned long overruns = 0;
2646 int cpu;
2647
2648 /* if you care about this being correct, lock the buffer */
2649 for_each_buffer_cpu(buffer, cpu) {
2650 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt77ae3652009-03-27 11:00:29 -04002651 overruns += local_read(&cpu_buffer->overrun);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002652 }
2653
2654 return overruns;
2655}
Robert Richterc4f50182008-12-11 16:49:22 +01002656EXPORT_SYMBOL_GPL(ring_buffer_overruns);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002657
Steven Rostedt642edba2008-11-12 00:01:26 -05002658static void rb_iter_reset(struct ring_buffer_iter *iter)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002659{
2660 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
2661
Steven Rostedtd7690412008-10-01 00:29:53 -04002662 /* Iterator usage is expected to have record disabled */
2663 if (list_empty(&cpu_buffer->reader_page->list)) {
Steven Rostedt77ae3652009-03-27 11:00:29 -04002664 iter->head_page = rb_set_head_page(cpu_buffer);
2665 if (unlikely(!iter->head_page))
2666 return;
2667 iter->head = iter->head_page->read;
Steven Rostedtd7690412008-10-01 00:29:53 -04002668 } else {
2669 iter->head_page = cpu_buffer->reader_page;
Steven Rostedt6f807ac2008-10-04 02:00:58 -04002670 iter->head = cpu_buffer->reader_page->read;
Steven Rostedtd7690412008-10-01 00:29:53 -04002671 }
2672 if (iter->head)
2673 iter->read_stamp = cpu_buffer->read_stamp;
2674 else
Steven Rostedtabc9b562008-12-02 15:34:06 -05002675 iter->read_stamp = iter->head_page->page->time_stamp;
Steven Rostedt642edba2008-11-12 00:01:26 -05002676}
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002677
Steven Rostedt642edba2008-11-12 00:01:26 -05002678/**
2679 * ring_buffer_iter_reset - reset an iterator
2680 * @iter: The iterator to reset
2681 *
2682 * Resets the iterator, so that it will start from the beginning
2683 * again.
2684 */
2685void ring_buffer_iter_reset(struct ring_buffer_iter *iter)
2686{
Steven Rostedt554f7862009-03-11 22:00:13 -04002687 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedt642edba2008-11-12 00:01:26 -05002688 unsigned long flags;
2689
Steven Rostedt554f7862009-03-11 22:00:13 -04002690 if (!iter)
2691 return;
2692
2693 cpu_buffer = iter->cpu_buffer;
2694
Steven Rostedt642edba2008-11-12 00:01:26 -05002695 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
2696 rb_iter_reset(iter);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002697 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002698}
Robert Richterc4f50182008-12-11 16:49:22 +01002699EXPORT_SYMBOL_GPL(ring_buffer_iter_reset);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002700
2701/**
2702 * ring_buffer_iter_empty - check if an iterator has no more to read
2703 * @iter: The iterator to check
2704 */
2705int ring_buffer_iter_empty(struct ring_buffer_iter *iter)
2706{
2707 struct ring_buffer_per_cpu *cpu_buffer;
2708
2709 cpu_buffer = iter->cpu_buffer;
2710
Steven Rostedtbf41a152008-10-04 02:00:59 -04002711 return iter->head_page == cpu_buffer->commit_page &&
2712 iter->head == rb_commit_index(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002713}
Robert Richterc4f50182008-12-11 16:49:22 +01002714EXPORT_SYMBOL_GPL(ring_buffer_iter_empty);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002715
2716static void
2717rb_update_read_stamp(struct ring_buffer_per_cpu *cpu_buffer,
2718 struct ring_buffer_event *event)
2719{
2720 u64 delta;
2721
Lai Jiangshan334d4162009-04-24 11:27:05 +08002722 switch (event->type_len) {
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002723 case RINGBUF_TYPE_PADDING:
2724 return;
2725
2726 case RINGBUF_TYPE_TIME_EXTEND:
2727 delta = event->array[0];
2728 delta <<= TS_SHIFT;
2729 delta += event->time_delta;
2730 cpu_buffer->read_stamp += delta;
2731 return;
2732
2733 case RINGBUF_TYPE_TIME_STAMP:
2734 /* FIXME: not implemented */
2735 return;
2736
2737 case RINGBUF_TYPE_DATA:
2738 cpu_buffer->read_stamp += event->time_delta;
2739 return;
2740
2741 default:
2742 BUG();
2743 }
2744 return;
2745}
2746
2747static void
2748rb_update_iter_read_stamp(struct ring_buffer_iter *iter,
2749 struct ring_buffer_event *event)
2750{
2751 u64 delta;
2752
Lai Jiangshan334d4162009-04-24 11:27:05 +08002753 switch (event->type_len) {
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002754 case RINGBUF_TYPE_PADDING:
2755 return;
2756
2757 case RINGBUF_TYPE_TIME_EXTEND:
2758 delta = event->array[0];
2759 delta <<= TS_SHIFT;
2760 delta += event->time_delta;
2761 iter->read_stamp += delta;
2762 return;
2763
2764 case RINGBUF_TYPE_TIME_STAMP:
2765 /* FIXME: not implemented */
2766 return;
2767
2768 case RINGBUF_TYPE_DATA:
2769 iter->read_stamp += event->time_delta;
2770 return;
2771
2772 default:
2773 BUG();
2774 }
2775 return;
2776}
2777
Steven Rostedtd7690412008-10-01 00:29:53 -04002778static struct buffer_page *
2779rb_get_reader_page(struct ring_buffer_per_cpu *cpu_buffer)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002780{
Steven Rostedtd7690412008-10-01 00:29:53 -04002781 struct buffer_page *reader = NULL;
2782 unsigned long flags;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002783 int nr_loops = 0;
Steven Rostedt77ae3652009-03-27 11:00:29 -04002784 int ret;
Steven Rostedtd7690412008-10-01 00:29:53 -04002785
Steven Rostedt3e03fb72008-11-06 00:09:43 -05002786 local_irq_save(flags);
2787 __raw_spin_lock(&cpu_buffer->lock);
Steven Rostedtd7690412008-10-01 00:29:53 -04002788
2789 again:
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002790 /*
2791 * This should normally only loop twice. But because the
2792 * start of the reader inserts an empty page, it causes
2793 * a case where we will loop three times. There should be no
2794 * reason to loop four times (that I know of).
2795 */
Steven Rostedt3e89c7bb2008-11-11 15:28:41 -05002796 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 3)) {
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002797 reader = NULL;
2798 goto out;
2799 }
2800
Steven Rostedtd7690412008-10-01 00:29:53 -04002801 reader = cpu_buffer->reader_page;
2802
2803 /* If there's more to read, return this page */
Steven Rostedtbf41a152008-10-04 02:00:59 -04002804 if (cpu_buffer->reader_page->read < rb_page_size(reader))
Steven Rostedtd7690412008-10-01 00:29:53 -04002805 goto out;
2806
2807 /* Never should we have an index greater than the size */
Steven Rostedt3e89c7bb2008-11-11 15:28:41 -05002808 if (RB_WARN_ON(cpu_buffer,
2809 cpu_buffer->reader_page->read > rb_page_size(reader)))
2810 goto out;
Steven Rostedtd7690412008-10-01 00:29:53 -04002811
2812 /* check if we caught up to the tail */
2813 reader = NULL;
Steven Rostedtbf41a152008-10-04 02:00:59 -04002814 if (cpu_buffer->commit_page == cpu_buffer->reader_page)
Steven Rostedtd7690412008-10-01 00:29:53 -04002815 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002816
2817 /*
Steven Rostedtd7690412008-10-01 00:29:53 -04002818 * Reset the reader page to size zero.
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002819 */
Steven Rostedt77ae3652009-03-27 11:00:29 -04002820 local_set(&cpu_buffer->reader_page->write, 0);
2821 local_set(&cpu_buffer->reader_page->entries, 0);
2822 local_set(&cpu_buffer->reader_page->page->commit, 0);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002823
Steven Rostedt77ae3652009-03-27 11:00:29 -04002824 spin:
2825 /*
2826 * Splice the empty reader page into the list around the head.
2827 */
2828 reader = rb_set_head_page(cpu_buffer);
Steven Rostedtd7690412008-10-01 00:29:53 -04002829 cpu_buffer->reader_page->list.next = reader->list.next;
2830 cpu_buffer->reader_page->list.prev = reader->list.prev;
Steven Rostedtbf41a152008-10-04 02:00:59 -04002831
Steven Rostedt3adc54f2009-03-30 15:32:01 -04002832 /*
2833 * cpu_buffer->pages just needs to point to the buffer, it
2834 * has no specific buffer page to point to. Lets move it out
2835 * of our way so we don't accidently swap it.
2836 */
2837 cpu_buffer->pages = reader->list.prev;
2838
Steven Rostedt77ae3652009-03-27 11:00:29 -04002839 /* The reader page will be pointing to the new head */
2840 rb_set_list_to_head(cpu_buffer, &cpu_buffer->reader_page->list);
Steven Rostedtd7690412008-10-01 00:29:53 -04002841
2842 /*
Steven Rostedt77ae3652009-03-27 11:00:29 -04002843 * Here's the tricky part.
2844 *
2845 * We need to move the pointer past the header page.
2846 * But we can only do that if a writer is not currently
2847 * moving it. The page before the header page has the
2848 * flag bit '1' set if it is pointing to the page we want.
2849 * but if the writer is in the process of moving it
2850 * than it will be '2' or already moved '0'.
Steven Rostedtd7690412008-10-01 00:29:53 -04002851 */
Steven Rostedtd7690412008-10-01 00:29:53 -04002852
Steven Rostedt77ae3652009-03-27 11:00:29 -04002853 ret = rb_head_page_replace(reader, cpu_buffer->reader_page);
2854
2855 /*
2856 * If we did not convert it, then we must try again.
2857 */
2858 if (!ret)
2859 goto spin;
2860
2861 /*
2862 * Yeah! We succeeded in replacing the page.
2863 *
2864 * Now make the new head point back to the reader page.
2865 */
2866 reader->list.next->prev = &cpu_buffer->reader_page->list;
2867 rb_inc_page(cpu_buffer, &cpu_buffer->head_page);
Steven Rostedtd7690412008-10-01 00:29:53 -04002868
2869 /* Finally update the reader page to the new head */
2870 cpu_buffer->reader_page = reader;
2871 rb_reset_reader_page(cpu_buffer);
2872
2873 goto again;
2874
2875 out:
Steven Rostedt3e03fb72008-11-06 00:09:43 -05002876 __raw_spin_unlock(&cpu_buffer->lock);
2877 local_irq_restore(flags);
Steven Rostedtd7690412008-10-01 00:29:53 -04002878
2879 return reader;
2880}
2881
2882static void rb_advance_reader(struct ring_buffer_per_cpu *cpu_buffer)
2883{
2884 struct ring_buffer_event *event;
2885 struct buffer_page *reader;
2886 unsigned length;
2887
2888 reader = rb_get_reader_page(cpu_buffer);
2889
2890 /* This function should not be called when buffer is empty */
Steven Rostedt3e89c7bb2008-11-11 15:28:41 -05002891 if (RB_WARN_ON(cpu_buffer, !reader))
2892 return;
Steven Rostedtd7690412008-10-01 00:29:53 -04002893
2894 event = rb_reader_event(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002895
Lai Jiangshan334d4162009-04-24 11:27:05 +08002896 if (event->type_len <= RINGBUF_TYPE_DATA_TYPE_LEN_MAX
2897 || rb_discarded_event(event))
Steven Rostedte4906ef2009-04-30 20:49:44 -04002898 cpu_buffer->read++;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002899
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002900 rb_update_read_stamp(cpu_buffer, event);
2901
Steven Rostedtd7690412008-10-01 00:29:53 -04002902 length = rb_event_length(event);
Steven Rostedt6f807ac2008-10-04 02:00:58 -04002903 cpu_buffer->reader_page->read += length;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002904}
2905
2906static void rb_advance_iter(struct ring_buffer_iter *iter)
2907{
2908 struct ring_buffer *buffer;
2909 struct ring_buffer_per_cpu *cpu_buffer;
2910 struct ring_buffer_event *event;
2911 unsigned length;
2912
2913 cpu_buffer = iter->cpu_buffer;
2914 buffer = cpu_buffer->buffer;
2915
2916 /*
2917 * Check if we are at the end of the buffer.
2918 */
Steven Rostedtbf41a152008-10-04 02:00:59 -04002919 if (iter->head >= rb_page_size(iter->head_page)) {
Steven Rostedtea05b572009-06-03 09:30:10 -04002920 /* discarded commits can make the page empty */
2921 if (iter->head_page == cpu_buffer->commit_page)
Steven Rostedt3e89c7bb2008-11-11 15:28:41 -05002922 return;
Steven Rostedtd7690412008-10-01 00:29:53 -04002923 rb_inc_iter(iter);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002924 return;
2925 }
2926
2927 event = rb_iter_head_event(iter);
2928
2929 length = rb_event_length(event);
2930
2931 /*
2932 * This should not be called to advance the header if we are
2933 * at the tail of the buffer.
2934 */
Steven Rostedt3e89c7bb2008-11-11 15:28:41 -05002935 if (RB_WARN_ON(cpu_buffer,
Steven Rostedtf536aaf2008-11-10 23:07:30 -05002936 (iter->head_page == cpu_buffer->commit_page) &&
Steven Rostedt3e89c7bb2008-11-11 15:28:41 -05002937 (iter->head + length > rb_commit_index(cpu_buffer))))
2938 return;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002939
2940 rb_update_iter_read_stamp(iter, event);
2941
2942 iter->head += length;
2943
2944 /* check for end of page padding */
Steven Rostedtbf41a152008-10-04 02:00:59 -04002945 if ((iter->head >= rb_page_size(iter->head_page)) &&
2946 (iter->head_page != cpu_buffer->commit_page))
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002947 rb_advance_iter(iter);
2948}
2949
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002950static struct ring_buffer_event *
2951rb_buffer_peek(struct ring_buffer *buffer, int cpu, u64 *ts)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002952{
2953 struct ring_buffer_per_cpu *cpu_buffer;
2954 struct ring_buffer_event *event;
Steven Rostedtd7690412008-10-01 00:29:53 -04002955 struct buffer_page *reader;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002956 int nr_loops = 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002957
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002958 cpu_buffer = buffer->buffers[cpu];
2959
2960 again:
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002961 /*
2962 * We repeat when a timestamp is encountered. It is possible
2963 * to get multiple timestamps from an interrupt entering just
Steven Rostedtea05b572009-06-03 09:30:10 -04002964 * as one timestamp is about to be written, or from discarded
2965 * commits. The most that we can have is the number on a single page.
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002966 */
Steven Rostedtea05b572009-06-03 09:30:10 -04002967 if (RB_WARN_ON(cpu_buffer, ++nr_loops > RB_TIMESTAMPS_PER_PAGE))
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002968 return NULL;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002969
Steven Rostedtd7690412008-10-01 00:29:53 -04002970 reader = rb_get_reader_page(cpu_buffer);
2971 if (!reader)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002972 return NULL;
2973
Steven Rostedtd7690412008-10-01 00:29:53 -04002974 event = rb_reader_event(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002975
Lai Jiangshan334d4162009-04-24 11:27:05 +08002976 switch (event->type_len) {
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002977 case RINGBUF_TYPE_PADDING:
Tom Zanussi2d622712009-03-22 03:30:49 -05002978 if (rb_null_event(event))
2979 RB_WARN_ON(cpu_buffer, 1);
2980 /*
2981 * Because the writer could be discarding every
2982 * event it creates (which would probably be bad)
2983 * if we were to go back to "again" then we may never
2984 * catch up, and will trigger the warn on, or lock
2985 * the box. Return the padding, and we will release
2986 * the current locks, and try again.
2987 */
Steven Rostedtd7690412008-10-01 00:29:53 -04002988 rb_advance_reader(cpu_buffer);
Tom Zanussi2d622712009-03-22 03:30:49 -05002989 return event;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002990
2991 case RINGBUF_TYPE_TIME_EXTEND:
2992 /* Internal data, OK to advance */
Steven Rostedtd7690412008-10-01 00:29:53 -04002993 rb_advance_reader(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002994 goto again;
2995
2996 case RINGBUF_TYPE_TIME_STAMP:
2997 /* FIXME: not implemented */
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_DATA:
3002 if (ts) {
3003 *ts = cpu_buffer->read_stamp + event->time_delta;
Steven Rostedt37886f62009-03-17 17:22:06 -04003004 ring_buffer_normalize_time_stamp(buffer,
3005 cpu_buffer->cpu, ts);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003006 }
3007 return event;
3008
3009 default:
3010 BUG();
3011 }
3012
3013 return NULL;
3014}
Robert Richterc4f50182008-12-11 16:49:22 +01003015EXPORT_SYMBOL_GPL(ring_buffer_peek);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003016
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003017static struct ring_buffer_event *
3018rb_iter_peek(struct ring_buffer_iter *iter, u64 *ts)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003019{
3020 struct ring_buffer *buffer;
3021 struct ring_buffer_per_cpu *cpu_buffer;
3022 struct ring_buffer_event *event;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003023 int nr_loops = 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003024
3025 if (ring_buffer_iter_empty(iter))
3026 return NULL;
3027
3028 cpu_buffer = iter->cpu_buffer;
3029 buffer = cpu_buffer->buffer;
3030
3031 again:
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003032 /*
Steven Rostedtea05b572009-06-03 09:30:10 -04003033 * We repeat when a timestamp is encountered.
3034 * We can get multiple timestamps by nested interrupts or also
3035 * if filtering is on (discarding commits). Since discarding
3036 * commits can be frequent we can get a lot of timestamps.
3037 * But we limit them by not adding timestamps if they begin
3038 * at the start of a page.
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003039 */
Steven Rostedtea05b572009-06-03 09:30:10 -04003040 if (RB_WARN_ON(cpu_buffer, ++nr_loops > RB_TIMESTAMPS_PER_PAGE))
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003041 return NULL;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003042
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003043 if (rb_per_cpu_empty(cpu_buffer))
3044 return NULL;
3045
3046 event = rb_iter_head_event(iter);
3047
Lai Jiangshan334d4162009-04-24 11:27:05 +08003048 switch (event->type_len) {
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003049 case RINGBUF_TYPE_PADDING:
Tom Zanussi2d622712009-03-22 03:30:49 -05003050 if (rb_null_event(event)) {
3051 rb_inc_iter(iter);
3052 goto again;
3053 }
3054 rb_advance_iter(iter);
3055 return event;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003056
3057 case RINGBUF_TYPE_TIME_EXTEND:
3058 /* Internal data, OK to advance */
3059 rb_advance_iter(iter);
3060 goto again;
3061
3062 case RINGBUF_TYPE_TIME_STAMP:
3063 /* FIXME: not implemented */
3064 rb_advance_iter(iter);
3065 goto again;
3066
3067 case RINGBUF_TYPE_DATA:
3068 if (ts) {
3069 *ts = iter->read_stamp + event->time_delta;
Steven Rostedt37886f62009-03-17 17:22:06 -04003070 ring_buffer_normalize_time_stamp(buffer,
3071 cpu_buffer->cpu, ts);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003072 }
3073 return event;
3074
3075 default:
3076 BUG();
3077 }
3078
3079 return NULL;
3080}
Robert Richterc4f50182008-12-11 16:49:22 +01003081EXPORT_SYMBOL_GPL(ring_buffer_iter_peek);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003082
Steven Rostedt8d707e82009-06-16 21:22:48 -04003083static inline int rb_ok_to_lock(void)
3084{
3085 /*
3086 * If an NMI die dumps out the content of the ring buffer
3087 * do not grab locks. We also permanently disable the ring
3088 * buffer too. A one time deal is all you get from reading
3089 * the ring buffer from an NMI.
3090 */
3091 if (likely(!in_nmi() && !oops_in_progress))
3092 return 1;
3093
3094 tracing_off_permanent();
3095 return 0;
3096}
3097
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003098/**
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003099 * ring_buffer_peek - peek at the next event to be read
3100 * @buffer: The ring buffer to read
3101 * @cpu: The cpu to peak at
3102 * @ts: The timestamp counter of this event.
3103 *
3104 * This will return the event that will be read next, but does
3105 * not consume the data.
3106 */
3107struct ring_buffer_event *
3108ring_buffer_peek(struct ring_buffer *buffer, int cpu, u64 *ts)
3109{
3110 struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
Steven Rostedt8aabee52009-03-12 13:13:49 -04003111 struct ring_buffer_event *event;
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003112 unsigned long flags;
Steven Rostedt8d707e82009-06-16 21:22:48 -04003113 int dolock;
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003114
Steven Rostedt554f7862009-03-11 22:00:13 -04003115 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04003116 return NULL;
Steven Rostedt554f7862009-03-11 22:00:13 -04003117
Steven Rostedt8d707e82009-06-16 21:22:48 -04003118 dolock = rb_ok_to_lock();
Tom Zanussi2d622712009-03-22 03:30:49 -05003119 again:
Steven Rostedt8d707e82009-06-16 21:22:48 -04003120 local_irq_save(flags);
3121 if (dolock)
3122 spin_lock(&cpu_buffer->reader_lock);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003123 event = rb_buffer_peek(buffer, cpu, ts);
Steven Rostedt8d707e82009-06-16 21:22:48 -04003124 if (dolock)
3125 spin_unlock(&cpu_buffer->reader_lock);
3126 local_irq_restore(flags);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003127
Lai Jiangshan334d4162009-04-24 11:27:05 +08003128 if (event && event->type_len == RINGBUF_TYPE_PADDING) {
Tom Zanussi2d622712009-03-22 03:30:49 -05003129 cpu_relax();
3130 goto again;
3131 }
3132
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003133 return event;
3134}
3135
3136/**
3137 * ring_buffer_iter_peek - peek at the next event to be read
3138 * @iter: The ring buffer iterator
3139 * @ts: The timestamp counter of this event.
3140 *
3141 * This will return the event that will be read next, but does
3142 * not increment the iterator.
3143 */
3144struct ring_buffer_event *
3145ring_buffer_iter_peek(struct ring_buffer_iter *iter, u64 *ts)
3146{
3147 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
3148 struct ring_buffer_event *event;
3149 unsigned long flags;
3150
Tom Zanussi2d622712009-03-22 03:30:49 -05003151 again:
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003152 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
3153 event = rb_iter_peek(iter, ts);
3154 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
3155
Lai Jiangshan334d4162009-04-24 11:27:05 +08003156 if (event && event->type_len == RINGBUF_TYPE_PADDING) {
Tom Zanussi2d622712009-03-22 03:30:49 -05003157 cpu_relax();
3158 goto again;
3159 }
3160
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003161 return event;
3162}
3163
3164/**
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003165 * ring_buffer_consume - return an event and consume it
3166 * @buffer: The ring buffer to get the next event from
3167 *
3168 * Returns the next event in the ring buffer, and that event is consumed.
3169 * Meaning, that sequential reads will keep returning a different event,
3170 * and eventually empty the ring buffer if the producer is slower.
3171 */
3172struct ring_buffer_event *
3173ring_buffer_consume(struct ring_buffer *buffer, int cpu, u64 *ts)
3174{
Steven Rostedt554f7862009-03-11 22:00:13 -04003175 struct ring_buffer_per_cpu *cpu_buffer;
3176 struct ring_buffer_event *event = NULL;
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003177 unsigned long flags;
Steven Rostedt8d707e82009-06-16 21:22:48 -04003178 int dolock;
3179
3180 dolock = rb_ok_to_lock();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003181
Tom Zanussi2d622712009-03-22 03:30:49 -05003182 again:
Steven Rostedt554f7862009-03-11 22:00:13 -04003183 /* might be called in atomic */
3184 preempt_disable();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003185
Steven Rostedt554f7862009-03-11 22:00:13 -04003186 if (!cpumask_test_cpu(cpu, buffer->cpumask))
3187 goto out;
3188
3189 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt8d707e82009-06-16 21:22:48 -04003190 local_irq_save(flags);
3191 if (dolock)
3192 spin_lock(&cpu_buffer->reader_lock);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003193
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003194 event = rb_buffer_peek(buffer, cpu, ts);
3195 if (!event)
Steven Rostedt554f7862009-03-11 22:00:13 -04003196 goto out_unlock;
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003197
Steven Rostedtd7690412008-10-01 00:29:53 -04003198 rb_advance_reader(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003199
Steven Rostedt554f7862009-03-11 22:00:13 -04003200 out_unlock:
Steven Rostedt8d707e82009-06-16 21:22:48 -04003201 if (dolock)
3202 spin_unlock(&cpu_buffer->reader_lock);
3203 local_irq_restore(flags);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003204
Steven Rostedt554f7862009-03-11 22:00:13 -04003205 out:
3206 preempt_enable();
3207
Lai Jiangshan334d4162009-04-24 11:27:05 +08003208 if (event && event->type_len == RINGBUF_TYPE_PADDING) {
Tom Zanussi2d622712009-03-22 03:30:49 -05003209 cpu_relax();
3210 goto again;
3211 }
3212
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003213 return event;
3214}
Robert Richterc4f50182008-12-11 16:49:22 +01003215EXPORT_SYMBOL_GPL(ring_buffer_consume);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003216
3217/**
3218 * ring_buffer_read_start - start a non consuming read of the buffer
3219 * @buffer: The ring buffer to read from
3220 * @cpu: The cpu buffer to iterate over
3221 *
3222 * This starts up an iteration through the buffer. It also disables
3223 * the recording to the buffer until the reading is finished.
3224 * This prevents the reading from being corrupted. This is not
3225 * a consuming read, so a producer is not expected.
3226 *
3227 * Must be paired with ring_buffer_finish.
3228 */
3229struct ring_buffer_iter *
3230ring_buffer_read_start(struct ring_buffer *buffer, int cpu)
3231{
3232 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedt8aabee52009-03-12 13:13:49 -04003233 struct ring_buffer_iter *iter;
Steven Rostedtd7690412008-10-01 00:29:53 -04003234 unsigned long flags;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003235
Rusty Russell9e01c1b2009-01-01 10:12:22 +10303236 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04003237 return NULL;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003238
3239 iter = kmalloc(sizeof(*iter), GFP_KERNEL);
3240 if (!iter)
Steven Rostedt8aabee52009-03-12 13:13:49 -04003241 return NULL;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003242
3243 cpu_buffer = buffer->buffers[cpu];
3244
3245 iter->cpu_buffer = cpu_buffer;
3246
3247 atomic_inc(&cpu_buffer->record_disabled);
3248 synchronize_sched();
3249
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003250 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
Steven Rostedt3e03fb72008-11-06 00:09:43 -05003251 __raw_spin_lock(&cpu_buffer->lock);
Steven Rostedt642edba2008-11-12 00:01:26 -05003252 rb_iter_reset(iter);
Steven Rostedt3e03fb72008-11-06 00:09:43 -05003253 __raw_spin_unlock(&cpu_buffer->lock);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003254 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003255
3256 return iter;
3257}
Robert Richterc4f50182008-12-11 16:49:22 +01003258EXPORT_SYMBOL_GPL(ring_buffer_read_start);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003259
3260/**
3261 * ring_buffer_finish - finish reading the iterator of the buffer
3262 * @iter: The iterator retrieved by ring_buffer_start
3263 *
3264 * This re-enables the recording to the buffer, and frees the
3265 * iterator.
3266 */
3267void
3268ring_buffer_read_finish(struct ring_buffer_iter *iter)
3269{
3270 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
3271
3272 atomic_dec(&cpu_buffer->record_disabled);
3273 kfree(iter);
3274}
Robert Richterc4f50182008-12-11 16:49:22 +01003275EXPORT_SYMBOL_GPL(ring_buffer_read_finish);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003276
3277/**
3278 * ring_buffer_read - read the next item in the ring buffer by the iterator
3279 * @iter: The ring buffer iterator
3280 * @ts: The time stamp of the event read.
3281 *
3282 * This reads the next event in the ring buffer and increments the iterator.
3283 */
3284struct ring_buffer_event *
3285ring_buffer_read(struct ring_buffer_iter *iter, u64 *ts)
3286{
3287 struct ring_buffer_event *event;
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003288 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
3289 unsigned long flags;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003290
Tom Zanussi2d622712009-03-22 03:30:49 -05003291 again:
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003292 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
3293 event = rb_iter_peek(iter, ts);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003294 if (!event)
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003295 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003296
3297 rb_advance_iter(iter);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003298 out:
3299 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003300
Lai Jiangshan334d4162009-04-24 11:27:05 +08003301 if (event && event->type_len == RINGBUF_TYPE_PADDING) {
Tom Zanussi2d622712009-03-22 03:30:49 -05003302 cpu_relax();
3303 goto again;
3304 }
3305
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003306 return event;
3307}
Robert Richterc4f50182008-12-11 16:49:22 +01003308EXPORT_SYMBOL_GPL(ring_buffer_read);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003309
3310/**
3311 * ring_buffer_size - return the size of the ring buffer (in bytes)
3312 * @buffer: The ring buffer.
3313 */
3314unsigned long ring_buffer_size(struct ring_buffer *buffer)
3315{
3316 return BUF_PAGE_SIZE * buffer->pages;
3317}
Robert Richterc4f50182008-12-11 16:49:22 +01003318EXPORT_SYMBOL_GPL(ring_buffer_size);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003319
3320static void
3321rb_reset_cpu(struct ring_buffer_per_cpu *cpu_buffer)
3322{
Steven Rostedt77ae3652009-03-27 11:00:29 -04003323 rb_head_page_deactivate(cpu_buffer);
3324
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003325 cpu_buffer->head_page
Steven Rostedt3adc54f2009-03-30 15:32:01 -04003326 = list_entry(cpu_buffer->pages, struct buffer_page, list);
Steven Rostedtbf41a152008-10-04 02:00:59 -04003327 local_set(&cpu_buffer->head_page->write, 0);
Steven Rostedt778c55d2009-05-01 18:44:45 -04003328 local_set(&cpu_buffer->head_page->entries, 0);
Steven Rostedtabc9b562008-12-02 15:34:06 -05003329 local_set(&cpu_buffer->head_page->page->commit, 0);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003330
Steven Rostedt6f807ac2008-10-04 02:00:58 -04003331 cpu_buffer->head_page->read = 0;
Steven Rostedtbf41a152008-10-04 02:00:59 -04003332
3333 cpu_buffer->tail_page = cpu_buffer->head_page;
3334 cpu_buffer->commit_page = cpu_buffer->head_page;
3335
3336 INIT_LIST_HEAD(&cpu_buffer->reader_page->list);
3337 local_set(&cpu_buffer->reader_page->write, 0);
Steven Rostedt778c55d2009-05-01 18:44:45 -04003338 local_set(&cpu_buffer->reader_page->entries, 0);
Steven Rostedtabc9b562008-12-02 15:34:06 -05003339 local_set(&cpu_buffer->reader_page->page->commit, 0);
Steven Rostedt6f807ac2008-10-04 02:00:58 -04003340 cpu_buffer->reader_page->read = 0;
Steven Rostedtd7690412008-10-01 00:29:53 -04003341
Steven Rostedt77ae3652009-03-27 11:00:29 -04003342 local_set(&cpu_buffer->commit_overrun, 0);
3343 local_set(&cpu_buffer->overrun, 0);
Steven Rostedte4906ef2009-04-30 20:49:44 -04003344 local_set(&cpu_buffer->entries, 0);
Steven Rostedtfa743952009-06-16 12:37:57 -04003345 local_set(&cpu_buffer->committing, 0);
3346 local_set(&cpu_buffer->commits, 0);
Steven Rostedt77ae3652009-03-27 11:00:29 -04003347 cpu_buffer->read = 0;
Steven Rostedt69507c02009-01-21 18:45:57 -05003348
3349 cpu_buffer->write_stamp = 0;
3350 cpu_buffer->read_stamp = 0;
Steven Rostedt77ae3652009-03-27 11:00:29 -04003351
3352 rb_head_page_activate(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003353}
3354
3355/**
3356 * ring_buffer_reset_cpu - reset a ring buffer per CPU buffer
3357 * @buffer: The ring buffer to reset a per cpu buffer of
3358 * @cpu: The CPU buffer to be reset
3359 */
3360void ring_buffer_reset_cpu(struct ring_buffer *buffer, int cpu)
3361{
3362 struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
3363 unsigned long flags;
3364
Rusty Russell9e01c1b2009-01-01 10:12:22 +10303365 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04003366 return;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003367
Steven Rostedt41ede232009-05-01 20:26:54 -04003368 atomic_inc(&cpu_buffer->record_disabled);
3369
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003370 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
3371
Steven Rostedt3e03fb72008-11-06 00:09:43 -05003372 __raw_spin_lock(&cpu_buffer->lock);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003373
3374 rb_reset_cpu(cpu_buffer);
3375
Steven Rostedt3e03fb72008-11-06 00:09:43 -05003376 __raw_spin_unlock(&cpu_buffer->lock);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003377
3378 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
Steven Rostedt41ede232009-05-01 20:26:54 -04003379
3380 atomic_dec(&cpu_buffer->record_disabled);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003381}
Robert Richterc4f50182008-12-11 16:49:22 +01003382EXPORT_SYMBOL_GPL(ring_buffer_reset_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003383
3384/**
3385 * ring_buffer_reset - reset a ring buffer
3386 * @buffer: The ring buffer to reset all cpu buffers
3387 */
3388void ring_buffer_reset(struct ring_buffer *buffer)
3389{
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003390 int cpu;
3391
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003392 for_each_buffer_cpu(buffer, cpu)
Steven Rostedtd7690412008-10-01 00:29:53 -04003393 ring_buffer_reset_cpu(buffer, cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003394}
Robert Richterc4f50182008-12-11 16:49:22 +01003395EXPORT_SYMBOL_GPL(ring_buffer_reset);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003396
3397/**
3398 * rind_buffer_empty - is the ring buffer empty?
3399 * @buffer: The ring buffer to test
3400 */
3401int ring_buffer_empty(struct ring_buffer *buffer)
3402{
3403 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedtd4788202009-06-17 00:39:43 -04003404 unsigned long flags;
Steven Rostedt8d707e82009-06-16 21:22:48 -04003405 int dolock;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003406 int cpu;
Steven Rostedtd4788202009-06-17 00:39:43 -04003407 int ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003408
Steven Rostedt8d707e82009-06-16 21:22:48 -04003409 dolock = rb_ok_to_lock();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003410
3411 /* yes this is racy, but if you don't like the race, lock the buffer */
3412 for_each_buffer_cpu(buffer, cpu) {
3413 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt8d707e82009-06-16 21:22:48 -04003414 local_irq_save(flags);
3415 if (dolock)
3416 spin_lock(&cpu_buffer->reader_lock);
Steven Rostedtd4788202009-06-17 00:39:43 -04003417 ret = rb_per_cpu_empty(cpu_buffer);
Steven Rostedt8d707e82009-06-16 21:22:48 -04003418 if (dolock)
3419 spin_unlock(&cpu_buffer->reader_lock);
3420 local_irq_restore(flags);
3421
Steven Rostedtd4788202009-06-17 00:39:43 -04003422 if (!ret)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003423 return 0;
3424 }
Steven Rostedt554f7862009-03-11 22:00:13 -04003425
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003426 return 1;
3427}
Robert Richterc4f50182008-12-11 16:49:22 +01003428EXPORT_SYMBOL_GPL(ring_buffer_empty);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003429
3430/**
3431 * ring_buffer_empty_cpu - is a cpu buffer of a ring buffer empty?
3432 * @buffer: The ring buffer
3433 * @cpu: The CPU buffer to test
3434 */
3435int ring_buffer_empty_cpu(struct ring_buffer *buffer, int cpu)
3436{
3437 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedtd4788202009-06-17 00:39:43 -04003438 unsigned long flags;
Steven Rostedt8d707e82009-06-16 21:22:48 -04003439 int dolock;
Steven Rostedt8aabee52009-03-12 13:13:49 -04003440 int ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003441
Rusty Russell9e01c1b2009-01-01 10:12:22 +10303442 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04003443 return 1;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003444
Steven Rostedt8d707e82009-06-16 21:22:48 -04003445 dolock = rb_ok_to_lock();
Steven Rostedt554f7862009-03-11 22:00:13 -04003446
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003447 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt8d707e82009-06-16 21:22:48 -04003448 local_irq_save(flags);
3449 if (dolock)
3450 spin_lock(&cpu_buffer->reader_lock);
Steven Rostedt554f7862009-03-11 22:00:13 -04003451 ret = rb_per_cpu_empty(cpu_buffer);
Steven Rostedt8d707e82009-06-16 21:22:48 -04003452 if (dolock)
3453 spin_unlock(&cpu_buffer->reader_lock);
3454 local_irq_restore(flags);
Steven Rostedt554f7862009-03-11 22:00:13 -04003455
3456 return ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003457}
Robert Richterc4f50182008-12-11 16:49:22 +01003458EXPORT_SYMBOL_GPL(ring_buffer_empty_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003459
3460/**
3461 * ring_buffer_swap_cpu - swap a CPU buffer between two ring buffers
3462 * @buffer_a: One buffer to swap with
3463 * @buffer_b: The other buffer to swap with
3464 *
3465 * This function is useful for tracers that want to take a "snapshot"
3466 * of a CPU buffer and has another back up buffer lying around.
3467 * it is expected that the tracer handles the cpu buffer not being
3468 * used at the moment.
3469 */
3470int ring_buffer_swap_cpu(struct ring_buffer *buffer_a,
3471 struct ring_buffer *buffer_b, int cpu)
3472{
3473 struct ring_buffer_per_cpu *cpu_buffer_a;
3474 struct ring_buffer_per_cpu *cpu_buffer_b;
Steven Rostedt554f7862009-03-11 22:00:13 -04003475 int ret = -EINVAL;
3476
Rusty Russell9e01c1b2009-01-01 10:12:22 +10303477 if (!cpumask_test_cpu(cpu, buffer_a->cpumask) ||
3478 !cpumask_test_cpu(cpu, buffer_b->cpumask))
Steven Rostedt554f7862009-03-11 22:00:13 -04003479 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003480
3481 /* At least make sure the two buffers are somewhat the same */
Lai Jiangshan6d102bc2008-12-17 17:48:23 +08003482 if (buffer_a->pages != buffer_b->pages)
Steven Rostedt554f7862009-03-11 22:00:13 -04003483 goto out;
3484
3485 ret = -EAGAIN;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003486
Steven Rostedt97b17ef2009-01-21 15:24:56 -05003487 if (ring_buffer_flags != RB_BUFFERS_ON)
Steven Rostedt554f7862009-03-11 22:00:13 -04003488 goto out;
Steven Rostedt97b17ef2009-01-21 15:24:56 -05003489
3490 if (atomic_read(&buffer_a->record_disabled))
Steven Rostedt554f7862009-03-11 22:00:13 -04003491 goto out;
Steven Rostedt97b17ef2009-01-21 15:24:56 -05003492
3493 if (atomic_read(&buffer_b->record_disabled))
Steven Rostedt554f7862009-03-11 22:00:13 -04003494 goto out;
Steven Rostedt97b17ef2009-01-21 15:24:56 -05003495
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003496 cpu_buffer_a = buffer_a->buffers[cpu];
3497 cpu_buffer_b = buffer_b->buffers[cpu];
3498
Steven Rostedt97b17ef2009-01-21 15:24:56 -05003499 if (atomic_read(&cpu_buffer_a->record_disabled))
Steven Rostedt554f7862009-03-11 22:00:13 -04003500 goto out;
Steven Rostedt97b17ef2009-01-21 15:24:56 -05003501
3502 if (atomic_read(&cpu_buffer_b->record_disabled))
Steven Rostedt554f7862009-03-11 22:00:13 -04003503 goto out;
Steven Rostedt97b17ef2009-01-21 15:24:56 -05003504
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003505 /*
3506 * We can't do a synchronize_sched here because this
3507 * function can be called in atomic context.
3508 * Normally this will be called from the same CPU as cpu.
3509 * If not it's up to the caller to protect this.
3510 */
3511 atomic_inc(&cpu_buffer_a->record_disabled);
3512 atomic_inc(&cpu_buffer_b->record_disabled);
3513
3514 buffer_a->buffers[cpu] = cpu_buffer_b;
3515 buffer_b->buffers[cpu] = cpu_buffer_a;
3516
3517 cpu_buffer_b->buffer = buffer_a;
3518 cpu_buffer_a->buffer = buffer_b;
3519
3520 atomic_dec(&cpu_buffer_a->record_disabled);
3521 atomic_dec(&cpu_buffer_b->record_disabled);
3522
Steven Rostedt554f7862009-03-11 22:00:13 -04003523 ret = 0;
3524out:
Steven Rostedt554f7862009-03-11 22:00:13 -04003525 return ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003526}
Robert Richterc4f50182008-12-11 16:49:22 +01003527EXPORT_SYMBOL_GPL(ring_buffer_swap_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003528
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003529/**
3530 * ring_buffer_alloc_read_page - allocate a page to read from buffer
3531 * @buffer: the buffer to allocate for.
3532 *
3533 * This function is used in conjunction with ring_buffer_read_page.
3534 * When reading a full page from the ring buffer, these functions
3535 * can be used to speed up the process. The calling function should
3536 * allocate a few pages first with this function. Then when it
3537 * needs to get pages from the ring buffer, it passes the result
3538 * of this function into ring_buffer_read_page, which will swap
3539 * the page that was allocated, with the read page of the buffer.
3540 *
3541 * Returns:
3542 * The page allocated, or NULL on error.
3543 */
3544void *ring_buffer_alloc_read_page(struct ring_buffer *buffer)
3545{
Steven Rostedt044fa782008-12-02 23:50:03 -05003546 struct buffer_data_page *bpage;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003547 unsigned long addr;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003548
3549 addr = __get_free_page(GFP_KERNEL);
3550 if (!addr)
3551 return NULL;
3552
Steven Rostedt044fa782008-12-02 23:50:03 -05003553 bpage = (void *)addr;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003554
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003555 rb_init_page(bpage);
3556
Steven Rostedt044fa782008-12-02 23:50:03 -05003557 return bpage;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003558}
Steven Rostedtd6ce96d2009-05-05 01:15:24 -04003559EXPORT_SYMBOL_GPL(ring_buffer_alloc_read_page);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003560
3561/**
3562 * ring_buffer_free_read_page - free an allocated read page
3563 * @buffer: the buffer the page was allocate for
3564 * @data: the page to free
3565 *
3566 * Free a page allocated from ring_buffer_alloc_read_page.
3567 */
3568void ring_buffer_free_read_page(struct ring_buffer *buffer, void *data)
3569{
3570 free_page((unsigned long)data);
3571}
Steven Rostedtd6ce96d2009-05-05 01:15:24 -04003572EXPORT_SYMBOL_GPL(ring_buffer_free_read_page);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003573
3574/**
3575 * ring_buffer_read_page - extract a page from the ring buffer
3576 * @buffer: buffer to extract from
3577 * @data_page: the page to use allocated from ring_buffer_alloc_read_page
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003578 * @len: amount to extract
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003579 * @cpu: the cpu of the buffer to extract
3580 * @full: should the extraction only happen when the page is full.
3581 *
3582 * This function will pull out a page from the ring buffer and consume it.
3583 * @data_page must be the address of the variable that was returned
3584 * from ring_buffer_alloc_read_page. This is because the page might be used
3585 * to swap with a page in the ring buffer.
3586 *
3587 * for example:
Lai Jiangshanb85fa012009-02-09 14:21:14 +08003588 * rpage = ring_buffer_alloc_read_page(buffer);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003589 * if (!rpage)
3590 * return error;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003591 * ret = ring_buffer_read_page(buffer, &rpage, len, cpu, 0);
Lai Jiangshan667d2412009-02-09 14:21:17 +08003592 * if (ret >= 0)
3593 * process_page(rpage, ret);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003594 *
3595 * When @full is set, the function will not return true unless
3596 * the writer is off the reader page.
3597 *
3598 * Note: it is up to the calling functions to handle sleeps and wakeups.
3599 * The ring buffer can be used anywhere in the kernel and can not
3600 * blindly call wake_up. The layer that uses the ring buffer must be
3601 * responsible for that.
3602 *
3603 * Returns:
Lai Jiangshan667d2412009-02-09 14:21:17 +08003604 * >=0 if data has been transferred, returns the offset of consumed data.
3605 * <0 if no data has been transferred.
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003606 */
3607int ring_buffer_read_page(struct ring_buffer *buffer,
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003608 void **data_page, size_t len, int cpu, int full)
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003609{
3610 struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
3611 struct ring_buffer_event *event;
Steven Rostedt044fa782008-12-02 23:50:03 -05003612 struct buffer_data_page *bpage;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003613 struct buffer_page *reader;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003614 unsigned long flags;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003615 unsigned int commit;
Lai Jiangshan667d2412009-02-09 14:21:17 +08003616 unsigned int read;
Steven Rostedt4f3640f2009-03-03 23:52:42 -05003617 u64 save_timestamp;
Lai Jiangshan667d2412009-02-09 14:21:17 +08003618 int ret = -1;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003619
Steven Rostedt554f7862009-03-11 22:00:13 -04003620 if (!cpumask_test_cpu(cpu, buffer->cpumask))
3621 goto out;
3622
Steven Rostedt474d32b2009-03-03 19:51:40 -05003623 /*
3624 * If len is not big enough to hold the page header, then
3625 * we can not copy anything.
3626 */
3627 if (len <= BUF_PAGE_HDR_SIZE)
Steven Rostedt554f7862009-03-11 22:00:13 -04003628 goto out;
Steven Rostedt474d32b2009-03-03 19:51:40 -05003629
3630 len -= BUF_PAGE_HDR_SIZE;
3631
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003632 if (!data_page)
Steven Rostedt554f7862009-03-11 22:00:13 -04003633 goto out;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003634
Steven Rostedt044fa782008-12-02 23:50:03 -05003635 bpage = *data_page;
3636 if (!bpage)
Steven Rostedt554f7862009-03-11 22:00:13 -04003637 goto out;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003638
3639 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
3640
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003641 reader = rb_get_reader_page(cpu_buffer);
3642 if (!reader)
Steven Rostedt554f7862009-03-11 22:00:13 -04003643 goto out_unlock;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003644
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003645 event = rb_reader_event(cpu_buffer);
Lai Jiangshan667d2412009-02-09 14:21:17 +08003646
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003647 read = reader->read;
3648 commit = rb_page_commit(reader);
3649
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003650 /*
Steven Rostedt474d32b2009-03-03 19:51:40 -05003651 * If this page has been partially read or
3652 * if len is not big enough to read the rest of the page or
3653 * a writer is still on the page, then
3654 * we must copy the data from the page to the buffer.
3655 * Otherwise, we can simply swap the page with the one passed in.
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003656 */
Steven Rostedt474d32b2009-03-03 19:51:40 -05003657 if (read || (len < (commit - read)) ||
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003658 cpu_buffer->reader_page == cpu_buffer->commit_page) {
Lai Jiangshan667d2412009-02-09 14:21:17 +08003659 struct buffer_data_page *rpage = cpu_buffer->reader_page->page;
Steven Rostedt474d32b2009-03-03 19:51:40 -05003660 unsigned int rpos = read;
3661 unsigned int pos = 0;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003662 unsigned int size;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003663
3664 if (full)
Steven Rostedt554f7862009-03-11 22:00:13 -04003665 goto out_unlock;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003666
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003667 if (len > (commit - read))
3668 len = (commit - read);
3669
3670 size = rb_event_length(event);
3671
3672 if (len < size)
Steven Rostedt554f7862009-03-11 22:00:13 -04003673 goto out_unlock;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003674
Steven Rostedt4f3640f2009-03-03 23:52:42 -05003675 /* save the current timestamp, since the user will need it */
3676 save_timestamp = cpu_buffer->read_stamp;
3677
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003678 /* Need to copy one event at a time */
3679 do {
Steven Rostedt474d32b2009-03-03 19:51:40 -05003680 memcpy(bpage->data + pos, rpage->data + rpos, size);
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003681
3682 len -= size;
3683
3684 rb_advance_reader(cpu_buffer);
Steven Rostedt474d32b2009-03-03 19:51:40 -05003685 rpos = reader->read;
3686 pos += size;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003687
3688 event = rb_reader_event(cpu_buffer);
3689 size = rb_event_length(event);
3690 } while (len > size);
Lai Jiangshan667d2412009-02-09 14:21:17 +08003691
3692 /* update bpage */
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003693 local_set(&bpage->commit, pos);
Steven Rostedt4f3640f2009-03-03 23:52:42 -05003694 bpage->time_stamp = save_timestamp;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003695
Steven Rostedt474d32b2009-03-03 19:51:40 -05003696 /* we copied everything to the beginning */
3697 read = 0;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003698 } else {
Steven Rostedtafbab762009-05-01 19:40:05 -04003699 /* update the entry counter */
Steven Rostedt77ae3652009-03-27 11:00:29 -04003700 cpu_buffer->read += rb_page_entries(reader);
Steven Rostedtafbab762009-05-01 19:40:05 -04003701
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003702 /* swap the pages */
Steven Rostedt044fa782008-12-02 23:50:03 -05003703 rb_init_page(bpage);
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003704 bpage = reader->page;
3705 reader->page = *data_page;
3706 local_set(&reader->write, 0);
Steven Rostedt778c55d2009-05-01 18:44:45 -04003707 local_set(&reader->entries, 0);
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003708 reader->read = 0;
Steven Rostedt044fa782008-12-02 23:50:03 -05003709 *data_page = bpage;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003710 }
Lai Jiangshan667d2412009-02-09 14:21:17 +08003711 ret = read;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003712
Steven Rostedt554f7862009-03-11 22:00:13 -04003713 out_unlock:
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003714 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
3715
Steven Rostedt554f7862009-03-11 22:00:13 -04003716 out:
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003717 return ret;
3718}
Steven Rostedtd6ce96d2009-05-05 01:15:24 -04003719EXPORT_SYMBOL_GPL(ring_buffer_read_page);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003720
Paul Mundt1155de42009-06-25 14:30:12 +09003721#ifdef CONFIG_TRACING
Steven Rostedta3583242008-11-11 15:01:42 -05003722static ssize_t
3723rb_simple_read(struct file *filp, char __user *ubuf,
3724 size_t cnt, loff_t *ppos)
3725{
Hannes Eder5e398412009-02-10 19:44:34 +01003726 unsigned long *p = filp->private_data;
Steven Rostedta3583242008-11-11 15:01:42 -05003727 char buf[64];
3728 int r;
3729
Steven Rostedt033601a2008-11-21 12:41:55 -05003730 if (test_bit(RB_BUFFERS_DISABLED_BIT, p))
3731 r = sprintf(buf, "permanently disabled\n");
3732 else
3733 r = sprintf(buf, "%d\n", test_bit(RB_BUFFERS_ON_BIT, p));
Steven Rostedta3583242008-11-11 15:01:42 -05003734
3735 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
3736}
3737
3738static ssize_t
3739rb_simple_write(struct file *filp, const char __user *ubuf,
3740 size_t cnt, loff_t *ppos)
3741{
Hannes Eder5e398412009-02-10 19:44:34 +01003742 unsigned long *p = filp->private_data;
Steven Rostedta3583242008-11-11 15:01:42 -05003743 char buf[64];
Hannes Eder5e398412009-02-10 19:44:34 +01003744 unsigned long val;
Steven Rostedta3583242008-11-11 15:01:42 -05003745 int ret;
3746
3747 if (cnt >= sizeof(buf))
3748 return -EINVAL;
3749
3750 if (copy_from_user(&buf, ubuf, cnt))
3751 return -EFAULT;
3752
3753 buf[cnt] = 0;
3754
3755 ret = strict_strtoul(buf, 10, &val);
3756 if (ret < 0)
3757 return ret;
3758
Steven Rostedt033601a2008-11-21 12:41:55 -05003759 if (val)
3760 set_bit(RB_BUFFERS_ON_BIT, p);
3761 else
3762 clear_bit(RB_BUFFERS_ON_BIT, p);
Steven Rostedta3583242008-11-11 15:01:42 -05003763
3764 (*ppos)++;
3765
3766 return cnt;
3767}
3768
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003769static const struct file_operations rb_simple_fops = {
Steven Rostedta3583242008-11-11 15:01:42 -05003770 .open = tracing_open_generic,
3771 .read = rb_simple_read,
3772 .write = rb_simple_write,
3773};
3774
3775
3776static __init int rb_init_debugfs(void)
3777{
3778 struct dentry *d_tracer;
Steven Rostedta3583242008-11-11 15:01:42 -05003779
3780 d_tracer = tracing_init_dentry();
3781
Frederic Weisbecker5452af62009-03-27 00:25:38 +01003782 trace_create_file("tracing_on", 0644, d_tracer,
3783 &ring_buffer_flags, &rb_simple_fops);
Steven Rostedta3583242008-11-11 15:01:42 -05003784
3785 return 0;
3786}
3787
3788fs_initcall(rb_init_debugfs);
Paul Mundt1155de42009-06-25 14:30:12 +09003789#endif
Steven Rostedt554f7862009-03-11 22:00:13 -04003790
Steven Rostedt59222ef2009-03-12 11:46:03 -04003791#ifdef CONFIG_HOTPLUG_CPU
Frederic Weisbecker09c9e842009-03-21 04:33:36 +01003792static int rb_cpu_notify(struct notifier_block *self,
3793 unsigned long action, void *hcpu)
Steven Rostedt554f7862009-03-11 22:00:13 -04003794{
3795 struct ring_buffer *buffer =
3796 container_of(self, struct ring_buffer, cpu_notify);
3797 long cpu = (long)hcpu;
3798
3799 switch (action) {
3800 case CPU_UP_PREPARE:
3801 case CPU_UP_PREPARE_FROZEN:
Rusty Russell3f237a72009-06-12 21:15:30 +09303802 if (cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt554f7862009-03-11 22:00:13 -04003803 return NOTIFY_OK;
3804
3805 buffer->buffers[cpu] =
3806 rb_allocate_cpu_buffer(buffer, cpu);
3807 if (!buffer->buffers[cpu]) {
3808 WARN(1, "failed to allocate ring buffer on CPU %ld\n",
3809 cpu);
3810 return NOTIFY_OK;
3811 }
3812 smp_wmb();
Rusty Russell3f237a72009-06-12 21:15:30 +09303813 cpumask_set_cpu(cpu, buffer->cpumask);
Steven Rostedt554f7862009-03-11 22:00:13 -04003814 break;
3815 case CPU_DOWN_PREPARE:
3816 case CPU_DOWN_PREPARE_FROZEN:
3817 /*
3818 * Do nothing.
3819 * If we were to free the buffer, then the user would
3820 * lose any trace that was in the buffer.
3821 */
3822 break;
3823 default:
3824 break;
3825 }
3826 return NOTIFY_OK;
3827}
3828#endif