blob: c8d2a66e1d1fde776e2efacee78800d570324456 [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>
Vegard Nossum1744a212009-02-28 08:29:44 +010013#include <linux/kmemcheck.h>
Steven Rostedt7a8e76a2008-09-29 23:02:38 -040014#include <linux/module.h>
15#include <linux/percpu.h>
16#include <linux/mutex.h>
Steven Rostedt7a8e76a2008-09-29 23:02:38 -040017#include <linux/init.h>
18#include <linux/hash.h>
19#include <linux/list.h>
Steven Rostedt554f7862009-03-11 22:00:13 -040020#include <linux/cpu.h>
Steven Rostedt7a8e76a2008-09-29 23:02:38 -040021#include <linux/fs.h>
22
Steven Rostedt182e9f52008-11-03 23:15:56 -050023#include "trace.h"
24
Steven Rostedt033601a2008-11-21 12:41:55 -050025/*
Steven Rostedtd1b182a2009-04-15 16:53:47 -040026 * The ring buffer header is special. We must manually up keep it.
27 */
28int ring_buffer_print_entry_header(struct trace_seq *s)
29{
30 int ret;
31
Lai Jiangshan334d4162009-04-24 11:27:05 +080032 ret = trace_seq_printf(s, "# compressed entry header\n");
33 ret = trace_seq_printf(s, "\ttype_len : 5 bits\n");
Steven Rostedtd1b182a2009-04-15 16:53:47 -040034 ret = trace_seq_printf(s, "\ttime_delta : 27 bits\n");
35 ret = trace_seq_printf(s, "\tarray : 32 bits\n");
36 ret = trace_seq_printf(s, "\n");
37 ret = trace_seq_printf(s, "\tpadding : type == %d\n",
38 RINGBUF_TYPE_PADDING);
39 ret = trace_seq_printf(s, "\ttime_extend : type == %d\n",
40 RINGBUF_TYPE_TIME_EXTEND);
Lai Jiangshan334d4162009-04-24 11:27:05 +080041 ret = trace_seq_printf(s, "\tdata max type_len == %d\n",
42 RINGBUF_TYPE_DATA_TYPE_LEN_MAX);
Steven Rostedtd1b182a2009-04-15 16:53:47 -040043
44 return ret;
45}
46
47/*
Steven Rostedt5cc98542009-03-12 22:24:17 -040048 * The ring buffer is made up of a list of pages. A separate list of pages is
49 * allocated for each CPU. A writer may only write to a buffer that is
50 * associated with the CPU it is currently executing on. A reader may read
51 * from any per cpu buffer.
52 *
53 * The reader is special. For each per cpu buffer, the reader has its own
54 * reader page. When a reader has read the entire reader page, this reader
55 * page is swapped with another page in the ring buffer.
56 *
57 * Now, as long as the writer is off the reader page, the reader can do what
58 * ever it wants with that page. The writer will never write to that page
59 * again (as long as it is out of the ring buffer).
60 *
61 * Here's some silly ASCII art.
62 *
63 * +------+
64 * |reader| RING BUFFER
65 * |page |
66 * +------+ +---+ +---+ +---+
67 * | |-->| |-->| |
68 * +---+ +---+ +---+
69 * ^ |
70 * | |
71 * +---------------+
72 *
73 *
74 * +------+
75 * |reader| RING BUFFER
76 * |page |------------------v
77 * +------+ +---+ +---+ +---+
78 * | |-->| |-->| |
79 * +---+ +---+ +---+
80 * ^ |
81 * | |
82 * +---------------+
83 *
84 *
85 * +------+
86 * |reader| RING BUFFER
87 * |page |------------------v
88 * +------+ +---+ +---+ +---+
89 * ^ | |-->| |-->| |
90 * | +---+ +---+ +---+
91 * | |
92 * | |
93 * +------------------------------+
94 *
95 *
96 * +------+
97 * |buffer| RING BUFFER
98 * |page |------------------v
99 * +------+ +---+ +---+ +---+
100 * ^ | | | |-->| |
101 * | New +---+ +---+ +---+
102 * | Reader------^ |
103 * | page |
104 * +------------------------------+
105 *
106 *
107 * After we make this swap, the reader can hand this page off to the splice
108 * code and be done with it. It can even allocate a new page if it needs to
109 * and swap that into the ring buffer.
110 *
111 * We will be using cmpxchg soon to make all this lockless.
112 *
113 */
114
115/*
Steven Rostedt033601a2008-11-21 12:41:55 -0500116 * A fast way to enable or disable all ring buffers is to
117 * call tracing_on or tracing_off. Turning off the ring buffers
118 * prevents all ring buffers from being recorded to.
119 * Turning this switch on, makes it OK to write to the
120 * ring buffer, if the ring buffer is enabled itself.
121 *
122 * There's three layers that must be on in order to write
123 * to the ring buffer.
124 *
125 * 1) This global flag must be set.
126 * 2) The ring buffer must be enabled for recording.
127 * 3) The per cpu buffer must be enabled for recording.
128 *
129 * In case of an anomaly, this global flag has a bit set that
130 * will permantly disable all ring buffers.
131 */
132
133/*
134 * Global flag to disable all recording to ring buffers
135 * This has two bits: ON, DISABLED
136 *
137 * ON DISABLED
138 * ---- ----------
139 * 0 0 : ring buffers are off
140 * 1 0 : ring buffers are on
141 * X 1 : ring buffers are permanently disabled
142 */
143
144enum {
145 RB_BUFFERS_ON_BIT = 0,
146 RB_BUFFERS_DISABLED_BIT = 1,
147};
148
149enum {
150 RB_BUFFERS_ON = 1 << RB_BUFFERS_ON_BIT,
151 RB_BUFFERS_DISABLED = 1 << RB_BUFFERS_DISABLED_BIT,
152};
153
Hannes Eder5e398412009-02-10 19:44:34 +0100154static unsigned long ring_buffer_flags __read_mostly = RB_BUFFERS_ON;
Steven Rostedta3583242008-11-11 15:01:42 -0500155
Steven Rostedt474d32b2009-03-03 19:51:40 -0500156#define BUF_PAGE_HDR_SIZE offsetof(struct buffer_data_page, data)
157
Steven Rostedta3583242008-11-11 15:01:42 -0500158/**
159 * tracing_on - enable all tracing buffers
160 *
161 * This function enables all tracing buffers that may have been
162 * disabled with tracing_off.
163 */
164void tracing_on(void)
165{
Steven Rostedt033601a2008-11-21 12:41:55 -0500166 set_bit(RB_BUFFERS_ON_BIT, &ring_buffer_flags);
Steven Rostedta3583242008-11-11 15:01:42 -0500167}
Robert Richterc4f50182008-12-11 16:49:22 +0100168EXPORT_SYMBOL_GPL(tracing_on);
Steven Rostedta3583242008-11-11 15:01:42 -0500169
170/**
171 * tracing_off - turn off all tracing buffers
172 *
173 * This function stops all tracing buffers from recording data.
174 * It does not disable any overhead the tracers themselves may
175 * be causing. This function simply causes all recording to
176 * the ring buffers to fail.
177 */
178void tracing_off(void)
179{
Steven Rostedt033601a2008-11-21 12:41:55 -0500180 clear_bit(RB_BUFFERS_ON_BIT, &ring_buffer_flags);
181}
Robert Richterc4f50182008-12-11 16:49:22 +0100182EXPORT_SYMBOL_GPL(tracing_off);
Steven Rostedt033601a2008-11-21 12:41:55 -0500183
184/**
185 * tracing_off_permanent - permanently disable ring buffers
186 *
187 * This function, once called, will disable all ring buffers
Wenji Huangc3706f02009-02-10 01:03:18 -0500188 * permanently.
Steven Rostedt033601a2008-11-21 12:41:55 -0500189 */
190void tracing_off_permanent(void)
191{
192 set_bit(RB_BUFFERS_DISABLED_BIT, &ring_buffer_flags);
Steven Rostedta3583242008-11-11 15:01:42 -0500193}
194
Steven Rostedt988ae9d2009-02-14 19:17:02 -0500195/**
196 * tracing_is_on - show state of ring buffers enabled
197 */
198int tracing_is_on(void)
199{
200 return ring_buffer_flags == RB_BUFFERS_ON;
201}
202EXPORT_SYMBOL_GPL(tracing_is_on);
203
Ingo Molnard06bbd62008-11-12 10:11:37 +0100204#include "trace.h"
205
Steven Rostedte3d6bf02009-03-03 13:53:07 -0500206#define RB_EVNT_HDR_SIZE (offsetof(struct ring_buffer_event, array))
Andrew Morton67d34722009-01-09 12:27:09 -0800207#define RB_ALIGNMENT 4U
Lai Jiangshan334d4162009-04-24 11:27:05 +0800208#define RB_MAX_SMALL_DATA (RB_ALIGNMENT * RINGBUF_TYPE_DATA_TYPE_LEN_MAX)
Steven Rostedtc7b09302009-06-11 11:12:00 -0400209#define RB_EVNT_MIN_SIZE 8U /* two 32bit words */
Lai Jiangshan334d4162009-04-24 11:27:05 +0800210
211/* define RINGBUF_TYPE_DATA for 'case RINGBUF_TYPE_DATA:' */
212#define RINGBUF_TYPE_DATA 0 ... RINGBUF_TYPE_DATA_TYPE_LEN_MAX
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400213
214enum {
215 RB_LEN_TIME_EXTEND = 8,
216 RB_LEN_TIME_STAMP = 16,
217};
218
Tom Zanussi2d622712009-03-22 03:30:49 -0500219static inline int rb_null_event(struct ring_buffer_event *event)
220{
Steven Rostedta1863c22009-09-03 10:23:58 -0400221 return event->type_len == RINGBUF_TYPE_PADDING && !event->time_delta;
Tom Zanussi2d622712009-03-22 03:30:49 -0500222}
223
224static void rb_event_set_padding(struct ring_buffer_event *event)
225{
Steven Rostedta1863c22009-09-03 10:23:58 -0400226 /* padding has a NULL time_delta */
Lai Jiangshan334d4162009-04-24 11:27:05 +0800227 event->type_len = RINGBUF_TYPE_PADDING;
Tom Zanussi2d622712009-03-22 03:30:49 -0500228 event->time_delta = 0;
229}
230
Tom Zanussi2d622712009-03-22 03:30:49 -0500231static unsigned
232rb_event_data_length(struct ring_buffer_event *event)
233{
234 unsigned length;
235
Lai Jiangshan334d4162009-04-24 11:27:05 +0800236 if (event->type_len)
237 length = event->type_len * RB_ALIGNMENT;
Tom Zanussi2d622712009-03-22 03:30:49 -0500238 else
239 length = event->array[0];
240 return length + RB_EVNT_HDR_SIZE;
241}
242
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400243/* inline for ring buffer fast paths */
Andrew Morton34a148b2009-01-09 12:27:09 -0800244static unsigned
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400245rb_event_length(struct ring_buffer_event *event)
246{
Lai Jiangshan334d4162009-04-24 11:27:05 +0800247 switch (event->type_len) {
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400248 case RINGBUF_TYPE_PADDING:
Tom Zanussi2d622712009-03-22 03:30:49 -0500249 if (rb_null_event(event))
250 /* undefined */
251 return -1;
Lai Jiangshan334d4162009-04-24 11:27:05 +0800252 return event->array[0] + RB_EVNT_HDR_SIZE;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400253
254 case RINGBUF_TYPE_TIME_EXTEND:
255 return RB_LEN_TIME_EXTEND;
256
257 case RINGBUF_TYPE_TIME_STAMP:
258 return RB_LEN_TIME_STAMP;
259
260 case RINGBUF_TYPE_DATA:
Tom Zanussi2d622712009-03-22 03:30:49 -0500261 return rb_event_data_length(event);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400262 default:
263 BUG();
264 }
265 /* not hit */
266 return 0;
267}
268
269/**
270 * ring_buffer_event_length - return the length of the event
271 * @event: the event to get the length of
272 */
273unsigned ring_buffer_event_length(struct ring_buffer_event *event)
274{
Robert Richter465634a2009-01-07 15:32:11 +0100275 unsigned length = rb_event_length(event);
Lai Jiangshan334d4162009-04-24 11:27:05 +0800276 if (event->type_len > RINGBUF_TYPE_DATA_TYPE_LEN_MAX)
Robert Richter465634a2009-01-07 15:32:11 +0100277 return length;
278 length -= RB_EVNT_HDR_SIZE;
279 if (length > RB_MAX_SMALL_DATA + sizeof(event->array[0]))
280 length -= sizeof(event->array[0]);
281 return length;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400282}
Robert Richterc4f50182008-12-11 16:49:22 +0100283EXPORT_SYMBOL_GPL(ring_buffer_event_length);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400284
285/* inline for ring buffer fast paths */
Andrew Morton34a148b2009-01-09 12:27:09 -0800286static void *
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400287rb_event_data(struct ring_buffer_event *event)
288{
Lai Jiangshan334d4162009-04-24 11:27:05 +0800289 BUG_ON(event->type_len > RINGBUF_TYPE_DATA_TYPE_LEN_MAX);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400290 /* If length is in len field, then array[0] has the data */
Lai Jiangshan334d4162009-04-24 11:27:05 +0800291 if (event->type_len)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400292 return (void *)&event->array[0];
293 /* Otherwise length is in array[0] and array[1] has the data */
294 return (void *)&event->array[1];
295}
296
297/**
298 * ring_buffer_event_data - return the data of the event
299 * @event: the event to get the data from
300 */
301void *ring_buffer_event_data(struct ring_buffer_event *event)
302{
303 return rb_event_data(event);
304}
Robert Richterc4f50182008-12-11 16:49:22 +0100305EXPORT_SYMBOL_GPL(ring_buffer_event_data);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400306
307#define for_each_buffer_cpu(buffer, cpu) \
Rusty Russell9e01c1b2009-01-01 10:12:22 +1030308 for_each_cpu(cpu, buffer->cpumask)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400309
310#define TS_SHIFT 27
311#define TS_MASK ((1ULL << TS_SHIFT) - 1)
312#define TS_DELTA_TEST (~TS_MASK)
313
Steven Rostedtabc9b562008-12-02 15:34:06 -0500314struct buffer_data_page {
Steven Rostedte4c2ce82008-10-01 11:14:54 -0400315 u64 time_stamp; /* page time stamp */
Wenji Huangc3706f02009-02-10 01:03:18 -0500316 local_t commit; /* write committed index */
Steven Rostedtabc9b562008-12-02 15:34:06 -0500317 unsigned char data[]; /* data of buffer page */
318};
319
Steven Rostedt77ae3652009-03-27 11:00:29 -0400320/*
321 * Note, the buffer_page list must be first. The buffer pages
322 * are allocated in cache lines, which means that each buffer
323 * page will be at the beginning of a cache line, and thus
324 * the least significant bits will be zero. We use this to
325 * add flags in the list struct pointers, to make the ring buffer
326 * lockless.
327 */
Steven Rostedtabc9b562008-12-02 15:34:06 -0500328struct buffer_page {
Steven Rostedt778c55d2009-05-01 18:44:45 -0400329 struct list_head list; /* list of buffer pages */
Steven Rostedtabc9b562008-12-02 15:34:06 -0500330 local_t write; /* index for next write */
Steven Rostedt6f807ac2008-10-04 02:00:58 -0400331 unsigned read; /* index for next read */
Steven Rostedt778c55d2009-05-01 18:44:45 -0400332 local_t entries; /* entries on this page */
Steven Rostedtabc9b562008-12-02 15:34:06 -0500333 struct buffer_data_page *page; /* Actual data page */
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400334};
335
Steven Rostedt77ae3652009-03-27 11:00:29 -0400336/*
337 * The buffer page counters, write and entries, must be reset
338 * atomically when crossing page boundaries. To synchronize this
339 * update, two counters are inserted into the number. One is
340 * the actual counter for the write position or count on the page.
341 *
342 * The other is a counter of updaters. Before an update happens
343 * the update partition of the counter is incremented. This will
344 * allow the updater to update the counter atomically.
345 *
346 * The counter is 20 bits, and the state data is 12.
347 */
348#define RB_WRITE_MASK 0xfffff
349#define RB_WRITE_INTCNT (1 << 20)
350
Steven Rostedt044fa782008-12-02 23:50:03 -0500351static void rb_init_page(struct buffer_data_page *bpage)
Steven Rostedtabc9b562008-12-02 15:34:06 -0500352{
Steven Rostedt044fa782008-12-02 23:50:03 -0500353 local_set(&bpage->commit, 0);
Steven Rostedtabc9b562008-12-02 15:34:06 -0500354}
355
Steven Rostedt474d32b2009-03-03 19:51:40 -0500356/**
357 * ring_buffer_page_len - the size of data on the page.
358 * @page: The page to read
359 *
360 * Returns the amount of data on the page, including buffer page header.
361 */
Steven Rostedtef7a4a12009-03-03 00:27:49 -0500362size_t ring_buffer_page_len(void *page)
363{
Steven Rostedt474d32b2009-03-03 19:51:40 -0500364 return local_read(&((struct buffer_data_page *)page)->commit)
365 + BUF_PAGE_HDR_SIZE;
Steven Rostedtef7a4a12009-03-03 00:27:49 -0500366}
367
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400368/*
Steven Rostedted568292008-09-29 23:02:40 -0400369 * Also stolen from mm/slob.c. Thanks to Mathieu Desnoyers for pointing
370 * this issue out.
371 */
Andrew Morton34a148b2009-01-09 12:27:09 -0800372static void free_buffer_page(struct buffer_page *bpage)
Steven Rostedted568292008-09-29 23:02:40 -0400373{
Andrew Morton34a148b2009-01-09 12:27:09 -0800374 free_page((unsigned long)bpage->page);
Steven Rostedte4c2ce82008-10-01 11:14:54 -0400375 kfree(bpage);
Steven Rostedted568292008-09-29 23:02:40 -0400376}
377
378/*
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400379 * We need to fit the time_stamp delta into 27 bits.
380 */
381static inline int test_time_stamp(u64 delta)
382{
383 if (delta & TS_DELTA_TEST)
384 return 1;
385 return 0;
386}
387
Steven Rostedt474d32b2009-03-03 19:51:40 -0500388#define BUF_PAGE_SIZE (PAGE_SIZE - BUF_PAGE_HDR_SIZE)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400389
Steven Rostedtbe957c42009-05-11 14:42:53 -0400390/* Max payload is BUF_PAGE_SIZE - header (8bytes) */
391#define BUF_MAX_DATA_SIZE (BUF_PAGE_SIZE - (sizeof(u32) * 2))
392
Steven Rostedtea05b572009-06-03 09:30:10 -0400393/* Max number of timestamps that can fit on a page */
394#define RB_TIMESTAMPS_PER_PAGE (BUF_PAGE_SIZE / RB_LEN_TIME_STAMP)
395
Steven Rostedtd1b182a2009-04-15 16:53:47 -0400396int ring_buffer_print_page_header(struct trace_seq *s)
397{
398 struct buffer_data_page field;
399 int ret;
400
401 ret = trace_seq_printf(s, "\tfield: u64 timestamp;\t"
402 "offset:0;\tsize:%u;\n",
403 (unsigned int)sizeof(field.time_stamp));
404
405 ret = trace_seq_printf(s, "\tfield: local_t commit;\t"
406 "offset:%u;\tsize:%u;\n",
407 (unsigned int)offsetof(typeof(field), commit),
408 (unsigned int)sizeof(field.commit));
409
410 ret = trace_seq_printf(s, "\tfield: char data;\t"
411 "offset:%u;\tsize:%u;\n",
412 (unsigned int)offsetof(typeof(field), data),
413 (unsigned int)BUF_PAGE_SIZE);
414
415 return ret;
416}
417
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400418/*
419 * head_page == tail_page && head == tail then buffer is empty.
420 */
421struct ring_buffer_per_cpu {
422 int cpu;
423 struct ring_buffer *buffer;
Steven Rostedt77ae3652009-03-27 11:00:29 -0400424 spinlock_t reader_lock; /* serialize readers */
Steven Rostedt3e03fb72008-11-06 00:09:43 -0500425 raw_spinlock_t lock;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400426 struct lock_class_key lock_key;
Steven Rostedt3adc54f2009-03-30 15:32:01 -0400427 struct list_head *pages;
Steven Rostedt6f807ac2008-10-04 02:00:58 -0400428 struct buffer_page *head_page; /* read from head */
429 struct buffer_page *tail_page; /* write to tail */
Wenji Huangc3706f02009-02-10 01:03:18 -0500430 struct buffer_page *commit_page; /* committed pages */
Steven Rostedtd7690412008-10-01 00:29:53 -0400431 struct buffer_page *reader_page;
Steven Rostedt77ae3652009-03-27 11:00:29 -0400432 local_t commit_overrun;
433 local_t overrun;
Steven Rostedte4906ef2009-04-30 20:49:44 -0400434 local_t entries;
Steven Rostedtfa743952009-06-16 12:37:57 -0400435 local_t committing;
436 local_t commits;
Steven Rostedt77ae3652009-03-27 11:00:29 -0400437 unsigned long read;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400438 u64 write_stamp;
439 u64 read_stamp;
440 atomic_t record_disabled;
441};
442
443struct ring_buffer {
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400444 unsigned pages;
445 unsigned flags;
446 int cpus;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400447 atomic_t record_disabled;
Arnaldo Carvalho de Melo00f62f62009-02-09 17:04:06 -0200448 cpumask_var_t cpumask;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400449
Peter Zijlstra1f8a6a12009-06-08 18:18:39 +0200450 struct lock_class_key *reader_lock_key;
451
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400452 struct mutex mutex;
453
454 struct ring_buffer_per_cpu **buffers;
Steven Rostedt554f7862009-03-11 22:00:13 -0400455
Steven Rostedt59222ef2009-03-12 11:46:03 -0400456#ifdef CONFIG_HOTPLUG_CPU
Steven Rostedt554f7862009-03-11 22:00:13 -0400457 struct notifier_block cpu_notify;
458#endif
Steven Rostedt37886f62009-03-17 17:22:06 -0400459 u64 (*clock)(void);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400460};
461
462struct ring_buffer_iter {
463 struct ring_buffer_per_cpu *cpu_buffer;
464 unsigned long head;
465 struct buffer_page *head_page;
466 u64 read_stamp;
467};
468
Steven Rostedtf536aaf2008-11-10 23:07:30 -0500469/* buffer may be either ring_buffer or ring_buffer_per_cpu */
Steven Rostedtbf41a152008-10-04 02:00:59 -0400470#define RB_WARN_ON(buffer, cond) \
Steven Rostedt3e89c7b2008-11-11 15:28:41 -0500471 ({ \
472 int _____ret = unlikely(cond); \
473 if (_____ret) { \
Steven Rostedtbf41a152008-10-04 02:00:59 -0400474 atomic_inc(&buffer->record_disabled); \
475 WARN_ON(1); \
476 } \
Steven Rostedt3e89c7b2008-11-11 15:28:41 -0500477 _____ret; \
478 })
Steven Rostedtf536aaf2008-11-10 23:07:30 -0500479
Steven Rostedt37886f62009-03-17 17:22:06 -0400480/* Up this if you want to test the TIME_EXTENTS and normalization */
481#define DEBUG_SHIFT 0
482
Steven Rostedt88eb0122009-05-11 16:28:23 -0400483static inline u64 rb_time_stamp(struct ring_buffer *buffer, int cpu)
484{
485 /* shift to debug/test normalization and TIME_EXTENTS */
486 return buffer->clock() << DEBUG_SHIFT;
487}
488
Steven Rostedt37886f62009-03-17 17:22:06 -0400489u64 ring_buffer_time_stamp(struct ring_buffer *buffer, int cpu)
490{
491 u64 time;
492
493 preempt_disable_notrace();
Steven Rostedt88eb0122009-05-11 16:28:23 -0400494 time = rb_time_stamp(buffer, cpu);
Steven Rostedt37886f62009-03-17 17:22:06 -0400495 preempt_enable_no_resched_notrace();
496
497 return time;
498}
499EXPORT_SYMBOL_GPL(ring_buffer_time_stamp);
500
501void ring_buffer_normalize_time_stamp(struct ring_buffer *buffer,
502 int cpu, u64 *ts)
503{
504 /* Just stupid testing the normalize function and deltas */
505 *ts >>= DEBUG_SHIFT;
506}
507EXPORT_SYMBOL_GPL(ring_buffer_normalize_time_stamp);
508
Steven Rostedt77ae3652009-03-27 11:00:29 -0400509/*
510 * Making the ring buffer lockless makes things tricky.
511 * Although writes only happen on the CPU that they are on,
512 * and they only need to worry about interrupts. Reads can
513 * happen on any CPU.
514 *
515 * The reader page is always off the ring buffer, but when the
516 * reader finishes with a page, it needs to swap its page with
517 * a new one from the buffer. The reader needs to take from
518 * the head (writes go to the tail). But if a writer is in overwrite
519 * mode and wraps, it must push the head page forward.
520 *
521 * Here lies the problem.
522 *
523 * The reader must be careful to replace only the head page, and
524 * not another one. As described at the top of the file in the
525 * ASCII art, the reader sets its old page to point to the next
526 * page after head. It then sets the page after head to point to
527 * the old reader page. But if the writer moves the head page
528 * during this operation, the reader could end up with the tail.
529 *
530 * We use cmpxchg to help prevent this race. We also do something
531 * special with the page before head. We set the LSB to 1.
532 *
533 * When the writer must push the page forward, it will clear the
534 * bit that points to the head page, move the head, and then set
535 * the bit that points to the new head page.
536 *
537 * We also don't want an interrupt coming in and moving the head
538 * page on another writer. Thus we use the second LSB to catch
539 * that too. Thus:
540 *
541 * head->list->prev->next bit 1 bit 0
542 * ------- -------
543 * Normal page 0 0
544 * Points to head page 0 1
545 * New head page 1 0
546 *
547 * Note we can not trust the prev pointer of the head page, because:
548 *
549 * +----+ +-----+ +-----+
550 * | |------>| T |---X--->| N |
551 * | |<------| | | |
552 * +----+ +-----+ +-----+
553 * ^ ^ |
554 * | +-----+ | |
555 * +----------| R |----------+ |
556 * | |<-----------+
557 * +-----+
558 *
559 * Key: ---X--> HEAD flag set in pointer
560 * T Tail page
561 * R Reader page
562 * N Next page
563 *
564 * (see __rb_reserve_next() to see where this happens)
565 *
566 * What the above shows is that the reader just swapped out
567 * the reader page with a page in the buffer, but before it
568 * could make the new header point back to the new page added
569 * it was preempted by a writer. The writer moved forward onto
570 * the new page added by the reader and is about to move forward
571 * again.
572 *
573 * You can see, it is legitimate for the previous pointer of
574 * the head (or any page) not to point back to itself. But only
575 * temporarially.
576 */
577
578#define RB_PAGE_NORMAL 0UL
579#define RB_PAGE_HEAD 1UL
580#define RB_PAGE_UPDATE 2UL
581
582
583#define RB_FLAG_MASK 3UL
584
585/* PAGE_MOVED is not part of the mask */
586#define RB_PAGE_MOVED 4UL
587
588/*
589 * rb_list_head - remove any bit
590 */
591static struct list_head *rb_list_head(struct list_head *list)
592{
593 unsigned long val = (unsigned long)list;
594
595 return (struct list_head *)(val & ~RB_FLAG_MASK);
596}
597
598/*
599 * rb_is_head_page - test if the give page is the head page
600 *
601 * Because the reader may move the head_page pointer, we can
602 * not trust what the head page is (it may be pointing to
603 * the reader page). But if the next page is a header page,
604 * its flags will be non zero.
605 */
606static int inline
607rb_is_head_page(struct ring_buffer_per_cpu *cpu_buffer,
608 struct buffer_page *page, struct list_head *list)
609{
610 unsigned long val;
611
612 val = (unsigned long)list->next;
613
614 if ((val & ~RB_FLAG_MASK) != (unsigned long)&page->list)
615 return RB_PAGE_MOVED;
616
617 return val & RB_FLAG_MASK;
618}
619
620/*
621 * rb_is_reader_page
622 *
623 * The unique thing about the reader page, is that, if the
624 * writer is ever on it, the previous pointer never points
625 * back to the reader page.
626 */
627static int rb_is_reader_page(struct buffer_page *page)
628{
629 struct list_head *list = page->list.prev;
630
631 return rb_list_head(list->next) != &page->list;
632}
633
634/*
635 * rb_set_list_to_head - set a list_head to be pointing to head.
636 */
637static void rb_set_list_to_head(struct ring_buffer_per_cpu *cpu_buffer,
638 struct list_head *list)
639{
640 unsigned long *ptr;
641
642 ptr = (unsigned long *)&list->next;
643 *ptr |= RB_PAGE_HEAD;
644 *ptr &= ~RB_PAGE_UPDATE;
645}
646
647/*
648 * rb_head_page_activate - sets up head page
649 */
650static void rb_head_page_activate(struct ring_buffer_per_cpu *cpu_buffer)
651{
652 struct buffer_page *head;
653
654 head = cpu_buffer->head_page;
655 if (!head)
656 return;
657
658 /*
659 * Set the previous list pointer to have the HEAD flag.
660 */
661 rb_set_list_to_head(cpu_buffer, head->list.prev);
662}
663
664static void rb_list_head_clear(struct list_head *list)
665{
666 unsigned long *ptr = (unsigned long *)&list->next;
667
668 *ptr &= ~RB_FLAG_MASK;
669}
670
671/*
672 * rb_head_page_dactivate - clears head page ptr (for free list)
673 */
674static void
675rb_head_page_deactivate(struct ring_buffer_per_cpu *cpu_buffer)
676{
677 struct list_head *hd;
678
679 /* Go through the whole list and clear any pointers found. */
680 rb_list_head_clear(cpu_buffer->pages);
681
682 list_for_each(hd, cpu_buffer->pages)
683 rb_list_head_clear(hd);
684}
685
686static int rb_head_page_set(struct ring_buffer_per_cpu *cpu_buffer,
687 struct buffer_page *head,
688 struct buffer_page *prev,
689 int old_flag, int new_flag)
690{
691 struct list_head *list;
692 unsigned long val = (unsigned long)&head->list;
693 unsigned long ret;
694
695 list = &prev->list;
696
697 val &= ~RB_FLAG_MASK;
698
699 ret = (unsigned long)cmpxchg(&list->next,
700 val | old_flag, val | new_flag);
701
702 /* check if the reader took the page */
703 if ((ret & ~RB_FLAG_MASK) != val)
704 return RB_PAGE_MOVED;
705
706 return ret & RB_FLAG_MASK;
707}
708
709static int rb_head_page_set_update(struct ring_buffer_per_cpu *cpu_buffer,
710 struct buffer_page *head,
711 struct buffer_page *prev,
712 int old_flag)
713{
714 return rb_head_page_set(cpu_buffer, head, prev,
715 old_flag, RB_PAGE_UPDATE);
716}
717
718static int rb_head_page_set_head(struct ring_buffer_per_cpu *cpu_buffer,
719 struct buffer_page *head,
720 struct buffer_page *prev,
721 int old_flag)
722{
723 return rb_head_page_set(cpu_buffer, head, prev,
724 old_flag, RB_PAGE_HEAD);
725}
726
727static int rb_head_page_set_normal(struct ring_buffer_per_cpu *cpu_buffer,
728 struct buffer_page *head,
729 struct buffer_page *prev,
730 int old_flag)
731{
732 return rb_head_page_set(cpu_buffer, head, prev,
733 old_flag, RB_PAGE_NORMAL);
734}
735
736static inline void rb_inc_page(struct ring_buffer_per_cpu *cpu_buffer,
737 struct buffer_page **bpage)
738{
739 struct list_head *p = rb_list_head((*bpage)->list.next);
740
741 *bpage = list_entry(p, struct buffer_page, list);
742}
743
744static struct buffer_page *
745rb_set_head_page(struct ring_buffer_per_cpu *cpu_buffer)
746{
747 struct buffer_page *head;
748 struct buffer_page *page;
749 struct list_head *list;
750 int i;
751
752 if (RB_WARN_ON(cpu_buffer, !cpu_buffer->head_page))
753 return NULL;
754
755 /* sanity check */
756 list = cpu_buffer->pages;
757 if (RB_WARN_ON(cpu_buffer, rb_list_head(list->prev->next) != list))
758 return NULL;
759
760 page = head = cpu_buffer->head_page;
761 /*
762 * It is possible that the writer moves the header behind
763 * where we started, and we miss in one loop.
764 * A second loop should grab the header, but we'll do
765 * three loops just because I'm paranoid.
766 */
767 for (i = 0; i < 3; i++) {
768 do {
769 if (rb_is_head_page(cpu_buffer, page, page->list.prev)) {
770 cpu_buffer->head_page = page;
771 return page;
772 }
773 rb_inc_page(cpu_buffer, &page);
774 } while (page != head);
775 }
776
777 RB_WARN_ON(cpu_buffer, 1);
778
779 return NULL;
780}
781
782static int rb_head_page_replace(struct buffer_page *old,
783 struct buffer_page *new)
784{
785 unsigned long *ptr = (unsigned long *)&old->list.prev->next;
786 unsigned long val;
787 unsigned long ret;
788
789 val = *ptr & ~RB_FLAG_MASK;
790 val |= RB_PAGE_HEAD;
791
792 ret = cmpxchg(ptr, val, &new->list);
793
794 return ret == val;
795}
796
797/*
798 * rb_tail_page_update - move the tail page forward
799 *
800 * Returns 1 if moved tail page, 0 if someone else did.
801 */
802static int rb_tail_page_update(struct ring_buffer_per_cpu *cpu_buffer,
803 struct buffer_page *tail_page,
804 struct buffer_page *next_page)
805{
806 struct buffer_page *old_tail;
807 unsigned long old_entries;
808 unsigned long old_write;
809 int ret = 0;
810
811 /*
812 * The tail page now needs to be moved forward.
813 *
814 * We need to reset the tail page, but without messing
815 * with possible erasing of data brought in by interrupts
816 * that have moved the tail page and are currently on it.
817 *
818 * We add a counter to the write field to denote this.
819 */
820 old_write = local_add_return(RB_WRITE_INTCNT, &next_page->write);
821 old_entries = local_add_return(RB_WRITE_INTCNT, &next_page->entries);
822
823 /*
824 * Just make sure we have seen our old_write and synchronize
825 * with any interrupts that come in.
826 */
827 barrier();
828
829 /*
830 * If the tail page is still the same as what we think
831 * it is, then it is up to us to update the tail
832 * pointer.
833 */
834 if (tail_page == cpu_buffer->tail_page) {
835 /* Zero the write counter */
836 unsigned long val = old_write & ~RB_WRITE_MASK;
837 unsigned long eval = old_entries & ~RB_WRITE_MASK;
838
839 /*
840 * This will only succeed if an interrupt did
841 * not come in and change it. In which case, we
842 * do not want to modify it.
Lai Jiangshanda706d82009-07-15 16:27:30 +0800843 *
844 * We add (void) to let the compiler know that we do not care
845 * about the return value of these functions. We use the
846 * cmpxchg to only update if an interrupt did not already
847 * do it for us. If the cmpxchg fails, we don't care.
Steven Rostedt77ae3652009-03-27 11:00:29 -0400848 */
Lai Jiangshanda706d82009-07-15 16:27:30 +0800849 (void)local_cmpxchg(&next_page->write, old_write, val);
850 (void)local_cmpxchg(&next_page->entries, old_entries, eval);
Steven Rostedt77ae3652009-03-27 11:00:29 -0400851
852 /*
853 * No need to worry about races with clearing out the commit.
854 * it only can increment when a commit takes place. But that
855 * only happens in the outer most nested commit.
856 */
857 local_set(&next_page->page->commit, 0);
858
859 old_tail = cmpxchg(&cpu_buffer->tail_page,
860 tail_page, next_page);
861
862 if (old_tail == tail_page)
863 ret = 1;
864 }
865
866 return ret;
867}
868
869static int rb_check_bpage(struct ring_buffer_per_cpu *cpu_buffer,
870 struct buffer_page *bpage)
871{
872 unsigned long val = (unsigned long)bpage;
873
874 if (RB_WARN_ON(cpu_buffer, val & RB_FLAG_MASK))
875 return 1;
876
877 return 0;
878}
879
880/**
881 * rb_check_list - make sure a pointer to a list has the last bits zero
882 */
883static int rb_check_list(struct ring_buffer_per_cpu *cpu_buffer,
884 struct list_head *list)
885{
886 if (RB_WARN_ON(cpu_buffer, rb_list_head(list->prev) != list->prev))
887 return 1;
888 if (RB_WARN_ON(cpu_buffer, rb_list_head(list->next) != list->next))
889 return 1;
890 return 0;
891}
892
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400893/**
894 * check_pages - integrity check of buffer pages
895 * @cpu_buffer: CPU buffer with pages to test
896 *
Wenji Huangc3706f02009-02-10 01:03:18 -0500897 * As a safety measure we check to make sure the data pages have not
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400898 * been corrupted.
899 */
900static int rb_check_pages(struct ring_buffer_per_cpu *cpu_buffer)
901{
Steven Rostedt3adc54f2009-03-30 15:32:01 -0400902 struct list_head *head = cpu_buffer->pages;
Steven Rostedt044fa782008-12-02 23:50:03 -0500903 struct buffer_page *bpage, *tmp;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400904
Steven Rostedt77ae3652009-03-27 11:00:29 -0400905 rb_head_page_deactivate(cpu_buffer);
906
Steven Rostedt3e89c7b2008-11-11 15:28:41 -0500907 if (RB_WARN_ON(cpu_buffer, head->next->prev != head))
908 return -1;
909 if (RB_WARN_ON(cpu_buffer, head->prev->next != head))
910 return -1;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400911
Steven Rostedt77ae3652009-03-27 11:00:29 -0400912 if (rb_check_list(cpu_buffer, head))
913 return -1;
914
Steven Rostedt044fa782008-12-02 23:50:03 -0500915 list_for_each_entry_safe(bpage, tmp, head, list) {
Steven Rostedt3e89c7b2008-11-11 15:28:41 -0500916 if (RB_WARN_ON(cpu_buffer,
Steven Rostedt044fa782008-12-02 23:50:03 -0500917 bpage->list.next->prev != &bpage->list))
Steven Rostedt3e89c7b2008-11-11 15:28:41 -0500918 return -1;
919 if (RB_WARN_ON(cpu_buffer,
Steven Rostedt044fa782008-12-02 23:50:03 -0500920 bpage->list.prev->next != &bpage->list))
Steven Rostedt3e89c7b2008-11-11 15:28:41 -0500921 return -1;
Steven Rostedt77ae3652009-03-27 11:00:29 -0400922 if (rb_check_list(cpu_buffer, &bpage->list))
923 return -1;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400924 }
925
Steven Rostedt77ae3652009-03-27 11:00:29 -0400926 rb_head_page_activate(cpu_buffer);
927
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400928 return 0;
929}
930
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400931static int rb_allocate_pages(struct ring_buffer_per_cpu *cpu_buffer,
932 unsigned nr_pages)
933{
Steven Rostedt044fa782008-12-02 23:50:03 -0500934 struct buffer_page *bpage, *tmp;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400935 unsigned long addr;
936 LIST_HEAD(pages);
937 unsigned i;
938
Steven Rostedt3adc54f2009-03-30 15:32:01 -0400939 WARN_ON(!nr_pages);
940
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400941 for (i = 0; i < nr_pages; i++) {
Steven Rostedt044fa782008-12-02 23:50:03 -0500942 bpage = kzalloc_node(ALIGN(sizeof(*bpage), cache_line_size()),
Steven Rostedtaa1e0e32008-10-02 19:18:09 -0400943 GFP_KERNEL, cpu_to_node(cpu_buffer->cpu));
Steven Rostedt044fa782008-12-02 23:50:03 -0500944 if (!bpage)
Steven Rostedte4c2ce82008-10-01 11:14:54 -0400945 goto free_pages;
Steven Rostedt77ae3652009-03-27 11:00:29 -0400946
947 rb_check_bpage(cpu_buffer, bpage);
948
Steven Rostedt044fa782008-12-02 23:50:03 -0500949 list_add(&bpage->list, &pages);
Steven Rostedte4c2ce82008-10-01 11:14:54 -0400950
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400951 addr = __get_free_page(GFP_KERNEL);
952 if (!addr)
953 goto free_pages;
Steven Rostedt044fa782008-12-02 23:50:03 -0500954 bpage->page = (void *)addr;
955 rb_init_page(bpage->page);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400956 }
957
Steven Rostedt3adc54f2009-03-30 15:32:01 -0400958 /*
959 * The ring buffer page list is a circular list that does not
960 * start and end with a list head. All page list items point to
961 * other pages.
962 */
963 cpu_buffer->pages = pages.next;
964 list_del(&pages);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400965
966 rb_check_pages(cpu_buffer);
967
968 return 0;
969
970 free_pages:
Steven Rostedt044fa782008-12-02 23:50:03 -0500971 list_for_each_entry_safe(bpage, tmp, &pages, list) {
972 list_del_init(&bpage->list);
973 free_buffer_page(bpage);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400974 }
975 return -ENOMEM;
976}
977
978static struct ring_buffer_per_cpu *
979rb_allocate_cpu_buffer(struct ring_buffer *buffer, int cpu)
980{
981 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedt044fa782008-12-02 23:50:03 -0500982 struct buffer_page *bpage;
Steven Rostedtd7690412008-10-01 00:29:53 -0400983 unsigned long addr;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400984 int ret;
985
986 cpu_buffer = kzalloc_node(ALIGN(sizeof(*cpu_buffer), cache_line_size()),
987 GFP_KERNEL, cpu_to_node(cpu));
988 if (!cpu_buffer)
989 return NULL;
990
991 cpu_buffer->cpu = cpu;
992 cpu_buffer->buffer = buffer;
Steven Rostedtf83c9d02008-11-11 18:47:44 +0100993 spin_lock_init(&cpu_buffer->reader_lock);
Peter Zijlstra1f8a6a12009-06-08 18:18:39 +0200994 lockdep_set_class(&cpu_buffer->reader_lock, buffer->reader_lock_key);
Steven Rostedt3e03fb72008-11-06 00:09:43 -0500995 cpu_buffer->lock = (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400996
Steven Rostedt044fa782008-12-02 23:50:03 -0500997 bpage = kzalloc_node(ALIGN(sizeof(*bpage), cache_line_size()),
Steven Rostedte4c2ce82008-10-01 11:14:54 -0400998 GFP_KERNEL, cpu_to_node(cpu));
Steven Rostedt044fa782008-12-02 23:50:03 -0500999 if (!bpage)
Steven Rostedte4c2ce82008-10-01 11:14:54 -04001000 goto fail_free_buffer;
1001
Steven Rostedt77ae3652009-03-27 11:00:29 -04001002 rb_check_bpage(cpu_buffer, bpage);
1003
Steven Rostedt044fa782008-12-02 23:50:03 -05001004 cpu_buffer->reader_page = bpage;
Steven Rostedtd7690412008-10-01 00:29:53 -04001005 addr = __get_free_page(GFP_KERNEL);
1006 if (!addr)
Steven Rostedte4c2ce82008-10-01 11:14:54 -04001007 goto fail_free_reader;
Steven Rostedt044fa782008-12-02 23:50:03 -05001008 bpage->page = (void *)addr;
1009 rb_init_page(bpage->page);
Steven Rostedte4c2ce82008-10-01 11:14:54 -04001010
Steven Rostedtd7690412008-10-01 00:29:53 -04001011 INIT_LIST_HEAD(&cpu_buffer->reader_page->list);
Steven Rostedtd7690412008-10-01 00:29:53 -04001012
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001013 ret = rb_allocate_pages(cpu_buffer, buffer->pages);
1014 if (ret < 0)
Steven Rostedtd7690412008-10-01 00:29:53 -04001015 goto fail_free_reader;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001016
1017 cpu_buffer->head_page
Steven Rostedt3adc54f2009-03-30 15:32:01 -04001018 = list_entry(cpu_buffer->pages, struct buffer_page, list);
Steven Rostedtbf41a152008-10-04 02:00:59 -04001019 cpu_buffer->tail_page = cpu_buffer->commit_page = cpu_buffer->head_page;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001020
Steven Rostedt77ae3652009-03-27 11:00:29 -04001021 rb_head_page_activate(cpu_buffer);
1022
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001023 return cpu_buffer;
1024
Steven Rostedtd7690412008-10-01 00:29:53 -04001025 fail_free_reader:
1026 free_buffer_page(cpu_buffer->reader_page);
1027
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001028 fail_free_buffer:
1029 kfree(cpu_buffer);
1030 return NULL;
1031}
1032
1033static void rb_free_cpu_buffer(struct ring_buffer_per_cpu *cpu_buffer)
1034{
Steven Rostedt3adc54f2009-03-30 15:32:01 -04001035 struct list_head *head = cpu_buffer->pages;
Steven Rostedt044fa782008-12-02 23:50:03 -05001036 struct buffer_page *bpage, *tmp;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001037
Steven Rostedtd7690412008-10-01 00:29:53 -04001038 free_buffer_page(cpu_buffer->reader_page);
1039
Steven Rostedt77ae3652009-03-27 11:00:29 -04001040 rb_head_page_deactivate(cpu_buffer);
1041
Steven Rostedt3adc54f2009-03-30 15:32:01 -04001042 if (head) {
1043 list_for_each_entry_safe(bpage, tmp, head, list) {
1044 list_del_init(&bpage->list);
1045 free_buffer_page(bpage);
1046 }
1047 bpage = list_entry(head, struct buffer_page, list);
Steven Rostedt044fa782008-12-02 23:50:03 -05001048 free_buffer_page(bpage);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001049 }
Steven Rostedt3adc54f2009-03-30 15:32:01 -04001050
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001051 kfree(cpu_buffer);
1052}
1053
Steven Rostedt59222ef2009-03-12 11:46:03 -04001054#ifdef CONFIG_HOTPLUG_CPU
Frederic Weisbecker09c9e842009-03-21 04:33:36 +01001055static int rb_cpu_notify(struct notifier_block *self,
1056 unsigned long action, void *hcpu);
Steven Rostedt554f7862009-03-11 22:00:13 -04001057#endif
1058
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001059/**
1060 * ring_buffer_alloc - allocate a new ring_buffer
Robert Richter68814b52008-11-24 12:24:12 +01001061 * @size: the size in bytes per cpu that is needed.
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001062 * @flags: attributes to set for the ring buffer.
1063 *
1064 * Currently the only flag that is available is the RB_FL_OVERWRITE
1065 * flag. This flag means that the buffer will overwrite old data
1066 * when the buffer wraps. If this flag is not set, the buffer will
1067 * drop data when the tail hits the head.
1068 */
Peter Zijlstra1f8a6a12009-06-08 18:18:39 +02001069struct ring_buffer *__ring_buffer_alloc(unsigned long size, unsigned flags,
1070 struct lock_class_key *key)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001071{
1072 struct ring_buffer *buffer;
1073 int bsize;
1074 int cpu;
1075
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001076 /* keep it in its own cache line */
1077 buffer = kzalloc(ALIGN(sizeof(*buffer), cache_line_size()),
1078 GFP_KERNEL);
1079 if (!buffer)
1080 return NULL;
1081
Rusty Russell9e01c1b2009-01-01 10:12:22 +10301082 if (!alloc_cpumask_var(&buffer->cpumask, GFP_KERNEL))
1083 goto fail_free_buffer;
1084
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001085 buffer->pages = DIV_ROUND_UP(size, BUF_PAGE_SIZE);
1086 buffer->flags = flags;
Steven Rostedt37886f62009-03-17 17:22:06 -04001087 buffer->clock = trace_clock_local;
Peter Zijlstra1f8a6a12009-06-08 18:18:39 +02001088 buffer->reader_lock_key = key;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001089
1090 /* need at least two pages */
Steven Rostedt5f78abe2009-06-17 14:11:10 -04001091 if (buffer->pages < 2)
1092 buffer->pages = 2;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001093
Frederic Weisbecker3bf832c2009-03-19 14:47:33 +01001094 /*
1095 * In case of non-hotplug cpu, if the ring-buffer is allocated
1096 * in early initcall, it will not be notified of secondary cpus.
1097 * In that off case, we need to allocate for all possible cpus.
1098 */
1099#ifdef CONFIG_HOTPLUG_CPU
Steven Rostedt554f7862009-03-11 22:00:13 -04001100 get_online_cpus();
1101 cpumask_copy(buffer->cpumask, cpu_online_mask);
Frederic Weisbecker3bf832c2009-03-19 14:47:33 +01001102#else
1103 cpumask_copy(buffer->cpumask, cpu_possible_mask);
1104#endif
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001105 buffer->cpus = nr_cpu_ids;
1106
1107 bsize = sizeof(void *) * nr_cpu_ids;
1108 buffer->buffers = kzalloc(ALIGN(bsize, cache_line_size()),
1109 GFP_KERNEL);
1110 if (!buffer->buffers)
Rusty Russell9e01c1b2009-01-01 10:12:22 +10301111 goto fail_free_cpumask;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001112
1113 for_each_buffer_cpu(buffer, cpu) {
1114 buffer->buffers[cpu] =
1115 rb_allocate_cpu_buffer(buffer, cpu);
1116 if (!buffer->buffers[cpu])
1117 goto fail_free_buffers;
1118 }
1119
Steven Rostedt59222ef2009-03-12 11:46:03 -04001120#ifdef CONFIG_HOTPLUG_CPU
Steven Rostedt554f7862009-03-11 22:00:13 -04001121 buffer->cpu_notify.notifier_call = rb_cpu_notify;
1122 buffer->cpu_notify.priority = 0;
1123 register_cpu_notifier(&buffer->cpu_notify);
1124#endif
1125
1126 put_online_cpus();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001127 mutex_init(&buffer->mutex);
1128
1129 return buffer;
1130
1131 fail_free_buffers:
1132 for_each_buffer_cpu(buffer, cpu) {
1133 if (buffer->buffers[cpu])
1134 rb_free_cpu_buffer(buffer->buffers[cpu]);
1135 }
1136 kfree(buffer->buffers);
1137
Rusty Russell9e01c1b2009-01-01 10:12:22 +10301138 fail_free_cpumask:
1139 free_cpumask_var(buffer->cpumask);
Steven Rostedt554f7862009-03-11 22:00:13 -04001140 put_online_cpus();
Rusty Russell9e01c1b2009-01-01 10:12:22 +10301141
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001142 fail_free_buffer:
1143 kfree(buffer);
1144 return NULL;
1145}
Peter Zijlstra1f8a6a12009-06-08 18:18:39 +02001146EXPORT_SYMBOL_GPL(__ring_buffer_alloc);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001147
1148/**
1149 * ring_buffer_free - free a ring buffer.
1150 * @buffer: the buffer to free.
1151 */
1152void
1153ring_buffer_free(struct ring_buffer *buffer)
1154{
1155 int cpu;
1156
Steven Rostedt554f7862009-03-11 22:00:13 -04001157 get_online_cpus();
1158
Steven Rostedt59222ef2009-03-12 11:46:03 -04001159#ifdef CONFIG_HOTPLUG_CPU
Steven Rostedt554f7862009-03-11 22:00:13 -04001160 unregister_cpu_notifier(&buffer->cpu_notify);
1161#endif
1162
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001163 for_each_buffer_cpu(buffer, cpu)
1164 rb_free_cpu_buffer(buffer->buffers[cpu]);
1165
Steven Rostedt554f7862009-03-11 22:00:13 -04001166 put_online_cpus();
1167
Eric Dumazetbd3f0222009-08-07 12:49:29 +02001168 kfree(buffer->buffers);
Rusty Russell9e01c1b2009-01-01 10:12:22 +10301169 free_cpumask_var(buffer->cpumask);
1170
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001171 kfree(buffer);
1172}
Robert Richterc4f50182008-12-11 16:49:22 +01001173EXPORT_SYMBOL_GPL(ring_buffer_free);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001174
Steven Rostedt37886f62009-03-17 17:22:06 -04001175void ring_buffer_set_clock(struct ring_buffer *buffer,
1176 u64 (*clock)(void))
1177{
1178 buffer->clock = clock;
1179}
1180
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001181static void rb_reset_cpu(struct ring_buffer_per_cpu *cpu_buffer);
1182
1183static void
1184rb_remove_pages(struct ring_buffer_per_cpu *cpu_buffer, unsigned nr_pages)
1185{
Steven Rostedt044fa782008-12-02 23:50:03 -05001186 struct buffer_page *bpage;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001187 struct list_head *p;
1188 unsigned i;
1189
1190 atomic_inc(&cpu_buffer->record_disabled);
1191 synchronize_sched();
1192
Steven Rostedt77ae3652009-03-27 11:00:29 -04001193 rb_head_page_deactivate(cpu_buffer);
1194
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001195 for (i = 0; i < nr_pages; i++) {
Steven Rostedt3adc54f2009-03-30 15:32:01 -04001196 if (RB_WARN_ON(cpu_buffer, list_empty(cpu_buffer->pages)))
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05001197 return;
Steven Rostedt3adc54f2009-03-30 15:32:01 -04001198 p = cpu_buffer->pages->next;
Steven Rostedt044fa782008-12-02 23:50:03 -05001199 bpage = list_entry(p, struct buffer_page, list);
1200 list_del_init(&bpage->list);
1201 free_buffer_page(bpage);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001202 }
Steven Rostedt3adc54f2009-03-30 15:32:01 -04001203 if (RB_WARN_ON(cpu_buffer, list_empty(cpu_buffer->pages)))
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05001204 return;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001205
1206 rb_reset_cpu(cpu_buffer);
1207
1208 rb_check_pages(cpu_buffer);
1209
1210 atomic_dec(&cpu_buffer->record_disabled);
1211
1212}
1213
1214static void
1215rb_insert_pages(struct ring_buffer_per_cpu *cpu_buffer,
1216 struct list_head *pages, unsigned nr_pages)
1217{
Steven Rostedt044fa782008-12-02 23:50:03 -05001218 struct buffer_page *bpage;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001219 struct list_head *p;
1220 unsigned i;
1221
1222 atomic_inc(&cpu_buffer->record_disabled);
1223 synchronize_sched();
1224
Steven Rostedt77ae3652009-03-27 11:00:29 -04001225 spin_lock_irq(&cpu_buffer->reader_lock);
1226 rb_head_page_deactivate(cpu_buffer);
1227
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001228 for (i = 0; i < nr_pages; i++) {
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05001229 if (RB_WARN_ON(cpu_buffer, list_empty(pages)))
1230 return;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001231 p = pages->next;
Steven Rostedt044fa782008-12-02 23:50:03 -05001232 bpage = list_entry(p, struct buffer_page, list);
1233 list_del_init(&bpage->list);
Steven Rostedt3adc54f2009-03-30 15:32:01 -04001234 list_add_tail(&bpage->list, cpu_buffer->pages);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001235 }
1236 rb_reset_cpu(cpu_buffer);
Steven Rostedt77ae3652009-03-27 11:00:29 -04001237 spin_unlock_irq(&cpu_buffer->reader_lock);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001238
1239 rb_check_pages(cpu_buffer);
1240
1241 atomic_dec(&cpu_buffer->record_disabled);
1242}
1243
1244/**
1245 * ring_buffer_resize - resize the ring buffer
1246 * @buffer: the buffer to resize.
1247 * @size: the new size.
1248 *
1249 * The tracer is responsible for making sure that the buffer is
1250 * not being used while changing the size.
1251 * Note: We may be able to change the above requirement by using
1252 * RCU synchronizations.
1253 *
1254 * Minimum size is 2 * BUF_PAGE_SIZE.
1255 *
1256 * Returns -1 on failure.
1257 */
1258int ring_buffer_resize(struct ring_buffer *buffer, unsigned long size)
1259{
1260 struct ring_buffer_per_cpu *cpu_buffer;
1261 unsigned nr_pages, rm_pages, new_pages;
Steven Rostedt044fa782008-12-02 23:50:03 -05001262 struct buffer_page *bpage, *tmp;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001263 unsigned long buffer_size;
1264 unsigned long addr;
1265 LIST_HEAD(pages);
1266 int i, cpu;
1267
Ingo Molnaree51a1d2008-11-13 14:58:31 +01001268 /*
1269 * Always succeed at resizing a non-existent buffer:
1270 */
1271 if (!buffer)
1272 return size;
1273
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001274 size = DIV_ROUND_UP(size, BUF_PAGE_SIZE);
1275 size *= BUF_PAGE_SIZE;
1276 buffer_size = buffer->pages * BUF_PAGE_SIZE;
1277
1278 /* we need a minimum of two pages */
1279 if (size < BUF_PAGE_SIZE * 2)
1280 size = BUF_PAGE_SIZE * 2;
1281
1282 if (size == buffer_size)
1283 return size;
1284
1285 mutex_lock(&buffer->mutex);
Steven Rostedt554f7862009-03-11 22:00:13 -04001286 get_online_cpus();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001287
1288 nr_pages = DIV_ROUND_UP(size, BUF_PAGE_SIZE);
1289
1290 if (size < buffer_size) {
1291
1292 /* easy case, just free pages */
Steven Rostedt554f7862009-03-11 22:00:13 -04001293 if (RB_WARN_ON(buffer, nr_pages >= buffer->pages))
1294 goto out_fail;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001295
1296 rm_pages = buffer->pages - nr_pages;
1297
1298 for_each_buffer_cpu(buffer, cpu) {
1299 cpu_buffer = buffer->buffers[cpu];
1300 rb_remove_pages(cpu_buffer, rm_pages);
1301 }
1302 goto out;
1303 }
1304
1305 /*
1306 * This is a bit more difficult. We only want to add pages
1307 * when we can allocate enough for all CPUs. We do this
1308 * by allocating all the pages and storing them on a local
1309 * link list. If we succeed in our allocation, then we
1310 * add these pages to the cpu_buffers. Otherwise we just free
1311 * them all and return -ENOMEM;
1312 */
Steven Rostedt554f7862009-03-11 22:00:13 -04001313 if (RB_WARN_ON(buffer, nr_pages <= buffer->pages))
1314 goto out_fail;
Steven Rostedtf536aaf2008-11-10 23:07:30 -05001315
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001316 new_pages = nr_pages - buffer->pages;
1317
1318 for_each_buffer_cpu(buffer, cpu) {
1319 for (i = 0; i < new_pages; i++) {
Steven Rostedt044fa782008-12-02 23:50:03 -05001320 bpage = kzalloc_node(ALIGN(sizeof(*bpage),
Steven Rostedte4c2ce82008-10-01 11:14:54 -04001321 cache_line_size()),
1322 GFP_KERNEL, cpu_to_node(cpu));
Steven Rostedt044fa782008-12-02 23:50:03 -05001323 if (!bpage)
Steven Rostedte4c2ce82008-10-01 11:14:54 -04001324 goto free_pages;
Steven Rostedt044fa782008-12-02 23:50:03 -05001325 list_add(&bpage->list, &pages);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001326 addr = __get_free_page(GFP_KERNEL);
1327 if (!addr)
1328 goto free_pages;
Steven Rostedt044fa782008-12-02 23:50:03 -05001329 bpage->page = (void *)addr;
1330 rb_init_page(bpage->page);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001331 }
1332 }
1333
1334 for_each_buffer_cpu(buffer, cpu) {
1335 cpu_buffer = buffer->buffers[cpu];
1336 rb_insert_pages(cpu_buffer, &pages, new_pages);
1337 }
1338
Steven Rostedt554f7862009-03-11 22:00:13 -04001339 if (RB_WARN_ON(buffer, !list_empty(&pages)))
1340 goto out_fail;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001341
1342 out:
1343 buffer->pages = nr_pages;
Steven Rostedt554f7862009-03-11 22:00:13 -04001344 put_online_cpus();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001345 mutex_unlock(&buffer->mutex);
1346
1347 return size;
1348
1349 free_pages:
Steven Rostedt044fa782008-12-02 23:50:03 -05001350 list_for_each_entry_safe(bpage, tmp, &pages, list) {
1351 list_del_init(&bpage->list);
1352 free_buffer_page(bpage);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001353 }
Steven Rostedt554f7862009-03-11 22:00:13 -04001354 put_online_cpus();
Vegard Nossum641d2f62008-11-18 19:22:13 +01001355 mutex_unlock(&buffer->mutex);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001356 return -ENOMEM;
Steven Rostedt554f7862009-03-11 22:00:13 -04001357
1358 /*
1359 * Something went totally wrong, and we are too paranoid
1360 * to even clean up the mess.
1361 */
1362 out_fail:
1363 put_online_cpus();
1364 mutex_unlock(&buffer->mutex);
1365 return -1;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001366}
Robert Richterc4f50182008-12-11 16:49:22 +01001367EXPORT_SYMBOL_GPL(ring_buffer_resize);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001368
Steven Rostedt8789a9e2008-12-02 15:34:07 -05001369static inline void *
Steven Rostedt044fa782008-12-02 23:50:03 -05001370__rb_data_page_index(struct buffer_data_page *bpage, unsigned index)
Steven Rostedt8789a9e2008-12-02 15:34:07 -05001371{
Steven Rostedt044fa782008-12-02 23:50:03 -05001372 return bpage->data + index;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05001373}
1374
Steven Rostedt044fa782008-12-02 23:50:03 -05001375static inline void *__rb_page_index(struct buffer_page *bpage, unsigned index)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001376{
Steven Rostedt044fa782008-12-02 23:50:03 -05001377 return bpage->page->data + index;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001378}
1379
1380static inline struct ring_buffer_event *
Steven Rostedtd7690412008-10-01 00:29:53 -04001381rb_reader_event(struct ring_buffer_per_cpu *cpu_buffer)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001382{
Steven Rostedt6f807ac2008-10-04 02:00:58 -04001383 return __rb_page_index(cpu_buffer->reader_page,
1384 cpu_buffer->reader_page->read);
1385}
1386
1387static inline struct ring_buffer_event *
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001388rb_iter_head_event(struct ring_buffer_iter *iter)
1389{
Steven Rostedt6f807ac2008-10-04 02:00:58 -04001390 return __rb_page_index(iter->head_page, iter->head);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001391}
1392
Steven Rostedt77ae3652009-03-27 11:00:29 -04001393static inline unsigned long rb_page_write(struct buffer_page *bpage)
Steven Rostedtbf41a152008-10-04 02:00:59 -04001394{
Steven Rostedt77ae3652009-03-27 11:00:29 -04001395 return local_read(&bpage->write) & RB_WRITE_MASK;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001396}
1397
1398static inline unsigned rb_page_commit(struct buffer_page *bpage)
1399{
Steven Rostedtabc9b562008-12-02 15:34:06 -05001400 return local_read(&bpage->page->commit);
Steven Rostedtbf41a152008-10-04 02:00:59 -04001401}
1402
Steven Rostedt77ae3652009-03-27 11:00:29 -04001403static inline unsigned long rb_page_entries(struct buffer_page *bpage)
1404{
1405 return local_read(&bpage->entries) & RB_WRITE_MASK;
1406}
1407
Steven Rostedtbf41a152008-10-04 02:00:59 -04001408/* Size is determined by what has been commited */
1409static inline unsigned rb_page_size(struct buffer_page *bpage)
1410{
1411 return rb_page_commit(bpage);
1412}
1413
1414static inline unsigned
1415rb_commit_index(struct ring_buffer_per_cpu *cpu_buffer)
1416{
1417 return rb_page_commit(cpu_buffer->commit_page);
1418}
1419
Steven Rostedtbf41a152008-10-04 02:00:59 -04001420static inline unsigned
1421rb_event_index(struct ring_buffer_event *event)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001422{
Steven Rostedtbf41a152008-10-04 02:00:59 -04001423 unsigned long addr = (unsigned long)event;
1424
Steven Rostedt22f470f2009-06-11 09:29:58 -04001425 return (addr & ~PAGE_MASK) - BUF_PAGE_HDR_SIZE;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001426}
1427
Steven Rostedt0f0c85f2009-05-11 16:08:00 -04001428static inline int
Steven Rostedtfa743952009-06-16 12:37:57 -04001429rb_event_is_commit(struct ring_buffer_per_cpu *cpu_buffer,
1430 struct ring_buffer_event *event)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001431{
Steven Rostedtbf41a152008-10-04 02:00:59 -04001432 unsigned long addr = (unsigned long)event;
1433 unsigned long index;
1434
1435 index = rb_event_index(event);
1436 addr &= PAGE_MASK;
1437
1438 return cpu_buffer->commit_page->page == (void *)addr &&
1439 rb_commit_index(cpu_buffer) == index;
1440}
1441
Andrew Morton34a148b2009-01-09 12:27:09 -08001442static void
Steven Rostedtbf41a152008-10-04 02:00:59 -04001443rb_set_commit_to_write(struct ring_buffer_per_cpu *cpu_buffer)
1444{
Steven Rostedt77ae3652009-03-27 11:00:29 -04001445 unsigned long max_count;
1446
Steven Rostedtbf41a152008-10-04 02:00:59 -04001447 /*
1448 * We only race with interrupts and NMIs on this CPU.
1449 * If we own the commit event, then we can commit
1450 * all others that interrupted us, since the interruptions
1451 * are in stack format (they finish before they come
1452 * back to us). This allows us to do a simple loop to
1453 * assign the commit to the tail.
1454 */
Steven Rostedta8ccf1d2008-12-23 11:32:24 -05001455 again:
Steven Rostedt77ae3652009-03-27 11:00:29 -04001456 max_count = cpu_buffer->buffer->pages * 100;
1457
Steven Rostedtbf41a152008-10-04 02:00:59 -04001458 while (cpu_buffer->commit_page != cpu_buffer->tail_page) {
Steven Rostedt77ae3652009-03-27 11:00:29 -04001459 if (RB_WARN_ON(cpu_buffer, !(--max_count)))
1460 return;
1461 if (RB_WARN_ON(cpu_buffer,
1462 rb_is_reader_page(cpu_buffer->tail_page)))
1463 return;
1464 local_set(&cpu_buffer->commit_page->page->commit,
1465 rb_page_write(cpu_buffer->commit_page));
Steven Rostedtbf41a152008-10-04 02:00:59 -04001466 rb_inc_page(cpu_buffer, &cpu_buffer->commit_page);
Steven Rostedtabc9b562008-12-02 15:34:06 -05001467 cpu_buffer->write_stamp =
1468 cpu_buffer->commit_page->page->time_stamp;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001469 /* add barrier to keep gcc from optimizing too much */
1470 barrier();
1471 }
1472 while (rb_commit_index(cpu_buffer) !=
1473 rb_page_write(cpu_buffer->commit_page)) {
Steven Rostedt77ae3652009-03-27 11:00:29 -04001474
1475 local_set(&cpu_buffer->commit_page->page->commit,
1476 rb_page_write(cpu_buffer->commit_page));
1477 RB_WARN_ON(cpu_buffer,
1478 local_read(&cpu_buffer->commit_page->page->commit) &
1479 ~RB_WRITE_MASK);
Steven Rostedtbf41a152008-10-04 02:00:59 -04001480 barrier();
1481 }
Steven Rostedta8ccf1d2008-12-23 11:32:24 -05001482
1483 /* again, keep gcc from optimizing */
1484 barrier();
1485
1486 /*
1487 * If an interrupt came in just after the first while loop
1488 * and pushed the tail page forward, we will be left with
1489 * a dangling commit that will never go forward.
1490 */
1491 if (unlikely(cpu_buffer->commit_page != cpu_buffer->tail_page))
1492 goto again;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001493}
1494
Steven Rostedtd7690412008-10-01 00:29:53 -04001495static void rb_reset_reader_page(struct ring_buffer_per_cpu *cpu_buffer)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001496{
Steven Rostedtabc9b562008-12-02 15:34:06 -05001497 cpu_buffer->read_stamp = cpu_buffer->reader_page->page->time_stamp;
Steven Rostedt6f807ac2008-10-04 02:00:58 -04001498 cpu_buffer->reader_page->read = 0;
Steven Rostedtd7690412008-10-01 00:29:53 -04001499}
1500
Andrew Morton34a148b2009-01-09 12:27:09 -08001501static void rb_inc_iter(struct ring_buffer_iter *iter)
Steven Rostedtd7690412008-10-01 00:29:53 -04001502{
1503 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
1504
1505 /*
1506 * The iterator could be on the reader page (it starts there).
1507 * But the head could have moved, since the reader was
1508 * found. Check for this case and assign the iterator
1509 * to the head page instead of next.
1510 */
1511 if (iter->head_page == cpu_buffer->reader_page)
Steven Rostedt77ae3652009-03-27 11:00:29 -04001512 iter->head_page = rb_set_head_page(cpu_buffer);
Steven Rostedtd7690412008-10-01 00:29:53 -04001513 else
1514 rb_inc_page(cpu_buffer, &iter->head_page);
1515
Steven Rostedtabc9b562008-12-02 15:34:06 -05001516 iter->read_stamp = iter->head_page->page->time_stamp;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001517 iter->head = 0;
1518}
1519
1520/**
1521 * ring_buffer_update_event - update event type and data
1522 * @event: the even to update
1523 * @type: the type of event
1524 * @length: the size of the event field in the ring buffer
1525 *
1526 * Update the type and data fields of the event. The length
1527 * is the actual size that is written to the ring buffer,
1528 * and with this, we can determine what to place into the
1529 * data field.
1530 */
Andrew Morton34a148b2009-01-09 12:27:09 -08001531static void
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001532rb_update_event(struct ring_buffer_event *event,
1533 unsigned type, unsigned length)
1534{
Lai Jiangshan334d4162009-04-24 11:27:05 +08001535 event->type_len = type;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001536
1537 switch (type) {
1538
1539 case RINGBUF_TYPE_PADDING:
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001540 case RINGBUF_TYPE_TIME_EXTEND:
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001541 case RINGBUF_TYPE_TIME_STAMP:
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001542 break;
1543
Lai Jiangshan334d4162009-04-24 11:27:05 +08001544 case 0:
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001545 length -= RB_EVNT_HDR_SIZE;
Lai Jiangshan334d4162009-04-24 11:27:05 +08001546 if (length > RB_MAX_SMALL_DATA)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001547 event->array[0] = length;
Lai Jiangshan334d4162009-04-24 11:27:05 +08001548 else
1549 event->type_len = DIV_ROUND_UP(length, RB_ALIGNMENT);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001550 break;
1551 default:
1552 BUG();
1553 }
1554}
1555
Steven Rostedt77ae3652009-03-27 11:00:29 -04001556/*
1557 * rb_handle_head_page - writer hit the head page
1558 *
1559 * Returns: +1 to retry page
1560 * 0 to continue
1561 * -1 on error
1562 */
1563static int
1564rb_handle_head_page(struct ring_buffer_per_cpu *cpu_buffer,
1565 struct buffer_page *tail_page,
1566 struct buffer_page *next_page)
1567{
1568 struct buffer_page *new_head;
1569 int entries;
1570 int type;
1571 int ret;
1572
1573 entries = rb_page_entries(next_page);
1574
1575 /*
1576 * The hard part is here. We need to move the head
1577 * forward, and protect against both readers on
1578 * other CPUs and writers coming in via interrupts.
1579 */
1580 type = rb_head_page_set_update(cpu_buffer, next_page, tail_page,
1581 RB_PAGE_HEAD);
1582
1583 /*
1584 * type can be one of four:
1585 * NORMAL - an interrupt already moved it for us
1586 * HEAD - we are the first to get here.
1587 * UPDATE - we are the interrupt interrupting
1588 * a current move.
1589 * MOVED - a reader on another CPU moved the next
1590 * pointer to its reader page. Give up
1591 * and try again.
1592 */
1593
1594 switch (type) {
1595 case RB_PAGE_HEAD:
1596 /*
1597 * We changed the head to UPDATE, thus
1598 * it is our responsibility to update
1599 * the counters.
1600 */
1601 local_add(entries, &cpu_buffer->overrun);
1602
1603 /*
1604 * The entries will be zeroed out when we move the
1605 * tail page.
1606 */
1607
1608 /* still more to do */
1609 break;
1610
1611 case RB_PAGE_UPDATE:
1612 /*
1613 * This is an interrupt that interrupt the
1614 * previous update. Still more to do.
1615 */
1616 break;
1617 case RB_PAGE_NORMAL:
1618 /*
1619 * An interrupt came in before the update
1620 * and processed this for us.
1621 * Nothing left to do.
1622 */
1623 return 1;
1624 case RB_PAGE_MOVED:
1625 /*
1626 * The reader is on another CPU and just did
1627 * a swap with our next_page.
1628 * Try again.
1629 */
1630 return 1;
1631 default:
1632 RB_WARN_ON(cpu_buffer, 1); /* WTF??? */
1633 return -1;
1634 }
1635
1636 /*
1637 * Now that we are here, the old head pointer is
1638 * set to UPDATE. This will keep the reader from
1639 * swapping the head page with the reader page.
1640 * The reader (on another CPU) will spin till
1641 * we are finished.
1642 *
1643 * We just need to protect against interrupts
1644 * doing the job. We will set the next pointer
1645 * to HEAD. After that, we set the old pointer
1646 * to NORMAL, but only if it was HEAD before.
1647 * otherwise we are an interrupt, and only
1648 * want the outer most commit to reset it.
1649 */
1650 new_head = next_page;
1651 rb_inc_page(cpu_buffer, &new_head);
1652
1653 ret = rb_head_page_set_head(cpu_buffer, new_head, next_page,
1654 RB_PAGE_NORMAL);
1655
1656 /*
1657 * Valid returns are:
1658 * HEAD - an interrupt came in and already set it.
1659 * NORMAL - One of two things:
1660 * 1) We really set it.
1661 * 2) A bunch of interrupts came in and moved
1662 * the page forward again.
1663 */
1664 switch (ret) {
1665 case RB_PAGE_HEAD:
1666 case RB_PAGE_NORMAL:
1667 /* OK */
1668 break;
1669 default:
1670 RB_WARN_ON(cpu_buffer, 1);
1671 return -1;
1672 }
1673
1674 /*
1675 * It is possible that an interrupt came in,
1676 * set the head up, then more interrupts came in
1677 * and moved it again. When we get back here,
1678 * the page would have been set to NORMAL but we
1679 * just set it back to HEAD.
1680 *
1681 * How do you detect this? Well, if that happened
1682 * the tail page would have moved.
1683 */
1684 if (ret == RB_PAGE_NORMAL) {
1685 /*
1686 * If the tail had moved passed next, then we need
1687 * to reset the pointer.
1688 */
1689 if (cpu_buffer->tail_page != tail_page &&
1690 cpu_buffer->tail_page != next_page)
1691 rb_head_page_set_normal(cpu_buffer, new_head,
1692 next_page,
1693 RB_PAGE_HEAD);
1694 }
1695
1696 /*
1697 * If this was the outer most commit (the one that
1698 * changed the original pointer from HEAD to UPDATE),
1699 * then it is up to us to reset it to NORMAL.
1700 */
1701 if (type == RB_PAGE_HEAD) {
1702 ret = rb_head_page_set_normal(cpu_buffer, next_page,
1703 tail_page,
1704 RB_PAGE_UPDATE);
1705 if (RB_WARN_ON(cpu_buffer,
1706 ret != RB_PAGE_UPDATE))
1707 return -1;
1708 }
1709
1710 return 0;
1711}
1712
Andrew Morton34a148b2009-01-09 12:27:09 -08001713static unsigned rb_calculate_event_length(unsigned length)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001714{
1715 struct ring_buffer_event event; /* Used only for sizeof array */
1716
1717 /* zero length can cause confusions */
1718 if (!length)
1719 length = 1;
1720
1721 if (length > RB_MAX_SMALL_DATA)
1722 length += sizeof(event.array[0]);
1723
1724 length += RB_EVNT_HDR_SIZE;
1725 length = ALIGN(length, RB_ALIGNMENT);
1726
1727 return length;
1728}
1729
Steven Rostedtc7b09302009-06-11 11:12:00 -04001730static inline void
1731rb_reset_tail(struct ring_buffer_per_cpu *cpu_buffer,
1732 struct buffer_page *tail_page,
1733 unsigned long tail, unsigned long length)
1734{
1735 struct ring_buffer_event *event;
1736
1737 /*
1738 * Only the event that crossed the page boundary
1739 * must fill the old tail_page with padding.
1740 */
1741 if (tail >= BUF_PAGE_SIZE) {
1742 local_sub(length, &tail_page->write);
1743 return;
1744 }
1745
1746 event = __rb_page_index(tail_page, tail);
Linus Torvaldsb0b70652009-06-20 10:56:46 -07001747 kmemcheck_annotate_bitfield(event, bitfield);
Steven Rostedtc7b09302009-06-11 11:12:00 -04001748
1749 /*
1750 * If this event is bigger than the minimum size, then
1751 * we need to be careful that we don't subtract the
1752 * write counter enough to allow another writer to slip
1753 * in on this page.
1754 * We put in a discarded commit instead, to make sure
1755 * that this space is not used again.
1756 *
1757 * If we are less than the minimum size, we don't need to
1758 * worry about it.
1759 */
1760 if (tail > (BUF_PAGE_SIZE - RB_EVNT_MIN_SIZE)) {
1761 /* No room for any events */
1762
1763 /* Mark the rest of the page with padding */
1764 rb_event_set_padding(event);
1765
1766 /* Set the write back to the previous setting */
1767 local_sub(length, &tail_page->write);
1768 return;
1769 }
1770
1771 /* Put in a discarded event */
1772 event->array[0] = (BUF_PAGE_SIZE - tail) - RB_EVNT_HDR_SIZE;
1773 event->type_len = RINGBUF_TYPE_PADDING;
1774 /* time delta must be non zero */
1775 event->time_delta = 1;
Steven Rostedtc7b09302009-06-11 11:12:00 -04001776
1777 /* Set write to end of buffer */
1778 length = (tail + length) - BUF_PAGE_SIZE;
1779 local_sub(length, &tail_page->write);
1780}
Steven Rostedt6634ff22009-05-06 15:30:07 -04001781
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001782static struct ring_buffer_event *
Steven Rostedt6634ff22009-05-06 15:30:07 -04001783rb_move_tail(struct ring_buffer_per_cpu *cpu_buffer,
1784 unsigned long length, unsigned long tail,
1785 struct buffer_page *commit_page,
1786 struct buffer_page *tail_page, u64 *ts)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001787{
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001788 struct ring_buffer *buffer = cpu_buffer->buffer;
Steven Rostedt77ae3652009-03-27 11:00:29 -04001789 struct buffer_page *next_page;
1790 int ret;
Steven Rostedtaa20ae82009-05-05 21:16:11 -04001791
1792 next_page = tail_page;
1793
Steven Rostedtaa20ae82009-05-05 21:16:11 -04001794 rb_inc_page(cpu_buffer, &next_page);
1795
Steven Rostedtaa20ae82009-05-05 21:16:11 -04001796 /*
1797 * If for some reason, we had an interrupt storm that made
1798 * it all the way around the buffer, bail, and warn
1799 * about it.
1800 */
1801 if (unlikely(next_page == commit_page)) {
Steven Rostedt77ae3652009-03-27 11:00:29 -04001802 local_inc(&cpu_buffer->commit_overrun);
Steven Rostedtaa20ae82009-05-05 21:16:11 -04001803 goto out_reset;
1804 }
1805
Steven Rostedt77ae3652009-03-27 11:00:29 -04001806 /*
1807 * This is where the fun begins!
1808 *
1809 * We are fighting against races between a reader that
1810 * could be on another CPU trying to swap its reader
1811 * page with the buffer head.
1812 *
1813 * We are also fighting against interrupts coming in and
1814 * moving the head or tail on us as well.
1815 *
1816 * If the next page is the head page then we have filled
1817 * the buffer, unless the commit page is still on the
1818 * reader page.
1819 */
1820 if (rb_is_head_page(cpu_buffer, next_page, &tail_page->list)) {
Steven Rostedtaa20ae82009-05-05 21:16:11 -04001821
Steven Rostedt77ae3652009-03-27 11:00:29 -04001822 /*
1823 * If the commit is not on the reader page, then
1824 * move the header page.
1825 */
1826 if (!rb_is_reader_page(cpu_buffer->commit_page)) {
1827 /*
1828 * If we are not in overwrite mode,
1829 * this is easy, just stop here.
1830 */
1831 if (!(buffer->flags & RB_FL_OVERWRITE))
1832 goto out_reset;
Steven Rostedtaa20ae82009-05-05 21:16:11 -04001833
Steven Rostedt77ae3652009-03-27 11:00:29 -04001834 ret = rb_handle_head_page(cpu_buffer,
1835 tail_page,
1836 next_page);
1837 if (ret < 0)
1838 goto out_reset;
1839 if (ret)
1840 goto out_again;
1841 } else {
1842 /*
1843 * We need to be careful here too. The
1844 * commit page could still be on the reader
1845 * page. We could have a small buffer, and
1846 * have filled up the buffer with events
1847 * from interrupts and such, and wrapped.
1848 *
1849 * Note, if the tail page is also the on the
1850 * reader_page, we let it move out.
1851 */
1852 if (unlikely((cpu_buffer->commit_page !=
1853 cpu_buffer->tail_page) &&
1854 (cpu_buffer->commit_page ==
1855 cpu_buffer->reader_page))) {
1856 local_inc(&cpu_buffer->commit_overrun);
1857 goto out_reset;
1858 }
Steven Rostedtaa20ae82009-05-05 21:16:11 -04001859 }
1860 }
1861
Steven Rostedt77ae3652009-03-27 11:00:29 -04001862 ret = rb_tail_page_update(cpu_buffer, tail_page, next_page);
1863 if (ret) {
1864 /*
1865 * Nested commits always have zero deltas, so
1866 * just reread the time stamp
1867 */
Steven Rostedt88eb0122009-05-11 16:28:23 -04001868 *ts = rb_time_stamp(buffer, cpu_buffer->cpu);
Steven Rostedt77ae3652009-03-27 11:00:29 -04001869 next_page->page->time_stamp = *ts;
Steven Rostedtaa20ae82009-05-05 21:16:11 -04001870 }
1871
Steven Rostedt77ae3652009-03-27 11:00:29 -04001872 out_again:
Steven Rostedtaa20ae82009-05-05 21:16:11 -04001873
Steven Rostedt77ae3652009-03-27 11:00:29 -04001874 rb_reset_tail(cpu_buffer, tail_page, tail, length);
Steven Rostedtaa20ae82009-05-05 21:16:11 -04001875
1876 /* fail and let the caller try again */
1877 return ERR_PTR(-EAGAIN);
1878
Steven Rostedt45141d42009-02-12 13:19:48 -05001879 out_reset:
Lai Jiangshan6f3b3442009-01-12 11:06:18 +08001880 /* reset write */
Steven Rostedtc7b09302009-06-11 11:12:00 -04001881 rb_reset_tail(cpu_buffer, tail_page, tail, length);
Lai Jiangshan6f3b3442009-01-12 11:06:18 +08001882
Steven Rostedtbf41a152008-10-04 02:00:59 -04001883 return NULL;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001884}
1885
Steven Rostedt6634ff22009-05-06 15:30:07 -04001886static struct ring_buffer_event *
1887__rb_reserve_next(struct ring_buffer_per_cpu *cpu_buffer,
1888 unsigned type, unsigned long length, u64 *ts)
1889{
1890 struct buffer_page *tail_page, *commit_page;
1891 struct ring_buffer_event *event;
1892 unsigned long tail, write;
1893
1894 commit_page = cpu_buffer->commit_page;
1895 /* we just need to protect against interrupts */
1896 barrier();
1897 tail_page = cpu_buffer->tail_page;
1898 write = local_add_return(length, &tail_page->write);
Steven Rostedt77ae3652009-03-27 11:00:29 -04001899
1900 /* set write to only the index of the write */
1901 write &= RB_WRITE_MASK;
Steven Rostedt6634ff22009-05-06 15:30:07 -04001902 tail = write - length;
1903
1904 /* See if we shot pass the end of this buffer page */
1905 if (write > BUF_PAGE_SIZE)
1906 return rb_move_tail(cpu_buffer, length, tail,
1907 commit_page, tail_page, ts);
1908
1909 /* We reserved something on the buffer */
1910
Steven Rostedt6634ff22009-05-06 15:30:07 -04001911 event = __rb_page_index(tail_page, tail);
Vegard Nossum1744a212009-02-28 08:29:44 +01001912 kmemcheck_annotate_bitfield(event, bitfield);
Steven Rostedt6634ff22009-05-06 15:30:07 -04001913 rb_update_event(event, type, length);
1914
1915 /* The passed in type is zero for DATA */
1916 if (likely(!type))
1917 local_inc(&tail_page->entries);
1918
1919 /*
Steven Rostedtfa743952009-06-16 12:37:57 -04001920 * If this is the first commit on the page, then update
1921 * its timestamp.
Steven Rostedt6634ff22009-05-06 15:30:07 -04001922 */
Steven Rostedtfa743952009-06-16 12:37:57 -04001923 if (!tail)
1924 tail_page->page->time_stamp = *ts;
Steven Rostedt6634ff22009-05-06 15:30:07 -04001925
1926 return event;
1927}
1928
Steven Rostedtedd813bf2009-06-02 23:00:53 -04001929static inline int
1930rb_try_to_discard(struct ring_buffer_per_cpu *cpu_buffer,
1931 struct ring_buffer_event *event)
1932{
1933 unsigned long new_index, old_index;
1934 struct buffer_page *bpage;
1935 unsigned long index;
1936 unsigned long addr;
1937
1938 new_index = rb_event_index(event);
1939 old_index = new_index + rb_event_length(event);
1940 addr = (unsigned long)event;
1941 addr &= PAGE_MASK;
1942
1943 bpage = cpu_buffer->tail_page;
1944
1945 if (bpage->page == (void *)addr && rb_page_write(bpage) == old_index) {
Steven Rostedt77ae3652009-03-27 11:00:29 -04001946 unsigned long write_mask =
1947 local_read(&bpage->write) & ~RB_WRITE_MASK;
Steven Rostedtedd813bf2009-06-02 23:00:53 -04001948 /*
1949 * This is on the tail page. It is possible that
1950 * a write could come in and move the tail page
1951 * and write to the next page. That is fine
1952 * because we just shorten what is on this page.
1953 */
Steven Rostedt77ae3652009-03-27 11:00:29 -04001954 old_index += write_mask;
1955 new_index += write_mask;
Steven Rostedtedd813bf2009-06-02 23:00:53 -04001956 index = local_cmpxchg(&bpage->write, old_index, new_index);
1957 if (index == old_index)
1958 return 1;
1959 }
1960
1961 /* could not discard */
1962 return 0;
1963}
1964
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001965static int
1966rb_add_time_stamp(struct ring_buffer_per_cpu *cpu_buffer,
1967 u64 *ts, u64 *delta)
1968{
1969 struct ring_buffer_event *event;
1970 static int once;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001971 int ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001972
1973 if (unlikely(*delta > (1ULL << 59) && !once++)) {
1974 printk(KERN_WARNING "Delta way too big! %llu"
1975 " ts=%llu write stamp = %llu\n",
Stephen Rothwelle2862c92008-10-27 17:43:28 +11001976 (unsigned long long)*delta,
1977 (unsigned long long)*ts,
1978 (unsigned long long)cpu_buffer->write_stamp);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001979 WARN_ON(1);
1980 }
1981
1982 /*
1983 * The delta is too big, we to add a
1984 * new timestamp.
1985 */
1986 event = __rb_reserve_next(cpu_buffer,
1987 RINGBUF_TYPE_TIME_EXTEND,
1988 RB_LEN_TIME_EXTEND,
1989 ts);
1990 if (!event)
Steven Rostedtbf41a152008-10-04 02:00:59 -04001991 return -EBUSY;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001992
Steven Rostedtbf41a152008-10-04 02:00:59 -04001993 if (PTR_ERR(event) == -EAGAIN)
1994 return -EAGAIN;
1995
1996 /* Only a commited time event can update the write stamp */
Steven Rostedtfa743952009-06-16 12:37:57 -04001997 if (rb_event_is_commit(cpu_buffer, event)) {
Steven Rostedtbf41a152008-10-04 02:00:59 -04001998 /*
Steven Rostedtfa743952009-06-16 12:37:57 -04001999 * If this is the first on the page, then it was
2000 * updated with the page itself. Try to discard it
2001 * and if we can't just make it zero.
Steven Rostedtbf41a152008-10-04 02:00:59 -04002002 */
2003 if (rb_event_index(event)) {
2004 event->time_delta = *delta & TS_MASK;
2005 event->array[0] = *delta >> TS_SHIFT;
2006 } else {
Steven Rostedtea05b572009-06-03 09:30:10 -04002007 /* try to discard, since we do not need this */
2008 if (!rb_try_to_discard(cpu_buffer, event)) {
2009 /* nope, just zero it */
2010 event->time_delta = 0;
2011 event->array[0] = 0;
2012 }
Steven Rostedtbf41a152008-10-04 02:00:59 -04002013 }
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002014 cpu_buffer->write_stamp = *ts;
Steven Rostedtbf41a152008-10-04 02:00:59 -04002015 /* let the caller know this was the commit */
2016 ret = 1;
2017 } else {
Steven Rostedtedd813bf2009-06-02 23:00:53 -04002018 /* Try to discard the event */
2019 if (!rb_try_to_discard(cpu_buffer, event)) {
2020 /* Darn, this is just wasted space */
2021 event->time_delta = 0;
2022 event->array[0] = 0;
Steven Rostedtedd813bf2009-06-02 23:00:53 -04002023 }
Steven Rostedtf57a8a12009-06-05 14:11:30 -04002024 ret = 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002025 }
2026
Steven Rostedtbf41a152008-10-04 02:00:59 -04002027 *delta = 0;
2028
2029 return ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002030}
2031
Steven Rostedtfa743952009-06-16 12:37:57 -04002032static void rb_start_commit(struct ring_buffer_per_cpu *cpu_buffer)
2033{
2034 local_inc(&cpu_buffer->committing);
2035 local_inc(&cpu_buffer->commits);
2036}
2037
2038static void rb_end_commit(struct ring_buffer_per_cpu *cpu_buffer)
2039{
2040 unsigned long commits;
2041
2042 if (RB_WARN_ON(cpu_buffer,
2043 !local_read(&cpu_buffer->committing)))
2044 return;
2045
2046 again:
2047 commits = local_read(&cpu_buffer->commits);
2048 /* synchronize with interrupts */
2049 barrier();
2050 if (local_read(&cpu_buffer->committing) == 1)
2051 rb_set_commit_to_write(cpu_buffer);
2052
2053 local_dec(&cpu_buffer->committing);
2054
2055 /* synchronize with interrupts */
2056 barrier();
2057
2058 /*
2059 * Need to account for interrupts coming in between the
2060 * updating of the commit page and the clearing of the
2061 * committing counter.
2062 */
2063 if (unlikely(local_read(&cpu_buffer->commits) != commits) &&
2064 !local_read(&cpu_buffer->committing)) {
2065 local_inc(&cpu_buffer->committing);
2066 goto again;
2067 }
2068}
2069
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002070static struct ring_buffer_event *
2071rb_reserve_next_event(struct ring_buffer_per_cpu *cpu_buffer,
Steven Rostedt1cd8d732009-05-11 14:08:09 -04002072 unsigned long length)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002073{
2074 struct ring_buffer_event *event;
Steven Rostedt168b6b12009-05-11 22:11:05 -04002075 u64 ts, delta = 0;
Steven Rostedtbf41a152008-10-04 02:00:59 -04002076 int commit = 0;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002077 int nr_loops = 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002078
Steven Rostedtfa743952009-06-16 12:37:57 -04002079 rb_start_commit(cpu_buffer);
2080
Steven Rostedtbe957c42009-05-11 14:42:53 -04002081 length = rb_calculate_event_length(length);
Steven Rostedtbf41a152008-10-04 02:00:59 -04002082 again:
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002083 /*
2084 * We allow for interrupts to reenter here and do a trace.
2085 * If one does, it will cause this original code to loop
2086 * back here. Even with heavy interrupts happening, this
2087 * should only happen a few times in a row. If this happens
2088 * 1000 times in a row, there must be either an interrupt
2089 * storm or we have something buggy.
2090 * Bail!
2091 */
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05002092 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 1000))
Steven Rostedtfa743952009-06-16 12:37:57 -04002093 goto out_fail;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002094
Steven Rostedt88eb0122009-05-11 16:28:23 -04002095 ts = rb_time_stamp(cpu_buffer->buffer, cpu_buffer->cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002096
Steven Rostedtbf41a152008-10-04 02:00:59 -04002097 /*
2098 * Only the first commit can update the timestamp.
2099 * Yes there is a race here. If an interrupt comes in
2100 * just after the conditional and it traces too, then it
2101 * will also check the deltas. More than one timestamp may
2102 * also be made. But only the entry that did the actual
2103 * commit will be something other than zero.
2104 */
Steven Rostedt0f0c85f2009-05-11 16:08:00 -04002105 if (likely(cpu_buffer->tail_page == cpu_buffer->commit_page &&
2106 rb_page_write(cpu_buffer->tail_page) ==
2107 rb_commit_index(cpu_buffer))) {
Steven Rostedt168b6b12009-05-11 22:11:05 -04002108 u64 diff;
Steven Rostedtbf41a152008-10-04 02:00:59 -04002109
Steven Rostedt168b6b12009-05-11 22:11:05 -04002110 diff = ts - cpu_buffer->write_stamp;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002111
Steven Rostedt168b6b12009-05-11 22:11:05 -04002112 /* make sure this diff is calculated here */
Steven Rostedtbf41a152008-10-04 02:00:59 -04002113 barrier();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002114
Steven Rostedtbf41a152008-10-04 02:00:59 -04002115 /* Did the write stamp get updated already? */
2116 if (unlikely(ts < cpu_buffer->write_stamp))
Steven Rostedt168b6b12009-05-11 22:11:05 -04002117 goto get_event;
Steven Rostedtbf41a152008-10-04 02:00:59 -04002118
Steven Rostedt168b6b12009-05-11 22:11:05 -04002119 delta = diff;
2120 if (unlikely(test_time_stamp(delta))) {
Steven Rostedtbf41a152008-10-04 02:00:59 -04002121
2122 commit = rb_add_time_stamp(cpu_buffer, &ts, &delta);
Steven Rostedtbf41a152008-10-04 02:00:59 -04002123 if (commit == -EBUSY)
Steven Rostedtfa743952009-06-16 12:37:57 -04002124 goto out_fail;
Steven Rostedtbf41a152008-10-04 02:00:59 -04002125
2126 if (commit == -EAGAIN)
2127 goto again;
2128
2129 RB_WARN_ON(cpu_buffer, commit < 0);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002130 }
Steven Rostedt168b6b12009-05-11 22:11:05 -04002131 }
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002132
Steven Rostedt168b6b12009-05-11 22:11:05 -04002133 get_event:
Steven Rostedt1cd8d732009-05-11 14:08:09 -04002134 event = __rb_reserve_next(cpu_buffer, 0, length, &ts);
Steven Rostedt168b6b12009-05-11 22:11:05 -04002135 if (unlikely(PTR_ERR(event) == -EAGAIN))
Steven Rostedtbf41a152008-10-04 02:00:59 -04002136 goto again;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002137
Steven Rostedtfa743952009-06-16 12:37:57 -04002138 if (!event)
2139 goto out_fail;
Steven Rostedtbf41a152008-10-04 02:00:59 -04002140
Steven Rostedtfa743952009-06-16 12:37:57 -04002141 if (!rb_event_is_commit(cpu_buffer, event))
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002142 delta = 0;
2143
2144 event->time_delta = delta;
2145
2146 return event;
Steven Rostedtfa743952009-06-16 12:37:57 -04002147
2148 out_fail:
2149 rb_end_commit(cpu_buffer);
2150 return NULL;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002151}
2152
Paul Mundt1155de42009-06-25 14:30:12 +09002153#ifdef CONFIG_TRACING
2154
Steven Rostedtaa18efb2009-04-20 16:16:11 -04002155#define TRACE_RECURSIVE_DEPTH 16
Steven Rostedt261842b2009-04-16 21:41:52 -04002156
2157static int trace_recursive_lock(void)
2158{
Steven Rostedtaa18efb2009-04-20 16:16:11 -04002159 current->trace_recursion++;
Steven Rostedt261842b2009-04-16 21:41:52 -04002160
Steven Rostedtaa18efb2009-04-20 16:16:11 -04002161 if (likely(current->trace_recursion < TRACE_RECURSIVE_DEPTH))
2162 return 0;
Steven Rostedt261842b2009-04-16 21:41:52 -04002163
Steven Rostedtaa18efb2009-04-20 16:16:11 -04002164 /* Disable all tracing before we do anything else */
2165 tracing_off_permanent();
Frederic Weisbeckere057a5e2009-04-19 23:38:12 +02002166
Steven Rostedt7d7d2b82009-04-27 12:37:49 -04002167 printk_once(KERN_WARNING "Tracing recursion: depth[%ld]:"
Steven Rostedtaa18efb2009-04-20 16:16:11 -04002168 "HC[%lu]:SC[%lu]:NMI[%lu]\n",
2169 current->trace_recursion,
2170 hardirq_count() >> HARDIRQ_SHIFT,
2171 softirq_count() >> SOFTIRQ_SHIFT,
2172 in_nmi());
Frederic Weisbeckere057a5e2009-04-19 23:38:12 +02002173
Steven Rostedtaa18efb2009-04-20 16:16:11 -04002174 WARN_ON_ONCE(1);
2175 return -1;
Steven Rostedt261842b2009-04-16 21:41:52 -04002176}
2177
2178static void trace_recursive_unlock(void)
2179{
Steven Rostedtaa18efb2009-04-20 16:16:11 -04002180 WARN_ON_ONCE(!current->trace_recursion);
Steven Rostedt261842b2009-04-16 21:41:52 -04002181
Steven Rostedtaa18efb2009-04-20 16:16:11 -04002182 current->trace_recursion--;
Steven Rostedt261842b2009-04-16 21:41:52 -04002183}
2184
Paul Mundt1155de42009-06-25 14:30:12 +09002185#else
2186
2187#define trace_recursive_lock() (0)
2188#define trace_recursive_unlock() do { } while (0)
2189
2190#endif
2191
Steven Rostedtbf41a152008-10-04 02:00:59 -04002192static DEFINE_PER_CPU(int, rb_need_resched);
2193
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002194/**
2195 * ring_buffer_lock_reserve - reserve a part of the buffer
2196 * @buffer: the ring buffer to reserve from
2197 * @length: the length of the data to reserve (excluding event header)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002198 *
2199 * Returns a reseverd event on the ring buffer to copy directly to.
2200 * The user of this interface will need to get the body to write into
2201 * and can use the ring_buffer_event_data() interface.
2202 *
2203 * The length is the length of the data needed, not the event length
2204 * which also includes the event header.
2205 *
2206 * Must be paired with ring_buffer_unlock_commit, unless NULL is returned.
2207 * If NULL is returned, then nothing has been allocated or locked.
2208 */
2209struct ring_buffer_event *
Arnaldo Carvalho de Melo0a987752009-02-05 16:12:56 -02002210ring_buffer_lock_reserve(struct ring_buffer *buffer, unsigned long length)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002211{
2212 struct ring_buffer_per_cpu *cpu_buffer;
2213 struct ring_buffer_event *event;
Steven Rostedtbf41a152008-10-04 02:00:59 -04002214 int cpu, resched;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002215
Steven Rostedt033601a2008-11-21 12:41:55 -05002216 if (ring_buffer_flags != RB_BUFFERS_ON)
Steven Rostedta3583242008-11-11 15:01:42 -05002217 return NULL;
2218
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002219 if (atomic_read(&buffer->record_disabled))
2220 return NULL;
2221
Steven Rostedtbf41a152008-10-04 02:00:59 -04002222 /* If we are tracing schedule, we don't want to recurse */
Steven Rostedt182e9f52008-11-03 23:15:56 -05002223 resched = ftrace_preempt_disable();
Steven Rostedtbf41a152008-10-04 02:00:59 -04002224
Steven Rostedt261842b2009-04-16 21:41:52 -04002225 if (trace_recursive_lock())
2226 goto out_nocheck;
2227
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002228 cpu = raw_smp_processor_id();
2229
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302230 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedtd7690412008-10-01 00:29:53 -04002231 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002232
2233 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002234
2235 if (atomic_read(&cpu_buffer->record_disabled))
Steven Rostedtd7690412008-10-01 00:29:53 -04002236 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002237
Steven Rostedtbe957c42009-05-11 14:42:53 -04002238 if (length > BUF_MAX_DATA_SIZE)
Steven Rostedtbf41a152008-10-04 02:00:59 -04002239 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002240
Steven Rostedt1cd8d732009-05-11 14:08:09 -04002241 event = rb_reserve_next_event(cpu_buffer, length);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002242 if (!event)
Steven Rostedtd7690412008-10-01 00:29:53 -04002243 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002244
Steven Rostedtbf41a152008-10-04 02:00:59 -04002245 /*
2246 * Need to store resched state on this cpu.
2247 * Only the first needs to.
2248 */
2249
2250 if (preempt_count() == 1)
2251 per_cpu(rb_need_resched, cpu) = resched;
2252
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002253 return event;
2254
Steven Rostedtd7690412008-10-01 00:29:53 -04002255 out:
Steven Rostedt261842b2009-04-16 21:41:52 -04002256 trace_recursive_unlock();
2257
2258 out_nocheck:
Steven Rostedt182e9f52008-11-03 23:15:56 -05002259 ftrace_preempt_enable(resched);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002260 return NULL;
2261}
Robert Richterc4f50182008-12-11 16:49:22 +01002262EXPORT_SYMBOL_GPL(ring_buffer_lock_reserve);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002263
Steven Rostedta1863c22009-09-03 10:23:58 -04002264static void
2265rb_update_write_stamp(struct ring_buffer_per_cpu *cpu_buffer,
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002266 struct ring_buffer_event *event)
2267{
Steven Rostedtfa743952009-06-16 12:37:57 -04002268 /*
2269 * The event first in the commit queue updates the
2270 * time stamp.
2271 */
2272 if (rb_event_is_commit(cpu_buffer, event))
2273 cpu_buffer->write_stamp += event->time_delta;
Steven Rostedta1863c22009-09-03 10:23:58 -04002274}
Steven Rostedtbf41a152008-10-04 02:00:59 -04002275
Steven Rostedta1863c22009-09-03 10:23:58 -04002276static void rb_commit(struct ring_buffer_per_cpu *cpu_buffer,
2277 struct ring_buffer_event *event)
2278{
2279 local_inc(&cpu_buffer->entries);
2280 rb_update_write_stamp(cpu_buffer, event);
Steven Rostedtfa743952009-06-16 12:37:57 -04002281 rb_end_commit(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002282}
2283
2284/**
2285 * ring_buffer_unlock_commit - commit a reserved
2286 * @buffer: The buffer to commit to
2287 * @event: The event pointer to commit.
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002288 *
2289 * This commits the data to the ring buffer, and releases any locks held.
2290 *
2291 * Must be paired with ring_buffer_lock_reserve.
2292 */
2293int ring_buffer_unlock_commit(struct ring_buffer *buffer,
Arnaldo Carvalho de Melo0a987752009-02-05 16:12:56 -02002294 struct ring_buffer_event *event)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002295{
2296 struct ring_buffer_per_cpu *cpu_buffer;
2297 int cpu = raw_smp_processor_id();
2298
2299 cpu_buffer = buffer->buffers[cpu];
2300
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002301 rb_commit(cpu_buffer, event);
2302
Steven Rostedt261842b2009-04-16 21:41:52 -04002303 trace_recursive_unlock();
2304
Steven Rostedtbf41a152008-10-04 02:00:59 -04002305 /*
2306 * Only the last preempt count needs to restore preemption.
2307 */
Steven Rostedt182e9f52008-11-03 23:15:56 -05002308 if (preempt_count() == 1)
2309 ftrace_preempt_enable(per_cpu(rb_need_resched, cpu));
2310 else
Steven Rostedtbf41a152008-10-04 02:00:59 -04002311 preempt_enable_no_resched_notrace();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002312
2313 return 0;
2314}
Robert Richterc4f50182008-12-11 16:49:22 +01002315EXPORT_SYMBOL_GPL(ring_buffer_unlock_commit);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002316
Frederic Weisbeckerf3b9aae2009-04-19 23:39:33 +02002317static inline void rb_event_discard(struct ring_buffer_event *event)
2318{
Lai Jiangshan334d4162009-04-24 11:27:05 +08002319 /* array[0] holds the actual length for the discarded event */
2320 event->array[0] = rb_event_data_length(event) - RB_EVNT_HDR_SIZE;
2321 event->type_len = RINGBUF_TYPE_PADDING;
Frederic Weisbeckerf3b9aae2009-04-19 23:39:33 +02002322 /* time delta must be non zero */
2323 if (!event->time_delta)
2324 event->time_delta = 1;
2325}
2326
Steven Rostedta1863c22009-09-03 10:23:58 -04002327/*
2328 * Decrement the entries to the page that an event is on.
2329 * The event does not even need to exist, only the pointer
2330 * to the page it is on. This may only be called before the commit
2331 * takes place.
2332 */
2333static inline void
2334rb_decrement_entry(struct ring_buffer_per_cpu *cpu_buffer,
2335 struct ring_buffer_event *event)
2336{
2337 unsigned long addr = (unsigned long)event;
2338 struct buffer_page *bpage = cpu_buffer->commit_page;
2339 struct buffer_page *start;
2340
2341 addr &= PAGE_MASK;
2342
2343 /* Do the likely case first */
2344 if (likely(bpage->page == (void *)addr)) {
2345 local_dec(&bpage->entries);
2346 return;
2347 }
2348
2349 /*
2350 * Because the commit page may be on the reader page we
2351 * start with the next page and check the end loop there.
2352 */
2353 rb_inc_page(cpu_buffer, &bpage);
2354 start = bpage;
2355 do {
2356 if (bpage->page == (void *)addr) {
2357 local_dec(&bpage->entries);
2358 return;
2359 }
2360 rb_inc_page(cpu_buffer, &bpage);
2361 } while (bpage != start);
2362
2363 /* commit not part of this buffer?? */
2364 RB_WARN_ON(cpu_buffer, 1);
2365}
2366
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002367/**
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002368 * ring_buffer_commit_discard - discard an event that has not been committed
2369 * @buffer: the ring buffer
2370 * @event: non committed event to discard
2371 *
Steven Rostedtdc892f72009-09-03 15:33:41 -04002372 * Sometimes an event that is in the ring buffer needs to be ignored.
2373 * This function lets the user discard an event in the ring buffer
2374 * and then that event will not be read later.
2375 *
2376 * This function only works if it is called before the the item has been
2377 * committed. It will try to free the event from the ring buffer
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002378 * if another event has not been added behind it.
2379 *
2380 * If another event has been added behind it, it will set the event
2381 * up as discarded, and perform the commit.
2382 *
2383 * If this function is called, do not call ring_buffer_unlock_commit on
2384 * the event.
2385 */
2386void ring_buffer_discard_commit(struct ring_buffer *buffer,
2387 struct ring_buffer_event *event)
2388{
2389 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002390 int cpu;
2391
2392 /* The event is discarded regardless */
Frederic Weisbeckerf3b9aae2009-04-19 23:39:33 +02002393 rb_event_discard(event);
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002394
Steven Rostedtfa743952009-06-16 12:37:57 -04002395 cpu = smp_processor_id();
2396 cpu_buffer = buffer->buffers[cpu];
2397
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002398 /*
2399 * This must only be called if the event has not been
2400 * committed yet. Thus we can assume that preemption
2401 * is still disabled.
2402 */
Steven Rostedtfa743952009-06-16 12:37:57 -04002403 RB_WARN_ON(buffer, !local_read(&cpu_buffer->committing));
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002404
Steven Rostedta1863c22009-09-03 10:23:58 -04002405 rb_decrement_entry(cpu_buffer, event);
Steven Rostedt0f2541d2009-08-05 12:02:48 -04002406 if (rb_try_to_discard(cpu_buffer, event))
Steven Rostedtedd813bf2009-06-02 23:00:53 -04002407 goto out;
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002408
2409 /*
2410 * The commit is still visible by the reader, so we
Steven Rostedta1863c22009-09-03 10:23:58 -04002411 * must still update the timestamp.
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002412 */
Steven Rostedta1863c22009-09-03 10:23:58 -04002413 rb_update_write_stamp(cpu_buffer, event);
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002414 out:
Steven Rostedtfa743952009-06-16 12:37:57 -04002415 rb_end_commit(cpu_buffer);
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002416
Frederic Weisbeckerf3b9aae2009-04-19 23:39:33 +02002417 trace_recursive_unlock();
2418
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002419 /*
2420 * Only the last preempt count needs to restore preemption.
2421 */
2422 if (preempt_count() == 1)
2423 ftrace_preempt_enable(per_cpu(rb_need_resched, cpu));
2424 else
2425 preempt_enable_no_resched_notrace();
2426
2427}
2428EXPORT_SYMBOL_GPL(ring_buffer_discard_commit);
2429
2430/**
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002431 * ring_buffer_write - write data to the buffer without reserving
2432 * @buffer: The ring buffer to write to.
2433 * @length: The length of the data being written (excluding the event header)
2434 * @data: The data to write to the buffer.
2435 *
2436 * This is like ring_buffer_lock_reserve and ring_buffer_unlock_commit as
2437 * one function. If you already have the data to write to the buffer, it
2438 * may be easier to simply call this function.
2439 *
2440 * Note, like ring_buffer_lock_reserve, the length is the length of the data
2441 * and not the length of the event which would hold the header.
2442 */
2443int ring_buffer_write(struct ring_buffer *buffer,
2444 unsigned long length,
2445 void *data)
2446{
2447 struct ring_buffer_per_cpu *cpu_buffer;
2448 struct ring_buffer_event *event;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002449 void *body;
2450 int ret = -EBUSY;
Steven Rostedtbf41a152008-10-04 02:00:59 -04002451 int cpu, resched;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002452
Steven Rostedt033601a2008-11-21 12:41:55 -05002453 if (ring_buffer_flags != RB_BUFFERS_ON)
Steven Rostedta3583242008-11-11 15:01:42 -05002454 return -EBUSY;
2455
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002456 if (atomic_read(&buffer->record_disabled))
2457 return -EBUSY;
2458
Steven Rostedt182e9f52008-11-03 23:15:56 -05002459 resched = ftrace_preempt_disable();
Steven Rostedtbf41a152008-10-04 02:00:59 -04002460
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002461 cpu = raw_smp_processor_id();
2462
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302463 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedtd7690412008-10-01 00:29:53 -04002464 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002465
2466 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002467
2468 if (atomic_read(&cpu_buffer->record_disabled))
2469 goto out;
2470
Steven Rostedtbe957c42009-05-11 14:42:53 -04002471 if (length > BUF_MAX_DATA_SIZE)
2472 goto out;
2473
2474 event = rb_reserve_next_event(cpu_buffer, length);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002475 if (!event)
2476 goto out;
2477
2478 body = rb_event_data(event);
2479
2480 memcpy(body, data, length);
2481
2482 rb_commit(cpu_buffer, event);
2483
2484 ret = 0;
2485 out:
Steven Rostedt182e9f52008-11-03 23:15:56 -05002486 ftrace_preempt_enable(resched);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002487
2488 return ret;
2489}
Robert Richterc4f50182008-12-11 16:49:22 +01002490EXPORT_SYMBOL_GPL(ring_buffer_write);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002491
Andrew Morton34a148b2009-01-09 12:27:09 -08002492static int rb_per_cpu_empty(struct ring_buffer_per_cpu *cpu_buffer)
Steven Rostedtbf41a152008-10-04 02:00:59 -04002493{
2494 struct buffer_page *reader = cpu_buffer->reader_page;
Steven Rostedt77ae3652009-03-27 11:00:29 -04002495 struct buffer_page *head = rb_set_head_page(cpu_buffer);
Steven Rostedtbf41a152008-10-04 02:00:59 -04002496 struct buffer_page *commit = cpu_buffer->commit_page;
2497
Steven Rostedt77ae3652009-03-27 11:00:29 -04002498 /* In case of error, head will be NULL */
2499 if (unlikely(!head))
2500 return 1;
2501
Steven Rostedtbf41a152008-10-04 02:00:59 -04002502 return reader->read == rb_page_commit(reader) &&
2503 (commit == reader ||
2504 (commit == head &&
2505 head->read == rb_page_commit(commit)));
2506}
2507
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002508/**
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002509 * ring_buffer_record_disable - stop all writes into the buffer
2510 * @buffer: The ring buffer to stop writes to.
2511 *
2512 * This prevents all writes to the buffer. Any attempt to write
2513 * to the buffer after this will fail and return NULL.
2514 *
2515 * The caller should call synchronize_sched() after this.
2516 */
2517void ring_buffer_record_disable(struct ring_buffer *buffer)
2518{
2519 atomic_inc(&buffer->record_disabled);
2520}
Robert Richterc4f50182008-12-11 16:49:22 +01002521EXPORT_SYMBOL_GPL(ring_buffer_record_disable);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002522
2523/**
2524 * ring_buffer_record_enable - enable writes to the buffer
2525 * @buffer: The ring buffer to enable writes
2526 *
2527 * Note, multiple disables will need the same number of enables
2528 * to truely enable the writing (much like preempt_disable).
2529 */
2530void ring_buffer_record_enable(struct ring_buffer *buffer)
2531{
2532 atomic_dec(&buffer->record_disabled);
2533}
Robert Richterc4f50182008-12-11 16:49:22 +01002534EXPORT_SYMBOL_GPL(ring_buffer_record_enable);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002535
2536/**
2537 * ring_buffer_record_disable_cpu - stop all writes into the cpu_buffer
2538 * @buffer: The ring buffer to stop writes to.
2539 * @cpu: The CPU buffer to stop
2540 *
2541 * This prevents all writes to the buffer. Any attempt to write
2542 * to the buffer after this will fail and return NULL.
2543 *
2544 * The caller should call synchronize_sched() after this.
2545 */
2546void ring_buffer_record_disable_cpu(struct ring_buffer *buffer, int cpu)
2547{
2548 struct ring_buffer_per_cpu *cpu_buffer;
2549
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302550 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04002551 return;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002552
2553 cpu_buffer = buffer->buffers[cpu];
2554 atomic_inc(&cpu_buffer->record_disabled);
2555}
Robert Richterc4f50182008-12-11 16:49:22 +01002556EXPORT_SYMBOL_GPL(ring_buffer_record_disable_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002557
2558/**
2559 * ring_buffer_record_enable_cpu - enable writes to the buffer
2560 * @buffer: The ring buffer to enable writes
2561 * @cpu: The CPU to enable.
2562 *
2563 * Note, multiple disables will need the same number of enables
2564 * to truely enable the writing (much like preempt_disable).
2565 */
2566void ring_buffer_record_enable_cpu(struct ring_buffer *buffer, int cpu)
2567{
2568 struct ring_buffer_per_cpu *cpu_buffer;
2569
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302570 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04002571 return;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002572
2573 cpu_buffer = buffer->buffers[cpu];
2574 atomic_dec(&cpu_buffer->record_disabled);
2575}
Robert Richterc4f50182008-12-11 16:49:22 +01002576EXPORT_SYMBOL_GPL(ring_buffer_record_enable_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002577
2578/**
2579 * ring_buffer_entries_cpu - get the number of entries in a cpu buffer
2580 * @buffer: The ring buffer
2581 * @cpu: The per CPU buffer to get the entries from.
2582 */
2583unsigned long ring_buffer_entries_cpu(struct ring_buffer *buffer, int cpu)
2584{
2585 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedt8aabee52009-03-12 13:13:49 -04002586 unsigned long ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002587
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302588 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04002589 return 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002590
2591 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt77ae3652009-03-27 11:00:29 -04002592 ret = (local_read(&cpu_buffer->entries) - local_read(&cpu_buffer->overrun))
Steven Rostedte4906ef2009-04-30 20:49:44 -04002593 - cpu_buffer->read;
Steven Rostedt554f7862009-03-11 22:00:13 -04002594
2595 return ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002596}
Robert Richterc4f50182008-12-11 16:49:22 +01002597EXPORT_SYMBOL_GPL(ring_buffer_entries_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002598
2599/**
2600 * ring_buffer_overrun_cpu - get the number of overruns in a cpu_buffer
2601 * @buffer: The ring buffer
2602 * @cpu: The per CPU buffer to get the number of overruns from
2603 */
2604unsigned long ring_buffer_overrun_cpu(struct ring_buffer *buffer, int cpu)
2605{
2606 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedt8aabee52009-03-12 13:13:49 -04002607 unsigned long ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002608
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302609 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04002610 return 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002611
2612 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt77ae3652009-03-27 11:00:29 -04002613 ret = local_read(&cpu_buffer->overrun);
Steven Rostedt554f7862009-03-11 22:00:13 -04002614
2615 return ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002616}
Robert Richterc4f50182008-12-11 16:49:22 +01002617EXPORT_SYMBOL_GPL(ring_buffer_overrun_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002618
2619/**
Steven Rostedtf0d2c682009-04-29 13:43:37 -04002620 * ring_buffer_commit_overrun_cpu - get the number of overruns caused by commits
2621 * @buffer: The ring buffer
2622 * @cpu: The per CPU buffer to get the number of overruns from
2623 */
2624unsigned long
2625ring_buffer_commit_overrun_cpu(struct ring_buffer *buffer, int cpu)
2626{
2627 struct ring_buffer_per_cpu *cpu_buffer;
2628 unsigned long ret;
2629
2630 if (!cpumask_test_cpu(cpu, buffer->cpumask))
2631 return 0;
2632
2633 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt77ae3652009-03-27 11:00:29 -04002634 ret = local_read(&cpu_buffer->commit_overrun);
Steven Rostedtf0d2c682009-04-29 13:43:37 -04002635
2636 return ret;
2637}
2638EXPORT_SYMBOL_GPL(ring_buffer_commit_overrun_cpu);
2639
2640/**
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002641 * ring_buffer_entries - get the number of entries in a buffer
2642 * @buffer: The ring buffer
2643 *
2644 * Returns the total number of entries in the ring buffer
2645 * (all CPU entries)
2646 */
2647unsigned long ring_buffer_entries(struct ring_buffer *buffer)
2648{
2649 struct ring_buffer_per_cpu *cpu_buffer;
2650 unsigned long entries = 0;
2651 int cpu;
2652
2653 /* if you care about this being correct, lock the buffer */
2654 for_each_buffer_cpu(buffer, cpu) {
2655 cpu_buffer = buffer->buffers[cpu];
Steven Rostedte4906ef2009-04-30 20:49:44 -04002656 entries += (local_read(&cpu_buffer->entries) -
Steven Rostedt77ae3652009-03-27 11:00:29 -04002657 local_read(&cpu_buffer->overrun)) - cpu_buffer->read;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002658 }
2659
2660 return entries;
2661}
Robert Richterc4f50182008-12-11 16:49:22 +01002662EXPORT_SYMBOL_GPL(ring_buffer_entries);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002663
2664/**
2665 * ring_buffer_overrun_cpu - get the number of overruns in buffer
2666 * @buffer: The ring buffer
2667 *
2668 * Returns the total number of overruns in the ring buffer
2669 * (all CPU entries)
2670 */
2671unsigned long ring_buffer_overruns(struct ring_buffer *buffer)
2672{
2673 struct ring_buffer_per_cpu *cpu_buffer;
2674 unsigned long overruns = 0;
2675 int cpu;
2676
2677 /* if you care about this being correct, lock the buffer */
2678 for_each_buffer_cpu(buffer, cpu) {
2679 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt77ae3652009-03-27 11:00:29 -04002680 overruns += local_read(&cpu_buffer->overrun);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002681 }
2682
2683 return overruns;
2684}
Robert Richterc4f50182008-12-11 16:49:22 +01002685EXPORT_SYMBOL_GPL(ring_buffer_overruns);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002686
Steven Rostedt642edba2008-11-12 00:01:26 -05002687static void rb_iter_reset(struct ring_buffer_iter *iter)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002688{
2689 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
2690
Steven Rostedtd7690412008-10-01 00:29:53 -04002691 /* Iterator usage is expected to have record disabled */
2692 if (list_empty(&cpu_buffer->reader_page->list)) {
Steven Rostedt77ae3652009-03-27 11:00:29 -04002693 iter->head_page = rb_set_head_page(cpu_buffer);
2694 if (unlikely(!iter->head_page))
2695 return;
2696 iter->head = iter->head_page->read;
Steven Rostedtd7690412008-10-01 00:29:53 -04002697 } else {
2698 iter->head_page = cpu_buffer->reader_page;
Steven Rostedt6f807ac2008-10-04 02:00:58 -04002699 iter->head = cpu_buffer->reader_page->read;
Steven Rostedtd7690412008-10-01 00:29:53 -04002700 }
2701 if (iter->head)
2702 iter->read_stamp = cpu_buffer->read_stamp;
2703 else
Steven Rostedtabc9b562008-12-02 15:34:06 -05002704 iter->read_stamp = iter->head_page->page->time_stamp;
Steven Rostedt642edba2008-11-12 00:01:26 -05002705}
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002706
Steven Rostedt642edba2008-11-12 00:01:26 -05002707/**
2708 * ring_buffer_iter_reset - reset an iterator
2709 * @iter: The iterator to reset
2710 *
2711 * Resets the iterator, so that it will start from the beginning
2712 * again.
2713 */
2714void ring_buffer_iter_reset(struct ring_buffer_iter *iter)
2715{
Steven Rostedt554f7862009-03-11 22:00:13 -04002716 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedt642edba2008-11-12 00:01:26 -05002717 unsigned long flags;
2718
Steven Rostedt554f7862009-03-11 22:00:13 -04002719 if (!iter)
2720 return;
2721
2722 cpu_buffer = iter->cpu_buffer;
2723
Steven Rostedt642edba2008-11-12 00:01:26 -05002724 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
2725 rb_iter_reset(iter);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002726 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002727}
Robert Richterc4f50182008-12-11 16:49:22 +01002728EXPORT_SYMBOL_GPL(ring_buffer_iter_reset);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002729
2730/**
2731 * ring_buffer_iter_empty - check if an iterator has no more to read
2732 * @iter: The iterator to check
2733 */
2734int ring_buffer_iter_empty(struct ring_buffer_iter *iter)
2735{
2736 struct ring_buffer_per_cpu *cpu_buffer;
2737
2738 cpu_buffer = iter->cpu_buffer;
2739
Steven Rostedtbf41a152008-10-04 02:00:59 -04002740 return iter->head_page == cpu_buffer->commit_page &&
2741 iter->head == rb_commit_index(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002742}
Robert Richterc4f50182008-12-11 16:49:22 +01002743EXPORT_SYMBOL_GPL(ring_buffer_iter_empty);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002744
2745static void
2746rb_update_read_stamp(struct ring_buffer_per_cpu *cpu_buffer,
2747 struct ring_buffer_event *event)
2748{
2749 u64 delta;
2750
Lai Jiangshan334d4162009-04-24 11:27:05 +08002751 switch (event->type_len) {
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002752 case RINGBUF_TYPE_PADDING:
2753 return;
2754
2755 case RINGBUF_TYPE_TIME_EXTEND:
2756 delta = event->array[0];
2757 delta <<= TS_SHIFT;
2758 delta += event->time_delta;
2759 cpu_buffer->read_stamp += delta;
2760 return;
2761
2762 case RINGBUF_TYPE_TIME_STAMP:
2763 /* FIXME: not implemented */
2764 return;
2765
2766 case RINGBUF_TYPE_DATA:
2767 cpu_buffer->read_stamp += event->time_delta;
2768 return;
2769
2770 default:
2771 BUG();
2772 }
2773 return;
2774}
2775
2776static void
2777rb_update_iter_read_stamp(struct ring_buffer_iter *iter,
2778 struct ring_buffer_event *event)
2779{
2780 u64 delta;
2781
Lai Jiangshan334d4162009-04-24 11:27:05 +08002782 switch (event->type_len) {
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002783 case RINGBUF_TYPE_PADDING:
2784 return;
2785
2786 case RINGBUF_TYPE_TIME_EXTEND:
2787 delta = event->array[0];
2788 delta <<= TS_SHIFT;
2789 delta += event->time_delta;
2790 iter->read_stamp += delta;
2791 return;
2792
2793 case RINGBUF_TYPE_TIME_STAMP:
2794 /* FIXME: not implemented */
2795 return;
2796
2797 case RINGBUF_TYPE_DATA:
2798 iter->read_stamp += event->time_delta;
2799 return;
2800
2801 default:
2802 BUG();
2803 }
2804 return;
2805}
2806
Steven Rostedtd7690412008-10-01 00:29:53 -04002807static struct buffer_page *
2808rb_get_reader_page(struct ring_buffer_per_cpu *cpu_buffer)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002809{
Steven Rostedtd7690412008-10-01 00:29:53 -04002810 struct buffer_page *reader = NULL;
2811 unsigned long flags;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002812 int nr_loops = 0;
Steven Rostedt77ae3652009-03-27 11:00:29 -04002813 int ret;
Steven Rostedtd7690412008-10-01 00:29:53 -04002814
Steven Rostedt3e03fb72008-11-06 00:09:43 -05002815 local_irq_save(flags);
2816 __raw_spin_lock(&cpu_buffer->lock);
Steven Rostedtd7690412008-10-01 00:29:53 -04002817
2818 again:
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002819 /*
2820 * This should normally only loop twice. But because the
2821 * start of the reader inserts an empty page, it causes
2822 * a case where we will loop three times. There should be no
2823 * reason to loop four times (that I know of).
2824 */
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05002825 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 3)) {
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002826 reader = NULL;
2827 goto out;
2828 }
2829
Steven Rostedtd7690412008-10-01 00:29:53 -04002830 reader = cpu_buffer->reader_page;
2831
2832 /* If there's more to read, return this page */
Steven Rostedtbf41a152008-10-04 02:00:59 -04002833 if (cpu_buffer->reader_page->read < rb_page_size(reader))
Steven Rostedtd7690412008-10-01 00:29:53 -04002834 goto out;
2835
2836 /* Never should we have an index greater than the size */
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05002837 if (RB_WARN_ON(cpu_buffer,
2838 cpu_buffer->reader_page->read > rb_page_size(reader)))
2839 goto out;
Steven Rostedtd7690412008-10-01 00:29:53 -04002840
2841 /* check if we caught up to the tail */
2842 reader = NULL;
Steven Rostedtbf41a152008-10-04 02:00:59 -04002843 if (cpu_buffer->commit_page == cpu_buffer->reader_page)
Steven Rostedtd7690412008-10-01 00:29:53 -04002844 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002845
2846 /*
Steven Rostedtd7690412008-10-01 00:29:53 -04002847 * Reset the reader page to size zero.
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002848 */
Steven Rostedt77ae3652009-03-27 11:00:29 -04002849 local_set(&cpu_buffer->reader_page->write, 0);
2850 local_set(&cpu_buffer->reader_page->entries, 0);
2851 local_set(&cpu_buffer->reader_page->page->commit, 0);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002852
Steven Rostedt77ae3652009-03-27 11:00:29 -04002853 spin:
2854 /*
2855 * Splice the empty reader page into the list around the head.
2856 */
2857 reader = rb_set_head_page(cpu_buffer);
Steven Rostedtd7690412008-10-01 00:29:53 -04002858 cpu_buffer->reader_page->list.next = reader->list.next;
2859 cpu_buffer->reader_page->list.prev = reader->list.prev;
Steven Rostedtbf41a152008-10-04 02:00:59 -04002860
Steven Rostedt3adc54f2009-03-30 15:32:01 -04002861 /*
2862 * cpu_buffer->pages just needs to point to the buffer, it
2863 * has no specific buffer page to point to. Lets move it out
2864 * of our way so we don't accidently swap it.
2865 */
2866 cpu_buffer->pages = reader->list.prev;
2867
Steven Rostedt77ae3652009-03-27 11:00:29 -04002868 /* The reader page will be pointing to the new head */
2869 rb_set_list_to_head(cpu_buffer, &cpu_buffer->reader_page->list);
Steven Rostedtd7690412008-10-01 00:29:53 -04002870
2871 /*
Steven Rostedt77ae3652009-03-27 11:00:29 -04002872 * Here's the tricky part.
2873 *
2874 * We need to move the pointer past the header page.
2875 * But we can only do that if a writer is not currently
2876 * moving it. The page before the header page has the
2877 * flag bit '1' set if it is pointing to the page we want.
2878 * but if the writer is in the process of moving it
2879 * than it will be '2' or already moved '0'.
Steven Rostedtd7690412008-10-01 00:29:53 -04002880 */
Steven Rostedtd7690412008-10-01 00:29:53 -04002881
Steven Rostedt77ae3652009-03-27 11:00:29 -04002882 ret = rb_head_page_replace(reader, cpu_buffer->reader_page);
2883
2884 /*
2885 * If we did not convert it, then we must try again.
2886 */
2887 if (!ret)
2888 goto spin;
2889
2890 /*
2891 * Yeah! We succeeded in replacing the page.
2892 *
2893 * Now make the new head point back to the reader page.
2894 */
2895 reader->list.next->prev = &cpu_buffer->reader_page->list;
2896 rb_inc_page(cpu_buffer, &cpu_buffer->head_page);
Steven Rostedtd7690412008-10-01 00:29:53 -04002897
2898 /* Finally update the reader page to the new head */
2899 cpu_buffer->reader_page = reader;
2900 rb_reset_reader_page(cpu_buffer);
2901
2902 goto again;
2903
2904 out:
Steven Rostedt3e03fb72008-11-06 00:09:43 -05002905 __raw_spin_unlock(&cpu_buffer->lock);
2906 local_irq_restore(flags);
Steven Rostedtd7690412008-10-01 00:29:53 -04002907
2908 return reader;
2909}
2910
2911static void rb_advance_reader(struct ring_buffer_per_cpu *cpu_buffer)
2912{
2913 struct ring_buffer_event *event;
2914 struct buffer_page *reader;
2915 unsigned length;
2916
2917 reader = rb_get_reader_page(cpu_buffer);
2918
2919 /* This function should not be called when buffer is empty */
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05002920 if (RB_WARN_ON(cpu_buffer, !reader))
2921 return;
Steven Rostedtd7690412008-10-01 00:29:53 -04002922
2923 event = rb_reader_event(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002924
Steven Rostedta1863c22009-09-03 10:23:58 -04002925 if (event->type_len <= RINGBUF_TYPE_DATA_TYPE_LEN_MAX)
Steven Rostedte4906ef2009-04-30 20:49:44 -04002926 cpu_buffer->read++;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002927
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002928 rb_update_read_stamp(cpu_buffer, event);
2929
Steven Rostedtd7690412008-10-01 00:29:53 -04002930 length = rb_event_length(event);
Steven Rostedt6f807ac2008-10-04 02:00:58 -04002931 cpu_buffer->reader_page->read += length;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002932}
2933
2934static void rb_advance_iter(struct ring_buffer_iter *iter)
2935{
2936 struct ring_buffer *buffer;
2937 struct ring_buffer_per_cpu *cpu_buffer;
2938 struct ring_buffer_event *event;
2939 unsigned length;
2940
2941 cpu_buffer = iter->cpu_buffer;
2942 buffer = cpu_buffer->buffer;
2943
2944 /*
2945 * Check if we are at the end of the buffer.
2946 */
Steven Rostedtbf41a152008-10-04 02:00:59 -04002947 if (iter->head >= rb_page_size(iter->head_page)) {
Steven Rostedtea05b572009-06-03 09:30:10 -04002948 /* discarded commits can make the page empty */
2949 if (iter->head_page == cpu_buffer->commit_page)
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05002950 return;
Steven Rostedtd7690412008-10-01 00:29:53 -04002951 rb_inc_iter(iter);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002952 return;
2953 }
2954
2955 event = rb_iter_head_event(iter);
2956
2957 length = rb_event_length(event);
2958
2959 /*
2960 * This should not be called to advance the header if we are
2961 * at the tail of the buffer.
2962 */
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05002963 if (RB_WARN_ON(cpu_buffer,
Steven Rostedtf536aaf2008-11-10 23:07:30 -05002964 (iter->head_page == cpu_buffer->commit_page) &&
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05002965 (iter->head + length > rb_commit_index(cpu_buffer))))
2966 return;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002967
2968 rb_update_iter_read_stamp(iter, event);
2969
2970 iter->head += length;
2971
2972 /* check for end of page padding */
Steven Rostedtbf41a152008-10-04 02:00:59 -04002973 if ((iter->head >= rb_page_size(iter->head_page)) &&
2974 (iter->head_page != cpu_buffer->commit_page))
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002975 rb_advance_iter(iter);
2976}
2977
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002978static struct ring_buffer_event *
2979rb_buffer_peek(struct ring_buffer *buffer, int cpu, u64 *ts)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002980{
2981 struct ring_buffer_per_cpu *cpu_buffer;
2982 struct ring_buffer_event *event;
Steven Rostedtd7690412008-10-01 00:29:53 -04002983 struct buffer_page *reader;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002984 int nr_loops = 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002985
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002986 cpu_buffer = buffer->buffers[cpu];
2987
2988 again:
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002989 /*
2990 * We repeat when a timestamp is encountered. It is possible
2991 * to get multiple timestamps from an interrupt entering just
Steven Rostedtea05b572009-06-03 09:30:10 -04002992 * as one timestamp is about to be written, or from discarded
2993 * commits. The most that we can have is the number on a single page.
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002994 */
Steven Rostedtea05b572009-06-03 09:30:10 -04002995 if (RB_WARN_ON(cpu_buffer, ++nr_loops > RB_TIMESTAMPS_PER_PAGE))
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002996 return NULL;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002997
Steven Rostedtd7690412008-10-01 00:29:53 -04002998 reader = rb_get_reader_page(cpu_buffer);
2999 if (!reader)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003000 return NULL;
3001
Steven Rostedtd7690412008-10-01 00:29:53 -04003002 event = rb_reader_event(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003003
Lai Jiangshan334d4162009-04-24 11:27:05 +08003004 switch (event->type_len) {
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003005 case RINGBUF_TYPE_PADDING:
Tom Zanussi2d622712009-03-22 03:30:49 -05003006 if (rb_null_event(event))
3007 RB_WARN_ON(cpu_buffer, 1);
3008 /*
3009 * Because the writer could be discarding every
3010 * event it creates (which would probably be bad)
3011 * if we were to go back to "again" then we may never
3012 * catch up, and will trigger the warn on, or lock
3013 * the box. Return the padding, and we will release
3014 * the current locks, and try again.
3015 */
Tom Zanussi2d622712009-03-22 03:30:49 -05003016 return event;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003017
3018 case RINGBUF_TYPE_TIME_EXTEND:
3019 /* Internal data, OK to advance */
Steven Rostedtd7690412008-10-01 00:29:53 -04003020 rb_advance_reader(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003021 goto again;
3022
3023 case RINGBUF_TYPE_TIME_STAMP:
3024 /* FIXME: not implemented */
Steven Rostedtd7690412008-10-01 00:29:53 -04003025 rb_advance_reader(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003026 goto again;
3027
3028 case RINGBUF_TYPE_DATA:
3029 if (ts) {
3030 *ts = cpu_buffer->read_stamp + event->time_delta;
Steven Rostedt37886f62009-03-17 17:22:06 -04003031 ring_buffer_normalize_time_stamp(buffer,
3032 cpu_buffer->cpu, ts);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003033 }
3034 return event;
3035
3036 default:
3037 BUG();
3038 }
3039
3040 return NULL;
3041}
Robert Richterc4f50182008-12-11 16:49:22 +01003042EXPORT_SYMBOL_GPL(ring_buffer_peek);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003043
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003044static struct ring_buffer_event *
3045rb_iter_peek(struct ring_buffer_iter *iter, u64 *ts)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003046{
3047 struct ring_buffer *buffer;
3048 struct ring_buffer_per_cpu *cpu_buffer;
3049 struct ring_buffer_event *event;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003050 int nr_loops = 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003051
3052 if (ring_buffer_iter_empty(iter))
3053 return NULL;
3054
3055 cpu_buffer = iter->cpu_buffer;
3056 buffer = cpu_buffer->buffer;
3057
3058 again:
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003059 /*
Steven Rostedtea05b572009-06-03 09:30:10 -04003060 * We repeat when a timestamp is encountered.
3061 * We can get multiple timestamps by nested interrupts or also
3062 * if filtering is on (discarding commits). Since discarding
3063 * commits can be frequent we can get a lot of timestamps.
3064 * But we limit them by not adding timestamps if they begin
3065 * at the start of a page.
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003066 */
Steven Rostedtea05b572009-06-03 09:30:10 -04003067 if (RB_WARN_ON(cpu_buffer, ++nr_loops > RB_TIMESTAMPS_PER_PAGE))
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003068 return NULL;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003069
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003070 if (rb_per_cpu_empty(cpu_buffer))
3071 return NULL;
3072
3073 event = rb_iter_head_event(iter);
3074
Lai Jiangshan334d4162009-04-24 11:27:05 +08003075 switch (event->type_len) {
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003076 case RINGBUF_TYPE_PADDING:
Tom Zanussi2d622712009-03-22 03:30:49 -05003077 if (rb_null_event(event)) {
3078 rb_inc_iter(iter);
3079 goto again;
3080 }
3081 rb_advance_iter(iter);
3082 return event;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003083
3084 case RINGBUF_TYPE_TIME_EXTEND:
3085 /* Internal data, OK to advance */
3086 rb_advance_iter(iter);
3087 goto again;
3088
3089 case RINGBUF_TYPE_TIME_STAMP:
3090 /* FIXME: not implemented */
3091 rb_advance_iter(iter);
3092 goto again;
3093
3094 case RINGBUF_TYPE_DATA:
3095 if (ts) {
3096 *ts = iter->read_stamp + event->time_delta;
Steven Rostedt37886f62009-03-17 17:22:06 -04003097 ring_buffer_normalize_time_stamp(buffer,
3098 cpu_buffer->cpu, ts);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003099 }
3100 return event;
3101
3102 default:
3103 BUG();
3104 }
3105
3106 return NULL;
3107}
Robert Richterc4f50182008-12-11 16:49:22 +01003108EXPORT_SYMBOL_GPL(ring_buffer_iter_peek);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003109
Steven Rostedt8d707e82009-06-16 21:22:48 -04003110static inline int rb_ok_to_lock(void)
3111{
3112 /*
3113 * If an NMI die dumps out the content of the ring buffer
3114 * do not grab locks. We also permanently disable the ring
3115 * buffer too. A one time deal is all you get from reading
3116 * the ring buffer from an NMI.
3117 */
Steven Rostedt464e85e2009-08-05 15:26:37 -04003118 if (likely(!in_nmi()))
Steven Rostedt8d707e82009-06-16 21:22:48 -04003119 return 1;
3120
3121 tracing_off_permanent();
3122 return 0;
3123}
3124
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003125/**
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003126 * ring_buffer_peek - peek at the next event to be read
3127 * @buffer: The ring buffer to read
3128 * @cpu: The cpu to peak at
3129 * @ts: The timestamp counter of this event.
3130 *
3131 * This will return the event that will be read next, but does
3132 * not consume the data.
3133 */
3134struct ring_buffer_event *
3135ring_buffer_peek(struct ring_buffer *buffer, int cpu, u64 *ts)
3136{
3137 struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
Steven Rostedt8aabee52009-03-12 13:13:49 -04003138 struct ring_buffer_event *event;
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003139 unsigned long flags;
Steven Rostedt8d707e82009-06-16 21:22:48 -04003140 int dolock;
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003141
Steven Rostedt554f7862009-03-11 22:00:13 -04003142 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04003143 return NULL;
Steven Rostedt554f7862009-03-11 22:00:13 -04003144
Steven Rostedt8d707e82009-06-16 21:22:48 -04003145 dolock = rb_ok_to_lock();
Tom Zanussi2d622712009-03-22 03:30:49 -05003146 again:
Steven Rostedt8d707e82009-06-16 21:22:48 -04003147 local_irq_save(flags);
3148 if (dolock)
3149 spin_lock(&cpu_buffer->reader_lock);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003150 event = rb_buffer_peek(buffer, cpu, ts);
Robert Richter469535a2009-07-30 19:19:18 +02003151 if (event && event->type_len == RINGBUF_TYPE_PADDING)
3152 rb_advance_reader(cpu_buffer);
Steven Rostedt8d707e82009-06-16 21:22:48 -04003153 if (dolock)
3154 spin_unlock(&cpu_buffer->reader_lock);
3155 local_irq_restore(flags);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003156
Steven Rostedt1b959e12009-09-03 10:12:13 -04003157 if (event && event->type_len == RINGBUF_TYPE_PADDING)
Tom Zanussi2d622712009-03-22 03:30:49 -05003158 goto again;
Tom Zanussi2d622712009-03-22 03:30:49 -05003159
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003160 return event;
3161}
3162
3163/**
3164 * ring_buffer_iter_peek - peek at the next event to be read
3165 * @iter: The ring buffer iterator
3166 * @ts: The timestamp counter of this event.
3167 *
3168 * This will return the event that will be read next, but does
3169 * not increment the iterator.
3170 */
3171struct ring_buffer_event *
3172ring_buffer_iter_peek(struct ring_buffer_iter *iter, u64 *ts)
3173{
3174 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
3175 struct ring_buffer_event *event;
3176 unsigned long flags;
3177
Tom Zanussi2d622712009-03-22 03:30:49 -05003178 again:
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003179 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
3180 event = rb_iter_peek(iter, ts);
3181 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
3182
Steven Rostedt1b959e12009-09-03 10:12:13 -04003183 if (event && event->type_len == RINGBUF_TYPE_PADDING)
Tom Zanussi2d622712009-03-22 03:30:49 -05003184 goto again;
Tom Zanussi2d622712009-03-22 03:30:49 -05003185
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003186 return event;
3187}
3188
3189/**
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003190 * ring_buffer_consume - return an event and consume it
3191 * @buffer: The ring buffer to get the next event from
3192 *
3193 * Returns the next event in the ring buffer, and that event is consumed.
3194 * Meaning, that sequential reads will keep returning a different event,
3195 * and eventually empty the ring buffer if the producer is slower.
3196 */
3197struct ring_buffer_event *
3198ring_buffer_consume(struct ring_buffer *buffer, int cpu, u64 *ts)
3199{
Steven Rostedt554f7862009-03-11 22:00:13 -04003200 struct ring_buffer_per_cpu *cpu_buffer;
3201 struct ring_buffer_event *event = NULL;
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003202 unsigned long flags;
Steven Rostedt8d707e82009-06-16 21:22:48 -04003203 int dolock;
3204
3205 dolock = rb_ok_to_lock();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003206
Tom Zanussi2d622712009-03-22 03:30:49 -05003207 again:
Steven Rostedt554f7862009-03-11 22:00:13 -04003208 /* might be called in atomic */
3209 preempt_disable();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003210
Steven Rostedt554f7862009-03-11 22:00:13 -04003211 if (!cpumask_test_cpu(cpu, buffer->cpumask))
3212 goto out;
3213
3214 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt8d707e82009-06-16 21:22:48 -04003215 local_irq_save(flags);
3216 if (dolock)
3217 spin_lock(&cpu_buffer->reader_lock);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003218
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003219 event = rb_buffer_peek(buffer, cpu, ts);
Robert Richter469535a2009-07-30 19:19:18 +02003220 if (event)
3221 rb_advance_reader(cpu_buffer);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003222
Steven Rostedt8d707e82009-06-16 21:22:48 -04003223 if (dolock)
3224 spin_unlock(&cpu_buffer->reader_lock);
3225 local_irq_restore(flags);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003226
Steven Rostedt554f7862009-03-11 22:00:13 -04003227 out:
3228 preempt_enable();
3229
Steven Rostedt1b959e12009-09-03 10:12:13 -04003230 if (event && event->type_len == RINGBUF_TYPE_PADDING)
Tom Zanussi2d622712009-03-22 03:30:49 -05003231 goto again;
Tom Zanussi2d622712009-03-22 03:30:49 -05003232
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003233 return event;
3234}
Robert Richterc4f50182008-12-11 16:49:22 +01003235EXPORT_SYMBOL_GPL(ring_buffer_consume);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003236
3237/**
3238 * ring_buffer_read_start - start a non consuming read of the buffer
3239 * @buffer: The ring buffer to read from
3240 * @cpu: The cpu buffer to iterate over
3241 *
3242 * This starts up an iteration through the buffer. It also disables
3243 * the recording to the buffer until the reading is finished.
3244 * This prevents the reading from being corrupted. This is not
3245 * a consuming read, so a producer is not expected.
3246 *
3247 * Must be paired with ring_buffer_finish.
3248 */
3249struct ring_buffer_iter *
3250ring_buffer_read_start(struct ring_buffer *buffer, int cpu)
3251{
3252 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedt8aabee52009-03-12 13:13:49 -04003253 struct ring_buffer_iter *iter;
Steven Rostedtd7690412008-10-01 00:29:53 -04003254 unsigned long flags;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003255
Rusty Russell9e01c1b2009-01-01 10:12:22 +10303256 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04003257 return NULL;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003258
3259 iter = kmalloc(sizeof(*iter), GFP_KERNEL);
3260 if (!iter)
Steven Rostedt8aabee52009-03-12 13:13:49 -04003261 return NULL;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003262
3263 cpu_buffer = buffer->buffers[cpu];
3264
3265 iter->cpu_buffer = cpu_buffer;
3266
3267 atomic_inc(&cpu_buffer->record_disabled);
3268 synchronize_sched();
3269
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003270 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
Steven Rostedt3e03fb72008-11-06 00:09:43 -05003271 __raw_spin_lock(&cpu_buffer->lock);
Steven Rostedt642edba2008-11-12 00:01:26 -05003272 rb_iter_reset(iter);
Steven Rostedt3e03fb72008-11-06 00:09:43 -05003273 __raw_spin_unlock(&cpu_buffer->lock);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003274 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003275
3276 return iter;
3277}
Robert Richterc4f50182008-12-11 16:49:22 +01003278EXPORT_SYMBOL_GPL(ring_buffer_read_start);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003279
3280/**
3281 * ring_buffer_finish - finish reading the iterator of the buffer
3282 * @iter: The iterator retrieved by ring_buffer_start
3283 *
3284 * This re-enables the recording to the buffer, and frees the
3285 * iterator.
3286 */
3287void
3288ring_buffer_read_finish(struct ring_buffer_iter *iter)
3289{
3290 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
3291
3292 atomic_dec(&cpu_buffer->record_disabled);
3293 kfree(iter);
3294}
Robert Richterc4f50182008-12-11 16:49:22 +01003295EXPORT_SYMBOL_GPL(ring_buffer_read_finish);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003296
3297/**
3298 * ring_buffer_read - read the next item in the ring buffer by the iterator
3299 * @iter: The ring buffer iterator
3300 * @ts: The time stamp of the event read.
3301 *
3302 * This reads the next event in the ring buffer and increments the iterator.
3303 */
3304struct ring_buffer_event *
3305ring_buffer_read(struct ring_buffer_iter *iter, u64 *ts)
3306{
3307 struct ring_buffer_event *event;
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003308 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
3309 unsigned long flags;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003310
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003311 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
Steven Rostedt7e9391c2009-09-03 10:02:09 -04003312 again:
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003313 event = rb_iter_peek(iter, ts);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003314 if (!event)
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003315 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003316
Steven Rostedt7e9391c2009-09-03 10:02:09 -04003317 if (event->type_len == RINGBUF_TYPE_PADDING)
3318 goto again;
3319
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003320 rb_advance_iter(iter);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003321 out:
3322 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003323
3324 return event;
3325}
Robert Richterc4f50182008-12-11 16:49:22 +01003326EXPORT_SYMBOL_GPL(ring_buffer_read);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003327
3328/**
3329 * ring_buffer_size - return the size of the ring buffer (in bytes)
3330 * @buffer: The ring buffer.
3331 */
3332unsigned long ring_buffer_size(struct ring_buffer *buffer)
3333{
3334 return BUF_PAGE_SIZE * buffer->pages;
3335}
Robert Richterc4f50182008-12-11 16:49:22 +01003336EXPORT_SYMBOL_GPL(ring_buffer_size);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003337
3338static void
3339rb_reset_cpu(struct ring_buffer_per_cpu *cpu_buffer)
3340{
Steven Rostedt77ae3652009-03-27 11:00:29 -04003341 rb_head_page_deactivate(cpu_buffer);
3342
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003343 cpu_buffer->head_page
Steven Rostedt3adc54f2009-03-30 15:32:01 -04003344 = list_entry(cpu_buffer->pages, struct buffer_page, list);
Steven Rostedtbf41a152008-10-04 02:00:59 -04003345 local_set(&cpu_buffer->head_page->write, 0);
Steven Rostedt778c55d2009-05-01 18:44:45 -04003346 local_set(&cpu_buffer->head_page->entries, 0);
Steven Rostedtabc9b562008-12-02 15:34:06 -05003347 local_set(&cpu_buffer->head_page->page->commit, 0);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003348
Steven Rostedt6f807ac2008-10-04 02:00:58 -04003349 cpu_buffer->head_page->read = 0;
Steven Rostedtbf41a152008-10-04 02:00:59 -04003350
3351 cpu_buffer->tail_page = cpu_buffer->head_page;
3352 cpu_buffer->commit_page = cpu_buffer->head_page;
3353
3354 INIT_LIST_HEAD(&cpu_buffer->reader_page->list);
3355 local_set(&cpu_buffer->reader_page->write, 0);
Steven Rostedt778c55d2009-05-01 18:44:45 -04003356 local_set(&cpu_buffer->reader_page->entries, 0);
Steven Rostedtabc9b562008-12-02 15:34:06 -05003357 local_set(&cpu_buffer->reader_page->page->commit, 0);
Steven Rostedt6f807ac2008-10-04 02:00:58 -04003358 cpu_buffer->reader_page->read = 0;
Steven Rostedtd7690412008-10-01 00:29:53 -04003359
Steven Rostedt77ae3652009-03-27 11:00:29 -04003360 local_set(&cpu_buffer->commit_overrun, 0);
3361 local_set(&cpu_buffer->overrun, 0);
Steven Rostedte4906ef2009-04-30 20:49:44 -04003362 local_set(&cpu_buffer->entries, 0);
Steven Rostedtfa743952009-06-16 12:37:57 -04003363 local_set(&cpu_buffer->committing, 0);
3364 local_set(&cpu_buffer->commits, 0);
Steven Rostedt77ae3652009-03-27 11:00:29 -04003365 cpu_buffer->read = 0;
Steven Rostedt69507c02009-01-21 18:45:57 -05003366
3367 cpu_buffer->write_stamp = 0;
3368 cpu_buffer->read_stamp = 0;
Steven Rostedt77ae3652009-03-27 11:00:29 -04003369
3370 rb_head_page_activate(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003371}
3372
3373/**
3374 * ring_buffer_reset_cpu - reset a ring buffer per CPU buffer
3375 * @buffer: The ring buffer to reset a per cpu buffer of
3376 * @cpu: The CPU buffer to be reset
3377 */
3378void ring_buffer_reset_cpu(struct ring_buffer *buffer, int cpu)
3379{
3380 struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
3381 unsigned long flags;
3382
Rusty Russell9e01c1b2009-01-01 10:12:22 +10303383 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04003384 return;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003385
Steven Rostedt41ede232009-05-01 20:26:54 -04003386 atomic_inc(&cpu_buffer->record_disabled);
3387
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003388 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
3389
Steven Rostedt41b6a952009-09-02 09:59:48 -04003390 if (RB_WARN_ON(cpu_buffer, local_read(&cpu_buffer->committing)))
3391 goto out;
3392
Steven Rostedt3e03fb72008-11-06 00:09:43 -05003393 __raw_spin_lock(&cpu_buffer->lock);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003394
3395 rb_reset_cpu(cpu_buffer);
3396
Steven Rostedt3e03fb72008-11-06 00:09:43 -05003397 __raw_spin_unlock(&cpu_buffer->lock);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003398
Steven Rostedt41b6a952009-09-02 09:59:48 -04003399 out:
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003400 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
Steven Rostedt41ede232009-05-01 20:26:54 -04003401
3402 atomic_dec(&cpu_buffer->record_disabled);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003403}
Robert Richterc4f50182008-12-11 16:49:22 +01003404EXPORT_SYMBOL_GPL(ring_buffer_reset_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003405
3406/**
3407 * ring_buffer_reset - reset a ring buffer
3408 * @buffer: The ring buffer to reset all cpu buffers
3409 */
3410void ring_buffer_reset(struct ring_buffer *buffer)
3411{
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003412 int cpu;
3413
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003414 for_each_buffer_cpu(buffer, cpu)
Steven Rostedtd7690412008-10-01 00:29:53 -04003415 ring_buffer_reset_cpu(buffer, cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003416}
Robert Richterc4f50182008-12-11 16:49:22 +01003417EXPORT_SYMBOL_GPL(ring_buffer_reset);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003418
3419/**
3420 * rind_buffer_empty - is the ring buffer empty?
3421 * @buffer: The ring buffer to test
3422 */
3423int ring_buffer_empty(struct ring_buffer *buffer)
3424{
3425 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedtd4788202009-06-17 00:39:43 -04003426 unsigned long flags;
Steven Rostedt8d707e82009-06-16 21:22:48 -04003427 int dolock;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003428 int cpu;
Steven Rostedtd4788202009-06-17 00:39:43 -04003429 int ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003430
Steven Rostedt8d707e82009-06-16 21:22:48 -04003431 dolock = rb_ok_to_lock();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003432
3433 /* yes this is racy, but if you don't like the race, lock the buffer */
3434 for_each_buffer_cpu(buffer, cpu) {
3435 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt8d707e82009-06-16 21:22:48 -04003436 local_irq_save(flags);
3437 if (dolock)
3438 spin_lock(&cpu_buffer->reader_lock);
Steven Rostedtd4788202009-06-17 00:39:43 -04003439 ret = rb_per_cpu_empty(cpu_buffer);
Steven Rostedt8d707e82009-06-16 21:22:48 -04003440 if (dolock)
3441 spin_unlock(&cpu_buffer->reader_lock);
3442 local_irq_restore(flags);
3443
Steven Rostedtd4788202009-06-17 00:39:43 -04003444 if (!ret)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003445 return 0;
3446 }
Steven Rostedt554f7862009-03-11 22:00:13 -04003447
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003448 return 1;
3449}
Robert Richterc4f50182008-12-11 16:49:22 +01003450EXPORT_SYMBOL_GPL(ring_buffer_empty);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003451
3452/**
3453 * ring_buffer_empty_cpu - is a cpu buffer of a ring buffer empty?
3454 * @buffer: The ring buffer
3455 * @cpu: The CPU buffer to test
3456 */
3457int ring_buffer_empty_cpu(struct ring_buffer *buffer, int cpu)
3458{
3459 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedtd4788202009-06-17 00:39:43 -04003460 unsigned long flags;
Steven Rostedt8d707e82009-06-16 21:22:48 -04003461 int dolock;
Steven Rostedt8aabee52009-03-12 13:13:49 -04003462 int ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003463
Rusty Russell9e01c1b2009-01-01 10:12:22 +10303464 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04003465 return 1;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003466
Steven Rostedt8d707e82009-06-16 21:22:48 -04003467 dolock = rb_ok_to_lock();
Steven Rostedt554f7862009-03-11 22:00:13 -04003468
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003469 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt8d707e82009-06-16 21:22:48 -04003470 local_irq_save(flags);
3471 if (dolock)
3472 spin_lock(&cpu_buffer->reader_lock);
Steven Rostedt554f7862009-03-11 22:00:13 -04003473 ret = rb_per_cpu_empty(cpu_buffer);
Steven Rostedt8d707e82009-06-16 21:22:48 -04003474 if (dolock)
3475 spin_unlock(&cpu_buffer->reader_lock);
3476 local_irq_restore(flags);
Steven Rostedt554f7862009-03-11 22:00:13 -04003477
3478 return ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003479}
Robert Richterc4f50182008-12-11 16:49:22 +01003480EXPORT_SYMBOL_GPL(ring_buffer_empty_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003481
3482/**
3483 * ring_buffer_swap_cpu - swap a CPU buffer between two ring buffers
3484 * @buffer_a: One buffer to swap with
3485 * @buffer_b: The other buffer to swap with
3486 *
3487 * This function is useful for tracers that want to take a "snapshot"
3488 * of a CPU buffer and has another back up buffer lying around.
3489 * it is expected that the tracer handles the cpu buffer not being
3490 * used at the moment.
3491 */
3492int ring_buffer_swap_cpu(struct ring_buffer *buffer_a,
3493 struct ring_buffer *buffer_b, int cpu)
3494{
3495 struct ring_buffer_per_cpu *cpu_buffer_a;
3496 struct ring_buffer_per_cpu *cpu_buffer_b;
Steven Rostedt554f7862009-03-11 22:00:13 -04003497 int ret = -EINVAL;
3498
Rusty Russell9e01c1b2009-01-01 10:12:22 +10303499 if (!cpumask_test_cpu(cpu, buffer_a->cpumask) ||
3500 !cpumask_test_cpu(cpu, buffer_b->cpumask))
Steven Rostedt554f7862009-03-11 22:00:13 -04003501 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003502
3503 /* At least make sure the two buffers are somewhat the same */
Lai Jiangshan6d102bc2008-12-17 17:48:23 +08003504 if (buffer_a->pages != buffer_b->pages)
Steven Rostedt554f7862009-03-11 22:00:13 -04003505 goto out;
3506
3507 ret = -EAGAIN;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003508
Steven Rostedt97b17ef2009-01-21 15:24:56 -05003509 if (ring_buffer_flags != RB_BUFFERS_ON)
Steven Rostedt554f7862009-03-11 22:00:13 -04003510 goto out;
Steven Rostedt97b17ef2009-01-21 15:24:56 -05003511
3512 if (atomic_read(&buffer_a->record_disabled))
Steven Rostedt554f7862009-03-11 22:00:13 -04003513 goto out;
Steven Rostedt97b17ef2009-01-21 15:24:56 -05003514
3515 if (atomic_read(&buffer_b->record_disabled))
Steven Rostedt554f7862009-03-11 22:00:13 -04003516 goto out;
Steven Rostedt97b17ef2009-01-21 15:24:56 -05003517
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003518 cpu_buffer_a = buffer_a->buffers[cpu];
3519 cpu_buffer_b = buffer_b->buffers[cpu];
3520
Steven Rostedt97b17ef2009-01-21 15:24:56 -05003521 if (atomic_read(&cpu_buffer_a->record_disabled))
Steven Rostedt554f7862009-03-11 22:00:13 -04003522 goto out;
Steven Rostedt97b17ef2009-01-21 15:24:56 -05003523
3524 if (atomic_read(&cpu_buffer_b->record_disabled))
Steven Rostedt554f7862009-03-11 22:00:13 -04003525 goto out;
Steven Rostedt97b17ef2009-01-21 15:24:56 -05003526
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003527 /*
3528 * We can't do a synchronize_sched here because this
3529 * function can be called in atomic context.
3530 * Normally this will be called from the same CPU as cpu.
3531 * If not it's up to the caller to protect this.
3532 */
3533 atomic_inc(&cpu_buffer_a->record_disabled);
3534 atomic_inc(&cpu_buffer_b->record_disabled);
3535
Steven Rostedt98277992009-09-02 10:56:15 -04003536 ret = -EBUSY;
3537 if (local_read(&cpu_buffer_a->committing))
3538 goto out_dec;
3539 if (local_read(&cpu_buffer_b->committing))
3540 goto out_dec;
3541
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003542 buffer_a->buffers[cpu] = cpu_buffer_b;
3543 buffer_b->buffers[cpu] = cpu_buffer_a;
3544
3545 cpu_buffer_b->buffer = buffer_a;
3546 cpu_buffer_a->buffer = buffer_b;
3547
Steven Rostedt98277992009-09-02 10:56:15 -04003548 ret = 0;
3549
3550out_dec:
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003551 atomic_dec(&cpu_buffer_a->record_disabled);
3552 atomic_dec(&cpu_buffer_b->record_disabled);
Steven Rostedt554f7862009-03-11 22:00:13 -04003553out:
Steven Rostedt554f7862009-03-11 22:00:13 -04003554 return ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003555}
Robert Richterc4f50182008-12-11 16:49:22 +01003556EXPORT_SYMBOL_GPL(ring_buffer_swap_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003557
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003558/**
3559 * ring_buffer_alloc_read_page - allocate a page to read from buffer
3560 * @buffer: the buffer to allocate for.
3561 *
3562 * This function is used in conjunction with ring_buffer_read_page.
3563 * When reading a full page from the ring buffer, these functions
3564 * can be used to speed up the process. The calling function should
3565 * allocate a few pages first with this function. Then when it
3566 * needs to get pages from the ring buffer, it passes the result
3567 * of this function into ring_buffer_read_page, which will swap
3568 * the page that was allocated, with the read page of the buffer.
3569 *
3570 * Returns:
3571 * The page allocated, or NULL on error.
3572 */
3573void *ring_buffer_alloc_read_page(struct ring_buffer *buffer)
3574{
Steven Rostedt044fa782008-12-02 23:50:03 -05003575 struct buffer_data_page *bpage;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003576 unsigned long addr;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003577
3578 addr = __get_free_page(GFP_KERNEL);
3579 if (!addr)
3580 return NULL;
3581
Steven Rostedt044fa782008-12-02 23:50:03 -05003582 bpage = (void *)addr;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003583
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003584 rb_init_page(bpage);
3585
Steven Rostedt044fa782008-12-02 23:50:03 -05003586 return bpage;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003587}
Steven Rostedtd6ce96d2009-05-05 01:15:24 -04003588EXPORT_SYMBOL_GPL(ring_buffer_alloc_read_page);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003589
3590/**
3591 * ring_buffer_free_read_page - free an allocated read page
3592 * @buffer: the buffer the page was allocate for
3593 * @data: the page to free
3594 *
3595 * Free a page allocated from ring_buffer_alloc_read_page.
3596 */
3597void ring_buffer_free_read_page(struct ring_buffer *buffer, void *data)
3598{
3599 free_page((unsigned long)data);
3600}
Steven Rostedtd6ce96d2009-05-05 01:15:24 -04003601EXPORT_SYMBOL_GPL(ring_buffer_free_read_page);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003602
3603/**
3604 * ring_buffer_read_page - extract a page from the ring buffer
3605 * @buffer: buffer to extract from
3606 * @data_page: the page to use allocated from ring_buffer_alloc_read_page
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003607 * @len: amount to extract
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003608 * @cpu: the cpu of the buffer to extract
3609 * @full: should the extraction only happen when the page is full.
3610 *
3611 * This function will pull out a page from the ring buffer and consume it.
3612 * @data_page must be the address of the variable that was returned
3613 * from ring_buffer_alloc_read_page. This is because the page might be used
3614 * to swap with a page in the ring buffer.
3615 *
3616 * for example:
Lai Jiangshanb85fa012009-02-09 14:21:14 +08003617 * rpage = ring_buffer_alloc_read_page(buffer);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003618 * if (!rpage)
3619 * return error;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003620 * ret = ring_buffer_read_page(buffer, &rpage, len, cpu, 0);
Lai Jiangshan667d2412009-02-09 14:21:17 +08003621 * if (ret >= 0)
3622 * process_page(rpage, ret);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003623 *
3624 * When @full is set, the function will not return true unless
3625 * the writer is off the reader page.
3626 *
3627 * Note: it is up to the calling functions to handle sleeps and wakeups.
3628 * The ring buffer can be used anywhere in the kernel and can not
3629 * blindly call wake_up. The layer that uses the ring buffer must be
3630 * responsible for that.
3631 *
3632 * Returns:
Lai Jiangshan667d2412009-02-09 14:21:17 +08003633 * >=0 if data has been transferred, returns the offset of consumed data.
3634 * <0 if no data has been transferred.
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003635 */
3636int ring_buffer_read_page(struct ring_buffer *buffer,
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003637 void **data_page, size_t len, int cpu, int full)
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003638{
3639 struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
3640 struct ring_buffer_event *event;
Steven Rostedt044fa782008-12-02 23:50:03 -05003641 struct buffer_data_page *bpage;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003642 struct buffer_page *reader;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003643 unsigned long flags;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003644 unsigned int commit;
Lai Jiangshan667d2412009-02-09 14:21:17 +08003645 unsigned int read;
Steven Rostedt4f3640f2009-03-03 23:52:42 -05003646 u64 save_timestamp;
Lai Jiangshan667d2412009-02-09 14:21:17 +08003647 int ret = -1;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003648
Steven Rostedt554f7862009-03-11 22:00:13 -04003649 if (!cpumask_test_cpu(cpu, buffer->cpumask))
3650 goto out;
3651
Steven Rostedt474d32b2009-03-03 19:51:40 -05003652 /*
3653 * If len is not big enough to hold the page header, then
3654 * we can not copy anything.
3655 */
3656 if (len <= BUF_PAGE_HDR_SIZE)
Steven Rostedt554f7862009-03-11 22:00:13 -04003657 goto out;
Steven Rostedt474d32b2009-03-03 19:51:40 -05003658
3659 len -= BUF_PAGE_HDR_SIZE;
3660
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003661 if (!data_page)
Steven Rostedt554f7862009-03-11 22:00:13 -04003662 goto out;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003663
Steven Rostedt044fa782008-12-02 23:50:03 -05003664 bpage = *data_page;
3665 if (!bpage)
Steven Rostedt554f7862009-03-11 22:00:13 -04003666 goto out;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003667
3668 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
3669
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003670 reader = rb_get_reader_page(cpu_buffer);
3671 if (!reader)
Steven Rostedt554f7862009-03-11 22:00:13 -04003672 goto out_unlock;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003673
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003674 event = rb_reader_event(cpu_buffer);
Lai Jiangshan667d2412009-02-09 14:21:17 +08003675
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003676 read = reader->read;
3677 commit = rb_page_commit(reader);
3678
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003679 /*
Steven Rostedt474d32b2009-03-03 19:51:40 -05003680 * If this page has been partially read or
3681 * if len is not big enough to read the rest of the page or
3682 * a writer is still on the page, then
3683 * we must copy the data from the page to the buffer.
3684 * Otherwise, we can simply swap the page with the one passed in.
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003685 */
Steven Rostedt474d32b2009-03-03 19:51:40 -05003686 if (read || (len < (commit - read)) ||
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003687 cpu_buffer->reader_page == cpu_buffer->commit_page) {
Lai Jiangshan667d2412009-02-09 14:21:17 +08003688 struct buffer_data_page *rpage = cpu_buffer->reader_page->page;
Steven Rostedt474d32b2009-03-03 19:51:40 -05003689 unsigned int rpos = read;
3690 unsigned int pos = 0;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003691 unsigned int size;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003692
3693 if (full)
Steven Rostedt554f7862009-03-11 22:00:13 -04003694 goto out_unlock;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003695
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003696 if (len > (commit - read))
3697 len = (commit - read);
3698
3699 size = rb_event_length(event);
3700
3701 if (len < size)
Steven Rostedt554f7862009-03-11 22:00:13 -04003702 goto out_unlock;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003703
Steven Rostedt4f3640f2009-03-03 23:52:42 -05003704 /* save the current timestamp, since the user will need it */
3705 save_timestamp = cpu_buffer->read_stamp;
3706
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003707 /* Need to copy one event at a time */
3708 do {
Steven Rostedt474d32b2009-03-03 19:51:40 -05003709 memcpy(bpage->data + pos, rpage->data + rpos, size);
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003710
3711 len -= size;
3712
3713 rb_advance_reader(cpu_buffer);
Steven Rostedt474d32b2009-03-03 19:51:40 -05003714 rpos = reader->read;
3715 pos += size;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003716
3717 event = rb_reader_event(cpu_buffer);
3718 size = rb_event_length(event);
3719 } while (len > size);
Lai Jiangshan667d2412009-02-09 14:21:17 +08003720
3721 /* update bpage */
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003722 local_set(&bpage->commit, pos);
Steven Rostedt4f3640f2009-03-03 23:52:42 -05003723 bpage->time_stamp = save_timestamp;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003724
Steven Rostedt474d32b2009-03-03 19:51:40 -05003725 /* we copied everything to the beginning */
3726 read = 0;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003727 } else {
Steven Rostedtafbab762009-05-01 19:40:05 -04003728 /* update the entry counter */
Steven Rostedt77ae3652009-03-27 11:00:29 -04003729 cpu_buffer->read += rb_page_entries(reader);
Steven Rostedtafbab762009-05-01 19:40:05 -04003730
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003731 /* swap the pages */
Steven Rostedt044fa782008-12-02 23:50:03 -05003732 rb_init_page(bpage);
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003733 bpage = reader->page;
3734 reader->page = *data_page;
3735 local_set(&reader->write, 0);
Steven Rostedt778c55d2009-05-01 18:44:45 -04003736 local_set(&reader->entries, 0);
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003737 reader->read = 0;
Steven Rostedt044fa782008-12-02 23:50:03 -05003738 *data_page = bpage;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003739 }
Lai Jiangshan667d2412009-02-09 14:21:17 +08003740 ret = read;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003741
Steven Rostedt554f7862009-03-11 22:00:13 -04003742 out_unlock:
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003743 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
3744
Steven Rostedt554f7862009-03-11 22:00:13 -04003745 out:
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003746 return ret;
3747}
Steven Rostedtd6ce96d2009-05-05 01:15:24 -04003748EXPORT_SYMBOL_GPL(ring_buffer_read_page);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003749
Paul Mundt1155de42009-06-25 14:30:12 +09003750#ifdef CONFIG_TRACING
Steven Rostedta3583242008-11-11 15:01:42 -05003751static ssize_t
3752rb_simple_read(struct file *filp, char __user *ubuf,
3753 size_t cnt, loff_t *ppos)
3754{
Hannes Eder5e398412009-02-10 19:44:34 +01003755 unsigned long *p = filp->private_data;
Steven Rostedta3583242008-11-11 15:01:42 -05003756 char buf[64];
3757 int r;
3758
Steven Rostedt033601a2008-11-21 12:41:55 -05003759 if (test_bit(RB_BUFFERS_DISABLED_BIT, p))
3760 r = sprintf(buf, "permanently disabled\n");
3761 else
3762 r = sprintf(buf, "%d\n", test_bit(RB_BUFFERS_ON_BIT, p));
Steven Rostedta3583242008-11-11 15:01:42 -05003763
3764 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
3765}
3766
3767static ssize_t
3768rb_simple_write(struct file *filp, const char __user *ubuf,
3769 size_t cnt, loff_t *ppos)
3770{
Hannes Eder5e398412009-02-10 19:44:34 +01003771 unsigned long *p = filp->private_data;
Steven Rostedta3583242008-11-11 15:01:42 -05003772 char buf[64];
Hannes Eder5e398412009-02-10 19:44:34 +01003773 unsigned long val;
Steven Rostedta3583242008-11-11 15:01:42 -05003774 int ret;
3775
3776 if (cnt >= sizeof(buf))
3777 return -EINVAL;
3778
3779 if (copy_from_user(&buf, ubuf, cnt))
3780 return -EFAULT;
3781
3782 buf[cnt] = 0;
3783
3784 ret = strict_strtoul(buf, 10, &val);
3785 if (ret < 0)
3786 return ret;
3787
Steven Rostedt033601a2008-11-21 12:41:55 -05003788 if (val)
3789 set_bit(RB_BUFFERS_ON_BIT, p);
3790 else
3791 clear_bit(RB_BUFFERS_ON_BIT, p);
Steven Rostedta3583242008-11-11 15:01:42 -05003792
3793 (*ppos)++;
3794
3795 return cnt;
3796}
3797
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003798static const struct file_operations rb_simple_fops = {
Steven Rostedta3583242008-11-11 15:01:42 -05003799 .open = tracing_open_generic,
3800 .read = rb_simple_read,
3801 .write = rb_simple_write,
3802};
3803
3804
3805static __init int rb_init_debugfs(void)
3806{
3807 struct dentry *d_tracer;
Steven Rostedta3583242008-11-11 15:01:42 -05003808
3809 d_tracer = tracing_init_dentry();
3810
Frederic Weisbecker5452af62009-03-27 00:25:38 +01003811 trace_create_file("tracing_on", 0644, d_tracer,
3812 &ring_buffer_flags, &rb_simple_fops);
Steven Rostedta3583242008-11-11 15:01:42 -05003813
3814 return 0;
3815}
3816
3817fs_initcall(rb_init_debugfs);
Paul Mundt1155de42009-06-25 14:30:12 +09003818#endif
Steven Rostedt554f7862009-03-11 22:00:13 -04003819
Steven Rostedt59222ef2009-03-12 11:46:03 -04003820#ifdef CONFIG_HOTPLUG_CPU
Frederic Weisbecker09c9e842009-03-21 04:33:36 +01003821static int rb_cpu_notify(struct notifier_block *self,
3822 unsigned long action, void *hcpu)
Steven Rostedt554f7862009-03-11 22:00:13 -04003823{
3824 struct ring_buffer *buffer =
3825 container_of(self, struct ring_buffer, cpu_notify);
3826 long cpu = (long)hcpu;
3827
3828 switch (action) {
3829 case CPU_UP_PREPARE:
3830 case CPU_UP_PREPARE_FROZEN:
Rusty Russell3f237a72009-06-12 21:15:30 +09303831 if (cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt554f7862009-03-11 22:00:13 -04003832 return NOTIFY_OK;
3833
3834 buffer->buffers[cpu] =
3835 rb_allocate_cpu_buffer(buffer, cpu);
3836 if (!buffer->buffers[cpu]) {
3837 WARN(1, "failed to allocate ring buffer on CPU %ld\n",
3838 cpu);
3839 return NOTIFY_OK;
3840 }
3841 smp_wmb();
Rusty Russell3f237a72009-06-12 21:15:30 +09303842 cpumask_set_cpu(cpu, buffer->cpumask);
Steven Rostedt554f7862009-03-11 22:00:13 -04003843 break;
3844 case CPU_DOWN_PREPARE:
3845 case CPU_DOWN_PREPARE_FROZEN:
3846 /*
3847 * Do nothing.
3848 * If we were to free the buffer, then the user would
3849 * lose any trace that was in the buffer.
3850 */
3851 break;
3852 default:
3853 break;
3854 }
3855 return NOTIFY_OK;
3856}
3857#endif