blob: 50926601a28dd3da0239746865d30643f76d0649 [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 Rostedtedd813b2009-06-02 23:00:53 -04001338static inline int
1339rb_try_to_discard(struct ring_buffer_per_cpu *cpu_buffer,
1340 struct ring_buffer_event *event)
1341{
1342 unsigned long new_index, old_index;
1343 struct buffer_page *bpage;
1344 unsigned long index;
1345 unsigned long addr;
1346
1347 new_index = rb_event_index(event);
1348 old_index = new_index + rb_event_length(event);
1349 addr = (unsigned long)event;
1350 addr &= PAGE_MASK;
1351
1352 bpage = cpu_buffer->tail_page;
1353
1354 if (bpage->page == (void *)addr && rb_page_write(bpage) == old_index) {
1355 /*
1356 * This is on the tail page. It is possible that
1357 * a write could come in and move the tail page
1358 * and write to the next page. That is fine
1359 * because we just shorten what is on this page.
1360 */
1361 index = local_cmpxchg(&bpage->write, old_index, new_index);
1362 if (index == old_index)
1363 return 1;
1364 }
1365
1366 /* could not discard */
1367 return 0;
1368}
1369
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001370static int
1371rb_add_time_stamp(struct ring_buffer_per_cpu *cpu_buffer,
1372 u64 *ts, u64 *delta)
1373{
1374 struct ring_buffer_event *event;
1375 static int once;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001376 int ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001377
1378 if (unlikely(*delta > (1ULL << 59) && !once++)) {
1379 printk(KERN_WARNING "Delta way too big! %llu"
1380 " ts=%llu write stamp = %llu\n",
Stephen Rothwelle2862c92008-10-27 17:43:28 +11001381 (unsigned long long)*delta,
1382 (unsigned long long)*ts,
1383 (unsigned long long)cpu_buffer->write_stamp);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001384 WARN_ON(1);
1385 }
1386
1387 /*
1388 * The delta is too big, we to add a
1389 * new timestamp.
1390 */
1391 event = __rb_reserve_next(cpu_buffer,
1392 RINGBUF_TYPE_TIME_EXTEND,
1393 RB_LEN_TIME_EXTEND,
1394 ts);
1395 if (!event)
Steven Rostedtbf41a152008-10-04 02:00:59 -04001396 return -EBUSY;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001397
Steven Rostedtbf41a152008-10-04 02:00:59 -04001398 if (PTR_ERR(event) == -EAGAIN)
1399 return -EAGAIN;
1400
1401 /* Only a commited time event can update the write stamp */
1402 if (rb_is_commit(cpu_buffer, event)) {
1403 /*
1404 * If this is the first on the page, then we need to
1405 * update the page itself, and just put in a zero.
1406 */
1407 if (rb_event_index(event)) {
1408 event->time_delta = *delta & TS_MASK;
1409 event->array[0] = *delta >> TS_SHIFT;
1410 } else {
Steven Rostedtabc9b562008-12-02 15:34:06 -05001411 cpu_buffer->commit_page->page->time_stamp = *ts;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001412 event->time_delta = 0;
1413 event->array[0] = 0;
1414 }
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001415 cpu_buffer->write_stamp = *ts;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001416 /* let the caller know this was the commit */
1417 ret = 1;
1418 } else {
Steven Rostedtedd813b2009-06-02 23:00:53 -04001419 /* Try to discard the event */
1420 if (!rb_try_to_discard(cpu_buffer, event)) {
1421 /* Darn, this is just wasted space */
1422 event->time_delta = 0;
1423 event->array[0] = 0;
1424 ret = 0;
1425 }
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001426 }
1427
Steven Rostedtbf41a152008-10-04 02:00:59 -04001428 *delta = 0;
1429
1430 return ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001431}
1432
1433static struct ring_buffer_event *
1434rb_reserve_next_event(struct ring_buffer_per_cpu *cpu_buffer,
Steven Rostedt1cd8d732009-05-11 14:08:09 -04001435 unsigned long length)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001436{
1437 struct ring_buffer_event *event;
Steven Rostedt168b6b12009-05-11 22:11:05 -04001438 u64 ts, delta = 0;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001439 int commit = 0;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04001440 int nr_loops = 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001441
Steven Rostedtbe957c42009-05-11 14:42:53 -04001442 length = rb_calculate_event_length(length);
Steven Rostedtbf41a152008-10-04 02:00:59 -04001443 again:
Steven Rostedt818e3dd2008-10-31 09:58:35 -04001444 /*
1445 * We allow for interrupts to reenter here and do a trace.
1446 * If one does, it will cause this original code to loop
1447 * back here. Even with heavy interrupts happening, this
1448 * should only happen a few times in a row. If this happens
1449 * 1000 times in a row, there must be either an interrupt
1450 * storm or we have something buggy.
1451 * Bail!
1452 */
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05001453 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 1000))
Steven Rostedt818e3dd2008-10-31 09:58:35 -04001454 return NULL;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04001455
Steven Rostedt88eb0122009-05-11 16:28:23 -04001456 ts = rb_time_stamp(cpu_buffer->buffer, cpu_buffer->cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001457
Steven Rostedtbf41a152008-10-04 02:00:59 -04001458 /*
1459 * Only the first commit can update the timestamp.
1460 * Yes there is a race here. If an interrupt comes in
1461 * just after the conditional and it traces too, then it
1462 * will also check the deltas. More than one timestamp may
1463 * also be made. But only the entry that did the actual
1464 * commit will be something other than zero.
1465 */
Steven Rostedt0f0c85f2009-05-11 16:08:00 -04001466 if (likely(cpu_buffer->tail_page == cpu_buffer->commit_page &&
1467 rb_page_write(cpu_buffer->tail_page) ==
1468 rb_commit_index(cpu_buffer))) {
Steven Rostedt168b6b12009-05-11 22:11:05 -04001469 u64 diff;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001470
Steven Rostedt168b6b12009-05-11 22:11:05 -04001471 diff = ts - cpu_buffer->write_stamp;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001472
Steven Rostedt168b6b12009-05-11 22:11:05 -04001473 /* make sure this diff is calculated here */
Steven Rostedtbf41a152008-10-04 02:00:59 -04001474 barrier();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001475
Steven Rostedtbf41a152008-10-04 02:00:59 -04001476 /* Did the write stamp get updated already? */
1477 if (unlikely(ts < cpu_buffer->write_stamp))
Steven Rostedt168b6b12009-05-11 22:11:05 -04001478 goto get_event;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001479
Steven Rostedt168b6b12009-05-11 22:11:05 -04001480 delta = diff;
1481 if (unlikely(test_time_stamp(delta))) {
Steven Rostedtbf41a152008-10-04 02:00:59 -04001482
1483 commit = rb_add_time_stamp(cpu_buffer, &ts, &delta);
Steven Rostedtbf41a152008-10-04 02:00:59 -04001484 if (commit == -EBUSY)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001485 return NULL;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001486
1487 if (commit == -EAGAIN)
1488 goto again;
1489
1490 RB_WARN_ON(cpu_buffer, commit < 0);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001491 }
Steven Rostedt168b6b12009-05-11 22:11:05 -04001492 }
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001493
Steven Rostedt168b6b12009-05-11 22:11:05 -04001494 get_event:
Steven Rostedt1cd8d732009-05-11 14:08:09 -04001495 event = __rb_reserve_next(cpu_buffer, 0, length, &ts);
Steven Rostedt168b6b12009-05-11 22:11:05 -04001496 if (unlikely(PTR_ERR(event) == -EAGAIN))
Steven Rostedtbf41a152008-10-04 02:00:59 -04001497 goto again;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001498
Steven Rostedtbf41a152008-10-04 02:00:59 -04001499 if (!event) {
1500 if (unlikely(commit))
1501 /*
1502 * Ouch! We needed a timestamp and it was commited. But
1503 * we didn't get our event reserved.
1504 */
1505 rb_set_commit_to_write(cpu_buffer);
1506 return NULL;
1507 }
1508
1509 /*
1510 * If the timestamp was commited, make the commit our entry
1511 * now so that we will update it when needed.
1512 */
Steven Rostedt0f0c85f2009-05-11 16:08:00 -04001513 if (unlikely(commit))
Steven Rostedtbf41a152008-10-04 02:00:59 -04001514 rb_set_commit_event(cpu_buffer, event);
1515 else if (!rb_is_commit(cpu_buffer, event))
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001516 delta = 0;
1517
1518 event->time_delta = delta;
1519
1520 return event;
1521}
1522
Steven Rostedtaa18efb2009-04-20 16:16:11 -04001523#define TRACE_RECURSIVE_DEPTH 16
Steven Rostedt261842b2009-04-16 21:41:52 -04001524
1525static int trace_recursive_lock(void)
1526{
Steven Rostedtaa18efb2009-04-20 16:16:11 -04001527 current->trace_recursion++;
Steven Rostedt261842b2009-04-16 21:41:52 -04001528
Steven Rostedtaa18efb2009-04-20 16:16:11 -04001529 if (likely(current->trace_recursion < TRACE_RECURSIVE_DEPTH))
1530 return 0;
Steven Rostedt261842b2009-04-16 21:41:52 -04001531
Steven Rostedtaa18efb2009-04-20 16:16:11 -04001532 /* Disable all tracing before we do anything else */
1533 tracing_off_permanent();
Frederic Weisbeckere057a5e2009-04-19 23:38:12 +02001534
Steven Rostedt7d7d2b82009-04-27 12:37:49 -04001535 printk_once(KERN_WARNING "Tracing recursion: depth[%ld]:"
Steven Rostedtaa18efb2009-04-20 16:16:11 -04001536 "HC[%lu]:SC[%lu]:NMI[%lu]\n",
1537 current->trace_recursion,
1538 hardirq_count() >> HARDIRQ_SHIFT,
1539 softirq_count() >> SOFTIRQ_SHIFT,
1540 in_nmi());
Frederic Weisbeckere057a5e2009-04-19 23:38:12 +02001541
Steven Rostedtaa18efb2009-04-20 16:16:11 -04001542 WARN_ON_ONCE(1);
1543 return -1;
Steven Rostedt261842b2009-04-16 21:41:52 -04001544}
1545
1546static void trace_recursive_unlock(void)
1547{
Steven Rostedtaa18efb2009-04-20 16:16:11 -04001548 WARN_ON_ONCE(!current->trace_recursion);
Steven Rostedt261842b2009-04-16 21:41:52 -04001549
Steven Rostedtaa18efb2009-04-20 16:16:11 -04001550 current->trace_recursion--;
Steven Rostedt261842b2009-04-16 21:41:52 -04001551}
1552
Steven Rostedtbf41a152008-10-04 02:00:59 -04001553static DEFINE_PER_CPU(int, rb_need_resched);
1554
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001555/**
1556 * ring_buffer_lock_reserve - reserve a part of the buffer
1557 * @buffer: the ring buffer to reserve from
1558 * @length: the length of the data to reserve (excluding event header)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001559 *
1560 * Returns a reseverd event on the ring buffer to copy directly to.
1561 * The user of this interface will need to get the body to write into
1562 * and can use the ring_buffer_event_data() interface.
1563 *
1564 * The length is the length of the data needed, not the event length
1565 * which also includes the event header.
1566 *
1567 * Must be paired with ring_buffer_unlock_commit, unless NULL is returned.
1568 * If NULL is returned, then nothing has been allocated or locked.
1569 */
1570struct ring_buffer_event *
Arnaldo Carvalho de Melo0a987752009-02-05 16:12:56 -02001571ring_buffer_lock_reserve(struct ring_buffer *buffer, unsigned long length)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001572{
1573 struct ring_buffer_per_cpu *cpu_buffer;
1574 struct ring_buffer_event *event;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001575 int cpu, resched;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001576
Steven Rostedt033601a2008-11-21 12:41:55 -05001577 if (ring_buffer_flags != RB_BUFFERS_ON)
Steven Rostedta3583242008-11-11 15:01:42 -05001578 return NULL;
1579
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001580 if (atomic_read(&buffer->record_disabled))
1581 return NULL;
1582
Steven Rostedtbf41a152008-10-04 02:00:59 -04001583 /* If we are tracing schedule, we don't want to recurse */
Steven Rostedt182e9f52008-11-03 23:15:56 -05001584 resched = ftrace_preempt_disable();
Steven Rostedtbf41a152008-10-04 02:00:59 -04001585
Steven Rostedt261842b2009-04-16 21:41:52 -04001586 if (trace_recursive_lock())
1587 goto out_nocheck;
1588
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001589 cpu = raw_smp_processor_id();
1590
Rusty Russell9e01c1b2009-01-01 10:12:22 +10301591 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedtd7690412008-10-01 00:29:53 -04001592 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001593
1594 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001595
1596 if (atomic_read(&cpu_buffer->record_disabled))
Steven Rostedtd7690412008-10-01 00:29:53 -04001597 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001598
Steven Rostedtbe957c42009-05-11 14:42:53 -04001599 if (length > BUF_MAX_DATA_SIZE)
Steven Rostedtbf41a152008-10-04 02:00:59 -04001600 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001601
Steven Rostedt1cd8d732009-05-11 14:08:09 -04001602 event = rb_reserve_next_event(cpu_buffer, length);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001603 if (!event)
Steven Rostedtd7690412008-10-01 00:29:53 -04001604 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001605
Steven Rostedtbf41a152008-10-04 02:00:59 -04001606 /*
1607 * Need to store resched state on this cpu.
1608 * Only the first needs to.
1609 */
1610
1611 if (preempt_count() == 1)
1612 per_cpu(rb_need_resched, cpu) = resched;
1613
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001614 return event;
1615
Steven Rostedtd7690412008-10-01 00:29:53 -04001616 out:
Steven Rostedt261842b2009-04-16 21:41:52 -04001617 trace_recursive_unlock();
1618
1619 out_nocheck:
Steven Rostedt182e9f52008-11-03 23:15:56 -05001620 ftrace_preempt_enable(resched);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001621 return NULL;
1622}
Robert Richterc4f50182008-12-11 16:49:22 +01001623EXPORT_SYMBOL_GPL(ring_buffer_lock_reserve);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001624
1625static void rb_commit(struct ring_buffer_per_cpu *cpu_buffer,
1626 struct ring_buffer_event *event)
1627{
Steven Rostedte4906ef2009-04-30 20:49:44 -04001628 local_inc(&cpu_buffer->entries);
Steven Rostedtbf41a152008-10-04 02:00:59 -04001629
1630 /* Only process further if we own the commit */
1631 if (!rb_is_commit(cpu_buffer, event))
1632 return;
1633
1634 cpu_buffer->write_stamp += event->time_delta;
1635
1636 rb_set_commit_to_write(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001637}
1638
1639/**
1640 * ring_buffer_unlock_commit - commit a reserved
1641 * @buffer: The buffer to commit to
1642 * @event: The event pointer to commit.
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001643 *
1644 * This commits the data to the ring buffer, and releases any locks held.
1645 *
1646 * Must be paired with ring_buffer_lock_reserve.
1647 */
1648int ring_buffer_unlock_commit(struct ring_buffer *buffer,
Arnaldo Carvalho de Melo0a987752009-02-05 16:12:56 -02001649 struct ring_buffer_event *event)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001650{
1651 struct ring_buffer_per_cpu *cpu_buffer;
1652 int cpu = raw_smp_processor_id();
1653
1654 cpu_buffer = buffer->buffers[cpu];
1655
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001656 rb_commit(cpu_buffer, event);
1657
Steven Rostedt261842b2009-04-16 21:41:52 -04001658 trace_recursive_unlock();
1659
Steven Rostedtbf41a152008-10-04 02:00:59 -04001660 /*
1661 * Only the last preempt count needs to restore preemption.
1662 */
Steven Rostedt182e9f52008-11-03 23:15:56 -05001663 if (preempt_count() == 1)
1664 ftrace_preempt_enable(per_cpu(rb_need_resched, cpu));
1665 else
Steven Rostedtbf41a152008-10-04 02:00:59 -04001666 preempt_enable_no_resched_notrace();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001667
1668 return 0;
1669}
Robert Richterc4f50182008-12-11 16:49:22 +01001670EXPORT_SYMBOL_GPL(ring_buffer_unlock_commit);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001671
Frederic Weisbeckerf3b9aae2009-04-19 23:39:33 +02001672static inline void rb_event_discard(struct ring_buffer_event *event)
1673{
Lai Jiangshan334d4162009-04-24 11:27:05 +08001674 /* array[0] holds the actual length for the discarded event */
1675 event->array[0] = rb_event_data_length(event) - RB_EVNT_HDR_SIZE;
1676 event->type_len = RINGBUF_TYPE_PADDING;
Frederic Weisbeckerf3b9aae2009-04-19 23:39:33 +02001677 /* time delta must be non zero */
1678 if (!event->time_delta)
1679 event->time_delta = 1;
1680}
1681
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001682/**
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04001683 * ring_buffer_event_discard - discard any event in the ring buffer
1684 * @event: the event to discard
1685 *
1686 * Sometimes a event that is in the ring buffer needs to be ignored.
1687 * This function lets the user discard an event in the ring buffer
1688 * and then that event will not be read later.
1689 *
1690 * Note, it is up to the user to be careful with this, and protect
1691 * against races. If the user discards an event that has been consumed
1692 * it is possible that it could corrupt the ring buffer.
1693 */
1694void ring_buffer_event_discard(struct ring_buffer_event *event)
1695{
Frederic Weisbeckerf3b9aae2009-04-19 23:39:33 +02001696 rb_event_discard(event);
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04001697}
1698EXPORT_SYMBOL_GPL(ring_buffer_event_discard);
1699
1700/**
1701 * ring_buffer_commit_discard - discard an event that has not been committed
1702 * @buffer: the ring buffer
1703 * @event: non committed event to discard
1704 *
1705 * This is similar to ring_buffer_event_discard but must only be
1706 * performed on an event that has not been committed yet. The difference
1707 * is that this will also try to free the event from the ring buffer
1708 * if another event has not been added behind it.
1709 *
1710 * If another event has been added behind it, it will set the event
1711 * up as discarded, and perform the commit.
1712 *
1713 * If this function is called, do not call ring_buffer_unlock_commit on
1714 * the event.
1715 */
1716void ring_buffer_discard_commit(struct ring_buffer *buffer,
1717 struct ring_buffer_event *event)
1718{
1719 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04001720 int cpu;
1721
1722 /* The event is discarded regardless */
Frederic Weisbeckerf3b9aae2009-04-19 23:39:33 +02001723 rb_event_discard(event);
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04001724
1725 /*
1726 * This must only be called if the event has not been
1727 * committed yet. Thus we can assume that preemption
1728 * is still disabled.
1729 */
Steven Rostedt74f4fd22009-05-07 19:58:55 -04001730 RB_WARN_ON(buffer, preemptible());
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04001731
1732 cpu = smp_processor_id();
1733 cpu_buffer = buffer->buffers[cpu];
1734
Steven Rostedtedd813b2009-06-02 23:00:53 -04001735 if (!rb_try_to_discard(cpu_buffer, event))
1736 goto out;
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04001737
1738 /*
1739 * The commit is still visible by the reader, so we
1740 * must increment entries.
1741 */
Steven Rostedte4906ef2009-04-30 20:49:44 -04001742 local_inc(&cpu_buffer->entries);
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04001743 out:
1744 /*
1745 * If a write came in and pushed the tail page
1746 * we still need to update the commit pointer
1747 * if we were the commit.
1748 */
1749 if (rb_is_commit(cpu_buffer, event))
1750 rb_set_commit_to_write(cpu_buffer);
1751
Frederic Weisbeckerf3b9aae2009-04-19 23:39:33 +02001752 trace_recursive_unlock();
1753
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04001754 /*
1755 * Only the last preempt count needs to restore preemption.
1756 */
1757 if (preempt_count() == 1)
1758 ftrace_preempt_enable(per_cpu(rb_need_resched, cpu));
1759 else
1760 preempt_enable_no_resched_notrace();
1761
1762}
1763EXPORT_SYMBOL_GPL(ring_buffer_discard_commit);
1764
1765/**
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001766 * ring_buffer_write - write data to the buffer without reserving
1767 * @buffer: The ring buffer to write to.
1768 * @length: The length of the data being written (excluding the event header)
1769 * @data: The data to write to the buffer.
1770 *
1771 * This is like ring_buffer_lock_reserve and ring_buffer_unlock_commit as
1772 * one function. If you already have the data to write to the buffer, it
1773 * may be easier to simply call this function.
1774 *
1775 * Note, like ring_buffer_lock_reserve, the length is the length of the data
1776 * and not the length of the event which would hold the header.
1777 */
1778int ring_buffer_write(struct ring_buffer *buffer,
1779 unsigned long length,
1780 void *data)
1781{
1782 struct ring_buffer_per_cpu *cpu_buffer;
1783 struct ring_buffer_event *event;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001784 void *body;
1785 int ret = -EBUSY;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001786 int cpu, resched;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001787
Steven Rostedt033601a2008-11-21 12:41:55 -05001788 if (ring_buffer_flags != RB_BUFFERS_ON)
Steven Rostedta3583242008-11-11 15:01:42 -05001789 return -EBUSY;
1790
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001791 if (atomic_read(&buffer->record_disabled))
1792 return -EBUSY;
1793
Steven Rostedt182e9f52008-11-03 23:15:56 -05001794 resched = ftrace_preempt_disable();
Steven Rostedtbf41a152008-10-04 02:00:59 -04001795
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001796 cpu = raw_smp_processor_id();
1797
Rusty Russell9e01c1b2009-01-01 10:12:22 +10301798 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedtd7690412008-10-01 00:29:53 -04001799 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001800
1801 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001802
1803 if (atomic_read(&cpu_buffer->record_disabled))
1804 goto out;
1805
Steven Rostedtbe957c42009-05-11 14:42:53 -04001806 if (length > BUF_MAX_DATA_SIZE)
1807 goto out;
1808
1809 event = rb_reserve_next_event(cpu_buffer, length);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001810 if (!event)
1811 goto out;
1812
1813 body = rb_event_data(event);
1814
1815 memcpy(body, data, length);
1816
1817 rb_commit(cpu_buffer, event);
1818
1819 ret = 0;
1820 out:
Steven Rostedt182e9f52008-11-03 23:15:56 -05001821 ftrace_preempt_enable(resched);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001822
1823 return ret;
1824}
Robert Richterc4f50182008-12-11 16:49:22 +01001825EXPORT_SYMBOL_GPL(ring_buffer_write);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001826
Andrew Morton34a148b2009-01-09 12:27:09 -08001827static int rb_per_cpu_empty(struct ring_buffer_per_cpu *cpu_buffer)
Steven Rostedtbf41a152008-10-04 02:00:59 -04001828{
1829 struct buffer_page *reader = cpu_buffer->reader_page;
1830 struct buffer_page *head = cpu_buffer->head_page;
1831 struct buffer_page *commit = cpu_buffer->commit_page;
1832
1833 return reader->read == rb_page_commit(reader) &&
1834 (commit == reader ||
1835 (commit == head &&
1836 head->read == rb_page_commit(commit)));
1837}
1838
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001839/**
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001840 * ring_buffer_record_disable - stop all writes into the buffer
1841 * @buffer: The ring buffer to stop writes to.
1842 *
1843 * This prevents all writes to the buffer. Any attempt to write
1844 * to the buffer after this will fail and return NULL.
1845 *
1846 * The caller should call synchronize_sched() after this.
1847 */
1848void ring_buffer_record_disable(struct ring_buffer *buffer)
1849{
1850 atomic_inc(&buffer->record_disabled);
1851}
Robert Richterc4f50182008-12-11 16:49:22 +01001852EXPORT_SYMBOL_GPL(ring_buffer_record_disable);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001853
1854/**
1855 * ring_buffer_record_enable - enable writes to the buffer
1856 * @buffer: The ring buffer to enable writes
1857 *
1858 * Note, multiple disables will need the same number of enables
1859 * to truely enable the writing (much like preempt_disable).
1860 */
1861void ring_buffer_record_enable(struct ring_buffer *buffer)
1862{
1863 atomic_dec(&buffer->record_disabled);
1864}
Robert Richterc4f50182008-12-11 16:49:22 +01001865EXPORT_SYMBOL_GPL(ring_buffer_record_enable);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001866
1867/**
1868 * ring_buffer_record_disable_cpu - stop all writes into the cpu_buffer
1869 * @buffer: The ring buffer to stop writes to.
1870 * @cpu: The CPU buffer to stop
1871 *
1872 * This prevents all writes to the buffer. Any attempt to write
1873 * to the buffer after this will fail and return NULL.
1874 *
1875 * The caller should call synchronize_sched() after this.
1876 */
1877void ring_buffer_record_disable_cpu(struct ring_buffer *buffer, int cpu)
1878{
1879 struct ring_buffer_per_cpu *cpu_buffer;
1880
Rusty Russell9e01c1b2009-01-01 10:12:22 +10301881 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04001882 return;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001883
1884 cpu_buffer = buffer->buffers[cpu];
1885 atomic_inc(&cpu_buffer->record_disabled);
1886}
Robert Richterc4f50182008-12-11 16:49:22 +01001887EXPORT_SYMBOL_GPL(ring_buffer_record_disable_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001888
1889/**
1890 * ring_buffer_record_enable_cpu - enable writes to the buffer
1891 * @buffer: The ring buffer to enable writes
1892 * @cpu: The CPU to enable.
1893 *
1894 * Note, multiple disables will need the same number of enables
1895 * to truely enable the writing (much like preempt_disable).
1896 */
1897void ring_buffer_record_enable_cpu(struct ring_buffer *buffer, int cpu)
1898{
1899 struct ring_buffer_per_cpu *cpu_buffer;
1900
Rusty Russell9e01c1b2009-01-01 10:12:22 +10301901 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04001902 return;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001903
1904 cpu_buffer = buffer->buffers[cpu];
1905 atomic_dec(&cpu_buffer->record_disabled);
1906}
Robert Richterc4f50182008-12-11 16:49:22 +01001907EXPORT_SYMBOL_GPL(ring_buffer_record_enable_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001908
1909/**
1910 * ring_buffer_entries_cpu - get the number of entries in a cpu buffer
1911 * @buffer: The ring buffer
1912 * @cpu: The per CPU buffer to get the entries from.
1913 */
1914unsigned long ring_buffer_entries_cpu(struct ring_buffer *buffer, int cpu)
1915{
1916 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedt8aabee52009-03-12 13:13:49 -04001917 unsigned long ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001918
Rusty Russell9e01c1b2009-01-01 10:12:22 +10301919 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04001920 return 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001921
1922 cpu_buffer = buffer->buffers[cpu];
Steven Rostedte4906ef2009-04-30 20:49:44 -04001923 ret = (local_read(&cpu_buffer->entries) - cpu_buffer->overrun)
1924 - cpu_buffer->read;
Steven Rostedt554f7862009-03-11 22:00:13 -04001925
1926 return ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001927}
Robert Richterc4f50182008-12-11 16:49:22 +01001928EXPORT_SYMBOL_GPL(ring_buffer_entries_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001929
1930/**
1931 * ring_buffer_overrun_cpu - get the number of overruns in a cpu_buffer
1932 * @buffer: The ring buffer
1933 * @cpu: The per CPU buffer to get the number of overruns from
1934 */
1935unsigned long ring_buffer_overrun_cpu(struct ring_buffer *buffer, int cpu)
1936{
1937 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedt8aabee52009-03-12 13:13:49 -04001938 unsigned long ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001939
Rusty Russell9e01c1b2009-01-01 10:12:22 +10301940 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04001941 return 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001942
1943 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt554f7862009-03-11 22:00:13 -04001944 ret = cpu_buffer->overrun;
Steven Rostedt554f7862009-03-11 22:00:13 -04001945
1946 return ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001947}
Robert Richterc4f50182008-12-11 16:49:22 +01001948EXPORT_SYMBOL_GPL(ring_buffer_overrun_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001949
1950/**
Steven Rostedtf0d2c682009-04-29 13:43:37 -04001951 * ring_buffer_nmi_dropped_cpu - get the number of nmis that were dropped
1952 * @buffer: The ring buffer
1953 * @cpu: The per CPU buffer to get the number of overruns from
1954 */
1955unsigned long ring_buffer_nmi_dropped_cpu(struct ring_buffer *buffer, int cpu)
1956{
1957 struct ring_buffer_per_cpu *cpu_buffer;
1958 unsigned long ret;
1959
1960 if (!cpumask_test_cpu(cpu, buffer->cpumask))
1961 return 0;
1962
1963 cpu_buffer = buffer->buffers[cpu];
1964 ret = cpu_buffer->nmi_dropped;
1965
1966 return ret;
1967}
1968EXPORT_SYMBOL_GPL(ring_buffer_nmi_dropped_cpu);
1969
1970/**
1971 * ring_buffer_commit_overrun_cpu - get the number of overruns caused by commits
1972 * @buffer: The ring buffer
1973 * @cpu: The per CPU buffer to get the number of overruns from
1974 */
1975unsigned long
1976ring_buffer_commit_overrun_cpu(struct ring_buffer *buffer, int cpu)
1977{
1978 struct ring_buffer_per_cpu *cpu_buffer;
1979 unsigned long ret;
1980
1981 if (!cpumask_test_cpu(cpu, buffer->cpumask))
1982 return 0;
1983
1984 cpu_buffer = buffer->buffers[cpu];
1985 ret = cpu_buffer->commit_overrun;
1986
1987 return ret;
1988}
1989EXPORT_SYMBOL_GPL(ring_buffer_commit_overrun_cpu);
1990
1991/**
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001992 * ring_buffer_entries - get the number of entries in a buffer
1993 * @buffer: The ring buffer
1994 *
1995 * Returns the total number of entries in the ring buffer
1996 * (all CPU entries)
1997 */
1998unsigned long ring_buffer_entries(struct ring_buffer *buffer)
1999{
2000 struct ring_buffer_per_cpu *cpu_buffer;
2001 unsigned long entries = 0;
2002 int cpu;
2003
2004 /* if you care about this being correct, lock the buffer */
2005 for_each_buffer_cpu(buffer, cpu) {
2006 cpu_buffer = buffer->buffers[cpu];
Steven Rostedte4906ef2009-04-30 20:49:44 -04002007 entries += (local_read(&cpu_buffer->entries) -
2008 cpu_buffer->overrun) - cpu_buffer->read;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002009 }
2010
2011 return entries;
2012}
Robert Richterc4f50182008-12-11 16:49:22 +01002013EXPORT_SYMBOL_GPL(ring_buffer_entries);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002014
2015/**
2016 * ring_buffer_overrun_cpu - get the number of overruns in buffer
2017 * @buffer: The ring buffer
2018 *
2019 * Returns the total number of overruns in the ring buffer
2020 * (all CPU entries)
2021 */
2022unsigned long ring_buffer_overruns(struct ring_buffer *buffer)
2023{
2024 struct ring_buffer_per_cpu *cpu_buffer;
2025 unsigned long overruns = 0;
2026 int cpu;
2027
2028 /* if you care about this being correct, lock the buffer */
2029 for_each_buffer_cpu(buffer, cpu) {
2030 cpu_buffer = buffer->buffers[cpu];
2031 overruns += cpu_buffer->overrun;
2032 }
2033
2034 return overruns;
2035}
Robert Richterc4f50182008-12-11 16:49:22 +01002036EXPORT_SYMBOL_GPL(ring_buffer_overruns);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002037
Steven Rostedt642edba2008-11-12 00:01:26 -05002038static void rb_iter_reset(struct ring_buffer_iter *iter)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002039{
2040 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
2041
Steven Rostedtd7690412008-10-01 00:29:53 -04002042 /* Iterator usage is expected to have record disabled */
2043 if (list_empty(&cpu_buffer->reader_page->list)) {
2044 iter->head_page = cpu_buffer->head_page;
Steven Rostedt6f807ac2008-10-04 02:00:58 -04002045 iter->head = cpu_buffer->head_page->read;
Steven Rostedtd7690412008-10-01 00:29:53 -04002046 } else {
2047 iter->head_page = cpu_buffer->reader_page;
Steven Rostedt6f807ac2008-10-04 02:00:58 -04002048 iter->head = cpu_buffer->reader_page->read;
Steven Rostedtd7690412008-10-01 00:29:53 -04002049 }
2050 if (iter->head)
2051 iter->read_stamp = cpu_buffer->read_stamp;
2052 else
Steven Rostedtabc9b562008-12-02 15:34:06 -05002053 iter->read_stamp = iter->head_page->page->time_stamp;
Steven Rostedt642edba2008-11-12 00:01:26 -05002054}
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002055
Steven Rostedt642edba2008-11-12 00:01:26 -05002056/**
2057 * ring_buffer_iter_reset - reset an iterator
2058 * @iter: The iterator to reset
2059 *
2060 * Resets the iterator, so that it will start from the beginning
2061 * again.
2062 */
2063void ring_buffer_iter_reset(struct ring_buffer_iter *iter)
2064{
Steven Rostedt554f7862009-03-11 22:00:13 -04002065 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedt642edba2008-11-12 00:01:26 -05002066 unsigned long flags;
2067
Steven Rostedt554f7862009-03-11 22:00:13 -04002068 if (!iter)
2069 return;
2070
2071 cpu_buffer = iter->cpu_buffer;
2072
Steven Rostedt642edba2008-11-12 00:01:26 -05002073 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
2074 rb_iter_reset(iter);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002075 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002076}
Robert Richterc4f50182008-12-11 16:49:22 +01002077EXPORT_SYMBOL_GPL(ring_buffer_iter_reset);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002078
2079/**
2080 * ring_buffer_iter_empty - check if an iterator has no more to read
2081 * @iter: The iterator to check
2082 */
2083int ring_buffer_iter_empty(struct ring_buffer_iter *iter)
2084{
2085 struct ring_buffer_per_cpu *cpu_buffer;
2086
2087 cpu_buffer = iter->cpu_buffer;
2088
Steven Rostedtbf41a152008-10-04 02:00:59 -04002089 return iter->head_page == cpu_buffer->commit_page &&
2090 iter->head == rb_commit_index(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002091}
Robert Richterc4f50182008-12-11 16:49:22 +01002092EXPORT_SYMBOL_GPL(ring_buffer_iter_empty);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002093
2094static void
2095rb_update_read_stamp(struct ring_buffer_per_cpu *cpu_buffer,
2096 struct ring_buffer_event *event)
2097{
2098 u64 delta;
2099
Lai Jiangshan334d4162009-04-24 11:27:05 +08002100 switch (event->type_len) {
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002101 case RINGBUF_TYPE_PADDING:
2102 return;
2103
2104 case RINGBUF_TYPE_TIME_EXTEND:
2105 delta = event->array[0];
2106 delta <<= TS_SHIFT;
2107 delta += event->time_delta;
2108 cpu_buffer->read_stamp += delta;
2109 return;
2110
2111 case RINGBUF_TYPE_TIME_STAMP:
2112 /* FIXME: not implemented */
2113 return;
2114
2115 case RINGBUF_TYPE_DATA:
2116 cpu_buffer->read_stamp += event->time_delta;
2117 return;
2118
2119 default:
2120 BUG();
2121 }
2122 return;
2123}
2124
2125static void
2126rb_update_iter_read_stamp(struct ring_buffer_iter *iter,
2127 struct ring_buffer_event *event)
2128{
2129 u64 delta;
2130
Lai Jiangshan334d4162009-04-24 11:27:05 +08002131 switch (event->type_len) {
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002132 case RINGBUF_TYPE_PADDING:
2133 return;
2134
2135 case RINGBUF_TYPE_TIME_EXTEND:
2136 delta = event->array[0];
2137 delta <<= TS_SHIFT;
2138 delta += event->time_delta;
2139 iter->read_stamp += delta;
2140 return;
2141
2142 case RINGBUF_TYPE_TIME_STAMP:
2143 /* FIXME: not implemented */
2144 return;
2145
2146 case RINGBUF_TYPE_DATA:
2147 iter->read_stamp += event->time_delta;
2148 return;
2149
2150 default:
2151 BUG();
2152 }
2153 return;
2154}
2155
Steven Rostedtd7690412008-10-01 00:29:53 -04002156static struct buffer_page *
2157rb_get_reader_page(struct ring_buffer_per_cpu *cpu_buffer)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002158{
Steven Rostedtd7690412008-10-01 00:29:53 -04002159 struct buffer_page *reader = NULL;
2160 unsigned long flags;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002161 int nr_loops = 0;
Steven Rostedtd7690412008-10-01 00:29:53 -04002162
Steven Rostedt3e03fb72008-11-06 00:09:43 -05002163 local_irq_save(flags);
2164 __raw_spin_lock(&cpu_buffer->lock);
Steven Rostedtd7690412008-10-01 00:29:53 -04002165
2166 again:
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002167 /*
2168 * This should normally only loop twice. But because the
2169 * start of the reader inserts an empty page, it causes
2170 * a case where we will loop three times. There should be no
2171 * reason to loop four times (that I know of).
2172 */
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05002173 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 3)) {
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002174 reader = NULL;
2175 goto out;
2176 }
2177
Steven Rostedtd7690412008-10-01 00:29:53 -04002178 reader = cpu_buffer->reader_page;
2179
2180 /* If there's more to read, return this page */
Steven Rostedtbf41a152008-10-04 02:00:59 -04002181 if (cpu_buffer->reader_page->read < rb_page_size(reader))
Steven Rostedtd7690412008-10-01 00:29:53 -04002182 goto out;
2183
2184 /* Never should we have an index greater than the size */
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05002185 if (RB_WARN_ON(cpu_buffer,
2186 cpu_buffer->reader_page->read > rb_page_size(reader)))
2187 goto out;
Steven Rostedtd7690412008-10-01 00:29:53 -04002188
2189 /* check if we caught up to the tail */
2190 reader = NULL;
Steven Rostedtbf41a152008-10-04 02:00:59 -04002191 if (cpu_buffer->commit_page == cpu_buffer->reader_page)
Steven Rostedtd7690412008-10-01 00:29:53 -04002192 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002193
2194 /*
Steven Rostedtd7690412008-10-01 00:29:53 -04002195 * Splice the empty reader page into the list around the head.
2196 * Reset the reader page to size zero.
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002197 */
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002198
Steven Rostedtd7690412008-10-01 00:29:53 -04002199 reader = cpu_buffer->head_page;
2200 cpu_buffer->reader_page->list.next = reader->list.next;
2201 cpu_buffer->reader_page->list.prev = reader->list.prev;
Steven Rostedtbf41a152008-10-04 02:00:59 -04002202
2203 local_set(&cpu_buffer->reader_page->write, 0);
Steven Rostedt778c55d2009-05-01 18:44:45 -04002204 local_set(&cpu_buffer->reader_page->entries, 0);
Steven Rostedtabc9b562008-12-02 15:34:06 -05002205 local_set(&cpu_buffer->reader_page->page->commit, 0);
Steven Rostedtd7690412008-10-01 00:29:53 -04002206
2207 /* Make the reader page now replace the head */
2208 reader->list.prev->next = &cpu_buffer->reader_page->list;
2209 reader->list.next->prev = &cpu_buffer->reader_page->list;
2210
2211 /*
2212 * If the tail is on the reader, then we must set the head
2213 * to the inserted page, otherwise we set it one before.
2214 */
2215 cpu_buffer->head_page = cpu_buffer->reader_page;
2216
Steven Rostedtbf41a152008-10-04 02:00:59 -04002217 if (cpu_buffer->commit_page != reader)
Steven Rostedtd7690412008-10-01 00:29:53 -04002218 rb_inc_page(cpu_buffer, &cpu_buffer->head_page);
2219
2220 /* Finally update the reader page to the new head */
2221 cpu_buffer->reader_page = reader;
2222 rb_reset_reader_page(cpu_buffer);
2223
2224 goto again;
2225
2226 out:
Steven Rostedt3e03fb72008-11-06 00:09:43 -05002227 __raw_spin_unlock(&cpu_buffer->lock);
2228 local_irq_restore(flags);
Steven Rostedtd7690412008-10-01 00:29:53 -04002229
2230 return reader;
2231}
2232
2233static void rb_advance_reader(struct ring_buffer_per_cpu *cpu_buffer)
2234{
2235 struct ring_buffer_event *event;
2236 struct buffer_page *reader;
2237 unsigned length;
2238
2239 reader = rb_get_reader_page(cpu_buffer);
2240
2241 /* This function should not be called when buffer is empty */
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05002242 if (RB_WARN_ON(cpu_buffer, !reader))
2243 return;
Steven Rostedtd7690412008-10-01 00:29:53 -04002244
2245 event = rb_reader_event(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002246
Lai Jiangshan334d4162009-04-24 11:27:05 +08002247 if (event->type_len <= RINGBUF_TYPE_DATA_TYPE_LEN_MAX
2248 || rb_discarded_event(event))
Steven Rostedte4906ef2009-04-30 20:49:44 -04002249 cpu_buffer->read++;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002250
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002251 rb_update_read_stamp(cpu_buffer, event);
2252
Steven Rostedtd7690412008-10-01 00:29:53 -04002253 length = rb_event_length(event);
Steven Rostedt6f807ac2008-10-04 02:00:58 -04002254 cpu_buffer->reader_page->read += length;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002255}
2256
2257static void rb_advance_iter(struct ring_buffer_iter *iter)
2258{
2259 struct ring_buffer *buffer;
2260 struct ring_buffer_per_cpu *cpu_buffer;
2261 struct ring_buffer_event *event;
2262 unsigned length;
2263
2264 cpu_buffer = iter->cpu_buffer;
2265 buffer = cpu_buffer->buffer;
2266
2267 /*
2268 * Check if we are at the end of the buffer.
2269 */
Steven Rostedtbf41a152008-10-04 02:00:59 -04002270 if (iter->head >= rb_page_size(iter->head_page)) {
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05002271 if (RB_WARN_ON(buffer,
2272 iter->head_page == cpu_buffer->commit_page))
2273 return;
Steven Rostedtd7690412008-10-01 00:29:53 -04002274 rb_inc_iter(iter);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002275 return;
2276 }
2277
2278 event = rb_iter_head_event(iter);
2279
2280 length = rb_event_length(event);
2281
2282 /*
2283 * This should not be called to advance the header if we are
2284 * at the tail of the buffer.
2285 */
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05002286 if (RB_WARN_ON(cpu_buffer,
Steven Rostedtf536aaf2008-11-10 23:07:30 -05002287 (iter->head_page == cpu_buffer->commit_page) &&
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05002288 (iter->head + length > rb_commit_index(cpu_buffer))))
2289 return;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002290
2291 rb_update_iter_read_stamp(iter, event);
2292
2293 iter->head += length;
2294
2295 /* check for end of page padding */
Steven Rostedtbf41a152008-10-04 02:00:59 -04002296 if ((iter->head >= rb_page_size(iter->head_page)) &&
2297 (iter->head_page != cpu_buffer->commit_page))
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002298 rb_advance_iter(iter);
2299}
2300
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002301static struct ring_buffer_event *
2302rb_buffer_peek(struct ring_buffer *buffer, int cpu, u64 *ts)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002303{
2304 struct ring_buffer_per_cpu *cpu_buffer;
2305 struct ring_buffer_event *event;
Steven Rostedtd7690412008-10-01 00:29:53 -04002306 struct buffer_page *reader;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002307 int nr_loops = 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002308
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002309 cpu_buffer = buffer->buffers[cpu];
2310
2311 again:
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002312 /*
2313 * We repeat when a timestamp is encountered. It is possible
2314 * to get multiple timestamps from an interrupt entering just
2315 * as one timestamp is about to be written. The max times
2316 * that this can happen is the number of nested interrupts we
2317 * can have. Nesting 10 deep of interrupts is clearly
2318 * an anomaly.
2319 */
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05002320 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 10))
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002321 return NULL;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002322
Steven Rostedtd7690412008-10-01 00:29:53 -04002323 reader = rb_get_reader_page(cpu_buffer);
2324 if (!reader)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002325 return NULL;
2326
Steven Rostedtd7690412008-10-01 00:29:53 -04002327 event = rb_reader_event(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002328
Lai Jiangshan334d4162009-04-24 11:27:05 +08002329 switch (event->type_len) {
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002330 case RINGBUF_TYPE_PADDING:
Tom Zanussi2d622712009-03-22 03:30:49 -05002331 if (rb_null_event(event))
2332 RB_WARN_ON(cpu_buffer, 1);
2333 /*
2334 * Because the writer could be discarding every
2335 * event it creates (which would probably be bad)
2336 * if we were to go back to "again" then we may never
2337 * catch up, and will trigger the warn on, or lock
2338 * the box. Return the padding, and we will release
2339 * the current locks, and try again.
2340 */
Steven Rostedtd7690412008-10-01 00:29:53 -04002341 rb_advance_reader(cpu_buffer);
Tom Zanussi2d622712009-03-22 03:30:49 -05002342 return event;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002343
2344 case RINGBUF_TYPE_TIME_EXTEND:
2345 /* Internal data, OK to advance */
Steven Rostedtd7690412008-10-01 00:29:53 -04002346 rb_advance_reader(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002347 goto again;
2348
2349 case RINGBUF_TYPE_TIME_STAMP:
2350 /* FIXME: not implemented */
Steven Rostedtd7690412008-10-01 00:29:53 -04002351 rb_advance_reader(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002352 goto again;
2353
2354 case RINGBUF_TYPE_DATA:
2355 if (ts) {
2356 *ts = cpu_buffer->read_stamp + event->time_delta;
Steven Rostedt37886f62009-03-17 17:22:06 -04002357 ring_buffer_normalize_time_stamp(buffer,
2358 cpu_buffer->cpu, ts);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002359 }
2360 return event;
2361
2362 default:
2363 BUG();
2364 }
2365
2366 return NULL;
2367}
Robert Richterc4f50182008-12-11 16:49:22 +01002368EXPORT_SYMBOL_GPL(ring_buffer_peek);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002369
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002370static struct ring_buffer_event *
2371rb_iter_peek(struct ring_buffer_iter *iter, u64 *ts)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002372{
2373 struct ring_buffer *buffer;
2374 struct ring_buffer_per_cpu *cpu_buffer;
2375 struct ring_buffer_event *event;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002376 int nr_loops = 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002377
2378 if (ring_buffer_iter_empty(iter))
2379 return NULL;
2380
2381 cpu_buffer = iter->cpu_buffer;
2382 buffer = cpu_buffer->buffer;
2383
2384 again:
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002385 /*
2386 * We repeat when a timestamp is encountered. It is possible
2387 * to get multiple timestamps from an interrupt entering just
2388 * as one timestamp is about to be written. The max times
2389 * that this can happen is the number of nested interrupts we
2390 * can have. Nesting 10 deep of interrupts is clearly
2391 * an anomaly.
2392 */
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05002393 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 10))
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002394 return NULL;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002395
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002396 if (rb_per_cpu_empty(cpu_buffer))
2397 return NULL;
2398
2399 event = rb_iter_head_event(iter);
2400
Lai Jiangshan334d4162009-04-24 11:27:05 +08002401 switch (event->type_len) {
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002402 case RINGBUF_TYPE_PADDING:
Tom Zanussi2d622712009-03-22 03:30:49 -05002403 if (rb_null_event(event)) {
2404 rb_inc_iter(iter);
2405 goto again;
2406 }
2407 rb_advance_iter(iter);
2408 return event;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002409
2410 case RINGBUF_TYPE_TIME_EXTEND:
2411 /* Internal data, OK to advance */
2412 rb_advance_iter(iter);
2413 goto again;
2414
2415 case RINGBUF_TYPE_TIME_STAMP:
2416 /* FIXME: not implemented */
2417 rb_advance_iter(iter);
2418 goto again;
2419
2420 case RINGBUF_TYPE_DATA:
2421 if (ts) {
2422 *ts = iter->read_stamp + event->time_delta;
Steven Rostedt37886f62009-03-17 17:22:06 -04002423 ring_buffer_normalize_time_stamp(buffer,
2424 cpu_buffer->cpu, ts);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002425 }
2426 return event;
2427
2428 default:
2429 BUG();
2430 }
2431
2432 return NULL;
2433}
Robert Richterc4f50182008-12-11 16:49:22 +01002434EXPORT_SYMBOL_GPL(ring_buffer_iter_peek);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002435
2436/**
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002437 * ring_buffer_peek - peek at the next event to be read
2438 * @buffer: The ring buffer to read
2439 * @cpu: The cpu to peak at
2440 * @ts: The timestamp counter of this event.
2441 *
2442 * This will return the event that will be read next, but does
2443 * not consume the data.
2444 */
2445struct ring_buffer_event *
2446ring_buffer_peek(struct ring_buffer *buffer, int cpu, u64 *ts)
2447{
2448 struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
Steven Rostedt8aabee52009-03-12 13:13:49 -04002449 struct ring_buffer_event *event;
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002450 unsigned long flags;
2451
Steven Rostedt554f7862009-03-11 22:00:13 -04002452 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04002453 return NULL;
Steven Rostedt554f7862009-03-11 22:00:13 -04002454
Tom Zanussi2d622712009-03-22 03:30:49 -05002455 again:
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002456 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
2457 event = rb_buffer_peek(buffer, cpu, ts);
2458 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
2459
Lai Jiangshan334d4162009-04-24 11:27:05 +08002460 if (event && event->type_len == RINGBUF_TYPE_PADDING) {
Tom Zanussi2d622712009-03-22 03:30:49 -05002461 cpu_relax();
2462 goto again;
2463 }
2464
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002465 return event;
2466}
2467
2468/**
2469 * ring_buffer_iter_peek - peek at the next event to be read
2470 * @iter: The ring buffer iterator
2471 * @ts: The timestamp counter of this event.
2472 *
2473 * This will return the event that will be read next, but does
2474 * not increment the iterator.
2475 */
2476struct ring_buffer_event *
2477ring_buffer_iter_peek(struct ring_buffer_iter *iter, u64 *ts)
2478{
2479 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
2480 struct ring_buffer_event *event;
2481 unsigned long flags;
2482
Tom Zanussi2d622712009-03-22 03:30:49 -05002483 again:
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002484 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
2485 event = rb_iter_peek(iter, ts);
2486 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
2487
Lai Jiangshan334d4162009-04-24 11:27:05 +08002488 if (event && event->type_len == RINGBUF_TYPE_PADDING) {
Tom Zanussi2d622712009-03-22 03:30:49 -05002489 cpu_relax();
2490 goto again;
2491 }
2492
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002493 return event;
2494}
2495
2496/**
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002497 * ring_buffer_consume - return an event and consume it
2498 * @buffer: The ring buffer to get the next event from
2499 *
2500 * Returns the next event in the ring buffer, and that event is consumed.
2501 * Meaning, that sequential reads will keep returning a different event,
2502 * and eventually empty the ring buffer if the producer is slower.
2503 */
2504struct ring_buffer_event *
2505ring_buffer_consume(struct ring_buffer *buffer, int cpu, u64 *ts)
2506{
Steven Rostedt554f7862009-03-11 22:00:13 -04002507 struct ring_buffer_per_cpu *cpu_buffer;
2508 struct ring_buffer_event *event = NULL;
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002509 unsigned long flags;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002510
Tom Zanussi2d622712009-03-22 03:30:49 -05002511 again:
Steven Rostedt554f7862009-03-11 22:00:13 -04002512 /* might be called in atomic */
2513 preempt_disable();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002514
Steven Rostedt554f7862009-03-11 22:00:13 -04002515 if (!cpumask_test_cpu(cpu, buffer->cpumask))
2516 goto out;
2517
2518 cpu_buffer = buffer->buffers[cpu];
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002519 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002520
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002521 event = rb_buffer_peek(buffer, cpu, ts);
2522 if (!event)
Steven Rostedt554f7862009-03-11 22:00:13 -04002523 goto out_unlock;
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002524
Steven Rostedtd7690412008-10-01 00:29:53 -04002525 rb_advance_reader(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002526
Steven Rostedt554f7862009-03-11 22:00:13 -04002527 out_unlock:
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002528 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
2529
Steven Rostedt554f7862009-03-11 22:00:13 -04002530 out:
2531 preempt_enable();
2532
Lai Jiangshan334d4162009-04-24 11:27:05 +08002533 if (event && event->type_len == RINGBUF_TYPE_PADDING) {
Tom Zanussi2d622712009-03-22 03:30:49 -05002534 cpu_relax();
2535 goto again;
2536 }
2537
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002538 return event;
2539}
Robert Richterc4f50182008-12-11 16:49:22 +01002540EXPORT_SYMBOL_GPL(ring_buffer_consume);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002541
2542/**
2543 * ring_buffer_read_start - start a non consuming read of the buffer
2544 * @buffer: The ring buffer to read from
2545 * @cpu: The cpu buffer to iterate over
2546 *
2547 * This starts up an iteration through the buffer. It also disables
2548 * the recording to the buffer until the reading is finished.
2549 * This prevents the reading from being corrupted. This is not
2550 * a consuming read, so a producer is not expected.
2551 *
2552 * Must be paired with ring_buffer_finish.
2553 */
2554struct ring_buffer_iter *
2555ring_buffer_read_start(struct ring_buffer *buffer, int cpu)
2556{
2557 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedt8aabee52009-03-12 13:13:49 -04002558 struct ring_buffer_iter *iter;
Steven Rostedtd7690412008-10-01 00:29:53 -04002559 unsigned long flags;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002560
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302561 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04002562 return NULL;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002563
2564 iter = kmalloc(sizeof(*iter), GFP_KERNEL);
2565 if (!iter)
Steven Rostedt8aabee52009-03-12 13:13:49 -04002566 return NULL;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002567
2568 cpu_buffer = buffer->buffers[cpu];
2569
2570 iter->cpu_buffer = cpu_buffer;
2571
2572 atomic_inc(&cpu_buffer->record_disabled);
2573 synchronize_sched();
2574
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002575 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
Steven Rostedt3e03fb72008-11-06 00:09:43 -05002576 __raw_spin_lock(&cpu_buffer->lock);
Steven Rostedt642edba2008-11-12 00:01:26 -05002577 rb_iter_reset(iter);
Steven Rostedt3e03fb72008-11-06 00:09:43 -05002578 __raw_spin_unlock(&cpu_buffer->lock);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002579 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002580
2581 return iter;
2582}
Robert Richterc4f50182008-12-11 16:49:22 +01002583EXPORT_SYMBOL_GPL(ring_buffer_read_start);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002584
2585/**
2586 * ring_buffer_finish - finish reading the iterator of the buffer
2587 * @iter: The iterator retrieved by ring_buffer_start
2588 *
2589 * This re-enables the recording to the buffer, and frees the
2590 * iterator.
2591 */
2592void
2593ring_buffer_read_finish(struct ring_buffer_iter *iter)
2594{
2595 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
2596
2597 atomic_dec(&cpu_buffer->record_disabled);
2598 kfree(iter);
2599}
Robert Richterc4f50182008-12-11 16:49:22 +01002600EXPORT_SYMBOL_GPL(ring_buffer_read_finish);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002601
2602/**
2603 * ring_buffer_read - read the next item in the ring buffer by the iterator
2604 * @iter: The ring buffer iterator
2605 * @ts: The time stamp of the event read.
2606 *
2607 * This reads the next event in the ring buffer and increments the iterator.
2608 */
2609struct ring_buffer_event *
2610ring_buffer_read(struct ring_buffer_iter *iter, u64 *ts)
2611{
2612 struct ring_buffer_event *event;
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002613 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
2614 unsigned long flags;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002615
Tom Zanussi2d622712009-03-22 03:30:49 -05002616 again:
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002617 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
2618 event = rb_iter_peek(iter, ts);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002619 if (!event)
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002620 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002621
2622 rb_advance_iter(iter);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002623 out:
2624 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002625
Lai Jiangshan334d4162009-04-24 11:27:05 +08002626 if (event && event->type_len == RINGBUF_TYPE_PADDING) {
Tom Zanussi2d622712009-03-22 03:30:49 -05002627 cpu_relax();
2628 goto again;
2629 }
2630
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002631 return event;
2632}
Robert Richterc4f50182008-12-11 16:49:22 +01002633EXPORT_SYMBOL_GPL(ring_buffer_read);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002634
2635/**
2636 * ring_buffer_size - return the size of the ring buffer (in bytes)
2637 * @buffer: The ring buffer.
2638 */
2639unsigned long ring_buffer_size(struct ring_buffer *buffer)
2640{
2641 return BUF_PAGE_SIZE * buffer->pages;
2642}
Robert Richterc4f50182008-12-11 16:49:22 +01002643EXPORT_SYMBOL_GPL(ring_buffer_size);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002644
2645static void
2646rb_reset_cpu(struct ring_buffer_per_cpu *cpu_buffer)
2647{
2648 cpu_buffer->head_page
2649 = list_entry(cpu_buffer->pages.next, struct buffer_page, list);
Steven Rostedtbf41a152008-10-04 02:00:59 -04002650 local_set(&cpu_buffer->head_page->write, 0);
Steven Rostedt778c55d2009-05-01 18:44:45 -04002651 local_set(&cpu_buffer->head_page->entries, 0);
Steven Rostedtabc9b562008-12-02 15:34:06 -05002652 local_set(&cpu_buffer->head_page->page->commit, 0);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002653
Steven Rostedt6f807ac2008-10-04 02:00:58 -04002654 cpu_buffer->head_page->read = 0;
Steven Rostedtbf41a152008-10-04 02:00:59 -04002655
2656 cpu_buffer->tail_page = cpu_buffer->head_page;
2657 cpu_buffer->commit_page = cpu_buffer->head_page;
2658
2659 INIT_LIST_HEAD(&cpu_buffer->reader_page->list);
2660 local_set(&cpu_buffer->reader_page->write, 0);
Steven Rostedt778c55d2009-05-01 18:44:45 -04002661 local_set(&cpu_buffer->reader_page->entries, 0);
Steven Rostedtabc9b562008-12-02 15:34:06 -05002662 local_set(&cpu_buffer->reader_page->page->commit, 0);
Steven Rostedt6f807ac2008-10-04 02:00:58 -04002663 cpu_buffer->reader_page->read = 0;
Steven Rostedtd7690412008-10-01 00:29:53 -04002664
Steven Rostedtf0d2c682009-04-29 13:43:37 -04002665 cpu_buffer->nmi_dropped = 0;
2666 cpu_buffer->commit_overrun = 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002667 cpu_buffer->overrun = 0;
Steven Rostedte4906ef2009-04-30 20:49:44 -04002668 cpu_buffer->read = 0;
2669 local_set(&cpu_buffer->entries, 0);
Steven Rostedt69507c02009-01-21 18:45:57 -05002670
2671 cpu_buffer->write_stamp = 0;
2672 cpu_buffer->read_stamp = 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002673}
2674
2675/**
2676 * ring_buffer_reset_cpu - reset a ring buffer per CPU buffer
2677 * @buffer: The ring buffer to reset a per cpu buffer of
2678 * @cpu: The CPU buffer to be reset
2679 */
2680void ring_buffer_reset_cpu(struct ring_buffer *buffer, int cpu)
2681{
2682 struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
2683 unsigned long flags;
2684
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302685 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04002686 return;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002687
Steven Rostedt41ede232009-05-01 20:26:54 -04002688 atomic_inc(&cpu_buffer->record_disabled);
2689
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002690 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
2691
Steven Rostedt3e03fb72008-11-06 00:09:43 -05002692 __raw_spin_lock(&cpu_buffer->lock);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002693
2694 rb_reset_cpu(cpu_buffer);
2695
Steven Rostedt3e03fb72008-11-06 00:09:43 -05002696 __raw_spin_unlock(&cpu_buffer->lock);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002697
2698 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
Steven Rostedt41ede232009-05-01 20:26:54 -04002699
2700 atomic_dec(&cpu_buffer->record_disabled);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002701}
Robert Richterc4f50182008-12-11 16:49:22 +01002702EXPORT_SYMBOL_GPL(ring_buffer_reset_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002703
2704/**
2705 * ring_buffer_reset - reset a ring buffer
2706 * @buffer: The ring buffer to reset all cpu buffers
2707 */
2708void ring_buffer_reset(struct ring_buffer *buffer)
2709{
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002710 int cpu;
2711
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002712 for_each_buffer_cpu(buffer, cpu)
Steven Rostedtd7690412008-10-01 00:29:53 -04002713 ring_buffer_reset_cpu(buffer, cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002714}
Robert Richterc4f50182008-12-11 16:49:22 +01002715EXPORT_SYMBOL_GPL(ring_buffer_reset);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002716
2717/**
2718 * rind_buffer_empty - is the ring buffer empty?
2719 * @buffer: The ring buffer to test
2720 */
2721int ring_buffer_empty(struct ring_buffer *buffer)
2722{
2723 struct ring_buffer_per_cpu *cpu_buffer;
2724 int cpu;
2725
2726 /* yes this is racy, but if you don't like the race, lock the buffer */
2727 for_each_buffer_cpu(buffer, cpu) {
2728 cpu_buffer = buffer->buffers[cpu];
2729 if (!rb_per_cpu_empty(cpu_buffer))
2730 return 0;
2731 }
Steven Rostedt554f7862009-03-11 22:00:13 -04002732
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002733 return 1;
2734}
Robert Richterc4f50182008-12-11 16:49:22 +01002735EXPORT_SYMBOL_GPL(ring_buffer_empty);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002736
2737/**
2738 * ring_buffer_empty_cpu - is a cpu buffer of a ring buffer empty?
2739 * @buffer: The ring buffer
2740 * @cpu: The CPU buffer to test
2741 */
2742int ring_buffer_empty_cpu(struct ring_buffer *buffer, int cpu)
2743{
2744 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedt8aabee52009-03-12 13:13:49 -04002745 int ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002746
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302747 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04002748 return 1;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002749
2750 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt554f7862009-03-11 22:00:13 -04002751 ret = rb_per_cpu_empty(cpu_buffer);
2752
Steven Rostedt554f7862009-03-11 22:00:13 -04002753
2754 return ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002755}
Robert Richterc4f50182008-12-11 16:49:22 +01002756EXPORT_SYMBOL_GPL(ring_buffer_empty_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002757
2758/**
2759 * ring_buffer_swap_cpu - swap a CPU buffer between two ring buffers
2760 * @buffer_a: One buffer to swap with
2761 * @buffer_b: The other buffer to swap with
2762 *
2763 * This function is useful for tracers that want to take a "snapshot"
2764 * of a CPU buffer and has another back up buffer lying around.
2765 * it is expected that the tracer handles the cpu buffer not being
2766 * used at the moment.
2767 */
2768int ring_buffer_swap_cpu(struct ring_buffer *buffer_a,
2769 struct ring_buffer *buffer_b, int cpu)
2770{
2771 struct ring_buffer_per_cpu *cpu_buffer_a;
2772 struct ring_buffer_per_cpu *cpu_buffer_b;
Steven Rostedt554f7862009-03-11 22:00:13 -04002773 int ret = -EINVAL;
2774
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302775 if (!cpumask_test_cpu(cpu, buffer_a->cpumask) ||
2776 !cpumask_test_cpu(cpu, buffer_b->cpumask))
Steven Rostedt554f7862009-03-11 22:00:13 -04002777 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002778
2779 /* At least make sure the two buffers are somewhat the same */
Lai Jiangshan6d102bc2008-12-17 17:48:23 +08002780 if (buffer_a->pages != buffer_b->pages)
Steven Rostedt554f7862009-03-11 22:00:13 -04002781 goto out;
2782
2783 ret = -EAGAIN;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002784
Steven Rostedt97b17ef2009-01-21 15:24:56 -05002785 if (ring_buffer_flags != RB_BUFFERS_ON)
Steven Rostedt554f7862009-03-11 22:00:13 -04002786 goto out;
Steven Rostedt97b17ef2009-01-21 15:24:56 -05002787
2788 if (atomic_read(&buffer_a->record_disabled))
Steven Rostedt554f7862009-03-11 22:00:13 -04002789 goto out;
Steven Rostedt97b17ef2009-01-21 15:24:56 -05002790
2791 if (atomic_read(&buffer_b->record_disabled))
Steven Rostedt554f7862009-03-11 22:00:13 -04002792 goto out;
Steven Rostedt97b17ef2009-01-21 15:24:56 -05002793
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002794 cpu_buffer_a = buffer_a->buffers[cpu];
2795 cpu_buffer_b = buffer_b->buffers[cpu];
2796
Steven Rostedt97b17ef2009-01-21 15:24:56 -05002797 if (atomic_read(&cpu_buffer_a->record_disabled))
Steven Rostedt554f7862009-03-11 22:00:13 -04002798 goto out;
Steven Rostedt97b17ef2009-01-21 15:24:56 -05002799
2800 if (atomic_read(&cpu_buffer_b->record_disabled))
Steven Rostedt554f7862009-03-11 22:00:13 -04002801 goto out;
Steven Rostedt97b17ef2009-01-21 15:24:56 -05002802
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002803 /*
2804 * We can't do a synchronize_sched here because this
2805 * function can be called in atomic context.
2806 * Normally this will be called from the same CPU as cpu.
2807 * If not it's up to the caller to protect this.
2808 */
2809 atomic_inc(&cpu_buffer_a->record_disabled);
2810 atomic_inc(&cpu_buffer_b->record_disabled);
2811
2812 buffer_a->buffers[cpu] = cpu_buffer_b;
2813 buffer_b->buffers[cpu] = cpu_buffer_a;
2814
2815 cpu_buffer_b->buffer = buffer_a;
2816 cpu_buffer_a->buffer = buffer_b;
2817
2818 atomic_dec(&cpu_buffer_a->record_disabled);
2819 atomic_dec(&cpu_buffer_b->record_disabled);
2820
Steven Rostedt554f7862009-03-11 22:00:13 -04002821 ret = 0;
2822out:
Steven Rostedt554f7862009-03-11 22:00:13 -04002823 return ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002824}
Robert Richterc4f50182008-12-11 16:49:22 +01002825EXPORT_SYMBOL_GPL(ring_buffer_swap_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002826
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002827/**
2828 * ring_buffer_alloc_read_page - allocate a page to read from buffer
2829 * @buffer: the buffer to allocate for.
2830 *
2831 * This function is used in conjunction with ring_buffer_read_page.
2832 * When reading a full page from the ring buffer, these functions
2833 * can be used to speed up the process. The calling function should
2834 * allocate a few pages first with this function. Then when it
2835 * needs to get pages from the ring buffer, it passes the result
2836 * of this function into ring_buffer_read_page, which will swap
2837 * the page that was allocated, with the read page of the buffer.
2838 *
2839 * Returns:
2840 * The page allocated, or NULL on error.
2841 */
2842void *ring_buffer_alloc_read_page(struct ring_buffer *buffer)
2843{
Steven Rostedt044fa782008-12-02 23:50:03 -05002844 struct buffer_data_page *bpage;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05002845 unsigned long addr;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002846
2847 addr = __get_free_page(GFP_KERNEL);
2848 if (!addr)
2849 return NULL;
2850
Steven Rostedt044fa782008-12-02 23:50:03 -05002851 bpage = (void *)addr;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002852
Steven Rostedtef7a4a12009-03-03 00:27:49 -05002853 rb_init_page(bpage);
2854
Steven Rostedt044fa782008-12-02 23:50:03 -05002855 return bpage;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002856}
Steven Rostedtd6ce96d2009-05-05 01:15:24 -04002857EXPORT_SYMBOL_GPL(ring_buffer_alloc_read_page);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002858
2859/**
2860 * ring_buffer_free_read_page - free an allocated read page
2861 * @buffer: the buffer the page was allocate for
2862 * @data: the page to free
2863 *
2864 * Free a page allocated from ring_buffer_alloc_read_page.
2865 */
2866void ring_buffer_free_read_page(struct ring_buffer *buffer, void *data)
2867{
2868 free_page((unsigned long)data);
2869}
Steven Rostedtd6ce96d2009-05-05 01:15:24 -04002870EXPORT_SYMBOL_GPL(ring_buffer_free_read_page);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002871
2872/**
2873 * ring_buffer_read_page - extract a page from the ring buffer
2874 * @buffer: buffer to extract from
2875 * @data_page: the page to use allocated from ring_buffer_alloc_read_page
Steven Rostedtef7a4a12009-03-03 00:27:49 -05002876 * @len: amount to extract
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002877 * @cpu: the cpu of the buffer to extract
2878 * @full: should the extraction only happen when the page is full.
2879 *
2880 * This function will pull out a page from the ring buffer and consume it.
2881 * @data_page must be the address of the variable that was returned
2882 * from ring_buffer_alloc_read_page. This is because the page might be used
2883 * to swap with a page in the ring buffer.
2884 *
2885 * for example:
Lai Jiangshanb85fa012009-02-09 14:21:14 +08002886 * rpage = ring_buffer_alloc_read_page(buffer);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002887 * if (!rpage)
2888 * return error;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05002889 * ret = ring_buffer_read_page(buffer, &rpage, len, cpu, 0);
Lai Jiangshan667d2412009-02-09 14:21:17 +08002890 * if (ret >= 0)
2891 * process_page(rpage, ret);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002892 *
2893 * When @full is set, the function will not return true unless
2894 * the writer is off the reader page.
2895 *
2896 * Note: it is up to the calling functions to handle sleeps and wakeups.
2897 * The ring buffer can be used anywhere in the kernel and can not
2898 * blindly call wake_up. The layer that uses the ring buffer must be
2899 * responsible for that.
2900 *
2901 * Returns:
Lai Jiangshan667d2412009-02-09 14:21:17 +08002902 * >=0 if data has been transferred, returns the offset of consumed data.
2903 * <0 if no data has been transferred.
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002904 */
2905int ring_buffer_read_page(struct ring_buffer *buffer,
Steven Rostedtef7a4a12009-03-03 00:27:49 -05002906 void **data_page, size_t len, int cpu, int full)
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002907{
2908 struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
2909 struct ring_buffer_event *event;
Steven Rostedt044fa782008-12-02 23:50:03 -05002910 struct buffer_data_page *bpage;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05002911 struct buffer_page *reader;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002912 unsigned long flags;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05002913 unsigned int commit;
Lai Jiangshan667d2412009-02-09 14:21:17 +08002914 unsigned int read;
Steven Rostedt4f3640f2009-03-03 23:52:42 -05002915 u64 save_timestamp;
Lai Jiangshan667d2412009-02-09 14:21:17 +08002916 int ret = -1;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002917
Steven Rostedt554f7862009-03-11 22:00:13 -04002918 if (!cpumask_test_cpu(cpu, buffer->cpumask))
2919 goto out;
2920
Steven Rostedt474d32b2009-03-03 19:51:40 -05002921 /*
2922 * If len is not big enough to hold the page header, then
2923 * we can not copy anything.
2924 */
2925 if (len <= BUF_PAGE_HDR_SIZE)
Steven Rostedt554f7862009-03-11 22:00:13 -04002926 goto out;
Steven Rostedt474d32b2009-03-03 19:51:40 -05002927
2928 len -= BUF_PAGE_HDR_SIZE;
2929
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002930 if (!data_page)
Steven Rostedt554f7862009-03-11 22:00:13 -04002931 goto out;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002932
Steven Rostedt044fa782008-12-02 23:50:03 -05002933 bpage = *data_page;
2934 if (!bpage)
Steven Rostedt554f7862009-03-11 22:00:13 -04002935 goto out;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002936
2937 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
2938
Steven Rostedtef7a4a12009-03-03 00:27:49 -05002939 reader = rb_get_reader_page(cpu_buffer);
2940 if (!reader)
Steven Rostedt554f7862009-03-11 22:00:13 -04002941 goto out_unlock;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002942
Steven Rostedtef7a4a12009-03-03 00:27:49 -05002943 event = rb_reader_event(cpu_buffer);
Lai Jiangshan667d2412009-02-09 14:21:17 +08002944
Steven Rostedtef7a4a12009-03-03 00:27:49 -05002945 read = reader->read;
2946 commit = rb_page_commit(reader);
2947
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002948 /*
Steven Rostedt474d32b2009-03-03 19:51:40 -05002949 * If this page has been partially read or
2950 * if len is not big enough to read the rest of the page or
2951 * a writer is still on the page, then
2952 * we must copy the data from the page to the buffer.
2953 * Otherwise, we can simply swap the page with the one passed in.
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002954 */
Steven Rostedt474d32b2009-03-03 19:51:40 -05002955 if (read || (len < (commit - read)) ||
Steven Rostedtef7a4a12009-03-03 00:27:49 -05002956 cpu_buffer->reader_page == cpu_buffer->commit_page) {
Lai Jiangshan667d2412009-02-09 14:21:17 +08002957 struct buffer_data_page *rpage = cpu_buffer->reader_page->page;
Steven Rostedt474d32b2009-03-03 19:51:40 -05002958 unsigned int rpos = read;
2959 unsigned int pos = 0;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05002960 unsigned int size;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002961
2962 if (full)
Steven Rostedt554f7862009-03-11 22:00:13 -04002963 goto out_unlock;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002964
Steven Rostedtef7a4a12009-03-03 00:27:49 -05002965 if (len > (commit - read))
2966 len = (commit - read);
2967
2968 size = rb_event_length(event);
2969
2970 if (len < size)
Steven Rostedt554f7862009-03-11 22:00:13 -04002971 goto out_unlock;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05002972
Steven Rostedt4f3640f2009-03-03 23:52:42 -05002973 /* save the current timestamp, since the user will need it */
2974 save_timestamp = cpu_buffer->read_stamp;
2975
Steven Rostedtef7a4a12009-03-03 00:27:49 -05002976 /* Need to copy one event at a time */
2977 do {
Steven Rostedt474d32b2009-03-03 19:51:40 -05002978 memcpy(bpage->data + pos, rpage->data + rpos, size);
Steven Rostedtef7a4a12009-03-03 00:27:49 -05002979
2980 len -= size;
2981
2982 rb_advance_reader(cpu_buffer);
Steven Rostedt474d32b2009-03-03 19:51:40 -05002983 rpos = reader->read;
2984 pos += size;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05002985
2986 event = rb_reader_event(cpu_buffer);
2987 size = rb_event_length(event);
2988 } while (len > size);
Lai Jiangshan667d2412009-02-09 14:21:17 +08002989
2990 /* update bpage */
Steven Rostedtef7a4a12009-03-03 00:27:49 -05002991 local_set(&bpage->commit, pos);
Steven Rostedt4f3640f2009-03-03 23:52:42 -05002992 bpage->time_stamp = save_timestamp;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05002993
Steven Rostedt474d32b2009-03-03 19:51:40 -05002994 /* we copied everything to the beginning */
2995 read = 0;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002996 } else {
Steven Rostedtafbab762009-05-01 19:40:05 -04002997 /* update the entry counter */
2998 cpu_buffer->read += local_read(&reader->entries);
2999
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003000 /* swap the pages */
Steven Rostedt044fa782008-12-02 23:50:03 -05003001 rb_init_page(bpage);
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003002 bpage = reader->page;
3003 reader->page = *data_page;
3004 local_set(&reader->write, 0);
Steven Rostedt778c55d2009-05-01 18:44:45 -04003005 local_set(&reader->entries, 0);
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003006 reader->read = 0;
Steven Rostedt044fa782008-12-02 23:50:03 -05003007 *data_page = bpage;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003008 }
Lai Jiangshan667d2412009-02-09 14:21:17 +08003009 ret = read;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003010
Steven Rostedt554f7862009-03-11 22:00:13 -04003011 out_unlock:
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003012 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
3013
Steven Rostedt554f7862009-03-11 22:00:13 -04003014 out:
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003015 return ret;
3016}
Steven Rostedtd6ce96d2009-05-05 01:15:24 -04003017EXPORT_SYMBOL_GPL(ring_buffer_read_page);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003018
Steven Rostedta3583242008-11-11 15:01:42 -05003019static ssize_t
3020rb_simple_read(struct file *filp, char __user *ubuf,
3021 size_t cnt, loff_t *ppos)
3022{
Hannes Eder5e398412009-02-10 19:44:34 +01003023 unsigned long *p = filp->private_data;
Steven Rostedta3583242008-11-11 15:01:42 -05003024 char buf[64];
3025 int r;
3026
Steven Rostedt033601a2008-11-21 12:41:55 -05003027 if (test_bit(RB_BUFFERS_DISABLED_BIT, p))
3028 r = sprintf(buf, "permanently disabled\n");
3029 else
3030 r = sprintf(buf, "%d\n", test_bit(RB_BUFFERS_ON_BIT, p));
Steven Rostedta3583242008-11-11 15:01:42 -05003031
3032 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
3033}
3034
3035static ssize_t
3036rb_simple_write(struct file *filp, const char __user *ubuf,
3037 size_t cnt, loff_t *ppos)
3038{
Hannes Eder5e398412009-02-10 19:44:34 +01003039 unsigned long *p = filp->private_data;
Steven Rostedta3583242008-11-11 15:01:42 -05003040 char buf[64];
Hannes Eder5e398412009-02-10 19:44:34 +01003041 unsigned long val;
Steven Rostedta3583242008-11-11 15:01:42 -05003042 int ret;
3043
3044 if (cnt >= sizeof(buf))
3045 return -EINVAL;
3046
3047 if (copy_from_user(&buf, ubuf, cnt))
3048 return -EFAULT;
3049
3050 buf[cnt] = 0;
3051
3052 ret = strict_strtoul(buf, 10, &val);
3053 if (ret < 0)
3054 return ret;
3055
Steven Rostedt033601a2008-11-21 12:41:55 -05003056 if (val)
3057 set_bit(RB_BUFFERS_ON_BIT, p);
3058 else
3059 clear_bit(RB_BUFFERS_ON_BIT, p);
Steven Rostedta3583242008-11-11 15:01:42 -05003060
3061 (*ppos)++;
3062
3063 return cnt;
3064}
3065
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003066static const struct file_operations rb_simple_fops = {
Steven Rostedta3583242008-11-11 15:01:42 -05003067 .open = tracing_open_generic,
3068 .read = rb_simple_read,
3069 .write = rb_simple_write,
3070};
3071
3072
3073static __init int rb_init_debugfs(void)
3074{
3075 struct dentry *d_tracer;
Steven Rostedta3583242008-11-11 15:01:42 -05003076
3077 d_tracer = tracing_init_dentry();
3078
Frederic Weisbecker5452af62009-03-27 00:25:38 +01003079 trace_create_file("tracing_on", 0644, d_tracer,
3080 &ring_buffer_flags, &rb_simple_fops);
Steven Rostedta3583242008-11-11 15:01:42 -05003081
3082 return 0;
3083}
3084
3085fs_initcall(rb_init_debugfs);
Steven Rostedt554f7862009-03-11 22:00:13 -04003086
Steven Rostedt59222ef2009-03-12 11:46:03 -04003087#ifdef CONFIG_HOTPLUG_CPU
Frederic Weisbecker09c9e842009-03-21 04:33:36 +01003088static int rb_cpu_notify(struct notifier_block *self,
3089 unsigned long action, void *hcpu)
Steven Rostedt554f7862009-03-11 22:00:13 -04003090{
3091 struct ring_buffer *buffer =
3092 container_of(self, struct ring_buffer, cpu_notify);
3093 long cpu = (long)hcpu;
3094
3095 switch (action) {
3096 case CPU_UP_PREPARE:
3097 case CPU_UP_PREPARE_FROZEN:
3098 if (cpu_isset(cpu, *buffer->cpumask))
3099 return NOTIFY_OK;
3100
3101 buffer->buffers[cpu] =
3102 rb_allocate_cpu_buffer(buffer, cpu);
3103 if (!buffer->buffers[cpu]) {
3104 WARN(1, "failed to allocate ring buffer on CPU %ld\n",
3105 cpu);
3106 return NOTIFY_OK;
3107 }
3108 smp_wmb();
3109 cpu_set(cpu, *buffer->cpumask);
3110 break;
3111 case CPU_DOWN_PREPARE:
3112 case CPU_DOWN_PREPARE_FROZEN:
3113 /*
3114 * Do nothing.
3115 * If we were to free the buffer, then the user would
3116 * lose any trace that was in the buffer.
3117 */
3118 break;
3119 default:
3120 break;
3121 }
3122 return NOTIFY_OK;
3123}
3124#endif