blob: 4b8293fa545ec72cbcab109d846e5e268b22ec2f [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
Steven Rostedte3d6bf02009-03-03 13:53:07 -0500204#define RB_EVNT_HDR_SIZE (offsetof(struct ring_buffer_event, array))
Andrew Morton67d34722009-01-09 12:27:09 -0800205#define RB_ALIGNMENT 4U
Lai Jiangshan334d4162009-04-24 11:27:05 +0800206#define RB_MAX_SMALL_DATA (RB_ALIGNMENT * RINGBUF_TYPE_DATA_TYPE_LEN_MAX)
Steven Rostedtc7b09302009-06-11 11:12:00 -0400207#define RB_EVNT_MIN_SIZE 8U /* two 32bit words */
Lai Jiangshan334d4162009-04-24 11:27:05 +0800208
209/* define RINGBUF_TYPE_DATA for 'case RINGBUF_TYPE_DATA:' */
210#define RINGBUF_TYPE_DATA 0 ... RINGBUF_TYPE_DATA_TYPE_LEN_MAX
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400211
212enum {
213 RB_LEN_TIME_EXTEND = 8,
214 RB_LEN_TIME_STAMP = 16,
215};
216
Tom Zanussi2d622712009-03-22 03:30:49 -0500217static inline int rb_null_event(struct ring_buffer_event *event)
218{
Steven Rostedta1863c22009-09-03 10:23:58 -0400219 return event->type_len == RINGBUF_TYPE_PADDING && !event->time_delta;
Tom Zanussi2d622712009-03-22 03:30:49 -0500220}
221
222static void rb_event_set_padding(struct ring_buffer_event *event)
223{
Steven Rostedta1863c22009-09-03 10:23:58 -0400224 /* padding has a NULL time_delta */
Lai Jiangshan334d4162009-04-24 11:27:05 +0800225 event->type_len = RINGBUF_TYPE_PADDING;
Tom Zanussi2d622712009-03-22 03:30:49 -0500226 event->time_delta = 0;
227}
228
Tom Zanussi2d622712009-03-22 03:30:49 -0500229static unsigned
230rb_event_data_length(struct ring_buffer_event *event)
231{
232 unsigned length;
233
Lai Jiangshan334d4162009-04-24 11:27:05 +0800234 if (event->type_len)
235 length = event->type_len * RB_ALIGNMENT;
Tom Zanussi2d622712009-03-22 03:30:49 -0500236 else
237 length = event->array[0];
238 return length + RB_EVNT_HDR_SIZE;
239}
240
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400241/* inline for ring buffer fast paths */
Andrew Morton34a148b2009-01-09 12:27:09 -0800242static unsigned
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400243rb_event_length(struct ring_buffer_event *event)
244{
Lai Jiangshan334d4162009-04-24 11:27:05 +0800245 switch (event->type_len) {
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400246 case RINGBUF_TYPE_PADDING:
Tom Zanussi2d622712009-03-22 03:30:49 -0500247 if (rb_null_event(event))
248 /* undefined */
249 return -1;
Lai Jiangshan334d4162009-04-24 11:27:05 +0800250 return event->array[0] + RB_EVNT_HDR_SIZE;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400251
252 case RINGBUF_TYPE_TIME_EXTEND:
253 return RB_LEN_TIME_EXTEND;
254
255 case RINGBUF_TYPE_TIME_STAMP:
256 return RB_LEN_TIME_STAMP;
257
258 case RINGBUF_TYPE_DATA:
Tom Zanussi2d622712009-03-22 03:30:49 -0500259 return rb_event_data_length(event);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400260 default:
261 BUG();
262 }
263 /* not hit */
264 return 0;
265}
266
267/**
268 * ring_buffer_event_length - return the length of the event
269 * @event: the event to get the length of
270 */
271unsigned ring_buffer_event_length(struct ring_buffer_event *event)
272{
Robert Richter465634a2009-01-07 15:32:11 +0100273 unsigned length = rb_event_length(event);
Lai Jiangshan334d4162009-04-24 11:27:05 +0800274 if (event->type_len > RINGBUF_TYPE_DATA_TYPE_LEN_MAX)
Robert Richter465634a2009-01-07 15:32:11 +0100275 return length;
276 length -= RB_EVNT_HDR_SIZE;
277 if (length > RB_MAX_SMALL_DATA + sizeof(event->array[0]))
278 length -= sizeof(event->array[0]);
279 return length;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400280}
Robert Richterc4f50182008-12-11 16:49:22 +0100281EXPORT_SYMBOL_GPL(ring_buffer_event_length);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400282
283/* inline for ring buffer fast paths */
Andrew Morton34a148b2009-01-09 12:27:09 -0800284static void *
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400285rb_event_data(struct ring_buffer_event *event)
286{
Lai Jiangshan334d4162009-04-24 11:27:05 +0800287 BUG_ON(event->type_len > RINGBUF_TYPE_DATA_TYPE_LEN_MAX);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400288 /* If length is in len field, then array[0] has the data */
Lai Jiangshan334d4162009-04-24 11:27:05 +0800289 if (event->type_len)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400290 return (void *)&event->array[0];
291 /* Otherwise length is in array[0] and array[1] has the data */
292 return (void *)&event->array[1];
293}
294
295/**
296 * ring_buffer_event_data - return the data of the event
297 * @event: the event to get the data from
298 */
299void *ring_buffer_event_data(struct ring_buffer_event *event)
300{
301 return rb_event_data(event);
302}
Robert Richterc4f50182008-12-11 16:49:22 +0100303EXPORT_SYMBOL_GPL(ring_buffer_event_data);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400304
305#define for_each_buffer_cpu(buffer, cpu) \
Rusty Russell9e01c1b2009-01-01 10:12:22 +1030306 for_each_cpu(cpu, buffer->cpumask)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400307
308#define TS_SHIFT 27
309#define TS_MASK ((1ULL << TS_SHIFT) - 1)
310#define TS_DELTA_TEST (~TS_MASK)
311
Steven Rostedtabc9b562008-12-02 15:34:06 -0500312struct buffer_data_page {
Steven Rostedte4c2ce82008-10-01 11:14:54 -0400313 u64 time_stamp; /* page time stamp */
Wenji Huangc3706f02009-02-10 01:03:18 -0500314 local_t commit; /* write committed index */
Steven Rostedtabc9b562008-12-02 15:34:06 -0500315 unsigned char data[]; /* data of buffer page */
316};
317
Steven Rostedt77ae3652009-03-27 11:00:29 -0400318/*
319 * Note, the buffer_page list must be first. The buffer pages
320 * are allocated in cache lines, which means that each buffer
321 * page will be at the beginning of a cache line, and thus
322 * the least significant bits will be zero. We use this to
323 * add flags in the list struct pointers, to make the ring buffer
324 * lockless.
325 */
Steven Rostedtabc9b562008-12-02 15:34:06 -0500326struct buffer_page {
Steven Rostedt778c55d2009-05-01 18:44:45 -0400327 struct list_head list; /* list of buffer pages */
Steven Rostedtabc9b562008-12-02 15:34:06 -0500328 local_t write; /* index for next write */
Steven Rostedt6f807ac2008-10-04 02:00:58 -0400329 unsigned read; /* index for next read */
Steven Rostedt778c55d2009-05-01 18:44:45 -0400330 local_t entries; /* entries on this page */
Steven Rostedtabc9b562008-12-02 15:34:06 -0500331 struct buffer_data_page *page; /* Actual data page */
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400332};
333
Steven Rostedt77ae3652009-03-27 11:00:29 -0400334/*
335 * The buffer page counters, write and entries, must be reset
336 * atomically when crossing page boundaries. To synchronize this
337 * update, two counters are inserted into the number. One is
338 * the actual counter for the write position or count on the page.
339 *
340 * The other is a counter of updaters. Before an update happens
341 * the update partition of the counter is incremented. This will
342 * allow the updater to update the counter atomically.
343 *
344 * The counter is 20 bits, and the state data is 12.
345 */
346#define RB_WRITE_MASK 0xfffff
347#define RB_WRITE_INTCNT (1 << 20)
348
Steven Rostedt044fa782008-12-02 23:50:03 -0500349static void rb_init_page(struct buffer_data_page *bpage)
Steven Rostedtabc9b562008-12-02 15:34:06 -0500350{
Steven Rostedt044fa782008-12-02 23:50:03 -0500351 local_set(&bpage->commit, 0);
Steven Rostedtabc9b562008-12-02 15:34:06 -0500352}
353
Steven Rostedt474d32b2009-03-03 19:51:40 -0500354/**
355 * ring_buffer_page_len - the size of data on the page.
356 * @page: The page to read
357 *
358 * Returns the amount of data on the page, including buffer page header.
359 */
Steven Rostedtef7a4a12009-03-03 00:27:49 -0500360size_t ring_buffer_page_len(void *page)
361{
Steven Rostedt474d32b2009-03-03 19:51:40 -0500362 return local_read(&((struct buffer_data_page *)page)->commit)
363 + BUF_PAGE_HDR_SIZE;
Steven Rostedtef7a4a12009-03-03 00:27:49 -0500364}
365
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400366/*
Steven Rostedted568292008-09-29 23:02:40 -0400367 * Also stolen from mm/slob.c. Thanks to Mathieu Desnoyers for pointing
368 * this issue out.
369 */
Andrew Morton34a148b2009-01-09 12:27:09 -0800370static void free_buffer_page(struct buffer_page *bpage)
Steven Rostedted568292008-09-29 23:02:40 -0400371{
Andrew Morton34a148b2009-01-09 12:27:09 -0800372 free_page((unsigned long)bpage->page);
Steven Rostedte4c2ce82008-10-01 11:14:54 -0400373 kfree(bpage);
Steven Rostedted568292008-09-29 23:02:40 -0400374}
375
376/*
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400377 * We need to fit the time_stamp delta into 27 bits.
378 */
379static inline int test_time_stamp(u64 delta)
380{
381 if (delta & TS_DELTA_TEST)
382 return 1;
383 return 0;
384}
385
Steven Rostedt474d32b2009-03-03 19:51:40 -0500386#define BUF_PAGE_SIZE (PAGE_SIZE - BUF_PAGE_HDR_SIZE)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400387
Steven Rostedtbe957c42009-05-11 14:42:53 -0400388/* Max payload is BUF_PAGE_SIZE - header (8bytes) */
389#define BUF_MAX_DATA_SIZE (BUF_PAGE_SIZE - (sizeof(u32) * 2))
390
Steven Rostedtea05b572009-06-03 09:30:10 -0400391/* Max number of timestamps that can fit on a page */
392#define RB_TIMESTAMPS_PER_PAGE (BUF_PAGE_SIZE / RB_LEN_TIME_STAMP)
393
Steven Rostedtd1b182a2009-04-15 16:53:47 -0400394int ring_buffer_print_page_header(struct trace_seq *s)
395{
396 struct buffer_data_page field;
397 int ret;
398
399 ret = trace_seq_printf(s, "\tfield: u64 timestamp;\t"
400 "offset:0;\tsize:%u;\n",
401 (unsigned int)sizeof(field.time_stamp));
402
403 ret = trace_seq_printf(s, "\tfield: local_t commit;\t"
404 "offset:%u;\tsize:%u;\n",
405 (unsigned int)offsetof(typeof(field), commit),
406 (unsigned int)sizeof(field.commit));
407
408 ret = trace_seq_printf(s, "\tfield: char data;\t"
409 "offset:%u;\tsize:%u;\n",
410 (unsigned int)offsetof(typeof(field), data),
411 (unsigned int)BUF_PAGE_SIZE);
412
413 return ret;
414}
415
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400416/*
417 * head_page == tail_page && head == tail then buffer is empty.
418 */
419struct ring_buffer_per_cpu {
420 int cpu;
421 struct ring_buffer *buffer;
Steven Rostedt77ae3652009-03-27 11:00:29 -0400422 spinlock_t reader_lock; /* serialize readers */
Steven Rostedt3e03fb72008-11-06 00:09:43 -0500423 raw_spinlock_t lock;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400424 struct lock_class_key lock_key;
Steven Rostedt3adc54f2009-03-30 15:32:01 -0400425 struct list_head *pages;
Steven Rostedt6f807ac2008-10-04 02:00:58 -0400426 struct buffer_page *head_page; /* read from head */
427 struct buffer_page *tail_page; /* write to tail */
Wenji Huangc3706f02009-02-10 01:03:18 -0500428 struct buffer_page *commit_page; /* committed pages */
Steven Rostedtd7690412008-10-01 00:29:53 -0400429 struct buffer_page *reader_page;
Steven Rostedt77ae3652009-03-27 11:00:29 -0400430 local_t commit_overrun;
431 local_t overrun;
Steven Rostedte4906ef2009-04-30 20:49:44 -0400432 local_t entries;
Steven Rostedtfa743952009-06-16 12:37:57 -0400433 local_t committing;
434 local_t commits;
Steven Rostedt77ae3652009-03-27 11:00:29 -0400435 unsigned long read;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400436 u64 write_stamp;
437 u64 read_stamp;
438 atomic_t record_disabled;
439};
440
441struct ring_buffer {
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400442 unsigned pages;
443 unsigned flags;
444 int cpus;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400445 atomic_t record_disabled;
Arnaldo Carvalho de Melo00f62f62009-02-09 17:04:06 -0200446 cpumask_var_t cpumask;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400447
Peter Zijlstra1f8a6a12009-06-08 18:18:39 +0200448 struct lock_class_key *reader_lock_key;
449
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400450 struct mutex mutex;
451
452 struct ring_buffer_per_cpu **buffers;
Steven Rostedt554f7862009-03-11 22:00:13 -0400453
Steven Rostedt59222ef2009-03-12 11:46:03 -0400454#ifdef CONFIG_HOTPLUG_CPU
Steven Rostedt554f7862009-03-11 22:00:13 -0400455 struct notifier_block cpu_notify;
456#endif
Steven Rostedt37886f62009-03-17 17:22:06 -0400457 u64 (*clock)(void);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400458};
459
460struct ring_buffer_iter {
461 struct ring_buffer_per_cpu *cpu_buffer;
462 unsigned long head;
463 struct buffer_page *head_page;
464 u64 read_stamp;
465};
466
Steven Rostedtf536aaf2008-11-10 23:07:30 -0500467/* buffer may be either ring_buffer or ring_buffer_per_cpu */
Steven Rostedt077c5402009-09-03 19:53:46 -0400468#define RB_WARN_ON(b, cond) \
469 ({ \
470 int _____ret = unlikely(cond); \
471 if (_____ret) { \
472 if (__same_type(*(b), struct ring_buffer_per_cpu)) { \
473 struct ring_buffer_per_cpu *__b = \
474 (void *)b; \
475 atomic_inc(&__b->buffer->record_disabled); \
476 } else \
477 atomic_inc(&b->record_disabled); \
478 WARN_ON(1); \
479 } \
480 _____ret; \
Steven Rostedt3e89c7b2008-11-11 15:28:41 -0500481 })
Steven Rostedtf536aaf2008-11-10 23:07:30 -0500482
Steven Rostedt37886f62009-03-17 17:22:06 -0400483/* Up this if you want to test the TIME_EXTENTS and normalization */
484#define DEBUG_SHIFT 0
485
Jiri Olsa6d3f1e12009-10-23 19:36:19 -0400486static inline u64 rb_time_stamp(struct ring_buffer *buffer)
Steven Rostedt88eb0122009-05-11 16:28:23 -0400487{
488 /* shift to debug/test normalization and TIME_EXTENTS */
489 return buffer->clock() << DEBUG_SHIFT;
490}
491
Steven Rostedt37886f62009-03-17 17:22:06 -0400492u64 ring_buffer_time_stamp(struct ring_buffer *buffer, int cpu)
493{
494 u64 time;
495
496 preempt_disable_notrace();
Jiri Olsa6d3f1e12009-10-23 19:36:19 -0400497 time = rb_time_stamp(buffer);
Steven Rostedt37886f62009-03-17 17:22:06 -0400498 preempt_enable_no_resched_notrace();
499
500 return time;
501}
502EXPORT_SYMBOL_GPL(ring_buffer_time_stamp);
503
504void ring_buffer_normalize_time_stamp(struct ring_buffer *buffer,
505 int cpu, u64 *ts)
506{
507 /* Just stupid testing the normalize function and deltas */
508 *ts >>= DEBUG_SHIFT;
509}
510EXPORT_SYMBOL_GPL(ring_buffer_normalize_time_stamp);
511
Steven Rostedt77ae3652009-03-27 11:00:29 -0400512/*
513 * Making the ring buffer lockless makes things tricky.
514 * Although writes only happen on the CPU that they are on,
515 * and they only need to worry about interrupts. Reads can
516 * happen on any CPU.
517 *
518 * The reader page is always off the ring buffer, but when the
519 * reader finishes with a page, it needs to swap its page with
520 * a new one from the buffer. The reader needs to take from
521 * the head (writes go to the tail). But if a writer is in overwrite
522 * mode and wraps, it must push the head page forward.
523 *
524 * Here lies the problem.
525 *
526 * The reader must be careful to replace only the head page, and
527 * not another one. As described at the top of the file in the
528 * ASCII art, the reader sets its old page to point to the next
529 * page after head. It then sets the page after head to point to
530 * the old reader page. But if the writer moves the head page
531 * during this operation, the reader could end up with the tail.
532 *
533 * We use cmpxchg to help prevent this race. We also do something
534 * special with the page before head. We set the LSB to 1.
535 *
536 * When the writer must push the page forward, it will clear the
537 * bit that points to the head page, move the head, and then set
538 * the bit that points to the new head page.
539 *
540 * We also don't want an interrupt coming in and moving the head
541 * page on another writer. Thus we use the second LSB to catch
542 * that too. Thus:
543 *
544 * head->list->prev->next bit 1 bit 0
545 * ------- -------
546 * Normal page 0 0
547 * Points to head page 0 1
548 * New head page 1 0
549 *
550 * Note we can not trust the prev pointer of the head page, because:
551 *
552 * +----+ +-----+ +-----+
553 * | |------>| T |---X--->| N |
554 * | |<------| | | |
555 * +----+ +-----+ +-----+
556 * ^ ^ |
557 * | +-----+ | |
558 * +----------| R |----------+ |
559 * | |<-----------+
560 * +-----+
561 *
562 * Key: ---X--> HEAD flag set in pointer
563 * T Tail page
564 * R Reader page
565 * N Next page
566 *
567 * (see __rb_reserve_next() to see where this happens)
568 *
569 * What the above shows is that the reader just swapped out
570 * the reader page with a page in the buffer, but before it
571 * could make the new header point back to the new page added
572 * it was preempted by a writer. The writer moved forward onto
573 * the new page added by the reader and is about to move forward
574 * again.
575 *
576 * You can see, it is legitimate for the previous pointer of
577 * the head (or any page) not to point back to itself. But only
578 * temporarially.
579 */
580
581#define RB_PAGE_NORMAL 0UL
582#define RB_PAGE_HEAD 1UL
583#define RB_PAGE_UPDATE 2UL
584
585
586#define RB_FLAG_MASK 3UL
587
588/* PAGE_MOVED is not part of the mask */
589#define RB_PAGE_MOVED 4UL
590
591/*
592 * rb_list_head - remove any bit
593 */
594static struct list_head *rb_list_head(struct list_head *list)
595{
596 unsigned long val = (unsigned long)list;
597
598 return (struct list_head *)(val & ~RB_FLAG_MASK);
599}
600
601/*
Jiri Olsa6d3f1e12009-10-23 19:36:19 -0400602 * rb_is_head_page - test if the given page is the head page
Steven Rostedt77ae3652009-03-27 11:00:29 -0400603 *
604 * Because the reader may move the head_page pointer, we can
605 * not trust what the head page is (it may be pointing to
606 * the reader page). But if the next page is a header page,
607 * its flags will be non zero.
608 */
609static int inline
610rb_is_head_page(struct ring_buffer_per_cpu *cpu_buffer,
611 struct buffer_page *page, struct list_head *list)
612{
613 unsigned long val;
614
615 val = (unsigned long)list->next;
616
617 if ((val & ~RB_FLAG_MASK) != (unsigned long)&page->list)
618 return RB_PAGE_MOVED;
619
620 return val & RB_FLAG_MASK;
621}
622
623/*
624 * rb_is_reader_page
625 *
626 * The unique thing about the reader page, is that, if the
627 * writer is ever on it, the previous pointer never points
628 * back to the reader page.
629 */
630static int rb_is_reader_page(struct buffer_page *page)
631{
632 struct list_head *list = page->list.prev;
633
634 return rb_list_head(list->next) != &page->list;
635}
636
637/*
638 * rb_set_list_to_head - set a list_head to be pointing to head.
639 */
640static void rb_set_list_to_head(struct ring_buffer_per_cpu *cpu_buffer,
641 struct list_head *list)
642{
643 unsigned long *ptr;
644
645 ptr = (unsigned long *)&list->next;
646 *ptr |= RB_PAGE_HEAD;
647 *ptr &= ~RB_PAGE_UPDATE;
648}
649
650/*
651 * rb_head_page_activate - sets up head page
652 */
653static void rb_head_page_activate(struct ring_buffer_per_cpu *cpu_buffer)
654{
655 struct buffer_page *head;
656
657 head = cpu_buffer->head_page;
658 if (!head)
659 return;
660
661 /*
662 * Set the previous list pointer to have the HEAD flag.
663 */
664 rb_set_list_to_head(cpu_buffer, head->list.prev);
665}
666
667static void rb_list_head_clear(struct list_head *list)
668{
669 unsigned long *ptr = (unsigned long *)&list->next;
670
671 *ptr &= ~RB_FLAG_MASK;
672}
673
674/*
675 * rb_head_page_dactivate - clears head page ptr (for free list)
676 */
677static void
678rb_head_page_deactivate(struct ring_buffer_per_cpu *cpu_buffer)
679{
680 struct list_head *hd;
681
682 /* Go through the whole list and clear any pointers found. */
683 rb_list_head_clear(cpu_buffer->pages);
684
685 list_for_each(hd, cpu_buffer->pages)
686 rb_list_head_clear(hd);
687}
688
689static int rb_head_page_set(struct ring_buffer_per_cpu *cpu_buffer,
690 struct buffer_page *head,
691 struct buffer_page *prev,
692 int old_flag, int new_flag)
693{
694 struct list_head *list;
695 unsigned long val = (unsigned long)&head->list;
696 unsigned long ret;
697
698 list = &prev->list;
699
700 val &= ~RB_FLAG_MASK;
701
Steven Rostedt08a40812009-09-14 09:31:35 -0400702 ret = cmpxchg((unsigned long *)&list->next,
703 val | old_flag, val | new_flag);
Steven Rostedt77ae3652009-03-27 11:00:29 -0400704
705 /* check if the reader took the page */
706 if ((ret & ~RB_FLAG_MASK) != val)
707 return RB_PAGE_MOVED;
708
709 return ret & RB_FLAG_MASK;
710}
711
712static int rb_head_page_set_update(struct ring_buffer_per_cpu *cpu_buffer,
713 struct buffer_page *head,
714 struct buffer_page *prev,
715 int old_flag)
716{
717 return rb_head_page_set(cpu_buffer, head, prev,
718 old_flag, RB_PAGE_UPDATE);
719}
720
721static int rb_head_page_set_head(struct ring_buffer_per_cpu *cpu_buffer,
722 struct buffer_page *head,
723 struct buffer_page *prev,
724 int old_flag)
725{
726 return rb_head_page_set(cpu_buffer, head, prev,
727 old_flag, RB_PAGE_HEAD);
728}
729
730static int rb_head_page_set_normal(struct ring_buffer_per_cpu *cpu_buffer,
731 struct buffer_page *head,
732 struct buffer_page *prev,
733 int old_flag)
734{
735 return rb_head_page_set(cpu_buffer, head, prev,
736 old_flag, RB_PAGE_NORMAL);
737}
738
739static inline void rb_inc_page(struct ring_buffer_per_cpu *cpu_buffer,
740 struct buffer_page **bpage)
741{
742 struct list_head *p = rb_list_head((*bpage)->list.next);
743
744 *bpage = list_entry(p, struct buffer_page, list);
745}
746
747static struct buffer_page *
748rb_set_head_page(struct ring_buffer_per_cpu *cpu_buffer)
749{
750 struct buffer_page *head;
751 struct buffer_page *page;
752 struct list_head *list;
753 int i;
754
755 if (RB_WARN_ON(cpu_buffer, !cpu_buffer->head_page))
756 return NULL;
757
758 /* sanity check */
759 list = cpu_buffer->pages;
760 if (RB_WARN_ON(cpu_buffer, rb_list_head(list->prev->next) != list))
761 return NULL;
762
763 page = head = cpu_buffer->head_page;
764 /*
765 * It is possible that the writer moves the header behind
766 * where we started, and we miss in one loop.
767 * A second loop should grab the header, but we'll do
768 * three loops just because I'm paranoid.
769 */
770 for (i = 0; i < 3; i++) {
771 do {
772 if (rb_is_head_page(cpu_buffer, page, page->list.prev)) {
773 cpu_buffer->head_page = page;
774 return page;
775 }
776 rb_inc_page(cpu_buffer, &page);
777 } while (page != head);
778 }
779
780 RB_WARN_ON(cpu_buffer, 1);
781
782 return NULL;
783}
784
785static int rb_head_page_replace(struct buffer_page *old,
786 struct buffer_page *new)
787{
788 unsigned long *ptr = (unsigned long *)&old->list.prev->next;
789 unsigned long val;
790 unsigned long ret;
791
792 val = *ptr & ~RB_FLAG_MASK;
793 val |= RB_PAGE_HEAD;
794
Steven Rostedt08a40812009-09-14 09:31:35 -0400795 ret = cmpxchg(ptr, val, (unsigned long)&new->list);
Steven Rostedt77ae3652009-03-27 11:00:29 -0400796
797 return ret == val;
798}
799
800/*
801 * rb_tail_page_update - move the tail page forward
802 *
803 * Returns 1 if moved tail page, 0 if someone else did.
804 */
805static int rb_tail_page_update(struct ring_buffer_per_cpu *cpu_buffer,
806 struct buffer_page *tail_page,
807 struct buffer_page *next_page)
808{
809 struct buffer_page *old_tail;
810 unsigned long old_entries;
811 unsigned long old_write;
812 int ret = 0;
813
814 /*
815 * The tail page now needs to be moved forward.
816 *
817 * We need to reset the tail page, but without messing
818 * with possible erasing of data brought in by interrupts
819 * that have moved the tail page and are currently on it.
820 *
821 * We add a counter to the write field to denote this.
822 */
823 old_write = local_add_return(RB_WRITE_INTCNT, &next_page->write);
824 old_entries = local_add_return(RB_WRITE_INTCNT, &next_page->entries);
825
826 /*
827 * Just make sure we have seen our old_write and synchronize
828 * with any interrupts that come in.
829 */
830 barrier();
831
832 /*
833 * If the tail page is still the same as what we think
834 * it is, then it is up to us to update the tail
835 * pointer.
836 */
837 if (tail_page == cpu_buffer->tail_page) {
838 /* Zero the write counter */
839 unsigned long val = old_write & ~RB_WRITE_MASK;
840 unsigned long eval = old_entries & ~RB_WRITE_MASK;
841
842 /*
843 * This will only succeed if an interrupt did
844 * not come in and change it. In which case, we
845 * do not want to modify it.
Lai Jiangshanda706d82009-07-15 16:27:30 +0800846 *
847 * We add (void) to let the compiler know that we do not care
848 * about the return value of these functions. We use the
849 * cmpxchg to only update if an interrupt did not already
850 * do it for us. If the cmpxchg fails, we don't care.
Steven Rostedt77ae3652009-03-27 11:00:29 -0400851 */
Lai Jiangshanda706d82009-07-15 16:27:30 +0800852 (void)local_cmpxchg(&next_page->write, old_write, val);
853 (void)local_cmpxchg(&next_page->entries, old_entries, eval);
Steven Rostedt77ae3652009-03-27 11:00:29 -0400854
855 /*
856 * No need to worry about races with clearing out the commit.
857 * it only can increment when a commit takes place. But that
858 * only happens in the outer most nested commit.
859 */
860 local_set(&next_page->page->commit, 0);
861
862 old_tail = cmpxchg(&cpu_buffer->tail_page,
863 tail_page, next_page);
864
865 if (old_tail == tail_page)
866 ret = 1;
867 }
868
869 return ret;
870}
871
872static int rb_check_bpage(struct ring_buffer_per_cpu *cpu_buffer,
873 struct buffer_page *bpage)
874{
875 unsigned long val = (unsigned long)bpage;
876
877 if (RB_WARN_ON(cpu_buffer, val & RB_FLAG_MASK))
878 return 1;
879
880 return 0;
881}
882
883/**
884 * rb_check_list - make sure a pointer to a list has the last bits zero
885 */
886static int rb_check_list(struct ring_buffer_per_cpu *cpu_buffer,
887 struct list_head *list)
888{
889 if (RB_WARN_ON(cpu_buffer, rb_list_head(list->prev) != list->prev))
890 return 1;
891 if (RB_WARN_ON(cpu_buffer, rb_list_head(list->next) != list->next))
892 return 1;
893 return 0;
894}
895
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400896/**
897 * check_pages - integrity check of buffer pages
898 * @cpu_buffer: CPU buffer with pages to test
899 *
Wenji Huangc3706f02009-02-10 01:03:18 -0500900 * As a safety measure we check to make sure the data pages have not
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400901 * been corrupted.
902 */
903static int rb_check_pages(struct ring_buffer_per_cpu *cpu_buffer)
904{
Steven Rostedt3adc54f2009-03-30 15:32:01 -0400905 struct list_head *head = cpu_buffer->pages;
Steven Rostedt044fa782008-12-02 23:50:03 -0500906 struct buffer_page *bpage, *tmp;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400907
Steven Rostedt77ae3652009-03-27 11:00:29 -0400908 rb_head_page_deactivate(cpu_buffer);
909
Steven Rostedt3e89c7b2008-11-11 15:28:41 -0500910 if (RB_WARN_ON(cpu_buffer, head->next->prev != head))
911 return -1;
912 if (RB_WARN_ON(cpu_buffer, head->prev->next != head))
913 return -1;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400914
Steven Rostedt77ae3652009-03-27 11:00:29 -0400915 if (rb_check_list(cpu_buffer, head))
916 return -1;
917
Steven Rostedt044fa782008-12-02 23:50:03 -0500918 list_for_each_entry_safe(bpage, tmp, head, list) {
Steven Rostedt3e89c7b2008-11-11 15:28:41 -0500919 if (RB_WARN_ON(cpu_buffer,
Steven Rostedt044fa782008-12-02 23:50:03 -0500920 bpage->list.next->prev != &bpage->list))
Steven Rostedt3e89c7b2008-11-11 15:28:41 -0500921 return -1;
922 if (RB_WARN_ON(cpu_buffer,
Steven Rostedt044fa782008-12-02 23:50:03 -0500923 bpage->list.prev->next != &bpage->list))
Steven Rostedt3e89c7b2008-11-11 15:28:41 -0500924 return -1;
Steven Rostedt77ae3652009-03-27 11:00:29 -0400925 if (rb_check_list(cpu_buffer, &bpage->list))
926 return -1;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400927 }
928
Steven Rostedt77ae3652009-03-27 11:00:29 -0400929 rb_head_page_activate(cpu_buffer);
930
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400931 return 0;
932}
933
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400934static int rb_allocate_pages(struct ring_buffer_per_cpu *cpu_buffer,
935 unsigned nr_pages)
936{
Steven Rostedt044fa782008-12-02 23:50:03 -0500937 struct buffer_page *bpage, *tmp;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400938 unsigned long addr;
939 LIST_HEAD(pages);
940 unsigned i;
941
Steven Rostedt3adc54f2009-03-30 15:32:01 -0400942 WARN_ON(!nr_pages);
943
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400944 for (i = 0; i < nr_pages; i++) {
Steven Rostedt044fa782008-12-02 23:50:03 -0500945 bpage = kzalloc_node(ALIGN(sizeof(*bpage), cache_line_size()),
Steven Rostedtaa1e0e32008-10-02 19:18:09 -0400946 GFP_KERNEL, cpu_to_node(cpu_buffer->cpu));
Steven Rostedt044fa782008-12-02 23:50:03 -0500947 if (!bpage)
Steven Rostedte4c2ce82008-10-01 11:14:54 -0400948 goto free_pages;
Steven Rostedt77ae3652009-03-27 11:00:29 -0400949
950 rb_check_bpage(cpu_buffer, bpage);
951
Steven Rostedt044fa782008-12-02 23:50:03 -0500952 list_add(&bpage->list, &pages);
Steven Rostedte4c2ce82008-10-01 11:14:54 -0400953
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400954 addr = __get_free_page(GFP_KERNEL);
955 if (!addr)
956 goto free_pages;
Steven Rostedt044fa782008-12-02 23:50:03 -0500957 bpage->page = (void *)addr;
958 rb_init_page(bpage->page);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400959 }
960
Steven Rostedt3adc54f2009-03-30 15:32:01 -0400961 /*
962 * The ring buffer page list is a circular list that does not
963 * start and end with a list head. All page list items point to
964 * other pages.
965 */
966 cpu_buffer->pages = pages.next;
967 list_del(&pages);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400968
969 rb_check_pages(cpu_buffer);
970
971 return 0;
972
973 free_pages:
Steven Rostedt044fa782008-12-02 23:50:03 -0500974 list_for_each_entry_safe(bpage, tmp, &pages, list) {
975 list_del_init(&bpage->list);
976 free_buffer_page(bpage);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400977 }
978 return -ENOMEM;
979}
980
981static struct ring_buffer_per_cpu *
982rb_allocate_cpu_buffer(struct ring_buffer *buffer, int cpu)
983{
984 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedt044fa782008-12-02 23:50:03 -0500985 struct buffer_page *bpage;
Steven Rostedtd7690412008-10-01 00:29:53 -0400986 unsigned long addr;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400987 int ret;
988
989 cpu_buffer = kzalloc_node(ALIGN(sizeof(*cpu_buffer), cache_line_size()),
990 GFP_KERNEL, cpu_to_node(cpu));
991 if (!cpu_buffer)
992 return NULL;
993
994 cpu_buffer->cpu = cpu;
995 cpu_buffer->buffer = buffer;
Steven Rostedtf83c9d02008-11-11 18:47:44 +0100996 spin_lock_init(&cpu_buffer->reader_lock);
Peter Zijlstra1f8a6a12009-06-08 18:18:39 +0200997 lockdep_set_class(&cpu_buffer->reader_lock, buffer->reader_lock_key);
Steven Rostedt3e03fb72008-11-06 00:09:43 -0500998 cpu_buffer->lock = (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400999
Steven Rostedt044fa782008-12-02 23:50:03 -05001000 bpage = kzalloc_node(ALIGN(sizeof(*bpage), cache_line_size()),
Steven Rostedte4c2ce82008-10-01 11:14:54 -04001001 GFP_KERNEL, cpu_to_node(cpu));
Steven Rostedt044fa782008-12-02 23:50:03 -05001002 if (!bpage)
Steven Rostedte4c2ce82008-10-01 11:14:54 -04001003 goto fail_free_buffer;
1004
Steven Rostedt77ae3652009-03-27 11:00:29 -04001005 rb_check_bpage(cpu_buffer, bpage);
1006
Steven Rostedt044fa782008-12-02 23:50:03 -05001007 cpu_buffer->reader_page = bpage;
Steven Rostedtd7690412008-10-01 00:29:53 -04001008 addr = __get_free_page(GFP_KERNEL);
1009 if (!addr)
Steven Rostedte4c2ce82008-10-01 11:14:54 -04001010 goto fail_free_reader;
Steven Rostedt044fa782008-12-02 23:50:03 -05001011 bpage->page = (void *)addr;
1012 rb_init_page(bpage->page);
Steven Rostedte4c2ce82008-10-01 11:14:54 -04001013
Steven Rostedtd7690412008-10-01 00:29:53 -04001014 INIT_LIST_HEAD(&cpu_buffer->reader_page->list);
Steven Rostedtd7690412008-10-01 00:29:53 -04001015
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001016 ret = rb_allocate_pages(cpu_buffer, buffer->pages);
1017 if (ret < 0)
Steven Rostedtd7690412008-10-01 00:29:53 -04001018 goto fail_free_reader;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001019
1020 cpu_buffer->head_page
Steven Rostedt3adc54f2009-03-30 15:32:01 -04001021 = list_entry(cpu_buffer->pages, struct buffer_page, list);
Steven Rostedtbf41a152008-10-04 02:00:59 -04001022 cpu_buffer->tail_page = cpu_buffer->commit_page = cpu_buffer->head_page;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001023
Steven Rostedt77ae3652009-03-27 11:00:29 -04001024 rb_head_page_activate(cpu_buffer);
1025
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001026 return cpu_buffer;
1027
Steven Rostedtd7690412008-10-01 00:29:53 -04001028 fail_free_reader:
1029 free_buffer_page(cpu_buffer->reader_page);
1030
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001031 fail_free_buffer:
1032 kfree(cpu_buffer);
1033 return NULL;
1034}
1035
1036static void rb_free_cpu_buffer(struct ring_buffer_per_cpu *cpu_buffer)
1037{
Steven Rostedt3adc54f2009-03-30 15:32:01 -04001038 struct list_head *head = cpu_buffer->pages;
Steven Rostedt044fa782008-12-02 23:50:03 -05001039 struct buffer_page *bpage, *tmp;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001040
Steven Rostedtd7690412008-10-01 00:29:53 -04001041 free_buffer_page(cpu_buffer->reader_page);
1042
Steven Rostedt77ae3652009-03-27 11:00:29 -04001043 rb_head_page_deactivate(cpu_buffer);
1044
Steven Rostedt3adc54f2009-03-30 15:32:01 -04001045 if (head) {
1046 list_for_each_entry_safe(bpage, tmp, head, list) {
1047 list_del_init(&bpage->list);
1048 free_buffer_page(bpage);
1049 }
1050 bpage = list_entry(head, struct buffer_page, list);
Steven Rostedt044fa782008-12-02 23:50:03 -05001051 free_buffer_page(bpage);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001052 }
Steven Rostedt3adc54f2009-03-30 15:32:01 -04001053
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001054 kfree(cpu_buffer);
1055}
1056
Steven Rostedt59222ef2009-03-12 11:46:03 -04001057#ifdef CONFIG_HOTPLUG_CPU
Frederic Weisbecker09c9e842009-03-21 04:33:36 +01001058static int rb_cpu_notify(struct notifier_block *self,
1059 unsigned long action, void *hcpu);
Steven Rostedt554f7862009-03-11 22:00:13 -04001060#endif
1061
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001062/**
1063 * ring_buffer_alloc - allocate a new ring_buffer
Robert Richter68814b52008-11-24 12:24:12 +01001064 * @size: the size in bytes per cpu that is needed.
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001065 * @flags: attributes to set for the ring buffer.
1066 *
1067 * Currently the only flag that is available is the RB_FL_OVERWRITE
1068 * flag. This flag means that the buffer will overwrite old data
1069 * when the buffer wraps. If this flag is not set, the buffer will
1070 * drop data when the tail hits the head.
1071 */
Peter Zijlstra1f8a6a12009-06-08 18:18:39 +02001072struct ring_buffer *__ring_buffer_alloc(unsigned long size, unsigned flags,
1073 struct lock_class_key *key)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001074{
1075 struct ring_buffer *buffer;
1076 int bsize;
1077 int cpu;
1078
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001079 /* keep it in its own cache line */
1080 buffer = kzalloc(ALIGN(sizeof(*buffer), cache_line_size()),
1081 GFP_KERNEL);
1082 if (!buffer)
1083 return NULL;
1084
Rusty Russell9e01c1b2009-01-01 10:12:22 +10301085 if (!alloc_cpumask_var(&buffer->cpumask, GFP_KERNEL))
1086 goto fail_free_buffer;
1087
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001088 buffer->pages = DIV_ROUND_UP(size, BUF_PAGE_SIZE);
1089 buffer->flags = flags;
Steven Rostedt37886f62009-03-17 17:22:06 -04001090 buffer->clock = trace_clock_local;
Peter Zijlstra1f8a6a12009-06-08 18:18:39 +02001091 buffer->reader_lock_key = key;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001092
1093 /* need at least two pages */
Steven Rostedt5f78abe2009-06-17 14:11:10 -04001094 if (buffer->pages < 2)
1095 buffer->pages = 2;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001096
Frederic Weisbecker3bf832c2009-03-19 14:47:33 +01001097 /*
1098 * In case of non-hotplug cpu, if the ring-buffer is allocated
1099 * in early initcall, it will not be notified of secondary cpus.
1100 * In that off case, we need to allocate for all possible cpus.
1101 */
1102#ifdef CONFIG_HOTPLUG_CPU
Steven Rostedt554f7862009-03-11 22:00:13 -04001103 get_online_cpus();
1104 cpumask_copy(buffer->cpumask, cpu_online_mask);
Frederic Weisbecker3bf832c2009-03-19 14:47:33 +01001105#else
1106 cpumask_copy(buffer->cpumask, cpu_possible_mask);
1107#endif
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001108 buffer->cpus = nr_cpu_ids;
1109
1110 bsize = sizeof(void *) * nr_cpu_ids;
1111 buffer->buffers = kzalloc(ALIGN(bsize, cache_line_size()),
1112 GFP_KERNEL);
1113 if (!buffer->buffers)
Rusty Russell9e01c1b2009-01-01 10:12:22 +10301114 goto fail_free_cpumask;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001115
1116 for_each_buffer_cpu(buffer, cpu) {
1117 buffer->buffers[cpu] =
1118 rb_allocate_cpu_buffer(buffer, cpu);
1119 if (!buffer->buffers[cpu])
1120 goto fail_free_buffers;
1121 }
1122
Steven Rostedt59222ef2009-03-12 11:46:03 -04001123#ifdef CONFIG_HOTPLUG_CPU
Steven Rostedt554f7862009-03-11 22:00:13 -04001124 buffer->cpu_notify.notifier_call = rb_cpu_notify;
1125 buffer->cpu_notify.priority = 0;
1126 register_cpu_notifier(&buffer->cpu_notify);
1127#endif
1128
1129 put_online_cpus();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001130 mutex_init(&buffer->mutex);
1131
1132 return buffer;
1133
1134 fail_free_buffers:
1135 for_each_buffer_cpu(buffer, cpu) {
1136 if (buffer->buffers[cpu])
1137 rb_free_cpu_buffer(buffer->buffers[cpu]);
1138 }
1139 kfree(buffer->buffers);
1140
Rusty Russell9e01c1b2009-01-01 10:12:22 +10301141 fail_free_cpumask:
1142 free_cpumask_var(buffer->cpumask);
Steven Rostedt554f7862009-03-11 22:00:13 -04001143 put_online_cpus();
Rusty Russell9e01c1b2009-01-01 10:12:22 +10301144
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001145 fail_free_buffer:
1146 kfree(buffer);
1147 return NULL;
1148}
Peter Zijlstra1f8a6a12009-06-08 18:18:39 +02001149EXPORT_SYMBOL_GPL(__ring_buffer_alloc);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001150
1151/**
1152 * ring_buffer_free - free a ring buffer.
1153 * @buffer: the buffer to free.
1154 */
1155void
1156ring_buffer_free(struct ring_buffer *buffer)
1157{
1158 int cpu;
1159
Steven Rostedt554f7862009-03-11 22:00:13 -04001160 get_online_cpus();
1161
Steven Rostedt59222ef2009-03-12 11:46:03 -04001162#ifdef CONFIG_HOTPLUG_CPU
Steven Rostedt554f7862009-03-11 22:00:13 -04001163 unregister_cpu_notifier(&buffer->cpu_notify);
1164#endif
1165
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001166 for_each_buffer_cpu(buffer, cpu)
1167 rb_free_cpu_buffer(buffer->buffers[cpu]);
1168
Steven Rostedt554f7862009-03-11 22:00:13 -04001169 put_online_cpus();
1170
Eric Dumazetbd3f0222009-08-07 12:49:29 +02001171 kfree(buffer->buffers);
Rusty Russell9e01c1b2009-01-01 10:12:22 +10301172 free_cpumask_var(buffer->cpumask);
1173
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001174 kfree(buffer);
1175}
Robert Richterc4f50182008-12-11 16:49:22 +01001176EXPORT_SYMBOL_GPL(ring_buffer_free);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001177
Steven Rostedt37886f62009-03-17 17:22:06 -04001178void ring_buffer_set_clock(struct ring_buffer *buffer,
1179 u64 (*clock)(void))
1180{
1181 buffer->clock = clock;
1182}
1183
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001184static void rb_reset_cpu(struct ring_buffer_per_cpu *cpu_buffer);
1185
1186static void
1187rb_remove_pages(struct ring_buffer_per_cpu *cpu_buffer, unsigned nr_pages)
1188{
Steven Rostedt044fa782008-12-02 23:50:03 -05001189 struct buffer_page *bpage;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001190 struct list_head *p;
1191 unsigned i;
1192
1193 atomic_inc(&cpu_buffer->record_disabled);
1194 synchronize_sched();
1195
Steven Rostedt77ae3652009-03-27 11:00:29 -04001196 rb_head_page_deactivate(cpu_buffer);
1197
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001198 for (i = 0; i < nr_pages; i++) {
Steven Rostedt3adc54f2009-03-30 15:32:01 -04001199 if (RB_WARN_ON(cpu_buffer, list_empty(cpu_buffer->pages)))
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05001200 return;
Steven Rostedt3adc54f2009-03-30 15:32:01 -04001201 p = cpu_buffer->pages->next;
Steven Rostedt044fa782008-12-02 23:50:03 -05001202 bpage = list_entry(p, struct buffer_page, list);
1203 list_del_init(&bpage->list);
1204 free_buffer_page(bpage);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001205 }
Steven Rostedt3adc54f2009-03-30 15:32:01 -04001206 if (RB_WARN_ON(cpu_buffer, list_empty(cpu_buffer->pages)))
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05001207 return;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001208
1209 rb_reset_cpu(cpu_buffer);
1210
1211 rb_check_pages(cpu_buffer);
1212
1213 atomic_dec(&cpu_buffer->record_disabled);
1214
1215}
1216
1217static void
1218rb_insert_pages(struct ring_buffer_per_cpu *cpu_buffer,
1219 struct list_head *pages, unsigned nr_pages)
1220{
Steven Rostedt044fa782008-12-02 23:50:03 -05001221 struct buffer_page *bpage;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001222 struct list_head *p;
1223 unsigned i;
1224
1225 atomic_inc(&cpu_buffer->record_disabled);
1226 synchronize_sched();
1227
Steven Rostedt77ae3652009-03-27 11:00:29 -04001228 spin_lock_irq(&cpu_buffer->reader_lock);
1229 rb_head_page_deactivate(cpu_buffer);
1230
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001231 for (i = 0; i < nr_pages; i++) {
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05001232 if (RB_WARN_ON(cpu_buffer, list_empty(pages)))
1233 return;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001234 p = pages->next;
Steven Rostedt044fa782008-12-02 23:50:03 -05001235 bpage = list_entry(p, struct buffer_page, list);
1236 list_del_init(&bpage->list);
Steven Rostedt3adc54f2009-03-30 15:32:01 -04001237 list_add_tail(&bpage->list, cpu_buffer->pages);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001238 }
1239 rb_reset_cpu(cpu_buffer);
Steven Rostedt77ae3652009-03-27 11:00:29 -04001240 spin_unlock_irq(&cpu_buffer->reader_lock);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001241
1242 rb_check_pages(cpu_buffer);
1243
1244 atomic_dec(&cpu_buffer->record_disabled);
1245}
1246
1247/**
1248 * ring_buffer_resize - resize the ring buffer
1249 * @buffer: the buffer to resize.
1250 * @size: the new size.
1251 *
1252 * The tracer is responsible for making sure that the buffer is
1253 * not being used while changing the size.
1254 * Note: We may be able to change the above requirement by using
1255 * RCU synchronizations.
1256 *
1257 * Minimum size is 2 * BUF_PAGE_SIZE.
1258 *
1259 * Returns -1 on failure.
1260 */
1261int ring_buffer_resize(struct ring_buffer *buffer, unsigned long size)
1262{
1263 struct ring_buffer_per_cpu *cpu_buffer;
1264 unsigned nr_pages, rm_pages, new_pages;
Steven Rostedt044fa782008-12-02 23:50:03 -05001265 struct buffer_page *bpage, *tmp;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001266 unsigned long buffer_size;
1267 unsigned long addr;
1268 LIST_HEAD(pages);
1269 int i, cpu;
1270
Ingo Molnaree51a1d2008-11-13 14:58:31 +01001271 /*
1272 * Always succeed at resizing a non-existent buffer:
1273 */
1274 if (!buffer)
1275 return size;
1276
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001277 size = DIV_ROUND_UP(size, BUF_PAGE_SIZE);
1278 size *= BUF_PAGE_SIZE;
1279 buffer_size = buffer->pages * BUF_PAGE_SIZE;
1280
1281 /* we need a minimum of two pages */
1282 if (size < BUF_PAGE_SIZE * 2)
1283 size = BUF_PAGE_SIZE * 2;
1284
1285 if (size == buffer_size)
1286 return size;
1287
1288 mutex_lock(&buffer->mutex);
Steven Rostedt554f7862009-03-11 22:00:13 -04001289 get_online_cpus();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001290
1291 nr_pages = DIV_ROUND_UP(size, BUF_PAGE_SIZE);
1292
1293 if (size < buffer_size) {
1294
1295 /* easy case, just free pages */
Steven Rostedt554f7862009-03-11 22:00:13 -04001296 if (RB_WARN_ON(buffer, nr_pages >= buffer->pages))
1297 goto out_fail;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001298
1299 rm_pages = buffer->pages - nr_pages;
1300
1301 for_each_buffer_cpu(buffer, cpu) {
1302 cpu_buffer = buffer->buffers[cpu];
1303 rb_remove_pages(cpu_buffer, rm_pages);
1304 }
1305 goto out;
1306 }
1307
1308 /*
1309 * This is a bit more difficult. We only want to add pages
1310 * when we can allocate enough for all CPUs. We do this
1311 * by allocating all the pages and storing them on a local
1312 * link list. If we succeed in our allocation, then we
1313 * add these pages to the cpu_buffers. Otherwise we just free
1314 * them all and return -ENOMEM;
1315 */
Steven Rostedt554f7862009-03-11 22:00:13 -04001316 if (RB_WARN_ON(buffer, nr_pages <= buffer->pages))
1317 goto out_fail;
Steven Rostedtf536aaf2008-11-10 23:07:30 -05001318
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001319 new_pages = nr_pages - buffer->pages;
1320
1321 for_each_buffer_cpu(buffer, cpu) {
1322 for (i = 0; i < new_pages; i++) {
Steven Rostedt044fa782008-12-02 23:50:03 -05001323 bpage = kzalloc_node(ALIGN(sizeof(*bpage),
Steven Rostedte4c2ce82008-10-01 11:14:54 -04001324 cache_line_size()),
1325 GFP_KERNEL, cpu_to_node(cpu));
Steven Rostedt044fa782008-12-02 23:50:03 -05001326 if (!bpage)
Steven Rostedte4c2ce82008-10-01 11:14:54 -04001327 goto free_pages;
Steven Rostedt044fa782008-12-02 23:50:03 -05001328 list_add(&bpage->list, &pages);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001329 addr = __get_free_page(GFP_KERNEL);
1330 if (!addr)
1331 goto free_pages;
Steven Rostedt044fa782008-12-02 23:50:03 -05001332 bpage->page = (void *)addr;
1333 rb_init_page(bpage->page);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001334 }
1335 }
1336
1337 for_each_buffer_cpu(buffer, cpu) {
1338 cpu_buffer = buffer->buffers[cpu];
1339 rb_insert_pages(cpu_buffer, &pages, new_pages);
1340 }
1341
Steven Rostedt554f7862009-03-11 22:00:13 -04001342 if (RB_WARN_ON(buffer, !list_empty(&pages)))
1343 goto out_fail;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001344
1345 out:
1346 buffer->pages = nr_pages;
Steven Rostedt554f7862009-03-11 22:00:13 -04001347 put_online_cpus();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001348 mutex_unlock(&buffer->mutex);
1349
1350 return size;
1351
1352 free_pages:
Steven Rostedt044fa782008-12-02 23:50:03 -05001353 list_for_each_entry_safe(bpage, tmp, &pages, list) {
1354 list_del_init(&bpage->list);
1355 free_buffer_page(bpage);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001356 }
Steven Rostedt554f7862009-03-11 22:00:13 -04001357 put_online_cpus();
Vegard Nossum641d2f62008-11-18 19:22:13 +01001358 mutex_unlock(&buffer->mutex);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001359 return -ENOMEM;
Steven Rostedt554f7862009-03-11 22:00:13 -04001360
1361 /*
1362 * Something went totally wrong, and we are too paranoid
1363 * to even clean up the mess.
1364 */
1365 out_fail:
1366 put_online_cpus();
1367 mutex_unlock(&buffer->mutex);
1368 return -1;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001369}
Robert Richterc4f50182008-12-11 16:49:22 +01001370EXPORT_SYMBOL_GPL(ring_buffer_resize);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001371
Steven Rostedt8789a9e2008-12-02 15:34:07 -05001372static inline void *
Steven Rostedt044fa782008-12-02 23:50:03 -05001373__rb_data_page_index(struct buffer_data_page *bpage, unsigned index)
Steven Rostedt8789a9e2008-12-02 15:34:07 -05001374{
Steven Rostedt044fa782008-12-02 23:50:03 -05001375 return bpage->data + index;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05001376}
1377
Steven Rostedt044fa782008-12-02 23:50:03 -05001378static inline void *__rb_page_index(struct buffer_page *bpage, unsigned index)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001379{
Steven Rostedt044fa782008-12-02 23:50:03 -05001380 return bpage->page->data + index;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001381}
1382
1383static inline struct ring_buffer_event *
Steven Rostedtd7690412008-10-01 00:29:53 -04001384rb_reader_event(struct ring_buffer_per_cpu *cpu_buffer)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001385{
Steven Rostedt6f807ac2008-10-04 02:00:58 -04001386 return __rb_page_index(cpu_buffer->reader_page,
1387 cpu_buffer->reader_page->read);
1388}
1389
1390static inline struct ring_buffer_event *
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001391rb_iter_head_event(struct ring_buffer_iter *iter)
1392{
Steven Rostedt6f807ac2008-10-04 02:00:58 -04001393 return __rb_page_index(iter->head_page, iter->head);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001394}
1395
Steven Rostedt77ae3652009-03-27 11:00:29 -04001396static inline unsigned long rb_page_write(struct buffer_page *bpage)
Steven Rostedtbf41a152008-10-04 02:00:59 -04001397{
Steven Rostedt77ae3652009-03-27 11:00:29 -04001398 return local_read(&bpage->write) & RB_WRITE_MASK;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001399}
1400
1401static inline unsigned rb_page_commit(struct buffer_page *bpage)
1402{
Steven Rostedtabc9b562008-12-02 15:34:06 -05001403 return local_read(&bpage->page->commit);
Steven Rostedtbf41a152008-10-04 02:00:59 -04001404}
1405
Steven Rostedt77ae3652009-03-27 11:00:29 -04001406static inline unsigned long rb_page_entries(struct buffer_page *bpage)
1407{
1408 return local_read(&bpage->entries) & RB_WRITE_MASK;
1409}
1410
Steven Rostedtbf41a152008-10-04 02:00:59 -04001411/* Size is determined by what has been commited */
1412static inline unsigned rb_page_size(struct buffer_page *bpage)
1413{
1414 return rb_page_commit(bpage);
1415}
1416
1417static inline unsigned
1418rb_commit_index(struct ring_buffer_per_cpu *cpu_buffer)
1419{
1420 return rb_page_commit(cpu_buffer->commit_page);
1421}
1422
Steven Rostedtbf41a152008-10-04 02:00:59 -04001423static inline unsigned
1424rb_event_index(struct ring_buffer_event *event)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001425{
Steven Rostedtbf41a152008-10-04 02:00:59 -04001426 unsigned long addr = (unsigned long)event;
1427
Steven Rostedt22f470f2009-06-11 09:29:58 -04001428 return (addr & ~PAGE_MASK) - BUF_PAGE_HDR_SIZE;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001429}
1430
Steven Rostedt0f0c85f2009-05-11 16:08:00 -04001431static inline int
Steven Rostedtfa743952009-06-16 12:37:57 -04001432rb_event_is_commit(struct ring_buffer_per_cpu *cpu_buffer,
1433 struct ring_buffer_event *event)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001434{
Steven Rostedtbf41a152008-10-04 02:00:59 -04001435 unsigned long addr = (unsigned long)event;
1436 unsigned long index;
1437
1438 index = rb_event_index(event);
1439 addr &= PAGE_MASK;
1440
1441 return cpu_buffer->commit_page->page == (void *)addr &&
1442 rb_commit_index(cpu_buffer) == index;
1443}
1444
Andrew Morton34a148b2009-01-09 12:27:09 -08001445static void
Steven Rostedtbf41a152008-10-04 02:00:59 -04001446rb_set_commit_to_write(struct ring_buffer_per_cpu *cpu_buffer)
1447{
Steven Rostedt77ae3652009-03-27 11:00:29 -04001448 unsigned long max_count;
1449
Steven Rostedtbf41a152008-10-04 02:00:59 -04001450 /*
1451 * We only race with interrupts and NMIs on this CPU.
1452 * If we own the commit event, then we can commit
1453 * all others that interrupted us, since the interruptions
1454 * are in stack format (they finish before they come
1455 * back to us). This allows us to do a simple loop to
1456 * assign the commit to the tail.
1457 */
Steven Rostedta8ccf1d2008-12-23 11:32:24 -05001458 again:
Steven Rostedt77ae3652009-03-27 11:00:29 -04001459 max_count = cpu_buffer->buffer->pages * 100;
1460
Steven Rostedtbf41a152008-10-04 02:00:59 -04001461 while (cpu_buffer->commit_page != cpu_buffer->tail_page) {
Steven Rostedt77ae3652009-03-27 11:00:29 -04001462 if (RB_WARN_ON(cpu_buffer, !(--max_count)))
1463 return;
1464 if (RB_WARN_ON(cpu_buffer,
1465 rb_is_reader_page(cpu_buffer->tail_page)))
1466 return;
1467 local_set(&cpu_buffer->commit_page->page->commit,
1468 rb_page_write(cpu_buffer->commit_page));
Steven Rostedtbf41a152008-10-04 02:00:59 -04001469 rb_inc_page(cpu_buffer, &cpu_buffer->commit_page);
Steven Rostedtabc9b562008-12-02 15:34:06 -05001470 cpu_buffer->write_stamp =
1471 cpu_buffer->commit_page->page->time_stamp;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001472 /* add barrier to keep gcc from optimizing too much */
1473 barrier();
1474 }
1475 while (rb_commit_index(cpu_buffer) !=
1476 rb_page_write(cpu_buffer->commit_page)) {
Steven Rostedt77ae3652009-03-27 11:00:29 -04001477
1478 local_set(&cpu_buffer->commit_page->page->commit,
1479 rb_page_write(cpu_buffer->commit_page));
1480 RB_WARN_ON(cpu_buffer,
1481 local_read(&cpu_buffer->commit_page->page->commit) &
1482 ~RB_WRITE_MASK);
Steven Rostedtbf41a152008-10-04 02:00:59 -04001483 barrier();
1484 }
Steven Rostedta8ccf1d2008-12-23 11:32:24 -05001485
1486 /* again, keep gcc from optimizing */
1487 barrier();
1488
1489 /*
1490 * If an interrupt came in just after the first while loop
1491 * and pushed the tail page forward, we will be left with
1492 * a dangling commit that will never go forward.
1493 */
1494 if (unlikely(cpu_buffer->commit_page != cpu_buffer->tail_page))
1495 goto again;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001496}
1497
Steven Rostedtd7690412008-10-01 00:29:53 -04001498static void rb_reset_reader_page(struct ring_buffer_per_cpu *cpu_buffer)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001499{
Steven Rostedtabc9b562008-12-02 15:34:06 -05001500 cpu_buffer->read_stamp = cpu_buffer->reader_page->page->time_stamp;
Steven Rostedt6f807ac2008-10-04 02:00:58 -04001501 cpu_buffer->reader_page->read = 0;
Steven Rostedtd7690412008-10-01 00:29:53 -04001502}
1503
Andrew Morton34a148b2009-01-09 12:27:09 -08001504static void rb_inc_iter(struct ring_buffer_iter *iter)
Steven Rostedtd7690412008-10-01 00:29:53 -04001505{
1506 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
1507
1508 /*
1509 * The iterator could be on the reader page (it starts there).
1510 * But the head could have moved, since the reader was
1511 * found. Check for this case and assign the iterator
1512 * to the head page instead of next.
1513 */
1514 if (iter->head_page == cpu_buffer->reader_page)
Steven Rostedt77ae3652009-03-27 11:00:29 -04001515 iter->head_page = rb_set_head_page(cpu_buffer);
Steven Rostedtd7690412008-10-01 00:29:53 -04001516 else
1517 rb_inc_page(cpu_buffer, &iter->head_page);
1518
Steven Rostedtabc9b562008-12-02 15:34:06 -05001519 iter->read_stamp = iter->head_page->page->time_stamp;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001520 iter->head = 0;
1521}
1522
1523/**
1524 * ring_buffer_update_event - update event type and data
1525 * @event: the even to update
1526 * @type: the type of event
1527 * @length: the size of the event field in the ring buffer
1528 *
1529 * Update the type and data fields of the event. The length
1530 * is the actual size that is written to the ring buffer,
1531 * and with this, we can determine what to place into the
1532 * data field.
1533 */
Andrew Morton34a148b2009-01-09 12:27:09 -08001534static void
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001535rb_update_event(struct ring_buffer_event *event,
1536 unsigned type, unsigned length)
1537{
Lai Jiangshan334d4162009-04-24 11:27:05 +08001538 event->type_len = type;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001539
1540 switch (type) {
1541
1542 case RINGBUF_TYPE_PADDING:
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001543 case RINGBUF_TYPE_TIME_EXTEND:
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001544 case RINGBUF_TYPE_TIME_STAMP:
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001545 break;
1546
Lai Jiangshan334d4162009-04-24 11:27:05 +08001547 case 0:
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001548 length -= RB_EVNT_HDR_SIZE;
Lai Jiangshan334d4162009-04-24 11:27:05 +08001549 if (length > RB_MAX_SMALL_DATA)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001550 event->array[0] = length;
Lai Jiangshan334d4162009-04-24 11:27:05 +08001551 else
1552 event->type_len = DIV_ROUND_UP(length, RB_ALIGNMENT);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001553 break;
1554 default:
1555 BUG();
1556 }
1557}
1558
Steven Rostedt77ae3652009-03-27 11:00:29 -04001559/*
1560 * rb_handle_head_page - writer hit the head page
1561 *
1562 * Returns: +1 to retry page
1563 * 0 to continue
1564 * -1 on error
1565 */
1566static int
1567rb_handle_head_page(struct ring_buffer_per_cpu *cpu_buffer,
1568 struct buffer_page *tail_page,
1569 struct buffer_page *next_page)
1570{
1571 struct buffer_page *new_head;
1572 int entries;
1573 int type;
1574 int ret;
1575
1576 entries = rb_page_entries(next_page);
1577
1578 /*
1579 * The hard part is here. We need to move the head
1580 * forward, and protect against both readers on
1581 * other CPUs and writers coming in via interrupts.
1582 */
1583 type = rb_head_page_set_update(cpu_buffer, next_page, tail_page,
1584 RB_PAGE_HEAD);
1585
1586 /*
1587 * type can be one of four:
1588 * NORMAL - an interrupt already moved it for us
1589 * HEAD - we are the first to get here.
1590 * UPDATE - we are the interrupt interrupting
1591 * a current move.
1592 * MOVED - a reader on another CPU moved the next
1593 * pointer to its reader page. Give up
1594 * and try again.
1595 */
1596
1597 switch (type) {
1598 case RB_PAGE_HEAD:
1599 /*
1600 * We changed the head to UPDATE, thus
1601 * it is our responsibility to update
1602 * the counters.
1603 */
1604 local_add(entries, &cpu_buffer->overrun);
1605
1606 /*
1607 * The entries will be zeroed out when we move the
1608 * tail page.
1609 */
1610
1611 /* still more to do */
1612 break;
1613
1614 case RB_PAGE_UPDATE:
1615 /*
1616 * This is an interrupt that interrupt the
1617 * previous update. Still more to do.
1618 */
1619 break;
1620 case RB_PAGE_NORMAL:
1621 /*
1622 * An interrupt came in before the update
1623 * and processed this for us.
1624 * Nothing left to do.
1625 */
1626 return 1;
1627 case RB_PAGE_MOVED:
1628 /*
1629 * The reader is on another CPU and just did
1630 * a swap with our next_page.
1631 * Try again.
1632 */
1633 return 1;
1634 default:
1635 RB_WARN_ON(cpu_buffer, 1); /* WTF??? */
1636 return -1;
1637 }
1638
1639 /*
1640 * Now that we are here, the old head pointer is
1641 * set to UPDATE. This will keep the reader from
1642 * swapping the head page with the reader page.
1643 * The reader (on another CPU) will spin till
1644 * we are finished.
1645 *
1646 * We just need to protect against interrupts
1647 * doing the job. We will set the next pointer
1648 * to HEAD. After that, we set the old pointer
1649 * to NORMAL, but only if it was HEAD before.
1650 * otherwise we are an interrupt, and only
1651 * want the outer most commit to reset it.
1652 */
1653 new_head = next_page;
1654 rb_inc_page(cpu_buffer, &new_head);
1655
1656 ret = rb_head_page_set_head(cpu_buffer, new_head, next_page,
1657 RB_PAGE_NORMAL);
1658
1659 /*
1660 * Valid returns are:
1661 * HEAD - an interrupt came in and already set it.
1662 * NORMAL - One of two things:
1663 * 1) We really set it.
1664 * 2) A bunch of interrupts came in and moved
1665 * the page forward again.
1666 */
1667 switch (ret) {
1668 case RB_PAGE_HEAD:
1669 case RB_PAGE_NORMAL:
1670 /* OK */
1671 break;
1672 default:
1673 RB_WARN_ON(cpu_buffer, 1);
1674 return -1;
1675 }
1676
1677 /*
1678 * It is possible that an interrupt came in,
1679 * set the head up, then more interrupts came in
1680 * and moved it again. When we get back here,
1681 * the page would have been set to NORMAL but we
1682 * just set it back to HEAD.
1683 *
1684 * How do you detect this? Well, if that happened
1685 * the tail page would have moved.
1686 */
1687 if (ret == RB_PAGE_NORMAL) {
1688 /*
1689 * If the tail had moved passed next, then we need
1690 * to reset the pointer.
1691 */
1692 if (cpu_buffer->tail_page != tail_page &&
1693 cpu_buffer->tail_page != next_page)
1694 rb_head_page_set_normal(cpu_buffer, new_head,
1695 next_page,
1696 RB_PAGE_HEAD);
1697 }
1698
1699 /*
1700 * If this was the outer most commit (the one that
1701 * changed the original pointer from HEAD to UPDATE),
1702 * then it is up to us to reset it to NORMAL.
1703 */
1704 if (type == RB_PAGE_HEAD) {
1705 ret = rb_head_page_set_normal(cpu_buffer, next_page,
1706 tail_page,
1707 RB_PAGE_UPDATE);
1708 if (RB_WARN_ON(cpu_buffer,
1709 ret != RB_PAGE_UPDATE))
1710 return -1;
1711 }
1712
1713 return 0;
1714}
1715
Andrew Morton34a148b2009-01-09 12:27:09 -08001716static unsigned rb_calculate_event_length(unsigned length)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001717{
1718 struct ring_buffer_event event; /* Used only for sizeof array */
1719
1720 /* zero length can cause confusions */
1721 if (!length)
1722 length = 1;
1723
1724 if (length > RB_MAX_SMALL_DATA)
1725 length += sizeof(event.array[0]);
1726
1727 length += RB_EVNT_HDR_SIZE;
1728 length = ALIGN(length, RB_ALIGNMENT);
1729
1730 return length;
1731}
1732
Steven Rostedtc7b09302009-06-11 11:12:00 -04001733static inline void
1734rb_reset_tail(struct ring_buffer_per_cpu *cpu_buffer,
1735 struct buffer_page *tail_page,
1736 unsigned long tail, unsigned long length)
1737{
1738 struct ring_buffer_event *event;
1739
1740 /*
1741 * Only the event that crossed the page boundary
1742 * must fill the old tail_page with padding.
1743 */
1744 if (tail >= BUF_PAGE_SIZE) {
1745 local_sub(length, &tail_page->write);
1746 return;
1747 }
1748
1749 event = __rb_page_index(tail_page, tail);
Linus Torvaldsb0b70652009-06-20 10:56:46 -07001750 kmemcheck_annotate_bitfield(event, bitfield);
Steven Rostedtc7b09302009-06-11 11:12:00 -04001751
1752 /*
1753 * If this event is bigger than the minimum size, then
1754 * we need to be careful that we don't subtract the
1755 * write counter enough to allow another writer to slip
1756 * in on this page.
1757 * We put in a discarded commit instead, to make sure
1758 * that this space is not used again.
1759 *
1760 * If we are less than the minimum size, we don't need to
1761 * worry about it.
1762 */
1763 if (tail > (BUF_PAGE_SIZE - RB_EVNT_MIN_SIZE)) {
1764 /* No room for any events */
1765
1766 /* Mark the rest of the page with padding */
1767 rb_event_set_padding(event);
1768
1769 /* Set the write back to the previous setting */
1770 local_sub(length, &tail_page->write);
1771 return;
1772 }
1773
1774 /* Put in a discarded event */
1775 event->array[0] = (BUF_PAGE_SIZE - tail) - RB_EVNT_HDR_SIZE;
1776 event->type_len = RINGBUF_TYPE_PADDING;
1777 /* time delta must be non zero */
1778 event->time_delta = 1;
Steven Rostedtc7b09302009-06-11 11:12:00 -04001779
1780 /* Set write to end of buffer */
1781 length = (tail + length) - BUF_PAGE_SIZE;
1782 local_sub(length, &tail_page->write);
1783}
Steven Rostedt6634ff22009-05-06 15:30:07 -04001784
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001785static struct ring_buffer_event *
Steven Rostedt6634ff22009-05-06 15:30:07 -04001786rb_move_tail(struct ring_buffer_per_cpu *cpu_buffer,
1787 unsigned long length, unsigned long tail,
Steven Rostedt6634ff22009-05-06 15:30:07 -04001788 struct buffer_page *tail_page, u64 *ts)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001789{
Steven Rostedt5a50e332009-11-17 08:43:01 -05001790 struct buffer_page *commit_page = cpu_buffer->commit_page;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001791 struct ring_buffer *buffer = cpu_buffer->buffer;
Steven Rostedt77ae3652009-03-27 11:00:29 -04001792 struct buffer_page *next_page;
1793 int ret;
Steven Rostedtaa20ae82009-05-05 21:16:11 -04001794
1795 next_page = tail_page;
1796
Steven Rostedtaa20ae82009-05-05 21:16:11 -04001797 rb_inc_page(cpu_buffer, &next_page);
1798
Steven Rostedtaa20ae82009-05-05 21:16:11 -04001799 /*
1800 * If for some reason, we had an interrupt storm that made
1801 * it all the way around the buffer, bail, and warn
1802 * about it.
1803 */
1804 if (unlikely(next_page == commit_page)) {
Steven Rostedt77ae3652009-03-27 11:00:29 -04001805 local_inc(&cpu_buffer->commit_overrun);
Steven Rostedtaa20ae82009-05-05 21:16:11 -04001806 goto out_reset;
1807 }
1808
Steven Rostedt77ae3652009-03-27 11:00:29 -04001809 /*
1810 * This is where the fun begins!
1811 *
1812 * We are fighting against races between a reader that
1813 * could be on another CPU trying to swap its reader
1814 * page with the buffer head.
1815 *
1816 * We are also fighting against interrupts coming in and
1817 * moving the head or tail on us as well.
1818 *
1819 * If the next page is the head page then we have filled
1820 * the buffer, unless the commit page is still on the
1821 * reader page.
1822 */
1823 if (rb_is_head_page(cpu_buffer, next_page, &tail_page->list)) {
Steven Rostedtaa20ae82009-05-05 21:16:11 -04001824
Steven Rostedt77ae3652009-03-27 11:00:29 -04001825 /*
1826 * If the commit is not on the reader page, then
1827 * move the header page.
1828 */
1829 if (!rb_is_reader_page(cpu_buffer->commit_page)) {
1830 /*
1831 * If we are not in overwrite mode,
1832 * this is easy, just stop here.
1833 */
1834 if (!(buffer->flags & RB_FL_OVERWRITE))
1835 goto out_reset;
Steven Rostedtaa20ae82009-05-05 21:16:11 -04001836
Steven Rostedt77ae3652009-03-27 11:00:29 -04001837 ret = rb_handle_head_page(cpu_buffer,
1838 tail_page,
1839 next_page);
1840 if (ret < 0)
1841 goto out_reset;
1842 if (ret)
1843 goto out_again;
1844 } else {
1845 /*
1846 * We need to be careful here too. The
1847 * commit page could still be on the reader
1848 * page. We could have a small buffer, and
1849 * have filled up the buffer with events
1850 * from interrupts and such, and wrapped.
1851 *
1852 * Note, if the tail page is also the on the
1853 * reader_page, we let it move out.
1854 */
1855 if (unlikely((cpu_buffer->commit_page !=
1856 cpu_buffer->tail_page) &&
1857 (cpu_buffer->commit_page ==
1858 cpu_buffer->reader_page))) {
1859 local_inc(&cpu_buffer->commit_overrun);
1860 goto out_reset;
1861 }
Steven Rostedtaa20ae82009-05-05 21:16:11 -04001862 }
1863 }
1864
Steven Rostedt77ae3652009-03-27 11:00:29 -04001865 ret = rb_tail_page_update(cpu_buffer, tail_page, next_page);
1866 if (ret) {
1867 /*
1868 * Nested commits always have zero deltas, so
1869 * just reread the time stamp
1870 */
Jiri Olsa6d3f1e12009-10-23 19:36:19 -04001871 *ts = rb_time_stamp(buffer);
Steven Rostedt77ae3652009-03-27 11:00:29 -04001872 next_page->page->time_stamp = *ts;
Steven Rostedtaa20ae82009-05-05 21:16:11 -04001873 }
1874
Steven Rostedt77ae3652009-03-27 11:00:29 -04001875 out_again:
Steven Rostedtaa20ae82009-05-05 21:16:11 -04001876
Steven Rostedt77ae3652009-03-27 11:00:29 -04001877 rb_reset_tail(cpu_buffer, tail_page, tail, length);
Steven Rostedtaa20ae82009-05-05 21:16:11 -04001878
1879 /* fail and let the caller try again */
1880 return ERR_PTR(-EAGAIN);
1881
Steven Rostedt45141d42009-02-12 13:19:48 -05001882 out_reset:
Lai Jiangshan6f3b3442009-01-12 11:06:18 +08001883 /* reset write */
Steven Rostedtc7b09302009-06-11 11:12:00 -04001884 rb_reset_tail(cpu_buffer, tail_page, tail, length);
Lai Jiangshan6f3b3442009-01-12 11:06:18 +08001885
Steven Rostedtbf41a152008-10-04 02:00:59 -04001886 return NULL;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001887}
1888
Steven Rostedt6634ff22009-05-06 15:30:07 -04001889static struct ring_buffer_event *
1890__rb_reserve_next(struct ring_buffer_per_cpu *cpu_buffer,
1891 unsigned type, unsigned long length, u64 *ts)
1892{
Steven Rostedt5a50e332009-11-17 08:43:01 -05001893 struct buffer_page *tail_page;
Steven Rostedt6634ff22009-05-06 15:30:07 -04001894 struct ring_buffer_event *event;
1895 unsigned long tail, write;
1896
Steven Rostedt6634ff22009-05-06 15:30:07 -04001897 tail_page = cpu_buffer->tail_page;
1898 write = local_add_return(length, &tail_page->write);
Steven Rostedt77ae3652009-03-27 11:00:29 -04001899
1900 /* set write to only the index of the write */
1901 write &= RB_WRITE_MASK;
Steven Rostedt6634ff22009-05-06 15:30:07 -04001902 tail = write - length;
1903
1904 /* See if we shot pass the end of this buffer page */
1905 if (write > BUF_PAGE_SIZE)
1906 return rb_move_tail(cpu_buffer, length, tail,
Steven Rostedt5a50e332009-11-17 08:43:01 -05001907 tail_page, ts);
Steven Rostedt6634ff22009-05-06 15:30:07 -04001908
1909 /* We reserved something on the buffer */
1910
Steven Rostedt6634ff22009-05-06 15:30:07 -04001911 event = __rb_page_index(tail_page, tail);
Vegard Nossum1744a212009-02-28 08:29:44 +01001912 kmemcheck_annotate_bitfield(event, bitfield);
Steven Rostedt6634ff22009-05-06 15:30:07 -04001913 rb_update_event(event, type, length);
1914
1915 /* The passed in type is zero for DATA */
1916 if (likely(!type))
1917 local_inc(&tail_page->entries);
1918
1919 /*
Steven Rostedtfa743952009-06-16 12:37:57 -04001920 * If this is the first commit on the page, then update
1921 * its timestamp.
Steven Rostedt6634ff22009-05-06 15:30:07 -04001922 */
Steven Rostedtfa743952009-06-16 12:37:57 -04001923 if (!tail)
1924 tail_page->page->time_stamp = *ts;
Steven Rostedt6634ff22009-05-06 15:30:07 -04001925
1926 return event;
1927}
1928
Steven Rostedtedd813bf2009-06-02 23:00:53 -04001929static inline int
1930rb_try_to_discard(struct ring_buffer_per_cpu *cpu_buffer,
1931 struct ring_buffer_event *event)
1932{
1933 unsigned long new_index, old_index;
1934 struct buffer_page *bpage;
1935 unsigned long index;
1936 unsigned long addr;
1937
1938 new_index = rb_event_index(event);
1939 old_index = new_index + rb_event_length(event);
1940 addr = (unsigned long)event;
1941 addr &= PAGE_MASK;
1942
1943 bpage = cpu_buffer->tail_page;
1944
1945 if (bpage->page == (void *)addr && rb_page_write(bpage) == old_index) {
Steven Rostedt77ae3652009-03-27 11:00:29 -04001946 unsigned long write_mask =
1947 local_read(&bpage->write) & ~RB_WRITE_MASK;
Steven Rostedtedd813bf2009-06-02 23:00:53 -04001948 /*
1949 * This is on the tail page. It is possible that
1950 * a write could come in and move the tail page
1951 * and write to the next page. That is fine
1952 * because we just shorten what is on this page.
1953 */
Steven Rostedt77ae3652009-03-27 11:00:29 -04001954 old_index += write_mask;
1955 new_index += write_mask;
Steven Rostedtedd813bf2009-06-02 23:00:53 -04001956 index = local_cmpxchg(&bpage->write, old_index, new_index);
1957 if (index == old_index)
1958 return 1;
1959 }
1960
1961 /* could not discard */
1962 return 0;
1963}
1964
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001965static int
1966rb_add_time_stamp(struct ring_buffer_per_cpu *cpu_buffer,
1967 u64 *ts, u64 *delta)
1968{
1969 struct ring_buffer_event *event;
1970 static int once;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001971 int ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001972
1973 if (unlikely(*delta > (1ULL << 59) && !once++)) {
1974 printk(KERN_WARNING "Delta way too big! %llu"
1975 " ts=%llu write stamp = %llu\n",
Stephen Rothwelle2862c92008-10-27 17:43:28 +11001976 (unsigned long long)*delta,
1977 (unsigned long long)*ts,
1978 (unsigned long long)cpu_buffer->write_stamp);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001979 WARN_ON(1);
1980 }
1981
1982 /*
1983 * The delta is too big, we to add a
1984 * new timestamp.
1985 */
1986 event = __rb_reserve_next(cpu_buffer,
1987 RINGBUF_TYPE_TIME_EXTEND,
1988 RB_LEN_TIME_EXTEND,
1989 ts);
1990 if (!event)
Steven Rostedtbf41a152008-10-04 02:00:59 -04001991 return -EBUSY;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001992
Steven Rostedtbf41a152008-10-04 02:00:59 -04001993 if (PTR_ERR(event) == -EAGAIN)
1994 return -EAGAIN;
1995
1996 /* Only a commited time event can update the write stamp */
Steven Rostedtfa743952009-06-16 12:37:57 -04001997 if (rb_event_is_commit(cpu_buffer, event)) {
Steven Rostedtbf41a152008-10-04 02:00:59 -04001998 /*
Steven Rostedtfa743952009-06-16 12:37:57 -04001999 * If this is the first on the page, then it was
2000 * updated with the page itself. Try to discard it
2001 * and if we can't just make it zero.
Steven Rostedtbf41a152008-10-04 02:00:59 -04002002 */
2003 if (rb_event_index(event)) {
2004 event->time_delta = *delta & TS_MASK;
2005 event->array[0] = *delta >> TS_SHIFT;
2006 } else {
Steven Rostedtea05b572009-06-03 09:30:10 -04002007 /* try to discard, since we do not need this */
2008 if (!rb_try_to_discard(cpu_buffer, event)) {
2009 /* nope, just zero it */
2010 event->time_delta = 0;
2011 event->array[0] = 0;
2012 }
Steven Rostedtbf41a152008-10-04 02:00:59 -04002013 }
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002014 cpu_buffer->write_stamp = *ts;
Steven Rostedtbf41a152008-10-04 02:00:59 -04002015 /* let the caller know this was the commit */
2016 ret = 1;
2017 } else {
Steven Rostedtedd813bf2009-06-02 23:00:53 -04002018 /* Try to discard the event */
2019 if (!rb_try_to_discard(cpu_buffer, event)) {
2020 /* Darn, this is just wasted space */
2021 event->time_delta = 0;
2022 event->array[0] = 0;
Steven Rostedtedd813bf2009-06-02 23:00:53 -04002023 }
Steven Rostedtf57a8a12009-06-05 14:11:30 -04002024 ret = 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002025 }
2026
Steven Rostedtbf41a152008-10-04 02:00:59 -04002027 *delta = 0;
2028
2029 return ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002030}
2031
Steven Rostedtfa743952009-06-16 12:37:57 -04002032static void rb_start_commit(struct ring_buffer_per_cpu *cpu_buffer)
2033{
2034 local_inc(&cpu_buffer->committing);
2035 local_inc(&cpu_buffer->commits);
2036}
2037
2038static void rb_end_commit(struct ring_buffer_per_cpu *cpu_buffer)
2039{
2040 unsigned long commits;
2041
2042 if (RB_WARN_ON(cpu_buffer,
2043 !local_read(&cpu_buffer->committing)))
2044 return;
2045
2046 again:
2047 commits = local_read(&cpu_buffer->commits);
2048 /* synchronize with interrupts */
2049 barrier();
2050 if (local_read(&cpu_buffer->committing) == 1)
2051 rb_set_commit_to_write(cpu_buffer);
2052
2053 local_dec(&cpu_buffer->committing);
2054
2055 /* synchronize with interrupts */
2056 barrier();
2057
2058 /*
2059 * Need to account for interrupts coming in between the
2060 * updating of the commit page and the clearing of the
2061 * committing counter.
2062 */
2063 if (unlikely(local_read(&cpu_buffer->commits) != commits) &&
2064 !local_read(&cpu_buffer->committing)) {
2065 local_inc(&cpu_buffer->committing);
2066 goto again;
2067 }
2068}
2069
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002070static struct ring_buffer_event *
Steven Rostedt62f0b3e2009-09-04 14:11:34 -04002071rb_reserve_next_event(struct ring_buffer *buffer,
2072 struct ring_buffer_per_cpu *cpu_buffer,
Steven Rostedt1cd8d732009-05-11 14:08:09 -04002073 unsigned long length)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002074{
2075 struct ring_buffer_event *event;
Steven Rostedt168b6b12009-05-11 22:11:05 -04002076 u64 ts, delta = 0;
Steven Rostedtbf41a152008-10-04 02:00:59 -04002077 int commit = 0;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002078 int nr_loops = 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002079
Steven Rostedtfa743952009-06-16 12:37:57 -04002080 rb_start_commit(cpu_buffer);
2081
Steven Rostedt85bac322009-09-04 14:24:40 -04002082#ifdef CONFIG_RING_BUFFER_ALLOW_SWAP
Steven Rostedt62f0b3e2009-09-04 14:11:34 -04002083 /*
2084 * Due to the ability to swap a cpu buffer from a buffer
2085 * it is possible it was swapped before we committed.
2086 * (committing stops a swap). We check for it here and
2087 * if it happened, we have to fail the write.
2088 */
2089 barrier();
2090 if (unlikely(ACCESS_ONCE(cpu_buffer->buffer) != buffer)) {
2091 local_dec(&cpu_buffer->committing);
2092 local_dec(&cpu_buffer->commits);
2093 return NULL;
2094 }
Steven Rostedt85bac322009-09-04 14:24:40 -04002095#endif
Steven Rostedt62f0b3e2009-09-04 14:11:34 -04002096
Steven Rostedtbe957c42009-05-11 14:42:53 -04002097 length = rb_calculate_event_length(length);
Steven Rostedtbf41a152008-10-04 02:00:59 -04002098 again:
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002099 /*
2100 * We allow for interrupts to reenter here and do a trace.
2101 * If one does, it will cause this original code to loop
2102 * back here. Even with heavy interrupts happening, this
2103 * should only happen a few times in a row. If this happens
2104 * 1000 times in a row, there must be either an interrupt
2105 * storm or we have something buggy.
2106 * Bail!
2107 */
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05002108 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 1000))
Steven Rostedtfa743952009-06-16 12:37:57 -04002109 goto out_fail;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002110
Jiri Olsa6d3f1e12009-10-23 19:36:19 -04002111 ts = rb_time_stamp(cpu_buffer->buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002112
Steven Rostedtbf41a152008-10-04 02:00:59 -04002113 /*
2114 * Only the first commit can update the timestamp.
2115 * Yes there is a race here. If an interrupt comes in
2116 * just after the conditional and it traces too, then it
2117 * will also check the deltas. More than one timestamp may
2118 * also be made. But only the entry that did the actual
2119 * commit will be something other than zero.
2120 */
Steven Rostedt0f0c85f2009-05-11 16:08:00 -04002121 if (likely(cpu_buffer->tail_page == cpu_buffer->commit_page &&
2122 rb_page_write(cpu_buffer->tail_page) ==
2123 rb_commit_index(cpu_buffer))) {
Steven Rostedt168b6b12009-05-11 22:11:05 -04002124 u64 diff;
Steven Rostedtbf41a152008-10-04 02:00:59 -04002125
Steven Rostedt168b6b12009-05-11 22:11:05 -04002126 diff = ts - cpu_buffer->write_stamp;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002127
Steven Rostedt168b6b12009-05-11 22:11:05 -04002128 /* make sure this diff is calculated here */
Steven Rostedtbf41a152008-10-04 02:00:59 -04002129 barrier();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002130
Steven Rostedtbf41a152008-10-04 02:00:59 -04002131 /* Did the write stamp get updated already? */
2132 if (unlikely(ts < cpu_buffer->write_stamp))
Steven Rostedt168b6b12009-05-11 22:11:05 -04002133 goto get_event;
Steven Rostedtbf41a152008-10-04 02:00:59 -04002134
Steven Rostedt168b6b12009-05-11 22:11:05 -04002135 delta = diff;
2136 if (unlikely(test_time_stamp(delta))) {
Steven Rostedtbf41a152008-10-04 02:00:59 -04002137
2138 commit = rb_add_time_stamp(cpu_buffer, &ts, &delta);
Steven Rostedtbf41a152008-10-04 02:00:59 -04002139 if (commit == -EBUSY)
Steven Rostedtfa743952009-06-16 12:37:57 -04002140 goto out_fail;
Steven Rostedtbf41a152008-10-04 02:00:59 -04002141
2142 if (commit == -EAGAIN)
2143 goto again;
2144
2145 RB_WARN_ON(cpu_buffer, commit < 0);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002146 }
Steven Rostedt168b6b12009-05-11 22:11:05 -04002147 }
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002148
Steven Rostedt168b6b12009-05-11 22:11:05 -04002149 get_event:
Steven Rostedt1cd8d732009-05-11 14:08:09 -04002150 event = __rb_reserve_next(cpu_buffer, 0, length, &ts);
Steven Rostedt168b6b12009-05-11 22:11:05 -04002151 if (unlikely(PTR_ERR(event) == -EAGAIN))
Steven Rostedtbf41a152008-10-04 02:00:59 -04002152 goto again;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002153
Steven Rostedtfa743952009-06-16 12:37:57 -04002154 if (!event)
2155 goto out_fail;
Steven Rostedtbf41a152008-10-04 02:00:59 -04002156
Steven Rostedtfa743952009-06-16 12:37:57 -04002157 if (!rb_event_is_commit(cpu_buffer, event))
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002158 delta = 0;
2159
2160 event->time_delta = delta;
2161
2162 return event;
Steven Rostedtfa743952009-06-16 12:37:57 -04002163
2164 out_fail:
2165 rb_end_commit(cpu_buffer);
2166 return NULL;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002167}
2168
Paul Mundt1155de42009-06-25 14:30:12 +09002169#ifdef CONFIG_TRACING
2170
Steven Rostedtaa18efb2009-04-20 16:16:11 -04002171#define TRACE_RECURSIVE_DEPTH 16
Steven Rostedt261842b2009-04-16 21:41:52 -04002172
2173static int trace_recursive_lock(void)
2174{
Steven Rostedtaa18efb2009-04-20 16:16:11 -04002175 current->trace_recursion++;
Steven Rostedt261842b2009-04-16 21:41:52 -04002176
Steven Rostedtaa18efb2009-04-20 16:16:11 -04002177 if (likely(current->trace_recursion < TRACE_RECURSIVE_DEPTH))
2178 return 0;
Steven Rostedt261842b2009-04-16 21:41:52 -04002179
Steven Rostedtaa18efb2009-04-20 16:16:11 -04002180 /* Disable all tracing before we do anything else */
2181 tracing_off_permanent();
Frederic Weisbeckere057a5e2009-04-19 23:38:12 +02002182
Steven Rostedt7d7d2b82009-04-27 12:37:49 -04002183 printk_once(KERN_WARNING "Tracing recursion: depth[%ld]:"
Steven Rostedtaa18efb2009-04-20 16:16:11 -04002184 "HC[%lu]:SC[%lu]:NMI[%lu]\n",
2185 current->trace_recursion,
2186 hardirq_count() >> HARDIRQ_SHIFT,
2187 softirq_count() >> SOFTIRQ_SHIFT,
2188 in_nmi());
Frederic Weisbeckere057a5e2009-04-19 23:38:12 +02002189
Steven Rostedtaa18efb2009-04-20 16:16:11 -04002190 WARN_ON_ONCE(1);
2191 return -1;
Steven Rostedt261842b2009-04-16 21:41:52 -04002192}
2193
2194static void trace_recursive_unlock(void)
2195{
Steven Rostedtaa18efb2009-04-20 16:16:11 -04002196 WARN_ON_ONCE(!current->trace_recursion);
Steven Rostedt261842b2009-04-16 21:41:52 -04002197
Steven Rostedtaa18efb2009-04-20 16:16:11 -04002198 current->trace_recursion--;
Steven Rostedt261842b2009-04-16 21:41:52 -04002199}
2200
Paul Mundt1155de42009-06-25 14:30:12 +09002201#else
2202
2203#define trace_recursive_lock() (0)
2204#define trace_recursive_unlock() do { } while (0)
2205
2206#endif
2207
Steven Rostedtbf41a152008-10-04 02:00:59 -04002208static DEFINE_PER_CPU(int, rb_need_resched);
2209
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002210/**
2211 * ring_buffer_lock_reserve - reserve a part of the buffer
2212 * @buffer: the ring buffer to reserve from
2213 * @length: the length of the data to reserve (excluding event header)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002214 *
2215 * Returns a reseverd event on the ring buffer to copy directly to.
2216 * The user of this interface will need to get the body to write into
2217 * and can use the ring_buffer_event_data() interface.
2218 *
2219 * The length is the length of the data needed, not the event length
2220 * which also includes the event header.
2221 *
2222 * Must be paired with ring_buffer_unlock_commit, unless NULL is returned.
2223 * If NULL is returned, then nothing has been allocated or locked.
2224 */
2225struct ring_buffer_event *
Arnaldo Carvalho de Melo0a987752009-02-05 16:12:56 -02002226ring_buffer_lock_reserve(struct ring_buffer *buffer, unsigned long length)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002227{
2228 struct ring_buffer_per_cpu *cpu_buffer;
2229 struct ring_buffer_event *event;
Steven Rostedtbf41a152008-10-04 02:00:59 -04002230 int cpu, resched;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002231
Steven Rostedt033601a2008-11-21 12:41:55 -05002232 if (ring_buffer_flags != RB_BUFFERS_ON)
Steven Rostedta3583242008-11-11 15:01:42 -05002233 return NULL;
2234
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002235 if (atomic_read(&buffer->record_disabled))
2236 return NULL;
2237
Steven Rostedtbf41a152008-10-04 02:00:59 -04002238 /* If we are tracing schedule, we don't want to recurse */
Steven Rostedt182e9f52008-11-03 23:15:56 -05002239 resched = ftrace_preempt_disable();
Steven Rostedtbf41a152008-10-04 02:00:59 -04002240
Steven Rostedt261842b2009-04-16 21:41:52 -04002241 if (trace_recursive_lock())
2242 goto out_nocheck;
2243
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002244 cpu = raw_smp_processor_id();
2245
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302246 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedtd7690412008-10-01 00:29:53 -04002247 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002248
2249 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002250
2251 if (atomic_read(&cpu_buffer->record_disabled))
Steven Rostedtd7690412008-10-01 00:29:53 -04002252 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002253
Steven Rostedtbe957c42009-05-11 14:42:53 -04002254 if (length > BUF_MAX_DATA_SIZE)
Steven Rostedtbf41a152008-10-04 02:00:59 -04002255 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002256
Steven Rostedt62f0b3e2009-09-04 14:11:34 -04002257 event = rb_reserve_next_event(buffer, cpu_buffer, length);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002258 if (!event)
Steven Rostedtd7690412008-10-01 00:29:53 -04002259 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002260
Steven Rostedtbf41a152008-10-04 02:00:59 -04002261 /*
2262 * Need to store resched state on this cpu.
2263 * Only the first needs to.
2264 */
2265
2266 if (preempt_count() == 1)
2267 per_cpu(rb_need_resched, cpu) = resched;
2268
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002269 return event;
2270
Steven Rostedtd7690412008-10-01 00:29:53 -04002271 out:
Steven Rostedt261842b2009-04-16 21:41:52 -04002272 trace_recursive_unlock();
2273
2274 out_nocheck:
Steven Rostedt182e9f52008-11-03 23:15:56 -05002275 ftrace_preempt_enable(resched);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002276 return NULL;
2277}
Robert Richterc4f50182008-12-11 16:49:22 +01002278EXPORT_SYMBOL_GPL(ring_buffer_lock_reserve);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002279
Steven Rostedta1863c22009-09-03 10:23:58 -04002280static void
2281rb_update_write_stamp(struct ring_buffer_per_cpu *cpu_buffer,
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002282 struct ring_buffer_event *event)
2283{
Steven Rostedtfa743952009-06-16 12:37:57 -04002284 /*
2285 * The event first in the commit queue updates the
2286 * time stamp.
2287 */
2288 if (rb_event_is_commit(cpu_buffer, event))
2289 cpu_buffer->write_stamp += event->time_delta;
Steven Rostedta1863c22009-09-03 10:23:58 -04002290}
Steven Rostedtbf41a152008-10-04 02:00:59 -04002291
Steven Rostedta1863c22009-09-03 10:23:58 -04002292static void rb_commit(struct ring_buffer_per_cpu *cpu_buffer,
2293 struct ring_buffer_event *event)
2294{
2295 local_inc(&cpu_buffer->entries);
2296 rb_update_write_stamp(cpu_buffer, event);
Steven Rostedtfa743952009-06-16 12:37:57 -04002297 rb_end_commit(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002298}
2299
2300/**
2301 * ring_buffer_unlock_commit - commit a reserved
2302 * @buffer: The buffer to commit to
2303 * @event: The event pointer to commit.
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002304 *
2305 * This commits the data to the ring buffer, and releases any locks held.
2306 *
2307 * Must be paired with ring_buffer_lock_reserve.
2308 */
2309int ring_buffer_unlock_commit(struct ring_buffer *buffer,
Arnaldo Carvalho de Melo0a987752009-02-05 16:12:56 -02002310 struct ring_buffer_event *event)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002311{
2312 struct ring_buffer_per_cpu *cpu_buffer;
2313 int cpu = raw_smp_processor_id();
2314
2315 cpu_buffer = buffer->buffers[cpu];
2316
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002317 rb_commit(cpu_buffer, event);
2318
Steven Rostedt261842b2009-04-16 21:41:52 -04002319 trace_recursive_unlock();
2320
Steven Rostedtbf41a152008-10-04 02:00:59 -04002321 /*
2322 * Only the last preempt count needs to restore preemption.
2323 */
Steven Rostedt182e9f52008-11-03 23:15:56 -05002324 if (preempt_count() == 1)
2325 ftrace_preempt_enable(per_cpu(rb_need_resched, cpu));
2326 else
Steven Rostedtbf41a152008-10-04 02:00:59 -04002327 preempt_enable_no_resched_notrace();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002328
2329 return 0;
2330}
Robert Richterc4f50182008-12-11 16:49:22 +01002331EXPORT_SYMBOL_GPL(ring_buffer_unlock_commit);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002332
Frederic Weisbeckerf3b9aae2009-04-19 23:39:33 +02002333static inline void rb_event_discard(struct ring_buffer_event *event)
2334{
Lai Jiangshan334d4162009-04-24 11:27:05 +08002335 /* array[0] holds the actual length for the discarded event */
2336 event->array[0] = rb_event_data_length(event) - RB_EVNT_HDR_SIZE;
2337 event->type_len = RINGBUF_TYPE_PADDING;
Frederic Weisbeckerf3b9aae2009-04-19 23:39:33 +02002338 /* time delta must be non zero */
2339 if (!event->time_delta)
2340 event->time_delta = 1;
2341}
2342
Steven Rostedta1863c22009-09-03 10:23:58 -04002343/*
2344 * Decrement the entries to the page that an event is on.
2345 * The event does not even need to exist, only the pointer
2346 * to the page it is on. This may only be called before the commit
2347 * takes place.
2348 */
2349static inline void
2350rb_decrement_entry(struct ring_buffer_per_cpu *cpu_buffer,
2351 struct ring_buffer_event *event)
2352{
2353 unsigned long addr = (unsigned long)event;
2354 struct buffer_page *bpage = cpu_buffer->commit_page;
2355 struct buffer_page *start;
2356
2357 addr &= PAGE_MASK;
2358
2359 /* Do the likely case first */
2360 if (likely(bpage->page == (void *)addr)) {
2361 local_dec(&bpage->entries);
2362 return;
2363 }
2364
2365 /*
2366 * Because the commit page may be on the reader page we
2367 * start with the next page and check the end loop there.
2368 */
2369 rb_inc_page(cpu_buffer, &bpage);
2370 start = bpage;
2371 do {
2372 if (bpage->page == (void *)addr) {
2373 local_dec(&bpage->entries);
2374 return;
2375 }
2376 rb_inc_page(cpu_buffer, &bpage);
2377 } while (bpage != start);
2378
2379 /* commit not part of this buffer?? */
2380 RB_WARN_ON(cpu_buffer, 1);
2381}
2382
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002383/**
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002384 * ring_buffer_commit_discard - discard an event that has not been committed
2385 * @buffer: the ring buffer
2386 * @event: non committed event to discard
2387 *
Steven Rostedtdc892f72009-09-03 15:33:41 -04002388 * Sometimes an event that is in the ring buffer needs to be ignored.
2389 * This function lets the user discard an event in the ring buffer
2390 * and then that event will not be read later.
2391 *
2392 * This function only works if it is called before the the item has been
2393 * committed. It will try to free the event from the ring buffer
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002394 * if another event has not been added behind it.
2395 *
2396 * If another event has been added behind it, it will set the event
2397 * up as discarded, and perform the commit.
2398 *
2399 * If this function is called, do not call ring_buffer_unlock_commit on
2400 * the event.
2401 */
2402void ring_buffer_discard_commit(struct ring_buffer *buffer,
2403 struct ring_buffer_event *event)
2404{
2405 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002406 int cpu;
2407
2408 /* The event is discarded regardless */
Frederic Weisbeckerf3b9aae2009-04-19 23:39:33 +02002409 rb_event_discard(event);
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002410
Steven Rostedtfa743952009-06-16 12:37:57 -04002411 cpu = smp_processor_id();
2412 cpu_buffer = buffer->buffers[cpu];
2413
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002414 /*
2415 * This must only be called if the event has not been
2416 * committed yet. Thus we can assume that preemption
2417 * is still disabled.
2418 */
Steven Rostedtfa743952009-06-16 12:37:57 -04002419 RB_WARN_ON(buffer, !local_read(&cpu_buffer->committing));
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002420
Steven Rostedta1863c22009-09-03 10:23:58 -04002421 rb_decrement_entry(cpu_buffer, event);
Steven Rostedt0f2541d2009-08-05 12:02:48 -04002422 if (rb_try_to_discard(cpu_buffer, event))
Steven Rostedtedd813bf2009-06-02 23:00:53 -04002423 goto out;
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002424
2425 /*
2426 * The commit is still visible by the reader, so we
Steven Rostedta1863c22009-09-03 10:23:58 -04002427 * must still update the timestamp.
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002428 */
Steven Rostedta1863c22009-09-03 10:23:58 -04002429 rb_update_write_stamp(cpu_buffer, event);
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002430 out:
Steven Rostedtfa743952009-06-16 12:37:57 -04002431 rb_end_commit(cpu_buffer);
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002432
Frederic Weisbeckerf3b9aae2009-04-19 23:39:33 +02002433 trace_recursive_unlock();
2434
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002435 /*
2436 * Only the last preempt count needs to restore preemption.
2437 */
2438 if (preempt_count() == 1)
2439 ftrace_preempt_enable(per_cpu(rb_need_resched, cpu));
2440 else
2441 preempt_enable_no_resched_notrace();
2442
2443}
2444EXPORT_SYMBOL_GPL(ring_buffer_discard_commit);
2445
2446/**
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002447 * ring_buffer_write - write data to the buffer without reserving
2448 * @buffer: The ring buffer to write to.
2449 * @length: The length of the data being written (excluding the event header)
2450 * @data: The data to write to the buffer.
2451 *
2452 * This is like ring_buffer_lock_reserve and ring_buffer_unlock_commit as
2453 * one function. If you already have the data to write to the buffer, it
2454 * may be easier to simply call this function.
2455 *
2456 * Note, like ring_buffer_lock_reserve, the length is the length of the data
2457 * and not the length of the event which would hold the header.
2458 */
2459int ring_buffer_write(struct ring_buffer *buffer,
2460 unsigned long length,
2461 void *data)
2462{
2463 struct ring_buffer_per_cpu *cpu_buffer;
2464 struct ring_buffer_event *event;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002465 void *body;
2466 int ret = -EBUSY;
Steven Rostedtbf41a152008-10-04 02:00:59 -04002467 int cpu, resched;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002468
Steven Rostedt033601a2008-11-21 12:41:55 -05002469 if (ring_buffer_flags != RB_BUFFERS_ON)
Steven Rostedta3583242008-11-11 15:01:42 -05002470 return -EBUSY;
2471
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002472 if (atomic_read(&buffer->record_disabled))
2473 return -EBUSY;
2474
Steven Rostedt182e9f52008-11-03 23:15:56 -05002475 resched = ftrace_preempt_disable();
Steven Rostedtbf41a152008-10-04 02:00:59 -04002476
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002477 cpu = raw_smp_processor_id();
2478
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302479 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedtd7690412008-10-01 00:29:53 -04002480 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002481
2482 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002483
2484 if (atomic_read(&cpu_buffer->record_disabled))
2485 goto out;
2486
Steven Rostedtbe957c42009-05-11 14:42:53 -04002487 if (length > BUF_MAX_DATA_SIZE)
2488 goto out;
2489
Steven Rostedt62f0b3e2009-09-04 14:11:34 -04002490 event = rb_reserve_next_event(buffer, cpu_buffer, length);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002491 if (!event)
2492 goto out;
2493
2494 body = rb_event_data(event);
2495
2496 memcpy(body, data, length);
2497
2498 rb_commit(cpu_buffer, event);
2499
2500 ret = 0;
2501 out:
Steven Rostedt182e9f52008-11-03 23:15:56 -05002502 ftrace_preempt_enable(resched);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002503
2504 return ret;
2505}
Robert Richterc4f50182008-12-11 16:49:22 +01002506EXPORT_SYMBOL_GPL(ring_buffer_write);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002507
Andrew Morton34a148b2009-01-09 12:27:09 -08002508static int rb_per_cpu_empty(struct ring_buffer_per_cpu *cpu_buffer)
Steven Rostedtbf41a152008-10-04 02:00:59 -04002509{
2510 struct buffer_page *reader = cpu_buffer->reader_page;
Steven Rostedt77ae3652009-03-27 11:00:29 -04002511 struct buffer_page *head = rb_set_head_page(cpu_buffer);
Steven Rostedtbf41a152008-10-04 02:00:59 -04002512 struct buffer_page *commit = cpu_buffer->commit_page;
2513
Steven Rostedt77ae3652009-03-27 11:00:29 -04002514 /* In case of error, head will be NULL */
2515 if (unlikely(!head))
2516 return 1;
2517
Steven Rostedtbf41a152008-10-04 02:00:59 -04002518 return reader->read == rb_page_commit(reader) &&
2519 (commit == reader ||
2520 (commit == head &&
2521 head->read == rb_page_commit(commit)));
2522}
2523
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002524/**
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002525 * ring_buffer_record_disable - stop all writes into the buffer
2526 * @buffer: The ring buffer to stop writes to.
2527 *
2528 * This prevents all writes to the buffer. Any attempt to write
2529 * to the buffer after this will fail and return NULL.
2530 *
2531 * The caller should call synchronize_sched() after this.
2532 */
2533void ring_buffer_record_disable(struct ring_buffer *buffer)
2534{
2535 atomic_inc(&buffer->record_disabled);
2536}
Robert Richterc4f50182008-12-11 16:49:22 +01002537EXPORT_SYMBOL_GPL(ring_buffer_record_disable);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002538
2539/**
2540 * ring_buffer_record_enable - enable writes to the buffer
2541 * @buffer: The ring buffer to enable writes
2542 *
2543 * Note, multiple disables will need the same number of enables
2544 * to truely enable the writing (much like preempt_disable).
2545 */
2546void ring_buffer_record_enable(struct ring_buffer *buffer)
2547{
2548 atomic_dec(&buffer->record_disabled);
2549}
Robert Richterc4f50182008-12-11 16:49:22 +01002550EXPORT_SYMBOL_GPL(ring_buffer_record_enable);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002551
2552/**
2553 * ring_buffer_record_disable_cpu - stop all writes into the cpu_buffer
2554 * @buffer: The ring buffer to stop writes to.
2555 * @cpu: The CPU buffer to stop
2556 *
2557 * This prevents all writes to the buffer. Any attempt to write
2558 * to the buffer after this will fail and return NULL.
2559 *
2560 * The caller should call synchronize_sched() after this.
2561 */
2562void ring_buffer_record_disable_cpu(struct ring_buffer *buffer, int cpu)
2563{
2564 struct ring_buffer_per_cpu *cpu_buffer;
2565
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302566 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04002567 return;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002568
2569 cpu_buffer = buffer->buffers[cpu];
2570 atomic_inc(&cpu_buffer->record_disabled);
2571}
Robert Richterc4f50182008-12-11 16:49:22 +01002572EXPORT_SYMBOL_GPL(ring_buffer_record_disable_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002573
2574/**
2575 * ring_buffer_record_enable_cpu - enable writes to the buffer
2576 * @buffer: The ring buffer to enable writes
2577 * @cpu: The CPU to enable.
2578 *
2579 * Note, multiple disables will need the same number of enables
2580 * to truely enable the writing (much like preempt_disable).
2581 */
2582void ring_buffer_record_enable_cpu(struct ring_buffer *buffer, int cpu)
2583{
2584 struct ring_buffer_per_cpu *cpu_buffer;
2585
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302586 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04002587 return;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002588
2589 cpu_buffer = buffer->buffers[cpu];
2590 atomic_dec(&cpu_buffer->record_disabled);
2591}
Robert Richterc4f50182008-12-11 16:49:22 +01002592EXPORT_SYMBOL_GPL(ring_buffer_record_enable_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002593
2594/**
2595 * ring_buffer_entries_cpu - get the number of entries in a cpu buffer
2596 * @buffer: The ring buffer
2597 * @cpu: The per CPU buffer to get the entries from.
2598 */
2599unsigned long ring_buffer_entries_cpu(struct ring_buffer *buffer, int cpu)
2600{
2601 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedt8aabee52009-03-12 13:13:49 -04002602 unsigned long ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002603
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302604 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04002605 return 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002606
2607 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt77ae3652009-03-27 11:00:29 -04002608 ret = (local_read(&cpu_buffer->entries) - local_read(&cpu_buffer->overrun))
Steven Rostedte4906ef2009-04-30 20:49:44 -04002609 - cpu_buffer->read;
Steven Rostedt554f7862009-03-11 22:00:13 -04002610
2611 return ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002612}
Robert Richterc4f50182008-12-11 16:49:22 +01002613EXPORT_SYMBOL_GPL(ring_buffer_entries_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002614
2615/**
2616 * ring_buffer_overrun_cpu - get the number of overruns in a cpu_buffer
2617 * @buffer: The ring buffer
2618 * @cpu: The per CPU buffer to get the number of overruns from
2619 */
2620unsigned long ring_buffer_overrun_cpu(struct ring_buffer *buffer, int cpu)
2621{
2622 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedt8aabee52009-03-12 13:13:49 -04002623 unsigned long ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002624
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302625 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04002626 return 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002627
2628 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt77ae3652009-03-27 11:00:29 -04002629 ret = local_read(&cpu_buffer->overrun);
Steven Rostedt554f7862009-03-11 22:00:13 -04002630
2631 return ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002632}
Robert Richterc4f50182008-12-11 16:49:22 +01002633EXPORT_SYMBOL_GPL(ring_buffer_overrun_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002634
2635/**
Steven Rostedtf0d2c682009-04-29 13:43:37 -04002636 * ring_buffer_commit_overrun_cpu - get the number of overruns caused by commits
2637 * @buffer: The ring buffer
2638 * @cpu: The per CPU buffer to get the number of overruns from
2639 */
2640unsigned long
2641ring_buffer_commit_overrun_cpu(struct ring_buffer *buffer, int cpu)
2642{
2643 struct ring_buffer_per_cpu *cpu_buffer;
2644 unsigned long ret;
2645
2646 if (!cpumask_test_cpu(cpu, buffer->cpumask))
2647 return 0;
2648
2649 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt77ae3652009-03-27 11:00:29 -04002650 ret = local_read(&cpu_buffer->commit_overrun);
Steven Rostedtf0d2c682009-04-29 13:43:37 -04002651
2652 return ret;
2653}
2654EXPORT_SYMBOL_GPL(ring_buffer_commit_overrun_cpu);
2655
2656/**
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002657 * ring_buffer_entries - get the number of entries in a buffer
2658 * @buffer: The ring buffer
2659 *
2660 * Returns the total number of entries in the ring buffer
2661 * (all CPU entries)
2662 */
2663unsigned long ring_buffer_entries(struct ring_buffer *buffer)
2664{
2665 struct ring_buffer_per_cpu *cpu_buffer;
2666 unsigned long entries = 0;
2667 int cpu;
2668
2669 /* if you care about this being correct, lock the buffer */
2670 for_each_buffer_cpu(buffer, cpu) {
2671 cpu_buffer = buffer->buffers[cpu];
Steven Rostedte4906ef2009-04-30 20:49:44 -04002672 entries += (local_read(&cpu_buffer->entries) -
Steven Rostedt77ae3652009-03-27 11:00:29 -04002673 local_read(&cpu_buffer->overrun)) - cpu_buffer->read;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002674 }
2675
2676 return entries;
2677}
Robert Richterc4f50182008-12-11 16:49:22 +01002678EXPORT_SYMBOL_GPL(ring_buffer_entries);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002679
2680/**
Jiri Olsa67b394f2009-10-23 19:36:18 -04002681 * ring_buffer_overruns - get the number of overruns in buffer
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002682 * @buffer: The ring buffer
2683 *
2684 * Returns the total number of overruns in the ring buffer
2685 * (all CPU entries)
2686 */
2687unsigned long ring_buffer_overruns(struct ring_buffer *buffer)
2688{
2689 struct ring_buffer_per_cpu *cpu_buffer;
2690 unsigned long overruns = 0;
2691 int cpu;
2692
2693 /* if you care about this being correct, lock the buffer */
2694 for_each_buffer_cpu(buffer, cpu) {
2695 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt77ae3652009-03-27 11:00:29 -04002696 overruns += local_read(&cpu_buffer->overrun);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002697 }
2698
2699 return overruns;
2700}
Robert Richterc4f50182008-12-11 16:49:22 +01002701EXPORT_SYMBOL_GPL(ring_buffer_overruns);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002702
Steven Rostedt642edba2008-11-12 00:01:26 -05002703static void rb_iter_reset(struct ring_buffer_iter *iter)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002704{
2705 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
2706
Steven Rostedtd7690412008-10-01 00:29:53 -04002707 /* Iterator usage is expected to have record disabled */
2708 if (list_empty(&cpu_buffer->reader_page->list)) {
Steven Rostedt77ae3652009-03-27 11:00:29 -04002709 iter->head_page = rb_set_head_page(cpu_buffer);
2710 if (unlikely(!iter->head_page))
2711 return;
2712 iter->head = iter->head_page->read;
Steven Rostedtd7690412008-10-01 00:29:53 -04002713 } else {
2714 iter->head_page = cpu_buffer->reader_page;
Steven Rostedt6f807ac2008-10-04 02:00:58 -04002715 iter->head = cpu_buffer->reader_page->read;
Steven Rostedtd7690412008-10-01 00:29:53 -04002716 }
2717 if (iter->head)
2718 iter->read_stamp = cpu_buffer->read_stamp;
2719 else
Steven Rostedtabc9b562008-12-02 15:34:06 -05002720 iter->read_stamp = iter->head_page->page->time_stamp;
Steven Rostedt642edba2008-11-12 00:01:26 -05002721}
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002722
Steven Rostedt642edba2008-11-12 00:01:26 -05002723/**
2724 * ring_buffer_iter_reset - reset an iterator
2725 * @iter: The iterator to reset
2726 *
2727 * Resets the iterator, so that it will start from the beginning
2728 * again.
2729 */
2730void ring_buffer_iter_reset(struct ring_buffer_iter *iter)
2731{
Steven Rostedt554f7862009-03-11 22:00:13 -04002732 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedt642edba2008-11-12 00:01:26 -05002733 unsigned long flags;
2734
Steven Rostedt554f7862009-03-11 22:00:13 -04002735 if (!iter)
2736 return;
2737
2738 cpu_buffer = iter->cpu_buffer;
2739
Steven Rostedt642edba2008-11-12 00:01:26 -05002740 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
2741 rb_iter_reset(iter);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002742 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002743}
Robert Richterc4f50182008-12-11 16:49:22 +01002744EXPORT_SYMBOL_GPL(ring_buffer_iter_reset);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002745
2746/**
2747 * ring_buffer_iter_empty - check if an iterator has no more to read
2748 * @iter: The iterator to check
2749 */
2750int ring_buffer_iter_empty(struct ring_buffer_iter *iter)
2751{
2752 struct ring_buffer_per_cpu *cpu_buffer;
2753
2754 cpu_buffer = iter->cpu_buffer;
2755
Steven Rostedtbf41a152008-10-04 02:00:59 -04002756 return iter->head_page == cpu_buffer->commit_page &&
2757 iter->head == rb_commit_index(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002758}
Robert Richterc4f50182008-12-11 16:49:22 +01002759EXPORT_SYMBOL_GPL(ring_buffer_iter_empty);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002760
2761static void
2762rb_update_read_stamp(struct ring_buffer_per_cpu *cpu_buffer,
2763 struct ring_buffer_event *event)
2764{
2765 u64 delta;
2766
Lai Jiangshan334d4162009-04-24 11:27:05 +08002767 switch (event->type_len) {
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002768 case RINGBUF_TYPE_PADDING:
2769 return;
2770
2771 case RINGBUF_TYPE_TIME_EXTEND:
2772 delta = event->array[0];
2773 delta <<= TS_SHIFT;
2774 delta += event->time_delta;
2775 cpu_buffer->read_stamp += delta;
2776 return;
2777
2778 case RINGBUF_TYPE_TIME_STAMP:
2779 /* FIXME: not implemented */
2780 return;
2781
2782 case RINGBUF_TYPE_DATA:
2783 cpu_buffer->read_stamp += event->time_delta;
2784 return;
2785
2786 default:
2787 BUG();
2788 }
2789 return;
2790}
2791
2792static void
2793rb_update_iter_read_stamp(struct ring_buffer_iter *iter,
2794 struct ring_buffer_event *event)
2795{
2796 u64 delta;
2797
Lai Jiangshan334d4162009-04-24 11:27:05 +08002798 switch (event->type_len) {
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002799 case RINGBUF_TYPE_PADDING:
2800 return;
2801
2802 case RINGBUF_TYPE_TIME_EXTEND:
2803 delta = event->array[0];
2804 delta <<= TS_SHIFT;
2805 delta += event->time_delta;
2806 iter->read_stamp += delta;
2807 return;
2808
2809 case RINGBUF_TYPE_TIME_STAMP:
2810 /* FIXME: not implemented */
2811 return;
2812
2813 case RINGBUF_TYPE_DATA:
2814 iter->read_stamp += event->time_delta;
2815 return;
2816
2817 default:
2818 BUG();
2819 }
2820 return;
2821}
2822
Steven Rostedtd7690412008-10-01 00:29:53 -04002823static struct buffer_page *
2824rb_get_reader_page(struct ring_buffer_per_cpu *cpu_buffer)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002825{
Steven Rostedtd7690412008-10-01 00:29:53 -04002826 struct buffer_page *reader = NULL;
2827 unsigned long flags;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002828 int nr_loops = 0;
Steven Rostedt77ae3652009-03-27 11:00:29 -04002829 int ret;
Steven Rostedtd7690412008-10-01 00:29:53 -04002830
Steven Rostedt3e03fb72008-11-06 00:09:43 -05002831 local_irq_save(flags);
2832 __raw_spin_lock(&cpu_buffer->lock);
Steven Rostedtd7690412008-10-01 00:29:53 -04002833
2834 again:
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002835 /*
2836 * This should normally only loop twice. But because the
2837 * start of the reader inserts an empty page, it causes
2838 * a case where we will loop three times. There should be no
2839 * reason to loop four times (that I know of).
2840 */
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05002841 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 3)) {
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002842 reader = NULL;
2843 goto out;
2844 }
2845
Steven Rostedtd7690412008-10-01 00:29:53 -04002846 reader = cpu_buffer->reader_page;
2847
2848 /* If there's more to read, return this page */
Steven Rostedtbf41a152008-10-04 02:00:59 -04002849 if (cpu_buffer->reader_page->read < rb_page_size(reader))
Steven Rostedtd7690412008-10-01 00:29:53 -04002850 goto out;
2851
2852 /* Never should we have an index greater than the size */
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05002853 if (RB_WARN_ON(cpu_buffer,
2854 cpu_buffer->reader_page->read > rb_page_size(reader)))
2855 goto out;
Steven Rostedtd7690412008-10-01 00:29:53 -04002856
2857 /* check if we caught up to the tail */
2858 reader = NULL;
Steven Rostedtbf41a152008-10-04 02:00:59 -04002859 if (cpu_buffer->commit_page == cpu_buffer->reader_page)
Steven Rostedtd7690412008-10-01 00:29:53 -04002860 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002861
2862 /*
Steven Rostedtd7690412008-10-01 00:29:53 -04002863 * Reset the reader page to size zero.
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002864 */
Steven Rostedt77ae3652009-03-27 11:00:29 -04002865 local_set(&cpu_buffer->reader_page->write, 0);
2866 local_set(&cpu_buffer->reader_page->entries, 0);
2867 local_set(&cpu_buffer->reader_page->page->commit, 0);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002868
Steven Rostedt77ae3652009-03-27 11:00:29 -04002869 spin:
2870 /*
2871 * Splice the empty reader page into the list around the head.
2872 */
2873 reader = rb_set_head_page(cpu_buffer);
Steven Rostedtd7690412008-10-01 00:29:53 -04002874 cpu_buffer->reader_page->list.next = reader->list.next;
2875 cpu_buffer->reader_page->list.prev = reader->list.prev;
Steven Rostedtbf41a152008-10-04 02:00:59 -04002876
Steven Rostedt3adc54f2009-03-30 15:32:01 -04002877 /*
2878 * cpu_buffer->pages just needs to point to the buffer, it
2879 * has no specific buffer page to point to. Lets move it out
2880 * of our way so we don't accidently swap it.
2881 */
2882 cpu_buffer->pages = reader->list.prev;
2883
Steven Rostedt77ae3652009-03-27 11:00:29 -04002884 /* The reader page will be pointing to the new head */
2885 rb_set_list_to_head(cpu_buffer, &cpu_buffer->reader_page->list);
Steven Rostedtd7690412008-10-01 00:29:53 -04002886
2887 /*
Steven Rostedt77ae3652009-03-27 11:00:29 -04002888 * Here's the tricky part.
2889 *
2890 * We need to move the pointer past the header page.
2891 * But we can only do that if a writer is not currently
2892 * moving it. The page before the header page has the
2893 * flag bit '1' set if it is pointing to the page we want.
2894 * but if the writer is in the process of moving it
2895 * than it will be '2' or already moved '0'.
Steven Rostedtd7690412008-10-01 00:29:53 -04002896 */
Steven Rostedtd7690412008-10-01 00:29:53 -04002897
Steven Rostedt77ae3652009-03-27 11:00:29 -04002898 ret = rb_head_page_replace(reader, cpu_buffer->reader_page);
2899
2900 /*
2901 * If we did not convert it, then we must try again.
2902 */
2903 if (!ret)
2904 goto spin;
2905
2906 /*
2907 * Yeah! We succeeded in replacing the page.
2908 *
2909 * Now make the new head point back to the reader page.
2910 */
2911 reader->list.next->prev = &cpu_buffer->reader_page->list;
2912 rb_inc_page(cpu_buffer, &cpu_buffer->head_page);
Steven Rostedtd7690412008-10-01 00:29:53 -04002913
2914 /* Finally update the reader page to the new head */
2915 cpu_buffer->reader_page = reader;
2916 rb_reset_reader_page(cpu_buffer);
2917
2918 goto again;
2919
2920 out:
Steven Rostedt3e03fb72008-11-06 00:09:43 -05002921 __raw_spin_unlock(&cpu_buffer->lock);
2922 local_irq_restore(flags);
Steven Rostedtd7690412008-10-01 00:29:53 -04002923
2924 return reader;
2925}
2926
2927static void rb_advance_reader(struct ring_buffer_per_cpu *cpu_buffer)
2928{
2929 struct ring_buffer_event *event;
2930 struct buffer_page *reader;
2931 unsigned length;
2932
2933 reader = rb_get_reader_page(cpu_buffer);
2934
2935 /* This function should not be called when buffer is empty */
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05002936 if (RB_WARN_ON(cpu_buffer, !reader))
2937 return;
Steven Rostedtd7690412008-10-01 00:29:53 -04002938
2939 event = rb_reader_event(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002940
Steven Rostedta1863c22009-09-03 10:23:58 -04002941 if (event->type_len <= RINGBUF_TYPE_DATA_TYPE_LEN_MAX)
Steven Rostedte4906ef2009-04-30 20:49:44 -04002942 cpu_buffer->read++;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002943
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002944 rb_update_read_stamp(cpu_buffer, event);
2945
Steven Rostedtd7690412008-10-01 00:29:53 -04002946 length = rb_event_length(event);
Steven Rostedt6f807ac2008-10-04 02:00:58 -04002947 cpu_buffer->reader_page->read += length;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002948}
2949
2950static void rb_advance_iter(struct ring_buffer_iter *iter)
2951{
2952 struct ring_buffer *buffer;
2953 struct ring_buffer_per_cpu *cpu_buffer;
2954 struct ring_buffer_event *event;
2955 unsigned length;
2956
2957 cpu_buffer = iter->cpu_buffer;
2958 buffer = cpu_buffer->buffer;
2959
2960 /*
2961 * Check if we are at the end of the buffer.
2962 */
Steven Rostedtbf41a152008-10-04 02:00:59 -04002963 if (iter->head >= rb_page_size(iter->head_page)) {
Steven Rostedtea05b572009-06-03 09:30:10 -04002964 /* discarded commits can make the page empty */
2965 if (iter->head_page == cpu_buffer->commit_page)
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05002966 return;
Steven Rostedtd7690412008-10-01 00:29:53 -04002967 rb_inc_iter(iter);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002968 return;
2969 }
2970
2971 event = rb_iter_head_event(iter);
2972
2973 length = rb_event_length(event);
2974
2975 /*
2976 * This should not be called to advance the header if we are
2977 * at the tail of the buffer.
2978 */
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05002979 if (RB_WARN_ON(cpu_buffer,
Steven Rostedtf536aaf2008-11-10 23:07:30 -05002980 (iter->head_page == cpu_buffer->commit_page) &&
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05002981 (iter->head + length > rb_commit_index(cpu_buffer))))
2982 return;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002983
2984 rb_update_iter_read_stamp(iter, event);
2985
2986 iter->head += length;
2987
2988 /* check for end of page padding */
Steven Rostedtbf41a152008-10-04 02:00:59 -04002989 if ((iter->head >= rb_page_size(iter->head_page)) &&
2990 (iter->head_page != cpu_buffer->commit_page))
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002991 rb_advance_iter(iter);
2992}
2993
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002994static struct ring_buffer_event *
Robert Richterd8eeb2d2009-07-31 14:58:04 +02002995rb_buffer_peek(struct ring_buffer_per_cpu *cpu_buffer, u64 *ts)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002996{
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002997 struct ring_buffer_event *event;
Steven Rostedtd7690412008-10-01 00:29:53 -04002998 struct buffer_page *reader;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002999 int nr_loops = 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003000
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003001 again:
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003002 /*
3003 * We repeat when a timestamp is encountered. It is possible
3004 * to get multiple timestamps from an interrupt entering just
Steven Rostedtea05b572009-06-03 09:30:10 -04003005 * as one timestamp is about to be written, or from discarded
3006 * commits. The most that we can have is the number on a single page.
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003007 */
Steven Rostedtea05b572009-06-03 09:30:10 -04003008 if (RB_WARN_ON(cpu_buffer, ++nr_loops > RB_TIMESTAMPS_PER_PAGE))
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003009 return NULL;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003010
Steven Rostedtd7690412008-10-01 00:29:53 -04003011 reader = rb_get_reader_page(cpu_buffer);
3012 if (!reader)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003013 return NULL;
3014
Steven Rostedtd7690412008-10-01 00:29:53 -04003015 event = rb_reader_event(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003016
Lai Jiangshan334d4162009-04-24 11:27:05 +08003017 switch (event->type_len) {
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003018 case RINGBUF_TYPE_PADDING:
Tom Zanussi2d622712009-03-22 03:30:49 -05003019 if (rb_null_event(event))
3020 RB_WARN_ON(cpu_buffer, 1);
3021 /*
3022 * Because the writer could be discarding every
3023 * event it creates (which would probably be bad)
3024 * if we were to go back to "again" then we may never
3025 * catch up, and will trigger the warn on, or lock
3026 * the box. Return the padding, and we will release
3027 * the current locks, and try again.
3028 */
Tom Zanussi2d622712009-03-22 03:30:49 -05003029 return event;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003030
3031 case RINGBUF_TYPE_TIME_EXTEND:
3032 /* Internal data, OK to advance */
Steven Rostedtd7690412008-10-01 00:29:53 -04003033 rb_advance_reader(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003034 goto again;
3035
3036 case RINGBUF_TYPE_TIME_STAMP:
3037 /* FIXME: not implemented */
Steven Rostedtd7690412008-10-01 00:29:53 -04003038 rb_advance_reader(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003039 goto again;
3040
3041 case RINGBUF_TYPE_DATA:
3042 if (ts) {
3043 *ts = cpu_buffer->read_stamp + event->time_delta;
Robert Richterd8eeb2d2009-07-31 14:58:04 +02003044 ring_buffer_normalize_time_stamp(cpu_buffer->buffer,
Steven Rostedt37886f62009-03-17 17:22:06 -04003045 cpu_buffer->cpu, ts);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003046 }
3047 return event;
3048
3049 default:
3050 BUG();
3051 }
3052
3053 return NULL;
3054}
Robert Richterc4f50182008-12-11 16:49:22 +01003055EXPORT_SYMBOL_GPL(ring_buffer_peek);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003056
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003057static struct ring_buffer_event *
3058rb_iter_peek(struct ring_buffer_iter *iter, u64 *ts)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003059{
3060 struct ring_buffer *buffer;
3061 struct ring_buffer_per_cpu *cpu_buffer;
3062 struct ring_buffer_event *event;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003063 int nr_loops = 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003064
3065 if (ring_buffer_iter_empty(iter))
3066 return NULL;
3067
3068 cpu_buffer = iter->cpu_buffer;
3069 buffer = cpu_buffer->buffer;
3070
3071 again:
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003072 /*
Steven Rostedtea05b572009-06-03 09:30:10 -04003073 * We repeat when a timestamp is encountered.
3074 * We can get multiple timestamps by nested interrupts or also
3075 * if filtering is on (discarding commits). Since discarding
3076 * commits can be frequent we can get a lot of timestamps.
3077 * But we limit them by not adding timestamps if they begin
3078 * at the start of a page.
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003079 */
Steven Rostedtea05b572009-06-03 09:30:10 -04003080 if (RB_WARN_ON(cpu_buffer, ++nr_loops > RB_TIMESTAMPS_PER_PAGE))
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003081 return NULL;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003082
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003083 if (rb_per_cpu_empty(cpu_buffer))
3084 return NULL;
3085
3086 event = rb_iter_head_event(iter);
3087
Lai Jiangshan334d4162009-04-24 11:27:05 +08003088 switch (event->type_len) {
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003089 case RINGBUF_TYPE_PADDING:
Tom Zanussi2d622712009-03-22 03:30:49 -05003090 if (rb_null_event(event)) {
3091 rb_inc_iter(iter);
3092 goto again;
3093 }
3094 rb_advance_iter(iter);
3095 return event;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003096
3097 case RINGBUF_TYPE_TIME_EXTEND:
3098 /* Internal data, OK to advance */
3099 rb_advance_iter(iter);
3100 goto again;
3101
3102 case RINGBUF_TYPE_TIME_STAMP:
3103 /* FIXME: not implemented */
3104 rb_advance_iter(iter);
3105 goto again;
3106
3107 case RINGBUF_TYPE_DATA:
3108 if (ts) {
3109 *ts = iter->read_stamp + event->time_delta;
Steven Rostedt37886f62009-03-17 17:22:06 -04003110 ring_buffer_normalize_time_stamp(buffer,
3111 cpu_buffer->cpu, ts);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003112 }
3113 return event;
3114
3115 default:
3116 BUG();
3117 }
3118
3119 return NULL;
3120}
Robert Richterc4f50182008-12-11 16:49:22 +01003121EXPORT_SYMBOL_GPL(ring_buffer_iter_peek);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003122
Steven Rostedt8d707e82009-06-16 21:22:48 -04003123static inline int rb_ok_to_lock(void)
3124{
3125 /*
3126 * If an NMI die dumps out the content of the ring buffer
3127 * do not grab locks. We also permanently disable the ring
3128 * buffer too. A one time deal is all you get from reading
3129 * the ring buffer from an NMI.
3130 */
Steven Rostedt464e85e2009-08-05 15:26:37 -04003131 if (likely(!in_nmi()))
Steven Rostedt8d707e82009-06-16 21:22:48 -04003132 return 1;
3133
3134 tracing_off_permanent();
3135 return 0;
3136}
3137
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003138/**
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003139 * ring_buffer_peek - peek at the next event to be read
3140 * @buffer: The ring buffer to read
3141 * @cpu: The cpu to peak at
3142 * @ts: The timestamp counter of this event.
3143 *
3144 * This will return the event that will be read next, but does
3145 * not consume the data.
3146 */
3147struct ring_buffer_event *
3148ring_buffer_peek(struct ring_buffer *buffer, int cpu, u64 *ts)
3149{
3150 struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
Steven Rostedt8aabee52009-03-12 13:13:49 -04003151 struct ring_buffer_event *event;
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003152 unsigned long flags;
Steven Rostedt8d707e82009-06-16 21:22:48 -04003153 int dolock;
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003154
Steven Rostedt554f7862009-03-11 22:00:13 -04003155 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04003156 return NULL;
Steven Rostedt554f7862009-03-11 22:00:13 -04003157
Steven Rostedt8d707e82009-06-16 21:22:48 -04003158 dolock = rb_ok_to_lock();
Tom Zanussi2d622712009-03-22 03:30:49 -05003159 again:
Steven Rostedt8d707e82009-06-16 21:22:48 -04003160 local_irq_save(flags);
3161 if (dolock)
3162 spin_lock(&cpu_buffer->reader_lock);
Robert Richterd8eeb2d2009-07-31 14:58:04 +02003163 event = rb_buffer_peek(cpu_buffer, ts);
Robert Richter469535a2009-07-30 19:19:18 +02003164 if (event && event->type_len == RINGBUF_TYPE_PADDING)
3165 rb_advance_reader(cpu_buffer);
Steven Rostedt8d707e82009-06-16 21:22:48 -04003166 if (dolock)
3167 spin_unlock(&cpu_buffer->reader_lock);
3168 local_irq_restore(flags);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003169
Steven Rostedt1b959e12009-09-03 10:12:13 -04003170 if (event && event->type_len == RINGBUF_TYPE_PADDING)
Tom Zanussi2d622712009-03-22 03:30:49 -05003171 goto again;
Tom Zanussi2d622712009-03-22 03:30:49 -05003172
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003173 return event;
3174}
3175
3176/**
3177 * ring_buffer_iter_peek - peek at the next event to be read
3178 * @iter: The ring buffer iterator
3179 * @ts: The timestamp counter of this event.
3180 *
3181 * This will return the event that will be read next, but does
3182 * not increment the iterator.
3183 */
3184struct ring_buffer_event *
3185ring_buffer_iter_peek(struct ring_buffer_iter *iter, u64 *ts)
3186{
3187 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
3188 struct ring_buffer_event *event;
3189 unsigned long flags;
3190
Tom Zanussi2d622712009-03-22 03:30:49 -05003191 again:
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003192 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
3193 event = rb_iter_peek(iter, ts);
3194 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
3195
Steven Rostedt1b959e12009-09-03 10:12:13 -04003196 if (event && event->type_len == RINGBUF_TYPE_PADDING)
Tom Zanussi2d622712009-03-22 03:30:49 -05003197 goto again;
Tom Zanussi2d622712009-03-22 03:30:49 -05003198
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003199 return event;
3200}
3201
3202/**
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003203 * ring_buffer_consume - return an event and consume it
3204 * @buffer: The ring buffer to get the next event from
3205 *
3206 * Returns the next event in the ring buffer, and that event is consumed.
3207 * Meaning, that sequential reads will keep returning a different event,
3208 * and eventually empty the ring buffer if the producer is slower.
3209 */
3210struct ring_buffer_event *
3211ring_buffer_consume(struct ring_buffer *buffer, int cpu, u64 *ts)
3212{
Steven Rostedt554f7862009-03-11 22:00:13 -04003213 struct ring_buffer_per_cpu *cpu_buffer;
3214 struct ring_buffer_event *event = NULL;
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003215 unsigned long flags;
Steven Rostedt8d707e82009-06-16 21:22:48 -04003216 int dolock;
3217
3218 dolock = rb_ok_to_lock();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003219
Tom Zanussi2d622712009-03-22 03:30:49 -05003220 again:
Steven Rostedt554f7862009-03-11 22:00:13 -04003221 /* might be called in atomic */
3222 preempt_disable();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003223
Steven Rostedt554f7862009-03-11 22:00:13 -04003224 if (!cpumask_test_cpu(cpu, buffer->cpumask))
3225 goto out;
3226
3227 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt8d707e82009-06-16 21:22:48 -04003228 local_irq_save(flags);
3229 if (dolock)
3230 spin_lock(&cpu_buffer->reader_lock);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003231
Robert Richterd8eeb2d2009-07-31 14:58:04 +02003232 event = rb_buffer_peek(cpu_buffer, ts);
Robert Richter469535a2009-07-30 19:19:18 +02003233 if (event)
3234 rb_advance_reader(cpu_buffer);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003235
Steven Rostedt8d707e82009-06-16 21:22:48 -04003236 if (dolock)
3237 spin_unlock(&cpu_buffer->reader_lock);
3238 local_irq_restore(flags);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003239
Steven Rostedt554f7862009-03-11 22:00:13 -04003240 out:
3241 preempt_enable();
3242
Steven Rostedt1b959e12009-09-03 10:12:13 -04003243 if (event && event->type_len == RINGBUF_TYPE_PADDING)
Tom Zanussi2d622712009-03-22 03:30:49 -05003244 goto again;
Tom Zanussi2d622712009-03-22 03:30:49 -05003245
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003246 return event;
3247}
Robert Richterc4f50182008-12-11 16:49:22 +01003248EXPORT_SYMBOL_GPL(ring_buffer_consume);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003249
3250/**
3251 * ring_buffer_read_start - start a non consuming read of the buffer
3252 * @buffer: The ring buffer to read from
3253 * @cpu: The cpu buffer to iterate over
3254 *
3255 * This starts up an iteration through the buffer. It also disables
3256 * the recording to the buffer until the reading is finished.
3257 * This prevents the reading from being corrupted. This is not
3258 * a consuming read, so a producer is not expected.
3259 *
3260 * Must be paired with ring_buffer_finish.
3261 */
3262struct ring_buffer_iter *
3263ring_buffer_read_start(struct ring_buffer *buffer, int cpu)
3264{
3265 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedt8aabee52009-03-12 13:13:49 -04003266 struct ring_buffer_iter *iter;
Steven Rostedtd7690412008-10-01 00:29:53 -04003267 unsigned long flags;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003268
Rusty Russell9e01c1b2009-01-01 10:12:22 +10303269 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04003270 return NULL;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003271
3272 iter = kmalloc(sizeof(*iter), GFP_KERNEL);
3273 if (!iter)
Steven Rostedt8aabee52009-03-12 13:13:49 -04003274 return NULL;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003275
3276 cpu_buffer = buffer->buffers[cpu];
3277
3278 iter->cpu_buffer = cpu_buffer;
3279
3280 atomic_inc(&cpu_buffer->record_disabled);
3281 synchronize_sched();
3282
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003283 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
Steven Rostedt3e03fb72008-11-06 00:09:43 -05003284 __raw_spin_lock(&cpu_buffer->lock);
Steven Rostedt642edba2008-11-12 00:01:26 -05003285 rb_iter_reset(iter);
Steven Rostedt3e03fb72008-11-06 00:09:43 -05003286 __raw_spin_unlock(&cpu_buffer->lock);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003287 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003288
3289 return iter;
3290}
Robert Richterc4f50182008-12-11 16:49:22 +01003291EXPORT_SYMBOL_GPL(ring_buffer_read_start);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003292
3293/**
3294 * ring_buffer_finish - finish reading the iterator of the buffer
3295 * @iter: The iterator retrieved by ring_buffer_start
3296 *
3297 * This re-enables the recording to the buffer, and frees the
3298 * iterator.
3299 */
3300void
3301ring_buffer_read_finish(struct ring_buffer_iter *iter)
3302{
3303 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
3304
3305 atomic_dec(&cpu_buffer->record_disabled);
3306 kfree(iter);
3307}
Robert Richterc4f50182008-12-11 16:49:22 +01003308EXPORT_SYMBOL_GPL(ring_buffer_read_finish);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003309
3310/**
3311 * ring_buffer_read - read the next item in the ring buffer by the iterator
3312 * @iter: The ring buffer iterator
3313 * @ts: The time stamp of the event read.
3314 *
3315 * This reads the next event in the ring buffer and increments the iterator.
3316 */
3317struct ring_buffer_event *
3318ring_buffer_read(struct ring_buffer_iter *iter, u64 *ts)
3319{
3320 struct ring_buffer_event *event;
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003321 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
3322 unsigned long flags;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003323
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003324 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
Steven Rostedt7e9391c2009-09-03 10:02:09 -04003325 again:
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003326 event = rb_iter_peek(iter, ts);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003327 if (!event)
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003328 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003329
Steven Rostedt7e9391c2009-09-03 10:02:09 -04003330 if (event->type_len == RINGBUF_TYPE_PADDING)
3331 goto again;
3332
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003333 rb_advance_iter(iter);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003334 out:
3335 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003336
3337 return event;
3338}
Robert Richterc4f50182008-12-11 16:49:22 +01003339EXPORT_SYMBOL_GPL(ring_buffer_read);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003340
3341/**
3342 * ring_buffer_size - return the size of the ring buffer (in bytes)
3343 * @buffer: The ring buffer.
3344 */
3345unsigned long ring_buffer_size(struct ring_buffer *buffer)
3346{
3347 return BUF_PAGE_SIZE * buffer->pages;
3348}
Robert Richterc4f50182008-12-11 16:49:22 +01003349EXPORT_SYMBOL_GPL(ring_buffer_size);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003350
3351static void
3352rb_reset_cpu(struct ring_buffer_per_cpu *cpu_buffer)
3353{
Steven Rostedt77ae3652009-03-27 11:00:29 -04003354 rb_head_page_deactivate(cpu_buffer);
3355
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003356 cpu_buffer->head_page
Steven Rostedt3adc54f2009-03-30 15:32:01 -04003357 = list_entry(cpu_buffer->pages, struct buffer_page, list);
Steven Rostedtbf41a152008-10-04 02:00:59 -04003358 local_set(&cpu_buffer->head_page->write, 0);
Steven Rostedt778c55d2009-05-01 18:44:45 -04003359 local_set(&cpu_buffer->head_page->entries, 0);
Steven Rostedtabc9b562008-12-02 15:34:06 -05003360 local_set(&cpu_buffer->head_page->page->commit, 0);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003361
Steven Rostedt6f807ac2008-10-04 02:00:58 -04003362 cpu_buffer->head_page->read = 0;
Steven Rostedtbf41a152008-10-04 02:00:59 -04003363
3364 cpu_buffer->tail_page = cpu_buffer->head_page;
3365 cpu_buffer->commit_page = cpu_buffer->head_page;
3366
3367 INIT_LIST_HEAD(&cpu_buffer->reader_page->list);
3368 local_set(&cpu_buffer->reader_page->write, 0);
Steven Rostedt778c55d2009-05-01 18:44:45 -04003369 local_set(&cpu_buffer->reader_page->entries, 0);
Steven Rostedtabc9b562008-12-02 15:34:06 -05003370 local_set(&cpu_buffer->reader_page->page->commit, 0);
Steven Rostedt6f807ac2008-10-04 02:00:58 -04003371 cpu_buffer->reader_page->read = 0;
Steven Rostedtd7690412008-10-01 00:29:53 -04003372
Steven Rostedt77ae3652009-03-27 11:00:29 -04003373 local_set(&cpu_buffer->commit_overrun, 0);
3374 local_set(&cpu_buffer->overrun, 0);
Steven Rostedte4906ef2009-04-30 20:49:44 -04003375 local_set(&cpu_buffer->entries, 0);
Steven Rostedtfa743952009-06-16 12:37:57 -04003376 local_set(&cpu_buffer->committing, 0);
3377 local_set(&cpu_buffer->commits, 0);
Steven Rostedt77ae3652009-03-27 11:00:29 -04003378 cpu_buffer->read = 0;
Steven Rostedt69507c02009-01-21 18:45:57 -05003379
3380 cpu_buffer->write_stamp = 0;
3381 cpu_buffer->read_stamp = 0;
Steven Rostedt77ae3652009-03-27 11:00:29 -04003382
3383 rb_head_page_activate(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003384}
3385
3386/**
3387 * ring_buffer_reset_cpu - reset a ring buffer per CPU buffer
3388 * @buffer: The ring buffer to reset a per cpu buffer of
3389 * @cpu: The CPU buffer to be reset
3390 */
3391void ring_buffer_reset_cpu(struct ring_buffer *buffer, int cpu)
3392{
3393 struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
3394 unsigned long flags;
3395
Rusty Russell9e01c1b2009-01-01 10:12:22 +10303396 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04003397 return;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003398
Steven Rostedt41ede232009-05-01 20:26:54 -04003399 atomic_inc(&cpu_buffer->record_disabled);
3400
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003401 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
3402
Steven Rostedt41b6a952009-09-02 09:59:48 -04003403 if (RB_WARN_ON(cpu_buffer, local_read(&cpu_buffer->committing)))
3404 goto out;
3405
Steven Rostedt3e03fb72008-11-06 00:09:43 -05003406 __raw_spin_lock(&cpu_buffer->lock);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003407
3408 rb_reset_cpu(cpu_buffer);
3409
Steven Rostedt3e03fb72008-11-06 00:09:43 -05003410 __raw_spin_unlock(&cpu_buffer->lock);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003411
Steven Rostedt41b6a952009-09-02 09:59:48 -04003412 out:
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003413 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
Steven Rostedt41ede232009-05-01 20:26:54 -04003414
3415 atomic_dec(&cpu_buffer->record_disabled);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003416}
Robert Richterc4f50182008-12-11 16:49:22 +01003417EXPORT_SYMBOL_GPL(ring_buffer_reset_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003418
3419/**
3420 * ring_buffer_reset - reset a ring buffer
3421 * @buffer: The ring buffer to reset all cpu buffers
3422 */
3423void ring_buffer_reset(struct ring_buffer *buffer)
3424{
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003425 int cpu;
3426
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003427 for_each_buffer_cpu(buffer, cpu)
Steven Rostedtd7690412008-10-01 00:29:53 -04003428 ring_buffer_reset_cpu(buffer, cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003429}
Robert Richterc4f50182008-12-11 16:49:22 +01003430EXPORT_SYMBOL_GPL(ring_buffer_reset);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003431
3432/**
3433 * rind_buffer_empty - is the ring buffer empty?
3434 * @buffer: The ring buffer to test
3435 */
3436int ring_buffer_empty(struct ring_buffer *buffer)
3437{
3438 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedtd4788202009-06-17 00:39:43 -04003439 unsigned long flags;
Steven Rostedt8d707e82009-06-16 21:22:48 -04003440 int dolock;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003441 int cpu;
Steven Rostedtd4788202009-06-17 00:39:43 -04003442 int ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003443
Steven Rostedt8d707e82009-06-16 21:22:48 -04003444 dolock = rb_ok_to_lock();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003445
3446 /* yes this is racy, but if you don't like the race, lock the buffer */
3447 for_each_buffer_cpu(buffer, cpu) {
3448 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt8d707e82009-06-16 21:22:48 -04003449 local_irq_save(flags);
3450 if (dolock)
3451 spin_lock(&cpu_buffer->reader_lock);
Steven Rostedtd4788202009-06-17 00:39:43 -04003452 ret = rb_per_cpu_empty(cpu_buffer);
Steven Rostedt8d707e82009-06-16 21:22:48 -04003453 if (dolock)
3454 spin_unlock(&cpu_buffer->reader_lock);
3455 local_irq_restore(flags);
3456
Steven Rostedtd4788202009-06-17 00:39:43 -04003457 if (!ret)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003458 return 0;
3459 }
Steven Rostedt554f7862009-03-11 22:00:13 -04003460
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003461 return 1;
3462}
Robert Richterc4f50182008-12-11 16:49:22 +01003463EXPORT_SYMBOL_GPL(ring_buffer_empty);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003464
3465/**
3466 * ring_buffer_empty_cpu - is a cpu buffer of a ring buffer empty?
3467 * @buffer: The ring buffer
3468 * @cpu: The CPU buffer to test
3469 */
3470int ring_buffer_empty_cpu(struct ring_buffer *buffer, int cpu)
3471{
3472 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedtd4788202009-06-17 00:39:43 -04003473 unsigned long flags;
Steven Rostedt8d707e82009-06-16 21:22:48 -04003474 int dolock;
Steven Rostedt8aabee52009-03-12 13:13:49 -04003475 int ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003476
Rusty Russell9e01c1b2009-01-01 10:12:22 +10303477 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04003478 return 1;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003479
Steven Rostedt8d707e82009-06-16 21:22:48 -04003480 dolock = rb_ok_to_lock();
Steven Rostedt554f7862009-03-11 22:00:13 -04003481
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003482 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt8d707e82009-06-16 21:22:48 -04003483 local_irq_save(flags);
3484 if (dolock)
3485 spin_lock(&cpu_buffer->reader_lock);
Steven Rostedt554f7862009-03-11 22:00:13 -04003486 ret = rb_per_cpu_empty(cpu_buffer);
Steven Rostedt8d707e82009-06-16 21:22:48 -04003487 if (dolock)
3488 spin_unlock(&cpu_buffer->reader_lock);
3489 local_irq_restore(flags);
Steven Rostedt554f7862009-03-11 22:00:13 -04003490
3491 return ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003492}
Robert Richterc4f50182008-12-11 16:49:22 +01003493EXPORT_SYMBOL_GPL(ring_buffer_empty_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003494
Steven Rostedt85bac322009-09-04 14:24:40 -04003495#ifdef CONFIG_RING_BUFFER_ALLOW_SWAP
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003496/**
3497 * ring_buffer_swap_cpu - swap a CPU buffer between two ring buffers
3498 * @buffer_a: One buffer to swap with
3499 * @buffer_b: The other buffer to swap with
3500 *
3501 * This function is useful for tracers that want to take a "snapshot"
3502 * of a CPU buffer and has another back up buffer lying around.
3503 * it is expected that the tracer handles the cpu buffer not being
3504 * used at the moment.
3505 */
3506int ring_buffer_swap_cpu(struct ring_buffer *buffer_a,
3507 struct ring_buffer *buffer_b, int cpu)
3508{
3509 struct ring_buffer_per_cpu *cpu_buffer_a;
3510 struct ring_buffer_per_cpu *cpu_buffer_b;
Steven Rostedt554f7862009-03-11 22:00:13 -04003511 int ret = -EINVAL;
3512
Rusty Russell9e01c1b2009-01-01 10:12:22 +10303513 if (!cpumask_test_cpu(cpu, buffer_a->cpumask) ||
3514 !cpumask_test_cpu(cpu, buffer_b->cpumask))
Steven Rostedt554f7862009-03-11 22:00:13 -04003515 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003516
3517 /* At least make sure the two buffers are somewhat the same */
Lai Jiangshan6d102bc2008-12-17 17:48:23 +08003518 if (buffer_a->pages != buffer_b->pages)
Steven Rostedt554f7862009-03-11 22:00:13 -04003519 goto out;
3520
3521 ret = -EAGAIN;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003522
Steven Rostedt97b17ef2009-01-21 15:24:56 -05003523 if (ring_buffer_flags != RB_BUFFERS_ON)
Steven Rostedt554f7862009-03-11 22:00:13 -04003524 goto out;
Steven Rostedt97b17ef2009-01-21 15:24:56 -05003525
3526 if (atomic_read(&buffer_a->record_disabled))
Steven Rostedt554f7862009-03-11 22:00:13 -04003527 goto out;
Steven Rostedt97b17ef2009-01-21 15:24:56 -05003528
3529 if (atomic_read(&buffer_b->record_disabled))
Steven Rostedt554f7862009-03-11 22:00:13 -04003530 goto out;
Steven Rostedt97b17ef2009-01-21 15:24:56 -05003531
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003532 cpu_buffer_a = buffer_a->buffers[cpu];
3533 cpu_buffer_b = buffer_b->buffers[cpu];
3534
Steven Rostedt97b17ef2009-01-21 15:24:56 -05003535 if (atomic_read(&cpu_buffer_a->record_disabled))
Steven Rostedt554f7862009-03-11 22:00:13 -04003536 goto out;
Steven Rostedt97b17ef2009-01-21 15:24:56 -05003537
3538 if (atomic_read(&cpu_buffer_b->record_disabled))
Steven Rostedt554f7862009-03-11 22:00:13 -04003539 goto out;
Steven Rostedt97b17ef2009-01-21 15:24:56 -05003540
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003541 /*
3542 * We can't do a synchronize_sched here because this
3543 * function can be called in atomic context.
3544 * Normally this will be called from the same CPU as cpu.
3545 * If not it's up to the caller to protect this.
3546 */
3547 atomic_inc(&cpu_buffer_a->record_disabled);
3548 atomic_inc(&cpu_buffer_b->record_disabled);
3549
Steven Rostedt98277992009-09-02 10:56:15 -04003550 ret = -EBUSY;
3551 if (local_read(&cpu_buffer_a->committing))
3552 goto out_dec;
3553 if (local_read(&cpu_buffer_b->committing))
3554 goto out_dec;
3555
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003556 buffer_a->buffers[cpu] = cpu_buffer_b;
3557 buffer_b->buffers[cpu] = cpu_buffer_a;
3558
3559 cpu_buffer_b->buffer = buffer_a;
3560 cpu_buffer_a->buffer = buffer_b;
3561
Steven Rostedt98277992009-09-02 10:56:15 -04003562 ret = 0;
3563
3564out_dec:
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003565 atomic_dec(&cpu_buffer_a->record_disabled);
3566 atomic_dec(&cpu_buffer_b->record_disabled);
Steven Rostedt554f7862009-03-11 22:00:13 -04003567out:
Steven Rostedt554f7862009-03-11 22:00:13 -04003568 return ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003569}
Robert Richterc4f50182008-12-11 16:49:22 +01003570EXPORT_SYMBOL_GPL(ring_buffer_swap_cpu);
Steven Rostedt85bac322009-09-04 14:24:40 -04003571#endif /* CONFIG_RING_BUFFER_ALLOW_SWAP */
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003572
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003573/**
3574 * ring_buffer_alloc_read_page - allocate a page to read from buffer
3575 * @buffer: the buffer to allocate for.
3576 *
3577 * This function is used in conjunction with ring_buffer_read_page.
3578 * When reading a full page from the ring buffer, these functions
3579 * can be used to speed up the process. The calling function should
3580 * allocate a few pages first with this function. Then when it
3581 * needs to get pages from the ring buffer, it passes the result
3582 * of this function into ring_buffer_read_page, which will swap
3583 * the page that was allocated, with the read page of the buffer.
3584 *
3585 * Returns:
3586 * The page allocated, or NULL on error.
3587 */
3588void *ring_buffer_alloc_read_page(struct ring_buffer *buffer)
3589{
Steven Rostedt044fa782008-12-02 23:50:03 -05003590 struct buffer_data_page *bpage;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003591 unsigned long addr;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003592
3593 addr = __get_free_page(GFP_KERNEL);
3594 if (!addr)
3595 return NULL;
3596
Steven Rostedt044fa782008-12-02 23:50:03 -05003597 bpage = (void *)addr;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003598
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003599 rb_init_page(bpage);
3600
Steven Rostedt044fa782008-12-02 23:50:03 -05003601 return bpage;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003602}
Steven Rostedtd6ce96d2009-05-05 01:15:24 -04003603EXPORT_SYMBOL_GPL(ring_buffer_alloc_read_page);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003604
3605/**
3606 * ring_buffer_free_read_page - free an allocated read page
3607 * @buffer: the buffer the page was allocate for
3608 * @data: the page to free
3609 *
3610 * Free a page allocated from ring_buffer_alloc_read_page.
3611 */
3612void ring_buffer_free_read_page(struct ring_buffer *buffer, void *data)
3613{
3614 free_page((unsigned long)data);
3615}
Steven Rostedtd6ce96d2009-05-05 01:15:24 -04003616EXPORT_SYMBOL_GPL(ring_buffer_free_read_page);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003617
3618/**
3619 * ring_buffer_read_page - extract a page from the ring buffer
3620 * @buffer: buffer to extract from
3621 * @data_page: the page to use allocated from ring_buffer_alloc_read_page
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003622 * @len: amount to extract
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003623 * @cpu: the cpu of the buffer to extract
3624 * @full: should the extraction only happen when the page is full.
3625 *
3626 * This function will pull out a page from the ring buffer and consume it.
3627 * @data_page must be the address of the variable that was returned
3628 * from ring_buffer_alloc_read_page. This is because the page might be used
3629 * to swap with a page in the ring buffer.
3630 *
3631 * for example:
Lai Jiangshanb85fa012009-02-09 14:21:14 +08003632 * rpage = ring_buffer_alloc_read_page(buffer);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003633 * if (!rpage)
3634 * return error;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003635 * ret = ring_buffer_read_page(buffer, &rpage, len, cpu, 0);
Lai Jiangshan667d2412009-02-09 14:21:17 +08003636 * if (ret >= 0)
3637 * process_page(rpage, ret);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003638 *
3639 * When @full is set, the function will not return true unless
3640 * the writer is off the reader page.
3641 *
3642 * Note: it is up to the calling functions to handle sleeps and wakeups.
3643 * The ring buffer can be used anywhere in the kernel and can not
3644 * blindly call wake_up. The layer that uses the ring buffer must be
3645 * responsible for that.
3646 *
3647 * Returns:
Lai Jiangshan667d2412009-02-09 14:21:17 +08003648 * >=0 if data has been transferred, returns the offset of consumed data.
3649 * <0 if no data has been transferred.
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003650 */
3651int ring_buffer_read_page(struct ring_buffer *buffer,
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003652 void **data_page, size_t len, int cpu, int full)
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003653{
3654 struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
3655 struct ring_buffer_event *event;
Steven Rostedt044fa782008-12-02 23:50:03 -05003656 struct buffer_data_page *bpage;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003657 struct buffer_page *reader;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003658 unsigned long flags;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003659 unsigned int commit;
Lai Jiangshan667d2412009-02-09 14:21:17 +08003660 unsigned int read;
Steven Rostedt4f3640f2009-03-03 23:52:42 -05003661 u64 save_timestamp;
Lai Jiangshan667d2412009-02-09 14:21:17 +08003662 int ret = -1;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003663
Steven Rostedt554f7862009-03-11 22:00:13 -04003664 if (!cpumask_test_cpu(cpu, buffer->cpumask))
3665 goto out;
3666
Steven Rostedt474d32b2009-03-03 19:51:40 -05003667 /*
3668 * If len is not big enough to hold the page header, then
3669 * we can not copy anything.
3670 */
3671 if (len <= BUF_PAGE_HDR_SIZE)
Steven Rostedt554f7862009-03-11 22:00:13 -04003672 goto out;
Steven Rostedt474d32b2009-03-03 19:51:40 -05003673
3674 len -= BUF_PAGE_HDR_SIZE;
3675
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003676 if (!data_page)
Steven Rostedt554f7862009-03-11 22:00:13 -04003677 goto out;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003678
Steven Rostedt044fa782008-12-02 23:50:03 -05003679 bpage = *data_page;
3680 if (!bpage)
Steven Rostedt554f7862009-03-11 22:00:13 -04003681 goto out;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003682
3683 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
3684
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003685 reader = rb_get_reader_page(cpu_buffer);
3686 if (!reader)
Steven Rostedt554f7862009-03-11 22:00:13 -04003687 goto out_unlock;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003688
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003689 event = rb_reader_event(cpu_buffer);
Lai Jiangshan667d2412009-02-09 14:21:17 +08003690
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003691 read = reader->read;
3692 commit = rb_page_commit(reader);
3693
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003694 /*
Steven Rostedt474d32b2009-03-03 19:51:40 -05003695 * If this page has been partially read or
3696 * if len is not big enough to read the rest of the page or
3697 * a writer is still on the page, then
3698 * we must copy the data from the page to the buffer.
3699 * Otherwise, we can simply swap the page with the one passed in.
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003700 */
Steven Rostedt474d32b2009-03-03 19:51:40 -05003701 if (read || (len < (commit - read)) ||
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003702 cpu_buffer->reader_page == cpu_buffer->commit_page) {
Lai Jiangshan667d2412009-02-09 14:21:17 +08003703 struct buffer_data_page *rpage = cpu_buffer->reader_page->page;
Steven Rostedt474d32b2009-03-03 19:51:40 -05003704 unsigned int rpos = read;
3705 unsigned int pos = 0;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003706 unsigned int size;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003707
3708 if (full)
Steven Rostedt554f7862009-03-11 22:00:13 -04003709 goto out_unlock;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003710
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003711 if (len > (commit - read))
3712 len = (commit - read);
3713
3714 size = rb_event_length(event);
3715
3716 if (len < size)
Steven Rostedt554f7862009-03-11 22:00:13 -04003717 goto out_unlock;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003718
Steven Rostedt4f3640f2009-03-03 23:52:42 -05003719 /* save the current timestamp, since the user will need it */
3720 save_timestamp = cpu_buffer->read_stamp;
3721
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003722 /* Need to copy one event at a time */
3723 do {
Steven Rostedt474d32b2009-03-03 19:51:40 -05003724 memcpy(bpage->data + pos, rpage->data + rpos, size);
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003725
3726 len -= size;
3727
3728 rb_advance_reader(cpu_buffer);
Steven Rostedt474d32b2009-03-03 19:51:40 -05003729 rpos = reader->read;
3730 pos += size;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003731
3732 event = rb_reader_event(cpu_buffer);
3733 size = rb_event_length(event);
3734 } while (len > size);
Lai Jiangshan667d2412009-02-09 14:21:17 +08003735
3736 /* update bpage */
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003737 local_set(&bpage->commit, pos);
Steven Rostedt4f3640f2009-03-03 23:52:42 -05003738 bpage->time_stamp = save_timestamp;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003739
Steven Rostedt474d32b2009-03-03 19:51:40 -05003740 /* we copied everything to the beginning */
3741 read = 0;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003742 } else {
Steven Rostedtafbab762009-05-01 19:40:05 -04003743 /* update the entry counter */
Steven Rostedt77ae3652009-03-27 11:00:29 -04003744 cpu_buffer->read += rb_page_entries(reader);
Steven Rostedtafbab762009-05-01 19:40:05 -04003745
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003746 /* swap the pages */
Steven Rostedt044fa782008-12-02 23:50:03 -05003747 rb_init_page(bpage);
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003748 bpage = reader->page;
3749 reader->page = *data_page;
3750 local_set(&reader->write, 0);
Steven Rostedt778c55d2009-05-01 18:44:45 -04003751 local_set(&reader->entries, 0);
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003752 reader->read = 0;
Steven Rostedt044fa782008-12-02 23:50:03 -05003753 *data_page = bpage;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003754 }
Lai Jiangshan667d2412009-02-09 14:21:17 +08003755 ret = read;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003756
Steven Rostedt554f7862009-03-11 22:00:13 -04003757 out_unlock:
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003758 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
3759
Steven Rostedt554f7862009-03-11 22:00:13 -04003760 out:
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003761 return ret;
3762}
Steven Rostedtd6ce96d2009-05-05 01:15:24 -04003763EXPORT_SYMBOL_GPL(ring_buffer_read_page);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003764
Paul Mundt1155de42009-06-25 14:30:12 +09003765#ifdef CONFIG_TRACING
Steven Rostedta3583242008-11-11 15:01:42 -05003766static ssize_t
3767rb_simple_read(struct file *filp, char __user *ubuf,
3768 size_t cnt, loff_t *ppos)
3769{
Hannes Eder5e398412009-02-10 19:44:34 +01003770 unsigned long *p = filp->private_data;
Steven Rostedta3583242008-11-11 15:01:42 -05003771 char buf[64];
3772 int r;
3773
Steven Rostedt033601a2008-11-21 12:41:55 -05003774 if (test_bit(RB_BUFFERS_DISABLED_BIT, p))
3775 r = sprintf(buf, "permanently disabled\n");
3776 else
3777 r = sprintf(buf, "%d\n", test_bit(RB_BUFFERS_ON_BIT, p));
Steven Rostedta3583242008-11-11 15:01:42 -05003778
3779 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
3780}
3781
3782static ssize_t
3783rb_simple_write(struct file *filp, const char __user *ubuf,
3784 size_t cnt, loff_t *ppos)
3785{
Hannes Eder5e398412009-02-10 19:44:34 +01003786 unsigned long *p = filp->private_data;
Steven Rostedta3583242008-11-11 15:01:42 -05003787 char buf[64];
Hannes Eder5e398412009-02-10 19:44:34 +01003788 unsigned long val;
Steven Rostedta3583242008-11-11 15:01:42 -05003789 int ret;
3790
3791 if (cnt >= sizeof(buf))
3792 return -EINVAL;
3793
3794 if (copy_from_user(&buf, ubuf, cnt))
3795 return -EFAULT;
3796
3797 buf[cnt] = 0;
3798
3799 ret = strict_strtoul(buf, 10, &val);
3800 if (ret < 0)
3801 return ret;
3802
Steven Rostedt033601a2008-11-21 12:41:55 -05003803 if (val)
3804 set_bit(RB_BUFFERS_ON_BIT, p);
3805 else
3806 clear_bit(RB_BUFFERS_ON_BIT, p);
Steven Rostedta3583242008-11-11 15:01:42 -05003807
3808 (*ppos)++;
3809
3810 return cnt;
3811}
3812
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003813static const struct file_operations rb_simple_fops = {
Steven Rostedta3583242008-11-11 15:01:42 -05003814 .open = tracing_open_generic,
3815 .read = rb_simple_read,
3816 .write = rb_simple_write,
3817};
3818
3819
3820static __init int rb_init_debugfs(void)
3821{
3822 struct dentry *d_tracer;
Steven Rostedta3583242008-11-11 15:01:42 -05003823
3824 d_tracer = tracing_init_dentry();
3825
Frederic Weisbecker5452af62009-03-27 00:25:38 +01003826 trace_create_file("tracing_on", 0644, d_tracer,
3827 &ring_buffer_flags, &rb_simple_fops);
Steven Rostedta3583242008-11-11 15:01:42 -05003828
3829 return 0;
3830}
3831
3832fs_initcall(rb_init_debugfs);
Paul Mundt1155de42009-06-25 14:30:12 +09003833#endif
Steven Rostedt554f7862009-03-11 22:00:13 -04003834
Steven Rostedt59222ef2009-03-12 11:46:03 -04003835#ifdef CONFIG_HOTPLUG_CPU
Frederic Weisbecker09c9e842009-03-21 04:33:36 +01003836static int rb_cpu_notify(struct notifier_block *self,
3837 unsigned long action, void *hcpu)
Steven Rostedt554f7862009-03-11 22:00:13 -04003838{
3839 struct ring_buffer *buffer =
3840 container_of(self, struct ring_buffer, cpu_notify);
3841 long cpu = (long)hcpu;
3842
3843 switch (action) {
3844 case CPU_UP_PREPARE:
3845 case CPU_UP_PREPARE_FROZEN:
Rusty Russell3f237a72009-06-12 21:15:30 +09303846 if (cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt554f7862009-03-11 22:00:13 -04003847 return NOTIFY_OK;
3848
3849 buffer->buffers[cpu] =
3850 rb_allocate_cpu_buffer(buffer, cpu);
3851 if (!buffer->buffers[cpu]) {
3852 WARN(1, "failed to allocate ring buffer on CPU %ld\n",
3853 cpu);
3854 return NOTIFY_OK;
3855 }
3856 smp_wmb();
Rusty Russell3f237a72009-06-12 21:15:30 +09303857 cpumask_set_cpu(cpu, buffer->cpumask);
Steven Rostedt554f7862009-03-11 22:00:13 -04003858 break;
3859 case CPU_DOWN_PREPARE:
3860 case CPU_DOWN_PREPARE_FROZEN:
3861 /*
3862 * Do nothing.
3863 * If we were to free the buffer, then the user would
3864 * lose any trace that was in the buffer.
3865 */
3866 break;
3867 default:
3868 break;
3869 }
3870 return NOTIFY_OK;
3871}
3872#endif