blob: a28bdd17c853ed5cb9ce7fc1a843b82575579148 [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 */
Steven Rostedt0b074362013-01-22 16:58:30 -05006#include <linux/ftrace_event.h>
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04007#include <linux/ring_buffer.h>
Ingo Molnar14131f22009-02-26 18:47:11 +01008#include <linux/trace_clock.h>
Steven Rostedt0b074362013-01-22 16:58:30 -05009#include <linux/trace_seq.h>
Steven Rostedt7a8e76a2008-09-29 23:02:38 -040010#include <linux/spinlock.h>
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -050011#include <linux/irq_work.h>
Steven Rostedt7a8e76a2008-09-29 23:02:38 -040012#include <linux/debugfs.h>
13#include <linux/uaccess.h>
Steven Rostedta81bd802009-02-06 01:45:16 -050014#include <linux/hardirq.h>
Steven Rostedt (Red Hat)6c43e552013-03-15 11:32:53 -040015#include <linux/kthread.h> /* for self test */
Vegard Nossum1744a212009-02-28 08:29:44 +010016#include <linux/kmemcheck.h>
Steven Rostedt7a8e76a2008-09-29 23:02:38 -040017#include <linux/module.h>
18#include <linux/percpu.h>
19#include <linux/mutex.h>
Steven Rostedt (Red Hat)6c43e552013-03-15 11:32:53 -040020#include <linux/delay.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090021#include <linux/slab.h>
Steven Rostedt7a8e76a2008-09-29 23:02:38 -040022#include <linux/init.h>
23#include <linux/hash.h>
24#include <linux/list.h>
Steven Rostedt554f7862009-03-11 22:00:13 -040025#include <linux/cpu.h>
Steven Rostedt7a8e76a2008-09-29 23:02:38 -040026#include <linux/fs.h>
27
Christoph Lameter79615762010-01-05 15:34:50 +090028#include <asm/local.h>
Steven Rostedt182e9f52008-11-03 23:15:56 -050029
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -070030static void update_pages_handler(struct work_struct *work);
31
Steven Rostedt033601a2008-11-21 12:41:55 -050032/*
Steven Rostedtd1b182a2009-04-15 16:53:47 -040033 * The ring buffer header is special. We must manually up keep it.
34 */
35int ring_buffer_print_entry_header(struct trace_seq *s)
36{
Steven Rostedt (Red Hat)c0cd93a2014-11-12 11:49:00 -050037 trace_seq_puts(s, "# compressed entry header\n");
38 trace_seq_puts(s, "\ttype_len : 5 bits\n");
39 trace_seq_puts(s, "\ttime_delta : 27 bits\n");
40 trace_seq_puts(s, "\tarray : 32 bits\n");
41 trace_seq_putc(s, '\n');
42 trace_seq_printf(s, "\tpadding : type == %d\n",
43 RINGBUF_TYPE_PADDING);
44 trace_seq_printf(s, "\ttime_extend : type == %d\n",
45 RINGBUF_TYPE_TIME_EXTEND);
46 trace_seq_printf(s, "\tdata max type_len == %d\n",
47 RINGBUF_TYPE_DATA_TYPE_LEN_MAX);
Steven Rostedtd1b182a2009-04-15 16:53:47 -040048
Steven Rostedt (Red Hat)c0cd93a2014-11-12 11:49:00 -050049 return !trace_seq_has_overflowed(s);
Steven Rostedtd1b182a2009-04-15 16:53:47 -040050}
51
52/*
Steven Rostedt5cc98542009-03-12 22:24:17 -040053 * The ring buffer is made up of a list of pages. A separate list of pages is
54 * allocated for each CPU. A writer may only write to a buffer that is
55 * associated with the CPU it is currently executing on. A reader may read
56 * from any per cpu buffer.
57 *
58 * The reader is special. For each per cpu buffer, the reader has its own
59 * reader page. When a reader has read the entire reader page, this reader
60 * page is swapped with another page in the ring buffer.
61 *
62 * Now, as long as the writer is off the reader page, the reader can do what
63 * ever it wants with that page. The writer will never write to that page
64 * again (as long as it is out of the ring buffer).
65 *
66 * Here's some silly ASCII art.
67 *
68 * +------+
69 * |reader| RING BUFFER
70 * |page |
71 * +------+ +---+ +---+ +---+
72 * | |-->| |-->| |
73 * +---+ +---+ +---+
74 * ^ |
75 * | |
76 * +---------------+
77 *
78 *
79 * +------+
80 * |reader| RING BUFFER
81 * |page |------------------v
82 * +------+ +---+ +---+ +---+
83 * | |-->| |-->| |
84 * +---+ +---+ +---+
85 * ^ |
86 * | |
87 * +---------------+
88 *
89 *
90 * +------+
91 * |reader| RING BUFFER
92 * |page |------------------v
93 * +------+ +---+ +---+ +---+
94 * ^ | |-->| |-->| |
95 * | +---+ +---+ +---+
96 * | |
97 * | |
98 * +------------------------------+
99 *
100 *
101 * +------+
102 * |buffer| RING BUFFER
103 * |page |------------------v
104 * +------+ +---+ +---+ +---+
105 * ^ | | | |-->| |
106 * | New +---+ +---+ +---+
107 * | Reader------^ |
108 * | page |
109 * +------------------------------+
110 *
111 *
112 * After we make this swap, the reader can hand this page off to the splice
113 * code and be done with it. It can even allocate a new page if it needs to
114 * and swap that into the ring buffer.
115 *
116 * We will be using cmpxchg soon to make all this lockless.
117 *
118 */
119
120/*
Steven Rostedt033601a2008-11-21 12:41:55 -0500121 * A fast way to enable or disable all ring buffers is to
122 * call tracing_on or tracing_off. Turning off the ring buffers
123 * prevents all ring buffers from being recorded to.
124 * Turning this switch on, makes it OK to write to the
125 * ring buffer, if the ring buffer is enabled itself.
126 *
127 * There's three layers that must be on in order to write
128 * to the ring buffer.
129 *
130 * 1) This global flag must be set.
131 * 2) The ring buffer must be enabled for recording.
132 * 3) The per cpu buffer must be enabled for recording.
133 *
134 * In case of an anomaly, this global flag has a bit set that
135 * will permantly disable all ring buffers.
136 */
137
138/*
139 * Global flag to disable all recording to ring buffers
140 * This has two bits: ON, DISABLED
141 *
142 * ON DISABLED
143 * ---- ----------
144 * 0 0 : ring buffers are off
145 * 1 0 : ring buffers are on
146 * X 1 : ring buffers are permanently disabled
147 */
148
149enum {
150 RB_BUFFERS_ON_BIT = 0,
151 RB_BUFFERS_DISABLED_BIT = 1,
152};
153
154enum {
155 RB_BUFFERS_ON = 1 << RB_BUFFERS_ON_BIT,
156 RB_BUFFERS_DISABLED = 1 << RB_BUFFERS_DISABLED_BIT,
157};
158
Hannes Eder5e398412009-02-10 19:44:34 +0100159static unsigned long ring_buffer_flags __read_mostly = RB_BUFFERS_ON;
Steven Rostedta3583242008-11-11 15:01:42 -0500160
Steven Rostedt499e5472012-02-22 15:50:28 -0500161/* Used for individual buffers (after the counter) */
162#define RB_BUFFER_OFF (1 << 20)
163
Steven Rostedt474d32b2009-03-03 19:51:40 -0500164#define BUF_PAGE_HDR_SIZE offsetof(struct buffer_data_page, data)
165
Steven Rostedta3583242008-11-11 15:01:42 -0500166/**
Steven Rostedt033601a2008-11-21 12:41:55 -0500167 * tracing_off_permanent - permanently disable ring buffers
168 *
169 * This function, once called, will disable all ring buffers
Wenji Huangc3706f02009-02-10 01:03:18 -0500170 * permanently.
Steven Rostedt033601a2008-11-21 12:41:55 -0500171 */
172void tracing_off_permanent(void)
173{
174 set_bit(RB_BUFFERS_DISABLED_BIT, &ring_buffer_flags);
Steven Rostedta3583242008-11-11 15:01:42 -0500175}
176
Steven Rostedte3d6bf02009-03-03 13:53:07 -0500177#define RB_EVNT_HDR_SIZE (offsetof(struct ring_buffer_event, array))
Andrew Morton67d34722009-01-09 12:27:09 -0800178#define RB_ALIGNMENT 4U
Lai Jiangshan334d4162009-04-24 11:27:05 +0800179#define RB_MAX_SMALL_DATA (RB_ALIGNMENT * RINGBUF_TYPE_DATA_TYPE_LEN_MAX)
Steven Rostedtc7b09302009-06-11 11:12:00 -0400180#define RB_EVNT_MIN_SIZE 8U /* two 32bit words */
Lai Jiangshan334d4162009-04-24 11:27:05 +0800181
James Hogan649508f2012-05-30 12:11:19 +0100182#ifndef CONFIG_HAVE_64BIT_ALIGNED_ACCESS
Steven Rostedt22710482010-03-18 17:54:19 -0400183# define RB_FORCE_8BYTE_ALIGNMENT 0
184# define RB_ARCH_ALIGNMENT RB_ALIGNMENT
185#else
186# define RB_FORCE_8BYTE_ALIGNMENT 1
187# define RB_ARCH_ALIGNMENT 8U
188#endif
189
James Hogan649508f2012-05-30 12:11:19 +0100190#define RB_ALIGN_DATA __aligned(RB_ARCH_ALIGNMENT)
191
Lai Jiangshan334d4162009-04-24 11:27:05 +0800192/* define RINGBUF_TYPE_DATA for 'case RINGBUF_TYPE_DATA:' */
193#define RINGBUF_TYPE_DATA 0 ... RINGBUF_TYPE_DATA_TYPE_LEN_MAX
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400194
195enum {
196 RB_LEN_TIME_EXTEND = 8,
197 RB_LEN_TIME_STAMP = 16,
198};
199
Steven Rostedt69d1b832010-10-07 18:18:05 -0400200#define skip_time_extend(event) \
201 ((struct ring_buffer_event *)((char *)event + RB_LEN_TIME_EXTEND))
202
Tom Zanussi2d622712009-03-22 03:30:49 -0500203static inline int rb_null_event(struct ring_buffer_event *event)
204{
Steven Rostedta1863c22009-09-03 10:23:58 -0400205 return event->type_len == RINGBUF_TYPE_PADDING && !event->time_delta;
Tom Zanussi2d622712009-03-22 03:30:49 -0500206}
207
208static void rb_event_set_padding(struct ring_buffer_event *event)
209{
Steven Rostedta1863c22009-09-03 10:23:58 -0400210 /* padding has a NULL time_delta */
Lai Jiangshan334d4162009-04-24 11:27:05 +0800211 event->type_len = RINGBUF_TYPE_PADDING;
Tom Zanussi2d622712009-03-22 03:30:49 -0500212 event->time_delta = 0;
213}
214
Tom Zanussi2d622712009-03-22 03:30:49 -0500215static unsigned
216rb_event_data_length(struct ring_buffer_event *event)
217{
218 unsigned length;
219
Lai Jiangshan334d4162009-04-24 11:27:05 +0800220 if (event->type_len)
221 length = event->type_len * RB_ALIGNMENT;
Tom Zanussi2d622712009-03-22 03:30:49 -0500222 else
223 length = event->array[0];
224 return length + RB_EVNT_HDR_SIZE;
225}
226
Steven Rostedt69d1b832010-10-07 18:18:05 -0400227/*
228 * Return the length of the given event. Will return
229 * the length of the time extend if the event is a
230 * time extend.
231 */
232static inline unsigned
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400233rb_event_length(struct ring_buffer_event *event)
234{
Lai Jiangshan334d4162009-04-24 11:27:05 +0800235 switch (event->type_len) {
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400236 case RINGBUF_TYPE_PADDING:
Tom Zanussi2d622712009-03-22 03:30:49 -0500237 if (rb_null_event(event))
238 /* undefined */
239 return -1;
Lai Jiangshan334d4162009-04-24 11:27:05 +0800240 return event->array[0] + RB_EVNT_HDR_SIZE;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400241
242 case RINGBUF_TYPE_TIME_EXTEND:
243 return RB_LEN_TIME_EXTEND;
244
245 case RINGBUF_TYPE_TIME_STAMP:
246 return RB_LEN_TIME_STAMP;
247
248 case RINGBUF_TYPE_DATA:
Tom Zanussi2d622712009-03-22 03:30:49 -0500249 return rb_event_data_length(event);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400250 default:
251 BUG();
252 }
253 /* not hit */
254 return 0;
255}
256
Steven Rostedt69d1b832010-10-07 18:18:05 -0400257/*
258 * Return total length of time extend and data,
259 * or just the event length for all other events.
260 */
261static inline unsigned
262rb_event_ts_length(struct ring_buffer_event *event)
263{
264 unsigned len = 0;
265
266 if (event->type_len == RINGBUF_TYPE_TIME_EXTEND) {
267 /* time extends include the data event after it */
268 len = RB_LEN_TIME_EXTEND;
269 event = skip_time_extend(event);
270 }
271 return len + rb_event_length(event);
272}
273
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400274/**
275 * ring_buffer_event_length - return the length of the event
276 * @event: the event to get the length of
Steven Rostedt69d1b832010-10-07 18:18:05 -0400277 *
278 * Returns the size of the data load of a data event.
279 * If the event is something other than a data event, it
280 * returns the size of the event itself. With the exception
281 * of a TIME EXTEND, where it still returns the size of the
282 * data load of the data event after it.
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400283 */
284unsigned ring_buffer_event_length(struct ring_buffer_event *event)
285{
Steven Rostedt69d1b832010-10-07 18:18:05 -0400286 unsigned length;
287
288 if (event->type_len == RINGBUF_TYPE_TIME_EXTEND)
289 event = skip_time_extend(event);
290
291 length = rb_event_length(event);
Lai Jiangshan334d4162009-04-24 11:27:05 +0800292 if (event->type_len > RINGBUF_TYPE_DATA_TYPE_LEN_MAX)
Robert Richter465634a2009-01-07 15:32:11 +0100293 return length;
294 length -= RB_EVNT_HDR_SIZE;
295 if (length > RB_MAX_SMALL_DATA + sizeof(event->array[0]))
296 length -= sizeof(event->array[0]);
297 return length;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400298}
Robert Richterc4f50182008-12-11 16:49:22 +0100299EXPORT_SYMBOL_GPL(ring_buffer_event_length);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400300
301/* inline for ring buffer fast paths */
Andrew Morton34a148b2009-01-09 12:27:09 -0800302static void *
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400303rb_event_data(struct ring_buffer_event *event)
304{
Steven Rostedt69d1b832010-10-07 18:18:05 -0400305 if (event->type_len == RINGBUF_TYPE_TIME_EXTEND)
306 event = skip_time_extend(event);
Lai Jiangshan334d4162009-04-24 11:27:05 +0800307 BUG_ON(event->type_len > RINGBUF_TYPE_DATA_TYPE_LEN_MAX);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400308 /* If length is in len field, then array[0] has the data */
Lai Jiangshan334d4162009-04-24 11:27:05 +0800309 if (event->type_len)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400310 return (void *)&event->array[0];
311 /* Otherwise length is in array[0] and array[1] has the data */
312 return (void *)&event->array[1];
313}
314
315/**
316 * ring_buffer_event_data - return the data of the event
317 * @event: the event to get the data from
318 */
319void *ring_buffer_event_data(struct ring_buffer_event *event)
320{
321 return rb_event_data(event);
322}
Robert Richterc4f50182008-12-11 16:49:22 +0100323EXPORT_SYMBOL_GPL(ring_buffer_event_data);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400324
325#define for_each_buffer_cpu(buffer, cpu) \
Rusty Russell9e01c1b2009-01-01 10:12:22 +1030326 for_each_cpu(cpu, buffer->cpumask)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400327
328#define TS_SHIFT 27
329#define TS_MASK ((1ULL << TS_SHIFT) - 1)
330#define TS_DELTA_TEST (~TS_MASK)
331
Steven Rostedt66a8cb92010-03-31 13:21:56 -0400332/* Flag when events were overwritten */
333#define RB_MISSED_EVENTS (1 << 31)
Steven Rostedtff0ff842010-03-31 22:11:42 -0400334/* Missed count stored at end */
335#define RB_MISSED_STORED (1 << 30)
Steven Rostedt66a8cb92010-03-31 13:21:56 -0400336
Steven Rostedtabc9b562008-12-02 15:34:06 -0500337struct buffer_data_page {
Steven Rostedte4c2ce82008-10-01 11:14:54 -0400338 u64 time_stamp; /* page time stamp */
Wenji Huangc3706f02009-02-10 01:03:18 -0500339 local_t commit; /* write committed index */
James Hogan649508f2012-05-30 12:11:19 +0100340 unsigned char data[] RB_ALIGN_DATA; /* data of buffer page */
Steven Rostedtabc9b562008-12-02 15:34:06 -0500341};
342
Steven Rostedt77ae3652009-03-27 11:00:29 -0400343/*
344 * Note, the buffer_page list must be first. The buffer pages
345 * are allocated in cache lines, which means that each buffer
346 * page will be at the beginning of a cache line, and thus
347 * the least significant bits will be zero. We use this to
348 * add flags in the list struct pointers, to make the ring buffer
349 * lockless.
350 */
Steven Rostedtabc9b562008-12-02 15:34:06 -0500351struct buffer_page {
Steven Rostedt778c55d2009-05-01 18:44:45 -0400352 struct list_head list; /* list of buffer pages */
Steven Rostedtabc9b562008-12-02 15:34:06 -0500353 local_t write; /* index for next write */
Steven Rostedt6f807ac2008-10-04 02:00:58 -0400354 unsigned read; /* index for next read */
Steven Rostedt778c55d2009-05-01 18:44:45 -0400355 local_t entries; /* entries on this page */
Steven Rostedtff0ff842010-03-31 22:11:42 -0400356 unsigned long real_end; /* real end of data */
Steven Rostedtabc9b562008-12-02 15:34:06 -0500357 struct buffer_data_page *page; /* Actual data page */
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400358};
359
Steven Rostedt77ae3652009-03-27 11:00:29 -0400360/*
361 * The buffer page counters, write and entries, must be reset
362 * atomically when crossing page boundaries. To synchronize this
363 * update, two counters are inserted into the number. One is
364 * the actual counter for the write position or count on the page.
365 *
366 * The other is a counter of updaters. Before an update happens
367 * the update partition of the counter is incremented. This will
368 * allow the updater to update the counter atomically.
369 *
370 * The counter is 20 bits, and the state data is 12.
371 */
372#define RB_WRITE_MASK 0xfffff
373#define RB_WRITE_INTCNT (1 << 20)
374
Steven Rostedt044fa782008-12-02 23:50:03 -0500375static void rb_init_page(struct buffer_data_page *bpage)
Steven Rostedtabc9b562008-12-02 15:34:06 -0500376{
Steven Rostedt044fa782008-12-02 23:50:03 -0500377 local_set(&bpage->commit, 0);
Steven Rostedtabc9b562008-12-02 15:34:06 -0500378}
379
Steven Rostedt474d32b2009-03-03 19:51:40 -0500380/**
381 * ring_buffer_page_len - the size of data on the page.
382 * @page: The page to read
383 *
384 * Returns the amount of data on the page, including buffer page header.
385 */
Steven Rostedtef7a4a12009-03-03 00:27:49 -0500386size_t ring_buffer_page_len(void *page)
387{
Steven Rostedt474d32b2009-03-03 19:51:40 -0500388 return local_read(&((struct buffer_data_page *)page)->commit)
389 + BUF_PAGE_HDR_SIZE;
Steven Rostedtef7a4a12009-03-03 00:27:49 -0500390}
391
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400392/*
Steven Rostedted568292008-09-29 23:02:40 -0400393 * Also stolen from mm/slob.c. Thanks to Mathieu Desnoyers for pointing
394 * this issue out.
395 */
Andrew Morton34a148b2009-01-09 12:27:09 -0800396static void free_buffer_page(struct buffer_page *bpage)
Steven Rostedted568292008-09-29 23:02:40 -0400397{
Andrew Morton34a148b2009-01-09 12:27:09 -0800398 free_page((unsigned long)bpage->page);
Steven Rostedte4c2ce82008-10-01 11:14:54 -0400399 kfree(bpage);
Steven Rostedted568292008-09-29 23:02:40 -0400400}
401
402/*
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400403 * We need to fit the time_stamp delta into 27 bits.
404 */
405static inline int test_time_stamp(u64 delta)
406{
407 if (delta & TS_DELTA_TEST)
408 return 1;
409 return 0;
410}
411
Steven Rostedt474d32b2009-03-03 19:51:40 -0500412#define BUF_PAGE_SIZE (PAGE_SIZE - BUF_PAGE_HDR_SIZE)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400413
Steven Rostedtbe957c42009-05-11 14:42:53 -0400414/* Max payload is BUF_PAGE_SIZE - header (8bytes) */
415#define BUF_MAX_DATA_SIZE (BUF_PAGE_SIZE - (sizeof(u32) * 2))
416
Steven Rostedtd1b182a2009-04-15 16:53:47 -0400417int ring_buffer_print_page_header(struct trace_seq *s)
418{
419 struct buffer_data_page field;
Steven Rostedtd1b182a2009-04-15 16:53:47 -0400420
Steven Rostedt (Red Hat)c0cd93a2014-11-12 11:49:00 -0500421 trace_seq_printf(s, "\tfield: u64 timestamp;\t"
422 "offset:0;\tsize:%u;\tsigned:%u;\n",
423 (unsigned int)sizeof(field.time_stamp),
424 (unsigned int)is_signed_type(u64));
Steven Rostedtd1b182a2009-04-15 16:53:47 -0400425
Steven Rostedt (Red Hat)c0cd93a2014-11-12 11:49:00 -0500426 trace_seq_printf(s, "\tfield: local_t commit;\t"
427 "offset:%u;\tsize:%u;\tsigned:%u;\n",
428 (unsigned int)offsetof(typeof(field), commit),
429 (unsigned int)sizeof(field.commit),
430 (unsigned int)is_signed_type(long));
Steven Rostedtd1b182a2009-04-15 16:53:47 -0400431
Steven Rostedt (Red Hat)c0cd93a2014-11-12 11:49:00 -0500432 trace_seq_printf(s, "\tfield: int overwrite;\t"
433 "offset:%u;\tsize:%u;\tsigned:%u;\n",
434 (unsigned int)offsetof(typeof(field), commit),
435 1,
436 (unsigned int)is_signed_type(long));
Steven Rostedt66a8cb92010-03-31 13:21:56 -0400437
Steven Rostedt (Red Hat)c0cd93a2014-11-12 11:49:00 -0500438 trace_seq_printf(s, "\tfield: char data;\t"
439 "offset:%u;\tsize:%u;\tsigned:%u;\n",
440 (unsigned int)offsetof(typeof(field), data),
441 (unsigned int)BUF_PAGE_SIZE,
442 (unsigned int)is_signed_type(char));
Steven Rostedtd1b182a2009-04-15 16:53:47 -0400443
Steven Rostedt (Red Hat)c0cd93a2014-11-12 11:49:00 -0500444 return !trace_seq_has_overflowed(s);
Steven Rostedtd1b182a2009-04-15 16:53:47 -0400445}
446
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -0500447struct rb_irq_work {
448 struct irq_work work;
449 wait_queue_head_t waiters;
450 bool waiters_pending;
451};
452
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400453/*
454 * head_page == tail_page && head == tail then buffer is empty.
455 */
456struct ring_buffer_per_cpu {
457 int cpu;
Richard Kennedy985023d2010-03-25 11:27:36 +0000458 atomic_t record_disabled;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400459 struct ring_buffer *buffer;
Thomas Gleixner5389f6f2009-07-25 17:13:33 +0200460 raw_spinlock_t reader_lock; /* serialize readers */
Thomas Gleixner445c8952009-12-02 19:49:50 +0100461 arch_spinlock_t lock;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400462 struct lock_class_key lock_key;
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -0800463 unsigned int nr_pages;
Steven Rostedt3adc54f2009-03-30 15:32:01 -0400464 struct list_head *pages;
Steven Rostedt6f807ac2008-10-04 02:00:58 -0400465 struct buffer_page *head_page; /* read from head */
466 struct buffer_page *tail_page; /* write to tail */
Wenji Huangc3706f02009-02-10 01:03:18 -0500467 struct buffer_page *commit_page; /* committed pages */
Steven Rostedtd7690412008-10-01 00:29:53 -0400468 struct buffer_page *reader_page;
Steven Rostedt66a8cb92010-03-31 13:21:56 -0400469 unsigned long lost_events;
470 unsigned long last_overrun;
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -0700471 local_t entries_bytes;
Steven Rostedte4906ef2009-04-30 20:49:44 -0400472 local_t entries;
Slava Pestov884bfe82011-07-15 14:23:58 -0700473 local_t overrun;
474 local_t commit_overrun;
475 local_t dropped_events;
Steven Rostedtfa743952009-06-16 12:37:57 -0400476 local_t committing;
477 local_t commits;
Steven Rostedt77ae3652009-03-27 11:00:29 -0400478 unsigned long read;
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -0700479 unsigned long read_bytes;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400480 u64 write_stamp;
481 u64 read_stamp;
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -0800482 /* ring buffer pages to update, > 0 to add, < 0 to remove */
483 int nr_pages_to_update;
484 struct list_head new_pages; /* new pages to add */
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -0700485 struct work_struct update_pages_work;
Vaibhav Nagarnaik05fdd702012-05-18 13:29:51 -0700486 struct completion update_done;
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -0500487
488 struct rb_irq_work irq_work;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400489};
490
491struct ring_buffer {
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400492 unsigned flags;
493 int cpus;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400494 atomic_t record_disabled;
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -0700495 atomic_t resize_disabled;
Arnaldo Carvalho de Melo00f62f62009-02-09 17:04:06 -0200496 cpumask_var_t cpumask;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400497
Peter Zijlstra1f8a6a12009-06-08 18:18:39 +0200498 struct lock_class_key *reader_lock_key;
499
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400500 struct mutex mutex;
501
502 struct ring_buffer_per_cpu **buffers;
Steven Rostedt554f7862009-03-11 22:00:13 -0400503
Steven Rostedt59222ef2009-03-12 11:46:03 -0400504#ifdef CONFIG_HOTPLUG_CPU
Steven Rostedt554f7862009-03-11 22:00:13 -0400505 struct notifier_block cpu_notify;
506#endif
Steven Rostedt37886f62009-03-17 17:22:06 -0400507 u64 (*clock)(void);
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -0500508
509 struct rb_irq_work irq_work;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400510};
511
512struct ring_buffer_iter {
513 struct ring_buffer_per_cpu *cpu_buffer;
514 unsigned long head;
515 struct buffer_page *head_page;
Steven Rostedt492a74f2010-01-25 15:17:47 -0500516 struct buffer_page *cache_reader_page;
517 unsigned long cache_read;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400518 u64 read_stamp;
519};
520
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -0500521/*
522 * rb_wake_up_waiters - wake up tasks waiting for ring buffer input
523 *
524 * Schedules a delayed work to wake up any task that is blocked on the
525 * ring buffer waiters queue.
526 */
527static void rb_wake_up_waiters(struct irq_work *work)
528{
529 struct rb_irq_work *rbwork = container_of(work, struct rb_irq_work, work);
530
531 wake_up_all(&rbwork->waiters);
532}
533
534/**
535 * ring_buffer_wait - wait for input to the ring buffer
536 * @buffer: buffer to wait on
537 * @cpu: the cpu buffer to wait on
538 *
539 * If @cpu == RING_BUFFER_ALL_CPUS then the task will wake up as soon
540 * as data is added to any of the @buffer's cpu buffers. Otherwise
541 * it will wait for data to be added to a specific cpu buffer.
542 */
Steven Rostedt (Red Hat)8b8b3682014-06-10 09:46:00 -0400543int ring_buffer_wait(struct ring_buffer *buffer, int cpu)
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -0500544{
545 struct ring_buffer_per_cpu *cpu_buffer;
546 DEFINE_WAIT(wait);
547 struct rb_irq_work *work;
548
549 /*
550 * Depending on what the caller is waiting for, either any
551 * data in any cpu buffer, or a specific buffer, put the
552 * caller on the appropriate wait queue.
553 */
554 if (cpu == RING_BUFFER_ALL_CPUS)
555 work = &buffer->irq_work;
556 else {
Steven Rostedt (Red Hat)8b8b3682014-06-10 09:46:00 -0400557 if (!cpumask_test_cpu(cpu, buffer->cpumask))
558 return -ENODEV;
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -0500559 cpu_buffer = buffer->buffers[cpu];
560 work = &cpu_buffer->irq_work;
561 }
562
563
564 prepare_to_wait(&work->waiters, &wait, TASK_INTERRUPTIBLE);
565
566 /*
567 * The events can happen in critical sections where
568 * checking a work queue can cause deadlocks.
569 * After adding a task to the queue, this flag is set
570 * only to notify events to try to wake up the queue
571 * using irq_work.
572 *
573 * We don't clear it even if the buffer is no longer
574 * empty. The flag only causes the next event to run
575 * irq_work to do the work queue wake up. The worse
576 * that can happen if we race with !trace_empty() is that
577 * an event will cause an irq_work to try to wake up
578 * an empty queue.
579 *
580 * There's no reason to protect this flag either, as
581 * the work queue and irq_work logic will do the necessary
582 * synchronization for the wake ups. The only thing
583 * that is necessary is that the wake up happens after
584 * a task has been queued. It's OK for spurious wake ups.
585 */
586 work->waiters_pending = true;
587
588 if ((cpu == RING_BUFFER_ALL_CPUS && ring_buffer_empty(buffer)) ||
589 (cpu != RING_BUFFER_ALL_CPUS && ring_buffer_empty_cpu(buffer, cpu)))
590 schedule();
591
592 finish_wait(&work->waiters, &wait);
Steven Rostedt (Red Hat)8b8b3682014-06-10 09:46:00 -0400593 return 0;
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -0500594}
595
596/**
597 * ring_buffer_poll_wait - poll on buffer input
598 * @buffer: buffer to wait on
599 * @cpu: the cpu buffer to wait on
600 * @filp: the file descriptor
601 * @poll_table: The poll descriptor
602 *
603 * If @cpu == RING_BUFFER_ALL_CPUS then the task will wake up as soon
604 * as data is added to any of the @buffer's cpu buffers. Otherwise
605 * it will wait for data to be added to a specific cpu buffer.
606 *
607 * Returns POLLIN | POLLRDNORM if data exists in the buffers,
608 * zero otherwise.
609 */
610int ring_buffer_poll_wait(struct ring_buffer *buffer, int cpu,
611 struct file *filp, poll_table *poll_table)
612{
613 struct ring_buffer_per_cpu *cpu_buffer;
614 struct rb_irq_work *work;
615
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -0500616 if (cpu == RING_BUFFER_ALL_CPUS)
617 work = &buffer->irq_work;
618 else {
Steven Rostedt (Red Hat)6721cb62013-05-23 14:21:36 -0400619 if (!cpumask_test_cpu(cpu, buffer->cpumask))
620 return -EINVAL;
621
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -0500622 cpu_buffer = buffer->buffers[cpu];
623 work = &cpu_buffer->irq_work;
624 }
625
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -0500626 poll_wait(filp, &work->waiters, poll_table);
Josef Bacik4ce97db2014-08-25 13:59:41 -0400627 work->waiters_pending = true;
628 /*
629 * There's a tight race between setting the waiters_pending and
630 * checking if the ring buffer is empty. Once the waiters_pending bit
631 * is set, the next event will wake the task up, but we can get stuck
632 * if there's only a single event in.
633 *
634 * FIXME: Ideally, we need a memory barrier on the writer side as well,
635 * but adding a memory barrier to all events will cause too much of a
636 * performance hit in the fast path. We only need a memory barrier when
637 * the buffer goes from empty to having content. But as this race is
638 * extremely small, and it's not a problem if another event comes in, we
639 * will fix it later.
640 */
641 smp_mb();
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -0500642
643 if ((cpu == RING_BUFFER_ALL_CPUS && !ring_buffer_empty(buffer)) ||
644 (cpu != RING_BUFFER_ALL_CPUS && !ring_buffer_empty_cpu(buffer, cpu)))
645 return POLLIN | POLLRDNORM;
646 return 0;
647}
648
Steven Rostedtf536aaf2008-11-10 23:07:30 -0500649/* buffer may be either ring_buffer or ring_buffer_per_cpu */
Steven Rostedt077c5402009-09-03 19:53:46 -0400650#define RB_WARN_ON(b, cond) \
651 ({ \
652 int _____ret = unlikely(cond); \
653 if (_____ret) { \
654 if (__same_type(*(b), struct ring_buffer_per_cpu)) { \
655 struct ring_buffer_per_cpu *__b = \
656 (void *)b; \
657 atomic_inc(&__b->buffer->record_disabled); \
658 } else \
659 atomic_inc(&b->record_disabled); \
660 WARN_ON(1); \
661 } \
662 _____ret; \
Steven Rostedt3e89c7bb2008-11-11 15:28:41 -0500663 })
Steven Rostedtf536aaf2008-11-10 23:07:30 -0500664
Steven Rostedt37886f62009-03-17 17:22:06 -0400665/* Up this if you want to test the TIME_EXTENTS and normalization */
666#define DEBUG_SHIFT 0
667
Jiri Olsa6d3f1e12009-10-23 19:36:19 -0400668static inline u64 rb_time_stamp(struct ring_buffer *buffer)
Steven Rostedt88eb0122009-05-11 16:28:23 -0400669{
670 /* shift to debug/test normalization and TIME_EXTENTS */
671 return buffer->clock() << DEBUG_SHIFT;
672}
673
Steven Rostedt37886f62009-03-17 17:22:06 -0400674u64 ring_buffer_time_stamp(struct ring_buffer *buffer, int cpu)
675{
676 u64 time;
677
678 preempt_disable_notrace();
Jiri Olsa6d3f1e12009-10-23 19:36:19 -0400679 time = rb_time_stamp(buffer);
Steven Rostedt37886f62009-03-17 17:22:06 -0400680 preempt_enable_no_resched_notrace();
681
682 return time;
683}
684EXPORT_SYMBOL_GPL(ring_buffer_time_stamp);
685
686void ring_buffer_normalize_time_stamp(struct ring_buffer *buffer,
687 int cpu, u64 *ts)
688{
689 /* Just stupid testing the normalize function and deltas */
690 *ts >>= DEBUG_SHIFT;
691}
692EXPORT_SYMBOL_GPL(ring_buffer_normalize_time_stamp);
693
Steven Rostedt77ae3652009-03-27 11:00:29 -0400694/*
695 * Making the ring buffer lockless makes things tricky.
696 * Although writes only happen on the CPU that they are on,
697 * and they only need to worry about interrupts. Reads can
698 * happen on any CPU.
699 *
700 * The reader page is always off the ring buffer, but when the
701 * reader finishes with a page, it needs to swap its page with
702 * a new one from the buffer. The reader needs to take from
703 * the head (writes go to the tail). But if a writer is in overwrite
704 * mode and wraps, it must push the head page forward.
705 *
706 * Here lies the problem.
707 *
708 * The reader must be careful to replace only the head page, and
709 * not another one. As described at the top of the file in the
710 * ASCII art, the reader sets its old page to point to the next
711 * page after head. It then sets the page after head to point to
712 * the old reader page. But if the writer moves the head page
713 * during this operation, the reader could end up with the tail.
714 *
715 * We use cmpxchg to help prevent this race. We also do something
716 * special with the page before head. We set the LSB to 1.
717 *
718 * When the writer must push the page forward, it will clear the
719 * bit that points to the head page, move the head, and then set
720 * the bit that points to the new head page.
721 *
722 * We also don't want an interrupt coming in and moving the head
723 * page on another writer. Thus we use the second LSB to catch
724 * that too. Thus:
725 *
726 * head->list->prev->next bit 1 bit 0
727 * ------- -------
728 * Normal page 0 0
729 * Points to head page 0 1
730 * New head page 1 0
731 *
732 * Note we can not trust the prev pointer of the head page, because:
733 *
734 * +----+ +-----+ +-----+
735 * | |------>| T |---X--->| N |
736 * | |<------| | | |
737 * +----+ +-----+ +-----+
738 * ^ ^ |
739 * | +-----+ | |
740 * +----------| R |----------+ |
741 * | |<-----------+
742 * +-----+
743 *
744 * Key: ---X--> HEAD flag set in pointer
745 * T Tail page
746 * R Reader page
747 * N Next page
748 *
749 * (see __rb_reserve_next() to see where this happens)
750 *
751 * What the above shows is that the reader just swapped out
752 * the reader page with a page in the buffer, but before it
753 * could make the new header point back to the new page added
754 * it was preempted by a writer. The writer moved forward onto
755 * the new page added by the reader and is about to move forward
756 * again.
757 *
758 * You can see, it is legitimate for the previous pointer of
759 * the head (or any page) not to point back to itself. But only
760 * temporarially.
761 */
762
763#define RB_PAGE_NORMAL 0UL
764#define RB_PAGE_HEAD 1UL
765#define RB_PAGE_UPDATE 2UL
766
767
768#define RB_FLAG_MASK 3UL
769
770/* PAGE_MOVED is not part of the mask */
771#define RB_PAGE_MOVED 4UL
772
773/*
774 * rb_list_head - remove any bit
775 */
776static struct list_head *rb_list_head(struct list_head *list)
777{
778 unsigned long val = (unsigned long)list;
779
780 return (struct list_head *)(val & ~RB_FLAG_MASK);
781}
782
783/*
Jiri Olsa6d3f1e12009-10-23 19:36:19 -0400784 * rb_is_head_page - test if the given page is the head page
Steven Rostedt77ae3652009-03-27 11:00:29 -0400785 *
786 * Because the reader may move the head_page pointer, we can
787 * not trust what the head page is (it may be pointing to
788 * the reader page). But if the next page is a header page,
789 * its flags will be non zero.
790 */
Jesper Juhl42b16b32011-01-17 00:09:38 +0100791static inline int
Steven Rostedt77ae3652009-03-27 11:00:29 -0400792rb_is_head_page(struct ring_buffer_per_cpu *cpu_buffer,
793 struct buffer_page *page, struct list_head *list)
794{
795 unsigned long val;
796
797 val = (unsigned long)list->next;
798
799 if ((val & ~RB_FLAG_MASK) != (unsigned long)&page->list)
800 return RB_PAGE_MOVED;
801
802 return val & RB_FLAG_MASK;
803}
804
805/*
806 * rb_is_reader_page
807 *
808 * The unique thing about the reader page, is that, if the
809 * writer is ever on it, the previous pointer never points
810 * back to the reader page.
811 */
812static int rb_is_reader_page(struct buffer_page *page)
813{
814 struct list_head *list = page->list.prev;
815
816 return rb_list_head(list->next) != &page->list;
817}
818
819/*
820 * rb_set_list_to_head - set a list_head to be pointing to head.
821 */
822static void rb_set_list_to_head(struct ring_buffer_per_cpu *cpu_buffer,
823 struct list_head *list)
824{
825 unsigned long *ptr;
826
827 ptr = (unsigned long *)&list->next;
828 *ptr |= RB_PAGE_HEAD;
829 *ptr &= ~RB_PAGE_UPDATE;
830}
831
832/*
833 * rb_head_page_activate - sets up head page
834 */
835static void rb_head_page_activate(struct ring_buffer_per_cpu *cpu_buffer)
836{
837 struct buffer_page *head;
838
839 head = cpu_buffer->head_page;
840 if (!head)
841 return;
842
843 /*
844 * Set the previous list pointer to have the HEAD flag.
845 */
846 rb_set_list_to_head(cpu_buffer, head->list.prev);
847}
848
849static void rb_list_head_clear(struct list_head *list)
850{
851 unsigned long *ptr = (unsigned long *)&list->next;
852
853 *ptr &= ~RB_FLAG_MASK;
854}
855
856/*
857 * rb_head_page_dactivate - clears head page ptr (for free list)
858 */
859static void
860rb_head_page_deactivate(struct ring_buffer_per_cpu *cpu_buffer)
861{
862 struct list_head *hd;
863
864 /* Go through the whole list and clear any pointers found. */
865 rb_list_head_clear(cpu_buffer->pages);
866
867 list_for_each(hd, cpu_buffer->pages)
868 rb_list_head_clear(hd);
869}
870
871static int rb_head_page_set(struct ring_buffer_per_cpu *cpu_buffer,
872 struct buffer_page *head,
873 struct buffer_page *prev,
874 int old_flag, int new_flag)
875{
876 struct list_head *list;
877 unsigned long val = (unsigned long)&head->list;
878 unsigned long ret;
879
880 list = &prev->list;
881
882 val &= ~RB_FLAG_MASK;
883
Steven Rostedt08a40812009-09-14 09:31:35 -0400884 ret = cmpxchg((unsigned long *)&list->next,
885 val | old_flag, val | new_flag);
Steven Rostedt77ae3652009-03-27 11:00:29 -0400886
887 /* check if the reader took the page */
888 if ((ret & ~RB_FLAG_MASK) != val)
889 return RB_PAGE_MOVED;
890
891 return ret & RB_FLAG_MASK;
892}
893
894static int rb_head_page_set_update(struct ring_buffer_per_cpu *cpu_buffer,
895 struct buffer_page *head,
896 struct buffer_page *prev,
897 int old_flag)
898{
899 return rb_head_page_set(cpu_buffer, head, prev,
900 old_flag, RB_PAGE_UPDATE);
901}
902
903static int rb_head_page_set_head(struct ring_buffer_per_cpu *cpu_buffer,
904 struct buffer_page *head,
905 struct buffer_page *prev,
906 int old_flag)
907{
908 return rb_head_page_set(cpu_buffer, head, prev,
909 old_flag, RB_PAGE_HEAD);
910}
911
912static int rb_head_page_set_normal(struct ring_buffer_per_cpu *cpu_buffer,
913 struct buffer_page *head,
914 struct buffer_page *prev,
915 int old_flag)
916{
917 return rb_head_page_set(cpu_buffer, head, prev,
918 old_flag, RB_PAGE_NORMAL);
919}
920
921static inline void rb_inc_page(struct ring_buffer_per_cpu *cpu_buffer,
922 struct buffer_page **bpage)
923{
924 struct list_head *p = rb_list_head((*bpage)->list.next);
925
926 *bpage = list_entry(p, struct buffer_page, list);
927}
928
929static struct buffer_page *
930rb_set_head_page(struct ring_buffer_per_cpu *cpu_buffer)
931{
932 struct buffer_page *head;
933 struct buffer_page *page;
934 struct list_head *list;
935 int i;
936
937 if (RB_WARN_ON(cpu_buffer, !cpu_buffer->head_page))
938 return NULL;
939
940 /* sanity check */
941 list = cpu_buffer->pages;
942 if (RB_WARN_ON(cpu_buffer, rb_list_head(list->prev->next) != list))
943 return NULL;
944
945 page = head = cpu_buffer->head_page;
946 /*
947 * It is possible that the writer moves the header behind
948 * where we started, and we miss in one loop.
949 * A second loop should grab the header, but we'll do
950 * three loops just because I'm paranoid.
951 */
952 for (i = 0; i < 3; i++) {
953 do {
954 if (rb_is_head_page(cpu_buffer, page, page->list.prev)) {
955 cpu_buffer->head_page = page;
956 return page;
957 }
958 rb_inc_page(cpu_buffer, &page);
959 } while (page != head);
960 }
961
962 RB_WARN_ON(cpu_buffer, 1);
963
964 return NULL;
965}
966
967static int rb_head_page_replace(struct buffer_page *old,
968 struct buffer_page *new)
969{
970 unsigned long *ptr = (unsigned long *)&old->list.prev->next;
971 unsigned long val;
972 unsigned long ret;
973
974 val = *ptr & ~RB_FLAG_MASK;
975 val |= RB_PAGE_HEAD;
976
Steven Rostedt08a40812009-09-14 09:31:35 -0400977 ret = cmpxchg(ptr, val, (unsigned long)&new->list);
Steven Rostedt77ae3652009-03-27 11:00:29 -0400978
979 return ret == val;
980}
981
982/*
983 * rb_tail_page_update - move the tail page forward
984 *
985 * Returns 1 if moved tail page, 0 if someone else did.
986 */
987static int rb_tail_page_update(struct ring_buffer_per_cpu *cpu_buffer,
988 struct buffer_page *tail_page,
989 struct buffer_page *next_page)
990{
991 struct buffer_page *old_tail;
992 unsigned long old_entries;
993 unsigned long old_write;
994 int ret = 0;
995
996 /*
997 * The tail page now needs to be moved forward.
998 *
999 * We need to reset the tail page, but without messing
1000 * with possible erasing of data brought in by interrupts
1001 * that have moved the tail page and are currently on it.
1002 *
1003 * We add a counter to the write field to denote this.
1004 */
1005 old_write = local_add_return(RB_WRITE_INTCNT, &next_page->write);
1006 old_entries = local_add_return(RB_WRITE_INTCNT, &next_page->entries);
1007
1008 /*
1009 * Just make sure we have seen our old_write and synchronize
1010 * with any interrupts that come in.
1011 */
1012 barrier();
1013
1014 /*
1015 * If the tail page is still the same as what we think
1016 * it is, then it is up to us to update the tail
1017 * pointer.
1018 */
1019 if (tail_page == cpu_buffer->tail_page) {
1020 /* Zero the write counter */
1021 unsigned long val = old_write & ~RB_WRITE_MASK;
1022 unsigned long eval = old_entries & ~RB_WRITE_MASK;
1023
1024 /*
1025 * This will only succeed if an interrupt did
1026 * not come in and change it. In which case, we
1027 * do not want to modify it.
Lai Jiangshanda706d82009-07-15 16:27:30 +08001028 *
1029 * We add (void) to let the compiler know that we do not care
1030 * about the return value of these functions. We use the
1031 * cmpxchg to only update if an interrupt did not already
1032 * do it for us. If the cmpxchg fails, we don't care.
Steven Rostedt77ae3652009-03-27 11:00:29 -04001033 */
Lai Jiangshanda706d82009-07-15 16:27:30 +08001034 (void)local_cmpxchg(&next_page->write, old_write, val);
1035 (void)local_cmpxchg(&next_page->entries, old_entries, eval);
Steven Rostedt77ae3652009-03-27 11:00:29 -04001036
1037 /*
1038 * No need to worry about races with clearing out the commit.
1039 * it only can increment when a commit takes place. But that
1040 * only happens in the outer most nested commit.
1041 */
1042 local_set(&next_page->page->commit, 0);
1043
1044 old_tail = cmpxchg(&cpu_buffer->tail_page,
1045 tail_page, next_page);
1046
1047 if (old_tail == tail_page)
1048 ret = 1;
1049 }
1050
1051 return ret;
1052}
1053
1054static int rb_check_bpage(struct ring_buffer_per_cpu *cpu_buffer,
1055 struct buffer_page *bpage)
1056{
1057 unsigned long val = (unsigned long)bpage;
1058
1059 if (RB_WARN_ON(cpu_buffer, val & RB_FLAG_MASK))
1060 return 1;
1061
1062 return 0;
1063}
1064
1065/**
1066 * rb_check_list - make sure a pointer to a list has the last bits zero
1067 */
1068static int rb_check_list(struct ring_buffer_per_cpu *cpu_buffer,
1069 struct list_head *list)
1070{
1071 if (RB_WARN_ON(cpu_buffer, rb_list_head(list->prev) != list->prev))
1072 return 1;
1073 if (RB_WARN_ON(cpu_buffer, rb_list_head(list->next) != list->next))
1074 return 1;
1075 return 0;
1076}
1077
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001078/**
zhangwei(Jovi)d6118512013-07-15 16:32:50 +08001079 * rb_check_pages - integrity check of buffer pages
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001080 * @cpu_buffer: CPU buffer with pages to test
1081 *
Wenji Huangc3706f02009-02-10 01:03:18 -05001082 * As a safety measure we check to make sure the data pages have not
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001083 * been corrupted.
1084 */
1085static int rb_check_pages(struct ring_buffer_per_cpu *cpu_buffer)
1086{
Steven Rostedt3adc54f2009-03-30 15:32:01 -04001087 struct list_head *head = cpu_buffer->pages;
Steven Rostedt044fa782008-12-02 23:50:03 -05001088 struct buffer_page *bpage, *tmp;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001089
Steven Rostedt308f7ee2012-05-16 19:46:32 -04001090 /* Reset the head page if it exists */
1091 if (cpu_buffer->head_page)
1092 rb_set_head_page(cpu_buffer);
1093
Steven Rostedt77ae3652009-03-27 11:00:29 -04001094 rb_head_page_deactivate(cpu_buffer);
1095
Steven Rostedt3e89c7bb2008-11-11 15:28:41 -05001096 if (RB_WARN_ON(cpu_buffer, head->next->prev != head))
1097 return -1;
1098 if (RB_WARN_ON(cpu_buffer, head->prev->next != head))
1099 return -1;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001100
Steven Rostedt77ae3652009-03-27 11:00:29 -04001101 if (rb_check_list(cpu_buffer, head))
1102 return -1;
1103
Steven Rostedt044fa782008-12-02 23:50:03 -05001104 list_for_each_entry_safe(bpage, tmp, head, list) {
Steven Rostedt3e89c7bb2008-11-11 15:28:41 -05001105 if (RB_WARN_ON(cpu_buffer,
Steven Rostedt044fa782008-12-02 23:50:03 -05001106 bpage->list.next->prev != &bpage->list))
Steven Rostedt3e89c7bb2008-11-11 15:28:41 -05001107 return -1;
1108 if (RB_WARN_ON(cpu_buffer,
Steven Rostedt044fa782008-12-02 23:50:03 -05001109 bpage->list.prev->next != &bpage->list))
Steven Rostedt3e89c7bb2008-11-11 15:28:41 -05001110 return -1;
Steven Rostedt77ae3652009-03-27 11:00:29 -04001111 if (rb_check_list(cpu_buffer, &bpage->list))
1112 return -1;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001113 }
1114
Steven Rostedt77ae3652009-03-27 11:00:29 -04001115 rb_head_page_activate(cpu_buffer);
1116
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001117 return 0;
1118}
1119
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001120static int __rb_allocate_pages(int nr_pages, struct list_head *pages, int cpu)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001121{
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001122 int i;
Steven Rostedt044fa782008-12-02 23:50:03 -05001123 struct buffer_page *bpage, *tmp;
Steven Rostedt3adc54f2009-03-30 15:32:01 -04001124
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001125 for (i = 0; i < nr_pages; i++) {
Vaibhav Nagarnaik7ea59062011-05-03 17:56:42 -07001126 struct page *page;
Vaibhav Nagarnaikd7ec4bf2011-06-07 17:01:42 -07001127 /*
1128 * __GFP_NORETRY flag makes sure that the allocation fails
1129 * gracefully without invoking oom-killer and the system is
1130 * not destabilized.
1131 */
Steven Rostedt044fa782008-12-02 23:50:03 -05001132 bpage = kzalloc_node(ALIGN(sizeof(*bpage), cache_line_size()),
Vaibhav Nagarnaikd7ec4bf2011-06-07 17:01:42 -07001133 GFP_KERNEL | __GFP_NORETRY,
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001134 cpu_to_node(cpu));
Steven Rostedt044fa782008-12-02 23:50:03 -05001135 if (!bpage)
Steven Rostedte4c2ce82008-10-01 11:14:54 -04001136 goto free_pages;
Steven Rostedt77ae3652009-03-27 11:00:29 -04001137
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001138 list_add(&bpage->list, pages);
Steven Rostedt77ae3652009-03-27 11:00:29 -04001139
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001140 page = alloc_pages_node(cpu_to_node(cpu),
Vaibhav Nagarnaikd7ec4bf2011-06-07 17:01:42 -07001141 GFP_KERNEL | __GFP_NORETRY, 0);
Vaibhav Nagarnaik7ea59062011-05-03 17:56:42 -07001142 if (!page)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001143 goto free_pages;
Vaibhav Nagarnaik7ea59062011-05-03 17:56:42 -07001144 bpage->page = page_address(page);
Steven Rostedt044fa782008-12-02 23:50:03 -05001145 rb_init_page(bpage->page);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001146 }
1147
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001148 return 0;
1149
1150free_pages:
1151 list_for_each_entry_safe(bpage, tmp, pages, list) {
1152 list_del_init(&bpage->list);
1153 free_buffer_page(bpage);
1154 }
1155
1156 return -ENOMEM;
1157}
1158
1159static int rb_allocate_pages(struct ring_buffer_per_cpu *cpu_buffer,
1160 unsigned nr_pages)
1161{
1162 LIST_HEAD(pages);
1163
1164 WARN_ON(!nr_pages);
1165
1166 if (__rb_allocate_pages(nr_pages, &pages, cpu_buffer->cpu))
1167 return -ENOMEM;
1168
Steven Rostedt3adc54f2009-03-30 15:32:01 -04001169 /*
1170 * The ring buffer page list is a circular list that does not
1171 * start and end with a list head. All page list items point to
1172 * other pages.
1173 */
1174 cpu_buffer->pages = pages.next;
1175 list_del(&pages);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001176
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001177 cpu_buffer->nr_pages = nr_pages;
1178
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001179 rb_check_pages(cpu_buffer);
1180
1181 return 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001182}
1183
1184static struct ring_buffer_per_cpu *
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001185rb_allocate_cpu_buffer(struct ring_buffer *buffer, int nr_pages, int cpu)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001186{
1187 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedt044fa782008-12-02 23:50:03 -05001188 struct buffer_page *bpage;
Vaibhav Nagarnaik7ea59062011-05-03 17:56:42 -07001189 struct page *page;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001190 int ret;
1191
1192 cpu_buffer = kzalloc_node(ALIGN(sizeof(*cpu_buffer), cache_line_size()),
1193 GFP_KERNEL, cpu_to_node(cpu));
1194 if (!cpu_buffer)
1195 return NULL;
1196
1197 cpu_buffer->cpu = cpu;
1198 cpu_buffer->buffer = buffer;
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02001199 raw_spin_lock_init(&cpu_buffer->reader_lock);
Peter Zijlstra1f8a6a12009-06-08 18:18:39 +02001200 lockdep_set_class(&cpu_buffer->reader_lock, buffer->reader_lock_key);
Thomas Gleixneredc35bd2009-12-03 12:38:57 +01001201 cpu_buffer->lock = (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001202 INIT_WORK(&cpu_buffer->update_pages_work, update_pages_handler);
Vaibhav Nagarnaik05fdd702012-05-18 13:29:51 -07001203 init_completion(&cpu_buffer->update_done);
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -05001204 init_irq_work(&cpu_buffer->irq_work.work, rb_wake_up_waiters);
Steven Rostedt (Red Hat)f1dc6722013-03-04 17:33:05 -05001205 init_waitqueue_head(&cpu_buffer->irq_work.waiters);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001206
Steven Rostedt044fa782008-12-02 23:50:03 -05001207 bpage = kzalloc_node(ALIGN(sizeof(*bpage), cache_line_size()),
Steven Rostedte4c2ce82008-10-01 11:14:54 -04001208 GFP_KERNEL, cpu_to_node(cpu));
Steven Rostedt044fa782008-12-02 23:50:03 -05001209 if (!bpage)
Steven Rostedte4c2ce82008-10-01 11:14:54 -04001210 goto fail_free_buffer;
1211
Steven Rostedt77ae3652009-03-27 11:00:29 -04001212 rb_check_bpage(cpu_buffer, bpage);
1213
Steven Rostedt044fa782008-12-02 23:50:03 -05001214 cpu_buffer->reader_page = bpage;
Vaibhav Nagarnaik7ea59062011-05-03 17:56:42 -07001215 page = alloc_pages_node(cpu_to_node(cpu), GFP_KERNEL, 0);
1216 if (!page)
Steven Rostedte4c2ce82008-10-01 11:14:54 -04001217 goto fail_free_reader;
Vaibhav Nagarnaik7ea59062011-05-03 17:56:42 -07001218 bpage->page = page_address(page);
Steven Rostedt044fa782008-12-02 23:50:03 -05001219 rb_init_page(bpage->page);
Steven Rostedte4c2ce82008-10-01 11:14:54 -04001220
Steven Rostedtd7690412008-10-01 00:29:53 -04001221 INIT_LIST_HEAD(&cpu_buffer->reader_page->list);
Vaibhav Nagarnaik44b99462012-06-22 11:50:05 -07001222 INIT_LIST_HEAD(&cpu_buffer->new_pages);
Steven Rostedtd7690412008-10-01 00:29:53 -04001223
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001224 ret = rb_allocate_pages(cpu_buffer, nr_pages);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001225 if (ret < 0)
Steven Rostedtd7690412008-10-01 00:29:53 -04001226 goto fail_free_reader;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001227
1228 cpu_buffer->head_page
Steven Rostedt3adc54f2009-03-30 15:32:01 -04001229 = list_entry(cpu_buffer->pages, struct buffer_page, list);
Steven Rostedtbf41a152008-10-04 02:00:59 -04001230 cpu_buffer->tail_page = cpu_buffer->commit_page = cpu_buffer->head_page;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001231
Steven Rostedt77ae3652009-03-27 11:00:29 -04001232 rb_head_page_activate(cpu_buffer);
1233
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001234 return cpu_buffer;
1235
Steven Rostedtd7690412008-10-01 00:29:53 -04001236 fail_free_reader:
1237 free_buffer_page(cpu_buffer->reader_page);
1238
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001239 fail_free_buffer:
1240 kfree(cpu_buffer);
1241 return NULL;
1242}
1243
1244static void rb_free_cpu_buffer(struct ring_buffer_per_cpu *cpu_buffer)
1245{
Steven Rostedt3adc54f2009-03-30 15:32:01 -04001246 struct list_head *head = cpu_buffer->pages;
Steven Rostedt044fa782008-12-02 23:50:03 -05001247 struct buffer_page *bpage, *tmp;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001248
Steven Rostedtd7690412008-10-01 00:29:53 -04001249 free_buffer_page(cpu_buffer->reader_page);
1250
Steven Rostedt77ae3652009-03-27 11:00:29 -04001251 rb_head_page_deactivate(cpu_buffer);
1252
Steven Rostedt3adc54f2009-03-30 15:32:01 -04001253 if (head) {
1254 list_for_each_entry_safe(bpage, tmp, head, list) {
1255 list_del_init(&bpage->list);
1256 free_buffer_page(bpage);
1257 }
1258 bpage = list_entry(head, struct buffer_page, list);
Steven Rostedt044fa782008-12-02 23:50:03 -05001259 free_buffer_page(bpage);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001260 }
Steven Rostedt3adc54f2009-03-30 15:32:01 -04001261
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001262 kfree(cpu_buffer);
1263}
1264
Steven Rostedt59222ef2009-03-12 11:46:03 -04001265#ifdef CONFIG_HOTPLUG_CPU
Frederic Weisbecker09c9e842009-03-21 04:33:36 +01001266static int rb_cpu_notify(struct notifier_block *self,
1267 unsigned long action, void *hcpu);
Steven Rostedt554f7862009-03-11 22:00:13 -04001268#endif
1269
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001270/**
zhangwei(Jovi)d6118512013-07-15 16:32:50 +08001271 * __ring_buffer_alloc - allocate a new ring_buffer
Robert Richter68814b52008-11-24 12:24:12 +01001272 * @size: the size in bytes per cpu that is needed.
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001273 * @flags: attributes to set for the ring buffer.
1274 *
1275 * Currently the only flag that is available is the RB_FL_OVERWRITE
1276 * flag. This flag means that the buffer will overwrite old data
1277 * when the buffer wraps. If this flag is not set, the buffer will
1278 * drop data when the tail hits the head.
1279 */
Peter Zijlstra1f8a6a12009-06-08 18:18:39 +02001280struct ring_buffer *__ring_buffer_alloc(unsigned long size, unsigned flags,
1281 struct lock_class_key *key)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001282{
1283 struct ring_buffer *buffer;
1284 int bsize;
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001285 int cpu, nr_pages;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001286
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001287 /* keep it in its own cache line */
1288 buffer = kzalloc(ALIGN(sizeof(*buffer), cache_line_size()),
1289 GFP_KERNEL);
1290 if (!buffer)
1291 return NULL;
1292
Rusty Russell9e01c1b2009-01-01 10:12:22 +10301293 if (!alloc_cpumask_var(&buffer->cpumask, GFP_KERNEL))
1294 goto fail_free_buffer;
1295
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001296 nr_pages = DIV_ROUND_UP(size, BUF_PAGE_SIZE);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001297 buffer->flags = flags;
Steven Rostedt37886f62009-03-17 17:22:06 -04001298 buffer->clock = trace_clock_local;
Peter Zijlstra1f8a6a12009-06-08 18:18:39 +02001299 buffer->reader_lock_key = key;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001300
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -05001301 init_irq_work(&buffer->irq_work.work, rb_wake_up_waiters);
Steven Rostedt (Red Hat)f1dc6722013-03-04 17:33:05 -05001302 init_waitqueue_head(&buffer->irq_work.waiters);
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -05001303
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001304 /* need at least two pages */
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001305 if (nr_pages < 2)
1306 nr_pages = 2;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001307
Frederic Weisbecker3bf832c2009-03-19 14:47:33 +01001308 /*
1309 * In case of non-hotplug cpu, if the ring-buffer is allocated
1310 * in early initcall, it will not be notified of secondary cpus.
1311 * In that off case, we need to allocate for all possible cpus.
1312 */
1313#ifdef CONFIG_HOTPLUG_CPU
Srivatsa S. Bhatd39ad272014-03-11 02:11:56 +05301314 cpu_notifier_register_begin();
Steven Rostedt554f7862009-03-11 22:00:13 -04001315 cpumask_copy(buffer->cpumask, cpu_online_mask);
Frederic Weisbecker3bf832c2009-03-19 14:47:33 +01001316#else
1317 cpumask_copy(buffer->cpumask, cpu_possible_mask);
1318#endif
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001319 buffer->cpus = nr_cpu_ids;
1320
1321 bsize = sizeof(void *) * nr_cpu_ids;
1322 buffer->buffers = kzalloc(ALIGN(bsize, cache_line_size()),
1323 GFP_KERNEL);
1324 if (!buffer->buffers)
Rusty Russell9e01c1b2009-01-01 10:12:22 +10301325 goto fail_free_cpumask;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001326
1327 for_each_buffer_cpu(buffer, cpu) {
1328 buffer->buffers[cpu] =
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001329 rb_allocate_cpu_buffer(buffer, nr_pages, cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001330 if (!buffer->buffers[cpu])
1331 goto fail_free_buffers;
1332 }
1333
Steven Rostedt59222ef2009-03-12 11:46:03 -04001334#ifdef CONFIG_HOTPLUG_CPU
Steven Rostedt554f7862009-03-11 22:00:13 -04001335 buffer->cpu_notify.notifier_call = rb_cpu_notify;
1336 buffer->cpu_notify.priority = 0;
Srivatsa S. Bhatd39ad272014-03-11 02:11:56 +05301337 __register_cpu_notifier(&buffer->cpu_notify);
1338 cpu_notifier_register_done();
Steven Rostedt554f7862009-03-11 22:00:13 -04001339#endif
1340
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001341 mutex_init(&buffer->mutex);
1342
1343 return buffer;
1344
1345 fail_free_buffers:
1346 for_each_buffer_cpu(buffer, cpu) {
1347 if (buffer->buffers[cpu])
1348 rb_free_cpu_buffer(buffer->buffers[cpu]);
1349 }
1350 kfree(buffer->buffers);
1351
Rusty Russell9e01c1b2009-01-01 10:12:22 +10301352 fail_free_cpumask:
1353 free_cpumask_var(buffer->cpumask);
Srivatsa S. Bhatd39ad272014-03-11 02:11:56 +05301354#ifdef CONFIG_HOTPLUG_CPU
1355 cpu_notifier_register_done();
1356#endif
Rusty Russell9e01c1b2009-01-01 10:12:22 +10301357
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001358 fail_free_buffer:
1359 kfree(buffer);
1360 return NULL;
1361}
Peter Zijlstra1f8a6a12009-06-08 18:18:39 +02001362EXPORT_SYMBOL_GPL(__ring_buffer_alloc);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001363
1364/**
1365 * ring_buffer_free - free a ring buffer.
1366 * @buffer: the buffer to free.
1367 */
1368void
1369ring_buffer_free(struct ring_buffer *buffer)
1370{
1371 int cpu;
1372
Steven Rostedt59222ef2009-03-12 11:46:03 -04001373#ifdef CONFIG_HOTPLUG_CPU
Srivatsa S. Bhatd39ad272014-03-11 02:11:56 +05301374 cpu_notifier_register_begin();
1375 __unregister_cpu_notifier(&buffer->cpu_notify);
Steven Rostedt554f7862009-03-11 22:00:13 -04001376#endif
1377
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001378 for_each_buffer_cpu(buffer, cpu)
1379 rb_free_cpu_buffer(buffer->buffers[cpu]);
1380
Srivatsa S. Bhatd39ad272014-03-11 02:11:56 +05301381#ifdef CONFIG_HOTPLUG_CPU
1382 cpu_notifier_register_done();
1383#endif
Steven Rostedt554f7862009-03-11 22:00:13 -04001384
Eric Dumazetbd3f0222009-08-07 12:49:29 +02001385 kfree(buffer->buffers);
Rusty Russell9e01c1b2009-01-01 10:12:22 +10301386 free_cpumask_var(buffer->cpumask);
1387
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001388 kfree(buffer);
1389}
Robert Richterc4f50182008-12-11 16:49:22 +01001390EXPORT_SYMBOL_GPL(ring_buffer_free);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001391
Steven Rostedt37886f62009-03-17 17:22:06 -04001392void ring_buffer_set_clock(struct ring_buffer *buffer,
1393 u64 (*clock)(void))
1394{
1395 buffer->clock = clock;
1396}
1397
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001398static void rb_reset_cpu(struct ring_buffer_per_cpu *cpu_buffer);
1399
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001400static inline unsigned long rb_page_entries(struct buffer_page *bpage)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001401{
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001402 return local_read(&bpage->entries) & RB_WRITE_MASK;
1403}
1404
1405static inline unsigned long rb_page_write(struct buffer_page *bpage)
1406{
1407 return local_read(&bpage->write) & RB_WRITE_MASK;
1408}
1409
Vaibhav Nagarnaik5040b4b2012-05-03 18:59:51 -07001410static int
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001411rb_remove_pages(struct ring_buffer_per_cpu *cpu_buffer, unsigned int nr_pages)
1412{
1413 struct list_head *tail_page, *to_remove, *next_page;
1414 struct buffer_page *to_remove_page, *tmp_iter_page;
1415 struct buffer_page *last_page, *first_page;
1416 unsigned int nr_removed;
1417 unsigned long head_bit;
1418 int page_entries;
1419
1420 head_bit = 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001421
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02001422 raw_spin_lock_irq(&cpu_buffer->reader_lock);
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001423 atomic_inc(&cpu_buffer->record_disabled);
1424 /*
1425 * We don't race with the readers since we have acquired the reader
1426 * lock. We also don't race with writers after disabling recording.
1427 * This makes it easy to figure out the first and the last page to be
1428 * removed from the list. We unlink all the pages in between including
1429 * the first and last pages. This is done in a busy loop so that we
1430 * lose the least number of traces.
1431 * The pages are freed after we restart recording and unlock readers.
1432 */
1433 tail_page = &cpu_buffer->tail_page->list;
Steven Rostedt77ae3652009-03-27 11:00:29 -04001434
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001435 /*
1436 * tail page might be on reader page, we remove the next page
1437 * from the ring buffer
1438 */
1439 if (cpu_buffer->tail_page == cpu_buffer->reader_page)
1440 tail_page = rb_list_head(tail_page->next);
1441 to_remove = tail_page;
1442
1443 /* start of pages to remove */
1444 first_page = list_entry(rb_list_head(to_remove->next),
1445 struct buffer_page, list);
1446
1447 for (nr_removed = 0; nr_removed < nr_pages; nr_removed++) {
1448 to_remove = rb_list_head(to_remove)->next;
1449 head_bit |= (unsigned long)to_remove & RB_PAGE_HEAD;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001450 }
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001451
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001452 next_page = rb_list_head(to_remove)->next;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001453
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001454 /*
1455 * Now we remove all pages between tail_page and next_page.
1456 * Make sure that we have head_bit value preserved for the
1457 * next page
1458 */
1459 tail_page->next = (struct list_head *)((unsigned long)next_page |
1460 head_bit);
1461 next_page = rb_list_head(next_page);
1462 next_page->prev = tail_page;
1463
1464 /* make sure pages points to a valid page in the ring buffer */
1465 cpu_buffer->pages = next_page;
1466
1467 /* update head page */
1468 if (head_bit)
1469 cpu_buffer->head_page = list_entry(next_page,
1470 struct buffer_page, list);
1471
1472 /*
1473 * change read pointer to make sure any read iterators reset
1474 * themselves
1475 */
1476 cpu_buffer->read = 0;
1477
1478 /* pages are removed, resume tracing and then free the pages */
1479 atomic_dec(&cpu_buffer->record_disabled);
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02001480 raw_spin_unlock_irq(&cpu_buffer->reader_lock);
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001481
1482 RB_WARN_ON(cpu_buffer, list_empty(cpu_buffer->pages));
1483
1484 /* last buffer page to remove */
1485 last_page = list_entry(rb_list_head(to_remove), struct buffer_page,
1486 list);
1487 tmp_iter_page = first_page;
1488
1489 do {
1490 to_remove_page = tmp_iter_page;
1491 rb_inc_page(cpu_buffer, &tmp_iter_page);
1492
1493 /* update the counters */
1494 page_entries = rb_page_entries(to_remove_page);
1495 if (page_entries) {
1496 /*
1497 * If something was added to this page, it was full
1498 * since it is not the tail page. So we deduct the
1499 * bytes consumed in ring buffer from here.
Vaibhav Nagarnaik48fdc722012-06-29 12:31:41 -07001500 * Increment overrun to account for the lost events.
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001501 */
Vaibhav Nagarnaik48fdc722012-06-29 12:31:41 -07001502 local_add(page_entries, &cpu_buffer->overrun);
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001503 local_sub(BUF_PAGE_SIZE, &cpu_buffer->entries_bytes);
1504 }
1505
1506 /*
1507 * We have already removed references to this list item, just
1508 * free up the buffer_page and its page
1509 */
1510 free_buffer_page(to_remove_page);
1511 nr_removed--;
1512
1513 } while (to_remove_page != last_page);
1514
1515 RB_WARN_ON(cpu_buffer, nr_removed);
Vaibhav Nagarnaik5040b4b2012-05-03 18:59:51 -07001516
1517 return nr_removed == 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001518}
1519
Vaibhav Nagarnaik5040b4b2012-05-03 18:59:51 -07001520static int
1521rb_insert_pages(struct ring_buffer_per_cpu *cpu_buffer)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001522{
Vaibhav Nagarnaik5040b4b2012-05-03 18:59:51 -07001523 struct list_head *pages = &cpu_buffer->new_pages;
1524 int retries, success;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001525
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02001526 raw_spin_lock_irq(&cpu_buffer->reader_lock);
Vaibhav Nagarnaik5040b4b2012-05-03 18:59:51 -07001527 /*
1528 * We are holding the reader lock, so the reader page won't be swapped
1529 * in the ring buffer. Now we are racing with the writer trying to
1530 * move head page and the tail page.
1531 * We are going to adapt the reader page update process where:
1532 * 1. We first splice the start and end of list of new pages between
1533 * the head page and its previous page.
1534 * 2. We cmpxchg the prev_page->next to point from head page to the
1535 * start of new pages list.
1536 * 3. Finally, we update the head->prev to the end of new list.
1537 *
1538 * We will try this process 10 times, to make sure that we don't keep
1539 * spinning.
1540 */
1541 retries = 10;
1542 success = 0;
1543 while (retries--) {
1544 struct list_head *head_page, *prev_page, *r;
1545 struct list_head *last_page, *first_page;
1546 struct list_head *head_page_with_bit;
Steven Rostedt77ae3652009-03-27 11:00:29 -04001547
Vaibhav Nagarnaik5040b4b2012-05-03 18:59:51 -07001548 head_page = &rb_set_head_page(cpu_buffer)->list;
Steven Rostedt54f7be52012-11-29 22:27:22 -05001549 if (!head_page)
1550 break;
Vaibhav Nagarnaik5040b4b2012-05-03 18:59:51 -07001551 prev_page = head_page->prev;
1552
1553 first_page = pages->next;
1554 last_page = pages->prev;
1555
1556 head_page_with_bit = (struct list_head *)
1557 ((unsigned long)head_page | RB_PAGE_HEAD);
1558
1559 last_page->next = head_page_with_bit;
1560 first_page->prev = prev_page;
1561
1562 r = cmpxchg(&prev_page->next, head_page_with_bit, first_page);
1563
1564 if (r == head_page_with_bit) {
1565 /*
1566 * yay, we replaced the page pointer to our new list,
1567 * now, we just have to update to head page's prev
1568 * pointer to point to end of list
1569 */
1570 head_page->prev = last_page;
1571 success = 1;
1572 break;
1573 }
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001574 }
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001575
Vaibhav Nagarnaik5040b4b2012-05-03 18:59:51 -07001576 if (success)
1577 INIT_LIST_HEAD(pages);
1578 /*
1579 * If we weren't successful in adding in new pages, warn and stop
1580 * tracing
1581 */
1582 RB_WARN_ON(cpu_buffer, !success);
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02001583 raw_spin_unlock_irq(&cpu_buffer->reader_lock);
Vaibhav Nagarnaik5040b4b2012-05-03 18:59:51 -07001584
1585 /* free pages if they weren't inserted */
1586 if (!success) {
1587 struct buffer_page *bpage, *tmp;
1588 list_for_each_entry_safe(bpage, tmp, &cpu_buffer->new_pages,
1589 list) {
1590 list_del_init(&bpage->list);
1591 free_buffer_page(bpage);
1592 }
1593 }
1594 return success;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001595}
1596
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001597static void rb_update_pages(struct ring_buffer_per_cpu *cpu_buffer)
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001598{
Vaibhav Nagarnaik5040b4b2012-05-03 18:59:51 -07001599 int success;
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001600
Vaibhav Nagarnaik5040b4b2012-05-03 18:59:51 -07001601 if (cpu_buffer->nr_pages_to_update > 0)
1602 success = rb_insert_pages(cpu_buffer);
1603 else
1604 success = rb_remove_pages(cpu_buffer,
1605 -cpu_buffer->nr_pages_to_update);
1606
1607 if (success)
1608 cpu_buffer->nr_pages += cpu_buffer->nr_pages_to_update;
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001609}
1610
1611static void update_pages_handler(struct work_struct *work)
1612{
1613 struct ring_buffer_per_cpu *cpu_buffer = container_of(work,
1614 struct ring_buffer_per_cpu, update_pages_work);
1615 rb_update_pages(cpu_buffer);
Vaibhav Nagarnaik05fdd702012-05-18 13:29:51 -07001616 complete(&cpu_buffer->update_done);
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001617}
1618
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001619/**
1620 * ring_buffer_resize - resize the ring buffer
1621 * @buffer: the buffer to resize.
1622 * @size: the new size.
zhangwei(Jovi)d6118512013-07-15 16:32:50 +08001623 * @cpu_id: the cpu buffer to resize
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001624 *
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001625 * Minimum size is 2 * BUF_PAGE_SIZE.
1626 *
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001627 * Returns 0 on success and < 0 on failure.
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001628 */
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001629int ring_buffer_resize(struct ring_buffer *buffer, unsigned long size,
1630 int cpu_id)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001631{
1632 struct ring_buffer_per_cpu *cpu_buffer;
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001633 unsigned nr_pages;
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001634 int cpu, err = 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001635
Ingo Molnaree51a1d2008-11-13 14:58:31 +01001636 /*
1637 * Always succeed at resizing a non-existent buffer:
1638 */
1639 if (!buffer)
1640 return size;
1641
Steven Rostedt6a31e1f2012-05-23 15:35:17 -04001642 /* Make sure the requested buffer exists */
1643 if (cpu_id != RING_BUFFER_ALL_CPUS &&
1644 !cpumask_test_cpu(cpu_id, buffer->cpumask))
1645 return size;
1646
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001647 size = DIV_ROUND_UP(size, BUF_PAGE_SIZE);
1648 size *= BUF_PAGE_SIZE;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001649
1650 /* we need a minimum of two pages */
1651 if (size < BUF_PAGE_SIZE * 2)
1652 size = BUF_PAGE_SIZE * 2;
1653
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001654 nr_pages = DIV_ROUND_UP(size, BUF_PAGE_SIZE);
1655
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001656 /*
1657 * Don't succeed if resizing is disabled, as a reader might be
1658 * manipulating the ring buffer and is expecting a sane state while
1659 * this is true.
1660 */
1661 if (atomic_read(&buffer->resize_disabled))
1662 return -EBUSY;
1663
1664 /* prevent another thread from changing buffer sizes */
1665 mutex_lock(&buffer->mutex);
1666
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001667 if (cpu_id == RING_BUFFER_ALL_CPUS) {
1668 /* calculate the pages to update */
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001669 for_each_buffer_cpu(buffer, cpu) {
1670 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001671
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001672 cpu_buffer->nr_pages_to_update = nr_pages -
1673 cpu_buffer->nr_pages;
Vaibhav Nagarnaikd7ec4bf2011-06-07 17:01:42 -07001674 /*
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001675 * nothing more to do for removing pages or no update
Vaibhav Nagarnaikd7ec4bf2011-06-07 17:01:42 -07001676 */
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001677 if (cpu_buffer->nr_pages_to_update <= 0)
1678 continue;
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001679 /*
1680 * to add pages, make sure all new pages can be
1681 * allocated without receiving ENOMEM
1682 */
1683 INIT_LIST_HEAD(&cpu_buffer->new_pages);
1684 if (__rb_allocate_pages(cpu_buffer->nr_pages_to_update,
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001685 &cpu_buffer->new_pages, cpu)) {
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001686 /* not enough memory for new pages */
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001687 err = -ENOMEM;
1688 goto out_err;
1689 }
1690 }
1691
1692 get_online_cpus();
1693 /*
1694 * Fire off all the required work handlers
Vaibhav Nagarnaik05fdd702012-05-18 13:29:51 -07001695 * We can't schedule on offline CPUs, but it's not necessary
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001696 * since we can change their buffer sizes without any race.
1697 */
1698 for_each_buffer_cpu(buffer, cpu) {
1699 cpu_buffer = buffer->buffers[cpu];
Vaibhav Nagarnaik05fdd702012-05-18 13:29:51 -07001700 if (!cpu_buffer->nr_pages_to_update)
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001701 continue;
1702
Corey Minyard021c5b32014-07-16 14:07:13 -05001703 /* Can't run something on an offline CPU. */
1704 if (!cpu_online(cpu)) {
Steven Rostedt (Red Hat)f5eb5582013-03-07 09:27:42 -05001705 rb_update_pages(cpu_buffer);
1706 cpu_buffer->nr_pages_to_update = 0;
1707 } else {
Vaibhav Nagarnaik05fdd702012-05-18 13:29:51 -07001708 schedule_work_on(cpu,
1709 &cpu_buffer->update_pages_work);
Steven Rostedt (Red Hat)f5eb5582013-03-07 09:27:42 -05001710 }
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001711 }
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001712
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001713 /* wait for all the updates to complete */
1714 for_each_buffer_cpu(buffer, cpu) {
1715 cpu_buffer = buffer->buffers[cpu];
Vaibhav Nagarnaik05fdd702012-05-18 13:29:51 -07001716 if (!cpu_buffer->nr_pages_to_update)
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001717 continue;
1718
Vaibhav Nagarnaik05fdd702012-05-18 13:29:51 -07001719 if (cpu_online(cpu))
1720 wait_for_completion(&cpu_buffer->update_done);
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001721 cpu_buffer->nr_pages_to_update = 0;
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001722 }
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001723
1724 put_online_cpus();
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001725 } else {
Vaibhav Nagarnaik8e49f412012-10-10 16:40:27 -07001726 /* Make sure this CPU has been intitialized */
1727 if (!cpumask_test_cpu(cpu_id, buffer->cpumask))
1728 goto out;
1729
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001730 cpu_buffer = buffer->buffers[cpu_id];
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001731
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001732 if (nr_pages == cpu_buffer->nr_pages)
1733 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001734
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001735 cpu_buffer->nr_pages_to_update = nr_pages -
1736 cpu_buffer->nr_pages;
1737
1738 INIT_LIST_HEAD(&cpu_buffer->new_pages);
1739 if (cpu_buffer->nr_pages_to_update > 0 &&
1740 __rb_allocate_pages(cpu_buffer->nr_pages_to_update,
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001741 &cpu_buffer->new_pages, cpu_id)) {
1742 err = -ENOMEM;
1743 goto out_err;
1744 }
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001745
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001746 get_online_cpus();
1747
Corey Minyard021c5b32014-07-16 14:07:13 -05001748 /* Can't run something on an offline CPU. */
1749 if (!cpu_online(cpu_id))
Steven Rostedt (Red Hat)f5eb5582013-03-07 09:27:42 -05001750 rb_update_pages(cpu_buffer);
1751 else {
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001752 schedule_work_on(cpu_id,
1753 &cpu_buffer->update_pages_work);
Vaibhav Nagarnaik05fdd702012-05-18 13:29:51 -07001754 wait_for_completion(&cpu_buffer->update_done);
Steven Rostedt (Red Hat)f5eb5582013-03-07 09:27:42 -05001755 }
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001756
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001757 cpu_buffer->nr_pages_to_update = 0;
Vaibhav Nagarnaik05fdd702012-05-18 13:29:51 -07001758 put_online_cpus();
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001759 }
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001760
1761 out:
Steven Rostedt659f4512012-05-14 17:02:33 -04001762 /*
1763 * The ring buffer resize can happen with the ring buffer
1764 * enabled, so that the update disturbs the tracing as little
1765 * as possible. But if the buffer is disabled, we do not need
1766 * to worry about that, and we can take the time to verify
1767 * that the buffer is not corrupt.
1768 */
1769 if (atomic_read(&buffer->record_disabled)) {
1770 atomic_inc(&buffer->record_disabled);
1771 /*
1772 * Even though the buffer was disabled, we must make sure
1773 * that it is truly disabled before calling rb_check_pages.
1774 * There could have been a race between checking
1775 * record_disable and incrementing it.
1776 */
1777 synchronize_sched();
1778 for_each_buffer_cpu(buffer, cpu) {
1779 cpu_buffer = buffer->buffers[cpu];
1780 rb_check_pages(cpu_buffer);
1781 }
1782 atomic_dec(&buffer->record_disabled);
1783 }
1784
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001785 mutex_unlock(&buffer->mutex);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001786 return size;
1787
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001788 out_err:
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001789 for_each_buffer_cpu(buffer, cpu) {
1790 struct buffer_page *bpage, *tmp;
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001791
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001792 cpu_buffer = buffer->buffers[cpu];
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001793 cpu_buffer->nr_pages_to_update = 0;
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001794
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001795 if (list_empty(&cpu_buffer->new_pages))
1796 continue;
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001797
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001798 list_for_each_entry_safe(bpage, tmp, &cpu_buffer->new_pages,
1799 list) {
1800 list_del_init(&bpage->list);
1801 free_buffer_page(bpage);
1802 }
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001803 }
Vegard Nossum641d2f62008-11-18 19:22:13 +01001804 mutex_unlock(&buffer->mutex);
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001805 return err;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001806}
Robert Richterc4f50182008-12-11 16:49:22 +01001807EXPORT_SYMBOL_GPL(ring_buffer_resize);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001808
David Sharp750912f2010-12-08 13:46:47 -08001809void ring_buffer_change_overwrite(struct ring_buffer *buffer, int val)
1810{
1811 mutex_lock(&buffer->mutex);
1812 if (val)
1813 buffer->flags |= RB_FL_OVERWRITE;
1814 else
1815 buffer->flags &= ~RB_FL_OVERWRITE;
1816 mutex_unlock(&buffer->mutex);
1817}
1818EXPORT_SYMBOL_GPL(ring_buffer_change_overwrite);
1819
Steven Rostedt8789a9e2008-12-02 15:34:07 -05001820static inline void *
Steven Rostedt044fa782008-12-02 23:50:03 -05001821__rb_data_page_index(struct buffer_data_page *bpage, unsigned index)
Steven Rostedt8789a9e2008-12-02 15:34:07 -05001822{
Steven Rostedt044fa782008-12-02 23:50:03 -05001823 return bpage->data + index;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05001824}
1825
Steven Rostedt044fa782008-12-02 23:50:03 -05001826static inline void *__rb_page_index(struct buffer_page *bpage, unsigned index)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001827{
Steven Rostedt044fa782008-12-02 23:50:03 -05001828 return bpage->page->data + index;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001829}
1830
1831static inline struct ring_buffer_event *
Steven Rostedtd7690412008-10-01 00:29:53 -04001832rb_reader_event(struct ring_buffer_per_cpu *cpu_buffer)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001833{
Steven Rostedt6f807ac2008-10-04 02:00:58 -04001834 return __rb_page_index(cpu_buffer->reader_page,
1835 cpu_buffer->reader_page->read);
1836}
1837
1838static inline struct ring_buffer_event *
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001839rb_iter_head_event(struct ring_buffer_iter *iter)
1840{
Steven Rostedt6f807ac2008-10-04 02:00:58 -04001841 return __rb_page_index(iter->head_page, iter->head);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001842}
1843
Steven Rostedtbf41a152008-10-04 02:00:59 -04001844static inline unsigned rb_page_commit(struct buffer_page *bpage)
1845{
Steven Rostedtabc9b562008-12-02 15:34:06 -05001846 return local_read(&bpage->page->commit);
Steven Rostedtbf41a152008-10-04 02:00:59 -04001847}
1848
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001849/* Size is determined by what has been committed */
Steven Rostedtbf41a152008-10-04 02:00:59 -04001850static inline unsigned rb_page_size(struct buffer_page *bpage)
1851{
1852 return rb_page_commit(bpage);
1853}
1854
1855static inline unsigned
1856rb_commit_index(struct ring_buffer_per_cpu *cpu_buffer)
1857{
1858 return rb_page_commit(cpu_buffer->commit_page);
1859}
1860
Steven Rostedtbf41a152008-10-04 02:00:59 -04001861static inline unsigned
1862rb_event_index(struct ring_buffer_event *event)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001863{
Steven Rostedtbf41a152008-10-04 02:00:59 -04001864 unsigned long addr = (unsigned long)event;
1865
Steven Rostedt22f470f2009-06-11 09:29:58 -04001866 return (addr & ~PAGE_MASK) - BUF_PAGE_HDR_SIZE;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001867}
1868
Steven Rostedt0f0c85f2009-05-11 16:08:00 -04001869static inline int
Steven Rostedtfa743952009-06-16 12:37:57 -04001870rb_event_is_commit(struct ring_buffer_per_cpu *cpu_buffer,
1871 struct ring_buffer_event *event)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001872{
Steven Rostedtbf41a152008-10-04 02:00:59 -04001873 unsigned long addr = (unsigned long)event;
1874 unsigned long index;
1875
1876 index = rb_event_index(event);
1877 addr &= PAGE_MASK;
1878
1879 return cpu_buffer->commit_page->page == (void *)addr &&
1880 rb_commit_index(cpu_buffer) == index;
1881}
1882
Andrew Morton34a148b2009-01-09 12:27:09 -08001883static void
Steven Rostedtbf41a152008-10-04 02:00:59 -04001884rb_set_commit_to_write(struct ring_buffer_per_cpu *cpu_buffer)
1885{
Steven Rostedt77ae3652009-03-27 11:00:29 -04001886 unsigned long max_count;
1887
Steven Rostedtbf41a152008-10-04 02:00:59 -04001888 /*
1889 * We only race with interrupts and NMIs on this CPU.
1890 * If we own the commit event, then we can commit
1891 * all others that interrupted us, since the interruptions
1892 * are in stack format (they finish before they come
1893 * back to us). This allows us to do a simple loop to
1894 * assign the commit to the tail.
1895 */
Steven Rostedta8ccf1d2008-12-23 11:32:24 -05001896 again:
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001897 max_count = cpu_buffer->nr_pages * 100;
Steven Rostedt77ae3652009-03-27 11:00:29 -04001898
Steven Rostedtbf41a152008-10-04 02:00:59 -04001899 while (cpu_buffer->commit_page != cpu_buffer->tail_page) {
Steven Rostedt77ae3652009-03-27 11:00:29 -04001900 if (RB_WARN_ON(cpu_buffer, !(--max_count)))
1901 return;
1902 if (RB_WARN_ON(cpu_buffer,
1903 rb_is_reader_page(cpu_buffer->tail_page)))
1904 return;
1905 local_set(&cpu_buffer->commit_page->page->commit,
1906 rb_page_write(cpu_buffer->commit_page));
Steven Rostedtbf41a152008-10-04 02:00:59 -04001907 rb_inc_page(cpu_buffer, &cpu_buffer->commit_page);
Steven Rostedtabc9b562008-12-02 15:34:06 -05001908 cpu_buffer->write_stamp =
1909 cpu_buffer->commit_page->page->time_stamp;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001910 /* add barrier to keep gcc from optimizing too much */
1911 barrier();
1912 }
1913 while (rb_commit_index(cpu_buffer) !=
1914 rb_page_write(cpu_buffer->commit_page)) {
Steven Rostedt77ae3652009-03-27 11:00:29 -04001915
1916 local_set(&cpu_buffer->commit_page->page->commit,
1917 rb_page_write(cpu_buffer->commit_page));
1918 RB_WARN_ON(cpu_buffer,
1919 local_read(&cpu_buffer->commit_page->page->commit) &
1920 ~RB_WRITE_MASK);
Steven Rostedtbf41a152008-10-04 02:00:59 -04001921 barrier();
1922 }
Steven Rostedta8ccf1d2008-12-23 11:32:24 -05001923
1924 /* again, keep gcc from optimizing */
1925 barrier();
1926
1927 /*
1928 * If an interrupt came in just after the first while loop
1929 * and pushed the tail page forward, we will be left with
1930 * a dangling commit that will never go forward.
1931 */
1932 if (unlikely(cpu_buffer->commit_page != cpu_buffer->tail_page))
1933 goto again;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001934}
1935
Steven Rostedtd7690412008-10-01 00:29:53 -04001936static void rb_reset_reader_page(struct ring_buffer_per_cpu *cpu_buffer)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001937{
Steven Rostedtabc9b562008-12-02 15:34:06 -05001938 cpu_buffer->read_stamp = cpu_buffer->reader_page->page->time_stamp;
Steven Rostedt6f807ac2008-10-04 02:00:58 -04001939 cpu_buffer->reader_page->read = 0;
Steven Rostedtd7690412008-10-01 00:29:53 -04001940}
1941
Andrew Morton34a148b2009-01-09 12:27:09 -08001942static void rb_inc_iter(struct ring_buffer_iter *iter)
Steven Rostedtd7690412008-10-01 00:29:53 -04001943{
1944 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
1945
1946 /*
1947 * The iterator could be on the reader page (it starts there).
1948 * But the head could have moved, since the reader was
1949 * found. Check for this case and assign the iterator
1950 * to the head page instead of next.
1951 */
1952 if (iter->head_page == cpu_buffer->reader_page)
Steven Rostedt77ae3652009-03-27 11:00:29 -04001953 iter->head_page = rb_set_head_page(cpu_buffer);
Steven Rostedtd7690412008-10-01 00:29:53 -04001954 else
1955 rb_inc_page(cpu_buffer, &iter->head_page);
1956
Steven Rostedtabc9b562008-12-02 15:34:06 -05001957 iter->read_stamp = iter->head_page->page->time_stamp;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001958 iter->head = 0;
1959}
1960
Steven Rostedt69d1b832010-10-07 18:18:05 -04001961/* Slow path, do not inline */
1962static noinline struct ring_buffer_event *
1963rb_add_time_stamp(struct ring_buffer_event *event, u64 delta)
1964{
1965 event->type_len = RINGBUF_TYPE_TIME_EXTEND;
1966
1967 /* Not the first event on the page? */
1968 if (rb_event_index(event)) {
1969 event->time_delta = delta & TS_MASK;
1970 event->array[0] = delta >> TS_SHIFT;
1971 } else {
1972 /* nope, just zero it */
1973 event->time_delta = 0;
1974 event->array[0] = 0;
1975 }
1976
1977 return skip_time_extend(event);
1978}
1979
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001980/**
David Sharp01e3e712012-06-07 16:46:24 -07001981 * rb_update_event - update event type and data
Steven Rostedt (Red Hat)021de3d2014-08-06 15:36:31 -04001982 * @event: the event to update
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001983 * @type: the type of event
1984 * @length: the size of the event field in the ring buffer
1985 *
1986 * Update the type and data fields of the event. The length
1987 * is the actual size that is written to the ring buffer,
1988 * and with this, we can determine what to place into the
1989 * data field.
1990 */
Andrew Morton34a148b2009-01-09 12:27:09 -08001991static void
Steven Rostedt69d1b832010-10-07 18:18:05 -04001992rb_update_event(struct ring_buffer_per_cpu *cpu_buffer,
1993 struct ring_buffer_event *event, unsigned length,
1994 int add_timestamp, u64 delta)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001995{
Steven Rostedt69d1b832010-10-07 18:18:05 -04001996 /* Only a commit updates the timestamp */
1997 if (unlikely(!rb_event_is_commit(cpu_buffer, event)))
1998 delta = 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001999
Steven Rostedt69d1b832010-10-07 18:18:05 -04002000 /*
2001 * If we need to add a timestamp, then we
2002 * add it to the start of the resevered space.
2003 */
2004 if (unlikely(add_timestamp)) {
2005 event = rb_add_time_stamp(event, delta);
2006 length -= RB_LEN_TIME_EXTEND;
2007 delta = 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002008 }
Steven Rostedt69d1b832010-10-07 18:18:05 -04002009
2010 event->time_delta = delta;
2011 length -= RB_EVNT_HDR_SIZE;
2012 if (length > RB_MAX_SMALL_DATA || RB_FORCE_8BYTE_ALIGNMENT) {
2013 event->type_len = 0;
2014 event->array[0] = length;
2015 } else
2016 event->type_len = DIV_ROUND_UP(length, RB_ALIGNMENT);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002017}
2018
Steven Rostedt77ae3652009-03-27 11:00:29 -04002019/*
2020 * rb_handle_head_page - writer hit the head page
2021 *
2022 * Returns: +1 to retry page
2023 * 0 to continue
2024 * -1 on error
2025 */
2026static int
2027rb_handle_head_page(struct ring_buffer_per_cpu *cpu_buffer,
2028 struct buffer_page *tail_page,
2029 struct buffer_page *next_page)
2030{
2031 struct buffer_page *new_head;
2032 int entries;
2033 int type;
2034 int ret;
2035
2036 entries = rb_page_entries(next_page);
2037
2038 /*
2039 * The hard part is here. We need to move the head
2040 * forward, and protect against both readers on
2041 * other CPUs and writers coming in via interrupts.
2042 */
2043 type = rb_head_page_set_update(cpu_buffer, next_page, tail_page,
2044 RB_PAGE_HEAD);
2045
2046 /*
2047 * type can be one of four:
2048 * NORMAL - an interrupt already moved it for us
2049 * HEAD - we are the first to get here.
2050 * UPDATE - we are the interrupt interrupting
2051 * a current move.
2052 * MOVED - a reader on another CPU moved the next
2053 * pointer to its reader page. Give up
2054 * and try again.
2055 */
2056
2057 switch (type) {
2058 case RB_PAGE_HEAD:
2059 /*
2060 * We changed the head to UPDATE, thus
2061 * it is our responsibility to update
2062 * the counters.
2063 */
2064 local_add(entries, &cpu_buffer->overrun);
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -07002065 local_sub(BUF_PAGE_SIZE, &cpu_buffer->entries_bytes);
Steven Rostedt77ae3652009-03-27 11:00:29 -04002066
2067 /*
2068 * The entries will be zeroed out when we move the
2069 * tail page.
2070 */
2071
2072 /* still more to do */
2073 break;
2074
2075 case RB_PAGE_UPDATE:
2076 /*
2077 * This is an interrupt that interrupt the
2078 * previous update. Still more to do.
2079 */
2080 break;
2081 case RB_PAGE_NORMAL:
2082 /*
2083 * An interrupt came in before the update
2084 * and processed this for us.
2085 * Nothing left to do.
2086 */
2087 return 1;
2088 case RB_PAGE_MOVED:
2089 /*
2090 * The reader is on another CPU and just did
2091 * a swap with our next_page.
2092 * Try again.
2093 */
2094 return 1;
2095 default:
2096 RB_WARN_ON(cpu_buffer, 1); /* WTF??? */
2097 return -1;
2098 }
2099
2100 /*
2101 * Now that we are here, the old head pointer is
2102 * set to UPDATE. This will keep the reader from
2103 * swapping the head page with the reader page.
2104 * The reader (on another CPU) will spin till
2105 * we are finished.
2106 *
2107 * We just need to protect against interrupts
2108 * doing the job. We will set the next pointer
2109 * to HEAD. After that, we set the old pointer
2110 * to NORMAL, but only if it was HEAD before.
2111 * otherwise we are an interrupt, and only
2112 * want the outer most commit to reset it.
2113 */
2114 new_head = next_page;
2115 rb_inc_page(cpu_buffer, &new_head);
2116
2117 ret = rb_head_page_set_head(cpu_buffer, new_head, next_page,
2118 RB_PAGE_NORMAL);
2119
2120 /*
2121 * Valid returns are:
2122 * HEAD - an interrupt came in and already set it.
2123 * NORMAL - One of two things:
2124 * 1) We really set it.
2125 * 2) A bunch of interrupts came in and moved
2126 * the page forward again.
2127 */
2128 switch (ret) {
2129 case RB_PAGE_HEAD:
2130 case RB_PAGE_NORMAL:
2131 /* OK */
2132 break;
2133 default:
2134 RB_WARN_ON(cpu_buffer, 1);
2135 return -1;
2136 }
2137
2138 /*
2139 * It is possible that an interrupt came in,
2140 * set the head up, then more interrupts came in
2141 * and moved it again. When we get back here,
2142 * the page would have been set to NORMAL but we
2143 * just set it back to HEAD.
2144 *
2145 * How do you detect this? Well, if that happened
2146 * the tail page would have moved.
2147 */
2148 if (ret == RB_PAGE_NORMAL) {
2149 /*
2150 * If the tail had moved passed next, then we need
2151 * to reset the pointer.
2152 */
2153 if (cpu_buffer->tail_page != tail_page &&
2154 cpu_buffer->tail_page != next_page)
2155 rb_head_page_set_normal(cpu_buffer, new_head,
2156 next_page,
2157 RB_PAGE_HEAD);
2158 }
2159
2160 /*
2161 * If this was the outer most commit (the one that
2162 * changed the original pointer from HEAD to UPDATE),
2163 * then it is up to us to reset it to NORMAL.
2164 */
2165 if (type == RB_PAGE_HEAD) {
2166 ret = rb_head_page_set_normal(cpu_buffer, next_page,
2167 tail_page,
2168 RB_PAGE_UPDATE);
2169 if (RB_WARN_ON(cpu_buffer,
2170 ret != RB_PAGE_UPDATE))
2171 return -1;
2172 }
2173
2174 return 0;
2175}
2176
Andrew Morton34a148b2009-01-09 12:27:09 -08002177static unsigned rb_calculate_event_length(unsigned length)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002178{
2179 struct ring_buffer_event event; /* Used only for sizeof array */
2180
2181 /* zero length can cause confusions */
2182 if (!length)
2183 length = 1;
2184
Steven Rostedt22710482010-03-18 17:54:19 -04002185 if (length > RB_MAX_SMALL_DATA || RB_FORCE_8BYTE_ALIGNMENT)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002186 length += sizeof(event.array[0]);
2187
2188 length += RB_EVNT_HDR_SIZE;
Steven Rostedt22710482010-03-18 17:54:19 -04002189 length = ALIGN(length, RB_ARCH_ALIGNMENT);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002190
2191 return length;
2192}
2193
Steven Rostedtc7b09302009-06-11 11:12:00 -04002194static inline void
2195rb_reset_tail(struct ring_buffer_per_cpu *cpu_buffer,
2196 struct buffer_page *tail_page,
2197 unsigned long tail, unsigned long length)
2198{
2199 struct ring_buffer_event *event;
2200
2201 /*
2202 * Only the event that crossed the page boundary
2203 * must fill the old tail_page with padding.
2204 */
2205 if (tail >= BUF_PAGE_SIZE) {
Steven Rostedtb3230c82010-05-21 11:55:21 -04002206 /*
2207 * If the page was filled, then we still need
2208 * to update the real_end. Reset it to zero
2209 * and the reader will ignore it.
2210 */
2211 if (tail == BUF_PAGE_SIZE)
2212 tail_page->real_end = 0;
2213
Steven Rostedtc7b09302009-06-11 11:12:00 -04002214 local_sub(length, &tail_page->write);
2215 return;
2216 }
2217
2218 event = __rb_page_index(tail_page, tail);
Linus Torvaldsb0b70652009-06-20 10:56:46 -07002219 kmemcheck_annotate_bitfield(event, bitfield);
Steven Rostedtc7b09302009-06-11 11:12:00 -04002220
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -07002221 /* account for padding bytes */
2222 local_add(BUF_PAGE_SIZE - tail, &cpu_buffer->entries_bytes);
2223
Steven Rostedtc7b09302009-06-11 11:12:00 -04002224 /*
Steven Rostedtff0ff842010-03-31 22:11:42 -04002225 * Save the original length to the meta data.
2226 * This will be used by the reader to add lost event
2227 * counter.
2228 */
2229 tail_page->real_end = tail;
2230
2231 /*
Steven Rostedtc7b09302009-06-11 11:12:00 -04002232 * If this event is bigger than the minimum size, then
2233 * we need to be careful that we don't subtract the
2234 * write counter enough to allow another writer to slip
2235 * in on this page.
2236 * We put in a discarded commit instead, to make sure
2237 * that this space is not used again.
2238 *
2239 * If we are less than the minimum size, we don't need to
2240 * worry about it.
2241 */
2242 if (tail > (BUF_PAGE_SIZE - RB_EVNT_MIN_SIZE)) {
2243 /* No room for any events */
2244
2245 /* Mark the rest of the page with padding */
2246 rb_event_set_padding(event);
2247
2248 /* Set the write back to the previous setting */
2249 local_sub(length, &tail_page->write);
2250 return;
2251 }
2252
2253 /* Put in a discarded event */
2254 event->array[0] = (BUF_PAGE_SIZE - tail) - RB_EVNT_HDR_SIZE;
2255 event->type_len = RINGBUF_TYPE_PADDING;
2256 /* time delta must be non zero */
2257 event->time_delta = 1;
Steven Rostedtc7b09302009-06-11 11:12:00 -04002258
2259 /* Set write to end of buffer */
2260 length = (tail + length) - BUF_PAGE_SIZE;
2261 local_sub(length, &tail_page->write);
2262}
Steven Rostedt6634ff22009-05-06 15:30:07 -04002263
Steven Rostedt747e94a2010-10-08 13:51:48 -04002264/*
2265 * This is the slow path, force gcc not to inline it.
2266 */
2267static noinline struct ring_buffer_event *
Steven Rostedt6634ff22009-05-06 15:30:07 -04002268rb_move_tail(struct ring_buffer_per_cpu *cpu_buffer,
2269 unsigned long length, unsigned long tail,
Steven Rostedte8bc43e2010-10-20 10:58:02 -04002270 struct buffer_page *tail_page, u64 ts)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002271{
Steven Rostedt5a50e332009-11-17 08:43:01 -05002272 struct buffer_page *commit_page = cpu_buffer->commit_page;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002273 struct ring_buffer *buffer = cpu_buffer->buffer;
Steven Rostedt77ae3652009-03-27 11:00:29 -04002274 struct buffer_page *next_page;
2275 int ret;
Steven Rostedtaa20ae82009-05-05 21:16:11 -04002276
2277 next_page = tail_page;
2278
Steven Rostedtaa20ae82009-05-05 21:16:11 -04002279 rb_inc_page(cpu_buffer, &next_page);
2280
Steven Rostedtaa20ae82009-05-05 21:16:11 -04002281 /*
2282 * If for some reason, we had an interrupt storm that made
2283 * it all the way around the buffer, bail, and warn
2284 * about it.
2285 */
2286 if (unlikely(next_page == commit_page)) {
Steven Rostedt77ae3652009-03-27 11:00:29 -04002287 local_inc(&cpu_buffer->commit_overrun);
Steven Rostedtaa20ae82009-05-05 21:16:11 -04002288 goto out_reset;
2289 }
2290
Steven Rostedt77ae3652009-03-27 11:00:29 -04002291 /*
2292 * This is where the fun begins!
2293 *
2294 * We are fighting against races between a reader that
2295 * could be on another CPU trying to swap its reader
2296 * page with the buffer head.
2297 *
2298 * We are also fighting against interrupts coming in and
2299 * moving the head or tail on us as well.
2300 *
2301 * If the next page is the head page then we have filled
2302 * the buffer, unless the commit page is still on the
2303 * reader page.
2304 */
2305 if (rb_is_head_page(cpu_buffer, next_page, &tail_page->list)) {
Steven Rostedtaa20ae82009-05-05 21:16:11 -04002306
Steven Rostedt77ae3652009-03-27 11:00:29 -04002307 /*
2308 * If the commit is not on the reader page, then
2309 * move the header page.
2310 */
2311 if (!rb_is_reader_page(cpu_buffer->commit_page)) {
2312 /*
2313 * If we are not in overwrite mode,
2314 * this is easy, just stop here.
2315 */
Slava Pestov884bfe82011-07-15 14:23:58 -07002316 if (!(buffer->flags & RB_FL_OVERWRITE)) {
2317 local_inc(&cpu_buffer->dropped_events);
Steven Rostedt77ae3652009-03-27 11:00:29 -04002318 goto out_reset;
Slava Pestov884bfe82011-07-15 14:23:58 -07002319 }
Steven Rostedtaa20ae82009-05-05 21:16:11 -04002320
Steven Rostedt77ae3652009-03-27 11:00:29 -04002321 ret = rb_handle_head_page(cpu_buffer,
2322 tail_page,
2323 next_page);
2324 if (ret < 0)
2325 goto out_reset;
2326 if (ret)
2327 goto out_again;
2328 } else {
2329 /*
2330 * We need to be careful here too. The
2331 * commit page could still be on the reader
2332 * page. We could have a small buffer, and
2333 * have filled up the buffer with events
2334 * from interrupts and such, and wrapped.
2335 *
2336 * Note, if the tail page is also the on the
2337 * reader_page, we let it move out.
2338 */
2339 if (unlikely((cpu_buffer->commit_page !=
2340 cpu_buffer->tail_page) &&
2341 (cpu_buffer->commit_page ==
2342 cpu_buffer->reader_page))) {
2343 local_inc(&cpu_buffer->commit_overrun);
2344 goto out_reset;
2345 }
Steven Rostedtaa20ae82009-05-05 21:16:11 -04002346 }
2347 }
2348
Steven Rostedt77ae3652009-03-27 11:00:29 -04002349 ret = rb_tail_page_update(cpu_buffer, tail_page, next_page);
2350 if (ret) {
2351 /*
2352 * Nested commits always have zero deltas, so
2353 * just reread the time stamp
2354 */
Steven Rostedte8bc43e2010-10-20 10:58:02 -04002355 ts = rb_time_stamp(buffer);
2356 next_page->page->time_stamp = ts;
Steven Rostedtaa20ae82009-05-05 21:16:11 -04002357 }
2358
Steven Rostedt77ae3652009-03-27 11:00:29 -04002359 out_again:
Steven Rostedtaa20ae82009-05-05 21:16:11 -04002360
Steven Rostedt77ae3652009-03-27 11:00:29 -04002361 rb_reset_tail(cpu_buffer, tail_page, tail, length);
Steven Rostedtaa20ae82009-05-05 21:16:11 -04002362
2363 /* fail and let the caller try again */
2364 return ERR_PTR(-EAGAIN);
2365
Steven Rostedt45141d42009-02-12 13:19:48 -05002366 out_reset:
Lai Jiangshan6f3b3442009-01-12 11:06:18 +08002367 /* reset write */
Steven Rostedtc7b09302009-06-11 11:12:00 -04002368 rb_reset_tail(cpu_buffer, tail_page, tail, length);
Lai Jiangshan6f3b3442009-01-12 11:06:18 +08002369
Steven Rostedtbf41a152008-10-04 02:00:59 -04002370 return NULL;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002371}
2372
Steven Rostedt6634ff22009-05-06 15:30:07 -04002373static struct ring_buffer_event *
2374__rb_reserve_next(struct ring_buffer_per_cpu *cpu_buffer,
Steven Rostedt69d1b832010-10-07 18:18:05 -04002375 unsigned long length, u64 ts,
2376 u64 delta, int add_timestamp)
Steven Rostedt6634ff22009-05-06 15:30:07 -04002377{
Steven Rostedt5a50e332009-11-17 08:43:01 -05002378 struct buffer_page *tail_page;
Steven Rostedt6634ff22009-05-06 15:30:07 -04002379 struct ring_buffer_event *event;
2380 unsigned long tail, write;
2381
Steven Rostedt69d1b832010-10-07 18:18:05 -04002382 /*
2383 * If the time delta since the last event is too big to
2384 * hold in the time field of the event, then we append a
2385 * TIME EXTEND event ahead of the data event.
2386 */
2387 if (unlikely(add_timestamp))
2388 length += RB_LEN_TIME_EXTEND;
2389
Steven Rostedt6634ff22009-05-06 15:30:07 -04002390 tail_page = cpu_buffer->tail_page;
2391 write = local_add_return(length, &tail_page->write);
Steven Rostedt77ae3652009-03-27 11:00:29 -04002392
2393 /* set write to only the index of the write */
2394 write &= RB_WRITE_MASK;
Steven Rostedt6634ff22009-05-06 15:30:07 -04002395 tail = write - length;
2396
Steven Rostedt (Red Hat)d651aa12014-02-11 13:38:54 -05002397 /*
2398 * If this is the first commit on the page, then it has the same
2399 * timestamp as the page itself.
2400 */
2401 if (!tail)
2402 delta = 0;
2403
Steven Rostedt6634ff22009-05-06 15:30:07 -04002404 /* See if we shot pass the end of this buffer page */
Steven Rostedt747e94a2010-10-08 13:51:48 -04002405 if (unlikely(write > BUF_PAGE_SIZE))
Steven Rostedt6634ff22009-05-06 15:30:07 -04002406 return rb_move_tail(cpu_buffer, length, tail,
Steven Rostedt5a50e332009-11-17 08:43:01 -05002407 tail_page, ts);
Steven Rostedt6634ff22009-05-06 15:30:07 -04002408
2409 /* We reserved something on the buffer */
2410
Steven Rostedt6634ff22009-05-06 15:30:07 -04002411 event = __rb_page_index(tail_page, tail);
Vegard Nossum1744a212009-02-28 08:29:44 +01002412 kmemcheck_annotate_bitfield(event, bitfield);
Steven Rostedt69d1b832010-10-07 18:18:05 -04002413 rb_update_event(cpu_buffer, event, length, add_timestamp, delta);
Steven Rostedt6634ff22009-05-06 15:30:07 -04002414
Steven Rostedt69d1b832010-10-07 18:18:05 -04002415 local_inc(&tail_page->entries);
Steven Rostedt6634ff22009-05-06 15:30:07 -04002416
2417 /*
Steven Rostedtfa743952009-06-16 12:37:57 -04002418 * If this is the first commit on the page, then update
2419 * its timestamp.
Steven Rostedt6634ff22009-05-06 15:30:07 -04002420 */
Steven Rostedtfa743952009-06-16 12:37:57 -04002421 if (!tail)
Steven Rostedte8bc43e2010-10-20 10:58:02 -04002422 tail_page->page->time_stamp = ts;
Steven Rostedt6634ff22009-05-06 15:30:07 -04002423
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -07002424 /* account for these added bytes */
2425 local_add(length, &cpu_buffer->entries_bytes);
2426
Steven Rostedt6634ff22009-05-06 15:30:07 -04002427 return event;
2428}
2429
Steven Rostedtedd813bf2009-06-02 23:00:53 -04002430static inline int
2431rb_try_to_discard(struct ring_buffer_per_cpu *cpu_buffer,
2432 struct ring_buffer_event *event)
2433{
2434 unsigned long new_index, old_index;
2435 struct buffer_page *bpage;
2436 unsigned long index;
2437 unsigned long addr;
2438
2439 new_index = rb_event_index(event);
Steven Rostedt69d1b832010-10-07 18:18:05 -04002440 old_index = new_index + rb_event_ts_length(event);
Steven Rostedtedd813bf2009-06-02 23:00:53 -04002441 addr = (unsigned long)event;
2442 addr &= PAGE_MASK;
2443
2444 bpage = cpu_buffer->tail_page;
2445
2446 if (bpage->page == (void *)addr && rb_page_write(bpage) == old_index) {
Steven Rostedt77ae3652009-03-27 11:00:29 -04002447 unsigned long write_mask =
2448 local_read(&bpage->write) & ~RB_WRITE_MASK;
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -07002449 unsigned long event_length = rb_event_length(event);
Steven Rostedtedd813bf2009-06-02 23:00:53 -04002450 /*
2451 * This is on the tail page. It is possible that
2452 * a write could come in and move the tail page
2453 * and write to the next page. That is fine
2454 * because we just shorten what is on this page.
2455 */
Steven Rostedt77ae3652009-03-27 11:00:29 -04002456 old_index += write_mask;
2457 new_index += write_mask;
Steven Rostedtedd813bf2009-06-02 23:00:53 -04002458 index = local_cmpxchg(&bpage->write, old_index, new_index);
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -07002459 if (index == old_index) {
2460 /* update counters */
2461 local_sub(event_length, &cpu_buffer->entries_bytes);
Steven Rostedtedd813bf2009-06-02 23:00:53 -04002462 return 1;
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -07002463 }
Steven Rostedtedd813bf2009-06-02 23:00:53 -04002464 }
2465
2466 /* could not discard */
2467 return 0;
2468}
2469
Steven Rostedtfa743952009-06-16 12:37:57 -04002470static void rb_start_commit(struct ring_buffer_per_cpu *cpu_buffer)
2471{
2472 local_inc(&cpu_buffer->committing);
2473 local_inc(&cpu_buffer->commits);
2474}
2475
Steven Rostedtd9abde22010-10-19 13:17:08 -04002476static inline void rb_end_commit(struct ring_buffer_per_cpu *cpu_buffer)
Steven Rostedtfa743952009-06-16 12:37:57 -04002477{
2478 unsigned long commits;
2479
2480 if (RB_WARN_ON(cpu_buffer,
2481 !local_read(&cpu_buffer->committing)))
2482 return;
2483
2484 again:
2485 commits = local_read(&cpu_buffer->commits);
2486 /* synchronize with interrupts */
2487 barrier();
2488 if (local_read(&cpu_buffer->committing) == 1)
2489 rb_set_commit_to_write(cpu_buffer);
2490
2491 local_dec(&cpu_buffer->committing);
2492
2493 /* synchronize with interrupts */
2494 barrier();
2495
2496 /*
2497 * Need to account for interrupts coming in between the
2498 * updating of the commit page and the clearing of the
2499 * committing counter.
2500 */
2501 if (unlikely(local_read(&cpu_buffer->commits) != commits) &&
2502 !local_read(&cpu_buffer->committing)) {
2503 local_inc(&cpu_buffer->committing);
2504 goto again;
2505 }
2506}
2507
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002508static struct ring_buffer_event *
Steven Rostedt62f0b3e2009-09-04 14:11:34 -04002509rb_reserve_next_event(struct ring_buffer *buffer,
2510 struct ring_buffer_per_cpu *cpu_buffer,
Steven Rostedt1cd8d732009-05-11 14:08:09 -04002511 unsigned long length)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002512{
2513 struct ring_buffer_event *event;
Steven Rostedt69d1b832010-10-07 18:18:05 -04002514 u64 ts, delta;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002515 int nr_loops = 0;
Steven Rostedt69d1b832010-10-07 18:18:05 -04002516 int add_timestamp;
Steven Rostedt140ff892010-10-08 10:50:30 -04002517 u64 diff;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002518
Steven Rostedtfa743952009-06-16 12:37:57 -04002519 rb_start_commit(cpu_buffer);
2520
Steven Rostedt85bac322009-09-04 14:24:40 -04002521#ifdef CONFIG_RING_BUFFER_ALLOW_SWAP
Steven Rostedt62f0b3e2009-09-04 14:11:34 -04002522 /*
2523 * Due to the ability to swap a cpu buffer from a buffer
2524 * it is possible it was swapped before we committed.
2525 * (committing stops a swap). We check for it here and
2526 * if it happened, we have to fail the write.
2527 */
2528 barrier();
2529 if (unlikely(ACCESS_ONCE(cpu_buffer->buffer) != buffer)) {
2530 local_dec(&cpu_buffer->committing);
2531 local_dec(&cpu_buffer->commits);
2532 return NULL;
2533 }
Steven Rostedt85bac322009-09-04 14:24:40 -04002534#endif
Steven Rostedt62f0b3e2009-09-04 14:11:34 -04002535
Steven Rostedtbe957c42009-05-11 14:42:53 -04002536 length = rb_calculate_event_length(length);
Steven Rostedtbf41a152008-10-04 02:00:59 -04002537 again:
Steven Rostedt69d1b832010-10-07 18:18:05 -04002538 add_timestamp = 0;
2539 delta = 0;
2540
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002541 /*
2542 * We allow for interrupts to reenter here and do a trace.
2543 * If one does, it will cause this original code to loop
2544 * back here. Even with heavy interrupts happening, this
2545 * should only happen a few times in a row. If this happens
2546 * 1000 times in a row, there must be either an interrupt
2547 * storm or we have something buggy.
2548 * Bail!
2549 */
Steven Rostedt3e89c7bb2008-11-11 15:28:41 -05002550 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 1000))
Steven Rostedtfa743952009-06-16 12:37:57 -04002551 goto out_fail;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002552
Jiri Olsa6d3f1e12009-10-23 19:36:19 -04002553 ts = rb_time_stamp(cpu_buffer->buffer);
Steven Rostedt140ff892010-10-08 10:50:30 -04002554 diff = ts - cpu_buffer->write_stamp;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002555
Steven Rostedt140ff892010-10-08 10:50:30 -04002556 /* make sure this diff is calculated here */
2557 barrier();
Steven Rostedtbf41a152008-10-04 02:00:59 -04002558
Steven Rostedt140ff892010-10-08 10:50:30 -04002559 /* Did the write stamp get updated already? */
2560 if (likely(ts >= cpu_buffer->write_stamp)) {
Steven Rostedt168b6b12009-05-11 22:11:05 -04002561 delta = diff;
2562 if (unlikely(test_time_stamp(delta))) {
Jiri Olsa31274d72011-02-18 15:52:19 +01002563 int local_clock_stable = 1;
2564#ifdef CONFIG_HAVE_UNSTABLE_SCHED_CLOCK
Peter Zijlstra35af99e2013-11-28 19:38:42 +01002565 local_clock_stable = sched_clock_stable();
Jiri Olsa31274d72011-02-18 15:52:19 +01002566#endif
Steven Rostedt69d1b832010-10-07 18:18:05 -04002567 WARN_ONCE(delta > (1ULL << 59),
Jiri Olsa31274d72011-02-18 15:52:19 +01002568 KERN_WARNING "Delta way too big! %llu ts=%llu write stamp = %llu\n%s",
Steven Rostedt69d1b832010-10-07 18:18:05 -04002569 (unsigned long long)delta,
2570 (unsigned long long)ts,
Jiri Olsa31274d72011-02-18 15:52:19 +01002571 (unsigned long long)cpu_buffer->write_stamp,
2572 local_clock_stable ? "" :
2573 "If you just came from a suspend/resume,\n"
2574 "please switch to the trace global clock:\n"
2575 " echo global > /sys/kernel/debug/tracing/trace_clock\n");
Steven Rostedt69d1b832010-10-07 18:18:05 -04002576 add_timestamp = 1;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002577 }
Steven Rostedt168b6b12009-05-11 22:11:05 -04002578 }
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002579
Steven Rostedt69d1b832010-10-07 18:18:05 -04002580 event = __rb_reserve_next(cpu_buffer, length, ts,
2581 delta, add_timestamp);
Steven Rostedt168b6b12009-05-11 22:11:05 -04002582 if (unlikely(PTR_ERR(event) == -EAGAIN))
Steven Rostedtbf41a152008-10-04 02:00:59 -04002583 goto again;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002584
Steven Rostedtfa743952009-06-16 12:37:57 -04002585 if (!event)
2586 goto out_fail;
Steven Rostedtbf41a152008-10-04 02:00:59 -04002587
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002588 return event;
Steven Rostedtfa743952009-06-16 12:37:57 -04002589
2590 out_fail:
2591 rb_end_commit(cpu_buffer);
2592 return NULL;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002593}
2594
Paul Mundt1155de42009-06-25 14:30:12 +09002595#ifdef CONFIG_TRACING
2596
Steven Rostedt567cd4d2012-11-02 18:33:05 -04002597/*
2598 * The lock and unlock are done within a preempt disable section.
2599 * The current_context per_cpu variable can only be modified
2600 * by the current task between lock and unlock. But it can
2601 * be modified more than once via an interrupt. To pass this
2602 * information from the lock to the unlock without having to
2603 * access the 'in_interrupt()' functions again (which do show
2604 * a bit of overhead in something as critical as function tracing,
2605 * we use a bitmask trick.
2606 *
2607 * bit 0 = NMI context
2608 * bit 1 = IRQ context
2609 * bit 2 = SoftIRQ context
2610 * bit 3 = normal context.
2611 *
2612 * This works because this is the order of contexts that can
2613 * preempt other contexts. A SoftIRQ never preempts an IRQ
2614 * context.
2615 *
2616 * When the context is determined, the corresponding bit is
2617 * checked and set (if it was set, then a recursion of that context
2618 * happened).
2619 *
2620 * On unlock, we need to clear this bit. To do so, just subtract
2621 * 1 from the current_context and AND it to itself.
2622 *
2623 * (binary)
2624 * 101 - 1 = 100
2625 * 101 & 100 = 100 (clearing bit zero)
2626 *
2627 * 1010 - 1 = 1001
2628 * 1010 & 1001 = 1000 (clearing bit 1)
2629 *
2630 * The least significant bit can be cleared this way, and it
2631 * just so happens that it is the same bit corresponding to
2632 * the current context.
2633 */
2634static DEFINE_PER_CPU(unsigned int, current_context);
Steven Rostedt261842b2009-04-16 21:41:52 -04002635
Steven Rostedt567cd4d2012-11-02 18:33:05 -04002636static __always_inline int trace_recursive_lock(void)
Steven Rostedt261842b2009-04-16 21:41:52 -04002637{
Steven Rostedt567cd4d2012-11-02 18:33:05 -04002638 unsigned int val = this_cpu_read(current_context);
2639 int bit;
Frederic Weisbeckere057a5e2009-04-19 23:38:12 +02002640
Steven Rostedt567cd4d2012-11-02 18:33:05 -04002641 if (in_interrupt()) {
2642 if (in_nmi())
2643 bit = 0;
2644 else if (in_irq())
2645 bit = 1;
2646 else
2647 bit = 2;
2648 } else
2649 bit = 3;
Frederic Weisbeckere057a5e2009-04-19 23:38:12 +02002650
Steven Rostedt567cd4d2012-11-02 18:33:05 -04002651 if (unlikely(val & (1 << bit)))
2652 return 1;
2653
2654 val |= (1 << bit);
2655 this_cpu_write(current_context, val);
2656
2657 return 0;
Steven Rostedtd9abde22010-10-19 13:17:08 -04002658}
2659
Steven Rostedt567cd4d2012-11-02 18:33:05 -04002660static __always_inline void trace_recursive_unlock(void)
Steven Rostedtd9abde22010-10-19 13:17:08 -04002661{
Steven Rostedt567cd4d2012-11-02 18:33:05 -04002662 unsigned int val = this_cpu_read(current_context);
Steven Rostedtd9abde22010-10-19 13:17:08 -04002663
Steven Rostedt567cd4d2012-11-02 18:33:05 -04002664 val--;
2665 val &= this_cpu_read(current_context);
2666 this_cpu_write(current_context, val);
Steven Rostedt261842b2009-04-16 21:41:52 -04002667}
2668
Paul Mundt1155de42009-06-25 14:30:12 +09002669#else
2670
2671#define trace_recursive_lock() (0)
2672#define trace_recursive_unlock() do { } while (0)
2673
2674#endif
2675
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002676/**
2677 * ring_buffer_lock_reserve - reserve a part of the buffer
2678 * @buffer: the ring buffer to reserve from
2679 * @length: the length of the data to reserve (excluding event header)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002680 *
2681 * Returns a reseverd event on the ring buffer to copy directly to.
2682 * The user of this interface will need to get the body to write into
2683 * and can use the ring_buffer_event_data() interface.
2684 *
2685 * The length is the length of the data needed, not the event length
2686 * which also includes the event header.
2687 *
2688 * Must be paired with ring_buffer_unlock_commit, unless NULL is returned.
2689 * If NULL is returned, then nothing has been allocated or locked.
2690 */
2691struct ring_buffer_event *
Arnaldo Carvalho de Melo0a987752009-02-05 16:12:56 -02002692ring_buffer_lock_reserve(struct ring_buffer *buffer, unsigned long length)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002693{
2694 struct ring_buffer_per_cpu *cpu_buffer;
2695 struct ring_buffer_event *event;
Steven Rostedt5168ae52010-06-03 09:36:50 -04002696 int cpu;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002697
Steven Rostedt033601a2008-11-21 12:41:55 -05002698 if (ring_buffer_flags != RB_BUFFERS_ON)
Steven Rostedta3583242008-11-11 15:01:42 -05002699 return NULL;
2700
Steven Rostedtbf41a152008-10-04 02:00:59 -04002701 /* If we are tracing schedule, we don't want to recurse */
Steven Rostedt5168ae52010-06-03 09:36:50 -04002702 preempt_disable_notrace();
Steven Rostedtbf41a152008-10-04 02:00:59 -04002703
Lai Jiangshan52fbe9c2010-03-08 14:50:43 +08002704 if (atomic_read(&buffer->record_disabled))
2705 goto out_nocheck;
2706
Steven Rostedt261842b2009-04-16 21:41:52 -04002707 if (trace_recursive_lock())
2708 goto out_nocheck;
2709
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002710 cpu = raw_smp_processor_id();
2711
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302712 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedtd7690412008-10-01 00:29:53 -04002713 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002714
2715 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002716
2717 if (atomic_read(&cpu_buffer->record_disabled))
Steven Rostedtd7690412008-10-01 00:29:53 -04002718 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002719
Steven Rostedtbe957c42009-05-11 14:42:53 -04002720 if (length > BUF_MAX_DATA_SIZE)
Steven Rostedtbf41a152008-10-04 02:00:59 -04002721 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002722
Steven Rostedt62f0b3e2009-09-04 14:11:34 -04002723 event = rb_reserve_next_event(buffer, cpu_buffer, length);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002724 if (!event)
Steven Rostedtd7690412008-10-01 00:29:53 -04002725 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002726
2727 return event;
2728
Steven Rostedtd7690412008-10-01 00:29:53 -04002729 out:
Steven Rostedt261842b2009-04-16 21:41:52 -04002730 trace_recursive_unlock();
2731
2732 out_nocheck:
Steven Rostedt5168ae52010-06-03 09:36:50 -04002733 preempt_enable_notrace();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002734 return NULL;
2735}
Robert Richterc4f50182008-12-11 16:49:22 +01002736EXPORT_SYMBOL_GPL(ring_buffer_lock_reserve);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002737
Steven Rostedta1863c22009-09-03 10:23:58 -04002738static void
2739rb_update_write_stamp(struct ring_buffer_per_cpu *cpu_buffer,
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002740 struct ring_buffer_event *event)
2741{
Steven Rostedt69d1b832010-10-07 18:18:05 -04002742 u64 delta;
2743
Steven Rostedtfa743952009-06-16 12:37:57 -04002744 /*
2745 * The event first in the commit queue updates the
2746 * time stamp.
2747 */
Steven Rostedt69d1b832010-10-07 18:18:05 -04002748 if (rb_event_is_commit(cpu_buffer, event)) {
2749 /*
2750 * A commit event that is first on a page
2751 * updates the write timestamp with the page stamp
2752 */
2753 if (!rb_event_index(event))
2754 cpu_buffer->write_stamp =
2755 cpu_buffer->commit_page->page->time_stamp;
2756 else if (event->type_len == RINGBUF_TYPE_TIME_EXTEND) {
2757 delta = event->array[0];
2758 delta <<= TS_SHIFT;
2759 delta += event->time_delta;
2760 cpu_buffer->write_stamp += delta;
2761 } else
2762 cpu_buffer->write_stamp += event->time_delta;
2763 }
Steven Rostedta1863c22009-09-03 10:23:58 -04002764}
Steven Rostedtbf41a152008-10-04 02:00:59 -04002765
Steven Rostedta1863c22009-09-03 10:23:58 -04002766static void rb_commit(struct ring_buffer_per_cpu *cpu_buffer,
2767 struct ring_buffer_event *event)
2768{
2769 local_inc(&cpu_buffer->entries);
2770 rb_update_write_stamp(cpu_buffer, event);
Steven Rostedtfa743952009-06-16 12:37:57 -04002771 rb_end_commit(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002772}
2773
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -05002774static __always_inline void
2775rb_wakeups(struct ring_buffer *buffer, struct ring_buffer_per_cpu *cpu_buffer)
2776{
2777 if (buffer->irq_work.waiters_pending) {
2778 buffer->irq_work.waiters_pending = false;
2779 /* irq_work_queue() supplies it's own memory barriers */
2780 irq_work_queue(&buffer->irq_work.work);
2781 }
2782
2783 if (cpu_buffer->irq_work.waiters_pending) {
2784 cpu_buffer->irq_work.waiters_pending = false;
2785 /* irq_work_queue() supplies it's own memory barriers */
2786 irq_work_queue(&cpu_buffer->irq_work.work);
2787 }
2788}
2789
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002790/**
2791 * ring_buffer_unlock_commit - commit a reserved
2792 * @buffer: The buffer to commit to
2793 * @event: The event pointer to commit.
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002794 *
2795 * This commits the data to the ring buffer, and releases any locks held.
2796 *
2797 * Must be paired with ring_buffer_lock_reserve.
2798 */
2799int ring_buffer_unlock_commit(struct ring_buffer *buffer,
Arnaldo Carvalho de Melo0a987752009-02-05 16:12:56 -02002800 struct ring_buffer_event *event)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002801{
2802 struct ring_buffer_per_cpu *cpu_buffer;
2803 int cpu = raw_smp_processor_id();
2804
2805 cpu_buffer = buffer->buffers[cpu];
2806
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002807 rb_commit(cpu_buffer, event);
2808
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -05002809 rb_wakeups(buffer, cpu_buffer);
2810
Steven Rostedt261842b2009-04-16 21:41:52 -04002811 trace_recursive_unlock();
2812
Steven Rostedt5168ae52010-06-03 09:36:50 -04002813 preempt_enable_notrace();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002814
2815 return 0;
2816}
Robert Richterc4f50182008-12-11 16:49:22 +01002817EXPORT_SYMBOL_GPL(ring_buffer_unlock_commit);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002818
Frederic Weisbeckerf3b9aae2009-04-19 23:39:33 +02002819static inline void rb_event_discard(struct ring_buffer_event *event)
2820{
Steven Rostedt69d1b832010-10-07 18:18:05 -04002821 if (event->type_len == RINGBUF_TYPE_TIME_EXTEND)
2822 event = skip_time_extend(event);
2823
Lai Jiangshan334d4162009-04-24 11:27:05 +08002824 /* array[0] holds the actual length for the discarded event */
2825 event->array[0] = rb_event_data_length(event) - RB_EVNT_HDR_SIZE;
2826 event->type_len = RINGBUF_TYPE_PADDING;
Frederic Weisbeckerf3b9aae2009-04-19 23:39:33 +02002827 /* time delta must be non zero */
2828 if (!event->time_delta)
2829 event->time_delta = 1;
2830}
2831
Steven Rostedta1863c22009-09-03 10:23:58 -04002832/*
2833 * Decrement the entries to the page that an event is on.
2834 * The event does not even need to exist, only the pointer
2835 * to the page it is on. This may only be called before the commit
2836 * takes place.
2837 */
2838static inline void
2839rb_decrement_entry(struct ring_buffer_per_cpu *cpu_buffer,
2840 struct ring_buffer_event *event)
2841{
2842 unsigned long addr = (unsigned long)event;
2843 struct buffer_page *bpage = cpu_buffer->commit_page;
2844 struct buffer_page *start;
2845
2846 addr &= PAGE_MASK;
2847
2848 /* Do the likely case first */
2849 if (likely(bpage->page == (void *)addr)) {
2850 local_dec(&bpage->entries);
2851 return;
2852 }
2853
2854 /*
2855 * Because the commit page may be on the reader page we
2856 * start with the next page and check the end loop there.
2857 */
2858 rb_inc_page(cpu_buffer, &bpage);
2859 start = bpage;
2860 do {
2861 if (bpage->page == (void *)addr) {
2862 local_dec(&bpage->entries);
2863 return;
2864 }
2865 rb_inc_page(cpu_buffer, &bpage);
2866 } while (bpage != start);
2867
2868 /* commit not part of this buffer?? */
2869 RB_WARN_ON(cpu_buffer, 1);
2870}
2871
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002872/**
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002873 * ring_buffer_commit_discard - discard an event that has not been committed
2874 * @buffer: the ring buffer
2875 * @event: non committed event to discard
2876 *
Steven Rostedtdc892f72009-09-03 15:33:41 -04002877 * Sometimes an event that is in the ring buffer needs to be ignored.
2878 * This function lets the user discard an event in the ring buffer
2879 * and then that event will not be read later.
2880 *
2881 * This function only works if it is called before the the item has been
2882 * committed. It will try to free the event from the ring buffer
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002883 * if another event has not been added behind it.
2884 *
2885 * If another event has been added behind it, it will set the event
2886 * up as discarded, and perform the commit.
2887 *
2888 * If this function is called, do not call ring_buffer_unlock_commit on
2889 * the event.
2890 */
2891void ring_buffer_discard_commit(struct ring_buffer *buffer,
2892 struct ring_buffer_event *event)
2893{
2894 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002895 int cpu;
2896
2897 /* The event is discarded regardless */
Frederic Weisbeckerf3b9aae2009-04-19 23:39:33 +02002898 rb_event_discard(event);
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002899
Steven Rostedtfa743952009-06-16 12:37:57 -04002900 cpu = smp_processor_id();
2901 cpu_buffer = buffer->buffers[cpu];
2902
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002903 /*
2904 * This must only be called if the event has not been
2905 * committed yet. Thus we can assume that preemption
2906 * is still disabled.
2907 */
Steven Rostedtfa743952009-06-16 12:37:57 -04002908 RB_WARN_ON(buffer, !local_read(&cpu_buffer->committing));
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002909
Steven Rostedta1863c22009-09-03 10:23:58 -04002910 rb_decrement_entry(cpu_buffer, event);
Steven Rostedt0f2541d2009-08-05 12:02:48 -04002911 if (rb_try_to_discard(cpu_buffer, event))
Steven Rostedtedd813bf2009-06-02 23:00:53 -04002912 goto out;
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002913
2914 /*
2915 * The commit is still visible by the reader, so we
Steven Rostedta1863c22009-09-03 10:23:58 -04002916 * must still update the timestamp.
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002917 */
Steven Rostedta1863c22009-09-03 10:23:58 -04002918 rb_update_write_stamp(cpu_buffer, event);
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002919 out:
Steven Rostedtfa743952009-06-16 12:37:57 -04002920 rb_end_commit(cpu_buffer);
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002921
Frederic Weisbeckerf3b9aae2009-04-19 23:39:33 +02002922 trace_recursive_unlock();
2923
Steven Rostedt5168ae52010-06-03 09:36:50 -04002924 preempt_enable_notrace();
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002925
2926}
2927EXPORT_SYMBOL_GPL(ring_buffer_discard_commit);
2928
2929/**
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002930 * ring_buffer_write - write data to the buffer without reserving
2931 * @buffer: The ring buffer to write to.
2932 * @length: The length of the data being written (excluding the event header)
2933 * @data: The data to write to the buffer.
2934 *
2935 * This is like ring_buffer_lock_reserve and ring_buffer_unlock_commit as
2936 * one function. If you already have the data to write to the buffer, it
2937 * may be easier to simply call this function.
2938 *
2939 * Note, like ring_buffer_lock_reserve, the length is the length of the data
2940 * and not the length of the event which would hold the header.
2941 */
2942int ring_buffer_write(struct ring_buffer *buffer,
David Sharp01e3e712012-06-07 16:46:24 -07002943 unsigned long length,
2944 void *data)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002945{
2946 struct ring_buffer_per_cpu *cpu_buffer;
2947 struct ring_buffer_event *event;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002948 void *body;
2949 int ret = -EBUSY;
Steven Rostedt5168ae52010-06-03 09:36:50 -04002950 int cpu;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002951
Steven Rostedt033601a2008-11-21 12:41:55 -05002952 if (ring_buffer_flags != RB_BUFFERS_ON)
Steven Rostedta3583242008-11-11 15:01:42 -05002953 return -EBUSY;
2954
Steven Rostedt5168ae52010-06-03 09:36:50 -04002955 preempt_disable_notrace();
Steven Rostedtbf41a152008-10-04 02:00:59 -04002956
Lai Jiangshan52fbe9c2010-03-08 14:50:43 +08002957 if (atomic_read(&buffer->record_disabled))
2958 goto out;
2959
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002960 cpu = raw_smp_processor_id();
2961
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302962 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedtd7690412008-10-01 00:29:53 -04002963 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002964
2965 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002966
2967 if (atomic_read(&cpu_buffer->record_disabled))
2968 goto out;
2969
Steven Rostedtbe957c42009-05-11 14:42:53 -04002970 if (length > BUF_MAX_DATA_SIZE)
2971 goto out;
2972
Steven Rostedt62f0b3e2009-09-04 14:11:34 -04002973 event = rb_reserve_next_event(buffer, cpu_buffer, length);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002974 if (!event)
2975 goto out;
2976
2977 body = rb_event_data(event);
2978
2979 memcpy(body, data, length);
2980
2981 rb_commit(cpu_buffer, event);
2982
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -05002983 rb_wakeups(buffer, cpu_buffer);
2984
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002985 ret = 0;
2986 out:
Steven Rostedt5168ae52010-06-03 09:36:50 -04002987 preempt_enable_notrace();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002988
2989 return ret;
2990}
Robert Richterc4f50182008-12-11 16:49:22 +01002991EXPORT_SYMBOL_GPL(ring_buffer_write);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002992
Andrew Morton34a148b2009-01-09 12:27:09 -08002993static int rb_per_cpu_empty(struct ring_buffer_per_cpu *cpu_buffer)
Steven Rostedtbf41a152008-10-04 02:00:59 -04002994{
2995 struct buffer_page *reader = cpu_buffer->reader_page;
Steven Rostedt77ae3652009-03-27 11:00:29 -04002996 struct buffer_page *head = rb_set_head_page(cpu_buffer);
Steven Rostedtbf41a152008-10-04 02:00:59 -04002997 struct buffer_page *commit = cpu_buffer->commit_page;
2998
Steven Rostedt77ae3652009-03-27 11:00:29 -04002999 /* In case of error, head will be NULL */
3000 if (unlikely(!head))
3001 return 1;
3002
Steven Rostedtbf41a152008-10-04 02:00:59 -04003003 return reader->read == rb_page_commit(reader) &&
3004 (commit == reader ||
3005 (commit == head &&
3006 head->read == rb_page_commit(commit)));
3007}
3008
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003009/**
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003010 * ring_buffer_record_disable - stop all writes into the buffer
3011 * @buffer: The ring buffer to stop writes to.
3012 *
3013 * This prevents all writes to the buffer. Any attempt to write
3014 * to the buffer after this will fail and return NULL.
3015 *
3016 * The caller should call synchronize_sched() after this.
3017 */
3018void ring_buffer_record_disable(struct ring_buffer *buffer)
3019{
3020 atomic_inc(&buffer->record_disabled);
3021}
Robert Richterc4f50182008-12-11 16:49:22 +01003022EXPORT_SYMBOL_GPL(ring_buffer_record_disable);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003023
3024/**
3025 * ring_buffer_record_enable - enable writes to the buffer
3026 * @buffer: The ring buffer to enable writes
3027 *
3028 * Note, multiple disables will need the same number of enables
Adam Buchbinderc41b20e2009-12-11 16:35:39 -05003029 * to truly enable the writing (much like preempt_disable).
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003030 */
3031void ring_buffer_record_enable(struct ring_buffer *buffer)
3032{
3033 atomic_dec(&buffer->record_disabled);
3034}
Robert Richterc4f50182008-12-11 16:49:22 +01003035EXPORT_SYMBOL_GPL(ring_buffer_record_enable);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003036
3037/**
Steven Rostedt499e5472012-02-22 15:50:28 -05003038 * ring_buffer_record_off - stop all writes into the buffer
3039 * @buffer: The ring buffer to stop writes to.
3040 *
3041 * This prevents all writes to the buffer. Any attempt to write
3042 * to the buffer after this will fail and return NULL.
3043 *
3044 * This is different than ring_buffer_record_disable() as
Wang Tianhong87abb3b2012-08-02 14:02:00 +08003045 * it works like an on/off switch, where as the disable() version
Steven Rostedt499e5472012-02-22 15:50:28 -05003046 * must be paired with a enable().
3047 */
3048void ring_buffer_record_off(struct ring_buffer *buffer)
3049{
3050 unsigned int rd;
3051 unsigned int new_rd;
3052
3053 do {
3054 rd = atomic_read(&buffer->record_disabled);
3055 new_rd = rd | RB_BUFFER_OFF;
3056 } while (atomic_cmpxchg(&buffer->record_disabled, rd, new_rd) != rd);
3057}
3058EXPORT_SYMBOL_GPL(ring_buffer_record_off);
3059
3060/**
3061 * ring_buffer_record_on - restart writes into the buffer
3062 * @buffer: The ring buffer to start writes to.
3063 *
3064 * This enables all writes to the buffer that was disabled by
3065 * ring_buffer_record_off().
3066 *
3067 * This is different than ring_buffer_record_enable() as
Wang Tianhong87abb3b2012-08-02 14:02:00 +08003068 * it works like an on/off switch, where as the enable() version
Steven Rostedt499e5472012-02-22 15:50:28 -05003069 * must be paired with a disable().
3070 */
3071void ring_buffer_record_on(struct ring_buffer *buffer)
3072{
3073 unsigned int rd;
3074 unsigned int new_rd;
3075
3076 do {
3077 rd = atomic_read(&buffer->record_disabled);
3078 new_rd = rd & ~RB_BUFFER_OFF;
3079 } while (atomic_cmpxchg(&buffer->record_disabled, rd, new_rd) != rd);
3080}
3081EXPORT_SYMBOL_GPL(ring_buffer_record_on);
3082
3083/**
3084 * ring_buffer_record_is_on - return true if the ring buffer can write
3085 * @buffer: The ring buffer to see if write is enabled
3086 *
3087 * Returns true if the ring buffer is in a state that it accepts writes.
3088 */
3089int ring_buffer_record_is_on(struct ring_buffer *buffer)
3090{
3091 return !atomic_read(&buffer->record_disabled);
3092}
3093
3094/**
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003095 * ring_buffer_record_disable_cpu - stop all writes into the cpu_buffer
3096 * @buffer: The ring buffer to stop writes to.
3097 * @cpu: The CPU buffer to stop
3098 *
3099 * This prevents all writes to the buffer. Any attempt to write
3100 * to the buffer after this will fail and return NULL.
3101 *
3102 * The caller should call synchronize_sched() after this.
3103 */
3104void ring_buffer_record_disable_cpu(struct ring_buffer *buffer, int cpu)
3105{
3106 struct ring_buffer_per_cpu *cpu_buffer;
3107
Rusty Russell9e01c1b2009-01-01 10:12:22 +10303108 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04003109 return;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003110
3111 cpu_buffer = buffer->buffers[cpu];
3112 atomic_inc(&cpu_buffer->record_disabled);
3113}
Robert Richterc4f50182008-12-11 16:49:22 +01003114EXPORT_SYMBOL_GPL(ring_buffer_record_disable_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003115
3116/**
3117 * ring_buffer_record_enable_cpu - enable writes to the buffer
3118 * @buffer: The ring buffer to enable writes
3119 * @cpu: The CPU to enable.
3120 *
3121 * Note, multiple disables will need the same number of enables
Adam Buchbinderc41b20e2009-12-11 16:35:39 -05003122 * to truly enable the writing (much like preempt_disable).
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003123 */
3124void ring_buffer_record_enable_cpu(struct ring_buffer *buffer, int cpu)
3125{
3126 struct ring_buffer_per_cpu *cpu_buffer;
3127
Rusty Russell9e01c1b2009-01-01 10:12:22 +10303128 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04003129 return;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003130
3131 cpu_buffer = buffer->buffers[cpu];
3132 atomic_dec(&cpu_buffer->record_disabled);
3133}
Robert Richterc4f50182008-12-11 16:49:22 +01003134EXPORT_SYMBOL_GPL(ring_buffer_record_enable_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003135
Steven Rostedtf6195aa2010-09-01 12:23:12 -04003136/*
3137 * The total entries in the ring buffer is the running counter
3138 * of entries entered into the ring buffer, minus the sum of
3139 * the entries read from the ring buffer and the number of
3140 * entries that were overwritten.
3141 */
3142static inline unsigned long
3143rb_num_of_entries(struct ring_buffer_per_cpu *cpu_buffer)
3144{
3145 return local_read(&cpu_buffer->entries) -
3146 (local_read(&cpu_buffer->overrun) + cpu_buffer->read);
3147}
3148
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003149/**
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -07003150 * ring_buffer_oldest_event_ts - get the oldest event timestamp from the buffer
3151 * @buffer: The ring buffer
3152 * @cpu: The per CPU buffer to read from.
3153 */
Yoshihiro YUNOMAE50ecf2c2012-10-11 16:27:54 -07003154u64 ring_buffer_oldest_event_ts(struct ring_buffer *buffer, int cpu)
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -07003155{
3156 unsigned long flags;
3157 struct ring_buffer_per_cpu *cpu_buffer;
3158 struct buffer_page *bpage;
Linus Torvaldsda830e52012-12-11 18:18:58 -08003159 u64 ret = 0;
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -07003160
3161 if (!cpumask_test_cpu(cpu, buffer->cpumask))
3162 return 0;
3163
3164 cpu_buffer = buffer->buffers[cpu];
Linus Torvalds7115e3f2011-10-26 17:03:38 +02003165 raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -07003166 /*
3167 * if the tail is on reader_page, oldest time stamp is on the reader
3168 * page
3169 */
3170 if (cpu_buffer->tail_page == cpu_buffer->reader_page)
3171 bpage = cpu_buffer->reader_page;
3172 else
3173 bpage = rb_set_head_page(cpu_buffer);
Steven Rostedt54f7be52012-11-29 22:27:22 -05003174 if (bpage)
3175 ret = bpage->page->time_stamp;
Linus Torvalds7115e3f2011-10-26 17:03:38 +02003176 raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -07003177
3178 return ret;
3179}
3180EXPORT_SYMBOL_GPL(ring_buffer_oldest_event_ts);
3181
3182/**
3183 * ring_buffer_bytes_cpu - get the number of bytes consumed in a cpu buffer
3184 * @buffer: The ring buffer
3185 * @cpu: The per CPU buffer to read from.
3186 */
3187unsigned long ring_buffer_bytes_cpu(struct ring_buffer *buffer, int cpu)
3188{
3189 struct ring_buffer_per_cpu *cpu_buffer;
3190 unsigned long ret;
3191
3192 if (!cpumask_test_cpu(cpu, buffer->cpumask))
3193 return 0;
3194
3195 cpu_buffer = buffer->buffers[cpu];
3196 ret = local_read(&cpu_buffer->entries_bytes) - cpu_buffer->read_bytes;
3197
3198 return ret;
3199}
3200EXPORT_SYMBOL_GPL(ring_buffer_bytes_cpu);
3201
3202/**
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003203 * ring_buffer_entries_cpu - get the number of entries in a cpu buffer
3204 * @buffer: The ring buffer
3205 * @cpu: The per CPU buffer to get the entries from.
3206 */
3207unsigned long ring_buffer_entries_cpu(struct ring_buffer *buffer, int cpu)
3208{
3209 struct ring_buffer_per_cpu *cpu_buffer;
3210
Rusty Russell9e01c1b2009-01-01 10:12:22 +10303211 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04003212 return 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003213
3214 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt554f7862009-03-11 22:00:13 -04003215
Steven Rostedtf6195aa2010-09-01 12:23:12 -04003216 return rb_num_of_entries(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003217}
Robert Richterc4f50182008-12-11 16:49:22 +01003218EXPORT_SYMBOL_GPL(ring_buffer_entries_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003219
3220/**
Slava Pestov884bfe82011-07-15 14:23:58 -07003221 * ring_buffer_overrun_cpu - get the number of overruns caused by the ring
3222 * buffer wrapping around (only if RB_FL_OVERWRITE is on).
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003223 * @buffer: The ring buffer
3224 * @cpu: The per CPU buffer to get the number of overruns from
3225 */
3226unsigned long ring_buffer_overrun_cpu(struct ring_buffer *buffer, int cpu)
3227{
3228 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedt8aabee52009-03-12 13:13:49 -04003229 unsigned long ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003230
Rusty Russell9e01c1b2009-01-01 10:12:22 +10303231 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04003232 return 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003233
3234 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt77ae3652009-03-27 11:00:29 -04003235 ret = local_read(&cpu_buffer->overrun);
Steven Rostedt554f7862009-03-11 22:00:13 -04003236
3237 return ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003238}
Robert Richterc4f50182008-12-11 16:49:22 +01003239EXPORT_SYMBOL_GPL(ring_buffer_overrun_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003240
3241/**
Slava Pestov884bfe82011-07-15 14:23:58 -07003242 * ring_buffer_commit_overrun_cpu - get the number of overruns caused by
3243 * commits failing due to the buffer wrapping around while there are uncommitted
3244 * events, such as during an interrupt storm.
Steven Rostedtf0d2c682009-04-29 13:43:37 -04003245 * @buffer: The ring buffer
3246 * @cpu: The per CPU buffer to get the number of overruns from
3247 */
3248unsigned long
3249ring_buffer_commit_overrun_cpu(struct ring_buffer *buffer, int cpu)
3250{
3251 struct ring_buffer_per_cpu *cpu_buffer;
3252 unsigned long ret;
3253
3254 if (!cpumask_test_cpu(cpu, buffer->cpumask))
3255 return 0;
3256
3257 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt77ae3652009-03-27 11:00:29 -04003258 ret = local_read(&cpu_buffer->commit_overrun);
Steven Rostedtf0d2c682009-04-29 13:43:37 -04003259
3260 return ret;
3261}
3262EXPORT_SYMBOL_GPL(ring_buffer_commit_overrun_cpu);
3263
3264/**
Slava Pestov884bfe82011-07-15 14:23:58 -07003265 * ring_buffer_dropped_events_cpu - get the number of dropped events caused by
3266 * the ring buffer filling up (only if RB_FL_OVERWRITE is off).
3267 * @buffer: The ring buffer
3268 * @cpu: The per CPU buffer to get the number of overruns from
3269 */
3270unsigned long
3271ring_buffer_dropped_events_cpu(struct ring_buffer *buffer, int cpu)
3272{
3273 struct ring_buffer_per_cpu *cpu_buffer;
3274 unsigned long ret;
3275
3276 if (!cpumask_test_cpu(cpu, buffer->cpumask))
3277 return 0;
3278
3279 cpu_buffer = buffer->buffers[cpu];
3280 ret = local_read(&cpu_buffer->dropped_events);
3281
3282 return ret;
3283}
3284EXPORT_SYMBOL_GPL(ring_buffer_dropped_events_cpu);
3285
3286/**
Steven Rostedt (Red Hat)ad964702013-01-29 17:45:49 -05003287 * ring_buffer_read_events_cpu - get the number of events successfully read
3288 * @buffer: The ring buffer
3289 * @cpu: The per CPU buffer to get the number of events read
3290 */
3291unsigned long
3292ring_buffer_read_events_cpu(struct ring_buffer *buffer, int cpu)
3293{
3294 struct ring_buffer_per_cpu *cpu_buffer;
3295
3296 if (!cpumask_test_cpu(cpu, buffer->cpumask))
3297 return 0;
3298
3299 cpu_buffer = buffer->buffers[cpu];
3300 return cpu_buffer->read;
3301}
3302EXPORT_SYMBOL_GPL(ring_buffer_read_events_cpu);
3303
3304/**
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003305 * ring_buffer_entries - get the number of entries in a buffer
3306 * @buffer: The ring buffer
3307 *
3308 * Returns the total number of entries in the ring buffer
3309 * (all CPU entries)
3310 */
3311unsigned long ring_buffer_entries(struct ring_buffer *buffer)
3312{
3313 struct ring_buffer_per_cpu *cpu_buffer;
3314 unsigned long entries = 0;
3315 int cpu;
3316
3317 /* if you care about this being correct, lock the buffer */
3318 for_each_buffer_cpu(buffer, cpu) {
3319 cpu_buffer = buffer->buffers[cpu];
Steven Rostedtf6195aa2010-09-01 12:23:12 -04003320 entries += rb_num_of_entries(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003321 }
3322
3323 return entries;
3324}
Robert Richterc4f50182008-12-11 16:49:22 +01003325EXPORT_SYMBOL_GPL(ring_buffer_entries);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003326
3327/**
Jiri Olsa67b394f2009-10-23 19:36:18 -04003328 * ring_buffer_overruns - get the number of overruns in buffer
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003329 * @buffer: The ring buffer
3330 *
3331 * Returns the total number of overruns in the ring buffer
3332 * (all CPU entries)
3333 */
3334unsigned long ring_buffer_overruns(struct ring_buffer *buffer)
3335{
3336 struct ring_buffer_per_cpu *cpu_buffer;
3337 unsigned long overruns = 0;
3338 int cpu;
3339
3340 /* if you care about this being correct, lock the buffer */
3341 for_each_buffer_cpu(buffer, cpu) {
3342 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt77ae3652009-03-27 11:00:29 -04003343 overruns += local_read(&cpu_buffer->overrun);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003344 }
3345
3346 return overruns;
3347}
Robert Richterc4f50182008-12-11 16:49:22 +01003348EXPORT_SYMBOL_GPL(ring_buffer_overruns);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003349
Steven Rostedt642edba2008-11-12 00:01:26 -05003350static void rb_iter_reset(struct ring_buffer_iter *iter)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003351{
3352 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
3353
Steven Rostedtd7690412008-10-01 00:29:53 -04003354 /* Iterator usage is expected to have record disabled */
Steven Rostedt (Red Hat)651e22f2014-08-06 14:11:33 -04003355 iter->head_page = cpu_buffer->reader_page;
3356 iter->head = cpu_buffer->reader_page->read;
3357
3358 iter->cache_reader_page = iter->head_page;
Steven Rostedt (Red Hat)24607f12014-10-02 16:51:18 -04003359 iter->cache_read = cpu_buffer->read;
Steven Rostedt (Red Hat)651e22f2014-08-06 14:11:33 -04003360
Steven Rostedtd7690412008-10-01 00:29:53 -04003361 if (iter->head)
3362 iter->read_stamp = cpu_buffer->read_stamp;
3363 else
Steven Rostedtabc9b562008-12-02 15:34:06 -05003364 iter->read_stamp = iter->head_page->page->time_stamp;
Steven Rostedt642edba2008-11-12 00:01:26 -05003365}
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003366
Steven Rostedt642edba2008-11-12 00:01:26 -05003367/**
3368 * ring_buffer_iter_reset - reset an iterator
3369 * @iter: The iterator to reset
3370 *
3371 * Resets the iterator, so that it will start from the beginning
3372 * again.
3373 */
3374void ring_buffer_iter_reset(struct ring_buffer_iter *iter)
3375{
Steven Rostedt554f7862009-03-11 22:00:13 -04003376 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedt642edba2008-11-12 00:01:26 -05003377 unsigned long flags;
3378
Steven Rostedt554f7862009-03-11 22:00:13 -04003379 if (!iter)
3380 return;
3381
3382 cpu_buffer = iter->cpu_buffer;
3383
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02003384 raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
Steven Rostedt642edba2008-11-12 00:01:26 -05003385 rb_iter_reset(iter);
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02003386 raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003387}
Robert Richterc4f50182008-12-11 16:49:22 +01003388EXPORT_SYMBOL_GPL(ring_buffer_iter_reset);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003389
3390/**
3391 * ring_buffer_iter_empty - check if an iterator has no more to read
3392 * @iter: The iterator to check
3393 */
3394int ring_buffer_iter_empty(struct ring_buffer_iter *iter)
3395{
3396 struct ring_buffer_per_cpu *cpu_buffer;
3397
3398 cpu_buffer = iter->cpu_buffer;
3399
Steven Rostedtbf41a152008-10-04 02:00:59 -04003400 return iter->head_page == cpu_buffer->commit_page &&
3401 iter->head == rb_commit_index(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003402}
Robert Richterc4f50182008-12-11 16:49:22 +01003403EXPORT_SYMBOL_GPL(ring_buffer_iter_empty);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003404
3405static void
3406rb_update_read_stamp(struct ring_buffer_per_cpu *cpu_buffer,
3407 struct ring_buffer_event *event)
3408{
3409 u64 delta;
3410
Lai Jiangshan334d4162009-04-24 11:27:05 +08003411 switch (event->type_len) {
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003412 case RINGBUF_TYPE_PADDING:
3413 return;
3414
3415 case RINGBUF_TYPE_TIME_EXTEND:
3416 delta = event->array[0];
3417 delta <<= TS_SHIFT;
3418 delta += event->time_delta;
3419 cpu_buffer->read_stamp += delta;
3420 return;
3421
3422 case RINGBUF_TYPE_TIME_STAMP:
3423 /* FIXME: not implemented */
3424 return;
3425
3426 case RINGBUF_TYPE_DATA:
3427 cpu_buffer->read_stamp += event->time_delta;
3428 return;
3429
3430 default:
3431 BUG();
3432 }
3433 return;
3434}
3435
3436static void
3437rb_update_iter_read_stamp(struct ring_buffer_iter *iter,
3438 struct ring_buffer_event *event)
3439{
3440 u64 delta;
3441
Lai Jiangshan334d4162009-04-24 11:27:05 +08003442 switch (event->type_len) {
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003443 case RINGBUF_TYPE_PADDING:
3444 return;
3445
3446 case RINGBUF_TYPE_TIME_EXTEND:
3447 delta = event->array[0];
3448 delta <<= TS_SHIFT;
3449 delta += event->time_delta;
3450 iter->read_stamp += delta;
3451 return;
3452
3453 case RINGBUF_TYPE_TIME_STAMP:
3454 /* FIXME: not implemented */
3455 return;
3456
3457 case RINGBUF_TYPE_DATA:
3458 iter->read_stamp += event->time_delta;
3459 return;
3460
3461 default:
3462 BUG();
3463 }
3464 return;
3465}
3466
Steven Rostedtd7690412008-10-01 00:29:53 -04003467static struct buffer_page *
3468rb_get_reader_page(struct ring_buffer_per_cpu *cpu_buffer)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003469{
Steven Rostedtd7690412008-10-01 00:29:53 -04003470 struct buffer_page *reader = NULL;
Steven Rostedt66a8cb92010-03-31 13:21:56 -04003471 unsigned long overwrite;
Steven Rostedtd7690412008-10-01 00:29:53 -04003472 unsigned long flags;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003473 int nr_loops = 0;
Steven Rostedt77ae3652009-03-27 11:00:29 -04003474 int ret;
Steven Rostedtd7690412008-10-01 00:29:53 -04003475
Steven Rostedt3e03fb72008-11-06 00:09:43 -05003476 local_irq_save(flags);
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01003477 arch_spin_lock(&cpu_buffer->lock);
Steven Rostedtd7690412008-10-01 00:29:53 -04003478
3479 again:
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003480 /*
3481 * This should normally only loop twice. But because the
3482 * start of the reader inserts an empty page, it causes
3483 * a case where we will loop three times. There should be no
3484 * reason to loop four times (that I know of).
3485 */
Steven Rostedt3e89c7bb2008-11-11 15:28:41 -05003486 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 3)) {
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003487 reader = NULL;
3488 goto out;
3489 }
3490
Steven Rostedtd7690412008-10-01 00:29:53 -04003491 reader = cpu_buffer->reader_page;
3492
3493 /* If there's more to read, return this page */
Steven Rostedtbf41a152008-10-04 02:00:59 -04003494 if (cpu_buffer->reader_page->read < rb_page_size(reader))
Steven Rostedtd7690412008-10-01 00:29:53 -04003495 goto out;
3496
3497 /* Never should we have an index greater than the size */
Steven Rostedt3e89c7bb2008-11-11 15:28:41 -05003498 if (RB_WARN_ON(cpu_buffer,
3499 cpu_buffer->reader_page->read > rb_page_size(reader)))
3500 goto out;
Steven Rostedtd7690412008-10-01 00:29:53 -04003501
3502 /* check if we caught up to the tail */
3503 reader = NULL;
Steven Rostedtbf41a152008-10-04 02:00:59 -04003504 if (cpu_buffer->commit_page == cpu_buffer->reader_page)
Steven Rostedtd7690412008-10-01 00:29:53 -04003505 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003506
Steven Rostedta5fb8332012-06-28 13:35:04 -04003507 /* Don't bother swapping if the ring buffer is empty */
3508 if (rb_num_of_entries(cpu_buffer) == 0)
3509 goto out;
3510
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003511 /*
Steven Rostedtd7690412008-10-01 00:29:53 -04003512 * Reset the reader page to size zero.
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003513 */
Steven Rostedt77ae3652009-03-27 11:00:29 -04003514 local_set(&cpu_buffer->reader_page->write, 0);
3515 local_set(&cpu_buffer->reader_page->entries, 0);
3516 local_set(&cpu_buffer->reader_page->page->commit, 0);
Steven Rostedtff0ff842010-03-31 22:11:42 -04003517 cpu_buffer->reader_page->real_end = 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003518
Steven Rostedt77ae3652009-03-27 11:00:29 -04003519 spin:
3520 /*
3521 * Splice the empty reader page into the list around the head.
3522 */
3523 reader = rb_set_head_page(cpu_buffer);
Steven Rostedt54f7be52012-11-29 22:27:22 -05003524 if (!reader)
3525 goto out;
Steven Rostedt0e1ff5d2010-01-06 20:40:44 -05003526 cpu_buffer->reader_page->list.next = rb_list_head(reader->list.next);
Steven Rostedtd7690412008-10-01 00:29:53 -04003527 cpu_buffer->reader_page->list.prev = reader->list.prev;
Steven Rostedtbf41a152008-10-04 02:00:59 -04003528
Steven Rostedt3adc54f2009-03-30 15:32:01 -04003529 /*
3530 * cpu_buffer->pages just needs to point to the buffer, it
3531 * has no specific buffer page to point to. Lets move it out
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003532 * of our way so we don't accidentally swap it.
Steven Rostedt3adc54f2009-03-30 15:32:01 -04003533 */
3534 cpu_buffer->pages = reader->list.prev;
3535
Steven Rostedt77ae3652009-03-27 11:00:29 -04003536 /* The reader page will be pointing to the new head */
3537 rb_set_list_to_head(cpu_buffer, &cpu_buffer->reader_page->list);
Steven Rostedtd7690412008-10-01 00:29:53 -04003538
3539 /*
Steven Rostedt66a8cb92010-03-31 13:21:56 -04003540 * We want to make sure we read the overruns after we set up our
3541 * pointers to the next object. The writer side does a
3542 * cmpxchg to cross pages which acts as the mb on the writer
3543 * side. Note, the reader will constantly fail the swap
3544 * while the writer is updating the pointers, so this
3545 * guarantees that the overwrite recorded here is the one we
3546 * want to compare with the last_overrun.
3547 */
3548 smp_mb();
3549 overwrite = local_read(&(cpu_buffer->overrun));
3550
3551 /*
Steven Rostedt77ae3652009-03-27 11:00:29 -04003552 * Here's the tricky part.
3553 *
3554 * We need to move the pointer past the header page.
3555 * But we can only do that if a writer is not currently
3556 * moving it. The page before the header page has the
3557 * flag bit '1' set if it is pointing to the page we want.
3558 * but if the writer is in the process of moving it
3559 * than it will be '2' or already moved '0'.
Steven Rostedtd7690412008-10-01 00:29:53 -04003560 */
Steven Rostedtd7690412008-10-01 00:29:53 -04003561
Steven Rostedt77ae3652009-03-27 11:00:29 -04003562 ret = rb_head_page_replace(reader, cpu_buffer->reader_page);
3563
3564 /*
3565 * If we did not convert it, then we must try again.
3566 */
3567 if (!ret)
3568 goto spin;
3569
3570 /*
3571 * Yeah! We succeeded in replacing the page.
3572 *
3573 * Now make the new head point back to the reader page.
3574 */
David Sharp5ded3dc62010-01-06 17:12:07 -08003575 rb_list_head(reader->list.next)->prev = &cpu_buffer->reader_page->list;
Steven Rostedt77ae3652009-03-27 11:00:29 -04003576 rb_inc_page(cpu_buffer, &cpu_buffer->head_page);
Steven Rostedtd7690412008-10-01 00:29:53 -04003577
3578 /* Finally update the reader page to the new head */
3579 cpu_buffer->reader_page = reader;
3580 rb_reset_reader_page(cpu_buffer);
3581
Steven Rostedt66a8cb92010-03-31 13:21:56 -04003582 if (overwrite != cpu_buffer->last_overrun) {
3583 cpu_buffer->lost_events = overwrite - cpu_buffer->last_overrun;
3584 cpu_buffer->last_overrun = overwrite;
3585 }
3586
Steven Rostedtd7690412008-10-01 00:29:53 -04003587 goto again;
3588
3589 out:
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01003590 arch_spin_unlock(&cpu_buffer->lock);
Steven Rostedt3e03fb72008-11-06 00:09:43 -05003591 local_irq_restore(flags);
Steven Rostedtd7690412008-10-01 00:29:53 -04003592
3593 return reader;
3594}
3595
3596static void rb_advance_reader(struct ring_buffer_per_cpu *cpu_buffer)
3597{
3598 struct ring_buffer_event *event;
3599 struct buffer_page *reader;
3600 unsigned length;
3601
3602 reader = rb_get_reader_page(cpu_buffer);
3603
3604 /* This function should not be called when buffer is empty */
Steven Rostedt3e89c7bb2008-11-11 15:28:41 -05003605 if (RB_WARN_ON(cpu_buffer, !reader))
3606 return;
Steven Rostedtd7690412008-10-01 00:29:53 -04003607
3608 event = rb_reader_event(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003609
Steven Rostedta1863c22009-09-03 10:23:58 -04003610 if (event->type_len <= RINGBUF_TYPE_DATA_TYPE_LEN_MAX)
Steven Rostedte4906ef2009-04-30 20:49:44 -04003611 cpu_buffer->read++;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003612
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003613 rb_update_read_stamp(cpu_buffer, event);
3614
Steven Rostedtd7690412008-10-01 00:29:53 -04003615 length = rb_event_length(event);
Steven Rostedt6f807ac2008-10-04 02:00:58 -04003616 cpu_buffer->reader_page->read += length;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003617}
3618
3619static void rb_advance_iter(struct ring_buffer_iter *iter)
3620{
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003621 struct ring_buffer_per_cpu *cpu_buffer;
3622 struct ring_buffer_event *event;
3623 unsigned length;
3624
3625 cpu_buffer = iter->cpu_buffer;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003626
3627 /*
3628 * Check if we are at the end of the buffer.
3629 */
Steven Rostedtbf41a152008-10-04 02:00:59 -04003630 if (iter->head >= rb_page_size(iter->head_page)) {
Steven Rostedtea05b572009-06-03 09:30:10 -04003631 /* discarded commits can make the page empty */
3632 if (iter->head_page == cpu_buffer->commit_page)
Steven Rostedt3e89c7bb2008-11-11 15:28:41 -05003633 return;
Steven Rostedtd7690412008-10-01 00:29:53 -04003634 rb_inc_iter(iter);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003635 return;
3636 }
3637
3638 event = rb_iter_head_event(iter);
3639
3640 length = rb_event_length(event);
3641
3642 /*
3643 * This should not be called to advance the header if we are
3644 * at the tail of the buffer.
3645 */
Steven Rostedt3e89c7bb2008-11-11 15:28:41 -05003646 if (RB_WARN_ON(cpu_buffer,
Steven Rostedtf536aaf2008-11-10 23:07:30 -05003647 (iter->head_page == cpu_buffer->commit_page) &&
Steven Rostedt3e89c7bb2008-11-11 15:28:41 -05003648 (iter->head + length > rb_commit_index(cpu_buffer))))
3649 return;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003650
3651 rb_update_iter_read_stamp(iter, event);
3652
3653 iter->head += length;
3654
3655 /* check for end of page padding */
Steven Rostedtbf41a152008-10-04 02:00:59 -04003656 if ((iter->head >= rb_page_size(iter->head_page)) &&
3657 (iter->head_page != cpu_buffer->commit_page))
Steven Rostedt771e0382012-11-30 10:41:57 -05003658 rb_inc_iter(iter);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003659}
3660
Steven Rostedt66a8cb92010-03-31 13:21:56 -04003661static int rb_lost_events(struct ring_buffer_per_cpu *cpu_buffer)
3662{
3663 return cpu_buffer->lost_events;
3664}
3665
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003666static struct ring_buffer_event *
Steven Rostedt66a8cb92010-03-31 13:21:56 -04003667rb_buffer_peek(struct ring_buffer_per_cpu *cpu_buffer, u64 *ts,
3668 unsigned long *lost_events)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003669{
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003670 struct ring_buffer_event *event;
Steven Rostedtd7690412008-10-01 00:29:53 -04003671 struct buffer_page *reader;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003672 int nr_loops = 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003673
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003674 again:
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003675 /*
Steven Rostedt69d1b832010-10-07 18:18:05 -04003676 * We repeat when a time extend is encountered.
3677 * Since the time extend is always attached to a data event,
3678 * we should never loop more than once.
3679 * (We never hit the following condition more than twice).
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003680 */
Steven Rostedt69d1b832010-10-07 18:18:05 -04003681 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 2))
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003682 return NULL;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003683
Steven Rostedtd7690412008-10-01 00:29:53 -04003684 reader = rb_get_reader_page(cpu_buffer);
3685 if (!reader)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003686 return NULL;
3687
Steven Rostedtd7690412008-10-01 00:29:53 -04003688 event = rb_reader_event(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003689
Lai Jiangshan334d4162009-04-24 11:27:05 +08003690 switch (event->type_len) {
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003691 case RINGBUF_TYPE_PADDING:
Tom Zanussi2d622712009-03-22 03:30:49 -05003692 if (rb_null_event(event))
3693 RB_WARN_ON(cpu_buffer, 1);
3694 /*
3695 * Because the writer could be discarding every
3696 * event it creates (which would probably be bad)
3697 * if we were to go back to "again" then we may never
3698 * catch up, and will trigger the warn on, or lock
3699 * the box. Return the padding, and we will release
3700 * the current locks, and try again.
3701 */
Tom Zanussi2d622712009-03-22 03:30:49 -05003702 return event;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003703
3704 case RINGBUF_TYPE_TIME_EXTEND:
3705 /* Internal data, OK to advance */
Steven Rostedtd7690412008-10-01 00:29:53 -04003706 rb_advance_reader(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003707 goto again;
3708
3709 case RINGBUF_TYPE_TIME_STAMP:
3710 /* FIXME: not implemented */
Steven Rostedtd7690412008-10-01 00:29:53 -04003711 rb_advance_reader(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003712 goto again;
3713
3714 case RINGBUF_TYPE_DATA:
3715 if (ts) {
3716 *ts = cpu_buffer->read_stamp + event->time_delta;
Robert Richterd8eeb2d2009-07-31 14:58:04 +02003717 ring_buffer_normalize_time_stamp(cpu_buffer->buffer,
Steven Rostedt37886f62009-03-17 17:22:06 -04003718 cpu_buffer->cpu, ts);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003719 }
Steven Rostedt66a8cb92010-03-31 13:21:56 -04003720 if (lost_events)
3721 *lost_events = rb_lost_events(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003722 return event;
3723
3724 default:
3725 BUG();
3726 }
3727
3728 return NULL;
3729}
Robert Richterc4f50182008-12-11 16:49:22 +01003730EXPORT_SYMBOL_GPL(ring_buffer_peek);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003731
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003732static struct ring_buffer_event *
3733rb_iter_peek(struct ring_buffer_iter *iter, u64 *ts)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003734{
3735 struct ring_buffer *buffer;
3736 struct ring_buffer_per_cpu *cpu_buffer;
3737 struct ring_buffer_event *event;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003738 int nr_loops = 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003739
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003740 cpu_buffer = iter->cpu_buffer;
3741 buffer = cpu_buffer->buffer;
3742
Steven Rostedt492a74f2010-01-25 15:17:47 -05003743 /*
3744 * Check if someone performed a consuming read to
3745 * the buffer. A consuming read invalidates the iterator
3746 * and we need to reset the iterator in this case.
3747 */
3748 if (unlikely(iter->cache_read != cpu_buffer->read ||
3749 iter->cache_reader_page != cpu_buffer->reader_page))
3750 rb_iter_reset(iter);
3751
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003752 again:
Steven Rostedt3c05d742010-01-26 16:14:08 -05003753 if (ring_buffer_iter_empty(iter))
3754 return NULL;
3755
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003756 /*
Steven Rostedt (Red Hat)021de3d2014-08-06 15:36:31 -04003757 * We repeat when a time extend is encountered or we hit
3758 * the end of the page. Since the time extend is always attached
3759 * to a data event, we should never loop more than three times.
3760 * Once for going to next page, once on time extend, and
3761 * finally once to get the event.
3762 * (We never hit the following condition more than thrice).
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003763 */
Steven Rostedt (Red Hat)021de3d2014-08-06 15:36:31 -04003764 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 3))
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003765 return NULL;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003766
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003767 if (rb_per_cpu_empty(cpu_buffer))
3768 return NULL;
3769
Steven Rostedt (Red Hat)10e83fd2014-07-23 19:45:12 -04003770 if (iter->head >= rb_page_size(iter->head_page)) {
Steven Rostedt3c05d742010-01-26 16:14:08 -05003771 rb_inc_iter(iter);
3772 goto again;
3773 }
3774
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003775 event = rb_iter_head_event(iter);
3776
Lai Jiangshan334d4162009-04-24 11:27:05 +08003777 switch (event->type_len) {
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003778 case RINGBUF_TYPE_PADDING:
Tom Zanussi2d622712009-03-22 03:30:49 -05003779 if (rb_null_event(event)) {
3780 rb_inc_iter(iter);
3781 goto again;
3782 }
3783 rb_advance_iter(iter);
3784 return event;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003785
3786 case RINGBUF_TYPE_TIME_EXTEND:
3787 /* Internal data, OK to advance */
3788 rb_advance_iter(iter);
3789 goto again;
3790
3791 case RINGBUF_TYPE_TIME_STAMP:
3792 /* FIXME: not implemented */
3793 rb_advance_iter(iter);
3794 goto again;
3795
3796 case RINGBUF_TYPE_DATA:
3797 if (ts) {
3798 *ts = iter->read_stamp + event->time_delta;
Steven Rostedt37886f62009-03-17 17:22:06 -04003799 ring_buffer_normalize_time_stamp(buffer,
3800 cpu_buffer->cpu, ts);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003801 }
3802 return event;
3803
3804 default:
3805 BUG();
3806 }
3807
3808 return NULL;
3809}
Robert Richterc4f50182008-12-11 16:49:22 +01003810EXPORT_SYMBOL_GPL(ring_buffer_iter_peek);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003811
Steven Rostedt8d707e82009-06-16 21:22:48 -04003812static inline int rb_ok_to_lock(void)
3813{
3814 /*
3815 * If an NMI die dumps out the content of the ring buffer
3816 * do not grab locks. We also permanently disable the ring
3817 * buffer too. A one time deal is all you get from reading
3818 * the ring buffer from an NMI.
3819 */
Steven Rostedt464e85e2009-08-05 15:26:37 -04003820 if (likely(!in_nmi()))
Steven Rostedt8d707e82009-06-16 21:22:48 -04003821 return 1;
3822
3823 tracing_off_permanent();
3824 return 0;
3825}
3826
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003827/**
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003828 * ring_buffer_peek - peek at the next event to be read
3829 * @buffer: The ring buffer to read
3830 * @cpu: The cpu to peak at
3831 * @ts: The timestamp counter of this event.
Steven Rostedt66a8cb92010-03-31 13:21:56 -04003832 * @lost_events: a variable to store if events were lost (may be NULL)
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003833 *
3834 * This will return the event that will be read next, but does
3835 * not consume the data.
3836 */
3837struct ring_buffer_event *
Steven Rostedt66a8cb92010-03-31 13:21:56 -04003838ring_buffer_peek(struct ring_buffer *buffer, int cpu, u64 *ts,
3839 unsigned long *lost_events)
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003840{
3841 struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
Steven Rostedt8aabee52009-03-12 13:13:49 -04003842 struct ring_buffer_event *event;
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003843 unsigned long flags;
Steven Rostedt8d707e82009-06-16 21:22:48 -04003844 int dolock;
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003845
Steven Rostedt554f7862009-03-11 22:00:13 -04003846 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04003847 return NULL;
Steven Rostedt554f7862009-03-11 22:00:13 -04003848
Steven Rostedt8d707e82009-06-16 21:22:48 -04003849 dolock = rb_ok_to_lock();
Tom Zanussi2d622712009-03-22 03:30:49 -05003850 again:
Steven Rostedt8d707e82009-06-16 21:22:48 -04003851 local_irq_save(flags);
3852 if (dolock)
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02003853 raw_spin_lock(&cpu_buffer->reader_lock);
Steven Rostedt66a8cb92010-03-31 13:21:56 -04003854 event = rb_buffer_peek(cpu_buffer, ts, lost_events);
Robert Richter469535a2009-07-30 19:19:18 +02003855 if (event && event->type_len == RINGBUF_TYPE_PADDING)
3856 rb_advance_reader(cpu_buffer);
Steven Rostedt8d707e82009-06-16 21:22:48 -04003857 if (dolock)
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02003858 raw_spin_unlock(&cpu_buffer->reader_lock);
Steven Rostedt8d707e82009-06-16 21:22:48 -04003859 local_irq_restore(flags);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003860
Steven Rostedt1b959e12009-09-03 10:12:13 -04003861 if (event && event->type_len == RINGBUF_TYPE_PADDING)
Tom Zanussi2d622712009-03-22 03:30:49 -05003862 goto again;
Tom Zanussi2d622712009-03-22 03:30:49 -05003863
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003864 return event;
3865}
3866
3867/**
3868 * ring_buffer_iter_peek - peek at the next event to be read
3869 * @iter: The ring buffer iterator
3870 * @ts: The timestamp counter of this event.
3871 *
3872 * This will return the event that will be read next, but does
3873 * not increment the iterator.
3874 */
3875struct ring_buffer_event *
3876ring_buffer_iter_peek(struct ring_buffer_iter *iter, u64 *ts)
3877{
3878 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
3879 struct ring_buffer_event *event;
3880 unsigned long flags;
3881
Tom Zanussi2d622712009-03-22 03:30:49 -05003882 again:
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02003883 raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003884 event = rb_iter_peek(iter, ts);
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02003885 raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003886
Steven Rostedt1b959e12009-09-03 10:12:13 -04003887 if (event && event->type_len == RINGBUF_TYPE_PADDING)
Tom Zanussi2d622712009-03-22 03:30:49 -05003888 goto again;
Tom Zanussi2d622712009-03-22 03:30:49 -05003889
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003890 return event;
3891}
3892
3893/**
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003894 * ring_buffer_consume - return an event and consume it
3895 * @buffer: The ring buffer to get the next event from
Steven Rostedt66a8cb92010-03-31 13:21:56 -04003896 * @cpu: the cpu to read the buffer from
3897 * @ts: a variable to store the timestamp (may be NULL)
3898 * @lost_events: a variable to store if events were lost (may be NULL)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003899 *
3900 * Returns the next event in the ring buffer, and that event is consumed.
3901 * Meaning, that sequential reads will keep returning a different event,
3902 * and eventually empty the ring buffer if the producer is slower.
3903 */
3904struct ring_buffer_event *
Steven Rostedt66a8cb92010-03-31 13:21:56 -04003905ring_buffer_consume(struct ring_buffer *buffer, int cpu, u64 *ts,
3906 unsigned long *lost_events)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003907{
Steven Rostedt554f7862009-03-11 22:00:13 -04003908 struct ring_buffer_per_cpu *cpu_buffer;
3909 struct ring_buffer_event *event = NULL;
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003910 unsigned long flags;
Steven Rostedt8d707e82009-06-16 21:22:48 -04003911 int dolock;
3912
3913 dolock = rb_ok_to_lock();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003914
Tom Zanussi2d622712009-03-22 03:30:49 -05003915 again:
Steven Rostedt554f7862009-03-11 22:00:13 -04003916 /* might be called in atomic */
3917 preempt_disable();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003918
Steven Rostedt554f7862009-03-11 22:00:13 -04003919 if (!cpumask_test_cpu(cpu, buffer->cpumask))
3920 goto out;
3921
3922 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt8d707e82009-06-16 21:22:48 -04003923 local_irq_save(flags);
3924 if (dolock)
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02003925 raw_spin_lock(&cpu_buffer->reader_lock);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003926
Steven Rostedt66a8cb92010-03-31 13:21:56 -04003927 event = rb_buffer_peek(cpu_buffer, ts, lost_events);
3928 if (event) {
3929 cpu_buffer->lost_events = 0;
Robert Richter469535a2009-07-30 19:19:18 +02003930 rb_advance_reader(cpu_buffer);
Steven Rostedt66a8cb92010-03-31 13:21:56 -04003931 }
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003932
Steven Rostedt8d707e82009-06-16 21:22:48 -04003933 if (dolock)
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02003934 raw_spin_unlock(&cpu_buffer->reader_lock);
Steven Rostedt8d707e82009-06-16 21:22:48 -04003935 local_irq_restore(flags);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003936
Steven Rostedt554f7862009-03-11 22:00:13 -04003937 out:
3938 preempt_enable();
3939
Steven Rostedt1b959e12009-09-03 10:12:13 -04003940 if (event && event->type_len == RINGBUF_TYPE_PADDING)
Tom Zanussi2d622712009-03-22 03:30:49 -05003941 goto again;
Tom Zanussi2d622712009-03-22 03:30:49 -05003942
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003943 return event;
3944}
Robert Richterc4f50182008-12-11 16:49:22 +01003945EXPORT_SYMBOL_GPL(ring_buffer_consume);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003946
3947/**
David Miller72c9ddf2010-04-20 15:47:11 -07003948 * ring_buffer_read_prepare - Prepare for a non consuming read of the buffer
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003949 * @buffer: The ring buffer to read from
3950 * @cpu: The cpu buffer to iterate over
3951 *
David Miller72c9ddf2010-04-20 15:47:11 -07003952 * This performs the initial preparations necessary to iterate
3953 * through the buffer. Memory is allocated, buffer recording
3954 * is disabled, and the iterator pointer is returned to the caller.
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003955 *
David Miller72c9ddf2010-04-20 15:47:11 -07003956 * Disabling buffer recordng prevents the reading from being
3957 * corrupted. This is not a consuming read, so a producer is not
3958 * expected.
3959 *
3960 * After a sequence of ring_buffer_read_prepare calls, the user is
zhangwei(Jovi)d6118512013-07-15 16:32:50 +08003961 * expected to make at least one call to ring_buffer_read_prepare_sync.
David Miller72c9ddf2010-04-20 15:47:11 -07003962 * Afterwards, ring_buffer_read_start is invoked to get things going
3963 * for real.
3964 *
zhangwei(Jovi)d6118512013-07-15 16:32:50 +08003965 * This overall must be paired with ring_buffer_read_finish.
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003966 */
3967struct ring_buffer_iter *
David Miller72c9ddf2010-04-20 15:47:11 -07003968ring_buffer_read_prepare(struct ring_buffer *buffer, int cpu)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003969{
3970 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedt8aabee52009-03-12 13:13:49 -04003971 struct ring_buffer_iter *iter;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003972
Rusty Russell9e01c1b2009-01-01 10:12:22 +10303973 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04003974 return NULL;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003975
3976 iter = kmalloc(sizeof(*iter), GFP_KERNEL);
3977 if (!iter)
Steven Rostedt8aabee52009-03-12 13:13:49 -04003978 return NULL;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003979
3980 cpu_buffer = buffer->buffers[cpu];
3981
3982 iter->cpu_buffer = cpu_buffer;
3983
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07003984 atomic_inc(&buffer->resize_disabled);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003985 atomic_inc(&cpu_buffer->record_disabled);
David Miller72c9ddf2010-04-20 15:47:11 -07003986
3987 return iter;
3988}
3989EXPORT_SYMBOL_GPL(ring_buffer_read_prepare);
3990
3991/**
3992 * ring_buffer_read_prepare_sync - Synchronize a set of prepare calls
3993 *
3994 * All previously invoked ring_buffer_read_prepare calls to prepare
3995 * iterators will be synchronized. Afterwards, read_buffer_read_start
3996 * calls on those iterators are allowed.
3997 */
3998void
3999ring_buffer_read_prepare_sync(void)
4000{
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004001 synchronize_sched();
David Miller72c9ddf2010-04-20 15:47:11 -07004002}
4003EXPORT_SYMBOL_GPL(ring_buffer_read_prepare_sync);
4004
4005/**
4006 * ring_buffer_read_start - start a non consuming read of the buffer
4007 * @iter: The iterator returned by ring_buffer_read_prepare
4008 *
4009 * This finalizes the startup of an iteration through the buffer.
4010 * The iterator comes from a call to ring_buffer_read_prepare and
4011 * an intervening ring_buffer_read_prepare_sync must have been
4012 * performed.
4013 *
zhangwei(Jovi)d6118512013-07-15 16:32:50 +08004014 * Must be paired with ring_buffer_read_finish.
David Miller72c9ddf2010-04-20 15:47:11 -07004015 */
4016void
4017ring_buffer_read_start(struct ring_buffer_iter *iter)
4018{
4019 struct ring_buffer_per_cpu *cpu_buffer;
4020 unsigned long flags;
4021
4022 if (!iter)
4023 return;
4024
4025 cpu_buffer = iter->cpu_buffer;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004026
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02004027 raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01004028 arch_spin_lock(&cpu_buffer->lock);
Steven Rostedt642edba2008-11-12 00:01:26 -05004029 rb_iter_reset(iter);
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01004030 arch_spin_unlock(&cpu_buffer->lock);
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02004031 raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004032}
Robert Richterc4f50182008-12-11 16:49:22 +01004033EXPORT_SYMBOL_GPL(ring_buffer_read_start);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004034
4035/**
zhangwei(Jovi)d6118512013-07-15 16:32:50 +08004036 * ring_buffer_read_finish - finish reading the iterator of the buffer
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004037 * @iter: The iterator retrieved by ring_buffer_start
4038 *
4039 * This re-enables the recording to the buffer, and frees the
4040 * iterator.
4041 */
4042void
4043ring_buffer_read_finish(struct ring_buffer_iter *iter)
4044{
4045 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
Steven Rostedt9366c1b2012-11-29 22:31:16 -05004046 unsigned long flags;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004047
Steven Rostedt659f4512012-05-14 17:02:33 -04004048 /*
4049 * Ring buffer is disabled from recording, here's a good place
Steven Rostedt9366c1b2012-11-29 22:31:16 -05004050 * to check the integrity of the ring buffer.
4051 * Must prevent readers from trying to read, as the check
4052 * clears the HEAD page and readers require it.
Steven Rostedt659f4512012-05-14 17:02:33 -04004053 */
Steven Rostedt9366c1b2012-11-29 22:31:16 -05004054 raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
Steven Rostedt659f4512012-05-14 17:02:33 -04004055 rb_check_pages(cpu_buffer);
Steven Rostedt9366c1b2012-11-29 22:31:16 -05004056 raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
Steven Rostedt659f4512012-05-14 17:02:33 -04004057
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004058 atomic_dec(&cpu_buffer->record_disabled);
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07004059 atomic_dec(&cpu_buffer->buffer->resize_disabled);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004060 kfree(iter);
4061}
Robert Richterc4f50182008-12-11 16:49:22 +01004062EXPORT_SYMBOL_GPL(ring_buffer_read_finish);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004063
4064/**
4065 * ring_buffer_read - read the next item in the ring buffer by the iterator
4066 * @iter: The ring buffer iterator
4067 * @ts: The time stamp of the event read.
4068 *
4069 * This reads the next event in the ring buffer and increments the iterator.
4070 */
4071struct ring_buffer_event *
4072ring_buffer_read(struct ring_buffer_iter *iter, u64 *ts)
4073{
4074 struct ring_buffer_event *event;
Steven Rostedtf83c9d02008-11-11 18:47:44 +01004075 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
4076 unsigned long flags;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004077
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02004078 raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
Steven Rostedt7e9391c2009-09-03 10:02:09 -04004079 again:
Steven Rostedtf83c9d02008-11-11 18:47:44 +01004080 event = rb_iter_peek(iter, ts);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004081 if (!event)
Steven Rostedtf83c9d02008-11-11 18:47:44 +01004082 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004083
Steven Rostedt7e9391c2009-09-03 10:02:09 -04004084 if (event->type_len == RINGBUF_TYPE_PADDING)
4085 goto again;
4086
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004087 rb_advance_iter(iter);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01004088 out:
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02004089 raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004090
4091 return event;
4092}
Robert Richterc4f50182008-12-11 16:49:22 +01004093EXPORT_SYMBOL_GPL(ring_buffer_read);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004094
4095/**
4096 * ring_buffer_size - return the size of the ring buffer (in bytes)
4097 * @buffer: The ring buffer.
4098 */
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08004099unsigned long ring_buffer_size(struct ring_buffer *buffer, int cpu)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004100{
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08004101 /*
4102 * Earlier, this method returned
4103 * BUF_PAGE_SIZE * buffer->nr_pages
4104 * Since the nr_pages field is now removed, we have converted this to
4105 * return the per cpu buffer value.
4106 */
4107 if (!cpumask_test_cpu(cpu, buffer->cpumask))
4108 return 0;
4109
4110 return BUF_PAGE_SIZE * buffer->buffers[cpu]->nr_pages;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004111}
Robert Richterc4f50182008-12-11 16:49:22 +01004112EXPORT_SYMBOL_GPL(ring_buffer_size);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004113
4114static void
4115rb_reset_cpu(struct ring_buffer_per_cpu *cpu_buffer)
4116{
Steven Rostedt77ae3652009-03-27 11:00:29 -04004117 rb_head_page_deactivate(cpu_buffer);
4118
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004119 cpu_buffer->head_page
Steven Rostedt3adc54f2009-03-30 15:32:01 -04004120 = list_entry(cpu_buffer->pages, struct buffer_page, list);
Steven Rostedtbf41a152008-10-04 02:00:59 -04004121 local_set(&cpu_buffer->head_page->write, 0);
Steven Rostedt778c55d2009-05-01 18:44:45 -04004122 local_set(&cpu_buffer->head_page->entries, 0);
Steven Rostedtabc9b562008-12-02 15:34:06 -05004123 local_set(&cpu_buffer->head_page->page->commit, 0);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004124
Steven Rostedt6f807ac2008-10-04 02:00:58 -04004125 cpu_buffer->head_page->read = 0;
Steven Rostedtbf41a152008-10-04 02:00:59 -04004126
4127 cpu_buffer->tail_page = cpu_buffer->head_page;
4128 cpu_buffer->commit_page = cpu_buffer->head_page;
4129
4130 INIT_LIST_HEAD(&cpu_buffer->reader_page->list);
Vaibhav Nagarnaik5040b4b2012-05-03 18:59:51 -07004131 INIT_LIST_HEAD(&cpu_buffer->new_pages);
Steven Rostedtbf41a152008-10-04 02:00:59 -04004132 local_set(&cpu_buffer->reader_page->write, 0);
Steven Rostedt778c55d2009-05-01 18:44:45 -04004133 local_set(&cpu_buffer->reader_page->entries, 0);
Steven Rostedtabc9b562008-12-02 15:34:06 -05004134 local_set(&cpu_buffer->reader_page->page->commit, 0);
Steven Rostedt6f807ac2008-10-04 02:00:58 -04004135 cpu_buffer->reader_page->read = 0;
Steven Rostedtd7690412008-10-01 00:29:53 -04004136
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -07004137 local_set(&cpu_buffer->entries_bytes, 0);
Steven Rostedt77ae3652009-03-27 11:00:29 -04004138 local_set(&cpu_buffer->overrun, 0);
Slava Pestov884bfe82011-07-15 14:23:58 -07004139 local_set(&cpu_buffer->commit_overrun, 0);
4140 local_set(&cpu_buffer->dropped_events, 0);
Steven Rostedte4906ef2009-04-30 20:49:44 -04004141 local_set(&cpu_buffer->entries, 0);
Steven Rostedtfa743952009-06-16 12:37:57 -04004142 local_set(&cpu_buffer->committing, 0);
4143 local_set(&cpu_buffer->commits, 0);
Steven Rostedt77ae3652009-03-27 11:00:29 -04004144 cpu_buffer->read = 0;
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -07004145 cpu_buffer->read_bytes = 0;
Steven Rostedt69507c02009-01-21 18:45:57 -05004146
4147 cpu_buffer->write_stamp = 0;
4148 cpu_buffer->read_stamp = 0;
Steven Rostedt77ae3652009-03-27 11:00:29 -04004149
Steven Rostedt66a8cb92010-03-31 13:21:56 -04004150 cpu_buffer->lost_events = 0;
4151 cpu_buffer->last_overrun = 0;
4152
Steven Rostedt77ae3652009-03-27 11:00:29 -04004153 rb_head_page_activate(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004154}
4155
4156/**
4157 * ring_buffer_reset_cpu - reset a ring buffer per CPU buffer
4158 * @buffer: The ring buffer to reset a per cpu buffer of
4159 * @cpu: The CPU buffer to be reset
4160 */
4161void ring_buffer_reset_cpu(struct ring_buffer *buffer, int cpu)
4162{
4163 struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
4164 unsigned long flags;
4165
Rusty Russell9e01c1b2009-01-01 10:12:22 +10304166 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04004167 return;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004168
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07004169 atomic_inc(&buffer->resize_disabled);
Steven Rostedt41ede232009-05-01 20:26:54 -04004170 atomic_inc(&cpu_buffer->record_disabled);
4171
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07004172 /* Make sure all commits have finished */
4173 synchronize_sched();
4174
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02004175 raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01004176
Steven Rostedt41b6a952009-09-02 09:59:48 -04004177 if (RB_WARN_ON(cpu_buffer, local_read(&cpu_buffer->committing)))
4178 goto out;
4179
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01004180 arch_spin_lock(&cpu_buffer->lock);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004181
4182 rb_reset_cpu(cpu_buffer);
4183
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01004184 arch_spin_unlock(&cpu_buffer->lock);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01004185
Steven Rostedt41b6a952009-09-02 09:59:48 -04004186 out:
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02004187 raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
Steven Rostedt41ede232009-05-01 20:26:54 -04004188
4189 atomic_dec(&cpu_buffer->record_disabled);
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07004190 atomic_dec(&buffer->resize_disabled);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004191}
Robert Richterc4f50182008-12-11 16:49:22 +01004192EXPORT_SYMBOL_GPL(ring_buffer_reset_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004193
4194/**
4195 * ring_buffer_reset - reset a ring buffer
4196 * @buffer: The ring buffer to reset all cpu buffers
4197 */
4198void ring_buffer_reset(struct ring_buffer *buffer)
4199{
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004200 int cpu;
4201
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004202 for_each_buffer_cpu(buffer, cpu)
Steven Rostedtd7690412008-10-01 00:29:53 -04004203 ring_buffer_reset_cpu(buffer, cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004204}
Robert Richterc4f50182008-12-11 16:49:22 +01004205EXPORT_SYMBOL_GPL(ring_buffer_reset);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004206
4207/**
4208 * rind_buffer_empty - is the ring buffer empty?
4209 * @buffer: The ring buffer to test
4210 */
4211int ring_buffer_empty(struct ring_buffer *buffer)
4212{
4213 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedtd4788202009-06-17 00:39:43 -04004214 unsigned long flags;
Steven Rostedt8d707e82009-06-16 21:22:48 -04004215 int dolock;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004216 int cpu;
Steven Rostedtd4788202009-06-17 00:39:43 -04004217 int ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004218
Steven Rostedt8d707e82009-06-16 21:22:48 -04004219 dolock = rb_ok_to_lock();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004220
4221 /* yes this is racy, but if you don't like the race, lock the buffer */
4222 for_each_buffer_cpu(buffer, cpu) {
4223 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt8d707e82009-06-16 21:22:48 -04004224 local_irq_save(flags);
4225 if (dolock)
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02004226 raw_spin_lock(&cpu_buffer->reader_lock);
Steven Rostedtd4788202009-06-17 00:39:43 -04004227 ret = rb_per_cpu_empty(cpu_buffer);
Steven Rostedt8d707e82009-06-16 21:22:48 -04004228 if (dolock)
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02004229 raw_spin_unlock(&cpu_buffer->reader_lock);
Steven Rostedt8d707e82009-06-16 21:22:48 -04004230 local_irq_restore(flags);
4231
Steven Rostedtd4788202009-06-17 00:39:43 -04004232 if (!ret)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004233 return 0;
4234 }
Steven Rostedt554f7862009-03-11 22:00:13 -04004235
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004236 return 1;
4237}
Robert Richterc4f50182008-12-11 16:49:22 +01004238EXPORT_SYMBOL_GPL(ring_buffer_empty);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004239
4240/**
4241 * ring_buffer_empty_cpu - is a cpu buffer of a ring buffer empty?
4242 * @buffer: The ring buffer
4243 * @cpu: The CPU buffer to test
4244 */
4245int ring_buffer_empty_cpu(struct ring_buffer *buffer, int cpu)
4246{
4247 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedtd4788202009-06-17 00:39:43 -04004248 unsigned long flags;
Steven Rostedt8d707e82009-06-16 21:22:48 -04004249 int dolock;
Steven Rostedt8aabee52009-03-12 13:13:49 -04004250 int ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004251
Rusty Russell9e01c1b2009-01-01 10:12:22 +10304252 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04004253 return 1;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004254
Steven Rostedt8d707e82009-06-16 21:22:48 -04004255 dolock = rb_ok_to_lock();
Steven Rostedt554f7862009-03-11 22:00:13 -04004256
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004257 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt8d707e82009-06-16 21:22:48 -04004258 local_irq_save(flags);
4259 if (dolock)
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02004260 raw_spin_lock(&cpu_buffer->reader_lock);
Steven Rostedt554f7862009-03-11 22:00:13 -04004261 ret = rb_per_cpu_empty(cpu_buffer);
Steven Rostedt8d707e82009-06-16 21:22:48 -04004262 if (dolock)
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02004263 raw_spin_unlock(&cpu_buffer->reader_lock);
Steven Rostedt8d707e82009-06-16 21:22:48 -04004264 local_irq_restore(flags);
Steven Rostedt554f7862009-03-11 22:00:13 -04004265
4266 return ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004267}
Robert Richterc4f50182008-12-11 16:49:22 +01004268EXPORT_SYMBOL_GPL(ring_buffer_empty_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004269
Steven Rostedt85bac322009-09-04 14:24:40 -04004270#ifdef CONFIG_RING_BUFFER_ALLOW_SWAP
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004271/**
4272 * ring_buffer_swap_cpu - swap a CPU buffer between two ring buffers
4273 * @buffer_a: One buffer to swap with
4274 * @buffer_b: The other buffer to swap with
4275 *
4276 * This function is useful for tracers that want to take a "snapshot"
4277 * of a CPU buffer and has another back up buffer lying around.
4278 * it is expected that the tracer handles the cpu buffer not being
4279 * used at the moment.
4280 */
4281int ring_buffer_swap_cpu(struct ring_buffer *buffer_a,
4282 struct ring_buffer *buffer_b, int cpu)
4283{
4284 struct ring_buffer_per_cpu *cpu_buffer_a;
4285 struct ring_buffer_per_cpu *cpu_buffer_b;
Steven Rostedt554f7862009-03-11 22:00:13 -04004286 int ret = -EINVAL;
4287
Rusty Russell9e01c1b2009-01-01 10:12:22 +10304288 if (!cpumask_test_cpu(cpu, buffer_a->cpumask) ||
4289 !cpumask_test_cpu(cpu, buffer_b->cpumask))
Steven Rostedt554f7862009-03-11 22:00:13 -04004290 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004291
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08004292 cpu_buffer_a = buffer_a->buffers[cpu];
4293 cpu_buffer_b = buffer_b->buffers[cpu];
4294
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004295 /* At least make sure the two buffers are somewhat the same */
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08004296 if (cpu_buffer_a->nr_pages != cpu_buffer_b->nr_pages)
Steven Rostedt554f7862009-03-11 22:00:13 -04004297 goto out;
4298
4299 ret = -EAGAIN;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004300
Steven Rostedt97b17ef2009-01-21 15:24:56 -05004301 if (ring_buffer_flags != RB_BUFFERS_ON)
Steven Rostedt554f7862009-03-11 22:00:13 -04004302 goto out;
Steven Rostedt97b17ef2009-01-21 15:24:56 -05004303
4304 if (atomic_read(&buffer_a->record_disabled))
Steven Rostedt554f7862009-03-11 22:00:13 -04004305 goto out;
Steven Rostedt97b17ef2009-01-21 15:24:56 -05004306
4307 if (atomic_read(&buffer_b->record_disabled))
Steven Rostedt554f7862009-03-11 22:00:13 -04004308 goto out;
Steven Rostedt97b17ef2009-01-21 15:24:56 -05004309
Steven Rostedt97b17ef2009-01-21 15:24:56 -05004310 if (atomic_read(&cpu_buffer_a->record_disabled))
Steven Rostedt554f7862009-03-11 22:00:13 -04004311 goto out;
Steven Rostedt97b17ef2009-01-21 15:24:56 -05004312
4313 if (atomic_read(&cpu_buffer_b->record_disabled))
Steven Rostedt554f7862009-03-11 22:00:13 -04004314 goto out;
Steven Rostedt97b17ef2009-01-21 15:24:56 -05004315
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004316 /*
4317 * We can't do a synchronize_sched here because this
4318 * function can be called in atomic context.
4319 * Normally this will be called from the same CPU as cpu.
4320 * If not it's up to the caller to protect this.
4321 */
4322 atomic_inc(&cpu_buffer_a->record_disabled);
4323 atomic_inc(&cpu_buffer_b->record_disabled);
4324
Steven Rostedt98277992009-09-02 10:56:15 -04004325 ret = -EBUSY;
4326 if (local_read(&cpu_buffer_a->committing))
4327 goto out_dec;
4328 if (local_read(&cpu_buffer_b->committing))
4329 goto out_dec;
4330
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004331 buffer_a->buffers[cpu] = cpu_buffer_b;
4332 buffer_b->buffers[cpu] = cpu_buffer_a;
4333
4334 cpu_buffer_b->buffer = buffer_a;
4335 cpu_buffer_a->buffer = buffer_b;
4336
Steven Rostedt98277992009-09-02 10:56:15 -04004337 ret = 0;
4338
4339out_dec:
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004340 atomic_dec(&cpu_buffer_a->record_disabled);
4341 atomic_dec(&cpu_buffer_b->record_disabled);
Steven Rostedt554f7862009-03-11 22:00:13 -04004342out:
Steven Rostedt554f7862009-03-11 22:00:13 -04004343 return ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004344}
Robert Richterc4f50182008-12-11 16:49:22 +01004345EXPORT_SYMBOL_GPL(ring_buffer_swap_cpu);
Steven Rostedt85bac322009-09-04 14:24:40 -04004346#endif /* CONFIG_RING_BUFFER_ALLOW_SWAP */
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004347
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004348/**
4349 * ring_buffer_alloc_read_page - allocate a page to read from buffer
4350 * @buffer: the buffer to allocate for.
zhangwei(Jovi)d6118512013-07-15 16:32:50 +08004351 * @cpu: the cpu buffer to allocate.
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004352 *
4353 * This function is used in conjunction with ring_buffer_read_page.
4354 * When reading a full page from the ring buffer, these functions
4355 * can be used to speed up the process. The calling function should
4356 * allocate a few pages first with this function. Then when it
4357 * needs to get pages from the ring buffer, it passes the result
4358 * of this function into ring_buffer_read_page, which will swap
4359 * the page that was allocated, with the read page of the buffer.
4360 *
4361 * Returns:
4362 * The page allocated, or NULL on error.
4363 */
Vaibhav Nagarnaik7ea59062011-05-03 17:56:42 -07004364void *ring_buffer_alloc_read_page(struct ring_buffer *buffer, int cpu)
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004365{
Steven Rostedt044fa782008-12-02 23:50:03 -05004366 struct buffer_data_page *bpage;
Vaibhav Nagarnaik7ea59062011-05-03 17:56:42 -07004367 struct page *page;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004368
Vaibhav Nagarnaikd7ec4bf2011-06-07 17:01:42 -07004369 page = alloc_pages_node(cpu_to_node(cpu),
4370 GFP_KERNEL | __GFP_NORETRY, 0);
Vaibhav Nagarnaik7ea59062011-05-03 17:56:42 -07004371 if (!page)
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004372 return NULL;
4373
Vaibhav Nagarnaik7ea59062011-05-03 17:56:42 -07004374 bpage = page_address(page);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004375
Steven Rostedtef7a4a12009-03-03 00:27:49 -05004376 rb_init_page(bpage);
4377
Steven Rostedt044fa782008-12-02 23:50:03 -05004378 return bpage;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004379}
Steven Rostedtd6ce96d2009-05-05 01:15:24 -04004380EXPORT_SYMBOL_GPL(ring_buffer_alloc_read_page);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004381
4382/**
4383 * ring_buffer_free_read_page - free an allocated read page
4384 * @buffer: the buffer the page was allocate for
4385 * @data: the page to free
4386 *
4387 * Free a page allocated from ring_buffer_alloc_read_page.
4388 */
4389void ring_buffer_free_read_page(struct ring_buffer *buffer, void *data)
4390{
4391 free_page((unsigned long)data);
4392}
Steven Rostedtd6ce96d2009-05-05 01:15:24 -04004393EXPORT_SYMBOL_GPL(ring_buffer_free_read_page);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004394
4395/**
4396 * ring_buffer_read_page - extract a page from the ring buffer
4397 * @buffer: buffer to extract from
4398 * @data_page: the page to use allocated from ring_buffer_alloc_read_page
Steven Rostedtef7a4a12009-03-03 00:27:49 -05004399 * @len: amount to extract
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004400 * @cpu: the cpu of the buffer to extract
4401 * @full: should the extraction only happen when the page is full.
4402 *
4403 * This function will pull out a page from the ring buffer and consume it.
4404 * @data_page must be the address of the variable that was returned
4405 * from ring_buffer_alloc_read_page. This is because the page might be used
4406 * to swap with a page in the ring buffer.
4407 *
4408 * for example:
zhangwei(Jovi)d6118512013-07-15 16:32:50 +08004409 * rpage = ring_buffer_alloc_read_page(buffer, cpu);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004410 * if (!rpage)
4411 * return error;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05004412 * ret = ring_buffer_read_page(buffer, &rpage, len, cpu, 0);
Lai Jiangshan667d2412009-02-09 14:21:17 +08004413 * if (ret >= 0)
4414 * process_page(rpage, ret);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004415 *
4416 * When @full is set, the function will not return true unless
4417 * the writer is off the reader page.
4418 *
4419 * Note: it is up to the calling functions to handle sleeps and wakeups.
4420 * The ring buffer can be used anywhere in the kernel and can not
4421 * blindly call wake_up. The layer that uses the ring buffer must be
4422 * responsible for that.
4423 *
4424 * Returns:
Lai Jiangshan667d2412009-02-09 14:21:17 +08004425 * >=0 if data has been transferred, returns the offset of consumed data.
4426 * <0 if no data has been transferred.
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004427 */
4428int ring_buffer_read_page(struct ring_buffer *buffer,
Steven Rostedtef7a4a12009-03-03 00:27:49 -05004429 void **data_page, size_t len, int cpu, int full)
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004430{
4431 struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
4432 struct ring_buffer_event *event;
Steven Rostedt044fa782008-12-02 23:50:03 -05004433 struct buffer_data_page *bpage;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05004434 struct buffer_page *reader;
Steven Rostedtff0ff842010-03-31 22:11:42 -04004435 unsigned long missed_events;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004436 unsigned long flags;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05004437 unsigned int commit;
Lai Jiangshan667d2412009-02-09 14:21:17 +08004438 unsigned int read;
Steven Rostedt4f3640f2009-03-03 23:52:42 -05004439 u64 save_timestamp;
Lai Jiangshan667d2412009-02-09 14:21:17 +08004440 int ret = -1;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004441
Steven Rostedt554f7862009-03-11 22:00:13 -04004442 if (!cpumask_test_cpu(cpu, buffer->cpumask))
4443 goto out;
4444
Steven Rostedt474d32b2009-03-03 19:51:40 -05004445 /*
4446 * If len is not big enough to hold the page header, then
4447 * we can not copy anything.
4448 */
4449 if (len <= BUF_PAGE_HDR_SIZE)
Steven Rostedt554f7862009-03-11 22:00:13 -04004450 goto out;
Steven Rostedt474d32b2009-03-03 19:51:40 -05004451
4452 len -= BUF_PAGE_HDR_SIZE;
4453
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004454 if (!data_page)
Steven Rostedt554f7862009-03-11 22:00:13 -04004455 goto out;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004456
Steven Rostedt044fa782008-12-02 23:50:03 -05004457 bpage = *data_page;
4458 if (!bpage)
Steven Rostedt554f7862009-03-11 22:00:13 -04004459 goto out;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004460
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02004461 raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004462
Steven Rostedtef7a4a12009-03-03 00:27:49 -05004463 reader = rb_get_reader_page(cpu_buffer);
4464 if (!reader)
Steven Rostedt554f7862009-03-11 22:00:13 -04004465 goto out_unlock;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004466
Steven Rostedtef7a4a12009-03-03 00:27:49 -05004467 event = rb_reader_event(cpu_buffer);
Lai Jiangshan667d2412009-02-09 14:21:17 +08004468
Steven Rostedtef7a4a12009-03-03 00:27:49 -05004469 read = reader->read;
4470 commit = rb_page_commit(reader);
4471
Steven Rostedt66a8cb92010-03-31 13:21:56 -04004472 /* Check if any events were dropped */
Steven Rostedtff0ff842010-03-31 22:11:42 -04004473 missed_events = cpu_buffer->lost_events;
Steven Rostedt66a8cb92010-03-31 13:21:56 -04004474
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004475 /*
Steven Rostedt474d32b2009-03-03 19:51:40 -05004476 * If this page has been partially read or
4477 * if len is not big enough to read the rest of the page or
4478 * a writer is still on the page, then
4479 * we must copy the data from the page to the buffer.
4480 * Otherwise, we can simply swap the page with the one passed in.
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004481 */
Steven Rostedt474d32b2009-03-03 19:51:40 -05004482 if (read || (len < (commit - read)) ||
Steven Rostedtef7a4a12009-03-03 00:27:49 -05004483 cpu_buffer->reader_page == cpu_buffer->commit_page) {
Lai Jiangshan667d2412009-02-09 14:21:17 +08004484 struct buffer_data_page *rpage = cpu_buffer->reader_page->page;
Steven Rostedt474d32b2009-03-03 19:51:40 -05004485 unsigned int rpos = read;
4486 unsigned int pos = 0;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05004487 unsigned int size;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004488
4489 if (full)
Steven Rostedt554f7862009-03-11 22:00:13 -04004490 goto out_unlock;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004491
Steven Rostedtef7a4a12009-03-03 00:27:49 -05004492 if (len > (commit - read))
4493 len = (commit - read);
4494
Steven Rostedt69d1b832010-10-07 18:18:05 -04004495 /* Always keep the time extend and data together */
4496 size = rb_event_ts_length(event);
Steven Rostedtef7a4a12009-03-03 00:27:49 -05004497
4498 if (len < size)
Steven Rostedt554f7862009-03-11 22:00:13 -04004499 goto out_unlock;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05004500
Steven Rostedt4f3640f2009-03-03 23:52:42 -05004501 /* save the current timestamp, since the user will need it */
4502 save_timestamp = cpu_buffer->read_stamp;
4503
Steven Rostedtef7a4a12009-03-03 00:27:49 -05004504 /* Need to copy one event at a time */
4505 do {
David Sharpe1e35922010-12-22 16:38:24 -08004506 /* We need the size of one event, because
4507 * rb_advance_reader only advances by one event,
4508 * whereas rb_event_ts_length may include the size of
4509 * one or two events.
4510 * We have already ensured there's enough space if this
4511 * is a time extend. */
4512 size = rb_event_length(event);
Steven Rostedt474d32b2009-03-03 19:51:40 -05004513 memcpy(bpage->data + pos, rpage->data + rpos, size);
Steven Rostedtef7a4a12009-03-03 00:27:49 -05004514
4515 len -= size;
4516
4517 rb_advance_reader(cpu_buffer);
Steven Rostedt474d32b2009-03-03 19:51:40 -05004518 rpos = reader->read;
4519 pos += size;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05004520
Huang Ying18fab912010-07-28 14:14:01 +08004521 if (rpos >= commit)
4522 break;
4523
Steven Rostedtef7a4a12009-03-03 00:27:49 -05004524 event = rb_reader_event(cpu_buffer);
Steven Rostedt69d1b832010-10-07 18:18:05 -04004525 /* Always keep the time extend and data together */
4526 size = rb_event_ts_length(event);
David Sharpe1e35922010-12-22 16:38:24 -08004527 } while (len >= size);
Lai Jiangshan667d2412009-02-09 14:21:17 +08004528
4529 /* update bpage */
Steven Rostedtef7a4a12009-03-03 00:27:49 -05004530 local_set(&bpage->commit, pos);
Steven Rostedt4f3640f2009-03-03 23:52:42 -05004531 bpage->time_stamp = save_timestamp;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05004532
Steven Rostedt474d32b2009-03-03 19:51:40 -05004533 /* we copied everything to the beginning */
4534 read = 0;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004535 } else {
Steven Rostedtafbab762009-05-01 19:40:05 -04004536 /* update the entry counter */
Steven Rostedt77ae3652009-03-27 11:00:29 -04004537 cpu_buffer->read += rb_page_entries(reader);
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -07004538 cpu_buffer->read_bytes += BUF_PAGE_SIZE;
Steven Rostedtafbab762009-05-01 19:40:05 -04004539
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004540 /* swap the pages */
Steven Rostedt044fa782008-12-02 23:50:03 -05004541 rb_init_page(bpage);
Steven Rostedtef7a4a12009-03-03 00:27:49 -05004542 bpage = reader->page;
4543 reader->page = *data_page;
4544 local_set(&reader->write, 0);
Steven Rostedt778c55d2009-05-01 18:44:45 -04004545 local_set(&reader->entries, 0);
Steven Rostedtef7a4a12009-03-03 00:27:49 -05004546 reader->read = 0;
Steven Rostedt044fa782008-12-02 23:50:03 -05004547 *data_page = bpage;
Steven Rostedtff0ff842010-03-31 22:11:42 -04004548
4549 /*
4550 * Use the real_end for the data size,
4551 * This gives us a chance to store the lost events
4552 * on the page.
4553 */
4554 if (reader->real_end)
4555 local_set(&bpage->commit, reader->real_end);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004556 }
Lai Jiangshan667d2412009-02-09 14:21:17 +08004557 ret = read;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004558
Steven Rostedt66a8cb92010-03-31 13:21:56 -04004559 cpu_buffer->lost_events = 0;
Steven Rostedt2711ca22010-05-21 13:32:26 -04004560
4561 commit = local_read(&bpage->commit);
Steven Rostedt66a8cb92010-03-31 13:21:56 -04004562 /*
4563 * Set a flag in the commit field if we lost events
4564 */
Steven Rostedtff0ff842010-03-31 22:11:42 -04004565 if (missed_events) {
Steven Rostedtff0ff842010-03-31 22:11:42 -04004566 /* If there is room at the end of the page to save the
4567 * missed events, then record it there.
4568 */
4569 if (BUF_PAGE_SIZE - commit >= sizeof(missed_events)) {
4570 memcpy(&bpage->data[commit], &missed_events,
4571 sizeof(missed_events));
4572 local_add(RB_MISSED_STORED, &bpage->commit);
Steven Rostedt2711ca22010-05-21 13:32:26 -04004573 commit += sizeof(missed_events);
Steven Rostedtff0ff842010-03-31 22:11:42 -04004574 }
Steven Rostedt66a8cb92010-03-31 13:21:56 -04004575 local_add(RB_MISSED_EVENTS, &bpage->commit);
Steven Rostedtff0ff842010-03-31 22:11:42 -04004576 }
Steven Rostedt66a8cb92010-03-31 13:21:56 -04004577
Steven Rostedt2711ca22010-05-21 13:32:26 -04004578 /*
4579 * This page may be off to user land. Zero it out here.
4580 */
4581 if (commit < BUF_PAGE_SIZE)
4582 memset(&bpage->data[commit], 0, BUF_PAGE_SIZE - commit);
4583
Steven Rostedt554f7862009-03-11 22:00:13 -04004584 out_unlock:
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02004585 raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004586
Steven Rostedt554f7862009-03-11 22:00:13 -04004587 out:
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004588 return ret;
4589}
Steven Rostedtd6ce96d2009-05-05 01:15:24 -04004590EXPORT_SYMBOL_GPL(ring_buffer_read_page);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004591
Steven Rostedt59222ef2009-03-12 11:46:03 -04004592#ifdef CONFIG_HOTPLUG_CPU
Frederic Weisbecker09c9e842009-03-21 04:33:36 +01004593static int rb_cpu_notify(struct notifier_block *self,
4594 unsigned long action, void *hcpu)
Steven Rostedt554f7862009-03-11 22:00:13 -04004595{
4596 struct ring_buffer *buffer =
4597 container_of(self, struct ring_buffer, cpu_notify);
4598 long cpu = (long)hcpu;
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08004599 int cpu_i, nr_pages_same;
4600 unsigned int nr_pages;
Steven Rostedt554f7862009-03-11 22:00:13 -04004601
4602 switch (action) {
4603 case CPU_UP_PREPARE:
4604 case CPU_UP_PREPARE_FROZEN:
Rusty Russell3f237a72009-06-12 21:15:30 +09304605 if (cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt554f7862009-03-11 22:00:13 -04004606 return NOTIFY_OK;
4607
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08004608 nr_pages = 0;
4609 nr_pages_same = 1;
4610 /* check if all cpu sizes are same */
4611 for_each_buffer_cpu(buffer, cpu_i) {
4612 /* fill in the size from first enabled cpu */
4613 if (nr_pages == 0)
4614 nr_pages = buffer->buffers[cpu_i]->nr_pages;
4615 if (nr_pages != buffer->buffers[cpu_i]->nr_pages) {
4616 nr_pages_same = 0;
4617 break;
4618 }
4619 }
4620 /* allocate minimum pages, user can later expand it */
4621 if (!nr_pages_same)
4622 nr_pages = 2;
Steven Rostedt554f7862009-03-11 22:00:13 -04004623 buffer->buffers[cpu] =
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08004624 rb_allocate_cpu_buffer(buffer, nr_pages, cpu);
Steven Rostedt554f7862009-03-11 22:00:13 -04004625 if (!buffer->buffers[cpu]) {
4626 WARN(1, "failed to allocate ring buffer on CPU %ld\n",
4627 cpu);
4628 return NOTIFY_OK;
4629 }
4630 smp_wmb();
Rusty Russell3f237a72009-06-12 21:15:30 +09304631 cpumask_set_cpu(cpu, buffer->cpumask);
Steven Rostedt554f7862009-03-11 22:00:13 -04004632 break;
4633 case CPU_DOWN_PREPARE:
4634 case CPU_DOWN_PREPARE_FROZEN:
4635 /*
4636 * Do nothing.
4637 * If we were to free the buffer, then the user would
4638 * lose any trace that was in the buffer.
4639 */
4640 break;
4641 default:
4642 break;
4643 }
4644 return NOTIFY_OK;
4645}
4646#endif
Steven Rostedt (Red Hat)6c43e552013-03-15 11:32:53 -04004647
4648#ifdef CONFIG_RING_BUFFER_STARTUP_TEST
4649/*
4650 * This is a basic integrity check of the ring buffer.
4651 * Late in the boot cycle this test will run when configured in.
4652 * It will kick off a thread per CPU that will go into a loop
4653 * writing to the per cpu ring buffer various sizes of data.
4654 * Some of the data will be large items, some small.
4655 *
4656 * Another thread is created that goes into a spin, sending out
4657 * IPIs to the other CPUs to also write into the ring buffer.
4658 * this is to test the nesting ability of the buffer.
4659 *
4660 * Basic stats are recorded and reported. If something in the
4661 * ring buffer should happen that's not expected, a big warning
4662 * is displayed and all ring buffers are disabled.
4663 */
4664static struct task_struct *rb_threads[NR_CPUS] __initdata;
4665
4666struct rb_test_data {
4667 struct ring_buffer *buffer;
4668 unsigned long events;
4669 unsigned long bytes_written;
4670 unsigned long bytes_alloc;
4671 unsigned long bytes_dropped;
4672 unsigned long events_nested;
4673 unsigned long bytes_written_nested;
4674 unsigned long bytes_alloc_nested;
4675 unsigned long bytes_dropped_nested;
4676 int min_size_nested;
4677 int max_size_nested;
4678 int max_size;
4679 int min_size;
4680 int cpu;
4681 int cnt;
4682};
4683
4684static struct rb_test_data rb_data[NR_CPUS] __initdata;
4685
4686/* 1 meg per cpu */
4687#define RB_TEST_BUFFER_SIZE 1048576
4688
4689static char rb_string[] __initdata =
4690 "abcdefghijklmnopqrstuvwxyz1234567890!@#$%^&*()?+\\"
4691 "?+|:';\",.<>/?abcdefghijklmnopqrstuvwxyz1234567890"
4692 "!@#$%^&*()?+\\?+|:';\",.<>/?abcdefghijklmnopqrstuv";
4693
4694static bool rb_test_started __initdata;
4695
4696struct rb_item {
4697 int size;
4698 char str[];
4699};
4700
4701static __init int rb_write_something(struct rb_test_data *data, bool nested)
4702{
4703 struct ring_buffer_event *event;
4704 struct rb_item *item;
4705 bool started;
4706 int event_len;
4707 int size;
4708 int len;
4709 int cnt;
4710
4711 /* Have nested writes different that what is written */
4712 cnt = data->cnt + (nested ? 27 : 0);
4713
4714 /* Multiply cnt by ~e, to make some unique increment */
4715 size = (data->cnt * 68 / 25) % (sizeof(rb_string) - 1);
4716
4717 len = size + sizeof(struct rb_item);
4718
4719 started = rb_test_started;
4720 /* read rb_test_started before checking buffer enabled */
4721 smp_rmb();
4722
4723 event = ring_buffer_lock_reserve(data->buffer, len);
4724 if (!event) {
4725 /* Ignore dropped events before test starts. */
4726 if (started) {
4727 if (nested)
4728 data->bytes_dropped += len;
4729 else
4730 data->bytes_dropped_nested += len;
4731 }
4732 return len;
4733 }
4734
4735 event_len = ring_buffer_event_length(event);
4736
4737 if (RB_WARN_ON(data->buffer, event_len < len))
4738 goto out;
4739
4740 item = ring_buffer_event_data(event);
4741 item->size = size;
4742 memcpy(item->str, rb_string, size);
4743
4744 if (nested) {
4745 data->bytes_alloc_nested += event_len;
4746 data->bytes_written_nested += len;
4747 data->events_nested++;
4748 if (!data->min_size_nested || len < data->min_size_nested)
4749 data->min_size_nested = len;
4750 if (len > data->max_size_nested)
4751 data->max_size_nested = len;
4752 } else {
4753 data->bytes_alloc += event_len;
4754 data->bytes_written += len;
4755 data->events++;
4756 if (!data->min_size || len < data->min_size)
4757 data->max_size = len;
4758 if (len > data->max_size)
4759 data->max_size = len;
4760 }
4761
4762 out:
4763 ring_buffer_unlock_commit(data->buffer, event);
4764
4765 return 0;
4766}
4767
4768static __init int rb_test(void *arg)
4769{
4770 struct rb_test_data *data = arg;
4771
4772 while (!kthread_should_stop()) {
4773 rb_write_something(data, false);
4774 data->cnt++;
4775
4776 set_current_state(TASK_INTERRUPTIBLE);
4777 /* Now sleep between a min of 100-300us and a max of 1ms */
4778 usleep_range(((data->cnt % 3) + 1) * 100, 1000);
4779 }
4780
4781 return 0;
4782}
4783
4784static __init void rb_ipi(void *ignore)
4785{
4786 struct rb_test_data *data;
4787 int cpu = smp_processor_id();
4788
4789 data = &rb_data[cpu];
4790 rb_write_something(data, true);
4791}
4792
4793static __init int rb_hammer_test(void *arg)
4794{
4795 while (!kthread_should_stop()) {
4796
4797 /* Send an IPI to all cpus to write data! */
4798 smp_call_function(rb_ipi, NULL, 1);
4799 /* No sleep, but for non preempt, let others run */
4800 schedule();
4801 }
4802
4803 return 0;
4804}
4805
4806static __init int test_ringbuffer(void)
4807{
4808 struct task_struct *rb_hammer;
4809 struct ring_buffer *buffer;
4810 int cpu;
4811 int ret = 0;
4812
4813 pr_info("Running ring buffer tests...\n");
4814
4815 buffer = ring_buffer_alloc(RB_TEST_BUFFER_SIZE, RB_FL_OVERWRITE);
4816 if (WARN_ON(!buffer))
4817 return 0;
4818
4819 /* Disable buffer so that threads can't write to it yet */
4820 ring_buffer_record_off(buffer);
4821
4822 for_each_online_cpu(cpu) {
4823 rb_data[cpu].buffer = buffer;
4824 rb_data[cpu].cpu = cpu;
4825 rb_data[cpu].cnt = cpu;
4826 rb_threads[cpu] = kthread_create(rb_test, &rb_data[cpu],
4827 "rbtester/%d", cpu);
4828 if (WARN_ON(!rb_threads[cpu])) {
4829 pr_cont("FAILED\n");
4830 ret = -1;
4831 goto out_free;
4832 }
4833
4834 kthread_bind(rb_threads[cpu], cpu);
4835 wake_up_process(rb_threads[cpu]);
4836 }
4837
4838 /* Now create the rb hammer! */
4839 rb_hammer = kthread_run(rb_hammer_test, NULL, "rbhammer");
4840 if (WARN_ON(!rb_hammer)) {
4841 pr_cont("FAILED\n");
4842 ret = -1;
4843 goto out_free;
4844 }
4845
4846 ring_buffer_record_on(buffer);
4847 /*
4848 * Show buffer is enabled before setting rb_test_started.
4849 * Yes there's a small race window where events could be
4850 * dropped and the thread wont catch it. But when a ring
4851 * buffer gets enabled, there will always be some kind of
4852 * delay before other CPUs see it. Thus, we don't care about
4853 * those dropped events. We care about events dropped after
4854 * the threads see that the buffer is active.
4855 */
4856 smp_wmb();
4857 rb_test_started = true;
4858
4859 set_current_state(TASK_INTERRUPTIBLE);
4860 /* Just run for 10 seconds */;
4861 schedule_timeout(10 * HZ);
4862
4863 kthread_stop(rb_hammer);
4864
4865 out_free:
4866 for_each_online_cpu(cpu) {
4867 if (!rb_threads[cpu])
4868 break;
4869 kthread_stop(rb_threads[cpu]);
4870 }
4871 if (ret) {
4872 ring_buffer_free(buffer);
4873 return ret;
4874 }
4875
4876 /* Report! */
4877 pr_info("finished\n");
4878 for_each_online_cpu(cpu) {
4879 struct ring_buffer_event *event;
4880 struct rb_test_data *data = &rb_data[cpu];
4881 struct rb_item *item;
4882 unsigned long total_events;
4883 unsigned long total_dropped;
4884 unsigned long total_written;
4885 unsigned long total_alloc;
4886 unsigned long total_read = 0;
4887 unsigned long total_size = 0;
4888 unsigned long total_len = 0;
4889 unsigned long total_lost = 0;
4890 unsigned long lost;
4891 int big_event_size;
4892 int small_event_size;
4893
4894 ret = -1;
4895
4896 total_events = data->events + data->events_nested;
4897 total_written = data->bytes_written + data->bytes_written_nested;
4898 total_alloc = data->bytes_alloc + data->bytes_alloc_nested;
4899 total_dropped = data->bytes_dropped + data->bytes_dropped_nested;
4900
4901 big_event_size = data->max_size + data->max_size_nested;
4902 small_event_size = data->min_size + data->min_size_nested;
4903
4904 pr_info("CPU %d:\n", cpu);
4905 pr_info(" events: %ld\n", total_events);
4906 pr_info(" dropped bytes: %ld\n", total_dropped);
4907 pr_info(" alloced bytes: %ld\n", total_alloc);
4908 pr_info(" written bytes: %ld\n", total_written);
4909 pr_info(" biggest event: %d\n", big_event_size);
4910 pr_info(" smallest event: %d\n", small_event_size);
4911
4912 if (RB_WARN_ON(buffer, total_dropped))
4913 break;
4914
4915 ret = 0;
4916
4917 while ((event = ring_buffer_consume(buffer, cpu, NULL, &lost))) {
4918 total_lost += lost;
4919 item = ring_buffer_event_data(event);
4920 total_len += ring_buffer_event_length(event);
4921 total_size += item->size + sizeof(struct rb_item);
4922 if (memcmp(&item->str[0], rb_string, item->size) != 0) {
4923 pr_info("FAILED!\n");
4924 pr_info("buffer had: %.*s\n", item->size, item->str);
4925 pr_info("expected: %.*s\n", item->size, rb_string);
4926 RB_WARN_ON(buffer, 1);
4927 ret = -1;
4928 break;
4929 }
4930 total_read++;
4931 }
4932 if (ret)
4933 break;
4934
4935 ret = -1;
4936
4937 pr_info(" read events: %ld\n", total_read);
4938 pr_info(" lost events: %ld\n", total_lost);
4939 pr_info(" total events: %ld\n", total_lost + total_read);
4940 pr_info(" recorded len bytes: %ld\n", total_len);
4941 pr_info(" recorded size bytes: %ld\n", total_size);
4942 if (total_lost)
4943 pr_info(" With dropped events, record len and size may not match\n"
4944 " alloced and written from above\n");
4945 if (!total_lost) {
4946 if (RB_WARN_ON(buffer, total_len != total_alloc ||
4947 total_size != total_written))
4948 break;
4949 }
4950 if (RB_WARN_ON(buffer, total_lost + total_read != total_events))
4951 break;
4952
4953 ret = 0;
4954 }
4955 if (!ret)
4956 pr_info("Ring buffer PASSED!\n");
4957
4958 ring_buffer_free(buffer);
4959 return 0;
4960}
4961
4962late_initcall(test_ringbuffer);
4963#endif /* CONFIG_RING_BUFFER_STARTUP_TEST */