blob: 1766c0e8db5a81b777e06eabb20487b68630d6b8 [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 Rostedt077c5402009-09-03 19:53:46 -0400470#define RB_WARN_ON(b, cond) \
471 ({ \
472 int _____ret = unlikely(cond); \
473 if (_____ret) { \
474 if (__same_type(*(b), struct ring_buffer_per_cpu)) { \
475 struct ring_buffer_per_cpu *__b = \
476 (void *)b; \
477 atomic_inc(&__b->buffer->record_disabled); \
478 } else \
479 atomic_inc(&b->record_disabled); \
480 WARN_ON(1); \
481 } \
482 _____ret; \
Steven Rostedt3e89c7b2008-11-11 15:28:41 -0500483 })
Steven Rostedtf536aaf2008-11-10 23:07:30 -0500484
Steven Rostedt37886f62009-03-17 17:22:06 -0400485/* Up this if you want to test the TIME_EXTENTS and normalization */
486#define DEBUG_SHIFT 0
487
Steven Rostedt88eb0122009-05-11 16:28:23 -0400488static inline u64 rb_time_stamp(struct ring_buffer *buffer, int cpu)
489{
490 /* shift to debug/test normalization and TIME_EXTENTS */
491 return buffer->clock() << DEBUG_SHIFT;
492}
493
Steven Rostedt37886f62009-03-17 17:22:06 -0400494u64 ring_buffer_time_stamp(struct ring_buffer *buffer, int cpu)
495{
496 u64 time;
497
498 preempt_disable_notrace();
Steven Rostedt88eb0122009-05-11 16:28:23 -0400499 time = rb_time_stamp(buffer, cpu);
Steven Rostedt37886f62009-03-17 17:22:06 -0400500 preempt_enable_no_resched_notrace();
501
502 return time;
503}
504EXPORT_SYMBOL_GPL(ring_buffer_time_stamp);
505
506void ring_buffer_normalize_time_stamp(struct ring_buffer *buffer,
507 int cpu, u64 *ts)
508{
509 /* Just stupid testing the normalize function and deltas */
510 *ts >>= DEBUG_SHIFT;
511}
512EXPORT_SYMBOL_GPL(ring_buffer_normalize_time_stamp);
513
Steven Rostedt77ae3652009-03-27 11:00:29 -0400514/*
515 * Making the ring buffer lockless makes things tricky.
516 * Although writes only happen on the CPU that they are on,
517 * and they only need to worry about interrupts. Reads can
518 * happen on any CPU.
519 *
520 * The reader page is always off the ring buffer, but when the
521 * reader finishes with a page, it needs to swap its page with
522 * a new one from the buffer. The reader needs to take from
523 * the head (writes go to the tail). But if a writer is in overwrite
524 * mode and wraps, it must push the head page forward.
525 *
526 * Here lies the problem.
527 *
528 * The reader must be careful to replace only the head page, and
529 * not another one. As described at the top of the file in the
530 * ASCII art, the reader sets its old page to point to the next
531 * page after head. It then sets the page after head to point to
532 * the old reader page. But if the writer moves the head page
533 * during this operation, the reader could end up with the tail.
534 *
535 * We use cmpxchg to help prevent this race. We also do something
536 * special with the page before head. We set the LSB to 1.
537 *
538 * When the writer must push the page forward, it will clear the
539 * bit that points to the head page, move the head, and then set
540 * the bit that points to the new head page.
541 *
542 * We also don't want an interrupt coming in and moving the head
543 * page on another writer. Thus we use the second LSB to catch
544 * that too. Thus:
545 *
546 * head->list->prev->next bit 1 bit 0
547 * ------- -------
548 * Normal page 0 0
549 * Points to head page 0 1
550 * New head page 1 0
551 *
552 * Note we can not trust the prev pointer of the head page, because:
553 *
554 * +----+ +-----+ +-----+
555 * | |------>| T |---X--->| N |
556 * | |<------| | | |
557 * +----+ +-----+ +-----+
558 * ^ ^ |
559 * | +-----+ | |
560 * +----------| R |----------+ |
561 * | |<-----------+
562 * +-----+
563 *
564 * Key: ---X--> HEAD flag set in pointer
565 * T Tail page
566 * R Reader page
567 * N Next page
568 *
569 * (see __rb_reserve_next() to see where this happens)
570 *
571 * What the above shows is that the reader just swapped out
572 * the reader page with a page in the buffer, but before it
573 * could make the new header point back to the new page added
574 * it was preempted by a writer. The writer moved forward onto
575 * the new page added by the reader and is about to move forward
576 * again.
577 *
578 * You can see, it is legitimate for the previous pointer of
579 * the head (or any page) not to point back to itself. But only
580 * temporarially.
581 */
582
583#define RB_PAGE_NORMAL 0UL
584#define RB_PAGE_HEAD 1UL
585#define RB_PAGE_UPDATE 2UL
586
587
588#define RB_FLAG_MASK 3UL
589
590/* PAGE_MOVED is not part of the mask */
591#define RB_PAGE_MOVED 4UL
592
593/*
594 * rb_list_head - remove any bit
595 */
596static struct list_head *rb_list_head(struct list_head *list)
597{
598 unsigned long val = (unsigned long)list;
599
600 return (struct list_head *)(val & ~RB_FLAG_MASK);
601}
602
603/*
604 * rb_is_head_page - test if the give page is the head page
605 *
606 * Because the reader may move the head_page pointer, we can
607 * not trust what the head page is (it may be pointing to
608 * the reader page). But if the next page is a header page,
609 * its flags will be non zero.
610 */
611static int inline
612rb_is_head_page(struct ring_buffer_per_cpu *cpu_buffer,
613 struct buffer_page *page, struct list_head *list)
614{
615 unsigned long val;
616
617 val = (unsigned long)list->next;
618
619 if ((val & ~RB_FLAG_MASK) != (unsigned long)&page->list)
620 return RB_PAGE_MOVED;
621
622 return val & RB_FLAG_MASK;
623}
624
625/*
626 * rb_is_reader_page
627 *
628 * The unique thing about the reader page, is that, if the
629 * writer is ever on it, the previous pointer never points
630 * back to the reader page.
631 */
632static int rb_is_reader_page(struct buffer_page *page)
633{
634 struct list_head *list = page->list.prev;
635
636 return rb_list_head(list->next) != &page->list;
637}
638
639/*
640 * rb_set_list_to_head - set a list_head to be pointing to head.
641 */
642static void rb_set_list_to_head(struct ring_buffer_per_cpu *cpu_buffer,
643 struct list_head *list)
644{
645 unsigned long *ptr;
646
647 ptr = (unsigned long *)&list->next;
648 *ptr |= RB_PAGE_HEAD;
649 *ptr &= ~RB_PAGE_UPDATE;
650}
651
652/*
653 * rb_head_page_activate - sets up head page
654 */
655static void rb_head_page_activate(struct ring_buffer_per_cpu *cpu_buffer)
656{
657 struct buffer_page *head;
658
659 head = cpu_buffer->head_page;
660 if (!head)
661 return;
662
663 /*
664 * Set the previous list pointer to have the HEAD flag.
665 */
666 rb_set_list_to_head(cpu_buffer, head->list.prev);
667}
668
669static void rb_list_head_clear(struct list_head *list)
670{
671 unsigned long *ptr = (unsigned long *)&list->next;
672
673 *ptr &= ~RB_FLAG_MASK;
674}
675
676/*
677 * rb_head_page_dactivate - clears head page ptr (for free list)
678 */
679static void
680rb_head_page_deactivate(struct ring_buffer_per_cpu *cpu_buffer)
681{
682 struct list_head *hd;
683
684 /* Go through the whole list and clear any pointers found. */
685 rb_list_head_clear(cpu_buffer->pages);
686
687 list_for_each(hd, cpu_buffer->pages)
688 rb_list_head_clear(hd);
689}
690
691static int rb_head_page_set(struct ring_buffer_per_cpu *cpu_buffer,
692 struct buffer_page *head,
693 struct buffer_page *prev,
694 int old_flag, int new_flag)
695{
696 struct list_head *list;
697 unsigned long val = (unsigned long)&head->list;
698 unsigned long ret;
699
700 list = &prev->list;
701
702 val &= ~RB_FLAG_MASK;
703
704 ret = (unsigned long)cmpxchg(&list->next,
705 val | old_flag, val | new_flag);
706
707 /* check if the reader took the page */
708 if ((ret & ~RB_FLAG_MASK) != val)
709 return RB_PAGE_MOVED;
710
711 return ret & RB_FLAG_MASK;
712}
713
714static int rb_head_page_set_update(struct ring_buffer_per_cpu *cpu_buffer,
715 struct buffer_page *head,
716 struct buffer_page *prev,
717 int old_flag)
718{
719 return rb_head_page_set(cpu_buffer, head, prev,
720 old_flag, RB_PAGE_UPDATE);
721}
722
723static int rb_head_page_set_head(struct ring_buffer_per_cpu *cpu_buffer,
724 struct buffer_page *head,
725 struct buffer_page *prev,
726 int old_flag)
727{
728 return rb_head_page_set(cpu_buffer, head, prev,
729 old_flag, RB_PAGE_HEAD);
730}
731
732static int rb_head_page_set_normal(struct ring_buffer_per_cpu *cpu_buffer,
733 struct buffer_page *head,
734 struct buffer_page *prev,
735 int old_flag)
736{
737 return rb_head_page_set(cpu_buffer, head, prev,
738 old_flag, RB_PAGE_NORMAL);
739}
740
741static inline void rb_inc_page(struct ring_buffer_per_cpu *cpu_buffer,
742 struct buffer_page **bpage)
743{
744 struct list_head *p = rb_list_head((*bpage)->list.next);
745
746 *bpage = list_entry(p, struct buffer_page, list);
747}
748
749static struct buffer_page *
750rb_set_head_page(struct ring_buffer_per_cpu *cpu_buffer)
751{
752 struct buffer_page *head;
753 struct buffer_page *page;
754 struct list_head *list;
755 int i;
756
757 if (RB_WARN_ON(cpu_buffer, !cpu_buffer->head_page))
758 return NULL;
759
760 /* sanity check */
761 list = cpu_buffer->pages;
762 if (RB_WARN_ON(cpu_buffer, rb_list_head(list->prev->next) != list))
763 return NULL;
764
765 page = head = cpu_buffer->head_page;
766 /*
767 * It is possible that the writer moves the header behind
768 * where we started, and we miss in one loop.
769 * A second loop should grab the header, but we'll do
770 * three loops just because I'm paranoid.
771 */
772 for (i = 0; i < 3; i++) {
773 do {
774 if (rb_is_head_page(cpu_buffer, page, page->list.prev)) {
775 cpu_buffer->head_page = page;
776 return page;
777 }
778 rb_inc_page(cpu_buffer, &page);
779 } while (page != head);
780 }
781
782 RB_WARN_ON(cpu_buffer, 1);
783
784 return NULL;
785}
786
787static int rb_head_page_replace(struct buffer_page *old,
788 struct buffer_page *new)
789{
790 unsigned long *ptr = (unsigned long *)&old->list.prev->next;
791 unsigned long val;
792 unsigned long ret;
793
794 val = *ptr & ~RB_FLAG_MASK;
795 val |= RB_PAGE_HEAD;
796
797 ret = cmpxchg(ptr, val, &new->list);
798
799 return ret == val;
800}
801
802/*
803 * rb_tail_page_update - move the tail page forward
804 *
805 * Returns 1 if moved tail page, 0 if someone else did.
806 */
807static int rb_tail_page_update(struct ring_buffer_per_cpu *cpu_buffer,
808 struct buffer_page *tail_page,
809 struct buffer_page *next_page)
810{
811 struct buffer_page *old_tail;
812 unsigned long old_entries;
813 unsigned long old_write;
814 int ret = 0;
815
816 /*
817 * The tail page now needs to be moved forward.
818 *
819 * We need to reset the tail page, but without messing
820 * with possible erasing of data brought in by interrupts
821 * that have moved the tail page and are currently on it.
822 *
823 * We add a counter to the write field to denote this.
824 */
825 old_write = local_add_return(RB_WRITE_INTCNT, &next_page->write);
826 old_entries = local_add_return(RB_WRITE_INTCNT, &next_page->entries);
827
828 /*
829 * Just make sure we have seen our old_write and synchronize
830 * with any interrupts that come in.
831 */
832 barrier();
833
834 /*
835 * If the tail page is still the same as what we think
836 * it is, then it is up to us to update the tail
837 * pointer.
838 */
839 if (tail_page == cpu_buffer->tail_page) {
840 /* Zero the write counter */
841 unsigned long val = old_write & ~RB_WRITE_MASK;
842 unsigned long eval = old_entries & ~RB_WRITE_MASK;
843
844 /*
845 * This will only succeed if an interrupt did
846 * not come in and change it. In which case, we
847 * do not want to modify it.
Lai Jiangshanda706d82009-07-15 16:27:30 +0800848 *
849 * We add (void) to let the compiler know that we do not care
850 * about the return value of these functions. We use the
851 * cmpxchg to only update if an interrupt did not already
852 * do it for us. If the cmpxchg fails, we don't care.
Steven Rostedt77ae3652009-03-27 11:00:29 -0400853 */
Lai Jiangshanda706d82009-07-15 16:27:30 +0800854 (void)local_cmpxchg(&next_page->write, old_write, val);
855 (void)local_cmpxchg(&next_page->entries, old_entries, eval);
Steven Rostedt77ae3652009-03-27 11:00:29 -0400856
857 /*
858 * No need to worry about races with clearing out the commit.
859 * it only can increment when a commit takes place. But that
860 * only happens in the outer most nested commit.
861 */
862 local_set(&next_page->page->commit, 0);
863
864 old_tail = cmpxchg(&cpu_buffer->tail_page,
865 tail_page, next_page);
866
867 if (old_tail == tail_page)
868 ret = 1;
869 }
870
871 return ret;
872}
873
874static int rb_check_bpage(struct ring_buffer_per_cpu *cpu_buffer,
875 struct buffer_page *bpage)
876{
877 unsigned long val = (unsigned long)bpage;
878
879 if (RB_WARN_ON(cpu_buffer, val & RB_FLAG_MASK))
880 return 1;
881
882 return 0;
883}
884
885/**
886 * rb_check_list - make sure a pointer to a list has the last bits zero
887 */
888static int rb_check_list(struct ring_buffer_per_cpu *cpu_buffer,
889 struct list_head *list)
890{
891 if (RB_WARN_ON(cpu_buffer, rb_list_head(list->prev) != list->prev))
892 return 1;
893 if (RB_WARN_ON(cpu_buffer, rb_list_head(list->next) != list->next))
894 return 1;
895 return 0;
896}
897
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400898/**
899 * check_pages - integrity check of buffer pages
900 * @cpu_buffer: CPU buffer with pages to test
901 *
Wenji Huangc3706f02009-02-10 01:03:18 -0500902 * As a safety measure we check to make sure the data pages have not
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400903 * been corrupted.
904 */
905static int rb_check_pages(struct ring_buffer_per_cpu *cpu_buffer)
906{
Steven Rostedt3adc54f2009-03-30 15:32:01 -0400907 struct list_head *head = cpu_buffer->pages;
Steven Rostedt044fa782008-12-02 23:50:03 -0500908 struct buffer_page *bpage, *tmp;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400909
Steven Rostedt77ae3652009-03-27 11:00:29 -0400910 rb_head_page_deactivate(cpu_buffer);
911
Steven Rostedt3e89c7b2008-11-11 15:28:41 -0500912 if (RB_WARN_ON(cpu_buffer, head->next->prev != head))
913 return -1;
914 if (RB_WARN_ON(cpu_buffer, head->prev->next != head))
915 return -1;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400916
Steven Rostedt77ae3652009-03-27 11:00:29 -0400917 if (rb_check_list(cpu_buffer, head))
918 return -1;
919
Steven Rostedt044fa782008-12-02 23:50:03 -0500920 list_for_each_entry_safe(bpage, tmp, head, list) {
Steven Rostedt3e89c7b2008-11-11 15:28:41 -0500921 if (RB_WARN_ON(cpu_buffer,
Steven Rostedt044fa782008-12-02 23:50:03 -0500922 bpage->list.next->prev != &bpage->list))
Steven Rostedt3e89c7b2008-11-11 15:28:41 -0500923 return -1;
924 if (RB_WARN_ON(cpu_buffer,
Steven Rostedt044fa782008-12-02 23:50:03 -0500925 bpage->list.prev->next != &bpage->list))
Steven Rostedt3e89c7b2008-11-11 15:28:41 -0500926 return -1;
Steven Rostedt77ae3652009-03-27 11:00:29 -0400927 if (rb_check_list(cpu_buffer, &bpage->list))
928 return -1;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400929 }
930
Steven Rostedt77ae3652009-03-27 11:00:29 -0400931 rb_head_page_activate(cpu_buffer);
932
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400933 return 0;
934}
935
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400936static int rb_allocate_pages(struct ring_buffer_per_cpu *cpu_buffer,
937 unsigned nr_pages)
938{
Steven Rostedt044fa782008-12-02 23:50:03 -0500939 struct buffer_page *bpage, *tmp;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400940 unsigned long addr;
941 LIST_HEAD(pages);
942 unsigned i;
943
Steven Rostedt3adc54f2009-03-30 15:32:01 -0400944 WARN_ON(!nr_pages);
945
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400946 for (i = 0; i < nr_pages; i++) {
Steven Rostedt044fa782008-12-02 23:50:03 -0500947 bpage = kzalloc_node(ALIGN(sizeof(*bpage), cache_line_size()),
Steven Rostedtaa1e0e3b2008-10-02 19:18:09 -0400948 GFP_KERNEL, cpu_to_node(cpu_buffer->cpu));
Steven Rostedt044fa782008-12-02 23:50:03 -0500949 if (!bpage)
Steven Rostedte4c2ce82008-10-01 11:14:54 -0400950 goto free_pages;
Steven Rostedt77ae3652009-03-27 11:00:29 -0400951
952 rb_check_bpage(cpu_buffer, bpage);
953
Steven Rostedt044fa782008-12-02 23:50:03 -0500954 list_add(&bpage->list, &pages);
Steven Rostedte4c2ce82008-10-01 11:14:54 -0400955
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400956 addr = __get_free_page(GFP_KERNEL);
957 if (!addr)
958 goto free_pages;
Steven Rostedt044fa782008-12-02 23:50:03 -0500959 bpage->page = (void *)addr;
960 rb_init_page(bpage->page);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400961 }
962
Steven Rostedt3adc54f2009-03-30 15:32:01 -0400963 /*
964 * The ring buffer page list is a circular list that does not
965 * start and end with a list head. All page list items point to
966 * other pages.
967 */
968 cpu_buffer->pages = pages.next;
969 list_del(&pages);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400970
971 rb_check_pages(cpu_buffer);
972
973 return 0;
974
975 free_pages:
Steven Rostedt044fa782008-12-02 23:50:03 -0500976 list_for_each_entry_safe(bpage, tmp, &pages, list) {
977 list_del_init(&bpage->list);
978 free_buffer_page(bpage);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400979 }
980 return -ENOMEM;
981}
982
983static struct ring_buffer_per_cpu *
984rb_allocate_cpu_buffer(struct ring_buffer *buffer, int cpu)
985{
986 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedt044fa782008-12-02 23:50:03 -0500987 struct buffer_page *bpage;
Steven Rostedtd7690412008-10-01 00:29:53 -0400988 unsigned long addr;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400989 int ret;
990
991 cpu_buffer = kzalloc_node(ALIGN(sizeof(*cpu_buffer), cache_line_size()),
992 GFP_KERNEL, cpu_to_node(cpu));
993 if (!cpu_buffer)
994 return NULL;
995
996 cpu_buffer->cpu = cpu;
997 cpu_buffer->buffer = buffer;
Steven Rostedtf83c9d02008-11-11 18:47:44 +0100998 spin_lock_init(&cpu_buffer->reader_lock);
Peter Zijlstra1f8a6a12009-06-08 18:18:39 +0200999 lockdep_set_class(&cpu_buffer->reader_lock, buffer->reader_lock_key);
Steven Rostedt3e03fb72008-11-06 00:09:43 -05001000 cpu_buffer->lock = (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001001
Steven Rostedt044fa782008-12-02 23:50:03 -05001002 bpage = kzalloc_node(ALIGN(sizeof(*bpage), cache_line_size()),
Steven Rostedte4c2ce82008-10-01 11:14:54 -04001003 GFP_KERNEL, cpu_to_node(cpu));
Steven Rostedt044fa782008-12-02 23:50:03 -05001004 if (!bpage)
Steven Rostedte4c2ce82008-10-01 11:14:54 -04001005 goto fail_free_buffer;
1006
Steven Rostedt77ae3652009-03-27 11:00:29 -04001007 rb_check_bpage(cpu_buffer, bpage);
1008
Steven Rostedt044fa782008-12-02 23:50:03 -05001009 cpu_buffer->reader_page = bpage;
Steven Rostedtd7690412008-10-01 00:29:53 -04001010 addr = __get_free_page(GFP_KERNEL);
1011 if (!addr)
Steven Rostedte4c2ce82008-10-01 11:14:54 -04001012 goto fail_free_reader;
Steven Rostedt044fa782008-12-02 23:50:03 -05001013 bpage->page = (void *)addr;
1014 rb_init_page(bpage->page);
Steven Rostedte4c2ce82008-10-01 11:14:54 -04001015
Steven Rostedtd7690412008-10-01 00:29:53 -04001016 INIT_LIST_HEAD(&cpu_buffer->reader_page->list);
Steven Rostedtd7690412008-10-01 00:29:53 -04001017
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001018 ret = rb_allocate_pages(cpu_buffer, buffer->pages);
1019 if (ret < 0)
Steven Rostedtd7690412008-10-01 00:29:53 -04001020 goto fail_free_reader;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001021
1022 cpu_buffer->head_page
Steven Rostedt3adc54f2009-03-30 15:32:01 -04001023 = list_entry(cpu_buffer->pages, struct buffer_page, list);
Steven Rostedtbf41a152008-10-04 02:00:59 -04001024 cpu_buffer->tail_page = cpu_buffer->commit_page = cpu_buffer->head_page;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001025
Steven Rostedt77ae3652009-03-27 11:00:29 -04001026 rb_head_page_activate(cpu_buffer);
1027
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001028 return cpu_buffer;
1029
Steven Rostedtd7690412008-10-01 00:29:53 -04001030 fail_free_reader:
1031 free_buffer_page(cpu_buffer->reader_page);
1032
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001033 fail_free_buffer:
1034 kfree(cpu_buffer);
1035 return NULL;
1036}
1037
1038static void rb_free_cpu_buffer(struct ring_buffer_per_cpu *cpu_buffer)
1039{
Steven Rostedt3adc54f2009-03-30 15:32:01 -04001040 struct list_head *head = cpu_buffer->pages;
Steven Rostedt044fa782008-12-02 23:50:03 -05001041 struct buffer_page *bpage, *tmp;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001042
Steven Rostedtd7690412008-10-01 00:29:53 -04001043 free_buffer_page(cpu_buffer->reader_page);
1044
Steven Rostedt77ae3652009-03-27 11:00:29 -04001045 rb_head_page_deactivate(cpu_buffer);
1046
Steven Rostedt3adc54f2009-03-30 15:32:01 -04001047 if (head) {
1048 list_for_each_entry_safe(bpage, tmp, head, list) {
1049 list_del_init(&bpage->list);
1050 free_buffer_page(bpage);
1051 }
1052 bpage = list_entry(head, struct buffer_page, list);
Steven Rostedt044fa782008-12-02 23:50:03 -05001053 free_buffer_page(bpage);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001054 }
Steven Rostedt3adc54f2009-03-30 15:32:01 -04001055
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001056 kfree(cpu_buffer);
1057}
1058
Steven Rostedt59222ef2009-03-12 11:46:03 -04001059#ifdef CONFIG_HOTPLUG_CPU
Frederic Weisbecker09c9e842009-03-21 04:33:36 +01001060static int rb_cpu_notify(struct notifier_block *self,
1061 unsigned long action, void *hcpu);
Steven Rostedt554f7862009-03-11 22:00:13 -04001062#endif
1063
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001064/**
1065 * ring_buffer_alloc - allocate a new ring_buffer
Robert Richter68814b52008-11-24 12:24:12 +01001066 * @size: the size in bytes per cpu that is needed.
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001067 * @flags: attributes to set for the ring buffer.
1068 *
1069 * Currently the only flag that is available is the RB_FL_OVERWRITE
1070 * flag. This flag means that the buffer will overwrite old data
1071 * when the buffer wraps. If this flag is not set, the buffer will
1072 * drop data when the tail hits the head.
1073 */
Peter Zijlstra1f8a6a12009-06-08 18:18:39 +02001074struct ring_buffer *__ring_buffer_alloc(unsigned long size, unsigned flags,
1075 struct lock_class_key *key)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001076{
1077 struct ring_buffer *buffer;
1078 int bsize;
1079 int cpu;
1080
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001081 /* keep it in its own cache line */
1082 buffer = kzalloc(ALIGN(sizeof(*buffer), cache_line_size()),
1083 GFP_KERNEL);
1084 if (!buffer)
1085 return NULL;
1086
Rusty Russell9e01c1b2009-01-01 10:12:22 +10301087 if (!alloc_cpumask_var(&buffer->cpumask, GFP_KERNEL))
1088 goto fail_free_buffer;
1089
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001090 buffer->pages = DIV_ROUND_UP(size, BUF_PAGE_SIZE);
1091 buffer->flags = flags;
Steven Rostedt37886f62009-03-17 17:22:06 -04001092 buffer->clock = trace_clock_local;
Peter Zijlstra1f8a6a12009-06-08 18:18:39 +02001093 buffer->reader_lock_key = key;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001094
1095 /* need at least two pages */
Steven Rostedt5f78abe2009-06-17 14:11:10 -04001096 if (buffer->pages < 2)
1097 buffer->pages = 2;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001098
Frederic Weisbecker3bf832c2009-03-19 14:47:33 +01001099 /*
1100 * In case of non-hotplug cpu, if the ring-buffer is allocated
1101 * in early initcall, it will not be notified of secondary cpus.
1102 * In that off case, we need to allocate for all possible cpus.
1103 */
1104#ifdef CONFIG_HOTPLUG_CPU
Steven Rostedt554f7862009-03-11 22:00:13 -04001105 get_online_cpus();
1106 cpumask_copy(buffer->cpumask, cpu_online_mask);
Frederic Weisbecker3bf832c2009-03-19 14:47:33 +01001107#else
1108 cpumask_copy(buffer->cpumask, cpu_possible_mask);
1109#endif
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001110 buffer->cpus = nr_cpu_ids;
1111
1112 bsize = sizeof(void *) * nr_cpu_ids;
1113 buffer->buffers = kzalloc(ALIGN(bsize, cache_line_size()),
1114 GFP_KERNEL);
1115 if (!buffer->buffers)
Rusty Russell9e01c1b2009-01-01 10:12:22 +10301116 goto fail_free_cpumask;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001117
1118 for_each_buffer_cpu(buffer, cpu) {
1119 buffer->buffers[cpu] =
1120 rb_allocate_cpu_buffer(buffer, cpu);
1121 if (!buffer->buffers[cpu])
1122 goto fail_free_buffers;
1123 }
1124
Steven Rostedt59222ef2009-03-12 11:46:03 -04001125#ifdef CONFIG_HOTPLUG_CPU
Steven Rostedt554f7862009-03-11 22:00:13 -04001126 buffer->cpu_notify.notifier_call = rb_cpu_notify;
1127 buffer->cpu_notify.priority = 0;
1128 register_cpu_notifier(&buffer->cpu_notify);
1129#endif
1130
1131 put_online_cpus();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001132 mutex_init(&buffer->mutex);
1133
1134 return buffer;
1135
1136 fail_free_buffers:
1137 for_each_buffer_cpu(buffer, cpu) {
1138 if (buffer->buffers[cpu])
1139 rb_free_cpu_buffer(buffer->buffers[cpu]);
1140 }
1141 kfree(buffer->buffers);
1142
Rusty Russell9e01c1b2009-01-01 10:12:22 +10301143 fail_free_cpumask:
1144 free_cpumask_var(buffer->cpumask);
Steven Rostedt554f7862009-03-11 22:00:13 -04001145 put_online_cpus();
Rusty Russell9e01c1b2009-01-01 10:12:22 +10301146
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001147 fail_free_buffer:
1148 kfree(buffer);
1149 return NULL;
1150}
Peter Zijlstra1f8a6a12009-06-08 18:18:39 +02001151EXPORT_SYMBOL_GPL(__ring_buffer_alloc);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001152
1153/**
1154 * ring_buffer_free - free a ring buffer.
1155 * @buffer: the buffer to free.
1156 */
1157void
1158ring_buffer_free(struct ring_buffer *buffer)
1159{
1160 int cpu;
1161
Steven Rostedt554f7862009-03-11 22:00:13 -04001162 get_online_cpus();
1163
Steven Rostedt59222ef2009-03-12 11:46:03 -04001164#ifdef CONFIG_HOTPLUG_CPU
Steven Rostedt554f7862009-03-11 22:00:13 -04001165 unregister_cpu_notifier(&buffer->cpu_notify);
1166#endif
1167
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001168 for_each_buffer_cpu(buffer, cpu)
1169 rb_free_cpu_buffer(buffer->buffers[cpu]);
1170
Steven Rostedt554f7862009-03-11 22:00:13 -04001171 put_online_cpus();
1172
Eric Dumazetbd3f0222009-08-07 12:49:29 +02001173 kfree(buffer->buffers);
Rusty Russell9e01c1b2009-01-01 10:12:22 +10301174 free_cpumask_var(buffer->cpumask);
1175
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001176 kfree(buffer);
1177}
Robert Richterc4f50182008-12-11 16:49:22 +01001178EXPORT_SYMBOL_GPL(ring_buffer_free);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001179
Steven Rostedt37886f62009-03-17 17:22:06 -04001180void ring_buffer_set_clock(struct ring_buffer *buffer,
1181 u64 (*clock)(void))
1182{
1183 buffer->clock = clock;
1184}
1185
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001186static void rb_reset_cpu(struct ring_buffer_per_cpu *cpu_buffer);
1187
1188static void
1189rb_remove_pages(struct ring_buffer_per_cpu *cpu_buffer, unsigned nr_pages)
1190{
Steven Rostedt044fa782008-12-02 23:50:03 -05001191 struct buffer_page *bpage;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001192 struct list_head *p;
1193 unsigned i;
1194
1195 atomic_inc(&cpu_buffer->record_disabled);
1196 synchronize_sched();
1197
Steven Rostedt77ae3652009-03-27 11:00:29 -04001198 rb_head_page_deactivate(cpu_buffer);
1199
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001200 for (i = 0; i < nr_pages; i++) {
Steven Rostedt3adc54f2009-03-30 15:32:01 -04001201 if (RB_WARN_ON(cpu_buffer, list_empty(cpu_buffer->pages)))
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05001202 return;
Steven Rostedt3adc54f2009-03-30 15:32:01 -04001203 p = cpu_buffer->pages->next;
Steven Rostedt044fa782008-12-02 23:50:03 -05001204 bpage = list_entry(p, struct buffer_page, list);
1205 list_del_init(&bpage->list);
1206 free_buffer_page(bpage);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001207 }
Steven Rostedt3adc54f2009-03-30 15:32:01 -04001208 if (RB_WARN_ON(cpu_buffer, list_empty(cpu_buffer->pages)))
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05001209 return;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001210
1211 rb_reset_cpu(cpu_buffer);
1212
1213 rb_check_pages(cpu_buffer);
1214
1215 atomic_dec(&cpu_buffer->record_disabled);
1216
1217}
1218
1219static void
1220rb_insert_pages(struct ring_buffer_per_cpu *cpu_buffer,
1221 struct list_head *pages, unsigned nr_pages)
1222{
Steven Rostedt044fa782008-12-02 23:50:03 -05001223 struct buffer_page *bpage;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001224 struct list_head *p;
1225 unsigned i;
1226
1227 atomic_inc(&cpu_buffer->record_disabled);
1228 synchronize_sched();
1229
Steven Rostedt77ae3652009-03-27 11:00:29 -04001230 spin_lock_irq(&cpu_buffer->reader_lock);
1231 rb_head_page_deactivate(cpu_buffer);
1232
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001233 for (i = 0; i < nr_pages; i++) {
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05001234 if (RB_WARN_ON(cpu_buffer, list_empty(pages)))
1235 return;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001236 p = pages->next;
Steven Rostedt044fa782008-12-02 23:50:03 -05001237 bpage = list_entry(p, struct buffer_page, list);
1238 list_del_init(&bpage->list);
Steven Rostedt3adc54f2009-03-30 15:32:01 -04001239 list_add_tail(&bpage->list, cpu_buffer->pages);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001240 }
1241 rb_reset_cpu(cpu_buffer);
Steven Rostedt77ae3652009-03-27 11:00:29 -04001242 spin_unlock_irq(&cpu_buffer->reader_lock);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001243
1244 rb_check_pages(cpu_buffer);
1245
1246 atomic_dec(&cpu_buffer->record_disabled);
1247}
1248
1249/**
1250 * ring_buffer_resize - resize the ring buffer
1251 * @buffer: the buffer to resize.
1252 * @size: the new size.
1253 *
1254 * The tracer is responsible for making sure that the buffer is
1255 * not being used while changing the size.
1256 * Note: We may be able to change the above requirement by using
1257 * RCU synchronizations.
1258 *
1259 * Minimum size is 2 * BUF_PAGE_SIZE.
1260 *
1261 * Returns -1 on failure.
1262 */
1263int ring_buffer_resize(struct ring_buffer *buffer, unsigned long size)
1264{
1265 struct ring_buffer_per_cpu *cpu_buffer;
1266 unsigned nr_pages, rm_pages, new_pages;
Steven Rostedt044fa782008-12-02 23:50:03 -05001267 struct buffer_page *bpage, *tmp;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001268 unsigned long buffer_size;
1269 unsigned long addr;
1270 LIST_HEAD(pages);
1271 int i, cpu;
1272
Ingo Molnaree51a1d2008-11-13 14:58:31 +01001273 /*
1274 * Always succeed at resizing a non-existent buffer:
1275 */
1276 if (!buffer)
1277 return size;
1278
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001279 size = DIV_ROUND_UP(size, BUF_PAGE_SIZE);
1280 size *= BUF_PAGE_SIZE;
1281 buffer_size = buffer->pages * BUF_PAGE_SIZE;
1282
1283 /* we need a minimum of two pages */
1284 if (size < BUF_PAGE_SIZE * 2)
1285 size = BUF_PAGE_SIZE * 2;
1286
1287 if (size == buffer_size)
1288 return size;
1289
1290 mutex_lock(&buffer->mutex);
Steven Rostedt554f7862009-03-11 22:00:13 -04001291 get_online_cpus();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001292
1293 nr_pages = DIV_ROUND_UP(size, BUF_PAGE_SIZE);
1294
1295 if (size < buffer_size) {
1296
1297 /* easy case, just free pages */
Steven Rostedt554f7862009-03-11 22:00:13 -04001298 if (RB_WARN_ON(buffer, nr_pages >= buffer->pages))
1299 goto out_fail;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001300
1301 rm_pages = buffer->pages - nr_pages;
1302
1303 for_each_buffer_cpu(buffer, cpu) {
1304 cpu_buffer = buffer->buffers[cpu];
1305 rb_remove_pages(cpu_buffer, rm_pages);
1306 }
1307 goto out;
1308 }
1309
1310 /*
1311 * This is a bit more difficult. We only want to add pages
1312 * when we can allocate enough for all CPUs. We do this
1313 * by allocating all the pages and storing them on a local
1314 * link list. If we succeed in our allocation, then we
1315 * add these pages to the cpu_buffers. Otherwise we just free
1316 * them all and return -ENOMEM;
1317 */
Steven Rostedt554f7862009-03-11 22:00:13 -04001318 if (RB_WARN_ON(buffer, nr_pages <= buffer->pages))
1319 goto out_fail;
Steven Rostedtf536aaf2008-11-10 23:07:30 -05001320
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001321 new_pages = nr_pages - buffer->pages;
1322
1323 for_each_buffer_cpu(buffer, cpu) {
1324 for (i = 0; i < new_pages; i++) {
Steven Rostedt044fa782008-12-02 23:50:03 -05001325 bpage = kzalloc_node(ALIGN(sizeof(*bpage),
Steven Rostedte4c2ce82008-10-01 11:14:54 -04001326 cache_line_size()),
1327 GFP_KERNEL, cpu_to_node(cpu));
Steven Rostedt044fa782008-12-02 23:50:03 -05001328 if (!bpage)
Steven Rostedte4c2ce82008-10-01 11:14:54 -04001329 goto free_pages;
Steven Rostedt044fa782008-12-02 23:50:03 -05001330 list_add(&bpage->list, &pages);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001331 addr = __get_free_page(GFP_KERNEL);
1332 if (!addr)
1333 goto free_pages;
Steven Rostedt044fa782008-12-02 23:50:03 -05001334 bpage->page = (void *)addr;
1335 rb_init_page(bpage->page);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001336 }
1337 }
1338
1339 for_each_buffer_cpu(buffer, cpu) {
1340 cpu_buffer = buffer->buffers[cpu];
1341 rb_insert_pages(cpu_buffer, &pages, new_pages);
1342 }
1343
Steven Rostedt554f7862009-03-11 22:00:13 -04001344 if (RB_WARN_ON(buffer, !list_empty(&pages)))
1345 goto out_fail;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001346
1347 out:
1348 buffer->pages = nr_pages;
Steven Rostedt554f7862009-03-11 22:00:13 -04001349 put_online_cpus();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001350 mutex_unlock(&buffer->mutex);
1351
1352 return size;
1353
1354 free_pages:
Steven Rostedt044fa782008-12-02 23:50:03 -05001355 list_for_each_entry_safe(bpage, tmp, &pages, list) {
1356 list_del_init(&bpage->list);
1357 free_buffer_page(bpage);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001358 }
Steven Rostedt554f7862009-03-11 22:00:13 -04001359 put_online_cpus();
Vegard Nossum641d2f62008-11-18 19:22:13 +01001360 mutex_unlock(&buffer->mutex);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001361 return -ENOMEM;
Steven Rostedt554f7862009-03-11 22:00:13 -04001362
1363 /*
1364 * Something went totally wrong, and we are too paranoid
1365 * to even clean up the mess.
1366 */
1367 out_fail:
1368 put_online_cpus();
1369 mutex_unlock(&buffer->mutex);
1370 return -1;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001371}
Robert Richterc4f50182008-12-11 16:49:22 +01001372EXPORT_SYMBOL_GPL(ring_buffer_resize);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001373
Steven Rostedt8789a9e2008-12-02 15:34:07 -05001374static inline void *
Steven Rostedt044fa782008-12-02 23:50:03 -05001375__rb_data_page_index(struct buffer_data_page *bpage, unsigned index)
Steven Rostedt8789a9e2008-12-02 15:34:07 -05001376{
Steven Rostedt044fa782008-12-02 23:50:03 -05001377 return bpage->data + index;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05001378}
1379
Steven Rostedt044fa782008-12-02 23:50:03 -05001380static inline void *__rb_page_index(struct buffer_page *bpage, unsigned index)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001381{
Steven Rostedt044fa782008-12-02 23:50:03 -05001382 return bpage->page->data + index;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001383}
1384
1385static inline struct ring_buffer_event *
Steven Rostedtd7690412008-10-01 00:29:53 -04001386rb_reader_event(struct ring_buffer_per_cpu *cpu_buffer)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001387{
Steven Rostedt6f807ac2008-10-04 02:00:58 -04001388 return __rb_page_index(cpu_buffer->reader_page,
1389 cpu_buffer->reader_page->read);
1390}
1391
1392static inline struct ring_buffer_event *
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001393rb_iter_head_event(struct ring_buffer_iter *iter)
1394{
Steven Rostedt6f807ac2008-10-04 02:00:58 -04001395 return __rb_page_index(iter->head_page, iter->head);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001396}
1397
Steven Rostedt77ae3652009-03-27 11:00:29 -04001398static inline unsigned long rb_page_write(struct buffer_page *bpage)
Steven Rostedtbf41a152008-10-04 02:00:59 -04001399{
Steven Rostedt77ae3652009-03-27 11:00:29 -04001400 return local_read(&bpage->write) & RB_WRITE_MASK;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001401}
1402
1403static inline unsigned rb_page_commit(struct buffer_page *bpage)
1404{
Steven Rostedtabc9b562008-12-02 15:34:06 -05001405 return local_read(&bpage->page->commit);
Steven Rostedtbf41a152008-10-04 02:00:59 -04001406}
1407
Steven Rostedt77ae3652009-03-27 11:00:29 -04001408static inline unsigned long rb_page_entries(struct buffer_page *bpage)
1409{
1410 return local_read(&bpage->entries) & RB_WRITE_MASK;
1411}
1412
Steven Rostedtbf41a152008-10-04 02:00:59 -04001413/* Size is determined by what has been commited */
1414static inline unsigned rb_page_size(struct buffer_page *bpage)
1415{
1416 return rb_page_commit(bpage);
1417}
1418
1419static inline unsigned
1420rb_commit_index(struct ring_buffer_per_cpu *cpu_buffer)
1421{
1422 return rb_page_commit(cpu_buffer->commit_page);
1423}
1424
Steven Rostedtbf41a152008-10-04 02:00:59 -04001425static inline unsigned
1426rb_event_index(struct ring_buffer_event *event)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001427{
Steven Rostedtbf41a152008-10-04 02:00:59 -04001428 unsigned long addr = (unsigned long)event;
1429
Steven Rostedt22f470f2009-06-11 09:29:58 -04001430 return (addr & ~PAGE_MASK) - BUF_PAGE_HDR_SIZE;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001431}
1432
Steven Rostedt0f0c85f2009-05-11 16:08:00 -04001433static inline int
Steven Rostedtfa743952009-06-16 12:37:57 -04001434rb_event_is_commit(struct ring_buffer_per_cpu *cpu_buffer,
1435 struct ring_buffer_event *event)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001436{
Steven Rostedtbf41a152008-10-04 02:00:59 -04001437 unsigned long addr = (unsigned long)event;
1438 unsigned long index;
1439
1440 index = rb_event_index(event);
1441 addr &= PAGE_MASK;
1442
1443 return cpu_buffer->commit_page->page == (void *)addr &&
1444 rb_commit_index(cpu_buffer) == index;
1445}
1446
Andrew Morton34a148b2009-01-09 12:27:09 -08001447static void
Steven Rostedtbf41a152008-10-04 02:00:59 -04001448rb_set_commit_to_write(struct ring_buffer_per_cpu *cpu_buffer)
1449{
Steven Rostedt77ae3652009-03-27 11:00:29 -04001450 unsigned long max_count;
1451
Steven Rostedtbf41a152008-10-04 02:00:59 -04001452 /*
1453 * We only race with interrupts and NMIs on this CPU.
1454 * If we own the commit event, then we can commit
1455 * all others that interrupted us, since the interruptions
1456 * are in stack format (they finish before they come
1457 * back to us). This allows us to do a simple loop to
1458 * assign the commit to the tail.
1459 */
Steven Rostedta8ccf1d2008-12-23 11:32:24 -05001460 again:
Steven Rostedt77ae3652009-03-27 11:00:29 -04001461 max_count = cpu_buffer->buffer->pages * 100;
1462
Steven Rostedtbf41a152008-10-04 02:00:59 -04001463 while (cpu_buffer->commit_page != cpu_buffer->tail_page) {
Steven Rostedt77ae3652009-03-27 11:00:29 -04001464 if (RB_WARN_ON(cpu_buffer, !(--max_count)))
1465 return;
1466 if (RB_WARN_ON(cpu_buffer,
1467 rb_is_reader_page(cpu_buffer->tail_page)))
1468 return;
1469 local_set(&cpu_buffer->commit_page->page->commit,
1470 rb_page_write(cpu_buffer->commit_page));
Steven Rostedtbf41a152008-10-04 02:00:59 -04001471 rb_inc_page(cpu_buffer, &cpu_buffer->commit_page);
Steven Rostedtabc9b562008-12-02 15:34:06 -05001472 cpu_buffer->write_stamp =
1473 cpu_buffer->commit_page->page->time_stamp;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001474 /* add barrier to keep gcc from optimizing too much */
1475 barrier();
1476 }
1477 while (rb_commit_index(cpu_buffer) !=
1478 rb_page_write(cpu_buffer->commit_page)) {
Steven Rostedt77ae3652009-03-27 11:00:29 -04001479
1480 local_set(&cpu_buffer->commit_page->page->commit,
1481 rb_page_write(cpu_buffer->commit_page));
1482 RB_WARN_ON(cpu_buffer,
1483 local_read(&cpu_buffer->commit_page->page->commit) &
1484 ~RB_WRITE_MASK);
Steven Rostedtbf41a152008-10-04 02:00:59 -04001485 barrier();
1486 }
Steven Rostedta8ccf1d2008-12-23 11:32:24 -05001487
1488 /* again, keep gcc from optimizing */
1489 barrier();
1490
1491 /*
1492 * If an interrupt came in just after the first while loop
1493 * and pushed the tail page forward, we will be left with
1494 * a dangling commit that will never go forward.
1495 */
1496 if (unlikely(cpu_buffer->commit_page != cpu_buffer->tail_page))
1497 goto again;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001498}
1499
Steven Rostedtd7690412008-10-01 00:29:53 -04001500static void rb_reset_reader_page(struct ring_buffer_per_cpu *cpu_buffer)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001501{
Steven Rostedtabc9b562008-12-02 15:34:06 -05001502 cpu_buffer->read_stamp = cpu_buffer->reader_page->page->time_stamp;
Steven Rostedt6f807ac2008-10-04 02:00:58 -04001503 cpu_buffer->reader_page->read = 0;
Steven Rostedtd7690412008-10-01 00:29:53 -04001504}
1505
Andrew Morton34a148b2009-01-09 12:27:09 -08001506static void rb_inc_iter(struct ring_buffer_iter *iter)
Steven Rostedtd7690412008-10-01 00:29:53 -04001507{
1508 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
1509
1510 /*
1511 * The iterator could be on the reader page (it starts there).
1512 * But the head could have moved, since the reader was
1513 * found. Check for this case and assign the iterator
1514 * to the head page instead of next.
1515 */
1516 if (iter->head_page == cpu_buffer->reader_page)
Steven Rostedt77ae3652009-03-27 11:00:29 -04001517 iter->head_page = rb_set_head_page(cpu_buffer);
Steven Rostedtd7690412008-10-01 00:29:53 -04001518 else
1519 rb_inc_page(cpu_buffer, &iter->head_page);
1520
Steven Rostedtabc9b562008-12-02 15:34:06 -05001521 iter->read_stamp = iter->head_page->page->time_stamp;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001522 iter->head = 0;
1523}
1524
1525/**
1526 * ring_buffer_update_event - update event type and data
1527 * @event: the even to update
1528 * @type: the type of event
1529 * @length: the size of the event field in the ring buffer
1530 *
1531 * Update the type and data fields of the event. The length
1532 * is the actual size that is written to the ring buffer,
1533 * and with this, we can determine what to place into the
1534 * data field.
1535 */
Andrew Morton34a148b2009-01-09 12:27:09 -08001536static void
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001537rb_update_event(struct ring_buffer_event *event,
1538 unsigned type, unsigned length)
1539{
Lai Jiangshan334d4162009-04-24 11:27:05 +08001540 event->type_len = type;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001541
1542 switch (type) {
1543
1544 case RINGBUF_TYPE_PADDING:
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001545 case RINGBUF_TYPE_TIME_EXTEND:
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001546 case RINGBUF_TYPE_TIME_STAMP:
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001547 break;
1548
Lai Jiangshan334d4162009-04-24 11:27:05 +08001549 case 0:
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001550 length -= RB_EVNT_HDR_SIZE;
Lai Jiangshan334d4162009-04-24 11:27:05 +08001551 if (length > RB_MAX_SMALL_DATA)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001552 event->array[0] = length;
Lai Jiangshan334d4162009-04-24 11:27:05 +08001553 else
1554 event->type_len = DIV_ROUND_UP(length, RB_ALIGNMENT);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001555 break;
1556 default:
1557 BUG();
1558 }
1559}
1560
Steven Rostedt77ae3652009-03-27 11:00:29 -04001561/*
1562 * rb_handle_head_page - writer hit the head page
1563 *
1564 * Returns: +1 to retry page
1565 * 0 to continue
1566 * -1 on error
1567 */
1568static int
1569rb_handle_head_page(struct ring_buffer_per_cpu *cpu_buffer,
1570 struct buffer_page *tail_page,
1571 struct buffer_page *next_page)
1572{
1573 struct buffer_page *new_head;
1574 int entries;
1575 int type;
1576 int ret;
1577
1578 entries = rb_page_entries(next_page);
1579
1580 /*
1581 * The hard part is here. We need to move the head
1582 * forward, and protect against both readers on
1583 * other CPUs and writers coming in via interrupts.
1584 */
1585 type = rb_head_page_set_update(cpu_buffer, next_page, tail_page,
1586 RB_PAGE_HEAD);
1587
1588 /*
1589 * type can be one of four:
1590 * NORMAL - an interrupt already moved it for us
1591 * HEAD - we are the first to get here.
1592 * UPDATE - we are the interrupt interrupting
1593 * a current move.
1594 * MOVED - a reader on another CPU moved the next
1595 * pointer to its reader page. Give up
1596 * and try again.
1597 */
1598
1599 switch (type) {
1600 case RB_PAGE_HEAD:
1601 /*
1602 * We changed the head to UPDATE, thus
1603 * it is our responsibility to update
1604 * the counters.
1605 */
1606 local_add(entries, &cpu_buffer->overrun);
1607
1608 /*
1609 * The entries will be zeroed out when we move the
1610 * tail page.
1611 */
1612
1613 /* still more to do */
1614 break;
1615
1616 case RB_PAGE_UPDATE:
1617 /*
1618 * This is an interrupt that interrupt the
1619 * previous update. Still more to do.
1620 */
1621 break;
1622 case RB_PAGE_NORMAL:
1623 /*
1624 * An interrupt came in before the update
1625 * and processed this for us.
1626 * Nothing left to do.
1627 */
1628 return 1;
1629 case RB_PAGE_MOVED:
1630 /*
1631 * The reader is on another CPU and just did
1632 * a swap with our next_page.
1633 * Try again.
1634 */
1635 return 1;
1636 default:
1637 RB_WARN_ON(cpu_buffer, 1); /* WTF??? */
1638 return -1;
1639 }
1640
1641 /*
1642 * Now that we are here, the old head pointer is
1643 * set to UPDATE. This will keep the reader from
1644 * swapping the head page with the reader page.
1645 * The reader (on another CPU) will spin till
1646 * we are finished.
1647 *
1648 * We just need to protect against interrupts
1649 * doing the job. We will set the next pointer
1650 * to HEAD. After that, we set the old pointer
1651 * to NORMAL, but only if it was HEAD before.
1652 * otherwise we are an interrupt, and only
1653 * want the outer most commit to reset it.
1654 */
1655 new_head = next_page;
1656 rb_inc_page(cpu_buffer, &new_head);
1657
1658 ret = rb_head_page_set_head(cpu_buffer, new_head, next_page,
1659 RB_PAGE_NORMAL);
1660
1661 /*
1662 * Valid returns are:
1663 * HEAD - an interrupt came in and already set it.
1664 * NORMAL - One of two things:
1665 * 1) We really set it.
1666 * 2) A bunch of interrupts came in and moved
1667 * the page forward again.
1668 */
1669 switch (ret) {
1670 case RB_PAGE_HEAD:
1671 case RB_PAGE_NORMAL:
1672 /* OK */
1673 break;
1674 default:
1675 RB_WARN_ON(cpu_buffer, 1);
1676 return -1;
1677 }
1678
1679 /*
1680 * It is possible that an interrupt came in,
1681 * set the head up, then more interrupts came in
1682 * and moved it again. When we get back here,
1683 * the page would have been set to NORMAL but we
1684 * just set it back to HEAD.
1685 *
1686 * How do you detect this? Well, if that happened
1687 * the tail page would have moved.
1688 */
1689 if (ret == RB_PAGE_NORMAL) {
1690 /*
1691 * If the tail had moved passed next, then we need
1692 * to reset the pointer.
1693 */
1694 if (cpu_buffer->tail_page != tail_page &&
1695 cpu_buffer->tail_page != next_page)
1696 rb_head_page_set_normal(cpu_buffer, new_head,
1697 next_page,
1698 RB_PAGE_HEAD);
1699 }
1700
1701 /*
1702 * If this was the outer most commit (the one that
1703 * changed the original pointer from HEAD to UPDATE),
1704 * then it is up to us to reset it to NORMAL.
1705 */
1706 if (type == RB_PAGE_HEAD) {
1707 ret = rb_head_page_set_normal(cpu_buffer, next_page,
1708 tail_page,
1709 RB_PAGE_UPDATE);
1710 if (RB_WARN_ON(cpu_buffer,
1711 ret != RB_PAGE_UPDATE))
1712 return -1;
1713 }
1714
1715 return 0;
1716}
1717
Andrew Morton34a148b2009-01-09 12:27:09 -08001718static unsigned rb_calculate_event_length(unsigned length)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001719{
1720 struct ring_buffer_event event; /* Used only for sizeof array */
1721
1722 /* zero length can cause confusions */
1723 if (!length)
1724 length = 1;
1725
1726 if (length > RB_MAX_SMALL_DATA)
1727 length += sizeof(event.array[0]);
1728
1729 length += RB_EVNT_HDR_SIZE;
1730 length = ALIGN(length, RB_ALIGNMENT);
1731
1732 return length;
1733}
1734
Steven Rostedtc7b09302009-06-11 11:12:00 -04001735static inline void
1736rb_reset_tail(struct ring_buffer_per_cpu *cpu_buffer,
1737 struct buffer_page *tail_page,
1738 unsigned long tail, unsigned long length)
1739{
1740 struct ring_buffer_event *event;
1741
1742 /*
1743 * Only the event that crossed the page boundary
1744 * must fill the old tail_page with padding.
1745 */
1746 if (tail >= BUF_PAGE_SIZE) {
1747 local_sub(length, &tail_page->write);
1748 return;
1749 }
1750
1751 event = __rb_page_index(tail_page, tail);
Linus Torvaldsb0b70652009-06-20 10:56:46 -07001752 kmemcheck_annotate_bitfield(event, bitfield);
Steven Rostedtc7b09302009-06-11 11:12:00 -04001753
1754 /*
1755 * If this event is bigger than the minimum size, then
1756 * we need to be careful that we don't subtract the
1757 * write counter enough to allow another writer to slip
1758 * in on this page.
1759 * We put in a discarded commit instead, to make sure
1760 * that this space is not used again.
1761 *
1762 * If we are less than the minimum size, we don't need to
1763 * worry about it.
1764 */
1765 if (tail > (BUF_PAGE_SIZE - RB_EVNT_MIN_SIZE)) {
1766 /* No room for any events */
1767
1768 /* Mark the rest of the page with padding */
1769 rb_event_set_padding(event);
1770
1771 /* Set the write back to the previous setting */
1772 local_sub(length, &tail_page->write);
1773 return;
1774 }
1775
1776 /* Put in a discarded event */
1777 event->array[0] = (BUF_PAGE_SIZE - tail) - RB_EVNT_HDR_SIZE;
1778 event->type_len = RINGBUF_TYPE_PADDING;
1779 /* time delta must be non zero */
1780 event->time_delta = 1;
Steven Rostedtc7b09302009-06-11 11:12:00 -04001781
1782 /* Set write to end of buffer */
1783 length = (tail + length) - BUF_PAGE_SIZE;
1784 local_sub(length, &tail_page->write);
1785}
Steven Rostedt6634ff22009-05-06 15:30:07 -04001786
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001787static struct ring_buffer_event *
Steven Rostedt6634ff22009-05-06 15:30:07 -04001788rb_move_tail(struct ring_buffer_per_cpu *cpu_buffer,
1789 unsigned long length, unsigned long tail,
1790 struct buffer_page *commit_page,
1791 struct buffer_page *tail_page, u64 *ts)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001792{
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001793 struct ring_buffer *buffer = cpu_buffer->buffer;
Steven Rostedt77ae3652009-03-27 11:00:29 -04001794 struct buffer_page *next_page;
1795 int ret;
Steven Rostedtaa20ae82009-05-05 21:16:11 -04001796
1797 next_page = tail_page;
1798
Steven Rostedtaa20ae82009-05-05 21:16:11 -04001799 rb_inc_page(cpu_buffer, &next_page);
1800
Steven Rostedtaa20ae82009-05-05 21:16:11 -04001801 /*
1802 * If for some reason, we had an interrupt storm that made
1803 * it all the way around the buffer, bail, and warn
1804 * about it.
1805 */
1806 if (unlikely(next_page == commit_page)) {
Steven Rostedt77ae3652009-03-27 11:00:29 -04001807 local_inc(&cpu_buffer->commit_overrun);
Steven Rostedtaa20ae82009-05-05 21:16:11 -04001808 goto out_reset;
1809 }
1810
Steven Rostedt77ae3652009-03-27 11:00:29 -04001811 /*
1812 * This is where the fun begins!
1813 *
1814 * We are fighting against races between a reader that
1815 * could be on another CPU trying to swap its reader
1816 * page with the buffer head.
1817 *
1818 * We are also fighting against interrupts coming in and
1819 * moving the head or tail on us as well.
1820 *
1821 * If the next page is the head page then we have filled
1822 * the buffer, unless the commit page is still on the
1823 * reader page.
1824 */
1825 if (rb_is_head_page(cpu_buffer, next_page, &tail_page->list)) {
Steven Rostedtaa20ae82009-05-05 21:16:11 -04001826
Steven Rostedt77ae3652009-03-27 11:00:29 -04001827 /*
1828 * If the commit is not on the reader page, then
1829 * move the header page.
1830 */
1831 if (!rb_is_reader_page(cpu_buffer->commit_page)) {
1832 /*
1833 * If we are not in overwrite mode,
1834 * this is easy, just stop here.
1835 */
1836 if (!(buffer->flags & RB_FL_OVERWRITE))
1837 goto out_reset;
Steven Rostedtaa20ae82009-05-05 21:16:11 -04001838
Steven Rostedt77ae3652009-03-27 11:00:29 -04001839 ret = rb_handle_head_page(cpu_buffer,
1840 tail_page,
1841 next_page);
1842 if (ret < 0)
1843 goto out_reset;
1844 if (ret)
1845 goto out_again;
1846 } else {
1847 /*
1848 * We need to be careful here too. The
1849 * commit page could still be on the reader
1850 * page. We could have a small buffer, and
1851 * have filled up the buffer with events
1852 * from interrupts and such, and wrapped.
1853 *
1854 * Note, if the tail page is also the on the
1855 * reader_page, we let it move out.
1856 */
1857 if (unlikely((cpu_buffer->commit_page !=
1858 cpu_buffer->tail_page) &&
1859 (cpu_buffer->commit_page ==
1860 cpu_buffer->reader_page))) {
1861 local_inc(&cpu_buffer->commit_overrun);
1862 goto out_reset;
1863 }
Steven Rostedtaa20ae82009-05-05 21:16:11 -04001864 }
1865 }
1866
Steven Rostedt77ae3652009-03-27 11:00:29 -04001867 ret = rb_tail_page_update(cpu_buffer, tail_page, next_page);
1868 if (ret) {
1869 /*
1870 * Nested commits always have zero deltas, so
1871 * just reread the time stamp
1872 */
Steven Rostedt88eb0122009-05-11 16:28:23 -04001873 *ts = rb_time_stamp(buffer, cpu_buffer->cpu);
Steven Rostedt77ae3652009-03-27 11:00:29 -04001874 next_page->page->time_stamp = *ts;
Steven Rostedtaa20ae82009-05-05 21:16:11 -04001875 }
1876
Steven Rostedt77ae3652009-03-27 11:00:29 -04001877 out_again:
Steven Rostedtaa20ae82009-05-05 21:16:11 -04001878
Steven Rostedt77ae3652009-03-27 11:00:29 -04001879 rb_reset_tail(cpu_buffer, tail_page, tail, length);
Steven Rostedtaa20ae82009-05-05 21:16:11 -04001880
1881 /* fail and let the caller try again */
1882 return ERR_PTR(-EAGAIN);
1883
Steven Rostedt45141d42009-02-12 13:19:48 -05001884 out_reset:
Lai Jiangshan6f3b3442009-01-12 11:06:18 +08001885 /* reset write */
Steven Rostedtc7b09302009-06-11 11:12:00 -04001886 rb_reset_tail(cpu_buffer, tail_page, tail, length);
Lai Jiangshan6f3b3442009-01-12 11:06:18 +08001887
Steven Rostedtbf41a152008-10-04 02:00:59 -04001888 return NULL;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001889}
1890
Steven Rostedt6634ff22009-05-06 15:30:07 -04001891static struct ring_buffer_event *
1892__rb_reserve_next(struct ring_buffer_per_cpu *cpu_buffer,
1893 unsigned type, unsigned long length, u64 *ts)
1894{
1895 struct buffer_page *tail_page, *commit_page;
1896 struct ring_buffer_event *event;
1897 unsigned long tail, write;
1898
1899 commit_page = cpu_buffer->commit_page;
1900 /* we just need to protect against interrupts */
1901 barrier();
1902 tail_page = cpu_buffer->tail_page;
1903 write = local_add_return(length, &tail_page->write);
Steven Rostedt77ae3652009-03-27 11:00:29 -04001904
1905 /* set write to only the index of the write */
1906 write &= RB_WRITE_MASK;
Steven Rostedt6634ff22009-05-06 15:30:07 -04001907 tail = write - length;
1908
1909 /* See if we shot pass the end of this buffer page */
1910 if (write > BUF_PAGE_SIZE)
1911 return rb_move_tail(cpu_buffer, length, tail,
1912 commit_page, tail_page, ts);
1913
1914 /* We reserved something on the buffer */
1915
Steven Rostedt6634ff22009-05-06 15:30:07 -04001916 event = __rb_page_index(tail_page, tail);
Vegard Nossum1744a212009-02-28 08:29:44 +01001917 kmemcheck_annotate_bitfield(event, bitfield);
Steven Rostedt6634ff22009-05-06 15:30:07 -04001918 rb_update_event(event, type, length);
1919
1920 /* The passed in type is zero for DATA */
1921 if (likely(!type))
1922 local_inc(&tail_page->entries);
1923
1924 /*
Steven Rostedtfa743952009-06-16 12:37:57 -04001925 * If this is the first commit on the page, then update
1926 * its timestamp.
Steven Rostedt6634ff22009-05-06 15:30:07 -04001927 */
Steven Rostedtfa743952009-06-16 12:37:57 -04001928 if (!tail)
1929 tail_page->page->time_stamp = *ts;
Steven Rostedt6634ff22009-05-06 15:30:07 -04001930
1931 return event;
1932}
1933
Steven Rostedtedd813bf2009-06-02 23:00:53 -04001934static inline int
1935rb_try_to_discard(struct ring_buffer_per_cpu *cpu_buffer,
1936 struct ring_buffer_event *event)
1937{
1938 unsigned long new_index, old_index;
1939 struct buffer_page *bpage;
1940 unsigned long index;
1941 unsigned long addr;
1942
1943 new_index = rb_event_index(event);
1944 old_index = new_index + rb_event_length(event);
1945 addr = (unsigned long)event;
1946 addr &= PAGE_MASK;
1947
1948 bpage = cpu_buffer->tail_page;
1949
1950 if (bpage->page == (void *)addr && rb_page_write(bpage) == old_index) {
Steven Rostedt77ae3652009-03-27 11:00:29 -04001951 unsigned long write_mask =
1952 local_read(&bpage->write) & ~RB_WRITE_MASK;
Steven Rostedtedd813bf2009-06-02 23:00:53 -04001953 /*
1954 * This is on the tail page. It is possible that
1955 * a write could come in and move the tail page
1956 * and write to the next page. That is fine
1957 * because we just shorten what is on this page.
1958 */
Steven Rostedt77ae3652009-03-27 11:00:29 -04001959 old_index += write_mask;
1960 new_index += write_mask;
Steven Rostedtedd813bf2009-06-02 23:00:53 -04001961 index = local_cmpxchg(&bpage->write, old_index, new_index);
1962 if (index == old_index)
1963 return 1;
1964 }
1965
1966 /* could not discard */
1967 return 0;
1968}
1969
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001970static int
1971rb_add_time_stamp(struct ring_buffer_per_cpu *cpu_buffer,
1972 u64 *ts, u64 *delta)
1973{
1974 struct ring_buffer_event *event;
1975 static int once;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001976 int ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001977
1978 if (unlikely(*delta > (1ULL << 59) && !once++)) {
1979 printk(KERN_WARNING "Delta way too big! %llu"
1980 " ts=%llu write stamp = %llu\n",
Stephen Rothwelle2862c92008-10-27 17:43:28 +11001981 (unsigned long long)*delta,
1982 (unsigned long long)*ts,
1983 (unsigned long long)cpu_buffer->write_stamp);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001984 WARN_ON(1);
1985 }
1986
1987 /*
1988 * The delta is too big, we to add a
1989 * new timestamp.
1990 */
1991 event = __rb_reserve_next(cpu_buffer,
1992 RINGBUF_TYPE_TIME_EXTEND,
1993 RB_LEN_TIME_EXTEND,
1994 ts);
1995 if (!event)
Steven Rostedtbf41a152008-10-04 02:00:59 -04001996 return -EBUSY;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001997
Steven Rostedtbf41a152008-10-04 02:00:59 -04001998 if (PTR_ERR(event) == -EAGAIN)
1999 return -EAGAIN;
2000
2001 /* Only a commited time event can update the write stamp */
Steven Rostedtfa743952009-06-16 12:37:57 -04002002 if (rb_event_is_commit(cpu_buffer, event)) {
Steven Rostedtbf41a152008-10-04 02:00:59 -04002003 /*
Steven Rostedtfa743952009-06-16 12:37:57 -04002004 * If this is the first on the page, then it was
2005 * updated with the page itself. Try to discard it
2006 * and if we can't just make it zero.
Steven Rostedtbf41a152008-10-04 02:00:59 -04002007 */
2008 if (rb_event_index(event)) {
2009 event->time_delta = *delta & TS_MASK;
2010 event->array[0] = *delta >> TS_SHIFT;
2011 } else {
Steven Rostedtea05b572009-06-03 09:30:10 -04002012 /* try to discard, since we do not need this */
2013 if (!rb_try_to_discard(cpu_buffer, event)) {
2014 /* nope, just zero it */
2015 event->time_delta = 0;
2016 event->array[0] = 0;
2017 }
Steven Rostedtbf41a152008-10-04 02:00:59 -04002018 }
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002019 cpu_buffer->write_stamp = *ts;
Steven Rostedtbf41a152008-10-04 02:00:59 -04002020 /* let the caller know this was the commit */
2021 ret = 1;
2022 } else {
Steven Rostedtedd813bf2009-06-02 23:00:53 -04002023 /* Try to discard the event */
2024 if (!rb_try_to_discard(cpu_buffer, event)) {
2025 /* Darn, this is just wasted space */
2026 event->time_delta = 0;
2027 event->array[0] = 0;
Steven Rostedtedd813bf2009-06-02 23:00:53 -04002028 }
Steven Rostedtf57a8a12009-06-05 14:11:30 -04002029 ret = 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002030 }
2031
Steven Rostedtbf41a152008-10-04 02:00:59 -04002032 *delta = 0;
2033
2034 return ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002035}
2036
Steven Rostedtfa743952009-06-16 12:37:57 -04002037static void rb_start_commit(struct ring_buffer_per_cpu *cpu_buffer)
2038{
2039 local_inc(&cpu_buffer->committing);
2040 local_inc(&cpu_buffer->commits);
2041}
2042
2043static void rb_end_commit(struct ring_buffer_per_cpu *cpu_buffer)
2044{
2045 unsigned long commits;
2046
2047 if (RB_WARN_ON(cpu_buffer,
2048 !local_read(&cpu_buffer->committing)))
2049 return;
2050
2051 again:
2052 commits = local_read(&cpu_buffer->commits);
2053 /* synchronize with interrupts */
2054 barrier();
2055 if (local_read(&cpu_buffer->committing) == 1)
2056 rb_set_commit_to_write(cpu_buffer);
2057
2058 local_dec(&cpu_buffer->committing);
2059
2060 /* synchronize with interrupts */
2061 barrier();
2062
2063 /*
2064 * Need to account for interrupts coming in between the
2065 * updating of the commit page and the clearing of the
2066 * committing counter.
2067 */
2068 if (unlikely(local_read(&cpu_buffer->commits) != commits) &&
2069 !local_read(&cpu_buffer->committing)) {
2070 local_inc(&cpu_buffer->committing);
2071 goto again;
2072 }
2073}
2074
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002075static struct ring_buffer_event *
Steven Rostedt62f0b3e2009-09-04 14:11:34 -04002076rb_reserve_next_event(struct ring_buffer *buffer,
2077 struct ring_buffer_per_cpu *cpu_buffer,
Steven Rostedt1cd8d732009-05-11 14:08:09 -04002078 unsigned long length)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002079{
2080 struct ring_buffer_event *event;
Steven Rostedt168b6b12009-05-11 22:11:05 -04002081 u64 ts, delta = 0;
Steven Rostedtbf41a152008-10-04 02:00:59 -04002082 int commit = 0;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002083 int nr_loops = 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002084
Steven Rostedtfa743952009-06-16 12:37:57 -04002085 rb_start_commit(cpu_buffer);
2086
Steven Rostedt62f0b3e2009-09-04 14:11:34 -04002087 /*
2088 * Due to the ability to swap a cpu buffer from a buffer
2089 * it is possible it was swapped before we committed.
2090 * (committing stops a swap). We check for it here and
2091 * if it happened, we have to fail the write.
2092 */
2093 barrier();
2094 if (unlikely(ACCESS_ONCE(cpu_buffer->buffer) != buffer)) {
2095 local_dec(&cpu_buffer->committing);
2096 local_dec(&cpu_buffer->commits);
2097 return NULL;
2098 }
2099
Steven Rostedtbe957c42009-05-11 14:42:53 -04002100 length = rb_calculate_event_length(length);
Steven Rostedtbf41a152008-10-04 02:00:59 -04002101 again:
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002102 /*
2103 * We allow for interrupts to reenter here and do a trace.
2104 * If one does, it will cause this original code to loop
2105 * back here. Even with heavy interrupts happening, this
2106 * should only happen a few times in a row. If this happens
2107 * 1000 times in a row, there must be either an interrupt
2108 * storm or we have something buggy.
2109 * Bail!
2110 */
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05002111 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 1000))
Steven Rostedtfa743952009-06-16 12:37:57 -04002112 goto out_fail;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002113
Steven Rostedt88eb0122009-05-11 16:28:23 -04002114 ts = rb_time_stamp(cpu_buffer->buffer, cpu_buffer->cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002115
Steven Rostedtbf41a152008-10-04 02:00:59 -04002116 /*
2117 * Only the first commit can update the timestamp.
2118 * Yes there is a race here. If an interrupt comes in
2119 * just after the conditional and it traces too, then it
2120 * will also check the deltas. More than one timestamp may
2121 * also be made. But only the entry that did the actual
2122 * commit will be something other than zero.
2123 */
Steven Rostedt0f0c85f2009-05-11 16:08:00 -04002124 if (likely(cpu_buffer->tail_page == cpu_buffer->commit_page &&
2125 rb_page_write(cpu_buffer->tail_page) ==
2126 rb_commit_index(cpu_buffer))) {
Steven Rostedt168b6b12009-05-11 22:11:05 -04002127 u64 diff;
Steven Rostedtbf41a152008-10-04 02:00:59 -04002128
Steven Rostedt168b6b12009-05-11 22:11:05 -04002129 diff = ts - cpu_buffer->write_stamp;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002130
Steven Rostedt168b6b12009-05-11 22:11:05 -04002131 /* make sure this diff is calculated here */
Steven Rostedtbf41a152008-10-04 02:00:59 -04002132 barrier();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002133
Steven Rostedtbf41a152008-10-04 02:00:59 -04002134 /* Did the write stamp get updated already? */
2135 if (unlikely(ts < cpu_buffer->write_stamp))
Steven Rostedt168b6b12009-05-11 22:11:05 -04002136 goto get_event;
Steven Rostedtbf41a152008-10-04 02:00:59 -04002137
Steven Rostedt168b6b12009-05-11 22:11:05 -04002138 delta = diff;
2139 if (unlikely(test_time_stamp(delta))) {
Steven Rostedtbf41a152008-10-04 02:00:59 -04002140
2141 commit = rb_add_time_stamp(cpu_buffer, &ts, &delta);
Steven Rostedtbf41a152008-10-04 02:00:59 -04002142 if (commit == -EBUSY)
Steven Rostedtfa743952009-06-16 12:37:57 -04002143 goto out_fail;
Steven Rostedtbf41a152008-10-04 02:00:59 -04002144
2145 if (commit == -EAGAIN)
2146 goto again;
2147
2148 RB_WARN_ON(cpu_buffer, commit < 0);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002149 }
Steven Rostedt168b6b12009-05-11 22:11:05 -04002150 }
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002151
Steven Rostedt168b6b12009-05-11 22:11:05 -04002152 get_event:
Steven Rostedt1cd8d732009-05-11 14:08:09 -04002153 event = __rb_reserve_next(cpu_buffer, 0, length, &ts);
Steven Rostedt168b6b12009-05-11 22:11:05 -04002154 if (unlikely(PTR_ERR(event) == -EAGAIN))
Steven Rostedtbf41a152008-10-04 02:00:59 -04002155 goto again;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002156
Steven Rostedtfa743952009-06-16 12:37:57 -04002157 if (!event)
2158 goto out_fail;
Steven Rostedtbf41a152008-10-04 02:00:59 -04002159
Steven Rostedtfa743952009-06-16 12:37:57 -04002160 if (!rb_event_is_commit(cpu_buffer, event))
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002161 delta = 0;
2162
2163 event->time_delta = delta;
2164
2165 return event;
Steven Rostedtfa743952009-06-16 12:37:57 -04002166
2167 out_fail:
2168 rb_end_commit(cpu_buffer);
2169 return NULL;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002170}
2171
Paul Mundt1155de42009-06-25 14:30:12 +09002172#ifdef CONFIG_TRACING
2173
Steven Rostedtaa18efb2009-04-20 16:16:11 -04002174#define TRACE_RECURSIVE_DEPTH 16
Steven Rostedt261842b2009-04-16 21:41:52 -04002175
2176static int trace_recursive_lock(void)
2177{
Steven Rostedtaa18efb2009-04-20 16:16:11 -04002178 current->trace_recursion++;
Steven Rostedt261842b2009-04-16 21:41:52 -04002179
Steven Rostedtaa18efb2009-04-20 16:16:11 -04002180 if (likely(current->trace_recursion < TRACE_RECURSIVE_DEPTH))
2181 return 0;
Steven Rostedt261842b2009-04-16 21:41:52 -04002182
Steven Rostedtaa18efb2009-04-20 16:16:11 -04002183 /* Disable all tracing before we do anything else */
2184 tracing_off_permanent();
Frederic Weisbeckere057a5e2009-04-19 23:38:12 +02002185
Steven Rostedt7d7d2b82009-04-27 12:37:49 -04002186 printk_once(KERN_WARNING "Tracing recursion: depth[%ld]:"
Steven Rostedtaa18efb2009-04-20 16:16:11 -04002187 "HC[%lu]:SC[%lu]:NMI[%lu]\n",
2188 current->trace_recursion,
2189 hardirq_count() >> HARDIRQ_SHIFT,
2190 softirq_count() >> SOFTIRQ_SHIFT,
2191 in_nmi());
Frederic Weisbeckere057a5e2009-04-19 23:38:12 +02002192
Steven Rostedtaa18efb2009-04-20 16:16:11 -04002193 WARN_ON_ONCE(1);
2194 return -1;
Steven Rostedt261842b2009-04-16 21:41:52 -04002195}
2196
2197static void trace_recursive_unlock(void)
2198{
Steven Rostedtaa18efb2009-04-20 16:16:11 -04002199 WARN_ON_ONCE(!current->trace_recursion);
Steven Rostedt261842b2009-04-16 21:41:52 -04002200
Steven Rostedtaa18efb2009-04-20 16:16:11 -04002201 current->trace_recursion--;
Steven Rostedt261842b2009-04-16 21:41:52 -04002202}
2203
Paul Mundt1155de42009-06-25 14:30:12 +09002204#else
2205
2206#define trace_recursive_lock() (0)
2207#define trace_recursive_unlock() do { } while (0)
2208
2209#endif
2210
Steven Rostedtbf41a152008-10-04 02:00:59 -04002211static DEFINE_PER_CPU(int, rb_need_resched);
2212
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002213/**
2214 * ring_buffer_lock_reserve - reserve a part of the buffer
2215 * @buffer: the ring buffer to reserve from
2216 * @length: the length of the data to reserve (excluding event header)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002217 *
2218 * Returns a reseverd event on the ring buffer to copy directly to.
2219 * The user of this interface will need to get the body to write into
2220 * and can use the ring_buffer_event_data() interface.
2221 *
2222 * The length is the length of the data needed, not the event length
2223 * which also includes the event header.
2224 *
2225 * Must be paired with ring_buffer_unlock_commit, unless NULL is returned.
2226 * If NULL is returned, then nothing has been allocated or locked.
2227 */
2228struct ring_buffer_event *
Arnaldo Carvalho de Melo0a987752009-02-05 16:12:56 -02002229ring_buffer_lock_reserve(struct ring_buffer *buffer, unsigned long length)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002230{
2231 struct ring_buffer_per_cpu *cpu_buffer;
2232 struct ring_buffer_event *event;
Steven Rostedtbf41a152008-10-04 02:00:59 -04002233 int cpu, resched;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002234
Steven Rostedt033601a2008-11-21 12:41:55 -05002235 if (ring_buffer_flags != RB_BUFFERS_ON)
Steven Rostedta3583242008-11-11 15:01:42 -05002236 return NULL;
2237
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002238 if (atomic_read(&buffer->record_disabled))
2239 return NULL;
2240
Steven Rostedtbf41a152008-10-04 02:00:59 -04002241 /* If we are tracing schedule, we don't want to recurse */
Steven Rostedt182e9f52008-11-03 23:15:56 -05002242 resched = ftrace_preempt_disable();
Steven Rostedtbf41a152008-10-04 02:00:59 -04002243
Steven Rostedt261842b2009-04-16 21:41:52 -04002244 if (trace_recursive_lock())
2245 goto out_nocheck;
2246
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002247 cpu = raw_smp_processor_id();
2248
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302249 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedtd7690412008-10-01 00:29:53 -04002250 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002251
2252 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002253
2254 if (atomic_read(&cpu_buffer->record_disabled))
Steven Rostedtd7690412008-10-01 00:29:53 -04002255 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002256
Steven Rostedtbe957c42009-05-11 14:42:53 -04002257 if (length > BUF_MAX_DATA_SIZE)
Steven Rostedtbf41a152008-10-04 02:00:59 -04002258 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002259
Steven Rostedt62f0b3e2009-09-04 14:11:34 -04002260 event = rb_reserve_next_event(buffer, cpu_buffer, length);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002261 if (!event)
Steven Rostedtd7690412008-10-01 00:29:53 -04002262 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002263
Steven Rostedtbf41a152008-10-04 02:00:59 -04002264 /*
2265 * Need to store resched state on this cpu.
2266 * Only the first needs to.
2267 */
2268
2269 if (preempt_count() == 1)
2270 per_cpu(rb_need_resched, cpu) = resched;
2271
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002272 return event;
2273
Steven Rostedtd7690412008-10-01 00:29:53 -04002274 out:
Steven Rostedt261842b2009-04-16 21:41:52 -04002275 trace_recursive_unlock();
2276
2277 out_nocheck:
Steven Rostedt182e9f52008-11-03 23:15:56 -05002278 ftrace_preempt_enable(resched);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002279 return NULL;
2280}
Robert Richterc4f50182008-12-11 16:49:22 +01002281EXPORT_SYMBOL_GPL(ring_buffer_lock_reserve);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002282
Steven Rostedta1863c22009-09-03 10:23:58 -04002283static void
2284rb_update_write_stamp(struct ring_buffer_per_cpu *cpu_buffer,
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002285 struct ring_buffer_event *event)
2286{
Steven Rostedtfa743952009-06-16 12:37:57 -04002287 /*
2288 * The event first in the commit queue updates the
2289 * time stamp.
2290 */
2291 if (rb_event_is_commit(cpu_buffer, event))
2292 cpu_buffer->write_stamp += event->time_delta;
Steven Rostedta1863c22009-09-03 10:23:58 -04002293}
Steven Rostedtbf41a152008-10-04 02:00:59 -04002294
Steven Rostedta1863c22009-09-03 10:23:58 -04002295static void rb_commit(struct ring_buffer_per_cpu *cpu_buffer,
2296 struct ring_buffer_event *event)
2297{
2298 local_inc(&cpu_buffer->entries);
2299 rb_update_write_stamp(cpu_buffer, event);
Steven Rostedtfa743952009-06-16 12:37:57 -04002300 rb_end_commit(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002301}
2302
2303/**
2304 * ring_buffer_unlock_commit - commit a reserved
2305 * @buffer: The buffer to commit to
2306 * @event: The event pointer to commit.
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002307 *
2308 * This commits the data to the ring buffer, and releases any locks held.
2309 *
2310 * Must be paired with ring_buffer_lock_reserve.
2311 */
2312int ring_buffer_unlock_commit(struct ring_buffer *buffer,
Arnaldo Carvalho de Melo0a987752009-02-05 16:12:56 -02002313 struct ring_buffer_event *event)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002314{
2315 struct ring_buffer_per_cpu *cpu_buffer;
2316 int cpu = raw_smp_processor_id();
2317
2318 cpu_buffer = buffer->buffers[cpu];
2319
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002320 rb_commit(cpu_buffer, event);
2321
Steven Rostedt261842b2009-04-16 21:41:52 -04002322 trace_recursive_unlock();
2323
Steven Rostedtbf41a152008-10-04 02:00:59 -04002324 /*
2325 * Only the last preempt count needs to restore preemption.
2326 */
Steven Rostedt182e9f52008-11-03 23:15:56 -05002327 if (preempt_count() == 1)
2328 ftrace_preempt_enable(per_cpu(rb_need_resched, cpu));
2329 else
Steven Rostedtbf41a152008-10-04 02:00:59 -04002330 preempt_enable_no_resched_notrace();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002331
2332 return 0;
2333}
Robert Richterc4f50182008-12-11 16:49:22 +01002334EXPORT_SYMBOL_GPL(ring_buffer_unlock_commit);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002335
Frederic Weisbeckerf3b9aae2009-04-19 23:39:33 +02002336static inline void rb_event_discard(struct ring_buffer_event *event)
2337{
Lai Jiangshan334d4162009-04-24 11:27:05 +08002338 /* array[0] holds the actual length for the discarded event */
2339 event->array[0] = rb_event_data_length(event) - RB_EVNT_HDR_SIZE;
2340 event->type_len = RINGBUF_TYPE_PADDING;
Frederic Weisbeckerf3b9aae2009-04-19 23:39:33 +02002341 /* time delta must be non zero */
2342 if (!event->time_delta)
2343 event->time_delta = 1;
2344}
2345
Steven Rostedta1863c22009-09-03 10:23:58 -04002346/*
2347 * Decrement the entries to the page that an event is on.
2348 * The event does not even need to exist, only the pointer
2349 * to the page it is on. This may only be called before the commit
2350 * takes place.
2351 */
2352static inline void
2353rb_decrement_entry(struct ring_buffer_per_cpu *cpu_buffer,
2354 struct ring_buffer_event *event)
2355{
2356 unsigned long addr = (unsigned long)event;
2357 struct buffer_page *bpage = cpu_buffer->commit_page;
2358 struct buffer_page *start;
2359
2360 addr &= PAGE_MASK;
2361
2362 /* Do the likely case first */
2363 if (likely(bpage->page == (void *)addr)) {
2364 local_dec(&bpage->entries);
2365 return;
2366 }
2367
2368 /*
2369 * Because the commit page may be on the reader page we
2370 * start with the next page and check the end loop there.
2371 */
2372 rb_inc_page(cpu_buffer, &bpage);
2373 start = bpage;
2374 do {
2375 if (bpage->page == (void *)addr) {
2376 local_dec(&bpage->entries);
2377 return;
2378 }
2379 rb_inc_page(cpu_buffer, &bpage);
2380 } while (bpage != start);
2381
2382 /* commit not part of this buffer?? */
2383 RB_WARN_ON(cpu_buffer, 1);
2384}
2385
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002386/**
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002387 * ring_buffer_commit_discard - discard an event that has not been committed
2388 * @buffer: the ring buffer
2389 * @event: non committed event to discard
2390 *
Steven Rostedtdc892f72009-09-03 15:33:41 -04002391 * Sometimes an event that is in the ring buffer needs to be ignored.
2392 * This function lets the user discard an event in the ring buffer
2393 * and then that event will not be read later.
2394 *
2395 * This function only works if it is called before the the item has been
2396 * committed. It will try to free the event from the ring buffer
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002397 * if another event has not been added behind it.
2398 *
2399 * If another event has been added behind it, it will set the event
2400 * up as discarded, and perform the commit.
2401 *
2402 * If this function is called, do not call ring_buffer_unlock_commit on
2403 * the event.
2404 */
2405void ring_buffer_discard_commit(struct ring_buffer *buffer,
2406 struct ring_buffer_event *event)
2407{
2408 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002409 int cpu;
2410
2411 /* The event is discarded regardless */
Frederic Weisbeckerf3b9aae2009-04-19 23:39:33 +02002412 rb_event_discard(event);
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002413
Steven Rostedtfa743952009-06-16 12:37:57 -04002414 cpu = smp_processor_id();
2415 cpu_buffer = buffer->buffers[cpu];
2416
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002417 /*
2418 * This must only be called if the event has not been
2419 * committed yet. Thus we can assume that preemption
2420 * is still disabled.
2421 */
Steven Rostedtfa743952009-06-16 12:37:57 -04002422 RB_WARN_ON(buffer, !local_read(&cpu_buffer->committing));
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002423
Steven Rostedta1863c22009-09-03 10:23:58 -04002424 rb_decrement_entry(cpu_buffer, event);
Steven Rostedt0f2541d2009-08-05 12:02:48 -04002425 if (rb_try_to_discard(cpu_buffer, event))
Steven Rostedtedd813bf2009-06-02 23:00:53 -04002426 goto out;
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002427
2428 /*
2429 * The commit is still visible by the reader, so we
Steven Rostedta1863c22009-09-03 10:23:58 -04002430 * must still update the timestamp.
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002431 */
Steven Rostedta1863c22009-09-03 10:23:58 -04002432 rb_update_write_stamp(cpu_buffer, event);
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002433 out:
Steven Rostedtfa743952009-06-16 12:37:57 -04002434 rb_end_commit(cpu_buffer);
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002435
Frederic Weisbeckerf3b9aae2009-04-19 23:39:33 +02002436 trace_recursive_unlock();
2437
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002438 /*
2439 * Only the last preempt count needs to restore preemption.
2440 */
2441 if (preempt_count() == 1)
2442 ftrace_preempt_enable(per_cpu(rb_need_resched, cpu));
2443 else
2444 preempt_enable_no_resched_notrace();
2445
2446}
2447EXPORT_SYMBOL_GPL(ring_buffer_discard_commit);
2448
2449/**
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002450 * ring_buffer_write - write data to the buffer without reserving
2451 * @buffer: The ring buffer to write to.
2452 * @length: The length of the data being written (excluding the event header)
2453 * @data: The data to write to the buffer.
2454 *
2455 * This is like ring_buffer_lock_reserve and ring_buffer_unlock_commit as
2456 * one function. If you already have the data to write to the buffer, it
2457 * may be easier to simply call this function.
2458 *
2459 * Note, like ring_buffer_lock_reserve, the length is the length of the data
2460 * and not the length of the event which would hold the header.
2461 */
2462int ring_buffer_write(struct ring_buffer *buffer,
2463 unsigned long length,
2464 void *data)
2465{
2466 struct ring_buffer_per_cpu *cpu_buffer;
2467 struct ring_buffer_event *event;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002468 void *body;
2469 int ret = -EBUSY;
Steven Rostedtbf41a152008-10-04 02:00:59 -04002470 int cpu, resched;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002471
Steven Rostedt033601a2008-11-21 12:41:55 -05002472 if (ring_buffer_flags != RB_BUFFERS_ON)
Steven Rostedta3583242008-11-11 15:01:42 -05002473 return -EBUSY;
2474
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002475 if (atomic_read(&buffer->record_disabled))
2476 return -EBUSY;
2477
Steven Rostedt182e9f52008-11-03 23:15:56 -05002478 resched = ftrace_preempt_disable();
Steven Rostedtbf41a152008-10-04 02:00:59 -04002479
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002480 cpu = raw_smp_processor_id();
2481
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302482 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedtd7690412008-10-01 00:29:53 -04002483 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002484
2485 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002486
2487 if (atomic_read(&cpu_buffer->record_disabled))
2488 goto out;
2489
Steven Rostedtbe957c42009-05-11 14:42:53 -04002490 if (length > BUF_MAX_DATA_SIZE)
2491 goto out;
2492
Steven Rostedt62f0b3e2009-09-04 14:11:34 -04002493 event = rb_reserve_next_event(buffer, cpu_buffer, length);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002494 if (!event)
2495 goto out;
2496
2497 body = rb_event_data(event);
2498
2499 memcpy(body, data, length);
2500
2501 rb_commit(cpu_buffer, event);
2502
2503 ret = 0;
2504 out:
Steven Rostedt182e9f52008-11-03 23:15:56 -05002505 ftrace_preempt_enable(resched);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002506
2507 return ret;
2508}
Robert Richterc4f50182008-12-11 16:49:22 +01002509EXPORT_SYMBOL_GPL(ring_buffer_write);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002510
Andrew Morton34a148b2009-01-09 12:27:09 -08002511static int rb_per_cpu_empty(struct ring_buffer_per_cpu *cpu_buffer)
Steven Rostedtbf41a152008-10-04 02:00:59 -04002512{
2513 struct buffer_page *reader = cpu_buffer->reader_page;
Steven Rostedt77ae3652009-03-27 11:00:29 -04002514 struct buffer_page *head = rb_set_head_page(cpu_buffer);
Steven Rostedtbf41a152008-10-04 02:00:59 -04002515 struct buffer_page *commit = cpu_buffer->commit_page;
2516
Steven Rostedt77ae3652009-03-27 11:00:29 -04002517 /* In case of error, head will be NULL */
2518 if (unlikely(!head))
2519 return 1;
2520
Steven Rostedtbf41a152008-10-04 02:00:59 -04002521 return reader->read == rb_page_commit(reader) &&
2522 (commit == reader ||
2523 (commit == head &&
2524 head->read == rb_page_commit(commit)));
2525}
2526
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002527/**
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002528 * ring_buffer_record_disable - stop all writes into the buffer
2529 * @buffer: The ring buffer to stop writes to.
2530 *
2531 * This prevents all writes to the buffer. Any attempt to write
2532 * to the buffer after this will fail and return NULL.
2533 *
2534 * The caller should call synchronize_sched() after this.
2535 */
2536void ring_buffer_record_disable(struct ring_buffer *buffer)
2537{
2538 atomic_inc(&buffer->record_disabled);
2539}
Robert Richterc4f50182008-12-11 16:49:22 +01002540EXPORT_SYMBOL_GPL(ring_buffer_record_disable);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002541
2542/**
2543 * ring_buffer_record_enable - enable writes to the buffer
2544 * @buffer: The ring buffer to enable writes
2545 *
2546 * Note, multiple disables will need the same number of enables
2547 * to truely enable the writing (much like preempt_disable).
2548 */
2549void ring_buffer_record_enable(struct ring_buffer *buffer)
2550{
2551 atomic_dec(&buffer->record_disabled);
2552}
Robert Richterc4f50182008-12-11 16:49:22 +01002553EXPORT_SYMBOL_GPL(ring_buffer_record_enable);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002554
2555/**
2556 * ring_buffer_record_disable_cpu - stop all writes into the cpu_buffer
2557 * @buffer: The ring buffer to stop writes to.
2558 * @cpu: The CPU buffer to stop
2559 *
2560 * This prevents all writes to the buffer. Any attempt to write
2561 * to the buffer after this will fail and return NULL.
2562 *
2563 * The caller should call synchronize_sched() after this.
2564 */
2565void ring_buffer_record_disable_cpu(struct ring_buffer *buffer, int cpu)
2566{
2567 struct ring_buffer_per_cpu *cpu_buffer;
2568
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302569 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04002570 return;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002571
2572 cpu_buffer = buffer->buffers[cpu];
2573 atomic_inc(&cpu_buffer->record_disabled);
2574}
Robert Richterc4f50182008-12-11 16:49:22 +01002575EXPORT_SYMBOL_GPL(ring_buffer_record_disable_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002576
2577/**
2578 * ring_buffer_record_enable_cpu - enable writes to the buffer
2579 * @buffer: The ring buffer to enable writes
2580 * @cpu: The CPU to enable.
2581 *
2582 * Note, multiple disables will need the same number of enables
2583 * to truely enable the writing (much like preempt_disable).
2584 */
2585void ring_buffer_record_enable_cpu(struct ring_buffer *buffer, int cpu)
2586{
2587 struct ring_buffer_per_cpu *cpu_buffer;
2588
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302589 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04002590 return;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002591
2592 cpu_buffer = buffer->buffers[cpu];
2593 atomic_dec(&cpu_buffer->record_disabled);
2594}
Robert Richterc4f50182008-12-11 16:49:22 +01002595EXPORT_SYMBOL_GPL(ring_buffer_record_enable_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002596
2597/**
2598 * ring_buffer_entries_cpu - get the number of entries in a cpu buffer
2599 * @buffer: The ring buffer
2600 * @cpu: The per CPU buffer to get the entries from.
2601 */
2602unsigned long ring_buffer_entries_cpu(struct ring_buffer *buffer, int cpu)
2603{
2604 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedt8aabee52009-03-12 13:13:49 -04002605 unsigned long ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002606
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302607 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04002608 return 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002609
2610 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt77ae3652009-03-27 11:00:29 -04002611 ret = (local_read(&cpu_buffer->entries) - local_read(&cpu_buffer->overrun))
Steven Rostedte4906ef2009-04-30 20:49:44 -04002612 - cpu_buffer->read;
Steven Rostedt554f7862009-03-11 22:00:13 -04002613
2614 return ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002615}
Robert Richterc4f50182008-12-11 16:49:22 +01002616EXPORT_SYMBOL_GPL(ring_buffer_entries_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002617
2618/**
2619 * ring_buffer_overrun_cpu - get the number of overruns in a cpu_buffer
2620 * @buffer: The ring buffer
2621 * @cpu: The per CPU buffer to get the number of overruns from
2622 */
2623unsigned long ring_buffer_overrun_cpu(struct ring_buffer *buffer, int cpu)
2624{
2625 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedt8aabee52009-03-12 13:13:49 -04002626 unsigned long ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002627
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302628 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04002629 return 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002630
2631 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt77ae3652009-03-27 11:00:29 -04002632 ret = local_read(&cpu_buffer->overrun);
Steven Rostedt554f7862009-03-11 22:00:13 -04002633
2634 return ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002635}
Robert Richterc4f50182008-12-11 16:49:22 +01002636EXPORT_SYMBOL_GPL(ring_buffer_overrun_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002637
2638/**
Steven Rostedtf0d2c682009-04-29 13:43:37 -04002639 * ring_buffer_commit_overrun_cpu - get the number of overruns caused by commits
2640 * @buffer: The ring buffer
2641 * @cpu: The per CPU buffer to get the number of overruns from
2642 */
2643unsigned long
2644ring_buffer_commit_overrun_cpu(struct ring_buffer *buffer, int cpu)
2645{
2646 struct ring_buffer_per_cpu *cpu_buffer;
2647 unsigned long ret;
2648
2649 if (!cpumask_test_cpu(cpu, buffer->cpumask))
2650 return 0;
2651
2652 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt77ae3652009-03-27 11:00:29 -04002653 ret = local_read(&cpu_buffer->commit_overrun);
Steven Rostedtf0d2c682009-04-29 13:43:37 -04002654
2655 return ret;
2656}
2657EXPORT_SYMBOL_GPL(ring_buffer_commit_overrun_cpu);
2658
2659/**
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002660 * ring_buffer_entries - get the number of entries in a buffer
2661 * @buffer: The ring buffer
2662 *
2663 * Returns the total number of entries in the ring buffer
2664 * (all CPU entries)
2665 */
2666unsigned long ring_buffer_entries(struct ring_buffer *buffer)
2667{
2668 struct ring_buffer_per_cpu *cpu_buffer;
2669 unsigned long entries = 0;
2670 int cpu;
2671
2672 /* if you care about this being correct, lock the buffer */
2673 for_each_buffer_cpu(buffer, cpu) {
2674 cpu_buffer = buffer->buffers[cpu];
Steven Rostedte4906ef2009-04-30 20:49:44 -04002675 entries += (local_read(&cpu_buffer->entries) -
Steven Rostedt77ae3652009-03-27 11:00:29 -04002676 local_read(&cpu_buffer->overrun)) - cpu_buffer->read;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002677 }
2678
2679 return entries;
2680}
Robert Richterc4f50182008-12-11 16:49:22 +01002681EXPORT_SYMBOL_GPL(ring_buffer_entries);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002682
2683/**
2684 * ring_buffer_overrun_cpu - get the number of overruns in buffer
2685 * @buffer: The ring buffer
2686 *
2687 * Returns the total number of overruns in the ring buffer
2688 * (all CPU entries)
2689 */
2690unsigned long ring_buffer_overruns(struct ring_buffer *buffer)
2691{
2692 struct ring_buffer_per_cpu *cpu_buffer;
2693 unsigned long overruns = 0;
2694 int cpu;
2695
2696 /* if you care about this being correct, lock the buffer */
2697 for_each_buffer_cpu(buffer, cpu) {
2698 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt77ae3652009-03-27 11:00:29 -04002699 overruns += local_read(&cpu_buffer->overrun);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002700 }
2701
2702 return overruns;
2703}
Robert Richterc4f50182008-12-11 16:49:22 +01002704EXPORT_SYMBOL_GPL(ring_buffer_overruns);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002705
Steven Rostedt642edba2008-11-12 00:01:26 -05002706static void rb_iter_reset(struct ring_buffer_iter *iter)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002707{
2708 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
2709
Steven Rostedtd7690412008-10-01 00:29:53 -04002710 /* Iterator usage is expected to have record disabled */
2711 if (list_empty(&cpu_buffer->reader_page->list)) {
Steven Rostedt77ae3652009-03-27 11:00:29 -04002712 iter->head_page = rb_set_head_page(cpu_buffer);
2713 if (unlikely(!iter->head_page))
2714 return;
2715 iter->head = iter->head_page->read;
Steven Rostedtd7690412008-10-01 00:29:53 -04002716 } else {
2717 iter->head_page = cpu_buffer->reader_page;
Steven Rostedt6f807ac2008-10-04 02:00:58 -04002718 iter->head = cpu_buffer->reader_page->read;
Steven Rostedtd7690412008-10-01 00:29:53 -04002719 }
2720 if (iter->head)
2721 iter->read_stamp = cpu_buffer->read_stamp;
2722 else
Steven Rostedtabc9b562008-12-02 15:34:06 -05002723 iter->read_stamp = iter->head_page->page->time_stamp;
Steven Rostedt642edba2008-11-12 00:01:26 -05002724}
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002725
Steven Rostedt642edba2008-11-12 00:01:26 -05002726/**
2727 * ring_buffer_iter_reset - reset an iterator
2728 * @iter: The iterator to reset
2729 *
2730 * Resets the iterator, so that it will start from the beginning
2731 * again.
2732 */
2733void ring_buffer_iter_reset(struct ring_buffer_iter *iter)
2734{
Steven Rostedt554f7862009-03-11 22:00:13 -04002735 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedt642edba2008-11-12 00:01:26 -05002736 unsigned long flags;
2737
Steven Rostedt554f7862009-03-11 22:00:13 -04002738 if (!iter)
2739 return;
2740
2741 cpu_buffer = iter->cpu_buffer;
2742
Steven Rostedt642edba2008-11-12 00:01:26 -05002743 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
2744 rb_iter_reset(iter);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002745 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002746}
Robert Richterc4f50182008-12-11 16:49:22 +01002747EXPORT_SYMBOL_GPL(ring_buffer_iter_reset);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002748
2749/**
2750 * ring_buffer_iter_empty - check if an iterator has no more to read
2751 * @iter: The iterator to check
2752 */
2753int ring_buffer_iter_empty(struct ring_buffer_iter *iter)
2754{
2755 struct ring_buffer_per_cpu *cpu_buffer;
2756
2757 cpu_buffer = iter->cpu_buffer;
2758
Steven Rostedtbf41a152008-10-04 02:00:59 -04002759 return iter->head_page == cpu_buffer->commit_page &&
2760 iter->head == rb_commit_index(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002761}
Robert Richterc4f50182008-12-11 16:49:22 +01002762EXPORT_SYMBOL_GPL(ring_buffer_iter_empty);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002763
2764static void
2765rb_update_read_stamp(struct ring_buffer_per_cpu *cpu_buffer,
2766 struct ring_buffer_event *event)
2767{
2768 u64 delta;
2769
Lai Jiangshan334d4162009-04-24 11:27:05 +08002770 switch (event->type_len) {
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002771 case RINGBUF_TYPE_PADDING:
2772 return;
2773
2774 case RINGBUF_TYPE_TIME_EXTEND:
2775 delta = event->array[0];
2776 delta <<= TS_SHIFT;
2777 delta += event->time_delta;
2778 cpu_buffer->read_stamp += delta;
2779 return;
2780
2781 case RINGBUF_TYPE_TIME_STAMP:
2782 /* FIXME: not implemented */
2783 return;
2784
2785 case RINGBUF_TYPE_DATA:
2786 cpu_buffer->read_stamp += event->time_delta;
2787 return;
2788
2789 default:
2790 BUG();
2791 }
2792 return;
2793}
2794
2795static void
2796rb_update_iter_read_stamp(struct ring_buffer_iter *iter,
2797 struct ring_buffer_event *event)
2798{
2799 u64 delta;
2800
Lai Jiangshan334d4162009-04-24 11:27:05 +08002801 switch (event->type_len) {
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002802 case RINGBUF_TYPE_PADDING:
2803 return;
2804
2805 case RINGBUF_TYPE_TIME_EXTEND:
2806 delta = event->array[0];
2807 delta <<= TS_SHIFT;
2808 delta += event->time_delta;
2809 iter->read_stamp += delta;
2810 return;
2811
2812 case RINGBUF_TYPE_TIME_STAMP:
2813 /* FIXME: not implemented */
2814 return;
2815
2816 case RINGBUF_TYPE_DATA:
2817 iter->read_stamp += event->time_delta;
2818 return;
2819
2820 default:
2821 BUG();
2822 }
2823 return;
2824}
2825
Steven Rostedtd7690412008-10-01 00:29:53 -04002826static struct buffer_page *
2827rb_get_reader_page(struct ring_buffer_per_cpu *cpu_buffer)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002828{
Steven Rostedtd7690412008-10-01 00:29:53 -04002829 struct buffer_page *reader = NULL;
2830 unsigned long flags;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002831 int nr_loops = 0;
Steven Rostedt77ae3652009-03-27 11:00:29 -04002832 int ret;
Steven Rostedtd7690412008-10-01 00:29:53 -04002833
Steven Rostedt3e03fb72008-11-06 00:09:43 -05002834 local_irq_save(flags);
2835 __raw_spin_lock(&cpu_buffer->lock);
Steven Rostedtd7690412008-10-01 00:29:53 -04002836
2837 again:
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002838 /*
2839 * This should normally only loop twice. But because the
2840 * start of the reader inserts an empty page, it causes
2841 * a case where we will loop three times. There should be no
2842 * reason to loop four times (that I know of).
2843 */
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05002844 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 3)) {
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002845 reader = NULL;
2846 goto out;
2847 }
2848
Steven Rostedtd7690412008-10-01 00:29:53 -04002849 reader = cpu_buffer->reader_page;
2850
2851 /* If there's more to read, return this page */
Steven Rostedtbf41a152008-10-04 02:00:59 -04002852 if (cpu_buffer->reader_page->read < rb_page_size(reader))
Steven Rostedtd7690412008-10-01 00:29:53 -04002853 goto out;
2854
2855 /* Never should we have an index greater than the size */
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05002856 if (RB_WARN_ON(cpu_buffer,
2857 cpu_buffer->reader_page->read > rb_page_size(reader)))
2858 goto out;
Steven Rostedtd7690412008-10-01 00:29:53 -04002859
2860 /* check if we caught up to the tail */
2861 reader = NULL;
Steven Rostedtbf41a152008-10-04 02:00:59 -04002862 if (cpu_buffer->commit_page == cpu_buffer->reader_page)
Steven Rostedtd7690412008-10-01 00:29:53 -04002863 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002864
2865 /*
Steven Rostedtd7690412008-10-01 00:29:53 -04002866 * Reset the reader page to size zero.
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002867 */
Steven Rostedt77ae3652009-03-27 11:00:29 -04002868 local_set(&cpu_buffer->reader_page->write, 0);
2869 local_set(&cpu_buffer->reader_page->entries, 0);
2870 local_set(&cpu_buffer->reader_page->page->commit, 0);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002871
Steven Rostedt77ae3652009-03-27 11:00:29 -04002872 spin:
2873 /*
2874 * Splice the empty reader page into the list around the head.
2875 */
2876 reader = rb_set_head_page(cpu_buffer);
Steven Rostedtd7690412008-10-01 00:29:53 -04002877 cpu_buffer->reader_page->list.next = reader->list.next;
2878 cpu_buffer->reader_page->list.prev = reader->list.prev;
Steven Rostedtbf41a152008-10-04 02:00:59 -04002879
Steven Rostedt3adc54f2009-03-30 15:32:01 -04002880 /*
2881 * cpu_buffer->pages just needs to point to the buffer, it
2882 * has no specific buffer page to point to. Lets move it out
2883 * of our way so we don't accidently swap it.
2884 */
2885 cpu_buffer->pages = reader->list.prev;
2886
Steven Rostedt77ae3652009-03-27 11:00:29 -04002887 /* The reader page will be pointing to the new head */
2888 rb_set_list_to_head(cpu_buffer, &cpu_buffer->reader_page->list);
Steven Rostedtd7690412008-10-01 00:29:53 -04002889
2890 /*
Steven Rostedt77ae3652009-03-27 11:00:29 -04002891 * Here's the tricky part.
2892 *
2893 * We need to move the pointer past the header page.
2894 * But we can only do that if a writer is not currently
2895 * moving it. The page before the header page has the
2896 * flag bit '1' set if it is pointing to the page we want.
2897 * but if the writer is in the process of moving it
2898 * than it will be '2' or already moved '0'.
Steven Rostedtd7690412008-10-01 00:29:53 -04002899 */
Steven Rostedtd7690412008-10-01 00:29:53 -04002900
Steven Rostedt77ae3652009-03-27 11:00:29 -04002901 ret = rb_head_page_replace(reader, cpu_buffer->reader_page);
2902
2903 /*
2904 * If we did not convert it, then we must try again.
2905 */
2906 if (!ret)
2907 goto spin;
2908
2909 /*
2910 * Yeah! We succeeded in replacing the page.
2911 *
2912 * Now make the new head point back to the reader page.
2913 */
2914 reader->list.next->prev = &cpu_buffer->reader_page->list;
2915 rb_inc_page(cpu_buffer, &cpu_buffer->head_page);
Steven Rostedtd7690412008-10-01 00:29:53 -04002916
2917 /* Finally update the reader page to the new head */
2918 cpu_buffer->reader_page = reader;
2919 rb_reset_reader_page(cpu_buffer);
2920
2921 goto again;
2922
2923 out:
Steven Rostedt3e03fb72008-11-06 00:09:43 -05002924 __raw_spin_unlock(&cpu_buffer->lock);
2925 local_irq_restore(flags);
Steven Rostedtd7690412008-10-01 00:29:53 -04002926
2927 return reader;
2928}
2929
2930static void rb_advance_reader(struct ring_buffer_per_cpu *cpu_buffer)
2931{
2932 struct ring_buffer_event *event;
2933 struct buffer_page *reader;
2934 unsigned length;
2935
2936 reader = rb_get_reader_page(cpu_buffer);
2937
2938 /* This function should not be called when buffer is empty */
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05002939 if (RB_WARN_ON(cpu_buffer, !reader))
2940 return;
Steven Rostedtd7690412008-10-01 00:29:53 -04002941
2942 event = rb_reader_event(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002943
Steven Rostedta1863c22009-09-03 10:23:58 -04002944 if (event->type_len <= RINGBUF_TYPE_DATA_TYPE_LEN_MAX)
Steven Rostedte4906ef2009-04-30 20:49:44 -04002945 cpu_buffer->read++;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002946
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002947 rb_update_read_stamp(cpu_buffer, event);
2948
Steven Rostedtd7690412008-10-01 00:29:53 -04002949 length = rb_event_length(event);
Steven Rostedt6f807ac2008-10-04 02:00:58 -04002950 cpu_buffer->reader_page->read += length;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002951}
2952
2953static void rb_advance_iter(struct ring_buffer_iter *iter)
2954{
2955 struct ring_buffer *buffer;
2956 struct ring_buffer_per_cpu *cpu_buffer;
2957 struct ring_buffer_event *event;
2958 unsigned length;
2959
2960 cpu_buffer = iter->cpu_buffer;
2961 buffer = cpu_buffer->buffer;
2962
2963 /*
2964 * Check if we are at the end of the buffer.
2965 */
Steven Rostedtbf41a152008-10-04 02:00:59 -04002966 if (iter->head >= rb_page_size(iter->head_page)) {
Steven Rostedtea05b572009-06-03 09:30:10 -04002967 /* discarded commits can make the page empty */
2968 if (iter->head_page == cpu_buffer->commit_page)
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05002969 return;
Steven Rostedtd7690412008-10-01 00:29:53 -04002970 rb_inc_iter(iter);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002971 return;
2972 }
2973
2974 event = rb_iter_head_event(iter);
2975
2976 length = rb_event_length(event);
2977
2978 /*
2979 * This should not be called to advance the header if we are
2980 * at the tail of the buffer.
2981 */
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05002982 if (RB_WARN_ON(cpu_buffer,
Steven Rostedtf536aaf2008-11-10 23:07:30 -05002983 (iter->head_page == cpu_buffer->commit_page) &&
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05002984 (iter->head + length > rb_commit_index(cpu_buffer))))
2985 return;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002986
2987 rb_update_iter_read_stamp(iter, event);
2988
2989 iter->head += length;
2990
2991 /* check for end of page padding */
Steven Rostedtbf41a152008-10-04 02:00:59 -04002992 if ((iter->head >= rb_page_size(iter->head_page)) &&
2993 (iter->head_page != cpu_buffer->commit_page))
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002994 rb_advance_iter(iter);
2995}
2996
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002997static struct ring_buffer_event *
2998rb_buffer_peek(struct ring_buffer *buffer, int cpu, u64 *ts)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002999{
3000 struct ring_buffer_per_cpu *cpu_buffer;
3001 struct ring_buffer_event *event;
Steven Rostedtd7690412008-10-01 00:29:53 -04003002 struct buffer_page *reader;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003003 int nr_loops = 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003004
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003005 cpu_buffer = buffer->buffers[cpu];
3006
3007 again:
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003008 /*
3009 * We repeat when a timestamp is encountered. It is possible
3010 * to get multiple timestamps from an interrupt entering just
Steven Rostedtea05b572009-06-03 09:30:10 -04003011 * as one timestamp is about to be written, or from discarded
3012 * commits. The most that we can have is the number on a single page.
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003013 */
Steven Rostedtea05b572009-06-03 09:30:10 -04003014 if (RB_WARN_ON(cpu_buffer, ++nr_loops > RB_TIMESTAMPS_PER_PAGE))
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003015 return NULL;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003016
Steven Rostedtd7690412008-10-01 00:29:53 -04003017 reader = rb_get_reader_page(cpu_buffer);
3018 if (!reader)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003019 return NULL;
3020
Steven Rostedtd7690412008-10-01 00:29:53 -04003021 event = rb_reader_event(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003022
Lai Jiangshan334d4162009-04-24 11:27:05 +08003023 switch (event->type_len) {
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003024 case RINGBUF_TYPE_PADDING:
Tom Zanussi2d622712009-03-22 03:30:49 -05003025 if (rb_null_event(event))
3026 RB_WARN_ON(cpu_buffer, 1);
3027 /*
3028 * Because the writer could be discarding every
3029 * event it creates (which would probably be bad)
3030 * if we were to go back to "again" then we may never
3031 * catch up, and will trigger the warn on, or lock
3032 * the box. Return the padding, and we will release
3033 * the current locks, and try again.
3034 */
Tom Zanussi2d622712009-03-22 03:30:49 -05003035 return event;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003036
3037 case RINGBUF_TYPE_TIME_EXTEND:
3038 /* Internal data, OK to advance */
Steven Rostedtd7690412008-10-01 00:29:53 -04003039 rb_advance_reader(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003040 goto again;
3041
3042 case RINGBUF_TYPE_TIME_STAMP:
3043 /* FIXME: not implemented */
Steven Rostedtd7690412008-10-01 00:29:53 -04003044 rb_advance_reader(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003045 goto again;
3046
3047 case RINGBUF_TYPE_DATA:
3048 if (ts) {
3049 *ts = cpu_buffer->read_stamp + event->time_delta;
Steven Rostedt37886f62009-03-17 17:22:06 -04003050 ring_buffer_normalize_time_stamp(buffer,
3051 cpu_buffer->cpu, ts);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003052 }
3053 return event;
3054
3055 default:
3056 BUG();
3057 }
3058
3059 return NULL;
3060}
Robert Richterc4f50182008-12-11 16:49:22 +01003061EXPORT_SYMBOL_GPL(ring_buffer_peek);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003062
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003063static struct ring_buffer_event *
3064rb_iter_peek(struct ring_buffer_iter *iter, u64 *ts)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003065{
3066 struct ring_buffer *buffer;
3067 struct ring_buffer_per_cpu *cpu_buffer;
3068 struct ring_buffer_event *event;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003069 int nr_loops = 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003070
3071 if (ring_buffer_iter_empty(iter))
3072 return NULL;
3073
3074 cpu_buffer = iter->cpu_buffer;
3075 buffer = cpu_buffer->buffer;
3076
3077 again:
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003078 /*
Steven Rostedtea05b572009-06-03 09:30:10 -04003079 * We repeat when a timestamp is encountered.
3080 * We can get multiple timestamps by nested interrupts or also
3081 * if filtering is on (discarding commits). Since discarding
3082 * commits can be frequent we can get a lot of timestamps.
3083 * But we limit them by not adding timestamps if they begin
3084 * at the start of a page.
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003085 */
Steven Rostedtea05b572009-06-03 09:30:10 -04003086 if (RB_WARN_ON(cpu_buffer, ++nr_loops > RB_TIMESTAMPS_PER_PAGE))
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003087 return NULL;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003088
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003089 if (rb_per_cpu_empty(cpu_buffer))
3090 return NULL;
3091
3092 event = rb_iter_head_event(iter);
3093
Lai Jiangshan334d4162009-04-24 11:27:05 +08003094 switch (event->type_len) {
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003095 case RINGBUF_TYPE_PADDING:
Tom Zanussi2d622712009-03-22 03:30:49 -05003096 if (rb_null_event(event)) {
3097 rb_inc_iter(iter);
3098 goto again;
3099 }
3100 rb_advance_iter(iter);
3101 return event;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003102
3103 case RINGBUF_TYPE_TIME_EXTEND:
3104 /* Internal data, OK to advance */
3105 rb_advance_iter(iter);
3106 goto again;
3107
3108 case RINGBUF_TYPE_TIME_STAMP:
3109 /* FIXME: not implemented */
3110 rb_advance_iter(iter);
3111 goto again;
3112
3113 case RINGBUF_TYPE_DATA:
3114 if (ts) {
3115 *ts = iter->read_stamp + event->time_delta;
Steven Rostedt37886f62009-03-17 17:22:06 -04003116 ring_buffer_normalize_time_stamp(buffer,
3117 cpu_buffer->cpu, ts);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003118 }
3119 return event;
3120
3121 default:
3122 BUG();
3123 }
3124
3125 return NULL;
3126}
Robert Richterc4f50182008-12-11 16:49:22 +01003127EXPORT_SYMBOL_GPL(ring_buffer_iter_peek);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003128
Steven Rostedt8d707e82009-06-16 21:22:48 -04003129static inline int rb_ok_to_lock(void)
3130{
3131 /*
3132 * If an NMI die dumps out the content of the ring buffer
3133 * do not grab locks. We also permanently disable the ring
3134 * buffer too. A one time deal is all you get from reading
3135 * the ring buffer from an NMI.
3136 */
Steven Rostedt464e85e2009-08-05 15:26:37 -04003137 if (likely(!in_nmi()))
Steven Rostedt8d707e82009-06-16 21:22:48 -04003138 return 1;
3139
3140 tracing_off_permanent();
3141 return 0;
3142}
3143
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003144/**
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003145 * ring_buffer_peek - peek at the next event to be read
3146 * @buffer: The ring buffer to read
3147 * @cpu: The cpu to peak at
3148 * @ts: The timestamp counter of this event.
3149 *
3150 * This will return the event that will be read next, but does
3151 * not consume the data.
3152 */
3153struct ring_buffer_event *
3154ring_buffer_peek(struct ring_buffer *buffer, int cpu, u64 *ts)
3155{
3156 struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
Steven Rostedt8aabee52009-03-12 13:13:49 -04003157 struct ring_buffer_event *event;
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003158 unsigned long flags;
Steven Rostedt8d707e82009-06-16 21:22:48 -04003159 int dolock;
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003160
Steven Rostedt554f7862009-03-11 22:00:13 -04003161 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04003162 return NULL;
Steven Rostedt554f7862009-03-11 22:00:13 -04003163
Steven Rostedt8d707e82009-06-16 21:22:48 -04003164 dolock = rb_ok_to_lock();
Tom Zanussi2d622712009-03-22 03:30:49 -05003165 again:
Steven Rostedt8d707e82009-06-16 21:22:48 -04003166 local_irq_save(flags);
3167 if (dolock)
3168 spin_lock(&cpu_buffer->reader_lock);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003169 event = rb_buffer_peek(buffer, cpu, ts);
Robert Richter469535a2009-07-30 19:19:18 +02003170 if (event && event->type_len == RINGBUF_TYPE_PADDING)
3171 rb_advance_reader(cpu_buffer);
Steven Rostedt8d707e82009-06-16 21:22:48 -04003172 if (dolock)
3173 spin_unlock(&cpu_buffer->reader_lock);
3174 local_irq_restore(flags);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003175
Steven Rostedt1b959e12009-09-03 10:12:13 -04003176 if (event && event->type_len == RINGBUF_TYPE_PADDING)
Tom Zanussi2d622712009-03-22 03:30:49 -05003177 goto again;
Tom Zanussi2d622712009-03-22 03:30:49 -05003178
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003179 return event;
3180}
3181
3182/**
3183 * ring_buffer_iter_peek - peek at the next event to be read
3184 * @iter: The ring buffer iterator
3185 * @ts: The timestamp counter of this event.
3186 *
3187 * This will return the event that will be read next, but does
3188 * not increment the iterator.
3189 */
3190struct ring_buffer_event *
3191ring_buffer_iter_peek(struct ring_buffer_iter *iter, u64 *ts)
3192{
3193 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
3194 struct ring_buffer_event *event;
3195 unsigned long flags;
3196
Tom Zanussi2d622712009-03-22 03:30:49 -05003197 again:
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003198 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
3199 event = rb_iter_peek(iter, ts);
3200 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
3201
Steven Rostedt1b959e12009-09-03 10:12:13 -04003202 if (event && event->type_len == RINGBUF_TYPE_PADDING)
Tom Zanussi2d622712009-03-22 03:30:49 -05003203 goto again;
Tom Zanussi2d622712009-03-22 03:30:49 -05003204
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003205 return event;
3206}
3207
3208/**
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003209 * ring_buffer_consume - return an event and consume it
3210 * @buffer: The ring buffer to get the next event from
3211 *
3212 * Returns the next event in the ring buffer, and that event is consumed.
3213 * Meaning, that sequential reads will keep returning a different event,
3214 * and eventually empty the ring buffer if the producer is slower.
3215 */
3216struct ring_buffer_event *
3217ring_buffer_consume(struct ring_buffer *buffer, int cpu, u64 *ts)
3218{
Steven Rostedt554f7862009-03-11 22:00:13 -04003219 struct ring_buffer_per_cpu *cpu_buffer;
3220 struct ring_buffer_event *event = NULL;
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003221 unsigned long flags;
Steven Rostedt8d707e82009-06-16 21:22:48 -04003222 int dolock;
3223
3224 dolock = rb_ok_to_lock();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003225
Tom Zanussi2d622712009-03-22 03:30:49 -05003226 again:
Steven Rostedt554f7862009-03-11 22:00:13 -04003227 /* might be called in atomic */
3228 preempt_disable();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003229
Steven Rostedt554f7862009-03-11 22:00:13 -04003230 if (!cpumask_test_cpu(cpu, buffer->cpumask))
3231 goto out;
3232
3233 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt8d707e82009-06-16 21:22:48 -04003234 local_irq_save(flags);
3235 if (dolock)
3236 spin_lock(&cpu_buffer->reader_lock);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003237
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003238 event = rb_buffer_peek(buffer, cpu, ts);
Robert Richter469535a2009-07-30 19:19:18 +02003239 if (event)
3240 rb_advance_reader(cpu_buffer);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003241
Steven Rostedt8d707e82009-06-16 21:22:48 -04003242 if (dolock)
3243 spin_unlock(&cpu_buffer->reader_lock);
3244 local_irq_restore(flags);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003245
Steven Rostedt554f7862009-03-11 22:00:13 -04003246 out:
3247 preempt_enable();
3248
Steven Rostedt1b959e12009-09-03 10:12:13 -04003249 if (event && event->type_len == RINGBUF_TYPE_PADDING)
Tom Zanussi2d622712009-03-22 03:30:49 -05003250 goto again;
Tom Zanussi2d622712009-03-22 03:30:49 -05003251
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003252 return event;
3253}
Robert Richterc4f50182008-12-11 16:49:22 +01003254EXPORT_SYMBOL_GPL(ring_buffer_consume);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003255
3256/**
3257 * ring_buffer_read_start - start a non consuming read of the buffer
3258 * @buffer: The ring buffer to read from
3259 * @cpu: The cpu buffer to iterate over
3260 *
3261 * This starts up an iteration through the buffer. It also disables
3262 * the recording to the buffer until the reading is finished.
3263 * This prevents the reading from being corrupted. This is not
3264 * a consuming read, so a producer is not expected.
3265 *
3266 * Must be paired with ring_buffer_finish.
3267 */
3268struct ring_buffer_iter *
3269ring_buffer_read_start(struct ring_buffer *buffer, int cpu)
3270{
3271 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedt8aabee52009-03-12 13:13:49 -04003272 struct ring_buffer_iter *iter;
Steven Rostedtd7690412008-10-01 00:29:53 -04003273 unsigned long flags;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003274
Rusty Russell9e01c1b2009-01-01 10:12:22 +10303275 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04003276 return NULL;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003277
3278 iter = kmalloc(sizeof(*iter), GFP_KERNEL);
3279 if (!iter)
Steven Rostedt8aabee52009-03-12 13:13:49 -04003280 return NULL;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003281
3282 cpu_buffer = buffer->buffers[cpu];
3283
3284 iter->cpu_buffer = cpu_buffer;
3285
3286 atomic_inc(&cpu_buffer->record_disabled);
3287 synchronize_sched();
3288
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003289 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
Steven Rostedt3e03fb72008-11-06 00:09:43 -05003290 __raw_spin_lock(&cpu_buffer->lock);
Steven Rostedt642edba2008-11-12 00:01:26 -05003291 rb_iter_reset(iter);
Steven Rostedt3e03fb72008-11-06 00:09:43 -05003292 __raw_spin_unlock(&cpu_buffer->lock);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003293 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003294
3295 return iter;
3296}
Robert Richterc4f50182008-12-11 16:49:22 +01003297EXPORT_SYMBOL_GPL(ring_buffer_read_start);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003298
3299/**
3300 * ring_buffer_finish - finish reading the iterator of the buffer
3301 * @iter: The iterator retrieved by ring_buffer_start
3302 *
3303 * This re-enables the recording to the buffer, and frees the
3304 * iterator.
3305 */
3306void
3307ring_buffer_read_finish(struct ring_buffer_iter *iter)
3308{
3309 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
3310
3311 atomic_dec(&cpu_buffer->record_disabled);
3312 kfree(iter);
3313}
Robert Richterc4f50182008-12-11 16:49:22 +01003314EXPORT_SYMBOL_GPL(ring_buffer_read_finish);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003315
3316/**
3317 * ring_buffer_read - read the next item in the ring buffer by the iterator
3318 * @iter: The ring buffer iterator
3319 * @ts: The time stamp of the event read.
3320 *
3321 * This reads the next event in the ring buffer and increments the iterator.
3322 */
3323struct ring_buffer_event *
3324ring_buffer_read(struct ring_buffer_iter *iter, u64 *ts)
3325{
3326 struct ring_buffer_event *event;
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003327 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
3328 unsigned long flags;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003329
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003330 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
Steven Rostedt7e9391c2009-09-03 10:02:09 -04003331 again:
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003332 event = rb_iter_peek(iter, ts);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003333 if (!event)
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003334 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003335
Steven Rostedt7e9391c2009-09-03 10:02:09 -04003336 if (event->type_len == RINGBUF_TYPE_PADDING)
3337 goto again;
3338
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003339 rb_advance_iter(iter);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003340 out:
3341 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003342
3343 return event;
3344}
Robert Richterc4f50182008-12-11 16:49:22 +01003345EXPORT_SYMBOL_GPL(ring_buffer_read);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003346
3347/**
3348 * ring_buffer_size - return the size of the ring buffer (in bytes)
3349 * @buffer: The ring buffer.
3350 */
3351unsigned long ring_buffer_size(struct ring_buffer *buffer)
3352{
3353 return BUF_PAGE_SIZE * buffer->pages;
3354}
Robert Richterc4f50182008-12-11 16:49:22 +01003355EXPORT_SYMBOL_GPL(ring_buffer_size);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003356
3357static void
3358rb_reset_cpu(struct ring_buffer_per_cpu *cpu_buffer)
3359{
Steven Rostedt77ae3652009-03-27 11:00:29 -04003360 rb_head_page_deactivate(cpu_buffer);
3361
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003362 cpu_buffer->head_page
Steven Rostedt3adc54f2009-03-30 15:32:01 -04003363 = list_entry(cpu_buffer->pages, struct buffer_page, list);
Steven Rostedtbf41a152008-10-04 02:00:59 -04003364 local_set(&cpu_buffer->head_page->write, 0);
Steven Rostedt778c55d2009-05-01 18:44:45 -04003365 local_set(&cpu_buffer->head_page->entries, 0);
Steven Rostedtabc9b562008-12-02 15:34:06 -05003366 local_set(&cpu_buffer->head_page->page->commit, 0);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003367
Steven Rostedt6f807ac2008-10-04 02:00:58 -04003368 cpu_buffer->head_page->read = 0;
Steven Rostedtbf41a152008-10-04 02:00:59 -04003369
3370 cpu_buffer->tail_page = cpu_buffer->head_page;
3371 cpu_buffer->commit_page = cpu_buffer->head_page;
3372
3373 INIT_LIST_HEAD(&cpu_buffer->reader_page->list);
3374 local_set(&cpu_buffer->reader_page->write, 0);
Steven Rostedt778c55d2009-05-01 18:44:45 -04003375 local_set(&cpu_buffer->reader_page->entries, 0);
Steven Rostedtabc9b562008-12-02 15:34:06 -05003376 local_set(&cpu_buffer->reader_page->page->commit, 0);
Steven Rostedt6f807ac2008-10-04 02:00:58 -04003377 cpu_buffer->reader_page->read = 0;
Steven Rostedtd7690412008-10-01 00:29:53 -04003378
Steven Rostedt77ae3652009-03-27 11:00:29 -04003379 local_set(&cpu_buffer->commit_overrun, 0);
3380 local_set(&cpu_buffer->overrun, 0);
Steven Rostedte4906ef2009-04-30 20:49:44 -04003381 local_set(&cpu_buffer->entries, 0);
Steven Rostedtfa743952009-06-16 12:37:57 -04003382 local_set(&cpu_buffer->committing, 0);
3383 local_set(&cpu_buffer->commits, 0);
Steven Rostedt77ae3652009-03-27 11:00:29 -04003384 cpu_buffer->read = 0;
Steven Rostedt69507c02009-01-21 18:45:57 -05003385
3386 cpu_buffer->write_stamp = 0;
3387 cpu_buffer->read_stamp = 0;
Steven Rostedt77ae3652009-03-27 11:00:29 -04003388
3389 rb_head_page_activate(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003390}
3391
3392/**
3393 * ring_buffer_reset_cpu - reset a ring buffer per CPU buffer
3394 * @buffer: The ring buffer to reset a per cpu buffer of
3395 * @cpu: The CPU buffer to be reset
3396 */
3397void ring_buffer_reset_cpu(struct ring_buffer *buffer, int cpu)
3398{
3399 struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
3400 unsigned long flags;
3401
Rusty Russell9e01c1b2009-01-01 10:12:22 +10303402 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04003403 return;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003404
Steven Rostedt41ede232009-05-01 20:26:54 -04003405 atomic_inc(&cpu_buffer->record_disabled);
3406
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003407 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
3408
Steven Rostedt41b6a952009-09-02 09:59:48 -04003409 if (RB_WARN_ON(cpu_buffer, local_read(&cpu_buffer->committing)))
3410 goto out;
3411
Steven Rostedt3e03fb72008-11-06 00:09:43 -05003412 __raw_spin_lock(&cpu_buffer->lock);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003413
3414 rb_reset_cpu(cpu_buffer);
3415
Steven Rostedt3e03fb72008-11-06 00:09:43 -05003416 __raw_spin_unlock(&cpu_buffer->lock);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003417
Steven Rostedt41b6a952009-09-02 09:59:48 -04003418 out:
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003419 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
Steven Rostedt41ede232009-05-01 20:26:54 -04003420
3421 atomic_dec(&cpu_buffer->record_disabled);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003422}
Robert Richterc4f50182008-12-11 16:49:22 +01003423EXPORT_SYMBOL_GPL(ring_buffer_reset_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003424
3425/**
3426 * ring_buffer_reset - reset a ring buffer
3427 * @buffer: The ring buffer to reset all cpu buffers
3428 */
3429void ring_buffer_reset(struct ring_buffer *buffer)
3430{
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003431 int cpu;
3432
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003433 for_each_buffer_cpu(buffer, cpu)
Steven Rostedtd7690412008-10-01 00:29:53 -04003434 ring_buffer_reset_cpu(buffer, cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003435}
Robert Richterc4f50182008-12-11 16:49:22 +01003436EXPORT_SYMBOL_GPL(ring_buffer_reset);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003437
3438/**
3439 * rind_buffer_empty - is the ring buffer empty?
3440 * @buffer: The ring buffer to test
3441 */
3442int ring_buffer_empty(struct ring_buffer *buffer)
3443{
3444 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedtd4788202009-06-17 00:39:43 -04003445 unsigned long flags;
Steven Rostedt8d707e82009-06-16 21:22:48 -04003446 int dolock;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003447 int cpu;
Steven Rostedtd4788202009-06-17 00:39:43 -04003448 int ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003449
Steven Rostedt8d707e82009-06-16 21:22:48 -04003450 dolock = rb_ok_to_lock();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003451
3452 /* yes this is racy, but if you don't like the race, lock the buffer */
3453 for_each_buffer_cpu(buffer, cpu) {
3454 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt8d707e82009-06-16 21:22:48 -04003455 local_irq_save(flags);
3456 if (dolock)
3457 spin_lock(&cpu_buffer->reader_lock);
Steven Rostedtd4788202009-06-17 00:39:43 -04003458 ret = rb_per_cpu_empty(cpu_buffer);
Steven Rostedt8d707e82009-06-16 21:22:48 -04003459 if (dolock)
3460 spin_unlock(&cpu_buffer->reader_lock);
3461 local_irq_restore(flags);
3462
Steven Rostedtd4788202009-06-17 00:39:43 -04003463 if (!ret)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003464 return 0;
3465 }
Steven Rostedt554f7862009-03-11 22:00:13 -04003466
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003467 return 1;
3468}
Robert Richterc4f50182008-12-11 16:49:22 +01003469EXPORT_SYMBOL_GPL(ring_buffer_empty);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003470
3471/**
3472 * ring_buffer_empty_cpu - is a cpu buffer of a ring buffer empty?
3473 * @buffer: The ring buffer
3474 * @cpu: The CPU buffer to test
3475 */
3476int ring_buffer_empty_cpu(struct ring_buffer *buffer, int cpu)
3477{
3478 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedtd4788202009-06-17 00:39:43 -04003479 unsigned long flags;
Steven Rostedt8d707e82009-06-16 21:22:48 -04003480 int dolock;
Steven Rostedt8aabee52009-03-12 13:13:49 -04003481 int ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003482
Rusty Russell9e01c1b2009-01-01 10:12:22 +10303483 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04003484 return 1;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003485
Steven Rostedt8d707e82009-06-16 21:22:48 -04003486 dolock = rb_ok_to_lock();
Steven Rostedt554f7862009-03-11 22:00:13 -04003487
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003488 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt8d707e82009-06-16 21:22:48 -04003489 local_irq_save(flags);
3490 if (dolock)
3491 spin_lock(&cpu_buffer->reader_lock);
Steven Rostedt554f7862009-03-11 22:00:13 -04003492 ret = rb_per_cpu_empty(cpu_buffer);
Steven Rostedt8d707e82009-06-16 21:22:48 -04003493 if (dolock)
3494 spin_unlock(&cpu_buffer->reader_lock);
3495 local_irq_restore(flags);
Steven Rostedt554f7862009-03-11 22:00:13 -04003496
3497 return ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003498}
Robert Richterc4f50182008-12-11 16:49:22 +01003499EXPORT_SYMBOL_GPL(ring_buffer_empty_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003500
3501/**
3502 * ring_buffer_swap_cpu - swap a CPU buffer between two ring buffers
3503 * @buffer_a: One buffer to swap with
3504 * @buffer_b: The other buffer to swap with
3505 *
3506 * This function is useful for tracers that want to take a "snapshot"
3507 * of a CPU buffer and has another back up buffer lying around.
3508 * it is expected that the tracer handles the cpu buffer not being
3509 * used at the moment.
3510 */
3511int ring_buffer_swap_cpu(struct ring_buffer *buffer_a,
3512 struct ring_buffer *buffer_b, int cpu)
3513{
3514 struct ring_buffer_per_cpu *cpu_buffer_a;
3515 struct ring_buffer_per_cpu *cpu_buffer_b;
Steven Rostedt554f7862009-03-11 22:00:13 -04003516 int ret = -EINVAL;
3517
Rusty Russell9e01c1b2009-01-01 10:12:22 +10303518 if (!cpumask_test_cpu(cpu, buffer_a->cpumask) ||
3519 !cpumask_test_cpu(cpu, buffer_b->cpumask))
Steven Rostedt554f7862009-03-11 22:00:13 -04003520 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003521
3522 /* At least make sure the two buffers are somewhat the same */
Lai Jiangshan6d102bc2008-12-17 17:48:23 +08003523 if (buffer_a->pages != buffer_b->pages)
Steven Rostedt554f7862009-03-11 22:00:13 -04003524 goto out;
3525
3526 ret = -EAGAIN;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003527
Steven Rostedt97b17ef2009-01-21 15:24:56 -05003528 if (ring_buffer_flags != RB_BUFFERS_ON)
Steven Rostedt554f7862009-03-11 22:00:13 -04003529 goto out;
Steven Rostedt97b17ef2009-01-21 15:24:56 -05003530
3531 if (atomic_read(&buffer_a->record_disabled))
Steven Rostedt554f7862009-03-11 22:00:13 -04003532 goto out;
Steven Rostedt97b17ef2009-01-21 15:24:56 -05003533
3534 if (atomic_read(&buffer_b->record_disabled))
Steven Rostedt554f7862009-03-11 22:00:13 -04003535 goto out;
Steven Rostedt97b17ef2009-01-21 15:24:56 -05003536
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003537 cpu_buffer_a = buffer_a->buffers[cpu];
3538 cpu_buffer_b = buffer_b->buffers[cpu];
3539
Steven Rostedt97b17ef2009-01-21 15:24:56 -05003540 if (atomic_read(&cpu_buffer_a->record_disabled))
Steven Rostedt554f7862009-03-11 22:00:13 -04003541 goto out;
Steven Rostedt97b17ef2009-01-21 15:24:56 -05003542
3543 if (atomic_read(&cpu_buffer_b->record_disabled))
Steven Rostedt554f7862009-03-11 22:00:13 -04003544 goto out;
Steven Rostedt97b17ef2009-01-21 15:24:56 -05003545
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003546 /*
3547 * We can't do a synchronize_sched here because this
3548 * function can be called in atomic context.
3549 * Normally this will be called from the same CPU as cpu.
3550 * If not it's up to the caller to protect this.
3551 */
3552 atomic_inc(&cpu_buffer_a->record_disabled);
3553 atomic_inc(&cpu_buffer_b->record_disabled);
3554
Steven Rostedt98277992009-09-02 10:56:15 -04003555 ret = -EBUSY;
3556 if (local_read(&cpu_buffer_a->committing))
3557 goto out_dec;
3558 if (local_read(&cpu_buffer_b->committing))
3559 goto out_dec;
3560
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003561 buffer_a->buffers[cpu] = cpu_buffer_b;
3562 buffer_b->buffers[cpu] = cpu_buffer_a;
3563
3564 cpu_buffer_b->buffer = buffer_a;
3565 cpu_buffer_a->buffer = buffer_b;
3566
Steven Rostedt98277992009-09-02 10:56:15 -04003567 ret = 0;
3568
3569out_dec:
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003570 atomic_dec(&cpu_buffer_a->record_disabled);
3571 atomic_dec(&cpu_buffer_b->record_disabled);
Steven Rostedt554f7862009-03-11 22:00:13 -04003572out:
Steven Rostedt554f7862009-03-11 22:00:13 -04003573 return ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003574}
Robert Richterc4f50182008-12-11 16:49:22 +01003575EXPORT_SYMBOL_GPL(ring_buffer_swap_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003576
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003577/**
3578 * ring_buffer_alloc_read_page - allocate a page to read from buffer
3579 * @buffer: the buffer to allocate for.
3580 *
3581 * This function is used in conjunction with ring_buffer_read_page.
3582 * When reading a full page from the ring buffer, these functions
3583 * can be used to speed up the process. The calling function should
3584 * allocate a few pages first with this function. Then when it
3585 * needs to get pages from the ring buffer, it passes the result
3586 * of this function into ring_buffer_read_page, which will swap
3587 * the page that was allocated, with the read page of the buffer.
3588 *
3589 * Returns:
3590 * The page allocated, or NULL on error.
3591 */
3592void *ring_buffer_alloc_read_page(struct ring_buffer *buffer)
3593{
Steven Rostedt044fa782008-12-02 23:50:03 -05003594 struct buffer_data_page *bpage;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003595 unsigned long addr;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003596
3597 addr = __get_free_page(GFP_KERNEL);
3598 if (!addr)
3599 return NULL;
3600
Steven Rostedt044fa782008-12-02 23:50:03 -05003601 bpage = (void *)addr;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003602
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003603 rb_init_page(bpage);
3604
Steven Rostedt044fa782008-12-02 23:50:03 -05003605 return bpage;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003606}
Steven Rostedtd6ce96d2009-05-05 01:15:24 -04003607EXPORT_SYMBOL_GPL(ring_buffer_alloc_read_page);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003608
3609/**
3610 * ring_buffer_free_read_page - free an allocated read page
3611 * @buffer: the buffer the page was allocate for
3612 * @data: the page to free
3613 *
3614 * Free a page allocated from ring_buffer_alloc_read_page.
3615 */
3616void ring_buffer_free_read_page(struct ring_buffer *buffer, void *data)
3617{
3618 free_page((unsigned long)data);
3619}
Steven Rostedtd6ce96d2009-05-05 01:15:24 -04003620EXPORT_SYMBOL_GPL(ring_buffer_free_read_page);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003621
3622/**
3623 * ring_buffer_read_page - extract a page from the ring buffer
3624 * @buffer: buffer to extract from
3625 * @data_page: the page to use allocated from ring_buffer_alloc_read_page
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003626 * @len: amount to extract
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003627 * @cpu: the cpu of the buffer to extract
3628 * @full: should the extraction only happen when the page is full.
3629 *
3630 * This function will pull out a page from the ring buffer and consume it.
3631 * @data_page must be the address of the variable that was returned
3632 * from ring_buffer_alloc_read_page. This is because the page might be used
3633 * to swap with a page in the ring buffer.
3634 *
3635 * for example:
Lai Jiangshanb85fa012009-02-09 14:21:14 +08003636 * rpage = ring_buffer_alloc_read_page(buffer);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003637 * if (!rpage)
3638 * return error;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003639 * ret = ring_buffer_read_page(buffer, &rpage, len, cpu, 0);
Lai Jiangshan667d2412009-02-09 14:21:17 +08003640 * if (ret >= 0)
3641 * process_page(rpage, ret);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003642 *
3643 * When @full is set, the function will not return true unless
3644 * the writer is off the reader page.
3645 *
3646 * Note: it is up to the calling functions to handle sleeps and wakeups.
3647 * The ring buffer can be used anywhere in the kernel and can not
3648 * blindly call wake_up. The layer that uses the ring buffer must be
3649 * responsible for that.
3650 *
3651 * Returns:
Lai Jiangshan667d2412009-02-09 14:21:17 +08003652 * >=0 if data has been transferred, returns the offset of consumed data.
3653 * <0 if no data has been transferred.
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003654 */
3655int ring_buffer_read_page(struct ring_buffer *buffer,
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003656 void **data_page, size_t len, int cpu, int full)
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003657{
3658 struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
3659 struct ring_buffer_event *event;
Steven Rostedt044fa782008-12-02 23:50:03 -05003660 struct buffer_data_page *bpage;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003661 struct buffer_page *reader;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003662 unsigned long flags;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003663 unsigned int commit;
Lai Jiangshan667d2412009-02-09 14:21:17 +08003664 unsigned int read;
Steven Rostedt4f3640f2009-03-03 23:52:42 -05003665 u64 save_timestamp;
Lai Jiangshan667d2412009-02-09 14:21:17 +08003666 int ret = -1;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003667
Steven Rostedt554f7862009-03-11 22:00:13 -04003668 if (!cpumask_test_cpu(cpu, buffer->cpumask))
3669 goto out;
3670
Steven Rostedt474d32b2009-03-03 19:51:40 -05003671 /*
3672 * If len is not big enough to hold the page header, then
3673 * we can not copy anything.
3674 */
3675 if (len <= BUF_PAGE_HDR_SIZE)
Steven Rostedt554f7862009-03-11 22:00:13 -04003676 goto out;
Steven Rostedt474d32b2009-03-03 19:51:40 -05003677
3678 len -= BUF_PAGE_HDR_SIZE;
3679
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003680 if (!data_page)
Steven Rostedt554f7862009-03-11 22:00:13 -04003681 goto out;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003682
Steven Rostedt044fa782008-12-02 23:50:03 -05003683 bpage = *data_page;
3684 if (!bpage)
Steven Rostedt554f7862009-03-11 22:00:13 -04003685 goto out;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003686
3687 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
3688
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003689 reader = rb_get_reader_page(cpu_buffer);
3690 if (!reader)
Steven Rostedt554f7862009-03-11 22:00:13 -04003691 goto out_unlock;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003692
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003693 event = rb_reader_event(cpu_buffer);
Lai Jiangshan667d2412009-02-09 14:21:17 +08003694
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003695 read = reader->read;
3696 commit = rb_page_commit(reader);
3697
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003698 /*
Steven Rostedt474d32b2009-03-03 19:51:40 -05003699 * If this page has been partially read or
3700 * if len is not big enough to read the rest of the page or
3701 * a writer is still on the page, then
3702 * we must copy the data from the page to the buffer.
3703 * Otherwise, we can simply swap the page with the one passed in.
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003704 */
Steven Rostedt474d32b2009-03-03 19:51:40 -05003705 if (read || (len < (commit - read)) ||
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003706 cpu_buffer->reader_page == cpu_buffer->commit_page) {
Lai Jiangshan667d2412009-02-09 14:21:17 +08003707 struct buffer_data_page *rpage = cpu_buffer->reader_page->page;
Steven Rostedt474d32b2009-03-03 19:51:40 -05003708 unsigned int rpos = read;
3709 unsigned int pos = 0;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003710 unsigned int size;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003711
3712 if (full)
Steven Rostedt554f7862009-03-11 22:00:13 -04003713 goto out_unlock;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003714
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003715 if (len > (commit - read))
3716 len = (commit - read);
3717
3718 size = rb_event_length(event);
3719
3720 if (len < size)
Steven Rostedt554f7862009-03-11 22:00:13 -04003721 goto out_unlock;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003722
Steven Rostedt4f3640f2009-03-03 23:52:42 -05003723 /* save the current timestamp, since the user will need it */
3724 save_timestamp = cpu_buffer->read_stamp;
3725
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003726 /* Need to copy one event at a time */
3727 do {
Steven Rostedt474d32b2009-03-03 19:51:40 -05003728 memcpy(bpage->data + pos, rpage->data + rpos, size);
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003729
3730 len -= size;
3731
3732 rb_advance_reader(cpu_buffer);
Steven Rostedt474d32b2009-03-03 19:51:40 -05003733 rpos = reader->read;
3734 pos += size;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003735
3736 event = rb_reader_event(cpu_buffer);
3737 size = rb_event_length(event);
3738 } while (len > size);
Lai Jiangshan667d2412009-02-09 14:21:17 +08003739
3740 /* update bpage */
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003741 local_set(&bpage->commit, pos);
Steven Rostedt4f3640f2009-03-03 23:52:42 -05003742 bpage->time_stamp = save_timestamp;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003743
Steven Rostedt474d32b2009-03-03 19:51:40 -05003744 /* we copied everything to the beginning */
3745 read = 0;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003746 } else {
Steven Rostedtafbab762009-05-01 19:40:05 -04003747 /* update the entry counter */
Steven Rostedt77ae3652009-03-27 11:00:29 -04003748 cpu_buffer->read += rb_page_entries(reader);
Steven Rostedtafbab762009-05-01 19:40:05 -04003749
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003750 /* swap the pages */
Steven Rostedt044fa782008-12-02 23:50:03 -05003751 rb_init_page(bpage);
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003752 bpage = reader->page;
3753 reader->page = *data_page;
3754 local_set(&reader->write, 0);
Steven Rostedt778c55d2009-05-01 18:44:45 -04003755 local_set(&reader->entries, 0);
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003756 reader->read = 0;
Steven Rostedt044fa782008-12-02 23:50:03 -05003757 *data_page = bpage;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003758 }
Lai Jiangshan667d2412009-02-09 14:21:17 +08003759 ret = read;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003760
Steven Rostedt554f7862009-03-11 22:00:13 -04003761 out_unlock:
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003762 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
3763
Steven Rostedt554f7862009-03-11 22:00:13 -04003764 out:
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003765 return ret;
3766}
Steven Rostedtd6ce96d2009-05-05 01:15:24 -04003767EXPORT_SYMBOL_GPL(ring_buffer_read_page);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003768
Paul Mundt1155de42009-06-25 14:30:12 +09003769#ifdef CONFIG_TRACING
Steven Rostedta3583242008-11-11 15:01:42 -05003770static ssize_t
3771rb_simple_read(struct file *filp, char __user *ubuf,
3772 size_t cnt, loff_t *ppos)
3773{
Hannes Eder5e398412009-02-10 19:44:34 +01003774 unsigned long *p = filp->private_data;
Steven Rostedta3583242008-11-11 15:01:42 -05003775 char buf[64];
3776 int r;
3777
Steven Rostedt033601a2008-11-21 12:41:55 -05003778 if (test_bit(RB_BUFFERS_DISABLED_BIT, p))
3779 r = sprintf(buf, "permanently disabled\n");
3780 else
3781 r = sprintf(buf, "%d\n", test_bit(RB_BUFFERS_ON_BIT, p));
Steven Rostedta3583242008-11-11 15:01:42 -05003782
3783 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
3784}
3785
3786static ssize_t
3787rb_simple_write(struct file *filp, const char __user *ubuf,
3788 size_t cnt, loff_t *ppos)
3789{
Hannes Eder5e398412009-02-10 19:44:34 +01003790 unsigned long *p = filp->private_data;
Steven Rostedta3583242008-11-11 15:01:42 -05003791 char buf[64];
Hannes Eder5e398412009-02-10 19:44:34 +01003792 unsigned long val;
Steven Rostedta3583242008-11-11 15:01:42 -05003793 int ret;
3794
3795 if (cnt >= sizeof(buf))
3796 return -EINVAL;
3797
3798 if (copy_from_user(&buf, ubuf, cnt))
3799 return -EFAULT;
3800
3801 buf[cnt] = 0;
3802
3803 ret = strict_strtoul(buf, 10, &val);
3804 if (ret < 0)
3805 return ret;
3806
Steven Rostedt033601a2008-11-21 12:41:55 -05003807 if (val)
3808 set_bit(RB_BUFFERS_ON_BIT, p);
3809 else
3810 clear_bit(RB_BUFFERS_ON_BIT, p);
Steven Rostedta3583242008-11-11 15:01:42 -05003811
3812 (*ppos)++;
3813
3814 return cnt;
3815}
3816
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003817static const struct file_operations rb_simple_fops = {
Steven Rostedta3583242008-11-11 15:01:42 -05003818 .open = tracing_open_generic,
3819 .read = rb_simple_read,
3820 .write = rb_simple_write,
3821};
3822
3823
3824static __init int rb_init_debugfs(void)
3825{
3826 struct dentry *d_tracer;
Steven Rostedta3583242008-11-11 15:01:42 -05003827
3828 d_tracer = tracing_init_dentry();
3829
Frederic Weisbecker5452af62009-03-27 00:25:38 +01003830 trace_create_file("tracing_on", 0644, d_tracer,
3831 &ring_buffer_flags, &rb_simple_fops);
Steven Rostedta3583242008-11-11 15:01:42 -05003832
3833 return 0;
3834}
3835
3836fs_initcall(rb_init_debugfs);
Paul Mundt1155de42009-06-25 14:30:12 +09003837#endif
Steven Rostedt554f7862009-03-11 22:00:13 -04003838
Steven Rostedt59222ef2009-03-12 11:46:03 -04003839#ifdef CONFIG_HOTPLUG_CPU
Frederic Weisbecker09c9e842009-03-21 04:33:36 +01003840static int rb_cpu_notify(struct notifier_block *self,
3841 unsigned long action, void *hcpu)
Steven Rostedt554f7862009-03-11 22:00:13 -04003842{
3843 struct ring_buffer *buffer =
3844 container_of(self, struct ring_buffer, cpu_notify);
3845 long cpu = (long)hcpu;
3846
3847 switch (action) {
3848 case CPU_UP_PREPARE:
3849 case CPU_UP_PREPARE_FROZEN:
Rusty Russell3f237a72009-06-12 21:15:30 +09303850 if (cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt554f7862009-03-11 22:00:13 -04003851 return NOTIFY_OK;
3852
3853 buffer->buffers[cpu] =
3854 rb_allocate_cpu_buffer(buffer, cpu);
3855 if (!buffer->buffers[cpu]) {
3856 WARN(1, "failed to allocate ring buffer on CPU %ld\n",
3857 cpu);
3858 return NOTIFY_OK;
3859 }
3860 smp_wmb();
Rusty Russell3f237a72009-06-12 21:15:30 +09303861 cpumask_set_cpu(cpu, buffer->cpumask);
Steven Rostedt554f7862009-03-11 22:00:13 -04003862 break;
3863 case CPU_DOWN_PREPARE:
3864 case CPU_DOWN_PREPARE_FROZEN:
3865 /*
3866 * Do nothing.
3867 * If we were to free the buffer, then the user would
3868 * lose any trace that was in the buffer.
3869 */
3870 break;
3871 default:
3872 break;
3873 }
3874 return NOTIFY_OK;
3875}
3876#endif