blob: 16b24d49604caed48b909b03914605232638d6c6 [file] [log] [blame]
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001/*
2 * Generic ring buffer
3 *
4 * Copyright (C) 2008 Steven Rostedt <srostedt@redhat.com>
5 */
6#include <linux/ring_buffer.h>
Ingo Molnar14131f22009-02-26 18:47:11 +01007#include <linux/trace_clock.h>
Steven Rostedt78d904b2009-02-05 18:43:07 -05008#include <linux/ftrace_irq.h>
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04009#include <linux/spinlock.h>
10#include <linux/debugfs.h>
11#include <linux/uaccess.h>
Steven Rostedta81bd802009-02-06 01:45:16 -050012#include <linux/hardirq.h>
Steven Rostedt7a8e76a2008-09-29 23:02:38 -040013#include <linux/module.h>
14#include <linux/percpu.h>
15#include <linux/mutex.h>
Steven Rostedt7a8e76a2008-09-29 23:02:38 -040016#include <linux/init.h>
17#include <linux/hash.h>
18#include <linux/list.h>
Steven Rostedt554f7862009-03-11 22:00:13 -040019#include <linux/cpu.h>
Steven Rostedt7a8e76a2008-09-29 23:02:38 -040020#include <linux/fs.h>
21
Steven Rostedt182e9f52008-11-03 23:15:56 -050022#include "trace.h"
23
Steven Rostedt033601a2008-11-21 12:41:55 -050024/*
Steven Rostedtd1b182a2009-04-15 16:53:47 -040025 * The ring buffer header is special. We must manually up keep it.
26 */
27int ring_buffer_print_entry_header(struct trace_seq *s)
28{
29 int ret;
30
Lai Jiangshan334d4162009-04-24 11:27:05 +080031 ret = trace_seq_printf(s, "# compressed entry header\n");
32 ret = trace_seq_printf(s, "\ttype_len : 5 bits\n");
Steven Rostedtd1b182a2009-04-15 16:53:47 -040033 ret = trace_seq_printf(s, "\ttime_delta : 27 bits\n");
34 ret = trace_seq_printf(s, "\tarray : 32 bits\n");
35 ret = trace_seq_printf(s, "\n");
36 ret = trace_seq_printf(s, "\tpadding : type == %d\n",
37 RINGBUF_TYPE_PADDING);
38 ret = trace_seq_printf(s, "\ttime_extend : type == %d\n",
39 RINGBUF_TYPE_TIME_EXTEND);
Lai Jiangshan334d4162009-04-24 11:27:05 +080040 ret = trace_seq_printf(s, "\tdata max type_len == %d\n",
41 RINGBUF_TYPE_DATA_TYPE_LEN_MAX);
Steven Rostedtd1b182a2009-04-15 16:53:47 -040042
43 return ret;
44}
45
46/*
Steven Rostedt5cc98542009-03-12 22:24:17 -040047 * The ring buffer is made up of a list of pages. A separate list of pages is
48 * allocated for each CPU. A writer may only write to a buffer that is
49 * associated with the CPU it is currently executing on. A reader may read
50 * from any per cpu buffer.
51 *
52 * The reader is special. For each per cpu buffer, the reader has its own
53 * reader page. When a reader has read the entire reader page, this reader
54 * page is swapped with another page in the ring buffer.
55 *
56 * Now, as long as the writer is off the reader page, the reader can do what
57 * ever it wants with that page. The writer will never write to that page
58 * again (as long as it is out of the ring buffer).
59 *
60 * Here's some silly ASCII art.
61 *
62 * +------+
63 * |reader| RING BUFFER
64 * |page |
65 * +------+ +---+ +---+ +---+
66 * | |-->| |-->| |
67 * +---+ +---+ +---+
68 * ^ |
69 * | |
70 * +---------------+
71 *
72 *
73 * +------+
74 * |reader| RING BUFFER
75 * |page |------------------v
76 * +------+ +---+ +---+ +---+
77 * | |-->| |-->| |
78 * +---+ +---+ +---+
79 * ^ |
80 * | |
81 * +---------------+
82 *
83 *
84 * +------+
85 * |reader| RING BUFFER
86 * |page |------------------v
87 * +------+ +---+ +---+ +---+
88 * ^ | |-->| |-->| |
89 * | +---+ +---+ +---+
90 * | |
91 * | |
92 * +------------------------------+
93 *
94 *
95 * +------+
96 * |buffer| RING BUFFER
97 * |page |------------------v
98 * +------+ +---+ +---+ +---+
99 * ^ | | | |-->| |
100 * | New +---+ +---+ +---+
101 * | Reader------^ |
102 * | page |
103 * +------------------------------+
104 *
105 *
106 * After we make this swap, the reader can hand this page off to the splice
107 * code and be done with it. It can even allocate a new page if it needs to
108 * and swap that into the ring buffer.
109 *
110 * We will be using cmpxchg soon to make all this lockless.
111 *
112 */
113
114/*
Steven Rostedt033601a2008-11-21 12:41:55 -0500115 * A fast way to enable or disable all ring buffers is to
116 * call tracing_on or tracing_off. Turning off the ring buffers
117 * prevents all ring buffers from being recorded to.
118 * Turning this switch on, makes it OK to write to the
119 * ring buffer, if the ring buffer is enabled itself.
120 *
121 * There's three layers that must be on in order to write
122 * to the ring buffer.
123 *
124 * 1) This global flag must be set.
125 * 2) The ring buffer must be enabled for recording.
126 * 3) The per cpu buffer must be enabled for recording.
127 *
128 * In case of an anomaly, this global flag has a bit set that
129 * will permantly disable all ring buffers.
130 */
131
132/*
133 * Global flag to disable all recording to ring buffers
134 * This has two bits: ON, DISABLED
135 *
136 * ON DISABLED
137 * ---- ----------
138 * 0 0 : ring buffers are off
139 * 1 0 : ring buffers are on
140 * X 1 : ring buffers are permanently disabled
141 */
142
143enum {
144 RB_BUFFERS_ON_BIT = 0,
145 RB_BUFFERS_DISABLED_BIT = 1,
146};
147
148enum {
149 RB_BUFFERS_ON = 1 << RB_BUFFERS_ON_BIT,
150 RB_BUFFERS_DISABLED = 1 << RB_BUFFERS_DISABLED_BIT,
151};
152
Hannes Eder5e398412009-02-10 19:44:34 +0100153static unsigned long ring_buffer_flags __read_mostly = RB_BUFFERS_ON;
Steven Rostedta3583242008-11-11 15:01:42 -0500154
Steven Rostedt474d32b2009-03-03 19:51:40 -0500155#define BUF_PAGE_HDR_SIZE offsetof(struct buffer_data_page, data)
156
Steven Rostedta3583242008-11-11 15:01:42 -0500157/**
158 * tracing_on - enable all tracing buffers
159 *
160 * This function enables all tracing buffers that may have been
161 * disabled with tracing_off.
162 */
163void tracing_on(void)
164{
Steven Rostedt033601a2008-11-21 12:41:55 -0500165 set_bit(RB_BUFFERS_ON_BIT, &ring_buffer_flags);
Steven Rostedta3583242008-11-11 15:01:42 -0500166}
Robert Richterc4f50182008-12-11 16:49:22 +0100167EXPORT_SYMBOL_GPL(tracing_on);
Steven Rostedta3583242008-11-11 15:01:42 -0500168
169/**
170 * tracing_off - turn off all tracing buffers
171 *
172 * This function stops all tracing buffers from recording data.
173 * It does not disable any overhead the tracers themselves may
174 * be causing. This function simply causes all recording to
175 * the ring buffers to fail.
176 */
177void tracing_off(void)
178{
Steven Rostedt033601a2008-11-21 12:41:55 -0500179 clear_bit(RB_BUFFERS_ON_BIT, &ring_buffer_flags);
180}
Robert Richterc4f50182008-12-11 16:49:22 +0100181EXPORT_SYMBOL_GPL(tracing_off);
Steven Rostedt033601a2008-11-21 12:41:55 -0500182
183/**
184 * tracing_off_permanent - permanently disable ring buffers
185 *
186 * This function, once called, will disable all ring buffers
Wenji Huangc3706f02009-02-10 01:03:18 -0500187 * permanently.
Steven Rostedt033601a2008-11-21 12:41:55 -0500188 */
189void tracing_off_permanent(void)
190{
191 set_bit(RB_BUFFERS_DISABLED_BIT, &ring_buffer_flags);
Steven Rostedta3583242008-11-11 15:01:42 -0500192}
193
Steven Rostedt988ae9d2009-02-14 19:17:02 -0500194/**
195 * tracing_is_on - show state of ring buffers enabled
196 */
197int tracing_is_on(void)
198{
199 return ring_buffer_flags == RB_BUFFERS_ON;
200}
201EXPORT_SYMBOL_GPL(tracing_is_on);
202
Ingo Molnard06bbd62008-11-12 10:11:37 +0100203#include "trace.h"
204
Steven Rostedte3d6bf02009-03-03 13:53:07 -0500205#define RB_EVNT_HDR_SIZE (offsetof(struct ring_buffer_event, array))
Andrew Morton67d34722009-01-09 12:27:09 -0800206#define RB_ALIGNMENT 4U
Lai Jiangshan334d4162009-04-24 11:27:05 +0800207#define RB_MAX_SMALL_DATA (RB_ALIGNMENT * RINGBUF_TYPE_DATA_TYPE_LEN_MAX)
208
209/* define RINGBUF_TYPE_DATA for 'case RINGBUF_TYPE_DATA:' */
210#define RINGBUF_TYPE_DATA 0 ... RINGBUF_TYPE_DATA_TYPE_LEN_MAX
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400211
212enum {
213 RB_LEN_TIME_EXTEND = 8,
214 RB_LEN_TIME_STAMP = 16,
215};
216
Tom Zanussi2d622712009-03-22 03:30:49 -0500217static inline int rb_null_event(struct ring_buffer_event *event)
218{
Lai Jiangshan334d4162009-04-24 11:27:05 +0800219 return event->type_len == RINGBUF_TYPE_PADDING
220 && event->time_delta == 0;
Tom Zanussi2d622712009-03-22 03:30:49 -0500221}
222
223static inline int rb_discarded_event(struct ring_buffer_event *event)
224{
Lai Jiangshan334d4162009-04-24 11:27:05 +0800225 return event->type_len == RINGBUF_TYPE_PADDING && event->time_delta;
Tom Zanussi2d622712009-03-22 03:30:49 -0500226}
227
228static void rb_event_set_padding(struct ring_buffer_event *event)
229{
Lai Jiangshan334d4162009-04-24 11:27:05 +0800230 event->type_len = RINGBUF_TYPE_PADDING;
Tom Zanussi2d622712009-03-22 03:30:49 -0500231 event->time_delta = 0;
232}
233
Tom Zanussi2d622712009-03-22 03:30:49 -0500234static unsigned
235rb_event_data_length(struct ring_buffer_event *event)
236{
237 unsigned length;
238
Lai Jiangshan334d4162009-04-24 11:27:05 +0800239 if (event->type_len)
240 length = event->type_len * RB_ALIGNMENT;
Tom Zanussi2d622712009-03-22 03:30:49 -0500241 else
242 length = event->array[0];
243 return length + RB_EVNT_HDR_SIZE;
244}
245
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400246/* inline for ring buffer fast paths */
Andrew Morton34a148b2009-01-09 12:27:09 -0800247static unsigned
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400248rb_event_length(struct ring_buffer_event *event)
249{
Lai Jiangshan334d4162009-04-24 11:27:05 +0800250 switch (event->type_len) {
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400251 case RINGBUF_TYPE_PADDING:
Tom Zanussi2d622712009-03-22 03:30:49 -0500252 if (rb_null_event(event))
253 /* undefined */
254 return -1;
Lai Jiangshan334d4162009-04-24 11:27:05 +0800255 return event->array[0] + RB_EVNT_HDR_SIZE;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400256
257 case RINGBUF_TYPE_TIME_EXTEND:
258 return RB_LEN_TIME_EXTEND;
259
260 case RINGBUF_TYPE_TIME_STAMP:
261 return RB_LEN_TIME_STAMP;
262
263 case RINGBUF_TYPE_DATA:
Tom Zanussi2d622712009-03-22 03:30:49 -0500264 return rb_event_data_length(event);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400265 default:
266 BUG();
267 }
268 /* not hit */
269 return 0;
270}
271
272/**
273 * ring_buffer_event_length - return the length of the event
274 * @event: the event to get the length of
275 */
276unsigned ring_buffer_event_length(struct ring_buffer_event *event)
277{
Robert Richter465634a2009-01-07 15:32:11 +0100278 unsigned length = rb_event_length(event);
Lai Jiangshan334d4162009-04-24 11:27:05 +0800279 if (event->type_len > RINGBUF_TYPE_DATA_TYPE_LEN_MAX)
Robert Richter465634a2009-01-07 15:32:11 +0100280 return length;
281 length -= RB_EVNT_HDR_SIZE;
282 if (length > RB_MAX_SMALL_DATA + sizeof(event->array[0]))
283 length -= sizeof(event->array[0]);
284 return length;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400285}
Robert Richterc4f50182008-12-11 16:49:22 +0100286EXPORT_SYMBOL_GPL(ring_buffer_event_length);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400287
288/* inline for ring buffer fast paths */
Andrew Morton34a148b2009-01-09 12:27:09 -0800289static void *
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400290rb_event_data(struct ring_buffer_event *event)
291{
Lai Jiangshan334d4162009-04-24 11:27:05 +0800292 BUG_ON(event->type_len > RINGBUF_TYPE_DATA_TYPE_LEN_MAX);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400293 /* If length is in len field, then array[0] has the data */
Lai Jiangshan334d4162009-04-24 11:27:05 +0800294 if (event->type_len)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400295 return (void *)&event->array[0];
296 /* Otherwise length is in array[0] and array[1] has the data */
297 return (void *)&event->array[1];
298}
299
300/**
301 * ring_buffer_event_data - return the data of the event
302 * @event: the event to get the data from
303 */
304void *ring_buffer_event_data(struct ring_buffer_event *event)
305{
306 return rb_event_data(event);
307}
Robert Richterc4f50182008-12-11 16:49:22 +0100308EXPORT_SYMBOL_GPL(ring_buffer_event_data);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400309
310#define for_each_buffer_cpu(buffer, cpu) \
Rusty Russell9e01c1b2009-01-01 10:12:22 +1030311 for_each_cpu(cpu, buffer->cpumask)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400312
313#define TS_SHIFT 27
314#define TS_MASK ((1ULL << TS_SHIFT) - 1)
315#define TS_DELTA_TEST (~TS_MASK)
316
Steven Rostedtabc9b562008-12-02 15:34:06 -0500317struct buffer_data_page {
Steven Rostedte4c2ce82008-10-01 11:14:54 -0400318 u64 time_stamp; /* page time stamp */
Wenji Huangc3706f02009-02-10 01:03:18 -0500319 local_t commit; /* write committed index */
Steven Rostedtabc9b562008-12-02 15:34:06 -0500320 unsigned char data[]; /* data of buffer page */
321};
322
323struct buffer_page {
Steven Rostedt778c55d2009-05-01 18:44:45 -0400324 struct list_head list; /* list of buffer pages */
Steven Rostedtabc9b562008-12-02 15:34:06 -0500325 local_t write; /* index for next write */
Steven Rostedt6f807ac2008-10-04 02:00:58 -0400326 unsigned read; /* index for next read */
Steven Rostedt778c55d2009-05-01 18:44:45 -0400327 local_t entries; /* entries on this page */
Steven Rostedtabc9b562008-12-02 15:34:06 -0500328 struct buffer_data_page *page; /* Actual data page */
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400329};
330
Steven Rostedt044fa782008-12-02 23:50:03 -0500331static void rb_init_page(struct buffer_data_page *bpage)
Steven Rostedtabc9b562008-12-02 15:34:06 -0500332{
Steven Rostedt044fa782008-12-02 23:50:03 -0500333 local_set(&bpage->commit, 0);
Steven Rostedtabc9b562008-12-02 15:34:06 -0500334}
335
Steven Rostedt474d32b2009-03-03 19:51:40 -0500336/**
337 * ring_buffer_page_len - the size of data on the page.
338 * @page: The page to read
339 *
340 * Returns the amount of data on the page, including buffer page header.
341 */
Steven Rostedtef7a4a12009-03-03 00:27:49 -0500342size_t ring_buffer_page_len(void *page)
343{
Steven Rostedt474d32b2009-03-03 19:51:40 -0500344 return local_read(&((struct buffer_data_page *)page)->commit)
345 + BUF_PAGE_HDR_SIZE;
Steven Rostedtef7a4a12009-03-03 00:27:49 -0500346}
347
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400348/*
Steven Rostedted568292008-09-29 23:02:40 -0400349 * Also stolen from mm/slob.c. Thanks to Mathieu Desnoyers for pointing
350 * this issue out.
351 */
Andrew Morton34a148b2009-01-09 12:27:09 -0800352static void free_buffer_page(struct buffer_page *bpage)
Steven Rostedted568292008-09-29 23:02:40 -0400353{
Andrew Morton34a148b2009-01-09 12:27:09 -0800354 free_page((unsigned long)bpage->page);
Steven Rostedte4c2ce82008-10-01 11:14:54 -0400355 kfree(bpage);
Steven Rostedted568292008-09-29 23:02:40 -0400356}
357
358/*
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400359 * We need to fit the time_stamp delta into 27 bits.
360 */
361static inline int test_time_stamp(u64 delta)
362{
363 if (delta & TS_DELTA_TEST)
364 return 1;
365 return 0;
366}
367
Steven Rostedt474d32b2009-03-03 19:51:40 -0500368#define BUF_PAGE_SIZE (PAGE_SIZE - BUF_PAGE_HDR_SIZE)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400369
Steven Rostedtbe957c42009-05-11 14:42:53 -0400370/* Max payload is BUF_PAGE_SIZE - header (8bytes) */
371#define BUF_MAX_DATA_SIZE (BUF_PAGE_SIZE - (sizeof(u32) * 2))
372
Steven Rostedtd1b182a2009-04-15 16:53:47 -0400373int ring_buffer_print_page_header(struct trace_seq *s)
374{
375 struct buffer_data_page field;
376 int ret;
377
378 ret = trace_seq_printf(s, "\tfield: u64 timestamp;\t"
379 "offset:0;\tsize:%u;\n",
380 (unsigned int)sizeof(field.time_stamp));
381
382 ret = trace_seq_printf(s, "\tfield: local_t commit;\t"
383 "offset:%u;\tsize:%u;\n",
384 (unsigned int)offsetof(typeof(field), commit),
385 (unsigned int)sizeof(field.commit));
386
387 ret = trace_seq_printf(s, "\tfield: char data;\t"
388 "offset:%u;\tsize:%u;\n",
389 (unsigned int)offsetof(typeof(field), data),
390 (unsigned int)BUF_PAGE_SIZE);
391
392 return ret;
393}
394
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400395/*
396 * head_page == tail_page && head == tail then buffer is empty.
397 */
398struct ring_buffer_per_cpu {
399 int cpu;
400 struct ring_buffer *buffer;
Steven Rostedtf83c9d02008-11-11 18:47:44 +0100401 spinlock_t reader_lock; /* serialize readers */
Steven Rostedt3e03fb72008-11-06 00:09:43 -0500402 raw_spinlock_t lock;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400403 struct lock_class_key lock_key;
404 struct list_head pages;
Steven Rostedt6f807ac2008-10-04 02:00:58 -0400405 struct buffer_page *head_page; /* read from head */
406 struct buffer_page *tail_page; /* write to tail */
Wenji Huangc3706f02009-02-10 01:03:18 -0500407 struct buffer_page *commit_page; /* committed pages */
Steven Rostedtd7690412008-10-01 00:29:53 -0400408 struct buffer_page *reader_page;
Steven Rostedtf0d2c682009-04-29 13:43:37 -0400409 unsigned long nmi_dropped;
410 unsigned long commit_overrun;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400411 unsigned long overrun;
Steven Rostedte4906ef2009-04-30 20:49:44 -0400412 unsigned long read;
413 local_t entries;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400414 u64 write_stamp;
415 u64 read_stamp;
416 atomic_t record_disabled;
417};
418
419struct ring_buffer {
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400420 unsigned pages;
421 unsigned flags;
422 int cpus;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400423 atomic_t record_disabled;
Arnaldo Carvalho de Melo00f62f62009-02-09 17:04:06 -0200424 cpumask_var_t cpumask;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400425
426 struct mutex mutex;
427
428 struct ring_buffer_per_cpu **buffers;
Steven Rostedt554f7862009-03-11 22:00:13 -0400429
Steven Rostedt59222ef2009-03-12 11:46:03 -0400430#ifdef CONFIG_HOTPLUG_CPU
Steven Rostedt554f7862009-03-11 22:00:13 -0400431 struct notifier_block cpu_notify;
432#endif
Steven Rostedt37886f62009-03-17 17:22:06 -0400433 u64 (*clock)(void);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400434};
435
436struct ring_buffer_iter {
437 struct ring_buffer_per_cpu *cpu_buffer;
438 unsigned long head;
439 struct buffer_page *head_page;
440 u64 read_stamp;
441};
442
Steven Rostedtf536aaf2008-11-10 23:07:30 -0500443/* buffer may be either ring_buffer or ring_buffer_per_cpu */
Steven Rostedtbf41a152008-10-04 02:00:59 -0400444#define RB_WARN_ON(buffer, cond) \
Steven Rostedt3e89c7b2008-11-11 15:28:41 -0500445 ({ \
446 int _____ret = unlikely(cond); \
447 if (_____ret) { \
Steven Rostedtbf41a152008-10-04 02:00:59 -0400448 atomic_inc(&buffer->record_disabled); \
449 WARN_ON(1); \
450 } \
Steven Rostedt3e89c7b2008-11-11 15:28:41 -0500451 _____ret; \
452 })
Steven Rostedtf536aaf2008-11-10 23:07:30 -0500453
Steven Rostedt37886f62009-03-17 17:22:06 -0400454/* Up this if you want to test the TIME_EXTENTS and normalization */
455#define DEBUG_SHIFT 0
456
Steven Rostedt88eb0122009-05-11 16:28:23 -0400457static inline u64 rb_time_stamp(struct ring_buffer *buffer, int cpu)
458{
459 /* shift to debug/test normalization and TIME_EXTENTS */
460 return buffer->clock() << DEBUG_SHIFT;
461}
462
Steven Rostedt37886f62009-03-17 17:22:06 -0400463u64 ring_buffer_time_stamp(struct ring_buffer *buffer, int cpu)
464{
465 u64 time;
466
467 preempt_disable_notrace();
Steven Rostedt88eb0122009-05-11 16:28:23 -0400468 time = rb_time_stamp(buffer, cpu);
Steven Rostedt37886f62009-03-17 17:22:06 -0400469 preempt_enable_no_resched_notrace();
470
471 return time;
472}
473EXPORT_SYMBOL_GPL(ring_buffer_time_stamp);
474
475void ring_buffer_normalize_time_stamp(struct ring_buffer *buffer,
476 int cpu, u64 *ts)
477{
478 /* Just stupid testing the normalize function and deltas */
479 *ts >>= DEBUG_SHIFT;
480}
481EXPORT_SYMBOL_GPL(ring_buffer_normalize_time_stamp);
482
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400483/**
484 * check_pages - integrity check of buffer pages
485 * @cpu_buffer: CPU buffer with pages to test
486 *
Wenji Huangc3706f02009-02-10 01:03:18 -0500487 * As a safety measure we check to make sure the data pages have not
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400488 * been corrupted.
489 */
490static int rb_check_pages(struct ring_buffer_per_cpu *cpu_buffer)
491{
492 struct list_head *head = &cpu_buffer->pages;
Steven Rostedt044fa782008-12-02 23:50:03 -0500493 struct buffer_page *bpage, *tmp;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400494
Steven Rostedt3e89c7b2008-11-11 15:28:41 -0500495 if (RB_WARN_ON(cpu_buffer, head->next->prev != head))
496 return -1;
497 if (RB_WARN_ON(cpu_buffer, head->prev->next != head))
498 return -1;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400499
Steven Rostedt044fa782008-12-02 23:50:03 -0500500 list_for_each_entry_safe(bpage, tmp, head, list) {
Steven Rostedt3e89c7b2008-11-11 15:28:41 -0500501 if (RB_WARN_ON(cpu_buffer,
Steven Rostedt044fa782008-12-02 23:50:03 -0500502 bpage->list.next->prev != &bpage->list))
Steven Rostedt3e89c7b2008-11-11 15:28:41 -0500503 return -1;
504 if (RB_WARN_ON(cpu_buffer,
Steven Rostedt044fa782008-12-02 23:50:03 -0500505 bpage->list.prev->next != &bpage->list))
Steven Rostedt3e89c7b2008-11-11 15:28:41 -0500506 return -1;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400507 }
508
509 return 0;
510}
511
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400512static int rb_allocate_pages(struct ring_buffer_per_cpu *cpu_buffer,
513 unsigned nr_pages)
514{
515 struct list_head *head = &cpu_buffer->pages;
Steven Rostedt044fa782008-12-02 23:50:03 -0500516 struct buffer_page *bpage, *tmp;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400517 unsigned long addr;
518 LIST_HEAD(pages);
519 unsigned i;
520
521 for (i = 0; i < nr_pages; i++) {
Steven Rostedt044fa782008-12-02 23:50:03 -0500522 bpage = kzalloc_node(ALIGN(sizeof(*bpage), cache_line_size()),
Steven Rostedtaa1e0e32008-10-02 19:18:09 -0400523 GFP_KERNEL, cpu_to_node(cpu_buffer->cpu));
Steven Rostedt044fa782008-12-02 23:50:03 -0500524 if (!bpage)
Steven Rostedte4c2ce82008-10-01 11:14:54 -0400525 goto free_pages;
Steven Rostedt044fa782008-12-02 23:50:03 -0500526 list_add(&bpage->list, &pages);
Steven Rostedte4c2ce82008-10-01 11:14:54 -0400527
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400528 addr = __get_free_page(GFP_KERNEL);
529 if (!addr)
530 goto free_pages;
Steven Rostedt044fa782008-12-02 23:50:03 -0500531 bpage->page = (void *)addr;
532 rb_init_page(bpage->page);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400533 }
534
535 list_splice(&pages, head);
536
537 rb_check_pages(cpu_buffer);
538
539 return 0;
540
541 free_pages:
Steven Rostedt044fa782008-12-02 23:50:03 -0500542 list_for_each_entry_safe(bpage, tmp, &pages, list) {
543 list_del_init(&bpage->list);
544 free_buffer_page(bpage);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400545 }
546 return -ENOMEM;
547}
548
549static struct ring_buffer_per_cpu *
550rb_allocate_cpu_buffer(struct ring_buffer *buffer, int cpu)
551{
552 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedt044fa782008-12-02 23:50:03 -0500553 struct buffer_page *bpage;
Steven Rostedtd7690412008-10-01 00:29:53 -0400554 unsigned long addr;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400555 int ret;
556
557 cpu_buffer = kzalloc_node(ALIGN(sizeof(*cpu_buffer), cache_line_size()),
558 GFP_KERNEL, cpu_to_node(cpu));
559 if (!cpu_buffer)
560 return NULL;
561
562 cpu_buffer->cpu = cpu;
563 cpu_buffer->buffer = buffer;
Steven Rostedtf83c9d02008-11-11 18:47:44 +0100564 spin_lock_init(&cpu_buffer->reader_lock);
Steven Rostedt3e03fb72008-11-06 00:09:43 -0500565 cpu_buffer->lock = (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400566 INIT_LIST_HEAD(&cpu_buffer->pages);
567
Steven Rostedt044fa782008-12-02 23:50:03 -0500568 bpage = kzalloc_node(ALIGN(sizeof(*bpage), cache_line_size()),
Steven Rostedte4c2ce82008-10-01 11:14:54 -0400569 GFP_KERNEL, cpu_to_node(cpu));
Steven Rostedt044fa782008-12-02 23:50:03 -0500570 if (!bpage)
Steven Rostedte4c2ce82008-10-01 11:14:54 -0400571 goto fail_free_buffer;
572
Steven Rostedt044fa782008-12-02 23:50:03 -0500573 cpu_buffer->reader_page = bpage;
Steven Rostedtd7690412008-10-01 00:29:53 -0400574 addr = __get_free_page(GFP_KERNEL);
575 if (!addr)
Steven Rostedte4c2ce82008-10-01 11:14:54 -0400576 goto fail_free_reader;
Steven Rostedt044fa782008-12-02 23:50:03 -0500577 bpage->page = (void *)addr;
578 rb_init_page(bpage->page);
Steven Rostedte4c2ce82008-10-01 11:14:54 -0400579
Steven Rostedtd7690412008-10-01 00:29:53 -0400580 INIT_LIST_HEAD(&cpu_buffer->reader_page->list);
Steven Rostedtd7690412008-10-01 00:29:53 -0400581
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400582 ret = rb_allocate_pages(cpu_buffer, buffer->pages);
583 if (ret < 0)
Steven Rostedtd7690412008-10-01 00:29:53 -0400584 goto fail_free_reader;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400585
586 cpu_buffer->head_page
587 = list_entry(cpu_buffer->pages.next, struct buffer_page, list);
Steven Rostedtbf41a152008-10-04 02:00:59 -0400588 cpu_buffer->tail_page = cpu_buffer->commit_page = cpu_buffer->head_page;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400589
590 return cpu_buffer;
591
Steven Rostedtd7690412008-10-01 00:29:53 -0400592 fail_free_reader:
593 free_buffer_page(cpu_buffer->reader_page);
594
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400595 fail_free_buffer:
596 kfree(cpu_buffer);
597 return NULL;
598}
599
600static void rb_free_cpu_buffer(struct ring_buffer_per_cpu *cpu_buffer)
601{
602 struct list_head *head = &cpu_buffer->pages;
Steven Rostedt044fa782008-12-02 23:50:03 -0500603 struct buffer_page *bpage, *tmp;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400604
Steven Rostedtd7690412008-10-01 00:29:53 -0400605 free_buffer_page(cpu_buffer->reader_page);
606
Steven Rostedt044fa782008-12-02 23:50:03 -0500607 list_for_each_entry_safe(bpage, tmp, head, list) {
608 list_del_init(&bpage->list);
609 free_buffer_page(bpage);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400610 }
611 kfree(cpu_buffer);
612}
613
Steven Rostedta7b13742008-09-29 23:02:39 -0400614/*
615 * Causes compile errors if the struct buffer_page gets bigger
616 * than the struct page.
617 */
618extern int ring_buffer_page_too_big(void);
619
Steven Rostedt59222ef2009-03-12 11:46:03 -0400620#ifdef CONFIG_HOTPLUG_CPU
Frederic Weisbecker09c9e842009-03-21 04:33:36 +0100621static int rb_cpu_notify(struct notifier_block *self,
622 unsigned long action, void *hcpu);
Steven Rostedt554f7862009-03-11 22:00:13 -0400623#endif
624
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400625/**
626 * ring_buffer_alloc - allocate a new ring_buffer
Robert Richter68814b52008-11-24 12:24:12 +0100627 * @size: the size in bytes per cpu that is needed.
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400628 * @flags: attributes to set for the ring buffer.
629 *
630 * Currently the only flag that is available is the RB_FL_OVERWRITE
631 * flag. This flag means that the buffer will overwrite old data
632 * when the buffer wraps. If this flag is not set, the buffer will
633 * drop data when the tail hits the head.
634 */
635struct ring_buffer *ring_buffer_alloc(unsigned long size, unsigned flags)
636{
637 struct ring_buffer *buffer;
638 int bsize;
639 int cpu;
640
Steven Rostedta7b13742008-09-29 23:02:39 -0400641 /* Paranoid! Optimizes out when all is well */
642 if (sizeof(struct buffer_page) > sizeof(struct page))
643 ring_buffer_page_too_big();
644
645
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400646 /* keep it in its own cache line */
647 buffer = kzalloc(ALIGN(sizeof(*buffer), cache_line_size()),
648 GFP_KERNEL);
649 if (!buffer)
650 return NULL;
651
Rusty Russell9e01c1b2009-01-01 10:12:22 +1030652 if (!alloc_cpumask_var(&buffer->cpumask, GFP_KERNEL))
653 goto fail_free_buffer;
654
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400655 buffer->pages = DIV_ROUND_UP(size, BUF_PAGE_SIZE);
656 buffer->flags = flags;
Steven Rostedt37886f62009-03-17 17:22:06 -0400657 buffer->clock = trace_clock_local;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400658
659 /* need at least two pages */
660 if (buffer->pages == 1)
661 buffer->pages++;
662
Frederic Weisbecker3bf832c2009-03-19 14:47:33 +0100663 /*
664 * In case of non-hotplug cpu, if the ring-buffer is allocated
665 * in early initcall, it will not be notified of secondary cpus.
666 * In that off case, we need to allocate for all possible cpus.
667 */
668#ifdef CONFIG_HOTPLUG_CPU
Steven Rostedt554f7862009-03-11 22:00:13 -0400669 get_online_cpus();
670 cpumask_copy(buffer->cpumask, cpu_online_mask);
Frederic Weisbecker3bf832c2009-03-19 14:47:33 +0100671#else
672 cpumask_copy(buffer->cpumask, cpu_possible_mask);
673#endif
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400674 buffer->cpus = nr_cpu_ids;
675
676 bsize = sizeof(void *) * nr_cpu_ids;
677 buffer->buffers = kzalloc(ALIGN(bsize, cache_line_size()),
678 GFP_KERNEL);
679 if (!buffer->buffers)
Rusty Russell9e01c1b2009-01-01 10:12:22 +1030680 goto fail_free_cpumask;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400681
682 for_each_buffer_cpu(buffer, cpu) {
683 buffer->buffers[cpu] =
684 rb_allocate_cpu_buffer(buffer, cpu);
685 if (!buffer->buffers[cpu])
686 goto fail_free_buffers;
687 }
688
Steven Rostedt59222ef2009-03-12 11:46:03 -0400689#ifdef CONFIG_HOTPLUG_CPU
Steven Rostedt554f7862009-03-11 22:00:13 -0400690 buffer->cpu_notify.notifier_call = rb_cpu_notify;
691 buffer->cpu_notify.priority = 0;
692 register_cpu_notifier(&buffer->cpu_notify);
693#endif
694
695 put_online_cpus();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400696 mutex_init(&buffer->mutex);
697
698 return buffer;
699
700 fail_free_buffers:
701 for_each_buffer_cpu(buffer, cpu) {
702 if (buffer->buffers[cpu])
703 rb_free_cpu_buffer(buffer->buffers[cpu]);
704 }
705 kfree(buffer->buffers);
706
Rusty Russell9e01c1b2009-01-01 10:12:22 +1030707 fail_free_cpumask:
708 free_cpumask_var(buffer->cpumask);
Steven Rostedt554f7862009-03-11 22:00:13 -0400709 put_online_cpus();
Rusty Russell9e01c1b2009-01-01 10:12:22 +1030710
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400711 fail_free_buffer:
712 kfree(buffer);
713 return NULL;
714}
Robert Richterc4f50182008-12-11 16:49:22 +0100715EXPORT_SYMBOL_GPL(ring_buffer_alloc);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400716
717/**
718 * ring_buffer_free - free a ring buffer.
719 * @buffer: the buffer to free.
720 */
721void
722ring_buffer_free(struct ring_buffer *buffer)
723{
724 int cpu;
725
Steven Rostedt554f7862009-03-11 22:00:13 -0400726 get_online_cpus();
727
Steven Rostedt59222ef2009-03-12 11:46:03 -0400728#ifdef CONFIG_HOTPLUG_CPU
Steven Rostedt554f7862009-03-11 22:00:13 -0400729 unregister_cpu_notifier(&buffer->cpu_notify);
730#endif
731
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400732 for_each_buffer_cpu(buffer, cpu)
733 rb_free_cpu_buffer(buffer->buffers[cpu]);
734
Steven Rostedt554f7862009-03-11 22:00:13 -0400735 put_online_cpus();
736
Rusty Russell9e01c1b2009-01-01 10:12:22 +1030737 free_cpumask_var(buffer->cpumask);
738
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400739 kfree(buffer);
740}
Robert Richterc4f50182008-12-11 16:49:22 +0100741EXPORT_SYMBOL_GPL(ring_buffer_free);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400742
Steven Rostedt37886f62009-03-17 17:22:06 -0400743void ring_buffer_set_clock(struct ring_buffer *buffer,
744 u64 (*clock)(void))
745{
746 buffer->clock = clock;
747}
748
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400749static void rb_reset_cpu(struct ring_buffer_per_cpu *cpu_buffer);
750
751static void
752rb_remove_pages(struct ring_buffer_per_cpu *cpu_buffer, unsigned nr_pages)
753{
Steven Rostedt044fa782008-12-02 23:50:03 -0500754 struct buffer_page *bpage;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400755 struct list_head *p;
756 unsigned i;
757
758 atomic_inc(&cpu_buffer->record_disabled);
759 synchronize_sched();
760
761 for (i = 0; i < nr_pages; i++) {
Steven Rostedt3e89c7b2008-11-11 15:28:41 -0500762 if (RB_WARN_ON(cpu_buffer, list_empty(&cpu_buffer->pages)))
763 return;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400764 p = cpu_buffer->pages.next;
Steven Rostedt044fa782008-12-02 23:50:03 -0500765 bpage = list_entry(p, struct buffer_page, list);
766 list_del_init(&bpage->list);
767 free_buffer_page(bpage);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400768 }
Steven Rostedt3e89c7b2008-11-11 15:28:41 -0500769 if (RB_WARN_ON(cpu_buffer, list_empty(&cpu_buffer->pages)))
770 return;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400771
772 rb_reset_cpu(cpu_buffer);
773
774 rb_check_pages(cpu_buffer);
775
776 atomic_dec(&cpu_buffer->record_disabled);
777
778}
779
780static void
781rb_insert_pages(struct ring_buffer_per_cpu *cpu_buffer,
782 struct list_head *pages, unsigned nr_pages)
783{
Steven Rostedt044fa782008-12-02 23:50:03 -0500784 struct buffer_page *bpage;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400785 struct list_head *p;
786 unsigned i;
787
788 atomic_inc(&cpu_buffer->record_disabled);
789 synchronize_sched();
790
791 for (i = 0; i < nr_pages; i++) {
Steven Rostedt3e89c7b2008-11-11 15:28:41 -0500792 if (RB_WARN_ON(cpu_buffer, list_empty(pages)))
793 return;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400794 p = pages->next;
Steven Rostedt044fa782008-12-02 23:50:03 -0500795 bpage = list_entry(p, struct buffer_page, list);
796 list_del_init(&bpage->list);
797 list_add_tail(&bpage->list, &cpu_buffer->pages);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400798 }
799 rb_reset_cpu(cpu_buffer);
800
801 rb_check_pages(cpu_buffer);
802
803 atomic_dec(&cpu_buffer->record_disabled);
804}
805
806/**
807 * ring_buffer_resize - resize the ring buffer
808 * @buffer: the buffer to resize.
809 * @size: the new size.
810 *
811 * The tracer is responsible for making sure that the buffer is
812 * not being used while changing the size.
813 * Note: We may be able to change the above requirement by using
814 * RCU synchronizations.
815 *
816 * Minimum size is 2 * BUF_PAGE_SIZE.
817 *
818 * Returns -1 on failure.
819 */
820int ring_buffer_resize(struct ring_buffer *buffer, unsigned long size)
821{
822 struct ring_buffer_per_cpu *cpu_buffer;
823 unsigned nr_pages, rm_pages, new_pages;
Steven Rostedt044fa782008-12-02 23:50:03 -0500824 struct buffer_page *bpage, *tmp;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400825 unsigned long buffer_size;
826 unsigned long addr;
827 LIST_HEAD(pages);
828 int i, cpu;
829
Ingo Molnaree51a1d2008-11-13 14:58:31 +0100830 /*
831 * Always succeed at resizing a non-existent buffer:
832 */
833 if (!buffer)
834 return size;
835
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400836 size = DIV_ROUND_UP(size, BUF_PAGE_SIZE);
837 size *= BUF_PAGE_SIZE;
838 buffer_size = buffer->pages * BUF_PAGE_SIZE;
839
840 /* we need a minimum of two pages */
841 if (size < BUF_PAGE_SIZE * 2)
842 size = BUF_PAGE_SIZE * 2;
843
844 if (size == buffer_size)
845 return size;
846
847 mutex_lock(&buffer->mutex);
Steven Rostedt554f7862009-03-11 22:00:13 -0400848 get_online_cpus();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400849
850 nr_pages = DIV_ROUND_UP(size, BUF_PAGE_SIZE);
851
852 if (size < buffer_size) {
853
854 /* easy case, just free pages */
Steven Rostedt554f7862009-03-11 22:00:13 -0400855 if (RB_WARN_ON(buffer, nr_pages >= buffer->pages))
856 goto out_fail;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400857
858 rm_pages = buffer->pages - nr_pages;
859
860 for_each_buffer_cpu(buffer, cpu) {
861 cpu_buffer = buffer->buffers[cpu];
862 rb_remove_pages(cpu_buffer, rm_pages);
863 }
864 goto out;
865 }
866
867 /*
868 * This is a bit more difficult. We only want to add pages
869 * when we can allocate enough for all CPUs. We do this
870 * by allocating all the pages and storing them on a local
871 * link list. If we succeed in our allocation, then we
872 * add these pages to the cpu_buffers. Otherwise we just free
873 * them all and return -ENOMEM;
874 */
Steven Rostedt554f7862009-03-11 22:00:13 -0400875 if (RB_WARN_ON(buffer, nr_pages <= buffer->pages))
876 goto out_fail;
Steven Rostedtf536aaf2008-11-10 23:07:30 -0500877
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400878 new_pages = nr_pages - buffer->pages;
879
880 for_each_buffer_cpu(buffer, cpu) {
881 for (i = 0; i < new_pages; i++) {
Steven Rostedt044fa782008-12-02 23:50:03 -0500882 bpage = kzalloc_node(ALIGN(sizeof(*bpage),
Steven Rostedte4c2ce82008-10-01 11:14:54 -0400883 cache_line_size()),
884 GFP_KERNEL, cpu_to_node(cpu));
Steven Rostedt044fa782008-12-02 23:50:03 -0500885 if (!bpage)
Steven Rostedte4c2ce82008-10-01 11:14:54 -0400886 goto free_pages;
Steven Rostedt044fa782008-12-02 23:50:03 -0500887 list_add(&bpage->list, &pages);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400888 addr = __get_free_page(GFP_KERNEL);
889 if (!addr)
890 goto free_pages;
Steven Rostedt044fa782008-12-02 23:50:03 -0500891 bpage->page = (void *)addr;
892 rb_init_page(bpage->page);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400893 }
894 }
895
896 for_each_buffer_cpu(buffer, cpu) {
897 cpu_buffer = buffer->buffers[cpu];
898 rb_insert_pages(cpu_buffer, &pages, new_pages);
899 }
900
Steven Rostedt554f7862009-03-11 22:00:13 -0400901 if (RB_WARN_ON(buffer, !list_empty(&pages)))
902 goto out_fail;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400903
904 out:
905 buffer->pages = nr_pages;
Steven Rostedt554f7862009-03-11 22:00:13 -0400906 put_online_cpus();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400907 mutex_unlock(&buffer->mutex);
908
909 return size;
910
911 free_pages:
Steven Rostedt044fa782008-12-02 23:50:03 -0500912 list_for_each_entry_safe(bpage, tmp, &pages, list) {
913 list_del_init(&bpage->list);
914 free_buffer_page(bpage);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400915 }
Steven Rostedt554f7862009-03-11 22:00:13 -0400916 put_online_cpus();
Vegard Nossum641d2f62008-11-18 19:22:13 +0100917 mutex_unlock(&buffer->mutex);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400918 return -ENOMEM;
Steven Rostedt554f7862009-03-11 22:00:13 -0400919
920 /*
921 * Something went totally wrong, and we are too paranoid
922 * to even clean up the mess.
923 */
924 out_fail:
925 put_online_cpus();
926 mutex_unlock(&buffer->mutex);
927 return -1;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400928}
Robert Richterc4f50182008-12-11 16:49:22 +0100929EXPORT_SYMBOL_GPL(ring_buffer_resize);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400930
Steven Rostedt8789a9e2008-12-02 15:34:07 -0500931static inline void *
Steven Rostedt044fa782008-12-02 23:50:03 -0500932__rb_data_page_index(struct buffer_data_page *bpage, unsigned index)
Steven Rostedt8789a9e2008-12-02 15:34:07 -0500933{
Steven Rostedt044fa782008-12-02 23:50:03 -0500934 return bpage->data + index;
Steven Rostedt8789a9e2008-12-02 15:34:07 -0500935}
936
Steven Rostedt044fa782008-12-02 23:50:03 -0500937static inline void *__rb_page_index(struct buffer_page *bpage, unsigned index)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400938{
Steven Rostedt044fa782008-12-02 23:50:03 -0500939 return bpage->page->data + index;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400940}
941
942static inline struct ring_buffer_event *
Steven Rostedtd7690412008-10-01 00:29:53 -0400943rb_reader_event(struct ring_buffer_per_cpu *cpu_buffer)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400944{
Steven Rostedt6f807ac2008-10-04 02:00:58 -0400945 return __rb_page_index(cpu_buffer->reader_page,
946 cpu_buffer->reader_page->read);
947}
948
949static inline struct ring_buffer_event *
950rb_head_event(struct ring_buffer_per_cpu *cpu_buffer)
951{
952 return __rb_page_index(cpu_buffer->head_page,
953 cpu_buffer->head_page->read);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400954}
955
956static inline struct ring_buffer_event *
957rb_iter_head_event(struct ring_buffer_iter *iter)
958{
Steven Rostedt6f807ac2008-10-04 02:00:58 -0400959 return __rb_page_index(iter->head_page, iter->head);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400960}
961
Steven Rostedtbf41a152008-10-04 02:00:59 -0400962static inline unsigned rb_page_write(struct buffer_page *bpage)
963{
964 return local_read(&bpage->write);
965}
966
967static inline unsigned rb_page_commit(struct buffer_page *bpage)
968{
Steven Rostedtabc9b562008-12-02 15:34:06 -0500969 return local_read(&bpage->page->commit);
Steven Rostedtbf41a152008-10-04 02:00:59 -0400970}
971
972/* Size is determined by what has been commited */
973static inline unsigned rb_page_size(struct buffer_page *bpage)
974{
975 return rb_page_commit(bpage);
976}
977
978static inline unsigned
979rb_commit_index(struct ring_buffer_per_cpu *cpu_buffer)
980{
981 return rb_page_commit(cpu_buffer->commit_page);
982}
983
984static inline unsigned rb_head_size(struct ring_buffer_per_cpu *cpu_buffer)
985{
986 return rb_page_commit(cpu_buffer->head_page);
987}
988
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400989static inline void rb_inc_page(struct ring_buffer_per_cpu *cpu_buffer,
Steven Rostedt044fa782008-12-02 23:50:03 -0500990 struct buffer_page **bpage)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400991{
Steven Rostedt044fa782008-12-02 23:50:03 -0500992 struct list_head *p = (*bpage)->list.next;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400993
994 if (p == &cpu_buffer->pages)
995 p = p->next;
996
Steven Rostedt044fa782008-12-02 23:50:03 -0500997 *bpage = list_entry(p, struct buffer_page, list);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400998}
999
Steven Rostedtbf41a152008-10-04 02:00:59 -04001000static inline unsigned
1001rb_event_index(struct ring_buffer_event *event)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001002{
Steven Rostedtbf41a152008-10-04 02:00:59 -04001003 unsigned long addr = (unsigned long)event;
1004
1005 return (addr & ~PAGE_MASK) - (PAGE_SIZE - BUF_PAGE_SIZE);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001006}
1007
Steven Rostedt0f0c85f2009-05-11 16:08:00 -04001008static inline int
Steven Rostedtbf41a152008-10-04 02:00:59 -04001009rb_is_commit(struct ring_buffer_per_cpu *cpu_buffer,
1010 struct ring_buffer_event *event)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001011{
Steven Rostedtbf41a152008-10-04 02:00:59 -04001012 unsigned long addr = (unsigned long)event;
1013 unsigned long index;
1014
1015 index = rb_event_index(event);
1016 addr &= PAGE_MASK;
1017
1018 return cpu_buffer->commit_page->page == (void *)addr &&
1019 rb_commit_index(cpu_buffer) == index;
1020}
1021
Andrew Morton34a148b2009-01-09 12:27:09 -08001022static void
Steven Rostedtbf41a152008-10-04 02:00:59 -04001023rb_set_commit_event(struct ring_buffer_per_cpu *cpu_buffer,
1024 struct ring_buffer_event *event)
1025{
1026 unsigned long addr = (unsigned long)event;
1027 unsigned long index;
1028
1029 index = rb_event_index(event);
1030 addr &= PAGE_MASK;
1031
1032 while (cpu_buffer->commit_page->page != (void *)addr) {
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05001033 if (RB_WARN_ON(cpu_buffer,
1034 cpu_buffer->commit_page == cpu_buffer->tail_page))
1035 return;
Steven Rostedtabc9b562008-12-02 15:34:06 -05001036 cpu_buffer->commit_page->page->commit =
Steven Rostedtbf41a152008-10-04 02:00:59 -04001037 cpu_buffer->commit_page->write;
1038 rb_inc_page(cpu_buffer, &cpu_buffer->commit_page);
Steven Rostedtabc9b562008-12-02 15:34:06 -05001039 cpu_buffer->write_stamp =
1040 cpu_buffer->commit_page->page->time_stamp;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001041 }
1042
1043 /* Now set the commit to the event's index */
Steven Rostedtabc9b562008-12-02 15:34:06 -05001044 local_set(&cpu_buffer->commit_page->page->commit, index);
Steven Rostedtbf41a152008-10-04 02:00:59 -04001045}
1046
Andrew Morton34a148b2009-01-09 12:27:09 -08001047static void
Steven Rostedtbf41a152008-10-04 02:00:59 -04001048rb_set_commit_to_write(struct ring_buffer_per_cpu *cpu_buffer)
1049{
1050 /*
1051 * We only race with interrupts and NMIs on this CPU.
1052 * If we own the commit event, then we can commit
1053 * all others that interrupted us, since the interruptions
1054 * are in stack format (they finish before they come
1055 * back to us). This allows us to do a simple loop to
1056 * assign the commit to the tail.
1057 */
Steven Rostedta8ccf1d2008-12-23 11:32:24 -05001058 again:
Steven Rostedtbf41a152008-10-04 02:00:59 -04001059 while (cpu_buffer->commit_page != cpu_buffer->tail_page) {
Steven Rostedtabc9b562008-12-02 15:34:06 -05001060 cpu_buffer->commit_page->page->commit =
Steven Rostedtbf41a152008-10-04 02:00:59 -04001061 cpu_buffer->commit_page->write;
1062 rb_inc_page(cpu_buffer, &cpu_buffer->commit_page);
Steven Rostedtabc9b562008-12-02 15:34:06 -05001063 cpu_buffer->write_stamp =
1064 cpu_buffer->commit_page->page->time_stamp;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001065 /* add barrier to keep gcc from optimizing too much */
1066 barrier();
1067 }
1068 while (rb_commit_index(cpu_buffer) !=
1069 rb_page_write(cpu_buffer->commit_page)) {
Steven Rostedtabc9b562008-12-02 15:34:06 -05001070 cpu_buffer->commit_page->page->commit =
Steven Rostedtbf41a152008-10-04 02:00:59 -04001071 cpu_buffer->commit_page->write;
1072 barrier();
1073 }
Steven Rostedta8ccf1d2008-12-23 11:32:24 -05001074
1075 /* again, keep gcc from optimizing */
1076 barrier();
1077
1078 /*
1079 * If an interrupt came in just after the first while loop
1080 * and pushed the tail page forward, we will be left with
1081 * a dangling commit that will never go forward.
1082 */
1083 if (unlikely(cpu_buffer->commit_page != cpu_buffer->tail_page))
1084 goto again;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001085}
1086
Steven Rostedtd7690412008-10-01 00:29:53 -04001087static void rb_reset_reader_page(struct ring_buffer_per_cpu *cpu_buffer)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001088{
Steven Rostedtabc9b562008-12-02 15:34:06 -05001089 cpu_buffer->read_stamp = cpu_buffer->reader_page->page->time_stamp;
Steven Rostedt6f807ac2008-10-04 02:00:58 -04001090 cpu_buffer->reader_page->read = 0;
Steven Rostedtd7690412008-10-01 00:29:53 -04001091}
1092
Andrew Morton34a148b2009-01-09 12:27:09 -08001093static void rb_inc_iter(struct ring_buffer_iter *iter)
Steven Rostedtd7690412008-10-01 00:29:53 -04001094{
1095 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
1096
1097 /*
1098 * The iterator could be on the reader page (it starts there).
1099 * But the head could have moved, since the reader was
1100 * found. Check for this case and assign the iterator
1101 * to the head page instead of next.
1102 */
1103 if (iter->head_page == cpu_buffer->reader_page)
1104 iter->head_page = cpu_buffer->head_page;
1105 else
1106 rb_inc_page(cpu_buffer, &iter->head_page);
1107
Steven Rostedtabc9b562008-12-02 15:34:06 -05001108 iter->read_stamp = iter->head_page->page->time_stamp;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001109 iter->head = 0;
1110}
1111
1112/**
1113 * ring_buffer_update_event - update event type and data
1114 * @event: the even to update
1115 * @type: the type of event
1116 * @length: the size of the event field in the ring buffer
1117 *
1118 * Update the type and data fields of the event. The length
1119 * is the actual size that is written to the ring buffer,
1120 * and with this, we can determine what to place into the
1121 * data field.
1122 */
Andrew Morton34a148b2009-01-09 12:27:09 -08001123static void
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001124rb_update_event(struct ring_buffer_event *event,
1125 unsigned type, unsigned length)
1126{
Lai Jiangshan334d4162009-04-24 11:27:05 +08001127 event->type_len = type;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001128
1129 switch (type) {
1130
1131 case RINGBUF_TYPE_PADDING:
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001132 case RINGBUF_TYPE_TIME_EXTEND:
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001133 case RINGBUF_TYPE_TIME_STAMP:
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001134 break;
1135
Lai Jiangshan334d4162009-04-24 11:27:05 +08001136 case 0:
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001137 length -= RB_EVNT_HDR_SIZE;
Lai Jiangshan334d4162009-04-24 11:27:05 +08001138 if (length > RB_MAX_SMALL_DATA)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001139 event->array[0] = length;
Lai Jiangshan334d4162009-04-24 11:27:05 +08001140 else
1141 event->type_len = DIV_ROUND_UP(length, RB_ALIGNMENT);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001142 break;
1143 default:
1144 BUG();
1145 }
1146}
1147
Andrew Morton34a148b2009-01-09 12:27:09 -08001148static unsigned rb_calculate_event_length(unsigned length)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001149{
1150 struct ring_buffer_event event; /* Used only for sizeof array */
1151
1152 /* zero length can cause confusions */
1153 if (!length)
1154 length = 1;
1155
1156 if (length > RB_MAX_SMALL_DATA)
1157 length += sizeof(event.array[0]);
1158
1159 length += RB_EVNT_HDR_SIZE;
1160 length = ALIGN(length, RB_ALIGNMENT);
1161
1162 return length;
1163}
1164
Steven Rostedt6634ff22009-05-06 15:30:07 -04001165
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001166static struct ring_buffer_event *
Steven Rostedt6634ff22009-05-06 15:30:07 -04001167rb_move_tail(struct ring_buffer_per_cpu *cpu_buffer,
1168 unsigned long length, unsigned long tail,
1169 struct buffer_page *commit_page,
1170 struct buffer_page *tail_page, u64 *ts)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001171{
Steven Rostedt6634ff22009-05-06 15:30:07 -04001172 struct buffer_page *next_page, *head_page, *reader_page;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001173 struct ring_buffer *buffer = cpu_buffer->buffer;
1174 struct ring_buffer_event *event;
Steven Rostedt78d904b2009-02-05 18:43:07 -05001175 bool lock_taken = false;
Steven Rostedt6634ff22009-05-06 15:30:07 -04001176 unsigned long flags;
Steven Rostedtaa20ae82009-05-05 21:16:11 -04001177
1178 next_page = tail_page;
1179
1180 local_irq_save(flags);
1181 /*
1182 * Since the write to the buffer is still not
1183 * fully lockless, we must be careful with NMIs.
1184 * The locks in the writers are taken when a write
1185 * crosses to a new page. The locks protect against
1186 * races with the readers (this will soon be fixed
1187 * with a lockless solution).
1188 *
1189 * Because we can not protect against NMIs, and we
1190 * want to keep traces reentrant, we need to manage
1191 * what happens when we are in an NMI.
1192 *
1193 * NMIs can happen after we take the lock.
1194 * If we are in an NMI, only take the lock
1195 * if it is not already taken. Otherwise
1196 * simply fail.
1197 */
1198 if (unlikely(in_nmi())) {
1199 if (!__raw_spin_trylock(&cpu_buffer->lock)) {
1200 cpu_buffer->nmi_dropped++;
1201 goto out_reset;
1202 }
1203 } else
1204 __raw_spin_lock(&cpu_buffer->lock);
1205
1206 lock_taken = true;
1207
1208 rb_inc_page(cpu_buffer, &next_page);
1209
1210 head_page = cpu_buffer->head_page;
1211 reader_page = cpu_buffer->reader_page;
1212
1213 /* we grabbed the lock before incrementing */
1214 if (RB_WARN_ON(cpu_buffer, next_page == reader_page))
1215 goto out_reset;
1216
1217 /*
1218 * If for some reason, we had an interrupt storm that made
1219 * it all the way around the buffer, bail, and warn
1220 * about it.
1221 */
1222 if (unlikely(next_page == commit_page)) {
1223 cpu_buffer->commit_overrun++;
1224 goto out_reset;
1225 }
1226
1227 if (next_page == head_page) {
1228 if (!(buffer->flags & RB_FL_OVERWRITE))
1229 goto out_reset;
1230
1231 /* tail_page has not moved yet? */
1232 if (tail_page == cpu_buffer->tail_page) {
1233 /* count overflows */
1234 cpu_buffer->overrun +=
1235 local_read(&head_page->entries);
1236
1237 rb_inc_page(cpu_buffer, &head_page);
1238 cpu_buffer->head_page = head_page;
1239 cpu_buffer->head_page->read = 0;
1240 }
1241 }
1242
1243 /*
1244 * If the tail page is still the same as what we think
1245 * it is, then it is up to us to update the tail
1246 * pointer.
1247 */
1248 if (tail_page == cpu_buffer->tail_page) {
1249 local_set(&next_page->write, 0);
1250 local_set(&next_page->entries, 0);
1251 local_set(&next_page->page->commit, 0);
1252 cpu_buffer->tail_page = next_page;
1253
1254 /* reread the time stamp */
Steven Rostedt88eb0122009-05-11 16:28:23 -04001255 *ts = rb_time_stamp(buffer, cpu_buffer->cpu);
Steven Rostedtaa20ae82009-05-05 21:16:11 -04001256 cpu_buffer->tail_page->page->time_stamp = *ts;
1257 }
1258
1259 /*
1260 * The actual tail page has moved forward.
1261 */
1262 if (tail < BUF_PAGE_SIZE) {
1263 /* Mark the rest of the page with padding */
1264 event = __rb_page_index(tail_page, tail);
1265 rb_event_set_padding(event);
1266 }
1267
Steven Rostedt8e7abf12009-05-06 10:26:45 -04001268 /* Set the write back to the previous setting */
1269 local_sub(length, &tail_page->write);
Steven Rostedtaa20ae82009-05-05 21:16:11 -04001270
1271 /*
1272 * If this was a commit entry that failed,
1273 * increment that too
1274 */
1275 if (tail_page == cpu_buffer->commit_page &&
1276 tail == rb_commit_index(cpu_buffer)) {
1277 rb_set_commit_to_write(cpu_buffer);
1278 }
1279
1280 __raw_spin_unlock(&cpu_buffer->lock);
1281 local_irq_restore(flags);
1282
1283 /* fail and let the caller try again */
1284 return ERR_PTR(-EAGAIN);
1285
Steven Rostedt45141d42009-02-12 13:19:48 -05001286 out_reset:
Lai Jiangshan6f3b3442009-01-12 11:06:18 +08001287 /* reset write */
Steven Rostedt8e7abf12009-05-06 10:26:45 -04001288 local_sub(length, &tail_page->write);
Lai Jiangshan6f3b3442009-01-12 11:06:18 +08001289
Steven Rostedt78d904b2009-02-05 18:43:07 -05001290 if (likely(lock_taken))
1291 __raw_spin_unlock(&cpu_buffer->lock);
Steven Rostedt3e03fb72008-11-06 00:09:43 -05001292 local_irq_restore(flags);
Steven Rostedtbf41a152008-10-04 02:00:59 -04001293 return NULL;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001294}
1295
Steven Rostedt6634ff22009-05-06 15:30:07 -04001296static struct ring_buffer_event *
1297__rb_reserve_next(struct ring_buffer_per_cpu *cpu_buffer,
1298 unsigned type, unsigned long length, u64 *ts)
1299{
1300 struct buffer_page *tail_page, *commit_page;
1301 struct ring_buffer_event *event;
1302 unsigned long tail, write;
1303
1304 commit_page = cpu_buffer->commit_page;
1305 /* we just need to protect against interrupts */
1306 barrier();
1307 tail_page = cpu_buffer->tail_page;
1308 write = local_add_return(length, &tail_page->write);
1309 tail = write - length;
1310
1311 /* See if we shot pass the end of this buffer page */
1312 if (write > BUF_PAGE_SIZE)
1313 return rb_move_tail(cpu_buffer, length, tail,
1314 commit_page, tail_page, ts);
1315
1316 /* We reserved something on the buffer */
1317
1318 if (RB_WARN_ON(cpu_buffer, write > BUF_PAGE_SIZE))
1319 return NULL;
1320
1321 event = __rb_page_index(tail_page, tail);
1322 rb_update_event(event, type, length);
1323
1324 /* The passed in type is zero for DATA */
1325 if (likely(!type))
1326 local_inc(&tail_page->entries);
1327
1328 /*
1329 * If this is a commit and the tail is zero, then update
1330 * this page's time stamp.
1331 */
1332 if (!tail && rb_is_commit(cpu_buffer, event))
1333 cpu_buffer->commit_page->page->time_stamp = *ts;
1334
1335 return event;
1336}
1337
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001338static int
1339rb_add_time_stamp(struct ring_buffer_per_cpu *cpu_buffer,
1340 u64 *ts, u64 *delta)
1341{
1342 struct ring_buffer_event *event;
1343 static int once;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001344 int ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001345
1346 if (unlikely(*delta > (1ULL << 59) && !once++)) {
1347 printk(KERN_WARNING "Delta way too big! %llu"
1348 " ts=%llu write stamp = %llu\n",
Stephen Rothwelle2862c92008-10-27 17:43:28 +11001349 (unsigned long long)*delta,
1350 (unsigned long long)*ts,
1351 (unsigned long long)cpu_buffer->write_stamp);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001352 WARN_ON(1);
1353 }
1354
1355 /*
1356 * The delta is too big, we to add a
1357 * new timestamp.
1358 */
1359 event = __rb_reserve_next(cpu_buffer,
1360 RINGBUF_TYPE_TIME_EXTEND,
1361 RB_LEN_TIME_EXTEND,
1362 ts);
1363 if (!event)
Steven Rostedtbf41a152008-10-04 02:00:59 -04001364 return -EBUSY;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001365
Steven Rostedtbf41a152008-10-04 02:00:59 -04001366 if (PTR_ERR(event) == -EAGAIN)
1367 return -EAGAIN;
1368
1369 /* Only a commited time event can update the write stamp */
1370 if (rb_is_commit(cpu_buffer, event)) {
1371 /*
1372 * If this is the first on the page, then we need to
1373 * update the page itself, and just put in a zero.
1374 */
1375 if (rb_event_index(event)) {
1376 event->time_delta = *delta & TS_MASK;
1377 event->array[0] = *delta >> TS_SHIFT;
1378 } else {
Steven Rostedtabc9b562008-12-02 15:34:06 -05001379 cpu_buffer->commit_page->page->time_stamp = *ts;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001380 event->time_delta = 0;
1381 event->array[0] = 0;
1382 }
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001383 cpu_buffer->write_stamp = *ts;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001384 /* let the caller know this was the commit */
1385 ret = 1;
1386 } else {
1387 /* Darn, this is just wasted space */
1388 event->time_delta = 0;
1389 event->array[0] = 0;
1390 ret = 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001391 }
1392
Steven Rostedtbf41a152008-10-04 02:00:59 -04001393 *delta = 0;
1394
1395 return ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001396}
1397
1398static struct ring_buffer_event *
1399rb_reserve_next_event(struct ring_buffer_per_cpu *cpu_buffer,
Steven Rostedt1cd8d732009-05-11 14:08:09 -04001400 unsigned long length)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001401{
1402 struct ring_buffer_event *event;
Steven Rostedt168b6b12009-05-11 22:11:05 -04001403 u64 ts, delta = 0;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001404 int commit = 0;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04001405 int nr_loops = 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001406
Steven Rostedtbe957c42009-05-11 14:42:53 -04001407 length = rb_calculate_event_length(length);
Steven Rostedtbf41a152008-10-04 02:00:59 -04001408 again:
Steven Rostedt818e3dd2008-10-31 09:58:35 -04001409 /*
1410 * We allow for interrupts to reenter here and do a trace.
1411 * If one does, it will cause this original code to loop
1412 * back here. Even with heavy interrupts happening, this
1413 * should only happen a few times in a row. If this happens
1414 * 1000 times in a row, there must be either an interrupt
1415 * storm or we have something buggy.
1416 * Bail!
1417 */
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05001418 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 1000))
Steven Rostedt818e3dd2008-10-31 09:58:35 -04001419 return NULL;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04001420
Steven Rostedt88eb0122009-05-11 16:28:23 -04001421 ts = rb_time_stamp(cpu_buffer->buffer, cpu_buffer->cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001422
Steven Rostedtbf41a152008-10-04 02:00:59 -04001423 /*
1424 * Only the first commit can update the timestamp.
1425 * Yes there is a race here. If an interrupt comes in
1426 * just after the conditional and it traces too, then it
1427 * will also check the deltas. More than one timestamp may
1428 * also be made. But only the entry that did the actual
1429 * commit will be something other than zero.
1430 */
Steven Rostedt0f0c85f2009-05-11 16:08:00 -04001431 if (likely(cpu_buffer->tail_page == cpu_buffer->commit_page &&
1432 rb_page_write(cpu_buffer->tail_page) ==
1433 rb_commit_index(cpu_buffer))) {
Steven Rostedt168b6b12009-05-11 22:11:05 -04001434 u64 diff;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001435
Steven Rostedt168b6b12009-05-11 22:11:05 -04001436 diff = ts - cpu_buffer->write_stamp;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001437
Steven Rostedt168b6b12009-05-11 22:11:05 -04001438 /* make sure this diff is calculated here */
Steven Rostedtbf41a152008-10-04 02:00:59 -04001439 barrier();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001440
Steven Rostedtbf41a152008-10-04 02:00:59 -04001441 /* Did the write stamp get updated already? */
1442 if (unlikely(ts < cpu_buffer->write_stamp))
Steven Rostedt168b6b12009-05-11 22:11:05 -04001443 goto get_event;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001444
Steven Rostedt168b6b12009-05-11 22:11:05 -04001445 delta = diff;
1446 if (unlikely(test_time_stamp(delta))) {
Steven Rostedtbf41a152008-10-04 02:00:59 -04001447
1448 commit = rb_add_time_stamp(cpu_buffer, &ts, &delta);
Steven Rostedtbf41a152008-10-04 02:00:59 -04001449 if (commit == -EBUSY)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001450 return NULL;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001451
1452 if (commit == -EAGAIN)
1453 goto again;
1454
1455 RB_WARN_ON(cpu_buffer, commit < 0);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001456 }
Steven Rostedt168b6b12009-05-11 22:11:05 -04001457 }
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001458
Steven Rostedt168b6b12009-05-11 22:11:05 -04001459 get_event:
Steven Rostedt1cd8d732009-05-11 14:08:09 -04001460 event = __rb_reserve_next(cpu_buffer, 0, length, &ts);
Steven Rostedt168b6b12009-05-11 22:11:05 -04001461 if (unlikely(PTR_ERR(event) == -EAGAIN))
Steven Rostedtbf41a152008-10-04 02:00:59 -04001462 goto again;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001463
Steven Rostedtbf41a152008-10-04 02:00:59 -04001464 if (!event) {
1465 if (unlikely(commit))
1466 /*
1467 * Ouch! We needed a timestamp and it was commited. But
1468 * we didn't get our event reserved.
1469 */
1470 rb_set_commit_to_write(cpu_buffer);
1471 return NULL;
1472 }
1473
1474 /*
1475 * If the timestamp was commited, make the commit our entry
1476 * now so that we will update it when needed.
1477 */
Steven Rostedt0f0c85f2009-05-11 16:08:00 -04001478 if (unlikely(commit))
Steven Rostedtbf41a152008-10-04 02:00:59 -04001479 rb_set_commit_event(cpu_buffer, event);
1480 else if (!rb_is_commit(cpu_buffer, event))
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001481 delta = 0;
1482
1483 event->time_delta = delta;
1484
1485 return event;
1486}
1487
Steven Rostedtaa18efb2009-04-20 16:16:11 -04001488#define TRACE_RECURSIVE_DEPTH 16
Steven Rostedt261842b2009-04-16 21:41:52 -04001489
1490static int trace_recursive_lock(void)
1491{
Steven Rostedtaa18efb2009-04-20 16:16:11 -04001492 current->trace_recursion++;
Steven Rostedt261842b2009-04-16 21:41:52 -04001493
Steven Rostedtaa18efb2009-04-20 16:16:11 -04001494 if (likely(current->trace_recursion < TRACE_RECURSIVE_DEPTH))
1495 return 0;
Steven Rostedt261842b2009-04-16 21:41:52 -04001496
Steven Rostedtaa18efb2009-04-20 16:16:11 -04001497 /* Disable all tracing before we do anything else */
1498 tracing_off_permanent();
Frederic Weisbeckere057a5e2009-04-19 23:38:12 +02001499
Steven Rostedt7d7d2b82009-04-27 12:37:49 -04001500 printk_once(KERN_WARNING "Tracing recursion: depth[%ld]:"
Steven Rostedtaa18efb2009-04-20 16:16:11 -04001501 "HC[%lu]:SC[%lu]:NMI[%lu]\n",
1502 current->trace_recursion,
1503 hardirq_count() >> HARDIRQ_SHIFT,
1504 softirq_count() >> SOFTIRQ_SHIFT,
1505 in_nmi());
Frederic Weisbeckere057a5e2009-04-19 23:38:12 +02001506
Steven Rostedtaa18efb2009-04-20 16:16:11 -04001507 WARN_ON_ONCE(1);
1508 return -1;
Steven Rostedt261842b2009-04-16 21:41:52 -04001509}
1510
1511static void trace_recursive_unlock(void)
1512{
Steven Rostedtaa18efb2009-04-20 16:16:11 -04001513 WARN_ON_ONCE(!current->trace_recursion);
Steven Rostedt261842b2009-04-16 21:41:52 -04001514
Steven Rostedtaa18efb2009-04-20 16:16:11 -04001515 current->trace_recursion--;
Steven Rostedt261842b2009-04-16 21:41:52 -04001516}
1517
Steven Rostedtbf41a152008-10-04 02:00:59 -04001518static DEFINE_PER_CPU(int, rb_need_resched);
1519
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001520/**
1521 * ring_buffer_lock_reserve - reserve a part of the buffer
1522 * @buffer: the ring buffer to reserve from
1523 * @length: the length of the data to reserve (excluding event header)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001524 *
1525 * Returns a reseverd event on the ring buffer to copy directly to.
1526 * The user of this interface will need to get the body to write into
1527 * and can use the ring_buffer_event_data() interface.
1528 *
1529 * The length is the length of the data needed, not the event length
1530 * which also includes the event header.
1531 *
1532 * Must be paired with ring_buffer_unlock_commit, unless NULL is returned.
1533 * If NULL is returned, then nothing has been allocated or locked.
1534 */
1535struct ring_buffer_event *
Arnaldo Carvalho de Melo0a987752009-02-05 16:12:56 -02001536ring_buffer_lock_reserve(struct ring_buffer *buffer, unsigned long length)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001537{
1538 struct ring_buffer_per_cpu *cpu_buffer;
1539 struct ring_buffer_event *event;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001540 int cpu, resched;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001541
Steven Rostedt033601a2008-11-21 12:41:55 -05001542 if (ring_buffer_flags != RB_BUFFERS_ON)
Steven Rostedta3583242008-11-11 15:01:42 -05001543 return NULL;
1544
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001545 if (atomic_read(&buffer->record_disabled))
1546 return NULL;
1547
Steven Rostedtbf41a152008-10-04 02:00:59 -04001548 /* If we are tracing schedule, we don't want to recurse */
Steven Rostedt182e9f52008-11-03 23:15:56 -05001549 resched = ftrace_preempt_disable();
Steven Rostedtbf41a152008-10-04 02:00:59 -04001550
Steven Rostedt261842b2009-04-16 21:41:52 -04001551 if (trace_recursive_lock())
1552 goto out_nocheck;
1553
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001554 cpu = raw_smp_processor_id();
1555
Rusty Russell9e01c1b2009-01-01 10:12:22 +10301556 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedtd7690412008-10-01 00:29:53 -04001557 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001558
1559 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001560
1561 if (atomic_read(&cpu_buffer->record_disabled))
Steven Rostedtd7690412008-10-01 00:29:53 -04001562 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001563
Steven Rostedtbe957c42009-05-11 14:42:53 -04001564 if (length > BUF_MAX_DATA_SIZE)
Steven Rostedtbf41a152008-10-04 02:00:59 -04001565 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001566
Steven Rostedt1cd8d732009-05-11 14:08:09 -04001567 event = rb_reserve_next_event(cpu_buffer, length);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001568 if (!event)
Steven Rostedtd7690412008-10-01 00:29:53 -04001569 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001570
Steven Rostedtbf41a152008-10-04 02:00:59 -04001571 /*
1572 * Need to store resched state on this cpu.
1573 * Only the first needs to.
1574 */
1575
1576 if (preempt_count() == 1)
1577 per_cpu(rb_need_resched, cpu) = resched;
1578
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001579 return event;
1580
Steven Rostedtd7690412008-10-01 00:29:53 -04001581 out:
Steven Rostedt261842b2009-04-16 21:41:52 -04001582 trace_recursive_unlock();
1583
1584 out_nocheck:
Steven Rostedt182e9f52008-11-03 23:15:56 -05001585 ftrace_preempt_enable(resched);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001586 return NULL;
1587}
Robert Richterc4f50182008-12-11 16:49:22 +01001588EXPORT_SYMBOL_GPL(ring_buffer_lock_reserve);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001589
1590static void rb_commit(struct ring_buffer_per_cpu *cpu_buffer,
1591 struct ring_buffer_event *event)
1592{
Steven Rostedte4906ef2009-04-30 20:49:44 -04001593 local_inc(&cpu_buffer->entries);
Steven Rostedtbf41a152008-10-04 02:00:59 -04001594
1595 /* Only process further if we own the commit */
1596 if (!rb_is_commit(cpu_buffer, event))
1597 return;
1598
1599 cpu_buffer->write_stamp += event->time_delta;
1600
1601 rb_set_commit_to_write(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001602}
1603
1604/**
1605 * ring_buffer_unlock_commit - commit a reserved
1606 * @buffer: The buffer to commit to
1607 * @event: The event pointer to commit.
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001608 *
1609 * This commits the data to the ring buffer, and releases any locks held.
1610 *
1611 * Must be paired with ring_buffer_lock_reserve.
1612 */
1613int ring_buffer_unlock_commit(struct ring_buffer *buffer,
Arnaldo Carvalho de Melo0a987752009-02-05 16:12:56 -02001614 struct ring_buffer_event *event)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001615{
1616 struct ring_buffer_per_cpu *cpu_buffer;
1617 int cpu = raw_smp_processor_id();
1618
1619 cpu_buffer = buffer->buffers[cpu];
1620
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001621 rb_commit(cpu_buffer, event);
1622
Steven Rostedt261842b2009-04-16 21:41:52 -04001623 trace_recursive_unlock();
1624
Steven Rostedtbf41a152008-10-04 02:00:59 -04001625 /*
1626 * Only the last preempt count needs to restore preemption.
1627 */
Steven Rostedt182e9f52008-11-03 23:15:56 -05001628 if (preempt_count() == 1)
1629 ftrace_preempt_enable(per_cpu(rb_need_resched, cpu));
1630 else
Steven Rostedtbf41a152008-10-04 02:00:59 -04001631 preempt_enable_no_resched_notrace();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001632
1633 return 0;
1634}
Robert Richterc4f50182008-12-11 16:49:22 +01001635EXPORT_SYMBOL_GPL(ring_buffer_unlock_commit);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001636
Frederic Weisbeckerf3b9aae2009-04-19 23:39:33 +02001637static inline void rb_event_discard(struct ring_buffer_event *event)
1638{
Lai Jiangshan334d4162009-04-24 11:27:05 +08001639 /* array[0] holds the actual length for the discarded event */
1640 event->array[0] = rb_event_data_length(event) - RB_EVNT_HDR_SIZE;
1641 event->type_len = RINGBUF_TYPE_PADDING;
Frederic Weisbeckerf3b9aae2009-04-19 23:39:33 +02001642 /* time delta must be non zero */
1643 if (!event->time_delta)
1644 event->time_delta = 1;
1645}
1646
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001647/**
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04001648 * ring_buffer_event_discard - discard any event in the ring buffer
1649 * @event: the event to discard
1650 *
1651 * Sometimes a event that is in the ring buffer needs to be ignored.
1652 * This function lets the user discard an event in the ring buffer
1653 * and then that event will not be read later.
1654 *
1655 * Note, it is up to the user to be careful with this, and protect
1656 * against races. If the user discards an event that has been consumed
1657 * it is possible that it could corrupt the ring buffer.
1658 */
1659void ring_buffer_event_discard(struct ring_buffer_event *event)
1660{
Frederic Weisbeckerf3b9aae2009-04-19 23:39:33 +02001661 rb_event_discard(event);
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04001662}
1663EXPORT_SYMBOL_GPL(ring_buffer_event_discard);
1664
1665/**
1666 * ring_buffer_commit_discard - discard an event that has not been committed
1667 * @buffer: the ring buffer
1668 * @event: non committed event to discard
1669 *
1670 * This is similar to ring_buffer_event_discard but must only be
1671 * performed on an event that has not been committed yet. The difference
1672 * is that this will also try to free the event from the ring buffer
1673 * if another event has not been added behind it.
1674 *
1675 * If another event has been added behind it, it will set the event
1676 * up as discarded, and perform the commit.
1677 *
1678 * If this function is called, do not call ring_buffer_unlock_commit on
1679 * the event.
1680 */
1681void ring_buffer_discard_commit(struct ring_buffer *buffer,
1682 struct ring_buffer_event *event)
1683{
1684 struct ring_buffer_per_cpu *cpu_buffer;
1685 unsigned long new_index, old_index;
1686 struct buffer_page *bpage;
1687 unsigned long index;
1688 unsigned long addr;
1689 int cpu;
1690
1691 /* The event is discarded regardless */
Frederic Weisbeckerf3b9aae2009-04-19 23:39:33 +02001692 rb_event_discard(event);
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04001693
1694 /*
1695 * This must only be called if the event has not been
1696 * committed yet. Thus we can assume that preemption
1697 * is still disabled.
1698 */
Steven Rostedt74f4fd22009-05-07 19:58:55 -04001699 RB_WARN_ON(buffer, preemptible());
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04001700
1701 cpu = smp_processor_id();
1702 cpu_buffer = buffer->buffers[cpu];
1703
1704 new_index = rb_event_index(event);
1705 old_index = new_index + rb_event_length(event);
1706 addr = (unsigned long)event;
1707 addr &= PAGE_MASK;
1708
1709 bpage = cpu_buffer->tail_page;
1710
1711 if (bpage == (void *)addr && rb_page_write(bpage) == old_index) {
1712 /*
1713 * This is on the tail page. It is possible that
1714 * a write could come in and move the tail page
1715 * and write to the next page. That is fine
1716 * because we just shorten what is on this page.
1717 */
1718 index = local_cmpxchg(&bpage->write, old_index, new_index);
1719 if (index == old_index)
1720 goto out;
1721 }
1722
1723 /*
1724 * The commit is still visible by the reader, so we
1725 * must increment entries.
1726 */
Steven Rostedte4906ef2009-04-30 20:49:44 -04001727 local_inc(&cpu_buffer->entries);
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04001728 out:
1729 /*
1730 * If a write came in and pushed the tail page
1731 * we still need to update the commit pointer
1732 * if we were the commit.
1733 */
1734 if (rb_is_commit(cpu_buffer, event))
1735 rb_set_commit_to_write(cpu_buffer);
1736
Frederic Weisbeckerf3b9aae2009-04-19 23:39:33 +02001737 trace_recursive_unlock();
1738
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04001739 /*
1740 * Only the last preempt count needs to restore preemption.
1741 */
1742 if (preempt_count() == 1)
1743 ftrace_preempt_enable(per_cpu(rb_need_resched, cpu));
1744 else
1745 preempt_enable_no_resched_notrace();
1746
1747}
1748EXPORT_SYMBOL_GPL(ring_buffer_discard_commit);
1749
1750/**
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001751 * ring_buffer_write - write data to the buffer without reserving
1752 * @buffer: The ring buffer to write to.
1753 * @length: The length of the data being written (excluding the event header)
1754 * @data: The data to write to the buffer.
1755 *
1756 * This is like ring_buffer_lock_reserve and ring_buffer_unlock_commit as
1757 * one function. If you already have the data to write to the buffer, it
1758 * may be easier to simply call this function.
1759 *
1760 * Note, like ring_buffer_lock_reserve, the length is the length of the data
1761 * and not the length of the event which would hold the header.
1762 */
1763int ring_buffer_write(struct ring_buffer *buffer,
1764 unsigned long length,
1765 void *data)
1766{
1767 struct ring_buffer_per_cpu *cpu_buffer;
1768 struct ring_buffer_event *event;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001769 void *body;
1770 int ret = -EBUSY;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001771 int cpu, resched;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001772
Steven Rostedt033601a2008-11-21 12:41:55 -05001773 if (ring_buffer_flags != RB_BUFFERS_ON)
Steven Rostedta3583242008-11-11 15:01:42 -05001774 return -EBUSY;
1775
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001776 if (atomic_read(&buffer->record_disabled))
1777 return -EBUSY;
1778
Steven Rostedt182e9f52008-11-03 23:15:56 -05001779 resched = ftrace_preempt_disable();
Steven Rostedtbf41a152008-10-04 02:00:59 -04001780
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001781 cpu = raw_smp_processor_id();
1782
Rusty Russell9e01c1b2009-01-01 10:12:22 +10301783 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedtd7690412008-10-01 00:29:53 -04001784 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001785
1786 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001787
1788 if (atomic_read(&cpu_buffer->record_disabled))
1789 goto out;
1790
Steven Rostedtbe957c42009-05-11 14:42:53 -04001791 if (length > BUF_MAX_DATA_SIZE)
1792 goto out;
1793
1794 event = rb_reserve_next_event(cpu_buffer, length);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001795 if (!event)
1796 goto out;
1797
1798 body = rb_event_data(event);
1799
1800 memcpy(body, data, length);
1801
1802 rb_commit(cpu_buffer, event);
1803
1804 ret = 0;
1805 out:
Steven Rostedt182e9f52008-11-03 23:15:56 -05001806 ftrace_preempt_enable(resched);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001807
1808 return ret;
1809}
Robert Richterc4f50182008-12-11 16:49:22 +01001810EXPORT_SYMBOL_GPL(ring_buffer_write);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001811
Andrew Morton34a148b2009-01-09 12:27:09 -08001812static int rb_per_cpu_empty(struct ring_buffer_per_cpu *cpu_buffer)
Steven Rostedtbf41a152008-10-04 02:00:59 -04001813{
1814 struct buffer_page *reader = cpu_buffer->reader_page;
1815 struct buffer_page *head = cpu_buffer->head_page;
1816 struct buffer_page *commit = cpu_buffer->commit_page;
1817
1818 return reader->read == rb_page_commit(reader) &&
1819 (commit == reader ||
1820 (commit == head &&
1821 head->read == rb_page_commit(commit)));
1822}
1823
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001824/**
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001825 * ring_buffer_record_disable - stop all writes into the buffer
1826 * @buffer: The ring buffer to stop writes to.
1827 *
1828 * This prevents all writes to the buffer. Any attempt to write
1829 * to the buffer after this will fail and return NULL.
1830 *
1831 * The caller should call synchronize_sched() after this.
1832 */
1833void ring_buffer_record_disable(struct ring_buffer *buffer)
1834{
1835 atomic_inc(&buffer->record_disabled);
1836}
Robert Richterc4f50182008-12-11 16:49:22 +01001837EXPORT_SYMBOL_GPL(ring_buffer_record_disable);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001838
1839/**
1840 * ring_buffer_record_enable - enable writes to the buffer
1841 * @buffer: The ring buffer to enable writes
1842 *
1843 * Note, multiple disables will need the same number of enables
1844 * to truely enable the writing (much like preempt_disable).
1845 */
1846void ring_buffer_record_enable(struct ring_buffer *buffer)
1847{
1848 atomic_dec(&buffer->record_disabled);
1849}
Robert Richterc4f50182008-12-11 16:49:22 +01001850EXPORT_SYMBOL_GPL(ring_buffer_record_enable);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001851
1852/**
1853 * ring_buffer_record_disable_cpu - stop all writes into the cpu_buffer
1854 * @buffer: The ring buffer to stop writes to.
1855 * @cpu: The CPU buffer to stop
1856 *
1857 * This prevents all writes to the buffer. Any attempt to write
1858 * to the buffer after this will fail and return NULL.
1859 *
1860 * The caller should call synchronize_sched() after this.
1861 */
1862void ring_buffer_record_disable_cpu(struct ring_buffer *buffer, int cpu)
1863{
1864 struct ring_buffer_per_cpu *cpu_buffer;
1865
Rusty Russell9e01c1b2009-01-01 10:12:22 +10301866 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04001867 return;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001868
1869 cpu_buffer = buffer->buffers[cpu];
1870 atomic_inc(&cpu_buffer->record_disabled);
1871}
Robert Richterc4f50182008-12-11 16:49:22 +01001872EXPORT_SYMBOL_GPL(ring_buffer_record_disable_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001873
1874/**
1875 * ring_buffer_record_enable_cpu - enable writes to the buffer
1876 * @buffer: The ring buffer to enable writes
1877 * @cpu: The CPU to enable.
1878 *
1879 * Note, multiple disables will need the same number of enables
1880 * to truely enable the writing (much like preempt_disable).
1881 */
1882void ring_buffer_record_enable_cpu(struct ring_buffer *buffer, int cpu)
1883{
1884 struct ring_buffer_per_cpu *cpu_buffer;
1885
Rusty Russell9e01c1b2009-01-01 10:12:22 +10301886 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04001887 return;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001888
1889 cpu_buffer = buffer->buffers[cpu];
1890 atomic_dec(&cpu_buffer->record_disabled);
1891}
Robert Richterc4f50182008-12-11 16:49:22 +01001892EXPORT_SYMBOL_GPL(ring_buffer_record_enable_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001893
1894/**
1895 * ring_buffer_entries_cpu - get the number of entries in a cpu buffer
1896 * @buffer: The ring buffer
1897 * @cpu: The per CPU buffer to get the entries from.
1898 */
1899unsigned long ring_buffer_entries_cpu(struct ring_buffer *buffer, int cpu)
1900{
1901 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedt8aabee52009-03-12 13:13:49 -04001902 unsigned long ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001903
Rusty Russell9e01c1b2009-01-01 10:12:22 +10301904 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04001905 return 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001906
1907 cpu_buffer = buffer->buffers[cpu];
Steven Rostedte4906ef2009-04-30 20:49:44 -04001908 ret = (local_read(&cpu_buffer->entries) - cpu_buffer->overrun)
1909 - cpu_buffer->read;
Steven Rostedt554f7862009-03-11 22:00:13 -04001910
1911 return ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001912}
Robert Richterc4f50182008-12-11 16:49:22 +01001913EXPORT_SYMBOL_GPL(ring_buffer_entries_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001914
1915/**
1916 * ring_buffer_overrun_cpu - get the number of overruns in a cpu_buffer
1917 * @buffer: The ring buffer
1918 * @cpu: The per CPU buffer to get the number of overruns from
1919 */
1920unsigned long ring_buffer_overrun_cpu(struct ring_buffer *buffer, int cpu)
1921{
1922 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedt8aabee52009-03-12 13:13:49 -04001923 unsigned long ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001924
Rusty Russell9e01c1b2009-01-01 10:12:22 +10301925 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04001926 return 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001927
1928 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt554f7862009-03-11 22:00:13 -04001929 ret = cpu_buffer->overrun;
Steven Rostedt554f7862009-03-11 22:00:13 -04001930
1931 return ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001932}
Robert Richterc4f50182008-12-11 16:49:22 +01001933EXPORT_SYMBOL_GPL(ring_buffer_overrun_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001934
1935/**
Steven Rostedtf0d2c682009-04-29 13:43:37 -04001936 * ring_buffer_nmi_dropped_cpu - get the number of nmis that were dropped
1937 * @buffer: The ring buffer
1938 * @cpu: The per CPU buffer to get the number of overruns from
1939 */
1940unsigned long ring_buffer_nmi_dropped_cpu(struct ring_buffer *buffer, int cpu)
1941{
1942 struct ring_buffer_per_cpu *cpu_buffer;
1943 unsigned long ret;
1944
1945 if (!cpumask_test_cpu(cpu, buffer->cpumask))
1946 return 0;
1947
1948 cpu_buffer = buffer->buffers[cpu];
1949 ret = cpu_buffer->nmi_dropped;
1950
1951 return ret;
1952}
1953EXPORT_SYMBOL_GPL(ring_buffer_nmi_dropped_cpu);
1954
1955/**
1956 * ring_buffer_commit_overrun_cpu - get the number of overruns caused by commits
1957 * @buffer: The ring buffer
1958 * @cpu: The per CPU buffer to get the number of overruns from
1959 */
1960unsigned long
1961ring_buffer_commit_overrun_cpu(struct ring_buffer *buffer, int cpu)
1962{
1963 struct ring_buffer_per_cpu *cpu_buffer;
1964 unsigned long ret;
1965
1966 if (!cpumask_test_cpu(cpu, buffer->cpumask))
1967 return 0;
1968
1969 cpu_buffer = buffer->buffers[cpu];
1970 ret = cpu_buffer->commit_overrun;
1971
1972 return ret;
1973}
1974EXPORT_SYMBOL_GPL(ring_buffer_commit_overrun_cpu);
1975
1976/**
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001977 * ring_buffer_entries - get the number of entries in a buffer
1978 * @buffer: The ring buffer
1979 *
1980 * Returns the total number of entries in the ring buffer
1981 * (all CPU entries)
1982 */
1983unsigned long ring_buffer_entries(struct ring_buffer *buffer)
1984{
1985 struct ring_buffer_per_cpu *cpu_buffer;
1986 unsigned long entries = 0;
1987 int cpu;
1988
1989 /* if you care about this being correct, lock the buffer */
1990 for_each_buffer_cpu(buffer, cpu) {
1991 cpu_buffer = buffer->buffers[cpu];
Steven Rostedte4906ef2009-04-30 20:49:44 -04001992 entries += (local_read(&cpu_buffer->entries) -
1993 cpu_buffer->overrun) - cpu_buffer->read;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001994 }
1995
1996 return entries;
1997}
Robert Richterc4f50182008-12-11 16:49:22 +01001998EXPORT_SYMBOL_GPL(ring_buffer_entries);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001999
2000/**
2001 * ring_buffer_overrun_cpu - get the number of overruns in buffer
2002 * @buffer: The ring buffer
2003 *
2004 * Returns the total number of overruns in the ring buffer
2005 * (all CPU entries)
2006 */
2007unsigned long ring_buffer_overruns(struct ring_buffer *buffer)
2008{
2009 struct ring_buffer_per_cpu *cpu_buffer;
2010 unsigned long overruns = 0;
2011 int cpu;
2012
2013 /* if you care about this being correct, lock the buffer */
2014 for_each_buffer_cpu(buffer, cpu) {
2015 cpu_buffer = buffer->buffers[cpu];
2016 overruns += cpu_buffer->overrun;
2017 }
2018
2019 return overruns;
2020}
Robert Richterc4f50182008-12-11 16:49:22 +01002021EXPORT_SYMBOL_GPL(ring_buffer_overruns);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002022
Steven Rostedt642edba2008-11-12 00:01:26 -05002023static void rb_iter_reset(struct ring_buffer_iter *iter)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002024{
2025 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
2026
Steven Rostedtd7690412008-10-01 00:29:53 -04002027 /* Iterator usage is expected to have record disabled */
2028 if (list_empty(&cpu_buffer->reader_page->list)) {
2029 iter->head_page = cpu_buffer->head_page;
Steven Rostedt6f807ac2008-10-04 02:00:58 -04002030 iter->head = cpu_buffer->head_page->read;
Steven Rostedtd7690412008-10-01 00:29:53 -04002031 } else {
2032 iter->head_page = cpu_buffer->reader_page;
Steven Rostedt6f807ac2008-10-04 02:00:58 -04002033 iter->head = cpu_buffer->reader_page->read;
Steven Rostedtd7690412008-10-01 00:29:53 -04002034 }
2035 if (iter->head)
2036 iter->read_stamp = cpu_buffer->read_stamp;
2037 else
Steven Rostedtabc9b562008-12-02 15:34:06 -05002038 iter->read_stamp = iter->head_page->page->time_stamp;
Steven Rostedt642edba2008-11-12 00:01:26 -05002039}
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002040
Steven Rostedt642edba2008-11-12 00:01:26 -05002041/**
2042 * ring_buffer_iter_reset - reset an iterator
2043 * @iter: The iterator to reset
2044 *
2045 * Resets the iterator, so that it will start from the beginning
2046 * again.
2047 */
2048void ring_buffer_iter_reset(struct ring_buffer_iter *iter)
2049{
Steven Rostedt554f7862009-03-11 22:00:13 -04002050 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedt642edba2008-11-12 00:01:26 -05002051 unsigned long flags;
2052
Steven Rostedt554f7862009-03-11 22:00:13 -04002053 if (!iter)
2054 return;
2055
2056 cpu_buffer = iter->cpu_buffer;
2057
Steven Rostedt642edba2008-11-12 00:01:26 -05002058 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
2059 rb_iter_reset(iter);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002060 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002061}
Robert Richterc4f50182008-12-11 16:49:22 +01002062EXPORT_SYMBOL_GPL(ring_buffer_iter_reset);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002063
2064/**
2065 * ring_buffer_iter_empty - check if an iterator has no more to read
2066 * @iter: The iterator to check
2067 */
2068int ring_buffer_iter_empty(struct ring_buffer_iter *iter)
2069{
2070 struct ring_buffer_per_cpu *cpu_buffer;
2071
2072 cpu_buffer = iter->cpu_buffer;
2073
Steven Rostedtbf41a152008-10-04 02:00:59 -04002074 return iter->head_page == cpu_buffer->commit_page &&
2075 iter->head == rb_commit_index(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002076}
Robert Richterc4f50182008-12-11 16:49:22 +01002077EXPORT_SYMBOL_GPL(ring_buffer_iter_empty);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002078
2079static void
2080rb_update_read_stamp(struct ring_buffer_per_cpu *cpu_buffer,
2081 struct ring_buffer_event *event)
2082{
2083 u64 delta;
2084
Lai Jiangshan334d4162009-04-24 11:27:05 +08002085 switch (event->type_len) {
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002086 case RINGBUF_TYPE_PADDING:
2087 return;
2088
2089 case RINGBUF_TYPE_TIME_EXTEND:
2090 delta = event->array[0];
2091 delta <<= TS_SHIFT;
2092 delta += event->time_delta;
2093 cpu_buffer->read_stamp += delta;
2094 return;
2095
2096 case RINGBUF_TYPE_TIME_STAMP:
2097 /* FIXME: not implemented */
2098 return;
2099
2100 case RINGBUF_TYPE_DATA:
2101 cpu_buffer->read_stamp += event->time_delta;
2102 return;
2103
2104 default:
2105 BUG();
2106 }
2107 return;
2108}
2109
2110static void
2111rb_update_iter_read_stamp(struct ring_buffer_iter *iter,
2112 struct ring_buffer_event *event)
2113{
2114 u64 delta;
2115
Lai Jiangshan334d4162009-04-24 11:27:05 +08002116 switch (event->type_len) {
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002117 case RINGBUF_TYPE_PADDING:
2118 return;
2119
2120 case RINGBUF_TYPE_TIME_EXTEND:
2121 delta = event->array[0];
2122 delta <<= TS_SHIFT;
2123 delta += event->time_delta;
2124 iter->read_stamp += delta;
2125 return;
2126
2127 case RINGBUF_TYPE_TIME_STAMP:
2128 /* FIXME: not implemented */
2129 return;
2130
2131 case RINGBUF_TYPE_DATA:
2132 iter->read_stamp += event->time_delta;
2133 return;
2134
2135 default:
2136 BUG();
2137 }
2138 return;
2139}
2140
Steven Rostedtd7690412008-10-01 00:29:53 -04002141static struct buffer_page *
2142rb_get_reader_page(struct ring_buffer_per_cpu *cpu_buffer)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002143{
Steven Rostedtd7690412008-10-01 00:29:53 -04002144 struct buffer_page *reader = NULL;
2145 unsigned long flags;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002146 int nr_loops = 0;
Steven Rostedtd7690412008-10-01 00:29:53 -04002147
Steven Rostedt3e03fb72008-11-06 00:09:43 -05002148 local_irq_save(flags);
2149 __raw_spin_lock(&cpu_buffer->lock);
Steven Rostedtd7690412008-10-01 00:29:53 -04002150
2151 again:
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002152 /*
2153 * This should normally only loop twice. But because the
2154 * start of the reader inserts an empty page, it causes
2155 * a case where we will loop three times. There should be no
2156 * reason to loop four times (that I know of).
2157 */
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05002158 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 3)) {
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002159 reader = NULL;
2160 goto out;
2161 }
2162
Steven Rostedtd7690412008-10-01 00:29:53 -04002163 reader = cpu_buffer->reader_page;
2164
2165 /* If there's more to read, return this page */
Steven Rostedtbf41a152008-10-04 02:00:59 -04002166 if (cpu_buffer->reader_page->read < rb_page_size(reader))
Steven Rostedtd7690412008-10-01 00:29:53 -04002167 goto out;
2168
2169 /* Never should we have an index greater than the size */
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05002170 if (RB_WARN_ON(cpu_buffer,
2171 cpu_buffer->reader_page->read > rb_page_size(reader)))
2172 goto out;
Steven Rostedtd7690412008-10-01 00:29:53 -04002173
2174 /* check if we caught up to the tail */
2175 reader = NULL;
Steven Rostedtbf41a152008-10-04 02:00:59 -04002176 if (cpu_buffer->commit_page == cpu_buffer->reader_page)
Steven Rostedtd7690412008-10-01 00:29:53 -04002177 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002178
2179 /*
Steven Rostedtd7690412008-10-01 00:29:53 -04002180 * Splice the empty reader page into the list around the head.
2181 * Reset the reader page to size zero.
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002182 */
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002183
Steven Rostedtd7690412008-10-01 00:29:53 -04002184 reader = cpu_buffer->head_page;
2185 cpu_buffer->reader_page->list.next = reader->list.next;
2186 cpu_buffer->reader_page->list.prev = reader->list.prev;
Steven Rostedtbf41a152008-10-04 02:00:59 -04002187
2188 local_set(&cpu_buffer->reader_page->write, 0);
Steven Rostedt778c55d2009-05-01 18:44:45 -04002189 local_set(&cpu_buffer->reader_page->entries, 0);
Steven Rostedtabc9b562008-12-02 15:34:06 -05002190 local_set(&cpu_buffer->reader_page->page->commit, 0);
Steven Rostedtd7690412008-10-01 00:29:53 -04002191
2192 /* Make the reader page now replace the head */
2193 reader->list.prev->next = &cpu_buffer->reader_page->list;
2194 reader->list.next->prev = &cpu_buffer->reader_page->list;
2195
2196 /*
2197 * If the tail is on the reader, then we must set the head
2198 * to the inserted page, otherwise we set it one before.
2199 */
2200 cpu_buffer->head_page = cpu_buffer->reader_page;
2201
Steven Rostedtbf41a152008-10-04 02:00:59 -04002202 if (cpu_buffer->commit_page != reader)
Steven Rostedtd7690412008-10-01 00:29:53 -04002203 rb_inc_page(cpu_buffer, &cpu_buffer->head_page);
2204
2205 /* Finally update the reader page to the new head */
2206 cpu_buffer->reader_page = reader;
2207 rb_reset_reader_page(cpu_buffer);
2208
2209 goto again;
2210
2211 out:
Steven Rostedt3e03fb72008-11-06 00:09:43 -05002212 __raw_spin_unlock(&cpu_buffer->lock);
2213 local_irq_restore(flags);
Steven Rostedtd7690412008-10-01 00:29:53 -04002214
2215 return reader;
2216}
2217
2218static void rb_advance_reader(struct ring_buffer_per_cpu *cpu_buffer)
2219{
2220 struct ring_buffer_event *event;
2221 struct buffer_page *reader;
2222 unsigned length;
2223
2224 reader = rb_get_reader_page(cpu_buffer);
2225
2226 /* This function should not be called when buffer is empty */
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05002227 if (RB_WARN_ON(cpu_buffer, !reader))
2228 return;
Steven Rostedtd7690412008-10-01 00:29:53 -04002229
2230 event = rb_reader_event(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002231
Lai Jiangshan334d4162009-04-24 11:27:05 +08002232 if (event->type_len <= RINGBUF_TYPE_DATA_TYPE_LEN_MAX
2233 || rb_discarded_event(event))
Steven Rostedte4906ef2009-04-30 20:49:44 -04002234 cpu_buffer->read++;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002235
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002236 rb_update_read_stamp(cpu_buffer, event);
2237
Steven Rostedtd7690412008-10-01 00:29:53 -04002238 length = rb_event_length(event);
Steven Rostedt6f807ac2008-10-04 02:00:58 -04002239 cpu_buffer->reader_page->read += length;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002240}
2241
2242static void rb_advance_iter(struct ring_buffer_iter *iter)
2243{
2244 struct ring_buffer *buffer;
2245 struct ring_buffer_per_cpu *cpu_buffer;
2246 struct ring_buffer_event *event;
2247 unsigned length;
2248
2249 cpu_buffer = iter->cpu_buffer;
2250 buffer = cpu_buffer->buffer;
2251
2252 /*
2253 * Check if we are at the end of the buffer.
2254 */
Steven Rostedtbf41a152008-10-04 02:00:59 -04002255 if (iter->head >= rb_page_size(iter->head_page)) {
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05002256 if (RB_WARN_ON(buffer,
2257 iter->head_page == cpu_buffer->commit_page))
2258 return;
Steven Rostedtd7690412008-10-01 00:29:53 -04002259 rb_inc_iter(iter);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002260 return;
2261 }
2262
2263 event = rb_iter_head_event(iter);
2264
2265 length = rb_event_length(event);
2266
2267 /*
2268 * This should not be called to advance the header if we are
2269 * at the tail of the buffer.
2270 */
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05002271 if (RB_WARN_ON(cpu_buffer,
Steven Rostedtf536aaf2008-11-10 23:07:30 -05002272 (iter->head_page == cpu_buffer->commit_page) &&
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05002273 (iter->head + length > rb_commit_index(cpu_buffer))))
2274 return;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002275
2276 rb_update_iter_read_stamp(iter, event);
2277
2278 iter->head += length;
2279
2280 /* check for end of page padding */
Steven Rostedtbf41a152008-10-04 02:00:59 -04002281 if ((iter->head >= rb_page_size(iter->head_page)) &&
2282 (iter->head_page != cpu_buffer->commit_page))
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002283 rb_advance_iter(iter);
2284}
2285
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002286static struct ring_buffer_event *
2287rb_buffer_peek(struct ring_buffer *buffer, int cpu, u64 *ts)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002288{
2289 struct ring_buffer_per_cpu *cpu_buffer;
2290 struct ring_buffer_event *event;
Steven Rostedtd7690412008-10-01 00:29:53 -04002291 struct buffer_page *reader;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002292 int nr_loops = 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002293
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002294 cpu_buffer = buffer->buffers[cpu];
2295
2296 again:
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002297 /*
2298 * We repeat when a timestamp is encountered. It is possible
2299 * to get multiple timestamps from an interrupt entering just
2300 * as one timestamp is about to be written. The max times
2301 * that this can happen is the number of nested interrupts we
2302 * can have. Nesting 10 deep of interrupts is clearly
2303 * an anomaly.
2304 */
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05002305 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 10))
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002306 return NULL;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002307
Steven Rostedtd7690412008-10-01 00:29:53 -04002308 reader = rb_get_reader_page(cpu_buffer);
2309 if (!reader)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002310 return NULL;
2311
Steven Rostedtd7690412008-10-01 00:29:53 -04002312 event = rb_reader_event(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002313
Lai Jiangshan334d4162009-04-24 11:27:05 +08002314 switch (event->type_len) {
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002315 case RINGBUF_TYPE_PADDING:
Tom Zanussi2d622712009-03-22 03:30:49 -05002316 if (rb_null_event(event))
2317 RB_WARN_ON(cpu_buffer, 1);
2318 /*
2319 * Because the writer could be discarding every
2320 * event it creates (which would probably be bad)
2321 * if we were to go back to "again" then we may never
2322 * catch up, and will trigger the warn on, or lock
2323 * the box. Return the padding, and we will release
2324 * the current locks, and try again.
2325 */
Steven Rostedtd7690412008-10-01 00:29:53 -04002326 rb_advance_reader(cpu_buffer);
Tom Zanussi2d622712009-03-22 03:30:49 -05002327 return event;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002328
2329 case RINGBUF_TYPE_TIME_EXTEND:
2330 /* Internal data, OK to advance */
Steven Rostedtd7690412008-10-01 00:29:53 -04002331 rb_advance_reader(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002332 goto again;
2333
2334 case RINGBUF_TYPE_TIME_STAMP:
2335 /* FIXME: not implemented */
Steven Rostedtd7690412008-10-01 00:29:53 -04002336 rb_advance_reader(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002337 goto again;
2338
2339 case RINGBUF_TYPE_DATA:
2340 if (ts) {
2341 *ts = cpu_buffer->read_stamp + event->time_delta;
Steven Rostedt37886f62009-03-17 17:22:06 -04002342 ring_buffer_normalize_time_stamp(buffer,
2343 cpu_buffer->cpu, ts);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002344 }
2345 return event;
2346
2347 default:
2348 BUG();
2349 }
2350
2351 return NULL;
2352}
Robert Richterc4f50182008-12-11 16:49:22 +01002353EXPORT_SYMBOL_GPL(ring_buffer_peek);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002354
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002355static struct ring_buffer_event *
2356rb_iter_peek(struct ring_buffer_iter *iter, u64 *ts)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002357{
2358 struct ring_buffer *buffer;
2359 struct ring_buffer_per_cpu *cpu_buffer;
2360 struct ring_buffer_event *event;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002361 int nr_loops = 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002362
2363 if (ring_buffer_iter_empty(iter))
2364 return NULL;
2365
2366 cpu_buffer = iter->cpu_buffer;
2367 buffer = cpu_buffer->buffer;
2368
2369 again:
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002370 /*
2371 * We repeat when a timestamp is encountered. It is possible
2372 * to get multiple timestamps from an interrupt entering just
2373 * as one timestamp is about to be written. The max times
2374 * that this can happen is the number of nested interrupts we
2375 * can have. Nesting 10 deep of interrupts is clearly
2376 * an anomaly.
2377 */
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05002378 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 10))
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002379 return NULL;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002380
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002381 if (rb_per_cpu_empty(cpu_buffer))
2382 return NULL;
2383
2384 event = rb_iter_head_event(iter);
2385
Lai Jiangshan334d4162009-04-24 11:27:05 +08002386 switch (event->type_len) {
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002387 case RINGBUF_TYPE_PADDING:
Tom Zanussi2d622712009-03-22 03:30:49 -05002388 if (rb_null_event(event)) {
2389 rb_inc_iter(iter);
2390 goto again;
2391 }
2392 rb_advance_iter(iter);
2393 return event;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002394
2395 case RINGBUF_TYPE_TIME_EXTEND:
2396 /* Internal data, OK to advance */
2397 rb_advance_iter(iter);
2398 goto again;
2399
2400 case RINGBUF_TYPE_TIME_STAMP:
2401 /* FIXME: not implemented */
2402 rb_advance_iter(iter);
2403 goto again;
2404
2405 case RINGBUF_TYPE_DATA:
2406 if (ts) {
2407 *ts = iter->read_stamp + event->time_delta;
Steven Rostedt37886f62009-03-17 17:22:06 -04002408 ring_buffer_normalize_time_stamp(buffer,
2409 cpu_buffer->cpu, ts);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002410 }
2411 return event;
2412
2413 default:
2414 BUG();
2415 }
2416
2417 return NULL;
2418}
Robert Richterc4f50182008-12-11 16:49:22 +01002419EXPORT_SYMBOL_GPL(ring_buffer_iter_peek);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002420
2421/**
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002422 * ring_buffer_peek - peek at the next event to be read
2423 * @buffer: The ring buffer to read
2424 * @cpu: The cpu to peak at
2425 * @ts: The timestamp counter of this event.
2426 *
2427 * This will return the event that will be read next, but does
2428 * not consume the data.
2429 */
2430struct ring_buffer_event *
2431ring_buffer_peek(struct ring_buffer *buffer, int cpu, u64 *ts)
2432{
2433 struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
Steven Rostedt8aabee52009-03-12 13:13:49 -04002434 struct ring_buffer_event *event;
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002435 unsigned long flags;
2436
Steven Rostedt554f7862009-03-11 22:00:13 -04002437 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04002438 return NULL;
Steven Rostedt554f7862009-03-11 22:00:13 -04002439
Tom Zanussi2d622712009-03-22 03:30:49 -05002440 again:
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002441 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
2442 event = rb_buffer_peek(buffer, cpu, ts);
2443 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
2444
Lai Jiangshan334d4162009-04-24 11:27:05 +08002445 if (event && event->type_len == RINGBUF_TYPE_PADDING) {
Tom Zanussi2d622712009-03-22 03:30:49 -05002446 cpu_relax();
2447 goto again;
2448 }
2449
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002450 return event;
2451}
2452
2453/**
2454 * ring_buffer_iter_peek - peek at the next event to be read
2455 * @iter: The ring buffer iterator
2456 * @ts: The timestamp counter of this event.
2457 *
2458 * This will return the event that will be read next, but does
2459 * not increment the iterator.
2460 */
2461struct ring_buffer_event *
2462ring_buffer_iter_peek(struct ring_buffer_iter *iter, u64 *ts)
2463{
2464 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
2465 struct ring_buffer_event *event;
2466 unsigned long flags;
2467
Tom Zanussi2d622712009-03-22 03:30:49 -05002468 again:
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002469 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
2470 event = rb_iter_peek(iter, ts);
2471 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
2472
Lai Jiangshan334d4162009-04-24 11:27:05 +08002473 if (event && event->type_len == RINGBUF_TYPE_PADDING) {
Tom Zanussi2d622712009-03-22 03:30:49 -05002474 cpu_relax();
2475 goto again;
2476 }
2477
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002478 return event;
2479}
2480
2481/**
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002482 * ring_buffer_consume - return an event and consume it
2483 * @buffer: The ring buffer to get the next event from
2484 *
2485 * Returns the next event in the ring buffer, and that event is consumed.
2486 * Meaning, that sequential reads will keep returning a different event,
2487 * and eventually empty the ring buffer if the producer is slower.
2488 */
2489struct ring_buffer_event *
2490ring_buffer_consume(struct ring_buffer *buffer, int cpu, u64 *ts)
2491{
Steven Rostedt554f7862009-03-11 22:00:13 -04002492 struct ring_buffer_per_cpu *cpu_buffer;
2493 struct ring_buffer_event *event = NULL;
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002494 unsigned long flags;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002495
Tom Zanussi2d622712009-03-22 03:30:49 -05002496 again:
Steven Rostedt554f7862009-03-11 22:00:13 -04002497 /* might be called in atomic */
2498 preempt_disable();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002499
Steven Rostedt554f7862009-03-11 22:00:13 -04002500 if (!cpumask_test_cpu(cpu, buffer->cpumask))
2501 goto out;
2502
2503 cpu_buffer = buffer->buffers[cpu];
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002504 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002505
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002506 event = rb_buffer_peek(buffer, cpu, ts);
2507 if (!event)
Steven Rostedt554f7862009-03-11 22:00:13 -04002508 goto out_unlock;
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002509
Steven Rostedtd7690412008-10-01 00:29:53 -04002510 rb_advance_reader(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002511
Steven Rostedt554f7862009-03-11 22:00:13 -04002512 out_unlock:
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002513 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
2514
Steven Rostedt554f7862009-03-11 22:00:13 -04002515 out:
2516 preempt_enable();
2517
Lai Jiangshan334d4162009-04-24 11:27:05 +08002518 if (event && event->type_len == RINGBUF_TYPE_PADDING) {
Tom Zanussi2d622712009-03-22 03:30:49 -05002519 cpu_relax();
2520 goto again;
2521 }
2522
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002523 return event;
2524}
Robert Richterc4f50182008-12-11 16:49:22 +01002525EXPORT_SYMBOL_GPL(ring_buffer_consume);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002526
2527/**
2528 * ring_buffer_read_start - start a non consuming read of the buffer
2529 * @buffer: The ring buffer to read from
2530 * @cpu: The cpu buffer to iterate over
2531 *
2532 * This starts up an iteration through the buffer. It also disables
2533 * the recording to the buffer until the reading is finished.
2534 * This prevents the reading from being corrupted. This is not
2535 * a consuming read, so a producer is not expected.
2536 *
2537 * Must be paired with ring_buffer_finish.
2538 */
2539struct ring_buffer_iter *
2540ring_buffer_read_start(struct ring_buffer *buffer, int cpu)
2541{
2542 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedt8aabee52009-03-12 13:13:49 -04002543 struct ring_buffer_iter *iter;
Steven Rostedtd7690412008-10-01 00:29:53 -04002544 unsigned long flags;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002545
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302546 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04002547 return NULL;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002548
2549 iter = kmalloc(sizeof(*iter), GFP_KERNEL);
2550 if (!iter)
Steven Rostedt8aabee52009-03-12 13:13:49 -04002551 return NULL;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002552
2553 cpu_buffer = buffer->buffers[cpu];
2554
2555 iter->cpu_buffer = cpu_buffer;
2556
2557 atomic_inc(&cpu_buffer->record_disabled);
2558 synchronize_sched();
2559
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002560 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
Steven Rostedt3e03fb72008-11-06 00:09:43 -05002561 __raw_spin_lock(&cpu_buffer->lock);
Steven Rostedt642edba2008-11-12 00:01:26 -05002562 rb_iter_reset(iter);
Steven Rostedt3e03fb72008-11-06 00:09:43 -05002563 __raw_spin_unlock(&cpu_buffer->lock);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002564 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002565
2566 return iter;
2567}
Robert Richterc4f50182008-12-11 16:49:22 +01002568EXPORT_SYMBOL_GPL(ring_buffer_read_start);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002569
2570/**
2571 * ring_buffer_finish - finish reading the iterator of the buffer
2572 * @iter: The iterator retrieved by ring_buffer_start
2573 *
2574 * This re-enables the recording to the buffer, and frees the
2575 * iterator.
2576 */
2577void
2578ring_buffer_read_finish(struct ring_buffer_iter *iter)
2579{
2580 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
2581
2582 atomic_dec(&cpu_buffer->record_disabled);
2583 kfree(iter);
2584}
Robert Richterc4f50182008-12-11 16:49:22 +01002585EXPORT_SYMBOL_GPL(ring_buffer_read_finish);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002586
2587/**
2588 * ring_buffer_read - read the next item in the ring buffer by the iterator
2589 * @iter: The ring buffer iterator
2590 * @ts: The time stamp of the event read.
2591 *
2592 * This reads the next event in the ring buffer and increments the iterator.
2593 */
2594struct ring_buffer_event *
2595ring_buffer_read(struct ring_buffer_iter *iter, u64 *ts)
2596{
2597 struct ring_buffer_event *event;
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002598 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
2599 unsigned long flags;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002600
Tom Zanussi2d622712009-03-22 03:30:49 -05002601 again:
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002602 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
2603 event = rb_iter_peek(iter, ts);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002604 if (!event)
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002605 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002606
2607 rb_advance_iter(iter);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002608 out:
2609 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002610
Lai Jiangshan334d4162009-04-24 11:27:05 +08002611 if (event && event->type_len == RINGBUF_TYPE_PADDING) {
Tom Zanussi2d622712009-03-22 03:30:49 -05002612 cpu_relax();
2613 goto again;
2614 }
2615
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002616 return event;
2617}
Robert Richterc4f50182008-12-11 16:49:22 +01002618EXPORT_SYMBOL_GPL(ring_buffer_read);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002619
2620/**
2621 * ring_buffer_size - return the size of the ring buffer (in bytes)
2622 * @buffer: The ring buffer.
2623 */
2624unsigned long ring_buffer_size(struct ring_buffer *buffer)
2625{
2626 return BUF_PAGE_SIZE * buffer->pages;
2627}
Robert Richterc4f50182008-12-11 16:49:22 +01002628EXPORT_SYMBOL_GPL(ring_buffer_size);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002629
2630static void
2631rb_reset_cpu(struct ring_buffer_per_cpu *cpu_buffer)
2632{
2633 cpu_buffer->head_page
2634 = list_entry(cpu_buffer->pages.next, struct buffer_page, list);
Steven Rostedtbf41a152008-10-04 02:00:59 -04002635 local_set(&cpu_buffer->head_page->write, 0);
Steven Rostedt778c55d2009-05-01 18:44:45 -04002636 local_set(&cpu_buffer->head_page->entries, 0);
Steven Rostedtabc9b562008-12-02 15:34:06 -05002637 local_set(&cpu_buffer->head_page->page->commit, 0);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002638
Steven Rostedt6f807ac2008-10-04 02:00:58 -04002639 cpu_buffer->head_page->read = 0;
Steven Rostedtbf41a152008-10-04 02:00:59 -04002640
2641 cpu_buffer->tail_page = cpu_buffer->head_page;
2642 cpu_buffer->commit_page = cpu_buffer->head_page;
2643
2644 INIT_LIST_HEAD(&cpu_buffer->reader_page->list);
2645 local_set(&cpu_buffer->reader_page->write, 0);
Steven Rostedt778c55d2009-05-01 18:44:45 -04002646 local_set(&cpu_buffer->reader_page->entries, 0);
Steven Rostedtabc9b562008-12-02 15:34:06 -05002647 local_set(&cpu_buffer->reader_page->page->commit, 0);
Steven Rostedt6f807ac2008-10-04 02:00:58 -04002648 cpu_buffer->reader_page->read = 0;
Steven Rostedtd7690412008-10-01 00:29:53 -04002649
Steven Rostedtf0d2c682009-04-29 13:43:37 -04002650 cpu_buffer->nmi_dropped = 0;
2651 cpu_buffer->commit_overrun = 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002652 cpu_buffer->overrun = 0;
Steven Rostedte4906ef2009-04-30 20:49:44 -04002653 cpu_buffer->read = 0;
2654 local_set(&cpu_buffer->entries, 0);
Steven Rostedt69507c02009-01-21 18:45:57 -05002655
2656 cpu_buffer->write_stamp = 0;
2657 cpu_buffer->read_stamp = 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002658}
2659
2660/**
2661 * ring_buffer_reset_cpu - reset a ring buffer per CPU buffer
2662 * @buffer: The ring buffer to reset a per cpu buffer of
2663 * @cpu: The CPU buffer to be reset
2664 */
2665void ring_buffer_reset_cpu(struct ring_buffer *buffer, int cpu)
2666{
2667 struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
2668 unsigned long flags;
2669
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302670 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04002671 return;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002672
Steven Rostedt41ede232009-05-01 20:26:54 -04002673 atomic_inc(&cpu_buffer->record_disabled);
2674
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002675 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
2676
Steven Rostedt3e03fb72008-11-06 00:09:43 -05002677 __raw_spin_lock(&cpu_buffer->lock);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002678
2679 rb_reset_cpu(cpu_buffer);
2680
Steven Rostedt3e03fb72008-11-06 00:09:43 -05002681 __raw_spin_unlock(&cpu_buffer->lock);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002682
2683 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
Steven Rostedt41ede232009-05-01 20:26:54 -04002684
2685 atomic_dec(&cpu_buffer->record_disabled);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002686}
Robert Richterc4f50182008-12-11 16:49:22 +01002687EXPORT_SYMBOL_GPL(ring_buffer_reset_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002688
2689/**
2690 * ring_buffer_reset - reset a ring buffer
2691 * @buffer: The ring buffer to reset all cpu buffers
2692 */
2693void ring_buffer_reset(struct ring_buffer *buffer)
2694{
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002695 int cpu;
2696
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002697 for_each_buffer_cpu(buffer, cpu)
Steven Rostedtd7690412008-10-01 00:29:53 -04002698 ring_buffer_reset_cpu(buffer, cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002699}
Robert Richterc4f50182008-12-11 16:49:22 +01002700EXPORT_SYMBOL_GPL(ring_buffer_reset);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002701
2702/**
2703 * rind_buffer_empty - is the ring buffer empty?
2704 * @buffer: The ring buffer to test
2705 */
2706int ring_buffer_empty(struct ring_buffer *buffer)
2707{
2708 struct ring_buffer_per_cpu *cpu_buffer;
2709 int cpu;
2710
2711 /* yes this is racy, but if you don't like the race, lock the buffer */
2712 for_each_buffer_cpu(buffer, cpu) {
2713 cpu_buffer = buffer->buffers[cpu];
2714 if (!rb_per_cpu_empty(cpu_buffer))
2715 return 0;
2716 }
Steven Rostedt554f7862009-03-11 22:00:13 -04002717
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002718 return 1;
2719}
Robert Richterc4f50182008-12-11 16:49:22 +01002720EXPORT_SYMBOL_GPL(ring_buffer_empty);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002721
2722/**
2723 * ring_buffer_empty_cpu - is a cpu buffer of a ring buffer empty?
2724 * @buffer: The ring buffer
2725 * @cpu: The CPU buffer to test
2726 */
2727int ring_buffer_empty_cpu(struct ring_buffer *buffer, int cpu)
2728{
2729 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedt8aabee52009-03-12 13:13:49 -04002730 int ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002731
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302732 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04002733 return 1;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002734
2735 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt554f7862009-03-11 22:00:13 -04002736 ret = rb_per_cpu_empty(cpu_buffer);
2737
Steven Rostedt554f7862009-03-11 22:00:13 -04002738
2739 return ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002740}
Robert Richterc4f50182008-12-11 16:49:22 +01002741EXPORT_SYMBOL_GPL(ring_buffer_empty_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002742
2743/**
2744 * ring_buffer_swap_cpu - swap a CPU buffer between two ring buffers
2745 * @buffer_a: One buffer to swap with
2746 * @buffer_b: The other buffer to swap with
2747 *
2748 * This function is useful for tracers that want to take a "snapshot"
2749 * of a CPU buffer and has another back up buffer lying around.
2750 * it is expected that the tracer handles the cpu buffer not being
2751 * used at the moment.
2752 */
2753int ring_buffer_swap_cpu(struct ring_buffer *buffer_a,
2754 struct ring_buffer *buffer_b, int cpu)
2755{
2756 struct ring_buffer_per_cpu *cpu_buffer_a;
2757 struct ring_buffer_per_cpu *cpu_buffer_b;
Steven Rostedt554f7862009-03-11 22:00:13 -04002758 int ret = -EINVAL;
2759
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302760 if (!cpumask_test_cpu(cpu, buffer_a->cpumask) ||
2761 !cpumask_test_cpu(cpu, buffer_b->cpumask))
Steven Rostedt554f7862009-03-11 22:00:13 -04002762 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002763
2764 /* At least make sure the two buffers are somewhat the same */
Lai Jiangshan6d102bc2008-12-17 17:48:23 +08002765 if (buffer_a->pages != buffer_b->pages)
Steven Rostedt554f7862009-03-11 22:00:13 -04002766 goto out;
2767
2768 ret = -EAGAIN;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002769
Steven Rostedt97b17ef2009-01-21 15:24:56 -05002770 if (ring_buffer_flags != RB_BUFFERS_ON)
Steven Rostedt554f7862009-03-11 22:00:13 -04002771 goto out;
Steven Rostedt97b17ef2009-01-21 15:24:56 -05002772
2773 if (atomic_read(&buffer_a->record_disabled))
Steven Rostedt554f7862009-03-11 22:00:13 -04002774 goto out;
Steven Rostedt97b17ef2009-01-21 15:24:56 -05002775
2776 if (atomic_read(&buffer_b->record_disabled))
Steven Rostedt554f7862009-03-11 22:00:13 -04002777 goto out;
Steven Rostedt97b17ef2009-01-21 15:24:56 -05002778
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002779 cpu_buffer_a = buffer_a->buffers[cpu];
2780 cpu_buffer_b = buffer_b->buffers[cpu];
2781
Steven Rostedt97b17ef2009-01-21 15:24:56 -05002782 if (atomic_read(&cpu_buffer_a->record_disabled))
Steven Rostedt554f7862009-03-11 22:00:13 -04002783 goto out;
Steven Rostedt97b17ef2009-01-21 15:24:56 -05002784
2785 if (atomic_read(&cpu_buffer_b->record_disabled))
Steven Rostedt554f7862009-03-11 22:00:13 -04002786 goto out;
Steven Rostedt97b17ef2009-01-21 15:24:56 -05002787
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002788 /*
2789 * We can't do a synchronize_sched here because this
2790 * function can be called in atomic context.
2791 * Normally this will be called from the same CPU as cpu.
2792 * If not it's up to the caller to protect this.
2793 */
2794 atomic_inc(&cpu_buffer_a->record_disabled);
2795 atomic_inc(&cpu_buffer_b->record_disabled);
2796
2797 buffer_a->buffers[cpu] = cpu_buffer_b;
2798 buffer_b->buffers[cpu] = cpu_buffer_a;
2799
2800 cpu_buffer_b->buffer = buffer_a;
2801 cpu_buffer_a->buffer = buffer_b;
2802
2803 atomic_dec(&cpu_buffer_a->record_disabled);
2804 atomic_dec(&cpu_buffer_b->record_disabled);
2805
Steven Rostedt554f7862009-03-11 22:00:13 -04002806 ret = 0;
2807out:
Steven Rostedt554f7862009-03-11 22:00:13 -04002808 return ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002809}
Robert Richterc4f50182008-12-11 16:49:22 +01002810EXPORT_SYMBOL_GPL(ring_buffer_swap_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002811
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002812/**
2813 * ring_buffer_alloc_read_page - allocate a page to read from buffer
2814 * @buffer: the buffer to allocate for.
2815 *
2816 * This function is used in conjunction with ring_buffer_read_page.
2817 * When reading a full page from the ring buffer, these functions
2818 * can be used to speed up the process. The calling function should
2819 * allocate a few pages first with this function. Then when it
2820 * needs to get pages from the ring buffer, it passes the result
2821 * of this function into ring_buffer_read_page, which will swap
2822 * the page that was allocated, with the read page of the buffer.
2823 *
2824 * Returns:
2825 * The page allocated, or NULL on error.
2826 */
2827void *ring_buffer_alloc_read_page(struct ring_buffer *buffer)
2828{
Steven Rostedt044fa782008-12-02 23:50:03 -05002829 struct buffer_data_page *bpage;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05002830 unsigned long addr;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002831
2832 addr = __get_free_page(GFP_KERNEL);
2833 if (!addr)
2834 return NULL;
2835
Steven Rostedt044fa782008-12-02 23:50:03 -05002836 bpage = (void *)addr;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002837
Steven Rostedtef7a4a12009-03-03 00:27:49 -05002838 rb_init_page(bpage);
2839
Steven Rostedt044fa782008-12-02 23:50:03 -05002840 return bpage;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002841}
Steven Rostedtd6ce96d2009-05-05 01:15:24 -04002842EXPORT_SYMBOL_GPL(ring_buffer_alloc_read_page);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002843
2844/**
2845 * ring_buffer_free_read_page - free an allocated read page
2846 * @buffer: the buffer the page was allocate for
2847 * @data: the page to free
2848 *
2849 * Free a page allocated from ring_buffer_alloc_read_page.
2850 */
2851void ring_buffer_free_read_page(struct ring_buffer *buffer, void *data)
2852{
2853 free_page((unsigned long)data);
2854}
Steven Rostedtd6ce96d2009-05-05 01:15:24 -04002855EXPORT_SYMBOL_GPL(ring_buffer_free_read_page);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002856
2857/**
2858 * ring_buffer_read_page - extract a page from the ring buffer
2859 * @buffer: buffer to extract from
2860 * @data_page: the page to use allocated from ring_buffer_alloc_read_page
Steven Rostedtef7a4a12009-03-03 00:27:49 -05002861 * @len: amount to extract
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002862 * @cpu: the cpu of the buffer to extract
2863 * @full: should the extraction only happen when the page is full.
2864 *
2865 * This function will pull out a page from the ring buffer and consume it.
2866 * @data_page must be the address of the variable that was returned
2867 * from ring_buffer_alloc_read_page. This is because the page might be used
2868 * to swap with a page in the ring buffer.
2869 *
2870 * for example:
Lai Jiangshanb85fa012009-02-09 14:21:14 +08002871 * rpage = ring_buffer_alloc_read_page(buffer);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002872 * if (!rpage)
2873 * return error;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05002874 * ret = ring_buffer_read_page(buffer, &rpage, len, cpu, 0);
Lai Jiangshan667d2412009-02-09 14:21:17 +08002875 * if (ret >= 0)
2876 * process_page(rpage, ret);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002877 *
2878 * When @full is set, the function will not return true unless
2879 * the writer is off the reader page.
2880 *
2881 * Note: it is up to the calling functions to handle sleeps and wakeups.
2882 * The ring buffer can be used anywhere in the kernel and can not
2883 * blindly call wake_up. The layer that uses the ring buffer must be
2884 * responsible for that.
2885 *
2886 * Returns:
Lai Jiangshan667d2412009-02-09 14:21:17 +08002887 * >=0 if data has been transferred, returns the offset of consumed data.
2888 * <0 if no data has been transferred.
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002889 */
2890int ring_buffer_read_page(struct ring_buffer *buffer,
Steven Rostedtef7a4a12009-03-03 00:27:49 -05002891 void **data_page, size_t len, int cpu, int full)
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002892{
2893 struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
2894 struct ring_buffer_event *event;
Steven Rostedt044fa782008-12-02 23:50:03 -05002895 struct buffer_data_page *bpage;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05002896 struct buffer_page *reader;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002897 unsigned long flags;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05002898 unsigned int commit;
Lai Jiangshan667d2412009-02-09 14:21:17 +08002899 unsigned int read;
Steven Rostedt4f3640f2009-03-03 23:52:42 -05002900 u64 save_timestamp;
Lai Jiangshan667d2412009-02-09 14:21:17 +08002901 int ret = -1;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002902
Steven Rostedt554f7862009-03-11 22:00:13 -04002903 if (!cpumask_test_cpu(cpu, buffer->cpumask))
2904 goto out;
2905
Steven Rostedt474d32b2009-03-03 19:51:40 -05002906 /*
2907 * If len is not big enough to hold the page header, then
2908 * we can not copy anything.
2909 */
2910 if (len <= BUF_PAGE_HDR_SIZE)
Steven Rostedt554f7862009-03-11 22:00:13 -04002911 goto out;
Steven Rostedt474d32b2009-03-03 19:51:40 -05002912
2913 len -= BUF_PAGE_HDR_SIZE;
2914
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002915 if (!data_page)
Steven Rostedt554f7862009-03-11 22:00:13 -04002916 goto out;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002917
Steven Rostedt044fa782008-12-02 23:50:03 -05002918 bpage = *data_page;
2919 if (!bpage)
Steven Rostedt554f7862009-03-11 22:00:13 -04002920 goto out;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002921
2922 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
2923
Steven Rostedtef7a4a12009-03-03 00:27:49 -05002924 reader = rb_get_reader_page(cpu_buffer);
2925 if (!reader)
Steven Rostedt554f7862009-03-11 22:00:13 -04002926 goto out_unlock;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002927
Steven Rostedtef7a4a12009-03-03 00:27:49 -05002928 event = rb_reader_event(cpu_buffer);
Lai Jiangshan667d2412009-02-09 14:21:17 +08002929
Steven Rostedtef7a4a12009-03-03 00:27:49 -05002930 read = reader->read;
2931 commit = rb_page_commit(reader);
2932
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002933 /*
Steven Rostedt474d32b2009-03-03 19:51:40 -05002934 * If this page has been partially read or
2935 * if len is not big enough to read the rest of the page or
2936 * a writer is still on the page, then
2937 * we must copy the data from the page to the buffer.
2938 * Otherwise, we can simply swap the page with the one passed in.
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002939 */
Steven Rostedt474d32b2009-03-03 19:51:40 -05002940 if (read || (len < (commit - read)) ||
Steven Rostedtef7a4a12009-03-03 00:27:49 -05002941 cpu_buffer->reader_page == cpu_buffer->commit_page) {
Lai Jiangshan667d2412009-02-09 14:21:17 +08002942 struct buffer_data_page *rpage = cpu_buffer->reader_page->page;
Steven Rostedt474d32b2009-03-03 19:51:40 -05002943 unsigned int rpos = read;
2944 unsigned int pos = 0;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05002945 unsigned int size;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002946
2947 if (full)
Steven Rostedt554f7862009-03-11 22:00:13 -04002948 goto out_unlock;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002949
Steven Rostedtef7a4a12009-03-03 00:27:49 -05002950 if (len > (commit - read))
2951 len = (commit - read);
2952
2953 size = rb_event_length(event);
2954
2955 if (len < size)
Steven Rostedt554f7862009-03-11 22:00:13 -04002956 goto out_unlock;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05002957
Steven Rostedt4f3640f2009-03-03 23:52:42 -05002958 /* save the current timestamp, since the user will need it */
2959 save_timestamp = cpu_buffer->read_stamp;
2960
Steven Rostedtef7a4a12009-03-03 00:27:49 -05002961 /* Need to copy one event at a time */
2962 do {
Steven Rostedt474d32b2009-03-03 19:51:40 -05002963 memcpy(bpage->data + pos, rpage->data + rpos, size);
Steven Rostedtef7a4a12009-03-03 00:27:49 -05002964
2965 len -= size;
2966
2967 rb_advance_reader(cpu_buffer);
Steven Rostedt474d32b2009-03-03 19:51:40 -05002968 rpos = reader->read;
2969 pos += size;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05002970
2971 event = rb_reader_event(cpu_buffer);
2972 size = rb_event_length(event);
2973 } while (len > size);
Lai Jiangshan667d2412009-02-09 14:21:17 +08002974
2975 /* update bpage */
Steven Rostedtef7a4a12009-03-03 00:27:49 -05002976 local_set(&bpage->commit, pos);
Steven Rostedt4f3640f2009-03-03 23:52:42 -05002977 bpage->time_stamp = save_timestamp;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05002978
Steven Rostedt474d32b2009-03-03 19:51:40 -05002979 /* we copied everything to the beginning */
2980 read = 0;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002981 } else {
Steven Rostedtafbab762009-05-01 19:40:05 -04002982 /* update the entry counter */
2983 cpu_buffer->read += local_read(&reader->entries);
2984
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002985 /* swap the pages */
Steven Rostedt044fa782008-12-02 23:50:03 -05002986 rb_init_page(bpage);
Steven Rostedtef7a4a12009-03-03 00:27:49 -05002987 bpage = reader->page;
2988 reader->page = *data_page;
2989 local_set(&reader->write, 0);
Steven Rostedt778c55d2009-05-01 18:44:45 -04002990 local_set(&reader->entries, 0);
Steven Rostedtef7a4a12009-03-03 00:27:49 -05002991 reader->read = 0;
Steven Rostedt044fa782008-12-02 23:50:03 -05002992 *data_page = bpage;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002993 }
Lai Jiangshan667d2412009-02-09 14:21:17 +08002994 ret = read;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002995
Steven Rostedt554f7862009-03-11 22:00:13 -04002996 out_unlock:
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002997 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
2998
Steven Rostedt554f7862009-03-11 22:00:13 -04002999 out:
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003000 return ret;
3001}
Steven Rostedtd6ce96d2009-05-05 01:15:24 -04003002EXPORT_SYMBOL_GPL(ring_buffer_read_page);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003003
Steven Rostedta3583242008-11-11 15:01:42 -05003004static ssize_t
3005rb_simple_read(struct file *filp, char __user *ubuf,
3006 size_t cnt, loff_t *ppos)
3007{
Hannes Eder5e398412009-02-10 19:44:34 +01003008 unsigned long *p = filp->private_data;
Steven Rostedta3583242008-11-11 15:01:42 -05003009 char buf[64];
3010 int r;
3011
Steven Rostedt033601a2008-11-21 12:41:55 -05003012 if (test_bit(RB_BUFFERS_DISABLED_BIT, p))
3013 r = sprintf(buf, "permanently disabled\n");
3014 else
3015 r = sprintf(buf, "%d\n", test_bit(RB_BUFFERS_ON_BIT, p));
Steven Rostedta3583242008-11-11 15:01:42 -05003016
3017 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
3018}
3019
3020static ssize_t
3021rb_simple_write(struct file *filp, const char __user *ubuf,
3022 size_t cnt, loff_t *ppos)
3023{
Hannes Eder5e398412009-02-10 19:44:34 +01003024 unsigned long *p = filp->private_data;
Steven Rostedta3583242008-11-11 15:01:42 -05003025 char buf[64];
Hannes Eder5e398412009-02-10 19:44:34 +01003026 unsigned long val;
Steven Rostedta3583242008-11-11 15:01:42 -05003027 int ret;
3028
3029 if (cnt >= sizeof(buf))
3030 return -EINVAL;
3031
3032 if (copy_from_user(&buf, ubuf, cnt))
3033 return -EFAULT;
3034
3035 buf[cnt] = 0;
3036
3037 ret = strict_strtoul(buf, 10, &val);
3038 if (ret < 0)
3039 return ret;
3040
Steven Rostedt033601a2008-11-21 12:41:55 -05003041 if (val)
3042 set_bit(RB_BUFFERS_ON_BIT, p);
3043 else
3044 clear_bit(RB_BUFFERS_ON_BIT, p);
Steven Rostedta3583242008-11-11 15:01:42 -05003045
3046 (*ppos)++;
3047
3048 return cnt;
3049}
3050
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003051static const struct file_operations rb_simple_fops = {
Steven Rostedta3583242008-11-11 15:01:42 -05003052 .open = tracing_open_generic,
3053 .read = rb_simple_read,
3054 .write = rb_simple_write,
3055};
3056
3057
3058static __init int rb_init_debugfs(void)
3059{
3060 struct dentry *d_tracer;
Steven Rostedta3583242008-11-11 15:01:42 -05003061
3062 d_tracer = tracing_init_dentry();
3063
Frederic Weisbecker5452af62009-03-27 00:25:38 +01003064 trace_create_file("tracing_on", 0644, d_tracer,
3065 &ring_buffer_flags, &rb_simple_fops);
Steven Rostedta3583242008-11-11 15:01:42 -05003066
3067 return 0;
3068}
3069
3070fs_initcall(rb_init_debugfs);
Steven Rostedt554f7862009-03-11 22:00:13 -04003071
Steven Rostedt59222ef2009-03-12 11:46:03 -04003072#ifdef CONFIG_HOTPLUG_CPU
Frederic Weisbecker09c9e842009-03-21 04:33:36 +01003073static int rb_cpu_notify(struct notifier_block *self,
3074 unsigned long action, void *hcpu)
Steven Rostedt554f7862009-03-11 22:00:13 -04003075{
3076 struct ring_buffer *buffer =
3077 container_of(self, struct ring_buffer, cpu_notify);
3078 long cpu = (long)hcpu;
3079
3080 switch (action) {
3081 case CPU_UP_PREPARE:
3082 case CPU_UP_PREPARE_FROZEN:
3083 if (cpu_isset(cpu, *buffer->cpumask))
3084 return NOTIFY_OK;
3085
3086 buffer->buffers[cpu] =
3087 rb_allocate_cpu_buffer(buffer, cpu);
3088 if (!buffer->buffers[cpu]) {
3089 WARN(1, "failed to allocate ring buffer on CPU %ld\n",
3090 cpu);
3091 return NOTIFY_OK;
3092 }
3093 smp_wmb();
3094 cpu_set(cpu, *buffer->cpumask);
3095 break;
3096 case CPU_DOWN_PREPARE:
3097 case CPU_DOWN_PREPARE_FROZEN:
3098 /*
3099 * Do nothing.
3100 * If we were to free the buffer, then the user would
3101 * lose any trace that was in the buffer.
3102 */
3103 break;
3104 default:
3105 break;
3106 }
3107 return NOTIFY_OK;
3108}
3109#endif