blob: f452de2ce4900af624ebdf0f113f2b616e0e72a7 [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 Rostedt3e89c7bb2008-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 Rostedt3e89c7bb2008-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
457u64 ring_buffer_time_stamp(struct ring_buffer *buffer, int cpu)
458{
459 u64 time;
460
461 preempt_disable_notrace();
462 /* shift to debug/test normalization and TIME_EXTENTS */
463 time = buffer->clock() << DEBUG_SHIFT;
464 preempt_enable_no_resched_notrace();
465
466 return time;
467}
468EXPORT_SYMBOL_GPL(ring_buffer_time_stamp);
469
470void ring_buffer_normalize_time_stamp(struct ring_buffer *buffer,
471 int cpu, u64 *ts)
472{
473 /* Just stupid testing the normalize function and deltas */
474 *ts >>= DEBUG_SHIFT;
475}
476EXPORT_SYMBOL_GPL(ring_buffer_normalize_time_stamp);
477
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400478/**
479 * check_pages - integrity check of buffer pages
480 * @cpu_buffer: CPU buffer with pages to test
481 *
Wenji Huangc3706f02009-02-10 01:03:18 -0500482 * As a safety measure we check to make sure the data pages have not
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400483 * been corrupted.
484 */
485static int rb_check_pages(struct ring_buffer_per_cpu *cpu_buffer)
486{
487 struct list_head *head = &cpu_buffer->pages;
Steven Rostedt044fa782008-12-02 23:50:03 -0500488 struct buffer_page *bpage, *tmp;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400489
Steven Rostedt3e89c7bb2008-11-11 15:28:41 -0500490 if (RB_WARN_ON(cpu_buffer, head->next->prev != head))
491 return -1;
492 if (RB_WARN_ON(cpu_buffer, head->prev->next != head))
493 return -1;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400494
Steven Rostedt044fa782008-12-02 23:50:03 -0500495 list_for_each_entry_safe(bpage, tmp, head, list) {
Steven Rostedt3e89c7bb2008-11-11 15:28:41 -0500496 if (RB_WARN_ON(cpu_buffer,
Steven Rostedt044fa782008-12-02 23:50:03 -0500497 bpage->list.next->prev != &bpage->list))
Steven Rostedt3e89c7bb2008-11-11 15:28:41 -0500498 return -1;
499 if (RB_WARN_ON(cpu_buffer,
Steven Rostedt044fa782008-12-02 23:50:03 -0500500 bpage->list.prev->next != &bpage->list))
Steven Rostedt3e89c7bb2008-11-11 15:28:41 -0500501 return -1;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400502 }
503
504 return 0;
505}
506
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400507static int rb_allocate_pages(struct ring_buffer_per_cpu *cpu_buffer,
508 unsigned nr_pages)
509{
510 struct list_head *head = &cpu_buffer->pages;
Steven Rostedt044fa782008-12-02 23:50:03 -0500511 struct buffer_page *bpage, *tmp;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400512 unsigned long addr;
513 LIST_HEAD(pages);
514 unsigned i;
515
516 for (i = 0; i < nr_pages; i++) {
Steven Rostedt044fa782008-12-02 23:50:03 -0500517 bpage = kzalloc_node(ALIGN(sizeof(*bpage), cache_line_size()),
Steven Rostedtaa1e0e3b2008-10-02 19:18:09 -0400518 GFP_KERNEL, cpu_to_node(cpu_buffer->cpu));
Steven Rostedt044fa782008-12-02 23:50:03 -0500519 if (!bpage)
Steven Rostedte4c2ce82008-10-01 11:14:54 -0400520 goto free_pages;
Steven Rostedt044fa782008-12-02 23:50:03 -0500521 list_add(&bpage->list, &pages);
Steven Rostedte4c2ce82008-10-01 11:14:54 -0400522
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400523 addr = __get_free_page(GFP_KERNEL);
524 if (!addr)
525 goto free_pages;
Steven Rostedt044fa782008-12-02 23:50:03 -0500526 bpage->page = (void *)addr;
527 rb_init_page(bpage->page);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400528 }
529
530 list_splice(&pages, head);
531
532 rb_check_pages(cpu_buffer);
533
534 return 0;
535
536 free_pages:
Steven Rostedt044fa782008-12-02 23:50:03 -0500537 list_for_each_entry_safe(bpage, tmp, &pages, list) {
538 list_del_init(&bpage->list);
539 free_buffer_page(bpage);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400540 }
541 return -ENOMEM;
542}
543
544static struct ring_buffer_per_cpu *
545rb_allocate_cpu_buffer(struct ring_buffer *buffer, int cpu)
546{
547 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedt044fa782008-12-02 23:50:03 -0500548 struct buffer_page *bpage;
Steven Rostedtd7690412008-10-01 00:29:53 -0400549 unsigned long addr;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400550 int ret;
551
552 cpu_buffer = kzalloc_node(ALIGN(sizeof(*cpu_buffer), cache_line_size()),
553 GFP_KERNEL, cpu_to_node(cpu));
554 if (!cpu_buffer)
555 return NULL;
556
557 cpu_buffer->cpu = cpu;
558 cpu_buffer->buffer = buffer;
Steven Rostedtf83c9d02008-11-11 18:47:44 +0100559 spin_lock_init(&cpu_buffer->reader_lock);
Steven Rostedt3e03fb72008-11-06 00:09:43 -0500560 cpu_buffer->lock = (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400561 INIT_LIST_HEAD(&cpu_buffer->pages);
562
Steven Rostedt044fa782008-12-02 23:50:03 -0500563 bpage = kzalloc_node(ALIGN(sizeof(*bpage), cache_line_size()),
Steven Rostedte4c2ce82008-10-01 11:14:54 -0400564 GFP_KERNEL, cpu_to_node(cpu));
Steven Rostedt044fa782008-12-02 23:50:03 -0500565 if (!bpage)
Steven Rostedte4c2ce82008-10-01 11:14:54 -0400566 goto fail_free_buffer;
567
Steven Rostedt044fa782008-12-02 23:50:03 -0500568 cpu_buffer->reader_page = bpage;
Steven Rostedtd7690412008-10-01 00:29:53 -0400569 addr = __get_free_page(GFP_KERNEL);
570 if (!addr)
Steven Rostedte4c2ce82008-10-01 11:14:54 -0400571 goto fail_free_reader;
Steven Rostedt044fa782008-12-02 23:50:03 -0500572 bpage->page = (void *)addr;
573 rb_init_page(bpage->page);
Steven Rostedte4c2ce82008-10-01 11:14:54 -0400574
Steven Rostedtd7690412008-10-01 00:29:53 -0400575 INIT_LIST_HEAD(&cpu_buffer->reader_page->list);
Steven Rostedtd7690412008-10-01 00:29:53 -0400576
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400577 ret = rb_allocate_pages(cpu_buffer, buffer->pages);
578 if (ret < 0)
Steven Rostedtd7690412008-10-01 00:29:53 -0400579 goto fail_free_reader;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400580
581 cpu_buffer->head_page
582 = list_entry(cpu_buffer->pages.next, struct buffer_page, list);
Steven Rostedtbf41a152008-10-04 02:00:59 -0400583 cpu_buffer->tail_page = cpu_buffer->commit_page = cpu_buffer->head_page;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400584
585 return cpu_buffer;
586
Steven Rostedtd7690412008-10-01 00:29:53 -0400587 fail_free_reader:
588 free_buffer_page(cpu_buffer->reader_page);
589
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400590 fail_free_buffer:
591 kfree(cpu_buffer);
592 return NULL;
593}
594
595static void rb_free_cpu_buffer(struct ring_buffer_per_cpu *cpu_buffer)
596{
597 struct list_head *head = &cpu_buffer->pages;
Steven Rostedt044fa782008-12-02 23:50:03 -0500598 struct buffer_page *bpage, *tmp;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400599
Steven Rostedtd7690412008-10-01 00:29:53 -0400600 free_buffer_page(cpu_buffer->reader_page);
601
Steven Rostedt044fa782008-12-02 23:50:03 -0500602 list_for_each_entry_safe(bpage, tmp, head, list) {
603 list_del_init(&bpage->list);
604 free_buffer_page(bpage);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400605 }
606 kfree(cpu_buffer);
607}
608
Steven Rostedta7b13742008-09-29 23:02:39 -0400609/*
610 * Causes compile errors if the struct buffer_page gets bigger
611 * than the struct page.
612 */
613extern int ring_buffer_page_too_big(void);
614
Steven Rostedt59222ef2009-03-12 11:46:03 -0400615#ifdef CONFIG_HOTPLUG_CPU
Frederic Weisbecker09c9e842009-03-21 04:33:36 +0100616static int rb_cpu_notify(struct notifier_block *self,
617 unsigned long action, void *hcpu);
Steven Rostedt554f7862009-03-11 22:00:13 -0400618#endif
619
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400620/**
621 * ring_buffer_alloc - allocate a new ring_buffer
Robert Richter68814b52008-11-24 12:24:12 +0100622 * @size: the size in bytes per cpu that is needed.
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400623 * @flags: attributes to set for the ring buffer.
624 *
625 * Currently the only flag that is available is the RB_FL_OVERWRITE
626 * flag. This flag means that the buffer will overwrite old data
627 * when the buffer wraps. If this flag is not set, the buffer will
628 * drop data when the tail hits the head.
629 */
630struct ring_buffer *ring_buffer_alloc(unsigned long size, unsigned flags)
631{
632 struct ring_buffer *buffer;
633 int bsize;
634 int cpu;
635
Steven Rostedta7b13742008-09-29 23:02:39 -0400636 /* Paranoid! Optimizes out when all is well */
637 if (sizeof(struct buffer_page) > sizeof(struct page))
638 ring_buffer_page_too_big();
639
640
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400641 /* keep it in its own cache line */
642 buffer = kzalloc(ALIGN(sizeof(*buffer), cache_line_size()),
643 GFP_KERNEL);
644 if (!buffer)
645 return NULL;
646
Rusty Russell9e01c1b2009-01-01 10:12:22 +1030647 if (!alloc_cpumask_var(&buffer->cpumask, GFP_KERNEL))
648 goto fail_free_buffer;
649
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400650 buffer->pages = DIV_ROUND_UP(size, BUF_PAGE_SIZE);
651 buffer->flags = flags;
Steven Rostedt37886f62009-03-17 17:22:06 -0400652 buffer->clock = trace_clock_local;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400653
654 /* need at least two pages */
655 if (buffer->pages == 1)
656 buffer->pages++;
657
Frederic Weisbecker3bf832c2009-03-19 14:47:33 +0100658 /*
659 * In case of non-hotplug cpu, if the ring-buffer is allocated
660 * in early initcall, it will not be notified of secondary cpus.
661 * In that off case, we need to allocate for all possible cpus.
662 */
663#ifdef CONFIG_HOTPLUG_CPU
Steven Rostedt554f7862009-03-11 22:00:13 -0400664 get_online_cpus();
665 cpumask_copy(buffer->cpumask, cpu_online_mask);
Frederic Weisbecker3bf832c2009-03-19 14:47:33 +0100666#else
667 cpumask_copy(buffer->cpumask, cpu_possible_mask);
668#endif
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400669 buffer->cpus = nr_cpu_ids;
670
671 bsize = sizeof(void *) * nr_cpu_ids;
672 buffer->buffers = kzalloc(ALIGN(bsize, cache_line_size()),
673 GFP_KERNEL);
674 if (!buffer->buffers)
Rusty Russell9e01c1b2009-01-01 10:12:22 +1030675 goto fail_free_cpumask;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400676
677 for_each_buffer_cpu(buffer, cpu) {
678 buffer->buffers[cpu] =
679 rb_allocate_cpu_buffer(buffer, cpu);
680 if (!buffer->buffers[cpu])
681 goto fail_free_buffers;
682 }
683
Steven Rostedt59222ef2009-03-12 11:46:03 -0400684#ifdef CONFIG_HOTPLUG_CPU
Steven Rostedt554f7862009-03-11 22:00:13 -0400685 buffer->cpu_notify.notifier_call = rb_cpu_notify;
686 buffer->cpu_notify.priority = 0;
687 register_cpu_notifier(&buffer->cpu_notify);
688#endif
689
690 put_online_cpus();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400691 mutex_init(&buffer->mutex);
692
693 return buffer;
694
695 fail_free_buffers:
696 for_each_buffer_cpu(buffer, cpu) {
697 if (buffer->buffers[cpu])
698 rb_free_cpu_buffer(buffer->buffers[cpu]);
699 }
700 kfree(buffer->buffers);
701
Rusty Russell9e01c1b2009-01-01 10:12:22 +1030702 fail_free_cpumask:
703 free_cpumask_var(buffer->cpumask);
Steven Rostedt554f7862009-03-11 22:00:13 -0400704 put_online_cpus();
Rusty Russell9e01c1b2009-01-01 10:12:22 +1030705
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400706 fail_free_buffer:
707 kfree(buffer);
708 return NULL;
709}
Robert Richterc4f50182008-12-11 16:49:22 +0100710EXPORT_SYMBOL_GPL(ring_buffer_alloc);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400711
712/**
713 * ring_buffer_free - free a ring buffer.
714 * @buffer: the buffer to free.
715 */
716void
717ring_buffer_free(struct ring_buffer *buffer)
718{
719 int cpu;
720
Steven Rostedt554f7862009-03-11 22:00:13 -0400721 get_online_cpus();
722
Steven Rostedt59222ef2009-03-12 11:46:03 -0400723#ifdef CONFIG_HOTPLUG_CPU
Steven Rostedt554f7862009-03-11 22:00:13 -0400724 unregister_cpu_notifier(&buffer->cpu_notify);
725#endif
726
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400727 for_each_buffer_cpu(buffer, cpu)
728 rb_free_cpu_buffer(buffer->buffers[cpu]);
729
Steven Rostedt554f7862009-03-11 22:00:13 -0400730 put_online_cpus();
731
Rusty Russell9e01c1b2009-01-01 10:12:22 +1030732 free_cpumask_var(buffer->cpumask);
733
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400734 kfree(buffer);
735}
Robert Richterc4f50182008-12-11 16:49:22 +0100736EXPORT_SYMBOL_GPL(ring_buffer_free);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400737
Steven Rostedt37886f62009-03-17 17:22:06 -0400738void ring_buffer_set_clock(struct ring_buffer *buffer,
739 u64 (*clock)(void))
740{
741 buffer->clock = clock;
742}
743
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400744static void rb_reset_cpu(struct ring_buffer_per_cpu *cpu_buffer);
745
746static void
747rb_remove_pages(struct ring_buffer_per_cpu *cpu_buffer, unsigned nr_pages)
748{
Steven Rostedt044fa782008-12-02 23:50:03 -0500749 struct buffer_page *bpage;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400750 struct list_head *p;
751 unsigned i;
752
753 atomic_inc(&cpu_buffer->record_disabled);
754 synchronize_sched();
755
756 for (i = 0; i < nr_pages; i++) {
Steven Rostedt3e89c7bb2008-11-11 15:28:41 -0500757 if (RB_WARN_ON(cpu_buffer, list_empty(&cpu_buffer->pages)))
758 return;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400759 p = cpu_buffer->pages.next;
Steven Rostedt044fa782008-12-02 23:50:03 -0500760 bpage = list_entry(p, struct buffer_page, list);
761 list_del_init(&bpage->list);
762 free_buffer_page(bpage);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400763 }
Steven Rostedt3e89c7bb2008-11-11 15:28:41 -0500764 if (RB_WARN_ON(cpu_buffer, list_empty(&cpu_buffer->pages)))
765 return;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400766
767 rb_reset_cpu(cpu_buffer);
768
769 rb_check_pages(cpu_buffer);
770
771 atomic_dec(&cpu_buffer->record_disabled);
772
773}
774
775static void
776rb_insert_pages(struct ring_buffer_per_cpu *cpu_buffer,
777 struct list_head *pages, unsigned nr_pages)
778{
Steven Rostedt044fa782008-12-02 23:50:03 -0500779 struct buffer_page *bpage;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400780 struct list_head *p;
781 unsigned i;
782
783 atomic_inc(&cpu_buffer->record_disabled);
784 synchronize_sched();
785
786 for (i = 0; i < nr_pages; i++) {
Steven Rostedt3e89c7bb2008-11-11 15:28:41 -0500787 if (RB_WARN_ON(cpu_buffer, list_empty(pages)))
788 return;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400789 p = pages->next;
Steven Rostedt044fa782008-12-02 23:50:03 -0500790 bpage = list_entry(p, struct buffer_page, list);
791 list_del_init(&bpage->list);
792 list_add_tail(&bpage->list, &cpu_buffer->pages);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400793 }
794 rb_reset_cpu(cpu_buffer);
795
796 rb_check_pages(cpu_buffer);
797
798 atomic_dec(&cpu_buffer->record_disabled);
799}
800
801/**
802 * ring_buffer_resize - resize the ring buffer
803 * @buffer: the buffer to resize.
804 * @size: the new size.
805 *
806 * The tracer is responsible for making sure that the buffer is
807 * not being used while changing the size.
808 * Note: We may be able to change the above requirement by using
809 * RCU synchronizations.
810 *
811 * Minimum size is 2 * BUF_PAGE_SIZE.
812 *
813 * Returns -1 on failure.
814 */
815int ring_buffer_resize(struct ring_buffer *buffer, unsigned long size)
816{
817 struct ring_buffer_per_cpu *cpu_buffer;
818 unsigned nr_pages, rm_pages, new_pages;
Steven Rostedt044fa782008-12-02 23:50:03 -0500819 struct buffer_page *bpage, *tmp;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400820 unsigned long buffer_size;
821 unsigned long addr;
822 LIST_HEAD(pages);
823 int i, cpu;
824
Ingo Molnaree51a1d2008-11-13 14:58:31 +0100825 /*
826 * Always succeed at resizing a non-existent buffer:
827 */
828 if (!buffer)
829 return size;
830
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400831 size = DIV_ROUND_UP(size, BUF_PAGE_SIZE);
832 size *= BUF_PAGE_SIZE;
833 buffer_size = buffer->pages * BUF_PAGE_SIZE;
834
835 /* we need a minimum of two pages */
836 if (size < BUF_PAGE_SIZE * 2)
837 size = BUF_PAGE_SIZE * 2;
838
839 if (size == buffer_size)
840 return size;
841
842 mutex_lock(&buffer->mutex);
Steven Rostedt554f7862009-03-11 22:00:13 -0400843 get_online_cpus();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400844
845 nr_pages = DIV_ROUND_UP(size, BUF_PAGE_SIZE);
846
847 if (size < buffer_size) {
848
849 /* easy case, just free pages */
Steven Rostedt554f7862009-03-11 22:00:13 -0400850 if (RB_WARN_ON(buffer, nr_pages >= buffer->pages))
851 goto out_fail;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400852
853 rm_pages = buffer->pages - nr_pages;
854
855 for_each_buffer_cpu(buffer, cpu) {
856 cpu_buffer = buffer->buffers[cpu];
857 rb_remove_pages(cpu_buffer, rm_pages);
858 }
859 goto out;
860 }
861
862 /*
863 * This is a bit more difficult. We only want to add pages
864 * when we can allocate enough for all CPUs. We do this
865 * by allocating all the pages and storing them on a local
866 * link list. If we succeed in our allocation, then we
867 * add these pages to the cpu_buffers. Otherwise we just free
868 * them all and return -ENOMEM;
869 */
Steven Rostedt554f7862009-03-11 22:00:13 -0400870 if (RB_WARN_ON(buffer, nr_pages <= buffer->pages))
871 goto out_fail;
Steven Rostedtf536aaf2008-11-10 23:07:30 -0500872
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400873 new_pages = nr_pages - buffer->pages;
874
875 for_each_buffer_cpu(buffer, cpu) {
876 for (i = 0; i < new_pages; i++) {
Steven Rostedt044fa782008-12-02 23:50:03 -0500877 bpage = kzalloc_node(ALIGN(sizeof(*bpage),
Steven Rostedte4c2ce82008-10-01 11:14:54 -0400878 cache_line_size()),
879 GFP_KERNEL, cpu_to_node(cpu));
Steven Rostedt044fa782008-12-02 23:50:03 -0500880 if (!bpage)
Steven Rostedte4c2ce82008-10-01 11:14:54 -0400881 goto free_pages;
Steven Rostedt044fa782008-12-02 23:50:03 -0500882 list_add(&bpage->list, &pages);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400883 addr = __get_free_page(GFP_KERNEL);
884 if (!addr)
885 goto free_pages;
Steven Rostedt044fa782008-12-02 23:50:03 -0500886 bpage->page = (void *)addr;
887 rb_init_page(bpage->page);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400888 }
889 }
890
891 for_each_buffer_cpu(buffer, cpu) {
892 cpu_buffer = buffer->buffers[cpu];
893 rb_insert_pages(cpu_buffer, &pages, new_pages);
894 }
895
Steven Rostedt554f7862009-03-11 22:00:13 -0400896 if (RB_WARN_ON(buffer, !list_empty(&pages)))
897 goto out_fail;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400898
899 out:
900 buffer->pages = nr_pages;
Steven Rostedt554f7862009-03-11 22:00:13 -0400901 put_online_cpus();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400902 mutex_unlock(&buffer->mutex);
903
904 return size;
905
906 free_pages:
Steven Rostedt044fa782008-12-02 23:50:03 -0500907 list_for_each_entry_safe(bpage, tmp, &pages, list) {
908 list_del_init(&bpage->list);
909 free_buffer_page(bpage);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400910 }
Steven Rostedt554f7862009-03-11 22:00:13 -0400911 put_online_cpus();
Vegard Nossum641d2f62008-11-18 19:22:13 +0100912 mutex_unlock(&buffer->mutex);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400913 return -ENOMEM;
Steven Rostedt554f7862009-03-11 22:00:13 -0400914
915 /*
916 * Something went totally wrong, and we are too paranoid
917 * to even clean up the mess.
918 */
919 out_fail:
920 put_online_cpus();
921 mutex_unlock(&buffer->mutex);
922 return -1;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400923}
Robert Richterc4f50182008-12-11 16:49:22 +0100924EXPORT_SYMBOL_GPL(ring_buffer_resize);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400925
Steven Rostedt8789a9e2008-12-02 15:34:07 -0500926static inline void *
Steven Rostedt044fa782008-12-02 23:50:03 -0500927__rb_data_page_index(struct buffer_data_page *bpage, unsigned index)
Steven Rostedt8789a9e2008-12-02 15:34:07 -0500928{
Steven Rostedt044fa782008-12-02 23:50:03 -0500929 return bpage->data + index;
Steven Rostedt8789a9e2008-12-02 15:34:07 -0500930}
931
Steven Rostedt044fa782008-12-02 23:50:03 -0500932static inline void *__rb_page_index(struct buffer_page *bpage, unsigned index)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400933{
Steven Rostedt044fa782008-12-02 23:50:03 -0500934 return bpage->page->data + index;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400935}
936
937static inline struct ring_buffer_event *
Steven Rostedtd7690412008-10-01 00:29:53 -0400938rb_reader_event(struct ring_buffer_per_cpu *cpu_buffer)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400939{
Steven Rostedt6f807ac2008-10-04 02:00:58 -0400940 return __rb_page_index(cpu_buffer->reader_page,
941 cpu_buffer->reader_page->read);
942}
943
944static inline struct ring_buffer_event *
945rb_head_event(struct ring_buffer_per_cpu *cpu_buffer)
946{
947 return __rb_page_index(cpu_buffer->head_page,
948 cpu_buffer->head_page->read);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400949}
950
951static inline struct ring_buffer_event *
952rb_iter_head_event(struct ring_buffer_iter *iter)
953{
Steven Rostedt6f807ac2008-10-04 02:00:58 -0400954 return __rb_page_index(iter->head_page, iter->head);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400955}
956
Steven Rostedtbf41a152008-10-04 02:00:59 -0400957static inline unsigned rb_page_write(struct buffer_page *bpage)
958{
959 return local_read(&bpage->write);
960}
961
962static inline unsigned rb_page_commit(struct buffer_page *bpage)
963{
Steven Rostedtabc9b562008-12-02 15:34:06 -0500964 return local_read(&bpage->page->commit);
Steven Rostedtbf41a152008-10-04 02:00:59 -0400965}
966
967/* Size is determined by what has been commited */
968static inline unsigned rb_page_size(struct buffer_page *bpage)
969{
970 return rb_page_commit(bpage);
971}
972
973static inline unsigned
974rb_commit_index(struct ring_buffer_per_cpu *cpu_buffer)
975{
976 return rb_page_commit(cpu_buffer->commit_page);
977}
978
979static inline unsigned rb_head_size(struct ring_buffer_per_cpu *cpu_buffer)
980{
981 return rb_page_commit(cpu_buffer->head_page);
982}
983
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400984static inline void rb_inc_page(struct ring_buffer_per_cpu *cpu_buffer,
Steven Rostedt044fa782008-12-02 23:50:03 -0500985 struct buffer_page **bpage)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400986{
Steven Rostedt044fa782008-12-02 23:50:03 -0500987 struct list_head *p = (*bpage)->list.next;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400988
989 if (p == &cpu_buffer->pages)
990 p = p->next;
991
Steven Rostedt044fa782008-12-02 23:50:03 -0500992 *bpage = list_entry(p, struct buffer_page, list);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400993}
994
Steven Rostedtbf41a152008-10-04 02:00:59 -0400995static inline unsigned
996rb_event_index(struct ring_buffer_event *event)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400997{
Steven Rostedtbf41a152008-10-04 02:00:59 -0400998 unsigned long addr = (unsigned long)event;
999
1000 return (addr & ~PAGE_MASK) - (PAGE_SIZE - BUF_PAGE_SIZE);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001001}
1002
Steven Rostedt0f0c85f2009-05-11 16:08:00 -04001003static inline int
Steven Rostedtbf41a152008-10-04 02:00:59 -04001004rb_is_commit(struct ring_buffer_per_cpu *cpu_buffer,
1005 struct ring_buffer_event *event)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001006{
Steven Rostedtbf41a152008-10-04 02:00:59 -04001007 unsigned long addr = (unsigned long)event;
1008 unsigned long index;
1009
1010 index = rb_event_index(event);
1011 addr &= PAGE_MASK;
1012
1013 return cpu_buffer->commit_page->page == (void *)addr &&
1014 rb_commit_index(cpu_buffer) == index;
1015}
1016
Andrew Morton34a148b2009-01-09 12:27:09 -08001017static void
Steven Rostedtbf41a152008-10-04 02:00:59 -04001018rb_set_commit_event(struct ring_buffer_per_cpu *cpu_buffer,
1019 struct ring_buffer_event *event)
1020{
1021 unsigned long addr = (unsigned long)event;
1022 unsigned long index;
1023
1024 index = rb_event_index(event);
1025 addr &= PAGE_MASK;
1026
1027 while (cpu_buffer->commit_page->page != (void *)addr) {
Steven Rostedt3e89c7bb2008-11-11 15:28:41 -05001028 if (RB_WARN_ON(cpu_buffer,
1029 cpu_buffer->commit_page == cpu_buffer->tail_page))
1030 return;
Steven Rostedtabc9b562008-12-02 15:34:06 -05001031 cpu_buffer->commit_page->page->commit =
Steven Rostedtbf41a152008-10-04 02:00:59 -04001032 cpu_buffer->commit_page->write;
1033 rb_inc_page(cpu_buffer, &cpu_buffer->commit_page);
Steven Rostedtabc9b562008-12-02 15:34:06 -05001034 cpu_buffer->write_stamp =
1035 cpu_buffer->commit_page->page->time_stamp;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001036 }
1037
1038 /* Now set the commit to the event's index */
Steven Rostedtabc9b562008-12-02 15:34:06 -05001039 local_set(&cpu_buffer->commit_page->page->commit, index);
Steven Rostedtbf41a152008-10-04 02:00:59 -04001040}
1041
Andrew Morton34a148b2009-01-09 12:27:09 -08001042static void
Steven Rostedtbf41a152008-10-04 02:00:59 -04001043rb_set_commit_to_write(struct ring_buffer_per_cpu *cpu_buffer)
1044{
1045 /*
1046 * We only race with interrupts and NMIs on this CPU.
1047 * If we own the commit event, then we can commit
1048 * all others that interrupted us, since the interruptions
1049 * are in stack format (they finish before they come
1050 * back to us). This allows us to do a simple loop to
1051 * assign the commit to the tail.
1052 */
Steven Rostedta8ccf1d2008-12-23 11:32:24 -05001053 again:
Steven Rostedtbf41a152008-10-04 02:00:59 -04001054 while (cpu_buffer->commit_page != cpu_buffer->tail_page) {
Steven Rostedtabc9b562008-12-02 15:34:06 -05001055 cpu_buffer->commit_page->page->commit =
Steven Rostedtbf41a152008-10-04 02:00:59 -04001056 cpu_buffer->commit_page->write;
1057 rb_inc_page(cpu_buffer, &cpu_buffer->commit_page);
Steven Rostedtabc9b562008-12-02 15:34:06 -05001058 cpu_buffer->write_stamp =
1059 cpu_buffer->commit_page->page->time_stamp;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001060 /* add barrier to keep gcc from optimizing too much */
1061 barrier();
1062 }
1063 while (rb_commit_index(cpu_buffer) !=
1064 rb_page_write(cpu_buffer->commit_page)) {
Steven Rostedtabc9b562008-12-02 15:34:06 -05001065 cpu_buffer->commit_page->page->commit =
Steven Rostedtbf41a152008-10-04 02:00:59 -04001066 cpu_buffer->commit_page->write;
1067 barrier();
1068 }
Steven Rostedta8ccf1d2008-12-23 11:32:24 -05001069
1070 /* again, keep gcc from optimizing */
1071 barrier();
1072
1073 /*
1074 * If an interrupt came in just after the first while loop
1075 * and pushed the tail page forward, we will be left with
1076 * a dangling commit that will never go forward.
1077 */
1078 if (unlikely(cpu_buffer->commit_page != cpu_buffer->tail_page))
1079 goto again;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001080}
1081
Steven Rostedtd7690412008-10-01 00:29:53 -04001082static void rb_reset_reader_page(struct ring_buffer_per_cpu *cpu_buffer)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001083{
Steven Rostedtabc9b562008-12-02 15:34:06 -05001084 cpu_buffer->read_stamp = cpu_buffer->reader_page->page->time_stamp;
Steven Rostedt6f807ac2008-10-04 02:00:58 -04001085 cpu_buffer->reader_page->read = 0;
Steven Rostedtd7690412008-10-01 00:29:53 -04001086}
1087
Andrew Morton34a148b2009-01-09 12:27:09 -08001088static void rb_inc_iter(struct ring_buffer_iter *iter)
Steven Rostedtd7690412008-10-01 00:29:53 -04001089{
1090 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
1091
1092 /*
1093 * The iterator could be on the reader page (it starts there).
1094 * But the head could have moved, since the reader was
1095 * found. Check for this case and assign the iterator
1096 * to the head page instead of next.
1097 */
1098 if (iter->head_page == cpu_buffer->reader_page)
1099 iter->head_page = cpu_buffer->head_page;
1100 else
1101 rb_inc_page(cpu_buffer, &iter->head_page);
1102
Steven Rostedtabc9b562008-12-02 15:34:06 -05001103 iter->read_stamp = iter->head_page->page->time_stamp;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001104 iter->head = 0;
1105}
1106
1107/**
1108 * ring_buffer_update_event - update event type and data
1109 * @event: the even to update
1110 * @type: the type of event
1111 * @length: the size of the event field in the ring buffer
1112 *
1113 * Update the type and data fields of the event. The length
1114 * is the actual size that is written to the ring buffer,
1115 * and with this, we can determine what to place into the
1116 * data field.
1117 */
Andrew Morton34a148b2009-01-09 12:27:09 -08001118static void
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001119rb_update_event(struct ring_buffer_event *event,
1120 unsigned type, unsigned length)
1121{
Lai Jiangshan334d4162009-04-24 11:27:05 +08001122 event->type_len = type;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001123
1124 switch (type) {
1125
1126 case RINGBUF_TYPE_PADDING:
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001127 case RINGBUF_TYPE_TIME_EXTEND:
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001128 case RINGBUF_TYPE_TIME_STAMP:
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001129 break;
1130
Lai Jiangshan334d4162009-04-24 11:27:05 +08001131 case 0:
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001132 length -= RB_EVNT_HDR_SIZE;
Lai Jiangshan334d4162009-04-24 11:27:05 +08001133 if (length > RB_MAX_SMALL_DATA)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001134 event->array[0] = length;
Lai Jiangshan334d4162009-04-24 11:27:05 +08001135 else
1136 event->type_len = DIV_ROUND_UP(length, RB_ALIGNMENT);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001137 break;
1138 default:
1139 BUG();
1140 }
1141}
1142
Andrew Morton34a148b2009-01-09 12:27:09 -08001143static unsigned rb_calculate_event_length(unsigned length)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001144{
1145 struct ring_buffer_event event; /* Used only for sizeof array */
1146
1147 /* zero length can cause confusions */
1148 if (!length)
1149 length = 1;
1150
1151 if (length > RB_MAX_SMALL_DATA)
1152 length += sizeof(event.array[0]);
1153
1154 length += RB_EVNT_HDR_SIZE;
1155 length = ALIGN(length, RB_ALIGNMENT);
1156
1157 return length;
1158}
1159
Steven Rostedt6634ff22009-05-06 15:30:07 -04001160
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001161static struct ring_buffer_event *
Steven Rostedt6634ff22009-05-06 15:30:07 -04001162rb_move_tail(struct ring_buffer_per_cpu *cpu_buffer,
1163 unsigned long length, unsigned long tail,
1164 struct buffer_page *commit_page,
1165 struct buffer_page *tail_page, u64 *ts)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001166{
Steven Rostedt6634ff22009-05-06 15:30:07 -04001167 struct buffer_page *next_page, *head_page, *reader_page;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001168 struct ring_buffer *buffer = cpu_buffer->buffer;
1169 struct ring_buffer_event *event;
Steven Rostedt78d904b2009-02-05 18:43:07 -05001170 bool lock_taken = false;
Steven Rostedt6634ff22009-05-06 15:30:07 -04001171 unsigned long flags;
Steven Rostedtaa20ae82009-05-05 21:16:11 -04001172
1173 next_page = tail_page;
1174
1175 local_irq_save(flags);
1176 /*
1177 * Since the write to the buffer is still not
1178 * fully lockless, we must be careful with NMIs.
1179 * The locks in the writers are taken when a write
1180 * crosses to a new page. The locks protect against
1181 * races with the readers (this will soon be fixed
1182 * with a lockless solution).
1183 *
1184 * Because we can not protect against NMIs, and we
1185 * want to keep traces reentrant, we need to manage
1186 * what happens when we are in an NMI.
1187 *
1188 * NMIs can happen after we take the lock.
1189 * If we are in an NMI, only take the lock
1190 * if it is not already taken. Otherwise
1191 * simply fail.
1192 */
1193 if (unlikely(in_nmi())) {
1194 if (!__raw_spin_trylock(&cpu_buffer->lock)) {
1195 cpu_buffer->nmi_dropped++;
1196 goto out_reset;
1197 }
1198 } else
1199 __raw_spin_lock(&cpu_buffer->lock);
1200
1201 lock_taken = true;
1202
1203 rb_inc_page(cpu_buffer, &next_page);
1204
1205 head_page = cpu_buffer->head_page;
1206 reader_page = cpu_buffer->reader_page;
1207
1208 /* we grabbed the lock before incrementing */
1209 if (RB_WARN_ON(cpu_buffer, next_page == reader_page))
1210 goto out_reset;
1211
1212 /*
1213 * If for some reason, we had an interrupt storm that made
1214 * it all the way around the buffer, bail, and warn
1215 * about it.
1216 */
1217 if (unlikely(next_page == commit_page)) {
1218 cpu_buffer->commit_overrun++;
1219 goto out_reset;
1220 }
1221
1222 if (next_page == head_page) {
1223 if (!(buffer->flags & RB_FL_OVERWRITE))
1224 goto out_reset;
1225
1226 /* tail_page has not moved yet? */
1227 if (tail_page == cpu_buffer->tail_page) {
1228 /* count overflows */
1229 cpu_buffer->overrun +=
1230 local_read(&head_page->entries);
1231
1232 rb_inc_page(cpu_buffer, &head_page);
1233 cpu_buffer->head_page = head_page;
1234 cpu_buffer->head_page->read = 0;
1235 }
1236 }
1237
1238 /*
1239 * If the tail page is still the same as what we think
1240 * it is, then it is up to us to update the tail
1241 * pointer.
1242 */
1243 if (tail_page == cpu_buffer->tail_page) {
1244 local_set(&next_page->write, 0);
1245 local_set(&next_page->entries, 0);
1246 local_set(&next_page->page->commit, 0);
1247 cpu_buffer->tail_page = next_page;
1248
1249 /* reread the time stamp */
1250 *ts = ring_buffer_time_stamp(buffer, cpu_buffer->cpu);
1251 cpu_buffer->tail_page->page->time_stamp = *ts;
1252 }
1253
1254 /*
1255 * The actual tail page has moved forward.
1256 */
1257 if (tail < BUF_PAGE_SIZE) {
1258 /* Mark the rest of the page with padding */
1259 event = __rb_page_index(tail_page, tail);
1260 rb_event_set_padding(event);
1261 }
1262
Steven Rostedt8e7abf12009-05-06 10:26:45 -04001263 /* Set the write back to the previous setting */
1264 local_sub(length, &tail_page->write);
Steven Rostedtaa20ae82009-05-05 21:16:11 -04001265
1266 /*
1267 * If this was a commit entry that failed,
1268 * increment that too
1269 */
1270 if (tail_page == cpu_buffer->commit_page &&
1271 tail == rb_commit_index(cpu_buffer)) {
1272 rb_set_commit_to_write(cpu_buffer);
1273 }
1274
1275 __raw_spin_unlock(&cpu_buffer->lock);
1276 local_irq_restore(flags);
1277
1278 /* fail and let the caller try again */
1279 return ERR_PTR(-EAGAIN);
1280
Steven Rostedt45141d42009-02-12 13:19:48 -05001281 out_reset:
Lai Jiangshan6f3b3442009-01-12 11:06:18 +08001282 /* reset write */
Steven Rostedt8e7abf12009-05-06 10:26:45 -04001283 local_sub(length, &tail_page->write);
Lai Jiangshan6f3b3442009-01-12 11:06:18 +08001284
Steven Rostedt78d904b2009-02-05 18:43:07 -05001285 if (likely(lock_taken))
1286 __raw_spin_unlock(&cpu_buffer->lock);
Steven Rostedt3e03fb72008-11-06 00:09:43 -05001287 local_irq_restore(flags);
Steven Rostedtbf41a152008-10-04 02:00:59 -04001288 return NULL;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001289}
1290
Steven Rostedt6634ff22009-05-06 15:30:07 -04001291static struct ring_buffer_event *
1292__rb_reserve_next(struct ring_buffer_per_cpu *cpu_buffer,
1293 unsigned type, unsigned long length, u64 *ts)
1294{
1295 struct buffer_page *tail_page, *commit_page;
1296 struct ring_buffer_event *event;
1297 unsigned long tail, write;
1298
1299 commit_page = cpu_buffer->commit_page;
1300 /* we just need to protect against interrupts */
1301 barrier();
1302 tail_page = cpu_buffer->tail_page;
1303 write = local_add_return(length, &tail_page->write);
1304 tail = write - length;
1305
1306 /* See if we shot pass the end of this buffer page */
1307 if (write > BUF_PAGE_SIZE)
1308 return rb_move_tail(cpu_buffer, length, tail,
1309 commit_page, tail_page, ts);
1310
1311 /* We reserved something on the buffer */
1312
1313 if (RB_WARN_ON(cpu_buffer, write > BUF_PAGE_SIZE))
1314 return NULL;
1315
1316 event = __rb_page_index(tail_page, tail);
1317 rb_update_event(event, type, length);
1318
1319 /* The passed in type is zero for DATA */
1320 if (likely(!type))
1321 local_inc(&tail_page->entries);
1322
1323 /*
1324 * If this is a commit and the tail is zero, then update
1325 * this page's time stamp.
1326 */
1327 if (!tail && rb_is_commit(cpu_buffer, event))
1328 cpu_buffer->commit_page->page->time_stamp = *ts;
1329
1330 return event;
1331}
1332
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001333static int
1334rb_add_time_stamp(struct ring_buffer_per_cpu *cpu_buffer,
1335 u64 *ts, u64 *delta)
1336{
1337 struct ring_buffer_event *event;
1338 static int once;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001339 int ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001340
1341 if (unlikely(*delta > (1ULL << 59) && !once++)) {
1342 printk(KERN_WARNING "Delta way too big! %llu"
1343 " ts=%llu write stamp = %llu\n",
Stephen Rothwelle2862c92008-10-27 17:43:28 +11001344 (unsigned long long)*delta,
1345 (unsigned long long)*ts,
1346 (unsigned long long)cpu_buffer->write_stamp);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001347 WARN_ON(1);
1348 }
1349
1350 /*
1351 * The delta is too big, we to add a
1352 * new timestamp.
1353 */
1354 event = __rb_reserve_next(cpu_buffer,
1355 RINGBUF_TYPE_TIME_EXTEND,
1356 RB_LEN_TIME_EXTEND,
1357 ts);
1358 if (!event)
Steven Rostedtbf41a152008-10-04 02:00:59 -04001359 return -EBUSY;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001360
Steven Rostedtbf41a152008-10-04 02:00:59 -04001361 if (PTR_ERR(event) == -EAGAIN)
1362 return -EAGAIN;
1363
1364 /* Only a commited time event can update the write stamp */
1365 if (rb_is_commit(cpu_buffer, event)) {
1366 /*
1367 * If this is the first on the page, then we need to
1368 * update the page itself, and just put in a zero.
1369 */
1370 if (rb_event_index(event)) {
1371 event->time_delta = *delta & TS_MASK;
1372 event->array[0] = *delta >> TS_SHIFT;
1373 } else {
Steven Rostedtabc9b562008-12-02 15:34:06 -05001374 cpu_buffer->commit_page->page->time_stamp = *ts;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001375 event->time_delta = 0;
1376 event->array[0] = 0;
1377 }
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001378 cpu_buffer->write_stamp = *ts;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001379 /* let the caller know this was the commit */
1380 ret = 1;
1381 } else {
1382 /* Darn, this is just wasted space */
1383 event->time_delta = 0;
1384 event->array[0] = 0;
1385 ret = 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001386 }
1387
Steven Rostedtbf41a152008-10-04 02:00:59 -04001388 *delta = 0;
1389
1390 return ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001391}
1392
1393static struct ring_buffer_event *
1394rb_reserve_next_event(struct ring_buffer_per_cpu *cpu_buffer,
Steven Rostedt1cd8d732009-05-11 14:08:09 -04001395 unsigned long length)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001396{
1397 struct ring_buffer_event *event;
1398 u64 ts, delta;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001399 int commit = 0;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04001400 int nr_loops = 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001401
Steven Rostedtbe957c42009-05-11 14:42:53 -04001402 length = rb_calculate_event_length(length);
Steven Rostedtbf41a152008-10-04 02:00:59 -04001403 again:
Steven Rostedt818e3dd2008-10-31 09:58:35 -04001404 /*
1405 * We allow for interrupts to reenter here and do a trace.
1406 * If one does, it will cause this original code to loop
1407 * back here. Even with heavy interrupts happening, this
1408 * should only happen a few times in a row. If this happens
1409 * 1000 times in a row, there must be either an interrupt
1410 * storm or we have something buggy.
1411 * Bail!
1412 */
Steven Rostedt3e89c7bb2008-11-11 15:28:41 -05001413 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 1000))
Steven Rostedt818e3dd2008-10-31 09:58:35 -04001414 return NULL;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04001415
Steven Rostedt37886f62009-03-17 17:22:06 -04001416 ts = ring_buffer_time_stamp(cpu_buffer->buffer, cpu_buffer->cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001417
Steven Rostedtbf41a152008-10-04 02:00:59 -04001418 /*
1419 * Only the first commit can update the timestamp.
1420 * Yes there is a race here. If an interrupt comes in
1421 * just after the conditional and it traces too, then it
1422 * will also check the deltas. More than one timestamp may
1423 * also be made. But only the entry that did the actual
1424 * commit will be something other than zero.
1425 */
Steven Rostedt0f0c85f2009-05-11 16:08:00 -04001426 if (likely(cpu_buffer->tail_page == cpu_buffer->commit_page &&
1427 rb_page_write(cpu_buffer->tail_page) ==
1428 rb_commit_index(cpu_buffer))) {
Steven Rostedtbf41a152008-10-04 02:00:59 -04001429
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001430 delta = ts - cpu_buffer->write_stamp;
1431
Steven Rostedtbf41a152008-10-04 02:00:59 -04001432 /* make sure this delta is calculated here */
1433 barrier();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001434
Steven Rostedtbf41a152008-10-04 02:00:59 -04001435 /* Did the write stamp get updated already? */
1436 if (unlikely(ts < cpu_buffer->write_stamp))
Steven Rostedt4143c5c2008-11-10 21:46:01 -05001437 delta = 0;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001438
Steven Rostedt0f0c85f2009-05-11 16:08:00 -04001439 else if (unlikely(test_time_stamp(delta))) {
Steven Rostedtbf41a152008-10-04 02:00:59 -04001440
1441 commit = rb_add_time_stamp(cpu_buffer, &ts, &delta);
1442
1443 if (commit == -EBUSY)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001444 return NULL;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001445
1446 if (commit == -EAGAIN)
1447 goto again;
1448
1449 RB_WARN_ON(cpu_buffer, commit < 0);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001450 }
Steven Rostedtbf41a152008-10-04 02:00:59 -04001451 } else
1452 /* Non commits have zero deltas */
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001453 delta = 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001454
Steven Rostedt1cd8d732009-05-11 14:08:09 -04001455 event = __rb_reserve_next(cpu_buffer, 0, length, &ts);
Steven Rostedtbf41a152008-10-04 02:00:59 -04001456 if (PTR_ERR(event) == -EAGAIN)
1457 goto again;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001458
Steven Rostedtbf41a152008-10-04 02:00:59 -04001459 if (!event) {
1460 if (unlikely(commit))
1461 /*
1462 * Ouch! We needed a timestamp and it was commited. But
1463 * we didn't get our event reserved.
1464 */
1465 rb_set_commit_to_write(cpu_buffer);
1466 return NULL;
1467 }
1468
1469 /*
1470 * If the timestamp was commited, make the commit our entry
1471 * now so that we will update it when needed.
1472 */
Steven Rostedt0f0c85f2009-05-11 16:08:00 -04001473 if (unlikely(commit))
Steven Rostedtbf41a152008-10-04 02:00:59 -04001474 rb_set_commit_event(cpu_buffer, event);
1475 else if (!rb_is_commit(cpu_buffer, event))
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001476 delta = 0;
1477
1478 event->time_delta = delta;
1479
1480 return event;
1481}
1482
Steven Rostedtaa18efb2009-04-20 16:16:11 -04001483#define TRACE_RECURSIVE_DEPTH 16
Steven Rostedt261842b2009-04-16 21:41:52 -04001484
1485static int trace_recursive_lock(void)
1486{
Steven Rostedtaa18efb2009-04-20 16:16:11 -04001487 current->trace_recursion++;
Steven Rostedt261842b2009-04-16 21:41:52 -04001488
Steven Rostedtaa18efb2009-04-20 16:16:11 -04001489 if (likely(current->trace_recursion < TRACE_RECURSIVE_DEPTH))
1490 return 0;
Steven Rostedt261842b2009-04-16 21:41:52 -04001491
Steven Rostedtaa18efb2009-04-20 16:16:11 -04001492 /* Disable all tracing before we do anything else */
1493 tracing_off_permanent();
Frederic Weisbeckere057a5e2009-04-19 23:38:12 +02001494
Steven Rostedt7d7d2b82009-04-27 12:37:49 -04001495 printk_once(KERN_WARNING "Tracing recursion: depth[%ld]:"
Steven Rostedtaa18efb2009-04-20 16:16:11 -04001496 "HC[%lu]:SC[%lu]:NMI[%lu]\n",
1497 current->trace_recursion,
1498 hardirq_count() >> HARDIRQ_SHIFT,
1499 softirq_count() >> SOFTIRQ_SHIFT,
1500 in_nmi());
Frederic Weisbeckere057a5e2009-04-19 23:38:12 +02001501
Steven Rostedtaa18efb2009-04-20 16:16:11 -04001502 WARN_ON_ONCE(1);
1503 return -1;
Steven Rostedt261842b2009-04-16 21:41:52 -04001504}
1505
1506static void trace_recursive_unlock(void)
1507{
Steven Rostedtaa18efb2009-04-20 16:16:11 -04001508 WARN_ON_ONCE(!current->trace_recursion);
Steven Rostedt261842b2009-04-16 21:41:52 -04001509
Steven Rostedtaa18efb2009-04-20 16:16:11 -04001510 current->trace_recursion--;
Steven Rostedt261842b2009-04-16 21:41:52 -04001511}
1512
Steven Rostedtbf41a152008-10-04 02:00:59 -04001513static DEFINE_PER_CPU(int, rb_need_resched);
1514
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001515/**
1516 * ring_buffer_lock_reserve - reserve a part of the buffer
1517 * @buffer: the ring buffer to reserve from
1518 * @length: the length of the data to reserve (excluding event header)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001519 *
1520 * Returns a reseverd event on the ring buffer to copy directly to.
1521 * The user of this interface will need to get the body to write into
1522 * and can use the ring_buffer_event_data() interface.
1523 *
1524 * The length is the length of the data needed, not the event length
1525 * which also includes the event header.
1526 *
1527 * Must be paired with ring_buffer_unlock_commit, unless NULL is returned.
1528 * If NULL is returned, then nothing has been allocated or locked.
1529 */
1530struct ring_buffer_event *
Arnaldo Carvalho de Melo0a987752009-02-05 16:12:56 -02001531ring_buffer_lock_reserve(struct ring_buffer *buffer, unsigned long length)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001532{
1533 struct ring_buffer_per_cpu *cpu_buffer;
1534 struct ring_buffer_event *event;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001535 int cpu, resched;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001536
Steven Rostedt033601a2008-11-21 12:41:55 -05001537 if (ring_buffer_flags != RB_BUFFERS_ON)
Steven Rostedta3583242008-11-11 15:01:42 -05001538 return NULL;
1539
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001540 if (atomic_read(&buffer->record_disabled))
1541 return NULL;
1542
Steven Rostedtbf41a152008-10-04 02:00:59 -04001543 /* If we are tracing schedule, we don't want to recurse */
Steven Rostedt182e9f52008-11-03 23:15:56 -05001544 resched = ftrace_preempt_disable();
Steven Rostedtbf41a152008-10-04 02:00:59 -04001545
Steven Rostedt261842b2009-04-16 21:41:52 -04001546 if (trace_recursive_lock())
1547 goto out_nocheck;
1548
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001549 cpu = raw_smp_processor_id();
1550
Rusty Russell9e01c1b2009-01-01 10:12:22 +10301551 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedtd7690412008-10-01 00:29:53 -04001552 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001553
1554 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001555
1556 if (atomic_read(&cpu_buffer->record_disabled))
Steven Rostedtd7690412008-10-01 00:29:53 -04001557 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001558
Steven Rostedtbe957c42009-05-11 14:42:53 -04001559 if (length > BUF_MAX_DATA_SIZE)
Steven Rostedtbf41a152008-10-04 02:00:59 -04001560 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001561
Steven Rostedt1cd8d732009-05-11 14:08:09 -04001562 event = rb_reserve_next_event(cpu_buffer, length);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001563 if (!event)
Steven Rostedtd7690412008-10-01 00:29:53 -04001564 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001565
Steven Rostedtbf41a152008-10-04 02:00:59 -04001566 /*
1567 * Need to store resched state on this cpu.
1568 * Only the first needs to.
1569 */
1570
1571 if (preempt_count() == 1)
1572 per_cpu(rb_need_resched, cpu) = resched;
1573
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001574 return event;
1575
Steven Rostedtd7690412008-10-01 00:29:53 -04001576 out:
Steven Rostedt261842b2009-04-16 21:41:52 -04001577 trace_recursive_unlock();
1578
1579 out_nocheck:
Steven Rostedt182e9f52008-11-03 23:15:56 -05001580 ftrace_preempt_enable(resched);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001581 return NULL;
1582}
Robert Richterc4f50182008-12-11 16:49:22 +01001583EXPORT_SYMBOL_GPL(ring_buffer_lock_reserve);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001584
1585static void rb_commit(struct ring_buffer_per_cpu *cpu_buffer,
1586 struct ring_buffer_event *event)
1587{
Steven Rostedte4906ef2009-04-30 20:49:44 -04001588 local_inc(&cpu_buffer->entries);
Steven Rostedtbf41a152008-10-04 02:00:59 -04001589
1590 /* Only process further if we own the commit */
1591 if (!rb_is_commit(cpu_buffer, event))
1592 return;
1593
1594 cpu_buffer->write_stamp += event->time_delta;
1595
1596 rb_set_commit_to_write(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001597}
1598
1599/**
1600 * ring_buffer_unlock_commit - commit a reserved
1601 * @buffer: The buffer to commit to
1602 * @event: The event pointer to commit.
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001603 *
1604 * This commits the data to the ring buffer, and releases any locks held.
1605 *
1606 * Must be paired with ring_buffer_lock_reserve.
1607 */
1608int ring_buffer_unlock_commit(struct ring_buffer *buffer,
Arnaldo Carvalho de Melo0a987752009-02-05 16:12:56 -02001609 struct ring_buffer_event *event)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001610{
1611 struct ring_buffer_per_cpu *cpu_buffer;
1612 int cpu = raw_smp_processor_id();
1613
1614 cpu_buffer = buffer->buffers[cpu];
1615
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001616 rb_commit(cpu_buffer, event);
1617
Steven Rostedt261842b2009-04-16 21:41:52 -04001618 trace_recursive_unlock();
1619
Steven Rostedtbf41a152008-10-04 02:00:59 -04001620 /*
1621 * Only the last preempt count needs to restore preemption.
1622 */
Steven Rostedt182e9f52008-11-03 23:15:56 -05001623 if (preempt_count() == 1)
1624 ftrace_preempt_enable(per_cpu(rb_need_resched, cpu));
1625 else
Steven Rostedtbf41a152008-10-04 02:00:59 -04001626 preempt_enable_no_resched_notrace();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001627
1628 return 0;
1629}
Robert Richterc4f50182008-12-11 16:49:22 +01001630EXPORT_SYMBOL_GPL(ring_buffer_unlock_commit);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001631
Frederic Weisbeckerf3b9aae2009-04-19 23:39:33 +02001632static inline void rb_event_discard(struct ring_buffer_event *event)
1633{
Lai Jiangshan334d4162009-04-24 11:27:05 +08001634 /* array[0] holds the actual length for the discarded event */
1635 event->array[0] = rb_event_data_length(event) - RB_EVNT_HDR_SIZE;
1636 event->type_len = RINGBUF_TYPE_PADDING;
Frederic Weisbeckerf3b9aae2009-04-19 23:39:33 +02001637 /* time delta must be non zero */
1638 if (!event->time_delta)
1639 event->time_delta = 1;
1640}
1641
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001642/**
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04001643 * ring_buffer_event_discard - discard any event in the ring buffer
1644 * @event: the event to discard
1645 *
1646 * Sometimes a event that is in the ring buffer needs to be ignored.
1647 * This function lets the user discard an event in the ring buffer
1648 * and then that event will not be read later.
1649 *
1650 * Note, it is up to the user to be careful with this, and protect
1651 * against races. If the user discards an event that has been consumed
1652 * it is possible that it could corrupt the ring buffer.
1653 */
1654void ring_buffer_event_discard(struct ring_buffer_event *event)
1655{
Frederic Weisbeckerf3b9aae2009-04-19 23:39:33 +02001656 rb_event_discard(event);
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04001657}
1658EXPORT_SYMBOL_GPL(ring_buffer_event_discard);
1659
1660/**
1661 * ring_buffer_commit_discard - discard an event that has not been committed
1662 * @buffer: the ring buffer
1663 * @event: non committed event to discard
1664 *
1665 * This is similar to ring_buffer_event_discard but must only be
1666 * performed on an event that has not been committed yet. The difference
1667 * is that this will also try to free the event from the ring buffer
1668 * if another event has not been added behind it.
1669 *
1670 * If another event has been added behind it, it will set the event
1671 * up as discarded, and perform the commit.
1672 *
1673 * If this function is called, do not call ring_buffer_unlock_commit on
1674 * the event.
1675 */
1676void ring_buffer_discard_commit(struct ring_buffer *buffer,
1677 struct ring_buffer_event *event)
1678{
1679 struct ring_buffer_per_cpu *cpu_buffer;
1680 unsigned long new_index, old_index;
1681 struct buffer_page *bpage;
1682 unsigned long index;
1683 unsigned long addr;
1684 int cpu;
1685
1686 /* The event is discarded regardless */
Frederic Weisbeckerf3b9aae2009-04-19 23:39:33 +02001687 rb_event_discard(event);
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04001688
1689 /*
1690 * This must only be called if the event has not been
1691 * committed yet. Thus we can assume that preemption
1692 * is still disabled.
1693 */
Steven Rostedt74f4fd22009-05-07 19:58:55 -04001694 RB_WARN_ON(buffer, preemptible());
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04001695
1696 cpu = smp_processor_id();
1697 cpu_buffer = buffer->buffers[cpu];
1698
1699 new_index = rb_event_index(event);
1700 old_index = new_index + rb_event_length(event);
1701 addr = (unsigned long)event;
1702 addr &= PAGE_MASK;
1703
1704 bpage = cpu_buffer->tail_page;
1705
1706 if (bpage == (void *)addr && rb_page_write(bpage) == old_index) {
1707 /*
1708 * This is on the tail page. It is possible that
1709 * a write could come in and move the tail page
1710 * and write to the next page. That is fine
1711 * because we just shorten what is on this page.
1712 */
1713 index = local_cmpxchg(&bpage->write, old_index, new_index);
1714 if (index == old_index)
1715 goto out;
1716 }
1717
1718 /*
1719 * The commit is still visible by the reader, so we
1720 * must increment entries.
1721 */
Steven Rostedte4906ef2009-04-30 20:49:44 -04001722 local_inc(&cpu_buffer->entries);
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04001723 out:
1724 /*
1725 * If a write came in and pushed the tail page
1726 * we still need to update the commit pointer
1727 * if we were the commit.
1728 */
1729 if (rb_is_commit(cpu_buffer, event))
1730 rb_set_commit_to_write(cpu_buffer);
1731
Frederic Weisbeckerf3b9aae2009-04-19 23:39:33 +02001732 trace_recursive_unlock();
1733
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04001734 /*
1735 * Only the last preempt count needs to restore preemption.
1736 */
1737 if (preempt_count() == 1)
1738 ftrace_preempt_enable(per_cpu(rb_need_resched, cpu));
1739 else
1740 preempt_enable_no_resched_notrace();
1741
1742}
1743EXPORT_SYMBOL_GPL(ring_buffer_discard_commit);
1744
1745/**
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001746 * ring_buffer_write - write data to the buffer without reserving
1747 * @buffer: The ring buffer to write to.
1748 * @length: The length of the data being written (excluding the event header)
1749 * @data: The data to write to the buffer.
1750 *
1751 * This is like ring_buffer_lock_reserve and ring_buffer_unlock_commit as
1752 * one function. If you already have the data to write to the buffer, it
1753 * may be easier to simply call this function.
1754 *
1755 * Note, like ring_buffer_lock_reserve, the length is the length of the data
1756 * and not the length of the event which would hold the header.
1757 */
1758int ring_buffer_write(struct ring_buffer *buffer,
1759 unsigned long length,
1760 void *data)
1761{
1762 struct ring_buffer_per_cpu *cpu_buffer;
1763 struct ring_buffer_event *event;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001764 void *body;
1765 int ret = -EBUSY;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001766 int cpu, resched;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001767
Steven Rostedt033601a2008-11-21 12:41:55 -05001768 if (ring_buffer_flags != RB_BUFFERS_ON)
Steven Rostedta3583242008-11-11 15:01:42 -05001769 return -EBUSY;
1770
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001771 if (atomic_read(&buffer->record_disabled))
1772 return -EBUSY;
1773
Steven Rostedt182e9f52008-11-03 23:15:56 -05001774 resched = ftrace_preempt_disable();
Steven Rostedtbf41a152008-10-04 02:00:59 -04001775
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001776 cpu = raw_smp_processor_id();
1777
Rusty Russell9e01c1b2009-01-01 10:12:22 +10301778 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedtd7690412008-10-01 00:29:53 -04001779 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001780
1781 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001782
1783 if (atomic_read(&cpu_buffer->record_disabled))
1784 goto out;
1785
Steven Rostedtbe957c42009-05-11 14:42:53 -04001786 if (length > BUF_MAX_DATA_SIZE)
1787 goto out;
1788
1789 event = rb_reserve_next_event(cpu_buffer, length);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001790 if (!event)
1791 goto out;
1792
1793 body = rb_event_data(event);
1794
1795 memcpy(body, data, length);
1796
1797 rb_commit(cpu_buffer, event);
1798
1799 ret = 0;
1800 out:
Steven Rostedt182e9f52008-11-03 23:15:56 -05001801 ftrace_preempt_enable(resched);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001802
1803 return ret;
1804}
Robert Richterc4f50182008-12-11 16:49:22 +01001805EXPORT_SYMBOL_GPL(ring_buffer_write);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001806
Andrew Morton34a148b2009-01-09 12:27:09 -08001807static int rb_per_cpu_empty(struct ring_buffer_per_cpu *cpu_buffer)
Steven Rostedtbf41a152008-10-04 02:00:59 -04001808{
1809 struct buffer_page *reader = cpu_buffer->reader_page;
1810 struct buffer_page *head = cpu_buffer->head_page;
1811 struct buffer_page *commit = cpu_buffer->commit_page;
1812
1813 return reader->read == rb_page_commit(reader) &&
1814 (commit == reader ||
1815 (commit == head &&
1816 head->read == rb_page_commit(commit)));
1817}
1818
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001819/**
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001820 * ring_buffer_record_disable - stop all writes into the buffer
1821 * @buffer: The ring buffer to stop writes to.
1822 *
1823 * This prevents all writes to the buffer. Any attempt to write
1824 * to the buffer after this will fail and return NULL.
1825 *
1826 * The caller should call synchronize_sched() after this.
1827 */
1828void ring_buffer_record_disable(struct ring_buffer *buffer)
1829{
1830 atomic_inc(&buffer->record_disabled);
1831}
Robert Richterc4f50182008-12-11 16:49:22 +01001832EXPORT_SYMBOL_GPL(ring_buffer_record_disable);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001833
1834/**
1835 * ring_buffer_record_enable - enable writes to the buffer
1836 * @buffer: The ring buffer to enable writes
1837 *
1838 * Note, multiple disables will need the same number of enables
1839 * to truely enable the writing (much like preempt_disable).
1840 */
1841void ring_buffer_record_enable(struct ring_buffer *buffer)
1842{
1843 atomic_dec(&buffer->record_disabled);
1844}
Robert Richterc4f50182008-12-11 16:49:22 +01001845EXPORT_SYMBOL_GPL(ring_buffer_record_enable);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001846
1847/**
1848 * ring_buffer_record_disable_cpu - stop all writes into the cpu_buffer
1849 * @buffer: The ring buffer to stop writes to.
1850 * @cpu: The CPU buffer to stop
1851 *
1852 * This prevents all writes to the buffer. Any attempt to write
1853 * to the buffer after this will fail and return NULL.
1854 *
1855 * The caller should call synchronize_sched() after this.
1856 */
1857void ring_buffer_record_disable_cpu(struct ring_buffer *buffer, int cpu)
1858{
1859 struct ring_buffer_per_cpu *cpu_buffer;
1860
Rusty Russell9e01c1b2009-01-01 10:12:22 +10301861 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04001862 return;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001863
1864 cpu_buffer = buffer->buffers[cpu];
1865 atomic_inc(&cpu_buffer->record_disabled);
1866}
Robert Richterc4f50182008-12-11 16:49:22 +01001867EXPORT_SYMBOL_GPL(ring_buffer_record_disable_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001868
1869/**
1870 * ring_buffer_record_enable_cpu - enable writes to the buffer
1871 * @buffer: The ring buffer to enable writes
1872 * @cpu: The CPU to enable.
1873 *
1874 * Note, multiple disables will need the same number of enables
1875 * to truely enable the writing (much like preempt_disable).
1876 */
1877void ring_buffer_record_enable_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_dec(&cpu_buffer->record_disabled);
1886}
Robert Richterc4f50182008-12-11 16:49:22 +01001887EXPORT_SYMBOL_GPL(ring_buffer_record_enable_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001888
1889/**
1890 * ring_buffer_entries_cpu - get the number of entries in a cpu buffer
1891 * @buffer: The ring buffer
1892 * @cpu: The per CPU buffer to get the entries from.
1893 */
1894unsigned long ring_buffer_entries_cpu(struct ring_buffer *buffer, int cpu)
1895{
1896 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedt8aabee52009-03-12 13:13:49 -04001897 unsigned long ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001898
Rusty Russell9e01c1b2009-01-01 10:12:22 +10301899 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04001900 return 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001901
1902 cpu_buffer = buffer->buffers[cpu];
Steven Rostedte4906ef2009-04-30 20:49:44 -04001903 ret = (local_read(&cpu_buffer->entries) - cpu_buffer->overrun)
1904 - cpu_buffer->read;
Steven Rostedt554f7862009-03-11 22:00:13 -04001905
1906 return ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001907}
Robert Richterc4f50182008-12-11 16:49:22 +01001908EXPORT_SYMBOL_GPL(ring_buffer_entries_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001909
1910/**
1911 * ring_buffer_overrun_cpu - get the number of overruns in a cpu_buffer
1912 * @buffer: The ring buffer
1913 * @cpu: The per CPU buffer to get the number of overruns from
1914 */
1915unsigned long ring_buffer_overrun_cpu(struct ring_buffer *buffer, int cpu)
1916{
1917 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedt8aabee52009-03-12 13:13:49 -04001918 unsigned long ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001919
Rusty Russell9e01c1b2009-01-01 10:12:22 +10301920 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04001921 return 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001922
1923 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt554f7862009-03-11 22:00:13 -04001924 ret = cpu_buffer->overrun;
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_overrun_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001929
1930/**
Steven Rostedtf0d2c682009-04-29 13:43:37 -04001931 * ring_buffer_nmi_dropped_cpu - get the number of nmis that were dropped
1932 * @buffer: The ring buffer
1933 * @cpu: The per CPU buffer to get the number of overruns from
1934 */
1935unsigned long ring_buffer_nmi_dropped_cpu(struct ring_buffer *buffer, int cpu)
1936{
1937 struct ring_buffer_per_cpu *cpu_buffer;
1938 unsigned long ret;
1939
1940 if (!cpumask_test_cpu(cpu, buffer->cpumask))
1941 return 0;
1942
1943 cpu_buffer = buffer->buffers[cpu];
1944 ret = cpu_buffer->nmi_dropped;
1945
1946 return ret;
1947}
1948EXPORT_SYMBOL_GPL(ring_buffer_nmi_dropped_cpu);
1949
1950/**
1951 * ring_buffer_commit_overrun_cpu - get the number of overruns caused by commits
1952 * @buffer: The ring buffer
1953 * @cpu: The per CPU buffer to get the number of overruns from
1954 */
1955unsigned long
1956ring_buffer_commit_overrun_cpu(struct ring_buffer *buffer, int cpu)
1957{
1958 struct ring_buffer_per_cpu *cpu_buffer;
1959 unsigned long ret;
1960
1961 if (!cpumask_test_cpu(cpu, buffer->cpumask))
1962 return 0;
1963
1964 cpu_buffer = buffer->buffers[cpu];
1965 ret = cpu_buffer->commit_overrun;
1966
1967 return ret;
1968}
1969EXPORT_SYMBOL_GPL(ring_buffer_commit_overrun_cpu);
1970
1971/**
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001972 * ring_buffer_entries - get the number of entries in a buffer
1973 * @buffer: The ring buffer
1974 *
1975 * Returns the total number of entries in the ring buffer
1976 * (all CPU entries)
1977 */
1978unsigned long ring_buffer_entries(struct ring_buffer *buffer)
1979{
1980 struct ring_buffer_per_cpu *cpu_buffer;
1981 unsigned long entries = 0;
1982 int cpu;
1983
1984 /* if you care about this being correct, lock the buffer */
1985 for_each_buffer_cpu(buffer, cpu) {
1986 cpu_buffer = buffer->buffers[cpu];
Steven Rostedte4906ef2009-04-30 20:49:44 -04001987 entries += (local_read(&cpu_buffer->entries) -
1988 cpu_buffer->overrun) - cpu_buffer->read;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001989 }
1990
1991 return entries;
1992}
Robert Richterc4f50182008-12-11 16:49:22 +01001993EXPORT_SYMBOL_GPL(ring_buffer_entries);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001994
1995/**
1996 * ring_buffer_overrun_cpu - get the number of overruns in buffer
1997 * @buffer: The ring buffer
1998 *
1999 * Returns the total number of overruns in the ring buffer
2000 * (all CPU entries)
2001 */
2002unsigned long ring_buffer_overruns(struct ring_buffer *buffer)
2003{
2004 struct ring_buffer_per_cpu *cpu_buffer;
2005 unsigned long overruns = 0;
2006 int cpu;
2007
2008 /* if you care about this being correct, lock the buffer */
2009 for_each_buffer_cpu(buffer, cpu) {
2010 cpu_buffer = buffer->buffers[cpu];
2011 overruns += cpu_buffer->overrun;
2012 }
2013
2014 return overruns;
2015}
Robert Richterc4f50182008-12-11 16:49:22 +01002016EXPORT_SYMBOL_GPL(ring_buffer_overruns);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002017
Steven Rostedt642edba2008-11-12 00:01:26 -05002018static void rb_iter_reset(struct ring_buffer_iter *iter)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002019{
2020 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
2021
Steven Rostedtd7690412008-10-01 00:29:53 -04002022 /* Iterator usage is expected to have record disabled */
2023 if (list_empty(&cpu_buffer->reader_page->list)) {
2024 iter->head_page = cpu_buffer->head_page;
Steven Rostedt6f807ac2008-10-04 02:00:58 -04002025 iter->head = cpu_buffer->head_page->read;
Steven Rostedtd7690412008-10-01 00:29:53 -04002026 } else {
2027 iter->head_page = cpu_buffer->reader_page;
Steven Rostedt6f807ac2008-10-04 02:00:58 -04002028 iter->head = cpu_buffer->reader_page->read;
Steven Rostedtd7690412008-10-01 00:29:53 -04002029 }
2030 if (iter->head)
2031 iter->read_stamp = cpu_buffer->read_stamp;
2032 else
Steven Rostedtabc9b562008-12-02 15:34:06 -05002033 iter->read_stamp = iter->head_page->page->time_stamp;
Steven Rostedt642edba2008-11-12 00:01:26 -05002034}
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002035
Steven Rostedt642edba2008-11-12 00:01:26 -05002036/**
2037 * ring_buffer_iter_reset - reset an iterator
2038 * @iter: The iterator to reset
2039 *
2040 * Resets the iterator, so that it will start from the beginning
2041 * again.
2042 */
2043void ring_buffer_iter_reset(struct ring_buffer_iter *iter)
2044{
Steven Rostedt554f7862009-03-11 22:00:13 -04002045 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedt642edba2008-11-12 00:01:26 -05002046 unsigned long flags;
2047
Steven Rostedt554f7862009-03-11 22:00:13 -04002048 if (!iter)
2049 return;
2050
2051 cpu_buffer = iter->cpu_buffer;
2052
Steven Rostedt642edba2008-11-12 00:01:26 -05002053 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
2054 rb_iter_reset(iter);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002055 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002056}
Robert Richterc4f50182008-12-11 16:49:22 +01002057EXPORT_SYMBOL_GPL(ring_buffer_iter_reset);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002058
2059/**
2060 * ring_buffer_iter_empty - check if an iterator has no more to read
2061 * @iter: The iterator to check
2062 */
2063int ring_buffer_iter_empty(struct ring_buffer_iter *iter)
2064{
2065 struct ring_buffer_per_cpu *cpu_buffer;
2066
2067 cpu_buffer = iter->cpu_buffer;
2068
Steven Rostedtbf41a152008-10-04 02:00:59 -04002069 return iter->head_page == cpu_buffer->commit_page &&
2070 iter->head == rb_commit_index(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002071}
Robert Richterc4f50182008-12-11 16:49:22 +01002072EXPORT_SYMBOL_GPL(ring_buffer_iter_empty);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002073
2074static void
2075rb_update_read_stamp(struct ring_buffer_per_cpu *cpu_buffer,
2076 struct ring_buffer_event *event)
2077{
2078 u64 delta;
2079
Lai Jiangshan334d4162009-04-24 11:27:05 +08002080 switch (event->type_len) {
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002081 case RINGBUF_TYPE_PADDING:
2082 return;
2083
2084 case RINGBUF_TYPE_TIME_EXTEND:
2085 delta = event->array[0];
2086 delta <<= TS_SHIFT;
2087 delta += event->time_delta;
2088 cpu_buffer->read_stamp += delta;
2089 return;
2090
2091 case RINGBUF_TYPE_TIME_STAMP:
2092 /* FIXME: not implemented */
2093 return;
2094
2095 case RINGBUF_TYPE_DATA:
2096 cpu_buffer->read_stamp += event->time_delta;
2097 return;
2098
2099 default:
2100 BUG();
2101 }
2102 return;
2103}
2104
2105static void
2106rb_update_iter_read_stamp(struct ring_buffer_iter *iter,
2107 struct ring_buffer_event *event)
2108{
2109 u64 delta;
2110
Lai Jiangshan334d4162009-04-24 11:27:05 +08002111 switch (event->type_len) {
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002112 case RINGBUF_TYPE_PADDING:
2113 return;
2114
2115 case RINGBUF_TYPE_TIME_EXTEND:
2116 delta = event->array[0];
2117 delta <<= TS_SHIFT;
2118 delta += event->time_delta;
2119 iter->read_stamp += delta;
2120 return;
2121
2122 case RINGBUF_TYPE_TIME_STAMP:
2123 /* FIXME: not implemented */
2124 return;
2125
2126 case RINGBUF_TYPE_DATA:
2127 iter->read_stamp += event->time_delta;
2128 return;
2129
2130 default:
2131 BUG();
2132 }
2133 return;
2134}
2135
Steven Rostedtd7690412008-10-01 00:29:53 -04002136static struct buffer_page *
2137rb_get_reader_page(struct ring_buffer_per_cpu *cpu_buffer)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002138{
Steven Rostedtd7690412008-10-01 00:29:53 -04002139 struct buffer_page *reader = NULL;
2140 unsigned long flags;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002141 int nr_loops = 0;
Steven Rostedtd7690412008-10-01 00:29:53 -04002142
Steven Rostedt3e03fb72008-11-06 00:09:43 -05002143 local_irq_save(flags);
2144 __raw_spin_lock(&cpu_buffer->lock);
Steven Rostedtd7690412008-10-01 00:29:53 -04002145
2146 again:
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002147 /*
2148 * This should normally only loop twice. But because the
2149 * start of the reader inserts an empty page, it causes
2150 * a case where we will loop three times. There should be no
2151 * reason to loop four times (that I know of).
2152 */
Steven Rostedt3e89c7bb2008-11-11 15:28:41 -05002153 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 3)) {
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002154 reader = NULL;
2155 goto out;
2156 }
2157
Steven Rostedtd7690412008-10-01 00:29:53 -04002158 reader = cpu_buffer->reader_page;
2159
2160 /* If there's more to read, return this page */
Steven Rostedtbf41a152008-10-04 02:00:59 -04002161 if (cpu_buffer->reader_page->read < rb_page_size(reader))
Steven Rostedtd7690412008-10-01 00:29:53 -04002162 goto out;
2163
2164 /* Never should we have an index greater than the size */
Steven Rostedt3e89c7bb2008-11-11 15:28:41 -05002165 if (RB_WARN_ON(cpu_buffer,
2166 cpu_buffer->reader_page->read > rb_page_size(reader)))
2167 goto out;
Steven Rostedtd7690412008-10-01 00:29:53 -04002168
2169 /* check if we caught up to the tail */
2170 reader = NULL;
Steven Rostedtbf41a152008-10-04 02:00:59 -04002171 if (cpu_buffer->commit_page == cpu_buffer->reader_page)
Steven Rostedtd7690412008-10-01 00:29:53 -04002172 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002173
2174 /*
Steven Rostedtd7690412008-10-01 00:29:53 -04002175 * Splice the empty reader page into the list around the head.
2176 * Reset the reader page to size zero.
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002177 */
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002178
Steven Rostedtd7690412008-10-01 00:29:53 -04002179 reader = cpu_buffer->head_page;
2180 cpu_buffer->reader_page->list.next = reader->list.next;
2181 cpu_buffer->reader_page->list.prev = reader->list.prev;
Steven Rostedtbf41a152008-10-04 02:00:59 -04002182
2183 local_set(&cpu_buffer->reader_page->write, 0);
Steven Rostedt778c55d2009-05-01 18:44:45 -04002184 local_set(&cpu_buffer->reader_page->entries, 0);
Steven Rostedtabc9b562008-12-02 15:34:06 -05002185 local_set(&cpu_buffer->reader_page->page->commit, 0);
Steven Rostedtd7690412008-10-01 00:29:53 -04002186
2187 /* Make the reader page now replace the head */
2188 reader->list.prev->next = &cpu_buffer->reader_page->list;
2189 reader->list.next->prev = &cpu_buffer->reader_page->list;
2190
2191 /*
2192 * If the tail is on the reader, then we must set the head
2193 * to the inserted page, otherwise we set it one before.
2194 */
2195 cpu_buffer->head_page = cpu_buffer->reader_page;
2196
Steven Rostedtbf41a152008-10-04 02:00:59 -04002197 if (cpu_buffer->commit_page != reader)
Steven Rostedtd7690412008-10-01 00:29:53 -04002198 rb_inc_page(cpu_buffer, &cpu_buffer->head_page);
2199
2200 /* Finally update the reader page to the new head */
2201 cpu_buffer->reader_page = reader;
2202 rb_reset_reader_page(cpu_buffer);
2203
2204 goto again;
2205
2206 out:
Steven Rostedt3e03fb72008-11-06 00:09:43 -05002207 __raw_spin_unlock(&cpu_buffer->lock);
2208 local_irq_restore(flags);
Steven Rostedtd7690412008-10-01 00:29:53 -04002209
2210 return reader;
2211}
2212
2213static void rb_advance_reader(struct ring_buffer_per_cpu *cpu_buffer)
2214{
2215 struct ring_buffer_event *event;
2216 struct buffer_page *reader;
2217 unsigned length;
2218
2219 reader = rb_get_reader_page(cpu_buffer);
2220
2221 /* This function should not be called when buffer is empty */
Steven Rostedt3e89c7bb2008-11-11 15:28:41 -05002222 if (RB_WARN_ON(cpu_buffer, !reader))
2223 return;
Steven Rostedtd7690412008-10-01 00:29:53 -04002224
2225 event = rb_reader_event(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002226
Lai Jiangshan334d4162009-04-24 11:27:05 +08002227 if (event->type_len <= RINGBUF_TYPE_DATA_TYPE_LEN_MAX
2228 || rb_discarded_event(event))
Steven Rostedte4906ef2009-04-30 20:49:44 -04002229 cpu_buffer->read++;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002230
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002231 rb_update_read_stamp(cpu_buffer, event);
2232
Steven Rostedtd7690412008-10-01 00:29:53 -04002233 length = rb_event_length(event);
Steven Rostedt6f807ac2008-10-04 02:00:58 -04002234 cpu_buffer->reader_page->read += length;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002235}
2236
2237static void rb_advance_iter(struct ring_buffer_iter *iter)
2238{
2239 struct ring_buffer *buffer;
2240 struct ring_buffer_per_cpu *cpu_buffer;
2241 struct ring_buffer_event *event;
2242 unsigned length;
2243
2244 cpu_buffer = iter->cpu_buffer;
2245 buffer = cpu_buffer->buffer;
2246
2247 /*
2248 * Check if we are at the end of the buffer.
2249 */
Steven Rostedtbf41a152008-10-04 02:00:59 -04002250 if (iter->head >= rb_page_size(iter->head_page)) {
Steven Rostedt3e89c7bb2008-11-11 15:28:41 -05002251 if (RB_WARN_ON(buffer,
2252 iter->head_page == cpu_buffer->commit_page))
2253 return;
Steven Rostedtd7690412008-10-01 00:29:53 -04002254 rb_inc_iter(iter);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002255 return;
2256 }
2257
2258 event = rb_iter_head_event(iter);
2259
2260 length = rb_event_length(event);
2261
2262 /*
2263 * This should not be called to advance the header if we are
2264 * at the tail of the buffer.
2265 */
Steven Rostedt3e89c7bb2008-11-11 15:28:41 -05002266 if (RB_WARN_ON(cpu_buffer,
Steven Rostedtf536aaf2008-11-10 23:07:30 -05002267 (iter->head_page == cpu_buffer->commit_page) &&
Steven Rostedt3e89c7bb2008-11-11 15:28:41 -05002268 (iter->head + length > rb_commit_index(cpu_buffer))))
2269 return;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002270
2271 rb_update_iter_read_stamp(iter, event);
2272
2273 iter->head += length;
2274
2275 /* check for end of page padding */
Steven Rostedtbf41a152008-10-04 02:00:59 -04002276 if ((iter->head >= rb_page_size(iter->head_page)) &&
2277 (iter->head_page != cpu_buffer->commit_page))
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002278 rb_advance_iter(iter);
2279}
2280
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002281static struct ring_buffer_event *
2282rb_buffer_peek(struct ring_buffer *buffer, int cpu, u64 *ts)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002283{
2284 struct ring_buffer_per_cpu *cpu_buffer;
2285 struct ring_buffer_event *event;
Steven Rostedtd7690412008-10-01 00:29:53 -04002286 struct buffer_page *reader;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002287 int nr_loops = 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002288
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002289 cpu_buffer = buffer->buffers[cpu];
2290
2291 again:
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002292 /*
2293 * We repeat when a timestamp is encountered. It is possible
2294 * to get multiple timestamps from an interrupt entering just
2295 * as one timestamp is about to be written. The max times
2296 * that this can happen is the number of nested interrupts we
2297 * can have. Nesting 10 deep of interrupts is clearly
2298 * an anomaly.
2299 */
Steven Rostedt3e89c7bb2008-11-11 15:28:41 -05002300 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 10))
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002301 return NULL;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002302
Steven Rostedtd7690412008-10-01 00:29:53 -04002303 reader = rb_get_reader_page(cpu_buffer);
2304 if (!reader)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002305 return NULL;
2306
Steven Rostedtd7690412008-10-01 00:29:53 -04002307 event = rb_reader_event(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002308
Lai Jiangshan334d4162009-04-24 11:27:05 +08002309 switch (event->type_len) {
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002310 case RINGBUF_TYPE_PADDING:
Tom Zanussi2d622712009-03-22 03:30:49 -05002311 if (rb_null_event(event))
2312 RB_WARN_ON(cpu_buffer, 1);
2313 /*
2314 * Because the writer could be discarding every
2315 * event it creates (which would probably be bad)
2316 * if we were to go back to "again" then we may never
2317 * catch up, and will trigger the warn on, or lock
2318 * the box. Return the padding, and we will release
2319 * the current locks, and try again.
2320 */
Steven Rostedtd7690412008-10-01 00:29:53 -04002321 rb_advance_reader(cpu_buffer);
Tom Zanussi2d622712009-03-22 03:30:49 -05002322 return event;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002323
2324 case RINGBUF_TYPE_TIME_EXTEND:
2325 /* Internal data, OK to advance */
Steven Rostedtd7690412008-10-01 00:29:53 -04002326 rb_advance_reader(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002327 goto again;
2328
2329 case RINGBUF_TYPE_TIME_STAMP:
2330 /* FIXME: not implemented */
Steven Rostedtd7690412008-10-01 00:29:53 -04002331 rb_advance_reader(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002332 goto again;
2333
2334 case RINGBUF_TYPE_DATA:
2335 if (ts) {
2336 *ts = cpu_buffer->read_stamp + event->time_delta;
Steven Rostedt37886f62009-03-17 17:22:06 -04002337 ring_buffer_normalize_time_stamp(buffer,
2338 cpu_buffer->cpu, ts);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002339 }
2340 return event;
2341
2342 default:
2343 BUG();
2344 }
2345
2346 return NULL;
2347}
Robert Richterc4f50182008-12-11 16:49:22 +01002348EXPORT_SYMBOL_GPL(ring_buffer_peek);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002349
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002350static struct ring_buffer_event *
2351rb_iter_peek(struct ring_buffer_iter *iter, u64 *ts)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002352{
2353 struct ring_buffer *buffer;
2354 struct ring_buffer_per_cpu *cpu_buffer;
2355 struct ring_buffer_event *event;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002356 int nr_loops = 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002357
2358 if (ring_buffer_iter_empty(iter))
2359 return NULL;
2360
2361 cpu_buffer = iter->cpu_buffer;
2362 buffer = cpu_buffer->buffer;
2363
2364 again:
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002365 /*
2366 * We repeat when a timestamp is encountered. It is possible
2367 * to get multiple timestamps from an interrupt entering just
2368 * as one timestamp is about to be written. The max times
2369 * that this can happen is the number of nested interrupts we
2370 * can have. Nesting 10 deep of interrupts is clearly
2371 * an anomaly.
2372 */
Steven Rostedt3e89c7bb2008-11-11 15:28:41 -05002373 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 10))
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002374 return NULL;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002375
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002376 if (rb_per_cpu_empty(cpu_buffer))
2377 return NULL;
2378
2379 event = rb_iter_head_event(iter);
2380
Lai Jiangshan334d4162009-04-24 11:27:05 +08002381 switch (event->type_len) {
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002382 case RINGBUF_TYPE_PADDING:
Tom Zanussi2d622712009-03-22 03:30:49 -05002383 if (rb_null_event(event)) {
2384 rb_inc_iter(iter);
2385 goto again;
2386 }
2387 rb_advance_iter(iter);
2388 return event;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002389
2390 case RINGBUF_TYPE_TIME_EXTEND:
2391 /* Internal data, OK to advance */
2392 rb_advance_iter(iter);
2393 goto again;
2394
2395 case RINGBUF_TYPE_TIME_STAMP:
2396 /* FIXME: not implemented */
2397 rb_advance_iter(iter);
2398 goto again;
2399
2400 case RINGBUF_TYPE_DATA:
2401 if (ts) {
2402 *ts = iter->read_stamp + event->time_delta;
Steven Rostedt37886f62009-03-17 17:22:06 -04002403 ring_buffer_normalize_time_stamp(buffer,
2404 cpu_buffer->cpu, ts);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002405 }
2406 return event;
2407
2408 default:
2409 BUG();
2410 }
2411
2412 return NULL;
2413}
Robert Richterc4f50182008-12-11 16:49:22 +01002414EXPORT_SYMBOL_GPL(ring_buffer_iter_peek);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002415
2416/**
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002417 * ring_buffer_peek - peek at the next event to be read
2418 * @buffer: The ring buffer to read
2419 * @cpu: The cpu to peak at
2420 * @ts: The timestamp counter of this event.
2421 *
2422 * This will return the event that will be read next, but does
2423 * not consume the data.
2424 */
2425struct ring_buffer_event *
2426ring_buffer_peek(struct ring_buffer *buffer, int cpu, u64 *ts)
2427{
2428 struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
Steven Rostedt8aabee52009-03-12 13:13:49 -04002429 struct ring_buffer_event *event;
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002430 unsigned long flags;
2431
Steven Rostedt554f7862009-03-11 22:00:13 -04002432 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04002433 return NULL;
Steven Rostedt554f7862009-03-11 22:00:13 -04002434
Tom Zanussi2d622712009-03-22 03:30:49 -05002435 again:
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002436 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
2437 event = rb_buffer_peek(buffer, cpu, ts);
2438 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
2439
Lai Jiangshan334d4162009-04-24 11:27:05 +08002440 if (event && event->type_len == RINGBUF_TYPE_PADDING) {
Tom Zanussi2d622712009-03-22 03:30:49 -05002441 cpu_relax();
2442 goto again;
2443 }
2444
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002445 return event;
2446}
2447
2448/**
2449 * ring_buffer_iter_peek - peek at the next event to be read
2450 * @iter: The ring buffer iterator
2451 * @ts: The timestamp counter of this event.
2452 *
2453 * This will return the event that will be read next, but does
2454 * not increment the iterator.
2455 */
2456struct ring_buffer_event *
2457ring_buffer_iter_peek(struct ring_buffer_iter *iter, u64 *ts)
2458{
2459 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
2460 struct ring_buffer_event *event;
2461 unsigned long flags;
2462
Tom Zanussi2d622712009-03-22 03:30:49 -05002463 again:
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002464 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
2465 event = rb_iter_peek(iter, ts);
2466 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
2467
Lai Jiangshan334d4162009-04-24 11:27:05 +08002468 if (event && event->type_len == RINGBUF_TYPE_PADDING) {
Tom Zanussi2d622712009-03-22 03:30:49 -05002469 cpu_relax();
2470 goto again;
2471 }
2472
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002473 return event;
2474}
2475
2476/**
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002477 * ring_buffer_consume - return an event and consume it
2478 * @buffer: The ring buffer to get the next event from
2479 *
2480 * Returns the next event in the ring buffer, and that event is consumed.
2481 * Meaning, that sequential reads will keep returning a different event,
2482 * and eventually empty the ring buffer if the producer is slower.
2483 */
2484struct ring_buffer_event *
2485ring_buffer_consume(struct ring_buffer *buffer, int cpu, u64 *ts)
2486{
Steven Rostedt554f7862009-03-11 22:00:13 -04002487 struct ring_buffer_per_cpu *cpu_buffer;
2488 struct ring_buffer_event *event = NULL;
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002489 unsigned long flags;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002490
Tom Zanussi2d622712009-03-22 03:30:49 -05002491 again:
Steven Rostedt554f7862009-03-11 22:00:13 -04002492 /* might be called in atomic */
2493 preempt_disable();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002494
Steven Rostedt554f7862009-03-11 22:00:13 -04002495 if (!cpumask_test_cpu(cpu, buffer->cpumask))
2496 goto out;
2497
2498 cpu_buffer = buffer->buffers[cpu];
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002499 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002500
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002501 event = rb_buffer_peek(buffer, cpu, ts);
2502 if (!event)
Steven Rostedt554f7862009-03-11 22:00:13 -04002503 goto out_unlock;
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002504
Steven Rostedtd7690412008-10-01 00:29:53 -04002505 rb_advance_reader(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002506
Steven Rostedt554f7862009-03-11 22:00:13 -04002507 out_unlock:
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002508 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
2509
Steven Rostedt554f7862009-03-11 22:00:13 -04002510 out:
2511 preempt_enable();
2512
Lai Jiangshan334d4162009-04-24 11:27:05 +08002513 if (event && event->type_len == RINGBUF_TYPE_PADDING) {
Tom Zanussi2d622712009-03-22 03:30:49 -05002514 cpu_relax();
2515 goto again;
2516 }
2517
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002518 return event;
2519}
Robert Richterc4f50182008-12-11 16:49:22 +01002520EXPORT_SYMBOL_GPL(ring_buffer_consume);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002521
2522/**
2523 * ring_buffer_read_start - start a non consuming read of the buffer
2524 * @buffer: The ring buffer to read from
2525 * @cpu: The cpu buffer to iterate over
2526 *
2527 * This starts up an iteration through the buffer. It also disables
2528 * the recording to the buffer until the reading is finished.
2529 * This prevents the reading from being corrupted. This is not
2530 * a consuming read, so a producer is not expected.
2531 *
2532 * Must be paired with ring_buffer_finish.
2533 */
2534struct ring_buffer_iter *
2535ring_buffer_read_start(struct ring_buffer *buffer, int cpu)
2536{
2537 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedt8aabee52009-03-12 13:13:49 -04002538 struct ring_buffer_iter *iter;
Steven Rostedtd7690412008-10-01 00:29:53 -04002539 unsigned long flags;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002540
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302541 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04002542 return NULL;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002543
2544 iter = kmalloc(sizeof(*iter), GFP_KERNEL);
2545 if (!iter)
Steven Rostedt8aabee52009-03-12 13:13:49 -04002546 return NULL;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002547
2548 cpu_buffer = buffer->buffers[cpu];
2549
2550 iter->cpu_buffer = cpu_buffer;
2551
2552 atomic_inc(&cpu_buffer->record_disabled);
2553 synchronize_sched();
2554
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002555 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
Steven Rostedt3e03fb72008-11-06 00:09:43 -05002556 __raw_spin_lock(&cpu_buffer->lock);
Steven Rostedt642edba2008-11-12 00:01:26 -05002557 rb_iter_reset(iter);
Steven Rostedt3e03fb72008-11-06 00:09:43 -05002558 __raw_spin_unlock(&cpu_buffer->lock);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002559 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002560
2561 return iter;
2562}
Robert Richterc4f50182008-12-11 16:49:22 +01002563EXPORT_SYMBOL_GPL(ring_buffer_read_start);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002564
2565/**
2566 * ring_buffer_finish - finish reading the iterator of the buffer
2567 * @iter: The iterator retrieved by ring_buffer_start
2568 *
2569 * This re-enables the recording to the buffer, and frees the
2570 * iterator.
2571 */
2572void
2573ring_buffer_read_finish(struct ring_buffer_iter *iter)
2574{
2575 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
2576
2577 atomic_dec(&cpu_buffer->record_disabled);
2578 kfree(iter);
2579}
Robert Richterc4f50182008-12-11 16:49:22 +01002580EXPORT_SYMBOL_GPL(ring_buffer_read_finish);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002581
2582/**
2583 * ring_buffer_read - read the next item in the ring buffer by the iterator
2584 * @iter: The ring buffer iterator
2585 * @ts: The time stamp of the event read.
2586 *
2587 * This reads the next event in the ring buffer and increments the iterator.
2588 */
2589struct ring_buffer_event *
2590ring_buffer_read(struct ring_buffer_iter *iter, u64 *ts)
2591{
2592 struct ring_buffer_event *event;
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002593 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
2594 unsigned long flags;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002595
Tom Zanussi2d622712009-03-22 03:30:49 -05002596 again:
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002597 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
2598 event = rb_iter_peek(iter, ts);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002599 if (!event)
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002600 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002601
2602 rb_advance_iter(iter);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002603 out:
2604 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002605
Lai Jiangshan334d4162009-04-24 11:27:05 +08002606 if (event && event->type_len == RINGBUF_TYPE_PADDING) {
Tom Zanussi2d622712009-03-22 03:30:49 -05002607 cpu_relax();
2608 goto again;
2609 }
2610
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002611 return event;
2612}
Robert Richterc4f50182008-12-11 16:49:22 +01002613EXPORT_SYMBOL_GPL(ring_buffer_read);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002614
2615/**
2616 * ring_buffer_size - return the size of the ring buffer (in bytes)
2617 * @buffer: The ring buffer.
2618 */
2619unsigned long ring_buffer_size(struct ring_buffer *buffer)
2620{
2621 return BUF_PAGE_SIZE * buffer->pages;
2622}
Robert Richterc4f50182008-12-11 16:49:22 +01002623EXPORT_SYMBOL_GPL(ring_buffer_size);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002624
2625static void
2626rb_reset_cpu(struct ring_buffer_per_cpu *cpu_buffer)
2627{
2628 cpu_buffer->head_page
2629 = list_entry(cpu_buffer->pages.next, struct buffer_page, list);
Steven Rostedtbf41a152008-10-04 02:00:59 -04002630 local_set(&cpu_buffer->head_page->write, 0);
Steven Rostedt778c55d2009-05-01 18:44:45 -04002631 local_set(&cpu_buffer->head_page->entries, 0);
Steven Rostedtabc9b562008-12-02 15:34:06 -05002632 local_set(&cpu_buffer->head_page->page->commit, 0);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002633
Steven Rostedt6f807ac2008-10-04 02:00:58 -04002634 cpu_buffer->head_page->read = 0;
Steven Rostedtbf41a152008-10-04 02:00:59 -04002635
2636 cpu_buffer->tail_page = cpu_buffer->head_page;
2637 cpu_buffer->commit_page = cpu_buffer->head_page;
2638
2639 INIT_LIST_HEAD(&cpu_buffer->reader_page->list);
2640 local_set(&cpu_buffer->reader_page->write, 0);
Steven Rostedt778c55d2009-05-01 18:44:45 -04002641 local_set(&cpu_buffer->reader_page->entries, 0);
Steven Rostedtabc9b562008-12-02 15:34:06 -05002642 local_set(&cpu_buffer->reader_page->page->commit, 0);
Steven Rostedt6f807ac2008-10-04 02:00:58 -04002643 cpu_buffer->reader_page->read = 0;
Steven Rostedtd7690412008-10-01 00:29:53 -04002644
Steven Rostedtf0d2c682009-04-29 13:43:37 -04002645 cpu_buffer->nmi_dropped = 0;
2646 cpu_buffer->commit_overrun = 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002647 cpu_buffer->overrun = 0;
Steven Rostedte4906ef2009-04-30 20:49:44 -04002648 cpu_buffer->read = 0;
2649 local_set(&cpu_buffer->entries, 0);
Steven Rostedt69507c02009-01-21 18:45:57 -05002650
2651 cpu_buffer->write_stamp = 0;
2652 cpu_buffer->read_stamp = 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002653}
2654
2655/**
2656 * ring_buffer_reset_cpu - reset a ring buffer per CPU buffer
2657 * @buffer: The ring buffer to reset a per cpu buffer of
2658 * @cpu: The CPU buffer to be reset
2659 */
2660void ring_buffer_reset_cpu(struct ring_buffer *buffer, int cpu)
2661{
2662 struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
2663 unsigned long flags;
2664
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302665 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04002666 return;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002667
Steven Rostedt41ede232009-05-01 20:26:54 -04002668 atomic_inc(&cpu_buffer->record_disabled);
2669
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002670 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
2671
Steven Rostedt3e03fb72008-11-06 00:09:43 -05002672 __raw_spin_lock(&cpu_buffer->lock);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002673
2674 rb_reset_cpu(cpu_buffer);
2675
Steven Rostedt3e03fb72008-11-06 00:09:43 -05002676 __raw_spin_unlock(&cpu_buffer->lock);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002677
2678 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
Steven Rostedt41ede232009-05-01 20:26:54 -04002679
2680 atomic_dec(&cpu_buffer->record_disabled);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002681}
Robert Richterc4f50182008-12-11 16:49:22 +01002682EXPORT_SYMBOL_GPL(ring_buffer_reset_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002683
2684/**
2685 * ring_buffer_reset - reset a ring buffer
2686 * @buffer: The ring buffer to reset all cpu buffers
2687 */
2688void ring_buffer_reset(struct ring_buffer *buffer)
2689{
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002690 int cpu;
2691
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002692 for_each_buffer_cpu(buffer, cpu)
Steven Rostedtd7690412008-10-01 00:29:53 -04002693 ring_buffer_reset_cpu(buffer, cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002694}
Robert Richterc4f50182008-12-11 16:49:22 +01002695EXPORT_SYMBOL_GPL(ring_buffer_reset);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002696
2697/**
2698 * rind_buffer_empty - is the ring buffer empty?
2699 * @buffer: The ring buffer to test
2700 */
2701int ring_buffer_empty(struct ring_buffer *buffer)
2702{
2703 struct ring_buffer_per_cpu *cpu_buffer;
2704 int cpu;
2705
2706 /* yes this is racy, but if you don't like the race, lock the buffer */
2707 for_each_buffer_cpu(buffer, cpu) {
2708 cpu_buffer = buffer->buffers[cpu];
2709 if (!rb_per_cpu_empty(cpu_buffer))
2710 return 0;
2711 }
Steven Rostedt554f7862009-03-11 22:00:13 -04002712
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002713 return 1;
2714}
Robert Richterc4f50182008-12-11 16:49:22 +01002715EXPORT_SYMBOL_GPL(ring_buffer_empty);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002716
2717/**
2718 * ring_buffer_empty_cpu - is a cpu buffer of a ring buffer empty?
2719 * @buffer: The ring buffer
2720 * @cpu: The CPU buffer to test
2721 */
2722int ring_buffer_empty_cpu(struct ring_buffer *buffer, int cpu)
2723{
2724 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedt8aabee52009-03-12 13:13:49 -04002725 int ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002726
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302727 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04002728 return 1;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002729
2730 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt554f7862009-03-11 22:00:13 -04002731 ret = rb_per_cpu_empty(cpu_buffer);
2732
Steven Rostedt554f7862009-03-11 22:00:13 -04002733
2734 return ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002735}
Robert Richterc4f50182008-12-11 16:49:22 +01002736EXPORT_SYMBOL_GPL(ring_buffer_empty_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002737
2738/**
2739 * ring_buffer_swap_cpu - swap a CPU buffer between two ring buffers
2740 * @buffer_a: One buffer to swap with
2741 * @buffer_b: The other buffer to swap with
2742 *
2743 * This function is useful for tracers that want to take a "snapshot"
2744 * of a CPU buffer and has another back up buffer lying around.
2745 * it is expected that the tracer handles the cpu buffer not being
2746 * used at the moment.
2747 */
2748int ring_buffer_swap_cpu(struct ring_buffer *buffer_a,
2749 struct ring_buffer *buffer_b, int cpu)
2750{
2751 struct ring_buffer_per_cpu *cpu_buffer_a;
2752 struct ring_buffer_per_cpu *cpu_buffer_b;
Steven Rostedt554f7862009-03-11 22:00:13 -04002753 int ret = -EINVAL;
2754
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302755 if (!cpumask_test_cpu(cpu, buffer_a->cpumask) ||
2756 !cpumask_test_cpu(cpu, buffer_b->cpumask))
Steven Rostedt554f7862009-03-11 22:00:13 -04002757 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002758
2759 /* At least make sure the two buffers are somewhat the same */
Lai Jiangshan6d102bc2008-12-17 17:48:23 +08002760 if (buffer_a->pages != buffer_b->pages)
Steven Rostedt554f7862009-03-11 22:00:13 -04002761 goto out;
2762
2763 ret = -EAGAIN;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002764
Steven Rostedt97b17ef2009-01-21 15:24:56 -05002765 if (ring_buffer_flags != RB_BUFFERS_ON)
Steven Rostedt554f7862009-03-11 22:00:13 -04002766 goto out;
Steven Rostedt97b17ef2009-01-21 15:24:56 -05002767
2768 if (atomic_read(&buffer_a->record_disabled))
Steven Rostedt554f7862009-03-11 22:00:13 -04002769 goto out;
Steven Rostedt97b17ef2009-01-21 15:24:56 -05002770
2771 if (atomic_read(&buffer_b->record_disabled))
Steven Rostedt554f7862009-03-11 22:00:13 -04002772 goto out;
Steven Rostedt97b17ef2009-01-21 15:24:56 -05002773
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002774 cpu_buffer_a = buffer_a->buffers[cpu];
2775 cpu_buffer_b = buffer_b->buffers[cpu];
2776
Steven Rostedt97b17ef2009-01-21 15:24:56 -05002777 if (atomic_read(&cpu_buffer_a->record_disabled))
Steven Rostedt554f7862009-03-11 22:00:13 -04002778 goto out;
Steven Rostedt97b17ef2009-01-21 15:24:56 -05002779
2780 if (atomic_read(&cpu_buffer_b->record_disabled))
Steven Rostedt554f7862009-03-11 22:00:13 -04002781 goto out;
Steven Rostedt97b17ef2009-01-21 15:24:56 -05002782
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002783 /*
2784 * We can't do a synchronize_sched here because this
2785 * function can be called in atomic context.
2786 * Normally this will be called from the same CPU as cpu.
2787 * If not it's up to the caller to protect this.
2788 */
2789 atomic_inc(&cpu_buffer_a->record_disabled);
2790 atomic_inc(&cpu_buffer_b->record_disabled);
2791
2792 buffer_a->buffers[cpu] = cpu_buffer_b;
2793 buffer_b->buffers[cpu] = cpu_buffer_a;
2794
2795 cpu_buffer_b->buffer = buffer_a;
2796 cpu_buffer_a->buffer = buffer_b;
2797
2798 atomic_dec(&cpu_buffer_a->record_disabled);
2799 atomic_dec(&cpu_buffer_b->record_disabled);
2800
Steven Rostedt554f7862009-03-11 22:00:13 -04002801 ret = 0;
2802out:
Steven Rostedt554f7862009-03-11 22:00:13 -04002803 return ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002804}
Robert Richterc4f50182008-12-11 16:49:22 +01002805EXPORT_SYMBOL_GPL(ring_buffer_swap_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002806
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002807/**
2808 * ring_buffer_alloc_read_page - allocate a page to read from buffer
2809 * @buffer: the buffer to allocate for.
2810 *
2811 * This function is used in conjunction with ring_buffer_read_page.
2812 * When reading a full page from the ring buffer, these functions
2813 * can be used to speed up the process. The calling function should
2814 * allocate a few pages first with this function. Then when it
2815 * needs to get pages from the ring buffer, it passes the result
2816 * of this function into ring_buffer_read_page, which will swap
2817 * the page that was allocated, with the read page of the buffer.
2818 *
2819 * Returns:
2820 * The page allocated, or NULL on error.
2821 */
2822void *ring_buffer_alloc_read_page(struct ring_buffer *buffer)
2823{
Steven Rostedt044fa782008-12-02 23:50:03 -05002824 struct buffer_data_page *bpage;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05002825 unsigned long addr;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002826
2827 addr = __get_free_page(GFP_KERNEL);
2828 if (!addr)
2829 return NULL;
2830
Steven Rostedt044fa782008-12-02 23:50:03 -05002831 bpage = (void *)addr;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002832
Steven Rostedtef7a4a12009-03-03 00:27:49 -05002833 rb_init_page(bpage);
2834
Steven Rostedt044fa782008-12-02 23:50:03 -05002835 return bpage;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002836}
Steven Rostedtd6ce96d2009-05-05 01:15:24 -04002837EXPORT_SYMBOL_GPL(ring_buffer_alloc_read_page);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002838
2839/**
2840 * ring_buffer_free_read_page - free an allocated read page
2841 * @buffer: the buffer the page was allocate for
2842 * @data: the page to free
2843 *
2844 * Free a page allocated from ring_buffer_alloc_read_page.
2845 */
2846void ring_buffer_free_read_page(struct ring_buffer *buffer, void *data)
2847{
2848 free_page((unsigned long)data);
2849}
Steven Rostedtd6ce96d2009-05-05 01:15:24 -04002850EXPORT_SYMBOL_GPL(ring_buffer_free_read_page);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002851
2852/**
2853 * ring_buffer_read_page - extract a page from the ring buffer
2854 * @buffer: buffer to extract from
2855 * @data_page: the page to use allocated from ring_buffer_alloc_read_page
Steven Rostedtef7a4a12009-03-03 00:27:49 -05002856 * @len: amount to extract
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002857 * @cpu: the cpu of the buffer to extract
2858 * @full: should the extraction only happen when the page is full.
2859 *
2860 * This function will pull out a page from the ring buffer and consume it.
2861 * @data_page must be the address of the variable that was returned
2862 * from ring_buffer_alloc_read_page. This is because the page might be used
2863 * to swap with a page in the ring buffer.
2864 *
2865 * for example:
Lai Jiangshanb85fa012009-02-09 14:21:14 +08002866 * rpage = ring_buffer_alloc_read_page(buffer);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002867 * if (!rpage)
2868 * return error;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05002869 * ret = ring_buffer_read_page(buffer, &rpage, len, cpu, 0);
Lai Jiangshan667d2412009-02-09 14:21:17 +08002870 * if (ret >= 0)
2871 * process_page(rpage, ret);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002872 *
2873 * When @full is set, the function will not return true unless
2874 * the writer is off the reader page.
2875 *
2876 * Note: it is up to the calling functions to handle sleeps and wakeups.
2877 * The ring buffer can be used anywhere in the kernel and can not
2878 * blindly call wake_up. The layer that uses the ring buffer must be
2879 * responsible for that.
2880 *
2881 * Returns:
Lai Jiangshan667d2412009-02-09 14:21:17 +08002882 * >=0 if data has been transferred, returns the offset of consumed data.
2883 * <0 if no data has been transferred.
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002884 */
2885int ring_buffer_read_page(struct ring_buffer *buffer,
Steven Rostedtef7a4a12009-03-03 00:27:49 -05002886 void **data_page, size_t len, int cpu, int full)
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002887{
2888 struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
2889 struct ring_buffer_event *event;
Steven Rostedt044fa782008-12-02 23:50:03 -05002890 struct buffer_data_page *bpage;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05002891 struct buffer_page *reader;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002892 unsigned long flags;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05002893 unsigned int commit;
Lai Jiangshan667d2412009-02-09 14:21:17 +08002894 unsigned int read;
Steven Rostedt4f3640f2009-03-03 23:52:42 -05002895 u64 save_timestamp;
Lai Jiangshan667d2412009-02-09 14:21:17 +08002896 int ret = -1;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002897
Steven Rostedt554f7862009-03-11 22:00:13 -04002898 if (!cpumask_test_cpu(cpu, buffer->cpumask))
2899 goto out;
2900
Steven Rostedt474d32b2009-03-03 19:51:40 -05002901 /*
2902 * If len is not big enough to hold the page header, then
2903 * we can not copy anything.
2904 */
2905 if (len <= BUF_PAGE_HDR_SIZE)
Steven Rostedt554f7862009-03-11 22:00:13 -04002906 goto out;
Steven Rostedt474d32b2009-03-03 19:51:40 -05002907
2908 len -= BUF_PAGE_HDR_SIZE;
2909
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002910 if (!data_page)
Steven Rostedt554f7862009-03-11 22:00:13 -04002911 goto out;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002912
Steven Rostedt044fa782008-12-02 23:50:03 -05002913 bpage = *data_page;
2914 if (!bpage)
Steven Rostedt554f7862009-03-11 22:00:13 -04002915 goto out;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002916
2917 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
2918
Steven Rostedtef7a4a12009-03-03 00:27:49 -05002919 reader = rb_get_reader_page(cpu_buffer);
2920 if (!reader)
Steven Rostedt554f7862009-03-11 22:00:13 -04002921 goto out_unlock;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002922
Steven Rostedtef7a4a12009-03-03 00:27:49 -05002923 event = rb_reader_event(cpu_buffer);
Lai Jiangshan667d2412009-02-09 14:21:17 +08002924
Steven Rostedtef7a4a12009-03-03 00:27:49 -05002925 read = reader->read;
2926 commit = rb_page_commit(reader);
2927
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002928 /*
Steven Rostedt474d32b2009-03-03 19:51:40 -05002929 * If this page has been partially read or
2930 * if len is not big enough to read the rest of the page or
2931 * a writer is still on the page, then
2932 * we must copy the data from the page to the buffer.
2933 * Otherwise, we can simply swap the page with the one passed in.
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002934 */
Steven Rostedt474d32b2009-03-03 19:51:40 -05002935 if (read || (len < (commit - read)) ||
Steven Rostedtef7a4a12009-03-03 00:27:49 -05002936 cpu_buffer->reader_page == cpu_buffer->commit_page) {
Lai Jiangshan667d2412009-02-09 14:21:17 +08002937 struct buffer_data_page *rpage = cpu_buffer->reader_page->page;
Steven Rostedt474d32b2009-03-03 19:51:40 -05002938 unsigned int rpos = read;
2939 unsigned int pos = 0;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05002940 unsigned int size;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002941
2942 if (full)
Steven Rostedt554f7862009-03-11 22:00:13 -04002943 goto out_unlock;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002944
Steven Rostedtef7a4a12009-03-03 00:27:49 -05002945 if (len > (commit - read))
2946 len = (commit - read);
2947
2948 size = rb_event_length(event);
2949
2950 if (len < size)
Steven Rostedt554f7862009-03-11 22:00:13 -04002951 goto out_unlock;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05002952
Steven Rostedt4f3640f2009-03-03 23:52:42 -05002953 /* save the current timestamp, since the user will need it */
2954 save_timestamp = cpu_buffer->read_stamp;
2955
Steven Rostedtef7a4a12009-03-03 00:27:49 -05002956 /* Need to copy one event at a time */
2957 do {
Steven Rostedt474d32b2009-03-03 19:51:40 -05002958 memcpy(bpage->data + pos, rpage->data + rpos, size);
Steven Rostedtef7a4a12009-03-03 00:27:49 -05002959
2960 len -= size;
2961
2962 rb_advance_reader(cpu_buffer);
Steven Rostedt474d32b2009-03-03 19:51:40 -05002963 rpos = reader->read;
2964 pos += size;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05002965
2966 event = rb_reader_event(cpu_buffer);
2967 size = rb_event_length(event);
2968 } while (len > size);
Lai Jiangshan667d2412009-02-09 14:21:17 +08002969
2970 /* update bpage */
Steven Rostedtef7a4a12009-03-03 00:27:49 -05002971 local_set(&bpage->commit, pos);
Steven Rostedt4f3640f2009-03-03 23:52:42 -05002972 bpage->time_stamp = save_timestamp;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05002973
Steven Rostedt474d32b2009-03-03 19:51:40 -05002974 /* we copied everything to the beginning */
2975 read = 0;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002976 } else {
Steven Rostedtafbab762009-05-01 19:40:05 -04002977 /* update the entry counter */
2978 cpu_buffer->read += local_read(&reader->entries);
2979
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002980 /* swap the pages */
Steven Rostedt044fa782008-12-02 23:50:03 -05002981 rb_init_page(bpage);
Steven Rostedtef7a4a12009-03-03 00:27:49 -05002982 bpage = reader->page;
2983 reader->page = *data_page;
2984 local_set(&reader->write, 0);
Steven Rostedt778c55d2009-05-01 18:44:45 -04002985 local_set(&reader->entries, 0);
Steven Rostedtef7a4a12009-03-03 00:27:49 -05002986 reader->read = 0;
Steven Rostedt044fa782008-12-02 23:50:03 -05002987 *data_page = bpage;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002988 }
Lai Jiangshan667d2412009-02-09 14:21:17 +08002989 ret = read;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002990
Steven Rostedt554f7862009-03-11 22:00:13 -04002991 out_unlock:
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002992 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
2993
Steven Rostedt554f7862009-03-11 22:00:13 -04002994 out:
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002995 return ret;
2996}
Steven Rostedtd6ce96d2009-05-05 01:15:24 -04002997EXPORT_SYMBOL_GPL(ring_buffer_read_page);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002998
Steven Rostedta3583242008-11-11 15:01:42 -05002999static ssize_t
3000rb_simple_read(struct file *filp, char __user *ubuf,
3001 size_t cnt, loff_t *ppos)
3002{
Hannes Eder5e398412009-02-10 19:44:34 +01003003 unsigned long *p = filp->private_data;
Steven Rostedta3583242008-11-11 15:01:42 -05003004 char buf[64];
3005 int r;
3006
Steven Rostedt033601a2008-11-21 12:41:55 -05003007 if (test_bit(RB_BUFFERS_DISABLED_BIT, p))
3008 r = sprintf(buf, "permanently disabled\n");
3009 else
3010 r = sprintf(buf, "%d\n", test_bit(RB_BUFFERS_ON_BIT, p));
Steven Rostedta3583242008-11-11 15:01:42 -05003011
3012 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
3013}
3014
3015static ssize_t
3016rb_simple_write(struct file *filp, const char __user *ubuf,
3017 size_t cnt, loff_t *ppos)
3018{
Hannes Eder5e398412009-02-10 19:44:34 +01003019 unsigned long *p = filp->private_data;
Steven Rostedta3583242008-11-11 15:01:42 -05003020 char buf[64];
Hannes Eder5e398412009-02-10 19:44:34 +01003021 unsigned long val;
Steven Rostedta3583242008-11-11 15:01:42 -05003022 int ret;
3023
3024 if (cnt >= sizeof(buf))
3025 return -EINVAL;
3026
3027 if (copy_from_user(&buf, ubuf, cnt))
3028 return -EFAULT;
3029
3030 buf[cnt] = 0;
3031
3032 ret = strict_strtoul(buf, 10, &val);
3033 if (ret < 0)
3034 return ret;
3035
Steven Rostedt033601a2008-11-21 12:41:55 -05003036 if (val)
3037 set_bit(RB_BUFFERS_ON_BIT, p);
3038 else
3039 clear_bit(RB_BUFFERS_ON_BIT, p);
Steven Rostedta3583242008-11-11 15:01:42 -05003040
3041 (*ppos)++;
3042
3043 return cnt;
3044}
3045
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003046static const struct file_operations rb_simple_fops = {
Steven Rostedta3583242008-11-11 15:01:42 -05003047 .open = tracing_open_generic,
3048 .read = rb_simple_read,
3049 .write = rb_simple_write,
3050};
3051
3052
3053static __init int rb_init_debugfs(void)
3054{
3055 struct dentry *d_tracer;
Steven Rostedta3583242008-11-11 15:01:42 -05003056
3057 d_tracer = tracing_init_dentry();
3058
Frederic Weisbecker5452af62009-03-27 00:25:38 +01003059 trace_create_file("tracing_on", 0644, d_tracer,
3060 &ring_buffer_flags, &rb_simple_fops);
Steven Rostedta3583242008-11-11 15:01:42 -05003061
3062 return 0;
3063}
3064
3065fs_initcall(rb_init_debugfs);
Steven Rostedt554f7862009-03-11 22:00:13 -04003066
Steven Rostedt59222ef2009-03-12 11:46:03 -04003067#ifdef CONFIG_HOTPLUG_CPU
Frederic Weisbecker09c9e842009-03-21 04:33:36 +01003068static int rb_cpu_notify(struct notifier_block *self,
3069 unsigned long action, void *hcpu)
Steven Rostedt554f7862009-03-11 22:00:13 -04003070{
3071 struct ring_buffer *buffer =
3072 container_of(self, struct ring_buffer, cpu_notify);
3073 long cpu = (long)hcpu;
3074
3075 switch (action) {
3076 case CPU_UP_PREPARE:
3077 case CPU_UP_PREPARE_FROZEN:
3078 if (cpu_isset(cpu, *buffer->cpumask))
3079 return NOTIFY_OK;
3080
3081 buffer->buffers[cpu] =
3082 rb_allocate_cpu_buffer(buffer, cpu);
3083 if (!buffer->buffers[cpu]) {
3084 WARN(1, "failed to allocate ring buffer on CPU %ld\n",
3085 cpu);
3086 return NOTIFY_OK;
3087 }
3088 smp_wmb();
3089 cpu_set(cpu, *buffer->cpumask);
3090 break;
3091 case CPU_DOWN_PREPARE:
3092 case CPU_DOWN_PREPARE_FROZEN:
3093 /*
3094 * Do nothing.
3095 * If we were to free the buffer, then the user would
3096 * lose any trace that was in the buffer.
3097 */
3098 break;
3099 default:
3100 break;
3101 }
3102 return NOTIFY_OK;
3103}
3104#endif