blob: 26e1359fe193e82d794875d2e7929baabf94d44c [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 {
324 local_t write; /* index for next write */
Steven Rostedt6f807ac2008-10-04 02:00:58 -0400325 unsigned read; /* index for next read */
Steven Rostedte4c2ce82008-10-01 11:14:54 -0400326 struct list_head list; /* list of free pages */
Steven Rostedtabc9b562008-12-02 15:34:06 -0500327 struct buffer_data_page *page; /* Actual data page */
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400328};
329
Steven Rostedt044fa782008-12-02 23:50:03 -0500330static void rb_init_page(struct buffer_data_page *bpage)
Steven Rostedtabc9b562008-12-02 15:34:06 -0500331{
Steven Rostedt044fa782008-12-02 23:50:03 -0500332 local_set(&bpage->commit, 0);
Steven Rostedtabc9b562008-12-02 15:34:06 -0500333}
334
Steven Rostedt474d32b2009-03-03 19:51:40 -0500335/**
336 * ring_buffer_page_len - the size of data on the page.
337 * @page: The page to read
338 *
339 * Returns the amount of data on the page, including buffer page header.
340 */
Steven Rostedtef7a4a12009-03-03 00:27:49 -0500341size_t ring_buffer_page_len(void *page)
342{
Steven Rostedt474d32b2009-03-03 19:51:40 -0500343 return local_read(&((struct buffer_data_page *)page)->commit)
344 + BUF_PAGE_HDR_SIZE;
Steven Rostedtef7a4a12009-03-03 00:27:49 -0500345}
346
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400347/*
Steven Rostedted568292008-09-29 23:02:40 -0400348 * Also stolen from mm/slob.c. Thanks to Mathieu Desnoyers for pointing
349 * this issue out.
350 */
Andrew Morton34a148b2009-01-09 12:27:09 -0800351static void free_buffer_page(struct buffer_page *bpage)
Steven Rostedted568292008-09-29 23:02:40 -0400352{
Andrew Morton34a148b2009-01-09 12:27:09 -0800353 free_page((unsigned long)bpage->page);
Steven Rostedte4c2ce82008-10-01 11:14:54 -0400354 kfree(bpage);
Steven Rostedted568292008-09-29 23:02:40 -0400355}
356
357/*
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400358 * We need to fit the time_stamp delta into 27 bits.
359 */
360static inline int test_time_stamp(u64 delta)
361{
362 if (delta & TS_DELTA_TEST)
363 return 1;
364 return 0;
365}
366
Steven Rostedt474d32b2009-03-03 19:51:40 -0500367#define BUF_PAGE_SIZE (PAGE_SIZE - BUF_PAGE_HDR_SIZE)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400368
Steven Rostedtd1b182a2009-04-15 16:53:47 -0400369int ring_buffer_print_page_header(struct trace_seq *s)
370{
371 struct buffer_data_page field;
372 int ret;
373
374 ret = trace_seq_printf(s, "\tfield: u64 timestamp;\t"
375 "offset:0;\tsize:%u;\n",
376 (unsigned int)sizeof(field.time_stamp));
377
378 ret = trace_seq_printf(s, "\tfield: local_t commit;\t"
379 "offset:%u;\tsize:%u;\n",
380 (unsigned int)offsetof(typeof(field), commit),
381 (unsigned int)sizeof(field.commit));
382
383 ret = trace_seq_printf(s, "\tfield: char data;\t"
384 "offset:%u;\tsize:%u;\n",
385 (unsigned int)offsetof(typeof(field), data),
386 (unsigned int)BUF_PAGE_SIZE);
387
388 return ret;
389}
390
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400391/*
392 * head_page == tail_page && head == tail then buffer is empty.
393 */
394struct ring_buffer_per_cpu {
395 int cpu;
396 struct ring_buffer *buffer;
Steven Rostedtf83c9d02008-11-11 18:47:44 +0100397 spinlock_t reader_lock; /* serialize readers */
Steven Rostedt3e03fb72008-11-06 00:09:43 -0500398 raw_spinlock_t lock;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400399 struct lock_class_key lock_key;
400 struct list_head pages;
Steven Rostedt6f807ac2008-10-04 02:00:58 -0400401 struct buffer_page *head_page; /* read from head */
402 struct buffer_page *tail_page; /* write to tail */
Wenji Huangc3706f02009-02-10 01:03:18 -0500403 struct buffer_page *commit_page; /* committed pages */
Steven Rostedtd7690412008-10-01 00:29:53 -0400404 struct buffer_page *reader_page;
Steven Rostedtf0d2c682009-04-29 13:43:37 -0400405 unsigned long nmi_dropped;
406 unsigned long commit_overrun;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400407 unsigned long overrun;
408 unsigned long entries;
409 u64 write_stamp;
410 u64 read_stamp;
411 atomic_t record_disabled;
412};
413
414struct ring_buffer {
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400415 unsigned pages;
416 unsigned flags;
417 int cpus;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400418 atomic_t record_disabled;
Arnaldo Carvalho de Melo00f62f62009-02-09 17:04:06 -0200419 cpumask_var_t cpumask;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400420
421 struct mutex mutex;
422
423 struct ring_buffer_per_cpu **buffers;
Steven Rostedt554f7862009-03-11 22:00:13 -0400424
Steven Rostedt59222ef2009-03-12 11:46:03 -0400425#ifdef CONFIG_HOTPLUG_CPU
Steven Rostedt554f7862009-03-11 22:00:13 -0400426 struct notifier_block cpu_notify;
427#endif
Steven Rostedt37886f62009-03-17 17:22:06 -0400428 u64 (*clock)(void);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400429};
430
431struct ring_buffer_iter {
432 struct ring_buffer_per_cpu *cpu_buffer;
433 unsigned long head;
434 struct buffer_page *head_page;
435 u64 read_stamp;
436};
437
Steven Rostedtf536aaf2008-11-10 23:07:30 -0500438/* buffer may be either ring_buffer or ring_buffer_per_cpu */
Steven Rostedtbf41a152008-10-04 02:00:59 -0400439#define RB_WARN_ON(buffer, cond) \
Steven Rostedt3e89c7b2008-11-11 15:28:41 -0500440 ({ \
441 int _____ret = unlikely(cond); \
442 if (_____ret) { \
Steven Rostedtbf41a152008-10-04 02:00:59 -0400443 atomic_inc(&buffer->record_disabled); \
444 WARN_ON(1); \
445 } \
Steven Rostedt3e89c7b2008-11-11 15:28:41 -0500446 _____ret; \
447 })
Steven Rostedtf536aaf2008-11-10 23:07:30 -0500448
Steven Rostedt37886f62009-03-17 17:22:06 -0400449/* Up this if you want to test the TIME_EXTENTS and normalization */
450#define DEBUG_SHIFT 0
451
452u64 ring_buffer_time_stamp(struct ring_buffer *buffer, int cpu)
453{
454 u64 time;
455
456 preempt_disable_notrace();
457 /* shift to debug/test normalization and TIME_EXTENTS */
458 time = buffer->clock() << DEBUG_SHIFT;
459 preempt_enable_no_resched_notrace();
460
461 return time;
462}
463EXPORT_SYMBOL_GPL(ring_buffer_time_stamp);
464
465void ring_buffer_normalize_time_stamp(struct ring_buffer *buffer,
466 int cpu, u64 *ts)
467{
468 /* Just stupid testing the normalize function and deltas */
469 *ts >>= DEBUG_SHIFT;
470}
471EXPORT_SYMBOL_GPL(ring_buffer_normalize_time_stamp);
472
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400473/**
474 * check_pages - integrity check of buffer pages
475 * @cpu_buffer: CPU buffer with pages to test
476 *
Wenji Huangc3706f02009-02-10 01:03:18 -0500477 * As a safety measure we check to make sure the data pages have not
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400478 * been corrupted.
479 */
480static int rb_check_pages(struct ring_buffer_per_cpu *cpu_buffer)
481{
482 struct list_head *head = &cpu_buffer->pages;
Steven Rostedt044fa782008-12-02 23:50:03 -0500483 struct buffer_page *bpage, *tmp;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400484
Steven Rostedt3e89c7b2008-11-11 15:28:41 -0500485 if (RB_WARN_ON(cpu_buffer, head->next->prev != head))
486 return -1;
487 if (RB_WARN_ON(cpu_buffer, head->prev->next != head))
488 return -1;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400489
Steven Rostedt044fa782008-12-02 23:50:03 -0500490 list_for_each_entry_safe(bpage, tmp, head, list) {
Steven Rostedt3e89c7b2008-11-11 15:28:41 -0500491 if (RB_WARN_ON(cpu_buffer,
Steven Rostedt044fa782008-12-02 23:50:03 -0500492 bpage->list.next->prev != &bpage->list))
Steven Rostedt3e89c7b2008-11-11 15:28:41 -0500493 return -1;
494 if (RB_WARN_ON(cpu_buffer,
Steven Rostedt044fa782008-12-02 23:50:03 -0500495 bpage->list.prev->next != &bpage->list))
Steven Rostedt3e89c7b2008-11-11 15:28:41 -0500496 return -1;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400497 }
498
499 return 0;
500}
501
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400502static int rb_allocate_pages(struct ring_buffer_per_cpu *cpu_buffer,
503 unsigned nr_pages)
504{
505 struct list_head *head = &cpu_buffer->pages;
Steven Rostedt044fa782008-12-02 23:50:03 -0500506 struct buffer_page *bpage, *tmp;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400507 unsigned long addr;
508 LIST_HEAD(pages);
509 unsigned i;
510
511 for (i = 0; i < nr_pages; i++) {
Steven Rostedt044fa782008-12-02 23:50:03 -0500512 bpage = kzalloc_node(ALIGN(sizeof(*bpage), cache_line_size()),
Steven Rostedtaa1e0e32008-10-02 19:18:09 -0400513 GFP_KERNEL, cpu_to_node(cpu_buffer->cpu));
Steven Rostedt044fa782008-12-02 23:50:03 -0500514 if (!bpage)
Steven Rostedte4c2ce82008-10-01 11:14:54 -0400515 goto free_pages;
Steven Rostedt044fa782008-12-02 23:50:03 -0500516 list_add(&bpage->list, &pages);
Steven Rostedte4c2ce82008-10-01 11:14:54 -0400517
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400518 addr = __get_free_page(GFP_KERNEL);
519 if (!addr)
520 goto free_pages;
Steven Rostedt044fa782008-12-02 23:50:03 -0500521 bpage->page = (void *)addr;
522 rb_init_page(bpage->page);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400523 }
524
525 list_splice(&pages, head);
526
527 rb_check_pages(cpu_buffer);
528
529 return 0;
530
531 free_pages:
Steven Rostedt044fa782008-12-02 23:50:03 -0500532 list_for_each_entry_safe(bpage, tmp, &pages, list) {
533 list_del_init(&bpage->list);
534 free_buffer_page(bpage);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400535 }
536 return -ENOMEM;
537}
538
539static struct ring_buffer_per_cpu *
540rb_allocate_cpu_buffer(struct ring_buffer *buffer, int cpu)
541{
542 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedt044fa782008-12-02 23:50:03 -0500543 struct buffer_page *bpage;
Steven Rostedtd7690412008-10-01 00:29:53 -0400544 unsigned long addr;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400545 int ret;
546
547 cpu_buffer = kzalloc_node(ALIGN(sizeof(*cpu_buffer), cache_line_size()),
548 GFP_KERNEL, cpu_to_node(cpu));
549 if (!cpu_buffer)
550 return NULL;
551
552 cpu_buffer->cpu = cpu;
553 cpu_buffer->buffer = buffer;
Steven Rostedtf83c9d02008-11-11 18:47:44 +0100554 spin_lock_init(&cpu_buffer->reader_lock);
Steven Rostedt3e03fb72008-11-06 00:09:43 -0500555 cpu_buffer->lock = (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400556 INIT_LIST_HEAD(&cpu_buffer->pages);
557
Steven Rostedt044fa782008-12-02 23:50:03 -0500558 bpage = kzalloc_node(ALIGN(sizeof(*bpage), cache_line_size()),
Steven Rostedte4c2ce82008-10-01 11:14:54 -0400559 GFP_KERNEL, cpu_to_node(cpu));
Steven Rostedt044fa782008-12-02 23:50:03 -0500560 if (!bpage)
Steven Rostedte4c2ce82008-10-01 11:14:54 -0400561 goto fail_free_buffer;
562
Steven Rostedt044fa782008-12-02 23:50:03 -0500563 cpu_buffer->reader_page = bpage;
Steven Rostedtd7690412008-10-01 00:29:53 -0400564 addr = __get_free_page(GFP_KERNEL);
565 if (!addr)
Steven Rostedte4c2ce82008-10-01 11:14:54 -0400566 goto fail_free_reader;
Steven Rostedt044fa782008-12-02 23:50:03 -0500567 bpage->page = (void *)addr;
568 rb_init_page(bpage->page);
Steven Rostedte4c2ce82008-10-01 11:14:54 -0400569
Steven Rostedtd7690412008-10-01 00:29:53 -0400570 INIT_LIST_HEAD(&cpu_buffer->reader_page->list);
Steven Rostedtd7690412008-10-01 00:29:53 -0400571
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400572 ret = rb_allocate_pages(cpu_buffer, buffer->pages);
573 if (ret < 0)
Steven Rostedtd7690412008-10-01 00:29:53 -0400574 goto fail_free_reader;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400575
576 cpu_buffer->head_page
577 = list_entry(cpu_buffer->pages.next, struct buffer_page, list);
Steven Rostedtbf41a152008-10-04 02:00:59 -0400578 cpu_buffer->tail_page = cpu_buffer->commit_page = cpu_buffer->head_page;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400579
580 return cpu_buffer;
581
Steven Rostedtd7690412008-10-01 00:29:53 -0400582 fail_free_reader:
583 free_buffer_page(cpu_buffer->reader_page);
584
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400585 fail_free_buffer:
586 kfree(cpu_buffer);
587 return NULL;
588}
589
590static void rb_free_cpu_buffer(struct ring_buffer_per_cpu *cpu_buffer)
591{
592 struct list_head *head = &cpu_buffer->pages;
Steven Rostedt044fa782008-12-02 23:50:03 -0500593 struct buffer_page *bpage, *tmp;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400594
Steven Rostedtd7690412008-10-01 00:29:53 -0400595 free_buffer_page(cpu_buffer->reader_page);
596
Steven Rostedt044fa782008-12-02 23:50:03 -0500597 list_for_each_entry_safe(bpage, tmp, head, list) {
598 list_del_init(&bpage->list);
599 free_buffer_page(bpage);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400600 }
601 kfree(cpu_buffer);
602}
603
Steven Rostedta7b13742008-09-29 23:02:39 -0400604/*
605 * Causes compile errors if the struct buffer_page gets bigger
606 * than the struct page.
607 */
608extern int ring_buffer_page_too_big(void);
609
Steven Rostedt59222ef2009-03-12 11:46:03 -0400610#ifdef CONFIG_HOTPLUG_CPU
Frederic Weisbecker09c9e842009-03-21 04:33:36 +0100611static int rb_cpu_notify(struct notifier_block *self,
612 unsigned long action, void *hcpu);
Steven Rostedt554f7862009-03-11 22:00:13 -0400613#endif
614
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400615/**
616 * ring_buffer_alloc - allocate a new ring_buffer
Robert Richter68814b52008-11-24 12:24:12 +0100617 * @size: the size in bytes per cpu that is needed.
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400618 * @flags: attributes to set for the ring buffer.
619 *
620 * Currently the only flag that is available is the RB_FL_OVERWRITE
621 * flag. This flag means that the buffer will overwrite old data
622 * when the buffer wraps. If this flag is not set, the buffer will
623 * drop data when the tail hits the head.
624 */
625struct ring_buffer *ring_buffer_alloc(unsigned long size, unsigned flags)
626{
627 struct ring_buffer *buffer;
628 int bsize;
629 int cpu;
630
Steven Rostedta7b13742008-09-29 23:02:39 -0400631 /* Paranoid! Optimizes out when all is well */
632 if (sizeof(struct buffer_page) > sizeof(struct page))
633 ring_buffer_page_too_big();
634
635
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400636 /* keep it in its own cache line */
637 buffer = kzalloc(ALIGN(sizeof(*buffer), cache_line_size()),
638 GFP_KERNEL);
639 if (!buffer)
640 return NULL;
641
Rusty Russell9e01c1b2009-01-01 10:12:22 +1030642 if (!alloc_cpumask_var(&buffer->cpumask, GFP_KERNEL))
643 goto fail_free_buffer;
644
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400645 buffer->pages = DIV_ROUND_UP(size, BUF_PAGE_SIZE);
646 buffer->flags = flags;
Steven Rostedt37886f62009-03-17 17:22:06 -0400647 buffer->clock = trace_clock_local;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400648
649 /* need at least two pages */
650 if (buffer->pages == 1)
651 buffer->pages++;
652
Frederic Weisbecker3bf832c2009-03-19 14:47:33 +0100653 /*
654 * In case of non-hotplug cpu, if the ring-buffer is allocated
655 * in early initcall, it will not be notified of secondary cpus.
656 * In that off case, we need to allocate for all possible cpus.
657 */
658#ifdef CONFIG_HOTPLUG_CPU
Steven Rostedt554f7862009-03-11 22:00:13 -0400659 get_online_cpus();
660 cpumask_copy(buffer->cpumask, cpu_online_mask);
Frederic Weisbecker3bf832c2009-03-19 14:47:33 +0100661#else
662 cpumask_copy(buffer->cpumask, cpu_possible_mask);
663#endif
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400664 buffer->cpus = nr_cpu_ids;
665
666 bsize = sizeof(void *) * nr_cpu_ids;
667 buffer->buffers = kzalloc(ALIGN(bsize, cache_line_size()),
668 GFP_KERNEL);
669 if (!buffer->buffers)
Rusty Russell9e01c1b2009-01-01 10:12:22 +1030670 goto fail_free_cpumask;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400671
672 for_each_buffer_cpu(buffer, cpu) {
673 buffer->buffers[cpu] =
674 rb_allocate_cpu_buffer(buffer, cpu);
675 if (!buffer->buffers[cpu])
676 goto fail_free_buffers;
677 }
678
Steven Rostedt59222ef2009-03-12 11:46:03 -0400679#ifdef CONFIG_HOTPLUG_CPU
Steven Rostedt554f7862009-03-11 22:00:13 -0400680 buffer->cpu_notify.notifier_call = rb_cpu_notify;
681 buffer->cpu_notify.priority = 0;
682 register_cpu_notifier(&buffer->cpu_notify);
683#endif
684
685 put_online_cpus();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400686 mutex_init(&buffer->mutex);
687
688 return buffer;
689
690 fail_free_buffers:
691 for_each_buffer_cpu(buffer, cpu) {
692 if (buffer->buffers[cpu])
693 rb_free_cpu_buffer(buffer->buffers[cpu]);
694 }
695 kfree(buffer->buffers);
696
Rusty Russell9e01c1b2009-01-01 10:12:22 +1030697 fail_free_cpumask:
698 free_cpumask_var(buffer->cpumask);
Steven Rostedt554f7862009-03-11 22:00:13 -0400699 put_online_cpus();
Rusty Russell9e01c1b2009-01-01 10:12:22 +1030700
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400701 fail_free_buffer:
702 kfree(buffer);
703 return NULL;
704}
Robert Richterc4f50182008-12-11 16:49:22 +0100705EXPORT_SYMBOL_GPL(ring_buffer_alloc);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400706
707/**
708 * ring_buffer_free - free a ring buffer.
709 * @buffer: the buffer to free.
710 */
711void
712ring_buffer_free(struct ring_buffer *buffer)
713{
714 int cpu;
715
Steven Rostedt554f7862009-03-11 22:00:13 -0400716 get_online_cpus();
717
Steven Rostedt59222ef2009-03-12 11:46:03 -0400718#ifdef CONFIG_HOTPLUG_CPU
Steven Rostedt554f7862009-03-11 22:00:13 -0400719 unregister_cpu_notifier(&buffer->cpu_notify);
720#endif
721
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400722 for_each_buffer_cpu(buffer, cpu)
723 rb_free_cpu_buffer(buffer->buffers[cpu]);
724
Steven Rostedt554f7862009-03-11 22:00:13 -0400725 put_online_cpus();
726
Rusty Russell9e01c1b2009-01-01 10:12:22 +1030727 free_cpumask_var(buffer->cpumask);
728
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400729 kfree(buffer);
730}
Robert Richterc4f50182008-12-11 16:49:22 +0100731EXPORT_SYMBOL_GPL(ring_buffer_free);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400732
Steven Rostedt37886f62009-03-17 17:22:06 -0400733void ring_buffer_set_clock(struct ring_buffer *buffer,
734 u64 (*clock)(void))
735{
736 buffer->clock = clock;
737}
738
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400739static void rb_reset_cpu(struct ring_buffer_per_cpu *cpu_buffer);
740
741static void
742rb_remove_pages(struct ring_buffer_per_cpu *cpu_buffer, unsigned nr_pages)
743{
Steven Rostedt044fa782008-12-02 23:50:03 -0500744 struct buffer_page *bpage;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400745 struct list_head *p;
746 unsigned i;
747
748 atomic_inc(&cpu_buffer->record_disabled);
749 synchronize_sched();
750
751 for (i = 0; i < nr_pages; i++) {
Steven Rostedt3e89c7b2008-11-11 15:28:41 -0500752 if (RB_WARN_ON(cpu_buffer, list_empty(&cpu_buffer->pages)))
753 return;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400754 p = cpu_buffer->pages.next;
Steven Rostedt044fa782008-12-02 23:50:03 -0500755 bpage = list_entry(p, struct buffer_page, list);
756 list_del_init(&bpage->list);
757 free_buffer_page(bpage);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400758 }
Steven Rostedt3e89c7b2008-11-11 15:28:41 -0500759 if (RB_WARN_ON(cpu_buffer, list_empty(&cpu_buffer->pages)))
760 return;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400761
762 rb_reset_cpu(cpu_buffer);
763
764 rb_check_pages(cpu_buffer);
765
766 atomic_dec(&cpu_buffer->record_disabled);
767
768}
769
770static void
771rb_insert_pages(struct ring_buffer_per_cpu *cpu_buffer,
772 struct list_head *pages, unsigned nr_pages)
773{
Steven Rostedt044fa782008-12-02 23:50:03 -0500774 struct buffer_page *bpage;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400775 struct list_head *p;
776 unsigned i;
777
778 atomic_inc(&cpu_buffer->record_disabled);
779 synchronize_sched();
780
781 for (i = 0; i < nr_pages; i++) {
Steven Rostedt3e89c7b2008-11-11 15:28:41 -0500782 if (RB_WARN_ON(cpu_buffer, list_empty(pages)))
783 return;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400784 p = pages->next;
Steven Rostedt044fa782008-12-02 23:50:03 -0500785 bpage = list_entry(p, struct buffer_page, list);
786 list_del_init(&bpage->list);
787 list_add_tail(&bpage->list, &cpu_buffer->pages);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400788 }
789 rb_reset_cpu(cpu_buffer);
790
791 rb_check_pages(cpu_buffer);
792
793 atomic_dec(&cpu_buffer->record_disabled);
794}
795
796/**
797 * ring_buffer_resize - resize the ring buffer
798 * @buffer: the buffer to resize.
799 * @size: the new size.
800 *
801 * The tracer is responsible for making sure that the buffer is
802 * not being used while changing the size.
803 * Note: We may be able to change the above requirement by using
804 * RCU synchronizations.
805 *
806 * Minimum size is 2 * BUF_PAGE_SIZE.
807 *
808 * Returns -1 on failure.
809 */
810int ring_buffer_resize(struct ring_buffer *buffer, unsigned long size)
811{
812 struct ring_buffer_per_cpu *cpu_buffer;
813 unsigned nr_pages, rm_pages, new_pages;
Steven Rostedt044fa782008-12-02 23:50:03 -0500814 struct buffer_page *bpage, *tmp;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400815 unsigned long buffer_size;
816 unsigned long addr;
817 LIST_HEAD(pages);
818 int i, cpu;
819
Ingo Molnaree51a1d2008-11-13 14:58:31 +0100820 /*
821 * Always succeed at resizing a non-existent buffer:
822 */
823 if (!buffer)
824 return size;
825
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400826 size = DIV_ROUND_UP(size, BUF_PAGE_SIZE);
827 size *= BUF_PAGE_SIZE;
828 buffer_size = buffer->pages * BUF_PAGE_SIZE;
829
830 /* we need a minimum of two pages */
831 if (size < BUF_PAGE_SIZE * 2)
832 size = BUF_PAGE_SIZE * 2;
833
834 if (size == buffer_size)
835 return size;
836
837 mutex_lock(&buffer->mutex);
Steven Rostedt554f7862009-03-11 22:00:13 -0400838 get_online_cpus();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400839
840 nr_pages = DIV_ROUND_UP(size, BUF_PAGE_SIZE);
841
842 if (size < buffer_size) {
843
844 /* easy case, just free pages */
Steven Rostedt554f7862009-03-11 22:00:13 -0400845 if (RB_WARN_ON(buffer, nr_pages >= buffer->pages))
846 goto out_fail;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400847
848 rm_pages = buffer->pages - nr_pages;
849
850 for_each_buffer_cpu(buffer, cpu) {
851 cpu_buffer = buffer->buffers[cpu];
852 rb_remove_pages(cpu_buffer, rm_pages);
853 }
854 goto out;
855 }
856
857 /*
858 * This is a bit more difficult. We only want to add pages
859 * when we can allocate enough for all CPUs. We do this
860 * by allocating all the pages and storing them on a local
861 * link list. If we succeed in our allocation, then we
862 * add these pages to the cpu_buffers. Otherwise we just free
863 * them all and return -ENOMEM;
864 */
Steven Rostedt554f7862009-03-11 22:00:13 -0400865 if (RB_WARN_ON(buffer, nr_pages <= buffer->pages))
866 goto out_fail;
Steven Rostedtf536aaf2008-11-10 23:07:30 -0500867
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400868 new_pages = nr_pages - buffer->pages;
869
870 for_each_buffer_cpu(buffer, cpu) {
871 for (i = 0; i < new_pages; i++) {
Steven Rostedt044fa782008-12-02 23:50:03 -0500872 bpage = kzalloc_node(ALIGN(sizeof(*bpage),
Steven Rostedte4c2ce82008-10-01 11:14:54 -0400873 cache_line_size()),
874 GFP_KERNEL, cpu_to_node(cpu));
Steven Rostedt044fa782008-12-02 23:50:03 -0500875 if (!bpage)
Steven Rostedte4c2ce82008-10-01 11:14:54 -0400876 goto free_pages;
Steven Rostedt044fa782008-12-02 23:50:03 -0500877 list_add(&bpage->list, &pages);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400878 addr = __get_free_page(GFP_KERNEL);
879 if (!addr)
880 goto free_pages;
Steven Rostedt044fa782008-12-02 23:50:03 -0500881 bpage->page = (void *)addr;
882 rb_init_page(bpage->page);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400883 }
884 }
885
886 for_each_buffer_cpu(buffer, cpu) {
887 cpu_buffer = buffer->buffers[cpu];
888 rb_insert_pages(cpu_buffer, &pages, new_pages);
889 }
890
Steven Rostedt554f7862009-03-11 22:00:13 -0400891 if (RB_WARN_ON(buffer, !list_empty(&pages)))
892 goto out_fail;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400893
894 out:
895 buffer->pages = nr_pages;
Steven Rostedt554f7862009-03-11 22:00:13 -0400896 put_online_cpus();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400897 mutex_unlock(&buffer->mutex);
898
899 return size;
900
901 free_pages:
Steven Rostedt044fa782008-12-02 23:50:03 -0500902 list_for_each_entry_safe(bpage, tmp, &pages, list) {
903 list_del_init(&bpage->list);
904 free_buffer_page(bpage);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400905 }
Steven Rostedt554f7862009-03-11 22:00:13 -0400906 put_online_cpus();
Vegard Nossum641d2f62008-11-18 19:22:13 +0100907 mutex_unlock(&buffer->mutex);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400908 return -ENOMEM;
Steven Rostedt554f7862009-03-11 22:00:13 -0400909
910 /*
911 * Something went totally wrong, and we are too paranoid
912 * to even clean up the mess.
913 */
914 out_fail:
915 put_online_cpus();
916 mutex_unlock(&buffer->mutex);
917 return -1;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400918}
Robert Richterc4f50182008-12-11 16:49:22 +0100919EXPORT_SYMBOL_GPL(ring_buffer_resize);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400920
Steven Rostedt8789a9e2008-12-02 15:34:07 -0500921static inline void *
Steven Rostedt044fa782008-12-02 23:50:03 -0500922__rb_data_page_index(struct buffer_data_page *bpage, unsigned index)
Steven Rostedt8789a9e2008-12-02 15:34:07 -0500923{
Steven Rostedt044fa782008-12-02 23:50:03 -0500924 return bpage->data + index;
Steven Rostedt8789a9e2008-12-02 15:34:07 -0500925}
926
Steven Rostedt044fa782008-12-02 23:50:03 -0500927static inline void *__rb_page_index(struct buffer_page *bpage, unsigned index)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400928{
Steven Rostedt044fa782008-12-02 23:50:03 -0500929 return bpage->page->data + index;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400930}
931
932static inline struct ring_buffer_event *
Steven Rostedtd7690412008-10-01 00:29:53 -0400933rb_reader_event(struct ring_buffer_per_cpu *cpu_buffer)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400934{
Steven Rostedt6f807ac2008-10-04 02:00:58 -0400935 return __rb_page_index(cpu_buffer->reader_page,
936 cpu_buffer->reader_page->read);
937}
938
939static inline struct ring_buffer_event *
940rb_head_event(struct ring_buffer_per_cpu *cpu_buffer)
941{
942 return __rb_page_index(cpu_buffer->head_page,
943 cpu_buffer->head_page->read);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400944}
945
946static inline struct ring_buffer_event *
947rb_iter_head_event(struct ring_buffer_iter *iter)
948{
Steven Rostedt6f807ac2008-10-04 02:00:58 -0400949 return __rb_page_index(iter->head_page, iter->head);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400950}
951
Steven Rostedtbf41a152008-10-04 02:00:59 -0400952static inline unsigned rb_page_write(struct buffer_page *bpage)
953{
954 return local_read(&bpage->write);
955}
956
957static inline unsigned rb_page_commit(struct buffer_page *bpage)
958{
Steven Rostedtabc9b562008-12-02 15:34:06 -0500959 return local_read(&bpage->page->commit);
Steven Rostedtbf41a152008-10-04 02:00:59 -0400960}
961
962/* Size is determined by what has been commited */
963static inline unsigned rb_page_size(struct buffer_page *bpage)
964{
965 return rb_page_commit(bpage);
966}
967
968static inline unsigned
969rb_commit_index(struct ring_buffer_per_cpu *cpu_buffer)
970{
971 return rb_page_commit(cpu_buffer->commit_page);
972}
973
974static inline unsigned rb_head_size(struct ring_buffer_per_cpu *cpu_buffer)
975{
976 return rb_page_commit(cpu_buffer->head_page);
977}
978
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400979/*
980 * When the tail hits the head and the buffer is in overwrite mode,
981 * the head jumps to the next page and all content on the previous
982 * page is discarded. But before doing so, we update the overrun
983 * variable of the buffer.
984 */
985static void rb_update_overflow(struct ring_buffer_per_cpu *cpu_buffer)
986{
987 struct ring_buffer_event *event;
988 unsigned long head;
989
990 for (head = 0; head < rb_head_size(cpu_buffer);
991 head += rb_event_length(event)) {
992
Steven Rostedt6f807ac2008-10-04 02:00:58 -0400993 event = __rb_page_index(cpu_buffer->head_page, head);
Steven Rostedt3e89c7b2008-11-11 15:28:41 -0500994 if (RB_WARN_ON(cpu_buffer, rb_null_event(event)))
995 return;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400996 /* Only count data entries */
Lai Jiangshan334d4162009-04-24 11:27:05 +0800997 if (event->type_len > RINGBUF_TYPE_DATA_TYPE_LEN_MAX)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400998 continue;
999 cpu_buffer->overrun++;
1000 cpu_buffer->entries--;
1001 }
1002}
1003
1004static inline void rb_inc_page(struct ring_buffer_per_cpu *cpu_buffer,
Steven Rostedt044fa782008-12-02 23:50:03 -05001005 struct buffer_page **bpage)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001006{
Steven Rostedt044fa782008-12-02 23:50:03 -05001007 struct list_head *p = (*bpage)->list.next;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001008
1009 if (p == &cpu_buffer->pages)
1010 p = p->next;
1011
Steven Rostedt044fa782008-12-02 23:50:03 -05001012 *bpage = list_entry(p, struct buffer_page, list);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001013}
1014
Steven Rostedtbf41a152008-10-04 02:00:59 -04001015static inline unsigned
1016rb_event_index(struct ring_buffer_event *event)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001017{
Steven Rostedtbf41a152008-10-04 02:00:59 -04001018 unsigned long addr = (unsigned long)event;
1019
1020 return (addr & ~PAGE_MASK) - (PAGE_SIZE - BUF_PAGE_SIZE);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001021}
1022
Andrew Morton34a148b2009-01-09 12:27:09 -08001023static int
Steven Rostedtbf41a152008-10-04 02:00:59 -04001024rb_is_commit(struct ring_buffer_per_cpu *cpu_buffer,
1025 struct ring_buffer_event *event)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001026{
Steven Rostedtbf41a152008-10-04 02:00:59 -04001027 unsigned long addr = (unsigned long)event;
1028 unsigned long index;
1029
1030 index = rb_event_index(event);
1031 addr &= PAGE_MASK;
1032
1033 return cpu_buffer->commit_page->page == (void *)addr &&
1034 rb_commit_index(cpu_buffer) == index;
1035}
1036
Andrew Morton34a148b2009-01-09 12:27:09 -08001037static void
Steven Rostedtbf41a152008-10-04 02:00:59 -04001038rb_set_commit_event(struct ring_buffer_per_cpu *cpu_buffer,
1039 struct ring_buffer_event *event)
1040{
1041 unsigned long addr = (unsigned long)event;
1042 unsigned long index;
1043
1044 index = rb_event_index(event);
1045 addr &= PAGE_MASK;
1046
1047 while (cpu_buffer->commit_page->page != (void *)addr) {
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05001048 if (RB_WARN_ON(cpu_buffer,
1049 cpu_buffer->commit_page == cpu_buffer->tail_page))
1050 return;
Steven Rostedtabc9b562008-12-02 15:34:06 -05001051 cpu_buffer->commit_page->page->commit =
Steven Rostedtbf41a152008-10-04 02:00:59 -04001052 cpu_buffer->commit_page->write;
1053 rb_inc_page(cpu_buffer, &cpu_buffer->commit_page);
Steven Rostedtabc9b562008-12-02 15:34:06 -05001054 cpu_buffer->write_stamp =
1055 cpu_buffer->commit_page->page->time_stamp;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001056 }
1057
1058 /* Now set the commit to the event's index */
Steven Rostedtabc9b562008-12-02 15:34:06 -05001059 local_set(&cpu_buffer->commit_page->page->commit, index);
Steven Rostedtbf41a152008-10-04 02:00:59 -04001060}
1061
Andrew Morton34a148b2009-01-09 12:27:09 -08001062static void
Steven Rostedtbf41a152008-10-04 02:00:59 -04001063rb_set_commit_to_write(struct ring_buffer_per_cpu *cpu_buffer)
1064{
1065 /*
1066 * We only race with interrupts and NMIs on this CPU.
1067 * If we own the commit event, then we can commit
1068 * all others that interrupted us, since the interruptions
1069 * are in stack format (they finish before they come
1070 * back to us). This allows us to do a simple loop to
1071 * assign the commit to the tail.
1072 */
Steven Rostedta8ccf1d2008-12-23 11:32:24 -05001073 again:
Steven Rostedtbf41a152008-10-04 02:00:59 -04001074 while (cpu_buffer->commit_page != cpu_buffer->tail_page) {
Steven Rostedtabc9b562008-12-02 15:34:06 -05001075 cpu_buffer->commit_page->page->commit =
Steven Rostedtbf41a152008-10-04 02:00:59 -04001076 cpu_buffer->commit_page->write;
1077 rb_inc_page(cpu_buffer, &cpu_buffer->commit_page);
Steven Rostedtabc9b562008-12-02 15:34:06 -05001078 cpu_buffer->write_stamp =
1079 cpu_buffer->commit_page->page->time_stamp;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001080 /* add barrier to keep gcc from optimizing too much */
1081 barrier();
1082 }
1083 while (rb_commit_index(cpu_buffer) !=
1084 rb_page_write(cpu_buffer->commit_page)) {
Steven Rostedtabc9b562008-12-02 15:34:06 -05001085 cpu_buffer->commit_page->page->commit =
Steven Rostedtbf41a152008-10-04 02:00:59 -04001086 cpu_buffer->commit_page->write;
1087 barrier();
1088 }
Steven Rostedta8ccf1d2008-12-23 11:32:24 -05001089
1090 /* again, keep gcc from optimizing */
1091 barrier();
1092
1093 /*
1094 * If an interrupt came in just after the first while loop
1095 * and pushed the tail page forward, we will be left with
1096 * a dangling commit that will never go forward.
1097 */
1098 if (unlikely(cpu_buffer->commit_page != cpu_buffer->tail_page))
1099 goto again;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001100}
1101
Steven Rostedtd7690412008-10-01 00:29:53 -04001102static void rb_reset_reader_page(struct ring_buffer_per_cpu *cpu_buffer)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001103{
Steven Rostedtabc9b562008-12-02 15:34:06 -05001104 cpu_buffer->read_stamp = cpu_buffer->reader_page->page->time_stamp;
Steven Rostedt6f807ac2008-10-04 02:00:58 -04001105 cpu_buffer->reader_page->read = 0;
Steven Rostedtd7690412008-10-01 00:29:53 -04001106}
1107
Andrew Morton34a148b2009-01-09 12:27:09 -08001108static void rb_inc_iter(struct ring_buffer_iter *iter)
Steven Rostedtd7690412008-10-01 00:29:53 -04001109{
1110 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
1111
1112 /*
1113 * The iterator could be on the reader page (it starts there).
1114 * But the head could have moved, since the reader was
1115 * found. Check for this case and assign the iterator
1116 * to the head page instead of next.
1117 */
1118 if (iter->head_page == cpu_buffer->reader_page)
1119 iter->head_page = cpu_buffer->head_page;
1120 else
1121 rb_inc_page(cpu_buffer, &iter->head_page);
1122
Steven Rostedtabc9b562008-12-02 15:34:06 -05001123 iter->read_stamp = iter->head_page->page->time_stamp;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001124 iter->head = 0;
1125}
1126
1127/**
1128 * ring_buffer_update_event - update event type and data
1129 * @event: the even to update
1130 * @type: the type of event
1131 * @length: the size of the event field in the ring buffer
1132 *
1133 * Update the type and data fields of the event. The length
1134 * is the actual size that is written to the ring buffer,
1135 * and with this, we can determine what to place into the
1136 * data field.
1137 */
Andrew Morton34a148b2009-01-09 12:27:09 -08001138static void
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001139rb_update_event(struct ring_buffer_event *event,
1140 unsigned type, unsigned length)
1141{
Lai Jiangshan334d4162009-04-24 11:27:05 +08001142 event->type_len = type;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001143
1144 switch (type) {
1145
1146 case RINGBUF_TYPE_PADDING:
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001147 case RINGBUF_TYPE_TIME_EXTEND:
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001148 case RINGBUF_TYPE_TIME_STAMP:
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001149 break;
1150
Lai Jiangshan334d4162009-04-24 11:27:05 +08001151 case 0:
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001152 length -= RB_EVNT_HDR_SIZE;
Lai Jiangshan334d4162009-04-24 11:27:05 +08001153 if (length > RB_MAX_SMALL_DATA)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001154 event->array[0] = length;
Lai Jiangshan334d4162009-04-24 11:27:05 +08001155 else
1156 event->type_len = DIV_ROUND_UP(length, RB_ALIGNMENT);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001157 break;
1158 default:
1159 BUG();
1160 }
1161}
1162
Andrew Morton34a148b2009-01-09 12:27:09 -08001163static unsigned rb_calculate_event_length(unsigned length)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001164{
1165 struct ring_buffer_event event; /* Used only for sizeof array */
1166
1167 /* zero length can cause confusions */
1168 if (!length)
1169 length = 1;
1170
1171 if (length > RB_MAX_SMALL_DATA)
1172 length += sizeof(event.array[0]);
1173
1174 length += RB_EVNT_HDR_SIZE;
1175 length = ALIGN(length, RB_ALIGNMENT);
1176
1177 return length;
1178}
1179
1180static struct ring_buffer_event *
1181__rb_reserve_next(struct ring_buffer_per_cpu *cpu_buffer,
1182 unsigned type, unsigned long length, u64 *ts)
1183{
Steven Rostedt98db8df2008-12-23 11:32:25 -05001184 struct buffer_page *tail_page, *head_page, *reader_page, *commit_page;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001185 unsigned long tail, write;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001186 struct ring_buffer *buffer = cpu_buffer->buffer;
1187 struct ring_buffer_event *event;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001188 unsigned long flags;
Steven Rostedt78d904b2009-02-05 18:43:07 -05001189 bool lock_taken = false;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001190
Steven Rostedt98db8df2008-12-23 11:32:25 -05001191 commit_page = cpu_buffer->commit_page;
1192 /* we just need to protect against interrupts */
1193 barrier();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001194 tail_page = cpu_buffer->tail_page;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001195 write = local_add_return(length, &tail_page->write);
1196 tail = write - length;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001197
Steven Rostedtbf41a152008-10-04 02:00:59 -04001198 /* See if we shot pass the end of this buffer page */
1199 if (write > BUF_PAGE_SIZE) {
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001200 struct buffer_page *next_page = tail_page;
1201
Steven Rostedt3e03fb72008-11-06 00:09:43 -05001202 local_irq_save(flags);
Steven Rostedt78d904b2009-02-05 18:43:07 -05001203 /*
Steven Rostedta81bd802009-02-06 01:45:16 -05001204 * Since the write to the buffer is still not
1205 * fully lockless, we must be careful with NMIs.
1206 * The locks in the writers are taken when a write
1207 * crosses to a new page. The locks protect against
1208 * races with the readers (this will soon be fixed
1209 * with a lockless solution).
1210 *
1211 * Because we can not protect against NMIs, and we
1212 * want to keep traces reentrant, we need to manage
1213 * what happens when we are in an NMI.
1214 *
Steven Rostedt78d904b2009-02-05 18:43:07 -05001215 * NMIs can happen after we take the lock.
1216 * If we are in an NMI, only take the lock
1217 * if it is not already taken. Otherwise
1218 * simply fail.
1219 */
Steven Rostedta81bd802009-02-06 01:45:16 -05001220 if (unlikely(in_nmi())) {
Steven Rostedtf0d2c682009-04-29 13:43:37 -04001221 if (!__raw_spin_trylock(&cpu_buffer->lock)) {
1222 cpu_buffer->nmi_dropped++;
Steven Rostedt45141d42009-02-12 13:19:48 -05001223 goto out_reset;
Steven Rostedtf0d2c682009-04-29 13:43:37 -04001224 }
Steven Rostedt78d904b2009-02-05 18:43:07 -05001225 } else
1226 __raw_spin_lock(&cpu_buffer->lock);
1227
1228 lock_taken = true;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001229
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001230 rb_inc_page(cpu_buffer, &next_page);
1231
Steven Rostedtd7690412008-10-01 00:29:53 -04001232 head_page = cpu_buffer->head_page;
1233 reader_page = cpu_buffer->reader_page;
1234
1235 /* we grabbed the lock before incrementing */
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05001236 if (RB_WARN_ON(cpu_buffer, next_page == reader_page))
Steven Rostedt45141d42009-02-12 13:19:48 -05001237 goto out_reset;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001238
1239 /*
1240 * If for some reason, we had an interrupt storm that made
1241 * it all the way around the buffer, bail, and warn
1242 * about it.
1243 */
Steven Rostedt98db8df2008-12-23 11:32:25 -05001244 if (unlikely(next_page == commit_page)) {
Steven Rostedtf0d2c682009-04-29 13:43:37 -04001245 cpu_buffer->commit_overrun++;
Steven Rostedt45141d42009-02-12 13:19:48 -05001246 goto out_reset;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001247 }
Steven Rostedtd7690412008-10-01 00:29:53 -04001248
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001249 if (next_page == head_page) {
Lai Jiangshan6f3b3442009-01-12 11:06:18 +08001250 if (!(buffer->flags & RB_FL_OVERWRITE))
Steven Rostedt45141d42009-02-12 13:19:48 -05001251 goto out_reset;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001252
Steven Rostedtbf41a152008-10-04 02:00:59 -04001253 /* tail_page has not moved yet? */
1254 if (tail_page == cpu_buffer->tail_page) {
1255 /* count overflows */
1256 rb_update_overflow(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001257
Steven Rostedtbf41a152008-10-04 02:00:59 -04001258 rb_inc_page(cpu_buffer, &head_page);
1259 cpu_buffer->head_page = head_page;
1260 cpu_buffer->head_page->read = 0;
1261 }
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001262 }
1263
Steven Rostedtbf41a152008-10-04 02:00:59 -04001264 /*
1265 * If the tail page is still the same as what we think
1266 * it is, then it is up to us to update the tail
1267 * pointer.
1268 */
1269 if (tail_page == cpu_buffer->tail_page) {
1270 local_set(&next_page->write, 0);
Steven Rostedtabc9b562008-12-02 15:34:06 -05001271 local_set(&next_page->page->commit, 0);
Steven Rostedtbf41a152008-10-04 02:00:59 -04001272 cpu_buffer->tail_page = next_page;
1273
1274 /* reread the time stamp */
Steven Rostedt37886f62009-03-17 17:22:06 -04001275 *ts = ring_buffer_time_stamp(buffer, cpu_buffer->cpu);
Steven Rostedtabc9b562008-12-02 15:34:06 -05001276 cpu_buffer->tail_page->page->time_stamp = *ts;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001277 }
1278
1279 /*
1280 * The actual tail page has moved forward.
1281 */
1282 if (tail < BUF_PAGE_SIZE) {
1283 /* Mark the rest of the page with padding */
Steven Rostedt6f807ac2008-10-04 02:00:58 -04001284 event = __rb_page_index(tail_page, tail);
Tom Zanussi2d622712009-03-22 03:30:49 -05001285 rb_event_set_padding(event);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001286 }
1287
Steven Rostedtbf41a152008-10-04 02:00:59 -04001288 if (tail <= BUF_PAGE_SIZE)
1289 /* Set the write back to the previous setting */
1290 local_set(&tail_page->write, tail);
1291
1292 /*
1293 * If this was a commit entry that failed,
1294 * increment that too
1295 */
1296 if (tail_page == cpu_buffer->commit_page &&
1297 tail == rb_commit_index(cpu_buffer)) {
1298 rb_set_commit_to_write(cpu_buffer);
1299 }
1300
Steven Rostedt3e03fb72008-11-06 00:09:43 -05001301 __raw_spin_unlock(&cpu_buffer->lock);
1302 local_irq_restore(flags);
Steven Rostedtbf41a152008-10-04 02:00:59 -04001303
1304 /* fail and let the caller try again */
1305 return ERR_PTR(-EAGAIN);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001306 }
1307
Steven Rostedtbf41a152008-10-04 02:00:59 -04001308 /* We reserved something on the buffer */
1309
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05001310 if (RB_WARN_ON(cpu_buffer, write > BUF_PAGE_SIZE))
1311 return NULL;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001312
Steven Rostedt6f807ac2008-10-04 02:00:58 -04001313 event = __rb_page_index(tail_page, tail);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001314 rb_update_event(event, type, length);
1315
Steven Rostedtbf41a152008-10-04 02:00:59 -04001316 /*
1317 * If this is a commit and the tail is zero, then update
1318 * this page's time stamp.
1319 */
1320 if (!tail && rb_is_commit(cpu_buffer, event))
Steven Rostedtabc9b562008-12-02 15:34:06 -05001321 cpu_buffer->commit_page->page->time_stamp = *ts;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001322
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001323 return event;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001324
Steven Rostedt45141d42009-02-12 13:19:48 -05001325 out_reset:
Lai Jiangshan6f3b3442009-01-12 11:06:18 +08001326 /* reset write */
1327 if (tail <= BUF_PAGE_SIZE)
1328 local_set(&tail_page->write, tail);
1329
Steven Rostedt78d904b2009-02-05 18:43:07 -05001330 if (likely(lock_taken))
1331 __raw_spin_unlock(&cpu_buffer->lock);
Steven Rostedt3e03fb72008-11-06 00:09:43 -05001332 local_irq_restore(flags);
Steven Rostedtbf41a152008-10-04 02:00:59 -04001333 return NULL;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001334}
1335
1336static int
1337rb_add_time_stamp(struct ring_buffer_per_cpu *cpu_buffer,
1338 u64 *ts, u64 *delta)
1339{
1340 struct ring_buffer_event *event;
1341 static int once;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001342 int ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001343
1344 if (unlikely(*delta > (1ULL << 59) && !once++)) {
1345 printk(KERN_WARNING "Delta way too big! %llu"
1346 " ts=%llu write stamp = %llu\n",
Stephen Rothwelle2862c92008-10-27 17:43:28 +11001347 (unsigned long long)*delta,
1348 (unsigned long long)*ts,
1349 (unsigned long long)cpu_buffer->write_stamp);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001350 WARN_ON(1);
1351 }
1352
1353 /*
1354 * The delta is too big, we to add a
1355 * new timestamp.
1356 */
1357 event = __rb_reserve_next(cpu_buffer,
1358 RINGBUF_TYPE_TIME_EXTEND,
1359 RB_LEN_TIME_EXTEND,
1360 ts);
1361 if (!event)
Steven Rostedtbf41a152008-10-04 02:00:59 -04001362 return -EBUSY;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001363
Steven Rostedtbf41a152008-10-04 02:00:59 -04001364 if (PTR_ERR(event) == -EAGAIN)
1365 return -EAGAIN;
1366
1367 /* Only a commited time event can update the write stamp */
1368 if (rb_is_commit(cpu_buffer, event)) {
1369 /*
1370 * If this is the first on the page, then we need to
1371 * update the page itself, and just put in a zero.
1372 */
1373 if (rb_event_index(event)) {
1374 event->time_delta = *delta & TS_MASK;
1375 event->array[0] = *delta >> TS_SHIFT;
1376 } else {
Steven Rostedtabc9b562008-12-02 15:34:06 -05001377 cpu_buffer->commit_page->page->time_stamp = *ts;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001378 event->time_delta = 0;
1379 event->array[0] = 0;
1380 }
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001381 cpu_buffer->write_stamp = *ts;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001382 /* let the caller know this was the commit */
1383 ret = 1;
1384 } else {
1385 /* Darn, this is just wasted space */
1386 event->time_delta = 0;
1387 event->array[0] = 0;
1388 ret = 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001389 }
1390
Steven Rostedtbf41a152008-10-04 02:00:59 -04001391 *delta = 0;
1392
1393 return ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001394}
1395
1396static struct ring_buffer_event *
1397rb_reserve_next_event(struct ring_buffer_per_cpu *cpu_buffer,
1398 unsigned type, unsigned long length)
1399{
1400 struct ring_buffer_event *event;
1401 u64 ts, delta;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001402 int commit = 0;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04001403 int nr_loops = 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001404
Steven Rostedtbf41a152008-10-04 02:00:59 -04001405 again:
Steven Rostedt818e3dd2008-10-31 09:58:35 -04001406 /*
1407 * We allow for interrupts to reenter here and do a trace.
1408 * If one does, it will cause this original code to loop
1409 * back here. Even with heavy interrupts happening, this
1410 * should only happen a few times in a row. If this happens
1411 * 1000 times in a row, there must be either an interrupt
1412 * storm or we have something buggy.
1413 * Bail!
1414 */
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05001415 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 1000))
Steven Rostedt818e3dd2008-10-31 09:58:35 -04001416 return NULL;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04001417
Steven Rostedt37886f62009-03-17 17:22:06 -04001418 ts = ring_buffer_time_stamp(cpu_buffer->buffer, cpu_buffer->cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001419
Steven Rostedtbf41a152008-10-04 02:00:59 -04001420 /*
1421 * Only the first commit can update the timestamp.
1422 * Yes there is a race here. If an interrupt comes in
1423 * just after the conditional and it traces too, then it
1424 * will also check the deltas. More than one timestamp may
1425 * also be made. But only the entry that did the actual
1426 * commit will be something other than zero.
1427 */
1428 if (cpu_buffer->tail_page == cpu_buffer->commit_page &&
1429 rb_page_write(cpu_buffer->tail_page) ==
1430 rb_commit_index(cpu_buffer)) {
1431
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001432 delta = ts - cpu_buffer->write_stamp;
1433
Steven Rostedtbf41a152008-10-04 02:00:59 -04001434 /* make sure this delta is calculated here */
1435 barrier();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001436
Steven Rostedtbf41a152008-10-04 02:00:59 -04001437 /* Did the write stamp get updated already? */
1438 if (unlikely(ts < cpu_buffer->write_stamp))
Steven Rostedt4143c5c2008-11-10 21:46:01 -05001439 delta = 0;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001440
1441 if (test_time_stamp(delta)) {
1442
1443 commit = rb_add_time_stamp(cpu_buffer, &ts, &delta);
1444
1445 if (commit == -EBUSY)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001446 return NULL;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001447
1448 if (commit == -EAGAIN)
1449 goto again;
1450
1451 RB_WARN_ON(cpu_buffer, commit < 0);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001452 }
Steven Rostedtbf41a152008-10-04 02:00:59 -04001453 } else
1454 /* Non commits have zero deltas */
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001455 delta = 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001456
1457 event = __rb_reserve_next(cpu_buffer, type, length, &ts);
Steven Rostedtbf41a152008-10-04 02:00:59 -04001458 if (PTR_ERR(event) == -EAGAIN)
1459 goto again;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001460
Steven Rostedtbf41a152008-10-04 02:00:59 -04001461 if (!event) {
1462 if (unlikely(commit))
1463 /*
1464 * Ouch! We needed a timestamp and it was commited. But
1465 * we didn't get our event reserved.
1466 */
1467 rb_set_commit_to_write(cpu_buffer);
1468 return NULL;
1469 }
1470
1471 /*
1472 * If the timestamp was commited, make the commit our entry
1473 * now so that we will update it when needed.
1474 */
1475 if (commit)
1476 rb_set_commit_event(cpu_buffer, event);
1477 else if (!rb_is_commit(cpu_buffer, event))
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001478 delta = 0;
1479
1480 event->time_delta = delta;
1481
1482 return event;
1483}
1484
Steven Rostedtaa18efb2009-04-20 16:16:11 -04001485#define TRACE_RECURSIVE_DEPTH 16
Steven Rostedt261842b2009-04-16 21:41:52 -04001486
1487static int trace_recursive_lock(void)
1488{
Steven Rostedtaa18efb2009-04-20 16:16:11 -04001489 current->trace_recursion++;
Steven Rostedt261842b2009-04-16 21:41:52 -04001490
Steven Rostedtaa18efb2009-04-20 16:16:11 -04001491 if (likely(current->trace_recursion < TRACE_RECURSIVE_DEPTH))
1492 return 0;
Steven Rostedt261842b2009-04-16 21:41:52 -04001493
Steven Rostedtaa18efb2009-04-20 16:16:11 -04001494 /* Disable all tracing before we do anything else */
1495 tracing_off_permanent();
Frederic Weisbeckere057a5e2009-04-19 23:38:12 +02001496
Steven Rostedt7d7d2b82009-04-27 12:37:49 -04001497 printk_once(KERN_WARNING "Tracing recursion: depth[%ld]:"
Steven Rostedtaa18efb2009-04-20 16:16:11 -04001498 "HC[%lu]:SC[%lu]:NMI[%lu]\n",
1499 current->trace_recursion,
1500 hardirq_count() >> HARDIRQ_SHIFT,
1501 softirq_count() >> SOFTIRQ_SHIFT,
1502 in_nmi());
Frederic Weisbeckere057a5e2009-04-19 23:38:12 +02001503
Steven Rostedtaa18efb2009-04-20 16:16:11 -04001504 WARN_ON_ONCE(1);
1505 return -1;
Steven Rostedt261842b2009-04-16 21:41:52 -04001506}
1507
1508static void trace_recursive_unlock(void)
1509{
Steven Rostedtaa18efb2009-04-20 16:16:11 -04001510 WARN_ON_ONCE(!current->trace_recursion);
Steven Rostedt261842b2009-04-16 21:41:52 -04001511
Steven Rostedtaa18efb2009-04-20 16:16:11 -04001512 current->trace_recursion--;
Steven Rostedt261842b2009-04-16 21:41:52 -04001513}
1514
Steven Rostedtbf41a152008-10-04 02:00:59 -04001515static DEFINE_PER_CPU(int, rb_need_resched);
1516
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001517/**
1518 * ring_buffer_lock_reserve - reserve a part of the buffer
1519 * @buffer: the ring buffer to reserve from
1520 * @length: the length of the data to reserve (excluding event header)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001521 *
1522 * Returns a reseverd event on the ring buffer to copy directly to.
1523 * The user of this interface will need to get the body to write into
1524 * and can use the ring_buffer_event_data() interface.
1525 *
1526 * The length is the length of the data needed, not the event length
1527 * which also includes the event header.
1528 *
1529 * Must be paired with ring_buffer_unlock_commit, unless NULL is returned.
1530 * If NULL is returned, then nothing has been allocated or locked.
1531 */
1532struct ring_buffer_event *
Arnaldo Carvalho de Melo0a987752009-02-05 16:12:56 -02001533ring_buffer_lock_reserve(struct ring_buffer *buffer, unsigned long length)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001534{
1535 struct ring_buffer_per_cpu *cpu_buffer;
1536 struct ring_buffer_event *event;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001537 int cpu, resched;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001538
Steven Rostedt033601a2008-11-21 12:41:55 -05001539 if (ring_buffer_flags != RB_BUFFERS_ON)
Steven Rostedta3583242008-11-11 15:01:42 -05001540 return NULL;
1541
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001542 if (atomic_read(&buffer->record_disabled))
1543 return NULL;
1544
Steven Rostedtbf41a152008-10-04 02:00:59 -04001545 /* If we are tracing schedule, we don't want to recurse */
Steven Rostedt182e9f52008-11-03 23:15:56 -05001546 resched = ftrace_preempt_disable();
Steven Rostedtbf41a152008-10-04 02:00:59 -04001547
Steven Rostedt261842b2009-04-16 21:41:52 -04001548 if (trace_recursive_lock())
1549 goto out_nocheck;
1550
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001551 cpu = raw_smp_processor_id();
1552
Rusty Russell9e01c1b2009-01-01 10:12:22 +10301553 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedtd7690412008-10-01 00:29:53 -04001554 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001555
1556 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001557
1558 if (atomic_read(&cpu_buffer->record_disabled))
Steven Rostedtd7690412008-10-01 00:29:53 -04001559 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001560
1561 length = rb_calculate_event_length(length);
1562 if (length > BUF_PAGE_SIZE)
Steven Rostedtbf41a152008-10-04 02:00:59 -04001563 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001564
Lai Jiangshan334d4162009-04-24 11:27:05 +08001565 event = rb_reserve_next_event(cpu_buffer, 0, length);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001566 if (!event)
Steven Rostedtd7690412008-10-01 00:29:53 -04001567 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001568
Steven Rostedtbf41a152008-10-04 02:00:59 -04001569 /*
1570 * Need to store resched state on this cpu.
1571 * Only the first needs to.
1572 */
1573
1574 if (preempt_count() == 1)
1575 per_cpu(rb_need_resched, cpu) = resched;
1576
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001577 return event;
1578
Steven Rostedtd7690412008-10-01 00:29:53 -04001579 out:
Steven Rostedt261842b2009-04-16 21:41:52 -04001580 trace_recursive_unlock();
1581
1582 out_nocheck:
Steven Rostedt182e9f52008-11-03 23:15:56 -05001583 ftrace_preempt_enable(resched);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001584 return NULL;
1585}
Robert Richterc4f50182008-12-11 16:49:22 +01001586EXPORT_SYMBOL_GPL(ring_buffer_lock_reserve);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001587
1588static void rb_commit(struct ring_buffer_per_cpu *cpu_buffer,
1589 struct ring_buffer_event *event)
1590{
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001591 cpu_buffer->entries++;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001592
1593 /* Only process further if we own the commit */
1594 if (!rb_is_commit(cpu_buffer, event))
1595 return;
1596
1597 cpu_buffer->write_stamp += event->time_delta;
1598
1599 rb_set_commit_to_write(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001600}
1601
1602/**
1603 * ring_buffer_unlock_commit - commit a reserved
1604 * @buffer: The buffer to commit to
1605 * @event: The event pointer to commit.
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001606 *
1607 * This commits the data to the ring buffer, and releases any locks held.
1608 *
1609 * Must be paired with ring_buffer_lock_reserve.
1610 */
1611int ring_buffer_unlock_commit(struct ring_buffer *buffer,
Arnaldo Carvalho de Melo0a987752009-02-05 16:12:56 -02001612 struct ring_buffer_event *event)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001613{
1614 struct ring_buffer_per_cpu *cpu_buffer;
1615 int cpu = raw_smp_processor_id();
1616
1617 cpu_buffer = buffer->buffers[cpu];
1618
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001619 rb_commit(cpu_buffer, event);
1620
Steven Rostedt261842b2009-04-16 21:41:52 -04001621 trace_recursive_unlock();
1622
Steven Rostedtbf41a152008-10-04 02:00:59 -04001623 /*
1624 * Only the last preempt count needs to restore preemption.
1625 */
Steven Rostedt182e9f52008-11-03 23:15:56 -05001626 if (preempt_count() == 1)
1627 ftrace_preempt_enable(per_cpu(rb_need_resched, cpu));
1628 else
Steven Rostedtbf41a152008-10-04 02:00:59 -04001629 preempt_enable_no_resched_notrace();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001630
1631 return 0;
1632}
Robert Richterc4f50182008-12-11 16:49:22 +01001633EXPORT_SYMBOL_GPL(ring_buffer_unlock_commit);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001634
Frederic Weisbeckerf3b9aae2009-04-19 23:39:33 +02001635static inline void rb_event_discard(struct ring_buffer_event *event)
1636{
Lai Jiangshan334d4162009-04-24 11:27:05 +08001637 /* array[0] holds the actual length for the discarded event */
1638 event->array[0] = rb_event_data_length(event) - RB_EVNT_HDR_SIZE;
1639 event->type_len = RINGBUF_TYPE_PADDING;
Frederic Weisbeckerf3b9aae2009-04-19 23:39:33 +02001640 /* time delta must be non zero */
1641 if (!event->time_delta)
1642 event->time_delta = 1;
1643}
1644
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001645/**
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04001646 * ring_buffer_event_discard - discard any event in the ring buffer
1647 * @event: the event to discard
1648 *
1649 * Sometimes a event that is in the ring buffer needs to be ignored.
1650 * This function lets the user discard an event in the ring buffer
1651 * and then that event will not be read later.
1652 *
1653 * Note, it is up to the user to be careful with this, and protect
1654 * against races. If the user discards an event that has been consumed
1655 * it is possible that it could corrupt the ring buffer.
1656 */
1657void ring_buffer_event_discard(struct ring_buffer_event *event)
1658{
Frederic Weisbeckerf3b9aae2009-04-19 23:39:33 +02001659 rb_event_discard(event);
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04001660}
1661EXPORT_SYMBOL_GPL(ring_buffer_event_discard);
1662
1663/**
1664 * ring_buffer_commit_discard - discard an event that has not been committed
1665 * @buffer: the ring buffer
1666 * @event: non committed event to discard
1667 *
1668 * This is similar to ring_buffer_event_discard but must only be
1669 * performed on an event that has not been committed yet. The difference
1670 * is that this will also try to free the event from the ring buffer
1671 * if another event has not been added behind it.
1672 *
1673 * If another event has been added behind it, it will set the event
1674 * up as discarded, and perform the commit.
1675 *
1676 * If this function is called, do not call ring_buffer_unlock_commit on
1677 * the event.
1678 */
1679void ring_buffer_discard_commit(struct ring_buffer *buffer,
1680 struct ring_buffer_event *event)
1681{
1682 struct ring_buffer_per_cpu *cpu_buffer;
1683 unsigned long new_index, old_index;
1684 struct buffer_page *bpage;
1685 unsigned long index;
1686 unsigned long addr;
1687 int cpu;
1688
1689 /* The event is discarded regardless */
Frederic Weisbeckerf3b9aae2009-04-19 23:39:33 +02001690 rb_event_discard(event);
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04001691
1692 /*
1693 * This must only be called if the event has not been
1694 * committed yet. Thus we can assume that preemption
1695 * is still disabled.
1696 */
1697 RB_WARN_ON(buffer, !preempt_count());
1698
1699 cpu = smp_processor_id();
1700 cpu_buffer = buffer->buffers[cpu];
1701
1702 new_index = rb_event_index(event);
1703 old_index = new_index + rb_event_length(event);
1704 addr = (unsigned long)event;
1705 addr &= PAGE_MASK;
1706
1707 bpage = cpu_buffer->tail_page;
1708
1709 if (bpage == (void *)addr && rb_page_write(bpage) == old_index) {
1710 /*
1711 * This is on the tail page. It is possible that
1712 * a write could come in and move the tail page
1713 * and write to the next page. That is fine
1714 * because we just shorten what is on this page.
1715 */
1716 index = local_cmpxchg(&bpage->write, old_index, new_index);
1717 if (index == old_index)
1718 goto out;
1719 }
1720
1721 /*
1722 * The commit is still visible by the reader, so we
1723 * must increment entries.
1724 */
1725 cpu_buffer->entries++;
1726 out:
1727 /*
1728 * If a write came in and pushed the tail page
1729 * we still need to update the commit pointer
1730 * if we were the commit.
1731 */
1732 if (rb_is_commit(cpu_buffer, event))
1733 rb_set_commit_to_write(cpu_buffer);
1734
Frederic Weisbeckerf3b9aae2009-04-19 23:39:33 +02001735 trace_recursive_unlock();
1736
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04001737 /*
1738 * Only the last preempt count needs to restore preemption.
1739 */
1740 if (preempt_count() == 1)
1741 ftrace_preempt_enable(per_cpu(rb_need_resched, cpu));
1742 else
1743 preempt_enable_no_resched_notrace();
1744
1745}
1746EXPORT_SYMBOL_GPL(ring_buffer_discard_commit);
1747
1748/**
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001749 * ring_buffer_write - write data to the buffer without reserving
1750 * @buffer: The ring buffer to write to.
1751 * @length: The length of the data being written (excluding the event header)
1752 * @data: The data to write to the buffer.
1753 *
1754 * This is like ring_buffer_lock_reserve and ring_buffer_unlock_commit as
1755 * one function. If you already have the data to write to the buffer, it
1756 * may be easier to simply call this function.
1757 *
1758 * Note, like ring_buffer_lock_reserve, the length is the length of the data
1759 * and not the length of the event which would hold the header.
1760 */
1761int ring_buffer_write(struct ring_buffer *buffer,
1762 unsigned long length,
1763 void *data)
1764{
1765 struct ring_buffer_per_cpu *cpu_buffer;
1766 struct ring_buffer_event *event;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001767 unsigned long event_length;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001768 void *body;
1769 int ret = -EBUSY;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001770 int cpu, resched;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001771
Steven Rostedt033601a2008-11-21 12:41:55 -05001772 if (ring_buffer_flags != RB_BUFFERS_ON)
Steven Rostedta3583242008-11-11 15:01:42 -05001773 return -EBUSY;
1774
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001775 if (atomic_read(&buffer->record_disabled))
1776 return -EBUSY;
1777
Steven Rostedt182e9f52008-11-03 23:15:56 -05001778 resched = ftrace_preempt_disable();
Steven Rostedtbf41a152008-10-04 02:00:59 -04001779
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001780 cpu = raw_smp_processor_id();
1781
Rusty Russell9e01c1b2009-01-01 10:12:22 +10301782 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedtd7690412008-10-01 00:29:53 -04001783 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001784
1785 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001786
1787 if (atomic_read(&cpu_buffer->record_disabled))
1788 goto out;
1789
1790 event_length = rb_calculate_event_length(length);
Lai Jiangshan334d4162009-04-24 11:27:05 +08001791 event = rb_reserve_next_event(cpu_buffer, 0, event_length);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001792 if (!event)
1793 goto out;
1794
1795 body = rb_event_data(event);
1796
1797 memcpy(body, data, length);
1798
1799 rb_commit(cpu_buffer, event);
1800
1801 ret = 0;
1802 out:
Steven Rostedt182e9f52008-11-03 23:15:56 -05001803 ftrace_preempt_enable(resched);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001804
1805 return ret;
1806}
Robert Richterc4f50182008-12-11 16:49:22 +01001807EXPORT_SYMBOL_GPL(ring_buffer_write);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001808
Andrew Morton34a148b2009-01-09 12:27:09 -08001809static int rb_per_cpu_empty(struct ring_buffer_per_cpu *cpu_buffer)
Steven Rostedtbf41a152008-10-04 02:00:59 -04001810{
1811 struct buffer_page *reader = cpu_buffer->reader_page;
1812 struct buffer_page *head = cpu_buffer->head_page;
1813 struct buffer_page *commit = cpu_buffer->commit_page;
1814
1815 return reader->read == rb_page_commit(reader) &&
1816 (commit == reader ||
1817 (commit == head &&
1818 head->read == rb_page_commit(commit)));
1819}
1820
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001821/**
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001822 * ring_buffer_record_disable - stop all writes into the buffer
1823 * @buffer: The ring buffer to stop writes to.
1824 *
1825 * This prevents all writes to the buffer. Any attempt to write
1826 * to the buffer after this will fail and return NULL.
1827 *
1828 * The caller should call synchronize_sched() after this.
1829 */
1830void ring_buffer_record_disable(struct ring_buffer *buffer)
1831{
1832 atomic_inc(&buffer->record_disabled);
1833}
Robert Richterc4f50182008-12-11 16:49:22 +01001834EXPORT_SYMBOL_GPL(ring_buffer_record_disable);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001835
1836/**
1837 * ring_buffer_record_enable - enable writes to the buffer
1838 * @buffer: The ring buffer to enable writes
1839 *
1840 * Note, multiple disables will need the same number of enables
1841 * to truely enable the writing (much like preempt_disable).
1842 */
1843void ring_buffer_record_enable(struct ring_buffer *buffer)
1844{
1845 atomic_dec(&buffer->record_disabled);
1846}
Robert Richterc4f50182008-12-11 16:49:22 +01001847EXPORT_SYMBOL_GPL(ring_buffer_record_enable);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001848
1849/**
1850 * ring_buffer_record_disable_cpu - stop all writes into the cpu_buffer
1851 * @buffer: The ring buffer to stop writes to.
1852 * @cpu: The CPU buffer to stop
1853 *
1854 * This prevents all writes to the buffer. Any attempt to write
1855 * to the buffer after this will fail and return NULL.
1856 *
1857 * The caller should call synchronize_sched() after this.
1858 */
1859void ring_buffer_record_disable_cpu(struct ring_buffer *buffer, int cpu)
1860{
1861 struct ring_buffer_per_cpu *cpu_buffer;
1862
Rusty Russell9e01c1b2009-01-01 10:12:22 +10301863 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04001864 return;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001865
1866 cpu_buffer = buffer->buffers[cpu];
1867 atomic_inc(&cpu_buffer->record_disabled);
1868}
Robert Richterc4f50182008-12-11 16:49:22 +01001869EXPORT_SYMBOL_GPL(ring_buffer_record_disable_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001870
1871/**
1872 * ring_buffer_record_enable_cpu - enable writes to the buffer
1873 * @buffer: The ring buffer to enable writes
1874 * @cpu: The CPU to enable.
1875 *
1876 * Note, multiple disables will need the same number of enables
1877 * to truely enable the writing (much like preempt_disable).
1878 */
1879void ring_buffer_record_enable_cpu(struct ring_buffer *buffer, int cpu)
1880{
1881 struct ring_buffer_per_cpu *cpu_buffer;
1882
Rusty Russell9e01c1b2009-01-01 10:12:22 +10301883 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04001884 return;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001885
1886 cpu_buffer = buffer->buffers[cpu];
1887 atomic_dec(&cpu_buffer->record_disabled);
1888}
Robert Richterc4f50182008-12-11 16:49:22 +01001889EXPORT_SYMBOL_GPL(ring_buffer_record_enable_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001890
1891/**
1892 * ring_buffer_entries_cpu - get the number of entries in a cpu buffer
1893 * @buffer: The ring buffer
1894 * @cpu: The per CPU buffer to get the entries from.
1895 */
1896unsigned long ring_buffer_entries_cpu(struct ring_buffer *buffer, int cpu)
1897{
1898 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedt8aabee52009-03-12 13:13:49 -04001899 unsigned long ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001900
Rusty Russell9e01c1b2009-01-01 10:12:22 +10301901 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04001902 return 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001903
1904 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt554f7862009-03-11 22:00:13 -04001905 ret = cpu_buffer->entries;
Steven Rostedt554f7862009-03-11 22:00:13 -04001906
1907 return ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001908}
Robert Richterc4f50182008-12-11 16:49:22 +01001909EXPORT_SYMBOL_GPL(ring_buffer_entries_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001910
1911/**
1912 * ring_buffer_overrun_cpu - get the number of overruns in a cpu_buffer
1913 * @buffer: The ring buffer
1914 * @cpu: The per CPU buffer to get the number of overruns from
1915 */
1916unsigned long ring_buffer_overrun_cpu(struct ring_buffer *buffer, int cpu)
1917{
1918 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedt8aabee52009-03-12 13:13:49 -04001919 unsigned long ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001920
Rusty Russell9e01c1b2009-01-01 10:12:22 +10301921 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04001922 return 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001923
1924 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt554f7862009-03-11 22:00:13 -04001925 ret = cpu_buffer->overrun;
Steven Rostedt554f7862009-03-11 22:00:13 -04001926
1927 return ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001928}
Robert Richterc4f50182008-12-11 16:49:22 +01001929EXPORT_SYMBOL_GPL(ring_buffer_overrun_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001930
1931/**
Steven Rostedtf0d2c682009-04-29 13:43:37 -04001932 * ring_buffer_nmi_dropped_cpu - get the number of nmis that were dropped
1933 * @buffer: The ring buffer
1934 * @cpu: The per CPU buffer to get the number of overruns from
1935 */
1936unsigned long ring_buffer_nmi_dropped_cpu(struct ring_buffer *buffer, int cpu)
1937{
1938 struct ring_buffer_per_cpu *cpu_buffer;
1939 unsigned long ret;
1940
1941 if (!cpumask_test_cpu(cpu, buffer->cpumask))
1942 return 0;
1943
1944 cpu_buffer = buffer->buffers[cpu];
1945 ret = cpu_buffer->nmi_dropped;
1946
1947 return ret;
1948}
1949EXPORT_SYMBOL_GPL(ring_buffer_nmi_dropped_cpu);
1950
1951/**
1952 * ring_buffer_commit_overrun_cpu - get the number of overruns caused by commits
1953 * @buffer: The ring buffer
1954 * @cpu: The per CPU buffer to get the number of overruns from
1955 */
1956unsigned long
1957ring_buffer_commit_overrun_cpu(struct ring_buffer *buffer, int cpu)
1958{
1959 struct ring_buffer_per_cpu *cpu_buffer;
1960 unsigned long ret;
1961
1962 if (!cpumask_test_cpu(cpu, buffer->cpumask))
1963 return 0;
1964
1965 cpu_buffer = buffer->buffers[cpu];
1966 ret = cpu_buffer->commit_overrun;
1967
1968 return ret;
1969}
1970EXPORT_SYMBOL_GPL(ring_buffer_commit_overrun_cpu);
1971
1972/**
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001973 * ring_buffer_entries - get the number of entries in a buffer
1974 * @buffer: The ring buffer
1975 *
1976 * Returns the total number of entries in the ring buffer
1977 * (all CPU entries)
1978 */
1979unsigned long ring_buffer_entries(struct ring_buffer *buffer)
1980{
1981 struct ring_buffer_per_cpu *cpu_buffer;
1982 unsigned long entries = 0;
1983 int cpu;
1984
1985 /* if you care about this being correct, lock the buffer */
1986 for_each_buffer_cpu(buffer, cpu) {
1987 cpu_buffer = buffer->buffers[cpu];
1988 entries += cpu_buffer->entries;
1989 }
1990
1991 return entries;
1992}
Robert Richterc4f50182008-12-11 16:49:22 +01001993EXPORT_SYMBOL_GPL(ring_buffer_entries);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001994
1995/**
1996 * ring_buffer_overrun_cpu - get the number of overruns in buffer
1997 * @buffer: The ring buffer
1998 *
1999 * Returns the total number of overruns in the ring buffer
2000 * (all CPU entries)
2001 */
2002unsigned long ring_buffer_overruns(struct ring_buffer *buffer)
2003{
2004 struct ring_buffer_per_cpu *cpu_buffer;
2005 unsigned long overruns = 0;
2006 int cpu;
2007
2008 /* if you care about this being correct, lock the buffer */
2009 for_each_buffer_cpu(buffer, cpu) {
2010 cpu_buffer = buffer->buffers[cpu];
2011 overruns += cpu_buffer->overrun;
2012 }
2013
2014 return overruns;
2015}
Robert Richterc4f50182008-12-11 16:49:22 +01002016EXPORT_SYMBOL_GPL(ring_buffer_overruns);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002017
Steven Rostedt642edba2008-11-12 00:01:26 -05002018static void rb_iter_reset(struct ring_buffer_iter *iter)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002019{
2020 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
2021
Steven Rostedtd7690412008-10-01 00:29:53 -04002022 /* Iterator usage is expected to have record disabled */
2023 if (list_empty(&cpu_buffer->reader_page->list)) {
2024 iter->head_page = cpu_buffer->head_page;
Steven Rostedt6f807ac2008-10-04 02:00:58 -04002025 iter->head = cpu_buffer->head_page->read;
Steven Rostedtd7690412008-10-01 00:29:53 -04002026 } else {
2027 iter->head_page = cpu_buffer->reader_page;
Steven Rostedt6f807ac2008-10-04 02:00:58 -04002028 iter->head = cpu_buffer->reader_page->read;
Steven Rostedtd7690412008-10-01 00:29:53 -04002029 }
2030 if (iter->head)
2031 iter->read_stamp = cpu_buffer->read_stamp;
2032 else
Steven Rostedtabc9b562008-12-02 15:34:06 -05002033 iter->read_stamp = iter->head_page->page->time_stamp;
Steven Rostedt642edba2008-11-12 00:01:26 -05002034}
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002035
Steven Rostedt642edba2008-11-12 00:01:26 -05002036/**
2037 * ring_buffer_iter_reset - reset an iterator
2038 * @iter: The iterator to reset
2039 *
2040 * Resets the iterator, so that it will start from the beginning
2041 * again.
2042 */
2043void ring_buffer_iter_reset(struct ring_buffer_iter *iter)
2044{
Steven Rostedt554f7862009-03-11 22:00:13 -04002045 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedt642edba2008-11-12 00:01:26 -05002046 unsigned long flags;
2047
Steven Rostedt554f7862009-03-11 22:00:13 -04002048 if (!iter)
2049 return;
2050
2051 cpu_buffer = iter->cpu_buffer;
2052
Steven Rostedt642edba2008-11-12 00:01:26 -05002053 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
2054 rb_iter_reset(iter);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002055 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002056}
Robert Richterc4f50182008-12-11 16:49:22 +01002057EXPORT_SYMBOL_GPL(ring_buffer_iter_reset);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002058
2059/**
2060 * ring_buffer_iter_empty - check if an iterator has no more to read
2061 * @iter: The iterator to check
2062 */
2063int ring_buffer_iter_empty(struct ring_buffer_iter *iter)
2064{
2065 struct ring_buffer_per_cpu *cpu_buffer;
2066
2067 cpu_buffer = iter->cpu_buffer;
2068
Steven Rostedtbf41a152008-10-04 02:00:59 -04002069 return iter->head_page == cpu_buffer->commit_page &&
2070 iter->head == rb_commit_index(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002071}
Robert Richterc4f50182008-12-11 16:49:22 +01002072EXPORT_SYMBOL_GPL(ring_buffer_iter_empty);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002073
2074static void
2075rb_update_read_stamp(struct ring_buffer_per_cpu *cpu_buffer,
2076 struct ring_buffer_event *event)
2077{
2078 u64 delta;
2079
Lai Jiangshan334d4162009-04-24 11:27:05 +08002080 switch (event->type_len) {
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002081 case RINGBUF_TYPE_PADDING:
2082 return;
2083
2084 case RINGBUF_TYPE_TIME_EXTEND:
2085 delta = event->array[0];
2086 delta <<= TS_SHIFT;
2087 delta += event->time_delta;
2088 cpu_buffer->read_stamp += delta;
2089 return;
2090
2091 case RINGBUF_TYPE_TIME_STAMP:
2092 /* FIXME: not implemented */
2093 return;
2094
2095 case RINGBUF_TYPE_DATA:
2096 cpu_buffer->read_stamp += event->time_delta;
2097 return;
2098
2099 default:
2100 BUG();
2101 }
2102 return;
2103}
2104
2105static void
2106rb_update_iter_read_stamp(struct ring_buffer_iter *iter,
2107 struct ring_buffer_event *event)
2108{
2109 u64 delta;
2110
Lai Jiangshan334d4162009-04-24 11:27:05 +08002111 switch (event->type_len) {
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002112 case RINGBUF_TYPE_PADDING:
2113 return;
2114
2115 case RINGBUF_TYPE_TIME_EXTEND:
2116 delta = event->array[0];
2117 delta <<= TS_SHIFT;
2118 delta += event->time_delta;
2119 iter->read_stamp += delta;
2120 return;
2121
2122 case RINGBUF_TYPE_TIME_STAMP:
2123 /* FIXME: not implemented */
2124 return;
2125
2126 case RINGBUF_TYPE_DATA:
2127 iter->read_stamp += event->time_delta;
2128 return;
2129
2130 default:
2131 BUG();
2132 }
2133 return;
2134}
2135
Steven Rostedtd7690412008-10-01 00:29:53 -04002136static struct buffer_page *
2137rb_get_reader_page(struct ring_buffer_per_cpu *cpu_buffer)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002138{
Steven Rostedtd7690412008-10-01 00:29:53 -04002139 struct buffer_page *reader = NULL;
2140 unsigned long flags;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002141 int nr_loops = 0;
Steven Rostedtd7690412008-10-01 00:29:53 -04002142
Steven Rostedt3e03fb72008-11-06 00:09:43 -05002143 local_irq_save(flags);
2144 __raw_spin_lock(&cpu_buffer->lock);
Steven Rostedtd7690412008-10-01 00:29:53 -04002145
2146 again:
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002147 /*
2148 * This should normally only loop twice. But because the
2149 * start of the reader inserts an empty page, it causes
2150 * a case where we will loop three times. There should be no
2151 * reason to loop four times (that I know of).
2152 */
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05002153 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 3)) {
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002154 reader = NULL;
2155 goto out;
2156 }
2157
Steven Rostedtd7690412008-10-01 00:29:53 -04002158 reader = cpu_buffer->reader_page;
2159
2160 /* If there's more to read, return this page */
Steven Rostedtbf41a152008-10-04 02:00:59 -04002161 if (cpu_buffer->reader_page->read < rb_page_size(reader))
Steven Rostedtd7690412008-10-01 00:29:53 -04002162 goto out;
2163
2164 /* Never should we have an index greater than the size */
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05002165 if (RB_WARN_ON(cpu_buffer,
2166 cpu_buffer->reader_page->read > rb_page_size(reader)))
2167 goto out;
Steven Rostedtd7690412008-10-01 00:29:53 -04002168
2169 /* check if we caught up to the tail */
2170 reader = NULL;
Steven Rostedtbf41a152008-10-04 02:00:59 -04002171 if (cpu_buffer->commit_page == cpu_buffer->reader_page)
Steven Rostedtd7690412008-10-01 00:29:53 -04002172 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002173
2174 /*
Steven Rostedtd7690412008-10-01 00:29:53 -04002175 * Splice the empty reader page into the list around the head.
2176 * Reset the reader page to size zero.
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002177 */
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002178
Steven Rostedtd7690412008-10-01 00:29:53 -04002179 reader = cpu_buffer->head_page;
2180 cpu_buffer->reader_page->list.next = reader->list.next;
2181 cpu_buffer->reader_page->list.prev = reader->list.prev;
Steven Rostedtbf41a152008-10-04 02:00:59 -04002182
2183 local_set(&cpu_buffer->reader_page->write, 0);
Steven Rostedtabc9b562008-12-02 15:34:06 -05002184 local_set(&cpu_buffer->reader_page->page->commit, 0);
Steven Rostedtd7690412008-10-01 00:29:53 -04002185
2186 /* Make the reader page now replace the head */
2187 reader->list.prev->next = &cpu_buffer->reader_page->list;
2188 reader->list.next->prev = &cpu_buffer->reader_page->list;
2189
2190 /*
2191 * If the tail is on the reader, then we must set the head
2192 * to the inserted page, otherwise we set it one before.
2193 */
2194 cpu_buffer->head_page = cpu_buffer->reader_page;
2195
Steven Rostedtbf41a152008-10-04 02:00:59 -04002196 if (cpu_buffer->commit_page != reader)
Steven Rostedtd7690412008-10-01 00:29:53 -04002197 rb_inc_page(cpu_buffer, &cpu_buffer->head_page);
2198
2199 /* Finally update the reader page to the new head */
2200 cpu_buffer->reader_page = reader;
2201 rb_reset_reader_page(cpu_buffer);
2202
2203 goto again;
2204
2205 out:
Steven Rostedt3e03fb72008-11-06 00:09:43 -05002206 __raw_spin_unlock(&cpu_buffer->lock);
2207 local_irq_restore(flags);
Steven Rostedtd7690412008-10-01 00:29:53 -04002208
2209 return reader;
2210}
2211
2212static void rb_advance_reader(struct ring_buffer_per_cpu *cpu_buffer)
2213{
2214 struct ring_buffer_event *event;
2215 struct buffer_page *reader;
2216 unsigned length;
2217
2218 reader = rb_get_reader_page(cpu_buffer);
2219
2220 /* This function should not be called when buffer is empty */
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05002221 if (RB_WARN_ON(cpu_buffer, !reader))
2222 return;
Steven Rostedtd7690412008-10-01 00:29:53 -04002223
2224 event = rb_reader_event(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002225
Lai Jiangshan334d4162009-04-24 11:27:05 +08002226 if (event->type_len <= RINGBUF_TYPE_DATA_TYPE_LEN_MAX
2227 || rb_discarded_event(event))
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002228 cpu_buffer->entries--;
2229
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002230 rb_update_read_stamp(cpu_buffer, event);
2231
Steven Rostedtd7690412008-10-01 00:29:53 -04002232 length = rb_event_length(event);
Steven Rostedt6f807ac2008-10-04 02:00:58 -04002233 cpu_buffer->reader_page->read += length;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002234}
2235
2236static void rb_advance_iter(struct ring_buffer_iter *iter)
2237{
2238 struct ring_buffer *buffer;
2239 struct ring_buffer_per_cpu *cpu_buffer;
2240 struct ring_buffer_event *event;
2241 unsigned length;
2242
2243 cpu_buffer = iter->cpu_buffer;
2244 buffer = cpu_buffer->buffer;
2245
2246 /*
2247 * Check if we are at the end of the buffer.
2248 */
Steven Rostedtbf41a152008-10-04 02:00:59 -04002249 if (iter->head >= rb_page_size(iter->head_page)) {
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05002250 if (RB_WARN_ON(buffer,
2251 iter->head_page == cpu_buffer->commit_page))
2252 return;
Steven Rostedtd7690412008-10-01 00:29:53 -04002253 rb_inc_iter(iter);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002254 return;
2255 }
2256
2257 event = rb_iter_head_event(iter);
2258
2259 length = rb_event_length(event);
2260
2261 /*
2262 * This should not be called to advance the header if we are
2263 * at the tail of the buffer.
2264 */
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05002265 if (RB_WARN_ON(cpu_buffer,
Steven Rostedtf536aaf2008-11-10 23:07:30 -05002266 (iter->head_page == cpu_buffer->commit_page) &&
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05002267 (iter->head + length > rb_commit_index(cpu_buffer))))
2268 return;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002269
2270 rb_update_iter_read_stamp(iter, event);
2271
2272 iter->head += length;
2273
2274 /* check for end of page padding */
Steven Rostedtbf41a152008-10-04 02:00:59 -04002275 if ((iter->head >= rb_page_size(iter->head_page)) &&
2276 (iter->head_page != cpu_buffer->commit_page))
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002277 rb_advance_iter(iter);
2278}
2279
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002280static struct ring_buffer_event *
2281rb_buffer_peek(struct ring_buffer *buffer, int cpu, u64 *ts)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002282{
2283 struct ring_buffer_per_cpu *cpu_buffer;
2284 struct ring_buffer_event *event;
Steven Rostedtd7690412008-10-01 00:29:53 -04002285 struct buffer_page *reader;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002286 int nr_loops = 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002287
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002288 cpu_buffer = buffer->buffers[cpu];
2289
2290 again:
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002291 /*
2292 * We repeat when a timestamp is encountered. It is possible
2293 * to get multiple timestamps from an interrupt entering just
2294 * as one timestamp is about to be written. The max times
2295 * that this can happen is the number of nested interrupts we
2296 * can have. Nesting 10 deep of interrupts is clearly
2297 * an anomaly.
2298 */
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05002299 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 10))
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002300 return NULL;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002301
Steven Rostedtd7690412008-10-01 00:29:53 -04002302 reader = rb_get_reader_page(cpu_buffer);
2303 if (!reader)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002304 return NULL;
2305
Steven Rostedtd7690412008-10-01 00:29:53 -04002306 event = rb_reader_event(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002307
Lai Jiangshan334d4162009-04-24 11:27:05 +08002308 switch (event->type_len) {
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002309 case RINGBUF_TYPE_PADDING:
Tom Zanussi2d622712009-03-22 03:30:49 -05002310 if (rb_null_event(event))
2311 RB_WARN_ON(cpu_buffer, 1);
2312 /*
2313 * Because the writer could be discarding every
2314 * event it creates (which would probably be bad)
2315 * if we were to go back to "again" then we may never
2316 * catch up, and will trigger the warn on, or lock
2317 * the box. Return the padding, and we will release
2318 * the current locks, and try again.
2319 */
Steven Rostedtd7690412008-10-01 00:29:53 -04002320 rb_advance_reader(cpu_buffer);
Tom Zanussi2d622712009-03-22 03:30:49 -05002321 return event;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002322
2323 case RINGBUF_TYPE_TIME_EXTEND:
2324 /* Internal data, OK to advance */
Steven Rostedtd7690412008-10-01 00:29:53 -04002325 rb_advance_reader(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002326 goto again;
2327
2328 case RINGBUF_TYPE_TIME_STAMP:
2329 /* FIXME: not implemented */
Steven Rostedtd7690412008-10-01 00:29:53 -04002330 rb_advance_reader(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002331 goto again;
2332
2333 case RINGBUF_TYPE_DATA:
2334 if (ts) {
2335 *ts = cpu_buffer->read_stamp + event->time_delta;
Steven Rostedt37886f62009-03-17 17:22:06 -04002336 ring_buffer_normalize_time_stamp(buffer,
2337 cpu_buffer->cpu, ts);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002338 }
2339 return event;
2340
2341 default:
2342 BUG();
2343 }
2344
2345 return NULL;
2346}
Robert Richterc4f50182008-12-11 16:49:22 +01002347EXPORT_SYMBOL_GPL(ring_buffer_peek);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002348
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002349static struct ring_buffer_event *
2350rb_iter_peek(struct ring_buffer_iter *iter, u64 *ts)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002351{
2352 struct ring_buffer *buffer;
2353 struct ring_buffer_per_cpu *cpu_buffer;
2354 struct ring_buffer_event *event;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002355 int nr_loops = 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002356
2357 if (ring_buffer_iter_empty(iter))
2358 return NULL;
2359
2360 cpu_buffer = iter->cpu_buffer;
2361 buffer = cpu_buffer->buffer;
2362
2363 again:
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002364 /*
2365 * We repeat when a timestamp is encountered. It is possible
2366 * to get multiple timestamps from an interrupt entering just
2367 * as one timestamp is about to be written. The max times
2368 * that this can happen is the number of nested interrupts we
2369 * can have. Nesting 10 deep of interrupts is clearly
2370 * an anomaly.
2371 */
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05002372 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 10))
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002373 return NULL;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002374
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002375 if (rb_per_cpu_empty(cpu_buffer))
2376 return NULL;
2377
2378 event = rb_iter_head_event(iter);
2379
Lai Jiangshan334d4162009-04-24 11:27:05 +08002380 switch (event->type_len) {
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002381 case RINGBUF_TYPE_PADDING:
Tom Zanussi2d622712009-03-22 03:30:49 -05002382 if (rb_null_event(event)) {
2383 rb_inc_iter(iter);
2384 goto again;
2385 }
2386 rb_advance_iter(iter);
2387 return event;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002388
2389 case RINGBUF_TYPE_TIME_EXTEND:
2390 /* Internal data, OK to advance */
2391 rb_advance_iter(iter);
2392 goto again;
2393
2394 case RINGBUF_TYPE_TIME_STAMP:
2395 /* FIXME: not implemented */
2396 rb_advance_iter(iter);
2397 goto again;
2398
2399 case RINGBUF_TYPE_DATA:
2400 if (ts) {
2401 *ts = iter->read_stamp + event->time_delta;
Steven Rostedt37886f62009-03-17 17:22:06 -04002402 ring_buffer_normalize_time_stamp(buffer,
2403 cpu_buffer->cpu, ts);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002404 }
2405 return event;
2406
2407 default:
2408 BUG();
2409 }
2410
2411 return NULL;
2412}
Robert Richterc4f50182008-12-11 16:49:22 +01002413EXPORT_SYMBOL_GPL(ring_buffer_iter_peek);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002414
2415/**
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002416 * ring_buffer_peek - peek at the next event to be read
2417 * @buffer: The ring buffer to read
2418 * @cpu: The cpu to peak at
2419 * @ts: The timestamp counter of this event.
2420 *
2421 * This will return the event that will be read next, but does
2422 * not consume the data.
2423 */
2424struct ring_buffer_event *
2425ring_buffer_peek(struct ring_buffer *buffer, int cpu, u64 *ts)
2426{
2427 struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
Steven Rostedt8aabee52009-03-12 13:13:49 -04002428 struct ring_buffer_event *event;
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002429 unsigned long flags;
2430
Steven Rostedt554f7862009-03-11 22:00:13 -04002431 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04002432 return NULL;
Steven Rostedt554f7862009-03-11 22:00:13 -04002433
Tom Zanussi2d622712009-03-22 03:30:49 -05002434 again:
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002435 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
2436 event = rb_buffer_peek(buffer, cpu, ts);
2437 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
2438
Lai Jiangshan334d4162009-04-24 11:27:05 +08002439 if (event && event->type_len == RINGBUF_TYPE_PADDING) {
Tom Zanussi2d622712009-03-22 03:30:49 -05002440 cpu_relax();
2441 goto again;
2442 }
2443
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002444 return event;
2445}
2446
2447/**
2448 * ring_buffer_iter_peek - peek at the next event to be read
2449 * @iter: The ring buffer iterator
2450 * @ts: The timestamp counter of this event.
2451 *
2452 * This will return the event that will be read next, but does
2453 * not increment the iterator.
2454 */
2455struct ring_buffer_event *
2456ring_buffer_iter_peek(struct ring_buffer_iter *iter, u64 *ts)
2457{
2458 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
2459 struct ring_buffer_event *event;
2460 unsigned long flags;
2461
Tom Zanussi2d622712009-03-22 03:30:49 -05002462 again:
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002463 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
2464 event = rb_iter_peek(iter, ts);
2465 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
2466
Lai Jiangshan334d4162009-04-24 11:27:05 +08002467 if (event && event->type_len == RINGBUF_TYPE_PADDING) {
Tom Zanussi2d622712009-03-22 03:30:49 -05002468 cpu_relax();
2469 goto again;
2470 }
2471
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002472 return event;
2473}
2474
2475/**
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002476 * ring_buffer_consume - return an event and consume it
2477 * @buffer: The ring buffer to get the next event from
2478 *
2479 * Returns the next event in the ring buffer, and that event is consumed.
2480 * Meaning, that sequential reads will keep returning a different event,
2481 * and eventually empty the ring buffer if the producer is slower.
2482 */
2483struct ring_buffer_event *
2484ring_buffer_consume(struct ring_buffer *buffer, int cpu, u64 *ts)
2485{
Steven Rostedt554f7862009-03-11 22:00:13 -04002486 struct ring_buffer_per_cpu *cpu_buffer;
2487 struct ring_buffer_event *event = NULL;
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002488 unsigned long flags;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002489
Tom Zanussi2d622712009-03-22 03:30:49 -05002490 again:
Steven Rostedt554f7862009-03-11 22:00:13 -04002491 /* might be called in atomic */
2492 preempt_disable();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002493
Steven Rostedt554f7862009-03-11 22:00:13 -04002494 if (!cpumask_test_cpu(cpu, buffer->cpumask))
2495 goto out;
2496
2497 cpu_buffer = buffer->buffers[cpu];
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002498 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002499
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002500 event = rb_buffer_peek(buffer, cpu, ts);
2501 if (!event)
Steven Rostedt554f7862009-03-11 22:00:13 -04002502 goto out_unlock;
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002503
Steven Rostedtd7690412008-10-01 00:29:53 -04002504 rb_advance_reader(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002505
Steven Rostedt554f7862009-03-11 22:00:13 -04002506 out_unlock:
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002507 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
2508
Steven Rostedt554f7862009-03-11 22:00:13 -04002509 out:
2510 preempt_enable();
2511
Lai Jiangshan334d4162009-04-24 11:27:05 +08002512 if (event && event->type_len == RINGBUF_TYPE_PADDING) {
Tom Zanussi2d622712009-03-22 03:30:49 -05002513 cpu_relax();
2514 goto again;
2515 }
2516
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002517 return event;
2518}
Robert Richterc4f50182008-12-11 16:49:22 +01002519EXPORT_SYMBOL_GPL(ring_buffer_consume);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002520
2521/**
2522 * ring_buffer_read_start - start a non consuming read of the buffer
2523 * @buffer: The ring buffer to read from
2524 * @cpu: The cpu buffer to iterate over
2525 *
2526 * This starts up an iteration through the buffer. It also disables
2527 * the recording to the buffer until the reading is finished.
2528 * This prevents the reading from being corrupted. This is not
2529 * a consuming read, so a producer is not expected.
2530 *
2531 * Must be paired with ring_buffer_finish.
2532 */
2533struct ring_buffer_iter *
2534ring_buffer_read_start(struct ring_buffer *buffer, int cpu)
2535{
2536 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedt8aabee52009-03-12 13:13:49 -04002537 struct ring_buffer_iter *iter;
Steven Rostedtd7690412008-10-01 00:29:53 -04002538 unsigned long flags;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002539
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302540 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04002541 return NULL;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002542
2543 iter = kmalloc(sizeof(*iter), GFP_KERNEL);
2544 if (!iter)
Steven Rostedt8aabee52009-03-12 13:13:49 -04002545 return NULL;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002546
2547 cpu_buffer = buffer->buffers[cpu];
2548
2549 iter->cpu_buffer = cpu_buffer;
2550
2551 atomic_inc(&cpu_buffer->record_disabled);
2552 synchronize_sched();
2553
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002554 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
Steven Rostedt3e03fb72008-11-06 00:09:43 -05002555 __raw_spin_lock(&cpu_buffer->lock);
Steven Rostedt642edba2008-11-12 00:01:26 -05002556 rb_iter_reset(iter);
Steven Rostedt3e03fb72008-11-06 00:09:43 -05002557 __raw_spin_unlock(&cpu_buffer->lock);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002558 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002559
2560 return iter;
2561}
Robert Richterc4f50182008-12-11 16:49:22 +01002562EXPORT_SYMBOL_GPL(ring_buffer_read_start);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002563
2564/**
2565 * ring_buffer_finish - finish reading the iterator of the buffer
2566 * @iter: The iterator retrieved by ring_buffer_start
2567 *
2568 * This re-enables the recording to the buffer, and frees the
2569 * iterator.
2570 */
2571void
2572ring_buffer_read_finish(struct ring_buffer_iter *iter)
2573{
2574 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
2575
2576 atomic_dec(&cpu_buffer->record_disabled);
2577 kfree(iter);
2578}
Robert Richterc4f50182008-12-11 16:49:22 +01002579EXPORT_SYMBOL_GPL(ring_buffer_read_finish);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002580
2581/**
2582 * ring_buffer_read - read the next item in the ring buffer by the iterator
2583 * @iter: The ring buffer iterator
2584 * @ts: The time stamp of the event read.
2585 *
2586 * This reads the next event in the ring buffer and increments the iterator.
2587 */
2588struct ring_buffer_event *
2589ring_buffer_read(struct ring_buffer_iter *iter, u64 *ts)
2590{
2591 struct ring_buffer_event *event;
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002592 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
2593 unsigned long flags;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002594
Tom Zanussi2d622712009-03-22 03:30:49 -05002595 again:
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002596 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
2597 event = rb_iter_peek(iter, ts);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002598 if (!event)
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002599 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002600
2601 rb_advance_iter(iter);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002602 out:
2603 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002604
Lai Jiangshan334d4162009-04-24 11:27:05 +08002605 if (event && event->type_len == RINGBUF_TYPE_PADDING) {
Tom Zanussi2d622712009-03-22 03:30:49 -05002606 cpu_relax();
2607 goto again;
2608 }
2609
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002610 return event;
2611}
Robert Richterc4f50182008-12-11 16:49:22 +01002612EXPORT_SYMBOL_GPL(ring_buffer_read);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002613
2614/**
2615 * ring_buffer_size - return the size of the ring buffer (in bytes)
2616 * @buffer: The ring buffer.
2617 */
2618unsigned long ring_buffer_size(struct ring_buffer *buffer)
2619{
2620 return BUF_PAGE_SIZE * buffer->pages;
2621}
Robert Richterc4f50182008-12-11 16:49:22 +01002622EXPORT_SYMBOL_GPL(ring_buffer_size);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002623
2624static void
2625rb_reset_cpu(struct ring_buffer_per_cpu *cpu_buffer)
2626{
2627 cpu_buffer->head_page
2628 = list_entry(cpu_buffer->pages.next, struct buffer_page, list);
Steven Rostedtbf41a152008-10-04 02:00:59 -04002629 local_set(&cpu_buffer->head_page->write, 0);
Steven Rostedtabc9b562008-12-02 15:34:06 -05002630 local_set(&cpu_buffer->head_page->page->commit, 0);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002631
Steven Rostedt6f807ac2008-10-04 02:00:58 -04002632 cpu_buffer->head_page->read = 0;
Steven Rostedtbf41a152008-10-04 02:00:59 -04002633
2634 cpu_buffer->tail_page = cpu_buffer->head_page;
2635 cpu_buffer->commit_page = cpu_buffer->head_page;
2636
2637 INIT_LIST_HEAD(&cpu_buffer->reader_page->list);
2638 local_set(&cpu_buffer->reader_page->write, 0);
Steven Rostedtabc9b562008-12-02 15:34:06 -05002639 local_set(&cpu_buffer->reader_page->page->commit, 0);
Steven Rostedt6f807ac2008-10-04 02:00:58 -04002640 cpu_buffer->reader_page->read = 0;
Steven Rostedtd7690412008-10-01 00:29:53 -04002641
Steven Rostedtf0d2c682009-04-29 13:43:37 -04002642 cpu_buffer->nmi_dropped = 0;
2643 cpu_buffer->commit_overrun = 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002644 cpu_buffer->overrun = 0;
2645 cpu_buffer->entries = 0;
Steven Rostedt69507c02009-01-21 18:45:57 -05002646
2647 cpu_buffer->write_stamp = 0;
2648 cpu_buffer->read_stamp = 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002649}
2650
2651/**
2652 * ring_buffer_reset_cpu - reset a ring buffer per CPU buffer
2653 * @buffer: The ring buffer to reset a per cpu buffer of
2654 * @cpu: The CPU buffer to be reset
2655 */
2656void ring_buffer_reset_cpu(struct ring_buffer *buffer, int cpu)
2657{
2658 struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
2659 unsigned long flags;
2660
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302661 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04002662 return;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002663
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002664 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
2665
Steven Rostedt3e03fb72008-11-06 00:09:43 -05002666 __raw_spin_lock(&cpu_buffer->lock);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002667
2668 rb_reset_cpu(cpu_buffer);
2669
Steven Rostedt3e03fb72008-11-06 00:09:43 -05002670 __raw_spin_unlock(&cpu_buffer->lock);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002671
2672 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002673}
Robert Richterc4f50182008-12-11 16:49:22 +01002674EXPORT_SYMBOL_GPL(ring_buffer_reset_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002675
2676/**
2677 * ring_buffer_reset - reset a ring buffer
2678 * @buffer: The ring buffer to reset all cpu buffers
2679 */
2680void ring_buffer_reset(struct ring_buffer *buffer)
2681{
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002682 int cpu;
2683
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002684 for_each_buffer_cpu(buffer, cpu)
Steven Rostedtd7690412008-10-01 00:29:53 -04002685 ring_buffer_reset_cpu(buffer, cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002686}
Robert Richterc4f50182008-12-11 16:49:22 +01002687EXPORT_SYMBOL_GPL(ring_buffer_reset);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002688
2689/**
2690 * rind_buffer_empty - is the ring buffer empty?
2691 * @buffer: The ring buffer to test
2692 */
2693int ring_buffer_empty(struct ring_buffer *buffer)
2694{
2695 struct ring_buffer_per_cpu *cpu_buffer;
2696 int cpu;
2697
2698 /* yes this is racy, but if you don't like the race, lock the buffer */
2699 for_each_buffer_cpu(buffer, cpu) {
2700 cpu_buffer = buffer->buffers[cpu];
2701 if (!rb_per_cpu_empty(cpu_buffer))
2702 return 0;
2703 }
Steven Rostedt554f7862009-03-11 22:00:13 -04002704
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002705 return 1;
2706}
Robert Richterc4f50182008-12-11 16:49:22 +01002707EXPORT_SYMBOL_GPL(ring_buffer_empty);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002708
2709/**
2710 * ring_buffer_empty_cpu - is a cpu buffer of a ring buffer empty?
2711 * @buffer: The ring buffer
2712 * @cpu: The CPU buffer to test
2713 */
2714int ring_buffer_empty_cpu(struct ring_buffer *buffer, int cpu)
2715{
2716 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedt8aabee52009-03-12 13:13:49 -04002717 int ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002718
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302719 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04002720 return 1;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002721
2722 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt554f7862009-03-11 22:00:13 -04002723 ret = rb_per_cpu_empty(cpu_buffer);
2724
Steven Rostedt554f7862009-03-11 22:00:13 -04002725
2726 return ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002727}
Robert Richterc4f50182008-12-11 16:49:22 +01002728EXPORT_SYMBOL_GPL(ring_buffer_empty_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002729
2730/**
2731 * ring_buffer_swap_cpu - swap a CPU buffer between two ring buffers
2732 * @buffer_a: One buffer to swap with
2733 * @buffer_b: The other buffer to swap with
2734 *
2735 * This function is useful for tracers that want to take a "snapshot"
2736 * of a CPU buffer and has another back up buffer lying around.
2737 * it is expected that the tracer handles the cpu buffer not being
2738 * used at the moment.
2739 */
2740int ring_buffer_swap_cpu(struct ring_buffer *buffer_a,
2741 struct ring_buffer *buffer_b, int cpu)
2742{
2743 struct ring_buffer_per_cpu *cpu_buffer_a;
2744 struct ring_buffer_per_cpu *cpu_buffer_b;
Steven Rostedt554f7862009-03-11 22:00:13 -04002745 int ret = -EINVAL;
2746
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302747 if (!cpumask_test_cpu(cpu, buffer_a->cpumask) ||
2748 !cpumask_test_cpu(cpu, buffer_b->cpumask))
Steven Rostedt554f7862009-03-11 22:00:13 -04002749 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002750
2751 /* At least make sure the two buffers are somewhat the same */
Lai Jiangshan6d102bc2008-12-17 17:48:23 +08002752 if (buffer_a->pages != buffer_b->pages)
Steven Rostedt554f7862009-03-11 22:00:13 -04002753 goto out;
2754
2755 ret = -EAGAIN;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002756
Steven Rostedt97b17ef2009-01-21 15:24:56 -05002757 if (ring_buffer_flags != RB_BUFFERS_ON)
Steven Rostedt554f7862009-03-11 22:00:13 -04002758 goto out;
Steven Rostedt97b17ef2009-01-21 15:24:56 -05002759
2760 if (atomic_read(&buffer_a->record_disabled))
Steven Rostedt554f7862009-03-11 22:00:13 -04002761 goto out;
Steven Rostedt97b17ef2009-01-21 15:24:56 -05002762
2763 if (atomic_read(&buffer_b->record_disabled))
Steven Rostedt554f7862009-03-11 22:00:13 -04002764 goto out;
Steven Rostedt97b17ef2009-01-21 15:24:56 -05002765
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002766 cpu_buffer_a = buffer_a->buffers[cpu];
2767 cpu_buffer_b = buffer_b->buffers[cpu];
2768
Steven Rostedt97b17ef2009-01-21 15:24:56 -05002769 if (atomic_read(&cpu_buffer_a->record_disabled))
Steven Rostedt554f7862009-03-11 22:00:13 -04002770 goto out;
Steven Rostedt97b17ef2009-01-21 15:24:56 -05002771
2772 if (atomic_read(&cpu_buffer_b->record_disabled))
Steven Rostedt554f7862009-03-11 22:00:13 -04002773 goto out;
Steven Rostedt97b17ef2009-01-21 15:24:56 -05002774
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002775 /*
2776 * We can't do a synchronize_sched here because this
2777 * function can be called in atomic context.
2778 * Normally this will be called from the same CPU as cpu.
2779 * If not it's up to the caller to protect this.
2780 */
2781 atomic_inc(&cpu_buffer_a->record_disabled);
2782 atomic_inc(&cpu_buffer_b->record_disabled);
2783
2784 buffer_a->buffers[cpu] = cpu_buffer_b;
2785 buffer_b->buffers[cpu] = cpu_buffer_a;
2786
2787 cpu_buffer_b->buffer = buffer_a;
2788 cpu_buffer_a->buffer = buffer_b;
2789
2790 atomic_dec(&cpu_buffer_a->record_disabled);
2791 atomic_dec(&cpu_buffer_b->record_disabled);
2792
Steven Rostedt554f7862009-03-11 22:00:13 -04002793 ret = 0;
2794out:
Steven Rostedt554f7862009-03-11 22:00:13 -04002795 return ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002796}
Robert Richterc4f50182008-12-11 16:49:22 +01002797EXPORT_SYMBOL_GPL(ring_buffer_swap_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002798
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002799static void rb_remove_entries(struct ring_buffer_per_cpu *cpu_buffer,
Lai Jiangshan667d2412009-02-09 14:21:17 +08002800 struct buffer_data_page *bpage,
2801 unsigned int offset)
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002802{
2803 struct ring_buffer_event *event;
2804 unsigned long head;
2805
2806 __raw_spin_lock(&cpu_buffer->lock);
Lai Jiangshan667d2412009-02-09 14:21:17 +08002807 for (head = offset; head < local_read(&bpage->commit);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002808 head += rb_event_length(event)) {
2809
Steven Rostedt044fa782008-12-02 23:50:03 -05002810 event = __rb_data_page_index(bpage, head);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002811 if (RB_WARN_ON(cpu_buffer, rb_null_event(event)))
2812 return;
2813 /* Only count data entries */
Lai Jiangshan334d4162009-04-24 11:27:05 +08002814 if (event->type_len > RINGBUF_TYPE_DATA_TYPE_LEN_MAX)
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002815 continue;
2816 cpu_buffer->entries--;
2817 }
2818 __raw_spin_unlock(&cpu_buffer->lock);
2819}
2820
2821/**
2822 * ring_buffer_alloc_read_page - allocate a page to read from buffer
2823 * @buffer: the buffer to allocate for.
2824 *
2825 * This function is used in conjunction with ring_buffer_read_page.
2826 * When reading a full page from the ring buffer, these functions
2827 * can be used to speed up the process. The calling function should
2828 * allocate a few pages first with this function. Then when it
2829 * needs to get pages from the ring buffer, it passes the result
2830 * of this function into ring_buffer_read_page, which will swap
2831 * the page that was allocated, with the read page of the buffer.
2832 *
2833 * Returns:
2834 * The page allocated, or NULL on error.
2835 */
2836void *ring_buffer_alloc_read_page(struct ring_buffer *buffer)
2837{
Steven Rostedt044fa782008-12-02 23:50:03 -05002838 struct buffer_data_page *bpage;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05002839 unsigned long addr;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002840
2841 addr = __get_free_page(GFP_KERNEL);
2842 if (!addr)
2843 return NULL;
2844
Steven Rostedt044fa782008-12-02 23:50:03 -05002845 bpage = (void *)addr;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002846
Steven Rostedtef7a4a12009-03-03 00:27:49 -05002847 rb_init_page(bpage);
2848
Steven Rostedt044fa782008-12-02 23:50:03 -05002849 return bpage;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002850}
Steven Rostedtd6ce96d2009-05-05 01:15:24 -04002851EXPORT_SYMBOL_GPL(ring_buffer_alloc_read_page);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002852
2853/**
2854 * ring_buffer_free_read_page - free an allocated read page
2855 * @buffer: the buffer the page was allocate for
2856 * @data: the page to free
2857 *
2858 * Free a page allocated from ring_buffer_alloc_read_page.
2859 */
2860void ring_buffer_free_read_page(struct ring_buffer *buffer, void *data)
2861{
2862 free_page((unsigned long)data);
2863}
Steven Rostedtd6ce96d2009-05-05 01:15:24 -04002864EXPORT_SYMBOL_GPL(ring_buffer_free_read_page);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002865
2866/**
2867 * ring_buffer_read_page - extract a page from the ring buffer
2868 * @buffer: buffer to extract from
2869 * @data_page: the page to use allocated from ring_buffer_alloc_read_page
Steven Rostedtef7a4a12009-03-03 00:27:49 -05002870 * @len: amount to extract
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002871 * @cpu: the cpu of the buffer to extract
2872 * @full: should the extraction only happen when the page is full.
2873 *
2874 * This function will pull out a page from the ring buffer and consume it.
2875 * @data_page must be the address of the variable that was returned
2876 * from ring_buffer_alloc_read_page. This is because the page might be used
2877 * to swap with a page in the ring buffer.
2878 *
2879 * for example:
Lai Jiangshanb85fa012009-02-09 14:21:14 +08002880 * rpage = ring_buffer_alloc_read_page(buffer);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002881 * if (!rpage)
2882 * return error;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05002883 * ret = ring_buffer_read_page(buffer, &rpage, len, cpu, 0);
Lai Jiangshan667d2412009-02-09 14:21:17 +08002884 * if (ret >= 0)
2885 * process_page(rpage, ret);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002886 *
2887 * When @full is set, the function will not return true unless
2888 * the writer is off the reader page.
2889 *
2890 * Note: it is up to the calling functions to handle sleeps and wakeups.
2891 * The ring buffer can be used anywhere in the kernel and can not
2892 * blindly call wake_up. The layer that uses the ring buffer must be
2893 * responsible for that.
2894 *
2895 * Returns:
Lai Jiangshan667d2412009-02-09 14:21:17 +08002896 * >=0 if data has been transferred, returns the offset of consumed data.
2897 * <0 if no data has been transferred.
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002898 */
2899int ring_buffer_read_page(struct ring_buffer *buffer,
Steven Rostedtef7a4a12009-03-03 00:27:49 -05002900 void **data_page, size_t len, int cpu, int full)
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002901{
2902 struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
2903 struct ring_buffer_event *event;
Steven Rostedt044fa782008-12-02 23:50:03 -05002904 struct buffer_data_page *bpage;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05002905 struct buffer_page *reader;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002906 unsigned long flags;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05002907 unsigned int commit;
Lai Jiangshan667d2412009-02-09 14:21:17 +08002908 unsigned int read;
Steven Rostedt4f3640f2009-03-03 23:52:42 -05002909 u64 save_timestamp;
Lai Jiangshan667d2412009-02-09 14:21:17 +08002910 int ret = -1;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002911
Steven Rostedt554f7862009-03-11 22:00:13 -04002912 if (!cpumask_test_cpu(cpu, buffer->cpumask))
2913 goto out;
2914
Steven Rostedt474d32b2009-03-03 19:51:40 -05002915 /*
2916 * If len is not big enough to hold the page header, then
2917 * we can not copy anything.
2918 */
2919 if (len <= BUF_PAGE_HDR_SIZE)
Steven Rostedt554f7862009-03-11 22:00:13 -04002920 goto out;
Steven Rostedt474d32b2009-03-03 19:51:40 -05002921
2922 len -= BUF_PAGE_HDR_SIZE;
2923
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002924 if (!data_page)
Steven Rostedt554f7862009-03-11 22:00:13 -04002925 goto out;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002926
Steven Rostedt044fa782008-12-02 23:50:03 -05002927 bpage = *data_page;
2928 if (!bpage)
Steven Rostedt554f7862009-03-11 22:00:13 -04002929 goto out;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002930
2931 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
2932
Steven Rostedtef7a4a12009-03-03 00:27:49 -05002933 reader = rb_get_reader_page(cpu_buffer);
2934 if (!reader)
Steven Rostedt554f7862009-03-11 22:00:13 -04002935 goto out_unlock;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002936
Steven Rostedtef7a4a12009-03-03 00:27:49 -05002937 event = rb_reader_event(cpu_buffer);
Lai Jiangshan667d2412009-02-09 14:21:17 +08002938
Steven Rostedtef7a4a12009-03-03 00:27:49 -05002939 read = reader->read;
2940 commit = rb_page_commit(reader);
2941
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002942 /*
Steven Rostedt474d32b2009-03-03 19:51:40 -05002943 * If this page has been partially read or
2944 * if len is not big enough to read the rest of the page or
2945 * a writer is still on the page, then
2946 * we must copy the data from the page to the buffer.
2947 * Otherwise, we can simply swap the page with the one passed in.
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002948 */
Steven Rostedt474d32b2009-03-03 19:51:40 -05002949 if (read || (len < (commit - read)) ||
Steven Rostedtef7a4a12009-03-03 00:27:49 -05002950 cpu_buffer->reader_page == cpu_buffer->commit_page) {
Lai Jiangshan667d2412009-02-09 14:21:17 +08002951 struct buffer_data_page *rpage = cpu_buffer->reader_page->page;
Steven Rostedt474d32b2009-03-03 19:51:40 -05002952 unsigned int rpos = read;
2953 unsigned int pos = 0;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05002954 unsigned int size;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002955
2956 if (full)
Steven Rostedt554f7862009-03-11 22:00:13 -04002957 goto out_unlock;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002958
Steven Rostedtef7a4a12009-03-03 00:27:49 -05002959 if (len > (commit - read))
2960 len = (commit - read);
2961
2962 size = rb_event_length(event);
2963
2964 if (len < size)
Steven Rostedt554f7862009-03-11 22:00:13 -04002965 goto out_unlock;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05002966
Steven Rostedt4f3640f2009-03-03 23:52:42 -05002967 /* save the current timestamp, since the user will need it */
2968 save_timestamp = cpu_buffer->read_stamp;
2969
Steven Rostedtef7a4a12009-03-03 00:27:49 -05002970 /* Need to copy one event at a time */
2971 do {
Steven Rostedt474d32b2009-03-03 19:51:40 -05002972 memcpy(bpage->data + pos, rpage->data + rpos, size);
Steven Rostedtef7a4a12009-03-03 00:27:49 -05002973
2974 len -= size;
2975
2976 rb_advance_reader(cpu_buffer);
Steven Rostedt474d32b2009-03-03 19:51:40 -05002977 rpos = reader->read;
2978 pos += size;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05002979
2980 event = rb_reader_event(cpu_buffer);
2981 size = rb_event_length(event);
2982 } while (len > size);
Lai Jiangshan667d2412009-02-09 14:21:17 +08002983
2984 /* update bpage */
Steven Rostedtef7a4a12009-03-03 00:27:49 -05002985 local_set(&bpage->commit, pos);
Steven Rostedt4f3640f2009-03-03 23:52:42 -05002986 bpage->time_stamp = save_timestamp;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05002987
Steven Rostedt474d32b2009-03-03 19:51:40 -05002988 /* we copied everything to the beginning */
2989 read = 0;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002990 } else {
2991 /* swap the pages */
Steven Rostedt044fa782008-12-02 23:50:03 -05002992 rb_init_page(bpage);
Steven Rostedtef7a4a12009-03-03 00:27:49 -05002993 bpage = reader->page;
2994 reader->page = *data_page;
2995 local_set(&reader->write, 0);
2996 reader->read = 0;
Steven Rostedt044fa782008-12-02 23:50:03 -05002997 *data_page = bpage;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05002998
2999 /* update the entry counter */
3000 rb_remove_entries(cpu_buffer, bpage, read);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003001 }
Lai Jiangshan667d2412009-02-09 14:21:17 +08003002 ret = read;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003003
Steven Rostedt554f7862009-03-11 22:00:13 -04003004 out_unlock:
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003005 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
3006
Steven Rostedt554f7862009-03-11 22:00:13 -04003007 out:
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003008 return ret;
3009}
Steven Rostedtd6ce96d2009-05-05 01:15:24 -04003010EXPORT_SYMBOL_GPL(ring_buffer_read_page);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003011
Steven Rostedta3583242008-11-11 15:01:42 -05003012static ssize_t
3013rb_simple_read(struct file *filp, char __user *ubuf,
3014 size_t cnt, loff_t *ppos)
3015{
Hannes Eder5e398412009-02-10 19:44:34 +01003016 unsigned long *p = filp->private_data;
Steven Rostedta3583242008-11-11 15:01:42 -05003017 char buf[64];
3018 int r;
3019
Steven Rostedt033601a2008-11-21 12:41:55 -05003020 if (test_bit(RB_BUFFERS_DISABLED_BIT, p))
3021 r = sprintf(buf, "permanently disabled\n");
3022 else
3023 r = sprintf(buf, "%d\n", test_bit(RB_BUFFERS_ON_BIT, p));
Steven Rostedta3583242008-11-11 15:01:42 -05003024
3025 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
3026}
3027
3028static ssize_t
3029rb_simple_write(struct file *filp, const char __user *ubuf,
3030 size_t cnt, loff_t *ppos)
3031{
Hannes Eder5e398412009-02-10 19:44:34 +01003032 unsigned long *p = filp->private_data;
Steven Rostedta3583242008-11-11 15:01:42 -05003033 char buf[64];
Hannes Eder5e398412009-02-10 19:44:34 +01003034 unsigned long val;
Steven Rostedta3583242008-11-11 15:01:42 -05003035 int ret;
3036
3037 if (cnt >= sizeof(buf))
3038 return -EINVAL;
3039
3040 if (copy_from_user(&buf, ubuf, cnt))
3041 return -EFAULT;
3042
3043 buf[cnt] = 0;
3044
3045 ret = strict_strtoul(buf, 10, &val);
3046 if (ret < 0)
3047 return ret;
3048
Steven Rostedt033601a2008-11-21 12:41:55 -05003049 if (val)
3050 set_bit(RB_BUFFERS_ON_BIT, p);
3051 else
3052 clear_bit(RB_BUFFERS_ON_BIT, p);
Steven Rostedta3583242008-11-11 15:01:42 -05003053
3054 (*ppos)++;
3055
3056 return cnt;
3057}
3058
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003059static const struct file_operations rb_simple_fops = {
Steven Rostedta3583242008-11-11 15:01:42 -05003060 .open = tracing_open_generic,
3061 .read = rb_simple_read,
3062 .write = rb_simple_write,
3063};
3064
3065
3066static __init int rb_init_debugfs(void)
3067{
3068 struct dentry *d_tracer;
Steven Rostedta3583242008-11-11 15:01:42 -05003069
3070 d_tracer = tracing_init_dentry();
3071
Frederic Weisbecker5452af62009-03-27 00:25:38 +01003072 trace_create_file("tracing_on", 0644, d_tracer,
3073 &ring_buffer_flags, &rb_simple_fops);
Steven Rostedta3583242008-11-11 15:01:42 -05003074
3075 return 0;
3076}
3077
3078fs_initcall(rb_init_debugfs);
Steven Rostedt554f7862009-03-11 22:00:13 -04003079
Steven Rostedt59222ef2009-03-12 11:46:03 -04003080#ifdef CONFIG_HOTPLUG_CPU
Frederic Weisbecker09c9e842009-03-21 04:33:36 +01003081static int rb_cpu_notify(struct notifier_block *self,
3082 unsigned long action, void *hcpu)
Steven Rostedt554f7862009-03-11 22:00:13 -04003083{
3084 struct ring_buffer *buffer =
3085 container_of(self, struct ring_buffer, cpu_notify);
3086 long cpu = (long)hcpu;
3087
3088 switch (action) {
3089 case CPU_UP_PREPARE:
3090 case CPU_UP_PREPARE_FROZEN:
3091 if (cpu_isset(cpu, *buffer->cpumask))
3092 return NOTIFY_OK;
3093
3094 buffer->buffers[cpu] =
3095 rb_allocate_cpu_buffer(buffer, cpu);
3096 if (!buffer->buffers[cpu]) {
3097 WARN(1, "failed to allocate ring buffer on CPU %ld\n",
3098 cpu);
3099 return NOTIFY_OK;
3100 }
3101 smp_wmb();
3102 cpu_set(cpu, *buffer->cpumask);
3103 break;
3104 case CPU_DOWN_PREPARE:
3105 case CPU_DOWN_PREPARE_FROZEN:
3106 /*
3107 * Do nothing.
3108 * If we were to free the buffer, then the user would
3109 * lose any trace that was in the buffer.
3110 */
3111 break;
3112 default:
3113 break;
3114 }
3115 return NOTIFY_OK;
3116}
3117#endif