blob: b5ed553e0a45f9a4faf3b74dacfc4fa18842bdd6 [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 Rostedt (Red Hat)af658dc2015-04-29 14:36:05 -04006#include <linux/trace_events.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/uaccess.h>
Steven Rostedta81bd802009-02-06 01:45:16 -050013#include <linux/hardirq.h>
Steven Rostedt (Red Hat)6c43e552013-03-15 11:32:53 -040014#include <linux/kthread.h> /* for self test */
Vegard Nossum1744a212009-02-28 08:29:44 +010015#include <linux/kmemcheck.h>
Steven Rostedt7a8e76a2008-09-29 23:02:38 -040016#include <linux/module.h>
17#include <linux/percpu.h>
18#include <linux/mutex.h>
Steven Rostedt (Red Hat)6c43e552013-03-15 11:32:53 -040019#include <linux/delay.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090020#include <linux/slab.h>
Steven Rostedt7a8e76a2008-09-29 23:02:38 -040021#include <linux/init.h>
22#include <linux/hash.h>
23#include <linux/list.h>
Steven Rostedt554f7862009-03-11 22:00:13 -040024#include <linux/cpu.h>
Steven Rostedt7a8e76a2008-09-29 23:02:38 -040025
Christoph Lameter79615762010-01-05 15:34:50 +090026#include <asm/local.h>
Steven Rostedt182e9f52008-11-03 23:15:56 -050027
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -070028static void update_pages_handler(struct work_struct *work);
29
Steven Rostedt033601a2008-11-21 12:41:55 -050030/*
Steven Rostedtd1b182a2009-04-15 16:53:47 -040031 * The ring buffer header is special. We must manually up keep it.
32 */
33int ring_buffer_print_entry_header(struct trace_seq *s)
34{
Steven Rostedt (Red Hat)c0cd93a2014-11-12 11:49:00 -050035 trace_seq_puts(s, "# compressed entry header\n");
36 trace_seq_puts(s, "\ttype_len : 5 bits\n");
37 trace_seq_puts(s, "\ttime_delta : 27 bits\n");
38 trace_seq_puts(s, "\tarray : 32 bits\n");
39 trace_seq_putc(s, '\n');
40 trace_seq_printf(s, "\tpadding : type == %d\n",
41 RINGBUF_TYPE_PADDING);
42 trace_seq_printf(s, "\ttime_extend : type == %d\n",
43 RINGBUF_TYPE_TIME_EXTEND);
44 trace_seq_printf(s, "\tdata max type_len == %d\n",
45 RINGBUF_TYPE_DATA_TYPE_LEN_MAX);
Steven Rostedtd1b182a2009-04-15 16:53:47 -040046
Steven Rostedt (Red Hat)c0cd93a2014-11-12 11:49:00 -050047 return !trace_seq_has_overflowed(s);
Steven Rostedtd1b182a2009-04-15 16:53:47 -040048}
49
50/*
Steven Rostedt5cc98542009-03-12 22:24:17 -040051 * The ring buffer is made up of a list of pages. A separate list of pages is
52 * allocated for each CPU. A writer may only write to a buffer that is
53 * associated with the CPU it is currently executing on. A reader may read
54 * from any per cpu buffer.
55 *
56 * The reader is special. For each per cpu buffer, the reader has its own
57 * reader page. When a reader has read the entire reader page, this reader
58 * page is swapped with another page in the ring buffer.
59 *
60 * Now, as long as the writer is off the reader page, the reader can do what
61 * ever it wants with that page. The writer will never write to that page
62 * again (as long as it is out of the ring buffer).
63 *
64 * Here's some silly ASCII art.
65 *
66 * +------+
67 * |reader| RING BUFFER
68 * |page |
69 * +------+ +---+ +---+ +---+
70 * | |-->| |-->| |
71 * +---+ +---+ +---+
72 * ^ |
73 * | |
74 * +---------------+
75 *
76 *
77 * +------+
78 * |reader| RING BUFFER
79 * |page |------------------v
80 * +------+ +---+ +---+ +---+
81 * | |-->| |-->| |
82 * +---+ +---+ +---+
83 * ^ |
84 * | |
85 * +---------------+
86 *
87 *
88 * +------+
89 * |reader| RING BUFFER
90 * |page |------------------v
91 * +------+ +---+ +---+ +---+
92 * ^ | |-->| |-->| |
93 * | +---+ +---+ +---+
94 * | |
95 * | |
96 * +------------------------------+
97 *
98 *
99 * +------+
100 * |buffer| RING BUFFER
101 * |page |------------------v
102 * +------+ +---+ +---+ +---+
103 * ^ | | | |-->| |
104 * | New +---+ +---+ +---+
105 * | Reader------^ |
106 * | page |
107 * +------------------------------+
108 *
109 *
110 * After we make this swap, the reader can hand this page off to the splice
111 * code and be done with it. It can even allocate a new page if it needs to
112 * and swap that into the ring buffer.
113 *
114 * We will be using cmpxchg soon to make all this lockless.
115 *
116 */
117
Steven Rostedt499e5472012-02-22 15:50:28 -0500118/* Used for individual buffers (after the counter) */
119#define RB_BUFFER_OFF (1 << 20)
120
Steven Rostedt474d32b2009-03-03 19:51:40 -0500121#define BUF_PAGE_HDR_SIZE offsetof(struct buffer_data_page, data)
122
Steven Rostedte3d6bf02009-03-03 13:53:07 -0500123#define RB_EVNT_HDR_SIZE (offsetof(struct ring_buffer_event, array))
Andrew Morton67d34722009-01-09 12:27:09 -0800124#define RB_ALIGNMENT 4U
Lai Jiangshan334d4162009-04-24 11:27:05 +0800125#define RB_MAX_SMALL_DATA (RB_ALIGNMENT * RINGBUF_TYPE_DATA_TYPE_LEN_MAX)
Steven Rostedtc7b09302009-06-11 11:12:00 -0400126#define RB_EVNT_MIN_SIZE 8U /* two 32bit words */
Lai Jiangshan334d4162009-04-24 11:27:05 +0800127
James Hogan649508f2012-05-30 12:11:19 +0100128#ifndef CONFIG_HAVE_64BIT_ALIGNED_ACCESS
Steven Rostedt22710482010-03-18 17:54:19 -0400129# define RB_FORCE_8BYTE_ALIGNMENT 0
130# define RB_ARCH_ALIGNMENT RB_ALIGNMENT
131#else
132# define RB_FORCE_8BYTE_ALIGNMENT 1
133# define RB_ARCH_ALIGNMENT 8U
134#endif
135
James Hogan649508f2012-05-30 12:11:19 +0100136#define RB_ALIGN_DATA __aligned(RB_ARCH_ALIGNMENT)
137
Lai Jiangshan334d4162009-04-24 11:27:05 +0800138/* define RINGBUF_TYPE_DATA for 'case RINGBUF_TYPE_DATA:' */
139#define RINGBUF_TYPE_DATA 0 ... RINGBUF_TYPE_DATA_TYPE_LEN_MAX
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400140
141enum {
142 RB_LEN_TIME_EXTEND = 8,
143 RB_LEN_TIME_STAMP = 16,
144};
145
Steven Rostedt69d1b832010-10-07 18:18:05 -0400146#define skip_time_extend(event) \
147 ((struct ring_buffer_event *)((char *)event + RB_LEN_TIME_EXTEND))
148
Tom Zanussi2d622712009-03-22 03:30:49 -0500149static inline int rb_null_event(struct ring_buffer_event *event)
150{
Steven Rostedta1863c22009-09-03 10:23:58 -0400151 return event->type_len == RINGBUF_TYPE_PADDING && !event->time_delta;
Tom Zanussi2d622712009-03-22 03:30:49 -0500152}
153
154static void rb_event_set_padding(struct ring_buffer_event *event)
155{
Steven Rostedta1863c22009-09-03 10:23:58 -0400156 /* padding has a NULL time_delta */
Lai Jiangshan334d4162009-04-24 11:27:05 +0800157 event->type_len = RINGBUF_TYPE_PADDING;
Tom Zanussi2d622712009-03-22 03:30:49 -0500158 event->time_delta = 0;
159}
160
Tom Zanussi2d622712009-03-22 03:30:49 -0500161static unsigned
162rb_event_data_length(struct ring_buffer_event *event)
163{
164 unsigned length;
165
Lai Jiangshan334d4162009-04-24 11:27:05 +0800166 if (event->type_len)
167 length = event->type_len * RB_ALIGNMENT;
Tom Zanussi2d622712009-03-22 03:30:49 -0500168 else
169 length = event->array[0];
170 return length + RB_EVNT_HDR_SIZE;
171}
172
Steven Rostedt69d1b832010-10-07 18:18:05 -0400173/*
174 * Return the length of the given event. Will return
175 * the length of the time extend if the event is a
176 * time extend.
177 */
178static inline unsigned
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400179rb_event_length(struct ring_buffer_event *event)
180{
Lai Jiangshan334d4162009-04-24 11:27:05 +0800181 switch (event->type_len) {
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400182 case RINGBUF_TYPE_PADDING:
Tom Zanussi2d622712009-03-22 03:30:49 -0500183 if (rb_null_event(event))
184 /* undefined */
185 return -1;
Lai Jiangshan334d4162009-04-24 11:27:05 +0800186 return event->array[0] + RB_EVNT_HDR_SIZE;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400187
188 case RINGBUF_TYPE_TIME_EXTEND:
189 return RB_LEN_TIME_EXTEND;
190
191 case RINGBUF_TYPE_TIME_STAMP:
192 return RB_LEN_TIME_STAMP;
193
194 case RINGBUF_TYPE_DATA:
Tom Zanussi2d622712009-03-22 03:30:49 -0500195 return rb_event_data_length(event);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400196 default:
197 BUG();
198 }
199 /* not hit */
200 return 0;
201}
202
Steven Rostedt69d1b832010-10-07 18:18:05 -0400203/*
204 * Return total length of time extend and data,
205 * or just the event length for all other events.
206 */
207static inline unsigned
208rb_event_ts_length(struct ring_buffer_event *event)
209{
210 unsigned len = 0;
211
212 if (event->type_len == RINGBUF_TYPE_TIME_EXTEND) {
213 /* time extends include the data event after it */
214 len = RB_LEN_TIME_EXTEND;
215 event = skip_time_extend(event);
216 }
217 return len + rb_event_length(event);
218}
219
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400220/**
221 * ring_buffer_event_length - return the length of the event
222 * @event: the event to get the length of
Steven Rostedt69d1b832010-10-07 18:18:05 -0400223 *
224 * Returns the size of the data load of a data event.
225 * If the event is something other than a data event, it
226 * returns the size of the event itself. With the exception
227 * of a TIME EXTEND, where it still returns the size of the
228 * data load of the data event after it.
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400229 */
230unsigned ring_buffer_event_length(struct ring_buffer_event *event)
231{
Steven Rostedt69d1b832010-10-07 18:18:05 -0400232 unsigned length;
233
234 if (event->type_len == RINGBUF_TYPE_TIME_EXTEND)
235 event = skip_time_extend(event);
236
237 length = rb_event_length(event);
Lai Jiangshan334d4162009-04-24 11:27:05 +0800238 if (event->type_len > RINGBUF_TYPE_DATA_TYPE_LEN_MAX)
Robert Richter465634a2009-01-07 15:32:11 +0100239 return length;
240 length -= RB_EVNT_HDR_SIZE;
241 if (length > RB_MAX_SMALL_DATA + sizeof(event->array[0]))
242 length -= sizeof(event->array[0]);
243 return length;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400244}
Robert Richterc4f50182008-12-11 16:49:22 +0100245EXPORT_SYMBOL_GPL(ring_buffer_event_length);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400246
247/* inline for ring buffer fast paths */
Andrew Morton34a148b2009-01-09 12:27:09 -0800248static void *
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400249rb_event_data(struct ring_buffer_event *event)
250{
Steven Rostedt69d1b832010-10-07 18:18:05 -0400251 if (event->type_len == RINGBUF_TYPE_TIME_EXTEND)
252 event = skip_time_extend(event);
Lai Jiangshan334d4162009-04-24 11:27:05 +0800253 BUG_ON(event->type_len > RINGBUF_TYPE_DATA_TYPE_LEN_MAX);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400254 /* If length is in len field, then array[0] has the data */
Lai Jiangshan334d4162009-04-24 11:27:05 +0800255 if (event->type_len)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400256 return (void *)&event->array[0];
257 /* Otherwise length is in array[0] and array[1] has the data */
258 return (void *)&event->array[1];
259}
260
261/**
262 * ring_buffer_event_data - return the data of the event
263 * @event: the event to get the data from
264 */
265void *ring_buffer_event_data(struct ring_buffer_event *event)
266{
267 return rb_event_data(event);
268}
Robert Richterc4f50182008-12-11 16:49:22 +0100269EXPORT_SYMBOL_GPL(ring_buffer_event_data);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400270
271#define for_each_buffer_cpu(buffer, cpu) \
Rusty Russell9e01c1b2009-01-01 10:12:22 +1030272 for_each_cpu(cpu, buffer->cpumask)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400273
274#define TS_SHIFT 27
275#define TS_MASK ((1ULL << TS_SHIFT) - 1)
276#define TS_DELTA_TEST (~TS_MASK)
277
Steven Rostedt66a8cb92010-03-31 13:21:56 -0400278/* Flag when events were overwritten */
279#define RB_MISSED_EVENTS (1 << 31)
Steven Rostedtff0ff842010-03-31 22:11:42 -0400280/* Missed count stored at end */
281#define RB_MISSED_STORED (1 << 30)
Steven Rostedt66a8cb92010-03-31 13:21:56 -0400282
Steven Rostedtabc9b562008-12-02 15:34:06 -0500283struct buffer_data_page {
Steven Rostedte4c2ce82008-10-01 11:14:54 -0400284 u64 time_stamp; /* page time stamp */
Wenji Huangc3706f02009-02-10 01:03:18 -0500285 local_t commit; /* write committed index */
James Hogan649508f2012-05-30 12:11:19 +0100286 unsigned char data[] RB_ALIGN_DATA; /* data of buffer page */
Steven Rostedtabc9b562008-12-02 15:34:06 -0500287};
288
Steven Rostedt77ae3652009-03-27 11:00:29 -0400289/*
290 * Note, the buffer_page list must be first. The buffer pages
291 * are allocated in cache lines, which means that each buffer
292 * page will be at the beginning of a cache line, and thus
293 * the least significant bits will be zero. We use this to
294 * add flags in the list struct pointers, to make the ring buffer
295 * lockless.
296 */
Steven Rostedtabc9b562008-12-02 15:34:06 -0500297struct buffer_page {
Steven Rostedt778c55d2009-05-01 18:44:45 -0400298 struct list_head list; /* list of buffer pages */
Steven Rostedtabc9b562008-12-02 15:34:06 -0500299 local_t write; /* index for next write */
Steven Rostedt6f807ac2008-10-04 02:00:58 -0400300 unsigned read; /* index for next read */
Steven Rostedt778c55d2009-05-01 18:44:45 -0400301 local_t entries; /* entries on this page */
Steven Rostedtff0ff842010-03-31 22:11:42 -0400302 unsigned long real_end; /* real end of data */
Steven Rostedtabc9b562008-12-02 15:34:06 -0500303 struct buffer_data_page *page; /* Actual data page */
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400304};
305
Steven Rostedt77ae3652009-03-27 11:00:29 -0400306/*
307 * The buffer page counters, write and entries, must be reset
308 * atomically when crossing page boundaries. To synchronize this
309 * update, two counters are inserted into the number. One is
310 * the actual counter for the write position or count on the page.
311 *
312 * The other is a counter of updaters. Before an update happens
313 * the update partition of the counter is incremented. This will
314 * allow the updater to update the counter atomically.
315 *
316 * The counter is 20 bits, and the state data is 12.
317 */
318#define RB_WRITE_MASK 0xfffff
319#define RB_WRITE_INTCNT (1 << 20)
320
Steven Rostedt044fa782008-12-02 23:50:03 -0500321static void rb_init_page(struct buffer_data_page *bpage)
Steven Rostedtabc9b562008-12-02 15:34:06 -0500322{
Steven Rostedt044fa782008-12-02 23:50:03 -0500323 local_set(&bpage->commit, 0);
Steven Rostedtabc9b562008-12-02 15:34:06 -0500324}
325
Steven Rostedt474d32b2009-03-03 19:51:40 -0500326/**
327 * ring_buffer_page_len - the size of data on the page.
328 * @page: The page to read
329 *
330 * Returns the amount of data on the page, including buffer page header.
331 */
Steven Rostedtef7a4a12009-03-03 00:27:49 -0500332size_t ring_buffer_page_len(void *page)
333{
Steven Rostedt474d32b2009-03-03 19:51:40 -0500334 return local_read(&((struct buffer_data_page *)page)->commit)
335 + BUF_PAGE_HDR_SIZE;
Steven Rostedtef7a4a12009-03-03 00:27:49 -0500336}
337
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400338/*
Steven Rostedted568292008-09-29 23:02:40 -0400339 * Also stolen from mm/slob.c. Thanks to Mathieu Desnoyers for pointing
340 * this issue out.
341 */
Andrew Morton34a148b2009-01-09 12:27:09 -0800342static void free_buffer_page(struct buffer_page *bpage)
Steven Rostedted568292008-09-29 23:02:40 -0400343{
Andrew Morton34a148b2009-01-09 12:27:09 -0800344 free_page((unsigned long)bpage->page);
Steven Rostedte4c2ce82008-10-01 11:14:54 -0400345 kfree(bpage);
Steven Rostedted568292008-09-29 23:02:40 -0400346}
347
348/*
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400349 * We need to fit the time_stamp delta into 27 bits.
350 */
351static inline int test_time_stamp(u64 delta)
352{
353 if (delta & TS_DELTA_TEST)
354 return 1;
355 return 0;
356}
357
Steven Rostedt474d32b2009-03-03 19:51:40 -0500358#define BUF_PAGE_SIZE (PAGE_SIZE - BUF_PAGE_HDR_SIZE)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400359
Steven Rostedtbe957c42009-05-11 14:42:53 -0400360/* Max payload is BUF_PAGE_SIZE - header (8bytes) */
361#define BUF_MAX_DATA_SIZE (BUF_PAGE_SIZE - (sizeof(u32) * 2))
362
Steven Rostedtd1b182a2009-04-15 16:53:47 -0400363int ring_buffer_print_page_header(struct trace_seq *s)
364{
365 struct buffer_data_page field;
Steven Rostedtd1b182a2009-04-15 16:53:47 -0400366
Steven Rostedt (Red Hat)c0cd93a2014-11-12 11:49:00 -0500367 trace_seq_printf(s, "\tfield: u64 timestamp;\t"
368 "offset:0;\tsize:%u;\tsigned:%u;\n",
369 (unsigned int)sizeof(field.time_stamp),
370 (unsigned int)is_signed_type(u64));
Steven Rostedtd1b182a2009-04-15 16:53:47 -0400371
Steven Rostedt (Red Hat)c0cd93a2014-11-12 11:49:00 -0500372 trace_seq_printf(s, "\tfield: local_t commit;\t"
373 "offset:%u;\tsize:%u;\tsigned:%u;\n",
374 (unsigned int)offsetof(typeof(field), commit),
375 (unsigned int)sizeof(field.commit),
376 (unsigned int)is_signed_type(long));
Steven Rostedtd1b182a2009-04-15 16:53:47 -0400377
Steven Rostedt (Red Hat)c0cd93a2014-11-12 11:49:00 -0500378 trace_seq_printf(s, "\tfield: int overwrite;\t"
379 "offset:%u;\tsize:%u;\tsigned:%u;\n",
380 (unsigned int)offsetof(typeof(field), commit),
381 1,
382 (unsigned int)is_signed_type(long));
Steven Rostedt66a8cb92010-03-31 13:21:56 -0400383
Steven Rostedt (Red Hat)c0cd93a2014-11-12 11:49:00 -0500384 trace_seq_printf(s, "\tfield: char data;\t"
385 "offset:%u;\tsize:%u;\tsigned:%u;\n",
386 (unsigned int)offsetof(typeof(field), data),
387 (unsigned int)BUF_PAGE_SIZE,
388 (unsigned int)is_signed_type(char));
Steven Rostedtd1b182a2009-04-15 16:53:47 -0400389
Steven Rostedt (Red Hat)c0cd93a2014-11-12 11:49:00 -0500390 return !trace_seq_has_overflowed(s);
Steven Rostedtd1b182a2009-04-15 16:53:47 -0400391}
392
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -0500393struct rb_irq_work {
394 struct irq_work work;
395 wait_queue_head_t waiters;
Steven Rostedt (Red Hat)1e0d6712015-02-10 22:14:53 -0500396 wait_queue_head_t full_waiters;
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -0500397 bool waiters_pending;
Steven Rostedt (Red Hat)1e0d6712015-02-10 22:14:53 -0500398 bool full_waiters_pending;
399 bool wakeup_full;
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -0500400};
401
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400402/*
Steven Rostedt (Red Hat)fcc742e2015-05-28 17:13:14 -0400403 * Structure to hold event state and handle nested events.
404 */
405struct rb_event_info {
406 u64 ts;
407 u64 delta;
408 unsigned long length;
409 struct buffer_page *tail_page;
410 int add_timestamp;
411};
412
413/*
Steven Rostedt (Red Hat)a497adb2015-05-29 10:32:28 -0400414 * Used for which event context the event is in.
415 * NMI = 0
416 * IRQ = 1
417 * SOFTIRQ = 2
418 * NORMAL = 3
419 *
420 * See trace_recursive_lock() comment below for more details.
421 */
422enum {
423 RB_CTX_NMI,
424 RB_CTX_IRQ,
425 RB_CTX_SOFTIRQ,
426 RB_CTX_NORMAL,
427 RB_CTX_MAX
428};
429
430/*
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400431 * head_page == tail_page && head == tail then buffer is empty.
432 */
433struct ring_buffer_per_cpu {
434 int cpu;
Richard Kennedy985023d2010-03-25 11:27:36 +0000435 atomic_t record_disabled;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400436 struct ring_buffer *buffer;
Thomas Gleixner5389f6f2009-07-25 17:13:33 +0200437 raw_spinlock_t reader_lock; /* serialize readers */
Thomas Gleixner445c8952009-12-02 19:49:50 +0100438 arch_spinlock_t lock;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400439 struct lock_class_key lock_key;
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -0800440 unsigned int nr_pages;
Steven Rostedt (Red Hat)58a09ec2015-05-27 10:27:47 -0400441 unsigned int current_context;
Steven Rostedt3adc54f2009-03-30 15:32:01 -0400442 struct list_head *pages;
Steven Rostedt6f807ac2008-10-04 02:00:58 -0400443 struct buffer_page *head_page; /* read from head */
444 struct buffer_page *tail_page; /* write to tail */
Wenji Huangc3706f02009-02-10 01:03:18 -0500445 struct buffer_page *commit_page; /* committed pages */
Steven Rostedtd7690412008-10-01 00:29:53 -0400446 struct buffer_page *reader_page;
Steven Rostedt66a8cb92010-03-31 13:21:56 -0400447 unsigned long lost_events;
448 unsigned long last_overrun;
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -0700449 local_t entries_bytes;
Steven Rostedte4906ef2009-04-30 20:49:44 -0400450 local_t entries;
Slava Pestov884bfe82011-07-15 14:23:58 -0700451 local_t overrun;
452 local_t commit_overrun;
453 local_t dropped_events;
Steven Rostedtfa743952009-06-16 12:37:57 -0400454 local_t committing;
455 local_t commits;
Steven Rostedt77ae3652009-03-27 11:00:29 -0400456 unsigned long read;
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -0700457 unsigned long read_bytes;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400458 u64 write_stamp;
459 u64 read_stamp;
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -0800460 /* ring buffer pages to update, > 0 to add, < 0 to remove */
461 int nr_pages_to_update;
462 struct list_head new_pages; /* new pages to add */
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -0700463 struct work_struct update_pages_work;
Vaibhav Nagarnaik05fdd702012-05-18 13:29:51 -0700464 struct completion update_done;
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -0500465
466 struct rb_irq_work irq_work;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400467};
468
469struct ring_buffer {
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400470 unsigned flags;
471 int cpus;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400472 atomic_t record_disabled;
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -0700473 atomic_t resize_disabled;
Arnaldo Carvalho de Melo00f62f62009-02-09 17:04:06 -0200474 cpumask_var_t cpumask;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400475
Peter Zijlstra1f8a6a12009-06-08 18:18:39 +0200476 struct lock_class_key *reader_lock_key;
477
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400478 struct mutex mutex;
479
480 struct ring_buffer_per_cpu **buffers;
Steven Rostedt554f7862009-03-11 22:00:13 -0400481
Steven Rostedt59222ef2009-03-12 11:46:03 -0400482#ifdef CONFIG_HOTPLUG_CPU
Steven Rostedt554f7862009-03-11 22:00:13 -0400483 struct notifier_block cpu_notify;
484#endif
Steven Rostedt37886f62009-03-17 17:22:06 -0400485 u64 (*clock)(void);
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -0500486
487 struct rb_irq_work irq_work;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400488};
489
490struct ring_buffer_iter {
491 struct ring_buffer_per_cpu *cpu_buffer;
492 unsigned long head;
493 struct buffer_page *head_page;
Steven Rostedt492a74f2010-01-25 15:17:47 -0500494 struct buffer_page *cache_reader_page;
495 unsigned long cache_read;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400496 u64 read_stamp;
497};
498
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -0500499/*
500 * rb_wake_up_waiters - wake up tasks waiting for ring buffer input
501 *
502 * Schedules a delayed work to wake up any task that is blocked on the
503 * ring buffer waiters queue.
504 */
505static void rb_wake_up_waiters(struct irq_work *work)
506{
507 struct rb_irq_work *rbwork = container_of(work, struct rb_irq_work, work);
508
509 wake_up_all(&rbwork->waiters);
Steven Rostedt (Red Hat)1e0d6712015-02-10 22:14:53 -0500510 if (rbwork->wakeup_full) {
511 rbwork->wakeup_full = false;
512 wake_up_all(&rbwork->full_waiters);
513 }
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -0500514}
515
516/**
517 * ring_buffer_wait - wait for input to the ring buffer
518 * @buffer: buffer to wait on
519 * @cpu: the cpu buffer to wait on
Rabin Vincente30f53a2014-11-10 19:46:34 +0100520 * @full: wait until a full page is available, if @cpu != RING_BUFFER_ALL_CPUS
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -0500521 *
522 * If @cpu == RING_BUFFER_ALL_CPUS then the task will wake up as soon
523 * as data is added to any of the @buffer's cpu buffers. Otherwise
524 * it will wait for data to be added to a specific cpu buffer.
525 */
Rabin Vincente30f53a2014-11-10 19:46:34 +0100526int ring_buffer_wait(struct ring_buffer *buffer, int cpu, bool full)
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -0500527{
Rabin Vincente30f53a2014-11-10 19:46:34 +0100528 struct ring_buffer_per_cpu *uninitialized_var(cpu_buffer);
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -0500529 DEFINE_WAIT(wait);
530 struct rb_irq_work *work;
Rabin Vincente30f53a2014-11-10 19:46:34 +0100531 int ret = 0;
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -0500532
533 /*
534 * Depending on what the caller is waiting for, either any
535 * data in any cpu buffer, or a specific buffer, put the
536 * caller on the appropriate wait queue.
537 */
Steven Rostedt (Red Hat)1e0d6712015-02-10 22:14:53 -0500538 if (cpu == RING_BUFFER_ALL_CPUS) {
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -0500539 work = &buffer->irq_work;
Steven Rostedt (Red Hat)1e0d6712015-02-10 22:14:53 -0500540 /* Full only makes sense on per cpu reads */
541 full = false;
542 } else {
Steven Rostedt (Red Hat)8b8b3682014-06-10 09:46:00 -0400543 if (!cpumask_test_cpu(cpu, buffer->cpumask))
544 return -ENODEV;
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -0500545 cpu_buffer = buffer->buffers[cpu];
546 work = &cpu_buffer->irq_work;
547 }
548
549
Rabin Vincente30f53a2014-11-10 19:46:34 +0100550 while (true) {
Steven Rostedt (Red Hat)1e0d6712015-02-10 22:14:53 -0500551 if (full)
552 prepare_to_wait(&work->full_waiters, &wait, TASK_INTERRUPTIBLE);
553 else
554 prepare_to_wait(&work->waiters, &wait, TASK_INTERRUPTIBLE);
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -0500555
Rabin Vincente30f53a2014-11-10 19:46:34 +0100556 /*
557 * The events can happen in critical sections where
558 * checking a work queue can cause deadlocks.
559 * After adding a task to the queue, this flag is set
560 * only to notify events to try to wake up the queue
561 * using irq_work.
562 *
563 * We don't clear it even if the buffer is no longer
564 * empty. The flag only causes the next event to run
565 * irq_work to do the work queue wake up. The worse
566 * that can happen if we race with !trace_empty() is that
567 * an event will cause an irq_work to try to wake up
568 * an empty queue.
569 *
570 * There's no reason to protect this flag either, as
571 * the work queue and irq_work logic will do the necessary
572 * synchronization for the wake ups. The only thing
573 * that is necessary is that the wake up happens after
574 * a task has been queued. It's OK for spurious wake ups.
575 */
Steven Rostedt (Red Hat)1e0d6712015-02-10 22:14:53 -0500576 if (full)
577 work->full_waiters_pending = true;
578 else
579 work->waiters_pending = true;
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -0500580
Rabin Vincente30f53a2014-11-10 19:46:34 +0100581 if (signal_pending(current)) {
582 ret = -EINTR;
583 break;
584 }
585
586 if (cpu == RING_BUFFER_ALL_CPUS && !ring_buffer_empty(buffer))
587 break;
588
589 if (cpu != RING_BUFFER_ALL_CPUS &&
590 !ring_buffer_empty_cpu(buffer, cpu)) {
591 unsigned long flags;
592 bool pagebusy;
593
594 if (!full)
595 break;
596
597 raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
598 pagebusy = cpu_buffer->reader_page == cpu_buffer->commit_page;
599 raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
600
601 if (!pagebusy)
602 break;
603 }
604
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -0500605 schedule();
Rabin Vincente30f53a2014-11-10 19:46:34 +0100606 }
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -0500607
Steven Rostedt (Red Hat)1e0d6712015-02-10 22:14:53 -0500608 if (full)
609 finish_wait(&work->full_waiters, &wait);
610 else
611 finish_wait(&work->waiters, &wait);
Rabin Vincente30f53a2014-11-10 19:46:34 +0100612
613 return ret;
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -0500614}
615
616/**
617 * ring_buffer_poll_wait - poll on buffer input
618 * @buffer: buffer to wait on
619 * @cpu: the cpu buffer to wait on
620 * @filp: the file descriptor
621 * @poll_table: The poll descriptor
622 *
623 * If @cpu == RING_BUFFER_ALL_CPUS then the task will wake up as soon
624 * as data is added to any of the @buffer's cpu buffers. Otherwise
625 * it will wait for data to be added to a specific cpu buffer.
626 *
627 * Returns POLLIN | POLLRDNORM if data exists in the buffers,
628 * zero otherwise.
629 */
630int ring_buffer_poll_wait(struct ring_buffer *buffer, int cpu,
631 struct file *filp, poll_table *poll_table)
632{
633 struct ring_buffer_per_cpu *cpu_buffer;
634 struct rb_irq_work *work;
635
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -0500636 if (cpu == RING_BUFFER_ALL_CPUS)
637 work = &buffer->irq_work;
638 else {
Steven Rostedt (Red Hat)6721cb62013-05-23 14:21:36 -0400639 if (!cpumask_test_cpu(cpu, buffer->cpumask))
640 return -EINVAL;
641
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -0500642 cpu_buffer = buffer->buffers[cpu];
643 work = &cpu_buffer->irq_work;
644 }
645
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -0500646 poll_wait(filp, &work->waiters, poll_table);
Josef Bacik4ce97db2014-08-25 13:59:41 -0400647 work->waiters_pending = true;
648 /*
649 * There's a tight race between setting the waiters_pending and
650 * checking if the ring buffer is empty. Once the waiters_pending bit
651 * is set, the next event will wake the task up, but we can get stuck
652 * if there's only a single event in.
653 *
654 * FIXME: Ideally, we need a memory barrier on the writer side as well,
655 * but adding a memory barrier to all events will cause too much of a
656 * performance hit in the fast path. We only need a memory barrier when
657 * the buffer goes from empty to having content. But as this race is
658 * extremely small, and it's not a problem if another event comes in, we
659 * will fix it later.
660 */
661 smp_mb();
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -0500662
663 if ((cpu == RING_BUFFER_ALL_CPUS && !ring_buffer_empty(buffer)) ||
664 (cpu != RING_BUFFER_ALL_CPUS && !ring_buffer_empty_cpu(buffer, cpu)))
665 return POLLIN | POLLRDNORM;
666 return 0;
667}
668
Steven Rostedtf536aaf2008-11-10 23:07:30 -0500669/* buffer may be either ring_buffer or ring_buffer_per_cpu */
Steven Rostedt077c5402009-09-03 19:53:46 -0400670#define RB_WARN_ON(b, cond) \
671 ({ \
672 int _____ret = unlikely(cond); \
673 if (_____ret) { \
674 if (__same_type(*(b), struct ring_buffer_per_cpu)) { \
675 struct ring_buffer_per_cpu *__b = \
676 (void *)b; \
677 atomic_inc(&__b->buffer->record_disabled); \
678 } else \
679 atomic_inc(&b->record_disabled); \
680 WARN_ON(1); \
681 } \
682 _____ret; \
Steven Rostedt3e89c7b2008-11-11 15:28:41 -0500683 })
Steven Rostedtf536aaf2008-11-10 23:07:30 -0500684
Steven Rostedt37886f62009-03-17 17:22:06 -0400685/* Up this if you want to test the TIME_EXTENTS and normalization */
686#define DEBUG_SHIFT 0
687
Jiri Olsa6d3f1e12009-10-23 19:36:19 -0400688static inline u64 rb_time_stamp(struct ring_buffer *buffer)
Steven Rostedt88eb0122009-05-11 16:28:23 -0400689{
690 /* shift to debug/test normalization and TIME_EXTENTS */
691 return buffer->clock() << DEBUG_SHIFT;
692}
693
Steven Rostedt37886f62009-03-17 17:22:06 -0400694u64 ring_buffer_time_stamp(struct ring_buffer *buffer, int cpu)
695{
696 u64 time;
697
698 preempt_disable_notrace();
Jiri Olsa6d3f1e12009-10-23 19:36:19 -0400699 time = rb_time_stamp(buffer);
Steven Rostedt37886f62009-03-17 17:22:06 -0400700 preempt_enable_no_resched_notrace();
701
702 return time;
703}
704EXPORT_SYMBOL_GPL(ring_buffer_time_stamp);
705
706void ring_buffer_normalize_time_stamp(struct ring_buffer *buffer,
707 int cpu, u64 *ts)
708{
709 /* Just stupid testing the normalize function and deltas */
710 *ts >>= DEBUG_SHIFT;
711}
712EXPORT_SYMBOL_GPL(ring_buffer_normalize_time_stamp);
713
Steven Rostedt77ae3652009-03-27 11:00:29 -0400714/*
715 * Making the ring buffer lockless makes things tricky.
716 * Although writes only happen on the CPU that they are on,
717 * and they only need to worry about interrupts. Reads can
718 * happen on any CPU.
719 *
720 * The reader page is always off the ring buffer, but when the
721 * reader finishes with a page, it needs to swap its page with
722 * a new one from the buffer. The reader needs to take from
723 * the head (writes go to the tail). But if a writer is in overwrite
724 * mode and wraps, it must push the head page forward.
725 *
726 * Here lies the problem.
727 *
728 * The reader must be careful to replace only the head page, and
729 * not another one. As described at the top of the file in the
730 * ASCII art, the reader sets its old page to point to the next
731 * page after head. It then sets the page after head to point to
732 * the old reader page. But if the writer moves the head page
733 * during this operation, the reader could end up with the tail.
734 *
735 * We use cmpxchg to help prevent this race. We also do something
736 * special with the page before head. We set the LSB to 1.
737 *
738 * When the writer must push the page forward, it will clear the
739 * bit that points to the head page, move the head, and then set
740 * the bit that points to the new head page.
741 *
742 * We also don't want an interrupt coming in and moving the head
743 * page on another writer. Thus we use the second LSB to catch
744 * that too. Thus:
745 *
746 * head->list->prev->next bit 1 bit 0
747 * ------- -------
748 * Normal page 0 0
749 * Points to head page 0 1
750 * New head page 1 0
751 *
752 * Note we can not trust the prev pointer of the head page, because:
753 *
754 * +----+ +-----+ +-----+
755 * | |------>| T |---X--->| N |
756 * | |<------| | | |
757 * +----+ +-----+ +-----+
758 * ^ ^ |
759 * | +-----+ | |
760 * +----------| R |----------+ |
761 * | |<-----------+
762 * +-----+
763 *
764 * Key: ---X--> HEAD flag set in pointer
765 * T Tail page
766 * R Reader page
767 * N Next page
768 *
769 * (see __rb_reserve_next() to see where this happens)
770 *
771 * What the above shows is that the reader just swapped out
772 * the reader page with a page in the buffer, but before it
773 * could make the new header point back to the new page added
774 * it was preempted by a writer. The writer moved forward onto
775 * the new page added by the reader and is about to move forward
776 * again.
777 *
778 * You can see, it is legitimate for the previous pointer of
779 * the head (or any page) not to point back to itself. But only
780 * temporarially.
781 */
782
783#define RB_PAGE_NORMAL 0UL
784#define RB_PAGE_HEAD 1UL
785#define RB_PAGE_UPDATE 2UL
786
787
788#define RB_FLAG_MASK 3UL
789
790/* PAGE_MOVED is not part of the mask */
791#define RB_PAGE_MOVED 4UL
792
793/*
794 * rb_list_head - remove any bit
795 */
796static struct list_head *rb_list_head(struct list_head *list)
797{
798 unsigned long val = (unsigned long)list;
799
800 return (struct list_head *)(val & ~RB_FLAG_MASK);
801}
802
803/*
Jiri Olsa6d3f1e12009-10-23 19:36:19 -0400804 * rb_is_head_page - test if the given page is the head page
Steven Rostedt77ae3652009-03-27 11:00:29 -0400805 *
806 * Because the reader may move the head_page pointer, we can
807 * not trust what the head page is (it may be pointing to
808 * the reader page). But if the next page is a header page,
809 * its flags will be non zero.
810 */
Jesper Juhl42b16b32011-01-17 00:09:38 +0100811static inline int
Steven Rostedt77ae3652009-03-27 11:00:29 -0400812rb_is_head_page(struct ring_buffer_per_cpu *cpu_buffer,
813 struct buffer_page *page, struct list_head *list)
814{
815 unsigned long val;
816
817 val = (unsigned long)list->next;
818
819 if ((val & ~RB_FLAG_MASK) != (unsigned long)&page->list)
820 return RB_PAGE_MOVED;
821
822 return val & RB_FLAG_MASK;
823}
824
825/*
826 * rb_is_reader_page
827 *
828 * The unique thing about the reader page, is that, if the
829 * writer is ever on it, the previous pointer never points
830 * back to the reader page.
831 */
832static int rb_is_reader_page(struct buffer_page *page)
833{
834 struct list_head *list = page->list.prev;
835
836 return rb_list_head(list->next) != &page->list;
837}
838
839/*
840 * rb_set_list_to_head - set a list_head to be pointing to head.
841 */
842static void rb_set_list_to_head(struct ring_buffer_per_cpu *cpu_buffer,
843 struct list_head *list)
844{
845 unsigned long *ptr;
846
847 ptr = (unsigned long *)&list->next;
848 *ptr |= RB_PAGE_HEAD;
849 *ptr &= ~RB_PAGE_UPDATE;
850}
851
852/*
853 * rb_head_page_activate - sets up head page
854 */
855static void rb_head_page_activate(struct ring_buffer_per_cpu *cpu_buffer)
856{
857 struct buffer_page *head;
858
859 head = cpu_buffer->head_page;
860 if (!head)
861 return;
862
863 /*
864 * Set the previous list pointer to have the HEAD flag.
865 */
866 rb_set_list_to_head(cpu_buffer, head->list.prev);
867}
868
869static void rb_list_head_clear(struct list_head *list)
870{
871 unsigned long *ptr = (unsigned long *)&list->next;
872
873 *ptr &= ~RB_FLAG_MASK;
874}
875
876/*
877 * rb_head_page_dactivate - clears head page ptr (for free list)
878 */
879static void
880rb_head_page_deactivate(struct ring_buffer_per_cpu *cpu_buffer)
881{
882 struct list_head *hd;
883
884 /* Go through the whole list and clear any pointers found. */
885 rb_list_head_clear(cpu_buffer->pages);
886
887 list_for_each(hd, cpu_buffer->pages)
888 rb_list_head_clear(hd);
889}
890
891static int rb_head_page_set(struct ring_buffer_per_cpu *cpu_buffer,
892 struct buffer_page *head,
893 struct buffer_page *prev,
894 int old_flag, int new_flag)
895{
896 struct list_head *list;
897 unsigned long val = (unsigned long)&head->list;
898 unsigned long ret;
899
900 list = &prev->list;
901
902 val &= ~RB_FLAG_MASK;
903
Steven Rostedt08a40812009-09-14 09:31:35 -0400904 ret = cmpxchg((unsigned long *)&list->next,
905 val | old_flag, val | new_flag);
Steven Rostedt77ae3652009-03-27 11:00:29 -0400906
907 /* check if the reader took the page */
908 if ((ret & ~RB_FLAG_MASK) != val)
909 return RB_PAGE_MOVED;
910
911 return ret & RB_FLAG_MASK;
912}
913
914static int rb_head_page_set_update(struct ring_buffer_per_cpu *cpu_buffer,
915 struct buffer_page *head,
916 struct buffer_page *prev,
917 int old_flag)
918{
919 return rb_head_page_set(cpu_buffer, head, prev,
920 old_flag, RB_PAGE_UPDATE);
921}
922
923static int rb_head_page_set_head(struct ring_buffer_per_cpu *cpu_buffer,
924 struct buffer_page *head,
925 struct buffer_page *prev,
926 int old_flag)
927{
928 return rb_head_page_set(cpu_buffer, head, prev,
929 old_flag, RB_PAGE_HEAD);
930}
931
932static int rb_head_page_set_normal(struct ring_buffer_per_cpu *cpu_buffer,
933 struct buffer_page *head,
934 struct buffer_page *prev,
935 int old_flag)
936{
937 return rb_head_page_set(cpu_buffer, head, prev,
938 old_flag, RB_PAGE_NORMAL);
939}
940
941static inline void rb_inc_page(struct ring_buffer_per_cpu *cpu_buffer,
942 struct buffer_page **bpage)
943{
944 struct list_head *p = rb_list_head((*bpage)->list.next);
945
946 *bpage = list_entry(p, struct buffer_page, list);
947}
948
949static struct buffer_page *
950rb_set_head_page(struct ring_buffer_per_cpu *cpu_buffer)
951{
952 struct buffer_page *head;
953 struct buffer_page *page;
954 struct list_head *list;
955 int i;
956
957 if (RB_WARN_ON(cpu_buffer, !cpu_buffer->head_page))
958 return NULL;
959
960 /* sanity check */
961 list = cpu_buffer->pages;
962 if (RB_WARN_ON(cpu_buffer, rb_list_head(list->prev->next) != list))
963 return NULL;
964
965 page = head = cpu_buffer->head_page;
966 /*
967 * It is possible that the writer moves the header behind
968 * where we started, and we miss in one loop.
969 * A second loop should grab the header, but we'll do
970 * three loops just because I'm paranoid.
971 */
972 for (i = 0; i < 3; i++) {
973 do {
974 if (rb_is_head_page(cpu_buffer, page, page->list.prev)) {
975 cpu_buffer->head_page = page;
976 return page;
977 }
978 rb_inc_page(cpu_buffer, &page);
979 } while (page != head);
980 }
981
982 RB_WARN_ON(cpu_buffer, 1);
983
984 return NULL;
985}
986
987static int rb_head_page_replace(struct buffer_page *old,
988 struct buffer_page *new)
989{
990 unsigned long *ptr = (unsigned long *)&old->list.prev->next;
991 unsigned long val;
992 unsigned long ret;
993
994 val = *ptr & ~RB_FLAG_MASK;
995 val |= RB_PAGE_HEAD;
996
Steven Rostedt08a40812009-09-14 09:31:35 -0400997 ret = cmpxchg(ptr, val, (unsigned long)&new->list);
Steven Rostedt77ae3652009-03-27 11:00:29 -0400998
999 return ret == val;
1000}
1001
1002/*
1003 * rb_tail_page_update - move the tail page forward
1004 *
1005 * Returns 1 if moved tail page, 0 if someone else did.
1006 */
1007static int rb_tail_page_update(struct ring_buffer_per_cpu *cpu_buffer,
1008 struct buffer_page *tail_page,
1009 struct buffer_page *next_page)
1010{
1011 struct buffer_page *old_tail;
1012 unsigned long old_entries;
1013 unsigned long old_write;
1014 int ret = 0;
1015
1016 /*
1017 * The tail page now needs to be moved forward.
1018 *
1019 * We need to reset the tail page, but without messing
1020 * with possible erasing of data brought in by interrupts
1021 * that have moved the tail page and are currently on it.
1022 *
1023 * We add a counter to the write field to denote this.
1024 */
1025 old_write = local_add_return(RB_WRITE_INTCNT, &next_page->write);
1026 old_entries = local_add_return(RB_WRITE_INTCNT, &next_page->entries);
1027
1028 /*
1029 * Just make sure we have seen our old_write and synchronize
1030 * with any interrupts that come in.
1031 */
1032 barrier();
1033
1034 /*
1035 * If the tail page is still the same as what we think
1036 * it is, then it is up to us to update the tail
1037 * pointer.
1038 */
1039 if (tail_page == cpu_buffer->tail_page) {
1040 /* Zero the write counter */
1041 unsigned long val = old_write & ~RB_WRITE_MASK;
1042 unsigned long eval = old_entries & ~RB_WRITE_MASK;
1043
1044 /*
1045 * This will only succeed if an interrupt did
1046 * not come in and change it. In which case, we
1047 * do not want to modify it.
Lai Jiangshanda706d82009-07-15 16:27:30 +08001048 *
1049 * We add (void) to let the compiler know that we do not care
1050 * about the return value of these functions. We use the
1051 * cmpxchg to only update if an interrupt did not already
1052 * do it for us. If the cmpxchg fails, we don't care.
Steven Rostedt77ae3652009-03-27 11:00:29 -04001053 */
Lai Jiangshanda706d82009-07-15 16:27:30 +08001054 (void)local_cmpxchg(&next_page->write, old_write, val);
1055 (void)local_cmpxchg(&next_page->entries, old_entries, eval);
Steven Rostedt77ae3652009-03-27 11:00:29 -04001056
1057 /*
1058 * No need to worry about races with clearing out the commit.
1059 * it only can increment when a commit takes place. But that
1060 * only happens in the outer most nested commit.
1061 */
1062 local_set(&next_page->page->commit, 0);
1063
1064 old_tail = cmpxchg(&cpu_buffer->tail_page,
1065 tail_page, next_page);
1066
1067 if (old_tail == tail_page)
1068 ret = 1;
1069 }
1070
1071 return ret;
1072}
1073
1074static int rb_check_bpage(struct ring_buffer_per_cpu *cpu_buffer,
1075 struct buffer_page *bpage)
1076{
1077 unsigned long val = (unsigned long)bpage;
1078
1079 if (RB_WARN_ON(cpu_buffer, val & RB_FLAG_MASK))
1080 return 1;
1081
1082 return 0;
1083}
1084
1085/**
1086 * rb_check_list - make sure a pointer to a list has the last bits zero
1087 */
1088static int rb_check_list(struct ring_buffer_per_cpu *cpu_buffer,
1089 struct list_head *list)
1090{
1091 if (RB_WARN_ON(cpu_buffer, rb_list_head(list->prev) != list->prev))
1092 return 1;
1093 if (RB_WARN_ON(cpu_buffer, rb_list_head(list->next) != list->next))
1094 return 1;
1095 return 0;
1096}
1097
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001098/**
zhangwei(Jovi)d6118512013-07-15 16:32:50 +08001099 * rb_check_pages - integrity check of buffer pages
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001100 * @cpu_buffer: CPU buffer with pages to test
1101 *
Wenji Huangc3706f02009-02-10 01:03:18 -05001102 * As a safety measure we check to make sure the data pages have not
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001103 * been corrupted.
1104 */
1105static int rb_check_pages(struct ring_buffer_per_cpu *cpu_buffer)
1106{
Steven Rostedt3adc54f2009-03-30 15:32:01 -04001107 struct list_head *head = cpu_buffer->pages;
Steven Rostedt044fa782008-12-02 23:50:03 -05001108 struct buffer_page *bpage, *tmp;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001109
Steven Rostedt308f7ee2012-05-16 19:46:32 -04001110 /* Reset the head page if it exists */
1111 if (cpu_buffer->head_page)
1112 rb_set_head_page(cpu_buffer);
1113
Steven Rostedt77ae3652009-03-27 11:00:29 -04001114 rb_head_page_deactivate(cpu_buffer);
1115
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05001116 if (RB_WARN_ON(cpu_buffer, head->next->prev != head))
1117 return -1;
1118 if (RB_WARN_ON(cpu_buffer, head->prev->next != head))
1119 return -1;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001120
Steven Rostedt77ae3652009-03-27 11:00:29 -04001121 if (rb_check_list(cpu_buffer, head))
1122 return -1;
1123
Steven Rostedt044fa782008-12-02 23:50:03 -05001124 list_for_each_entry_safe(bpage, tmp, head, list) {
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05001125 if (RB_WARN_ON(cpu_buffer,
Steven Rostedt044fa782008-12-02 23:50:03 -05001126 bpage->list.next->prev != &bpage->list))
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05001127 return -1;
1128 if (RB_WARN_ON(cpu_buffer,
Steven Rostedt044fa782008-12-02 23:50:03 -05001129 bpage->list.prev->next != &bpage->list))
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05001130 return -1;
Steven Rostedt77ae3652009-03-27 11:00:29 -04001131 if (rb_check_list(cpu_buffer, &bpage->list))
1132 return -1;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001133 }
1134
Steven Rostedt77ae3652009-03-27 11:00:29 -04001135 rb_head_page_activate(cpu_buffer);
1136
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001137 return 0;
1138}
1139
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001140static int __rb_allocate_pages(int nr_pages, struct list_head *pages, int cpu)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001141{
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001142 int i;
Steven Rostedt044fa782008-12-02 23:50:03 -05001143 struct buffer_page *bpage, *tmp;
Steven Rostedt3adc54f2009-03-30 15:32:01 -04001144
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001145 for (i = 0; i < nr_pages; i++) {
Vaibhav Nagarnaik7ea59062011-05-03 17:56:42 -07001146 struct page *page;
Vaibhav Nagarnaikd7ec4bf2011-06-07 17:01:42 -07001147 /*
1148 * __GFP_NORETRY flag makes sure that the allocation fails
1149 * gracefully without invoking oom-killer and the system is
1150 * not destabilized.
1151 */
Steven Rostedt044fa782008-12-02 23:50:03 -05001152 bpage = kzalloc_node(ALIGN(sizeof(*bpage), cache_line_size()),
Vaibhav Nagarnaikd7ec4bf2011-06-07 17:01:42 -07001153 GFP_KERNEL | __GFP_NORETRY,
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001154 cpu_to_node(cpu));
Steven Rostedt044fa782008-12-02 23:50:03 -05001155 if (!bpage)
Steven Rostedte4c2ce82008-10-01 11:14:54 -04001156 goto free_pages;
Steven Rostedt77ae3652009-03-27 11:00:29 -04001157
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001158 list_add(&bpage->list, pages);
Steven Rostedt77ae3652009-03-27 11:00:29 -04001159
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001160 page = alloc_pages_node(cpu_to_node(cpu),
Vaibhav Nagarnaikd7ec4bf2011-06-07 17:01:42 -07001161 GFP_KERNEL | __GFP_NORETRY, 0);
Vaibhav Nagarnaik7ea59062011-05-03 17:56:42 -07001162 if (!page)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001163 goto free_pages;
Vaibhav Nagarnaik7ea59062011-05-03 17:56:42 -07001164 bpage->page = page_address(page);
Steven Rostedt044fa782008-12-02 23:50:03 -05001165 rb_init_page(bpage->page);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001166 }
1167
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001168 return 0;
1169
1170free_pages:
1171 list_for_each_entry_safe(bpage, tmp, pages, list) {
1172 list_del_init(&bpage->list);
1173 free_buffer_page(bpage);
1174 }
1175
1176 return -ENOMEM;
1177}
1178
1179static int rb_allocate_pages(struct ring_buffer_per_cpu *cpu_buffer,
1180 unsigned nr_pages)
1181{
1182 LIST_HEAD(pages);
1183
1184 WARN_ON(!nr_pages);
1185
1186 if (__rb_allocate_pages(nr_pages, &pages, cpu_buffer->cpu))
1187 return -ENOMEM;
1188
Steven Rostedt3adc54f2009-03-30 15:32:01 -04001189 /*
1190 * The ring buffer page list is a circular list that does not
1191 * start and end with a list head. All page list items point to
1192 * other pages.
1193 */
1194 cpu_buffer->pages = pages.next;
1195 list_del(&pages);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001196
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001197 cpu_buffer->nr_pages = nr_pages;
1198
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001199 rb_check_pages(cpu_buffer);
1200
1201 return 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001202}
1203
1204static struct ring_buffer_per_cpu *
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001205rb_allocate_cpu_buffer(struct ring_buffer *buffer, int nr_pages, int cpu)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001206{
1207 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedt044fa782008-12-02 23:50:03 -05001208 struct buffer_page *bpage;
Vaibhav Nagarnaik7ea59062011-05-03 17:56:42 -07001209 struct page *page;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001210 int ret;
1211
1212 cpu_buffer = kzalloc_node(ALIGN(sizeof(*cpu_buffer), cache_line_size()),
1213 GFP_KERNEL, cpu_to_node(cpu));
1214 if (!cpu_buffer)
1215 return NULL;
1216
1217 cpu_buffer->cpu = cpu;
1218 cpu_buffer->buffer = buffer;
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02001219 raw_spin_lock_init(&cpu_buffer->reader_lock);
Peter Zijlstra1f8a6a12009-06-08 18:18:39 +02001220 lockdep_set_class(&cpu_buffer->reader_lock, buffer->reader_lock_key);
Thomas Gleixneredc35bd2009-12-03 12:38:57 +01001221 cpu_buffer->lock = (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001222 INIT_WORK(&cpu_buffer->update_pages_work, update_pages_handler);
Vaibhav Nagarnaik05fdd702012-05-18 13:29:51 -07001223 init_completion(&cpu_buffer->update_done);
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -05001224 init_irq_work(&cpu_buffer->irq_work.work, rb_wake_up_waiters);
Steven Rostedt (Red Hat)f1dc6722013-03-04 17:33:05 -05001225 init_waitqueue_head(&cpu_buffer->irq_work.waiters);
Steven Rostedt (Red Hat)1e0d6712015-02-10 22:14:53 -05001226 init_waitqueue_head(&cpu_buffer->irq_work.full_waiters);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001227
Steven Rostedt044fa782008-12-02 23:50:03 -05001228 bpage = kzalloc_node(ALIGN(sizeof(*bpage), cache_line_size()),
Steven Rostedte4c2ce82008-10-01 11:14:54 -04001229 GFP_KERNEL, cpu_to_node(cpu));
Steven Rostedt044fa782008-12-02 23:50:03 -05001230 if (!bpage)
Steven Rostedte4c2ce82008-10-01 11:14:54 -04001231 goto fail_free_buffer;
1232
Steven Rostedt77ae3652009-03-27 11:00:29 -04001233 rb_check_bpage(cpu_buffer, bpage);
1234
Steven Rostedt044fa782008-12-02 23:50:03 -05001235 cpu_buffer->reader_page = bpage;
Vaibhav Nagarnaik7ea59062011-05-03 17:56:42 -07001236 page = alloc_pages_node(cpu_to_node(cpu), GFP_KERNEL, 0);
1237 if (!page)
Steven Rostedte4c2ce82008-10-01 11:14:54 -04001238 goto fail_free_reader;
Vaibhav Nagarnaik7ea59062011-05-03 17:56:42 -07001239 bpage->page = page_address(page);
Steven Rostedt044fa782008-12-02 23:50:03 -05001240 rb_init_page(bpage->page);
Steven Rostedte4c2ce82008-10-01 11:14:54 -04001241
Steven Rostedtd7690412008-10-01 00:29:53 -04001242 INIT_LIST_HEAD(&cpu_buffer->reader_page->list);
Vaibhav Nagarnaik44b99462012-06-22 11:50:05 -07001243 INIT_LIST_HEAD(&cpu_buffer->new_pages);
Steven Rostedtd7690412008-10-01 00:29:53 -04001244
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001245 ret = rb_allocate_pages(cpu_buffer, nr_pages);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001246 if (ret < 0)
Steven Rostedtd7690412008-10-01 00:29:53 -04001247 goto fail_free_reader;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001248
1249 cpu_buffer->head_page
Steven Rostedt3adc54f2009-03-30 15:32:01 -04001250 = list_entry(cpu_buffer->pages, struct buffer_page, list);
Steven Rostedtbf41a152008-10-04 02:00:59 -04001251 cpu_buffer->tail_page = cpu_buffer->commit_page = cpu_buffer->head_page;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001252
Steven Rostedt77ae3652009-03-27 11:00:29 -04001253 rb_head_page_activate(cpu_buffer);
1254
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001255 return cpu_buffer;
1256
Steven Rostedtd7690412008-10-01 00:29:53 -04001257 fail_free_reader:
1258 free_buffer_page(cpu_buffer->reader_page);
1259
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001260 fail_free_buffer:
1261 kfree(cpu_buffer);
1262 return NULL;
1263}
1264
1265static void rb_free_cpu_buffer(struct ring_buffer_per_cpu *cpu_buffer)
1266{
Steven Rostedt3adc54f2009-03-30 15:32:01 -04001267 struct list_head *head = cpu_buffer->pages;
Steven Rostedt044fa782008-12-02 23:50:03 -05001268 struct buffer_page *bpage, *tmp;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001269
Steven Rostedtd7690412008-10-01 00:29:53 -04001270 free_buffer_page(cpu_buffer->reader_page);
1271
Steven Rostedt77ae3652009-03-27 11:00:29 -04001272 rb_head_page_deactivate(cpu_buffer);
1273
Steven Rostedt3adc54f2009-03-30 15:32:01 -04001274 if (head) {
1275 list_for_each_entry_safe(bpage, tmp, head, list) {
1276 list_del_init(&bpage->list);
1277 free_buffer_page(bpage);
1278 }
1279 bpage = list_entry(head, struct buffer_page, list);
Steven Rostedt044fa782008-12-02 23:50:03 -05001280 free_buffer_page(bpage);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001281 }
Steven Rostedt3adc54f2009-03-30 15:32:01 -04001282
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001283 kfree(cpu_buffer);
1284}
1285
Steven Rostedt59222ef2009-03-12 11:46:03 -04001286#ifdef CONFIG_HOTPLUG_CPU
Frederic Weisbecker09c9e842009-03-21 04:33:36 +01001287static int rb_cpu_notify(struct notifier_block *self,
1288 unsigned long action, void *hcpu);
Steven Rostedt554f7862009-03-11 22:00:13 -04001289#endif
1290
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001291/**
zhangwei(Jovi)d6118512013-07-15 16:32:50 +08001292 * __ring_buffer_alloc - allocate a new ring_buffer
Robert Richter68814b52008-11-24 12:24:12 +01001293 * @size: the size in bytes per cpu that is needed.
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001294 * @flags: attributes to set for the ring buffer.
1295 *
1296 * Currently the only flag that is available is the RB_FL_OVERWRITE
1297 * flag. This flag means that the buffer will overwrite old data
1298 * when the buffer wraps. If this flag is not set, the buffer will
1299 * drop data when the tail hits the head.
1300 */
Peter Zijlstra1f8a6a12009-06-08 18:18:39 +02001301struct ring_buffer *__ring_buffer_alloc(unsigned long size, unsigned flags,
1302 struct lock_class_key *key)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001303{
1304 struct ring_buffer *buffer;
1305 int bsize;
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001306 int cpu, nr_pages;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001307
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001308 /* keep it in its own cache line */
1309 buffer = kzalloc(ALIGN(sizeof(*buffer), cache_line_size()),
1310 GFP_KERNEL);
1311 if (!buffer)
1312 return NULL;
1313
Rusty Russell9e01c1b2009-01-01 10:12:22 +10301314 if (!alloc_cpumask_var(&buffer->cpumask, GFP_KERNEL))
1315 goto fail_free_buffer;
1316
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001317 nr_pages = DIV_ROUND_UP(size, BUF_PAGE_SIZE);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001318 buffer->flags = flags;
Steven Rostedt37886f62009-03-17 17:22:06 -04001319 buffer->clock = trace_clock_local;
Peter Zijlstra1f8a6a12009-06-08 18:18:39 +02001320 buffer->reader_lock_key = key;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001321
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -05001322 init_irq_work(&buffer->irq_work.work, rb_wake_up_waiters);
Steven Rostedt (Red Hat)f1dc6722013-03-04 17:33:05 -05001323 init_waitqueue_head(&buffer->irq_work.waiters);
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -05001324
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001325 /* need at least two pages */
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001326 if (nr_pages < 2)
1327 nr_pages = 2;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001328
Frederic Weisbecker3bf832c2009-03-19 14:47:33 +01001329 /*
1330 * In case of non-hotplug cpu, if the ring-buffer is allocated
1331 * in early initcall, it will not be notified of secondary cpus.
1332 * In that off case, we need to allocate for all possible cpus.
1333 */
1334#ifdef CONFIG_HOTPLUG_CPU
Srivatsa S. Bhatd39ad272014-03-11 02:11:56 +05301335 cpu_notifier_register_begin();
Steven Rostedt554f7862009-03-11 22:00:13 -04001336 cpumask_copy(buffer->cpumask, cpu_online_mask);
Frederic Weisbecker3bf832c2009-03-19 14:47:33 +01001337#else
1338 cpumask_copy(buffer->cpumask, cpu_possible_mask);
1339#endif
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001340 buffer->cpus = nr_cpu_ids;
1341
1342 bsize = sizeof(void *) * nr_cpu_ids;
1343 buffer->buffers = kzalloc(ALIGN(bsize, cache_line_size()),
1344 GFP_KERNEL);
1345 if (!buffer->buffers)
Rusty Russell9e01c1b2009-01-01 10:12:22 +10301346 goto fail_free_cpumask;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001347
1348 for_each_buffer_cpu(buffer, cpu) {
1349 buffer->buffers[cpu] =
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001350 rb_allocate_cpu_buffer(buffer, nr_pages, cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001351 if (!buffer->buffers[cpu])
1352 goto fail_free_buffers;
1353 }
1354
Steven Rostedt59222ef2009-03-12 11:46:03 -04001355#ifdef CONFIG_HOTPLUG_CPU
Steven Rostedt554f7862009-03-11 22:00:13 -04001356 buffer->cpu_notify.notifier_call = rb_cpu_notify;
1357 buffer->cpu_notify.priority = 0;
Srivatsa S. Bhatd39ad272014-03-11 02:11:56 +05301358 __register_cpu_notifier(&buffer->cpu_notify);
1359 cpu_notifier_register_done();
Steven Rostedt554f7862009-03-11 22:00:13 -04001360#endif
1361
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001362 mutex_init(&buffer->mutex);
1363
1364 return buffer;
1365
1366 fail_free_buffers:
1367 for_each_buffer_cpu(buffer, cpu) {
1368 if (buffer->buffers[cpu])
1369 rb_free_cpu_buffer(buffer->buffers[cpu]);
1370 }
1371 kfree(buffer->buffers);
1372
Rusty Russell9e01c1b2009-01-01 10:12:22 +10301373 fail_free_cpumask:
1374 free_cpumask_var(buffer->cpumask);
Srivatsa S. Bhatd39ad272014-03-11 02:11:56 +05301375#ifdef CONFIG_HOTPLUG_CPU
1376 cpu_notifier_register_done();
1377#endif
Rusty Russell9e01c1b2009-01-01 10:12:22 +10301378
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001379 fail_free_buffer:
1380 kfree(buffer);
1381 return NULL;
1382}
Peter Zijlstra1f8a6a12009-06-08 18:18:39 +02001383EXPORT_SYMBOL_GPL(__ring_buffer_alloc);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001384
1385/**
1386 * ring_buffer_free - free a ring buffer.
1387 * @buffer: the buffer to free.
1388 */
1389void
1390ring_buffer_free(struct ring_buffer *buffer)
1391{
1392 int cpu;
1393
Steven Rostedt59222ef2009-03-12 11:46:03 -04001394#ifdef CONFIG_HOTPLUG_CPU
Srivatsa S. Bhatd39ad272014-03-11 02:11:56 +05301395 cpu_notifier_register_begin();
1396 __unregister_cpu_notifier(&buffer->cpu_notify);
Steven Rostedt554f7862009-03-11 22:00:13 -04001397#endif
1398
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001399 for_each_buffer_cpu(buffer, cpu)
1400 rb_free_cpu_buffer(buffer->buffers[cpu]);
1401
Srivatsa S. Bhatd39ad272014-03-11 02:11:56 +05301402#ifdef CONFIG_HOTPLUG_CPU
1403 cpu_notifier_register_done();
1404#endif
Steven Rostedt554f7862009-03-11 22:00:13 -04001405
Eric Dumazetbd3f0222009-08-07 12:49:29 +02001406 kfree(buffer->buffers);
Rusty Russell9e01c1b2009-01-01 10:12:22 +10301407 free_cpumask_var(buffer->cpumask);
1408
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001409 kfree(buffer);
1410}
Robert Richterc4f50182008-12-11 16:49:22 +01001411EXPORT_SYMBOL_GPL(ring_buffer_free);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001412
Steven Rostedt37886f62009-03-17 17:22:06 -04001413void ring_buffer_set_clock(struct ring_buffer *buffer,
1414 u64 (*clock)(void))
1415{
1416 buffer->clock = clock;
1417}
1418
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001419static void rb_reset_cpu(struct ring_buffer_per_cpu *cpu_buffer);
1420
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001421static inline unsigned long rb_page_entries(struct buffer_page *bpage)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001422{
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001423 return local_read(&bpage->entries) & RB_WRITE_MASK;
1424}
1425
1426static inline unsigned long rb_page_write(struct buffer_page *bpage)
1427{
1428 return local_read(&bpage->write) & RB_WRITE_MASK;
1429}
1430
Vaibhav Nagarnaik5040b4b2012-05-03 18:59:51 -07001431static int
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001432rb_remove_pages(struct ring_buffer_per_cpu *cpu_buffer, unsigned int nr_pages)
1433{
1434 struct list_head *tail_page, *to_remove, *next_page;
1435 struct buffer_page *to_remove_page, *tmp_iter_page;
1436 struct buffer_page *last_page, *first_page;
1437 unsigned int nr_removed;
1438 unsigned long head_bit;
1439 int page_entries;
1440
1441 head_bit = 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001442
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02001443 raw_spin_lock_irq(&cpu_buffer->reader_lock);
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001444 atomic_inc(&cpu_buffer->record_disabled);
1445 /*
1446 * We don't race with the readers since we have acquired the reader
1447 * lock. We also don't race with writers after disabling recording.
1448 * This makes it easy to figure out the first and the last page to be
1449 * removed from the list. We unlink all the pages in between including
1450 * the first and last pages. This is done in a busy loop so that we
1451 * lose the least number of traces.
1452 * The pages are freed after we restart recording and unlock readers.
1453 */
1454 tail_page = &cpu_buffer->tail_page->list;
Steven Rostedt77ae3652009-03-27 11:00:29 -04001455
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001456 /*
1457 * tail page might be on reader page, we remove the next page
1458 * from the ring buffer
1459 */
1460 if (cpu_buffer->tail_page == cpu_buffer->reader_page)
1461 tail_page = rb_list_head(tail_page->next);
1462 to_remove = tail_page;
1463
1464 /* start of pages to remove */
1465 first_page = list_entry(rb_list_head(to_remove->next),
1466 struct buffer_page, list);
1467
1468 for (nr_removed = 0; nr_removed < nr_pages; nr_removed++) {
1469 to_remove = rb_list_head(to_remove)->next;
1470 head_bit |= (unsigned long)to_remove & RB_PAGE_HEAD;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001471 }
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001472
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001473 next_page = rb_list_head(to_remove)->next;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001474
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001475 /*
1476 * Now we remove all pages between tail_page and next_page.
1477 * Make sure that we have head_bit value preserved for the
1478 * next page
1479 */
1480 tail_page->next = (struct list_head *)((unsigned long)next_page |
1481 head_bit);
1482 next_page = rb_list_head(next_page);
1483 next_page->prev = tail_page;
1484
1485 /* make sure pages points to a valid page in the ring buffer */
1486 cpu_buffer->pages = next_page;
1487
1488 /* update head page */
1489 if (head_bit)
1490 cpu_buffer->head_page = list_entry(next_page,
1491 struct buffer_page, list);
1492
1493 /*
1494 * change read pointer to make sure any read iterators reset
1495 * themselves
1496 */
1497 cpu_buffer->read = 0;
1498
1499 /* pages are removed, resume tracing and then free the pages */
1500 atomic_dec(&cpu_buffer->record_disabled);
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02001501 raw_spin_unlock_irq(&cpu_buffer->reader_lock);
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001502
1503 RB_WARN_ON(cpu_buffer, list_empty(cpu_buffer->pages));
1504
1505 /* last buffer page to remove */
1506 last_page = list_entry(rb_list_head(to_remove), struct buffer_page,
1507 list);
1508 tmp_iter_page = first_page;
1509
1510 do {
1511 to_remove_page = tmp_iter_page;
1512 rb_inc_page(cpu_buffer, &tmp_iter_page);
1513
1514 /* update the counters */
1515 page_entries = rb_page_entries(to_remove_page);
1516 if (page_entries) {
1517 /*
1518 * If something was added to this page, it was full
1519 * since it is not the tail page. So we deduct the
1520 * bytes consumed in ring buffer from here.
Vaibhav Nagarnaik48fdc722012-06-29 12:31:41 -07001521 * Increment overrun to account for the lost events.
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001522 */
Vaibhav Nagarnaik48fdc722012-06-29 12:31:41 -07001523 local_add(page_entries, &cpu_buffer->overrun);
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001524 local_sub(BUF_PAGE_SIZE, &cpu_buffer->entries_bytes);
1525 }
1526
1527 /*
1528 * We have already removed references to this list item, just
1529 * free up the buffer_page and its page
1530 */
1531 free_buffer_page(to_remove_page);
1532 nr_removed--;
1533
1534 } while (to_remove_page != last_page);
1535
1536 RB_WARN_ON(cpu_buffer, nr_removed);
Vaibhav Nagarnaik5040b4b2012-05-03 18:59:51 -07001537
1538 return nr_removed == 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001539}
1540
Vaibhav Nagarnaik5040b4b2012-05-03 18:59:51 -07001541static int
1542rb_insert_pages(struct ring_buffer_per_cpu *cpu_buffer)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001543{
Vaibhav Nagarnaik5040b4b2012-05-03 18:59:51 -07001544 struct list_head *pages = &cpu_buffer->new_pages;
1545 int retries, success;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001546
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02001547 raw_spin_lock_irq(&cpu_buffer->reader_lock);
Vaibhav Nagarnaik5040b4b2012-05-03 18:59:51 -07001548 /*
1549 * We are holding the reader lock, so the reader page won't be swapped
1550 * in the ring buffer. Now we are racing with the writer trying to
1551 * move head page and the tail page.
1552 * We are going to adapt the reader page update process where:
1553 * 1. We first splice the start and end of list of new pages between
1554 * the head page and its previous page.
1555 * 2. We cmpxchg the prev_page->next to point from head page to the
1556 * start of new pages list.
1557 * 3. Finally, we update the head->prev to the end of new list.
1558 *
1559 * We will try this process 10 times, to make sure that we don't keep
1560 * spinning.
1561 */
1562 retries = 10;
1563 success = 0;
1564 while (retries--) {
1565 struct list_head *head_page, *prev_page, *r;
1566 struct list_head *last_page, *first_page;
1567 struct list_head *head_page_with_bit;
Steven Rostedt77ae3652009-03-27 11:00:29 -04001568
Vaibhav Nagarnaik5040b4b2012-05-03 18:59:51 -07001569 head_page = &rb_set_head_page(cpu_buffer)->list;
Steven Rostedt54f7be52012-11-29 22:27:22 -05001570 if (!head_page)
1571 break;
Vaibhav Nagarnaik5040b4b2012-05-03 18:59:51 -07001572 prev_page = head_page->prev;
1573
1574 first_page = pages->next;
1575 last_page = pages->prev;
1576
1577 head_page_with_bit = (struct list_head *)
1578 ((unsigned long)head_page | RB_PAGE_HEAD);
1579
1580 last_page->next = head_page_with_bit;
1581 first_page->prev = prev_page;
1582
1583 r = cmpxchg(&prev_page->next, head_page_with_bit, first_page);
1584
1585 if (r == head_page_with_bit) {
1586 /*
1587 * yay, we replaced the page pointer to our new list,
1588 * now, we just have to update to head page's prev
1589 * pointer to point to end of list
1590 */
1591 head_page->prev = last_page;
1592 success = 1;
1593 break;
1594 }
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001595 }
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001596
Vaibhav Nagarnaik5040b4b2012-05-03 18:59:51 -07001597 if (success)
1598 INIT_LIST_HEAD(pages);
1599 /*
1600 * If we weren't successful in adding in new pages, warn and stop
1601 * tracing
1602 */
1603 RB_WARN_ON(cpu_buffer, !success);
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02001604 raw_spin_unlock_irq(&cpu_buffer->reader_lock);
Vaibhav Nagarnaik5040b4b2012-05-03 18:59:51 -07001605
1606 /* free pages if they weren't inserted */
1607 if (!success) {
1608 struct buffer_page *bpage, *tmp;
1609 list_for_each_entry_safe(bpage, tmp, &cpu_buffer->new_pages,
1610 list) {
1611 list_del_init(&bpage->list);
1612 free_buffer_page(bpage);
1613 }
1614 }
1615 return success;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001616}
1617
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001618static void rb_update_pages(struct ring_buffer_per_cpu *cpu_buffer)
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001619{
Vaibhav Nagarnaik5040b4b2012-05-03 18:59:51 -07001620 int success;
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001621
Vaibhav Nagarnaik5040b4b2012-05-03 18:59:51 -07001622 if (cpu_buffer->nr_pages_to_update > 0)
1623 success = rb_insert_pages(cpu_buffer);
1624 else
1625 success = rb_remove_pages(cpu_buffer,
1626 -cpu_buffer->nr_pages_to_update);
1627
1628 if (success)
1629 cpu_buffer->nr_pages += cpu_buffer->nr_pages_to_update;
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001630}
1631
1632static void update_pages_handler(struct work_struct *work)
1633{
1634 struct ring_buffer_per_cpu *cpu_buffer = container_of(work,
1635 struct ring_buffer_per_cpu, update_pages_work);
1636 rb_update_pages(cpu_buffer);
Vaibhav Nagarnaik05fdd702012-05-18 13:29:51 -07001637 complete(&cpu_buffer->update_done);
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001638}
1639
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001640/**
1641 * ring_buffer_resize - resize the ring buffer
1642 * @buffer: the buffer to resize.
1643 * @size: the new size.
zhangwei(Jovi)d6118512013-07-15 16:32:50 +08001644 * @cpu_id: the cpu buffer to resize
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001645 *
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001646 * Minimum size is 2 * BUF_PAGE_SIZE.
1647 *
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001648 * Returns 0 on success and < 0 on failure.
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001649 */
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001650int ring_buffer_resize(struct ring_buffer *buffer, unsigned long size,
1651 int cpu_id)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001652{
1653 struct ring_buffer_per_cpu *cpu_buffer;
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001654 unsigned nr_pages;
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001655 int cpu, err = 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001656
Ingo Molnaree51a1d2008-11-13 14:58:31 +01001657 /*
1658 * Always succeed at resizing a non-existent buffer:
1659 */
1660 if (!buffer)
1661 return size;
1662
Steven Rostedt6a31e1f2012-05-23 15:35:17 -04001663 /* Make sure the requested buffer exists */
1664 if (cpu_id != RING_BUFFER_ALL_CPUS &&
1665 !cpumask_test_cpu(cpu_id, buffer->cpumask))
1666 return size;
1667
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001668 size = DIV_ROUND_UP(size, BUF_PAGE_SIZE);
1669 size *= BUF_PAGE_SIZE;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001670
1671 /* we need a minimum of two pages */
1672 if (size < BUF_PAGE_SIZE * 2)
1673 size = BUF_PAGE_SIZE * 2;
1674
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001675 nr_pages = DIV_ROUND_UP(size, BUF_PAGE_SIZE);
1676
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001677 /*
1678 * Don't succeed if resizing is disabled, as a reader might be
1679 * manipulating the ring buffer and is expecting a sane state while
1680 * this is true.
1681 */
1682 if (atomic_read(&buffer->resize_disabled))
1683 return -EBUSY;
1684
1685 /* prevent another thread from changing buffer sizes */
1686 mutex_lock(&buffer->mutex);
1687
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001688 if (cpu_id == RING_BUFFER_ALL_CPUS) {
1689 /* calculate the pages to update */
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001690 for_each_buffer_cpu(buffer, cpu) {
1691 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001692
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001693 cpu_buffer->nr_pages_to_update = nr_pages -
1694 cpu_buffer->nr_pages;
Vaibhav Nagarnaikd7ec4bf2011-06-07 17:01:42 -07001695 /*
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001696 * nothing more to do for removing pages or no update
Vaibhav Nagarnaikd7ec4bf2011-06-07 17:01:42 -07001697 */
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001698 if (cpu_buffer->nr_pages_to_update <= 0)
1699 continue;
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001700 /*
1701 * to add pages, make sure all new pages can be
1702 * allocated without receiving ENOMEM
1703 */
1704 INIT_LIST_HEAD(&cpu_buffer->new_pages);
1705 if (__rb_allocate_pages(cpu_buffer->nr_pages_to_update,
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001706 &cpu_buffer->new_pages, cpu)) {
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001707 /* not enough memory for new pages */
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001708 err = -ENOMEM;
1709 goto out_err;
1710 }
1711 }
1712
1713 get_online_cpus();
1714 /*
1715 * Fire off all the required work handlers
Vaibhav Nagarnaik05fdd702012-05-18 13:29:51 -07001716 * We can't schedule on offline CPUs, but it's not necessary
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001717 * since we can change their buffer sizes without any race.
1718 */
1719 for_each_buffer_cpu(buffer, cpu) {
1720 cpu_buffer = buffer->buffers[cpu];
Vaibhav Nagarnaik05fdd702012-05-18 13:29:51 -07001721 if (!cpu_buffer->nr_pages_to_update)
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001722 continue;
1723
Corey Minyard021c5b32014-07-16 14:07:13 -05001724 /* Can't run something on an offline CPU. */
1725 if (!cpu_online(cpu)) {
Steven Rostedt (Red Hat)f5eb5582013-03-07 09:27:42 -05001726 rb_update_pages(cpu_buffer);
1727 cpu_buffer->nr_pages_to_update = 0;
1728 } else {
Vaibhav Nagarnaik05fdd702012-05-18 13:29:51 -07001729 schedule_work_on(cpu,
1730 &cpu_buffer->update_pages_work);
Steven Rostedt (Red Hat)f5eb5582013-03-07 09:27:42 -05001731 }
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001732 }
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001733
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001734 /* wait for all the updates to complete */
1735 for_each_buffer_cpu(buffer, cpu) {
1736 cpu_buffer = buffer->buffers[cpu];
Vaibhav Nagarnaik05fdd702012-05-18 13:29:51 -07001737 if (!cpu_buffer->nr_pages_to_update)
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001738 continue;
1739
Vaibhav Nagarnaik05fdd702012-05-18 13:29:51 -07001740 if (cpu_online(cpu))
1741 wait_for_completion(&cpu_buffer->update_done);
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001742 cpu_buffer->nr_pages_to_update = 0;
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001743 }
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001744
1745 put_online_cpus();
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001746 } else {
Vaibhav Nagarnaik8e49f412012-10-10 16:40:27 -07001747 /* Make sure this CPU has been intitialized */
1748 if (!cpumask_test_cpu(cpu_id, buffer->cpumask))
1749 goto out;
1750
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001751 cpu_buffer = buffer->buffers[cpu_id];
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001752
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001753 if (nr_pages == cpu_buffer->nr_pages)
1754 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001755
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001756 cpu_buffer->nr_pages_to_update = nr_pages -
1757 cpu_buffer->nr_pages;
1758
1759 INIT_LIST_HEAD(&cpu_buffer->new_pages);
1760 if (cpu_buffer->nr_pages_to_update > 0 &&
1761 __rb_allocate_pages(cpu_buffer->nr_pages_to_update,
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001762 &cpu_buffer->new_pages, cpu_id)) {
1763 err = -ENOMEM;
1764 goto out_err;
1765 }
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001766
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001767 get_online_cpus();
1768
Corey Minyard021c5b32014-07-16 14:07:13 -05001769 /* Can't run something on an offline CPU. */
1770 if (!cpu_online(cpu_id))
Steven Rostedt (Red Hat)f5eb5582013-03-07 09:27:42 -05001771 rb_update_pages(cpu_buffer);
1772 else {
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001773 schedule_work_on(cpu_id,
1774 &cpu_buffer->update_pages_work);
Vaibhav Nagarnaik05fdd702012-05-18 13:29:51 -07001775 wait_for_completion(&cpu_buffer->update_done);
Steven Rostedt (Red Hat)f5eb5582013-03-07 09:27:42 -05001776 }
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001777
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001778 cpu_buffer->nr_pages_to_update = 0;
Vaibhav Nagarnaik05fdd702012-05-18 13:29:51 -07001779 put_online_cpus();
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001780 }
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001781
1782 out:
Steven Rostedt659f4512012-05-14 17:02:33 -04001783 /*
1784 * The ring buffer resize can happen with the ring buffer
1785 * enabled, so that the update disturbs the tracing as little
1786 * as possible. But if the buffer is disabled, we do not need
1787 * to worry about that, and we can take the time to verify
1788 * that the buffer is not corrupt.
1789 */
1790 if (atomic_read(&buffer->record_disabled)) {
1791 atomic_inc(&buffer->record_disabled);
1792 /*
1793 * Even though the buffer was disabled, we must make sure
1794 * that it is truly disabled before calling rb_check_pages.
1795 * There could have been a race between checking
1796 * record_disable and incrementing it.
1797 */
1798 synchronize_sched();
1799 for_each_buffer_cpu(buffer, cpu) {
1800 cpu_buffer = buffer->buffers[cpu];
1801 rb_check_pages(cpu_buffer);
1802 }
1803 atomic_dec(&buffer->record_disabled);
1804 }
1805
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001806 mutex_unlock(&buffer->mutex);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001807 return size;
1808
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001809 out_err:
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001810 for_each_buffer_cpu(buffer, cpu) {
1811 struct buffer_page *bpage, *tmp;
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001812
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001813 cpu_buffer = buffer->buffers[cpu];
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001814 cpu_buffer->nr_pages_to_update = 0;
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001815
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001816 if (list_empty(&cpu_buffer->new_pages))
1817 continue;
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001818
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001819 list_for_each_entry_safe(bpage, tmp, &cpu_buffer->new_pages,
1820 list) {
1821 list_del_init(&bpage->list);
1822 free_buffer_page(bpage);
1823 }
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001824 }
Vegard Nossum641d2f62008-11-18 19:22:13 +01001825 mutex_unlock(&buffer->mutex);
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001826 return err;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001827}
Robert Richterc4f50182008-12-11 16:49:22 +01001828EXPORT_SYMBOL_GPL(ring_buffer_resize);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001829
David Sharp750912f2010-12-08 13:46:47 -08001830void ring_buffer_change_overwrite(struct ring_buffer *buffer, int val)
1831{
1832 mutex_lock(&buffer->mutex);
1833 if (val)
1834 buffer->flags |= RB_FL_OVERWRITE;
1835 else
1836 buffer->flags &= ~RB_FL_OVERWRITE;
1837 mutex_unlock(&buffer->mutex);
1838}
1839EXPORT_SYMBOL_GPL(ring_buffer_change_overwrite);
1840
Steven Rostedt8789a9e2008-12-02 15:34:07 -05001841static inline void *
Steven Rostedt044fa782008-12-02 23:50:03 -05001842__rb_data_page_index(struct buffer_data_page *bpage, unsigned index)
Steven Rostedt8789a9e2008-12-02 15:34:07 -05001843{
Steven Rostedt044fa782008-12-02 23:50:03 -05001844 return bpage->data + index;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05001845}
1846
Steven Rostedt044fa782008-12-02 23:50:03 -05001847static inline void *__rb_page_index(struct buffer_page *bpage, unsigned index)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001848{
Steven Rostedt044fa782008-12-02 23:50:03 -05001849 return bpage->page->data + index;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001850}
1851
1852static inline struct ring_buffer_event *
Steven Rostedtd7690412008-10-01 00:29:53 -04001853rb_reader_event(struct ring_buffer_per_cpu *cpu_buffer)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001854{
Steven Rostedt6f807ac2008-10-04 02:00:58 -04001855 return __rb_page_index(cpu_buffer->reader_page,
1856 cpu_buffer->reader_page->read);
1857}
1858
1859static inline struct ring_buffer_event *
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001860rb_iter_head_event(struct ring_buffer_iter *iter)
1861{
Steven Rostedt6f807ac2008-10-04 02:00:58 -04001862 return __rb_page_index(iter->head_page, iter->head);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001863}
1864
Steven Rostedtbf41a152008-10-04 02:00:59 -04001865static inline unsigned rb_page_commit(struct buffer_page *bpage)
1866{
Steven Rostedtabc9b562008-12-02 15:34:06 -05001867 return local_read(&bpage->page->commit);
Steven Rostedtbf41a152008-10-04 02:00:59 -04001868}
1869
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001870/* Size is determined by what has been committed */
Steven Rostedtbf41a152008-10-04 02:00:59 -04001871static inline unsigned rb_page_size(struct buffer_page *bpage)
1872{
1873 return rb_page_commit(bpage);
1874}
1875
1876static inline unsigned
1877rb_commit_index(struct ring_buffer_per_cpu *cpu_buffer)
1878{
1879 return rb_page_commit(cpu_buffer->commit_page);
1880}
1881
Steven Rostedtbf41a152008-10-04 02:00:59 -04001882static inline unsigned
1883rb_event_index(struct ring_buffer_event *event)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001884{
Steven Rostedtbf41a152008-10-04 02:00:59 -04001885 unsigned long addr = (unsigned long)event;
1886
Steven Rostedt22f470f2009-06-11 09:29:58 -04001887 return (addr & ~PAGE_MASK) - BUF_PAGE_HDR_SIZE;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001888}
1889
Steven Rostedt0f0c85f2009-05-11 16:08:00 -04001890static inline int
Steven Rostedtfa743952009-06-16 12:37:57 -04001891rb_event_is_commit(struct ring_buffer_per_cpu *cpu_buffer,
1892 struct ring_buffer_event *event)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001893{
Steven Rostedtbf41a152008-10-04 02:00:59 -04001894 unsigned long addr = (unsigned long)event;
1895 unsigned long index;
1896
1897 index = rb_event_index(event);
1898 addr &= PAGE_MASK;
1899
1900 return cpu_buffer->commit_page->page == (void *)addr &&
1901 rb_commit_index(cpu_buffer) == index;
1902}
1903
Andrew Morton34a148b2009-01-09 12:27:09 -08001904static void
Steven Rostedtbf41a152008-10-04 02:00:59 -04001905rb_set_commit_to_write(struct ring_buffer_per_cpu *cpu_buffer)
1906{
Steven Rostedt77ae3652009-03-27 11:00:29 -04001907 unsigned long max_count;
1908
Steven Rostedtbf41a152008-10-04 02:00:59 -04001909 /*
1910 * We only race with interrupts and NMIs on this CPU.
1911 * If we own the commit event, then we can commit
1912 * all others that interrupted us, since the interruptions
1913 * are in stack format (they finish before they come
1914 * back to us). This allows us to do a simple loop to
1915 * assign the commit to the tail.
1916 */
Steven Rostedta8ccf1d2008-12-23 11:32:24 -05001917 again:
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001918 max_count = cpu_buffer->nr_pages * 100;
Steven Rostedt77ae3652009-03-27 11:00:29 -04001919
Steven Rostedtbf41a152008-10-04 02:00:59 -04001920 while (cpu_buffer->commit_page != cpu_buffer->tail_page) {
Steven Rostedt77ae3652009-03-27 11:00:29 -04001921 if (RB_WARN_ON(cpu_buffer, !(--max_count)))
1922 return;
1923 if (RB_WARN_ON(cpu_buffer,
1924 rb_is_reader_page(cpu_buffer->tail_page)))
1925 return;
1926 local_set(&cpu_buffer->commit_page->page->commit,
1927 rb_page_write(cpu_buffer->commit_page));
Steven Rostedtbf41a152008-10-04 02:00:59 -04001928 rb_inc_page(cpu_buffer, &cpu_buffer->commit_page);
Steven Rostedtabc9b562008-12-02 15:34:06 -05001929 cpu_buffer->write_stamp =
1930 cpu_buffer->commit_page->page->time_stamp;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001931 /* add barrier to keep gcc from optimizing too much */
1932 barrier();
1933 }
1934 while (rb_commit_index(cpu_buffer) !=
1935 rb_page_write(cpu_buffer->commit_page)) {
Steven Rostedt77ae3652009-03-27 11:00:29 -04001936
1937 local_set(&cpu_buffer->commit_page->page->commit,
1938 rb_page_write(cpu_buffer->commit_page));
1939 RB_WARN_ON(cpu_buffer,
1940 local_read(&cpu_buffer->commit_page->page->commit) &
1941 ~RB_WRITE_MASK);
Steven Rostedtbf41a152008-10-04 02:00:59 -04001942 barrier();
1943 }
Steven Rostedta8ccf1d2008-12-23 11:32:24 -05001944
1945 /* again, keep gcc from optimizing */
1946 barrier();
1947
1948 /*
1949 * If an interrupt came in just after the first while loop
1950 * and pushed the tail page forward, we will be left with
1951 * a dangling commit that will never go forward.
1952 */
1953 if (unlikely(cpu_buffer->commit_page != cpu_buffer->tail_page))
1954 goto again;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001955}
1956
Steven Rostedtd7690412008-10-01 00:29:53 -04001957static void rb_reset_reader_page(struct ring_buffer_per_cpu *cpu_buffer)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001958{
Steven Rostedtabc9b562008-12-02 15:34:06 -05001959 cpu_buffer->read_stamp = cpu_buffer->reader_page->page->time_stamp;
Steven Rostedt6f807ac2008-10-04 02:00:58 -04001960 cpu_buffer->reader_page->read = 0;
Steven Rostedtd7690412008-10-01 00:29:53 -04001961}
1962
Andrew Morton34a148b2009-01-09 12:27:09 -08001963static void rb_inc_iter(struct ring_buffer_iter *iter)
Steven Rostedtd7690412008-10-01 00:29:53 -04001964{
1965 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
1966
1967 /*
1968 * The iterator could be on the reader page (it starts there).
1969 * But the head could have moved, since the reader was
1970 * found. Check for this case and assign the iterator
1971 * to the head page instead of next.
1972 */
1973 if (iter->head_page == cpu_buffer->reader_page)
Steven Rostedt77ae3652009-03-27 11:00:29 -04001974 iter->head_page = rb_set_head_page(cpu_buffer);
Steven Rostedtd7690412008-10-01 00:29:53 -04001975 else
1976 rb_inc_page(cpu_buffer, &iter->head_page);
1977
Steven Rostedtabc9b562008-12-02 15:34:06 -05001978 iter->read_stamp = iter->head_page->page->time_stamp;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001979 iter->head = 0;
1980}
1981
Steven Rostedt69d1b832010-10-07 18:18:05 -04001982/* Slow path, do not inline */
1983static noinline struct ring_buffer_event *
1984rb_add_time_stamp(struct ring_buffer_event *event, u64 delta)
1985{
1986 event->type_len = RINGBUF_TYPE_TIME_EXTEND;
1987
1988 /* Not the first event on the page? */
1989 if (rb_event_index(event)) {
1990 event->time_delta = delta & TS_MASK;
1991 event->array[0] = delta >> TS_SHIFT;
1992 } else {
1993 /* nope, just zero it */
1994 event->time_delta = 0;
1995 event->array[0] = 0;
1996 }
1997
1998 return skip_time_extend(event);
1999}
2000
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002001/**
David Sharp01e3e712012-06-07 16:46:24 -07002002 * rb_update_event - update event type and data
Steven Rostedt (Red Hat)021de3d2014-08-06 15:36:31 -04002003 * @event: the event to update
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002004 * @type: the type of event
2005 * @length: the size of the event field in the ring buffer
2006 *
2007 * Update the type and data fields of the event. The length
2008 * is the actual size that is written to the ring buffer,
2009 * and with this, we can determine what to place into the
2010 * data field.
2011 */
Steven Rostedt (Red Hat)a4543a22015-05-29 09:40:18 -04002012static void __always_inline
Steven Rostedt69d1b832010-10-07 18:18:05 -04002013rb_update_event(struct ring_buffer_per_cpu *cpu_buffer,
Steven Rostedt (Red Hat)fcc742e2015-05-28 17:13:14 -04002014 struct ring_buffer_event *event,
2015 struct rb_event_info *info)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002016{
Steven Rostedt (Red Hat)fcc742e2015-05-28 17:13:14 -04002017 unsigned length = info->length;
2018 u64 delta = info->delta;
2019
Steven Rostedt69d1b832010-10-07 18:18:05 -04002020 /*
2021 * If we need to add a timestamp, then we
2022 * add it to the start of the resevered space.
2023 */
Steven Rostedt (Red Hat)fcc742e2015-05-28 17:13:14 -04002024 if (unlikely(info->add_timestamp)) {
Steven Rostedt69d1b832010-10-07 18:18:05 -04002025 event = rb_add_time_stamp(event, delta);
2026 length -= RB_LEN_TIME_EXTEND;
2027 delta = 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002028 }
Steven Rostedt69d1b832010-10-07 18:18:05 -04002029
2030 event->time_delta = delta;
2031 length -= RB_EVNT_HDR_SIZE;
2032 if (length > RB_MAX_SMALL_DATA || RB_FORCE_8BYTE_ALIGNMENT) {
2033 event->type_len = 0;
2034 event->array[0] = length;
2035 } else
2036 event->type_len = DIV_ROUND_UP(length, RB_ALIGNMENT);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002037}
2038
Steven Rostedt77ae3652009-03-27 11:00:29 -04002039/*
2040 * rb_handle_head_page - writer hit the head page
2041 *
2042 * Returns: +1 to retry page
2043 * 0 to continue
2044 * -1 on error
2045 */
2046static int
2047rb_handle_head_page(struct ring_buffer_per_cpu *cpu_buffer,
2048 struct buffer_page *tail_page,
2049 struct buffer_page *next_page)
2050{
2051 struct buffer_page *new_head;
2052 int entries;
2053 int type;
2054 int ret;
2055
2056 entries = rb_page_entries(next_page);
2057
2058 /*
2059 * The hard part is here. We need to move the head
2060 * forward, and protect against both readers on
2061 * other CPUs and writers coming in via interrupts.
2062 */
2063 type = rb_head_page_set_update(cpu_buffer, next_page, tail_page,
2064 RB_PAGE_HEAD);
2065
2066 /*
2067 * type can be one of four:
2068 * NORMAL - an interrupt already moved it for us
2069 * HEAD - we are the first to get here.
2070 * UPDATE - we are the interrupt interrupting
2071 * a current move.
2072 * MOVED - a reader on another CPU moved the next
2073 * pointer to its reader page. Give up
2074 * and try again.
2075 */
2076
2077 switch (type) {
2078 case RB_PAGE_HEAD:
2079 /*
2080 * We changed the head to UPDATE, thus
2081 * it is our responsibility to update
2082 * the counters.
2083 */
2084 local_add(entries, &cpu_buffer->overrun);
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -07002085 local_sub(BUF_PAGE_SIZE, &cpu_buffer->entries_bytes);
Steven Rostedt77ae3652009-03-27 11:00:29 -04002086
2087 /*
2088 * The entries will be zeroed out when we move the
2089 * tail page.
2090 */
2091
2092 /* still more to do */
2093 break;
2094
2095 case RB_PAGE_UPDATE:
2096 /*
2097 * This is an interrupt that interrupt the
2098 * previous update. Still more to do.
2099 */
2100 break;
2101 case RB_PAGE_NORMAL:
2102 /*
2103 * An interrupt came in before the update
2104 * and processed this for us.
2105 * Nothing left to do.
2106 */
2107 return 1;
2108 case RB_PAGE_MOVED:
2109 /*
2110 * The reader is on another CPU and just did
2111 * a swap with our next_page.
2112 * Try again.
2113 */
2114 return 1;
2115 default:
2116 RB_WARN_ON(cpu_buffer, 1); /* WTF??? */
2117 return -1;
2118 }
2119
2120 /*
2121 * Now that we are here, the old head pointer is
2122 * set to UPDATE. This will keep the reader from
2123 * swapping the head page with the reader page.
2124 * The reader (on another CPU) will spin till
2125 * we are finished.
2126 *
2127 * We just need to protect against interrupts
2128 * doing the job. We will set the next pointer
2129 * to HEAD. After that, we set the old pointer
2130 * to NORMAL, but only if it was HEAD before.
2131 * otherwise we are an interrupt, and only
2132 * want the outer most commit to reset it.
2133 */
2134 new_head = next_page;
2135 rb_inc_page(cpu_buffer, &new_head);
2136
2137 ret = rb_head_page_set_head(cpu_buffer, new_head, next_page,
2138 RB_PAGE_NORMAL);
2139
2140 /*
2141 * Valid returns are:
2142 * HEAD - an interrupt came in and already set it.
2143 * NORMAL - One of two things:
2144 * 1) We really set it.
2145 * 2) A bunch of interrupts came in and moved
2146 * the page forward again.
2147 */
2148 switch (ret) {
2149 case RB_PAGE_HEAD:
2150 case RB_PAGE_NORMAL:
2151 /* OK */
2152 break;
2153 default:
2154 RB_WARN_ON(cpu_buffer, 1);
2155 return -1;
2156 }
2157
2158 /*
2159 * It is possible that an interrupt came in,
2160 * set the head up, then more interrupts came in
2161 * and moved it again. When we get back here,
2162 * the page would have been set to NORMAL but we
2163 * just set it back to HEAD.
2164 *
2165 * How do you detect this? Well, if that happened
2166 * the tail page would have moved.
2167 */
2168 if (ret == RB_PAGE_NORMAL) {
2169 /*
2170 * If the tail had moved passed next, then we need
2171 * to reset the pointer.
2172 */
2173 if (cpu_buffer->tail_page != tail_page &&
2174 cpu_buffer->tail_page != next_page)
2175 rb_head_page_set_normal(cpu_buffer, new_head,
2176 next_page,
2177 RB_PAGE_HEAD);
2178 }
2179
2180 /*
2181 * If this was the outer most commit (the one that
2182 * changed the original pointer from HEAD to UPDATE),
2183 * then it is up to us to reset it to NORMAL.
2184 */
2185 if (type == RB_PAGE_HEAD) {
2186 ret = rb_head_page_set_normal(cpu_buffer, next_page,
2187 tail_page,
2188 RB_PAGE_UPDATE);
2189 if (RB_WARN_ON(cpu_buffer,
2190 ret != RB_PAGE_UPDATE))
2191 return -1;
2192 }
2193
2194 return 0;
2195}
2196
Andrew Morton34a148b2009-01-09 12:27:09 -08002197static unsigned rb_calculate_event_length(unsigned length)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002198{
2199 struct ring_buffer_event event; /* Used only for sizeof array */
2200
2201 /* zero length can cause confusions */
2202 if (!length)
Steven Rostedt (Red Hat)a497adb2015-05-29 10:32:28 -04002203 length++;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002204
Steven Rostedt22710482010-03-18 17:54:19 -04002205 if (length > RB_MAX_SMALL_DATA || RB_FORCE_8BYTE_ALIGNMENT)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002206 length += sizeof(event.array[0]);
2207
2208 length += RB_EVNT_HDR_SIZE;
Steven Rostedt22710482010-03-18 17:54:19 -04002209 length = ALIGN(length, RB_ARCH_ALIGNMENT);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002210
2211 return length;
2212}
2213
Steven Rostedtc7b09302009-06-11 11:12:00 -04002214static inline void
2215rb_reset_tail(struct ring_buffer_per_cpu *cpu_buffer,
Steven Rostedt (Red Hat)fcc742e2015-05-28 17:13:14 -04002216 unsigned long tail, struct rb_event_info *info)
Steven Rostedtc7b09302009-06-11 11:12:00 -04002217{
Steven Rostedt (Red Hat)fcc742e2015-05-28 17:13:14 -04002218 struct buffer_page *tail_page = info->tail_page;
Steven Rostedtc7b09302009-06-11 11:12:00 -04002219 struct ring_buffer_event *event;
Steven Rostedt (Red Hat)fcc742e2015-05-28 17:13:14 -04002220 unsigned long length = info->length;
Steven Rostedtc7b09302009-06-11 11:12:00 -04002221
2222 /*
2223 * Only the event that crossed the page boundary
2224 * must fill the old tail_page with padding.
2225 */
2226 if (tail >= BUF_PAGE_SIZE) {
Steven Rostedtb3230c82010-05-21 11:55:21 -04002227 /*
2228 * If the page was filled, then we still need
2229 * to update the real_end. Reset it to zero
2230 * and the reader will ignore it.
2231 */
2232 if (tail == BUF_PAGE_SIZE)
2233 tail_page->real_end = 0;
2234
Steven Rostedtc7b09302009-06-11 11:12:00 -04002235 local_sub(length, &tail_page->write);
2236 return;
2237 }
2238
2239 event = __rb_page_index(tail_page, tail);
Linus Torvaldsb0b70652009-06-20 10:56:46 -07002240 kmemcheck_annotate_bitfield(event, bitfield);
Steven Rostedtc7b09302009-06-11 11:12:00 -04002241
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -07002242 /* account for padding bytes */
2243 local_add(BUF_PAGE_SIZE - tail, &cpu_buffer->entries_bytes);
2244
Steven Rostedtc7b09302009-06-11 11:12:00 -04002245 /*
Steven Rostedtff0ff842010-03-31 22:11:42 -04002246 * Save the original length to the meta data.
2247 * This will be used by the reader to add lost event
2248 * counter.
2249 */
2250 tail_page->real_end = tail;
2251
2252 /*
Steven Rostedtc7b09302009-06-11 11:12:00 -04002253 * If this event is bigger than the minimum size, then
2254 * we need to be careful that we don't subtract the
2255 * write counter enough to allow another writer to slip
2256 * in on this page.
2257 * We put in a discarded commit instead, to make sure
2258 * that this space is not used again.
2259 *
2260 * If we are less than the minimum size, we don't need to
2261 * worry about it.
2262 */
2263 if (tail > (BUF_PAGE_SIZE - RB_EVNT_MIN_SIZE)) {
2264 /* No room for any events */
2265
2266 /* Mark the rest of the page with padding */
2267 rb_event_set_padding(event);
2268
2269 /* Set the write back to the previous setting */
2270 local_sub(length, &tail_page->write);
2271 return;
2272 }
2273
2274 /* Put in a discarded event */
2275 event->array[0] = (BUF_PAGE_SIZE - tail) - RB_EVNT_HDR_SIZE;
2276 event->type_len = RINGBUF_TYPE_PADDING;
2277 /* time delta must be non zero */
2278 event->time_delta = 1;
Steven Rostedtc7b09302009-06-11 11:12:00 -04002279
2280 /* Set write to end of buffer */
2281 length = (tail + length) - BUF_PAGE_SIZE;
2282 local_sub(length, &tail_page->write);
2283}
Steven Rostedt6634ff22009-05-06 15:30:07 -04002284
Steven Rostedt (Red Hat)a4543a22015-05-29 09:40:18 -04002285static inline void rb_end_commit(struct ring_buffer_per_cpu *cpu_buffer);
2286
Steven Rostedt747e94a2010-10-08 13:51:48 -04002287/*
2288 * This is the slow path, force gcc not to inline it.
2289 */
2290static noinline struct ring_buffer_event *
Steven Rostedt6634ff22009-05-06 15:30:07 -04002291rb_move_tail(struct ring_buffer_per_cpu *cpu_buffer,
Steven Rostedt (Red Hat)fcc742e2015-05-28 17:13:14 -04002292 unsigned long tail, struct rb_event_info *info)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002293{
Steven Rostedt (Red Hat)fcc742e2015-05-28 17:13:14 -04002294 struct buffer_page *tail_page = info->tail_page;
Steven Rostedt5a50e332009-11-17 08:43:01 -05002295 struct buffer_page *commit_page = cpu_buffer->commit_page;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002296 struct ring_buffer *buffer = cpu_buffer->buffer;
Steven Rostedt77ae3652009-03-27 11:00:29 -04002297 struct buffer_page *next_page;
2298 int ret;
Steven Rostedt (Red Hat)fcc742e2015-05-28 17:13:14 -04002299 u64 ts;
Steven Rostedtaa20ae82009-05-05 21:16:11 -04002300
Steven Rostedt (Red Hat)a4543a22015-05-29 09:40:18 -04002301 /*
2302 * If the event had a timestamp attached to it, remove it.
2303 * The first event on a page (nested or not) always uses
2304 * the full timestamp of the new page.
2305 */
2306 if (info->add_timestamp) {
2307 info->add_timestamp = 0;
2308 info->length -= RB_LEN_TIME_EXTEND;
2309 }
2310
Steven Rostedtaa20ae82009-05-05 21:16:11 -04002311 next_page = tail_page;
2312
Steven Rostedtaa20ae82009-05-05 21:16:11 -04002313 rb_inc_page(cpu_buffer, &next_page);
2314
Steven Rostedtaa20ae82009-05-05 21:16:11 -04002315 /*
2316 * If for some reason, we had an interrupt storm that made
2317 * it all the way around the buffer, bail, and warn
2318 * about it.
2319 */
2320 if (unlikely(next_page == commit_page)) {
Steven Rostedt77ae3652009-03-27 11:00:29 -04002321 local_inc(&cpu_buffer->commit_overrun);
Steven Rostedtaa20ae82009-05-05 21:16:11 -04002322 goto out_reset;
2323 }
2324
Steven Rostedt77ae3652009-03-27 11:00:29 -04002325 /*
2326 * This is where the fun begins!
2327 *
2328 * We are fighting against races between a reader that
2329 * could be on another CPU trying to swap its reader
2330 * page with the buffer head.
2331 *
2332 * We are also fighting against interrupts coming in and
2333 * moving the head or tail on us as well.
2334 *
2335 * If the next page is the head page then we have filled
2336 * the buffer, unless the commit page is still on the
2337 * reader page.
2338 */
2339 if (rb_is_head_page(cpu_buffer, next_page, &tail_page->list)) {
Steven Rostedtaa20ae82009-05-05 21:16:11 -04002340
Steven Rostedt77ae3652009-03-27 11:00:29 -04002341 /*
2342 * If the commit is not on the reader page, then
2343 * move the header page.
2344 */
2345 if (!rb_is_reader_page(cpu_buffer->commit_page)) {
2346 /*
2347 * If we are not in overwrite mode,
2348 * this is easy, just stop here.
2349 */
Slava Pestov884bfe82011-07-15 14:23:58 -07002350 if (!(buffer->flags & RB_FL_OVERWRITE)) {
2351 local_inc(&cpu_buffer->dropped_events);
Steven Rostedt77ae3652009-03-27 11:00:29 -04002352 goto out_reset;
Slava Pestov884bfe82011-07-15 14:23:58 -07002353 }
Steven Rostedtaa20ae82009-05-05 21:16:11 -04002354
Steven Rostedt77ae3652009-03-27 11:00:29 -04002355 ret = rb_handle_head_page(cpu_buffer,
2356 tail_page,
2357 next_page);
2358 if (ret < 0)
2359 goto out_reset;
2360 if (ret)
2361 goto out_again;
2362 } else {
2363 /*
2364 * We need to be careful here too. The
2365 * commit page could still be on the reader
2366 * page. We could have a small buffer, and
2367 * have filled up the buffer with events
2368 * from interrupts and such, and wrapped.
2369 *
2370 * Note, if the tail page is also the on the
2371 * reader_page, we let it move out.
2372 */
2373 if (unlikely((cpu_buffer->commit_page !=
2374 cpu_buffer->tail_page) &&
2375 (cpu_buffer->commit_page ==
2376 cpu_buffer->reader_page))) {
2377 local_inc(&cpu_buffer->commit_overrun);
2378 goto out_reset;
2379 }
Steven Rostedtaa20ae82009-05-05 21:16:11 -04002380 }
2381 }
2382
Steven Rostedt77ae3652009-03-27 11:00:29 -04002383 ret = rb_tail_page_update(cpu_buffer, tail_page, next_page);
2384 if (ret) {
2385 /*
2386 * Nested commits always have zero deltas, so
2387 * just reread the time stamp
2388 */
Steven Rostedte8bc43e2010-10-20 10:58:02 -04002389 ts = rb_time_stamp(buffer);
2390 next_page->page->time_stamp = ts;
Steven Rostedtaa20ae82009-05-05 21:16:11 -04002391 }
2392
Steven Rostedt77ae3652009-03-27 11:00:29 -04002393 out_again:
Steven Rostedtaa20ae82009-05-05 21:16:11 -04002394
Steven Rostedt (Red Hat)fcc742e2015-05-28 17:13:14 -04002395 rb_reset_tail(cpu_buffer, tail, info);
Steven Rostedtaa20ae82009-05-05 21:16:11 -04002396
Steven Rostedt (Red Hat)a4543a22015-05-29 09:40:18 -04002397 /* Commit what we have for now to update timestamps */
2398 rb_end_commit(cpu_buffer);
2399 /* rb_end_commit() decs committing */
2400 local_inc(&cpu_buffer->committing);
2401
Steven Rostedtaa20ae82009-05-05 21:16:11 -04002402 /* fail and let the caller try again */
2403 return ERR_PTR(-EAGAIN);
2404
Steven Rostedt45141d42009-02-12 13:19:48 -05002405 out_reset:
Lai Jiangshan6f3b3442009-01-12 11:06:18 +08002406 /* reset write */
Steven Rostedt (Red Hat)fcc742e2015-05-28 17:13:14 -04002407 rb_reset_tail(cpu_buffer, tail, info);
Lai Jiangshan6f3b3442009-01-12 11:06:18 +08002408
Steven Rostedtbf41a152008-10-04 02:00:59 -04002409 return NULL;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002410}
2411
Steven Rostedt (Red Hat)9826b272015-05-28 17:36:45 -04002412#ifndef CONFIG_HAVE_UNSTABLE_SCHED_CLOCK
2413static inline bool sched_clock_stable(void)
2414{
2415 return true;
2416}
2417#endif
2418
Steven Rostedt (Red Hat)a4543a22015-05-29 09:40:18 -04002419static inline int
2420rb_try_to_discard(struct ring_buffer_per_cpu *cpu_buffer,
2421 struct ring_buffer_event *event);
2422static inline void rb_event_discard(struct ring_buffer_event *event);
2423static void
2424rb_update_write_stamp(struct ring_buffer_per_cpu *cpu_buffer,
2425 struct ring_buffer_event *event);
2426
Steven Rostedt (Red Hat)9826b272015-05-28 17:36:45 -04002427static noinline void
2428rb_handle_timestamp(struct ring_buffer_per_cpu *cpu_buffer,
Steven Rostedt (Red Hat)a4543a22015-05-29 09:40:18 -04002429 struct ring_buffer_event *event,
Steven Rostedt (Red Hat)9826b272015-05-28 17:36:45 -04002430 struct rb_event_info *info)
2431{
Steven Rostedt (Red Hat)a4543a22015-05-29 09:40:18 -04002432 struct ring_buffer_event *padding;
2433 int length;
2434 int size;
2435
Steven Rostedt (Red Hat)9826b272015-05-28 17:36:45 -04002436 WARN_ONCE(info->delta > (1ULL << 59),
2437 KERN_WARNING "Delta way too big! %llu ts=%llu write stamp = %llu\n%s",
2438 (unsigned long long)info->delta,
2439 (unsigned long long)info->ts,
2440 (unsigned long long)cpu_buffer->write_stamp,
2441 sched_clock_stable() ? "" :
2442 "If you just came from a suspend/resume,\n"
2443 "please switch to the trace global clock:\n"
2444 " echo global > /sys/kernel/debug/tracing/trace_clock\n");
Steven Rostedt (Red Hat)a4543a22015-05-29 09:40:18 -04002445
2446 /*
2447 * Discarding this event to add a timestamp in front, but
2448 * we still need to update the length of it to perform the discard.
2449 */
2450 rb_update_event(cpu_buffer, event, info);
2451
2452 if (rb_try_to_discard(cpu_buffer, event)) {
2453 info->add_timestamp = 1;
2454 /*
2455 * The time delta since the last event is too big to
2456 * hold in the time field of the event, then we append a
2457 * TIME EXTEND event ahead of the data event.
2458 */
2459 info->length += RB_LEN_TIME_EXTEND;
2460 return;
2461 }
2462
2463 /*
2464 * Humpf! An event came in after this one, and because it is not a
2465 * commit, it will have a delta of zero, thus, it will take on
2466 * the timestamp of the previous commit, which happened a long time
2467 * ago (we need to add a timestamp, remember?).
2468 * We need to add the timestamp here. A timestamp is a fixed size
2469 * of 8 bytes. That means the rest of the event needs to be
2470 * padding.
2471 */
2472 size = info->length - RB_LEN_TIME_EXTEND;
2473
2474 /* The padding will have a delta of 1 */
2475 if (size)
2476 info->delta--;
2477
2478 padding = rb_add_time_stamp(event, info->delta);
2479
2480 if (size) {
2481 length = info->length;
2482 info->delta = 0;
2483 info->length = size;
2484 rb_update_event(cpu_buffer, padding, info);
2485
2486 rb_event_discard(padding);
2487
2488 /* Still visible, need to update write_stamp */
2489 rb_update_write_stamp(cpu_buffer, event);
2490
2491 /* Still need to commit the padding. */
2492 rb_end_commit(cpu_buffer);
2493
2494 /* rb_end_commit() decs committing */
2495 local_inc(&cpu_buffer->committing);
2496
2497 /* The next iteration still uses the original length */
2498 info->length = length;
2499 }
Steven Rostedt (Red Hat)9826b272015-05-28 17:36:45 -04002500}
2501
Steven Rostedt6634ff22009-05-06 15:30:07 -04002502static struct ring_buffer_event *
2503__rb_reserve_next(struct ring_buffer_per_cpu *cpu_buffer,
Steven Rostedt (Red Hat)fcc742e2015-05-28 17:13:14 -04002504 struct rb_event_info *info)
Steven Rostedt6634ff22009-05-06 15:30:07 -04002505{
Steven Rostedt6634ff22009-05-06 15:30:07 -04002506 struct ring_buffer_event *event;
Steven Rostedt (Red Hat)fcc742e2015-05-28 17:13:14 -04002507 struct buffer_page *tail_page;
Steven Rostedt6634ff22009-05-06 15:30:07 -04002508 unsigned long tail, write;
Steven Rostedt (Red Hat)a4543a22015-05-29 09:40:18 -04002509 bool is_commit;
Steven Rostedt69d1b832010-10-07 18:18:05 -04002510
Steven Rostedt (Red Hat)fcc742e2015-05-28 17:13:14 -04002511 tail_page = info->tail_page = cpu_buffer->tail_page;
2512 write = local_add_return(info->length, &tail_page->write);
Steven Rostedt77ae3652009-03-27 11:00:29 -04002513
2514 /* set write to only the index of the write */
2515 write &= RB_WRITE_MASK;
Steven Rostedt (Red Hat)fcc742e2015-05-28 17:13:14 -04002516 tail = write - info->length;
Steven Rostedt6634ff22009-05-06 15:30:07 -04002517
2518 /* See if we shot pass the end of this buffer page */
Steven Rostedt747e94a2010-10-08 13:51:48 -04002519 if (unlikely(write > BUF_PAGE_SIZE))
Steven Rostedt (Red Hat)fcc742e2015-05-28 17:13:14 -04002520 return rb_move_tail(cpu_buffer, tail, info);
Steven Rostedt6634ff22009-05-06 15:30:07 -04002521
2522 /* We reserved something on the buffer */
Steven Rostedt6634ff22009-05-06 15:30:07 -04002523 event = __rb_page_index(tail_page, tail);
Steven Rostedt (Red Hat)a4543a22015-05-29 09:40:18 -04002524
2525 /*
2526 * If this is the first commit on the page, then it has the same
2527 * timestamp as the page itself, otherwise we need to figure out
2528 * the delta.
2529 */
2530 info->ts = rb_time_stamp(cpu_buffer->buffer);
2531 is_commit = rb_event_is_commit(cpu_buffer, event);
2532
2533 /* Commits are special (non nested events) */
2534 info->delta = is_commit ? info->ts - cpu_buffer->write_stamp : 0;
2535
2536 if (!tail) {
2537 /*
2538 * If this is the first commit on the page, set the
2539 * page to its timestamp.
2540 */
2541 tail_page->page->time_stamp = info->ts;
2542 info->delta = 0;
2543
2544 } else if (unlikely(test_time_stamp(info->delta)) &&
2545 !info->add_timestamp) {
2546 rb_handle_timestamp(cpu_buffer, event, info);
2547 return ERR_PTR(-EAGAIN);
2548 }
2549
Vegard Nossum1744a212009-02-28 08:29:44 +01002550 kmemcheck_annotate_bitfield(event, bitfield);
Steven Rostedt (Red Hat)fcc742e2015-05-28 17:13:14 -04002551 rb_update_event(cpu_buffer, event, info);
Steven Rostedt6634ff22009-05-06 15:30:07 -04002552
Steven Rostedt69d1b832010-10-07 18:18:05 -04002553 local_inc(&tail_page->entries);
Steven Rostedt6634ff22009-05-06 15:30:07 -04002554
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -07002555 /* account for these added bytes */
Steven Rostedt (Red Hat)fcc742e2015-05-28 17:13:14 -04002556 local_add(info->length, &cpu_buffer->entries_bytes);
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -07002557
Steven Rostedt6634ff22009-05-06 15:30:07 -04002558 return event;
2559}
2560
Steven Rostedtedd813bf2009-06-02 23:00:53 -04002561static inline int
2562rb_try_to_discard(struct ring_buffer_per_cpu *cpu_buffer,
2563 struct ring_buffer_event *event)
2564{
2565 unsigned long new_index, old_index;
2566 struct buffer_page *bpage;
2567 unsigned long index;
2568 unsigned long addr;
2569
2570 new_index = rb_event_index(event);
Steven Rostedt69d1b832010-10-07 18:18:05 -04002571 old_index = new_index + rb_event_ts_length(event);
Steven Rostedtedd813bf2009-06-02 23:00:53 -04002572 addr = (unsigned long)event;
2573 addr &= PAGE_MASK;
2574
2575 bpage = cpu_buffer->tail_page;
2576
2577 if (bpage->page == (void *)addr && rb_page_write(bpage) == old_index) {
Steven Rostedt77ae3652009-03-27 11:00:29 -04002578 unsigned long write_mask =
2579 local_read(&bpage->write) & ~RB_WRITE_MASK;
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -07002580 unsigned long event_length = rb_event_length(event);
Steven Rostedtedd813bf2009-06-02 23:00:53 -04002581 /*
2582 * This is on the tail page. It is possible that
2583 * a write could come in and move the tail page
2584 * and write to the next page. That is fine
2585 * because we just shorten what is on this page.
2586 */
Steven Rostedt77ae3652009-03-27 11:00:29 -04002587 old_index += write_mask;
2588 new_index += write_mask;
Steven Rostedtedd813bf2009-06-02 23:00:53 -04002589 index = local_cmpxchg(&bpage->write, old_index, new_index);
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -07002590 if (index == old_index) {
2591 /* update counters */
2592 local_sub(event_length, &cpu_buffer->entries_bytes);
Steven Rostedtedd813bf2009-06-02 23:00:53 -04002593 return 1;
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -07002594 }
Steven Rostedtedd813bf2009-06-02 23:00:53 -04002595 }
2596
2597 /* could not discard */
2598 return 0;
2599}
2600
Steven Rostedtfa743952009-06-16 12:37:57 -04002601static void rb_start_commit(struct ring_buffer_per_cpu *cpu_buffer)
2602{
2603 local_inc(&cpu_buffer->committing);
2604 local_inc(&cpu_buffer->commits);
2605}
2606
Steven Rostedtd9abde22010-10-19 13:17:08 -04002607static inline void rb_end_commit(struct ring_buffer_per_cpu *cpu_buffer)
Steven Rostedtfa743952009-06-16 12:37:57 -04002608{
2609 unsigned long commits;
2610
2611 if (RB_WARN_ON(cpu_buffer,
2612 !local_read(&cpu_buffer->committing)))
2613 return;
2614
2615 again:
2616 commits = local_read(&cpu_buffer->commits);
2617 /* synchronize with interrupts */
2618 barrier();
2619 if (local_read(&cpu_buffer->committing) == 1)
2620 rb_set_commit_to_write(cpu_buffer);
2621
2622 local_dec(&cpu_buffer->committing);
2623
2624 /* synchronize with interrupts */
2625 barrier();
2626
2627 /*
2628 * Need to account for interrupts coming in between the
2629 * updating of the commit page and the clearing of the
2630 * committing counter.
2631 */
2632 if (unlikely(local_read(&cpu_buffer->commits) != commits) &&
2633 !local_read(&cpu_buffer->committing)) {
2634 local_inc(&cpu_buffer->committing);
2635 goto again;
2636 }
2637}
2638
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002639static struct ring_buffer_event *
Steven Rostedt62f0b3e2009-09-04 14:11:34 -04002640rb_reserve_next_event(struct ring_buffer *buffer,
2641 struct ring_buffer_per_cpu *cpu_buffer,
Steven Rostedt1cd8d732009-05-11 14:08:09 -04002642 unsigned long length)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002643{
2644 struct ring_buffer_event *event;
Steven Rostedt (Red Hat)fcc742e2015-05-28 17:13:14 -04002645 struct rb_event_info info;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002646 int nr_loops = 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002647
Steven Rostedtfa743952009-06-16 12:37:57 -04002648 rb_start_commit(cpu_buffer);
2649
Steven Rostedt85bac322009-09-04 14:24:40 -04002650#ifdef CONFIG_RING_BUFFER_ALLOW_SWAP
Steven Rostedt62f0b3e2009-09-04 14:11:34 -04002651 /*
2652 * Due to the ability to swap a cpu buffer from a buffer
2653 * it is possible it was swapped before we committed.
2654 * (committing stops a swap). We check for it here and
2655 * if it happened, we have to fail the write.
2656 */
2657 barrier();
2658 if (unlikely(ACCESS_ONCE(cpu_buffer->buffer) != buffer)) {
2659 local_dec(&cpu_buffer->committing);
2660 local_dec(&cpu_buffer->commits);
2661 return NULL;
2662 }
Steven Rostedt85bac322009-09-04 14:24:40 -04002663#endif
Steven Rostedt (Red Hat)fcc742e2015-05-28 17:13:14 -04002664 info.length = rb_calculate_event_length(length);
Steven Rostedt (Red Hat)fcc742e2015-05-28 17:13:14 -04002665 info.add_timestamp = 0;
Steven Rostedt (Red Hat)a4543a22015-05-29 09:40:18 -04002666 again:
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002667 /*
2668 * We allow for interrupts to reenter here and do a trace.
2669 * If one does, it will cause this original code to loop
2670 * back here. Even with heavy interrupts happening, this
2671 * should only happen a few times in a row. If this happens
2672 * 1000 times in a row, there must be either an interrupt
2673 * storm or we have something buggy.
2674 * Bail!
2675 */
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05002676 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 1000))
Steven Rostedtfa743952009-06-16 12:37:57 -04002677 goto out_fail;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002678
Steven Rostedt (Red Hat)fcc742e2015-05-28 17:13:14 -04002679 event = __rb_reserve_next(cpu_buffer, &info);
2680
Steven Rostedt168b6b12009-05-11 22:11:05 -04002681 if (unlikely(PTR_ERR(event) == -EAGAIN))
Steven Rostedtbf41a152008-10-04 02:00:59 -04002682 goto again;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002683
Steven Rostedtfa743952009-06-16 12:37:57 -04002684 if (!event)
2685 goto out_fail;
Steven Rostedtbf41a152008-10-04 02:00:59 -04002686
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002687 return event;
Steven Rostedtfa743952009-06-16 12:37:57 -04002688
2689 out_fail:
2690 rb_end_commit(cpu_buffer);
2691 return NULL;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002692}
2693
Steven Rostedt567cd4d2012-11-02 18:33:05 -04002694/*
2695 * The lock and unlock are done within a preempt disable section.
2696 * The current_context per_cpu variable can only be modified
2697 * by the current task between lock and unlock. But it can
2698 * be modified more than once via an interrupt. To pass this
2699 * information from the lock to the unlock without having to
2700 * access the 'in_interrupt()' functions again (which do show
2701 * a bit of overhead in something as critical as function tracing,
2702 * we use a bitmask trick.
2703 *
2704 * bit 0 = NMI context
2705 * bit 1 = IRQ context
2706 * bit 2 = SoftIRQ context
2707 * bit 3 = normal context.
2708 *
2709 * This works because this is the order of contexts that can
2710 * preempt other contexts. A SoftIRQ never preempts an IRQ
2711 * context.
2712 *
2713 * When the context is determined, the corresponding bit is
2714 * checked and set (if it was set, then a recursion of that context
2715 * happened).
2716 *
2717 * On unlock, we need to clear this bit. To do so, just subtract
2718 * 1 from the current_context and AND it to itself.
2719 *
2720 * (binary)
2721 * 101 - 1 = 100
2722 * 101 & 100 = 100 (clearing bit zero)
2723 *
2724 * 1010 - 1 = 1001
2725 * 1010 & 1001 = 1000 (clearing bit 1)
2726 *
2727 * The least significant bit can be cleared this way, and it
2728 * just so happens that it is the same bit corresponding to
2729 * the current context.
2730 */
Steven Rostedt261842b2009-04-16 21:41:52 -04002731
Steven Rostedt (Red Hat)58a09ec2015-05-27 10:27:47 -04002732static __always_inline int
2733trace_recursive_lock(struct ring_buffer_per_cpu *cpu_buffer)
Steven Rostedt261842b2009-04-16 21:41:52 -04002734{
Steven Rostedt (Red Hat)58a09ec2015-05-27 10:27:47 -04002735 unsigned int val = cpu_buffer->current_context;
Steven Rostedt567cd4d2012-11-02 18:33:05 -04002736 int bit;
Frederic Weisbeckere057a5e2009-04-19 23:38:12 +02002737
Steven Rostedt567cd4d2012-11-02 18:33:05 -04002738 if (in_interrupt()) {
2739 if (in_nmi())
Steven Rostedt (Red Hat)a497adb2015-05-29 10:32:28 -04002740 bit = RB_CTX_NMI;
Steven Rostedt567cd4d2012-11-02 18:33:05 -04002741 else if (in_irq())
Steven Rostedt (Red Hat)a497adb2015-05-29 10:32:28 -04002742 bit = RB_CTX_IRQ;
Steven Rostedt567cd4d2012-11-02 18:33:05 -04002743 else
Steven Rostedt (Red Hat)a497adb2015-05-29 10:32:28 -04002744 bit = RB_CTX_SOFTIRQ;
Steven Rostedt567cd4d2012-11-02 18:33:05 -04002745 } else
Steven Rostedt (Red Hat)a497adb2015-05-29 10:32:28 -04002746 bit = RB_CTX_NORMAL;
Frederic Weisbeckere057a5e2009-04-19 23:38:12 +02002747
Steven Rostedt567cd4d2012-11-02 18:33:05 -04002748 if (unlikely(val & (1 << bit)))
2749 return 1;
2750
2751 val |= (1 << bit);
Steven Rostedt (Red Hat)58a09ec2015-05-27 10:27:47 -04002752 cpu_buffer->current_context = val;
Steven Rostedt567cd4d2012-11-02 18:33:05 -04002753
2754 return 0;
Steven Rostedtd9abde22010-10-19 13:17:08 -04002755}
2756
Steven Rostedt (Red Hat)58a09ec2015-05-27 10:27:47 -04002757static __always_inline void
2758trace_recursive_unlock(struct ring_buffer_per_cpu *cpu_buffer)
Steven Rostedtd9abde22010-10-19 13:17:08 -04002759{
Steven Rostedt (Red Hat)58a09ec2015-05-27 10:27:47 -04002760 cpu_buffer->current_context &= cpu_buffer->current_context - 1;
Steven Rostedt261842b2009-04-16 21:41:52 -04002761}
2762
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002763/**
2764 * ring_buffer_lock_reserve - reserve a part of the buffer
2765 * @buffer: the ring buffer to reserve from
2766 * @length: the length of the data to reserve (excluding event header)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002767 *
2768 * Returns a reseverd event on the ring buffer to copy directly to.
2769 * The user of this interface will need to get the body to write into
2770 * and can use the ring_buffer_event_data() interface.
2771 *
2772 * The length is the length of the data needed, not the event length
2773 * which also includes the event header.
2774 *
2775 * Must be paired with ring_buffer_unlock_commit, unless NULL is returned.
2776 * If NULL is returned, then nothing has been allocated or locked.
2777 */
2778struct ring_buffer_event *
Arnaldo Carvalho de Melo0a987752009-02-05 16:12:56 -02002779ring_buffer_lock_reserve(struct ring_buffer *buffer, unsigned long length)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002780{
2781 struct ring_buffer_per_cpu *cpu_buffer;
2782 struct ring_buffer_event *event;
Steven Rostedt5168ae52010-06-03 09:36:50 -04002783 int cpu;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002784
Steven Rostedtbf41a152008-10-04 02:00:59 -04002785 /* If we are tracing schedule, we don't want to recurse */
Steven Rostedt5168ae52010-06-03 09:36:50 -04002786 preempt_disable_notrace();
Steven Rostedtbf41a152008-10-04 02:00:59 -04002787
Steven Rostedt (Red Hat)3205f802015-05-21 17:39:29 -04002788 if (unlikely(atomic_read(&buffer->record_disabled)))
Steven Rostedt (Red Hat)58a09ec2015-05-27 10:27:47 -04002789 goto out;
Steven Rostedt261842b2009-04-16 21:41:52 -04002790
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002791 cpu = raw_smp_processor_id();
2792
Steven Rostedt (Red Hat)3205f802015-05-21 17:39:29 -04002793 if (unlikely(!cpumask_test_cpu(cpu, buffer->cpumask)))
Steven Rostedtd7690412008-10-01 00:29:53 -04002794 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002795
2796 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002797
Steven Rostedt (Red Hat)3205f802015-05-21 17:39:29 -04002798 if (unlikely(atomic_read(&cpu_buffer->record_disabled)))
Steven Rostedtd7690412008-10-01 00:29:53 -04002799 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002800
Steven Rostedt (Red Hat)3205f802015-05-21 17:39:29 -04002801 if (unlikely(length > BUF_MAX_DATA_SIZE))
Steven Rostedtbf41a152008-10-04 02:00:59 -04002802 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002803
Steven Rostedt (Red Hat)58a09ec2015-05-27 10:27:47 -04002804 if (unlikely(trace_recursive_lock(cpu_buffer)))
2805 goto out;
2806
Steven Rostedt62f0b3e2009-09-04 14:11:34 -04002807 event = rb_reserve_next_event(buffer, cpu_buffer, length);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002808 if (!event)
Steven Rostedt (Red Hat)58a09ec2015-05-27 10:27:47 -04002809 goto out_unlock;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002810
2811 return event;
2812
Steven Rostedt (Red Hat)58a09ec2015-05-27 10:27:47 -04002813 out_unlock:
2814 trace_recursive_unlock(cpu_buffer);
Steven Rostedtd7690412008-10-01 00:29:53 -04002815 out:
Steven Rostedt5168ae52010-06-03 09:36:50 -04002816 preempt_enable_notrace();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002817 return NULL;
2818}
Robert Richterc4f50182008-12-11 16:49:22 +01002819EXPORT_SYMBOL_GPL(ring_buffer_lock_reserve);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002820
Steven Rostedta1863c22009-09-03 10:23:58 -04002821static void
2822rb_update_write_stamp(struct ring_buffer_per_cpu *cpu_buffer,
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002823 struct ring_buffer_event *event)
2824{
Steven Rostedt69d1b832010-10-07 18:18:05 -04002825 u64 delta;
2826
Steven Rostedtfa743952009-06-16 12:37:57 -04002827 /*
2828 * The event first in the commit queue updates the
2829 * time stamp.
2830 */
Steven Rostedt69d1b832010-10-07 18:18:05 -04002831 if (rb_event_is_commit(cpu_buffer, event)) {
2832 /*
2833 * A commit event that is first on a page
2834 * updates the write timestamp with the page stamp
2835 */
2836 if (!rb_event_index(event))
2837 cpu_buffer->write_stamp =
2838 cpu_buffer->commit_page->page->time_stamp;
2839 else if (event->type_len == RINGBUF_TYPE_TIME_EXTEND) {
2840 delta = event->array[0];
2841 delta <<= TS_SHIFT;
2842 delta += event->time_delta;
2843 cpu_buffer->write_stamp += delta;
2844 } else
2845 cpu_buffer->write_stamp += event->time_delta;
2846 }
Steven Rostedta1863c22009-09-03 10:23:58 -04002847}
Steven Rostedtbf41a152008-10-04 02:00:59 -04002848
Steven Rostedta1863c22009-09-03 10:23:58 -04002849static void rb_commit(struct ring_buffer_per_cpu *cpu_buffer,
2850 struct ring_buffer_event *event)
2851{
2852 local_inc(&cpu_buffer->entries);
2853 rb_update_write_stamp(cpu_buffer, event);
Steven Rostedtfa743952009-06-16 12:37:57 -04002854 rb_end_commit(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002855}
2856
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -05002857static __always_inline void
2858rb_wakeups(struct ring_buffer *buffer, struct ring_buffer_per_cpu *cpu_buffer)
2859{
Steven Rostedt (Red Hat)1e0d6712015-02-10 22:14:53 -05002860 bool pagebusy;
2861
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -05002862 if (buffer->irq_work.waiters_pending) {
2863 buffer->irq_work.waiters_pending = false;
2864 /* irq_work_queue() supplies it's own memory barriers */
2865 irq_work_queue(&buffer->irq_work.work);
2866 }
2867
2868 if (cpu_buffer->irq_work.waiters_pending) {
2869 cpu_buffer->irq_work.waiters_pending = false;
2870 /* irq_work_queue() supplies it's own memory barriers */
2871 irq_work_queue(&cpu_buffer->irq_work.work);
2872 }
Steven Rostedt (Red Hat)1e0d6712015-02-10 22:14:53 -05002873
2874 pagebusy = cpu_buffer->reader_page == cpu_buffer->commit_page;
2875
2876 if (!pagebusy && cpu_buffer->irq_work.full_waiters_pending) {
2877 cpu_buffer->irq_work.wakeup_full = true;
2878 cpu_buffer->irq_work.full_waiters_pending = false;
2879 /* irq_work_queue() supplies it's own memory barriers */
2880 irq_work_queue(&cpu_buffer->irq_work.work);
2881 }
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -05002882}
2883
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002884/**
2885 * ring_buffer_unlock_commit - commit a reserved
2886 * @buffer: The buffer to commit to
2887 * @event: The event pointer to commit.
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002888 *
2889 * This commits the data to the ring buffer, and releases any locks held.
2890 *
2891 * Must be paired with ring_buffer_lock_reserve.
2892 */
2893int ring_buffer_unlock_commit(struct ring_buffer *buffer,
Arnaldo Carvalho de Melo0a987752009-02-05 16:12:56 -02002894 struct ring_buffer_event *event)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002895{
2896 struct ring_buffer_per_cpu *cpu_buffer;
2897 int cpu = raw_smp_processor_id();
2898
2899 cpu_buffer = buffer->buffers[cpu];
2900
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002901 rb_commit(cpu_buffer, event);
2902
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -05002903 rb_wakeups(buffer, cpu_buffer);
2904
Steven Rostedt (Red Hat)58a09ec2015-05-27 10:27:47 -04002905 trace_recursive_unlock(cpu_buffer);
Steven Rostedt261842b2009-04-16 21:41:52 -04002906
Steven Rostedt5168ae52010-06-03 09:36:50 -04002907 preempt_enable_notrace();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002908
2909 return 0;
2910}
Robert Richterc4f50182008-12-11 16:49:22 +01002911EXPORT_SYMBOL_GPL(ring_buffer_unlock_commit);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002912
Frederic Weisbeckerf3b9aae2009-04-19 23:39:33 +02002913static inline void rb_event_discard(struct ring_buffer_event *event)
2914{
Steven Rostedt69d1b832010-10-07 18:18:05 -04002915 if (event->type_len == RINGBUF_TYPE_TIME_EXTEND)
2916 event = skip_time_extend(event);
2917
Lai Jiangshan334d4162009-04-24 11:27:05 +08002918 /* array[0] holds the actual length for the discarded event */
2919 event->array[0] = rb_event_data_length(event) - RB_EVNT_HDR_SIZE;
2920 event->type_len = RINGBUF_TYPE_PADDING;
Frederic Weisbeckerf3b9aae2009-04-19 23:39:33 +02002921 /* time delta must be non zero */
2922 if (!event->time_delta)
2923 event->time_delta = 1;
2924}
2925
Steven Rostedta1863c22009-09-03 10:23:58 -04002926/*
2927 * Decrement the entries to the page that an event is on.
2928 * The event does not even need to exist, only the pointer
2929 * to the page it is on. This may only be called before the commit
2930 * takes place.
2931 */
2932static inline void
2933rb_decrement_entry(struct ring_buffer_per_cpu *cpu_buffer,
2934 struct ring_buffer_event *event)
2935{
2936 unsigned long addr = (unsigned long)event;
2937 struct buffer_page *bpage = cpu_buffer->commit_page;
2938 struct buffer_page *start;
2939
2940 addr &= PAGE_MASK;
2941
2942 /* Do the likely case first */
2943 if (likely(bpage->page == (void *)addr)) {
2944 local_dec(&bpage->entries);
2945 return;
2946 }
2947
2948 /*
2949 * Because the commit page may be on the reader page we
2950 * start with the next page and check the end loop there.
2951 */
2952 rb_inc_page(cpu_buffer, &bpage);
2953 start = bpage;
2954 do {
2955 if (bpage->page == (void *)addr) {
2956 local_dec(&bpage->entries);
2957 return;
2958 }
2959 rb_inc_page(cpu_buffer, &bpage);
2960 } while (bpage != start);
2961
2962 /* commit not part of this buffer?? */
2963 RB_WARN_ON(cpu_buffer, 1);
2964}
2965
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002966/**
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002967 * ring_buffer_commit_discard - discard an event that has not been committed
2968 * @buffer: the ring buffer
2969 * @event: non committed event to discard
2970 *
Steven Rostedtdc892f72009-09-03 15:33:41 -04002971 * Sometimes an event that is in the ring buffer needs to be ignored.
2972 * This function lets the user discard an event in the ring buffer
2973 * and then that event will not be read later.
2974 *
2975 * This function only works if it is called before the the item has been
2976 * committed. It will try to free the event from the ring buffer
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002977 * if another event has not been added behind it.
2978 *
2979 * If another event has been added behind it, it will set the event
2980 * up as discarded, and perform the commit.
2981 *
2982 * If this function is called, do not call ring_buffer_unlock_commit on
2983 * the event.
2984 */
2985void ring_buffer_discard_commit(struct ring_buffer *buffer,
2986 struct ring_buffer_event *event)
2987{
2988 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002989 int cpu;
2990
2991 /* The event is discarded regardless */
Frederic Weisbeckerf3b9aae2009-04-19 23:39:33 +02002992 rb_event_discard(event);
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002993
Steven Rostedtfa743952009-06-16 12:37:57 -04002994 cpu = smp_processor_id();
2995 cpu_buffer = buffer->buffers[cpu];
2996
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002997 /*
2998 * This must only be called if the event has not been
2999 * committed yet. Thus we can assume that preemption
3000 * is still disabled.
3001 */
Steven Rostedtfa743952009-06-16 12:37:57 -04003002 RB_WARN_ON(buffer, !local_read(&cpu_buffer->committing));
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04003003
Steven Rostedta1863c22009-09-03 10:23:58 -04003004 rb_decrement_entry(cpu_buffer, event);
Steven Rostedt0f2541d2009-08-05 12:02:48 -04003005 if (rb_try_to_discard(cpu_buffer, event))
Steven Rostedtedd813bf2009-06-02 23:00:53 -04003006 goto out;
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04003007
3008 /*
3009 * The commit is still visible by the reader, so we
Steven Rostedta1863c22009-09-03 10:23:58 -04003010 * must still update the timestamp.
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04003011 */
Steven Rostedta1863c22009-09-03 10:23:58 -04003012 rb_update_write_stamp(cpu_buffer, event);
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04003013 out:
Steven Rostedtfa743952009-06-16 12:37:57 -04003014 rb_end_commit(cpu_buffer);
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04003015
Steven Rostedt (Red Hat)58a09ec2015-05-27 10:27:47 -04003016 trace_recursive_unlock(cpu_buffer);
Frederic Weisbeckerf3b9aae2009-04-19 23:39:33 +02003017
Steven Rostedt5168ae52010-06-03 09:36:50 -04003018 preempt_enable_notrace();
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04003019
3020}
3021EXPORT_SYMBOL_GPL(ring_buffer_discard_commit);
3022
3023/**
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003024 * ring_buffer_write - write data to the buffer without reserving
3025 * @buffer: The ring buffer to write to.
3026 * @length: The length of the data being written (excluding the event header)
3027 * @data: The data to write to the buffer.
3028 *
3029 * This is like ring_buffer_lock_reserve and ring_buffer_unlock_commit as
3030 * one function. If you already have the data to write to the buffer, it
3031 * may be easier to simply call this function.
3032 *
3033 * Note, like ring_buffer_lock_reserve, the length is the length of the data
3034 * and not the length of the event which would hold the header.
3035 */
3036int ring_buffer_write(struct ring_buffer *buffer,
David Sharp01e3e712012-06-07 16:46:24 -07003037 unsigned long length,
3038 void *data)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003039{
3040 struct ring_buffer_per_cpu *cpu_buffer;
3041 struct ring_buffer_event *event;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003042 void *body;
3043 int ret = -EBUSY;
Steven Rostedt5168ae52010-06-03 09:36:50 -04003044 int cpu;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003045
Steven Rostedt5168ae52010-06-03 09:36:50 -04003046 preempt_disable_notrace();
Steven Rostedtbf41a152008-10-04 02:00:59 -04003047
Lai Jiangshan52fbe9c2010-03-08 14:50:43 +08003048 if (atomic_read(&buffer->record_disabled))
3049 goto out;
3050
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003051 cpu = raw_smp_processor_id();
3052
Rusty Russell9e01c1b2009-01-01 10:12:22 +10303053 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedtd7690412008-10-01 00:29:53 -04003054 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003055
3056 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003057
3058 if (atomic_read(&cpu_buffer->record_disabled))
3059 goto out;
3060
Steven Rostedtbe957c42009-05-11 14:42:53 -04003061 if (length > BUF_MAX_DATA_SIZE)
3062 goto out;
3063
Steven Rostedt (Red Hat)985e8712015-05-27 10:48:56 -04003064 if (unlikely(trace_recursive_lock(cpu_buffer)))
3065 goto out;
3066
Steven Rostedt62f0b3e2009-09-04 14:11:34 -04003067 event = rb_reserve_next_event(buffer, cpu_buffer, length);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003068 if (!event)
Steven Rostedt (Red Hat)985e8712015-05-27 10:48:56 -04003069 goto out_unlock;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003070
3071 body = rb_event_data(event);
3072
3073 memcpy(body, data, length);
3074
3075 rb_commit(cpu_buffer, event);
3076
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -05003077 rb_wakeups(buffer, cpu_buffer);
3078
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003079 ret = 0;
Steven Rostedt (Red Hat)985e8712015-05-27 10:48:56 -04003080
3081 out_unlock:
3082 trace_recursive_unlock(cpu_buffer);
3083
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003084 out:
Steven Rostedt5168ae52010-06-03 09:36:50 -04003085 preempt_enable_notrace();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003086
3087 return ret;
3088}
Robert Richterc4f50182008-12-11 16:49:22 +01003089EXPORT_SYMBOL_GPL(ring_buffer_write);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003090
Andrew Morton34a148b2009-01-09 12:27:09 -08003091static int rb_per_cpu_empty(struct ring_buffer_per_cpu *cpu_buffer)
Steven Rostedtbf41a152008-10-04 02:00:59 -04003092{
3093 struct buffer_page *reader = cpu_buffer->reader_page;
Steven Rostedt77ae3652009-03-27 11:00:29 -04003094 struct buffer_page *head = rb_set_head_page(cpu_buffer);
Steven Rostedtbf41a152008-10-04 02:00:59 -04003095 struct buffer_page *commit = cpu_buffer->commit_page;
3096
Steven Rostedt77ae3652009-03-27 11:00:29 -04003097 /* In case of error, head will be NULL */
3098 if (unlikely(!head))
3099 return 1;
3100
Steven Rostedtbf41a152008-10-04 02:00:59 -04003101 return reader->read == rb_page_commit(reader) &&
3102 (commit == reader ||
3103 (commit == head &&
3104 head->read == rb_page_commit(commit)));
3105}
3106
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003107/**
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003108 * ring_buffer_record_disable - stop all writes into the buffer
3109 * @buffer: The ring buffer to stop writes to.
3110 *
3111 * This prevents all writes to the buffer. Any attempt to write
3112 * to the buffer after this will fail and return NULL.
3113 *
3114 * The caller should call synchronize_sched() after this.
3115 */
3116void ring_buffer_record_disable(struct ring_buffer *buffer)
3117{
3118 atomic_inc(&buffer->record_disabled);
3119}
Robert Richterc4f50182008-12-11 16:49:22 +01003120EXPORT_SYMBOL_GPL(ring_buffer_record_disable);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003121
3122/**
3123 * ring_buffer_record_enable - enable writes to the buffer
3124 * @buffer: The ring buffer to enable writes
3125 *
3126 * Note, multiple disables will need the same number of enables
Adam Buchbinderc41b20e2009-12-11 16:35:39 -05003127 * to truly enable the writing (much like preempt_disable).
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003128 */
3129void ring_buffer_record_enable(struct ring_buffer *buffer)
3130{
3131 atomic_dec(&buffer->record_disabled);
3132}
Robert Richterc4f50182008-12-11 16:49:22 +01003133EXPORT_SYMBOL_GPL(ring_buffer_record_enable);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003134
3135/**
Steven Rostedt499e5472012-02-22 15:50:28 -05003136 * ring_buffer_record_off - stop all writes into the buffer
3137 * @buffer: The ring buffer to stop writes to.
3138 *
3139 * This prevents all writes to the buffer. Any attempt to write
3140 * to the buffer after this will fail and return NULL.
3141 *
3142 * This is different than ring_buffer_record_disable() as
Wang Tianhong87abb3b2012-08-02 14:02:00 +08003143 * it works like an on/off switch, where as the disable() version
Steven Rostedt499e5472012-02-22 15:50:28 -05003144 * must be paired with a enable().
3145 */
3146void ring_buffer_record_off(struct ring_buffer *buffer)
3147{
3148 unsigned int rd;
3149 unsigned int new_rd;
3150
3151 do {
3152 rd = atomic_read(&buffer->record_disabled);
3153 new_rd = rd | RB_BUFFER_OFF;
3154 } while (atomic_cmpxchg(&buffer->record_disabled, rd, new_rd) != rd);
3155}
3156EXPORT_SYMBOL_GPL(ring_buffer_record_off);
3157
3158/**
3159 * ring_buffer_record_on - restart writes into the buffer
3160 * @buffer: The ring buffer to start writes to.
3161 *
3162 * This enables all writes to the buffer that was disabled by
3163 * ring_buffer_record_off().
3164 *
3165 * This is different than ring_buffer_record_enable() as
Wang Tianhong87abb3b2012-08-02 14:02:00 +08003166 * it works like an on/off switch, where as the enable() version
Steven Rostedt499e5472012-02-22 15:50:28 -05003167 * must be paired with a disable().
3168 */
3169void ring_buffer_record_on(struct ring_buffer *buffer)
3170{
3171 unsigned int rd;
3172 unsigned int new_rd;
3173
3174 do {
3175 rd = atomic_read(&buffer->record_disabled);
3176 new_rd = rd & ~RB_BUFFER_OFF;
3177 } while (atomic_cmpxchg(&buffer->record_disabled, rd, new_rd) != rd);
3178}
3179EXPORT_SYMBOL_GPL(ring_buffer_record_on);
3180
3181/**
3182 * ring_buffer_record_is_on - return true if the ring buffer can write
3183 * @buffer: The ring buffer to see if write is enabled
3184 *
3185 * Returns true if the ring buffer is in a state that it accepts writes.
3186 */
3187int ring_buffer_record_is_on(struct ring_buffer *buffer)
3188{
3189 return !atomic_read(&buffer->record_disabled);
3190}
3191
3192/**
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003193 * ring_buffer_record_disable_cpu - stop all writes into the cpu_buffer
3194 * @buffer: The ring buffer to stop writes to.
3195 * @cpu: The CPU buffer to stop
3196 *
3197 * This prevents all writes to the buffer. Any attempt to write
3198 * to the buffer after this will fail and return NULL.
3199 *
3200 * The caller should call synchronize_sched() after this.
3201 */
3202void ring_buffer_record_disable_cpu(struct ring_buffer *buffer, int cpu)
3203{
3204 struct ring_buffer_per_cpu *cpu_buffer;
3205
Rusty Russell9e01c1b2009-01-01 10:12:22 +10303206 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04003207 return;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003208
3209 cpu_buffer = buffer->buffers[cpu];
3210 atomic_inc(&cpu_buffer->record_disabled);
3211}
Robert Richterc4f50182008-12-11 16:49:22 +01003212EXPORT_SYMBOL_GPL(ring_buffer_record_disable_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003213
3214/**
3215 * ring_buffer_record_enable_cpu - enable writes to the buffer
3216 * @buffer: The ring buffer to enable writes
3217 * @cpu: The CPU to enable.
3218 *
3219 * Note, multiple disables will need the same number of enables
Adam Buchbinderc41b20e2009-12-11 16:35:39 -05003220 * to truly enable the writing (much like preempt_disable).
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003221 */
3222void ring_buffer_record_enable_cpu(struct ring_buffer *buffer, int cpu)
3223{
3224 struct ring_buffer_per_cpu *cpu_buffer;
3225
Rusty Russell9e01c1b2009-01-01 10:12:22 +10303226 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04003227 return;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003228
3229 cpu_buffer = buffer->buffers[cpu];
3230 atomic_dec(&cpu_buffer->record_disabled);
3231}
Robert Richterc4f50182008-12-11 16:49:22 +01003232EXPORT_SYMBOL_GPL(ring_buffer_record_enable_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003233
Steven Rostedtf6195aa2010-09-01 12:23:12 -04003234/*
3235 * The total entries in the ring buffer is the running counter
3236 * of entries entered into the ring buffer, minus the sum of
3237 * the entries read from the ring buffer and the number of
3238 * entries that were overwritten.
3239 */
3240static inline unsigned long
3241rb_num_of_entries(struct ring_buffer_per_cpu *cpu_buffer)
3242{
3243 return local_read(&cpu_buffer->entries) -
3244 (local_read(&cpu_buffer->overrun) + cpu_buffer->read);
3245}
3246
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003247/**
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -07003248 * ring_buffer_oldest_event_ts - get the oldest event timestamp from the buffer
3249 * @buffer: The ring buffer
3250 * @cpu: The per CPU buffer to read from.
3251 */
Yoshihiro YUNOMAE50ecf2c2012-10-11 16:27:54 -07003252u64 ring_buffer_oldest_event_ts(struct ring_buffer *buffer, int cpu)
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -07003253{
3254 unsigned long flags;
3255 struct ring_buffer_per_cpu *cpu_buffer;
3256 struct buffer_page *bpage;
Linus Torvaldsda830e52012-12-11 18:18:58 -08003257 u64 ret = 0;
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -07003258
3259 if (!cpumask_test_cpu(cpu, buffer->cpumask))
3260 return 0;
3261
3262 cpu_buffer = buffer->buffers[cpu];
Linus Torvalds7115e3f2011-10-26 17:03:38 +02003263 raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -07003264 /*
3265 * if the tail is on reader_page, oldest time stamp is on the reader
3266 * page
3267 */
3268 if (cpu_buffer->tail_page == cpu_buffer->reader_page)
3269 bpage = cpu_buffer->reader_page;
3270 else
3271 bpage = rb_set_head_page(cpu_buffer);
Steven Rostedt54f7be52012-11-29 22:27:22 -05003272 if (bpage)
3273 ret = bpage->page->time_stamp;
Linus Torvalds7115e3f2011-10-26 17:03:38 +02003274 raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -07003275
3276 return ret;
3277}
3278EXPORT_SYMBOL_GPL(ring_buffer_oldest_event_ts);
3279
3280/**
3281 * ring_buffer_bytes_cpu - get the number of bytes consumed in a cpu buffer
3282 * @buffer: The ring buffer
3283 * @cpu: The per CPU buffer to read from.
3284 */
3285unsigned long ring_buffer_bytes_cpu(struct ring_buffer *buffer, int cpu)
3286{
3287 struct ring_buffer_per_cpu *cpu_buffer;
3288 unsigned long ret;
3289
3290 if (!cpumask_test_cpu(cpu, buffer->cpumask))
3291 return 0;
3292
3293 cpu_buffer = buffer->buffers[cpu];
3294 ret = local_read(&cpu_buffer->entries_bytes) - cpu_buffer->read_bytes;
3295
3296 return ret;
3297}
3298EXPORT_SYMBOL_GPL(ring_buffer_bytes_cpu);
3299
3300/**
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003301 * ring_buffer_entries_cpu - get the number of entries in a cpu buffer
3302 * @buffer: The ring buffer
3303 * @cpu: The per CPU buffer to get the entries from.
3304 */
3305unsigned long ring_buffer_entries_cpu(struct ring_buffer *buffer, int cpu)
3306{
3307 struct ring_buffer_per_cpu *cpu_buffer;
3308
Rusty Russell9e01c1b2009-01-01 10:12:22 +10303309 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04003310 return 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003311
3312 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt554f7862009-03-11 22:00:13 -04003313
Steven Rostedtf6195aa2010-09-01 12:23:12 -04003314 return rb_num_of_entries(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003315}
Robert Richterc4f50182008-12-11 16:49:22 +01003316EXPORT_SYMBOL_GPL(ring_buffer_entries_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003317
3318/**
Slava Pestov884bfe82011-07-15 14:23:58 -07003319 * ring_buffer_overrun_cpu - get the number of overruns caused by the ring
3320 * buffer wrapping around (only if RB_FL_OVERWRITE is on).
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003321 * @buffer: The ring buffer
3322 * @cpu: The per CPU buffer to get the number of overruns from
3323 */
3324unsigned long ring_buffer_overrun_cpu(struct ring_buffer *buffer, int cpu)
3325{
3326 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedt8aabee52009-03-12 13:13:49 -04003327 unsigned long ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003328
Rusty Russell9e01c1b2009-01-01 10:12:22 +10303329 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04003330 return 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003331
3332 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt77ae3652009-03-27 11:00:29 -04003333 ret = local_read(&cpu_buffer->overrun);
Steven Rostedt554f7862009-03-11 22:00:13 -04003334
3335 return ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003336}
Robert Richterc4f50182008-12-11 16:49:22 +01003337EXPORT_SYMBOL_GPL(ring_buffer_overrun_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003338
3339/**
Slava Pestov884bfe82011-07-15 14:23:58 -07003340 * ring_buffer_commit_overrun_cpu - get the number of overruns caused by
3341 * commits failing due to the buffer wrapping around while there are uncommitted
3342 * events, such as during an interrupt storm.
Steven Rostedtf0d2c682009-04-29 13:43:37 -04003343 * @buffer: The ring buffer
3344 * @cpu: The per CPU buffer to get the number of overruns from
3345 */
3346unsigned long
3347ring_buffer_commit_overrun_cpu(struct ring_buffer *buffer, int cpu)
3348{
3349 struct ring_buffer_per_cpu *cpu_buffer;
3350 unsigned long ret;
3351
3352 if (!cpumask_test_cpu(cpu, buffer->cpumask))
3353 return 0;
3354
3355 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt77ae3652009-03-27 11:00:29 -04003356 ret = local_read(&cpu_buffer->commit_overrun);
Steven Rostedtf0d2c682009-04-29 13:43:37 -04003357
3358 return ret;
3359}
3360EXPORT_SYMBOL_GPL(ring_buffer_commit_overrun_cpu);
3361
3362/**
Slava Pestov884bfe82011-07-15 14:23:58 -07003363 * ring_buffer_dropped_events_cpu - get the number of dropped events caused by
3364 * the ring buffer filling up (only if RB_FL_OVERWRITE is off).
3365 * @buffer: The ring buffer
3366 * @cpu: The per CPU buffer to get the number of overruns from
3367 */
3368unsigned long
3369ring_buffer_dropped_events_cpu(struct ring_buffer *buffer, int cpu)
3370{
3371 struct ring_buffer_per_cpu *cpu_buffer;
3372 unsigned long ret;
3373
3374 if (!cpumask_test_cpu(cpu, buffer->cpumask))
3375 return 0;
3376
3377 cpu_buffer = buffer->buffers[cpu];
3378 ret = local_read(&cpu_buffer->dropped_events);
3379
3380 return ret;
3381}
3382EXPORT_SYMBOL_GPL(ring_buffer_dropped_events_cpu);
3383
3384/**
Steven Rostedt (Red Hat)ad964702013-01-29 17:45:49 -05003385 * ring_buffer_read_events_cpu - get the number of events successfully read
3386 * @buffer: The ring buffer
3387 * @cpu: The per CPU buffer to get the number of events read
3388 */
3389unsigned long
3390ring_buffer_read_events_cpu(struct ring_buffer *buffer, int cpu)
3391{
3392 struct ring_buffer_per_cpu *cpu_buffer;
3393
3394 if (!cpumask_test_cpu(cpu, buffer->cpumask))
3395 return 0;
3396
3397 cpu_buffer = buffer->buffers[cpu];
3398 return cpu_buffer->read;
3399}
3400EXPORT_SYMBOL_GPL(ring_buffer_read_events_cpu);
3401
3402/**
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003403 * ring_buffer_entries - get the number of entries in a buffer
3404 * @buffer: The ring buffer
3405 *
3406 * Returns the total number of entries in the ring buffer
3407 * (all CPU entries)
3408 */
3409unsigned long ring_buffer_entries(struct ring_buffer *buffer)
3410{
3411 struct ring_buffer_per_cpu *cpu_buffer;
3412 unsigned long entries = 0;
3413 int cpu;
3414
3415 /* if you care about this being correct, lock the buffer */
3416 for_each_buffer_cpu(buffer, cpu) {
3417 cpu_buffer = buffer->buffers[cpu];
Steven Rostedtf6195aa2010-09-01 12:23:12 -04003418 entries += rb_num_of_entries(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003419 }
3420
3421 return entries;
3422}
Robert Richterc4f50182008-12-11 16:49:22 +01003423EXPORT_SYMBOL_GPL(ring_buffer_entries);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003424
3425/**
Jiri Olsa67b394f2009-10-23 19:36:18 -04003426 * ring_buffer_overruns - get the number of overruns in buffer
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003427 * @buffer: The ring buffer
3428 *
3429 * Returns the total number of overruns in the ring buffer
3430 * (all CPU entries)
3431 */
3432unsigned long ring_buffer_overruns(struct ring_buffer *buffer)
3433{
3434 struct ring_buffer_per_cpu *cpu_buffer;
3435 unsigned long overruns = 0;
3436 int cpu;
3437
3438 /* if you care about this being correct, lock the buffer */
3439 for_each_buffer_cpu(buffer, cpu) {
3440 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt77ae3652009-03-27 11:00:29 -04003441 overruns += local_read(&cpu_buffer->overrun);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003442 }
3443
3444 return overruns;
3445}
Robert Richterc4f50182008-12-11 16:49:22 +01003446EXPORT_SYMBOL_GPL(ring_buffer_overruns);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003447
Steven Rostedt642edba2008-11-12 00:01:26 -05003448static void rb_iter_reset(struct ring_buffer_iter *iter)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003449{
3450 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
3451
Steven Rostedtd7690412008-10-01 00:29:53 -04003452 /* Iterator usage is expected to have record disabled */
Steven Rostedt (Red Hat)651e22f2014-08-06 14:11:33 -04003453 iter->head_page = cpu_buffer->reader_page;
3454 iter->head = cpu_buffer->reader_page->read;
3455
3456 iter->cache_reader_page = iter->head_page;
Steven Rostedt (Red Hat)24607f12014-10-02 16:51:18 -04003457 iter->cache_read = cpu_buffer->read;
Steven Rostedt (Red Hat)651e22f2014-08-06 14:11:33 -04003458
Steven Rostedtd7690412008-10-01 00:29:53 -04003459 if (iter->head)
3460 iter->read_stamp = cpu_buffer->read_stamp;
3461 else
Steven Rostedtabc9b562008-12-02 15:34:06 -05003462 iter->read_stamp = iter->head_page->page->time_stamp;
Steven Rostedt642edba2008-11-12 00:01:26 -05003463}
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003464
Steven Rostedt642edba2008-11-12 00:01:26 -05003465/**
3466 * ring_buffer_iter_reset - reset an iterator
3467 * @iter: The iterator to reset
3468 *
3469 * Resets the iterator, so that it will start from the beginning
3470 * again.
3471 */
3472void ring_buffer_iter_reset(struct ring_buffer_iter *iter)
3473{
Steven Rostedt554f7862009-03-11 22:00:13 -04003474 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedt642edba2008-11-12 00:01:26 -05003475 unsigned long flags;
3476
Steven Rostedt554f7862009-03-11 22:00:13 -04003477 if (!iter)
3478 return;
3479
3480 cpu_buffer = iter->cpu_buffer;
3481
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02003482 raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
Steven Rostedt642edba2008-11-12 00:01:26 -05003483 rb_iter_reset(iter);
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02003484 raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003485}
Robert Richterc4f50182008-12-11 16:49:22 +01003486EXPORT_SYMBOL_GPL(ring_buffer_iter_reset);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003487
3488/**
3489 * ring_buffer_iter_empty - check if an iterator has no more to read
3490 * @iter: The iterator to check
3491 */
3492int ring_buffer_iter_empty(struct ring_buffer_iter *iter)
3493{
3494 struct ring_buffer_per_cpu *cpu_buffer;
3495
3496 cpu_buffer = iter->cpu_buffer;
3497
Steven Rostedtbf41a152008-10-04 02:00:59 -04003498 return iter->head_page == cpu_buffer->commit_page &&
3499 iter->head == rb_commit_index(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003500}
Robert Richterc4f50182008-12-11 16:49:22 +01003501EXPORT_SYMBOL_GPL(ring_buffer_iter_empty);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003502
3503static void
3504rb_update_read_stamp(struct ring_buffer_per_cpu *cpu_buffer,
3505 struct ring_buffer_event *event)
3506{
3507 u64 delta;
3508
Lai Jiangshan334d4162009-04-24 11:27:05 +08003509 switch (event->type_len) {
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003510 case RINGBUF_TYPE_PADDING:
3511 return;
3512
3513 case RINGBUF_TYPE_TIME_EXTEND:
3514 delta = event->array[0];
3515 delta <<= TS_SHIFT;
3516 delta += event->time_delta;
3517 cpu_buffer->read_stamp += delta;
3518 return;
3519
3520 case RINGBUF_TYPE_TIME_STAMP:
3521 /* FIXME: not implemented */
3522 return;
3523
3524 case RINGBUF_TYPE_DATA:
3525 cpu_buffer->read_stamp += event->time_delta;
3526 return;
3527
3528 default:
3529 BUG();
3530 }
3531 return;
3532}
3533
3534static void
3535rb_update_iter_read_stamp(struct ring_buffer_iter *iter,
3536 struct ring_buffer_event *event)
3537{
3538 u64 delta;
3539
Lai Jiangshan334d4162009-04-24 11:27:05 +08003540 switch (event->type_len) {
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003541 case RINGBUF_TYPE_PADDING:
3542 return;
3543
3544 case RINGBUF_TYPE_TIME_EXTEND:
3545 delta = event->array[0];
3546 delta <<= TS_SHIFT;
3547 delta += event->time_delta;
3548 iter->read_stamp += delta;
3549 return;
3550
3551 case RINGBUF_TYPE_TIME_STAMP:
3552 /* FIXME: not implemented */
3553 return;
3554
3555 case RINGBUF_TYPE_DATA:
3556 iter->read_stamp += event->time_delta;
3557 return;
3558
3559 default:
3560 BUG();
3561 }
3562 return;
3563}
3564
Steven Rostedtd7690412008-10-01 00:29:53 -04003565static struct buffer_page *
3566rb_get_reader_page(struct ring_buffer_per_cpu *cpu_buffer)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003567{
Steven Rostedtd7690412008-10-01 00:29:53 -04003568 struct buffer_page *reader = NULL;
Steven Rostedt66a8cb92010-03-31 13:21:56 -04003569 unsigned long overwrite;
Steven Rostedtd7690412008-10-01 00:29:53 -04003570 unsigned long flags;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003571 int nr_loops = 0;
Steven Rostedt77ae3652009-03-27 11:00:29 -04003572 int ret;
Steven Rostedtd7690412008-10-01 00:29:53 -04003573
Steven Rostedt3e03fb72008-11-06 00:09:43 -05003574 local_irq_save(flags);
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01003575 arch_spin_lock(&cpu_buffer->lock);
Steven Rostedtd7690412008-10-01 00:29:53 -04003576
3577 again:
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003578 /*
3579 * This should normally only loop twice. But because the
3580 * start of the reader inserts an empty page, it causes
3581 * a case where we will loop three times. There should be no
3582 * reason to loop four times (that I know of).
3583 */
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05003584 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 3)) {
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003585 reader = NULL;
3586 goto out;
3587 }
3588
Steven Rostedtd7690412008-10-01 00:29:53 -04003589 reader = cpu_buffer->reader_page;
3590
3591 /* If there's more to read, return this page */
Steven Rostedtbf41a152008-10-04 02:00:59 -04003592 if (cpu_buffer->reader_page->read < rb_page_size(reader))
Steven Rostedtd7690412008-10-01 00:29:53 -04003593 goto out;
3594
3595 /* Never should we have an index greater than the size */
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05003596 if (RB_WARN_ON(cpu_buffer,
3597 cpu_buffer->reader_page->read > rb_page_size(reader)))
3598 goto out;
Steven Rostedtd7690412008-10-01 00:29:53 -04003599
3600 /* check if we caught up to the tail */
3601 reader = NULL;
Steven Rostedtbf41a152008-10-04 02:00:59 -04003602 if (cpu_buffer->commit_page == cpu_buffer->reader_page)
Steven Rostedtd7690412008-10-01 00:29:53 -04003603 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003604
Steven Rostedta5fb8332012-06-28 13:35:04 -04003605 /* Don't bother swapping if the ring buffer is empty */
3606 if (rb_num_of_entries(cpu_buffer) == 0)
3607 goto out;
3608
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003609 /*
Steven Rostedtd7690412008-10-01 00:29:53 -04003610 * Reset the reader page to size zero.
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003611 */
Steven Rostedt77ae3652009-03-27 11:00:29 -04003612 local_set(&cpu_buffer->reader_page->write, 0);
3613 local_set(&cpu_buffer->reader_page->entries, 0);
3614 local_set(&cpu_buffer->reader_page->page->commit, 0);
Steven Rostedtff0ff842010-03-31 22:11:42 -04003615 cpu_buffer->reader_page->real_end = 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003616
Steven Rostedt77ae3652009-03-27 11:00:29 -04003617 spin:
3618 /*
3619 * Splice the empty reader page into the list around the head.
3620 */
3621 reader = rb_set_head_page(cpu_buffer);
Steven Rostedt54f7be52012-11-29 22:27:22 -05003622 if (!reader)
3623 goto out;
Steven Rostedt0e1ff5d2010-01-06 20:40:44 -05003624 cpu_buffer->reader_page->list.next = rb_list_head(reader->list.next);
Steven Rostedtd7690412008-10-01 00:29:53 -04003625 cpu_buffer->reader_page->list.prev = reader->list.prev;
Steven Rostedtbf41a152008-10-04 02:00:59 -04003626
Steven Rostedt3adc54f2009-03-30 15:32:01 -04003627 /*
3628 * cpu_buffer->pages just needs to point to the buffer, it
3629 * has no specific buffer page to point to. Lets move it out
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003630 * of our way so we don't accidentally swap it.
Steven Rostedt3adc54f2009-03-30 15:32:01 -04003631 */
3632 cpu_buffer->pages = reader->list.prev;
3633
Steven Rostedt77ae3652009-03-27 11:00:29 -04003634 /* The reader page will be pointing to the new head */
3635 rb_set_list_to_head(cpu_buffer, &cpu_buffer->reader_page->list);
Steven Rostedtd7690412008-10-01 00:29:53 -04003636
3637 /*
Steven Rostedt66a8cb92010-03-31 13:21:56 -04003638 * We want to make sure we read the overruns after we set up our
3639 * pointers to the next object. The writer side does a
3640 * cmpxchg to cross pages which acts as the mb on the writer
3641 * side. Note, the reader will constantly fail the swap
3642 * while the writer is updating the pointers, so this
3643 * guarantees that the overwrite recorded here is the one we
3644 * want to compare with the last_overrun.
3645 */
3646 smp_mb();
3647 overwrite = local_read(&(cpu_buffer->overrun));
3648
3649 /*
Steven Rostedt77ae3652009-03-27 11:00:29 -04003650 * Here's the tricky part.
3651 *
3652 * We need to move the pointer past the header page.
3653 * But we can only do that if a writer is not currently
3654 * moving it. The page before the header page has the
3655 * flag bit '1' set if it is pointing to the page we want.
3656 * but if the writer is in the process of moving it
3657 * than it will be '2' or already moved '0'.
Steven Rostedtd7690412008-10-01 00:29:53 -04003658 */
Steven Rostedtd7690412008-10-01 00:29:53 -04003659
Steven Rostedt77ae3652009-03-27 11:00:29 -04003660 ret = rb_head_page_replace(reader, cpu_buffer->reader_page);
3661
3662 /*
3663 * If we did not convert it, then we must try again.
3664 */
3665 if (!ret)
3666 goto spin;
3667
3668 /*
3669 * Yeah! We succeeded in replacing the page.
3670 *
3671 * Now make the new head point back to the reader page.
3672 */
David Sharp5ded3dc62010-01-06 17:12:07 -08003673 rb_list_head(reader->list.next)->prev = &cpu_buffer->reader_page->list;
Steven Rostedt77ae3652009-03-27 11:00:29 -04003674 rb_inc_page(cpu_buffer, &cpu_buffer->head_page);
Steven Rostedtd7690412008-10-01 00:29:53 -04003675
3676 /* Finally update the reader page to the new head */
3677 cpu_buffer->reader_page = reader;
3678 rb_reset_reader_page(cpu_buffer);
3679
Steven Rostedt66a8cb92010-03-31 13:21:56 -04003680 if (overwrite != cpu_buffer->last_overrun) {
3681 cpu_buffer->lost_events = overwrite - cpu_buffer->last_overrun;
3682 cpu_buffer->last_overrun = overwrite;
3683 }
3684
Steven Rostedtd7690412008-10-01 00:29:53 -04003685 goto again;
3686
3687 out:
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01003688 arch_spin_unlock(&cpu_buffer->lock);
Steven Rostedt3e03fb72008-11-06 00:09:43 -05003689 local_irq_restore(flags);
Steven Rostedtd7690412008-10-01 00:29:53 -04003690
3691 return reader;
3692}
3693
3694static void rb_advance_reader(struct ring_buffer_per_cpu *cpu_buffer)
3695{
3696 struct ring_buffer_event *event;
3697 struct buffer_page *reader;
3698 unsigned length;
3699
3700 reader = rb_get_reader_page(cpu_buffer);
3701
3702 /* This function should not be called when buffer is empty */
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05003703 if (RB_WARN_ON(cpu_buffer, !reader))
3704 return;
Steven Rostedtd7690412008-10-01 00:29:53 -04003705
3706 event = rb_reader_event(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003707
Steven Rostedta1863c22009-09-03 10:23:58 -04003708 if (event->type_len <= RINGBUF_TYPE_DATA_TYPE_LEN_MAX)
Steven Rostedte4906ef2009-04-30 20:49:44 -04003709 cpu_buffer->read++;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003710
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003711 rb_update_read_stamp(cpu_buffer, event);
3712
Steven Rostedtd7690412008-10-01 00:29:53 -04003713 length = rb_event_length(event);
Steven Rostedt6f807ac2008-10-04 02:00:58 -04003714 cpu_buffer->reader_page->read += length;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003715}
3716
3717static void rb_advance_iter(struct ring_buffer_iter *iter)
3718{
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003719 struct ring_buffer_per_cpu *cpu_buffer;
3720 struct ring_buffer_event *event;
3721 unsigned length;
3722
3723 cpu_buffer = iter->cpu_buffer;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003724
3725 /*
3726 * Check if we are at the end of the buffer.
3727 */
Steven Rostedtbf41a152008-10-04 02:00:59 -04003728 if (iter->head >= rb_page_size(iter->head_page)) {
Steven Rostedtea05b572009-06-03 09:30:10 -04003729 /* discarded commits can make the page empty */
3730 if (iter->head_page == cpu_buffer->commit_page)
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05003731 return;
Steven Rostedtd7690412008-10-01 00:29:53 -04003732 rb_inc_iter(iter);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003733 return;
3734 }
3735
3736 event = rb_iter_head_event(iter);
3737
3738 length = rb_event_length(event);
3739
3740 /*
3741 * This should not be called to advance the header if we are
3742 * at the tail of the buffer.
3743 */
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05003744 if (RB_WARN_ON(cpu_buffer,
Steven Rostedtf536aaf2008-11-10 23:07:30 -05003745 (iter->head_page == cpu_buffer->commit_page) &&
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05003746 (iter->head + length > rb_commit_index(cpu_buffer))))
3747 return;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003748
3749 rb_update_iter_read_stamp(iter, event);
3750
3751 iter->head += length;
3752
3753 /* check for end of page padding */
Steven Rostedtbf41a152008-10-04 02:00:59 -04003754 if ((iter->head >= rb_page_size(iter->head_page)) &&
3755 (iter->head_page != cpu_buffer->commit_page))
Steven Rostedt771e0382012-11-30 10:41:57 -05003756 rb_inc_iter(iter);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003757}
3758
Steven Rostedt66a8cb92010-03-31 13:21:56 -04003759static int rb_lost_events(struct ring_buffer_per_cpu *cpu_buffer)
3760{
3761 return cpu_buffer->lost_events;
3762}
3763
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003764static struct ring_buffer_event *
Steven Rostedt66a8cb92010-03-31 13:21:56 -04003765rb_buffer_peek(struct ring_buffer_per_cpu *cpu_buffer, u64 *ts,
3766 unsigned long *lost_events)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003767{
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003768 struct ring_buffer_event *event;
Steven Rostedtd7690412008-10-01 00:29:53 -04003769 struct buffer_page *reader;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003770 int nr_loops = 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003771
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003772 again:
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003773 /*
Steven Rostedt69d1b832010-10-07 18:18:05 -04003774 * We repeat when a time extend is encountered.
3775 * Since the time extend is always attached to a data event,
3776 * we should never loop more than once.
3777 * (We never hit the following condition more than twice).
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003778 */
Steven Rostedt69d1b832010-10-07 18:18:05 -04003779 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 2))
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003780 return NULL;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003781
Steven Rostedtd7690412008-10-01 00:29:53 -04003782 reader = rb_get_reader_page(cpu_buffer);
3783 if (!reader)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003784 return NULL;
3785
Steven Rostedtd7690412008-10-01 00:29:53 -04003786 event = rb_reader_event(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003787
Lai Jiangshan334d4162009-04-24 11:27:05 +08003788 switch (event->type_len) {
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003789 case RINGBUF_TYPE_PADDING:
Tom Zanussi2d622712009-03-22 03:30:49 -05003790 if (rb_null_event(event))
3791 RB_WARN_ON(cpu_buffer, 1);
3792 /*
3793 * Because the writer could be discarding every
3794 * event it creates (which would probably be bad)
3795 * if we were to go back to "again" then we may never
3796 * catch up, and will trigger the warn on, or lock
3797 * the box. Return the padding, and we will release
3798 * the current locks, and try again.
3799 */
Tom Zanussi2d622712009-03-22 03:30:49 -05003800 return event;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003801
3802 case RINGBUF_TYPE_TIME_EXTEND:
3803 /* Internal data, OK to advance */
Steven Rostedtd7690412008-10-01 00:29:53 -04003804 rb_advance_reader(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003805 goto again;
3806
3807 case RINGBUF_TYPE_TIME_STAMP:
3808 /* FIXME: not implemented */
Steven Rostedtd7690412008-10-01 00:29:53 -04003809 rb_advance_reader(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003810 goto again;
3811
3812 case RINGBUF_TYPE_DATA:
3813 if (ts) {
3814 *ts = cpu_buffer->read_stamp + event->time_delta;
Robert Richterd8eeb2d2009-07-31 14:58:04 +02003815 ring_buffer_normalize_time_stamp(cpu_buffer->buffer,
Steven Rostedt37886f62009-03-17 17:22:06 -04003816 cpu_buffer->cpu, ts);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003817 }
Steven Rostedt66a8cb92010-03-31 13:21:56 -04003818 if (lost_events)
3819 *lost_events = rb_lost_events(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003820 return event;
3821
3822 default:
3823 BUG();
3824 }
3825
3826 return NULL;
3827}
Robert Richterc4f50182008-12-11 16:49:22 +01003828EXPORT_SYMBOL_GPL(ring_buffer_peek);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003829
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003830static struct ring_buffer_event *
3831rb_iter_peek(struct ring_buffer_iter *iter, u64 *ts)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003832{
3833 struct ring_buffer *buffer;
3834 struct ring_buffer_per_cpu *cpu_buffer;
3835 struct ring_buffer_event *event;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003836 int nr_loops = 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003837
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003838 cpu_buffer = iter->cpu_buffer;
3839 buffer = cpu_buffer->buffer;
3840
Steven Rostedt492a74f2010-01-25 15:17:47 -05003841 /*
3842 * Check if someone performed a consuming read to
3843 * the buffer. A consuming read invalidates the iterator
3844 * and we need to reset the iterator in this case.
3845 */
3846 if (unlikely(iter->cache_read != cpu_buffer->read ||
3847 iter->cache_reader_page != cpu_buffer->reader_page))
3848 rb_iter_reset(iter);
3849
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003850 again:
Steven Rostedt3c05d742010-01-26 16:14:08 -05003851 if (ring_buffer_iter_empty(iter))
3852 return NULL;
3853
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003854 /*
Steven Rostedt (Red Hat)021de3d2014-08-06 15:36:31 -04003855 * We repeat when a time extend is encountered or we hit
3856 * the end of the page. Since the time extend is always attached
3857 * to a data event, we should never loop more than three times.
3858 * Once for going to next page, once on time extend, and
3859 * finally once to get the event.
3860 * (We never hit the following condition more than thrice).
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003861 */
Steven Rostedt (Red Hat)021de3d2014-08-06 15:36:31 -04003862 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 3))
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003863 return NULL;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003864
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003865 if (rb_per_cpu_empty(cpu_buffer))
3866 return NULL;
3867
Steven Rostedt (Red Hat)10e83fd2014-07-23 19:45:12 -04003868 if (iter->head >= rb_page_size(iter->head_page)) {
Steven Rostedt3c05d742010-01-26 16:14:08 -05003869 rb_inc_iter(iter);
3870 goto again;
3871 }
3872
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003873 event = rb_iter_head_event(iter);
3874
Lai Jiangshan334d4162009-04-24 11:27:05 +08003875 switch (event->type_len) {
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003876 case RINGBUF_TYPE_PADDING:
Tom Zanussi2d622712009-03-22 03:30:49 -05003877 if (rb_null_event(event)) {
3878 rb_inc_iter(iter);
3879 goto again;
3880 }
3881 rb_advance_iter(iter);
3882 return event;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003883
3884 case RINGBUF_TYPE_TIME_EXTEND:
3885 /* Internal data, OK to advance */
3886 rb_advance_iter(iter);
3887 goto again;
3888
3889 case RINGBUF_TYPE_TIME_STAMP:
3890 /* FIXME: not implemented */
3891 rb_advance_iter(iter);
3892 goto again;
3893
3894 case RINGBUF_TYPE_DATA:
3895 if (ts) {
3896 *ts = iter->read_stamp + event->time_delta;
Steven Rostedt37886f62009-03-17 17:22:06 -04003897 ring_buffer_normalize_time_stamp(buffer,
3898 cpu_buffer->cpu, ts);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003899 }
3900 return event;
3901
3902 default:
3903 BUG();
3904 }
3905
3906 return NULL;
3907}
Robert Richterc4f50182008-12-11 16:49:22 +01003908EXPORT_SYMBOL_GPL(ring_buffer_iter_peek);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003909
Steven Rostedt (Red Hat)289a5a22015-05-28 13:14:51 -04003910static inline bool rb_reader_lock(struct ring_buffer_per_cpu *cpu_buffer)
Steven Rostedt8d707e82009-06-16 21:22:48 -04003911{
Steven Rostedt (Red Hat)289a5a22015-05-28 13:14:51 -04003912 if (likely(!in_nmi())) {
3913 raw_spin_lock(&cpu_buffer->reader_lock);
3914 return true;
3915 }
3916
Steven Rostedt8d707e82009-06-16 21:22:48 -04003917 /*
3918 * If an NMI die dumps out the content of the ring buffer
Steven Rostedt (Red Hat)289a5a22015-05-28 13:14:51 -04003919 * trylock must be used to prevent a deadlock if the NMI
3920 * preempted a task that holds the ring buffer locks. If
3921 * we get the lock then all is fine, if not, then continue
3922 * to do the read, but this can corrupt the ring buffer,
3923 * so it must be permanently disabled from future writes.
3924 * Reading from NMI is a oneshot deal.
Steven Rostedt8d707e82009-06-16 21:22:48 -04003925 */
Steven Rostedt (Red Hat)289a5a22015-05-28 13:14:51 -04003926 if (raw_spin_trylock(&cpu_buffer->reader_lock))
3927 return true;
Steven Rostedt8d707e82009-06-16 21:22:48 -04003928
Steven Rostedt (Red Hat)289a5a22015-05-28 13:14:51 -04003929 /* Continue without locking, but disable the ring buffer */
3930 atomic_inc(&cpu_buffer->record_disabled);
3931 return false;
3932}
3933
3934static inline void
3935rb_reader_unlock(struct ring_buffer_per_cpu *cpu_buffer, bool locked)
3936{
3937 if (likely(locked))
3938 raw_spin_unlock(&cpu_buffer->reader_lock);
3939 return;
Steven Rostedt8d707e82009-06-16 21:22:48 -04003940}
3941
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003942/**
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003943 * ring_buffer_peek - peek at the next event to be read
3944 * @buffer: The ring buffer to read
3945 * @cpu: The cpu to peak at
3946 * @ts: The timestamp counter of this event.
Steven Rostedt66a8cb92010-03-31 13:21:56 -04003947 * @lost_events: a variable to store if events were lost (may be NULL)
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003948 *
3949 * This will return the event that will be read next, but does
3950 * not consume the data.
3951 */
3952struct ring_buffer_event *
Steven Rostedt66a8cb92010-03-31 13:21:56 -04003953ring_buffer_peek(struct ring_buffer *buffer, int cpu, u64 *ts,
3954 unsigned long *lost_events)
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003955{
3956 struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
Steven Rostedt8aabee52009-03-12 13:13:49 -04003957 struct ring_buffer_event *event;
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003958 unsigned long flags;
Steven Rostedt (Red Hat)289a5a22015-05-28 13:14:51 -04003959 bool dolock;
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003960
Steven Rostedt554f7862009-03-11 22:00:13 -04003961 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04003962 return NULL;
Steven Rostedt554f7862009-03-11 22:00:13 -04003963
Tom Zanussi2d622712009-03-22 03:30:49 -05003964 again:
Steven Rostedt8d707e82009-06-16 21:22:48 -04003965 local_irq_save(flags);
Steven Rostedt (Red Hat)289a5a22015-05-28 13:14:51 -04003966 dolock = rb_reader_lock(cpu_buffer);
Steven Rostedt66a8cb92010-03-31 13:21:56 -04003967 event = rb_buffer_peek(cpu_buffer, ts, lost_events);
Robert Richter469535a2009-07-30 19:19:18 +02003968 if (event && event->type_len == RINGBUF_TYPE_PADDING)
3969 rb_advance_reader(cpu_buffer);
Steven Rostedt (Red Hat)289a5a22015-05-28 13:14:51 -04003970 rb_reader_unlock(cpu_buffer, dolock);
Steven Rostedt8d707e82009-06-16 21:22:48 -04003971 local_irq_restore(flags);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003972
Steven Rostedt1b959e12009-09-03 10:12:13 -04003973 if (event && event->type_len == RINGBUF_TYPE_PADDING)
Tom Zanussi2d622712009-03-22 03:30:49 -05003974 goto again;
Tom Zanussi2d622712009-03-22 03:30:49 -05003975
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003976 return event;
3977}
3978
3979/**
3980 * ring_buffer_iter_peek - peek at the next event to be read
3981 * @iter: The ring buffer iterator
3982 * @ts: The timestamp counter of this event.
3983 *
3984 * This will return the event that will be read next, but does
3985 * not increment the iterator.
3986 */
3987struct ring_buffer_event *
3988ring_buffer_iter_peek(struct ring_buffer_iter *iter, u64 *ts)
3989{
3990 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
3991 struct ring_buffer_event *event;
3992 unsigned long flags;
3993
Tom Zanussi2d622712009-03-22 03:30:49 -05003994 again:
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02003995 raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003996 event = rb_iter_peek(iter, ts);
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02003997 raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003998
Steven Rostedt1b959e12009-09-03 10:12:13 -04003999 if (event && event->type_len == RINGBUF_TYPE_PADDING)
Tom Zanussi2d622712009-03-22 03:30:49 -05004000 goto again;
Tom Zanussi2d622712009-03-22 03:30:49 -05004001
Steven Rostedtf83c9d02008-11-11 18:47:44 +01004002 return event;
4003}
4004
4005/**
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004006 * ring_buffer_consume - return an event and consume it
4007 * @buffer: The ring buffer to get the next event from
Steven Rostedt66a8cb92010-03-31 13:21:56 -04004008 * @cpu: the cpu to read the buffer from
4009 * @ts: a variable to store the timestamp (may be NULL)
4010 * @lost_events: a variable to store if events were lost (may be NULL)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004011 *
4012 * Returns the next event in the ring buffer, and that event is consumed.
4013 * Meaning, that sequential reads will keep returning a different event,
4014 * and eventually empty the ring buffer if the producer is slower.
4015 */
4016struct ring_buffer_event *
Steven Rostedt66a8cb92010-03-31 13:21:56 -04004017ring_buffer_consume(struct ring_buffer *buffer, int cpu, u64 *ts,
4018 unsigned long *lost_events)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004019{
Steven Rostedt554f7862009-03-11 22:00:13 -04004020 struct ring_buffer_per_cpu *cpu_buffer;
4021 struct ring_buffer_event *event = NULL;
Steven Rostedtf83c9d02008-11-11 18:47:44 +01004022 unsigned long flags;
Steven Rostedt (Red Hat)289a5a22015-05-28 13:14:51 -04004023 bool dolock;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004024
Tom Zanussi2d622712009-03-22 03:30:49 -05004025 again:
Steven Rostedt554f7862009-03-11 22:00:13 -04004026 /* might be called in atomic */
4027 preempt_disable();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004028
Steven Rostedt554f7862009-03-11 22:00:13 -04004029 if (!cpumask_test_cpu(cpu, buffer->cpumask))
4030 goto out;
4031
4032 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt8d707e82009-06-16 21:22:48 -04004033 local_irq_save(flags);
Steven Rostedt (Red Hat)289a5a22015-05-28 13:14:51 -04004034 dolock = rb_reader_lock(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004035
Steven Rostedt66a8cb92010-03-31 13:21:56 -04004036 event = rb_buffer_peek(cpu_buffer, ts, lost_events);
4037 if (event) {
4038 cpu_buffer->lost_events = 0;
Robert Richter469535a2009-07-30 19:19:18 +02004039 rb_advance_reader(cpu_buffer);
Steven Rostedt66a8cb92010-03-31 13:21:56 -04004040 }
Steven Rostedtf83c9d02008-11-11 18:47:44 +01004041
Steven Rostedt (Red Hat)289a5a22015-05-28 13:14:51 -04004042 rb_reader_unlock(cpu_buffer, dolock);
Steven Rostedt8d707e82009-06-16 21:22:48 -04004043 local_irq_restore(flags);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01004044
Steven Rostedt554f7862009-03-11 22:00:13 -04004045 out:
4046 preempt_enable();
4047
Steven Rostedt1b959e12009-09-03 10:12:13 -04004048 if (event && event->type_len == RINGBUF_TYPE_PADDING)
Tom Zanussi2d622712009-03-22 03:30:49 -05004049 goto again;
Tom Zanussi2d622712009-03-22 03:30:49 -05004050
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004051 return event;
4052}
Robert Richterc4f50182008-12-11 16:49:22 +01004053EXPORT_SYMBOL_GPL(ring_buffer_consume);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004054
4055/**
David Miller72c9ddf2010-04-20 15:47:11 -07004056 * ring_buffer_read_prepare - Prepare for a non consuming read of the buffer
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004057 * @buffer: The ring buffer to read from
4058 * @cpu: The cpu buffer to iterate over
4059 *
David Miller72c9ddf2010-04-20 15:47:11 -07004060 * This performs the initial preparations necessary to iterate
4061 * through the buffer. Memory is allocated, buffer recording
4062 * is disabled, and the iterator pointer is returned to the caller.
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004063 *
David Miller72c9ddf2010-04-20 15:47:11 -07004064 * Disabling buffer recordng prevents the reading from being
4065 * corrupted. This is not a consuming read, so a producer is not
4066 * expected.
4067 *
4068 * After a sequence of ring_buffer_read_prepare calls, the user is
zhangwei(Jovi)d6118512013-07-15 16:32:50 +08004069 * expected to make at least one call to ring_buffer_read_prepare_sync.
David Miller72c9ddf2010-04-20 15:47:11 -07004070 * Afterwards, ring_buffer_read_start is invoked to get things going
4071 * for real.
4072 *
zhangwei(Jovi)d6118512013-07-15 16:32:50 +08004073 * This overall must be paired with ring_buffer_read_finish.
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004074 */
4075struct ring_buffer_iter *
David Miller72c9ddf2010-04-20 15:47:11 -07004076ring_buffer_read_prepare(struct ring_buffer *buffer, int cpu)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004077{
4078 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedt8aabee52009-03-12 13:13:49 -04004079 struct ring_buffer_iter *iter;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004080
Rusty Russell9e01c1b2009-01-01 10:12:22 +10304081 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04004082 return NULL;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004083
4084 iter = kmalloc(sizeof(*iter), GFP_KERNEL);
4085 if (!iter)
Steven Rostedt8aabee52009-03-12 13:13:49 -04004086 return NULL;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004087
4088 cpu_buffer = buffer->buffers[cpu];
4089
4090 iter->cpu_buffer = cpu_buffer;
4091
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07004092 atomic_inc(&buffer->resize_disabled);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004093 atomic_inc(&cpu_buffer->record_disabled);
David Miller72c9ddf2010-04-20 15:47:11 -07004094
4095 return iter;
4096}
4097EXPORT_SYMBOL_GPL(ring_buffer_read_prepare);
4098
4099/**
4100 * ring_buffer_read_prepare_sync - Synchronize a set of prepare calls
4101 *
4102 * All previously invoked ring_buffer_read_prepare calls to prepare
4103 * iterators will be synchronized. Afterwards, read_buffer_read_start
4104 * calls on those iterators are allowed.
4105 */
4106void
4107ring_buffer_read_prepare_sync(void)
4108{
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004109 synchronize_sched();
David Miller72c9ddf2010-04-20 15:47:11 -07004110}
4111EXPORT_SYMBOL_GPL(ring_buffer_read_prepare_sync);
4112
4113/**
4114 * ring_buffer_read_start - start a non consuming read of the buffer
4115 * @iter: The iterator returned by ring_buffer_read_prepare
4116 *
4117 * This finalizes the startup of an iteration through the buffer.
4118 * The iterator comes from a call to ring_buffer_read_prepare and
4119 * an intervening ring_buffer_read_prepare_sync must have been
4120 * performed.
4121 *
zhangwei(Jovi)d6118512013-07-15 16:32:50 +08004122 * Must be paired with ring_buffer_read_finish.
David Miller72c9ddf2010-04-20 15:47:11 -07004123 */
4124void
4125ring_buffer_read_start(struct ring_buffer_iter *iter)
4126{
4127 struct ring_buffer_per_cpu *cpu_buffer;
4128 unsigned long flags;
4129
4130 if (!iter)
4131 return;
4132
4133 cpu_buffer = iter->cpu_buffer;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004134
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02004135 raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01004136 arch_spin_lock(&cpu_buffer->lock);
Steven Rostedt642edba2008-11-12 00:01:26 -05004137 rb_iter_reset(iter);
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01004138 arch_spin_unlock(&cpu_buffer->lock);
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02004139 raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004140}
Robert Richterc4f50182008-12-11 16:49:22 +01004141EXPORT_SYMBOL_GPL(ring_buffer_read_start);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004142
4143/**
zhangwei(Jovi)d6118512013-07-15 16:32:50 +08004144 * ring_buffer_read_finish - finish reading the iterator of the buffer
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004145 * @iter: The iterator retrieved by ring_buffer_start
4146 *
4147 * This re-enables the recording to the buffer, and frees the
4148 * iterator.
4149 */
4150void
4151ring_buffer_read_finish(struct ring_buffer_iter *iter)
4152{
4153 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
Steven Rostedt9366c1b2012-11-29 22:31:16 -05004154 unsigned long flags;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004155
Steven Rostedt659f4512012-05-14 17:02:33 -04004156 /*
4157 * Ring buffer is disabled from recording, here's a good place
Steven Rostedt9366c1b2012-11-29 22:31:16 -05004158 * to check the integrity of the ring buffer.
4159 * Must prevent readers from trying to read, as the check
4160 * clears the HEAD page and readers require it.
Steven Rostedt659f4512012-05-14 17:02:33 -04004161 */
Steven Rostedt9366c1b2012-11-29 22:31:16 -05004162 raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
Steven Rostedt659f4512012-05-14 17:02:33 -04004163 rb_check_pages(cpu_buffer);
Steven Rostedt9366c1b2012-11-29 22:31:16 -05004164 raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
Steven Rostedt659f4512012-05-14 17:02:33 -04004165
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004166 atomic_dec(&cpu_buffer->record_disabled);
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07004167 atomic_dec(&cpu_buffer->buffer->resize_disabled);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004168 kfree(iter);
4169}
Robert Richterc4f50182008-12-11 16:49:22 +01004170EXPORT_SYMBOL_GPL(ring_buffer_read_finish);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004171
4172/**
4173 * ring_buffer_read - read the next item in the ring buffer by the iterator
4174 * @iter: The ring buffer iterator
4175 * @ts: The time stamp of the event read.
4176 *
4177 * This reads the next event in the ring buffer and increments the iterator.
4178 */
4179struct ring_buffer_event *
4180ring_buffer_read(struct ring_buffer_iter *iter, u64 *ts)
4181{
4182 struct ring_buffer_event *event;
Steven Rostedtf83c9d02008-11-11 18:47:44 +01004183 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
4184 unsigned long flags;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004185
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02004186 raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
Steven Rostedt7e9391c2009-09-03 10:02:09 -04004187 again:
Steven Rostedtf83c9d02008-11-11 18:47:44 +01004188 event = rb_iter_peek(iter, ts);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004189 if (!event)
Steven Rostedtf83c9d02008-11-11 18:47:44 +01004190 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004191
Steven Rostedt7e9391c2009-09-03 10:02:09 -04004192 if (event->type_len == RINGBUF_TYPE_PADDING)
4193 goto again;
4194
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004195 rb_advance_iter(iter);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01004196 out:
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02004197 raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004198
4199 return event;
4200}
Robert Richterc4f50182008-12-11 16:49:22 +01004201EXPORT_SYMBOL_GPL(ring_buffer_read);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004202
4203/**
4204 * ring_buffer_size - return the size of the ring buffer (in bytes)
4205 * @buffer: The ring buffer.
4206 */
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08004207unsigned long ring_buffer_size(struct ring_buffer *buffer, int cpu)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004208{
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08004209 /*
4210 * Earlier, this method returned
4211 * BUF_PAGE_SIZE * buffer->nr_pages
4212 * Since the nr_pages field is now removed, we have converted this to
4213 * return the per cpu buffer value.
4214 */
4215 if (!cpumask_test_cpu(cpu, buffer->cpumask))
4216 return 0;
4217
4218 return BUF_PAGE_SIZE * buffer->buffers[cpu]->nr_pages;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004219}
Robert Richterc4f50182008-12-11 16:49:22 +01004220EXPORT_SYMBOL_GPL(ring_buffer_size);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004221
4222static void
4223rb_reset_cpu(struct ring_buffer_per_cpu *cpu_buffer)
4224{
Steven Rostedt77ae3652009-03-27 11:00:29 -04004225 rb_head_page_deactivate(cpu_buffer);
4226
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004227 cpu_buffer->head_page
Steven Rostedt3adc54f2009-03-30 15:32:01 -04004228 = list_entry(cpu_buffer->pages, struct buffer_page, list);
Steven Rostedtbf41a152008-10-04 02:00:59 -04004229 local_set(&cpu_buffer->head_page->write, 0);
Steven Rostedt778c55d2009-05-01 18:44:45 -04004230 local_set(&cpu_buffer->head_page->entries, 0);
Steven Rostedtabc9b562008-12-02 15:34:06 -05004231 local_set(&cpu_buffer->head_page->page->commit, 0);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004232
Steven Rostedt6f807ac2008-10-04 02:00:58 -04004233 cpu_buffer->head_page->read = 0;
Steven Rostedtbf41a152008-10-04 02:00:59 -04004234
4235 cpu_buffer->tail_page = cpu_buffer->head_page;
4236 cpu_buffer->commit_page = cpu_buffer->head_page;
4237
4238 INIT_LIST_HEAD(&cpu_buffer->reader_page->list);
Vaibhav Nagarnaik5040b4b2012-05-03 18:59:51 -07004239 INIT_LIST_HEAD(&cpu_buffer->new_pages);
Steven Rostedtbf41a152008-10-04 02:00:59 -04004240 local_set(&cpu_buffer->reader_page->write, 0);
Steven Rostedt778c55d2009-05-01 18:44:45 -04004241 local_set(&cpu_buffer->reader_page->entries, 0);
Steven Rostedtabc9b562008-12-02 15:34:06 -05004242 local_set(&cpu_buffer->reader_page->page->commit, 0);
Steven Rostedt6f807ac2008-10-04 02:00:58 -04004243 cpu_buffer->reader_page->read = 0;
Steven Rostedtd7690412008-10-01 00:29:53 -04004244
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -07004245 local_set(&cpu_buffer->entries_bytes, 0);
Steven Rostedt77ae3652009-03-27 11:00:29 -04004246 local_set(&cpu_buffer->overrun, 0);
Slava Pestov884bfe82011-07-15 14:23:58 -07004247 local_set(&cpu_buffer->commit_overrun, 0);
4248 local_set(&cpu_buffer->dropped_events, 0);
Steven Rostedte4906ef2009-04-30 20:49:44 -04004249 local_set(&cpu_buffer->entries, 0);
Steven Rostedtfa743952009-06-16 12:37:57 -04004250 local_set(&cpu_buffer->committing, 0);
4251 local_set(&cpu_buffer->commits, 0);
Steven Rostedt77ae3652009-03-27 11:00:29 -04004252 cpu_buffer->read = 0;
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -07004253 cpu_buffer->read_bytes = 0;
Steven Rostedt69507c02009-01-21 18:45:57 -05004254
4255 cpu_buffer->write_stamp = 0;
4256 cpu_buffer->read_stamp = 0;
Steven Rostedt77ae3652009-03-27 11:00:29 -04004257
Steven Rostedt66a8cb92010-03-31 13:21:56 -04004258 cpu_buffer->lost_events = 0;
4259 cpu_buffer->last_overrun = 0;
4260
Steven Rostedt77ae3652009-03-27 11:00:29 -04004261 rb_head_page_activate(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004262}
4263
4264/**
4265 * ring_buffer_reset_cpu - reset a ring buffer per CPU buffer
4266 * @buffer: The ring buffer to reset a per cpu buffer of
4267 * @cpu: The CPU buffer to be reset
4268 */
4269void ring_buffer_reset_cpu(struct ring_buffer *buffer, int cpu)
4270{
4271 struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
4272 unsigned long flags;
4273
Rusty Russell9e01c1b2009-01-01 10:12:22 +10304274 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04004275 return;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004276
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07004277 atomic_inc(&buffer->resize_disabled);
Steven Rostedt41ede232009-05-01 20:26:54 -04004278 atomic_inc(&cpu_buffer->record_disabled);
4279
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07004280 /* Make sure all commits have finished */
4281 synchronize_sched();
4282
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02004283 raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01004284
Steven Rostedt41b6a952009-09-02 09:59:48 -04004285 if (RB_WARN_ON(cpu_buffer, local_read(&cpu_buffer->committing)))
4286 goto out;
4287
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01004288 arch_spin_lock(&cpu_buffer->lock);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004289
4290 rb_reset_cpu(cpu_buffer);
4291
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01004292 arch_spin_unlock(&cpu_buffer->lock);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01004293
Steven Rostedt41b6a952009-09-02 09:59:48 -04004294 out:
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02004295 raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
Steven Rostedt41ede232009-05-01 20:26:54 -04004296
4297 atomic_dec(&cpu_buffer->record_disabled);
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07004298 atomic_dec(&buffer->resize_disabled);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004299}
Robert Richterc4f50182008-12-11 16:49:22 +01004300EXPORT_SYMBOL_GPL(ring_buffer_reset_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004301
4302/**
4303 * ring_buffer_reset - reset a ring buffer
4304 * @buffer: The ring buffer to reset all cpu buffers
4305 */
4306void ring_buffer_reset(struct ring_buffer *buffer)
4307{
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004308 int cpu;
4309
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004310 for_each_buffer_cpu(buffer, cpu)
Steven Rostedtd7690412008-10-01 00:29:53 -04004311 ring_buffer_reset_cpu(buffer, cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004312}
Robert Richterc4f50182008-12-11 16:49:22 +01004313EXPORT_SYMBOL_GPL(ring_buffer_reset);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004314
4315/**
4316 * rind_buffer_empty - is the ring buffer empty?
4317 * @buffer: The ring buffer to test
4318 */
4319int ring_buffer_empty(struct ring_buffer *buffer)
4320{
4321 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedtd4788202009-06-17 00:39:43 -04004322 unsigned long flags;
Steven Rostedt (Red Hat)289a5a22015-05-28 13:14:51 -04004323 bool dolock;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004324 int cpu;
Steven Rostedtd4788202009-06-17 00:39:43 -04004325 int ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004326
4327 /* yes this is racy, but if you don't like the race, lock the buffer */
4328 for_each_buffer_cpu(buffer, cpu) {
4329 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt8d707e82009-06-16 21:22:48 -04004330 local_irq_save(flags);
Steven Rostedt (Red Hat)289a5a22015-05-28 13:14:51 -04004331 dolock = rb_reader_lock(cpu_buffer);
Steven Rostedtd4788202009-06-17 00:39:43 -04004332 ret = rb_per_cpu_empty(cpu_buffer);
Steven Rostedt (Red Hat)289a5a22015-05-28 13:14:51 -04004333 rb_reader_unlock(cpu_buffer, dolock);
Steven Rostedt8d707e82009-06-16 21:22:48 -04004334 local_irq_restore(flags);
4335
Steven Rostedtd4788202009-06-17 00:39:43 -04004336 if (!ret)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004337 return 0;
4338 }
Steven Rostedt554f7862009-03-11 22:00:13 -04004339
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004340 return 1;
4341}
Robert Richterc4f50182008-12-11 16:49:22 +01004342EXPORT_SYMBOL_GPL(ring_buffer_empty);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004343
4344/**
4345 * ring_buffer_empty_cpu - is a cpu buffer of a ring buffer empty?
4346 * @buffer: The ring buffer
4347 * @cpu: The CPU buffer to test
4348 */
4349int ring_buffer_empty_cpu(struct ring_buffer *buffer, int cpu)
4350{
4351 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedtd4788202009-06-17 00:39:43 -04004352 unsigned long flags;
Steven Rostedt (Red Hat)289a5a22015-05-28 13:14:51 -04004353 bool dolock;
Steven Rostedt8aabee52009-03-12 13:13:49 -04004354 int ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004355
Rusty Russell9e01c1b2009-01-01 10:12:22 +10304356 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04004357 return 1;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004358
4359 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt8d707e82009-06-16 21:22:48 -04004360 local_irq_save(flags);
Steven Rostedt (Red Hat)289a5a22015-05-28 13:14:51 -04004361 dolock = rb_reader_lock(cpu_buffer);
Steven Rostedt554f7862009-03-11 22:00:13 -04004362 ret = rb_per_cpu_empty(cpu_buffer);
Steven Rostedt (Red Hat)289a5a22015-05-28 13:14:51 -04004363 rb_reader_unlock(cpu_buffer, dolock);
Steven Rostedt8d707e82009-06-16 21:22:48 -04004364 local_irq_restore(flags);
Steven Rostedt554f7862009-03-11 22:00:13 -04004365
4366 return ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004367}
Robert Richterc4f50182008-12-11 16:49:22 +01004368EXPORT_SYMBOL_GPL(ring_buffer_empty_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004369
Steven Rostedt85bac322009-09-04 14:24:40 -04004370#ifdef CONFIG_RING_BUFFER_ALLOW_SWAP
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004371/**
4372 * ring_buffer_swap_cpu - swap a CPU buffer between two ring buffers
4373 * @buffer_a: One buffer to swap with
4374 * @buffer_b: The other buffer to swap with
4375 *
4376 * This function is useful for tracers that want to take a "snapshot"
4377 * of a CPU buffer and has another back up buffer lying around.
4378 * it is expected that the tracer handles the cpu buffer not being
4379 * used at the moment.
4380 */
4381int ring_buffer_swap_cpu(struct ring_buffer *buffer_a,
4382 struct ring_buffer *buffer_b, int cpu)
4383{
4384 struct ring_buffer_per_cpu *cpu_buffer_a;
4385 struct ring_buffer_per_cpu *cpu_buffer_b;
Steven Rostedt554f7862009-03-11 22:00:13 -04004386 int ret = -EINVAL;
4387
Rusty Russell9e01c1b2009-01-01 10:12:22 +10304388 if (!cpumask_test_cpu(cpu, buffer_a->cpumask) ||
4389 !cpumask_test_cpu(cpu, buffer_b->cpumask))
Steven Rostedt554f7862009-03-11 22:00:13 -04004390 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004391
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08004392 cpu_buffer_a = buffer_a->buffers[cpu];
4393 cpu_buffer_b = buffer_b->buffers[cpu];
4394
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004395 /* At least make sure the two buffers are somewhat the same */
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08004396 if (cpu_buffer_a->nr_pages != cpu_buffer_b->nr_pages)
Steven Rostedt554f7862009-03-11 22:00:13 -04004397 goto out;
4398
4399 ret = -EAGAIN;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004400
Steven Rostedt97b17ef2009-01-21 15:24:56 -05004401 if (atomic_read(&buffer_a->record_disabled))
Steven Rostedt554f7862009-03-11 22:00:13 -04004402 goto out;
Steven Rostedt97b17ef2009-01-21 15:24:56 -05004403
4404 if (atomic_read(&buffer_b->record_disabled))
Steven Rostedt554f7862009-03-11 22:00:13 -04004405 goto out;
Steven Rostedt97b17ef2009-01-21 15:24:56 -05004406
Steven Rostedt97b17ef2009-01-21 15:24:56 -05004407 if (atomic_read(&cpu_buffer_a->record_disabled))
Steven Rostedt554f7862009-03-11 22:00:13 -04004408 goto out;
Steven Rostedt97b17ef2009-01-21 15:24:56 -05004409
4410 if (atomic_read(&cpu_buffer_b->record_disabled))
Steven Rostedt554f7862009-03-11 22:00:13 -04004411 goto out;
Steven Rostedt97b17ef2009-01-21 15:24:56 -05004412
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004413 /*
4414 * We can't do a synchronize_sched here because this
4415 * function can be called in atomic context.
4416 * Normally this will be called from the same CPU as cpu.
4417 * If not it's up to the caller to protect this.
4418 */
4419 atomic_inc(&cpu_buffer_a->record_disabled);
4420 atomic_inc(&cpu_buffer_b->record_disabled);
4421
Steven Rostedt98277992009-09-02 10:56:15 -04004422 ret = -EBUSY;
4423 if (local_read(&cpu_buffer_a->committing))
4424 goto out_dec;
4425 if (local_read(&cpu_buffer_b->committing))
4426 goto out_dec;
4427
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004428 buffer_a->buffers[cpu] = cpu_buffer_b;
4429 buffer_b->buffers[cpu] = cpu_buffer_a;
4430
4431 cpu_buffer_b->buffer = buffer_a;
4432 cpu_buffer_a->buffer = buffer_b;
4433
Steven Rostedt98277992009-09-02 10:56:15 -04004434 ret = 0;
4435
4436out_dec:
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004437 atomic_dec(&cpu_buffer_a->record_disabled);
4438 atomic_dec(&cpu_buffer_b->record_disabled);
Steven Rostedt554f7862009-03-11 22:00:13 -04004439out:
Steven Rostedt554f7862009-03-11 22:00:13 -04004440 return ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004441}
Robert Richterc4f50182008-12-11 16:49:22 +01004442EXPORT_SYMBOL_GPL(ring_buffer_swap_cpu);
Steven Rostedt85bac322009-09-04 14:24:40 -04004443#endif /* CONFIG_RING_BUFFER_ALLOW_SWAP */
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004444
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004445/**
4446 * ring_buffer_alloc_read_page - allocate a page to read from buffer
4447 * @buffer: the buffer to allocate for.
zhangwei(Jovi)d6118512013-07-15 16:32:50 +08004448 * @cpu: the cpu buffer to allocate.
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004449 *
4450 * This function is used in conjunction with ring_buffer_read_page.
4451 * When reading a full page from the ring buffer, these functions
4452 * can be used to speed up the process. The calling function should
4453 * allocate a few pages first with this function. Then when it
4454 * needs to get pages from the ring buffer, it passes the result
4455 * of this function into ring_buffer_read_page, which will swap
4456 * the page that was allocated, with the read page of the buffer.
4457 *
4458 * Returns:
4459 * The page allocated, or NULL on error.
4460 */
Vaibhav Nagarnaik7ea59062011-05-03 17:56:42 -07004461void *ring_buffer_alloc_read_page(struct ring_buffer *buffer, int cpu)
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004462{
Steven Rostedt044fa782008-12-02 23:50:03 -05004463 struct buffer_data_page *bpage;
Vaibhav Nagarnaik7ea59062011-05-03 17:56:42 -07004464 struct page *page;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004465
Vaibhav Nagarnaikd7ec4bf2011-06-07 17:01:42 -07004466 page = alloc_pages_node(cpu_to_node(cpu),
4467 GFP_KERNEL | __GFP_NORETRY, 0);
Vaibhav Nagarnaik7ea59062011-05-03 17:56:42 -07004468 if (!page)
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004469 return NULL;
4470
Vaibhav Nagarnaik7ea59062011-05-03 17:56:42 -07004471 bpage = page_address(page);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004472
Steven Rostedtef7a4a12009-03-03 00:27:49 -05004473 rb_init_page(bpage);
4474
Steven Rostedt044fa782008-12-02 23:50:03 -05004475 return bpage;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004476}
Steven Rostedtd6ce96d2009-05-05 01:15:24 -04004477EXPORT_SYMBOL_GPL(ring_buffer_alloc_read_page);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004478
4479/**
4480 * ring_buffer_free_read_page - free an allocated read page
4481 * @buffer: the buffer the page was allocate for
4482 * @data: the page to free
4483 *
4484 * Free a page allocated from ring_buffer_alloc_read_page.
4485 */
4486void ring_buffer_free_read_page(struct ring_buffer *buffer, void *data)
4487{
4488 free_page((unsigned long)data);
4489}
Steven Rostedtd6ce96d2009-05-05 01:15:24 -04004490EXPORT_SYMBOL_GPL(ring_buffer_free_read_page);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004491
4492/**
4493 * ring_buffer_read_page - extract a page from the ring buffer
4494 * @buffer: buffer to extract from
4495 * @data_page: the page to use allocated from ring_buffer_alloc_read_page
Steven Rostedtef7a4a12009-03-03 00:27:49 -05004496 * @len: amount to extract
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004497 * @cpu: the cpu of the buffer to extract
4498 * @full: should the extraction only happen when the page is full.
4499 *
4500 * This function will pull out a page from the ring buffer and consume it.
4501 * @data_page must be the address of the variable that was returned
4502 * from ring_buffer_alloc_read_page. This is because the page might be used
4503 * to swap with a page in the ring buffer.
4504 *
4505 * for example:
zhangwei(Jovi)d6118512013-07-15 16:32:50 +08004506 * rpage = ring_buffer_alloc_read_page(buffer, cpu);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004507 * if (!rpage)
4508 * return error;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05004509 * ret = ring_buffer_read_page(buffer, &rpage, len, cpu, 0);
Lai Jiangshan667d2412009-02-09 14:21:17 +08004510 * if (ret >= 0)
4511 * process_page(rpage, ret);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004512 *
4513 * When @full is set, the function will not return true unless
4514 * the writer is off the reader page.
4515 *
4516 * Note: it is up to the calling functions to handle sleeps and wakeups.
4517 * The ring buffer can be used anywhere in the kernel and can not
4518 * blindly call wake_up. The layer that uses the ring buffer must be
4519 * responsible for that.
4520 *
4521 * Returns:
Lai Jiangshan667d2412009-02-09 14:21:17 +08004522 * >=0 if data has been transferred, returns the offset of consumed data.
4523 * <0 if no data has been transferred.
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004524 */
4525int ring_buffer_read_page(struct ring_buffer *buffer,
Steven Rostedtef7a4a12009-03-03 00:27:49 -05004526 void **data_page, size_t len, int cpu, int full)
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004527{
4528 struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
4529 struct ring_buffer_event *event;
Steven Rostedt044fa782008-12-02 23:50:03 -05004530 struct buffer_data_page *bpage;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05004531 struct buffer_page *reader;
Steven Rostedtff0ff842010-03-31 22:11:42 -04004532 unsigned long missed_events;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004533 unsigned long flags;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05004534 unsigned int commit;
Lai Jiangshan667d2412009-02-09 14:21:17 +08004535 unsigned int read;
Steven Rostedt4f3640f2009-03-03 23:52:42 -05004536 u64 save_timestamp;
Lai Jiangshan667d2412009-02-09 14:21:17 +08004537 int ret = -1;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004538
Steven Rostedt554f7862009-03-11 22:00:13 -04004539 if (!cpumask_test_cpu(cpu, buffer->cpumask))
4540 goto out;
4541
Steven Rostedt474d32b2009-03-03 19:51:40 -05004542 /*
4543 * If len is not big enough to hold the page header, then
4544 * we can not copy anything.
4545 */
4546 if (len <= BUF_PAGE_HDR_SIZE)
Steven Rostedt554f7862009-03-11 22:00:13 -04004547 goto out;
Steven Rostedt474d32b2009-03-03 19:51:40 -05004548
4549 len -= BUF_PAGE_HDR_SIZE;
4550
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004551 if (!data_page)
Steven Rostedt554f7862009-03-11 22:00:13 -04004552 goto out;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004553
Steven Rostedt044fa782008-12-02 23:50:03 -05004554 bpage = *data_page;
4555 if (!bpage)
Steven Rostedt554f7862009-03-11 22:00:13 -04004556 goto out;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004557
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02004558 raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004559
Steven Rostedtef7a4a12009-03-03 00:27:49 -05004560 reader = rb_get_reader_page(cpu_buffer);
4561 if (!reader)
Steven Rostedt554f7862009-03-11 22:00:13 -04004562 goto out_unlock;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004563
Steven Rostedtef7a4a12009-03-03 00:27:49 -05004564 event = rb_reader_event(cpu_buffer);
Lai Jiangshan667d2412009-02-09 14:21:17 +08004565
Steven Rostedtef7a4a12009-03-03 00:27:49 -05004566 read = reader->read;
4567 commit = rb_page_commit(reader);
4568
Steven Rostedt66a8cb92010-03-31 13:21:56 -04004569 /* Check if any events were dropped */
Steven Rostedtff0ff842010-03-31 22:11:42 -04004570 missed_events = cpu_buffer->lost_events;
Steven Rostedt66a8cb92010-03-31 13:21:56 -04004571
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004572 /*
Steven Rostedt474d32b2009-03-03 19:51:40 -05004573 * If this page has been partially read or
4574 * if len is not big enough to read the rest of the page or
4575 * a writer is still on the page, then
4576 * we must copy the data from the page to the buffer.
4577 * Otherwise, we can simply swap the page with the one passed in.
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004578 */
Steven Rostedt474d32b2009-03-03 19:51:40 -05004579 if (read || (len < (commit - read)) ||
Steven Rostedtef7a4a12009-03-03 00:27:49 -05004580 cpu_buffer->reader_page == cpu_buffer->commit_page) {
Lai Jiangshan667d2412009-02-09 14:21:17 +08004581 struct buffer_data_page *rpage = cpu_buffer->reader_page->page;
Steven Rostedt474d32b2009-03-03 19:51:40 -05004582 unsigned int rpos = read;
4583 unsigned int pos = 0;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05004584 unsigned int size;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004585
4586 if (full)
Steven Rostedt554f7862009-03-11 22:00:13 -04004587 goto out_unlock;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004588
Steven Rostedtef7a4a12009-03-03 00:27:49 -05004589 if (len > (commit - read))
4590 len = (commit - read);
4591
Steven Rostedt69d1b832010-10-07 18:18:05 -04004592 /* Always keep the time extend and data together */
4593 size = rb_event_ts_length(event);
Steven Rostedtef7a4a12009-03-03 00:27:49 -05004594
4595 if (len < size)
Steven Rostedt554f7862009-03-11 22:00:13 -04004596 goto out_unlock;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05004597
Steven Rostedt4f3640f2009-03-03 23:52:42 -05004598 /* save the current timestamp, since the user will need it */
4599 save_timestamp = cpu_buffer->read_stamp;
4600
Steven Rostedtef7a4a12009-03-03 00:27:49 -05004601 /* Need to copy one event at a time */
4602 do {
David Sharpe1e35922010-12-22 16:38:24 -08004603 /* We need the size of one event, because
4604 * rb_advance_reader only advances by one event,
4605 * whereas rb_event_ts_length may include the size of
4606 * one or two events.
4607 * We have already ensured there's enough space if this
4608 * is a time extend. */
4609 size = rb_event_length(event);
Steven Rostedt474d32b2009-03-03 19:51:40 -05004610 memcpy(bpage->data + pos, rpage->data + rpos, size);
Steven Rostedtef7a4a12009-03-03 00:27:49 -05004611
4612 len -= size;
4613
4614 rb_advance_reader(cpu_buffer);
Steven Rostedt474d32b2009-03-03 19:51:40 -05004615 rpos = reader->read;
4616 pos += size;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05004617
Huang Ying18fab912010-07-28 14:14:01 +08004618 if (rpos >= commit)
4619 break;
4620
Steven Rostedtef7a4a12009-03-03 00:27:49 -05004621 event = rb_reader_event(cpu_buffer);
Steven Rostedt69d1b832010-10-07 18:18:05 -04004622 /* Always keep the time extend and data together */
4623 size = rb_event_ts_length(event);
David Sharpe1e35922010-12-22 16:38:24 -08004624 } while (len >= size);
Lai Jiangshan667d2412009-02-09 14:21:17 +08004625
4626 /* update bpage */
Steven Rostedtef7a4a12009-03-03 00:27:49 -05004627 local_set(&bpage->commit, pos);
Steven Rostedt4f3640f2009-03-03 23:52:42 -05004628 bpage->time_stamp = save_timestamp;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05004629
Steven Rostedt474d32b2009-03-03 19:51:40 -05004630 /* we copied everything to the beginning */
4631 read = 0;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004632 } else {
Steven Rostedtafbab762009-05-01 19:40:05 -04004633 /* update the entry counter */
Steven Rostedt77ae3652009-03-27 11:00:29 -04004634 cpu_buffer->read += rb_page_entries(reader);
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -07004635 cpu_buffer->read_bytes += BUF_PAGE_SIZE;
Steven Rostedtafbab762009-05-01 19:40:05 -04004636
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004637 /* swap the pages */
Steven Rostedt044fa782008-12-02 23:50:03 -05004638 rb_init_page(bpage);
Steven Rostedtef7a4a12009-03-03 00:27:49 -05004639 bpage = reader->page;
4640 reader->page = *data_page;
4641 local_set(&reader->write, 0);
Steven Rostedt778c55d2009-05-01 18:44:45 -04004642 local_set(&reader->entries, 0);
Steven Rostedtef7a4a12009-03-03 00:27:49 -05004643 reader->read = 0;
Steven Rostedt044fa782008-12-02 23:50:03 -05004644 *data_page = bpage;
Steven Rostedtff0ff842010-03-31 22:11:42 -04004645
4646 /*
4647 * Use the real_end for the data size,
4648 * This gives us a chance to store the lost events
4649 * on the page.
4650 */
4651 if (reader->real_end)
4652 local_set(&bpage->commit, reader->real_end);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004653 }
Lai Jiangshan667d2412009-02-09 14:21:17 +08004654 ret = read;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004655
Steven Rostedt66a8cb92010-03-31 13:21:56 -04004656 cpu_buffer->lost_events = 0;
Steven Rostedt2711ca22010-05-21 13:32:26 -04004657
4658 commit = local_read(&bpage->commit);
Steven Rostedt66a8cb92010-03-31 13:21:56 -04004659 /*
4660 * Set a flag in the commit field if we lost events
4661 */
Steven Rostedtff0ff842010-03-31 22:11:42 -04004662 if (missed_events) {
Steven Rostedtff0ff842010-03-31 22:11:42 -04004663 /* If there is room at the end of the page to save the
4664 * missed events, then record it there.
4665 */
4666 if (BUF_PAGE_SIZE - commit >= sizeof(missed_events)) {
4667 memcpy(&bpage->data[commit], &missed_events,
4668 sizeof(missed_events));
4669 local_add(RB_MISSED_STORED, &bpage->commit);
Steven Rostedt2711ca22010-05-21 13:32:26 -04004670 commit += sizeof(missed_events);
Steven Rostedtff0ff842010-03-31 22:11:42 -04004671 }
Steven Rostedt66a8cb92010-03-31 13:21:56 -04004672 local_add(RB_MISSED_EVENTS, &bpage->commit);
Steven Rostedtff0ff842010-03-31 22:11:42 -04004673 }
Steven Rostedt66a8cb92010-03-31 13:21:56 -04004674
Steven Rostedt2711ca22010-05-21 13:32:26 -04004675 /*
4676 * This page may be off to user land. Zero it out here.
4677 */
4678 if (commit < BUF_PAGE_SIZE)
4679 memset(&bpage->data[commit], 0, BUF_PAGE_SIZE - commit);
4680
Steven Rostedt554f7862009-03-11 22:00:13 -04004681 out_unlock:
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02004682 raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004683
Steven Rostedt554f7862009-03-11 22:00:13 -04004684 out:
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004685 return ret;
4686}
Steven Rostedtd6ce96d2009-05-05 01:15:24 -04004687EXPORT_SYMBOL_GPL(ring_buffer_read_page);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004688
Steven Rostedt59222ef2009-03-12 11:46:03 -04004689#ifdef CONFIG_HOTPLUG_CPU
Frederic Weisbecker09c9e842009-03-21 04:33:36 +01004690static int rb_cpu_notify(struct notifier_block *self,
4691 unsigned long action, void *hcpu)
Steven Rostedt554f7862009-03-11 22:00:13 -04004692{
4693 struct ring_buffer *buffer =
4694 container_of(self, struct ring_buffer, cpu_notify);
4695 long cpu = (long)hcpu;
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08004696 int cpu_i, nr_pages_same;
4697 unsigned int nr_pages;
Steven Rostedt554f7862009-03-11 22:00:13 -04004698
4699 switch (action) {
4700 case CPU_UP_PREPARE:
4701 case CPU_UP_PREPARE_FROZEN:
Rusty Russell3f237a72009-06-12 21:15:30 +09304702 if (cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt554f7862009-03-11 22:00:13 -04004703 return NOTIFY_OK;
4704
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08004705 nr_pages = 0;
4706 nr_pages_same = 1;
4707 /* check if all cpu sizes are same */
4708 for_each_buffer_cpu(buffer, cpu_i) {
4709 /* fill in the size from first enabled cpu */
4710 if (nr_pages == 0)
4711 nr_pages = buffer->buffers[cpu_i]->nr_pages;
4712 if (nr_pages != buffer->buffers[cpu_i]->nr_pages) {
4713 nr_pages_same = 0;
4714 break;
4715 }
4716 }
4717 /* allocate minimum pages, user can later expand it */
4718 if (!nr_pages_same)
4719 nr_pages = 2;
Steven Rostedt554f7862009-03-11 22:00:13 -04004720 buffer->buffers[cpu] =
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08004721 rb_allocate_cpu_buffer(buffer, nr_pages, cpu);
Steven Rostedt554f7862009-03-11 22:00:13 -04004722 if (!buffer->buffers[cpu]) {
4723 WARN(1, "failed to allocate ring buffer on CPU %ld\n",
4724 cpu);
4725 return NOTIFY_OK;
4726 }
4727 smp_wmb();
Rusty Russell3f237a72009-06-12 21:15:30 +09304728 cpumask_set_cpu(cpu, buffer->cpumask);
Steven Rostedt554f7862009-03-11 22:00:13 -04004729 break;
4730 case CPU_DOWN_PREPARE:
4731 case CPU_DOWN_PREPARE_FROZEN:
4732 /*
4733 * Do nothing.
4734 * If we were to free the buffer, then the user would
4735 * lose any trace that was in the buffer.
4736 */
4737 break;
4738 default:
4739 break;
4740 }
4741 return NOTIFY_OK;
4742}
4743#endif
Steven Rostedt (Red Hat)6c43e552013-03-15 11:32:53 -04004744
4745#ifdef CONFIG_RING_BUFFER_STARTUP_TEST
4746/*
4747 * This is a basic integrity check of the ring buffer.
4748 * Late in the boot cycle this test will run when configured in.
4749 * It will kick off a thread per CPU that will go into a loop
4750 * writing to the per cpu ring buffer various sizes of data.
4751 * Some of the data will be large items, some small.
4752 *
4753 * Another thread is created that goes into a spin, sending out
4754 * IPIs to the other CPUs to also write into the ring buffer.
4755 * this is to test the nesting ability of the buffer.
4756 *
4757 * Basic stats are recorded and reported. If something in the
4758 * ring buffer should happen that's not expected, a big warning
4759 * is displayed and all ring buffers are disabled.
4760 */
4761static struct task_struct *rb_threads[NR_CPUS] __initdata;
4762
4763struct rb_test_data {
4764 struct ring_buffer *buffer;
4765 unsigned long events;
4766 unsigned long bytes_written;
4767 unsigned long bytes_alloc;
4768 unsigned long bytes_dropped;
4769 unsigned long events_nested;
4770 unsigned long bytes_written_nested;
4771 unsigned long bytes_alloc_nested;
4772 unsigned long bytes_dropped_nested;
4773 int min_size_nested;
4774 int max_size_nested;
4775 int max_size;
4776 int min_size;
4777 int cpu;
4778 int cnt;
4779};
4780
4781static struct rb_test_data rb_data[NR_CPUS] __initdata;
4782
4783/* 1 meg per cpu */
4784#define RB_TEST_BUFFER_SIZE 1048576
4785
4786static char rb_string[] __initdata =
4787 "abcdefghijklmnopqrstuvwxyz1234567890!@#$%^&*()?+\\"
4788 "?+|:';\",.<>/?abcdefghijklmnopqrstuvwxyz1234567890"
4789 "!@#$%^&*()?+\\?+|:';\",.<>/?abcdefghijklmnopqrstuv";
4790
4791static bool rb_test_started __initdata;
4792
4793struct rb_item {
4794 int size;
4795 char str[];
4796};
4797
4798static __init int rb_write_something(struct rb_test_data *data, bool nested)
4799{
4800 struct ring_buffer_event *event;
4801 struct rb_item *item;
4802 bool started;
4803 int event_len;
4804 int size;
4805 int len;
4806 int cnt;
4807
4808 /* Have nested writes different that what is written */
4809 cnt = data->cnt + (nested ? 27 : 0);
4810
4811 /* Multiply cnt by ~e, to make some unique increment */
4812 size = (data->cnt * 68 / 25) % (sizeof(rb_string) - 1);
4813
4814 len = size + sizeof(struct rb_item);
4815
4816 started = rb_test_started;
4817 /* read rb_test_started before checking buffer enabled */
4818 smp_rmb();
4819
4820 event = ring_buffer_lock_reserve(data->buffer, len);
4821 if (!event) {
4822 /* Ignore dropped events before test starts. */
4823 if (started) {
4824 if (nested)
4825 data->bytes_dropped += len;
4826 else
4827 data->bytes_dropped_nested += len;
4828 }
4829 return len;
4830 }
4831
4832 event_len = ring_buffer_event_length(event);
4833
4834 if (RB_WARN_ON(data->buffer, event_len < len))
4835 goto out;
4836
4837 item = ring_buffer_event_data(event);
4838 item->size = size;
4839 memcpy(item->str, rb_string, size);
4840
4841 if (nested) {
4842 data->bytes_alloc_nested += event_len;
4843 data->bytes_written_nested += len;
4844 data->events_nested++;
4845 if (!data->min_size_nested || len < data->min_size_nested)
4846 data->min_size_nested = len;
4847 if (len > data->max_size_nested)
4848 data->max_size_nested = len;
4849 } else {
4850 data->bytes_alloc += event_len;
4851 data->bytes_written += len;
4852 data->events++;
4853 if (!data->min_size || len < data->min_size)
4854 data->max_size = len;
4855 if (len > data->max_size)
4856 data->max_size = len;
4857 }
4858
4859 out:
4860 ring_buffer_unlock_commit(data->buffer, event);
4861
4862 return 0;
4863}
4864
4865static __init int rb_test(void *arg)
4866{
4867 struct rb_test_data *data = arg;
4868
4869 while (!kthread_should_stop()) {
4870 rb_write_something(data, false);
4871 data->cnt++;
4872
4873 set_current_state(TASK_INTERRUPTIBLE);
4874 /* Now sleep between a min of 100-300us and a max of 1ms */
4875 usleep_range(((data->cnt % 3) + 1) * 100, 1000);
4876 }
4877
4878 return 0;
4879}
4880
4881static __init void rb_ipi(void *ignore)
4882{
4883 struct rb_test_data *data;
4884 int cpu = smp_processor_id();
4885
4886 data = &rb_data[cpu];
4887 rb_write_something(data, true);
4888}
4889
4890static __init int rb_hammer_test(void *arg)
4891{
4892 while (!kthread_should_stop()) {
4893
4894 /* Send an IPI to all cpus to write data! */
4895 smp_call_function(rb_ipi, NULL, 1);
4896 /* No sleep, but for non preempt, let others run */
4897 schedule();
4898 }
4899
4900 return 0;
4901}
4902
4903static __init int test_ringbuffer(void)
4904{
4905 struct task_struct *rb_hammer;
4906 struct ring_buffer *buffer;
4907 int cpu;
4908 int ret = 0;
4909
4910 pr_info("Running ring buffer tests...\n");
4911
4912 buffer = ring_buffer_alloc(RB_TEST_BUFFER_SIZE, RB_FL_OVERWRITE);
4913 if (WARN_ON(!buffer))
4914 return 0;
4915
4916 /* Disable buffer so that threads can't write to it yet */
4917 ring_buffer_record_off(buffer);
4918
4919 for_each_online_cpu(cpu) {
4920 rb_data[cpu].buffer = buffer;
4921 rb_data[cpu].cpu = cpu;
4922 rb_data[cpu].cnt = cpu;
4923 rb_threads[cpu] = kthread_create(rb_test, &rb_data[cpu],
4924 "rbtester/%d", cpu);
4925 if (WARN_ON(!rb_threads[cpu])) {
4926 pr_cont("FAILED\n");
4927 ret = -1;
4928 goto out_free;
4929 }
4930
4931 kthread_bind(rb_threads[cpu], cpu);
4932 wake_up_process(rb_threads[cpu]);
4933 }
4934
4935 /* Now create the rb hammer! */
4936 rb_hammer = kthread_run(rb_hammer_test, NULL, "rbhammer");
4937 if (WARN_ON(!rb_hammer)) {
4938 pr_cont("FAILED\n");
4939 ret = -1;
4940 goto out_free;
4941 }
4942
4943 ring_buffer_record_on(buffer);
4944 /*
4945 * Show buffer is enabled before setting rb_test_started.
4946 * Yes there's a small race window where events could be
4947 * dropped and the thread wont catch it. But when a ring
4948 * buffer gets enabled, there will always be some kind of
4949 * delay before other CPUs see it. Thus, we don't care about
4950 * those dropped events. We care about events dropped after
4951 * the threads see that the buffer is active.
4952 */
4953 smp_wmb();
4954 rb_test_started = true;
4955
4956 set_current_state(TASK_INTERRUPTIBLE);
4957 /* Just run for 10 seconds */;
4958 schedule_timeout(10 * HZ);
4959
4960 kthread_stop(rb_hammer);
4961
4962 out_free:
4963 for_each_online_cpu(cpu) {
4964 if (!rb_threads[cpu])
4965 break;
4966 kthread_stop(rb_threads[cpu]);
4967 }
4968 if (ret) {
4969 ring_buffer_free(buffer);
4970 return ret;
4971 }
4972
4973 /* Report! */
4974 pr_info("finished\n");
4975 for_each_online_cpu(cpu) {
4976 struct ring_buffer_event *event;
4977 struct rb_test_data *data = &rb_data[cpu];
4978 struct rb_item *item;
4979 unsigned long total_events;
4980 unsigned long total_dropped;
4981 unsigned long total_written;
4982 unsigned long total_alloc;
4983 unsigned long total_read = 0;
4984 unsigned long total_size = 0;
4985 unsigned long total_len = 0;
4986 unsigned long total_lost = 0;
4987 unsigned long lost;
4988 int big_event_size;
4989 int small_event_size;
4990
4991 ret = -1;
4992
4993 total_events = data->events + data->events_nested;
4994 total_written = data->bytes_written + data->bytes_written_nested;
4995 total_alloc = data->bytes_alloc + data->bytes_alloc_nested;
4996 total_dropped = data->bytes_dropped + data->bytes_dropped_nested;
4997
4998 big_event_size = data->max_size + data->max_size_nested;
4999 small_event_size = data->min_size + data->min_size_nested;
5000
5001 pr_info("CPU %d:\n", cpu);
5002 pr_info(" events: %ld\n", total_events);
5003 pr_info(" dropped bytes: %ld\n", total_dropped);
5004 pr_info(" alloced bytes: %ld\n", total_alloc);
5005 pr_info(" written bytes: %ld\n", total_written);
5006 pr_info(" biggest event: %d\n", big_event_size);
5007 pr_info(" smallest event: %d\n", small_event_size);
5008
5009 if (RB_WARN_ON(buffer, total_dropped))
5010 break;
5011
5012 ret = 0;
5013
5014 while ((event = ring_buffer_consume(buffer, cpu, NULL, &lost))) {
5015 total_lost += lost;
5016 item = ring_buffer_event_data(event);
5017 total_len += ring_buffer_event_length(event);
5018 total_size += item->size + sizeof(struct rb_item);
5019 if (memcmp(&item->str[0], rb_string, item->size) != 0) {
5020 pr_info("FAILED!\n");
5021 pr_info("buffer had: %.*s\n", item->size, item->str);
5022 pr_info("expected: %.*s\n", item->size, rb_string);
5023 RB_WARN_ON(buffer, 1);
5024 ret = -1;
5025 break;
5026 }
5027 total_read++;
5028 }
5029 if (ret)
5030 break;
5031
5032 ret = -1;
5033
5034 pr_info(" read events: %ld\n", total_read);
5035 pr_info(" lost events: %ld\n", total_lost);
5036 pr_info(" total events: %ld\n", total_lost + total_read);
5037 pr_info(" recorded len bytes: %ld\n", total_len);
5038 pr_info(" recorded size bytes: %ld\n", total_size);
5039 if (total_lost)
5040 pr_info(" With dropped events, record len and size may not match\n"
5041 " alloced and written from above\n");
5042 if (!total_lost) {
5043 if (RB_WARN_ON(buffer, total_len != total_alloc ||
5044 total_size != total_written))
5045 break;
5046 }
5047 if (RB_WARN_ON(buffer, total_lost + total_read != total_events))
5048 break;
5049
5050 ret = 0;
5051 }
5052 if (!ret)
5053 pr_info("Ring buffer PASSED!\n");
5054
5055 ring_buffer_free(buffer);
5056 return 0;
5057}
5058
5059late_initcall(test_ringbuffer);
5060#endif /* CONFIG_RING_BUFFER_STARTUP_TEST */