blob: 03ed52b67db308c7f05c7e8354551f63a97c8765 [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>
Steven Rostedt7a8e76a2008-09-29 23:02:38 -040013#include <linux/module.h>
14#include <linux/percpu.h>
15#include <linux/mutex.h>
Steven Rostedt7a8e76a2008-09-29 23:02:38 -040016#include <linux/init.h>
17#include <linux/hash.h>
18#include <linux/list.h>
Steven Rostedt554f7862009-03-11 22:00:13 -040019#include <linux/cpu.h>
Steven Rostedt7a8e76a2008-09-29 23:02:38 -040020#include <linux/fs.h>
21
Steven Rostedt182e9f52008-11-03 23:15:56 -050022#include "trace.h"
23
Steven Rostedt033601a2008-11-21 12:41:55 -050024/*
Steven Rostedtd1b182a2009-04-15 16:53:47 -040025 * The ring buffer header is special. We must manually up keep it.
26 */
27int ring_buffer_print_entry_header(struct trace_seq *s)
28{
29 int ret;
30
Lai Jiangshan334d4162009-04-24 11:27:05 +080031 ret = trace_seq_printf(s, "# compressed entry header\n");
32 ret = trace_seq_printf(s, "\ttype_len : 5 bits\n");
Steven Rostedtd1b182a2009-04-15 16:53:47 -040033 ret = trace_seq_printf(s, "\ttime_delta : 27 bits\n");
34 ret = trace_seq_printf(s, "\tarray : 32 bits\n");
35 ret = trace_seq_printf(s, "\n");
36 ret = trace_seq_printf(s, "\tpadding : type == %d\n",
37 RINGBUF_TYPE_PADDING);
38 ret = trace_seq_printf(s, "\ttime_extend : type == %d\n",
39 RINGBUF_TYPE_TIME_EXTEND);
Lai Jiangshan334d4162009-04-24 11:27:05 +080040 ret = trace_seq_printf(s, "\tdata max type_len == %d\n",
41 RINGBUF_TYPE_DATA_TYPE_LEN_MAX);
Steven Rostedtd1b182a2009-04-15 16:53:47 -040042
43 return ret;
44}
45
46/*
Steven Rostedt5cc98542009-03-12 22:24:17 -040047 * The ring buffer is made up of a list of pages. A separate list of pages is
48 * allocated for each CPU. A writer may only write to a buffer that is
49 * associated with the CPU it is currently executing on. A reader may read
50 * from any per cpu buffer.
51 *
52 * The reader is special. For each per cpu buffer, the reader has its own
53 * reader page. When a reader has read the entire reader page, this reader
54 * page is swapped with another page in the ring buffer.
55 *
56 * Now, as long as the writer is off the reader page, the reader can do what
57 * ever it wants with that page. The writer will never write to that page
58 * again (as long as it is out of the ring buffer).
59 *
60 * Here's some silly ASCII art.
61 *
62 * +------+
63 * |reader| RING BUFFER
64 * |page |
65 * +------+ +---+ +---+ +---+
66 * | |-->| |-->| |
67 * +---+ +---+ +---+
68 * ^ |
69 * | |
70 * +---------------+
71 *
72 *
73 * +------+
74 * |reader| RING BUFFER
75 * |page |------------------v
76 * +------+ +---+ +---+ +---+
77 * | |-->| |-->| |
78 * +---+ +---+ +---+
79 * ^ |
80 * | |
81 * +---------------+
82 *
83 *
84 * +------+
85 * |reader| RING BUFFER
86 * |page |------------------v
87 * +------+ +---+ +---+ +---+
88 * ^ | |-->| |-->| |
89 * | +---+ +---+ +---+
90 * | |
91 * | |
92 * +------------------------------+
93 *
94 *
95 * +------+
96 * |buffer| RING BUFFER
97 * |page |------------------v
98 * +------+ +---+ +---+ +---+
99 * ^ | | | |-->| |
100 * | New +---+ +---+ +---+
101 * | Reader------^ |
102 * | page |
103 * +------------------------------+
104 *
105 *
106 * After we make this swap, the reader can hand this page off to the splice
107 * code and be done with it. It can even allocate a new page if it needs to
108 * and swap that into the ring buffer.
109 *
110 * We will be using cmpxchg soon to make all this lockless.
111 *
112 */
113
114/*
Steven Rostedt033601a2008-11-21 12:41:55 -0500115 * A fast way to enable or disable all ring buffers is to
116 * call tracing_on or tracing_off. Turning off the ring buffers
117 * prevents all ring buffers from being recorded to.
118 * Turning this switch on, makes it OK to write to the
119 * ring buffer, if the ring buffer is enabled itself.
120 *
121 * There's three layers that must be on in order to write
122 * to the ring buffer.
123 *
124 * 1) This global flag must be set.
125 * 2) The ring buffer must be enabled for recording.
126 * 3) The per cpu buffer must be enabled for recording.
127 *
128 * In case of an anomaly, this global flag has a bit set that
129 * will permantly disable all ring buffers.
130 */
131
132/*
133 * Global flag to disable all recording to ring buffers
134 * This has two bits: ON, DISABLED
135 *
136 * ON DISABLED
137 * ---- ----------
138 * 0 0 : ring buffers are off
139 * 1 0 : ring buffers are on
140 * X 1 : ring buffers are permanently disabled
141 */
142
143enum {
144 RB_BUFFERS_ON_BIT = 0,
145 RB_BUFFERS_DISABLED_BIT = 1,
146};
147
148enum {
149 RB_BUFFERS_ON = 1 << RB_BUFFERS_ON_BIT,
150 RB_BUFFERS_DISABLED = 1 << RB_BUFFERS_DISABLED_BIT,
151};
152
Hannes Eder5e398412009-02-10 19:44:34 +0100153static unsigned long ring_buffer_flags __read_mostly = RB_BUFFERS_ON;
Steven Rostedta3583242008-11-11 15:01:42 -0500154
Steven Rostedt474d32b2009-03-03 19:51:40 -0500155#define BUF_PAGE_HDR_SIZE offsetof(struct buffer_data_page, data)
156
Steven Rostedta3583242008-11-11 15:01:42 -0500157/**
158 * tracing_on - enable all tracing buffers
159 *
160 * This function enables all tracing buffers that may have been
161 * disabled with tracing_off.
162 */
163void tracing_on(void)
164{
Steven Rostedt033601a2008-11-21 12:41:55 -0500165 set_bit(RB_BUFFERS_ON_BIT, &ring_buffer_flags);
Steven Rostedta3583242008-11-11 15:01:42 -0500166}
Robert Richterc4f50182008-12-11 16:49:22 +0100167EXPORT_SYMBOL_GPL(tracing_on);
Steven Rostedta3583242008-11-11 15:01:42 -0500168
169/**
170 * tracing_off - turn off all tracing buffers
171 *
172 * This function stops all tracing buffers from recording data.
173 * It does not disable any overhead the tracers themselves may
174 * be causing. This function simply causes all recording to
175 * the ring buffers to fail.
176 */
177void tracing_off(void)
178{
Steven Rostedt033601a2008-11-21 12:41:55 -0500179 clear_bit(RB_BUFFERS_ON_BIT, &ring_buffer_flags);
180}
Robert Richterc4f50182008-12-11 16:49:22 +0100181EXPORT_SYMBOL_GPL(tracing_off);
Steven Rostedt033601a2008-11-21 12:41:55 -0500182
183/**
184 * tracing_off_permanent - permanently disable ring buffers
185 *
186 * This function, once called, will disable all ring buffers
Wenji Huangc3706f02009-02-10 01:03:18 -0500187 * permanently.
Steven Rostedt033601a2008-11-21 12:41:55 -0500188 */
189void tracing_off_permanent(void)
190{
191 set_bit(RB_BUFFERS_DISABLED_BIT, &ring_buffer_flags);
Steven Rostedta3583242008-11-11 15:01:42 -0500192}
193
Steven Rostedt988ae9d2009-02-14 19:17:02 -0500194/**
195 * tracing_is_on - show state of ring buffers enabled
196 */
197int tracing_is_on(void)
198{
199 return ring_buffer_flags == RB_BUFFERS_ON;
200}
201EXPORT_SYMBOL_GPL(tracing_is_on);
202
Ingo Molnard06bbd62008-11-12 10:11:37 +0100203#include "trace.h"
204
Steven Rostedte3d6bf02009-03-03 13:53:07 -0500205#define RB_EVNT_HDR_SIZE (offsetof(struct ring_buffer_event, array))
Andrew Morton67d34722009-01-09 12:27:09 -0800206#define RB_ALIGNMENT 4U
Lai Jiangshan334d4162009-04-24 11:27:05 +0800207#define RB_MAX_SMALL_DATA (RB_ALIGNMENT * RINGBUF_TYPE_DATA_TYPE_LEN_MAX)
208
209/* define RINGBUF_TYPE_DATA for 'case RINGBUF_TYPE_DATA:' */
210#define RINGBUF_TYPE_DATA 0 ... RINGBUF_TYPE_DATA_TYPE_LEN_MAX
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400211
212enum {
213 RB_LEN_TIME_EXTEND = 8,
214 RB_LEN_TIME_STAMP = 16,
215};
216
Tom Zanussi2d622712009-03-22 03:30:49 -0500217static inline int rb_null_event(struct ring_buffer_event *event)
218{
Lai Jiangshan334d4162009-04-24 11:27:05 +0800219 return event->type_len == RINGBUF_TYPE_PADDING
220 && event->time_delta == 0;
Tom Zanussi2d622712009-03-22 03:30:49 -0500221}
222
223static inline int rb_discarded_event(struct ring_buffer_event *event)
224{
Lai Jiangshan334d4162009-04-24 11:27:05 +0800225 return event->type_len == RINGBUF_TYPE_PADDING && event->time_delta;
Tom Zanussi2d622712009-03-22 03:30:49 -0500226}
227
228static void rb_event_set_padding(struct ring_buffer_event *event)
229{
Lai Jiangshan334d4162009-04-24 11:27:05 +0800230 event->type_len = RINGBUF_TYPE_PADDING;
Tom Zanussi2d622712009-03-22 03:30:49 -0500231 event->time_delta = 0;
232}
233
Tom Zanussi2d622712009-03-22 03:30:49 -0500234static unsigned
235rb_event_data_length(struct ring_buffer_event *event)
236{
237 unsigned length;
238
Lai Jiangshan334d4162009-04-24 11:27:05 +0800239 if (event->type_len)
240 length = event->type_len * RB_ALIGNMENT;
Tom Zanussi2d622712009-03-22 03:30:49 -0500241 else
242 length = event->array[0];
243 return length + RB_EVNT_HDR_SIZE;
244}
245
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400246/* inline for ring buffer fast paths */
Andrew Morton34a148b2009-01-09 12:27:09 -0800247static unsigned
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400248rb_event_length(struct ring_buffer_event *event)
249{
Lai Jiangshan334d4162009-04-24 11:27:05 +0800250 switch (event->type_len) {
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400251 case RINGBUF_TYPE_PADDING:
Tom Zanussi2d622712009-03-22 03:30:49 -0500252 if (rb_null_event(event))
253 /* undefined */
254 return -1;
Lai Jiangshan334d4162009-04-24 11:27:05 +0800255 return event->array[0] + RB_EVNT_HDR_SIZE;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400256
257 case RINGBUF_TYPE_TIME_EXTEND:
258 return RB_LEN_TIME_EXTEND;
259
260 case RINGBUF_TYPE_TIME_STAMP:
261 return RB_LEN_TIME_STAMP;
262
263 case RINGBUF_TYPE_DATA:
Tom Zanussi2d622712009-03-22 03:30:49 -0500264 return rb_event_data_length(event);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400265 default:
266 BUG();
267 }
268 /* not hit */
269 return 0;
270}
271
272/**
273 * ring_buffer_event_length - return the length of the event
274 * @event: the event to get the length of
275 */
276unsigned ring_buffer_event_length(struct ring_buffer_event *event)
277{
Robert Richter465634a2009-01-07 15:32:11 +0100278 unsigned length = rb_event_length(event);
Lai Jiangshan334d4162009-04-24 11:27:05 +0800279 if (event->type_len > RINGBUF_TYPE_DATA_TYPE_LEN_MAX)
Robert Richter465634a2009-01-07 15:32:11 +0100280 return length;
281 length -= RB_EVNT_HDR_SIZE;
282 if (length > RB_MAX_SMALL_DATA + sizeof(event->array[0]))
283 length -= sizeof(event->array[0]);
284 return length;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400285}
Robert Richterc4f50182008-12-11 16:49:22 +0100286EXPORT_SYMBOL_GPL(ring_buffer_event_length);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400287
288/* inline for ring buffer fast paths */
Andrew Morton34a148b2009-01-09 12:27:09 -0800289static void *
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400290rb_event_data(struct ring_buffer_event *event)
291{
Lai Jiangshan334d4162009-04-24 11:27:05 +0800292 BUG_ON(event->type_len > RINGBUF_TYPE_DATA_TYPE_LEN_MAX);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400293 /* If length is in len field, then array[0] has the data */
Lai Jiangshan334d4162009-04-24 11:27:05 +0800294 if (event->type_len)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400295 return (void *)&event->array[0];
296 /* Otherwise length is in array[0] and array[1] has the data */
297 return (void *)&event->array[1];
298}
299
300/**
301 * ring_buffer_event_data - return the data of the event
302 * @event: the event to get the data from
303 */
304void *ring_buffer_event_data(struct ring_buffer_event *event)
305{
306 return rb_event_data(event);
307}
Robert Richterc4f50182008-12-11 16:49:22 +0100308EXPORT_SYMBOL_GPL(ring_buffer_event_data);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400309
310#define for_each_buffer_cpu(buffer, cpu) \
Rusty Russell9e01c1b2009-01-01 10:12:22 +1030311 for_each_cpu(cpu, buffer->cpumask)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400312
313#define TS_SHIFT 27
314#define TS_MASK ((1ULL << TS_SHIFT) - 1)
315#define TS_DELTA_TEST (~TS_MASK)
316
Steven Rostedtabc9b562008-12-02 15:34:06 -0500317struct buffer_data_page {
Steven Rostedte4c2ce82008-10-01 11:14:54 -0400318 u64 time_stamp; /* page time stamp */
Wenji Huangc3706f02009-02-10 01:03:18 -0500319 local_t commit; /* write committed index */
Steven Rostedtabc9b562008-12-02 15:34:06 -0500320 unsigned char data[]; /* data of buffer page */
321};
322
323struct buffer_page {
Steven Rostedt778c55d2009-05-01 18:44:45 -0400324 struct list_head list; /* list of buffer pages */
Steven Rostedtabc9b562008-12-02 15:34:06 -0500325 local_t write; /* index for next write */
Steven Rostedt6f807ac2008-10-04 02:00:58 -0400326 unsigned read; /* index for next read */
Steven Rostedt778c55d2009-05-01 18:44:45 -0400327 local_t entries; /* entries on this page */
Steven Rostedtabc9b562008-12-02 15:34:06 -0500328 struct buffer_data_page *page; /* Actual data page */
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400329};
330
Steven Rostedt044fa782008-12-02 23:50:03 -0500331static void rb_init_page(struct buffer_data_page *bpage)
Steven Rostedtabc9b562008-12-02 15:34:06 -0500332{
Steven Rostedt044fa782008-12-02 23:50:03 -0500333 local_set(&bpage->commit, 0);
Steven Rostedtabc9b562008-12-02 15:34:06 -0500334}
335
Steven Rostedt474d32b2009-03-03 19:51:40 -0500336/**
337 * ring_buffer_page_len - the size of data on the page.
338 * @page: The page to read
339 *
340 * Returns the amount of data on the page, including buffer page header.
341 */
Steven Rostedtef7a4a12009-03-03 00:27:49 -0500342size_t ring_buffer_page_len(void *page)
343{
Steven Rostedt474d32b2009-03-03 19:51:40 -0500344 return local_read(&((struct buffer_data_page *)page)->commit)
345 + BUF_PAGE_HDR_SIZE;
Steven Rostedtef7a4a12009-03-03 00:27:49 -0500346}
347
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400348/*
Steven Rostedted568292008-09-29 23:02:40 -0400349 * Also stolen from mm/slob.c. Thanks to Mathieu Desnoyers for pointing
350 * this issue out.
351 */
Andrew Morton34a148b2009-01-09 12:27:09 -0800352static void free_buffer_page(struct buffer_page *bpage)
Steven Rostedted568292008-09-29 23:02:40 -0400353{
Andrew Morton34a148b2009-01-09 12:27:09 -0800354 free_page((unsigned long)bpage->page);
Steven Rostedte4c2ce82008-10-01 11:14:54 -0400355 kfree(bpage);
Steven Rostedted568292008-09-29 23:02:40 -0400356}
357
358/*
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400359 * We need to fit the time_stamp delta into 27 bits.
360 */
361static inline int test_time_stamp(u64 delta)
362{
363 if (delta & TS_DELTA_TEST)
364 return 1;
365 return 0;
366}
367
Steven Rostedt474d32b2009-03-03 19:51:40 -0500368#define BUF_PAGE_SIZE (PAGE_SIZE - BUF_PAGE_HDR_SIZE)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400369
Steven Rostedtd1b182a2009-04-15 16:53:47 -0400370int ring_buffer_print_page_header(struct trace_seq *s)
371{
372 struct buffer_data_page field;
373 int ret;
374
375 ret = trace_seq_printf(s, "\tfield: u64 timestamp;\t"
376 "offset:0;\tsize:%u;\n",
377 (unsigned int)sizeof(field.time_stamp));
378
379 ret = trace_seq_printf(s, "\tfield: local_t commit;\t"
380 "offset:%u;\tsize:%u;\n",
381 (unsigned int)offsetof(typeof(field), commit),
382 (unsigned int)sizeof(field.commit));
383
384 ret = trace_seq_printf(s, "\tfield: char data;\t"
385 "offset:%u;\tsize:%u;\n",
386 (unsigned int)offsetof(typeof(field), data),
387 (unsigned int)BUF_PAGE_SIZE);
388
389 return ret;
390}
391
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400392/*
393 * head_page == tail_page && head == tail then buffer is empty.
394 */
395struct ring_buffer_per_cpu {
396 int cpu;
397 struct ring_buffer *buffer;
Steven Rostedtf83c9d02008-11-11 18:47:44 +0100398 spinlock_t reader_lock; /* serialize readers */
Steven Rostedt3e03fb72008-11-06 00:09:43 -0500399 raw_spinlock_t lock;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400400 struct lock_class_key lock_key;
401 struct list_head pages;
Steven Rostedt6f807ac2008-10-04 02:00:58 -0400402 struct buffer_page *head_page; /* read from head */
403 struct buffer_page *tail_page; /* write to tail */
Wenji Huangc3706f02009-02-10 01:03:18 -0500404 struct buffer_page *commit_page; /* committed pages */
Steven Rostedtd7690412008-10-01 00:29:53 -0400405 struct buffer_page *reader_page;
Steven Rostedtf0d2c682009-04-29 13:43:37 -0400406 unsigned long nmi_dropped;
407 unsigned long commit_overrun;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400408 unsigned long overrun;
Steven Rostedte4906ef2009-04-30 20:49:44 -0400409 unsigned long read;
410 local_t entries;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400411 u64 write_stamp;
412 u64 read_stamp;
413 atomic_t record_disabled;
414};
415
416struct ring_buffer {
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400417 unsigned pages;
418 unsigned flags;
419 int cpus;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400420 atomic_t record_disabled;
Arnaldo Carvalho de Melo00f62f62009-02-09 17:04:06 -0200421 cpumask_var_t cpumask;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400422
423 struct mutex mutex;
424
425 struct ring_buffer_per_cpu **buffers;
Steven Rostedt554f7862009-03-11 22:00:13 -0400426
Steven Rostedt59222ef2009-03-12 11:46:03 -0400427#ifdef CONFIG_HOTPLUG_CPU
Steven Rostedt554f7862009-03-11 22:00:13 -0400428 struct notifier_block cpu_notify;
429#endif
Steven Rostedt37886f62009-03-17 17:22:06 -0400430 u64 (*clock)(void);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400431};
432
433struct ring_buffer_iter {
434 struct ring_buffer_per_cpu *cpu_buffer;
435 unsigned long head;
436 struct buffer_page *head_page;
437 u64 read_stamp;
438};
439
Steven Rostedtf536aaf2008-11-10 23:07:30 -0500440/* buffer may be either ring_buffer or ring_buffer_per_cpu */
Steven Rostedtbf41a152008-10-04 02:00:59 -0400441#define RB_WARN_ON(buffer, cond) \
Steven Rostedt3e89c7b2008-11-11 15:28:41 -0500442 ({ \
443 int _____ret = unlikely(cond); \
444 if (_____ret) { \
Steven Rostedtbf41a152008-10-04 02:00:59 -0400445 atomic_inc(&buffer->record_disabled); \
446 WARN_ON(1); \
447 } \
Steven Rostedt3e89c7b2008-11-11 15:28:41 -0500448 _____ret; \
449 })
Steven Rostedtf536aaf2008-11-10 23:07:30 -0500450
Steven Rostedt37886f62009-03-17 17:22:06 -0400451/* Up this if you want to test the TIME_EXTENTS and normalization */
452#define DEBUG_SHIFT 0
453
454u64 ring_buffer_time_stamp(struct ring_buffer *buffer, int cpu)
455{
456 u64 time;
457
458 preempt_disable_notrace();
459 /* shift to debug/test normalization and TIME_EXTENTS */
460 time = buffer->clock() << DEBUG_SHIFT;
461 preempt_enable_no_resched_notrace();
462
463 return time;
464}
465EXPORT_SYMBOL_GPL(ring_buffer_time_stamp);
466
467void ring_buffer_normalize_time_stamp(struct ring_buffer *buffer,
468 int cpu, u64 *ts)
469{
470 /* Just stupid testing the normalize function and deltas */
471 *ts >>= DEBUG_SHIFT;
472}
473EXPORT_SYMBOL_GPL(ring_buffer_normalize_time_stamp);
474
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400475/**
476 * check_pages - integrity check of buffer pages
477 * @cpu_buffer: CPU buffer with pages to test
478 *
Wenji Huangc3706f02009-02-10 01:03:18 -0500479 * As a safety measure we check to make sure the data pages have not
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400480 * been corrupted.
481 */
482static int rb_check_pages(struct ring_buffer_per_cpu *cpu_buffer)
483{
484 struct list_head *head = &cpu_buffer->pages;
Steven Rostedt044fa782008-12-02 23:50:03 -0500485 struct buffer_page *bpage, *tmp;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400486
Steven Rostedt3e89c7b2008-11-11 15:28:41 -0500487 if (RB_WARN_ON(cpu_buffer, head->next->prev != head))
488 return -1;
489 if (RB_WARN_ON(cpu_buffer, head->prev->next != head))
490 return -1;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400491
Steven Rostedt044fa782008-12-02 23:50:03 -0500492 list_for_each_entry_safe(bpage, tmp, head, list) {
Steven Rostedt3e89c7b2008-11-11 15:28:41 -0500493 if (RB_WARN_ON(cpu_buffer,
Steven Rostedt044fa782008-12-02 23:50:03 -0500494 bpage->list.next->prev != &bpage->list))
Steven Rostedt3e89c7b2008-11-11 15:28:41 -0500495 return -1;
496 if (RB_WARN_ON(cpu_buffer,
Steven Rostedt044fa782008-12-02 23:50:03 -0500497 bpage->list.prev->next != &bpage->list))
Steven Rostedt3e89c7b2008-11-11 15:28:41 -0500498 return -1;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400499 }
500
501 return 0;
502}
503
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400504static int rb_allocate_pages(struct ring_buffer_per_cpu *cpu_buffer,
505 unsigned nr_pages)
506{
507 struct list_head *head = &cpu_buffer->pages;
Steven Rostedt044fa782008-12-02 23:50:03 -0500508 struct buffer_page *bpage, *tmp;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400509 unsigned long addr;
510 LIST_HEAD(pages);
511 unsigned i;
512
513 for (i = 0; i < nr_pages; i++) {
Steven Rostedt044fa782008-12-02 23:50:03 -0500514 bpage = kzalloc_node(ALIGN(sizeof(*bpage), cache_line_size()),
Steven Rostedtaa1e0e32008-10-02 19:18:09 -0400515 GFP_KERNEL, cpu_to_node(cpu_buffer->cpu));
Steven Rostedt044fa782008-12-02 23:50:03 -0500516 if (!bpage)
Steven Rostedte4c2ce82008-10-01 11:14:54 -0400517 goto free_pages;
Steven Rostedt044fa782008-12-02 23:50:03 -0500518 list_add(&bpage->list, &pages);
Steven Rostedte4c2ce82008-10-01 11:14:54 -0400519
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400520 addr = __get_free_page(GFP_KERNEL);
521 if (!addr)
522 goto free_pages;
Steven Rostedt044fa782008-12-02 23:50:03 -0500523 bpage->page = (void *)addr;
524 rb_init_page(bpage->page);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400525 }
526
527 list_splice(&pages, head);
528
529 rb_check_pages(cpu_buffer);
530
531 return 0;
532
533 free_pages:
Steven Rostedt044fa782008-12-02 23:50:03 -0500534 list_for_each_entry_safe(bpage, tmp, &pages, list) {
535 list_del_init(&bpage->list);
536 free_buffer_page(bpage);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400537 }
538 return -ENOMEM;
539}
540
541static struct ring_buffer_per_cpu *
542rb_allocate_cpu_buffer(struct ring_buffer *buffer, int cpu)
543{
544 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedt044fa782008-12-02 23:50:03 -0500545 struct buffer_page *bpage;
Steven Rostedtd7690412008-10-01 00:29:53 -0400546 unsigned long addr;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400547 int ret;
548
549 cpu_buffer = kzalloc_node(ALIGN(sizeof(*cpu_buffer), cache_line_size()),
550 GFP_KERNEL, cpu_to_node(cpu));
551 if (!cpu_buffer)
552 return NULL;
553
554 cpu_buffer->cpu = cpu;
555 cpu_buffer->buffer = buffer;
Steven Rostedtf83c9d02008-11-11 18:47:44 +0100556 spin_lock_init(&cpu_buffer->reader_lock);
Steven Rostedt3e03fb72008-11-06 00:09:43 -0500557 cpu_buffer->lock = (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400558 INIT_LIST_HEAD(&cpu_buffer->pages);
559
Steven Rostedt044fa782008-12-02 23:50:03 -0500560 bpage = kzalloc_node(ALIGN(sizeof(*bpage), cache_line_size()),
Steven Rostedte4c2ce82008-10-01 11:14:54 -0400561 GFP_KERNEL, cpu_to_node(cpu));
Steven Rostedt044fa782008-12-02 23:50:03 -0500562 if (!bpage)
Steven Rostedte4c2ce82008-10-01 11:14:54 -0400563 goto fail_free_buffer;
564
Steven Rostedt044fa782008-12-02 23:50:03 -0500565 cpu_buffer->reader_page = bpage;
Steven Rostedtd7690412008-10-01 00:29:53 -0400566 addr = __get_free_page(GFP_KERNEL);
567 if (!addr)
Steven Rostedte4c2ce82008-10-01 11:14:54 -0400568 goto fail_free_reader;
Steven Rostedt044fa782008-12-02 23:50:03 -0500569 bpage->page = (void *)addr;
570 rb_init_page(bpage->page);
Steven Rostedte4c2ce82008-10-01 11:14:54 -0400571
Steven Rostedtd7690412008-10-01 00:29:53 -0400572 INIT_LIST_HEAD(&cpu_buffer->reader_page->list);
Steven Rostedtd7690412008-10-01 00:29:53 -0400573
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400574 ret = rb_allocate_pages(cpu_buffer, buffer->pages);
575 if (ret < 0)
Steven Rostedtd7690412008-10-01 00:29:53 -0400576 goto fail_free_reader;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400577
578 cpu_buffer->head_page
579 = list_entry(cpu_buffer->pages.next, struct buffer_page, list);
Steven Rostedtbf41a152008-10-04 02:00:59 -0400580 cpu_buffer->tail_page = cpu_buffer->commit_page = cpu_buffer->head_page;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400581
582 return cpu_buffer;
583
Steven Rostedtd7690412008-10-01 00:29:53 -0400584 fail_free_reader:
585 free_buffer_page(cpu_buffer->reader_page);
586
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400587 fail_free_buffer:
588 kfree(cpu_buffer);
589 return NULL;
590}
591
592static void rb_free_cpu_buffer(struct ring_buffer_per_cpu *cpu_buffer)
593{
594 struct list_head *head = &cpu_buffer->pages;
Steven Rostedt044fa782008-12-02 23:50:03 -0500595 struct buffer_page *bpage, *tmp;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400596
Steven Rostedtd7690412008-10-01 00:29:53 -0400597 free_buffer_page(cpu_buffer->reader_page);
598
Steven Rostedt044fa782008-12-02 23:50:03 -0500599 list_for_each_entry_safe(bpage, tmp, head, list) {
600 list_del_init(&bpage->list);
601 free_buffer_page(bpage);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400602 }
603 kfree(cpu_buffer);
604}
605
Steven Rostedta7b13742008-09-29 23:02:39 -0400606/*
607 * Causes compile errors if the struct buffer_page gets bigger
608 * than the struct page.
609 */
610extern int ring_buffer_page_too_big(void);
611
Steven Rostedt59222ef2009-03-12 11:46:03 -0400612#ifdef CONFIG_HOTPLUG_CPU
Frederic Weisbecker09c9e842009-03-21 04:33:36 +0100613static int rb_cpu_notify(struct notifier_block *self,
614 unsigned long action, void *hcpu);
Steven Rostedt554f7862009-03-11 22:00:13 -0400615#endif
616
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400617/**
618 * ring_buffer_alloc - allocate a new ring_buffer
Robert Richter68814b52008-11-24 12:24:12 +0100619 * @size: the size in bytes per cpu that is needed.
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400620 * @flags: attributes to set for the ring buffer.
621 *
622 * Currently the only flag that is available is the RB_FL_OVERWRITE
623 * flag. This flag means that the buffer will overwrite old data
624 * when the buffer wraps. If this flag is not set, the buffer will
625 * drop data when the tail hits the head.
626 */
627struct ring_buffer *ring_buffer_alloc(unsigned long size, unsigned flags)
628{
629 struct ring_buffer *buffer;
630 int bsize;
631 int cpu;
632
Steven Rostedta7b13742008-09-29 23:02:39 -0400633 /* Paranoid! Optimizes out when all is well */
634 if (sizeof(struct buffer_page) > sizeof(struct page))
635 ring_buffer_page_too_big();
636
637
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400638 /* keep it in its own cache line */
639 buffer = kzalloc(ALIGN(sizeof(*buffer), cache_line_size()),
640 GFP_KERNEL);
641 if (!buffer)
642 return NULL;
643
Rusty Russell9e01c1b2009-01-01 10:12:22 +1030644 if (!alloc_cpumask_var(&buffer->cpumask, GFP_KERNEL))
645 goto fail_free_buffer;
646
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400647 buffer->pages = DIV_ROUND_UP(size, BUF_PAGE_SIZE);
648 buffer->flags = flags;
Steven Rostedt37886f62009-03-17 17:22:06 -0400649 buffer->clock = trace_clock_local;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400650
651 /* need at least two pages */
652 if (buffer->pages == 1)
653 buffer->pages++;
654
Frederic Weisbecker3bf832c2009-03-19 14:47:33 +0100655 /*
656 * In case of non-hotplug cpu, if the ring-buffer is allocated
657 * in early initcall, it will not be notified of secondary cpus.
658 * In that off case, we need to allocate for all possible cpus.
659 */
660#ifdef CONFIG_HOTPLUG_CPU
Steven Rostedt554f7862009-03-11 22:00:13 -0400661 get_online_cpus();
662 cpumask_copy(buffer->cpumask, cpu_online_mask);
Frederic Weisbecker3bf832c2009-03-19 14:47:33 +0100663#else
664 cpumask_copy(buffer->cpumask, cpu_possible_mask);
665#endif
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400666 buffer->cpus = nr_cpu_ids;
667
668 bsize = sizeof(void *) * nr_cpu_ids;
669 buffer->buffers = kzalloc(ALIGN(bsize, cache_line_size()),
670 GFP_KERNEL);
671 if (!buffer->buffers)
Rusty Russell9e01c1b2009-01-01 10:12:22 +1030672 goto fail_free_cpumask;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400673
674 for_each_buffer_cpu(buffer, cpu) {
675 buffer->buffers[cpu] =
676 rb_allocate_cpu_buffer(buffer, cpu);
677 if (!buffer->buffers[cpu])
678 goto fail_free_buffers;
679 }
680
Steven Rostedt59222ef2009-03-12 11:46:03 -0400681#ifdef CONFIG_HOTPLUG_CPU
Steven Rostedt554f7862009-03-11 22:00:13 -0400682 buffer->cpu_notify.notifier_call = rb_cpu_notify;
683 buffer->cpu_notify.priority = 0;
684 register_cpu_notifier(&buffer->cpu_notify);
685#endif
686
687 put_online_cpus();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400688 mutex_init(&buffer->mutex);
689
690 return buffer;
691
692 fail_free_buffers:
693 for_each_buffer_cpu(buffer, cpu) {
694 if (buffer->buffers[cpu])
695 rb_free_cpu_buffer(buffer->buffers[cpu]);
696 }
697 kfree(buffer->buffers);
698
Rusty Russell9e01c1b2009-01-01 10:12:22 +1030699 fail_free_cpumask:
700 free_cpumask_var(buffer->cpumask);
Steven Rostedt554f7862009-03-11 22:00:13 -0400701 put_online_cpus();
Rusty Russell9e01c1b2009-01-01 10:12:22 +1030702
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400703 fail_free_buffer:
704 kfree(buffer);
705 return NULL;
706}
Robert Richterc4f50182008-12-11 16:49:22 +0100707EXPORT_SYMBOL_GPL(ring_buffer_alloc);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400708
709/**
710 * ring_buffer_free - free a ring buffer.
711 * @buffer: the buffer to free.
712 */
713void
714ring_buffer_free(struct ring_buffer *buffer)
715{
716 int cpu;
717
Steven Rostedt554f7862009-03-11 22:00:13 -0400718 get_online_cpus();
719
Steven Rostedt59222ef2009-03-12 11:46:03 -0400720#ifdef CONFIG_HOTPLUG_CPU
Steven Rostedt554f7862009-03-11 22:00:13 -0400721 unregister_cpu_notifier(&buffer->cpu_notify);
722#endif
723
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400724 for_each_buffer_cpu(buffer, cpu)
725 rb_free_cpu_buffer(buffer->buffers[cpu]);
726
Steven Rostedt554f7862009-03-11 22:00:13 -0400727 put_online_cpus();
728
Rusty Russell9e01c1b2009-01-01 10:12:22 +1030729 free_cpumask_var(buffer->cpumask);
730
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400731 kfree(buffer);
732}
Robert Richterc4f50182008-12-11 16:49:22 +0100733EXPORT_SYMBOL_GPL(ring_buffer_free);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400734
Steven Rostedt37886f62009-03-17 17:22:06 -0400735void ring_buffer_set_clock(struct ring_buffer *buffer,
736 u64 (*clock)(void))
737{
738 buffer->clock = clock;
739}
740
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400741static void rb_reset_cpu(struct ring_buffer_per_cpu *cpu_buffer);
742
743static void
744rb_remove_pages(struct ring_buffer_per_cpu *cpu_buffer, unsigned nr_pages)
745{
Steven Rostedt044fa782008-12-02 23:50:03 -0500746 struct buffer_page *bpage;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400747 struct list_head *p;
748 unsigned i;
749
750 atomic_inc(&cpu_buffer->record_disabled);
751 synchronize_sched();
752
753 for (i = 0; i < nr_pages; i++) {
Steven Rostedt3e89c7b2008-11-11 15:28:41 -0500754 if (RB_WARN_ON(cpu_buffer, list_empty(&cpu_buffer->pages)))
755 return;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400756 p = cpu_buffer->pages.next;
Steven Rostedt044fa782008-12-02 23:50:03 -0500757 bpage = list_entry(p, struct buffer_page, list);
758 list_del_init(&bpage->list);
759 free_buffer_page(bpage);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400760 }
Steven Rostedt3e89c7b2008-11-11 15:28:41 -0500761 if (RB_WARN_ON(cpu_buffer, list_empty(&cpu_buffer->pages)))
762 return;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400763
764 rb_reset_cpu(cpu_buffer);
765
766 rb_check_pages(cpu_buffer);
767
768 atomic_dec(&cpu_buffer->record_disabled);
769
770}
771
772static void
773rb_insert_pages(struct ring_buffer_per_cpu *cpu_buffer,
774 struct list_head *pages, unsigned nr_pages)
775{
Steven Rostedt044fa782008-12-02 23:50:03 -0500776 struct buffer_page *bpage;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400777 struct list_head *p;
778 unsigned i;
779
780 atomic_inc(&cpu_buffer->record_disabled);
781 synchronize_sched();
782
783 for (i = 0; i < nr_pages; i++) {
Steven Rostedt3e89c7b2008-11-11 15:28:41 -0500784 if (RB_WARN_ON(cpu_buffer, list_empty(pages)))
785 return;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400786 p = pages->next;
Steven Rostedt044fa782008-12-02 23:50:03 -0500787 bpage = list_entry(p, struct buffer_page, list);
788 list_del_init(&bpage->list);
789 list_add_tail(&bpage->list, &cpu_buffer->pages);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400790 }
791 rb_reset_cpu(cpu_buffer);
792
793 rb_check_pages(cpu_buffer);
794
795 atomic_dec(&cpu_buffer->record_disabled);
796}
797
798/**
799 * ring_buffer_resize - resize the ring buffer
800 * @buffer: the buffer to resize.
801 * @size: the new size.
802 *
803 * The tracer is responsible for making sure that the buffer is
804 * not being used while changing the size.
805 * Note: We may be able to change the above requirement by using
806 * RCU synchronizations.
807 *
808 * Minimum size is 2 * BUF_PAGE_SIZE.
809 *
810 * Returns -1 on failure.
811 */
812int ring_buffer_resize(struct ring_buffer *buffer, unsigned long size)
813{
814 struct ring_buffer_per_cpu *cpu_buffer;
815 unsigned nr_pages, rm_pages, new_pages;
Steven Rostedt044fa782008-12-02 23:50:03 -0500816 struct buffer_page *bpage, *tmp;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400817 unsigned long buffer_size;
818 unsigned long addr;
819 LIST_HEAD(pages);
820 int i, cpu;
821
Ingo Molnaree51a1d2008-11-13 14:58:31 +0100822 /*
823 * Always succeed at resizing a non-existent buffer:
824 */
825 if (!buffer)
826 return size;
827
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400828 size = DIV_ROUND_UP(size, BUF_PAGE_SIZE);
829 size *= BUF_PAGE_SIZE;
830 buffer_size = buffer->pages * BUF_PAGE_SIZE;
831
832 /* we need a minimum of two pages */
833 if (size < BUF_PAGE_SIZE * 2)
834 size = BUF_PAGE_SIZE * 2;
835
836 if (size == buffer_size)
837 return size;
838
839 mutex_lock(&buffer->mutex);
Steven Rostedt554f7862009-03-11 22:00:13 -0400840 get_online_cpus();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400841
842 nr_pages = DIV_ROUND_UP(size, BUF_PAGE_SIZE);
843
844 if (size < buffer_size) {
845
846 /* easy case, just free pages */
Steven Rostedt554f7862009-03-11 22:00:13 -0400847 if (RB_WARN_ON(buffer, nr_pages >= buffer->pages))
848 goto out_fail;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400849
850 rm_pages = buffer->pages - nr_pages;
851
852 for_each_buffer_cpu(buffer, cpu) {
853 cpu_buffer = buffer->buffers[cpu];
854 rb_remove_pages(cpu_buffer, rm_pages);
855 }
856 goto out;
857 }
858
859 /*
860 * This is a bit more difficult. We only want to add pages
861 * when we can allocate enough for all CPUs. We do this
862 * by allocating all the pages and storing them on a local
863 * link list. If we succeed in our allocation, then we
864 * add these pages to the cpu_buffers. Otherwise we just free
865 * them all and return -ENOMEM;
866 */
Steven Rostedt554f7862009-03-11 22:00:13 -0400867 if (RB_WARN_ON(buffer, nr_pages <= buffer->pages))
868 goto out_fail;
Steven Rostedtf536aaf2008-11-10 23:07:30 -0500869
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400870 new_pages = nr_pages - buffer->pages;
871
872 for_each_buffer_cpu(buffer, cpu) {
873 for (i = 0; i < new_pages; i++) {
Steven Rostedt044fa782008-12-02 23:50:03 -0500874 bpage = kzalloc_node(ALIGN(sizeof(*bpage),
Steven Rostedte4c2ce82008-10-01 11:14:54 -0400875 cache_line_size()),
876 GFP_KERNEL, cpu_to_node(cpu));
Steven Rostedt044fa782008-12-02 23:50:03 -0500877 if (!bpage)
Steven Rostedte4c2ce82008-10-01 11:14:54 -0400878 goto free_pages;
Steven Rostedt044fa782008-12-02 23:50:03 -0500879 list_add(&bpage->list, &pages);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400880 addr = __get_free_page(GFP_KERNEL);
881 if (!addr)
882 goto free_pages;
Steven Rostedt044fa782008-12-02 23:50:03 -0500883 bpage->page = (void *)addr;
884 rb_init_page(bpage->page);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400885 }
886 }
887
888 for_each_buffer_cpu(buffer, cpu) {
889 cpu_buffer = buffer->buffers[cpu];
890 rb_insert_pages(cpu_buffer, &pages, new_pages);
891 }
892
Steven Rostedt554f7862009-03-11 22:00:13 -0400893 if (RB_WARN_ON(buffer, !list_empty(&pages)))
894 goto out_fail;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400895
896 out:
897 buffer->pages = nr_pages;
Steven Rostedt554f7862009-03-11 22:00:13 -0400898 put_online_cpus();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400899 mutex_unlock(&buffer->mutex);
900
901 return size;
902
903 free_pages:
Steven Rostedt044fa782008-12-02 23:50:03 -0500904 list_for_each_entry_safe(bpage, tmp, &pages, list) {
905 list_del_init(&bpage->list);
906 free_buffer_page(bpage);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400907 }
Steven Rostedt554f7862009-03-11 22:00:13 -0400908 put_online_cpus();
Vegard Nossum641d2f62008-11-18 19:22:13 +0100909 mutex_unlock(&buffer->mutex);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400910 return -ENOMEM;
Steven Rostedt554f7862009-03-11 22:00:13 -0400911
912 /*
913 * Something went totally wrong, and we are too paranoid
914 * to even clean up the mess.
915 */
916 out_fail:
917 put_online_cpus();
918 mutex_unlock(&buffer->mutex);
919 return -1;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400920}
Robert Richterc4f50182008-12-11 16:49:22 +0100921EXPORT_SYMBOL_GPL(ring_buffer_resize);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400922
Steven Rostedt8789a9e2008-12-02 15:34:07 -0500923static inline void *
Steven Rostedt044fa782008-12-02 23:50:03 -0500924__rb_data_page_index(struct buffer_data_page *bpage, unsigned index)
Steven Rostedt8789a9e2008-12-02 15:34:07 -0500925{
Steven Rostedt044fa782008-12-02 23:50:03 -0500926 return bpage->data + index;
Steven Rostedt8789a9e2008-12-02 15:34:07 -0500927}
928
Steven Rostedt044fa782008-12-02 23:50:03 -0500929static inline void *__rb_page_index(struct buffer_page *bpage, unsigned index)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400930{
Steven Rostedt044fa782008-12-02 23:50:03 -0500931 return bpage->page->data + index;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400932}
933
934static inline struct ring_buffer_event *
Steven Rostedtd7690412008-10-01 00:29:53 -0400935rb_reader_event(struct ring_buffer_per_cpu *cpu_buffer)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400936{
Steven Rostedt6f807ac2008-10-04 02:00:58 -0400937 return __rb_page_index(cpu_buffer->reader_page,
938 cpu_buffer->reader_page->read);
939}
940
941static inline struct ring_buffer_event *
942rb_head_event(struct ring_buffer_per_cpu *cpu_buffer)
943{
944 return __rb_page_index(cpu_buffer->head_page,
945 cpu_buffer->head_page->read);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400946}
947
948static inline struct ring_buffer_event *
949rb_iter_head_event(struct ring_buffer_iter *iter)
950{
Steven Rostedt6f807ac2008-10-04 02:00:58 -0400951 return __rb_page_index(iter->head_page, iter->head);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400952}
953
Steven Rostedtbf41a152008-10-04 02:00:59 -0400954static inline unsigned rb_page_write(struct buffer_page *bpage)
955{
956 return local_read(&bpage->write);
957}
958
959static inline unsigned rb_page_commit(struct buffer_page *bpage)
960{
Steven Rostedtabc9b562008-12-02 15:34:06 -0500961 return local_read(&bpage->page->commit);
Steven Rostedtbf41a152008-10-04 02:00:59 -0400962}
963
964/* Size is determined by what has been commited */
965static inline unsigned rb_page_size(struct buffer_page *bpage)
966{
967 return rb_page_commit(bpage);
968}
969
970static inline unsigned
971rb_commit_index(struct ring_buffer_per_cpu *cpu_buffer)
972{
973 return rb_page_commit(cpu_buffer->commit_page);
974}
975
976static inline unsigned rb_head_size(struct ring_buffer_per_cpu *cpu_buffer)
977{
978 return rb_page_commit(cpu_buffer->head_page);
979}
980
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400981static inline void rb_inc_page(struct ring_buffer_per_cpu *cpu_buffer,
Steven Rostedt044fa782008-12-02 23:50:03 -0500982 struct buffer_page **bpage)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400983{
Steven Rostedt044fa782008-12-02 23:50:03 -0500984 struct list_head *p = (*bpage)->list.next;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400985
986 if (p == &cpu_buffer->pages)
987 p = p->next;
988
Steven Rostedt044fa782008-12-02 23:50:03 -0500989 *bpage = list_entry(p, struct buffer_page, list);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400990}
991
Steven Rostedtbf41a152008-10-04 02:00:59 -0400992static inline unsigned
993rb_event_index(struct ring_buffer_event *event)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400994{
Steven Rostedtbf41a152008-10-04 02:00:59 -0400995 unsigned long addr = (unsigned long)event;
996
997 return (addr & ~PAGE_MASK) - (PAGE_SIZE - BUF_PAGE_SIZE);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400998}
999
Andrew Morton34a148b2009-01-09 12:27:09 -08001000static int
Steven Rostedtbf41a152008-10-04 02:00:59 -04001001rb_is_commit(struct ring_buffer_per_cpu *cpu_buffer,
1002 struct ring_buffer_event *event)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001003{
Steven Rostedtbf41a152008-10-04 02:00:59 -04001004 unsigned long addr = (unsigned long)event;
1005 unsigned long index;
1006
1007 index = rb_event_index(event);
1008 addr &= PAGE_MASK;
1009
1010 return cpu_buffer->commit_page->page == (void *)addr &&
1011 rb_commit_index(cpu_buffer) == index;
1012}
1013
Andrew Morton34a148b2009-01-09 12:27:09 -08001014static void
Steven Rostedtbf41a152008-10-04 02:00:59 -04001015rb_set_commit_event(struct ring_buffer_per_cpu *cpu_buffer,
1016 struct ring_buffer_event *event)
1017{
1018 unsigned long addr = (unsigned long)event;
1019 unsigned long index;
1020
1021 index = rb_event_index(event);
1022 addr &= PAGE_MASK;
1023
1024 while (cpu_buffer->commit_page->page != (void *)addr) {
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05001025 if (RB_WARN_ON(cpu_buffer,
1026 cpu_buffer->commit_page == cpu_buffer->tail_page))
1027 return;
Steven Rostedtabc9b562008-12-02 15:34:06 -05001028 cpu_buffer->commit_page->page->commit =
Steven Rostedtbf41a152008-10-04 02:00:59 -04001029 cpu_buffer->commit_page->write;
1030 rb_inc_page(cpu_buffer, &cpu_buffer->commit_page);
Steven Rostedtabc9b562008-12-02 15:34:06 -05001031 cpu_buffer->write_stamp =
1032 cpu_buffer->commit_page->page->time_stamp;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001033 }
1034
1035 /* Now set the commit to the event's index */
Steven Rostedtabc9b562008-12-02 15:34:06 -05001036 local_set(&cpu_buffer->commit_page->page->commit, index);
Steven Rostedtbf41a152008-10-04 02:00:59 -04001037}
1038
Andrew Morton34a148b2009-01-09 12:27:09 -08001039static void
Steven Rostedtbf41a152008-10-04 02:00:59 -04001040rb_set_commit_to_write(struct ring_buffer_per_cpu *cpu_buffer)
1041{
1042 /*
1043 * We only race with interrupts and NMIs on this CPU.
1044 * If we own the commit event, then we can commit
1045 * all others that interrupted us, since the interruptions
1046 * are in stack format (they finish before they come
1047 * back to us). This allows us to do a simple loop to
1048 * assign the commit to the tail.
1049 */
Steven Rostedta8ccf1d2008-12-23 11:32:24 -05001050 again:
Steven Rostedtbf41a152008-10-04 02:00:59 -04001051 while (cpu_buffer->commit_page != cpu_buffer->tail_page) {
Steven Rostedtabc9b562008-12-02 15:34:06 -05001052 cpu_buffer->commit_page->page->commit =
Steven Rostedtbf41a152008-10-04 02:00:59 -04001053 cpu_buffer->commit_page->write;
1054 rb_inc_page(cpu_buffer, &cpu_buffer->commit_page);
Steven Rostedtabc9b562008-12-02 15:34:06 -05001055 cpu_buffer->write_stamp =
1056 cpu_buffer->commit_page->page->time_stamp;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001057 /* add barrier to keep gcc from optimizing too much */
1058 barrier();
1059 }
1060 while (rb_commit_index(cpu_buffer) !=
1061 rb_page_write(cpu_buffer->commit_page)) {
Steven Rostedtabc9b562008-12-02 15:34:06 -05001062 cpu_buffer->commit_page->page->commit =
Steven Rostedtbf41a152008-10-04 02:00:59 -04001063 cpu_buffer->commit_page->write;
1064 barrier();
1065 }
Steven Rostedta8ccf1d2008-12-23 11:32:24 -05001066
1067 /* again, keep gcc from optimizing */
1068 barrier();
1069
1070 /*
1071 * If an interrupt came in just after the first while loop
1072 * and pushed the tail page forward, we will be left with
1073 * a dangling commit that will never go forward.
1074 */
1075 if (unlikely(cpu_buffer->commit_page != cpu_buffer->tail_page))
1076 goto again;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001077}
1078
Steven Rostedtd7690412008-10-01 00:29:53 -04001079static void rb_reset_reader_page(struct ring_buffer_per_cpu *cpu_buffer)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001080{
Steven Rostedtabc9b562008-12-02 15:34:06 -05001081 cpu_buffer->read_stamp = cpu_buffer->reader_page->page->time_stamp;
Steven Rostedt6f807ac2008-10-04 02:00:58 -04001082 cpu_buffer->reader_page->read = 0;
Steven Rostedtd7690412008-10-01 00:29:53 -04001083}
1084
Andrew Morton34a148b2009-01-09 12:27:09 -08001085static void rb_inc_iter(struct ring_buffer_iter *iter)
Steven Rostedtd7690412008-10-01 00:29:53 -04001086{
1087 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
1088
1089 /*
1090 * The iterator could be on the reader page (it starts there).
1091 * But the head could have moved, since the reader was
1092 * found. Check for this case and assign the iterator
1093 * to the head page instead of next.
1094 */
1095 if (iter->head_page == cpu_buffer->reader_page)
1096 iter->head_page = cpu_buffer->head_page;
1097 else
1098 rb_inc_page(cpu_buffer, &iter->head_page);
1099
Steven Rostedtabc9b562008-12-02 15:34:06 -05001100 iter->read_stamp = iter->head_page->page->time_stamp;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001101 iter->head = 0;
1102}
1103
1104/**
1105 * ring_buffer_update_event - update event type and data
1106 * @event: the even to update
1107 * @type: the type of event
1108 * @length: the size of the event field in the ring buffer
1109 *
1110 * Update the type and data fields of the event. The length
1111 * is the actual size that is written to the ring buffer,
1112 * and with this, we can determine what to place into the
1113 * data field.
1114 */
Andrew Morton34a148b2009-01-09 12:27:09 -08001115static void
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001116rb_update_event(struct ring_buffer_event *event,
1117 unsigned type, unsigned length)
1118{
Lai Jiangshan334d4162009-04-24 11:27:05 +08001119 event->type_len = type;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001120
1121 switch (type) {
1122
1123 case RINGBUF_TYPE_PADDING:
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001124 case RINGBUF_TYPE_TIME_EXTEND:
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001125 case RINGBUF_TYPE_TIME_STAMP:
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001126 break;
1127
Lai Jiangshan334d4162009-04-24 11:27:05 +08001128 case 0:
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001129 length -= RB_EVNT_HDR_SIZE;
Lai Jiangshan334d4162009-04-24 11:27:05 +08001130 if (length > RB_MAX_SMALL_DATA)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001131 event->array[0] = length;
Lai Jiangshan334d4162009-04-24 11:27:05 +08001132 else
1133 event->type_len = DIV_ROUND_UP(length, RB_ALIGNMENT);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001134 break;
1135 default:
1136 BUG();
1137 }
1138}
1139
Andrew Morton34a148b2009-01-09 12:27:09 -08001140static unsigned rb_calculate_event_length(unsigned length)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001141{
1142 struct ring_buffer_event event; /* Used only for sizeof array */
1143
1144 /* zero length can cause confusions */
1145 if (!length)
1146 length = 1;
1147
1148 if (length > RB_MAX_SMALL_DATA)
1149 length += sizeof(event.array[0]);
1150
1151 length += RB_EVNT_HDR_SIZE;
1152 length = ALIGN(length, RB_ALIGNMENT);
1153
1154 return length;
1155}
1156
1157static struct ring_buffer_event *
1158__rb_reserve_next(struct ring_buffer_per_cpu *cpu_buffer,
1159 unsigned type, unsigned long length, u64 *ts)
1160{
Steven Rostedt98db8df2008-12-23 11:32:25 -05001161 struct buffer_page *tail_page, *head_page, *reader_page, *commit_page;
Steven Rostedtaa20ae82009-05-05 21:16:11 -04001162 struct buffer_page *next_page;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001163 unsigned long tail, write;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001164 struct ring_buffer *buffer = cpu_buffer->buffer;
1165 struct ring_buffer_event *event;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001166 unsigned long flags;
Steven Rostedt78d904b2009-02-05 18:43:07 -05001167 bool lock_taken = false;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001168
Steven Rostedt98db8df2008-12-23 11:32:25 -05001169 commit_page = cpu_buffer->commit_page;
1170 /* we just need to protect against interrupts */
1171 barrier();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001172 tail_page = cpu_buffer->tail_page;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001173 write = local_add_return(length, &tail_page->write);
1174 tail = write - length;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001175
Steven Rostedtbf41a152008-10-04 02:00:59 -04001176 /* See if we shot pass the end of this buffer page */
Steven Rostedtaa20ae82009-05-05 21:16:11 -04001177 if (write > BUF_PAGE_SIZE)
1178 goto next_page;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001179
Steven Rostedtbf41a152008-10-04 02:00:59 -04001180 /* We reserved something on the buffer */
1181
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05001182 if (RB_WARN_ON(cpu_buffer, write > BUF_PAGE_SIZE))
1183 return NULL;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001184
Steven Rostedt6f807ac2008-10-04 02:00:58 -04001185 event = __rb_page_index(tail_page, tail);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001186 rb_update_event(event, type, length);
1187
Steven Rostedt778c55d2009-05-01 18:44:45 -04001188 /* The passed in type is zero for DATA */
1189 if (likely(!type))
1190 local_inc(&tail_page->entries);
1191
Steven Rostedtbf41a152008-10-04 02:00:59 -04001192 /*
1193 * If this is a commit and the tail is zero, then update
1194 * this page's time stamp.
1195 */
1196 if (!tail && rb_is_commit(cpu_buffer, event))
Steven Rostedtabc9b562008-12-02 15:34:06 -05001197 cpu_buffer->commit_page->page->time_stamp = *ts;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001198
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001199 return event;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001200
Steven Rostedtaa20ae82009-05-05 21:16:11 -04001201 next_page:
1202
1203 next_page = tail_page;
1204
1205 local_irq_save(flags);
1206 /*
1207 * Since the write to the buffer is still not
1208 * fully lockless, we must be careful with NMIs.
1209 * The locks in the writers are taken when a write
1210 * crosses to a new page. The locks protect against
1211 * races with the readers (this will soon be fixed
1212 * with a lockless solution).
1213 *
1214 * Because we can not protect against NMIs, and we
1215 * want to keep traces reentrant, we need to manage
1216 * what happens when we are in an NMI.
1217 *
1218 * NMIs can happen after we take the lock.
1219 * If we are in an NMI, only take the lock
1220 * if it is not already taken. Otherwise
1221 * simply fail.
1222 */
1223 if (unlikely(in_nmi())) {
1224 if (!__raw_spin_trylock(&cpu_buffer->lock)) {
1225 cpu_buffer->nmi_dropped++;
1226 goto out_reset;
1227 }
1228 } else
1229 __raw_spin_lock(&cpu_buffer->lock);
1230
1231 lock_taken = true;
1232
1233 rb_inc_page(cpu_buffer, &next_page);
1234
1235 head_page = cpu_buffer->head_page;
1236 reader_page = cpu_buffer->reader_page;
1237
1238 /* we grabbed the lock before incrementing */
1239 if (RB_WARN_ON(cpu_buffer, next_page == reader_page))
1240 goto out_reset;
1241
1242 /*
1243 * If for some reason, we had an interrupt storm that made
1244 * it all the way around the buffer, bail, and warn
1245 * about it.
1246 */
1247 if (unlikely(next_page == commit_page)) {
1248 cpu_buffer->commit_overrun++;
1249 goto out_reset;
1250 }
1251
1252 if (next_page == head_page) {
1253 if (!(buffer->flags & RB_FL_OVERWRITE))
1254 goto out_reset;
1255
1256 /* tail_page has not moved yet? */
1257 if (tail_page == cpu_buffer->tail_page) {
1258 /* count overflows */
1259 cpu_buffer->overrun +=
1260 local_read(&head_page->entries);
1261
1262 rb_inc_page(cpu_buffer, &head_page);
1263 cpu_buffer->head_page = head_page;
1264 cpu_buffer->head_page->read = 0;
1265 }
1266 }
1267
1268 /*
1269 * If the tail page is still the same as what we think
1270 * it is, then it is up to us to update the tail
1271 * pointer.
1272 */
1273 if (tail_page == cpu_buffer->tail_page) {
1274 local_set(&next_page->write, 0);
1275 local_set(&next_page->entries, 0);
1276 local_set(&next_page->page->commit, 0);
1277 cpu_buffer->tail_page = next_page;
1278
1279 /* reread the time stamp */
1280 *ts = ring_buffer_time_stamp(buffer, cpu_buffer->cpu);
1281 cpu_buffer->tail_page->page->time_stamp = *ts;
1282 }
1283
1284 /*
1285 * The actual tail page has moved forward.
1286 */
1287 if (tail < BUF_PAGE_SIZE) {
1288 /* Mark the rest of the page with padding */
1289 event = __rb_page_index(tail_page, tail);
1290 rb_event_set_padding(event);
1291 }
1292
Steven Rostedt8e7abf12009-05-06 10:26:45 -04001293 /* Set the write back to the previous setting */
1294 local_sub(length, &tail_page->write);
Steven Rostedtaa20ae82009-05-05 21:16:11 -04001295
1296 /*
1297 * If this was a commit entry that failed,
1298 * increment that too
1299 */
1300 if (tail_page == cpu_buffer->commit_page &&
1301 tail == rb_commit_index(cpu_buffer)) {
1302 rb_set_commit_to_write(cpu_buffer);
1303 }
1304
1305 __raw_spin_unlock(&cpu_buffer->lock);
1306 local_irq_restore(flags);
1307
1308 /* fail and let the caller try again */
1309 return ERR_PTR(-EAGAIN);
1310
Steven Rostedt45141d42009-02-12 13:19:48 -05001311 out_reset:
Lai Jiangshan6f3b3442009-01-12 11:06:18 +08001312 /* reset write */
Steven Rostedt8e7abf12009-05-06 10:26:45 -04001313 local_sub(length, &tail_page->write);
Lai Jiangshan6f3b3442009-01-12 11:06:18 +08001314
Steven Rostedt78d904b2009-02-05 18:43:07 -05001315 if (likely(lock_taken))
1316 __raw_spin_unlock(&cpu_buffer->lock);
Steven Rostedt3e03fb72008-11-06 00:09:43 -05001317 local_irq_restore(flags);
Steven Rostedtbf41a152008-10-04 02:00:59 -04001318 return NULL;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001319}
1320
1321static int
1322rb_add_time_stamp(struct ring_buffer_per_cpu *cpu_buffer,
1323 u64 *ts, u64 *delta)
1324{
1325 struct ring_buffer_event *event;
1326 static int once;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001327 int ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001328
1329 if (unlikely(*delta > (1ULL << 59) && !once++)) {
1330 printk(KERN_WARNING "Delta way too big! %llu"
1331 " ts=%llu write stamp = %llu\n",
Stephen Rothwelle2862c92008-10-27 17:43:28 +11001332 (unsigned long long)*delta,
1333 (unsigned long long)*ts,
1334 (unsigned long long)cpu_buffer->write_stamp);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001335 WARN_ON(1);
1336 }
1337
1338 /*
1339 * The delta is too big, we to add a
1340 * new timestamp.
1341 */
1342 event = __rb_reserve_next(cpu_buffer,
1343 RINGBUF_TYPE_TIME_EXTEND,
1344 RB_LEN_TIME_EXTEND,
1345 ts);
1346 if (!event)
Steven Rostedtbf41a152008-10-04 02:00:59 -04001347 return -EBUSY;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001348
Steven Rostedtbf41a152008-10-04 02:00:59 -04001349 if (PTR_ERR(event) == -EAGAIN)
1350 return -EAGAIN;
1351
1352 /* Only a commited time event can update the write stamp */
1353 if (rb_is_commit(cpu_buffer, event)) {
1354 /*
1355 * If this is the first on the page, then we need to
1356 * update the page itself, and just put in a zero.
1357 */
1358 if (rb_event_index(event)) {
1359 event->time_delta = *delta & TS_MASK;
1360 event->array[0] = *delta >> TS_SHIFT;
1361 } else {
Steven Rostedtabc9b562008-12-02 15:34:06 -05001362 cpu_buffer->commit_page->page->time_stamp = *ts;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001363 event->time_delta = 0;
1364 event->array[0] = 0;
1365 }
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001366 cpu_buffer->write_stamp = *ts;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001367 /* let the caller know this was the commit */
1368 ret = 1;
1369 } else {
1370 /* Darn, this is just wasted space */
1371 event->time_delta = 0;
1372 event->array[0] = 0;
1373 ret = 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001374 }
1375
Steven Rostedtbf41a152008-10-04 02:00:59 -04001376 *delta = 0;
1377
1378 return ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001379}
1380
1381static struct ring_buffer_event *
1382rb_reserve_next_event(struct ring_buffer_per_cpu *cpu_buffer,
1383 unsigned type, unsigned long length)
1384{
1385 struct ring_buffer_event *event;
1386 u64 ts, delta;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001387 int commit = 0;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04001388 int nr_loops = 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001389
Steven Rostedtbf41a152008-10-04 02:00:59 -04001390 again:
Steven Rostedt818e3dd2008-10-31 09:58:35 -04001391 /*
1392 * We allow for interrupts to reenter here and do a trace.
1393 * If one does, it will cause this original code to loop
1394 * back here. Even with heavy interrupts happening, this
1395 * should only happen a few times in a row. If this happens
1396 * 1000 times in a row, there must be either an interrupt
1397 * storm or we have something buggy.
1398 * Bail!
1399 */
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05001400 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 1000))
Steven Rostedt818e3dd2008-10-31 09:58:35 -04001401 return NULL;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04001402
Steven Rostedt37886f62009-03-17 17:22:06 -04001403 ts = ring_buffer_time_stamp(cpu_buffer->buffer, cpu_buffer->cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001404
Steven Rostedtbf41a152008-10-04 02:00:59 -04001405 /*
1406 * Only the first commit can update the timestamp.
1407 * Yes there is a race here. If an interrupt comes in
1408 * just after the conditional and it traces too, then it
1409 * will also check the deltas. More than one timestamp may
1410 * also be made. But only the entry that did the actual
1411 * commit will be something other than zero.
1412 */
1413 if (cpu_buffer->tail_page == cpu_buffer->commit_page &&
1414 rb_page_write(cpu_buffer->tail_page) ==
1415 rb_commit_index(cpu_buffer)) {
1416
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001417 delta = ts - cpu_buffer->write_stamp;
1418
Steven Rostedtbf41a152008-10-04 02:00:59 -04001419 /* make sure this delta is calculated here */
1420 barrier();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001421
Steven Rostedtbf41a152008-10-04 02:00:59 -04001422 /* Did the write stamp get updated already? */
1423 if (unlikely(ts < cpu_buffer->write_stamp))
Steven Rostedt4143c5c2008-11-10 21:46:01 -05001424 delta = 0;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001425
1426 if (test_time_stamp(delta)) {
1427
1428 commit = rb_add_time_stamp(cpu_buffer, &ts, &delta);
1429
1430 if (commit == -EBUSY)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001431 return NULL;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001432
1433 if (commit == -EAGAIN)
1434 goto again;
1435
1436 RB_WARN_ON(cpu_buffer, commit < 0);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001437 }
Steven Rostedtbf41a152008-10-04 02:00:59 -04001438 } else
1439 /* Non commits have zero deltas */
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001440 delta = 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001441
1442 event = __rb_reserve_next(cpu_buffer, type, length, &ts);
Steven Rostedtbf41a152008-10-04 02:00:59 -04001443 if (PTR_ERR(event) == -EAGAIN)
1444 goto again;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001445
Steven Rostedtbf41a152008-10-04 02:00:59 -04001446 if (!event) {
1447 if (unlikely(commit))
1448 /*
1449 * Ouch! We needed a timestamp and it was commited. But
1450 * we didn't get our event reserved.
1451 */
1452 rb_set_commit_to_write(cpu_buffer);
1453 return NULL;
1454 }
1455
1456 /*
1457 * If the timestamp was commited, make the commit our entry
1458 * now so that we will update it when needed.
1459 */
1460 if (commit)
1461 rb_set_commit_event(cpu_buffer, event);
1462 else if (!rb_is_commit(cpu_buffer, event))
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001463 delta = 0;
1464
1465 event->time_delta = delta;
1466
1467 return event;
1468}
1469
Steven Rostedtaa18efb2009-04-20 16:16:11 -04001470#define TRACE_RECURSIVE_DEPTH 16
Steven Rostedt261842b2009-04-16 21:41:52 -04001471
1472static int trace_recursive_lock(void)
1473{
Steven Rostedtaa18efb2009-04-20 16:16:11 -04001474 current->trace_recursion++;
Steven Rostedt261842b2009-04-16 21:41:52 -04001475
Steven Rostedtaa18efb2009-04-20 16:16:11 -04001476 if (likely(current->trace_recursion < TRACE_RECURSIVE_DEPTH))
1477 return 0;
Steven Rostedt261842b2009-04-16 21:41:52 -04001478
Steven Rostedtaa18efb2009-04-20 16:16:11 -04001479 /* Disable all tracing before we do anything else */
1480 tracing_off_permanent();
Frederic Weisbeckere057a5e2009-04-19 23:38:12 +02001481
Steven Rostedt7d7d2b82009-04-27 12:37:49 -04001482 printk_once(KERN_WARNING "Tracing recursion: depth[%ld]:"
Steven Rostedtaa18efb2009-04-20 16:16:11 -04001483 "HC[%lu]:SC[%lu]:NMI[%lu]\n",
1484 current->trace_recursion,
1485 hardirq_count() >> HARDIRQ_SHIFT,
1486 softirq_count() >> SOFTIRQ_SHIFT,
1487 in_nmi());
Frederic Weisbeckere057a5e2009-04-19 23:38:12 +02001488
Steven Rostedtaa18efb2009-04-20 16:16:11 -04001489 WARN_ON_ONCE(1);
1490 return -1;
Steven Rostedt261842b2009-04-16 21:41:52 -04001491}
1492
1493static void trace_recursive_unlock(void)
1494{
Steven Rostedtaa18efb2009-04-20 16:16:11 -04001495 WARN_ON_ONCE(!current->trace_recursion);
Steven Rostedt261842b2009-04-16 21:41:52 -04001496
Steven Rostedtaa18efb2009-04-20 16:16:11 -04001497 current->trace_recursion--;
Steven Rostedt261842b2009-04-16 21:41:52 -04001498}
1499
Steven Rostedtbf41a152008-10-04 02:00:59 -04001500static DEFINE_PER_CPU(int, rb_need_resched);
1501
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001502/**
1503 * ring_buffer_lock_reserve - reserve a part of the buffer
1504 * @buffer: the ring buffer to reserve from
1505 * @length: the length of the data to reserve (excluding event header)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001506 *
1507 * Returns a reseverd event on the ring buffer to copy directly to.
1508 * The user of this interface will need to get the body to write into
1509 * and can use the ring_buffer_event_data() interface.
1510 *
1511 * The length is the length of the data needed, not the event length
1512 * which also includes the event header.
1513 *
1514 * Must be paired with ring_buffer_unlock_commit, unless NULL is returned.
1515 * If NULL is returned, then nothing has been allocated or locked.
1516 */
1517struct ring_buffer_event *
Arnaldo Carvalho de Melo0a987752009-02-05 16:12:56 -02001518ring_buffer_lock_reserve(struct ring_buffer *buffer, unsigned long length)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001519{
1520 struct ring_buffer_per_cpu *cpu_buffer;
1521 struct ring_buffer_event *event;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001522 int cpu, resched;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001523
Steven Rostedt033601a2008-11-21 12:41:55 -05001524 if (ring_buffer_flags != RB_BUFFERS_ON)
Steven Rostedta3583242008-11-11 15:01:42 -05001525 return NULL;
1526
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001527 if (atomic_read(&buffer->record_disabled))
1528 return NULL;
1529
Steven Rostedtbf41a152008-10-04 02:00:59 -04001530 /* If we are tracing schedule, we don't want to recurse */
Steven Rostedt182e9f52008-11-03 23:15:56 -05001531 resched = ftrace_preempt_disable();
Steven Rostedtbf41a152008-10-04 02:00:59 -04001532
Steven Rostedt261842b2009-04-16 21:41:52 -04001533 if (trace_recursive_lock())
1534 goto out_nocheck;
1535
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001536 cpu = raw_smp_processor_id();
1537
Rusty Russell9e01c1b2009-01-01 10:12:22 +10301538 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedtd7690412008-10-01 00:29:53 -04001539 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001540
1541 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001542
1543 if (atomic_read(&cpu_buffer->record_disabled))
Steven Rostedtd7690412008-10-01 00:29:53 -04001544 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001545
1546 length = rb_calculate_event_length(length);
1547 if (length > BUF_PAGE_SIZE)
Steven Rostedtbf41a152008-10-04 02:00:59 -04001548 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001549
Lai Jiangshan334d4162009-04-24 11:27:05 +08001550 event = rb_reserve_next_event(cpu_buffer, 0, length);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001551 if (!event)
Steven Rostedtd7690412008-10-01 00:29:53 -04001552 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001553
Steven Rostedtbf41a152008-10-04 02:00:59 -04001554 /*
1555 * Need to store resched state on this cpu.
1556 * Only the first needs to.
1557 */
1558
1559 if (preempt_count() == 1)
1560 per_cpu(rb_need_resched, cpu) = resched;
1561
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001562 return event;
1563
Steven Rostedtd7690412008-10-01 00:29:53 -04001564 out:
Steven Rostedt261842b2009-04-16 21:41:52 -04001565 trace_recursive_unlock();
1566
1567 out_nocheck:
Steven Rostedt182e9f52008-11-03 23:15:56 -05001568 ftrace_preempt_enable(resched);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001569 return NULL;
1570}
Robert Richterc4f50182008-12-11 16:49:22 +01001571EXPORT_SYMBOL_GPL(ring_buffer_lock_reserve);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001572
1573static void rb_commit(struct ring_buffer_per_cpu *cpu_buffer,
1574 struct ring_buffer_event *event)
1575{
Steven Rostedte4906ef2009-04-30 20:49:44 -04001576 local_inc(&cpu_buffer->entries);
Steven Rostedtbf41a152008-10-04 02:00:59 -04001577
1578 /* Only process further if we own the commit */
1579 if (!rb_is_commit(cpu_buffer, event))
1580 return;
1581
1582 cpu_buffer->write_stamp += event->time_delta;
1583
1584 rb_set_commit_to_write(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001585}
1586
1587/**
1588 * ring_buffer_unlock_commit - commit a reserved
1589 * @buffer: The buffer to commit to
1590 * @event: The event pointer to commit.
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001591 *
1592 * This commits the data to the ring buffer, and releases any locks held.
1593 *
1594 * Must be paired with ring_buffer_lock_reserve.
1595 */
1596int ring_buffer_unlock_commit(struct ring_buffer *buffer,
Arnaldo Carvalho de Melo0a987752009-02-05 16:12:56 -02001597 struct ring_buffer_event *event)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001598{
1599 struct ring_buffer_per_cpu *cpu_buffer;
1600 int cpu = raw_smp_processor_id();
1601
1602 cpu_buffer = buffer->buffers[cpu];
1603
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001604 rb_commit(cpu_buffer, event);
1605
Steven Rostedt261842b2009-04-16 21:41:52 -04001606 trace_recursive_unlock();
1607
Steven Rostedtbf41a152008-10-04 02:00:59 -04001608 /*
1609 * Only the last preempt count needs to restore preemption.
1610 */
Steven Rostedt182e9f52008-11-03 23:15:56 -05001611 if (preempt_count() == 1)
1612 ftrace_preempt_enable(per_cpu(rb_need_resched, cpu));
1613 else
Steven Rostedtbf41a152008-10-04 02:00:59 -04001614 preempt_enable_no_resched_notrace();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001615
1616 return 0;
1617}
Robert Richterc4f50182008-12-11 16:49:22 +01001618EXPORT_SYMBOL_GPL(ring_buffer_unlock_commit);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001619
Frederic Weisbeckerf3b9aae2009-04-19 23:39:33 +02001620static inline void rb_event_discard(struct ring_buffer_event *event)
1621{
Lai Jiangshan334d4162009-04-24 11:27:05 +08001622 /* array[0] holds the actual length for the discarded event */
1623 event->array[0] = rb_event_data_length(event) - RB_EVNT_HDR_SIZE;
1624 event->type_len = RINGBUF_TYPE_PADDING;
Frederic Weisbeckerf3b9aae2009-04-19 23:39:33 +02001625 /* time delta must be non zero */
1626 if (!event->time_delta)
1627 event->time_delta = 1;
1628}
1629
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001630/**
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04001631 * ring_buffer_event_discard - discard any event in the ring buffer
1632 * @event: the event to discard
1633 *
1634 * Sometimes a event that is in the ring buffer needs to be ignored.
1635 * This function lets the user discard an event in the ring buffer
1636 * and then that event will not be read later.
1637 *
1638 * Note, it is up to the user to be careful with this, and protect
1639 * against races. If the user discards an event that has been consumed
1640 * it is possible that it could corrupt the ring buffer.
1641 */
1642void ring_buffer_event_discard(struct ring_buffer_event *event)
1643{
Frederic Weisbeckerf3b9aae2009-04-19 23:39:33 +02001644 rb_event_discard(event);
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04001645}
1646EXPORT_SYMBOL_GPL(ring_buffer_event_discard);
1647
1648/**
1649 * ring_buffer_commit_discard - discard an event that has not been committed
1650 * @buffer: the ring buffer
1651 * @event: non committed event to discard
1652 *
1653 * This is similar to ring_buffer_event_discard but must only be
1654 * performed on an event that has not been committed yet. The difference
1655 * is that this will also try to free the event from the ring buffer
1656 * if another event has not been added behind it.
1657 *
1658 * If another event has been added behind it, it will set the event
1659 * up as discarded, and perform the commit.
1660 *
1661 * If this function is called, do not call ring_buffer_unlock_commit on
1662 * the event.
1663 */
1664void ring_buffer_discard_commit(struct ring_buffer *buffer,
1665 struct ring_buffer_event *event)
1666{
1667 struct ring_buffer_per_cpu *cpu_buffer;
1668 unsigned long new_index, old_index;
1669 struct buffer_page *bpage;
1670 unsigned long index;
1671 unsigned long addr;
1672 int cpu;
1673
1674 /* The event is discarded regardless */
Frederic Weisbeckerf3b9aae2009-04-19 23:39:33 +02001675 rb_event_discard(event);
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04001676
1677 /*
1678 * This must only be called if the event has not been
1679 * committed yet. Thus we can assume that preemption
1680 * is still disabled.
1681 */
1682 RB_WARN_ON(buffer, !preempt_count());
1683
1684 cpu = smp_processor_id();
1685 cpu_buffer = buffer->buffers[cpu];
1686
1687 new_index = rb_event_index(event);
1688 old_index = new_index + rb_event_length(event);
1689 addr = (unsigned long)event;
1690 addr &= PAGE_MASK;
1691
1692 bpage = cpu_buffer->tail_page;
1693
1694 if (bpage == (void *)addr && rb_page_write(bpage) == old_index) {
1695 /*
1696 * This is on the tail page. It is possible that
1697 * a write could come in and move the tail page
1698 * and write to the next page. That is fine
1699 * because we just shorten what is on this page.
1700 */
1701 index = local_cmpxchg(&bpage->write, old_index, new_index);
1702 if (index == old_index)
1703 goto out;
1704 }
1705
1706 /*
1707 * The commit is still visible by the reader, so we
1708 * must increment entries.
1709 */
Steven Rostedte4906ef2009-04-30 20:49:44 -04001710 local_inc(&cpu_buffer->entries);
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04001711 out:
1712 /*
1713 * If a write came in and pushed the tail page
1714 * we still need to update the commit pointer
1715 * if we were the commit.
1716 */
1717 if (rb_is_commit(cpu_buffer, event))
1718 rb_set_commit_to_write(cpu_buffer);
1719
Frederic Weisbeckerf3b9aae2009-04-19 23:39:33 +02001720 trace_recursive_unlock();
1721
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04001722 /*
1723 * Only the last preempt count needs to restore preemption.
1724 */
1725 if (preempt_count() == 1)
1726 ftrace_preempt_enable(per_cpu(rb_need_resched, cpu));
1727 else
1728 preempt_enable_no_resched_notrace();
1729
1730}
1731EXPORT_SYMBOL_GPL(ring_buffer_discard_commit);
1732
1733/**
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001734 * ring_buffer_write - write data to the buffer without reserving
1735 * @buffer: The ring buffer to write to.
1736 * @length: The length of the data being written (excluding the event header)
1737 * @data: The data to write to the buffer.
1738 *
1739 * This is like ring_buffer_lock_reserve and ring_buffer_unlock_commit as
1740 * one function. If you already have the data to write to the buffer, it
1741 * may be easier to simply call this function.
1742 *
1743 * Note, like ring_buffer_lock_reserve, the length is the length of the data
1744 * and not the length of the event which would hold the header.
1745 */
1746int ring_buffer_write(struct ring_buffer *buffer,
1747 unsigned long length,
1748 void *data)
1749{
1750 struct ring_buffer_per_cpu *cpu_buffer;
1751 struct ring_buffer_event *event;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001752 unsigned long event_length;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001753 void *body;
1754 int ret = -EBUSY;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001755 int cpu, resched;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001756
Steven Rostedt033601a2008-11-21 12:41:55 -05001757 if (ring_buffer_flags != RB_BUFFERS_ON)
Steven Rostedta3583242008-11-11 15:01:42 -05001758 return -EBUSY;
1759
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001760 if (atomic_read(&buffer->record_disabled))
1761 return -EBUSY;
1762
Steven Rostedt182e9f52008-11-03 23:15:56 -05001763 resched = ftrace_preempt_disable();
Steven Rostedtbf41a152008-10-04 02:00:59 -04001764
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001765 cpu = raw_smp_processor_id();
1766
Rusty Russell9e01c1b2009-01-01 10:12:22 +10301767 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedtd7690412008-10-01 00:29:53 -04001768 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001769
1770 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001771
1772 if (atomic_read(&cpu_buffer->record_disabled))
1773 goto out;
1774
1775 event_length = rb_calculate_event_length(length);
Lai Jiangshan334d4162009-04-24 11:27:05 +08001776 event = rb_reserve_next_event(cpu_buffer, 0, event_length);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001777 if (!event)
1778 goto out;
1779
1780 body = rb_event_data(event);
1781
1782 memcpy(body, data, length);
1783
1784 rb_commit(cpu_buffer, event);
1785
1786 ret = 0;
1787 out:
Steven Rostedt182e9f52008-11-03 23:15:56 -05001788 ftrace_preempt_enable(resched);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001789
1790 return ret;
1791}
Robert Richterc4f50182008-12-11 16:49:22 +01001792EXPORT_SYMBOL_GPL(ring_buffer_write);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001793
Andrew Morton34a148b2009-01-09 12:27:09 -08001794static int rb_per_cpu_empty(struct ring_buffer_per_cpu *cpu_buffer)
Steven Rostedtbf41a152008-10-04 02:00:59 -04001795{
1796 struct buffer_page *reader = cpu_buffer->reader_page;
1797 struct buffer_page *head = cpu_buffer->head_page;
1798 struct buffer_page *commit = cpu_buffer->commit_page;
1799
1800 return reader->read == rb_page_commit(reader) &&
1801 (commit == reader ||
1802 (commit == head &&
1803 head->read == rb_page_commit(commit)));
1804}
1805
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001806/**
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001807 * ring_buffer_record_disable - stop all writes into the buffer
1808 * @buffer: The ring buffer to stop writes to.
1809 *
1810 * This prevents all writes to the buffer. Any attempt to write
1811 * to the buffer after this will fail and return NULL.
1812 *
1813 * The caller should call synchronize_sched() after this.
1814 */
1815void ring_buffer_record_disable(struct ring_buffer *buffer)
1816{
1817 atomic_inc(&buffer->record_disabled);
1818}
Robert Richterc4f50182008-12-11 16:49:22 +01001819EXPORT_SYMBOL_GPL(ring_buffer_record_disable);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001820
1821/**
1822 * ring_buffer_record_enable - enable writes to the buffer
1823 * @buffer: The ring buffer to enable writes
1824 *
1825 * Note, multiple disables will need the same number of enables
1826 * to truely enable the writing (much like preempt_disable).
1827 */
1828void ring_buffer_record_enable(struct ring_buffer *buffer)
1829{
1830 atomic_dec(&buffer->record_disabled);
1831}
Robert Richterc4f50182008-12-11 16:49:22 +01001832EXPORT_SYMBOL_GPL(ring_buffer_record_enable);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001833
1834/**
1835 * ring_buffer_record_disable_cpu - stop all writes into the cpu_buffer
1836 * @buffer: The ring buffer to stop writes to.
1837 * @cpu: The CPU buffer to stop
1838 *
1839 * This prevents all writes to the buffer. Any attempt to write
1840 * to the buffer after this will fail and return NULL.
1841 *
1842 * The caller should call synchronize_sched() after this.
1843 */
1844void ring_buffer_record_disable_cpu(struct ring_buffer *buffer, int cpu)
1845{
1846 struct ring_buffer_per_cpu *cpu_buffer;
1847
Rusty Russell9e01c1b2009-01-01 10:12:22 +10301848 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04001849 return;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001850
1851 cpu_buffer = buffer->buffers[cpu];
1852 atomic_inc(&cpu_buffer->record_disabled);
1853}
Robert Richterc4f50182008-12-11 16:49:22 +01001854EXPORT_SYMBOL_GPL(ring_buffer_record_disable_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001855
1856/**
1857 * ring_buffer_record_enable_cpu - enable writes to the buffer
1858 * @buffer: The ring buffer to enable writes
1859 * @cpu: The CPU to enable.
1860 *
1861 * Note, multiple disables will need the same number of enables
1862 * to truely enable the writing (much like preempt_disable).
1863 */
1864void ring_buffer_record_enable_cpu(struct ring_buffer *buffer, int cpu)
1865{
1866 struct ring_buffer_per_cpu *cpu_buffer;
1867
Rusty Russell9e01c1b2009-01-01 10:12:22 +10301868 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04001869 return;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001870
1871 cpu_buffer = buffer->buffers[cpu];
1872 atomic_dec(&cpu_buffer->record_disabled);
1873}
Robert Richterc4f50182008-12-11 16:49:22 +01001874EXPORT_SYMBOL_GPL(ring_buffer_record_enable_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001875
1876/**
1877 * ring_buffer_entries_cpu - get the number of entries in a cpu buffer
1878 * @buffer: The ring buffer
1879 * @cpu: The per CPU buffer to get the entries from.
1880 */
1881unsigned long ring_buffer_entries_cpu(struct ring_buffer *buffer, int cpu)
1882{
1883 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedt8aabee52009-03-12 13:13:49 -04001884 unsigned long ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001885
Rusty Russell9e01c1b2009-01-01 10:12:22 +10301886 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04001887 return 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001888
1889 cpu_buffer = buffer->buffers[cpu];
Steven Rostedte4906ef2009-04-30 20:49:44 -04001890 ret = (local_read(&cpu_buffer->entries) - cpu_buffer->overrun)
1891 - cpu_buffer->read;
Steven Rostedt554f7862009-03-11 22:00:13 -04001892
1893 return ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001894}
Robert Richterc4f50182008-12-11 16:49:22 +01001895EXPORT_SYMBOL_GPL(ring_buffer_entries_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001896
1897/**
1898 * ring_buffer_overrun_cpu - get the number of overruns in a cpu_buffer
1899 * @buffer: The ring buffer
1900 * @cpu: The per CPU buffer to get the number of overruns from
1901 */
1902unsigned long ring_buffer_overrun_cpu(struct ring_buffer *buffer, int cpu)
1903{
1904 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedt8aabee52009-03-12 13:13:49 -04001905 unsigned long ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001906
Rusty Russell9e01c1b2009-01-01 10:12:22 +10301907 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04001908 return 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001909
1910 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt554f7862009-03-11 22:00:13 -04001911 ret = cpu_buffer->overrun;
Steven Rostedt554f7862009-03-11 22:00:13 -04001912
1913 return ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001914}
Robert Richterc4f50182008-12-11 16:49:22 +01001915EXPORT_SYMBOL_GPL(ring_buffer_overrun_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001916
1917/**
Steven Rostedtf0d2c682009-04-29 13:43:37 -04001918 * ring_buffer_nmi_dropped_cpu - get the number of nmis that were dropped
1919 * @buffer: The ring buffer
1920 * @cpu: The per CPU buffer to get the number of overruns from
1921 */
1922unsigned long ring_buffer_nmi_dropped_cpu(struct ring_buffer *buffer, int cpu)
1923{
1924 struct ring_buffer_per_cpu *cpu_buffer;
1925 unsigned long ret;
1926
1927 if (!cpumask_test_cpu(cpu, buffer->cpumask))
1928 return 0;
1929
1930 cpu_buffer = buffer->buffers[cpu];
1931 ret = cpu_buffer->nmi_dropped;
1932
1933 return ret;
1934}
1935EXPORT_SYMBOL_GPL(ring_buffer_nmi_dropped_cpu);
1936
1937/**
1938 * ring_buffer_commit_overrun_cpu - get the number of overruns caused by commits
1939 * @buffer: The ring buffer
1940 * @cpu: The per CPU buffer to get the number of overruns from
1941 */
1942unsigned long
1943ring_buffer_commit_overrun_cpu(struct ring_buffer *buffer, int cpu)
1944{
1945 struct ring_buffer_per_cpu *cpu_buffer;
1946 unsigned long ret;
1947
1948 if (!cpumask_test_cpu(cpu, buffer->cpumask))
1949 return 0;
1950
1951 cpu_buffer = buffer->buffers[cpu];
1952 ret = cpu_buffer->commit_overrun;
1953
1954 return ret;
1955}
1956EXPORT_SYMBOL_GPL(ring_buffer_commit_overrun_cpu);
1957
1958/**
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001959 * ring_buffer_entries - get the number of entries in a buffer
1960 * @buffer: The ring buffer
1961 *
1962 * Returns the total number of entries in the ring buffer
1963 * (all CPU entries)
1964 */
1965unsigned long ring_buffer_entries(struct ring_buffer *buffer)
1966{
1967 struct ring_buffer_per_cpu *cpu_buffer;
1968 unsigned long entries = 0;
1969 int cpu;
1970
1971 /* if you care about this being correct, lock the buffer */
1972 for_each_buffer_cpu(buffer, cpu) {
1973 cpu_buffer = buffer->buffers[cpu];
Steven Rostedte4906ef2009-04-30 20:49:44 -04001974 entries += (local_read(&cpu_buffer->entries) -
1975 cpu_buffer->overrun) - cpu_buffer->read;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001976 }
1977
1978 return entries;
1979}
Robert Richterc4f50182008-12-11 16:49:22 +01001980EXPORT_SYMBOL_GPL(ring_buffer_entries);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001981
1982/**
1983 * ring_buffer_overrun_cpu - get the number of overruns in buffer
1984 * @buffer: The ring buffer
1985 *
1986 * Returns the total number of overruns in the ring buffer
1987 * (all CPU entries)
1988 */
1989unsigned long ring_buffer_overruns(struct ring_buffer *buffer)
1990{
1991 struct ring_buffer_per_cpu *cpu_buffer;
1992 unsigned long overruns = 0;
1993 int cpu;
1994
1995 /* if you care about this being correct, lock the buffer */
1996 for_each_buffer_cpu(buffer, cpu) {
1997 cpu_buffer = buffer->buffers[cpu];
1998 overruns += cpu_buffer->overrun;
1999 }
2000
2001 return overruns;
2002}
Robert Richterc4f50182008-12-11 16:49:22 +01002003EXPORT_SYMBOL_GPL(ring_buffer_overruns);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002004
Steven Rostedt642edba2008-11-12 00:01:26 -05002005static void rb_iter_reset(struct ring_buffer_iter *iter)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002006{
2007 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
2008
Steven Rostedtd7690412008-10-01 00:29:53 -04002009 /* Iterator usage is expected to have record disabled */
2010 if (list_empty(&cpu_buffer->reader_page->list)) {
2011 iter->head_page = cpu_buffer->head_page;
Steven Rostedt6f807ac2008-10-04 02:00:58 -04002012 iter->head = cpu_buffer->head_page->read;
Steven Rostedtd7690412008-10-01 00:29:53 -04002013 } else {
2014 iter->head_page = cpu_buffer->reader_page;
Steven Rostedt6f807ac2008-10-04 02:00:58 -04002015 iter->head = cpu_buffer->reader_page->read;
Steven Rostedtd7690412008-10-01 00:29:53 -04002016 }
2017 if (iter->head)
2018 iter->read_stamp = cpu_buffer->read_stamp;
2019 else
Steven Rostedtabc9b562008-12-02 15:34:06 -05002020 iter->read_stamp = iter->head_page->page->time_stamp;
Steven Rostedt642edba2008-11-12 00:01:26 -05002021}
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002022
Steven Rostedt642edba2008-11-12 00:01:26 -05002023/**
2024 * ring_buffer_iter_reset - reset an iterator
2025 * @iter: The iterator to reset
2026 *
2027 * Resets the iterator, so that it will start from the beginning
2028 * again.
2029 */
2030void ring_buffer_iter_reset(struct ring_buffer_iter *iter)
2031{
Steven Rostedt554f7862009-03-11 22:00:13 -04002032 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedt642edba2008-11-12 00:01:26 -05002033 unsigned long flags;
2034
Steven Rostedt554f7862009-03-11 22:00:13 -04002035 if (!iter)
2036 return;
2037
2038 cpu_buffer = iter->cpu_buffer;
2039
Steven Rostedt642edba2008-11-12 00:01:26 -05002040 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
2041 rb_iter_reset(iter);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002042 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002043}
Robert Richterc4f50182008-12-11 16:49:22 +01002044EXPORT_SYMBOL_GPL(ring_buffer_iter_reset);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002045
2046/**
2047 * ring_buffer_iter_empty - check if an iterator has no more to read
2048 * @iter: The iterator to check
2049 */
2050int ring_buffer_iter_empty(struct ring_buffer_iter *iter)
2051{
2052 struct ring_buffer_per_cpu *cpu_buffer;
2053
2054 cpu_buffer = iter->cpu_buffer;
2055
Steven Rostedtbf41a152008-10-04 02:00:59 -04002056 return iter->head_page == cpu_buffer->commit_page &&
2057 iter->head == rb_commit_index(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002058}
Robert Richterc4f50182008-12-11 16:49:22 +01002059EXPORT_SYMBOL_GPL(ring_buffer_iter_empty);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002060
2061static void
2062rb_update_read_stamp(struct ring_buffer_per_cpu *cpu_buffer,
2063 struct ring_buffer_event *event)
2064{
2065 u64 delta;
2066
Lai Jiangshan334d4162009-04-24 11:27:05 +08002067 switch (event->type_len) {
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002068 case RINGBUF_TYPE_PADDING:
2069 return;
2070
2071 case RINGBUF_TYPE_TIME_EXTEND:
2072 delta = event->array[0];
2073 delta <<= TS_SHIFT;
2074 delta += event->time_delta;
2075 cpu_buffer->read_stamp += delta;
2076 return;
2077
2078 case RINGBUF_TYPE_TIME_STAMP:
2079 /* FIXME: not implemented */
2080 return;
2081
2082 case RINGBUF_TYPE_DATA:
2083 cpu_buffer->read_stamp += event->time_delta;
2084 return;
2085
2086 default:
2087 BUG();
2088 }
2089 return;
2090}
2091
2092static void
2093rb_update_iter_read_stamp(struct ring_buffer_iter *iter,
2094 struct ring_buffer_event *event)
2095{
2096 u64 delta;
2097
Lai Jiangshan334d4162009-04-24 11:27:05 +08002098 switch (event->type_len) {
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002099 case RINGBUF_TYPE_PADDING:
2100 return;
2101
2102 case RINGBUF_TYPE_TIME_EXTEND:
2103 delta = event->array[0];
2104 delta <<= TS_SHIFT;
2105 delta += event->time_delta;
2106 iter->read_stamp += delta;
2107 return;
2108
2109 case RINGBUF_TYPE_TIME_STAMP:
2110 /* FIXME: not implemented */
2111 return;
2112
2113 case RINGBUF_TYPE_DATA:
2114 iter->read_stamp += event->time_delta;
2115 return;
2116
2117 default:
2118 BUG();
2119 }
2120 return;
2121}
2122
Steven Rostedtd7690412008-10-01 00:29:53 -04002123static struct buffer_page *
2124rb_get_reader_page(struct ring_buffer_per_cpu *cpu_buffer)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002125{
Steven Rostedtd7690412008-10-01 00:29:53 -04002126 struct buffer_page *reader = NULL;
2127 unsigned long flags;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002128 int nr_loops = 0;
Steven Rostedtd7690412008-10-01 00:29:53 -04002129
Steven Rostedt3e03fb72008-11-06 00:09:43 -05002130 local_irq_save(flags);
2131 __raw_spin_lock(&cpu_buffer->lock);
Steven Rostedtd7690412008-10-01 00:29:53 -04002132
2133 again:
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002134 /*
2135 * This should normally only loop twice. But because the
2136 * start of the reader inserts an empty page, it causes
2137 * a case where we will loop three times. There should be no
2138 * reason to loop four times (that I know of).
2139 */
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05002140 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 3)) {
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002141 reader = NULL;
2142 goto out;
2143 }
2144
Steven Rostedtd7690412008-10-01 00:29:53 -04002145 reader = cpu_buffer->reader_page;
2146
2147 /* If there's more to read, return this page */
Steven Rostedtbf41a152008-10-04 02:00:59 -04002148 if (cpu_buffer->reader_page->read < rb_page_size(reader))
Steven Rostedtd7690412008-10-01 00:29:53 -04002149 goto out;
2150
2151 /* Never should we have an index greater than the size */
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05002152 if (RB_WARN_ON(cpu_buffer,
2153 cpu_buffer->reader_page->read > rb_page_size(reader)))
2154 goto out;
Steven Rostedtd7690412008-10-01 00:29:53 -04002155
2156 /* check if we caught up to the tail */
2157 reader = NULL;
Steven Rostedtbf41a152008-10-04 02:00:59 -04002158 if (cpu_buffer->commit_page == cpu_buffer->reader_page)
Steven Rostedtd7690412008-10-01 00:29:53 -04002159 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002160
2161 /*
Steven Rostedtd7690412008-10-01 00:29:53 -04002162 * Splice the empty reader page into the list around the head.
2163 * Reset the reader page to size zero.
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002164 */
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002165
Steven Rostedtd7690412008-10-01 00:29:53 -04002166 reader = cpu_buffer->head_page;
2167 cpu_buffer->reader_page->list.next = reader->list.next;
2168 cpu_buffer->reader_page->list.prev = reader->list.prev;
Steven Rostedtbf41a152008-10-04 02:00:59 -04002169
2170 local_set(&cpu_buffer->reader_page->write, 0);
Steven Rostedt778c55d2009-05-01 18:44:45 -04002171 local_set(&cpu_buffer->reader_page->entries, 0);
Steven Rostedtabc9b562008-12-02 15:34:06 -05002172 local_set(&cpu_buffer->reader_page->page->commit, 0);
Steven Rostedtd7690412008-10-01 00:29:53 -04002173
2174 /* Make the reader page now replace the head */
2175 reader->list.prev->next = &cpu_buffer->reader_page->list;
2176 reader->list.next->prev = &cpu_buffer->reader_page->list;
2177
2178 /*
2179 * If the tail is on the reader, then we must set the head
2180 * to the inserted page, otherwise we set it one before.
2181 */
2182 cpu_buffer->head_page = cpu_buffer->reader_page;
2183
Steven Rostedtbf41a152008-10-04 02:00:59 -04002184 if (cpu_buffer->commit_page != reader)
Steven Rostedtd7690412008-10-01 00:29:53 -04002185 rb_inc_page(cpu_buffer, &cpu_buffer->head_page);
2186
2187 /* Finally update the reader page to the new head */
2188 cpu_buffer->reader_page = reader;
2189 rb_reset_reader_page(cpu_buffer);
2190
2191 goto again;
2192
2193 out:
Steven Rostedt3e03fb72008-11-06 00:09:43 -05002194 __raw_spin_unlock(&cpu_buffer->lock);
2195 local_irq_restore(flags);
Steven Rostedtd7690412008-10-01 00:29:53 -04002196
2197 return reader;
2198}
2199
2200static void rb_advance_reader(struct ring_buffer_per_cpu *cpu_buffer)
2201{
2202 struct ring_buffer_event *event;
2203 struct buffer_page *reader;
2204 unsigned length;
2205
2206 reader = rb_get_reader_page(cpu_buffer);
2207
2208 /* This function should not be called when buffer is empty */
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05002209 if (RB_WARN_ON(cpu_buffer, !reader))
2210 return;
Steven Rostedtd7690412008-10-01 00:29:53 -04002211
2212 event = rb_reader_event(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002213
Lai Jiangshan334d4162009-04-24 11:27:05 +08002214 if (event->type_len <= RINGBUF_TYPE_DATA_TYPE_LEN_MAX
2215 || rb_discarded_event(event))
Steven Rostedte4906ef2009-04-30 20:49:44 -04002216 cpu_buffer->read++;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002217
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002218 rb_update_read_stamp(cpu_buffer, event);
2219
Steven Rostedtd7690412008-10-01 00:29:53 -04002220 length = rb_event_length(event);
Steven Rostedt6f807ac2008-10-04 02:00:58 -04002221 cpu_buffer->reader_page->read += length;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002222}
2223
2224static void rb_advance_iter(struct ring_buffer_iter *iter)
2225{
2226 struct ring_buffer *buffer;
2227 struct ring_buffer_per_cpu *cpu_buffer;
2228 struct ring_buffer_event *event;
2229 unsigned length;
2230
2231 cpu_buffer = iter->cpu_buffer;
2232 buffer = cpu_buffer->buffer;
2233
2234 /*
2235 * Check if we are at the end of the buffer.
2236 */
Steven Rostedtbf41a152008-10-04 02:00:59 -04002237 if (iter->head >= rb_page_size(iter->head_page)) {
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05002238 if (RB_WARN_ON(buffer,
2239 iter->head_page == cpu_buffer->commit_page))
2240 return;
Steven Rostedtd7690412008-10-01 00:29:53 -04002241 rb_inc_iter(iter);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002242 return;
2243 }
2244
2245 event = rb_iter_head_event(iter);
2246
2247 length = rb_event_length(event);
2248
2249 /*
2250 * This should not be called to advance the header if we are
2251 * at the tail of the buffer.
2252 */
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05002253 if (RB_WARN_ON(cpu_buffer,
Steven Rostedtf536aaf2008-11-10 23:07:30 -05002254 (iter->head_page == cpu_buffer->commit_page) &&
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05002255 (iter->head + length > rb_commit_index(cpu_buffer))))
2256 return;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002257
2258 rb_update_iter_read_stamp(iter, event);
2259
2260 iter->head += length;
2261
2262 /* check for end of page padding */
Steven Rostedtbf41a152008-10-04 02:00:59 -04002263 if ((iter->head >= rb_page_size(iter->head_page)) &&
2264 (iter->head_page != cpu_buffer->commit_page))
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002265 rb_advance_iter(iter);
2266}
2267
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002268static struct ring_buffer_event *
2269rb_buffer_peek(struct ring_buffer *buffer, int cpu, u64 *ts)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002270{
2271 struct ring_buffer_per_cpu *cpu_buffer;
2272 struct ring_buffer_event *event;
Steven Rostedtd7690412008-10-01 00:29:53 -04002273 struct buffer_page *reader;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002274 int nr_loops = 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002275
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002276 cpu_buffer = buffer->buffers[cpu];
2277
2278 again:
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002279 /*
2280 * We repeat when a timestamp is encountered. It is possible
2281 * to get multiple timestamps from an interrupt entering just
2282 * as one timestamp is about to be written. The max times
2283 * that this can happen is the number of nested interrupts we
2284 * can have. Nesting 10 deep of interrupts is clearly
2285 * an anomaly.
2286 */
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05002287 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 10))
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002288 return NULL;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002289
Steven Rostedtd7690412008-10-01 00:29:53 -04002290 reader = rb_get_reader_page(cpu_buffer);
2291 if (!reader)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002292 return NULL;
2293
Steven Rostedtd7690412008-10-01 00:29:53 -04002294 event = rb_reader_event(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002295
Lai Jiangshan334d4162009-04-24 11:27:05 +08002296 switch (event->type_len) {
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002297 case RINGBUF_TYPE_PADDING:
Tom Zanussi2d622712009-03-22 03:30:49 -05002298 if (rb_null_event(event))
2299 RB_WARN_ON(cpu_buffer, 1);
2300 /*
2301 * Because the writer could be discarding every
2302 * event it creates (which would probably be bad)
2303 * if we were to go back to "again" then we may never
2304 * catch up, and will trigger the warn on, or lock
2305 * the box. Return the padding, and we will release
2306 * the current locks, and try again.
2307 */
Steven Rostedtd7690412008-10-01 00:29:53 -04002308 rb_advance_reader(cpu_buffer);
Tom Zanussi2d622712009-03-22 03:30:49 -05002309 return event;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002310
2311 case RINGBUF_TYPE_TIME_EXTEND:
2312 /* Internal data, OK to advance */
Steven Rostedtd7690412008-10-01 00:29:53 -04002313 rb_advance_reader(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002314 goto again;
2315
2316 case RINGBUF_TYPE_TIME_STAMP:
2317 /* FIXME: not implemented */
Steven Rostedtd7690412008-10-01 00:29:53 -04002318 rb_advance_reader(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002319 goto again;
2320
2321 case RINGBUF_TYPE_DATA:
2322 if (ts) {
2323 *ts = cpu_buffer->read_stamp + event->time_delta;
Steven Rostedt37886f62009-03-17 17:22:06 -04002324 ring_buffer_normalize_time_stamp(buffer,
2325 cpu_buffer->cpu, ts);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002326 }
2327 return event;
2328
2329 default:
2330 BUG();
2331 }
2332
2333 return NULL;
2334}
Robert Richterc4f50182008-12-11 16:49:22 +01002335EXPORT_SYMBOL_GPL(ring_buffer_peek);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002336
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002337static struct ring_buffer_event *
2338rb_iter_peek(struct ring_buffer_iter *iter, u64 *ts)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002339{
2340 struct ring_buffer *buffer;
2341 struct ring_buffer_per_cpu *cpu_buffer;
2342 struct ring_buffer_event *event;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002343 int nr_loops = 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002344
2345 if (ring_buffer_iter_empty(iter))
2346 return NULL;
2347
2348 cpu_buffer = iter->cpu_buffer;
2349 buffer = cpu_buffer->buffer;
2350
2351 again:
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002352 /*
2353 * We repeat when a timestamp is encountered. It is possible
2354 * to get multiple timestamps from an interrupt entering just
2355 * as one timestamp is about to be written. The max times
2356 * that this can happen is the number of nested interrupts we
2357 * can have. Nesting 10 deep of interrupts is clearly
2358 * an anomaly.
2359 */
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05002360 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 10))
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002361 return NULL;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002362
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002363 if (rb_per_cpu_empty(cpu_buffer))
2364 return NULL;
2365
2366 event = rb_iter_head_event(iter);
2367
Lai Jiangshan334d4162009-04-24 11:27:05 +08002368 switch (event->type_len) {
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002369 case RINGBUF_TYPE_PADDING:
Tom Zanussi2d622712009-03-22 03:30:49 -05002370 if (rb_null_event(event)) {
2371 rb_inc_iter(iter);
2372 goto again;
2373 }
2374 rb_advance_iter(iter);
2375 return event;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002376
2377 case RINGBUF_TYPE_TIME_EXTEND:
2378 /* Internal data, OK to advance */
2379 rb_advance_iter(iter);
2380 goto again;
2381
2382 case RINGBUF_TYPE_TIME_STAMP:
2383 /* FIXME: not implemented */
2384 rb_advance_iter(iter);
2385 goto again;
2386
2387 case RINGBUF_TYPE_DATA:
2388 if (ts) {
2389 *ts = iter->read_stamp + event->time_delta;
Steven Rostedt37886f62009-03-17 17:22:06 -04002390 ring_buffer_normalize_time_stamp(buffer,
2391 cpu_buffer->cpu, ts);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002392 }
2393 return event;
2394
2395 default:
2396 BUG();
2397 }
2398
2399 return NULL;
2400}
Robert Richterc4f50182008-12-11 16:49:22 +01002401EXPORT_SYMBOL_GPL(ring_buffer_iter_peek);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002402
2403/**
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002404 * ring_buffer_peek - peek at the next event to be read
2405 * @buffer: The ring buffer to read
2406 * @cpu: The cpu to peak at
2407 * @ts: The timestamp counter of this event.
2408 *
2409 * This will return the event that will be read next, but does
2410 * not consume the data.
2411 */
2412struct ring_buffer_event *
2413ring_buffer_peek(struct ring_buffer *buffer, int cpu, u64 *ts)
2414{
2415 struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
Steven Rostedt8aabee52009-03-12 13:13:49 -04002416 struct ring_buffer_event *event;
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002417 unsigned long flags;
2418
Steven Rostedt554f7862009-03-11 22:00:13 -04002419 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04002420 return NULL;
Steven Rostedt554f7862009-03-11 22:00:13 -04002421
Tom Zanussi2d622712009-03-22 03:30:49 -05002422 again:
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002423 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
2424 event = rb_buffer_peek(buffer, cpu, ts);
2425 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
2426
Lai Jiangshan334d4162009-04-24 11:27:05 +08002427 if (event && event->type_len == RINGBUF_TYPE_PADDING) {
Tom Zanussi2d622712009-03-22 03:30:49 -05002428 cpu_relax();
2429 goto again;
2430 }
2431
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002432 return event;
2433}
2434
2435/**
2436 * ring_buffer_iter_peek - peek at the next event to be read
2437 * @iter: The ring buffer iterator
2438 * @ts: The timestamp counter of this event.
2439 *
2440 * This will return the event that will be read next, but does
2441 * not increment the iterator.
2442 */
2443struct ring_buffer_event *
2444ring_buffer_iter_peek(struct ring_buffer_iter *iter, u64 *ts)
2445{
2446 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
2447 struct ring_buffer_event *event;
2448 unsigned long flags;
2449
Tom Zanussi2d622712009-03-22 03:30:49 -05002450 again:
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002451 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
2452 event = rb_iter_peek(iter, ts);
2453 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
2454
Lai Jiangshan334d4162009-04-24 11:27:05 +08002455 if (event && event->type_len == RINGBUF_TYPE_PADDING) {
Tom Zanussi2d622712009-03-22 03:30:49 -05002456 cpu_relax();
2457 goto again;
2458 }
2459
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002460 return event;
2461}
2462
2463/**
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002464 * ring_buffer_consume - return an event and consume it
2465 * @buffer: The ring buffer to get the next event from
2466 *
2467 * Returns the next event in the ring buffer, and that event is consumed.
2468 * Meaning, that sequential reads will keep returning a different event,
2469 * and eventually empty the ring buffer if the producer is slower.
2470 */
2471struct ring_buffer_event *
2472ring_buffer_consume(struct ring_buffer *buffer, int cpu, u64 *ts)
2473{
Steven Rostedt554f7862009-03-11 22:00:13 -04002474 struct ring_buffer_per_cpu *cpu_buffer;
2475 struct ring_buffer_event *event = NULL;
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002476 unsigned long flags;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002477
Tom Zanussi2d622712009-03-22 03:30:49 -05002478 again:
Steven Rostedt554f7862009-03-11 22:00:13 -04002479 /* might be called in atomic */
2480 preempt_disable();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002481
Steven Rostedt554f7862009-03-11 22:00:13 -04002482 if (!cpumask_test_cpu(cpu, buffer->cpumask))
2483 goto out;
2484
2485 cpu_buffer = buffer->buffers[cpu];
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002486 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002487
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002488 event = rb_buffer_peek(buffer, cpu, ts);
2489 if (!event)
Steven Rostedt554f7862009-03-11 22:00:13 -04002490 goto out_unlock;
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002491
Steven Rostedtd7690412008-10-01 00:29:53 -04002492 rb_advance_reader(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002493
Steven Rostedt554f7862009-03-11 22:00:13 -04002494 out_unlock:
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002495 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
2496
Steven Rostedt554f7862009-03-11 22:00:13 -04002497 out:
2498 preempt_enable();
2499
Lai Jiangshan334d4162009-04-24 11:27:05 +08002500 if (event && event->type_len == RINGBUF_TYPE_PADDING) {
Tom Zanussi2d622712009-03-22 03:30:49 -05002501 cpu_relax();
2502 goto again;
2503 }
2504
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002505 return event;
2506}
Robert Richterc4f50182008-12-11 16:49:22 +01002507EXPORT_SYMBOL_GPL(ring_buffer_consume);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002508
2509/**
2510 * ring_buffer_read_start - start a non consuming read of the buffer
2511 * @buffer: The ring buffer to read from
2512 * @cpu: The cpu buffer to iterate over
2513 *
2514 * This starts up an iteration through the buffer. It also disables
2515 * the recording to the buffer until the reading is finished.
2516 * This prevents the reading from being corrupted. This is not
2517 * a consuming read, so a producer is not expected.
2518 *
2519 * Must be paired with ring_buffer_finish.
2520 */
2521struct ring_buffer_iter *
2522ring_buffer_read_start(struct ring_buffer *buffer, int cpu)
2523{
2524 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedt8aabee52009-03-12 13:13:49 -04002525 struct ring_buffer_iter *iter;
Steven Rostedtd7690412008-10-01 00:29:53 -04002526 unsigned long flags;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002527
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302528 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04002529 return NULL;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002530
2531 iter = kmalloc(sizeof(*iter), GFP_KERNEL);
2532 if (!iter)
Steven Rostedt8aabee52009-03-12 13:13:49 -04002533 return NULL;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002534
2535 cpu_buffer = buffer->buffers[cpu];
2536
2537 iter->cpu_buffer = cpu_buffer;
2538
2539 atomic_inc(&cpu_buffer->record_disabled);
2540 synchronize_sched();
2541
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002542 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
Steven Rostedt3e03fb72008-11-06 00:09:43 -05002543 __raw_spin_lock(&cpu_buffer->lock);
Steven Rostedt642edba2008-11-12 00:01:26 -05002544 rb_iter_reset(iter);
Steven Rostedt3e03fb72008-11-06 00:09:43 -05002545 __raw_spin_unlock(&cpu_buffer->lock);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002546 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002547
2548 return iter;
2549}
Robert Richterc4f50182008-12-11 16:49:22 +01002550EXPORT_SYMBOL_GPL(ring_buffer_read_start);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002551
2552/**
2553 * ring_buffer_finish - finish reading the iterator of the buffer
2554 * @iter: The iterator retrieved by ring_buffer_start
2555 *
2556 * This re-enables the recording to the buffer, and frees the
2557 * iterator.
2558 */
2559void
2560ring_buffer_read_finish(struct ring_buffer_iter *iter)
2561{
2562 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
2563
2564 atomic_dec(&cpu_buffer->record_disabled);
2565 kfree(iter);
2566}
Robert Richterc4f50182008-12-11 16:49:22 +01002567EXPORT_SYMBOL_GPL(ring_buffer_read_finish);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002568
2569/**
2570 * ring_buffer_read - read the next item in the ring buffer by the iterator
2571 * @iter: The ring buffer iterator
2572 * @ts: The time stamp of the event read.
2573 *
2574 * This reads the next event in the ring buffer and increments the iterator.
2575 */
2576struct ring_buffer_event *
2577ring_buffer_read(struct ring_buffer_iter *iter, u64 *ts)
2578{
2579 struct ring_buffer_event *event;
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002580 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
2581 unsigned long flags;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002582
Tom Zanussi2d622712009-03-22 03:30:49 -05002583 again:
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002584 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
2585 event = rb_iter_peek(iter, ts);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002586 if (!event)
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002587 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002588
2589 rb_advance_iter(iter);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002590 out:
2591 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002592
Lai Jiangshan334d4162009-04-24 11:27:05 +08002593 if (event && event->type_len == RINGBUF_TYPE_PADDING) {
Tom Zanussi2d622712009-03-22 03:30:49 -05002594 cpu_relax();
2595 goto again;
2596 }
2597
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002598 return event;
2599}
Robert Richterc4f50182008-12-11 16:49:22 +01002600EXPORT_SYMBOL_GPL(ring_buffer_read);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002601
2602/**
2603 * ring_buffer_size - return the size of the ring buffer (in bytes)
2604 * @buffer: The ring buffer.
2605 */
2606unsigned long ring_buffer_size(struct ring_buffer *buffer)
2607{
2608 return BUF_PAGE_SIZE * buffer->pages;
2609}
Robert Richterc4f50182008-12-11 16:49:22 +01002610EXPORT_SYMBOL_GPL(ring_buffer_size);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002611
2612static void
2613rb_reset_cpu(struct ring_buffer_per_cpu *cpu_buffer)
2614{
2615 cpu_buffer->head_page
2616 = list_entry(cpu_buffer->pages.next, struct buffer_page, list);
Steven Rostedtbf41a152008-10-04 02:00:59 -04002617 local_set(&cpu_buffer->head_page->write, 0);
Steven Rostedt778c55d2009-05-01 18:44:45 -04002618 local_set(&cpu_buffer->head_page->entries, 0);
Steven Rostedtabc9b562008-12-02 15:34:06 -05002619 local_set(&cpu_buffer->head_page->page->commit, 0);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002620
Steven Rostedt6f807ac2008-10-04 02:00:58 -04002621 cpu_buffer->head_page->read = 0;
Steven Rostedtbf41a152008-10-04 02:00:59 -04002622
2623 cpu_buffer->tail_page = cpu_buffer->head_page;
2624 cpu_buffer->commit_page = cpu_buffer->head_page;
2625
2626 INIT_LIST_HEAD(&cpu_buffer->reader_page->list);
2627 local_set(&cpu_buffer->reader_page->write, 0);
Steven Rostedt778c55d2009-05-01 18:44:45 -04002628 local_set(&cpu_buffer->reader_page->entries, 0);
Steven Rostedtabc9b562008-12-02 15:34:06 -05002629 local_set(&cpu_buffer->reader_page->page->commit, 0);
Steven Rostedt6f807ac2008-10-04 02:00:58 -04002630 cpu_buffer->reader_page->read = 0;
Steven Rostedtd7690412008-10-01 00:29:53 -04002631
Steven Rostedtf0d2c682009-04-29 13:43:37 -04002632 cpu_buffer->nmi_dropped = 0;
2633 cpu_buffer->commit_overrun = 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002634 cpu_buffer->overrun = 0;
Steven Rostedte4906ef2009-04-30 20:49:44 -04002635 cpu_buffer->read = 0;
2636 local_set(&cpu_buffer->entries, 0);
Steven Rostedt69507c02009-01-21 18:45:57 -05002637
2638 cpu_buffer->write_stamp = 0;
2639 cpu_buffer->read_stamp = 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002640}
2641
2642/**
2643 * ring_buffer_reset_cpu - reset a ring buffer per CPU buffer
2644 * @buffer: The ring buffer to reset a per cpu buffer of
2645 * @cpu: The CPU buffer to be reset
2646 */
2647void ring_buffer_reset_cpu(struct ring_buffer *buffer, int cpu)
2648{
2649 struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
2650 unsigned long flags;
2651
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302652 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04002653 return;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002654
Steven Rostedt41ede232009-05-01 20:26:54 -04002655 atomic_inc(&cpu_buffer->record_disabled);
2656
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002657 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
2658
Steven Rostedt3e03fb72008-11-06 00:09:43 -05002659 __raw_spin_lock(&cpu_buffer->lock);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002660
2661 rb_reset_cpu(cpu_buffer);
2662
Steven Rostedt3e03fb72008-11-06 00:09:43 -05002663 __raw_spin_unlock(&cpu_buffer->lock);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002664
2665 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
Steven Rostedt41ede232009-05-01 20:26:54 -04002666
2667 atomic_dec(&cpu_buffer->record_disabled);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002668}
Robert Richterc4f50182008-12-11 16:49:22 +01002669EXPORT_SYMBOL_GPL(ring_buffer_reset_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002670
2671/**
2672 * ring_buffer_reset - reset a ring buffer
2673 * @buffer: The ring buffer to reset all cpu buffers
2674 */
2675void ring_buffer_reset(struct ring_buffer *buffer)
2676{
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002677 int cpu;
2678
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002679 for_each_buffer_cpu(buffer, cpu)
Steven Rostedtd7690412008-10-01 00:29:53 -04002680 ring_buffer_reset_cpu(buffer, cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002681}
Robert Richterc4f50182008-12-11 16:49:22 +01002682EXPORT_SYMBOL_GPL(ring_buffer_reset);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002683
2684/**
2685 * rind_buffer_empty - is the ring buffer empty?
2686 * @buffer: The ring buffer to test
2687 */
2688int ring_buffer_empty(struct ring_buffer *buffer)
2689{
2690 struct ring_buffer_per_cpu *cpu_buffer;
2691 int cpu;
2692
2693 /* yes this is racy, but if you don't like the race, lock the buffer */
2694 for_each_buffer_cpu(buffer, cpu) {
2695 cpu_buffer = buffer->buffers[cpu];
2696 if (!rb_per_cpu_empty(cpu_buffer))
2697 return 0;
2698 }
Steven Rostedt554f7862009-03-11 22:00:13 -04002699
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002700 return 1;
2701}
Robert Richterc4f50182008-12-11 16:49:22 +01002702EXPORT_SYMBOL_GPL(ring_buffer_empty);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002703
2704/**
2705 * ring_buffer_empty_cpu - is a cpu buffer of a ring buffer empty?
2706 * @buffer: The ring buffer
2707 * @cpu: The CPU buffer to test
2708 */
2709int ring_buffer_empty_cpu(struct ring_buffer *buffer, int cpu)
2710{
2711 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedt8aabee52009-03-12 13:13:49 -04002712 int ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002713
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302714 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04002715 return 1;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002716
2717 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt554f7862009-03-11 22:00:13 -04002718 ret = rb_per_cpu_empty(cpu_buffer);
2719
Steven Rostedt554f7862009-03-11 22:00:13 -04002720
2721 return ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002722}
Robert Richterc4f50182008-12-11 16:49:22 +01002723EXPORT_SYMBOL_GPL(ring_buffer_empty_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002724
2725/**
2726 * ring_buffer_swap_cpu - swap a CPU buffer between two ring buffers
2727 * @buffer_a: One buffer to swap with
2728 * @buffer_b: The other buffer to swap with
2729 *
2730 * This function is useful for tracers that want to take a "snapshot"
2731 * of a CPU buffer and has another back up buffer lying around.
2732 * it is expected that the tracer handles the cpu buffer not being
2733 * used at the moment.
2734 */
2735int ring_buffer_swap_cpu(struct ring_buffer *buffer_a,
2736 struct ring_buffer *buffer_b, int cpu)
2737{
2738 struct ring_buffer_per_cpu *cpu_buffer_a;
2739 struct ring_buffer_per_cpu *cpu_buffer_b;
Steven Rostedt554f7862009-03-11 22:00:13 -04002740 int ret = -EINVAL;
2741
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302742 if (!cpumask_test_cpu(cpu, buffer_a->cpumask) ||
2743 !cpumask_test_cpu(cpu, buffer_b->cpumask))
Steven Rostedt554f7862009-03-11 22:00:13 -04002744 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002745
2746 /* At least make sure the two buffers are somewhat the same */
Lai Jiangshan6d102bc2008-12-17 17:48:23 +08002747 if (buffer_a->pages != buffer_b->pages)
Steven Rostedt554f7862009-03-11 22:00:13 -04002748 goto out;
2749
2750 ret = -EAGAIN;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002751
Steven Rostedt97b17ef2009-01-21 15:24:56 -05002752 if (ring_buffer_flags != RB_BUFFERS_ON)
Steven Rostedt554f7862009-03-11 22:00:13 -04002753 goto out;
Steven Rostedt97b17ef2009-01-21 15:24:56 -05002754
2755 if (atomic_read(&buffer_a->record_disabled))
Steven Rostedt554f7862009-03-11 22:00:13 -04002756 goto out;
Steven Rostedt97b17ef2009-01-21 15:24:56 -05002757
2758 if (atomic_read(&buffer_b->record_disabled))
Steven Rostedt554f7862009-03-11 22:00:13 -04002759 goto out;
Steven Rostedt97b17ef2009-01-21 15:24:56 -05002760
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002761 cpu_buffer_a = buffer_a->buffers[cpu];
2762 cpu_buffer_b = buffer_b->buffers[cpu];
2763
Steven Rostedt97b17ef2009-01-21 15:24:56 -05002764 if (atomic_read(&cpu_buffer_a->record_disabled))
Steven Rostedt554f7862009-03-11 22:00:13 -04002765 goto out;
Steven Rostedt97b17ef2009-01-21 15:24:56 -05002766
2767 if (atomic_read(&cpu_buffer_b->record_disabled))
Steven Rostedt554f7862009-03-11 22:00:13 -04002768 goto out;
Steven Rostedt97b17ef2009-01-21 15:24:56 -05002769
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002770 /*
2771 * We can't do a synchronize_sched here because this
2772 * function can be called in atomic context.
2773 * Normally this will be called from the same CPU as cpu.
2774 * If not it's up to the caller to protect this.
2775 */
2776 atomic_inc(&cpu_buffer_a->record_disabled);
2777 atomic_inc(&cpu_buffer_b->record_disabled);
2778
2779 buffer_a->buffers[cpu] = cpu_buffer_b;
2780 buffer_b->buffers[cpu] = cpu_buffer_a;
2781
2782 cpu_buffer_b->buffer = buffer_a;
2783 cpu_buffer_a->buffer = buffer_b;
2784
2785 atomic_dec(&cpu_buffer_a->record_disabled);
2786 atomic_dec(&cpu_buffer_b->record_disabled);
2787
Steven Rostedt554f7862009-03-11 22:00:13 -04002788 ret = 0;
2789out:
Steven Rostedt554f7862009-03-11 22:00:13 -04002790 return ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002791}
Robert Richterc4f50182008-12-11 16:49:22 +01002792EXPORT_SYMBOL_GPL(ring_buffer_swap_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002793
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002794/**
2795 * ring_buffer_alloc_read_page - allocate a page to read from buffer
2796 * @buffer: the buffer to allocate for.
2797 *
2798 * This function is used in conjunction with ring_buffer_read_page.
2799 * When reading a full page from the ring buffer, these functions
2800 * can be used to speed up the process. The calling function should
2801 * allocate a few pages first with this function. Then when it
2802 * needs to get pages from the ring buffer, it passes the result
2803 * of this function into ring_buffer_read_page, which will swap
2804 * the page that was allocated, with the read page of the buffer.
2805 *
2806 * Returns:
2807 * The page allocated, or NULL on error.
2808 */
2809void *ring_buffer_alloc_read_page(struct ring_buffer *buffer)
2810{
Steven Rostedt044fa782008-12-02 23:50:03 -05002811 struct buffer_data_page *bpage;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05002812 unsigned long addr;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002813
2814 addr = __get_free_page(GFP_KERNEL);
2815 if (!addr)
2816 return NULL;
2817
Steven Rostedt044fa782008-12-02 23:50:03 -05002818 bpage = (void *)addr;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002819
Steven Rostedtef7a4a12009-03-03 00:27:49 -05002820 rb_init_page(bpage);
2821
Steven Rostedt044fa782008-12-02 23:50:03 -05002822 return bpage;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002823}
Steven Rostedtd6ce96d2009-05-05 01:15:24 -04002824EXPORT_SYMBOL_GPL(ring_buffer_alloc_read_page);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002825
2826/**
2827 * ring_buffer_free_read_page - free an allocated read page
2828 * @buffer: the buffer the page was allocate for
2829 * @data: the page to free
2830 *
2831 * Free a page allocated from ring_buffer_alloc_read_page.
2832 */
2833void ring_buffer_free_read_page(struct ring_buffer *buffer, void *data)
2834{
2835 free_page((unsigned long)data);
2836}
Steven Rostedtd6ce96d2009-05-05 01:15:24 -04002837EXPORT_SYMBOL_GPL(ring_buffer_free_read_page);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002838
2839/**
2840 * ring_buffer_read_page - extract a page from the ring buffer
2841 * @buffer: buffer to extract from
2842 * @data_page: the page to use allocated from ring_buffer_alloc_read_page
Steven Rostedtef7a4a12009-03-03 00:27:49 -05002843 * @len: amount to extract
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002844 * @cpu: the cpu of the buffer to extract
2845 * @full: should the extraction only happen when the page is full.
2846 *
2847 * This function will pull out a page from the ring buffer and consume it.
2848 * @data_page must be the address of the variable that was returned
2849 * from ring_buffer_alloc_read_page. This is because the page might be used
2850 * to swap with a page in the ring buffer.
2851 *
2852 * for example:
Lai Jiangshanb85fa012009-02-09 14:21:14 +08002853 * rpage = ring_buffer_alloc_read_page(buffer);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002854 * if (!rpage)
2855 * return error;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05002856 * ret = ring_buffer_read_page(buffer, &rpage, len, cpu, 0);
Lai Jiangshan667d2412009-02-09 14:21:17 +08002857 * if (ret >= 0)
2858 * process_page(rpage, ret);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002859 *
2860 * When @full is set, the function will not return true unless
2861 * the writer is off the reader page.
2862 *
2863 * Note: it is up to the calling functions to handle sleeps and wakeups.
2864 * The ring buffer can be used anywhere in the kernel and can not
2865 * blindly call wake_up. The layer that uses the ring buffer must be
2866 * responsible for that.
2867 *
2868 * Returns:
Lai Jiangshan667d2412009-02-09 14:21:17 +08002869 * >=0 if data has been transferred, returns the offset of consumed data.
2870 * <0 if no data has been transferred.
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002871 */
2872int ring_buffer_read_page(struct ring_buffer *buffer,
Steven Rostedtef7a4a12009-03-03 00:27:49 -05002873 void **data_page, size_t len, int cpu, int full)
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002874{
2875 struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
2876 struct ring_buffer_event *event;
Steven Rostedt044fa782008-12-02 23:50:03 -05002877 struct buffer_data_page *bpage;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05002878 struct buffer_page *reader;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002879 unsigned long flags;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05002880 unsigned int commit;
Lai Jiangshan667d2412009-02-09 14:21:17 +08002881 unsigned int read;
Steven Rostedt4f3640f2009-03-03 23:52:42 -05002882 u64 save_timestamp;
Lai Jiangshan667d2412009-02-09 14:21:17 +08002883 int ret = -1;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002884
Steven Rostedt554f7862009-03-11 22:00:13 -04002885 if (!cpumask_test_cpu(cpu, buffer->cpumask))
2886 goto out;
2887
Steven Rostedt474d32b2009-03-03 19:51:40 -05002888 /*
2889 * If len is not big enough to hold the page header, then
2890 * we can not copy anything.
2891 */
2892 if (len <= BUF_PAGE_HDR_SIZE)
Steven Rostedt554f7862009-03-11 22:00:13 -04002893 goto out;
Steven Rostedt474d32b2009-03-03 19:51:40 -05002894
2895 len -= BUF_PAGE_HDR_SIZE;
2896
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002897 if (!data_page)
Steven Rostedt554f7862009-03-11 22:00:13 -04002898 goto out;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002899
Steven Rostedt044fa782008-12-02 23:50:03 -05002900 bpage = *data_page;
2901 if (!bpage)
Steven Rostedt554f7862009-03-11 22:00:13 -04002902 goto out;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002903
2904 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
2905
Steven Rostedtef7a4a12009-03-03 00:27:49 -05002906 reader = rb_get_reader_page(cpu_buffer);
2907 if (!reader)
Steven Rostedt554f7862009-03-11 22:00:13 -04002908 goto out_unlock;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002909
Steven Rostedtef7a4a12009-03-03 00:27:49 -05002910 event = rb_reader_event(cpu_buffer);
Lai Jiangshan667d2412009-02-09 14:21:17 +08002911
Steven Rostedtef7a4a12009-03-03 00:27:49 -05002912 read = reader->read;
2913 commit = rb_page_commit(reader);
2914
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002915 /*
Steven Rostedt474d32b2009-03-03 19:51:40 -05002916 * If this page has been partially read or
2917 * if len is not big enough to read the rest of the page or
2918 * a writer is still on the page, then
2919 * we must copy the data from the page to the buffer.
2920 * Otherwise, we can simply swap the page with the one passed in.
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002921 */
Steven Rostedt474d32b2009-03-03 19:51:40 -05002922 if (read || (len < (commit - read)) ||
Steven Rostedtef7a4a12009-03-03 00:27:49 -05002923 cpu_buffer->reader_page == cpu_buffer->commit_page) {
Lai Jiangshan667d2412009-02-09 14:21:17 +08002924 struct buffer_data_page *rpage = cpu_buffer->reader_page->page;
Steven Rostedt474d32b2009-03-03 19:51:40 -05002925 unsigned int rpos = read;
2926 unsigned int pos = 0;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05002927 unsigned int size;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002928
2929 if (full)
Steven Rostedt554f7862009-03-11 22:00:13 -04002930 goto out_unlock;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002931
Steven Rostedtef7a4a12009-03-03 00:27:49 -05002932 if (len > (commit - read))
2933 len = (commit - read);
2934
2935 size = rb_event_length(event);
2936
2937 if (len < size)
Steven Rostedt554f7862009-03-11 22:00:13 -04002938 goto out_unlock;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05002939
Steven Rostedt4f3640f2009-03-03 23:52:42 -05002940 /* save the current timestamp, since the user will need it */
2941 save_timestamp = cpu_buffer->read_stamp;
2942
Steven Rostedtef7a4a12009-03-03 00:27:49 -05002943 /* Need to copy one event at a time */
2944 do {
Steven Rostedt474d32b2009-03-03 19:51:40 -05002945 memcpy(bpage->data + pos, rpage->data + rpos, size);
Steven Rostedtef7a4a12009-03-03 00:27:49 -05002946
2947 len -= size;
2948
2949 rb_advance_reader(cpu_buffer);
Steven Rostedt474d32b2009-03-03 19:51:40 -05002950 rpos = reader->read;
2951 pos += size;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05002952
2953 event = rb_reader_event(cpu_buffer);
2954 size = rb_event_length(event);
2955 } while (len > size);
Lai Jiangshan667d2412009-02-09 14:21:17 +08002956
2957 /* update bpage */
Steven Rostedtef7a4a12009-03-03 00:27:49 -05002958 local_set(&bpage->commit, pos);
Steven Rostedt4f3640f2009-03-03 23:52:42 -05002959 bpage->time_stamp = save_timestamp;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05002960
Steven Rostedt474d32b2009-03-03 19:51:40 -05002961 /* we copied everything to the beginning */
2962 read = 0;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002963 } else {
Steven Rostedtafbab762009-05-01 19:40:05 -04002964 /* update the entry counter */
2965 cpu_buffer->read += local_read(&reader->entries);
2966
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002967 /* swap the pages */
Steven Rostedt044fa782008-12-02 23:50:03 -05002968 rb_init_page(bpage);
Steven Rostedtef7a4a12009-03-03 00:27:49 -05002969 bpage = reader->page;
2970 reader->page = *data_page;
2971 local_set(&reader->write, 0);
Steven Rostedt778c55d2009-05-01 18:44:45 -04002972 local_set(&reader->entries, 0);
Steven Rostedtef7a4a12009-03-03 00:27:49 -05002973 reader->read = 0;
Steven Rostedt044fa782008-12-02 23:50:03 -05002974 *data_page = bpage;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002975 }
Lai Jiangshan667d2412009-02-09 14:21:17 +08002976 ret = read;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002977
Steven Rostedt554f7862009-03-11 22:00:13 -04002978 out_unlock:
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002979 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
2980
Steven Rostedt554f7862009-03-11 22:00:13 -04002981 out:
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002982 return ret;
2983}
Steven Rostedtd6ce96d2009-05-05 01:15:24 -04002984EXPORT_SYMBOL_GPL(ring_buffer_read_page);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002985
Steven Rostedta3583242008-11-11 15:01:42 -05002986static ssize_t
2987rb_simple_read(struct file *filp, char __user *ubuf,
2988 size_t cnt, loff_t *ppos)
2989{
Hannes Eder5e398412009-02-10 19:44:34 +01002990 unsigned long *p = filp->private_data;
Steven Rostedta3583242008-11-11 15:01:42 -05002991 char buf[64];
2992 int r;
2993
Steven Rostedt033601a2008-11-21 12:41:55 -05002994 if (test_bit(RB_BUFFERS_DISABLED_BIT, p))
2995 r = sprintf(buf, "permanently disabled\n");
2996 else
2997 r = sprintf(buf, "%d\n", test_bit(RB_BUFFERS_ON_BIT, p));
Steven Rostedta3583242008-11-11 15:01:42 -05002998
2999 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
3000}
3001
3002static ssize_t
3003rb_simple_write(struct file *filp, const char __user *ubuf,
3004 size_t cnt, loff_t *ppos)
3005{
Hannes Eder5e398412009-02-10 19:44:34 +01003006 unsigned long *p = filp->private_data;
Steven Rostedta3583242008-11-11 15:01:42 -05003007 char buf[64];
Hannes Eder5e398412009-02-10 19:44:34 +01003008 unsigned long val;
Steven Rostedta3583242008-11-11 15:01:42 -05003009 int ret;
3010
3011 if (cnt >= sizeof(buf))
3012 return -EINVAL;
3013
3014 if (copy_from_user(&buf, ubuf, cnt))
3015 return -EFAULT;
3016
3017 buf[cnt] = 0;
3018
3019 ret = strict_strtoul(buf, 10, &val);
3020 if (ret < 0)
3021 return ret;
3022
Steven Rostedt033601a2008-11-21 12:41:55 -05003023 if (val)
3024 set_bit(RB_BUFFERS_ON_BIT, p);
3025 else
3026 clear_bit(RB_BUFFERS_ON_BIT, p);
Steven Rostedta3583242008-11-11 15:01:42 -05003027
3028 (*ppos)++;
3029
3030 return cnt;
3031}
3032
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003033static const struct file_operations rb_simple_fops = {
Steven Rostedta3583242008-11-11 15:01:42 -05003034 .open = tracing_open_generic,
3035 .read = rb_simple_read,
3036 .write = rb_simple_write,
3037};
3038
3039
3040static __init int rb_init_debugfs(void)
3041{
3042 struct dentry *d_tracer;
Steven Rostedta3583242008-11-11 15:01:42 -05003043
3044 d_tracer = tracing_init_dentry();
3045
Frederic Weisbecker5452af62009-03-27 00:25:38 +01003046 trace_create_file("tracing_on", 0644, d_tracer,
3047 &ring_buffer_flags, &rb_simple_fops);
Steven Rostedta3583242008-11-11 15:01:42 -05003048
3049 return 0;
3050}
3051
3052fs_initcall(rb_init_debugfs);
Steven Rostedt554f7862009-03-11 22:00:13 -04003053
Steven Rostedt59222ef2009-03-12 11:46:03 -04003054#ifdef CONFIG_HOTPLUG_CPU
Frederic Weisbecker09c9e842009-03-21 04:33:36 +01003055static int rb_cpu_notify(struct notifier_block *self,
3056 unsigned long action, void *hcpu)
Steven Rostedt554f7862009-03-11 22:00:13 -04003057{
3058 struct ring_buffer *buffer =
3059 container_of(self, struct ring_buffer, cpu_notify);
3060 long cpu = (long)hcpu;
3061
3062 switch (action) {
3063 case CPU_UP_PREPARE:
3064 case CPU_UP_PREPARE_FROZEN:
3065 if (cpu_isset(cpu, *buffer->cpumask))
3066 return NOTIFY_OK;
3067
3068 buffer->buffers[cpu] =
3069 rb_allocate_cpu_buffer(buffer, cpu);
3070 if (!buffer->buffers[cpu]) {
3071 WARN(1, "failed to allocate ring buffer on CPU %ld\n",
3072 cpu);
3073 return NOTIFY_OK;
3074 }
3075 smp_wmb();
3076 cpu_set(cpu, *buffer->cpumask);
3077 break;
3078 case CPU_DOWN_PREPARE:
3079 case CPU_DOWN_PREPARE_FROZEN:
3080 /*
3081 * Do nothing.
3082 * If we were to free the buffer, then the user would
3083 * lose any trace that was in the buffer.
3084 */
3085 break;
3086 default:
3087 break;
3088 }
3089 return NOTIFY_OK;
3090}
3091#endif