blob: c792ea893b016b7bbce47e9f736419fd7b7815fd [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;
Steven Rostedte4906ef2009-04-30 20:49:44 -0400408 unsigned long read;
409 local_t entries;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400410 u64 write_stamp;
411 u64 read_stamp;
412 atomic_t record_disabled;
413};
414
415struct ring_buffer {
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400416 unsigned pages;
417 unsigned flags;
418 int cpus;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400419 atomic_t record_disabled;
Arnaldo Carvalho de Melo00f62f62009-02-09 17:04:06 -0200420 cpumask_var_t cpumask;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400421
422 struct mutex mutex;
423
424 struct ring_buffer_per_cpu **buffers;
Steven Rostedt554f7862009-03-11 22:00:13 -0400425
Steven Rostedt59222ef2009-03-12 11:46:03 -0400426#ifdef CONFIG_HOTPLUG_CPU
Steven Rostedt554f7862009-03-11 22:00:13 -0400427 struct notifier_block cpu_notify;
428#endif
Steven Rostedt37886f62009-03-17 17:22:06 -0400429 u64 (*clock)(void);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400430};
431
432struct ring_buffer_iter {
433 struct ring_buffer_per_cpu *cpu_buffer;
434 unsigned long head;
435 struct buffer_page *head_page;
436 u64 read_stamp;
437};
438
Steven Rostedtf536aaf2008-11-10 23:07:30 -0500439/* buffer may be either ring_buffer or ring_buffer_per_cpu */
Steven Rostedtbf41a152008-10-04 02:00:59 -0400440#define RB_WARN_ON(buffer, cond) \
Steven Rostedt3e89c7b2008-11-11 15:28:41 -0500441 ({ \
442 int _____ret = unlikely(cond); \
443 if (_____ret) { \
Steven Rostedtbf41a152008-10-04 02:00:59 -0400444 atomic_inc(&buffer->record_disabled); \
445 WARN_ON(1); \
446 } \
Steven Rostedt3e89c7b2008-11-11 15:28:41 -0500447 _____ret; \
448 })
Steven Rostedtf536aaf2008-11-10 23:07:30 -0500449
Steven Rostedt37886f62009-03-17 17:22:06 -0400450/* Up this if you want to test the TIME_EXTENTS and normalization */
451#define DEBUG_SHIFT 0
452
453u64 ring_buffer_time_stamp(struct ring_buffer *buffer, int cpu)
454{
455 u64 time;
456
457 preempt_disable_notrace();
458 /* shift to debug/test normalization and TIME_EXTENTS */
459 time = buffer->clock() << DEBUG_SHIFT;
460 preempt_enable_no_resched_notrace();
461
462 return time;
463}
464EXPORT_SYMBOL_GPL(ring_buffer_time_stamp);
465
466void ring_buffer_normalize_time_stamp(struct ring_buffer *buffer,
467 int cpu, u64 *ts)
468{
469 /* Just stupid testing the normalize function and deltas */
470 *ts >>= DEBUG_SHIFT;
471}
472EXPORT_SYMBOL_GPL(ring_buffer_normalize_time_stamp);
473
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400474/**
475 * check_pages - integrity check of buffer pages
476 * @cpu_buffer: CPU buffer with pages to test
477 *
Wenji Huangc3706f02009-02-10 01:03:18 -0500478 * As a safety measure we check to make sure the data pages have not
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400479 * been corrupted.
480 */
481static int rb_check_pages(struct ring_buffer_per_cpu *cpu_buffer)
482{
483 struct list_head *head = &cpu_buffer->pages;
Steven Rostedt044fa782008-12-02 23:50:03 -0500484 struct buffer_page *bpage, *tmp;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400485
Steven Rostedt3e89c7b2008-11-11 15:28:41 -0500486 if (RB_WARN_ON(cpu_buffer, head->next->prev != head))
487 return -1;
488 if (RB_WARN_ON(cpu_buffer, head->prev->next != head))
489 return -1;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400490
Steven Rostedt044fa782008-12-02 23:50:03 -0500491 list_for_each_entry_safe(bpage, tmp, head, list) {
Steven Rostedt3e89c7b2008-11-11 15:28:41 -0500492 if (RB_WARN_ON(cpu_buffer,
Steven Rostedt044fa782008-12-02 23:50:03 -0500493 bpage->list.next->prev != &bpage->list))
Steven Rostedt3e89c7b2008-11-11 15:28:41 -0500494 return -1;
495 if (RB_WARN_ON(cpu_buffer,
Steven Rostedt044fa782008-12-02 23:50:03 -0500496 bpage->list.prev->next != &bpage->list))
Steven Rostedt3e89c7b2008-11-11 15:28:41 -0500497 return -1;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400498 }
499
500 return 0;
501}
502
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400503static int rb_allocate_pages(struct ring_buffer_per_cpu *cpu_buffer,
504 unsigned nr_pages)
505{
506 struct list_head *head = &cpu_buffer->pages;
Steven Rostedt044fa782008-12-02 23:50:03 -0500507 struct buffer_page *bpage, *tmp;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400508 unsigned long addr;
509 LIST_HEAD(pages);
510 unsigned i;
511
512 for (i = 0; i < nr_pages; i++) {
Steven Rostedt044fa782008-12-02 23:50:03 -0500513 bpage = kzalloc_node(ALIGN(sizeof(*bpage), cache_line_size()),
Steven Rostedtaa1e0e3b2008-10-02 19:18:09 -0400514 GFP_KERNEL, cpu_to_node(cpu_buffer->cpu));
Steven Rostedt044fa782008-12-02 23:50:03 -0500515 if (!bpage)
Steven Rostedte4c2ce82008-10-01 11:14:54 -0400516 goto free_pages;
Steven Rostedt044fa782008-12-02 23:50:03 -0500517 list_add(&bpage->list, &pages);
Steven Rostedte4c2ce82008-10-01 11:14:54 -0400518
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400519 addr = __get_free_page(GFP_KERNEL);
520 if (!addr)
521 goto free_pages;
Steven Rostedt044fa782008-12-02 23:50:03 -0500522 bpage->page = (void *)addr;
523 rb_init_page(bpage->page);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400524 }
525
526 list_splice(&pages, head);
527
528 rb_check_pages(cpu_buffer);
529
530 return 0;
531
532 free_pages:
Steven Rostedt044fa782008-12-02 23:50:03 -0500533 list_for_each_entry_safe(bpage, tmp, &pages, list) {
534 list_del_init(&bpage->list);
535 free_buffer_page(bpage);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400536 }
537 return -ENOMEM;
538}
539
540static struct ring_buffer_per_cpu *
541rb_allocate_cpu_buffer(struct ring_buffer *buffer, int cpu)
542{
543 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedt044fa782008-12-02 23:50:03 -0500544 struct buffer_page *bpage;
Steven Rostedtd7690412008-10-01 00:29:53 -0400545 unsigned long addr;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400546 int ret;
547
548 cpu_buffer = kzalloc_node(ALIGN(sizeof(*cpu_buffer), cache_line_size()),
549 GFP_KERNEL, cpu_to_node(cpu));
550 if (!cpu_buffer)
551 return NULL;
552
553 cpu_buffer->cpu = cpu;
554 cpu_buffer->buffer = buffer;
Steven Rostedtf83c9d02008-11-11 18:47:44 +0100555 spin_lock_init(&cpu_buffer->reader_lock);
Steven Rostedt3e03fb72008-11-06 00:09:43 -0500556 cpu_buffer->lock = (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400557 INIT_LIST_HEAD(&cpu_buffer->pages);
558
Steven Rostedt044fa782008-12-02 23:50:03 -0500559 bpage = kzalloc_node(ALIGN(sizeof(*bpage), cache_line_size()),
Steven Rostedte4c2ce82008-10-01 11:14:54 -0400560 GFP_KERNEL, cpu_to_node(cpu));
Steven Rostedt044fa782008-12-02 23:50:03 -0500561 if (!bpage)
Steven Rostedte4c2ce82008-10-01 11:14:54 -0400562 goto fail_free_buffer;
563
Steven Rostedt044fa782008-12-02 23:50:03 -0500564 cpu_buffer->reader_page = bpage;
Steven Rostedtd7690412008-10-01 00:29:53 -0400565 addr = __get_free_page(GFP_KERNEL);
566 if (!addr)
Steven Rostedte4c2ce82008-10-01 11:14:54 -0400567 goto fail_free_reader;
Steven Rostedt044fa782008-12-02 23:50:03 -0500568 bpage->page = (void *)addr;
569 rb_init_page(bpage->page);
Steven Rostedte4c2ce82008-10-01 11:14:54 -0400570
Steven Rostedtd7690412008-10-01 00:29:53 -0400571 INIT_LIST_HEAD(&cpu_buffer->reader_page->list);
Steven Rostedtd7690412008-10-01 00:29:53 -0400572
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400573 ret = rb_allocate_pages(cpu_buffer, buffer->pages);
574 if (ret < 0)
Steven Rostedtd7690412008-10-01 00:29:53 -0400575 goto fail_free_reader;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400576
577 cpu_buffer->head_page
578 = list_entry(cpu_buffer->pages.next, struct buffer_page, list);
Steven Rostedtbf41a152008-10-04 02:00:59 -0400579 cpu_buffer->tail_page = cpu_buffer->commit_page = cpu_buffer->head_page;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400580
581 return cpu_buffer;
582
Steven Rostedtd7690412008-10-01 00:29:53 -0400583 fail_free_reader:
584 free_buffer_page(cpu_buffer->reader_page);
585
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400586 fail_free_buffer:
587 kfree(cpu_buffer);
588 return NULL;
589}
590
591static void rb_free_cpu_buffer(struct ring_buffer_per_cpu *cpu_buffer)
592{
593 struct list_head *head = &cpu_buffer->pages;
Steven Rostedt044fa782008-12-02 23:50:03 -0500594 struct buffer_page *bpage, *tmp;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400595
Steven Rostedtd7690412008-10-01 00:29:53 -0400596 free_buffer_page(cpu_buffer->reader_page);
597
Steven Rostedt044fa782008-12-02 23:50:03 -0500598 list_for_each_entry_safe(bpage, tmp, head, list) {
599 list_del_init(&bpage->list);
600 free_buffer_page(bpage);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400601 }
602 kfree(cpu_buffer);
603}
604
Steven Rostedta7b13742008-09-29 23:02:39 -0400605/*
606 * Causes compile errors if the struct buffer_page gets bigger
607 * than the struct page.
608 */
609extern int ring_buffer_page_too_big(void);
610
Steven Rostedt59222ef2009-03-12 11:46:03 -0400611#ifdef CONFIG_HOTPLUG_CPU
Frederic Weisbecker09c9e842009-03-21 04:33:36 +0100612static int rb_cpu_notify(struct notifier_block *self,
613 unsigned long action, void *hcpu);
Steven Rostedt554f7862009-03-11 22:00:13 -0400614#endif
615
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400616/**
617 * ring_buffer_alloc - allocate a new ring_buffer
Robert Richter68814b52008-11-24 12:24:12 +0100618 * @size: the size in bytes per cpu that is needed.
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400619 * @flags: attributes to set for the ring buffer.
620 *
621 * Currently the only flag that is available is the RB_FL_OVERWRITE
622 * flag. This flag means that the buffer will overwrite old data
623 * when the buffer wraps. If this flag is not set, the buffer will
624 * drop data when the tail hits the head.
625 */
626struct ring_buffer *ring_buffer_alloc(unsigned long size, unsigned flags)
627{
628 struct ring_buffer *buffer;
629 int bsize;
630 int cpu;
631
Steven Rostedta7b13742008-09-29 23:02:39 -0400632 /* Paranoid! Optimizes out when all is well */
633 if (sizeof(struct buffer_page) > sizeof(struct page))
634 ring_buffer_page_too_big();
635
636
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400637 /* keep it in its own cache line */
638 buffer = kzalloc(ALIGN(sizeof(*buffer), cache_line_size()),
639 GFP_KERNEL);
640 if (!buffer)
641 return NULL;
642
Rusty Russell9e01c1b2009-01-01 10:12:22 +1030643 if (!alloc_cpumask_var(&buffer->cpumask, GFP_KERNEL))
644 goto fail_free_buffer;
645
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400646 buffer->pages = DIV_ROUND_UP(size, BUF_PAGE_SIZE);
647 buffer->flags = flags;
Steven Rostedt37886f62009-03-17 17:22:06 -0400648 buffer->clock = trace_clock_local;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400649
650 /* need at least two pages */
651 if (buffer->pages == 1)
652 buffer->pages++;
653
Frederic Weisbecker3bf832c2009-03-19 14:47:33 +0100654 /*
655 * In case of non-hotplug cpu, if the ring-buffer is allocated
656 * in early initcall, it will not be notified of secondary cpus.
657 * In that off case, we need to allocate for all possible cpus.
658 */
659#ifdef CONFIG_HOTPLUG_CPU
Steven Rostedt554f7862009-03-11 22:00:13 -0400660 get_online_cpus();
661 cpumask_copy(buffer->cpumask, cpu_online_mask);
Frederic Weisbecker3bf832c2009-03-19 14:47:33 +0100662#else
663 cpumask_copy(buffer->cpumask, cpu_possible_mask);
664#endif
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400665 buffer->cpus = nr_cpu_ids;
666
667 bsize = sizeof(void *) * nr_cpu_ids;
668 buffer->buffers = kzalloc(ALIGN(bsize, cache_line_size()),
669 GFP_KERNEL);
670 if (!buffer->buffers)
Rusty Russell9e01c1b2009-01-01 10:12:22 +1030671 goto fail_free_cpumask;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400672
673 for_each_buffer_cpu(buffer, cpu) {
674 buffer->buffers[cpu] =
675 rb_allocate_cpu_buffer(buffer, cpu);
676 if (!buffer->buffers[cpu])
677 goto fail_free_buffers;
678 }
679
Steven Rostedt59222ef2009-03-12 11:46:03 -0400680#ifdef CONFIG_HOTPLUG_CPU
Steven Rostedt554f7862009-03-11 22:00:13 -0400681 buffer->cpu_notify.notifier_call = rb_cpu_notify;
682 buffer->cpu_notify.priority = 0;
683 register_cpu_notifier(&buffer->cpu_notify);
684#endif
685
686 put_online_cpus();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400687 mutex_init(&buffer->mutex);
688
689 return buffer;
690
691 fail_free_buffers:
692 for_each_buffer_cpu(buffer, cpu) {
693 if (buffer->buffers[cpu])
694 rb_free_cpu_buffer(buffer->buffers[cpu]);
695 }
696 kfree(buffer->buffers);
697
Rusty Russell9e01c1b2009-01-01 10:12:22 +1030698 fail_free_cpumask:
699 free_cpumask_var(buffer->cpumask);
Steven Rostedt554f7862009-03-11 22:00:13 -0400700 put_online_cpus();
Rusty Russell9e01c1b2009-01-01 10:12:22 +1030701
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400702 fail_free_buffer:
703 kfree(buffer);
704 return NULL;
705}
Robert Richterc4f50182008-12-11 16:49:22 +0100706EXPORT_SYMBOL_GPL(ring_buffer_alloc);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400707
708/**
709 * ring_buffer_free - free a ring buffer.
710 * @buffer: the buffer to free.
711 */
712void
713ring_buffer_free(struct ring_buffer *buffer)
714{
715 int cpu;
716
Steven Rostedt554f7862009-03-11 22:00:13 -0400717 get_online_cpus();
718
Steven Rostedt59222ef2009-03-12 11:46:03 -0400719#ifdef CONFIG_HOTPLUG_CPU
Steven Rostedt554f7862009-03-11 22:00:13 -0400720 unregister_cpu_notifier(&buffer->cpu_notify);
721#endif
722
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400723 for_each_buffer_cpu(buffer, cpu)
724 rb_free_cpu_buffer(buffer->buffers[cpu]);
725
Steven Rostedt554f7862009-03-11 22:00:13 -0400726 put_online_cpus();
727
Rusty Russell9e01c1b2009-01-01 10:12:22 +1030728 free_cpumask_var(buffer->cpumask);
729
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400730 kfree(buffer);
731}
Robert Richterc4f50182008-12-11 16:49:22 +0100732EXPORT_SYMBOL_GPL(ring_buffer_free);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400733
Steven Rostedt37886f62009-03-17 17:22:06 -0400734void ring_buffer_set_clock(struct ring_buffer *buffer,
735 u64 (*clock)(void))
736{
737 buffer->clock = clock;
738}
739
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400740static void rb_reset_cpu(struct ring_buffer_per_cpu *cpu_buffer);
741
742static void
743rb_remove_pages(struct ring_buffer_per_cpu *cpu_buffer, unsigned nr_pages)
744{
Steven Rostedt044fa782008-12-02 23:50:03 -0500745 struct buffer_page *bpage;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400746 struct list_head *p;
747 unsigned i;
748
749 atomic_inc(&cpu_buffer->record_disabled);
750 synchronize_sched();
751
752 for (i = 0; i < nr_pages; i++) {
Steven Rostedt3e89c7b2008-11-11 15:28:41 -0500753 if (RB_WARN_ON(cpu_buffer, list_empty(&cpu_buffer->pages)))
754 return;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400755 p = cpu_buffer->pages.next;
Steven Rostedt044fa782008-12-02 23:50:03 -0500756 bpage = list_entry(p, struct buffer_page, list);
757 list_del_init(&bpage->list);
758 free_buffer_page(bpage);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400759 }
Steven Rostedt3e89c7b2008-11-11 15:28:41 -0500760 if (RB_WARN_ON(cpu_buffer, list_empty(&cpu_buffer->pages)))
761 return;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400762
763 rb_reset_cpu(cpu_buffer);
764
765 rb_check_pages(cpu_buffer);
766
767 atomic_dec(&cpu_buffer->record_disabled);
768
769}
770
771static void
772rb_insert_pages(struct ring_buffer_per_cpu *cpu_buffer,
773 struct list_head *pages, unsigned nr_pages)
774{
Steven Rostedt044fa782008-12-02 23:50:03 -0500775 struct buffer_page *bpage;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400776 struct list_head *p;
777 unsigned i;
778
779 atomic_inc(&cpu_buffer->record_disabled);
780 synchronize_sched();
781
782 for (i = 0; i < nr_pages; i++) {
Steven Rostedt3e89c7b2008-11-11 15:28:41 -0500783 if (RB_WARN_ON(cpu_buffer, list_empty(pages)))
784 return;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400785 p = pages->next;
Steven Rostedt044fa782008-12-02 23:50:03 -0500786 bpage = list_entry(p, struct buffer_page, list);
787 list_del_init(&bpage->list);
788 list_add_tail(&bpage->list, &cpu_buffer->pages);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400789 }
790 rb_reset_cpu(cpu_buffer);
791
792 rb_check_pages(cpu_buffer);
793
794 atomic_dec(&cpu_buffer->record_disabled);
795}
796
797/**
798 * ring_buffer_resize - resize the ring buffer
799 * @buffer: the buffer to resize.
800 * @size: the new size.
801 *
802 * The tracer is responsible for making sure that the buffer is
803 * not being used while changing the size.
804 * Note: We may be able to change the above requirement by using
805 * RCU synchronizations.
806 *
807 * Minimum size is 2 * BUF_PAGE_SIZE.
808 *
809 * Returns -1 on failure.
810 */
811int ring_buffer_resize(struct ring_buffer *buffer, unsigned long size)
812{
813 struct ring_buffer_per_cpu *cpu_buffer;
814 unsigned nr_pages, rm_pages, new_pages;
Steven Rostedt044fa782008-12-02 23:50:03 -0500815 struct buffer_page *bpage, *tmp;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400816 unsigned long buffer_size;
817 unsigned long addr;
818 LIST_HEAD(pages);
819 int i, cpu;
820
Ingo Molnaree51a1d2008-11-13 14:58:31 +0100821 /*
822 * Always succeed at resizing a non-existent buffer:
823 */
824 if (!buffer)
825 return size;
826
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400827 size = DIV_ROUND_UP(size, BUF_PAGE_SIZE);
828 size *= BUF_PAGE_SIZE;
829 buffer_size = buffer->pages * BUF_PAGE_SIZE;
830
831 /* we need a minimum of two pages */
832 if (size < BUF_PAGE_SIZE * 2)
833 size = BUF_PAGE_SIZE * 2;
834
835 if (size == buffer_size)
836 return size;
837
838 mutex_lock(&buffer->mutex);
Steven Rostedt554f7862009-03-11 22:00:13 -0400839 get_online_cpus();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400840
841 nr_pages = DIV_ROUND_UP(size, BUF_PAGE_SIZE);
842
843 if (size < buffer_size) {
844
845 /* easy case, just free pages */
Steven Rostedt554f7862009-03-11 22:00:13 -0400846 if (RB_WARN_ON(buffer, nr_pages >= buffer->pages))
847 goto out_fail;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400848
849 rm_pages = buffer->pages - nr_pages;
850
851 for_each_buffer_cpu(buffer, cpu) {
852 cpu_buffer = buffer->buffers[cpu];
853 rb_remove_pages(cpu_buffer, rm_pages);
854 }
855 goto out;
856 }
857
858 /*
859 * This is a bit more difficult. We only want to add pages
860 * when we can allocate enough for all CPUs. We do this
861 * by allocating all the pages and storing them on a local
862 * link list. If we succeed in our allocation, then we
863 * add these pages to the cpu_buffers. Otherwise we just free
864 * them all and return -ENOMEM;
865 */
Steven Rostedt554f7862009-03-11 22:00:13 -0400866 if (RB_WARN_ON(buffer, nr_pages <= buffer->pages))
867 goto out_fail;
Steven Rostedtf536aaf2008-11-10 23:07:30 -0500868
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400869 new_pages = nr_pages - buffer->pages;
870
871 for_each_buffer_cpu(buffer, cpu) {
872 for (i = 0; i < new_pages; i++) {
Steven Rostedt044fa782008-12-02 23:50:03 -0500873 bpage = kzalloc_node(ALIGN(sizeof(*bpage),
Steven Rostedte4c2ce82008-10-01 11:14:54 -0400874 cache_line_size()),
875 GFP_KERNEL, cpu_to_node(cpu));
Steven Rostedt044fa782008-12-02 23:50:03 -0500876 if (!bpage)
Steven Rostedte4c2ce82008-10-01 11:14:54 -0400877 goto free_pages;
Steven Rostedt044fa782008-12-02 23:50:03 -0500878 list_add(&bpage->list, &pages);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400879 addr = __get_free_page(GFP_KERNEL);
880 if (!addr)
881 goto free_pages;
Steven Rostedt044fa782008-12-02 23:50:03 -0500882 bpage->page = (void *)addr;
883 rb_init_page(bpage->page);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400884 }
885 }
886
887 for_each_buffer_cpu(buffer, cpu) {
888 cpu_buffer = buffer->buffers[cpu];
889 rb_insert_pages(cpu_buffer, &pages, new_pages);
890 }
891
Steven Rostedt554f7862009-03-11 22:00:13 -0400892 if (RB_WARN_ON(buffer, !list_empty(&pages)))
893 goto out_fail;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400894
895 out:
896 buffer->pages = nr_pages;
Steven Rostedt554f7862009-03-11 22:00:13 -0400897 put_online_cpus();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400898 mutex_unlock(&buffer->mutex);
899
900 return size;
901
902 free_pages:
Steven Rostedt044fa782008-12-02 23:50:03 -0500903 list_for_each_entry_safe(bpage, tmp, &pages, list) {
904 list_del_init(&bpage->list);
905 free_buffer_page(bpage);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400906 }
Steven Rostedt554f7862009-03-11 22:00:13 -0400907 put_online_cpus();
Vegard Nossum641d2f62008-11-18 19:22:13 +0100908 mutex_unlock(&buffer->mutex);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400909 return -ENOMEM;
Steven Rostedt554f7862009-03-11 22:00:13 -0400910
911 /*
912 * Something went totally wrong, and we are too paranoid
913 * to even clean up the mess.
914 */
915 out_fail:
916 put_online_cpus();
917 mutex_unlock(&buffer->mutex);
918 return -1;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400919}
Robert Richterc4f50182008-12-11 16:49:22 +0100920EXPORT_SYMBOL_GPL(ring_buffer_resize);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400921
Steven Rostedt8789a9e2008-12-02 15:34:07 -0500922static inline void *
Steven Rostedt044fa782008-12-02 23:50:03 -0500923__rb_data_page_index(struct buffer_data_page *bpage, unsigned index)
Steven Rostedt8789a9e2008-12-02 15:34:07 -0500924{
Steven Rostedt044fa782008-12-02 23:50:03 -0500925 return bpage->data + index;
Steven Rostedt8789a9e2008-12-02 15:34:07 -0500926}
927
Steven Rostedt044fa782008-12-02 23:50:03 -0500928static inline void *__rb_page_index(struct buffer_page *bpage, unsigned index)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400929{
Steven Rostedt044fa782008-12-02 23:50:03 -0500930 return bpage->page->data + index;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400931}
932
933static inline struct ring_buffer_event *
Steven Rostedtd7690412008-10-01 00:29:53 -0400934rb_reader_event(struct ring_buffer_per_cpu *cpu_buffer)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400935{
Steven Rostedt6f807ac2008-10-04 02:00:58 -0400936 return __rb_page_index(cpu_buffer->reader_page,
937 cpu_buffer->reader_page->read);
938}
939
940static inline struct ring_buffer_event *
941rb_head_event(struct ring_buffer_per_cpu *cpu_buffer)
942{
943 return __rb_page_index(cpu_buffer->head_page,
944 cpu_buffer->head_page->read);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400945}
946
947static inline struct ring_buffer_event *
948rb_iter_head_event(struct ring_buffer_iter *iter)
949{
Steven Rostedt6f807ac2008-10-04 02:00:58 -0400950 return __rb_page_index(iter->head_page, iter->head);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400951}
952
Steven Rostedtbf41a152008-10-04 02:00:59 -0400953static inline unsigned rb_page_write(struct buffer_page *bpage)
954{
955 return local_read(&bpage->write);
956}
957
958static inline unsigned rb_page_commit(struct buffer_page *bpage)
959{
Steven Rostedtabc9b562008-12-02 15:34:06 -0500960 return local_read(&bpage->page->commit);
Steven Rostedtbf41a152008-10-04 02:00:59 -0400961}
962
963/* Size is determined by what has been commited */
964static inline unsigned rb_page_size(struct buffer_page *bpage)
965{
966 return rb_page_commit(bpage);
967}
968
969static inline unsigned
970rb_commit_index(struct ring_buffer_per_cpu *cpu_buffer)
971{
972 return rb_page_commit(cpu_buffer->commit_page);
973}
974
975static inline unsigned rb_head_size(struct ring_buffer_per_cpu *cpu_buffer)
976{
977 return rb_page_commit(cpu_buffer->head_page);
978}
979
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400980/*
981 * When the tail hits the head and the buffer is in overwrite mode,
982 * the head jumps to the next page and all content on the previous
983 * page is discarded. But before doing so, we update the overrun
984 * variable of the buffer.
985 */
986static void rb_update_overflow(struct ring_buffer_per_cpu *cpu_buffer)
987{
988 struct ring_buffer_event *event;
989 unsigned long head;
990
991 for (head = 0; head < rb_head_size(cpu_buffer);
992 head += rb_event_length(event)) {
993
Steven Rostedt6f807ac2008-10-04 02:00:58 -0400994 event = __rb_page_index(cpu_buffer->head_page, head);
Steven Rostedt3e89c7b2008-11-11 15:28:41 -0500995 if (RB_WARN_ON(cpu_buffer, rb_null_event(event)))
996 return;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400997 /* Only count data entries */
Lai Jiangshan334d4162009-04-24 11:27:05 +0800998 if (event->type_len > RINGBUF_TYPE_DATA_TYPE_LEN_MAX)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400999 continue;
1000 cpu_buffer->overrun++;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001001 }
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 Rostedte4906ef2009-04-30 20:49:44 -04001591 local_inc(&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 */
Steven Rostedte4906ef2009-04-30 20:49:44 -04001725 local_inc(&cpu_buffer->entries);
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04001726 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 Rostedte4906ef2009-04-30 20:49:44 -04001905 ret = (local_read(&cpu_buffer->entries) - cpu_buffer->overrun)
1906 - cpu_buffer->read;
Steven Rostedt554f7862009-03-11 22:00:13 -04001907
1908 return ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001909}
Robert Richterc4f50182008-12-11 16:49:22 +01001910EXPORT_SYMBOL_GPL(ring_buffer_entries_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001911
1912/**
1913 * ring_buffer_overrun_cpu - get the number of overruns in a cpu_buffer
1914 * @buffer: The ring buffer
1915 * @cpu: The per CPU buffer to get the number of overruns from
1916 */
1917unsigned long ring_buffer_overrun_cpu(struct ring_buffer *buffer, int cpu)
1918{
1919 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedt8aabee52009-03-12 13:13:49 -04001920 unsigned long ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001921
Rusty Russell9e01c1b2009-01-01 10:12:22 +10301922 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04001923 return 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001924
1925 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt554f7862009-03-11 22:00:13 -04001926 ret = cpu_buffer->overrun;
Steven Rostedt554f7862009-03-11 22:00:13 -04001927
1928 return ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001929}
Robert Richterc4f50182008-12-11 16:49:22 +01001930EXPORT_SYMBOL_GPL(ring_buffer_overrun_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001931
1932/**
Steven Rostedtf0d2c682009-04-29 13:43:37 -04001933 * ring_buffer_nmi_dropped_cpu - get the number of nmis that were dropped
1934 * @buffer: The ring buffer
1935 * @cpu: The per CPU buffer to get the number of overruns from
1936 */
1937unsigned long ring_buffer_nmi_dropped_cpu(struct ring_buffer *buffer, int cpu)
1938{
1939 struct ring_buffer_per_cpu *cpu_buffer;
1940 unsigned long ret;
1941
1942 if (!cpumask_test_cpu(cpu, buffer->cpumask))
1943 return 0;
1944
1945 cpu_buffer = buffer->buffers[cpu];
1946 ret = cpu_buffer->nmi_dropped;
1947
1948 return ret;
1949}
1950EXPORT_SYMBOL_GPL(ring_buffer_nmi_dropped_cpu);
1951
1952/**
1953 * ring_buffer_commit_overrun_cpu - get the number of overruns caused by commits
1954 * @buffer: The ring buffer
1955 * @cpu: The per CPU buffer to get the number of overruns from
1956 */
1957unsigned long
1958ring_buffer_commit_overrun_cpu(struct ring_buffer *buffer, int cpu)
1959{
1960 struct ring_buffer_per_cpu *cpu_buffer;
1961 unsigned long ret;
1962
1963 if (!cpumask_test_cpu(cpu, buffer->cpumask))
1964 return 0;
1965
1966 cpu_buffer = buffer->buffers[cpu];
1967 ret = cpu_buffer->commit_overrun;
1968
1969 return ret;
1970}
1971EXPORT_SYMBOL_GPL(ring_buffer_commit_overrun_cpu);
1972
1973/**
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001974 * ring_buffer_entries - get the number of entries in a buffer
1975 * @buffer: The ring buffer
1976 *
1977 * Returns the total number of entries in the ring buffer
1978 * (all CPU entries)
1979 */
1980unsigned long ring_buffer_entries(struct ring_buffer *buffer)
1981{
1982 struct ring_buffer_per_cpu *cpu_buffer;
1983 unsigned long entries = 0;
1984 int cpu;
1985
1986 /* if you care about this being correct, lock the buffer */
1987 for_each_buffer_cpu(buffer, cpu) {
1988 cpu_buffer = buffer->buffers[cpu];
Steven Rostedte4906ef2009-04-30 20:49:44 -04001989 entries += (local_read(&cpu_buffer->entries) -
1990 cpu_buffer->overrun) - cpu_buffer->read;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001991 }
1992
1993 return entries;
1994}
Robert Richterc4f50182008-12-11 16:49:22 +01001995EXPORT_SYMBOL_GPL(ring_buffer_entries);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001996
1997/**
1998 * ring_buffer_overrun_cpu - get the number of overruns in buffer
1999 * @buffer: The ring buffer
2000 *
2001 * Returns the total number of overruns in the ring buffer
2002 * (all CPU entries)
2003 */
2004unsigned long ring_buffer_overruns(struct ring_buffer *buffer)
2005{
2006 struct ring_buffer_per_cpu *cpu_buffer;
2007 unsigned long overruns = 0;
2008 int cpu;
2009
2010 /* if you care about this being correct, lock the buffer */
2011 for_each_buffer_cpu(buffer, cpu) {
2012 cpu_buffer = buffer->buffers[cpu];
2013 overruns += cpu_buffer->overrun;
2014 }
2015
2016 return overruns;
2017}
Robert Richterc4f50182008-12-11 16:49:22 +01002018EXPORT_SYMBOL_GPL(ring_buffer_overruns);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002019
Steven Rostedt642edba2008-11-12 00:01:26 -05002020static void rb_iter_reset(struct ring_buffer_iter *iter)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002021{
2022 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
2023
Steven Rostedtd7690412008-10-01 00:29:53 -04002024 /* Iterator usage is expected to have record disabled */
2025 if (list_empty(&cpu_buffer->reader_page->list)) {
2026 iter->head_page = cpu_buffer->head_page;
Steven Rostedt6f807ac2008-10-04 02:00:58 -04002027 iter->head = cpu_buffer->head_page->read;
Steven Rostedtd7690412008-10-01 00:29:53 -04002028 } else {
2029 iter->head_page = cpu_buffer->reader_page;
Steven Rostedt6f807ac2008-10-04 02:00:58 -04002030 iter->head = cpu_buffer->reader_page->read;
Steven Rostedtd7690412008-10-01 00:29:53 -04002031 }
2032 if (iter->head)
2033 iter->read_stamp = cpu_buffer->read_stamp;
2034 else
Steven Rostedtabc9b562008-12-02 15:34:06 -05002035 iter->read_stamp = iter->head_page->page->time_stamp;
Steven Rostedt642edba2008-11-12 00:01:26 -05002036}
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002037
Steven Rostedt642edba2008-11-12 00:01:26 -05002038/**
2039 * ring_buffer_iter_reset - reset an iterator
2040 * @iter: The iterator to reset
2041 *
2042 * Resets the iterator, so that it will start from the beginning
2043 * again.
2044 */
2045void ring_buffer_iter_reset(struct ring_buffer_iter *iter)
2046{
Steven Rostedt554f7862009-03-11 22:00:13 -04002047 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedt642edba2008-11-12 00:01:26 -05002048 unsigned long flags;
2049
Steven Rostedt554f7862009-03-11 22:00:13 -04002050 if (!iter)
2051 return;
2052
2053 cpu_buffer = iter->cpu_buffer;
2054
Steven Rostedt642edba2008-11-12 00:01:26 -05002055 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
2056 rb_iter_reset(iter);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002057 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002058}
Robert Richterc4f50182008-12-11 16:49:22 +01002059EXPORT_SYMBOL_GPL(ring_buffer_iter_reset);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002060
2061/**
2062 * ring_buffer_iter_empty - check if an iterator has no more to read
2063 * @iter: The iterator to check
2064 */
2065int ring_buffer_iter_empty(struct ring_buffer_iter *iter)
2066{
2067 struct ring_buffer_per_cpu *cpu_buffer;
2068
2069 cpu_buffer = iter->cpu_buffer;
2070
Steven Rostedtbf41a152008-10-04 02:00:59 -04002071 return iter->head_page == cpu_buffer->commit_page &&
2072 iter->head == rb_commit_index(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002073}
Robert Richterc4f50182008-12-11 16:49:22 +01002074EXPORT_SYMBOL_GPL(ring_buffer_iter_empty);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002075
2076static void
2077rb_update_read_stamp(struct ring_buffer_per_cpu *cpu_buffer,
2078 struct ring_buffer_event *event)
2079{
2080 u64 delta;
2081
Lai Jiangshan334d4162009-04-24 11:27:05 +08002082 switch (event->type_len) {
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002083 case RINGBUF_TYPE_PADDING:
2084 return;
2085
2086 case RINGBUF_TYPE_TIME_EXTEND:
2087 delta = event->array[0];
2088 delta <<= TS_SHIFT;
2089 delta += event->time_delta;
2090 cpu_buffer->read_stamp += delta;
2091 return;
2092
2093 case RINGBUF_TYPE_TIME_STAMP:
2094 /* FIXME: not implemented */
2095 return;
2096
2097 case RINGBUF_TYPE_DATA:
2098 cpu_buffer->read_stamp += event->time_delta;
2099 return;
2100
2101 default:
2102 BUG();
2103 }
2104 return;
2105}
2106
2107static void
2108rb_update_iter_read_stamp(struct ring_buffer_iter *iter,
2109 struct ring_buffer_event *event)
2110{
2111 u64 delta;
2112
Lai Jiangshan334d4162009-04-24 11:27:05 +08002113 switch (event->type_len) {
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002114 case RINGBUF_TYPE_PADDING:
2115 return;
2116
2117 case RINGBUF_TYPE_TIME_EXTEND:
2118 delta = event->array[0];
2119 delta <<= TS_SHIFT;
2120 delta += event->time_delta;
2121 iter->read_stamp += delta;
2122 return;
2123
2124 case RINGBUF_TYPE_TIME_STAMP:
2125 /* FIXME: not implemented */
2126 return;
2127
2128 case RINGBUF_TYPE_DATA:
2129 iter->read_stamp += event->time_delta;
2130 return;
2131
2132 default:
2133 BUG();
2134 }
2135 return;
2136}
2137
Steven Rostedtd7690412008-10-01 00:29:53 -04002138static struct buffer_page *
2139rb_get_reader_page(struct ring_buffer_per_cpu *cpu_buffer)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002140{
Steven Rostedtd7690412008-10-01 00:29:53 -04002141 struct buffer_page *reader = NULL;
2142 unsigned long flags;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002143 int nr_loops = 0;
Steven Rostedtd7690412008-10-01 00:29:53 -04002144
Steven Rostedt3e03fb72008-11-06 00:09:43 -05002145 local_irq_save(flags);
2146 __raw_spin_lock(&cpu_buffer->lock);
Steven Rostedtd7690412008-10-01 00:29:53 -04002147
2148 again:
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002149 /*
2150 * This should normally only loop twice. But because the
2151 * start of the reader inserts an empty page, it causes
2152 * a case where we will loop three times. There should be no
2153 * reason to loop four times (that I know of).
2154 */
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05002155 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 3)) {
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002156 reader = NULL;
2157 goto out;
2158 }
2159
Steven Rostedtd7690412008-10-01 00:29:53 -04002160 reader = cpu_buffer->reader_page;
2161
2162 /* If there's more to read, return this page */
Steven Rostedtbf41a152008-10-04 02:00:59 -04002163 if (cpu_buffer->reader_page->read < rb_page_size(reader))
Steven Rostedtd7690412008-10-01 00:29:53 -04002164 goto out;
2165
2166 /* Never should we have an index greater than the size */
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05002167 if (RB_WARN_ON(cpu_buffer,
2168 cpu_buffer->reader_page->read > rb_page_size(reader)))
2169 goto out;
Steven Rostedtd7690412008-10-01 00:29:53 -04002170
2171 /* check if we caught up to the tail */
2172 reader = NULL;
Steven Rostedtbf41a152008-10-04 02:00:59 -04002173 if (cpu_buffer->commit_page == cpu_buffer->reader_page)
Steven Rostedtd7690412008-10-01 00:29:53 -04002174 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002175
2176 /*
Steven Rostedtd7690412008-10-01 00:29:53 -04002177 * Splice the empty reader page into the list around the head.
2178 * Reset the reader page to size zero.
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002179 */
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002180
Steven Rostedtd7690412008-10-01 00:29:53 -04002181 reader = cpu_buffer->head_page;
2182 cpu_buffer->reader_page->list.next = reader->list.next;
2183 cpu_buffer->reader_page->list.prev = reader->list.prev;
Steven Rostedtbf41a152008-10-04 02:00:59 -04002184
2185 local_set(&cpu_buffer->reader_page->write, 0);
Steven Rostedtabc9b562008-12-02 15:34:06 -05002186 local_set(&cpu_buffer->reader_page->page->commit, 0);
Steven Rostedtd7690412008-10-01 00:29:53 -04002187
2188 /* Make the reader page now replace the head */
2189 reader->list.prev->next = &cpu_buffer->reader_page->list;
2190 reader->list.next->prev = &cpu_buffer->reader_page->list;
2191
2192 /*
2193 * If the tail is on the reader, then we must set the head
2194 * to the inserted page, otherwise we set it one before.
2195 */
2196 cpu_buffer->head_page = cpu_buffer->reader_page;
2197
Steven Rostedtbf41a152008-10-04 02:00:59 -04002198 if (cpu_buffer->commit_page != reader)
Steven Rostedtd7690412008-10-01 00:29:53 -04002199 rb_inc_page(cpu_buffer, &cpu_buffer->head_page);
2200
2201 /* Finally update the reader page to the new head */
2202 cpu_buffer->reader_page = reader;
2203 rb_reset_reader_page(cpu_buffer);
2204
2205 goto again;
2206
2207 out:
Steven Rostedt3e03fb72008-11-06 00:09:43 -05002208 __raw_spin_unlock(&cpu_buffer->lock);
2209 local_irq_restore(flags);
Steven Rostedtd7690412008-10-01 00:29:53 -04002210
2211 return reader;
2212}
2213
2214static void rb_advance_reader(struct ring_buffer_per_cpu *cpu_buffer)
2215{
2216 struct ring_buffer_event *event;
2217 struct buffer_page *reader;
2218 unsigned length;
2219
2220 reader = rb_get_reader_page(cpu_buffer);
2221
2222 /* This function should not be called when buffer is empty */
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05002223 if (RB_WARN_ON(cpu_buffer, !reader))
2224 return;
Steven Rostedtd7690412008-10-01 00:29:53 -04002225
2226 event = rb_reader_event(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002227
Lai Jiangshan334d4162009-04-24 11:27:05 +08002228 if (event->type_len <= RINGBUF_TYPE_DATA_TYPE_LEN_MAX
2229 || rb_discarded_event(event))
Steven Rostedte4906ef2009-04-30 20:49:44 -04002230 cpu_buffer->read++;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002231
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002232 rb_update_read_stamp(cpu_buffer, event);
2233
Steven Rostedtd7690412008-10-01 00:29:53 -04002234 length = rb_event_length(event);
Steven Rostedt6f807ac2008-10-04 02:00:58 -04002235 cpu_buffer->reader_page->read += length;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002236}
2237
2238static void rb_advance_iter(struct ring_buffer_iter *iter)
2239{
2240 struct ring_buffer *buffer;
2241 struct ring_buffer_per_cpu *cpu_buffer;
2242 struct ring_buffer_event *event;
2243 unsigned length;
2244
2245 cpu_buffer = iter->cpu_buffer;
2246 buffer = cpu_buffer->buffer;
2247
2248 /*
2249 * Check if we are at the end of the buffer.
2250 */
Steven Rostedtbf41a152008-10-04 02:00:59 -04002251 if (iter->head >= rb_page_size(iter->head_page)) {
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05002252 if (RB_WARN_ON(buffer,
2253 iter->head_page == cpu_buffer->commit_page))
2254 return;
Steven Rostedtd7690412008-10-01 00:29:53 -04002255 rb_inc_iter(iter);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002256 return;
2257 }
2258
2259 event = rb_iter_head_event(iter);
2260
2261 length = rb_event_length(event);
2262
2263 /*
2264 * This should not be called to advance the header if we are
2265 * at the tail of the buffer.
2266 */
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05002267 if (RB_WARN_ON(cpu_buffer,
Steven Rostedtf536aaf2008-11-10 23:07:30 -05002268 (iter->head_page == cpu_buffer->commit_page) &&
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05002269 (iter->head + length > rb_commit_index(cpu_buffer))))
2270 return;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002271
2272 rb_update_iter_read_stamp(iter, event);
2273
2274 iter->head += length;
2275
2276 /* check for end of page padding */
Steven Rostedtbf41a152008-10-04 02:00:59 -04002277 if ((iter->head >= rb_page_size(iter->head_page)) &&
2278 (iter->head_page != cpu_buffer->commit_page))
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002279 rb_advance_iter(iter);
2280}
2281
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002282static struct ring_buffer_event *
2283rb_buffer_peek(struct ring_buffer *buffer, int cpu, u64 *ts)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002284{
2285 struct ring_buffer_per_cpu *cpu_buffer;
2286 struct ring_buffer_event *event;
Steven Rostedtd7690412008-10-01 00:29:53 -04002287 struct buffer_page *reader;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002288 int nr_loops = 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002289
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002290 cpu_buffer = buffer->buffers[cpu];
2291
2292 again:
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002293 /*
2294 * We repeat when a timestamp is encountered. It is possible
2295 * to get multiple timestamps from an interrupt entering just
2296 * as one timestamp is about to be written. The max times
2297 * that this can happen is the number of nested interrupts we
2298 * can have. Nesting 10 deep of interrupts is clearly
2299 * an anomaly.
2300 */
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05002301 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 10))
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002302 return NULL;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002303
Steven Rostedtd7690412008-10-01 00:29:53 -04002304 reader = rb_get_reader_page(cpu_buffer);
2305 if (!reader)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002306 return NULL;
2307
Steven Rostedtd7690412008-10-01 00:29:53 -04002308 event = rb_reader_event(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002309
Lai Jiangshan334d4162009-04-24 11:27:05 +08002310 switch (event->type_len) {
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002311 case RINGBUF_TYPE_PADDING:
Tom Zanussi2d622712009-03-22 03:30:49 -05002312 if (rb_null_event(event))
2313 RB_WARN_ON(cpu_buffer, 1);
2314 /*
2315 * Because the writer could be discarding every
2316 * event it creates (which would probably be bad)
2317 * if we were to go back to "again" then we may never
2318 * catch up, and will trigger the warn on, or lock
2319 * the box. Return the padding, and we will release
2320 * the current locks, and try again.
2321 */
Steven Rostedtd7690412008-10-01 00:29:53 -04002322 rb_advance_reader(cpu_buffer);
Tom Zanussi2d622712009-03-22 03:30:49 -05002323 return event;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002324
2325 case RINGBUF_TYPE_TIME_EXTEND:
2326 /* Internal data, OK to advance */
Steven Rostedtd7690412008-10-01 00:29:53 -04002327 rb_advance_reader(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002328 goto again;
2329
2330 case RINGBUF_TYPE_TIME_STAMP:
2331 /* FIXME: not implemented */
Steven Rostedtd7690412008-10-01 00:29:53 -04002332 rb_advance_reader(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002333 goto again;
2334
2335 case RINGBUF_TYPE_DATA:
2336 if (ts) {
2337 *ts = cpu_buffer->read_stamp + event->time_delta;
Steven Rostedt37886f62009-03-17 17:22:06 -04002338 ring_buffer_normalize_time_stamp(buffer,
2339 cpu_buffer->cpu, ts);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002340 }
2341 return event;
2342
2343 default:
2344 BUG();
2345 }
2346
2347 return NULL;
2348}
Robert Richterc4f50182008-12-11 16:49:22 +01002349EXPORT_SYMBOL_GPL(ring_buffer_peek);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002350
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002351static struct ring_buffer_event *
2352rb_iter_peek(struct ring_buffer_iter *iter, u64 *ts)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002353{
2354 struct ring_buffer *buffer;
2355 struct ring_buffer_per_cpu *cpu_buffer;
2356 struct ring_buffer_event *event;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002357 int nr_loops = 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002358
2359 if (ring_buffer_iter_empty(iter))
2360 return NULL;
2361
2362 cpu_buffer = iter->cpu_buffer;
2363 buffer = cpu_buffer->buffer;
2364
2365 again:
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002366 /*
2367 * We repeat when a timestamp is encountered. It is possible
2368 * to get multiple timestamps from an interrupt entering just
2369 * as one timestamp is about to be written. The max times
2370 * that this can happen is the number of nested interrupts we
2371 * can have. Nesting 10 deep of interrupts is clearly
2372 * an anomaly.
2373 */
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05002374 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 10))
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002375 return NULL;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002376
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002377 if (rb_per_cpu_empty(cpu_buffer))
2378 return NULL;
2379
2380 event = rb_iter_head_event(iter);
2381
Lai Jiangshan334d4162009-04-24 11:27:05 +08002382 switch (event->type_len) {
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002383 case RINGBUF_TYPE_PADDING:
Tom Zanussi2d622712009-03-22 03:30:49 -05002384 if (rb_null_event(event)) {
2385 rb_inc_iter(iter);
2386 goto again;
2387 }
2388 rb_advance_iter(iter);
2389 return event;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002390
2391 case RINGBUF_TYPE_TIME_EXTEND:
2392 /* Internal data, OK to advance */
2393 rb_advance_iter(iter);
2394 goto again;
2395
2396 case RINGBUF_TYPE_TIME_STAMP:
2397 /* FIXME: not implemented */
2398 rb_advance_iter(iter);
2399 goto again;
2400
2401 case RINGBUF_TYPE_DATA:
2402 if (ts) {
2403 *ts = iter->read_stamp + event->time_delta;
Steven Rostedt37886f62009-03-17 17:22:06 -04002404 ring_buffer_normalize_time_stamp(buffer,
2405 cpu_buffer->cpu, ts);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002406 }
2407 return event;
2408
2409 default:
2410 BUG();
2411 }
2412
2413 return NULL;
2414}
Robert Richterc4f50182008-12-11 16:49:22 +01002415EXPORT_SYMBOL_GPL(ring_buffer_iter_peek);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002416
2417/**
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002418 * ring_buffer_peek - peek at the next event to be read
2419 * @buffer: The ring buffer to read
2420 * @cpu: The cpu to peak at
2421 * @ts: The timestamp counter of this event.
2422 *
2423 * This will return the event that will be read next, but does
2424 * not consume the data.
2425 */
2426struct ring_buffer_event *
2427ring_buffer_peek(struct ring_buffer *buffer, int cpu, u64 *ts)
2428{
2429 struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
Steven Rostedt8aabee52009-03-12 13:13:49 -04002430 struct ring_buffer_event *event;
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002431 unsigned long flags;
2432
Steven Rostedt554f7862009-03-11 22:00:13 -04002433 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04002434 return NULL;
Steven Rostedt554f7862009-03-11 22:00:13 -04002435
Tom Zanussi2d622712009-03-22 03:30:49 -05002436 again:
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002437 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
2438 event = rb_buffer_peek(buffer, cpu, ts);
2439 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
2440
Lai Jiangshan334d4162009-04-24 11:27:05 +08002441 if (event && event->type_len == RINGBUF_TYPE_PADDING) {
Tom Zanussi2d622712009-03-22 03:30:49 -05002442 cpu_relax();
2443 goto again;
2444 }
2445
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002446 return event;
2447}
2448
2449/**
2450 * ring_buffer_iter_peek - peek at the next event to be read
2451 * @iter: The ring buffer iterator
2452 * @ts: The timestamp counter of this event.
2453 *
2454 * This will return the event that will be read next, but does
2455 * not increment the iterator.
2456 */
2457struct ring_buffer_event *
2458ring_buffer_iter_peek(struct ring_buffer_iter *iter, u64 *ts)
2459{
2460 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
2461 struct ring_buffer_event *event;
2462 unsigned long flags;
2463
Tom Zanussi2d622712009-03-22 03:30:49 -05002464 again:
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002465 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
2466 event = rb_iter_peek(iter, ts);
2467 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
2468
Lai Jiangshan334d4162009-04-24 11:27:05 +08002469 if (event && event->type_len == RINGBUF_TYPE_PADDING) {
Tom Zanussi2d622712009-03-22 03:30:49 -05002470 cpu_relax();
2471 goto again;
2472 }
2473
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002474 return event;
2475}
2476
2477/**
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002478 * ring_buffer_consume - return an event and consume it
2479 * @buffer: The ring buffer to get the next event from
2480 *
2481 * Returns the next event in the ring buffer, and that event is consumed.
2482 * Meaning, that sequential reads will keep returning a different event,
2483 * and eventually empty the ring buffer if the producer is slower.
2484 */
2485struct ring_buffer_event *
2486ring_buffer_consume(struct ring_buffer *buffer, int cpu, u64 *ts)
2487{
Steven Rostedt554f7862009-03-11 22:00:13 -04002488 struct ring_buffer_per_cpu *cpu_buffer;
2489 struct ring_buffer_event *event = NULL;
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002490 unsigned long flags;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002491
Tom Zanussi2d622712009-03-22 03:30:49 -05002492 again:
Steven Rostedt554f7862009-03-11 22:00:13 -04002493 /* might be called in atomic */
2494 preempt_disable();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002495
Steven Rostedt554f7862009-03-11 22:00:13 -04002496 if (!cpumask_test_cpu(cpu, buffer->cpumask))
2497 goto out;
2498
2499 cpu_buffer = buffer->buffers[cpu];
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002500 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002501
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002502 event = rb_buffer_peek(buffer, cpu, ts);
2503 if (!event)
Steven Rostedt554f7862009-03-11 22:00:13 -04002504 goto out_unlock;
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002505
Steven Rostedtd7690412008-10-01 00:29:53 -04002506 rb_advance_reader(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002507
Steven Rostedt554f7862009-03-11 22:00:13 -04002508 out_unlock:
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002509 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
2510
Steven Rostedt554f7862009-03-11 22:00:13 -04002511 out:
2512 preempt_enable();
2513
Lai Jiangshan334d4162009-04-24 11:27:05 +08002514 if (event && event->type_len == RINGBUF_TYPE_PADDING) {
Tom Zanussi2d622712009-03-22 03:30:49 -05002515 cpu_relax();
2516 goto again;
2517 }
2518
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002519 return event;
2520}
Robert Richterc4f50182008-12-11 16:49:22 +01002521EXPORT_SYMBOL_GPL(ring_buffer_consume);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002522
2523/**
2524 * ring_buffer_read_start - start a non consuming read of the buffer
2525 * @buffer: The ring buffer to read from
2526 * @cpu: The cpu buffer to iterate over
2527 *
2528 * This starts up an iteration through the buffer. It also disables
2529 * the recording to the buffer until the reading is finished.
2530 * This prevents the reading from being corrupted. This is not
2531 * a consuming read, so a producer is not expected.
2532 *
2533 * Must be paired with ring_buffer_finish.
2534 */
2535struct ring_buffer_iter *
2536ring_buffer_read_start(struct ring_buffer *buffer, int cpu)
2537{
2538 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedt8aabee52009-03-12 13:13:49 -04002539 struct ring_buffer_iter *iter;
Steven Rostedtd7690412008-10-01 00:29:53 -04002540 unsigned long flags;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002541
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302542 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04002543 return NULL;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002544
2545 iter = kmalloc(sizeof(*iter), GFP_KERNEL);
2546 if (!iter)
Steven Rostedt8aabee52009-03-12 13:13:49 -04002547 return NULL;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002548
2549 cpu_buffer = buffer->buffers[cpu];
2550
2551 iter->cpu_buffer = cpu_buffer;
2552
2553 atomic_inc(&cpu_buffer->record_disabled);
2554 synchronize_sched();
2555
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002556 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
Steven Rostedt3e03fb72008-11-06 00:09:43 -05002557 __raw_spin_lock(&cpu_buffer->lock);
Steven Rostedt642edba2008-11-12 00:01:26 -05002558 rb_iter_reset(iter);
Steven Rostedt3e03fb72008-11-06 00:09:43 -05002559 __raw_spin_unlock(&cpu_buffer->lock);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002560 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002561
2562 return iter;
2563}
Robert Richterc4f50182008-12-11 16:49:22 +01002564EXPORT_SYMBOL_GPL(ring_buffer_read_start);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002565
2566/**
2567 * ring_buffer_finish - finish reading the iterator of the buffer
2568 * @iter: The iterator retrieved by ring_buffer_start
2569 *
2570 * This re-enables the recording to the buffer, and frees the
2571 * iterator.
2572 */
2573void
2574ring_buffer_read_finish(struct ring_buffer_iter *iter)
2575{
2576 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
2577
2578 atomic_dec(&cpu_buffer->record_disabled);
2579 kfree(iter);
2580}
Robert Richterc4f50182008-12-11 16:49:22 +01002581EXPORT_SYMBOL_GPL(ring_buffer_read_finish);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002582
2583/**
2584 * ring_buffer_read - read the next item in the ring buffer by the iterator
2585 * @iter: The ring buffer iterator
2586 * @ts: The time stamp of the event read.
2587 *
2588 * This reads the next event in the ring buffer and increments the iterator.
2589 */
2590struct ring_buffer_event *
2591ring_buffer_read(struct ring_buffer_iter *iter, u64 *ts)
2592{
2593 struct ring_buffer_event *event;
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002594 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
2595 unsigned long flags;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002596
Tom Zanussi2d622712009-03-22 03:30:49 -05002597 again:
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002598 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
2599 event = rb_iter_peek(iter, ts);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002600 if (!event)
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002601 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002602
2603 rb_advance_iter(iter);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002604 out:
2605 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002606
Lai Jiangshan334d4162009-04-24 11:27:05 +08002607 if (event && event->type_len == RINGBUF_TYPE_PADDING) {
Tom Zanussi2d622712009-03-22 03:30:49 -05002608 cpu_relax();
2609 goto again;
2610 }
2611
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002612 return event;
2613}
Robert Richterc4f50182008-12-11 16:49:22 +01002614EXPORT_SYMBOL_GPL(ring_buffer_read);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002615
2616/**
2617 * ring_buffer_size - return the size of the ring buffer (in bytes)
2618 * @buffer: The ring buffer.
2619 */
2620unsigned long ring_buffer_size(struct ring_buffer *buffer)
2621{
2622 return BUF_PAGE_SIZE * buffer->pages;
2623}
Robert Richterc4f50182008-12-11 16:49:22 +01002624EXPORT_SYMBOL_GPL(ring_buffer_size);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002625
2626static void
2627rb_reset_cpu(struct ring_buffer_per_cpu *cpu_buffer)
2628{
2629 cpu_buffer->head_page
2630 = list_entry(cpu_buffer->pages.next, struct buffer_page, list);
Steven Rostedtbf41a152008-10-04 02:00:59 -04002631 local_set(&cpu_buffer->head_page->write, 0);
Steven Rostedtabc9b562008-12-02 15:34:06 -05002632 local_set(&cpu_buffer->head_page->page->commit, 0);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002633
Steven Rostedt6f807ac2008-10-04 02:00:58 -04002634 cpu_buffer->head_page->read = 0;
Steven Rostedtbf41a152008-10-04 02:00:59 -04002635
2636 cpu_buffer->tail_page = cpu_buffer->head_page;
2637 cpu_buffer->commit_page = cpu_buffer->head_page;
2638
2639 INIT_LIST_HEAD(&cpu_buffer->reader_page->list);
2640 local_set(&cpu_buffer->reader_page->write, 0);
Steven Rostedtabc9b562008-12-02 15:34:06 -05002641 local_set(&cpu_buffer->reader_page->page->commit, 0);
Steven Rostedt6f807ac2008-10-04 02:00:58 -04002642 cpu_buffer->reader_page->read = 0;
Steven Rostedtd7690412008-10-01 00:29:53 -04002643
Steven Rostedtf0d2c682009-04-29 13:43:37 -04002644 cpu_buffer->nmi_dropped = 0;
2645 cpu_buffer->commit_overrun = 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002646 cpu_buffer->overrun = 0;
Steven Rostedte4906ef2009-04-30 20:49:44 -04002647 cpu_buffer->read = 0;
2648 local_set(&cpu_buffer->entries, 0);
Steven Rostedt69507c02009-01-21 18:45:57 -05002649
2650 cpu_buffer->write_stamp = 0;
2651 cpu_buffer->read_stamp = 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002652}
2653
2654/**
2655 * ring_buffer_reset_cpu - reset a ring buffer per CPU buffer
2656 * @buffer: The ring buffer to reset a per cpu buffer of
2657 * @cpu: The CPU buffer to be reset
2658 */
2659void ring_buffer_reset_cpu(struct ring_buffer *buffer, int cpu)
2660{
2661 struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
2662 unsigned long flags;
2663
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302664 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04002665 return;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002666
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002667 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
2668
Steven Rostedt3e03fb72008-11-06 00:09:43 -05002669 __raw_spin_lock(&cpu_buffer->lock);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002670
2671 rb_reset_cpu(cpu_buffer);
2672
Steven Rostedt3e03fb72008-11-06 00:09:43 -05002673 __raw_spin_unlock(&cpu_buffer->lock);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002674
2675 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002676}
Robert Richterc4f50182008-12-11 16:49:22 +01002677EXPORT_SYMBOL_GPL(ring_buffer_reset_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002678
2679/**
2680 * ring_buffer_reset - reset a ring buffer
2681 * @buffer: The ring buffer to reset all cpu buffers
2682 */
2683void ring_buffer_reset(struct ring_buffer *buffer)
2684{
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002685 int cpu;
2686
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002687 for_each_buffer_cpu(buffer, cpu)
Steven Rostedtd7690412008-10-01 00:29:53 -04002688 ring_buffer_reset_cpu(buffer, cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002689}
Robert Richterc4f50182008-12-11 16:49:22 +01002690EXPORT_SYMBOL_GPL(ring_buffer_reset);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002691
2692/**
2693 * rind_buffer_empty - is the ring buffer empty?
2694 * @buffer: The ring buffer to test
2695 */
2696int ring_buffer_empty(struct ring_buffer *buffer)
2697{
2698 struct ring_buffer_per_cpu *cpu_buffer;
2699 int cpu;
2700
2701 /* yes this is racy, but if you don't like the race, lock the buffer */
2702 for_each_buffer_cpu(buffer, cpu) {
2703 cpu_buffer = buffer->buffers[cpu];
2704 if (!rb_per_cpu_empty(cpu_buffer))
2705 return 0;
2706 }
Steven Rostedt554f7862009-03-11 22:00:13 -04002707
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002708 return 1;
2709}
Robert Richterc4f50182008-12-11 16:49:22 +01002710EXPORT_SYMBOL_GPL(ring_buffer_empty);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002711
2712/**
2713 * ring_buffer_empty_cpu - is a cpu buffer of a ring buffer empty?
2714 * @buffer: The ring buffer
2715 * @cpu: The CPU buffer to test
2716 */
2717int ring_buffer_empty_cpu(struct ring_buffer *buffer, int cpu)
2718{
2719 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedt8aabee52009-03-12 13:13:49 -04002720 int ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002721
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302722 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04002723 return 1;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002724
2725 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt554f7862009-03-11 22:00:13 -04002726 ret = rb_per_cpu_empty(cpu_buffer);
2727
Steven Rostedt554f7862009-03-11 22:00:13 -04002728
2729 return ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002730}
Robert Richterc4f50182008-12-11 16:49:22 +01002731EXPORT_SYMBOL_GPL(ring_buffer_empty_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002732
2733/**
2734 * ring_buffer_swap_cpu - swap a CPU buffer between two ring buffers
2735 * @buffer_a: One buffer to swap with
2736 * @buffer_b: The other buffer to swap with
2737 *
2738 * This function is useful for tracers that want to take a "snapshot"
2739 * of a CPU buffer and has another back up buffer lying around.
2740 * it is expected that the tracer handles the cpu buffer not being
2741 * used at the moment.
2742 */
2743int ring_buffer_swap_cpu(struct ring_buffer *buffer_a,
2744 struct ring_buffer *buffer_b, int cpu)
2745{
2746 struct ring_buffer_per_cpu *cpu_buffer_a;
2747 struct ring_buffer_per_cpu *cpu_buffer_b;
Steven Rostedt554f7862009-03-11 22:00:13 -04002748 int ret = -EINVAL;
2749
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302750 if (!cpumask_test_cpu(cpu, buffer_a->cpumask) ||
2751 !cpumask_test_cpu(cpu, buffer_b->cpumask))
Steven Rostedt554f7862009-03-11 22:00:13 -04002752 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002753
2754 /* At least make sure the two buffers are somewhat the same */
Lai Jiangshan6d102bc2008-12-17 17:48:23 +08002755 if (buffer_a->pages != buffer_b->pages)
Steven Rostedt554f7862009-03-11 22:00:13 -04002756 goto out;
2757
2758 ret = -EAGAIN;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002759
Steven Rostedt97b17ef2009-01-21 15:24:56 -05002760 if (ring_buffer_flags != RB_BUFFERS_ON)
Steven Rostedt554f7862009-03-11 22:00:13 -04002761 goto out;
Steven Rostedt97b17ef2009-01-21 15:24:56 -05002762
2763 if (atomic_read(&buffer_a->record_disabled))
Steven Rostedt554f7862009-03-11 22:00:13 -04002764 goto out;
Steven Rostedt97b17ef2009-01-21 15:24:56 -05002765
2766 if (atomic_read(&buffer_b->record_disabled))
Steven Rostedt554f7862009-03-11 22:00:13 -04002767 goto out;
Steven Rostedt97b17ef2009-01-21 15:24:56 -05002768
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002769 cpu_buffer_a = buffer_a->buffers[cpu];
2770 cpu_buffer_b = buffer_b->buffers[cpu];
2771
Steven Rostedt97b17ef2009-01-21 15:24:56 -05002772 if (atomic_read(&cpu_buffer_a->record_disabled))
Steven Rostedt554f7862009-03-11 22:00:13 -04002773 goto out;
Steven Rostedt97b17ef2009-01-21 15:24:56 -05002774
2775 if (atomic_read(&cpu_buffer_b->record_disabled))
Steven Rostedt554f7862009-03-11 22:00:13 -04002776 goto out;
Steven Rostedt97b17ef2009-01-21 15:24:56 -05002777
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002778 /*
2779 * We can't do a synchronize_sched here because this
2780 * function can be called in atomic context.
2781 * Normally this will be called from the same CPU as cpu.
2782 * If not it's up to the caller to protect this.
2783 */
2784 atomic_inc(&cpu_buffer_a->record_disabled);
2785 atomic_inc(&cpu_buffer_b->record_disabled);
2786
2787 buffer_a->buffers[cpu] = cpu_buffer_b;
2788 buffer_b->buffers[cpu] = cpu_buffer_a;
2789
2790 cpu_buffer_b->buffer = buffer_a;
2791 cpu_buffer_a->buffer = buffer_b;
2792
2793 atomic_dec(&cpu_buffer_a->record_disabled);
2794 atomic_dec(&cpu_buffer_b->record_disabled);
2795
Steven Rostedt554f7862009-03-11 22:00:13 -04002796 ret = 0;
2797out:
Steven Rostedt554f7862009-03-11 22:00:13 -04002798 return ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002799}
Robert Richterc4f50182008-12-11 16:49:22 +01002800EXPORT_SYMBOL_GPL(ring_buffer_swap_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002801
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002802static void rb_remove_entries(struct ring_buffer_per_cpu *cpu_buffer,
Lai Jiangshan667d2412009-02-09 14:21:17 +08002803 struct buffer_data_page *bpage,
2804 unsigned int offset)
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002805{
2806 struct ring_buffer_event *event;
2807 unsigned long head;
2808
2809 __raw_spin_lock(&cpu_buffer->lock);
Lai Jiangshan667d2412009-02-09 14:21:17 +08002810 for (head = offset; head < local_read(&bpage->commit);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002811 head += rb_event_length(event)) {
2812
Steven Rostedt044fa782008-12-02 23:50:03 -05002813 event = __rb_data_page_index(bpage, head);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002814 if (RB_WARN_ON(cpu_buffer, rb_null_event(event)))
2815 return;
2816 /* Only count data entries */
Lai Jiangshan334d4162009-04-24 11:27:05 +08002817 if (event->type_len > RINGBUF_TYPE_DATA_TYPE_LEN_MAX)
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002818 continue;
Steven Rostedte4906ef2009-04-30 20:49:44 -04002819 cpu_buffer->read++;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002820 }
2821 __raw_spin_unlock(&cpu_buffer->lock);
2822}
2823
2824/**
2825 * ring_buffer_alloc_read_page - allocate a page to read from buffer
2826 * @buffer: the buffer to allocate for.
2827 *
2828 * This function is used in conjunction with ring_buffer_read_page.
2829 * When reading a full page from the ring buffer, these functions
2830 * can be used to speed up the process. The calling function should
2831 * allocate a few pages first with this function. Then when it
2832 * needs to get pages from the ring buffer, it passes the result
2833 * of this function into ring_buffer_read_page, which will swap
2834 * the page that was allocated, with the read page of the buffer.
2835 *
2836 * Returns:
2837 * The page allocated, or NULL on error.
2838 */
2839void *ring_buffer_alloc_read_page(struct ring_buffer *buffer)
2840{
Steven Rostedt044fa782008-12-02 23:50:03 -05002841 struct buffer_data_page *bpage;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05002842 unsigned long addr;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002843
2844 addr = __get_free_page(GFP_KERNEL);
2845 if (!addr)
2846 return NULL;
2847
Steven Rostedt044fa782008-12-02 23:50:03 -05002848 bpage = (void *)addr;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002849
Steven Rostedtef7a4a12009-03-03 00:27:49 -05002850 rb_init_page(bpage);
2851
Steven Rostedt044fa782008-12-02 23:50:03 -05002852 return bpage;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002853}
Steven Rostedtd6ce96d2009-05-05 01:15:24 -04002854EXPORT_SYMBOL_GPL(ring_buffer_alloc_read_page);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002855
2856/**
2857 * ring_buffer_free_read_page - free an allocated read page
2858 * @buffer: the buffer the page was allocate for
2859 * @data: the page to free
2860 *
2861 * Free a page allocated from ring_buffer_alloc_read_page.
2862 */
2863void ring_buffer_free_read_page(struct ring_buffer *buffer, void *data)
2864{
2865 free_page((unsigned long)data);
2866}
Steven Rostedtd6ce96d2009-05-05 01:15:24 -04002867EXPORT_SYMBOL_GPL(ring_buffer_free_read_page);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002868
2869/**
2870 * ring_buffer_read_page - extract a page from the ring buffer
2871 * @buffer: buffer to extract from
2872 * @data_page: the page to use allocated from ring_buffer_alloc_read_page
Steven Rostedtef7a4a12009-03-03 00:27:49 -05002873 * @len: amount to extract
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002874 * @cpu: the cpu of the buffer to extract
2875 * @full: should the extraction only happen when the page is full.
2876 *
2877 * This function will pull out a page from the ring buffer and consume it.
2878 * @data_page must be the address of the variable that was returned
2879 * from ring_buffer_alloc_read_page. This is because the page might be used
2880 * to swap with a page in the ring buffer.
2881 *
2882 * for example:
Lai Jiangshanb85fa012009-02-09 14:21:14 +08002883 * rpage = ring_buffer_alloc_read_page(buffer);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002884 * if (!rpage)
2885 * return error;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05002886 * ret = ring_buffer_read_page(buffer, &rpage, len, cpu, 0);
Lai Jiangshan667d2412009-02-09 14:21:17 +08002887 * if (ret >= 0)
2888 * process_page(rpage, ret);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002889 *
2890 * When @full is set, the function will not return true unless
2891 * the writer is off the reader page.
2892 *
2893 * Note: it is up to the calling functions to handle sleeps and wakeups.
2894 * The ring buffer can be used anywhere in the kernel and can not
2895 * blindly call wake_up. The layer that uses the ring buffer must be
2896 * responsible for that.
2897 *
2898 * Returns:
Lai Jiangshan667d2412009-02-09 14:21:17 +08002899 * >=0 if data has been transferred, returns the offset of consumed data.
2900 * <0 if no data has been transferred.
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002901 */
2902int ring_buffer_read_page(struct ring_buffer *buffer,
Steven Rostedtef7a4a12009-03-03 00:27:49 -05002903 void **data_page, size_t len, int cpu, int full)
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002904{
2905 struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
2906 struct ring_buffer_event *event;
Steven Rostedt044fa782008-12-02 23:50:03 -05002907 struct buffer_data_page *bpage;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05002908 struct buffer_page *reader;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002909 unsigned long flags;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05002910 unsigned int commit;
Lai Jiangshan667d2412009-02-09 14:21:17 +08002911 unsigned int read;
Steven Rostedt4f3640f2009-03-03 23:52:42 -05002912 u64 save_timestamp;
Lai Jiangshan667d2412009-02-09 14:21:17 +08002913 int ret = -1;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002914
Steven Rostedt554f7862009-03-11 22:00:13 -04002915 if (!cpumask_test_cpu(cpu, buffer->cpumask))
2916 goto out;
2917
Steven Rostedt474d32b2009-03-03 19:51:40 -05002918 /*
2919 * If len is not big enough to hold the page header, then
2920 * we can not copy anything.
2921 */
2922 if (len <= BUF_PAGE_HDR_SIZE)
Steven Rostedt554f7862009-03-11 22:00:13 -04002923 goto out;
Steven Rostedt474d32b2009-03-03 19:51:40 -05002924
2925 len -= BUF_PAGE_HDR_SIZE;
2926
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002927 if (!data_page)
Steven Rostedt554f7862009-03-11 22:00:13 -04002928 goto out;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002929
Steven Rostedt044fa782008-12-02 23:50:03 -05002930 bpage = *data_page;
2931 if (!bpage)
Steven Rostedt554f7862009-03-11 22:00:13 -04002932 goto out;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002933
2934 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
2935
Steven Rostedtef7a4a12009-03-03 00:27:49 -05002936 reader = rb_get_reader_page(cpu_buffer);
2937 if (!reader)
Steven Rostedt554f7862009-03-11 22:00:13 -04002938 goto out_unlock;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002939
Steven Rostedtef7a4a12009-03-03 00:27:49 -05002940 event = rb_reader_event(cpu_buffer);
Lai Jiangshan667d2412009-02-09 14:21:17 +08002941
Steven Rostedtef7a4a12009-03-03 00:27:49 -05002942 read = reader->read;
2943 commit = rb_page_commit(reader);
2944
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002945 /*
Steven Rostedt474d32b2009-03-03 19:51:40 -05002946 * If this page has been partially read or
2947 * if len is not big enough to read the rest of the page or
2948 * a writer is still on the page, then
2949 * we must copy the data from the page to the buffer.
2950 * Otherwise, we can simply swap the page with the one passed in.
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002951 */
Steven Rostedt474d32b2009-03-03 19:51:40 -05002952 if (read || (len < (commit - read)) ||
Steven Rostedtef7a4a12009-03-03 00:27:49 -05002953 cpu_buffer->reader_page == cpu_buffer->commit_page) {
Lai Jiangshan667d2412009-02-09 14:21:17 +08002954 struct buffer_data_page *rpage = cpu_buffer->reader_page->page;
Steven Rostedt474d32b2009-03-03 19:51:40 -05002955 unsigned int rpos = read;
2956 unsigned int pos = 0;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05002957 unsigned int size;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002958
2959 if (full)
Steven Rostedt554f7862009-03-11 22:00:13 -04002960 goto out_unlock;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002961
Steven Rostedtef7a4a12009-03-03 00:27:49 -05002962 if (len > (commit - read))
2963 len = (commit - read);
2964
2965 size = rb_event_length(event);
2966
2967 if (len < size)
Steven Rostedt554f7862009-03-11 22:00:13 -04002968 goto out_unlock;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05002969
Steven Rostedt4f3640f2009-03-03 23:52:42 -05002970 /* save the current timestamp, since the user will need it */
2971 save_timestamp = cpu_buffer->read_stamp;
2972
Steven Rostedtef7a4a12009-03-03 00:27:49 -05002973 /* Need to copy one event at a time */
2974 do {
Steven Rostedt474d32b2009-03-03 19:51:40 -05002975 memcpy(bpage->data + pos, rpage->data + rpos, size);
Steven Rostedtef7a4a12009-03-03 00:27:49 -05002976
2977 len -= size;
2978
2979 rb_advance_reader(cpu_buffer);
Steven Rostedt474d32b2009-03-03 19:51:40 -05002980 rpos = reader->read;
2981 pos += size;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05002982
2983 event = rb_reader_event(cpu_buffer);
2984 size = rb_event_length(event);
2985 } while (len > size);
Lai Jiangshan667d2412009-02-09 14:21:17 +08002986
2987 /* update bpage */
Steven Rostedtef7a4a12009-03-03 00:27:49 -05002988 local_set(&bpage->commit, pos);
Steven Rostedt4f3640f2009-03-03 23:52:42 -05002989 bpage->time_stamp = save_timestamp;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05002990
Steven Rostedt474d32b2009-03-03 19:51:40 -05002991 /* we copied everything to the beginning */
2992 read = 0;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002993 } else {
2994 /* swap the pages */
Steven Rostedt044fa782008-12-02 23:50:03 -05002995 rb_init_page(bpage);
Steven Rostedtef7a4a12009-03-03 00:27:49 -05002996 bpage = reader->page;
2997 reader->page = *data_page;
2998 local_set(&reader->write, 0);
2999 reader->read = 0;
Steven Rostedt044fa782008-12-02 23:50:03 -05003000 *data_page = bpage;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003001
3002 /* update the entry counter */
3003 rb_remove_entries(cpu_buffer, bpage, read);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003004 }
Lai Jiangshan667d2412009-02-09 14:21:17 +08003005 ret = read;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003006
Steven Rostedt554f7862009-03-11 22:00:13 -04003007 out_unlock:
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003008 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
3009
Steven Rostedt554f7862009-03-11 22:00:13 -04003010 out:
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003011 return ret;
3012}
Steven Rostedtd6ce96d2009-05-05 01:15:24 -04003013EXPORT_SYMBOL_GPL(ring_buffer_read_page);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003014
Steven Rostedta3583242008-11-11 15:01:42 -05003015static ssize_t
3016rb_simple_read(struct file *filp, char __user *ubuf,
3017 size_t cnt, loff_t *ppos)
3018{
Hannes Eder5e398412009-02-10 19:44:34 +01003019 unsigned long *p = filp->private_data;
Steven Rostedta3583242008-11-11 15:01:42 -05003020 char buf[64];
3021 int r;
3022
Steven Rostedt033601a2008-11-21 12:41:55 -05003023 if (test_bit(RB_BUFFERS_DISABLED_BIT, p))
3024 r = sprintf(buf, "permanently disabled\n");
3025 else
3026 r = sprintf(buf, "%d\n", test_bit(RB_BUFFERS_ON_BIT, p));
Steven Rostedta3583242008-11-11 15:01:42 -05003027
3028 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
3029}
3030
3031static ssize_t
3032rb_simple_write(struct file *filp, const char __user *ubuf,
3033 size_t cnt, loff_t *ppos)
3034{
Hannes Eder5e398412009-02-10 19:44:34 +01003035 unsigned long *p = filp->private_data;
Steven Rostedta3583242008-11-11 15:01:42 -05003036 char buf[64];
Hannes Eder5e398412009-02-10 19:44:34 +01003037 unsigned long val;
Steven Rostedta3583242008-11-11 15:01:42 -05003038 int ret;
3039
3040 if (cnt >= sizeof(buf))
3041 return -EINVAL;
3042
3043 if (copy_from_user(&buf, ubuf, cnt))
3044 return -EFAULT;
3045
3046 buf[cnt] = 0;
3047
3048 ret = strict_strtoul(buf, 10, &val);
3049 if (ret < 0)
3050 return ret;
3051
Steven Rostedt033601a2008-11-21 12:41:55 -05003052 if (val)
3053 set_bit(RB_BUFFERS_ON_BIT, p);
3054 else
3055 clear_bit(RB_BUFFERS_ON_BIT, p);
Steven Rostedta3583242008-11-11 15:01:42 -05003056
3057 (*ppos)++;
3058
3059 return cnt;
3060}
3061
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003062static const struct file_operations rb_simple_fops = {
Steven Rostedta3583242008-11-11 15:01:42 -05003063 .open = tracing_open_generic,
3064 .read = rb_simple_read,
3065 .write = rb_simple_write,
3066};
3067
3068
3069static __init int rb_init_debugfs(void)
3070{
3071 struct dentry *d_tracer;
Steven Rostedta3583242008-11-11 15:01:42 -05003072
3073 d_tracer = tracing_init_dentry();
3074
Frederic Weisbecker5452af62009-03-27 00:25:38 +01003075 trace_create_file("tracing_on", 0644, d_tracer,
3076 &ring_buffer_flags, &rb_simple_fops);
Steven Rostedta3583242008-11-11 15:01:42 -05003077
3078 return 0;
3079}
3080
3081fs_initcall(rb_init_debugfs);
Steven Rostedt554f7862009-03-11 22:00:13 -04003082
Steven Rostedt59222ef2009-03-12 11:46:03 -04003083#ifdef CONFIG_HOTPLUG_CPU
Frederic Weisbecker09c9e842009-03-21 04:33:36 +01003084static int rb_cpu_notify(struct notifier_block *self,
3085 unsigned long action, void *hcpu)
Steven Rostedt554f7862009-03-11 22:00:13 -04003086{
3087 struct ring_buffer *buffer =
3088 container_of(self, struct ring_buffer, cpu_notify);
3089 long cpu = (long)hcpu;
3090
3091 switch (action) {
3092 case CPU_UP_PREPARE:
3093 case CPU_UP_PREPARE_FROZEN:
3094 if (cpu_isset(cpu, *buffer->cpumask))
3095 return NOTIFY_OK;
3096
3097 buffer->buffers[cpu] =
3098 rb_allocate_cpu_buffer(buffer, cpu);
3099 if (!buffer->buffers[cpu]) {
3100 WARN(1, "failed to allocate ring buffer on CPU %ld\n",
3101 cpu);
3102 return NOTIFY_OK;
3103 }
3104 smp_wmb();
3105 cpu_set(cpu, *buffer->cpumask);
3106 break;
3107 case CPU_DOWN_PREPARE:
3108 case CPU_DOWN_PREPARE_FROZEN:
3109 /*
3110 * Do nothing.
3111 * If we were to free the buffer, then the user would
3112 * lose any trace that was in the buffer.
3113 */
3114 break;
3115 default:
3116 break;
3117 }
3118 return NOTIFY_OK;
3119}
3120#endif