blob: 3e1d11f4fe445abbb01329b5476c81e13581a522 [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 Rostedt (VMware)2e0d4582017-12-22 20:32:35 -0500283#define RB_MISSED_FLAGS (RB_MISSED_EVENTS|RB_MISSED_STORED)
284
Steven Rostedtabc9b562008-12-02 15:34:06 -0500285struct buffer_data_page {
Steven Rostedte4c2ce82008-10-01 11:14:54 -0400286 u64 time_stamp; /* page time stamp */
Wenji Huangc3706f02009-02-10 01:03:18 -0500287 local_t commit; /* write committed index */
James Hogan649508f2012-05-30 12:11:19 +0100288 unsigned char data[] RB_ALIGN_DATA; /* data of buffer page */
Steven Rostedtabc9b562008-12-02 15:34:06 -0500289};
290
Steven Rostedt77ae3652009-03-27 11:00:29 -0400291/*
292 * Note, the buffer_page list must be first. The buffer pages
293 * are allocated in cache lines, which means that each buffer
294 * page will be at the beginning of a cache line, and thus
295 * the least significant bits will be zero. We use this to
296 * add flags in the list struct pointers, to make the ring buffer
297 * lockless.
298 */
Steven Rostedtabc9b562008-12-02 15:34:06 -0500299struct buffer_page {
Steven Rostedt778c55d2009-05-01 18:44:45 -0400300 struct list_head list; /* list of buffer pages */
Steven Rostedtabc9b562008-12-02 15:34:06 -0500301 local_t write; /* index for next write */
Steven Rostedt6f807ac2008-10-04 02:00:58 -0400302 unsigned read; /* index for next read */
Steven Rostedt778c55d2009-05-01 18:44:45 -0400303 local_t entries; /* entries on this page */
Steven Rostedtff0ff842010-03-31 22:11:42 -0400304 unsigned long real_end; /* real end of data */
Steven Rostedtabc9b562008-12-02 15:34:06 -0500305 struct buffer_data_page *page; /* Actual data page */
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400306};
307
Steven Rostedt77ae3652009-03-27 11:00:29 -0400308/*
309 * The buffer page counters, write and entries, must be reset
310 * atomically when crossing page boundaries. To synchronize this
311 * update, two counters are inserted into the number. One is
312 * the actual counter for the write position or count on the page.
313 *
314 * The other is a counter of updaters. Before an update happens
315 * the update partition of the counter is incremented. This will
316 * allow the updater to update the counter atomically.
317 *
318 * The counter is 20 bits, and the state data is 12.
319 */
320#define RB_WRITE_MASK 0xfffff
321#define RB_WRITE_INTCNT (1 << 20)
322
Steven Rostedt044fa782008-12-02 23:50:03 -0500323static void rb_init_page(struct buffer_data_page *bpage)
Steven Rostedtabc9b562008-12-02 15:34:06 -0500324{
Steven Rostedt044fa782008-12-02 23:50:03 -0500325 local_set(&bpage->commit, 0);
Steven Rostedtabc9b562008-12-02 15:34:06 -0500326}
327
Steven Rostedt474d32b2009-03-03 19:51:40 -0500328/**
329 * ring_buffer_page_len - the size of data on the page.
330 * @page: The page to read
331 *
332 * Returns the amount of data on the page, including buffer page header.
333 */
Steven Rostedtef7a4a12009-03-03 00:27:49 -0500334size_t ring_buffer_page_len(void *page)
335{
Steven Rostedt (VMware)2e0d4582017-12-22 20:32:35 -0500336 struct buffer_data_page *bpage = page;
337
338 return (local_read(&bpage->commit) & ~RB_MISSED_FLAGS)
Steven Rostedt474d32b2009-03-03 19:51:40 -0500339 + BUF_PAGE_HDR_SIZE;
Steven Rostedtef7a4a12009-03-03 00:27:49 -0500340}
341
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400342/*
Steven Rostedted568292008-09-29 23:02:40 -0400343 * Also stolen from mm/slob.c. Thanks to Mathieu Desnoyers for pointing
344 * this issue out.
345 */
Andrew Morton34a148b2009-01-09 12:27:09 -0800346static void free_buffer_page(struct buffer_page *bpage)
Steven Rostedted568292008-09-29 23:02:40 -0400347{
Andrew Morton34a148b2009-01-09 12:27:09 -0800348 free_page((unsigned long)bpage->page);
Steven Rostedte4c2ce82008-10-01 11:14:54 -0400349 kfree(bpage);
Steven Rostedted568292008-09-29 23:02:40 -0400350}
351
352/*
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400353 * We need to fit the time_stamp delta into 27 bits.
354 */
355static inline int test_time_stamp(u64 delta)
356{
357 if (delta & TS_DELTA_TEST)
358 return 1;
359 return 0;
360}
361
Steven Rostedt474d32b2009-03-03 19:51:40 -0500362#define BUF_PAGE_SIZE (PAGE_SIZE - BUF_PAGE_HDR_SIZE)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400363
Steven Rostedtbe957c42009-05-11 14:42:53 -0400364/* Max payload is BUF_PAGE_SIZE - header (8bytes) */
365#define BUF_MAX_DATA_SIZE (BUF_PAGE_SIZE - (sizeof(u32) * 2))
366
Steven Rostedtd1b182a2009-04-15 16:53:47 -0400367int ring_buffer_print_page_header(struct trace_seq *s)
368{
369 struct buffer_data_page field;
Steven Rostedtd1b182a2009-04-15 16:53:47 -0400370
Steven Rostedt (Red Hat)c0cd93a2014-11-12 11:49:00 -0500371 trace_seq_printf(s, "\tfield: u64 timestamp;\t"
372 "offset:0;\tsize:%u;\tsigned:%u;\n",
373 (unsigned int)sizeof(field.time_stamp),
374 (unsigned int)is_signed_type(u64));
Steven Rostedtd1b182a2009-04-15 16:53:47 -0400375
Steven Rostedt (Red Hat)c0cd93a2014-11-12 11:49:00 -0500376 trace_seq_printf(s, "\tfield: local_t commit;\t"
377 "offset:%u;\tsize:%u;\tsigned:%u;\n",
378 (unsigned int)offsetof(typeof(field), commit),
379 (unsigned int)sizeof(field.commit),
380 (unsigned int)is_signed_type(long));
Steven Rostedtd1b182a2009-04-15 16:53:47 -0400381
Steven Rostedt (Red Hat)c0cd93a2014-11-12 11:49:00 -0500382 trace_seq_printf(s, "\tfield: int overwrite;\t"
383 "offset:%u;\tsize:%u;\tsigned:%u;\n",
384 (unsigned int)offsetof(typeof(field), commit),
385 1,
386 (unsigned int)is_signed_type(long));
Steven Rostedt66a8cb92010-03-31 13:21:56 -0400387
Steven Rostedt (Red Hat)c0cd93a2014-11-12 11:49:00 -0500388 trace_seq_printf(s, "\tfield: char data;\t"
389 "offset:%u;\tsize:%u;\tsigned:%u;\n",
390 (unsigned int)offsetof(typeof(field), data),
391 (unsigned int)BUF_PAGE_SIZE,
392 (unsigned int)is_signed_type(char));
Steven Rostedtd1b182a2009-04-15 16:53:47 -0400393
Steven Rostedt (Red Hat)c0cd93a2014-11-12 11:49:00 -0500394 return !trace_seq_has_overflowed(s);
Steven Rostedtd1b182a2009-04-15 16:53:47 -0400395}
396
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -0500397struct rb_irq_work {
398 struct irq_work work;
399 wait_queue_head_t waiters;
Steven Rostedt (Red Hat)1e0d6712015-02-10 22:14:53 -0500400 wait_queue_head_t full_waiters;
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -0500401 bool waiters_pending;
Steven Rostedt (Red Hat)1e0d6712015-02-10 22:14:53 -0500402 bool full_waiters_pending;
403 bool wakeup_full;
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -0500404};
405
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400406/*
Steven Rostedt (Red Hat)fcc742e2015-05-28 17:13:14 -0400407 * Structure to hold event state and handle nested events.
408 */
409struct rb_event_info {
410 u64 ts;
411 u64 delta;
412 unsigned long length;
413 struct buffer_page *tail_page;
414 int add_timestamp;
415};
416
417/*
Steven Rostedt (Red Hat)a497adb2015-05-29 10:32:28 -0400418 * Used for which event context the event is in.
419 * NMI = 0
420 * IRQ = 1
421 * SOFTIRQ = 2
422 * NORMAL = 3
423 *
424 * See trace_recursive_lock() comment below for more details.
425 */
426enum {
427 RB_CTX_NMI,
428 RB_CTX_IRQ,
429 RB_CTX_SOFTIRQ,
430 RB_CTX_NORMAL,
431 RB_CTX_MAX
432};
433
434/*
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400435 * head_page == tail_page && head == tail then buffer is empty.
436 */
437struct ring_buffer_per_cpu {
438 int cpu;
Richard Kennedy985023d2010-03-25 11:27:36 +0000439 atomic_t record_disabled;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400440 struct ring_buffer *buffer;
Thomas Gleixner5389f6f2009-07-25 17:13:33 +0200441 raw_spinlock_t reader_lock; /* serialize readers */
Thomas Gleixner445c8952009-12-02 19:49:50 +0100442 arch_spinlock_t lock;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400443 struct lock_class_key lock_key;
Steven Rostedt (Red Hat)9b94a8f2016-05-12 11:01:24 -0400444 unsigned long nr_pages;
Steven Rostedt (Red Hat)58a09ec2015-05-27 10:27:47 -0400445 unsigned int current_context;
Steven Rostedt3adc54f2009-03-30 15:32:01 -0400446 struct list_head *pages;
Steven Rostedt6f807ac2008-10-04 02:00:58 -0400447 struct buffer_page *head_page; /* read from head */
448 struct buffer_page *tail_page; /* write to tail */
Wenji Huangc3706f02009-02-10 01:03:18 -0500449 struct buffer_page *commit_page; /* committed pages */
Steven Rostedtd7690412008-10-01 00:29:53 -0400450 struct buffer_page *reader_page;
Steven Rostedt66a8cb92010-03-31 13:21:56 -0400451 unsigned long lost_events;
452 unsigned long last_overrun;
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -0700453 local_t entries_bytes;
Steven Rostedte4906ef2009-04-30 20:49:44 -0400454 local_t entries;
Slava Pestov884bfe82011-07-15 14:23:58 -0700455 local_t overrun;
456 local_t commit_overrun;
457 local_t dropped_events;
Steven Rostedtfa743952009-06-16 12:37:57 -0400458 local_t committing;
459 local_t commits;
Steven Rostedt77ae3652009-03-27 11:00:29 -0400460 unsigned long read;
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -0700461 unsigned long read_bytes;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400462 u64 write_stamp;
463 u64 read_stamp;
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -0800464 /* ring buffer pages to update, > 0 to add, < 0 to remove */
Steven Rostedt (Red Hat)9b94a8f2016-05-12 11:01:24 -0400465 long nr_pages_to_update;
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -0800466 struct list_head new_pages; /* new pages to add */
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -0700467 struct work_struct update_pages_work;
Vaibhav Nagarnaik05fdd702012-05-18 13:29:51 -0700468 struct completion update_done;
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -0500469
470 struct rb_irq_work irq_work;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400471};
472
473struct ring_buffer {
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400474 unsigned flags;
475 int cpus;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400476 atomic_t record_disabled;
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -0700477 atomic_t resize_disabled;
Arnaldo Carvalho de Melo00f62f62009-02-09 17:04:06 -0200478 cpumask_var_t cpumask;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400479
Peter Zijlstra1f8a6a12009-06-08 18:18:39 +0200480 struct lock_class_key *reader_lock_key;
481
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400482 struct mutex mutex;
483
484 struct ring_buffer_per_cpu **buffers;
Steven Rostedt554f7862009-03-11 22:00:13 -0400485
Steven Rostedt59222ef2009-03-12 11:46:03 -0400486#ifdef CONFIG_HOTPLUG_CPU
Steven Rostedt554f7862009-03-11 22:00:13 -0400487 struct notifier_block cpu_notify;
488#endif
Steven Rostedt37886f62009-03-17 17:22:06 -0400489 u64 (*clock)(void);
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -0500490
491 struct rb_irq_work irq_work;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400492};
493
494struct ring_buffer_iter {
495 struct ring_buffer_per_cpu *cpu_buffer;
496 unsigned long head;
497 struct buffer_page *head_page;
Steven Rostedt492a74f2010-01-25 15:17:47 -0500498 struct buffer_page *cache_reader_page;
499 unsigned long cache_read;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400500 u64 read_stamp;
501};
502
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -0500503/*
504 * rb_wake_up_waiters - wake up tasks waiting for ring buffer input
505 *
506 * Schedules a delayed work to wake up any task that is blocked on the
507 * ring buffer waiters queue.
508 */
509static void rb_wake_up_waiters(struct irq_work *work)
510{
511 struct rb_irq_work *rbwork = container_of(work, struct rb_irq_work, work);
512
513 wake_up_all(&rbwork->waiters);
Steven Rostedt (Red Hat)1e0d6712015-02-10 22:14:53 -0500514 if (rbwork->wakeup_full) {
515 rbwork->wakeup_full = false;
516 wake_up_all(&rbwork->full_waiters);
517 }
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -0500518}
519
520/**
521 * ring_buffer_wait - wait for input to the ring buffer
522 * @buffer: buffer to wait on
523 * @cpu: the cpu buffer to wait on
Rabin Vincente30f53a2014-11-10 19:46:34 +0100524 * @full: wait until a full page is available, if @cpu != RING_BUFFER_ALL_CPUS
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -0500525 *
526 * If @cpu == RING_BUFFER_ALL_CPUS then the task will wake up as soon
527 * as data is added to any of the @buffer's cpu buffers. Otherwise
528 * it will wait for data to be added to a specific cpu buffer.
529 */
Rabin Vincente30f53a2014-11-10 19:46:34 +0100530int ring_buffer_wait(struct ring_buffer *buffer, int cpu, bool full)
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -0500531{
Rabin Vincente30f53a2014-11-10 19:46:34 +0100532 struct ring_buffer_per_cpu *uninitialized_var(cpu_buffer);
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -0500533 DEFINE_WAIT(wait);
534 struct rb_irq_work *work;
Rabin Vincente30f53a2014-11-10 19:46:34 +0100535 int ret = 0;
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -0500536
537 /*
538 * Depending on what the caller is waiting for, either any
539 * data in any cpu buffer, or a specific buffer, put the
540 * caller on the appropriate wait queue.
541 */
Steven Rostedt (Red Hat)1e0d6712015-02-10 22:14:53 -0500542 if (cpu == RING_BUFFER_ALL_CPUS) {
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -0500543 work = &buffer->irq_work;
Steven Rostedt (Red Hat)1e0d6712015-02-10 22:14:53 -0500544 /* Full only makes sense on per cpu reads */
545 full = false;
546 } else {
Steven Rostedt (Red Hat)8b8b3682014-06-10 09:46:00 -0400547 if (!cpumask_test_cpu(cpu, buffer->cpumask))
548 return -ENODEV;
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -0500549 cpu_buffer = buffer->buffers[cpu];
550 work = &cpu_buffer->irq_work;
551 }
552
553
Rabin Vincente30f53a2014-11-10 19:46:34 +0100554 while (true) {
Steven Rostedt (Red Hat)1e0d6712015-02-10 22:14:53 -0500555 if (full)
556 prepare_to_wait(&work->full_waiters, &wait, TASK_INTERRUPTIBLE);
557 else
558 prepare_to_wait(&work->waiters, &wait, TASK_INTERRUPTIBLE);
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -0500559
Rabin Vincente30f53a2014-11-10 19:46:34 +0100560 /*
561 * The events can happen in critical sections where
562 * checking a work queue can cause deadlocks.
563 * After adding a task to the queue, this flag is set
564 * only to notify events to try to wake up the queue
565 * using irq_work.
566 *
567 * We don't clear it even if the buffer is no longer
568 * empty. The flag only causes the next event to run
569 * irq_work to do the work queue wake up. The worse
570 * that can happen if we race with !trace_empty() is that
571 * an event will cause an irq_work to try to wake up
572 * an empty queue.
573 *
574 * There's no reason to protect this flag either, as
575 * the work queue and irq_work logic will do the necessary
576 * synchronization for the wake ups. The only thing
577 * that is necessary is that the wake up happens after
578 * a task has been queued. It's OK for spurious wake ups.
579 */
Steven Rostedt (Red Hat)1e0d6712015-02-10 22:14:53 -0500580 if (full)
581 work->full_waiters_pending = true;
582 else
583 work->waiters_pending = true;
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -0500584
Rabin Vincente30f53a2014-11-10 19:46:34 +0100585 if (signal_pending(current)) {
586 ret = -EINTR;
587 break;
588 }
589
590 if (cpu == RING_BUFFER_ALL_CPUS && !ring_buffer_empty(buffer))
591 break;
592
593 if (cpu != RING_BUFFER_ALL_CPUS &&
594 !ring_buffer_empty_cpu(buffer, cpu)) {
595 unsigned long flags;
596 bool pagebusy;
597
598 if (!full)
599 break;
600
601 raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
602 pagebusy = cpu_buffer->reader_page == cpu_buffer->commit_page;
603 raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
604
605 if (!pagebusy)
606 break;
607 }
608
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -0500609 schedule();
Rabin Vincente30f53a2014-11-10 19:46:34 +0100610 }
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -0500611
Steven Rostedt (Red Hat)1e0d6712015-02-10 22:14:53 -0500612 if (full)
613 finish_wait(&work->full_waiters, &wait);
614 else
615 finish_wait(&work->waiters, &wait);
Rabin Vincente30f53a2014-11-10 19:46:34 +0100616
617 return ret;
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -0500618}
619
620/**
621 * ring_buffer_poll_wait - poll on buffer input
622 * @buffer: buffer to wait on
623 * @cpu: the cpu buffer to wait on
624 * @filp: the file descriptor
625 * @poll_table: The poll descriptor
626 *
627 * If @cpu == RING_BUFFER_ALL_CPUS then the task will wake up as soon
628 * as data is added to any of the @buffer's cpu buffers. Otherwise
629 * it will wait for data to be added to a specific cpu buffer.
630 *
631 * Returns POLLIN | POLLRDNORM if data exists in the buffers,
632 * zero otherwise.
633 */
634int ring_buffer_poll_wait(struct ring_buffer *buffer, int cpu,
635 struct file *filp, poll_table *poll_table)
636{
637 struct ring_buffer_per_cpu *cpu_buffer;
638 struct rb_irq_work *work;
639
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -0500640 if (cpu == RING_BUFFER_ALL_CPUS)
641 work = &buffer->irq_work;
642 else {
Steven Rostedt (Red Hat)6721cb62013-05-23 14:21:36 -0400643 if (!cpumask_test_cpu(cpu, buffer->cpumask))
644 return -EINVAL;
645
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -0500646 cpu_buffer = buffer->buffers[cpu];
647 work = &cpu_buffer->irq_work;
648 }
649
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -0500650 poll_wait(filp, &work->waiters, poll_table);
Josef Bacik4ce97db2014-08-25 13:59:41 -0400651 work->waiters_pending = true;
652 /*
653 * There's a tight race between setting the waiters_pending and
654 * checking if the ring buffer is empty. Once the waiters_pending bit
655 * is set, the next event will wake the task up, but we can get stuck
656 * if there's only a single event in.
657 *
658 * FIXME: Ideally, we need a memory barrier on the writer side as well,
659 * but adding a memory barrier to all events will cause too much of a
660 * performance hit in the fast path. We only need a memory barrier when
661 * the buffer goes from empty to having content. But as this race is
662 * extremely small, and it's not a problem if another event comes in, we
663 * will fix it later.
664 */
665 smp_mb();
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -0500666
667 if ((cpu == RING_BUFFER_ALL_CPUS && !ring_buffer_empty(buffer)) ||
668 (cpu != RING_BUFFER_ALL_CPUS && !ring_buffer_empty_cpu(buffer, cpu)))
669 return POLLIN | POLLRDNORM;
670 return 0;
671}
672
Steven Rostedtf536aaf2008-11-10 23:07:30 -0500673/* buffer may be either ring_buffer or ring_buffer_per_cpu */
Steven Rostedt077c5402009-09-03 19:53:46 -0400674#define RB_WARN_ON(b, cond) \
675 ({ \
676 int _____ret = unlikely(cond); \
677 if (_____ret) { \
678 if (__same_type(*(b), struct ring_buffer_per_cpu)) { \
679 struct ring_buffer_per_cpu *__b = \
680 (void *)b; \
681 atomic_inc(&__b->buffer->record_disabled); \
682 } else \
683 atomic_inc(&b->record_disabled); \
684 WARN_ON(1); \
685 } \
686 _____ret; \
Steven Rostedt3e89c7b2008-11-11 15:28:41 -0500687 })
Steven Rostedtf536aaf2008-11-10 23:07:30 -0500688
Steven Rostedt37886f62009-03-17 17:22:06 -0400689/* Up this if you want to test the TIME_EXTENTS and normalization */
690#define DEBUG_SHIFT 0
691
Jiri Olsa6d3f1e12009-10-23 19:36:19 -0400692static inline u64 rb_time_stamp(struct ring_buffer *buffer)
Steven Rostedt88eb0122009-05-11 16:28:23 -0400693{
694 /* shift to debug/test normalization and TIME_EXTENTS */
695 return buffer->clock() << DEBUG_SHIFT;
696}
697
Steven Rostedt37886f62009-03-17 17:22:06 -0400698u64 ring_buffer_time_stamp(struct ring_buffer *buffer, int cpu)
699{
700 u64 time;
701
702 preempt_disable_notrace();
Jiri Olsa6d3f1e12009-10-23 19:36:19 -0400703 time = rb_time_stamp(buffer);
Steven Rostedt37886f62009-03-17 17:22:06 -0400704 preempt_enable_no_resched_notrace();
705
706 return time;
707}
708EXPORT_SYMBOL_GPL(ring_buffer_time_stamp);
709
710void ring_buffer_normalize_time_stamp(struct ring_buffer *buffer,
711 int cpu, u64 *ts)
712{
713 /* Just stupid testing the normalize function and deltas */
714 *ts >>= DEBUG_SHIFT;
715}
716EXPORT_SYMBOL_GPL(ring_buffer_normalize_time_stamp);
717
Steven Rostedt77ae3652009-03-27 11:00:29 -0400718/*
719 * Making the ring buffer lockless makes things tricky.
720 * Although writes only happen on the CPU that they are on,
721 * and they only need to worry about interrupts. Reads can
722 * happen on any CPU.
723 *
724 * The reader page is always off the ring buffer, but when the
725 * reader finishes with a page, it needs to swap its page with
726 * a new one from the buffer. The reader needs to take from
727 * the head (writes go to the tail). But if a writer is in overwrite
728 * mode and wraps, it must push the head page forward.
729 *
730 * Here lies the problem.
731 *
732 * The reader must be careful to replace only the head page, and
733 * not another one. As described at the top of the file in the
734 * ASCII art, the reader sets its old page to point to the next
735 * page after head. It then sets the page after head to point to
736 * the old reader page. But if the writer moves the head page
737 * during this operation, the reader could end up with the tail.
738 *
739 * We use cmpxchg to help prevent this race. We also do something
740 * special with the page before head. We set the LSB to 1.
741 *
742 * When the writer must push the page forward, it will clear the
743 * bit that points to the head page, move the head, and then set
744 * the bit that points to the new head page.
745 *
746 * We also don't want an interrupt coming in and moving the head
747 * page on another writer. Thus we use the second LSB to catch
748 * that too. Thus:
749 *
750 * head->list->prev->next bit 1 bit 0
751 * ------- -------
752 * Normal page 0 0
753 * Points to head page 0 1
754 * New head page 1 0
755 *
756 * Note we can not trust the prev pointer of the head page, because:
757 *
758 * +----+ +-----+ +-----+
759 * | |------>| T |---X--->| N |
760 * | |<------| | | |
761 * +----+ +-----+ +-----+
762 * ^ ^ |
763 * | +-----+ | |
764 * +----------| R |----------+ |
765 * | |<-----------+
766 * +-----+
767 *
768 * Key: ---X--> HEAD flag set in pointer
769 * T Tail page
770 * R Reader page
771 * N Next page
772 *
773 * (see __rb_reserve_next() to see where this happens)
774 *
775 * What the above shows is that the reader just swapped out
776 * the reader page with a page in the buffer, but before it
777 * could make the new header point back to the new page added
778 * it was preempted by a writer. The writer moved forward onto
779 * the new page added by the reader and is about to move forward
780 * again.
781 *
782 * You can see, it is legitimate for the previous pointer of
783 * the head (or any page) not to point back to itself. But only
784 * temporarially.
785 */
786
787#define RB_PAGE_NORMAL 0UL
788#define RB_PAGE_HEAD 1UL
789#define RB_PAGE_UPDATE 2UL
790
791
792#define RB_FLAG_MASK 3UL
793
794/* PAGE_MOVED is not part of the mask */
795#define RB_PAGE_MOVED 4UL
796
797/*
798 * rb_list_head - remove any bit
799 */
800static struct list_head *rb_list_head(struct list_head *list)
801{
802 unsigned long val = (unsigned long)list;
803
804 return (struct list_head *)(val & ~RB_FLAG_MASK);
805}
806
807/*
Jiri Olsa6d3f1e12009-10-23 19:36:19 -0400808 * rb_is_head_page - test if the given page is the head page
Steven Rostedt77ae3652009-03-27 11:00:29 -0400809 *
810 * Because the reader may move the head_page pointer, we can
811 * not trust what the head page is (it may be pointing to
812 * the reader page). But if the next page is a header page,
813 * its flags will be non zero.
814 */
Jesper Juhl42b16b32011-01-17 00:09:38 +0100815static inline int
Steven Rostedt77ae3652009-03-27 11:00:29 -0400816rb_is_head_page(struct ring_buffer_per_cpu *cpu_buffer,
817 struct buffer_page *page, struct list_head *list)
818{
819 unsigned long val;
820
821 val = (unsigned long)list->next;
822
823 if ((val & ~RB_FLAG_MASK) != (unsigned long)&page->list)
824 return RB_PAGE_MOVED;
825
826 return val & RB_FLAG_MASK;
827}
828
829/*
830 * rb_is_reader_page
831 *
832 * The unique thing about the reader page, is that, if the
833 * writer is ever on it, the previous pointer never points
834 * back to the reader page.
835 */
Yaowei Bai06ca3202015-09-29 22:43:31 +0800836static bool rb_is_reader_page(struct buffer_page *page)
Steven Rostedt77ae3652009-03-27 11:00:29 -0400837{
838 struct list_head *list = page->list.prev;
839
840 return rb_list_head(list->next) != &page->list;
841}
842
843/*
844 * rb_set_list_to_head - set a list_head to be pointing to head.
845 */
846static void rb_set_list_to_head(struct ring_buffer_per_cpu *cpu_buffer,
847 struct list_head *list)
848{
849 unsigned long *ptr;
850
851 ptr = (unsigned long *)&list->next;
852 *ptr |= RB_PAGE_HEAD;
853 *ptr &= ~RB_PAGE_UPDATE;
854}
855
856/*
857 * rb_head_page_activate - sets up head page
858 */
859static void rb_head_page_activate(struct ring_buffer_per_cpu *cpu_buffer)
860{
861 struct buffer_page *head;
862
863 head = cpu_buffer->head_page;
864 if (!head)
865 return;
866
867 /*
868 * Set the previous list pointer to have the HEAD flag.
869 */
870 rb_set_list_to_head(cpu_buffer, head->list.prev);
871}
872
873static void rb_list_head_clear(struct list_head *list)
874{
875 unsigned long *ptr = (unsigned long *)&list->next;
876
877 *ptr &= ~RB_FLAG_MASK;
878}
879
880/*
881 * rb_head_page_dactivate - clears head page ptr (for free list)
882 */
883static void
884rb_head_page_deactivate(struct ring_buffer_per_cpu *cpu_buffer)
885{
886 struct list_head *hd;
887
888 /* Go through the whole list and clear any pointers found. */
889 rb_list_head_clear(cpu_buffer->pages);
890
891 list_for_each(hd, cpu_buffer->pages)
892 rb_list_head_clear(hd);
893}
894
895static int rb_head_page_set(struct ring_buffer_per_cpu *cpu_buffer,
896 struct buffer_page *head,
897 struct buffer_page *prev,
898 int old_flag, int new_flag)
899{
900 struct list_head *list;
901 unsigned long val = (unsigned long)&head->list;
902 unsigned long ret;
903
904 list = &prev->list;
905
906 val &= ~RB_FLAG_MASK;
907
Steven Rostedt08a40812009-09-14 09:31:35 -0400908 ret = cmpxchg((unsigned long *)&list->next,
909 val | old_flag, val | new_flag);
Steven Rostedt77ae3652009-03-27 11:00:29 -0400910
911 /* check if the reader took the page */
912 if ((ret & ~RB_FLAG_MASK) != val)
913 return RB_PAGE_MOVED;
914
915 return ret & RB_FLAG_MASK;
916}
917
918static int rb_head_page_set_update(struct ring_buffer_per_cpu *cpu_buffer,
919 struct buffer_page *head,
920 struct buffer_page *prev,
921 int old_flag)
922{
923 return rb_head_page_set(cpu_buffer, head, prev,
924 old_flag, RB_PAGE_UPDATE);
925}
926
927static int rb_head_page_set_head(struct ring_buffer_per_cpu *cpu_buffer,
928 struct buffer_page *head,
929 struct buffer_page *prev,
930 int old_flag)
931{
932 return rb_head_page_set(cpu_buffer, head, prev,
933 old_flag, RB_PAGE_HEAD);
934}
935
936static int rb_head_page_set_normal(struct ring_buffer_per_cpu *cpu_buffer,
937 struct buffer_page *head,
938 struct buffer_page *prev,
939 int old_flag)
940{
941 return rb_head_page_set(cpu_buffer, head, prev,
942 old_flag, RB_PAGE_NORMAL);
943}
944
945static inline void rb_inc_page(struct ring_buffer_per_cpu *cpu_buffer,
946 struct buffer_page **bpage)
947{
948 struct list_head *p = rb_list_head((*bpage)->list.next);
949
950 *bpage = list_entry(p, struct buffer_page, list);
951}
952
953static struct buffer_page *
954rb_set_head_page(struct ring_buffer_per_cpu *cpu_buffer)
955{
956 struct buffer_page *head;
957 struct buffer_page *page;
958 struct list_head *list;
959 int i;
960
961 if (RB_WARN_ON(cpu_buffer, !cpu_buffer->head_page))
962 return NULL;
963
964 /* sanity check */
965 list = cpu_buffer->pages;
966 if (RB_WARN_ON(cpu_buffer, rb_list_head(list->prev->next) != list))
967 return NULL;
968
969 page = head = cpu_buffer->head_page;
970 /*
971 * It is possible that the writer moves the header behind
972 * where we started, and we miss in one loop.
973 * A second loop should grab the header, but we'll do
974 * three loops just because I'm paranoid.
975 */
976 for (i = 0; i < 3; i++) {
977 do {
978 if (rb_is_head_page(cpu_buffer, page, page->list.prev)) {
979 cpu_buffer->head_page = page;
980 return page;
981 }
982 rb_inc_page(cpu_buffer, &page);
983 } while (page != head);
984 }
985
986 RB_WARN_ON(cpu_buffer, 1);
987
988 return NULL;
989}
990
991static int rb_head_page_replace(struct buffer_page *old,
992 struct buffer_page *new)
993{
994 unsigned long *ptr = (unsigned long *)&old->list.prev->next;
995 unsigned long val;
996 unsigned long ret;
997
998 val = *ptr & ~RB_FLAG_MASK;
999 val |= RB_PAGE_HEAD;
1000
Steven Rostedt08a40812009-09-14 09:31:35 -04001001 ret = cmpxchg(ptr, val, (unsigned long)&new->list);
Steven Rostedt77ae3652009-03-27 11:00:29 -04001002
1003 return ret == val;
1004}
1005
1006/*
1007 * rb_tail_page_update - move the tail page forward
Steven Rostedt77ae3652009-03-27 11:00:29 -04001008 */
Steven Rostedt (Red Hat)70004982015-11-17 15:15:19 -05001009static void rb_tail_page_update(struct ring_buffer_per_cpu *cpu_buffer,
Steven Rostedt77ae3652009-03-27 11:00:29 -04001010 struct buffer_page *tail_page,
1011 struct buffer_page *next_page)
1012{
Steven Rostedt77ae3652009-03-27 11:00:29 -04001013 unsigned long old_entries;
1014 unsigned long old_write;
Steven Rostedt77ae3652009-03-27 11:00:29 -04001015
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 */
Steven Rostedt (Red Hat)85736362015-11-17 14:03:11 -05001039 if (tail_page == READ_ONCE(cpu_buffer->tail_page)) {
Steven Rostedt77ae3652009-03-27 11:00:29 -04001040 /* 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
Steven Rostedt (Red Hat)70004982015-11-17 15:15:19 -05001064 /* Again, either we update tail_page or an interrupt does */
1065 (void)cmpxchg(&cpu_buffer->tail_page, tail_page, next_page);
Steven Rostedt77ae3652009-03-27 11:00:29 -04001066 }
Steven Rostedt77ae3652009-03-27 11:00:29 -04001067}
1068
1069static int rb_check_bpage(struct ring_buffer_per_cpu *cpu_buffer,
1070 struct buffer_page *bpage)
1071{
1072 unsigned long val = (unsigned long)bpage;
1073
1074 if (RB_WARN_ON(cpu_buffer, val & RB_FLAG_MASK))
1075 return 1;
1076
1077 return 0;
1078}
1079
1080/**
1081 * rb_check_list - make sure a pointer to a list has the last bits zero
1082 */
1083static int rb_check_list(struct ring_buffer_per_cpu *cpu_buffer,
1084 struct list_head *list)
1085{
1086 if (RB_WARN_ON(cpu_buffer, rb_list_head(list->prev) != list->prev))
1087 return 1;
1088 if (RB_WARN_ON(cpu_buffer, rb_list_head(list->next) != list->next))
1089 return 1;
1090 return 0;
1091}
1092
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001093/**
zhangwei(Jovi)d6118512013-07-15 16:32:50 +08001094 * rb_check_pages - integrity check of buffer pages
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001095 * @cpu_buffer: CPU buffer with pages to test
1096 *
Wenji Huangc3706f02009-02-10 01:03:18 -05001097 * As a safety measure we check to make sure the data pages have not
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001098 * been corrupted.
1099 */
1100static int rb_check_pages(struct ring_buffer_per_cpu *cpu_buffer)
1101{
Steven Rostedt3adc54f2009-03-30 15:32:01 -04001102 struct list_head *head = cpu_buffer->pages;
Steven Rostedt044fa782008-12-02 23:50:03 -05001103 struct buffer_page *bpage, *tmp;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001104
Steven Rostedt308f7ee2012-05-16 19:46:32 -04001105 /* Reset the head page if it exists */
1106 if (cpu_buffer->head_page)
1107 rb_set_head_page(cpu_buffer);
1108
Steven Rostedt77ae3652009-03-27 11:00:29 -04001109 rb_head_page_deactivate(cpu_buffer);
1110
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05001111 if (RB_WARN_ON(cpu_buffer, head->next->prev != head))
1112 return -1;
1113 if (RB_WARN_ON(cpu_buffer, head->prev->next != head))
1114 return -1;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001115
Steven Rostedt77ae3652009-03-27 11:00:29 -04001116 if (rb_check_list(cpu_buffer, head))
1117 return -1;
1118
Steven Rostedt044fa782008-12-02 23:50:03 -05001119 list_for_each_entry_safe(bpage, tmp, head, list) {
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05001120 if (RB_WARN_ON(cpu_buffer,
Steven Rostedt044fa782008-12-02 23:50:03 -05001121 bpage->list.next->prev != &bpage->list))
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05001122 return -1;
1123 if (RB_WARN_ON(cpu_buffer,
Steven Rostedt044fa782008-12-02 23:50:03 -05001124 bpage->list.prev->next != &bpage->list))
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05001125 return -1;
Steven Rostedt77ae3652009-03-27 11:00:29 -04001126 if (rb_check_list(cpu_buffer, &bpage->list))
1127 return -1;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001128 }
1129
Steven Rostedt77ae3652009-03-27 11:00:29 -04001130 rb_head_page_activate(cpu_buffer);
1131
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001132 return 0;
1133}
1134
Steven Rostedt (Red Hat)9b94a8f2016-05-12 11:01:24 -04001135static int __rb_allocate_pages(long nr_pages, struct list_head *pages, int cpu)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001136{
Steven Rostedt044fa782008-12-02 23:50:03 -05001137 struct buffer_page *bpage, *tmp;
Steven Rostedt (Red Hat)9b94a8f2016-05-12 11:01:24 -04001138 long i;
Steven Rostedt3adc54f2009-03-30 15:32:01 -04001139
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001140 for (i = 0; i < nr_pages; i++) {
Vaibhav Nagarnaik7ea59062011-05-03 17:56:42 -07001141 struct page *page;
Vaibhav Nagarnaikd7ec4bf2011-06-07 17:01:42 -07001142 /*
1143 * __GFP_NORETRY flag makes sure that the allocation fails
1144 * gracefully without invoking oom-killer and the system is
1145 * not destabilized.
1146 */
Steven Rostedt044fa782008-12-02 23:50:03 -05001147 bpage = kzalloc_node(ALIGN(sizeof(*bpage), cache_line_size()),
Vaibhav Nagarnaikd7ec4bf2011-06-07 17:01:42 -07001148 GFP_KERNEL | __GFP_NORETRY,
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001149 cpu_to_node(cpu));
Steven Rostedt044fa782008-12-02 23:50:03 -05001150 if (!bpage)
Steven Rostedte4c2ce82008-10-01 11:14:54 -04001151 goto free_pages;
Steven Rostedt77ae3652009-03-27 11:00:29 -04001152
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001153 list_add(&bpage->list, pages);
Steven Rostedt77ae3652009-03-27 11:00:29 -04001154
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001155 page = alloc_pages_node(cpu_to_node(cpu),
Vaibhav Nagarnaikd7ec4bf2011-06-07 17:01:42 -07001156 GFP_KERNEL | __GFP_NORETRY, 0);
Vaibhav Nagarnaik7ea59062011-05-03 17:56:42 -07001157 if (!page)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001158 goto free_pages;
Vaibhav Nagarnaik7ea59062011-05-03 17:56:42 -07001159 bpage->page = page_address(page);
Steven Rostedt044fa782008-12-02 23:50:03 -05001160 rb_init_page(bpage->page);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001161 }
1162
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001163 return 0;
1164
1165free_pages:
1166 list_for_each_entry_safe(bpage, tmp, pages, list) {
1167 list_del_init(&bpage->list);
1168 free_buffer_page(bpage);
1169 }
1170
1171 return -ENOMEM;
1172}
1173
1174static int rb_allocate_pages(struct ring_buffer_per_cpu *cpu_buffer,
Steven Rostedt (Red Hat)9b94a8f2016-05-12 11:01:24 -04001175 unsigned long nr_pages)
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001176{
1177 LIST_HEAD(pages);
1178
1179 WARN_ON(!nr_pages);
1180
1181 if (__rb_allocate_pages(nr_pages, &pages, cpu_buffer->cpu))
1182 return -ENOMEM;
1183
Steven Rostedt3adc54f2009-03-30 15:32:01 -04001184 /*
1185 * The ring buffer page list is a circular list that does not
1186 * start and end with a list head. All page list items point to
1187 * other pages.
1188 */
1189 cpu_buffer->pages = pages.next;
1190 list_del(&pages);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001191
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001192 cpu_buffer->nr_pages = nr_pages;
1193
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001194 rb_check_pages(cpu_buffer);
1195
1196 return 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001197}
1198
1199static struct ring_buffer_per_cpu *
Steven Rostedt (Red Hat)9b94a8f2016-05-12 11:01:24 -04001200rb_allocate_cpu_buffer(struct ring_buffer *buffer, long nr_pages, int cpu)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001201{
1202 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedt044fa782008-12-02 23:50:03 -05001203 struct buffer_page *bpage;
Vaibhav Nagarnaik7ea59062011-05-03 17:56:42 -07001204 struct page *page;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001205 int ret;
1206
1207 cpu_buffer = kzalloc_node(ALIGN(sizeof(*cpu_buffer), cache_line_size()),
1208 GFP_KERNEL, cpu_to_node(cpu));
1209 if (!cpu_buffer)
1210 return NULL;
1211
1212 cpu_buffer->cpu = cpu;
1213 cpu_buffer->buffer = buffer;
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02001214 raw_spin_lock_init(&cpu_buffer->reader_lock);
Peter Zijlstra1f8a6a12009-06-08 18:18:39 +02001215 lockdep_set_class(&cpu_buffer->reader_lock, buffer->reader_lock_key);
Thomas Gleixneredc35bd2009-12-03 12:38:57 +01001216 cpu_buffer->lock = (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001217 INIT_WORK(&cpu_buffer->update_pages_work, update_pages_handler);
Vaibhav Nagarnaik05fdd702012-05-18 13:29:51 -07001218 init_completion(&cpu_buffer->update_done);
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -05001219 init_irq_work(&cpu_buffer->irq_work.work, rb_wake_up_waiters);
Steven Rostedt (Red Hat)f1dc6722013-03-04 17:33:05 -05001220 init_waitqueue_head(&cpu_buffer->irq_work.waiters);
Steven Rostedt (Red Hat)1e0d6712015-02-10 22:14:53 -05001221 init_waitqueue_head(&cpu_buffer->irq_work.full_waiters);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001222
Steven Rostedt044fa782008-12-02 23:50:03 -05001223 bpage = kzalloc_node(ALIGN(sizeof(*bpage), cache_line_size()),
Steven Rostedte4c2ce82008-10-01 11:14:54 -04001224 GFP_KERNEL, cpu_to_node(cpu));
Steven Rostedt044fa782008-12-02 23:50:03 -05001225 if (!bpage)
Steven Rostedte4c2ce82008-10-01 11:14:54 -04001226 goto fail_free_buffer;
1227
Steven Rostedt77ae3652009-03-27 11:00:29 -04001228 rb_check_bpage(cpu_buffer, bpage);
1229
Steven Rostedt044fa782008-12-02 23:50:03 -05001230 cpu_buffer->reader_page = bpage;
Vaibhav Nagarnaik7ea59062011-05-03 17:56:42 -07001231 page = alloc_pages_node(cpu_to_node(cpu), GFP_KERNEL, 0);
1232 if (!page)
Steven Rostedte4c2ce82008-10-01 11:14:54 -04001233 goto fail_free_reader;
Vaibhav Nagarnaik7ea59062011-05-03 17:56:42 -07001234 bpage->page = page_address(page);
Steven Rostedt044fa782008-12-02 23:50:03 -05001235 rb_init_page(bpage->page);
Steven Rostedte4c2ce82008-10-01 11:14:54 -04001236
Steven Rostedtd7690412008-10-01 00:29:53 -04001237 INIT_LIST_HEAD(&cpu_buffer->reader_page->list);
Vaibhav Nagarnaik44b99462012-06-22 11:50:05 -07001238 INIT_LIST_HEAD(&cpu_buffer->new_pages);
Steven Rostedtd7690412008-10-01 00:29:53 -04001239
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001240 ret = rb_allocate_pages(cpu_buffer, nr_pages);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001241 if (ret < 0)
Steven Rostedtd7690412008-10-01 00:29:53 -04001242 goto fail_free_reader;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001243
1244 cpu_buffer->head_page
Steven Rostedt3adc54f2009-03-30 15:32:01 -04001245 = list_entry(cpu_buffer->pages, struct buffer_page, list);
Steven Rostedtbf41a152008-10-04 02:00:59 -04001246 cpu_buffer->tail_page = cpu_buffer->commit_page = cpu_buffer->head_page;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001247
Steven Rostedt77ae3652009-03-27 11:00:29 -04001248 rb_head_page_activate(cpu_buffer);
1249
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001250 return cpu_buffer;
1251
Steven Rostedtd7690412008-10-01 00:29:53 -04001252 fail_free_reader:
1253 free_buffer_page(cpu_buffer->reader_page);
1254
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001255 fail_free_buffer:
1256 kfree(cpu_buffer);
1257 return NULL;
1258}
1259
1260static void rb_free_cpu_buffer(struct ring_buffer_per_cpu *cpu_buffer)
1261{
Steven Rostedt3adc54f2009-03-30 15:32:01 -04001262 struct list_head *head = cpu_buffer->pages;
Steven Rostedt044fa782008-12-02 23:50:03 -05001263 struct buffer_page *bpage, *tmp;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001264
Steven Rostedtd7690412008-10-01 00:29:53 -04001265 free_buffer_page(cpu_buffer->reader_page);
1266
Steven Rostedt77ae3652009-03-27 11:00:29 -04001267 rb_head_page_deactivate(cpu_buffer);
1268
Steven Rostedt3adc54f2009-03-30 15:32:01 -04001269 if (head) {
1270 list_for_each_entry_safe(bpage, tmp, head, list) {
1271 list_del_init(&bpage->list);
1272 free_buffer_page(bpage);
1273 }
1274 bpage = list_entry(head, struct buffer_page, list);
Steven Rostedt044fa782008-12-02 23:50:03 -05001275 free_buffer_page(bpage);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001276 }
Steven Rostedt3adc54f2009-03-30 15:32:01 -04001277
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001278 kfree(cpu_buffer);
1279}
1280
Steven Rostedt59222ef2009-03-12 11:46:03 -04001281#ifdef CONFIG_HOTPLUG_CPU
Frederic Weisbecker09c9e842009-03-21 04:33:36 +01001282static int rb_cpu_notify(struct notifier_block *self,
1283 unsigned long action, void *hcpu);
Steven Rostedt554f7862009-03-11 22:00:13 -04001284#endif
1285
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001286/**
zhangwei(Jovi)d6118512013-07-15 16:32:50 +08001287 * __ring_buffer_alloc - allocate a new ring_buffer
Robert Richter68814b52008-11-24 12:24:12 +01001288 * @size: the size in bytes per cpu that is needed.
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001289 * @flags: attributes to set for the ring buffer.
1290 *
1291 * Currently the only flag that is available is the RB_FL_OVERWRITE
1292 * flag. This flag means that the buffer will overwrite old data
1293 * when the buffer wraps. If this flag is not set, the buffer will
1294 * drop data when the tail hits the head.
1295 */
Peter Zijlstra1f8a6a12009-06-08 18:18:39 +02001296struct ring_buffer *__ring_buffer_alloc(unsigned long size, unsigned flags,
1297 struct lock_class_key *key)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001298{
1299 struct ring_buffer *buffer;
Steven Rostedt (Red Hat)9b94a8f2016-05-12 11:01:24 -04001300 long nr_pages;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001301 int bsize;
Steven Rostedt (Red Hat)9b94a8f2016-05-12 11:01:24 -04001302 int cpu;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001303
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001304 /* keep it in its own cache line */
1305 buffer = kzalloc(ALIGN(sizeof(*buffer), cache_line_size()),
1306 GFP_KERNEL);
1307 if (!buffer)
1308 return NULL;
1309
Rusty Russell9e01c1b2009-01-01 10:12:22 +10301310 if (!alloc_cpumask_var(&buffer->cpumask, GFP_KERNEL))
1311 goto fail_free_buffer;
1312
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001313 nr_pages = DIV_ROUND_UP(size, BUF_PAGE_SIZE);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001314 buffer->flags = flags;
Steven Rostedt37886f62009-03-17 17:22:06 -04001315 buffer->clock = trace_clock_local;
Peter Zijlstra1f8a6a12009-06-08 18:18:39 +02001316 buffer->reader_lock_key = key;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001317
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -05001318 init_irq_work(&buffer->irq_work.work, rb_wake_up_waiters);
Steven Rostedt (Red Hat)f1dc6722013-03-04 17:33:05 -05001319 init_waitqueue_head(&buffer->irq_work.waiters);
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -05001320
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001321 /* need at least two pages */
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001322 if (nr_pages < 2)
1323 nr_pages = 2;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001324
Frederic Weisbecker3bf832c2009-03-19 14:47:33 +01001325 /*
1326 * In case of non-hotplug cpu, if the ring-buffer is allocated
1327 * in early initcall, it will not be notified of secondary cpus.
1328 * In that off case, we need to allocate for all possible cpus.
1329 */
1330#ifdef CONFIG_HOTPLUG_CPU
Srivatsa S. Bhatd39ad272014-03-11 02:11:56 +05301331 cpu_notifier_register_begin();
Steven Rostedt554f7862009-03-11 22:00:13 -04001332 cpumask_copy(buffer->cpumask, cpu_online_mask);
Frederic Weisbecker3bf832c2009-03-19 14:47:33 +01001333#else
1334 cpumask_copy(buffer->cpumask, cpu_possible_mask);
1335#endif
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001336 buffer->cpus = nr_cpu_ids;
1337
1338 bsize = sizeof(void *) * nr_cpu_ids;
1339 buffer->buffers = kzalloc(ALIGN(bsize, cache_line_size()),
1340 GFP_KERNEL);
1341 if (!buffer->buffers)
Rusty Russell9e01c1b2009-01-01 10:12:22 +10301342 goto fail_free_cpumask;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001343
1344 for_each_buffer_cpu(buffer, cpu) {
1345 buffer->buffers[cpu] =
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001346 rb_allocate_cpu_buffer(buffer, nr_pages, cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001347 if (!buffer->buffers[cpu])
1348 goto fail_free_buffers;
1349 }
1350
Steven Rostedt59222ef2009-03-12 11:46:03 -04001351#ifdef CONFIG_HOTPLUG_CPU
Steven Rostedt554f7862009-03-11 22:00:13 -04001352 buffer->cpu_notify.notifier_call = rb_cpu_notify;
1353 buffer->cpu_notify.priority = 0;
Srivatsa S. Bhatd39ad272014-03-11 02:11:56 +05301354 __register_cpu_notifier(&buffer->cpu_notify);
1355 cpu_notifier_register_done();
Steven Rostedt554f7862009-03-11 22:00:13 -04001356#endif
1357
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001358 mutex_init(&buffer->mutex);
1359
1360 return buffer;
1361
1362 fail_free_buffers:
1363 for_each_buffer_cpu(buffer, cpu) {
1364 if (buffer->buffers[cpu])
1365 rb_free_cpu_buffer(buffer->buffers[cpu]);
1366 }
1367 kfree(buffer->buffers);
1368
Rusty Russell9e01c1b2009-01-01 10:12:22 +10301369 fail_free_cpumask:
1370 free_cpumask_var(buffer->cpumask);
Srivatsa S. Bhatd39ad272014-03-11 02:11:56 +05301371#ifdef CONFIG_HOTPLUG_CPU
1372 cpu_notifier_register_done();
1373#endif
Rusty Russell9e01c1b2009-01-01 10:12:22 +10301374
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001375 fail_free_buffer:
1376 kfree(buffer);
1377 return NULL;
1378}
Peter Zijlstra1f8a6a12009-06-08 18:18:39 +02001379EXPORT_SYMBOL_GPL(__ring_buffer_alloc);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001380
1381/**
1382 * ring_buffer_free - free a ring buffer.
1383 * @buffer: the buffer to free.
1384 */
1385void
1386ring_buffer_free(struct ring_buffer *buffer)
1387{
1388 int cpu;
1389
Steven Rostedt59222ef2009-03-12 11:46:03 -04001390#ifdef CONFIG_HOTPLUG_CPU
Srivatsa S. Bhatd39ad272014-03-11 02:11:56 +05301391 cpu_notifier_register_begin();
1392 __unregister_cpu_notifier(&buffer->cpu_notify);
Steven Rostedt554f7862009-03-11 22:00:13 -04001393#endif
1394
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001395 for_each_buffer_cpu(buffer, cpu)
1396 rb_free_cpu_buffer(buffer->buffers[cpu]);
1397
Srivatsa S. Bhatd39ad272014-03-11 02:11:56 +05301398#ifdef CONFIG_HOTPLUG_CPU
1399 cpu_notifier_register_done();
1400#endif
Steven Rostedt554f7862009-03-11 22:00:13 -04001401
Eric Dumazetbd3f0222009-08-07 12:49:29 +02001402 kfree(buffer->buffers);
Rusty Russell9e01c1b2009-01-01 10:12:22 +10301403 free_cpumask_var(buffer->cpumask);
1404
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001405 kfree(buffer);
1406}
Robert Richterc4f50182008-12-11 16:49:22 +01001407EXPORT_SYMBOL_GPL(ring_buffer_free);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001408
Steven Rostedt37886f62009-03-17 17:22:06 -04001409void ring_buffer_set_clock(struct ring_buffer *buffer,
1410 u64 (*clock)(void))
1411{
1412 buffer->clock = clock;
1413}
1414
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001415static void rb_reset_cpu(struct ring_buffer_per_cpu *cpu_buffer);
1416
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001417static inline unsigned long rb_page_entries(struct buffer_page *bpage)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001418{
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001419 return local_read(&bpage->entries) & RB_WRITE_MASK;
1420}
1421
1422static inline unsigned long rb_page_write(struct buffer_page *bpage)
1423{
1424 return local_read(&bpage->write) & RB_WRITE_MASK;
1425}
1426
Vaibhav Nagarnaik5040b4b2012-05-03 18:59:51 -07001427static int
Steven Rostedt (Red Hat)9b94a8f2016-05-12 11:01:24 -04001428rb_remove_pages(struct ring_buffer_per_cpu *cpu_buffer, unsigned long nr_pages)
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001429{
1430 struct list_head *tail_page, *to_remove, *next_page;
1431 struct buffer_page *to_remove_page, *tmp_iter_page;
1432 struct buffer_page *last_page, *first_page;
Steven Rostedt (Red Hat)9b94a8f2016-05-12 11:01:24 -04001433 unsigned long nr_removed;
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001434 unsigned long head_bit;
1435 int page_entries;
1436
1437 head_bit = 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001438
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02001439 raw_spin_lock_irq(&cpu_buffer->reader_lock);
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001440 atomic_inc(&cpu_buffer->record_disabled);
1441 /*
1442 * We don't race with the readers since we have acquired the reader
1443 * lock. We also don't race with writers after disabling recording.
1444 * This makes it easy to figure out the first and the last page to be
1445 * removed from the list. We unlink all the pages in between including
1446 * the first and last pages. This is done in a busy loop so that we
1447 * lose the least number of traces.
1448 * The pages are freed after we restart recording and unlock readers.
1449 */
1450 tail_page = &cpu_buffer->tail_page->list;
Steven Rostedt77ae3652009-03-27 11:00:29 -04001451
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001452 /*
1453 * tail page might be on reader page, we remove the next page
1454 * from the ring buffer
1455 */
1456 if (cpu_buffer->tail_page == cpu_buffer->reader_page)
1457 tail_page = rb_list_head(tail_page->next);
1458 to_remove = tail_page;
1459
1460 /* start of pages to remove */
1461 first_page = list_entry(rb_list_head(to_remove->next),
1462 struct buffer_page, list);
1463
1464 for (nr_removed = 0; nr_removed < nr_pages; nr_removed++) {
1465 to_remove = rb_list_head(to_remove)->next;
1466 head_bit |= (unsigned long)to_remove & RB_PAGE_HEAD;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001467 }
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001468
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001469 next_page = rb_list_head(to_remove)->next;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001470
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001471 /*
1472 * Now we remove all pages between tail_page and next_page.
1473 * Make sure that we have head_bit value preserved for the
1474 * next page
1475 */
1476 tail_page->next = (struct list_head *)((unsigned long)next_page |
1477 head_bit);
1478 next_page = rb_list_head(next_page);
1479 next_page->prev = tail_page;
1480
1481 /* make sure pages points to a valid page in the ring buffer */
1482 cpu_buffer->pages = next_page;
1483
1484 /* update head page */
1485 if (head_bit)
1486 cpu_buffer->head_page = list_entry(next_page,
1487 struct buffer_page, list);
1488
1489 /*
1490 * change read pointer to make sure any read iterators reset
1491 * themselves
1492 */
1493 cpu_buffer->read = 0;
1494
1495 /* pages are removed, resume tracing and then free the pages */
1496 atomic_dec(&cpu_buffer->record_disabled);
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02001497 raw_spin_unlock_irq(&cpu_buffer->reader_lock);
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001498
1499 RB_WARN_ON(cpu_buffer, list_empty(cpu_buffer->pages));
1500
1501 /* last buffer page to remove */
1502 last_page = list_entry(rb_list_head(to_remove), struct buffer_page,
1503 list);
1504 tmp_iter_page = first_page;
1505
1506 do {
1507 to_remove_page = tmp_iter_page;
1508 rb_inc_page(cpu_buffer, &tmp_iter_page);
1509
1510 /* update the counters */
1511 page_entries = rb_page_entries(to_remove_page);
1512 if (page_entries) {
1513 /*
1514 * If something was added to this page, it was full
1515 * since it is not the tail page. So we deduct the
1516 * bytes consumed in ring buffer from here.
Vaibhav Nagarnaik48fdc722012-06-29 12:31:41 -07001517 * Increment overrun to account for the lost events.
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001518 */
Vaibhav Nagarnaik48fdc722012-06-29 12:31:41 -07001519 local_add(page_entries, &cpu_buffer->overrun);
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001520 local_sub(BUF_PAGE_SIZE, &cpu_buffer->entries_bytes);
1521 }
1522
1523 /*
1524 * We have already removed references to this list item, just
1525 * free up the buffer_page and its page
1526 */
1527 free_buffer_page(to_remove_page);
1528 nr_removed--;
1529
1530 } while (to_remove_page != last_page);
1531
1532 RB_WARN_ON(cpu_buffer, nr_removed);
Vaibhav Nagarnaik5040b4b2012-05-03 18:59:51 -07001533
1534 return nr_removed == 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001535}
1536
Vaibhav Nagarnaik5040b4b2012-05-03 18:59:51 -07001537static int
1538rb_insert_pages(struct ring_buffer_per_cpu *cpu_buffer)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001539{
Vaibhav Nagarnaik5040b4b2012-05-03 18:59:51 -07001540 struct list_head *pages = &cpu_buffer->new_pages;
1541 int retries, success;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001542
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02001543 raw_spin_lock_irq(&cpu_buffer->reader_lock);
Vaibhav Nagarnaik5040b4b2012-05-03 18:59:51 -07001544 /*
1545 * We are holding the reader lock, so the reader page won't be swapped
1546 * in the ring buffer. Now we are racing with the writer trying to
1547 * move head page and the tail page.
1548 * We are going to adapt the reader page update process where:
1549 * 1. We first splice the start and end of list of new pages between
1550 * the head page and its previous page.
1551 * 2. We cmpxchg the prev_page->next to point from head page to the
1552 * start of new pages list.
1553 * 3. Finally, we update the head->prev to the end of new list.
1554 *
1555 * We will try this process 10 times, to make sure that we don't keep
1556 * spinning.
1557 */
1558 retries = 10;
1559 success = 0;
1560 while (retries--) {
1561 struct list_head *head_page, *prev_page, *r;
1562 struct list_head *last_page, *first_page;
1563 struct list_head *head_page_with_bit;
Steven Rostedt77ae3652009-03-27 11:00:29 -04001564
Vaibhav Nagarnaik5040b4b2012-05-03 18:59:51 -07001565 head_page = &rb_set_head_page(cpu_buffer)->list;
Steven Rostedt54f7be52012-11-29 22:27:22 -05001566 if (!head_page)
1567 break;
Vaibhav Nagarnaik5040b4b2012-05-03 18:59:51 -07001568 prev_page = head_page->prev;
1569
1570 first_page = pages->next;
1571 last_page = pages->prev;
1572
1573 head_page_with_bit = (struct list_head *)
1574 ((unsigned long)head_page | RB_PAGE_HEAD);
1575
1576 last_page->next = head_page_with_bit;
1577 first_page->prev = prev_page;
1578
1579 r = cmpxchg(&prev_page->next, head_page_with_bit, first_page);
1580
1581 if (r == head_page_with_bit) {
1582 /*
1583 * yay, we replaced the page pointer to our new list,
1584 * now, we just have to update to head page's prev
1585 * pointer to point to end of list
1586 */
1587 head_page->prev = last_page;
1588 success = 1;
1589 break;
1590 }
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001591 }
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001592
Vaibhav Nagarnaik5040b4b2012-05-03 18:59:51 -07001593 if (success)
1594 INIT_LIST_HEAD(pages);
1595 /*
1596 * If we weren't successful in adding in new pages, warn and stop
1597 * tracing
1598 */
1599 RB_WARN_ON(cpu_buffer, !success);
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02001600 raw_spin_unlock_irq(&cpu_buffer->reader_lock);
Vaibhav Nagarnaik5040b4b2012-05-03 18:59:51 -07001601
1602 /* free pages if they weren't inserted */
1603 if (!success) {
1604 struct buffer_page *bpage, *tmp;
1605 list_for_each_entry_safe(bpage, tmp, &cpu_buffer->new_pages,
1606 list) {
1607 list_del_init(&bpage->list);
1608 free_buffer_page(bpage);
1609 }
1610 }
1611 return success;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001612}
1613
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001614static void rb_update_pages(struct ring_buffer_per_cpu *cpu_buffer)
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001615{
Vaibhav Nagarnaik5040b4b2012-05-03 18:59:51 -07001616 int success;
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001617
Vaibhav Nagarnaik5040b4b2012-05-03 18:59:51 -07001618 if (cpu_buffer->nr_pages_to_update > 0)
1619 success = rb_insert_pages(cpu_buffer);
1620 else
1621 success = rb_remove_pages(cpu_buffer,
1622 -cpu_buffer->nr_pages_to_update);
1623
1624 if (success)
1625 cpu_buffer->nr_pages += cpu_buffer->nr_pages_to_update;
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001626}
1627
1628static void update_pages_handler(struct work_struct *work)
1629{
1630 struct ring_buffer_per_cpu *cpu_buffer = container_of(work,
1631 struct ring_buffer_per_cpu, update_pages_work);
1632 rb_update_pages(cpu_buffer);
Vaibhav Nagarnaik05fdd702012-05-18 13:29:51 -07001633 complete(&cpu_buffer->update_done);
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001634}
1635
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001636/**
1637 * ring_buffer_resize - resize the ring buffer
1638 * @buffer: the buffer to resize.
1639 * @size: the new size.
zhangwei(Jovi)d6118512013-07-15 16:32:50 +08001640 * @cpu_id: the cpu buffer to resize
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001641 *
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001642 * Minimum size is 2 * BUF_PAGE_SIZE.
1643 *
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001644 * Returns 0 on success and < 0 on failure.
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001645 */
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001646int ring_buffer_resize(struct ring_buffer *buffer, unsigned long size,
1647 int cpu_id)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001648{
1649 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedt (Red Hat)9b94a8f2016-05-12 11:01:24 -04001650 unsigned long nr_pages;
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001651 int cpu, err = 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001652
Ingo Molnaree51a1d2008-11-13 14:58:31 +01001653 /*
1654 * Always succeed at resizing a non-existent buffer:
1655 */
1656 if (!buffer)
1657 return size;
1658
Steven Rostedt6a31e1f2012-05-23 15:35:17 -04001659 /* Make sure the requested buffer exists */
1660 if (cpu_id != RING_BUFFER_ALL_CPUS &&
1661 !cpumask_test_cpu(cpu_id, buffer->cpumask))
1662 return size;
1663
Steven Rostedt (Red Hat)59643d12016-05-13 09:34:12 -04001664 nr_pages = DIV_ROUND_UP(size, BUF_PAGE_SIZE);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001665
1666 /* we need a minimum of two pages */
Steven Rostedt (Red Hat)59643d12016-05-13 09:34:12 -04001667 if (nr_pages < 2)
1668 nr_pages = 2;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001669
Steven Rostedt (Red Hat)59643d12016-05-13 09:34:12 -04001670 size = nr_pages * BUF_PAGE_SIZE;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001671
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001672 /*
1673 * Don't succeed if resizing is disabled, as a reader might be
1674 * manipulating the ring buffer and is expecting a sane state while
1675 * this is true.
1676 */
1677 if (atomic_read(&buffer->resize_disabled))
1678 return -EBUSY;
1679
1680 /* prevent another thread from changing buffer sizes */
1681 mutex_lock(&buffer->mutex);
1682
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001683 if (cpu_id == RING_BUFFER_ALL_CPUS) {
1684 /* calculate the pages to update */
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001685 for_each_buffer_cpu(buffer, cpu) {
1686 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001687
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001688 cpu_buffer->nr_pages_to_update = nr_pages -
1689 cpu_buffer->nr_pages;
Vaibhav Nagarnaikd7ec4bf2011-06-07 17:01:42 -07001690 /*
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001691 * nothing more to do for removing pages or no update
Vaibhav Nagarnaikd7ec4bf2011-06-07 17:01:42 -07001692 */
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001693 if (cpu_buffer->nr_pages_to_update <= 0)
1694 continue;
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001695 /*
1696 * to add pages, make sure all new pages can be
1697 * allocated without receiving ENOMEM
1698 */
1699 INIT_LIST_HEAD(&cpu_buffer->new_pages);
1700 if (__rb_allocate_pages(cpu_buffer->nr_pages_to_update,
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001701 &cpu_buffer->new_pages, cpu)) {
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001702 /* not enough memory for new pages */
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001703 err = -ENOMEM;
1704 goto out_err;
1705 }
1706 }
1707
1708 get_online_cpus();
1709 /*
1710 * Fire off all the required work handlers
Vaibhav Nagarnaik05fdd702012-05-18 13:29:51 -07001711 * We can't schedule on offline CPUs, but it's not necessary
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001712 * since we can change their buffer sizes without any race.
1713 */
1714 for_each_buffer_cpu(buffer, cpu) {
1715 cpu_buffer = buffer->buffers[cpu];
Vaibhav Nagarnaik05fdd702012-05-18 13:29:51 -07001716 if (!cpu_buffer->nr_pages_to_update)
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001717 continue;
1718
Corey Minyard021c5b32014-07-16 14:07:13 -05001719 /* Can't run something on an offline CPU. */
1720 if (!cpu_online(cpu)) {
Steven Rostedt (Red Hat)f5eb5582013-03-07 09:27:42 -05001721 rb_update_pages(cpu_buffer);
1722 cpu_buffer->nr_pages_to_update = 0;
1723 } else {
Vaibhav Nagarnaik05fdd702012-05-18 13:29:51 -07001724 schedule_work_on(cpu,
1725 &cpu_buffer->update_pages_work);
Steven Rostedt (Red Hat)f5eb5582013-03-07 09:27:42 -05001726 }
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001727 }
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001728
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001729 /* wait for all the updates to complete */
1730 for_each_buffer_cpu(buffer, cpu) {
1731 cpu_buffer = buffer->buffers[cpu];
Vaibhav Nagarnaik05fdd702012-05-18 13:29:51 -07001732 if (!cpu_buffer->nr_pages_to_update)
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001733 continue;
1734
Vaibhav Nagarnaik05fdd702012-05-18 13:29:51 -07001735 if (cpu_online(cpu))
1736 wait_for_completion(&cpu_buffer->update_done);
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001737 cpu_buffer->nr_pages_to_update = 0;
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001738 }
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001739
1740 put_online_cpus();
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001741 } else {
Vaibhav Nagarnaik8e49f412012-10-10 16:40:27 -07001742 /* Make sure this CPU has been intitialized */
1743 if (!cpumask_test_cpu(cpu_id, buffer->cpumask))
1744 goto out;
1745
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001746 cpu_buffer = buffer->buffers[cpu_id];
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001747
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001748 if (nr_pages == cpu_buffer->nr_pages)
1749 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001750
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001751 cpu_buffer->nr_pages_to_update = nr_pages -
1752 cpu_buffer->nr_pages;
1753
1754 INIT_LIST_HEAD(&cpu_buffer->new_pages);
1755 if (cpu_buffer->nr_pages_to_update > 0 &&
1756 __rb_allocate_pages(cpu_buffer->nr_pages_to_update,
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001757 &cpu_buffer->new_pages, cpu_id)) {
1758 err = -ENOMEM;
1759 goto out_err;
1760 }
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001761
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001762 get_online_cpus();
1763
Corey Minyard021c5b32014-07-16 14:07:13 -05001764 /* Can't run something on an offline CPU. */
1765 if (!cpu_online(cpu_id))
Steven Rostedt (Red Hat)f5eb5582013-03-07 09:27:42 -05001766 rb_update_pages(cpu_buffer);
1767 else {
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001768 schedule_work_on(cpu_id,
1769 &cpu_buffer->update_pages_work);
Vaibhav Nagarnaik05fdd702012-05-18 13:29:51 -07001770 wait_for_completion(&cpu_buffer->update_done);
Steven Rostedt (Red Hat)f5eb5582013-03-07 09:27:42 -05001771 }
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001772
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001773 cpu_buffer->nr_pages_to_update = 0;
Vaibhav Nagarnaik05fdd702012-05-18 13:29:51 -07001774 put_online_cpus();
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001775 }
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001776
1777 out:
Steven Rostedt659f4512012-05-14 17:02:33 -04001778 /*
1779 * The ring buffer resize can happen with the ring buffer
1780 * enabled, so that the update disturbs the tracing as little
1781 * as possible. But if the buffer is disabled, we do not need
1782 * to worry about that, and we can take the time to verify
1783 * that the buffer is not corrupt.
1784 */
1785 if (atomic_read(&buffer->record_disabled)) {
1786 atomic_inc(&buffer->record_disabled);
1787 /*
1788 * Even though the buffer was disabled, we must make sure
1789 * that it is truly disabled before calling rb_check_pages.
1790 * There could have been a race between checking
1791 * record_disable and incrementing it.
1792 */
1793 synchronize_sched();
1794 for_each_buffer_cpu(buffer, cpu) {
1795 cpu_buffer = buffer->buffers[cpu];
1796 rb_check_pages(cpu_buffer);
1797 }
1798 atomic_dec(&buffer->record_disabled);
1799 }
1800
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001801 mutex_unlock(&buffer->mutex);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001802 return size;
1803
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001804 out_err:
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001805 for_each_buffer_cpu(buffer, cpu) {
1806 struct buffer_page *bpage, *tmp;
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001807
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001808 cpu_buffer = buffer->buffers[cpu];
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001809 cpu_buffer->nr_pages_to_update = 0;
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001810
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001811 if (list_empty(&cpu_buffer->new_pages))
1812 continue;
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001813
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001814 list_for_each_entry_safe(bpage, tmp, &cpu_buffer->new_pages,
1815 list) {
1816 list_del_init(&bpage->list);
1817 free_buffer_page(bpage);
1818 }
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001819 }
Vegard Nossum641d2f62008-11-18 19:22:13 +01001820 mutex_unlock(&buffer->mutex);
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001821 return err;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001822}
Robert Richterc4f50182008-12-11 16:49:22 +01001823EXPORT_SYMBOL_GPL(ring_buffer_resize);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001824
David Sharp750912f2010-12-08 13:46:47 -08001825void ring_buffer_change_overwrite(struct ring_buffer *buffer, int val)
1826{
1827 mutex_lock(&buffer->mutex);
1828 if (val)
1829 buffer->flags |= RB_FL_OVERWRITE;
1830 else
1831 buffer->flags &= ~RB_FL_OVERWRITE;
1832 mutex_unlock(&buffer->mutex);
1833}
1834EXPORT_SYMBOL_GPL(ring_buffer_change_overwrite);
1835
Steven Rostedt8789a9e2008-12-02 15:34:07 -05001836static inline void *
Steven Rostedt044fa782008-12-02 23:50:03 -05001837__rb_data_page_index(struct buffer_data_page *bpage, unsigned index)
Steven Rostedt8789a9e2008-12-02 15:34:07 -05001838{
Steven Rostedt044fa782008-12-02 23:50:03 -05001839 return bpage->data + index;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05001840}
1841
Steven Rostedt044fa782008-12-02 23:50:03 -05001842static inline void *__rb_page_index(struct buffer_page *bpage, unsigned index)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001843{
Steven Rostedt044fa782008-12-02 23:50:03 -05001844 return bpage->page->data + index;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001845}
1846
1847static inline struct ring_buffer_event *
Steven Rostedtd7690412008-10-01 00:29:53 -04001848rb_reader_event(struct ring_buffer_per_cpu *cpu_buffer)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001849{
Steven Rostedt6f807ac2008-10-04 02:00:58 -04001850 return __rb_page_index(cpu_buffer->reader_page,
1851 cpu_buffer->reader_page->read);
1852}
1853
1854static inline struct ring_buffer_event *
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001855rb_iter_head_event(struct ring_buffer_iter *iter)
1856{
Steven Rostedt6f807ac2008-10-04 02:00:58 -04001857 return __rb_page_index(iter->head_page, iter->head);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001858}
1859
Steven Rostedtbf41a152008-10-04 02:00:59 -04001860static inline unsigned rb_page_commit(struct buffer_page *bpage)
1861{
Steven Rostedtabc9b562008-12-02 15:34:06 -05001862 return local_read(&bpage->page->commit);
Steven Rostedtbf41a152008-10-04 02:00:59 -04001863}
1864
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001865/* Size is determined by what has been committed */
Steven Rostedtbf41a152008-10-04 02:00:59 -04001866static inline unsigned rb_page_size(struct buffer_page *bpage)
1867{
1868 return rb_page_commit(bpage);
1869}
1870
1871static inline unsigned
1872rb_commit_index(struct ring_buffer_per_cpu *cpu_buffer)
1873{
1874 return rb_page_commit(cpu_buffer->commit_page);
1875}
1876
Steven Rostedtbf41a152008-10-04 02:00:59 -04001877static inline unsigned
1878rb_event_index(struct ring_buffer_event *event)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001879{
Steven Rostedtbf41a152008-10-04 02:00:59 -04001880 unsigned long addr = (unsigned long)event;
1881
Steven Rostedt22f470f2009-06-11 09:29:58 -04001882 return (addr & ~PAGE_MASK) - BUF_PAGE_HDR_SIZE;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001883}
1884
Andrew Morton34a148b2009-01-09 12:27:09 -08001885static void rb_inc_iter(struct ring_buffer_iter *iter)
Steven Rostedtd7690412008-10-01 00:29:53 -04001886{
1887 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
1888
1889 /*
1890 * The iterator could be on the reader page (it starts there).
1891 * But the head could have moved, since the reader was
1892 * found. Check for this case and assign the iterator
1893 * to the head page instead of next.
1894 */
1895 if (iter->head_page == cpu_buffer->reader_page)
Steven Rostedt77ae3652009-03-27 11:00:29 -04001896 iter->head_page = rb_set_head_page(cpu_buffer);
Steven Rostedtd7690412008-10-01 00:29:53 -04001897 else
1898 rb_inc_page(cpu_buffer, &iter->head_page);
1899
Steven Rostedtabc9b562008-12-02 15:34:06 -05001900 iter->read_stamp = iter->head_page->page->time_stamp;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001901 iter->head = 0;
1902}
1903
Steven Rostedt77ae3652009-03-27 11:00:29 -04001904/*
1905 * rb_handle_head_page - writer hit the head page
1906 *
1907 * Returns: +1 to retry page
1908 * 0 to continue
1909 * -1 on error
1910 */
1911static int
1912rb_handle_head_page(struct ring_buffer_per_cpu *cpu_buffer,
1913 struct buffer_page *tail_page,
1914 struct buffer_page *next_page)
1915{
1916 struct buffer_page *new_head;
1917 int entries;
1918 int type;
1919 int ret;
1920
1921 entries = rb_page_entries(next_page);
1922
1923 /*
1924 * The hard part is here. We need to move the head
1925 * forward, and protect against both readers on
1926 * other CPUs and writers coming in via interrupts.
1927 */
1928 type = rb_head_page_set_update(cpu_buffer, next_page, tail_page,
1929 RB_PAGE_HEAD);
1930
1931 /*
1932 * type can be one of four:
1933 * NORMAL - an interrupt already moved it for us
1934 * HEAD - we are the first to get here.
1935 * UPDATE - we are the interrupt interrupting
1936 * a current move.
1937 * MOVED - a reader on another CPU moved the next
1938 * pointer to its reader page. Give up
1939 * and try again.
1940 */
1941
1942 switch (type) {
1943 case RB_PAGE_HEAD:
1944 /*
1945 * We changed the head to UPDATE, thus
1946 * it is our responsibility to update
1947 * the counters.
1948 */
1949 local_add(entries, &cpu_buffer->overrun);
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -07001950 local_sub(BUF_PAGE_SIZE, &cpu_buffer->entries_bytes);
Steven Rostedt77ae3652009-03-27 11:00:29 -04001951
1952 /*
1953 * The entries will be zeroed out when we move the
1954 * tail page.
1955 */
1956
1957 /* still more to do */
1958 break;
1959
1960 case RB_PAGE_UPDATE:
1961 /*
1962 * This is an interrupt that interrupt the
1963 * previous update. Still more to do.
1964 */
1965 break;
1966 case RB_PAGE_NORMAL:
1967 /*
1968 * An interrupt came in before the update
1969 * and processed this for us.
1970 * Nothing left to do.
1971 */
1972 return 1;
1973 case RB_PAGE_MOVED:
1974 /*
1975 * The reader is on another CPU and just did
1976 * a swap with our next_page.
1977 * Try again.
1978 */
1979 return 1;
1980 default:
1981 RB_WARN_ON(cpu_buffer, 1); /* WTF??? */
1982 return -1;
1983 }
1984
1985 /*
1986 * Now that we are here, the old head pointer is
1987 * set to UPDATE. This will keep the reader from
1988 * swapping the head page with the reader page.
1989 * The reader (on another CPU) will spin till
1990 * we are finished.
1991 *
1992 * We just need to protect against interrupts
1993 * doing the job. We will set the next pointer
1994 * to HEAD. After that, we set the old pointer
1995 * to NORMAL, but only if it was HEAD before.
1996 * otherwise we are an interrupt, and only
1997 * want the outer most commit to reset it.
1998 */
1999 new_head = next_page;
2000 rb_inc_page(cpu_buffer, &new_head);
2001
2002 ret = rb_head_page_set_head(cpu_buffer, new_head, next_page,
2003 RB_PAGE_NORMAL);
2004
2005 /*
2006 * Valid returns are:
2007 * HEAD - an interrupt came in and already set it.
2008 * NORMAL - One of two things:
2009 * 1) We really set it.
2010 * 2) A bunch of interrupts came in and moved
2011 * the page forward again.
2012 */
2013 switch (ret) {
2014 case RB_PAGE_HEAD:
2015 case RB_PAGE_NORMAL:
2016 /* OK */
2017 break;
2018 default:
2019 RB_WARN_ON(cpu_buffer, 1);
2020 return -1;
2021 }
2022
2023 /*
2024 * It is possible that an interrupt came in,
2025 * set the head up, then more interrupts came in
2026 * and moved it again. When we get back here,
2027 * the page would have been set to NORMAL but we
2028 * just set it back to HEAD.
2029 *
2030 * How do you detect this? Well, if that happened
2031 * the tail page would have moved.
2032 */
2033 if (ret == RB_PAGE_NORMAL) {
Steven Rostedt (Red Hat)85736362015-11-17 14:03:11 -05002034 struct buffer_page *buffer_tail_page;
2035
2036 buffer_tail_page = READ_ONCE(cpu_buffer->tail_page);
Steven Rostedt77ae3652009-03-27 11:00:29 -04002037 /*
2038 * If the tail had moved passed next, then we need
2039 * to reset the pointer.
2040 */
Steven Rostedt (Red Hat)85736362015-11-17 14:03:11 -05002041 if (buffer_tail_page != tail_page &&
2042 buffer_tail_page != next_page)
Steven Rostedt77ae3652009-03-27 11:00:29 -04002043 rb_head_page_set_normal(cpu_buffer, new_head,
2044 next_page,
2045 RB_PAGE_HEAD);
2046 }
2047
2048 /*
2049 * If this was the outer most commit (the one that
2050 * changed the original pointer from HEAD to UPDATE),
2051 * then it is up to us to reset it to NORMAL.
2052 */
2053 if (type == RB_PAGE_HEAD) {
2054 ret = rb_head_page_set_normal(cpu_buffer, next_page,
2055 tail_page,
2056 RB_PAGE_UPDATE);
2057 if (RB_WARN_ON(cpu_buffer,
2058 ret != RB_PAGE_UPDATE))
2059 return -1;
2060 }
2061
2062 return 0;
2063}
2064
Steven Rostedtc7b09302009-06-11 11:12:00 -04002065static inline void
2066rb_reset_tail(struct ring_buffer_per_cpu *cpu_buffer,
Steven Rostedt (Red Hat)fcc742e2015-05-28 17:13:14 -04002067 unsigned long tail, struct rb_event_info *info)
Steven Rostedtc7b09302009-06-11 11:12:00 -04002068{
Steven Rostedt (Red Hat)fcc742e2015-05-28 17:13:14 -04002069 struct buffer_page *tail_page = info->tail_page;
Steven Rostedtc7b09302009-06-11 11:12:00 -04002070 struct ring_buffer_event *event;
Steven Rostedt (Red Hat)fcc742e2015-05-28 17:13:14 -04002071 unsigned long length = info->length;
Steven Rostedtc7b09302009-06-11 11:12:00 -04002072
2073 /*
2074 * Only the event that crossed the page boundary
2075 * must fill the old tail_page with padding.
2076 */
2077 if (tail >= BUF_PAGE_SIZE) {
Steven Rostedtb3230c82010-05-21 11:55:21 -04002078 /*
2079 * If the page was filled, then we still need
2080 * to update the real_end. Reset it to zero
2081 * and the reader will ignore it.
2082 */
2083 if (tail == BUF_PAGE_SIZE)
2084 tail_page->real_end = 0;
2085
Steven Rostedtc7b09302009-06-11 11:12:00 -04002086 local_sub(length, &tail_page->write);
2087 return;
2088 }
2089
2090 event = __rb_page_index(tail_page, tail);
Linus Torvaldsb0b70652009-06-20 10:56:46 -07002091 kmemcheck_annotate_bitfield(event, bitfield);
Steven Rostedtc7b09302009-06-11 11:12:00 -04002092
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -07002093 /* account for padding bytes */
2094 local_add(BUF_PAGE_SIZE - tail, &cpu_buffer->entries_bytes);
2095
Steven Rostedtc7b09302009-06-11 11:12:00 -04002096 /*
Steven Rostedtff0ff842010-03-31 22:11:42 -04002097 * Save the original length to the meta data.
2098 * This will be used by the reader to add lost event
2099 * counter.
2100 */
2101 tail_page->real_end = tail;
2102
2103 /*
Steven Rostedtc7b09302009-06-11 11:12:00 -04002104 * If this event is bigger than the minimum size, then
2105 * we need to be careful that we don't subtract the
2106 * write counter enough to allow another writer to slip
2107 * in on this page.
2108 * We put in a discarded commit instead, to make sure
2109 * that this space is not used again.
2110 *
2111 * If we are less than the minimum size, we don't need to
2112 * worry about it.
2113 */
2114 if (tail > (BUF_PAGE_SIZE - RB_EVNT_MIN_SIZE)) {
2115 /* No room for any events */
2116
2117 /* Mark the rest of the page with padding */
2118 rb_event_set_padding(event);
2119
2120 /* Set the write back to the previous setting */
2121 local_sub(length, &tail_page->write);
2122 return;
2123 }
2124
2125 /* Put in a discarded event */
2126 event->array[0] = (BUF_PAGE_SIZE - tail) - RB_EVNT_HDR_SIZE;
2127 event->type_len = RINGBUF_TYPE_PADDING;
2128 /* time delta must be non zero */
2129 event->time_delta = 1;
Steven Rostedtc7b09302009-06-11 11:12:00 -04002130
2131 /* Set write to end of buffer */
2132 length = (tail + length) - BUF_PAGE_SIZE;
2133 local_sub(length, &tail_page->write);
2134}
Steven Rostedt6634ff22009-05-06 15:30:07 -04002135
Steven Rostedt (Red Hat)4239c382015-11-17 16:36:06 -05002136static inline void rb_end_commit(struct ring_buffer_per_cpu *cpu_buffer);
2137
Steven Rostedt747e94a2010-10-08 13:51:48 -04002138/*
2139 * This is the slow path, force gcc not to inline it.
2140 */
2141static noinline struct ring_buffer_event *
Steven Rostedt6634ff22009-05-06 15:30:07 -04002142rb_move_tail(struct ring_buffer_per_cpu *cpu_buffer,
Steven Rostedt (Red Hat)fcc742e2015-05-28 17:13:14 -04002143 unsigned long tail, struct rb_event_info *info)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002144{
Steven Rostedt (Red Hat)fcc742e2015-05-28 17:13:14 -04002145 struct buffer_page *tail_page = info->tail_page;
Steven Rostedt5a50e332009-11-17 08:43:01 -05002146 struct buffer_page *commit_page = cpu_buffer->commit_page;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002147 struct ring_buffer *buffer = cpu_buffer->buffer;
Steven Rostedt77ae3652009-03-27 11:00:29 -04002148 struct buffer_page *next_page;
2149 int ret;
Steven Rostedtaa20ae82009-05-05 21:16:11 -04002150
2151 next_page = tail_page;
2152
Steven Rostedtaa20ae82009-05-05 21:16:11 -04002153 rb_inc_page(cpu_buffer, &next_page);
2154
Steven Rostedtaa20ae82009-05-05 21:16:11 -04002155 /*
2156 * If for some reason, we had an interrupt storm that made
2157 * it all the way around the buffer, bail, and warn
2158 * about it.
2159 */
2160 if (unlikely(next_page == commit_page)) {
Steven Rostedt77ae3652009-03-27 11:00:29 -04002161 local_inc(&cpu_buffer->commit_overrun);
Steven Rostedtaa20ae82009-05-05 21:16:11 -04002162 goto out_reset;
2163 }
2164
Steven Rostedt77ae3652009-03-27 11:00:29 -04002165 /*
2166 * This is where the fun begins!
2167 *
2168 * We are fighting against races between a reader that
2169 * could be on another CPU trying to swap its reader
2170 * page with the buffer head.
2171 *
2172 * We are also fighting against interrupts coming in and
2173 * moving the head or tail on us as well.
2174 *
2175 * If the next page is the head page then we have filled
2176 * the buffer, unless the commit page is still on the
2177 * reader page.
2178 */
2179 if (rb_is_head_page(cpu_buffer, next_page, &tail_page->list)) {
Steven Rostedtaa20ae82009-05-05 21:16:11 -04002180
Steven Rostedt77ae3652009-03-27 11:00:29 -04002181 /*
2182 * If the commit is not on the reader page, then
2183 * move the header page.
2184 */
2185 if (!rb_is_reader_page(cpu_buffer->commit_page)) {
2186 /*
2187 * If we are not in overwrite mode,
2188 * this is easy, just stop here.
2189 */
Slava Pestov884bfe82011-07-15 14:23:58 -07002190 if (!(buffer->flags & RB_FL_OVERWRITE)) {
2191 local_inc(&cpu_buffer->dropped_events);
Steven Rostedt77ae3652009-03-27 11:00:29 -04002192 goto out_reset;
Slava Pestov884bfe82011-07-15 14:23:58 -07002193 }
Steven Rostedtaa20ae82009-05-05 21:16:11 -04002194
Steven Rostedt77ae3652009-03-27 11:00:29 -04002195 ret = rb_handle_head_page(cpu_buffer,
2196 tail_page,
2197 next_page);
2198 if (ret < 0)
2199 goto out_reset;
2200 if (ret)
2201 goto out_again;
2202 } else {
2203 /*
2204 * We need to be careful here too. The
2205 * commit page could still be on the reader
2206 * page. We could have a small buffer, and
2207 * have filled up the buffer with events
2208 * from interrupts and such, and wrapped.
2209 *
2210 * Note, if the tail page is also the on the
2211 * reader_page, we let it move out.
2212 */
2213 if (unlikely((cpu_buffer->commit_page !=
2214 cpu_buffer->tail_page) &&
2215 (cpu_buffer->commit_page ==
2216 cpu_buffer->reader_page))) {
2217 local_inc(&cpu_buffer->commit_overrun);
2218 goto out_reset;
2219 }
Steven Rostedtaa20ae82009-05-05 21:16:11 -04002220 }
2221 }
2222
Steven Rostedt (Red Hat)70004982015-11-17 15:15:19 -05002223 rb_tail_page_update(cpu_buffer, tail_page, next_page);
Steven Rostedtaa20ae82009-05-05 21:16:11 -04002224
Steven Rostedt77ae3652009-03-27 11:00:29 -04002225 out_again:
Steven Rostedtaa20ae82009-05-05 21:16:11 -04002226
Steven Rostedt (Red Hat)fcc742e2015-05-28 17:13:14 -04002227 rb_reset_tail(cpu_buffer, tail, info);
Steven Rostedtaa20ae82009-05-05 21:16:11 -04002228
Steven Rostedt (Red Hat)4239c382015-11-17 16:36:06 -05002229 /* Commit what we have for now. */
2230 rb_end_commit(cpu_buffer);
2231 /* rb_end_commit() decs committing */
2232 local_inc(&cpu_buffer->committing);
2233
Steven Rostedtaa20ae82009-05-05 21:16:11 -04002234 /* fail and let the caller try again */
2235 return ERR_PTR(-EAGAIN);
2236
Steven Rostedt45141d42009-02-12 13:19:48 -05002237 out_reset:
Lai Jiangshan6f3b3442009-01-12 11:06:18 +08002238 /* reset write */
Steven Rostedt (Red Hat)fcc742e2015-05-28 17:13:14 -04002239 rb_reset_tail(cpu_buffer, tail, info);
Lai Jiangshan6f3b3442009-01-12 11:06:18 +08002240
Steven Rostedtbf41a152008-10-04 02:00:59 -04002241 return NULL;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002242}
2243
Steven Rostedt (Red Hat)d90fd772015-05-29 12:12:27 -04002244/* Slow path, do not inline */
2245static noinline struct ring_buffer_event *
2246rb_add_time_stamp(struct ring_buffer_event *event, u64 delta)
2247{
2248 event->type_len = RINGBUF_TYPE_TIME_EXTEND;
2249
2250 /* Not the first event on the page? */
2251 if (rb_event_index(event)) {
2252 event->time_delta = delta & TS_MASK;
2253 event->array[0] = delta >> TS_SHIFT;
2254 } else {
2255 /* nope, just zero it */
2256 event->time_delta = 0;
2257 event->array[0] = 0;
2258 }
2259
2260 return skip_time_extend(event);
2261}
2262
Yaowei Baicdb2a0a2015-09-29 22:43:34 +08002263static inline bool rb_event_is_commit(struct ring_buffer_per_cpu *cpu_buffer,
Steven Rostedt (Red Hat)b7dc42f2015-09-03 08:57:12 -04002264 struct ring_buffer_event *event);
2265
Steven Rostedt (Red Hat)d90fd772015-05-29 12:12:27 -04002266/**
2267 * rb_update_event - update event type and data
2268 * @event: the event to update
2269 * @type: the type of event
2270 * @length: the size of the event field in the ring buffer
2271 *
2272 * Update the type and data fields of the event. The length
2273 * is the actual size that is written to the ring buffer,
2274 * and with this, we can determine what to place into the
2275 * data field.
2276 */
Steven Rostedt (Red Hat)b7dc42f2015-09-03 08:57:12 -04002277static void
Steven Rostedt (Red Hat)d90fd772015-05-29 12:12:27 -04002278rb_update_event(struct ring_buffer_per_cpu *cpu_buffer,
2279 struct ring_buffer_event *event,
2280 struct rb_event_info *info)
2281{
2282 unsigned length = info->length;
2283 u64 delta = info->delta;
2284
Steven Rostedt (Red Hat)b7dc42f2015-09-03 08:57:12 -04002285 /* Only a commit updates the timestamp */
2286 if (unlikely(!rb_event_is_commit(cpu_buffer, event)))
2287 delta = 0;
2288
Steven Rostedt (Red Hat)d90fd772015-05-29 12:12:27 -04002289 /*
2290 * If we need to add a timestamp, then we
2291 * add it to the start of the resevered space.
2292 */
2293 if (unlikely(info->add_timestamp)) {
2294 event = rb_add_time_stamp(event, delta);
2295 length -= RB_LEN_TIME_EXTEND;
2296 delta = 0;
2297 }
2298
2299 event->time_delta = delta;
2300 length -= RB_EVNT_HDR_SIZE;
2301 if (length > RB_MAX_SMALL_DATA || RB_FORCE_8BYTE_ALIGNMENT) {
2302 event->type_len = 0;
2303 event->array[0] = length;
2304 } else
2305 event->type_len = DIV_ROUND_UP(length, RB_ALIGNMENT);
2306}
2307
2308static unsigned rb_calculate_event_length(unsigned length)
2309{
2310 struct ring_buffer_event event; /* Used only for sizeof array */
2311
2312 /* zero length can cause confusions */
2313 if (!length)
2314 length++;
2315
2316 if (length > RB_MAX_SMALL_DATA || RB_FORCE_8BYTE_ALIGNMENT)
2317 length += sizeof(event.array[0]);
2318
2319 length += RB_EVNT_HDR_SIZE;
2320 length = ALIGN(length, RB_ARCH_ALIGNMENT);
2321
2322 /*
2323 * In case the time delta is larger than the 27 bits for it
2324 * in the header, we need to add a timestamp. If another
2325 * event comes in when trying to discard this one to increase
2326 * the length, then the timestamp will be added in the allocated
2327 * space of this event. If length is bigger than the size needed
2328 * for the TIME_EXTEND, then padding has to be used. The events
2329 * length must be either RB_LEN_TIME_EXTEND, or greater than or equal
2330 * to RB_LEN_TIME_EXTEND + 8, as 8 is the minimum size for padding.
2331 * As length is a multiple of 4, we only need to worry if it
2332 * is 12 (RB_LEN_TIME_EXTEND + 4).
2333 */
2334 if (length == RB_LEN_TIME_EXTEND + RB_ALIGNMENT)
2335 length += RB_ALIGNMENT;
2336
2337 return length;
2338}
2339
Steven Rostedt (Red Hat)9826b272015-05-28 17:36:45 -04002340#ifndef CONFIG_HAVE_UNSTABLE_SCHED_CLOCK
2341static inline bool sched_clock_stable(void)
2342{
2343 return true;
2344}
2345#endif
2346
Steven Rostedt (Red Hat)a4543a22015-05-29 09:40:18 -04002347static inline int
2348rb_try_to_discard(struct ring_buffer_per_cpu *cpu_buffer,
Steven Rostedt (Red Hat)d90fd772015-05-29 12:12:27 -04002349 struct ring_buffer_event *event)
2350{
2351 unsigned long new_index, old_index;
2352 struct buffer_page *bpage;
2353 unsigned long index;
2354 unsigned long addr;
2355
2356 new_index = rb_event_index(event);
2357 old_index = new_index + rb_event_ts_length(event);
2358 addr = (unsigned long)event;
2359 addr &= PAGE_MASK;
2360
Steven Rostedt (Red Hat)85736362015-11-17 14:03:11 -05002361 bpage = READ_ONCE(cpu_buffer->tail_page);
Steven Rostedt (Red Hat)d90fd772015-05-29 12:12:27 -04002362
2363 if (bpage->page == (void *)addr && rb_page_write(bpage) == old_index) {
2364 unsigned long write_mask =
2365 local_read(&bpage->write) & ~RB_WRITE_MASK;
2366 unsigned long event_length = rb_event_length(event);
2367 /*
2368 * This is on the tail page. It is possible that
2369 * a write could come in and move the tail page
2370 * and write to the next page. That is fine
2371 * because we just shorten what is on this page.
2372 */
2373 old_index += write_mask;
2374 new_index += write_mask;
2375 index = local_cmpxchg(&bpage->write, old_index, new_index);
2376 if (index == old_index) {
2377 /* update counters */
2378 local_sub(event_length, &cpu_buffer->entries_bytes);
2379 return 1;
2380 }
2381 }
2382
2383 /* could not discard */
2384 return 0;
2385}
2386
2387static void rb_start_commit(struct ring_buffer_per_cpu *cpu_buffer)
2388{
2389 local_inc(&cpu_buffer->committing);
2390 local_inc(&cpu_buffer->commits);
2391}
2392
2393static void
2394rb_set_commit_to_write(struct ring_buffer_per_cpu *cpu_buffer)
2395{
2396 unsigned long max_count;
2397
2398 /*
2399 * We only race with interrupts and NMIs on this CPU.
2400 * If we own the commit event, then we can commit
2401 * all others that interrupted us, since the interruptions
2402 * are in stack format (they finish before they come
2403 * back to us). This allows us to do a simple loop to
2404 * assign the commit to the tail.
2405 */
2406 again:
2407 max_count = cpu_buffer->nr_pages * 100;
2408
Steven Rostedt (Red Hat)85736362015-11-17 14:03:11 -05002409 while (cpu_buffer->commit_page != READ_ONCE(cpu_buffer->tail_page)) {
Steven Rostedt (Red Hat)d90fd772015-05-29 12:12:27 -04002410 if (RB_WARN_ON(cpu_buffer, !(--max_count)))
2411 return;
2412 if (RB_WARN_ON(cpu_buffer,
2413 rb_is_reader_page(cpu_buffer->tail_page)))
2414 return;
2415 local_set(&cpu_buffer->commit_page->page->commit,
2416 rb_page_write(cpu_buffer->commit_page));
2417 rb_inc_page(cpu_buffer, &cpu_buffer->commit_page);
Steven Rostedt (Red Hat)70004982015-11-17 15:15:19 -05002418 /* Only update the write stamp if the page has an event */
2419 if (rb_page_write(cpu_buffer->commit_page))
2420 cpu_buffer->write_stamp =
2421 cpu_buffer->commit_page->page->time_stamp;
Steven Rostedt (Red Hat)d90fd772015-05-29 12:12:27 -04002422 /* add barrier to keep gcc from optimizing too much */
2423 barrier();
2424 }
2425 while (rb_commit_index(cpu_buffer) !=
2426 rb_page_write(cpu_buffer->commit_page)) {
2427
2428 local_set(&cpu_buffer->commit_page->page->commit,
2429 rb_page_write(cpu_buffer->commit_page));
2430 RB_WARN_ON(cpu_buffer,
2431 local_read(&cpu_buffer->commit_page->page->commit) &
2432 ~RB_WRITE_MASK);
2433 barrier();
2434 }
2435
2436 /* again, keep gcc from optimizing */
2437 barrier();
2438
2439 /*
2440 * If an interrupt came in just after the first while loop
2441 * and pushed the tail page forward, we will be left with
2442 * a dangling commit that will never go forward.
2443 */
Steven Rostedt (Red Hat)85736362015-11-17 14:03:11 -05002444 if (unlikely(cpu_buffer->commit_page != READ_ONCE(cpu_buffer->tail_page)))
Steven Rostedt (Red Hat)d90fd772015-05-29 12:12:27 -04002445 goto again;
2446}
2447
2448static inline void rb_end_commit(struct ring_buffer_per_cpu *cpu_buffer)
2449{
2450 unsigned long commits;
2451
2452 if (RB_WARN_ON(cpu_buffer,
2453 !local_read(&cpu_buffer->committing)))
2454 return;
2455
2456 again:
2457 commits = local_read(&cpu_buffer->commits);
2458 /* synchronize with interrupts */
2459 barrier();
2460 if (local_read(&cpu_buffer->committing) == 1)
2461 rb_set_commit_to_write(cpu_buffer);
2462
2463 local_dec(&cpu_buffer->committing);
2464
2465 /* synchronize with interrupts */
2466 barrier();
2467
2468 /*
2469 * Need to account for interrupts coming in between the
2470 * updating of the commit page and the clearing of the
2471 * committing counter.
2472 */
2473 if (unlikely(local_read(&cpu_buffer->commits) != commits) &&
2474 !local_read(&cpu_buffer->committing)) {
2475 local_inc(&cpu_buffer->committing);
2476 goto again;
2477 }
2478}
2479
2480static inline void rb_event_discard(struct ring_buffer_event *event)
2481{
2482 if (event->type_len == RINGBUF_TYPE_TIME_EXTEND)
2483 event = skip_time_extend(event);
2484
2485 /* array[0] holds the actual length for the discarded event */
2486 event->array[0] = rb_event_data_length(event) - RB_EVNT_HDR_SIZE;
2487 event->type_len = RINGBUF_TYPE_PADDING;
2488 /* time delta must be non zero */
2489 if (!event->time_delta)
2490 event->time_delta = 1;
2491}
2492
Yaowei Baicdb2a0a2015-09-29 22:43:34 +08002493static inline bool
Steven Rostedt (Red Hat)d90fd772015-05-29 12:12:27 -04002494rb_event_is_commit(struct ring_buffer_per_cpu *cpu_buffer,
2495 struct ring_buffer_event *event)
2496{
2497 unsigned long addr = (unsigned long)event;
2498 unsigned long index;
2499
2500 index = rb_event_index(event);
2501 addr &= PAGE_MASK;
2502
2503 return cpu_buffer->commit_page->page == (void *)addr &&
2504 rb_commit_index(cpu_buffer) == index;
2505}
2506
Steven Rostedt (Red Hat)a4543a22015-05-29 09:40:18 -04002507static void
2508rb_update_write_stamp(struct ring_buffer_per_cpu *cpu_buffer,
Steven Rostedt (Red Hat)d90fd772015-05-29 12:12:27 -04002509 struct ring_buffer_event *event)
2510{
2511 u64 delta;
2512
2513 /*
2514 * The event first in the commit queue updates the
2515 * time stamp.
2516 */
2517 if (rb_event_is_commit(cpu_buffer, event)) {
2518 /*
2519 * A commit event that is first on a page
2520 * updates the write timestamp with the page stamp
2521 */
2522 if (!rb_event_index(event))
2523 cpu_buffer->write_stamp =
2524 cpu_buffer->commit_page->page->time_stamp;
2525 else if (event->type_len == RINGBUF_TYPE_TIME_EXTEND) {
2526 delta = event->array[0];
2527 delta <<= TS_SHIFT;
2528 delta += event->time_delta;
2529 cpu_buffer->write_stamp += delta;
2530 } else
2531 cpu_buffer->write_stamp += event->time_delta;
2532 }
2533}
2534
2535static void rb_commit(struct ring_buffer_per_cpu *cpu_buffer,
2536 struct ring_buffer_event *event)
2537{
2538 local_inc(&cpu_buffer->entries);
2539 rb_update_write_stamp(cpu_buffer, event);
2540 rb_end_commit(cpu_buffer);
2541}
2542
2543static __always_inline void
2544rb_wakeups(struct ring_buffer *buffer, struct ring_buffer_per_cpu *cpu_buffer)
2545{
2546 bool pagebusy;
2547
2548 if (buffer->irq_work.waiters_pending) {
2549 buffer->irq_work.waiters_pending = false;
2550 /* irq_work_queue() supplies it's own memory barriers */
2551 irq_work_queue(&buffer->irq_work.work);
2552 }
2553
2554 if (cpu_buffer->irq_work.waiters_pending) {
2555 cpu_buffer->irq_work.waiters_pending = false;
2556 /* irq_work_queue() supplies it's own memory barriers */
2557 irq_work_queue(&cpu_buffer->irq_work.work);
2558 }
2559
2560 pagebusy = cpu_buffer->reader_page == cpu_buffer->commit_page;
2561
2562 if (!pagebusy && cpu_buffer->irq_work.full_waiters_pending) {
2563 cpu_buffer->irq_work.wakeup_full = true;
2564 cpu_buffer->irq_work.full_waiters_pending = false;
2565 /* irq_work_queue() supplies it's own memory barriers */
2566 irq_work_queue(&cpu_buffer->irq_work.work);
2567 }
2568}
2569
2570/*
2571 * The lock and unlock are done within a preempt disable section.
2572 * The current_context per_cpu variable can only be modified
2573 * by the current task between lock and unlock. But it can
2574 * be modified more than once via an interrupt. To pass this
2575 * information from the lock to the unlock without having to
2576 * access the 'in_interrupt()' functions again (which do show
2577 * a bit of overhead in something as critical as function tracing,
2578 * we use a bitmask trick.
2579 *
2580 * bit 0 = NMI context
2581 * bit 1 = IRQ context
2582 * bit 2 = SoftIRQ context
2583 * bit 3 = normal context.
2584 *
2585 * This works because this is the order of contexts that can
2586 * preempt other contexts. A SoftIRQ never preempts an IRQ
2587 * context.
2588 *
2589 * When the context is determined, the corresponding bit is
2590 * checked and set (if it was set, then a recursion of that context
2591 * happened).
2592 *
2593 * On unlock, we need to clear this bit. To do so, just subtract
2594 * 1 from the current_context and AND it to itself.
2595 *
2596 * (binary)
2597 * 101 - 1 = 100
2598 * 101 & 100 = 100 (clearing bit zero)
2599 *
2600 * 1010 - 1 = 1001
2601 * 1010 & 1001 = 1000 (clearing bit 1)
2602 *
2603 * The least significant bit can be cleared this way, and it
2604 * just so happens that it is the same bit corresponding to
2605 * the current context.
2606 */
2607
2608static __always_inline int
2609trace_recursive_lock(struct ring_buffer_per_cpu *cpu_buffer)
2610{
2611 unsigned int val = cpu_buffer->current_context;
2612 int bit;
2613
2614 if (in_interrupt()) {
2615 if (in_nmi())
2616 bit = RB_CTX_NMI;
2617 else if (in_irq())
2618 bit = RB_CTX_IRQ;
2619 else
2620 bit = RB_CTX_SOFTIRQ;
2621 } else
2622 bit = RB_CTX_NORMAL;
2623
2624 if (unlikely(val & (1 << bit)))
2625 return 1;
2626
2627 val |= (1 << bit);
2628 cpu_buffer->current_context = val;
2629
2630 return 0;
2631}
2632
2633static __always_inline void
2634trace_recursive_unlock(struct ring_buffer_per_cpu *cpu_buffer)
2635{
2636 cpu_buffer->current_context &= cpu_buffer->current_context - 1;
2637}
2638
2639/**
2640 * ring_buffer_unlock_commit - commit a reserved
2641 * @buffer: The buffer to commit to
2642 * @event: The event pointer to commit.
2643 *
2644 * This commits the data to the ring buffer, and releases any locks held.
2645 *
2646 * Must be paired with ring_buffer_lock_reserve.
2647 */
2648int ring_buffer_unlock_commit(struct ring_buffer *buffer,
2649 struct ring_buffer_event *event)
2650{
2651 struct ring_buffer_per_cpu *cpu_buffer;
2652 int cpu = raw_smp_processor_id();
2653
2654 cpu_buffer = buffer->buffers[cpu];
2655
2656 rb_commit(cpu_buffer, event);
2657
2658 rb_wakeups(buffer, cpu_buffer);
2659
2660 trace_recursive_unlock(cpu_buffer);
2661
2662 preempt_enable_notrace();
2663
2664 return 0;
2665}
2666EXPORT_SYMBOL_GPL(ring_buffer_unlock_commit);
Steven Rostedt (Red Hat)a4543a22015-05-29 09:40:18 -04002667
Steven Rostedt (Red Hat)9826b272015-05-28 17:36:45 -04002668static noinline void
2669rb_handle_timestamp(struct ring_buffer_per_cpu *cpu_buffer,
2670 struct rb_event_info *info)
2671{
2672 WARN_ONCE(info->delta > (1ULL << 59),
2673 KERN_WARNING "Delta way too big! %llu ts=%llu write stamp = %llu\n%s",
2674 (unsigned long long)info->delta,
2675 (unsigned long long)info->ts,
2676 (unsigned long long)cpu_buffer->write_stamp,
2677 sched_clock_stable() ? "" :
2678 "If you just came from a suspend/resume,\n"
2679 "please switch to the trace global clock:\n"
2680 " echo global > /sys/kernel/debug/tracing/trace_clock\n");
Steven Rostedt (Red Hat)b7dc42f2015-09-03 08:57:12 -04002681 info->add_timestamp = 1;
Steven Rostedt (Red Hat)9826b272015-05-28 17:36:45 -04002682}
2683
Steven Rostedt6634ff22009-05-06 15:30:07 -04002684static struct ring_buffer_event *
2685__rb_reserve_next(struct ring_buffer_per_cpu *cpu_buffer,
Steven Rostedt (Red Hat)fcc742e2015-05-28 17:13:14 -04002686 struct rb_event_info *info)
Steven Rostedt6634ff22009-05-06 15:30:07 -04002687{
Steven Rostedt6634ff22009-05-06 15:30:07 -04002688 struct ring_buffer_event *event;
Steven Rostedt (Red Hat)fcc742e2015-05-28 17:13:14 -04002689 struct buffer_page *tail_page;
Steven Rostedt6634ff22009-05-06 15:30:07 -04002690 unsigned long tail, write;
Steven Rostedt (Red Hat)b7dc42f2015-09-03 08:57:12 -04002691
2692 /*
2693 * If the time delta since the last event is too big to
2694 * hold in the time field of the event, then we append a
2695 * TIME EXTEND event ahead of the data event.
2696 */
2697 if (unlikely(info->add_timestamp))
2698 info->length += RB_LEN_TIME_EXTEND;
Steven Rostedt69d1b832010-10-07 18:18:05 -04002699
Steven Rostedt (Red Hat)85736362015-11-17 14:03:11 -05002700 /* Don't let the compiler play games with cpu_buffer->tail_page */
2701 tail_page = info->tail_page = READ_ONCE(cpu_buffer->tail_page);
Steven Rostedt (Red Hat)fcc742e2015-05-28 17:13:14 -04002702 write = local_add_return(info->length, &tail_page->write);
Steven Rostedt77ae3652009-03-27 11:00:29 -04002703
2704 /* set write to only the index of the write */
2705 write &= RB_WRITE_MASK;
Steven Rostedt (Red Hat)fcc742e2015-05-28 17:13:14 -04002706 tail = write - info->length;
Steven Rostedt6634ff22009-05-06 15:30:07 -04002707
Steven Rostedt (Red Hat)b7dc42f2015-09-03 08:57:12 -04002708 /*
2709 * If this is the first commit on the page, then it has the same
2710 * timestamp as the page itself.
2711 */
2712 if (!tail)
2713 info->delta = 0;
2714
Steven Rostedt6634ff22009-05-06 15:30:07 -04002715 /* See if we shot pass the end of this buffer page */
Steven Rostedt747e94a2010-10-08 13:51:48 -04002716 if (unlikely(write > BUF_PAGE_SIZE))
Steven Rostedt (Red Hat)fcc742e2015-05-28 17:13:14 -04002717 return rb_move_tail(cpu_buffer, tail, info);
Steven Rostedt6634ff22009-05-06 15:30:07 -04002718
2719 /* We reserved something on the buffer */
Steven Rostedt (Red Hat)b7dc42f2015-09-03 08:57:12 -04002720
Steven Rostedt6634ff22009-05-06 15:30:07 -04002721 event = __rb_page_index(tail_page, tail);
Vegard Nossum1744a212009-02-28 08:29:44 +01002722 kmemcheck_annotate_bitfield(event, bitfield);
Steven Rostedt (Red Hat)fcc742e2015-05-28 17:13:14 -04002723 rb_update_event(cpu_buffer, event, info);
Steven Rostedt6634ff22009-05-06 15:30:07 -04002724
Steven Rostedt69d1b832010-10-07 18:18:05 -04002725 local_inc(&tail_page->entries);
Steven Rostedt6634ff22009-05-06 15:30:07 -04002726
Steven Rostedt (Red Hat)b7dc42f2015-09-03 08:57:12 -04002727 /*
2728 * If this is the first commit on the page, then update
2729 * its timestamp.
2730 */
2731 if (!tail)
2732 tail_page->page->time_stamp = info->ts;
2733
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -07002734 /* account for these added bytes */
Steven Rostedt (Red Hat)fcc742e2015-05-28 17:13:14 -04002735 local_add(info->length, &cpu_buffer->entries_bytes);
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -07002736
Steven Rostedt6634ff22009-05-06 15:30:07 -04002737 return event;
2738}
2739
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002740static struct ring_buffer_event *
Steven Rostedt62f0b3e2009-09-04 14:11:34 -04002741rb_reserve_next_event(struct ring_buffer *buffer,
2742 struct ring_buffer_per_cpu *cpu_buffer,
Steven Rostedt1cd8d732009-05-11 14:08:09 -04002743 unsigned long length)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002744{
2745 struct ring_buffer_event *event;
Steven Rostedt (Red Hat)fcc742e2015-05-28 17:13:14 -04002746 struct rb_event_info info;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002747 int nr_loops = 0;
Steven Rostedt (Red Hat)b7dc42f2015-09-03 08:57:12 -04002748 u64 diff;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002749
Steven Rostedtfa743952009-06-16 12:37:57 -04002750 rb_start_commit(cpu_buffer);
2751
Steven Rostedt85bac322009-09-04 14:24:40 -04002752#ifdef CONFIG_RING_BUFFER_ALLOW_SWAP
Steven Rostedt62f0b3e2009-09-04 14:11:34 -04002753 /*
2754 * Due to the ability to swap a cpu buffer from a buffer
2755 * it is possible it was swapped before we committed.
2756 * (committing stops a swap). We check for it here and
2757 * if it happened, we have to fail the write.
2758 */
2759 barrier();
2760 if (unlikely(ACCESS_ONCE(cpu_buffer->buffer) != buffer)) {
2761 local_dec(&cpu_buffer->committing);
2762 local_dec(&cpu_buffer->commits);
2763 return NULL;
2764 }
Steven Rostedt85bac322009-09-04 14:24:40 -04002765#endif
Steven Rostedt (Red Hat)b7dc42f2015-09-03 08:57:12 -04002766
Steven Rostedt (Red Hat)fcc742e2015-05-28 17:13:14 -04002767 info.length = rb_calculate_event_length(length);
Steven Rostedt (Red Hat)a4543a22015-05-29 09:40:18 -04002768 again:
Steven Rostedt (Red Hat)b7dc42f2015-09-03 08:57:12 -04002769 info.add_timestamp = 0;
2770 info.delta = 0;
2771
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002772 /*
2773 * We allow for interrupts to reenter here and do a trace.
2774 * If one does, it will cause this original code to loop
2775 * back here. Even with heavy interrupts happening, this
2776 * should only happen a few times in a row. If this happens
2777 * 1000 times in a row, there must be either an interrupt
2778 * storm or we have something buggy.
2779 * Bail!
2780 */
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05002781 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 1000))
Steven Rostedtfa743952009-06-16 12:37:57 -04002782 goto out_fail;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002783
Steven Rostedt (Red Hat)b7dc42f2015-09-03 08:57:12 -04002784 info.ts = rb_time_stamp(cpu_buffer->buffer);
2785 diff = info.ts - cpu_buffer->write_stamp;
2786
2787 /* make sure this diff is calculated here */
2788 barrier();
2789
2790 /* Did the write stamp get updated already? */
2791 if (likely(info.ts >= cpu_buffer->write_stamp)) {
2792 info.delta = diff;
2793 if (unlikely(test_time_stamp(info.delta)))
2794 rb_handle_timestamp(cpu_buffer, &info);
2795 }
2796
Steven Rostedt (Red Hat)fcc742e2015-05-28 17:13:14 -04002797 event = __rb_reserve_next(cpu_buffer, &info);
2798
Steven Rostedt (Red Hat)bd1b7cd2015-11-23 17:35:24 -05002799 if (unlikely(PTR_ERR(event) == -EAGAIN)) {
2800 if (info.add_timestamp)
2801 info.length -= RB_LEN_TIME_EXTEND;
Steven Rostedtbf41a152008-10-04 02:00:59 -04002802 goto again;
Steven Rostedt (Red Hat)bd1b7cd2015-11-23 17:35:24 -05002803 }
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002804
Steven Rostedtfa743952009-06-16 12:37:57 -04002805 if (!event)
2806 goto out_fail;
Steven Rostedtbf41a152008-10-04 02:00:59 -04002807
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002808 return event;
Steven Rostedtfa743952009-06-16 12:37:57 -04002809
2810 out_fail:
2811 rb_end_commit(cpu_buffer);
2812 return NULL;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002813}
2814
2815/**
2816 * ring_buffer_lock_reserve - reserve a part of the buffer
2817 * @buffer: the ring buffer to reserve from
2818 * @length: the length of the data to reserve (excluding event header)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002819 *
2820 * Returns a reseverd event on the ring buffer to copy directly to.
2821 * The user of this interface will need to get the body to write into
2822 * and can use the ring_buffer_event_data() interface.
2823 *
2824 * The length is the length of the data needed, not the event length
2825 * which also includes the event header.
2826 *
2827 * Must be paired with ring_buffer_unlock_commit, unless NULL is returned.
2828 * If NULL is returned, then nothing has been allocated or locked.
2829 */
2830struct ring_buffer_event *
Arnaldo Carvalho de Melo0a987752009-02-05 16:12:56 -02002831ring_buffer_lock_reserve(struct ring_buffer *buffer, unsigned long length)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002832{
2833 struct ring_buffer_per_cpu *cpu_buffer;
2834 struct ring_buffer_event *event;
Steven Rostedt5168ae52010-06-03 09:36:50 -04002835 int cpu;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002836
Steven Rostedtbf41a152008-10-04 02:00:59 -04002837 /* If we are tracing schedule, we don't want to recurse */
Steven Rostedt5168ae52010-06-03 09:36:50 -04002838 preempt_disable_notrace();
Steven Rostedtbf41a152008-10-04 02:00:59 -04002839
Steven Rostedt (Red Hat)3205f802015-05-21 17:39:29 -04002840 if (unlikely(atomic_read(&buffer->record_disabled)))
Steven Rostedt (Red Hat)58a09ec2015-05-27 10:27:47 -04002841 goto out;
Steven Rostedt261842b2009-04-16 21:41:52 -04002842
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002843 cpu = raw_smp_processor_id();
2844
Steven Rostedt (Red Hat)3205f802015-05-21 17:39:29 -04002845 if (unlikely(!cpumask_test_cpu(cpu, buffer->cpumask)))
Steven Rostedtd7690412008-10-01 00:29:53 -04002846 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002847
2848 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002849
Steven Rostedt (Red Hat)3205f802015-05-21 17:39:29 -04002850 if (unlikely(atomic_read(&cpu_buffer->record_disabled)))
Steven Rostedtd7690412008-10-01 00:29:53 -04002851 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002852
Steven Rostedt (Red Hat)3205f802015-05-21 17:39:29 -04002853 if (unlikely(length > BUF_MAX_DATA_SIZE))
Steven Rostedtbf41a152008-10-04 02:00:59 -04002854 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002855
Steven Rostedt (Red Hat)58a09ec2015-05-27 10:27:47 -04002856 if (unlikely(trace_recursive_lock(cpu_buffer)))
2857 goto out;
2858
Steven Rostedt62f0b3e2009-09-04 14:11:34 -04002859 event = rb_reserve_next_event(buffer, cpu_buffer, length);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002860 if (!event)
Steven Rostedt (Red Hat)58a09ec2015-05-27 10:27:47 -04002861 goto out_unlock;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002862
2863 return event;
2864
Steven Rostedt (Red Hat)58a09ec2015-05-27 10:27:47 -04002865 out_unlock:
2866 trace_recursive_unlock(cpu_buffer);
Steven Rostedtd7690412008-10-01 00:29:53 -04002867 out:
Steven Rostedt5168ae52010-06-03 09:36:50 -04002868 preempt_enable_notrace();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002869 return NULL;
2870}
Robert Richterc4f50182008-12-11 16:49:22 +01002871EXPORT_SYMBOL_GPL(ring_buffer_lock_reserve);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002872
Steven Rostedta1863c22009-09-03 10:23:58 -04002873/*
2874 * Decrement the entries to the page that an event is on.
2875 * The event does not even need to exist, only the pointer
2876 * to the page it is on. This may only be called before the commit
2877 * takes place.
2878 */
2879static inline void
2880rb_decrement_entry(struct ring_buffer_per_cpu *cpu_buffer,
2881 struct ring_buffer_event *event)
2882{
2883 unsigned long addr = (unsigned long)event;
2884 struct buffer_page *bpage = cpu_buffer->commit_page;
2885 struct buffer_page *start;
2886
2887 addr &= PAGE_MASK;
2888
2889 /* Do the likely case first */
2890 if (likely(bpage->page == (void *)addr)) {
2891 local_dec(&bpage->entries);
2892 return;
2893 }
2894
2895 /*
2896 * Because the commit page may be on the reader page we
2897 * start with the next page and check the end loop there.
2898 */
2899 rb_inc_page(cpu_buffer, &bpage);
2900 start = bpage;
2901 do {
2902 if (bpage->page == (void *)addr) {
2903 local_dec(&bpage->entries);
2904 return;
2905 }
2906 rb_inc_page(cpu_buffer, &bpage);
2907 } while (bpage != start);
2908
2909 /* commit not part of this buffer?? */
2910 RB_WARN_ON(cpu_buffer, 1);
2911}
2912
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002913/**
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002914 * ring_buffer_commit_discard - discard an event that has not been committed
2915 * @buffer: the ring buffer
2916 * @event: non committed event to discard
2917 *
Steven Rostedtdc892f72009-09-03 15:33:41 -04002918 * Sometimes an event that is in the ring buffer needs to be ignored.
2919 * This function lets the user discard an event in the ring buffer
2920 * and then that event will not be read later.
2921 *
2922 * This function only works if it is called before the the item has been
2923 * committed. It will try to free the event from the ring buffer
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002924 * if another event has not been added behind it.
2925 *
2926 * If another event has been added behind it, it will set the event
2927 * up as discarded, and perform the commit.
2928 *
2929 * If this function is called, do not call ring_buffer_unlock_commit on
2930 * the event.
2931 */
2932void ring_buffer_discard_commit(struct ring_buffer *buffer,
2933 struct ring_buffer_event *event)
2934{
2935 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002936 int cpu;
2937
2938 /* The event is discarded regardless */
Frederic Weisbeckerf3b9aae2009-04-19 23:39:33 +02002939 rb_event_discard(event);
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002940
Steven Rostedtfa743952009-06-16 12:37:57 -04002941 cpu = smp_processor_id();
2942 cpu_buffer = buffer->buffers[cpu];
2943
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002944 /*
2945 * This must only be called if the event has not been
2946 * committed yet. Thus we can assume that preemption
2947 * is still disabled.
2948 */
Steven Rostedtfa743952009-06-16 12:37:57 -04002949 RB_WARN_ON(buffer, !local_read(&cpu_buffer->committing));
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002950
Steven Rostedta1863c22009-09-03 10:23:58 -04002951 rb_decrement_entry(cpu_buffer, event);
Steven Rostedt0f2541d2009-08-05 12:02:48 -04002952 if (rb_try_to_discard(cpu_buffer, event))
Steven Rostedtedd813bf2009-06-02 23:00:53 -04002953 goto out;
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002954
2955 /*
2956 * The commit is still visible by the reader, so we
Steven Rostedta1863c22009-09-03 10:23:58 -04002957 * must still update the timestamp.
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002958 */
Steven Rostedta1863c22009-09-03 10:23:58 -04002959 rb_update_write_stamp(cpu_buffer, event);
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002960 out:
Steven Rostedtfa743952009-06-16 12:37:57 -04002961 rb_end_commit(cpu_buffer);
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002962
Steven Rostedt (Red Hat)58a09ec2015-05-27 10:27:47 -04002963 trace_recursive_unlock(cpu_buffer);
Frederic Weisbeckerf3b9aae2009-04-19 23:39:33 +02002964
Steven Rostedt5168ae52010-06-03 09:36:50 -04002965 preempt_enable_notrace();
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002966
2967}
2968EXPORT_SYMBOL_GPL(ring_buffer_discard_commit);
2969
2970/**
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002971 * ring_buffer_write - write data to the buffer without reserving
2972 * @buffer: The ring buffer to write to.
2973 * @length: The length of the data being written (excluding the event header)
2974 * @data: The data to write to the buffer.
2975 *
2976 * This is like ring_buffer_lock_reserve and ring_buffer_unlock_commit as
2977 * one function. If you already have the data to write to the buffer, it
2978 * may be easier to simply call this function.
2979 *
2980 * Note, like ring_buffer_lock_reserve, the length is the length of the data
2981 * and not the length of the event which would hold the header.
2982 */
2983int ring_buffer_write(struct ring_buffer *buffer,
David Sharp01e3e712012-06-07 16:46:24 -07002984 unsigned long length,
2985 void *data)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002986{
2987 struct ring_buffer_per_cpu *cpu_buffer;
2988 struct ring_buffer_event *event;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002989 void *body;
2990 int ret = -EBUSY;
Steven Rostedt5168ae52010-06-03 09:36:50 -04002991 int cpu;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002992
Steven Rostedt5168ae52010-06-03 09:36:50 -04002993 preempt_disable_notrace();
Steven Rostedtbf41a152008-10-04 02:00:59 -04002994
Lai Jiangshan52fbe9c2010-03-08 14:50:43 +08002995 if (atomic_read(&buffer->record_disabled))
2996 goto out;
2997
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002998 cpu = raw_smp_processor_id();
2999
Rusty Russell9e01c1b2009-01-01 10:12:22 +10303000 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedtd7690412008-10-01 00:29:53 -04003001 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003002
3003 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003004
3005 if (atomic_read(&cpu_buffer->record_disabled))
3006 goto out;
3007
Steven Rostedtbe957c42009-05-11 14:42:53 -04003008 if (length > BUF_MAX_DATA_SIZE)
3009 goto out;
3010
Steven Rostedt (Red Hat)985e8712015-05-27 10:48:56 -04003011 if (unlikely(trace_recursive_lock(cpu_buffer)))
3012 goto out;
3013
Steven Rostedt62f0b3e2009-09-04 14:11:34 -04003014 event = rb_reserve_next_event(buffer, cpu_buffer, length);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003015 if (!event)
Steven Rostedt (Red Hat)985e8712015-05-27 10:48:56 -04003016 goto out_unlock;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003017
3018 body = rb_event_data(event);
3019
3020 memcpy(body, data, length);
3021
3022 rb_commit(cpu_buffer, event);
3023
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -05003024 rb_wakeups(buffer, cpu_buffer);
3025
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003026 ret = 0;
Steven Rostedt (Red Hat)985e8712015-05-27 10:48:56 -04003027
3028 out_unlock:
3029 trace_recursive_unlock(cpu_buffer);
3030
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003031 out:
Steven Rostedt5168ae52010-06-03 09:36:50 -04003032 preempt_enable_notrace();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003033
3034 return ret;
3035}
Robert Richterc4f50182008-12-11 16:49:22 +01003036EXPORT_SYMBOL_GPL(ring_buffer_write);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003037
Yaowei Baida588342015-09-29 22:43:33 +08003038static bool rb_per_cpu_empty(struct ring_buffer_per_cpu *cpu_buffer)
Steven Rostedtbf41a152008-10-04 02:00:59 -04003039{
3040 struct buffer_page *reader = cpu_buffer->reader_page;
Steven Rostedt77ae3652009-03-27 11:00:29 -04003041 struct buffer_page *head = rb_set_head_page(cpu_buffer);
Steven Rostedtbf41a152008-10-04 02:00:59 -04003042 struct buffer_page *commit = cpu_buffer->commit_page;
3043
Steven Rostedt77ae3652009-03-27 11:00:29 -04003044 /* In case of error, head will be NULL */
3045 if (unlikely(!head))
Yaowei Baida588342015-09-29 22:43:33 +08003046 return true;
Steven Rostedt77ae3652009-03-27 11:00:29 -04003047
Steven Rostedtbf41a152008-10-04 02:00:59 -04003048 return reader->read == rb_page_commit(reader) &&
3049 (commit == reader ||
3050 (commit == head &&
3051 head->read == rb_page_commit(commit)));
3052}
3053
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003054/**
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003055 * ring_buffer_record_disable - stop all writes into the buffer
3056 * @buffer: The ring buffer to stop writes to.
3057 *
3058 * This prevents all writes to the buffer. Any attempt to write
3059 * to the buffer after this will fail and return NULL.
3060 *
3061 * The caller should call synchronize_sched() after this.
3062 */
3063void ring_buffer_record_disable(struct ring_buffer *buffer)
3064{
3065 atomic_inc(&buffer->record_disabled);
3066}
Robert Richterc4f50182008-12-11 16:49:22 +01003067EXPORT_SYMBOL_GPL(ring_buffer_record_disable);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003068
3069/**
3070 * ring_buffer_record_enable - enable writes to the buffer
3071 * @buffer: The ring buffer to enable writes
3072 *
3073 * Note, multiple disables will need the same number of enables
Adam Buchbinderc41b20e2009-12-11 16:35:39 -05003074 * to truly enable the writing (much like preempt_disable).
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003075 */
3076void ring_buffer_record_enable(struct ring_buffer *buffer)
3077{
3078 atomic_dec(&buffer->record_disabled);
3079}
Robert Richterc4f50182008-12-11 16:49:22 +01003080EXPORT_SYMBOL_GPL(ring_buffer_record_enable);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003081
3082/**
Steven Rostedt499e5472012-02-22 15:50:28 -05003083 * ring_buffer_record_off - stop all writes into the buffer
3084 * @buffer: The ring buffer to stop writes to.
3085 *
3086 * This prevents all writes to the buffer. Any attempt to write
3087 * to the buffer after this will fail and return NULL.
3088 *
3089 * This is different than ring_buffer_record_disable() as
Wang Tianhong87abb3b2012-08-02 14:02:00 +08003090 * it works like an on/off switch, where as the disable() version
Steven Rostedt499e5472012-02-22 15:50:28 -05003091 * must be paired with a enable().
3092 */
3093void ring_buffer_record_off(struct ring_buffer *buffer)
3094{
3095 unsigned int rd;
3096 unsigned int new_rd;
3097
3098 do {
3099 rd = atomic_read(&buffer->record_disabled);
3100 new_rd = rd | RB_BUFFER_OFF;
3101 } while (atomic_cmpxchg(&buffer->record_disabled, rd, new_rd) != rd);
3102}
3103EXPORT_SYMBOL_GPL(ring_buffer_record_off);
3104
3105/**
3106 * ring_buffer_record_on - restart writes into the buffer
3107 * @buffer: The ring buffer to start writes to.
3108 *
3109 * This enables all writes to the buffer that was disabled by
3110 * ring_buffer_record_off().
3111 *
3112 * This is different than ring_buffer_record_enable() as
Wang Tianhong87abb3b2012-08-02 14:02:00 +08003113 * it works like an on/off switch, where as the enable() version
Steven Rostedt499e5472012-02-22 15:50:28 -05003114 * must be paired with a disable().
3115 */
3116void ring_buffer_record_on(struct ring_buffer *buffer)
3117{
3118 unsigned int rd;
3119 unsigned int new_rd;
3120
3121 do {
3122 rd = atomic_read(&buffer->record_disabled);
3123 new_rd = rd & ~RB_BUFFER_OFF;
3124 } while (atomic_cmpxchg(&buffer->record_disabled, rd, new_rd) != rd);
3125}
3126EXPORT_SYMBOL_GPL(ring_buffer_record_on);
3127
3128/**
3129 * ring_buffer_record_is_on - return true if the ring buffer can write
3130 * @buffer: The ring buffer to see if write is enabled
3131 *
3132 * Returns true if the ring buffer is in a state that it accepts writes.
3133 */
3134int ring_buffer_record_is_on(struct ring_buffer *buffer)
3135{
3136 return !atomic_read(&buffer->record_disabled);
3137}
3138
3139/**
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003140 * ring_buffer_record_disable_cpu - stop all writes into the cpu_buffer
3141 * @buffer: The ring buffer to stop writes to.
3142 * @cpu: The CPU buffer to stop
3143 *
3144 * This prevents all writes to the buffer. Any attempt to write
3145 * to the buffer after this will fail and return NULL.
3146 *
3147 * The caller should call synchronize_sched() after this.
3148 */
3149void ring_buffer_record_disable_cpu(struct ring_buffer *buffer, int cpu)
3150{
3151 struct ring_buffer_per_cpu *cpu_buffer;
3152
Rusty Russell9e01c1b2009-01-01 10:12:22 +10303153 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04003154 return;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003155
3156 cpu_buffer = buffer->buffers[cpu];
3157 atomic_inc(&cpu_buffer->record_disabled);
3158}
Robert Richterc4f50182008-12-11 16:49:22 +01003159EXPORT_SYMBOL_GPL(ring_buffer_record_disable_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003160
3161/**
3162 * ring_buffer_record_enable_cpu - enable writes to the buffer
3163 * @buffer: The ring buffer to enable writes
3164 * @cpu: The CPU to enable.
3165 *
3166 * Note, multiple disables will need the same number of enables
Adam Buchbinderc41b20e2009-12-11 16:35:39 -05003167 * to truly enable the writing (much like preempt_disable).
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003168 */
3169void ring_buffer_record_enable_cpu(struct ring_buffer *buffer, int cpu)
3170{
3171 struct ring_buffer_per_cpu *cpu_buffer;
3172
Rusty Russell9e01c1b2009-01-01 10:12:22 +10303173 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04003174 return;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003175
3176 cpu_buffer = buffer->buffers[cpu];
3177 atomic_dec(&cpu_buffer->record_disabled);
3178}
Robert Richterc4f50182008-12-11 16:49:22 +01003179EXPORT_SYMBOL_GPL(ring_buffer_record_enable_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003180
Steven Rostedtf6195aa2010-09-01 12:23:12 -04003181/*
3182 * The total entries in the ring buffer is the running counter
3183 * of entries entered into the ring buffer, minus the sum of
3184 * the entries read from the ring buffer and the number of
3185 * entries that were overwritten.
3186 */
3187static inline unsigned long
3188rb_num_of_entries(struct ring_buffer_per_cpu *cpu_buffer)
3189{
3190 return local_read(&cpu_buffer->entries) -
3191 (local_read(&cpu_buffer->overrun) + cpu_buffer->read);
3192}
3193
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003194/**
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -07003195 * ring_buffer_oldest_event_ts - get the oldest event timestamp from the buffer
3196 * @buffer: The ring buffer
3197 * @cpu: The per CPU buffer to read from.
3198 */
Yoshihiro YUNOMAE50ecf2c2012-10-11 16:27:54 -07003199u64 ring_buffer_oldest_event_ts(struct ring_buffer *buffer, int cpu)
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -07003200{
3201 unsigned long flags;
3202 struct ring_buffer_per_cpu *cpu_buffer;
3203 struct buffer_page *bpage;
Linus Torvaldsda830e52012-12-11 18:18:58 -08003204 u64 ret = 0;
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -07003205
3206 if (!cpumask_test_cpu(cpu, buffer->cpumask))
3207 return 0;
3208
3209 cpu_buffer = buffer->buffers[cpu];
Linus Torvalds7115e3f2011-10-26 17:03:38 +02003210 raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -07003211 /*
3212 * if the tail is on reader_page, oldest time stamp is on the reader
3213 * page
3214 */
3215 if (cpu_buffer->tail_page == cpu_buffer->reader_page)
3216 bpage = cpu_buffer->reader_page;
3217 else
3218 bpage = rb_set_head_page(cpu_buffer);
Steven Rostedt54f7be52012-11-29 22:27:22 -05003219 if (bpage)
3220 ret = bpage->page->time_stamp;
Linus Torvalds7115e3f2011-10-26 17:03:38 +02003221 raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -07003222
3223 return ret;
3224}
3225EXPORT_SYMBOL_GPL(ring_buffer_oldest_event_ts);
3226
3227/**
3228 * ring_buffer_bytes_cpu - get the number of bytes consumed in a cpu buffer
3229 * @buffer: The ring buffer
3230 * @cpu: The per CPU buffer to read from.
3231 */
3232unsigned long ring_buffer_bytes_cpu(struct ring_buffer *buffer, int cpu)
3233{
3234 struct ring_buffer_per_cpu *cpu_buffer;
3235 unsigned long ret;
3236
3237 if (!cpumask_test_cpu(cpu, buffer->cpumask))
3238 return 0;
3239
3240 cpu_buffer = buffer->buffers[cpu];
3241 ret = local_read(&cpu_buffer->entries_bytes) - cpu_buffer->read_bytes;
3242
3243 return ret;
3244}
3245EXPORT_SYMBOL_GPL(ring_buffer_bytes_cpu);
3246
3247/**
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003248 * ring_buffer_entries_cpu - get the number of entries in a cpu buffer
3249 * @buffer: The ring buffer
3250 * @cpu: The per CPU buffer to get the entries from.
3251 */
3252unsigned long ring_buffer_entries_cpu(struct ring_buffer *buffer, int cpu)
3253{
3254 struct ring_buffer_per_cpu *cpu_buffer;
3255
Rusty Russell9e01c1b2009-01-01 10:12:22 +10303256 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04003257 return 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003258
3259 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt554f7862009-03-11 22:00:13 -04003260
Steven Rostedtf6195aa2010-09-01 12:23:12 -04003261 return rb_num_of_entries(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003262}
Robert Richterc4f50182008-12-11 16:49:22 +01003263EXPORT_SYMBOL_GPL(ring_buffer_entries_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003264
3265/**
Slava Pestov884bfe82011-07-15 14:23:58 -07003266 * ring_buffer_overrun_cpu - get the number of overruns caused by the ring
3267 * buffer wrapping around (only if RB_FL_OVERWRITE is on).
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003268 * @buffer: The ring buffer
3269 * @cpu: The per CPU buffer to get the number of overruns from
3270 */
3271unsigned long ring_buffer_overrun_cpu(struct ring_buffer *buffer, int cpu)
3272{
3273 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedt8aabee52009-03-12 13:13:49 -04003274 unsigned long ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003275
Rusty Russell9e01c1b2009-01-01 10:12:22 +10303276 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04003277 return 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003278
3279 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt77ae3652009-03-27 11:00:29 -04003280 ret = local_read(&cpu_buffer->overrun);
Steven Rostedt554f7862009-03-11 22:00:13 -04003281
3282 return ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003283}
Robert Richterc4f50182008-12-11 16:49:22 +01003284EXPORT_SYMBOL_GPL(ring_buffer_overrun_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003285
3286/**
Slava Pestov884bfe82011-07-15 14:23:58 -07003287 * ring_buffer_commit_overrun_cpu - get the number of overruns caused by
3288 * commits failing due to the buffer wrapping around while there are uncommitted
3289 * events, such as during an interrupt storm.
Steven Rostedtf0d2c682009-04-29 13:43:37 -04003290 * @buffer: The ring buffer
3291 * @cpu: The per CPU buffer to get the number of overruns from
3292 */
3293unsigned long
3294ring_buffer_commit_overrun_cpu(struct ring_buffer *buffer, int cpu)
3295{
3296 struct ring_buffer_per_cpu *cpu_buffer;
3297 unsigned long ret;
3298
3299 if (!cpumask_test_cpu(cpu, buffer->cpumask))
3300 return 0;
3301
3302 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt77ae3652009-03-27 11:00:29 -04003303 ret = local_read(&cpu_buffer->commit_overrun);
Steven Rostedtf0d2c682009-04-29 13:43:37 -04003304
3305 return ret;
3306}
3307EXPORT_SYMBOL_GPL(ring_buffer_commit_overrun_cpu);
3308
3309/**
Slava Pestov884bfe82011-07-15 14:23:58 -07003310 * ring_buffer_dropped_events_cpu - get the number of dropped events caused by
3311 * the ring buffer filling up (only if RB_FL_OVERWRITE is off).
3312 * @buffer: The ring buffer
3313 * @cpu: The per CPU buffer to get the number of overruns from
3314 */
3315unsigned long
3316ring_buffer_dropped_events_cpu(struct ring_buffer *buffer, int cpu)
3317{
3318 struct ring_buffer_per_cpu *cpu_buffer;
3319 unsigned long ret;
3320
3321 if (!cpumask_test_cpu(cpu, buffer->cpumask))
3322 return 0;
3323
3324 cpu_buffer = buffer->buffers[cpu];
3325 ret = local_read(&cpu_buffer->dropped_events);
3326
3327 return ret;
3328}
3329EXPORT_SYMBOL_GPL(ring_buffer_dropped_events_cpu);
3330
3331/**
Steven Rostedt (Red Hat)ad964702013-01-29 17:45:49 -05003332 * ring_buffer_read_events_cpu - get the number of events successfully read
3333 * @buffer: The ring buffer
3334 * @cpu: The per CPU buffer to get the number of events read
3335 */
3336unsigned long
3337ring_buffer_read_events_cpu(struct ring_buffer *buffer, int cpu)
3338{
3339 struct ring_buffer_per_cpu *cpu_buffer;
3340
3341 if (!cpumask_test_cpu(cpu, buffer->cpumask))
3342 return 0;
3343
3344 cpu_buffer = buffer->buffers[cpu];
3345 return cpu_buffer->read;
3346}
3347EXPORT_SYMBOL_GPL(ring_buffer_read_events_cpu);
3348
3349/**
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003350 * ring_buffer_entries - get the number of entries in a buffer
3351 * @buffer: The ring buffer
3352 *
3353 * Returns the total number of entries in the ring buffer
3354 * (all CPU entries)
3355 */
3356unsigned long ring_buffer_entries(struct ring_buffer *buffer)
3357{
3358 struct ring_buffer_per_cpu *cpu_buffer;
3359 unsigned long entries = 0;
3360 int cpu;
3361
3362 /* if you care about this being correct, lock the buffer */
3363 for_each_buffer_cpu(buffer, cpu) {
3364 cpu_buffer = buffer->buffers[cpu];
Steven Rostedtf6195aa2010-09-01 12:23:12 -04003365 entries += rb_num_of_entries(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003366 }
3367
3368 return entries;
3369}
Robert Richterc4f50182008-12-11 16:49:22 +01003370EXPORT_SYMBOL_GPL(ring_buffer_entries);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003371
3372/**
Jiri Olsa67b394f2009-10-23 19:36:18 -04003373 * ring_buffer_overruns - get the number of overruns in buffer
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003374 * @buffer: The ring buffer
3375 *
3376 * Returns the total number of overruns in the ring buffer
3377 * (all CPU entries)
3378 */
3379unsigned long ring_buffer_overruns(struct ring_buffer *buffer)
3380{
3381 struct ring_buffer_per_cpu *cpu_buffer;
3382 unsigned long overruns = 0;
3383 int cpu;
3384
3385 /* if you care about this being correct, lock the buffer */
3386 for_each_buffer_cpu(buffer, cpu) {
3387 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt77ae3652009-03-27 11:00:29 -04003388 overruns += local_read(&cpu_buffer->overrun);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003389 }
3390
3391 return overruns;
3392}
Robert Richterc4f50182008-12-11 16:49:22 +01003393EXPORT_SYMBOL_GPL(ring_buffer_overruns);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003394
Steven Rostedt642edba2008-11-12 00:01:26 -05003395static void rb_iter_reset(struct ring_buffer_iter *iter)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003396{
3397 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
3398
Steven Rostedtd7690412008-10-01 00:29:53 -04003399 /* Iterator usage is expected to have record disabled */
Steven Rostedt (Red Hat)651e22f2014-08-06 14:11:33 -04003400 iter->head_page = cpu_buffer->reader_page;
3401 iter->head = cpu_buffer->reader_page->read;
3402
3403 iter->cache_reader_page = iter->head_page;
Steven Rostedt (Red Hat)24607f12014-10-02 16:51:18 -04003404 iter->cache_read = cpu_buffer->read;
Steven Rostedt (Red Hat)651e22f2014-08-06 14:11:33 -04003405
Steven Rostedtd7690412008-10-01 00:29:53 -04003406 if (iter->head)
3407 iter->read_stamp = cpu_buffer->read_stamp;
3408 else
Steven Rostedtabc9b562008-12-02 15:34:06 -05003409 iter->read_stamp = iter->head_page->page->time_stamp;
Steven Rostedt642edba2008-11-12 00:01:26 -05003410}
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003411
Steven Rostedt642edba2008-11-12 00:01:26 -05003412/**
3413 * ring_buffer_iter_reset - reset an iterator
3414 * @iter: The iterator to reset
3415 *
3416 * Resets the iterator, so that it will start from the beginning
3417 * again.
3418 */
3419void ring_buffer_iter_reset(struct ring_buffer_iter *iter)
3420{
Steven Rostedt554f7862009-03-11 22:00:13 -04003421 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedt642edba2008-11-12 00:01:26 -05003422 unsigned long flags;
3423
Steven Rostedt554f7862009-03-11 22:00:13 -04003424 if (!iter)
3425 return;
3426
3427 cpu_buffer = iter->cpu_buffer;
3428
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02003429 raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
Steven Rostedt642edba2008-11-12 00:01:26 -05003430 rb_iter_reset(iter);
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02003431 raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003432}
Robert Richterc4f50182008-12-11 16:49:22 +01003433EXPORT_SYMBOL_GPL(ring_buffer_iter_reset);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003434
3435/**
3436 * ring_buffer_iter_empty - check if an iterator has no more to read
3437 * @iter: The iterator to check
3438 */
3439int ring_buffer_iter_empty(struct ring_buffer_iter *iter)
3440{
3441 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedt (VMware)d80e90712017-04-19 14:29:46 -04003442 struct buffer_page *reader;
3443 struct buffer_page *head_page;
3444 struct buffer_page *commit_page;
3445 unsigned commit;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003446
3447 cpu_buffer = iter->cpu_buffer;
3448
Steven Rostedt (VMware)d80e90712017-04-19 14:29:46 -04003449 /* Remember, trace recording is off when iterator is in use */
3450 reader = cpu_buffer->reader_page;
3451 head_page = cpu_buffer->head_page;
3452 commit_page = cpu_buffer->commit_page;
3453 commit = rb_page_commit(commit_page);
3454
3455 return ((iter->head_page == commit_page && iter->head == commit) ||
3456 (iter->head_page == reader && commit_page == head_page &&
3457 head_page->read == commit &&
3458 iter->head == rb_page_commit(cpu_buffer->reader_page)));
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003459}
Robert Richterc4f50182008-12-11 16:49:22 +01003460EXPORT_SYMBOL_GPL(ring_buffer_iter_empty);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003461
3462static void
3463rb_update_read_stamp(struct ring_buffer_per_cpu *cpu_buffer,
3464 struct ring_buffer_event *event)
3465{
3466 u64 delta;
3467
Lai Jiangshan334d4162009-04-24 11:27:05 +08003468 switch (event->type_len) {
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003469 case RINGBUF_TYPE_PADDING:
3470 return;
3471
3472 case RINGBUF_TYPE_TIME_EXTEND:
3473 delta = event->array[0];
3474 delta <<= TS_SHIFT;
3475 delta += event->time_delta;
3476 cpu_buffer->read_stamp += delta;
3477 return;
3478
3479 case RINGBUF_TYPE_TIME_STAMP:
3480 /* FIXME: not implemented */
3481 return;
3482
3483 case RINGBUF_TYPE_DATA:
3484 cpu_buffer->read_stamp += event->time_delta;
3485 return;
3486
3487 default:
3488 BUG();
3489 }
3490 return;
3491}
3492
3493static void
3494rb_update_iter_read_stamp(struct ring_buffer_iter *iter,
3495 struct ring_buffer_event *event)
3496{
3497 u64 delta;
3498
Lai Jiangshan334d4162009-04-24 11:27:05 +08003499 switch (event->type_len) {
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003500 case RINGBUF_TYPE_PADDING:
3501 return;
3502
3503 case RINGBUF_TYPE_TIME_EXTEND:
3504 delta = event->array[0];
3505 delta <<= TS_SHIFT;
3506 delta += event->time_delta;
3507 iter->read_stamp += delta;
3508 return;
3509
3510 case RINGBUF_TYPE_TIME_STAMP:
3511 /* FIXME: not implemented */
3512 return;
3513
3514 case RINGBUF_TYPE_DATA:
3515 iter->read_stamp += event->time_delta;
3516 return;
3517
3518 default:
3519 BUG();
3520 }
3521 return;
3522}
3523
Steven Rostedtd7690412008-10-01 00:29:53 -04003524static struct buffer_page *
3525rb_get_reader_page(struct ring_buffer_per_cpu *cpu_buffer)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003526{
Steven Rostedtd7690412008-10-01 00:29:53 -04003527 struct buffer_page *reader = NULL;
Steven Rostedt66a8cb92010-03-31 13:21:56 -04003528 unsigned long overwrite;
Steven Rostedtd7690412008-10-01 00:29:53 -04003529 unsigned long flags;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003530 int nr_loops = 0;
Steven Rostedt77ae3652009-03-27 11:00:29 -04003531 int ret;
Steven Rostedtd7690412008-10-01 00:29:53 -04003532
Steven Rostedt3e03fb72008-11-06 00:09:43 -05003533 local_irq_save(flags);
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01003534 arch_spin_lock(&cpu_buffer->lock);
Steven Rostedtd7690412008-10-01 00:29:53 -04003535
3536 again:
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003537 /*
3538 * This should normally only loop twice. But because the
3539 * start of the reader inserts an empty page, it causes
3540 * a case where we will loop three times. There should be no
3541 * reason to loop four times (that I know of).
3542 */
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05003543 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 3)) {
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003544 reader = NULL;
3545 goto out;
3546 }
3547
Steven Rostedtd7690412008-10-01 00:29:53 -04003548 reader = cpu_buffer->reader_page;
3549
3550 /* If there's more to read, return this page */
Steven Rostedtbf41a152008-10-04 02:00:59 -04003551 if (cpu_buffer->reader_page->read < rb_page_size(reader))
Steven Rostedtd7690412008-10-01 00:29:53 -04003552 goto out;
3553
3554 /* Never should we have an index greater than the size */
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05003555 if (RB_WARN_ON(cpu_buffer,
3556 cpu_buffer->reader_page->read > rb_page_size(reader)))
3557 goto out;
Steven Rostedtd7690412008-10-01 00:29:53 -04003558
3559 /* check if we caught up to the tail */
3560 reader = NULL;
Steven Rostedtbf41a152008-10-04 02:00:59 -04003561 if (cpu_buffer->commit_page == cpu_buffer->reader_page)
Steven Rostedtd7690412008-10-01 00:29:53 -04003562 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003563
Steven Rostedta5fb8332012-06-28 13:35:04 -04003564 /* Don't bother swapping if the ring buffer is empty */
3565 if (rb_num_of_entries(cpu_buffer) == 0)
3566 goto out;
3567
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003568 /*
Steven Rostedtd7690412008-10-01 00:29:53 -04003569 * Reset the reader page to size zero.
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003570 */
Steven Rostedt77ae3652009-03-27 11:00:29 -04003571 local_set(&cpu_buffer->reader_page->write, 0);
3572 local_set(&cpu_buffer->reader_page->entries, 0);
3573 local_set(&cpu_buffer->reader_page->page->commit, 0);
Steven Rostedtff0ff842010-03-31 22:11:42 -04003574 cpu_buffer->reader_page->real_end = 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003575
Steven Rostedt77ae3652009-03-27 11:00:29 -04003576 spin:
3577 /*
3578 * Splice the empty reader page into the list around the head.
3579 */
3580 reader = rb_set_head_page(cpu_buffer);
Steven Rostedt54f7be52012-11-29 22:27:22 -05003581 if (!reader)
3582 goto out;
Steven Rostedt0e1ff5d2010-01-06 20:40:44 -05003583 cpu_buffer->reader_page->list.next = rb_list_head(reader->list.next);
Steven Rostedtd7690412008-10-01 00:29:53 -04003584 cpu_buffer->reader_page->list.prev = reader->list.prev;
Steven Rostedtbf41a152008-10-04 02:00:59 -04003585
Steven Rostedt3adc54f2009-03-30 15:32:01 -04003586 /*
3587 * cpu_buffer->pages just needs to point to the buffer, it
3588 * has no specific buffer page to point to. Lets move it out
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003589 * of our way so we don't accidentally swap it.
Steven Rostedt3adc54f2009-03-30 15:32:01 -04003590 */
3591 cpu_buffer->pages = reader->list.prev;
3592
Steven Rostedt77ae3652009-03-27 11:00:29 -04003593 /* The reader page will be pointing to the new head */
3594 rb_set_list_to_head(cpu_buffer, &cpu_buffer->reader_page->list);
Steven Rostedtd7690412008-10-01 00:29:53 -04003595
3596 /*
Steven Rostedt66a8cb92010-03-31 13:21:56 -04003597 * We want to make sure we read the overruns after we set up our
3598 * pointers to the next object. The writer side does a
3599 * cmpxchg to cross pages which acts as the mb on the writer
3600 * side. Note, the reader will constantly fail the swap
3601 * while the writer is updating the pointers, so this
3602 * guarantees that the overwrite recorded here is the one we
3603 * want to compare with the last_overrun.
3604 */
3605 smp_mb();
3606 overwrite = local_read(&(cpu_buffer->overrun));
3607
3608 /*
Steven Rostedt77ae3652009-03-27 11:00:29 -04003609 * Here's the tricky part.
3610 *
3611 * We need to move the pointer past the header page.
3612 * But we can only do that if a writer is not currently
3613 * moving it. The page before the header page has the
3614 * flag bit '1' set if it is pointing to the page we want.
3615 * but if the writer is in the process of moving it
3616 * than it will be '2' or already moved '0'.
Steven Rostedtd7690412008-10-01 00:29:53 -04003617 */
Steven Rostedtd7690412008-10-01 00:29:53 -04003618
Steven Rostedt77ae3652009-03-27 11:00:29 -04003619 ret = rb_head_page_replace(reader, cpu_buffer->reader_page);
3620
3621 /*
3622 * If we did not convert it, then we must try again.
3623 */
3624 if (!ret)
3625 goto spin;
3626
3627 /*
3628 * Yeah! We succeeded in replacing the page.
3629 *
3630 * Now make the new head point back to the reader page.
3631 */
David Sharp5ded3dc62010-01-06 17:12:07 -08003632 rb_list_head(reader->list.next)->prev = &cpu_buffer->reader_page->list;
Steven Rostedt77ae3652009-03-27 11:00:29 -04003633 rb_inc_page(cpu_buffer, &cpu_buffer->head_page);
Steven Rostedtd7690412008-10-01 00:29:53 -04003634
3635 /* Finally update the reader page to the new head */
3636 cpu_buffer->reader_page = reader;
Steven Rostedt (Red Hat)b81f4722015-11-23 10:35:36 -05003637 cpu_buffer->reader_page->read = 0;
Steven Rostedtd7690412008-10-01 00:29:53 -04003638
Steven Rostedt66a8cb92010-03-31 13:21:56 -04003639 if (overwrite != cpu_buffer->last_overrun) {
3640 cpu_buffer->lost_events = overwrite - cpu_buffer->last_overrun;
3641 cpu_buffer->last_overrun = overwrite;
3642 }
3643
Steven Rostedtd7690412008-10-01 00:29:53 -04003644 goto again;
3645
3646 out:
Steven Rostedt (Red Hat)b81f4722015-11-23 10:35:36 -05003647 /* Update the read_stamp on the first event */
3648 if (reader && reader->read == 0)
3649 cpu_buffer->read_stamp = reader->page->time_stamp;
3650
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01003651 arch_spin_unlock(&cpu_buffer->lock);
Steven Rostedt3e03fb72008-11-06 00:09:43 -05003652 local_irq_restore(flags);
Steven Rostedtd7690412008-10-01 00:29:53 -04003653
3654 return reader;
3655}
3656
3657static void rb_advance_reader(struct ring_buffer_per_cpu *cpu_buffer)
3658{
3659 struct ring_buffer_event *event;
3660 struct buffer_page *reader;
3661 unsigned length;
3662
3663 reader = rb_get_reader_page(cpu_buffer);
3664
3665 /* This function should not be called when buffer is empty */
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05003666 if (RB_WARN_ON(cpu_buffer, !reader))
3667 return;
Steven Rostedtd7690412008-10-01 00:29:53 -04003668
3669 event = rb_reader_event(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003670
Steven Rostedta1863c22009-09-03 10:23:58 -04003671 if (event->type_len <= RINGBUF_TYPE_DATA_TYPE_LEN_MAX)
Steven Rostedte4906ef2009-04-30 20:49:44 -04003672 cpu_buffer->read++;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003673
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003674 rb_update_read_stamp(cpu_buffer, event);
3675
Steven Rostedtd7690412008-10-01 00:29:53 -04003676 length = rb_event_length(event);
Steven Rostedt6f807ac2008-10-04 02:00:58 -04003677 cpu_buffer->reader_page->read += length;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003678}
3679
3680static void rb_advance_iter(struct ring_buffer_iter *iter)
3681{
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003682 struct ring_buffer_per_cpu *cpu_buffer;
3683 struct ring_buffer_event *event;
3684 unsigned length;
3685
3686 cpu_buffer = iter->cpu_buffer;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003687
3688 /*
3689 * Check if we are at the end of the buffer.
3690 */
Steven Rostedtbf41a152008-10-04 02:00:59 -04003691 if (iter->head >= rb_page_size(iter->head_page)) {
Steven Rostedtea05b572009-06-03 09:30:10 -04003692 /* discarded commits can make the page empty */
3693 if (iter->head_page == cpu_buffer->commit_page)
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05003694 return;
Steven Rostedtd7690412008-10-01 00:29:53 -04003695 rb_inc_iter(iter);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003696 return;
3697 }
3698
3699 event = rb_iter_head_event(iter);
3700
3701 length = rb_event_length(event);
3702
3703 /*
3704 * This should not be called to advance the header if we are
3705 * at the tail of the buffer.
3706 */
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05003707 if (RB_WARN_ON(cpu_buffer,
Steven Rostedtf536aaf2008-11-10 23:07:30 -05003708 (iter->head_page == cpu_buffer->commit_page) &&
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05003709 (iter->head + length > rb_commit_index(cpu_buffer))))
3710 return;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003711
3712 rb_update_iter_read_stamp(iter, event);
3713
3714 iter->head += length;
3715
3716 /* check for end of page padding */
Steven Rostedtbf41a152008-10-04 02:00:59 -04003717 if ((iter->head >= rb_page_size(iter->head_page)) &&
3718 (iter->head_page != cpu_buffer->commit_page))
Steven Rostedt771e0382012-11-30 10:41:57 -05003719 rb_inc_iter(iter);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003720}
3721
Steven Rostedt66a8cb92010-03-31 13:21:56 -04003722static int rb_lost_events(struct ring_buffer_per_cpu *cpu_buffer)
3723{
3724 return cpu_buffer->lost_events;
3725}
3726
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003727static struct ring_buffer_event *
Steven Rostedt66a8cb92010-03-31 13:21:56 -04003728rb_buffer_peek(struct ring_buffer_per_cpu *cpu_buffer, u64 *ts,
3729 unsigned long *lost_events)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003730{
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003731 struct ring_buffer_event *event;
Steven Rostedtd7690412008-10-01 00:29:53 -04003732 struct buffer_page *reader;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003733 int nr_loops = 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003734
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003735 again:
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003736 /*
Steven Rostedt69d1b832010-10-07 18:18:05 -04003737 * We repeat when a time extend is encountered.
3738 * Since the time extend is always attached to a data event,
3739 * we should never loop more than once.
3740 * (We never hit the following condition more than twice).
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003741 */
Steven Rostedt69d1b832010-10-07 18:18:05 -04003742 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 2))
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003743 return NULL;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003744
Steven Rostedtd7690412008-10-01 00:29:53 -04003745 reader = rb_get_reader_page(cpu_buffer);
3746 if (!reader)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003747 return NULL;
3748
Steven Rostedtd7690412008-10-01 00:29:53 -04003749 event = rb_reader_event(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003750
Lai Jiangshan334d4162009-04-24 11:27:05 +08003751 switch (event->type_len) {
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003752 case RINGBUF_TYPE_PADDING:
Tom Zanussi2d622712009-03-22 03:30:49 -05003753 if (rb_null_event(event))
3754 RB_WARN_ON(cpu_buffer, 1);
3755 /*
3756 * Because the writer could be discarding every
3757 * event it creates (which would probably be bad)
3758 * if we were to go back to "again" then we may never
3759 * catch up, and will trigger the warn on, or lock
3760 * the box. Return the padding, and we will release
3761 * the current locks, and try again.
3762 */
Tom Zanussi2d622712009-03-22 03:30:49 -05003763 return event;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003764
3765 case RINGBUF_TYPE_TIME_EXTEND:
3766 /* Internal data, OK to advance */
Steven Rostedtd7690412008-10-01 00:29:53 -04003767 rb_advance_reader(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003768 goto again;
3769
3770 case RINGBUF_TYPE_TIME_STAMP:
3771 /* FIXME: not implemented */
Steven Rostedtd7690412008-10-01 00:29:53 -04003772 rb_advance_reader(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003773 goto again;
3774
3775 case RINGBUF_TYPE_DATA:
3776 if (ts) {
3777 *ts = cpu_buffer->read_stamp + event->time_delta;
Robert Richterd8eeb2d2009-07-31 14:58:04 +02003778 ring_buffer_normalize_time_stamp(cpu_buffer->buffer,
Steven Rostedt37886f62009-03-17 17:22:06 -04003779 cpu_buffer->cpu, ts);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003780 }
Steven Rostedt66a8cb92010-03-31 13:21:56 -04003781 if (lost_events)
3782 *lost_events = rb_lost_events(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003783 return event;
3784
3785 default:
3786 BUG();
3787 }
3788
3789 return NULL;
3790}
Robert Richterc4f50182008-12-11 16:49:22 +01003791EXPORT_SYMBOL_GPL(ring_buffer_peek);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003792
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003793static struct ring_buffer_event *
3794rb_iter_peek(struct ring_buffer_iter *iter, u64 *ts)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003795{
3796 struct ring_buffer *buffer;
3797 struct ring_buffer_per_cpu *cpu_buffer;
3798 struct ring_buffer_event *event;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003799 int nr_loops = 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003800
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003801 cpu_buffer = iter->cpu_buffer;
3802 buffer = cpu_buffer->buffer;
3803
Steven Rostedt492a74f2010-01-25 15:17:47 -05003804 /*
3805 * Check if someone performed a consuming read to
3806 * the buffer. A consuming read invalidates the iterator
3807 * and we need to reset the iterator in this case.
3808 */
3809 if (unlikely(iter->cache_read != cpu_buffer->read ||
3810 iter->cache_reader_page != cpu_buffer->reader_page))
3811 rb_iter_reset(iter);
3812
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003813 again:
Steven Rostedt3c05d742010-01-26 16:14:08 -05003814 if (ring_buffer_iter_empty(iter))
3815 return NULL;
3816
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003817 /*
Steven Rostedt (Red Hat)021de3d2014-08-06 15:36:31 -04003818 * We repeat when a time extend is encountered or we hit
3819 * the end of the page. Since the time extend is always attached
3820 * to a data event, we should never loop more than three times.
3821 * Once for going to next page, once on time extend, and
3822 * finally once to get the event.
3823 * (We never hit the following condition more than thrice).
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003824 */
Steven Rostedt (Red Hat)021de3d2014-08-06 15:36:31 -04003825 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 3))
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003826 return NULL;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003827
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003828 if (rb_per_cpu_empty(cpu_buffer))
3829 return NULL;
3830
Steven Rostedt (Red Hat)10e83fd2014-07-23 19:45:12 -04003831 if (iter->head >= rb_page_size(iter->head_page)) {
Steven Rostedt3c05d742010-01-26 16:14:08 -05003832 rb_inc_iter(iter);
3833 goto again;
3834 }
3835
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003836 event = rb_iter_head_event(iter);
3837
Lai Jiangshan334d4162009-04-24 11:27:05 +08003838 switch (event->type_len) {
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003839 case RINGBUF_TYPE_PADDING:
Tom Zanussi2d622712009-03-22 03:30:49 -05003840 if (rb_null_event(event)) {
3841 rb_inc_iter(iter);
3842 goto again;
3843 }
3844 rb_advance_iter(iter);
3845 return event;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003846
3847 case RINGBUF_TYPE_TIME_EXTEND:
3848 /* Internal data, OK to advance */
3849 rb_advance_iter(iter);
3850 goto again;
3851
3852 case RINGBUF_TYPE_TIME_STAMP:
3853 /* FIXME: not implemented */
3854 rb_advance_iter(iter);
3855 goto again;
3856
3857 case RINGBUF_TYPE_DATA:
3858 if (ts) {
3859 *ts = iter->read_stamp + event->time_delta;
Steven Rostedt37886f62009-03-17 17:22:06 -04003860 ring_buffer_normalize_time_stamp(buffer,
3861 cpu_buffer->cpu, ts);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003862 }
3863 return event;
3864
3865 default:
3866 BUG();
3867 }
3868
3869 return NULL;
3870}
Robert Richterc4f50182008-12-11 16:49:22 +01003871EXPORT_SYMBOL_GPL(ring_buffer_iter_peek);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003872
Steven Rostedt (Red Hat)289a5a22015-05-28 13:14:51 -04003873static inline bool rb_reader_lock(struct ring_buffer_per_cpu *cpu_buffer)
Steven Rostedt8d707e82009-06-16 21:22:48 -04003874{
Steven Rostedt (Red Hat)289a5a22015-05-28 13:14:51 -04003875 if (likely(!in_nmi())) {
3876 raw_spin_lock(&cpu_buffer->reader_lock);
3877 return true;
3878 }
3879
Steven Rostedt8d707e82009-06-16 21:22:48 -04003880 /*
3881 * If an NMI die dumps out the content of the ring buffer
Steven Rostedt (Red Hat)289a5a22015-05-28 13:14:51 -04003882 * trylock must be used to prevent a deadlock if the NMI
3883 * preempted a task that holds the ring buffer locks. If
3884 * we get the lock then all is fine, if not, then continue
3885 * to do the read, but this can corrupt the ring buffer,
3886 * so it must be permanently disabled from future writes.
3887 * Reading from NMI is a oneshot deal.
Steven Rostedt8d707e82009-06-16 21:22:48 -04003888 */
Steven Rostedt (Red Hat)289a5a22015-05-28 13:14:51 -04003889 if (raw_spin_trylock(&cpu_buffer->reader_lock))
3890 return true;
Steven Rostedt8d707e82009-06-16 21:22:48 -04003891
Steven Rostedt (Red Hat)289a5a22015-05-28 13:14:51 -04003892 /* Continue without locking, but disable the ring buffer */
3893 atomic_inc(&cpu_buffer->record_disabled);
3894 return false;
3895}
3896
3897static inline void
3898rb_reader_unlock(struct ring_buffer_per_cpu *cpu_buffer, bool locked)
3899{
3900 if (likely(locked))
3901 raw_spin_unlock(&cpu_buffer->reader_lock);
3902 return;
Steven Rostedt8d707e82009-06-16 21:22:48 -04003903}
3904
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003905/**
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003906 * ring_buffer_peek - peek at the next event to be read
3907 * @buffer: The ring buffer to read
3908 * @cpu: The cpu to peak at
3909 * @ts: The timestamp counter of this event.
Steven Rostedt66a8cb92010-03-31 13:21:56 -04003910 * @lost_events: a variable to store if events were lost (may be NULL)
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003911 *
3912 * This will return the event that will be read next, but does
3913 * not consume the data.
3914 */
3915struct ring_buffer_event *
Steven Rostedt66a8cb92010-03-31 13:21:56 -04003916ring_buffer_peek(struct ring_buffer *buffer, int cpu, u64 *ts,
3917 unsigned long *lost_events)
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003918{
3919 struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
Steven Rostedt8aabee52009-03-12 13:13:49 -04003920 struct ring_buffer_event *event;
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003921 unsigned long flags;
Steven Rostedt (Red Hat)289a5a22015-05-28 13:14:51 -04003922 bool dolock;
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003923
Steven Rostedt554f7862009-03-11 22:00:13 -04003924 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04003925 return NULL;
Steven Rostedt554f7862009-03-11 22:00:13 -04003926
Tom Zanussi2d622712009-03-22 03:30:49 -05003927 again:
Steven Rostedt8d707e82009-06-16 21:22:48 -04003928 local_irq_save(flags);
Steven Rostedt (Red Hat)289a5a22015-05-28 13:14:51 -04003929 dolock = rb_reader_lock(cpu_buffer);
Steven Rostedt66a8cb92010-03-31 13:21:56 -04003930 event = rb_buffer_peek(cpu_buffer, ts, lost_events);
Robert Richter469535a2009-07-30 19:19:18 +02003931 if (event && event->type_len == RINGBUF_TYPE_PADDING)
3932 rb_advance_reader(cpu_buffer);
Steven Rostedt (Red Hat)289a5a22015-05-28 13:14:51 -04003933 rb_reader_unlock(cpu_buffer, dolock);
Steven Rostedt8d707e82009-06-16 21:22:48 -04003934 local_irq_restore(flags);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003935
Steven Rostedt1b959e12009-09-03 10:12:13 -04003936 if (event && event->type_len == RINGBUF_TYPE_PADDING)
Tom Zanussi2d622712009-03-22 03:30:49 -05003937 goto again;
Tom Zanussi2d622712009-03-22 03:30:49 -05003938
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003939 return event;
3940}
3941
3942/**
3943 * ring_buffer_iter_peek - peek at the next event to be read
3944 * @iter: The ring buffer iterator
3945 * @ts: The timestamp counter of this event.
3946 *
3947 * This will return the event that will be read next, but does
3948 * not increment the iterator.
3949 */
3950struct ring_buffer_event *
3951ring_buffer_iter_peek(struct ring_buffer_iter *iter, u64 *ts)
3952{
3953 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
3954 struct ring_buffer_event *event;
3955 unsigned long flags;
3956
Tom Zanussi2d622712009-03-22 03:30:49 -05003957 again:
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02003958 raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003959 event = rb_iter_peek(iter, ts);
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02003960 raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003961
Steven Rostedt1b959e12009-09-03 10:12:13 -04003962 if (event && event->type_len == RINGBUF_TYPE_PADDING)
Tom Zanussi2d622712009-03-22 03:30:49 -05003963 goto again;
Tom Zanussi2d622712009-03-22 03:30:49 -05003964
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003965 return event;
3966}
3967
3968/**
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003969 * ring_buffer_consume - return an event and consume it
3970 * @buffer: The ring buffer to get the next event from
Steven Rostedt66a8cb92010-03-31 13:21:56 -04003971 * @cpu: the cpu to read the buffer from
3972 * @ts: a variable to store the timestamp (may be NULL)
3973 * @lost_events: a variable to store if events were lost (may be NULL)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003974 *
3975 * Returns the next event in the ring buffer, and that event is consumed.
3976 * Meaning, that sequential reads will keep returning a different event,
3977 * and eventually empty the ring buffer if the producer is slower.
3978 */
3979struct ring_buffer_event *
Steven Rostedt66a8cb92010-03-31 13:21:56 -04003980ring_buffer_consume(struct ring_buffer *buffer, int cpu, u64 *ts,
3981 unsigned long *lost_events)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003982{
Steven Rostedt554f7862009-03-11 22:00:13 -04003983 struct ring_buffer_per_cpu *cpu_buffer;
3984 struct ring_buffer_event *event = NULL;
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003985 unsigned long flags;
Steven Rostedt (Red Hat)289a5a22015-05-28 13:14:51 -04003986 bool dolock;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003987
Tom Zanussi2d622712009-03-22 03:30:49 -05003988 again:
Steven Rostedt554f7862009-03-11 22:00:13 -04003989 /* might be called in atomic */
3990 preempt_disable();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003991
Steven Rostedt554f7862009-03-11 22:00:13 -04003992 if (!cpumask_test_cpu(cpu, buffer->cpumask))
3993 goto out;
3994
3995 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt8d707e82009-06-16 21:22:48 -04003996 local_irq_save(flags);
Steven Rostedt (Red Hat)289a5a22015-05-28 13:14:51 -04003997 dolock = rb_reader_lock(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003998
Steven Rostedt66a8cb92010-03-31 13:21:56 -04003999 event = rb_buffer_peek(cpu_buffer, ts, lost_events);
4000 if (event) {
4001 cpu_buffer->lost_events = 0;
Robert Richter469535a2009-07-30 19:19:18 +02004002 rb_advance_reader(cpu_buffer);
Steven Rostedt66a8cb92010-03-31 13:21:56 -04004003 }
Steven Rostedtf83c9d02008-11-11 18:47:44 +01004004
Steven Rostedt (Red Hat)289a5a22015-05-28 13:14:51 -04004005 rb_reader_unlock(cpu_buffer, dolock);
Steven Rostedt8d707e82009-06-16 21:22:48 -04004006 local_irq_restore(flags);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01004007
Steven Rostedt554f7862009-03-11 22:00:13 -04004008 out:
4009 preempt_enable();
4010
Steven Rostedt1b959e12009-09-03 10:12:13 -04004011 if (event && event->type_len == RINGBUF_TYPE_PADDING)
Tom Zanussi2d622712009-03-22 03:30:49 -05004012 goto again;
Tom Zanussi2d622712009-03-22 03:30:49 -05004013
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004014 return event;
4015}
Robert Richterc4f50182008-12-11 16:49:22 +01004016EXPORT_SYMBOL_GPL(ring_buffer_consume);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004017
4018/**
David Miller72c9ddf2010-04-20 15:47:11 -07004019 * ring_buffer_read_prepare - Prepare for a non consuming read of the buffer
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004020 * @buffer: The ring buffer to read from
4021 * @cpu: The cpu buffer to iterate over
4022 *
David Miller72c9ddf2010-04-20 15:47:11 -07004023 * This performs the initial preparations necessary to iterate
4024 * through the buffer. Memory is allocated, buffer recording
4025 * is disabled, and the iterator pointer is returned to the caller.
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004026 *
David Miller72c9ddf2010-04-20 15:47:11 -07004027 * Disabling buffer recordng prevents the reading from being
4028 * corrupted. This is not a consuming read, so a producer is not
4029 * expected.
4030 *
4031 * After a sequence of ring_buffer_read_prepare calls, the user is
zhangwei(Jovi)d6118512013-07-15 16:32:50 +08004032 * expected to make at least one call to ring_buffer_read_prepare_sync.
David Miller72c9ddf2010-04-20 15:47:11 -07004033 * Afterwards, ring_buffer_read_start is invoked to get things going
4034 * for real.
4035 *
zhangwei(Jovi)d6118512013-07-15 16:32:50 +08004036 * This overall must be paired with ring_buffer_read_finish.
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004037 */
4038struct ring_buffer_iter *
David Miller72c9ddf2010-04-20 15:47:11 -07004039ring_buffer_read_prepare(struct ring_buffer *buffer, int cpu)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004040{
4041 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedt8aabee52009-03-12 13:13:49 -04004042 struct ring_buffer_iter *iter;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004043
Rusty Russell9e01c1b2009-01-01 10:12:22 +10304044 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04004045 return NULL;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004046
4047 iter = kmalloc(sizeof(*iter), GFP_KERNEL);
4048 if (!iter)
Steven Rostedt8aabee52009-03-12 13:13:49 -04004049 return NULL;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004050
4051 cpu_buffer = buffer->buffers[cpu];
4052
4053 iter->cpu_buffer = cpu_buffer;
4054
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07004055 atomic_inc(&buffer->resize_disabled);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004056 atomic_inc(&cpu_buffer->record_disabled);
David Miller72c9ddf2010-04-20 15:47:11 -07004057
4058 return iter;
4059}
4060EXPORT_SYMBOL_GPL(ring_buffer_read_prepare);
4061
4062/**
4063 * ring_buffer_read_prepare_sync - Synchronize a set of prepare calls
4064 *
4065 * All previously invoked ring_buffer_read_prepare calls to prepare
4066 * iterators will be synchronized. Afterwards, read_buffer_read_start
4067 * calls on those iterators are allowed.
4068 */
4069void
4070ring_buffer_read_prepare_sync(void)
4071{
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004072 synchronize_sched();
David Miller72c9ddf2010-04-20 15:47:11 -07004073}
4074EXPORT_SYMBOL_GPL(ring_buffer_read_prepare_sync);
4075
4076/**
4077 * ring_buffer_read_start - start a non consuming read of the buffer
4078 * @iter: The iterator returned by ring_buffer_read_prepare
4079 *
4080 * This finalizes the startup of an iteration through the buffer.
4081 * The iterator comes from a call to ring_buffer_read_prepare and
4082 * an intervening ring_buffer_read_prepare_sync must have been
4083 * performed.
4084 *
zhangwei(Jovi)d6118512013-07-15 16:32:50 +08004085 * Must be paired with ring_buffer_read_finish.
David Miller72c9ddf2010-04-20 15:47:11 -07004086 */
4087void
4088ring_buffer_read_start(struct ring_buffer_iter *iter)
4089{
4090 struct ring_buffer_per_cpu *cpu_buffer;
4091 unsigned long flags;
4092
4093 if (!iter)
4094 return;
4095
4096 cpu_buffer = iter->cpu_buffer;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004097
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02004098 raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01004099 arch_spin_lock(&cpu_buffer->lock);
Steven Rostedt642edba2008-11-12 00:01:26 -05004100 rb_iter_reset(iter);
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01004101 arch_spin_unlock(&cpu_buffer->lock);
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02004102 raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004103}
Robert Richterc4f50182008-12-11 16:49:22 +01004104EXPORT_SYMBOL_GPL(ring_buffer_read_start);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004105
4106/**
zhangwei(Jovi)d6118512013-07-15 16:32:50 +08004107 * ring_buffer_read_finish - finish reading the iterator of the buffer
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004108 * @iter: The iterator retrieved by ring_buffer_start
4109 *
4110 * This re-enables the recording to the buffer, and frees the
4111 * iterator.
4112 */
4113void
4114ring_buffer_read_finish(struct ring_buffer_iter *iter)
4115{
4116 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
Steven Rostedt9366c1b2012-11-29 22:31:16 -05004117 unsigned long flags;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004118
Steven Rostedt659f4512012-05-14 17:02:33 -04004119 /*
4120 * Ring buffer is disabled from recording, here's a good place
Steven Rostedt9366c1b2012-11-29 22:31:16 -05004121 * to check the integrity of the ring buffer.
4122 * Must prevent readers from trying to read, as the check
4123 * clears the HEAD page and readers require it.
Steven Rostedt659f4512012-05-14 17:02:33 -04004124 */
Steven Rostedt9366c1b2012-11-29 22:31:16 -05004125 raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
Steven Rostedt659f4512012-05-14 17:02:33 -04004126 rb_check_pages(cpu_buffer);
Steven Rostedt9366c1b2012-11-29 22:31:16 -05004127 raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
Steven Rostedt659f4512012-05-14 17:02:33 -04004128
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004129 atomic_dec(&cpu_buffer->record_disabled);
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07004130 atomic_dec(&cpu_buffer->buffer->resize_disabled);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004131 kfree(iter);
4132}
Robert Richterc4f50182008-12-11 16:49:22 +01004133EXPORT_SYMBOL_GPL(ring_buffer_read_finish);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004134
4135/**
4136 * ring_buffer_read - read the next item in the ring buffer by the iterator
4137 * @iter: The ring buffer iterator
4138 * @ts: The time stamp of the event read.
4139 *
4140 * This reads the next event in the ring buffer and increments the iterator.
4141 */
4142struct ring_buffer_event *
4143ring_buffer_read(struct ring_buffer_iter *iter, u64 *ts)
4144{
4145 struct ring_buffer_event *event;
Steven Rostedtf83c9d02008-11-11 18:47:44 +01004146 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
4147 unsigned long flags;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004148
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02004149 raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
Steven Rostedt7e9391c2009-09-03 10:02:09 -04004150 again:
Steven Rostedtf83c9d02008-11-11 18:47:44 +01004151 event = rb_iter_peek(iter, ts);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004152 if (!event)
Steven Rostedtf83c9d02008-11-11 18:47:44 +01004153 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004154
Steven Rostedt7e9391c2009-09-03 10:02:09 -04004155 if (event->type_len == RINGBUF_TYPE_PADDING)
4156 goto again;
4157
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004158 rb_advance_iter(iter);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01004159 out:
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02004160 raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004161
4162 return event;
4163}
Robert Richterc4f50182008-12-11 16:49:22 +01004164EXPORT_SYMBOL_GPL(ring_buffer_read);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004165
4166/**
4167 * ring_buffer_size - return the size of the ring buffer (in bytes)
4168 * @buffer: The ring buffer.
4169 */
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08004170unsigned long ring_buffer_size(struct ring_buffer *buffer, int cpu)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004171{
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08004172 /*
4173 * Earlier, this method returned
4174 * BUF_PAGE_SIZE * buffer->nr_pages
4175 * Since the nr_pages field is now removed, we have converted this to
4176 * return the per cpu buffer value.
4177 */
4178 if (!cpumask_test_cpu(cpu, buffer->cpumask))
4179 return 0;
4180
4181 return BUF_PAGE_SIZE * buffer->buffers[cpu]->nr_pages;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004182}
Robert Richterc4f50182008-12-11 16:49:22 +01004183EXPORT_SYMBOL_GPL(ring_buffer_size);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004184
4185static void
4186rb_reset_cpu(struct ring_buffer_per_cpu *cpu_buffer)
4187{
Steven Rostedt77ae3652009-03-27 11:00:29 -04004188 rb_head_page_deactivate(cpu_buffer);
4189
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004190 cpu_buffer->head_page
Steven Rostedt3adc54f2009-03-30 15:32:01 -04004191 = list_entry(cpu_buffer->pages, struct buffer_page, list);
Steven Rostedtbf41a152008-10-04 02:00:59 -04004192 local_set(&cpu_buffer->head_page->write, 0);
Steven Rostedt778c55d2009-05-01 18:44:45 -04004193 local_set(&cpu_buffer->head_page->entries, 0);
Steven Rostedtabc9b562008-12-02 15:34:06 -05004194 local_set(&cpu_buffer->head_page->page->commit, 0);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004195
Steven Rostedt6f807ac2008-10-04 02:00:58 -04004196 cpu_buffer->head_page->read = 0;
Steven Rostedtbf41a152008-10-04 02:00:59 -04004197
4198 cpu_buffer->tail_page = cpu_buffer->head_page;
4199 cpu_buffer->commit_page = cpu_buffer->head_page;
4200
4201 INIT_LIST_HEAD(&cpu_buffer->reader_page->list);
Vaibhav Nagarnaik5040b4b2012-05-03 18:59:51 -07004202 INIT_LIST_HEAD(&cpu_buffer->new_pages);
Steven Rostedtbf41a152008-10-04 02:00:59 -04004203 local_set(&cpu_buffer->reader_page->write, 0);
Steven Rostedt778c55d2009-05-01 18:44:45 -04004204 local_set(&cpu_buffer->reader_page->entries, 0);
Steven Rostedtabc9b562008-12-02 15:34:06 -05004205 local_set(&cpu_buffer->reader_page->page->commit, 0);
Steven Rostedt6f807ac2008-10-04 02:00:58 -04004206 cpu_buffer->reader_page->read = 0;
Steven Rostedtd7690412008-10-01 00:29:53 -04004207
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -07004208 local_set(&cpu_buffer->entries_bytes, 0);
Steven Rostedt77ae3652009-03-27 11:00:29 -04004209 local_set(&cpu_buffer->overrun, 0);
Slava Pestov884bfe82011-07-15 14:23:58 -07004210 local_set(&cpu_buffer->commit_overrun, 0);
4211 local_set(&cpu_buffer->dropped_events, 0);
Steven Rostedte4906ef2009-04-30 20:49:44 -04004212 local_set(&cpu_buffer->entries, 0);
Steven Rostedtfa743952009-06-16 12:37:57 -04004213 local_set(&cpu_buffer->committing, 0);
4214 local_set(&cpu_buffer->commits, 0);
Steven Rostedt77ae3652009-03-27 11:00:29 -04004215 cpu_buffer->read = 0;
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -07004216 cpu_buffer->read_bytes = 0;
Steven Rostedt69507c02009-01-21 18:45:57 -05004217
4218 cpu_buffer->write_stamp = 0;
4219 cpu_buffer->read_stamp = 0;
Steven Rostedt77ae3652009-03-27 11:00:29 -04004220
Steven Rostedt66a8cb92010-03-31 13:21:56 -04004221 cpu_buffer->lost_events = 0;
4222 cpu_buffer->last_overrun = 0;
4223
Steven Rostedt77ae3652009-03-27 11:00:29 -04004224 rb_head_page_activate(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004225}
4226
4227/**
4228 * ring_buffer_reset_cpu - reset a ring buffer per CPU buffer
4229 * @buffer: The ring buffer to reset a per cpu buffer of
4230 * @cpu: The CPU buffer to be reset
4231 */
4232void ring_buffer_reset_cpu(struct ring_buffer *buffer, int cpu)
4233{
4234 struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
4235 unsigned long flags;
4236
Rusty Russell9e01c1b2009-01-01 10:12:22 +10304237 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04004238 return;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004239
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07004240 atomic_inc(&buffer->resize_disabled);
Steven Rostedt41ede232009-05-01 20:26:54 -04004241 atomic_inc(&cpu_buffer->record_disabled);
4242
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07004243 /* Make sure all commits have finished */
4244 synchronize_sched();
4245
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02004246 raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01004247
Steven Rostedt41b6a952009-09-02 09:59:48 -04004248 if (RB_WARN_ON(cpu_buffer, local_read(&cpu_buffer->committing)))
4249 goto out;
4250
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01004251 arch_spin_lock(&cpu_buffer->lock);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004252
4253 rb_reset_cpu(cpu_buffer);
4254
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01004255 arch_spin_unlock(&cpu_buffer->lock);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01004256
Steven Rostedt41b6a952009-09-02 09:59:48 -04004257 out:
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02004258 raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
Steven Rostedt41ede232009-05-01 20:26:54 -04004259
4260 atomic_dec(&cpu_buffer->record_disabled);
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07004261 atomic_dec(&buffer->resize_disabled);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004262}
Robert Richterc4f50182008-12-11 16:49:22 +01004263EXPORT_SYMBOL_GPL(ring_buffer_reset_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004264
4265/**
4266 * ring_buffer_reset - reset a ring buffer
4267 * @buffer: The ring buffer to reset all cpu buffers
4268 */
4269void ring_buffer_reset(struct ring_buffer *buffer)
4270{
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004271 int cpu;
4272
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004273 for_each_buffer_cpu(buffer, cpu)
Steven Rostedtd7690412008-10-01 00:29:53 -04004274 ring_buffer_reset_cpu(buffer, cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004275}
Robert Richterc4f50182008-12-11 16:49:22 +01004276EXPORT_SYMBOL_GPL(ring_buffer_reset);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004277
4278/**
4279 * rind_buffer_empty - is the ring buffer empty?
4280 * @buffer: The ring buffer to test
4281 */
Yaowei Bai3d4e2042015-09-29 22:43:32 +08004282bool ring_buffer_empty(struct ring_buffer *buffer)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004283{
4284 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedtd4788202009-06-17 00:39:43 -04004285 unsigned long flags;
Steven Rostedt (Red Hat)289a5a22015-05-28 13:14:51 -04004286 bool dolock;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004287 int cpu;
Steven Rostedtd4788202009-06-17 00:39:43 -04004288 int ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004289
4290 /* yes this is racy, but if you don't like the race, lock the buffer */
4291 for_each_buffer_cpu(buffer, cpu) {
4292 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt8d707e82009-06-16 21:22:48 -04004293 local_irq_save(flags);
Steven Rostedt (Red Hat)289a5a22015-05-28 13:14:51 -04004294 dolock = rb_reader_lock(cpu_buffer);
Steven Rostedtd4788202009-06-17 00:39:43 -04004295 ret = rb_per_cpu_empty(cpu_buffer);
Steven Rostedt (Red Hat)289a5a22015-05-28 13:14:51 -04004296 rb_reader_unlock(cpu_buffer, dolock);
Steven Rostedt8d707e82009-06-16 21:22:48 -04004297 local_irq_restore(flags);
4298
Steven Rostedtd4788202009-06-17 00:39:43 -04004299 if (!ret)
Yaowei Bai3d4e2042015-09-29 22:43:32 +08004300 return false;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004301 }
Steven Rostedt554f7862009-03-11 22:00:13 -04004302
Yaowei Bai3d4e2042015-09-29 22:43:32 +08004303 return true;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004304}
Robert Richterc4f50182008-12-11 16:49:22 +01004305EXPORT_SYMBOL_GPL(ring_buffer_empty);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004306
4307/**
4308 * ring_buffer_empty_cpu - is a cpu buffer of a ring buffer empty?
4309 * @buffer: The ring buffer
4310 * @cpu: The CPU buffer to test
4311 */
Yaowei Bai3d4e2042015-09-29 22:43:32 +08004312bool ring_buffer_empty_cpu(struct ring_buffer *buffer, int cpu)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004313{
4314 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedtd4788202009-06-17 00:39:43 -04004315 unsigned long flags;
Steven Rostedt (Red Hat)289a5a22015-05-28 13:14:51 -04004316 bool dolock;
Steven Rostedt8aabee52009-03-12 13:13:49 -04004317 int ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004318
Rusty Russell9e01c1b2009-01-01 10:12:22 +10304319 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Yaowei Bai3d4e2042015-09-29 22:43:32 +08004320 return true;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004321
4322 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt8d707e82009-06-16 21:22:48 -04004323 local_irq_save(flags);
Steven Rostedt (Red Hat)289a5a22015-05-28 13:14:51 -04004324 dolock = rb_reader_lock(cpu_buffer);
Steven Rostedt554f7862009-03-11 22:00:13 -04004325 ret = rb_per_cpu_empty(cpu_buffer);
Steven Rostedt (Red Hat)289a5a22015-05-28 13:14:51 -04004326 rb_reader_unlock(cpu_buffer, dolock);
Steven Rostedt8d707e82009-06-16 21:22:48 -04004327 local_irq_restore(flags);
Steven Rostedt554f7862009-03-11 22:00:13 -04004328
4329 return ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004330}
Robert Richterc4f50182008-12-11 16:49:22 +01004331EXPORT_SYMBOL_GPL(ring_buffer_empty_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004332
Steven Rostedt85bac322009-09-04 14:24:40 -04004333#ifdef CONFIG_RING_BUFFER_ALLOW_SWAP
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004334/**
4335 * ring_buffer_swap_cpu - swap a CPU buffer between two ring buffers
4336 * @buffer_a: One buffer to swap with
4337 * @buffer_b: The other buffer to swap with
4338 *
4339 * This function is useful for tracers that want to take a "snapshot"
4340 * of a CPU buffer and has another back up buffer lying around.
4341 * it is expected that the tracer handles the cpu buffer not being
4342 * used at the moment.
4343 */
4344int ring_buffer_swap_cpu(struct ring_buffer *buffer_a,
4345 struct ring_buffer *buffer_b, int cpu)
4346{
4347 struct ring_buffer_per_cpu *cpu_buffer_a;
4348 struct ring_buffer_per_cpu *cpu_buffer_b;
Steven Rostedt554f7862009-03-11 22:00:13 -04004349 int ret = -EINVAL;
4350
Rusty Russell9e01c1b2009-01-01 10:12:22 +10304351 if (!cpumask_test_cpu(cpu, buffer_a->cpumask) ||
4352 !cpumask_test_cpu(cpu, buffer_b->cpumask))
Steven Rostedt554f7862009-03-11 22:00:13 -04004353 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004354
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08004355 cpu_buffer_a = buffer_a->buffers[cpu];
4356 cpu_buffer_b = buffer_b->buffers[cpu];
4357
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004358 /* At least make sure the two buffers are somewhat the same */
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08004359 if (cpu_buffer_a->nr_pages != cpu_buffer_b->nr_pages)
Steven Rostedt554f7862009-03-11 22:00:13 -04004360 goto out;
4361
4362 ret = -EAGAIN;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004363
Steven Rostedt97b17ef2009-01-21 15:24:56 -05004364 if (atomic_read(&buffer_a->record_disabled))
Steven Rostedt554f7862009-03-11 22:00:13 -04004365 goto out;
Steven Rostedt97b17ef2009-01-21 15:24:56 -05004366
4367 if (atomic_read(&buffer_b->record_disabled))
Steven Rostedt554f7862009-03-11 22:00:13 -04004368 goto out;
Steven Rostedt97b17ef2009-01-21 15:24:56 -05004369
Steven Rostedt97b17ef2009-01-21 15:24:56 -05004370 if (atomic_read(&cpu_buffer_a->record_disabled))
Steven Rostedt554f7862009-03-11 22:00:13 -04004371 goto out;
Steven Rostedt97b17ef2009-01-21 15:24:56 -05004372
4373 if (atomic_read(&cpu_buffer_b->record_disabled))
Steven Rostedt554f7862009-03-11 22:00:13 -04004374 goto out;
Steven Rostedt97b17ef2009-01-21 15:24:56 -05004375
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004376 /*
4377 * We can't do a synchronize_sched here because this
4378 * function can be called in atomic context.
4379 * Normally this will be called from the same CPU as cpu.
4380 * If not it's up to the caller to protect this.
4381 */
4382 atomic_inc(&cpu_buffer_a->record_disabled);
4383 atomic_inc(&cpu_buffer_b->record_disabled);
4384
Steven Rostedt98277992009-09-02 10:56:15 -04004385 ret = -EBUSY;
4386 if (local_read(&cpu_buffer_a->committing))
4387 goto out_dec;
4388 if (local_read(&cpu_buffer_b->committing))
4389 goto out_dec;
4390
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004391 buffer_a->buffers[cpu] = cpu_buffer_b;
4392 buffer_b->buffers[cpu] = cpu_buffer_a;
4393
4394 cpu_buffer_b->buffer = buffer_a;
4395 cpu_buffer_a->buffer = buffer_b;
4396
Steven Rostedt98277992009-09-02 10:56:15 -04004397 ret = 0;
4398
4399out_dec:
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004400 atomic_dec(&cpu_buffer_a->record_disabled);
4401 atomic_dec(&cpu_buffer_b->record_disabled);
Steven Rostedt554f7862009-03-11 22:00:13 -04004402out:
Steven Rostedt554f7862009-03-11 22:00:13 -04004403 return ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004404}
Robert Richterc4f50182008-12-11 16:49:22 +01004405EXPORT_SYMBOL_GPL(ring_buffer_swap_cpu);
Steven Rostedt85bac322009-09-04 14:24:40 -04004406#endif /* CONFIG_RING_BUFFER_ALLOW_SWAP */
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004407
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004408/**
4409 * ring_buffer_alloc_read_page - allocate a page to read from buffer
4410 * @buffer: the buffer to allocate for.
zhangwei(Jovi)d6118512013-07-15 16:32:50 +08004411 * @cpu: the cpu buffer to allocate.
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004412 *
4413 * This function is used in conjunction with ring_buffer_read_page.
4414 * When reading a full page from the ring buffer, these functions
4415 * can be used to speed up the process. The calling function should
4416 * allocate a few pages first with this function. Then when it
4417 * needs to get pages from the ring buffer, it passes the result
4418 * of this function into ring_buffer_read_page, which will swap
4419 * the page that was allocated, with the read page of the buffer.
4420 *
4421 * Returns:
4422 * The page allocated, or NULL on error.
4423 */
Vaibhav Nagarnaik7ea59062011-05-03 17:56:42 -07004424void *ring_buffer_alloc_read_page(struct ring_buffer *buffer, int cpu)
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004425{
Steven Rostedt044fa782008-12-02 23:50:03 -05004426 struct buffer_data_page *bpage;
Vaibhav Nagarnaik7ea59062011-05-03 17:56:42 -07004427 struct page *page;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004428
Vaibhav Nagarnaikd7ec4bf2011-06-07 17:01:42 -07004429 page = alloc_pages_node(cpu_to_node(cpu),
4430 GFP_KERNEL | __GFP_NORETRY, 0);
Vaibhav Nagarnaik7ea59062011-05-03 17:56:42 -07004431 if (!page)
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004432 return NULL;
4433
Vaibhav Nagarnaik7ea59062011-05-03 17:56:42 -07004434 bpage = page_address(page);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004435
Steven Rostedtef7a4a12009-03-03 00:27:49 -05004436 rb_init_page(bpage);
4437
Steven Rostedt044fa782008-12-02 23:50:03 -05004438 return bpage;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004439}
Steven Rostedtd6ce96d2009-05-05 01:15:24 -04004440EXPORT_SYMBOL_GPL(ring_buffer_alloc_read_page);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004441
4442/**
4443 * ring_buffer_free_read_page - free an allocated read page
4444 * @buffer: the buffer the page was allocate for
4445 * @data: the page to free
4446 *
4447 * Free a page allocated from ring_buffer_alloc_read_page.
4448 */
4449void ring_buffer_free_read_page(struct ring_buffer *buffer, void *data)
4450{
4451 free_page((unsigned long)data);
4452}
Steven Rostedtd6ce96d2009-05-05 01:15:24 -04004453EXPORT_SYMBOL_GPL(ring_buffer_free_read_page);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004454
4455/**
4456 * ring_buffer_read_page - extract a page from the ring buffer
4457 * @buffer: buffer to extract from
4458 * @data_page: the page to use allocated from ring_buffer_alloc_read_page
Steven Rostedtef7a4a12009-03-03 00:27:49 -05004459 * @len: amount to extract
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004460 * @cpu: the cpu of the buffer to extract
4461 * @full: should the extraction only happen when the page is full.
4462 *
4463 * This function will pull out a page from the ring buffer and consume it.
4464 * @data_page must be the address of the variable that was returned
4465 * from ring_buffer_alloc_read_page. This is because the page might be used
4466 * to swap with a page in the ring buffer.
4467 *
4468 * for example:
zhangwei(Jovi)d6118512013-07-15 16:32:50 +08004469 * rpage = ring_buffer_alloc_read_page(buffer, cpu);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004470 * if (!rpage)
4471 * return error;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05004472 * ret = ring_buffer_read_page(buffer, &rpage, len, cpu, 0);
Lai Jiangshan667d2412009-02-09 14:21:17 +08004473 * if (ret >= 0)
4474 * process_page(rpage, ret);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004475 *
4476 * When @full is set, the function will not return true unless
4477 * the writer is off the reader page.
4478 *
4479 * Note: it is up to the calling functions to handle sleeps and wakeups.
4480 * The ring buffer can be used anywhere in the kernel and can not
4481 * blindly call wake_up. The layer that uses the ring buffer must be
4482 * responsible for that.
4483 *
4484 * Returns:
Lai Jiangshan667d2412009-02-09 14:21:17 +08004485 * >=0 if data has been transferred, returns the offset of consumed data.
4486 * <0 if no data has been transferred.
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004487 */
4488int ring_buffer_read_page(struct ring_buffer *buffer,
Steven Rostedtef7a4a12009-03-03 00:27:49 -05004489 void **data_page, size_t len, int cpu, int full)
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004490{
4491 struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
4492 struct ring_buffer_event *event;
Steven Rostedt044fa782008-12-02 23:50:03 -05004493 struct buffer_data_page *bpage;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05004494 struct buffer_page *reader;
Steven Rostedtff0ff842010-03-31 22:11:42 -04004495 unsigned long missed_events;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004496 unsigned long flags;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05004497 unsigned int commit;
Lai Jiangshan667d2412009-02-09 14:21:17 +08004498 unsigned int read;
Steven Rostedt4f3640f2009-03-03 23:52:42 -05004499 u64 save_timestamp;
Lai Jiangshan667d2412009-02-09 14:21:17 +08004500 int ret = -1;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004501
Steven Rostedt554f7862009-03-11 22:00:13 -04004502 if (!cpumask_test_cpu(cpu, buffer->cpumask))
4503 goto out;
4504
Steven Rostedt474d32b2009-03-03 19:51:40 -05004505 /*
4506 * If len is not big enough to hold the page header, then
4507 * we can not copy anything.
4508 */
4509 if (len <= BUF_PAGE_HDR_SIZE)
Steven Rostedt554f7862009-03-11 22:00:13 -04004510 goto out;
Steven Rostedt474d32b2009-03-03 19:51:40 -05004511
4512 len -= BUF_PAGE_HDR_SIZE;
4513
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004514 if (!data_page)
Steven Rostedt554f7862009-03-11 22:00:13 -04004515 goto out;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004516
Steven Rostedt044fa782008-12-02 23:50:03 -05004517 bpage = *data_page;
4518 if (!bpage)
Steven Rostedt554f7862009-03-11 22:00:13 -04004519 goto out;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004520
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02004521 raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004522
Steven Rostedtef7a4a12009-03-03 00:27:49 -05004523 reader = rb_get_reader_page(cpu_buffer);
4524 if (!reader)
Steven Rostedt554f7862009-03-11 22:00:13 -04004525 goto out_unlock;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004526
Steven Rostedtef7a4a12009-03-03 00:27:49 -05004527 event = rb_reader_event(cpu_buffer);
Lai Jiangshan667d2412009-02-09 14:21:17 +08004528
Steven Rostedtef7a4a12009-03-03 00:27:49 -05004529 read = reader->read;
4530 commit = rb_page_commit(reader);
4531
Steven Rostedt66a8cb92010-03-31 13:21:56 -04004532 /* Check if any events were dropped */
Steven Rostedtff0ff842010-03-31 22:11:42 -04004533 missed_events = cpu_buffer->lost_events;
Steven Rostedt66a8cb92010-03-31 13:21:56 -04004534
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004535 /*
Steven Rostedt474d32b2009-03-03 19:51:40 -05004536 * If this page has been partially read or
4537 * if len is not big enough to read the rest of the page or
4538 * a writer is still on the page, then
4539 * we must copy the data from the page to the buffer.
4540 * Otherwise, we can simply swap the page with the one passed in.
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004541 */
Steven Rostedt474d32b2009-03-03 19:51:40 -05004542 if (read || (len < (commit - read)) ||
Steven Rostedtef7a4a12009-03-03 00:27:49 -05004543 cpu_buffer->reader_page == cpu_buffer->commit_page) {
Lai Jiangshan667d2412009-02-09 14:21:17 +08004544 struct buffer_data_page *rpage = cpu_buffer->reader_page->page;
Steven Rostedt474d32b2009-03-03 19:51:40 -05004545 unsigned int rpos = read;
4546 unsigned int pos = 0;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05004547 unsigned int size;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004548
4549 if (full)
Steven Rostedt554f7862009-03-11 22:00:13 -04004550 goto out_unlock;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004551
Steven Rostedtef7a4a12009-03-03 00:27:49 -05004552 if (len > (commit - read))
4553 len = (commit - read);
4554
Steven Rostedt69d1b832010-10-07 18:18:05 -04004555 /* Always keep the time extend and data together */
4556 size = rb_event_ts_length(event);
Steven Rostedtef7a4a12009-03-03 00:27:49 -05004557
4558 if (len < size)
Steven Rostedt554f7862009-03-11 22:00:13 -04004559 goto out_unlock;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05004560
Steven Rostedt4f3640f2009-03-03 23:52:42 -05004561 /* save the current timestamp, since the user will need it */
4562 save_timestamp = cpu_buffer->read_stamp;
4563
Steven Rostedtef7a4a12009-03-03 00:27:49 -05004564 /* Need to copy one event at a time */
4565 do {
David Sharpe1e35922010-12-22 16:38:24 -08004566 /* We need the size of one event, because
4567 * rb_advance_reader only advances by one event,
4568 * whereas rb_event_ts_length may include the size of
4569 * one or two events.
4570 * We have already ensured there's enough space if this
4571 * is a time extend. */
4572 size = rb_event_length(event);
Steven Rostedt474d32b2009-03-03 19:51:40 -05004573 memcpy(bpage->data + pos, rpage->data + rpos, size);
Steven Rostedtef7a4a12009-03-03 00:27:49 -05004574
4575 len -= size;
4576
4577 rb_advance_reader(cpu_buffer);
Steven Rostedt474d32b2009-03-03 19:51:40 -05004578 rpos = reader->read;
4579 pos += size;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05004580
Huang Ying18fab912010-07-28 14:14:01 +08004581 if (rpos >= commit)
4582 break;
4583
Steven Rostedtef7a4a12009-03-03 00:27:49 -05004584 event = rb_reader_event(cpu_buffer);
Steven Rostedt69d1b832010-10-07 18:18:05 -04004585 /* Always keep the time extend and data together */
4586 size = rb_event_ts_length(event);
David Sharpe1e35922010-12-22 16:38:24 -08004587 } while (len >= size);
Lai Jiangshan667d2412009-02-09 14:21:17 +08004588
4589 /* update bpage */
Steven Rostedtef7a4a12009-03-03 00:27:49 -05004590 local_set(&bpage->commit, pos);
Steven Rostedt4f3640f2009-03-03 23:52:42 -05004591 bpage->time_stamp = save_timestamp;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05004592
Steven Rostedt474d32b2009-03-03 19:51:40 -05004593 /* we copied everything to the beginning */
4594 read = 0;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004595 } else {
Steven Rostedtafbab762009-05-01 19:40:05 -04004596 /* update the entry counter */
Steven Rostedt77ae3652009-03-27 11:00:29 -04004597 cpu_buffer->read += rb_page_entries(reader);
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -07004598 cpu_buffer->read_bytes += BUF_PAGE_SIZE;
Steven Rostedtafbab762009-05-01 19:40:05 -04004599
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004600 /* swap the pages */
Steven Rostedt044fa782008-12-02 23:50:03 -05004601 rb_init_page(bpage);
Steven Rostedtef7a4a12009-03-03 00:27:49 -05004602 bpage = reader->page;
4603 reader->page = *data_page;
4604 local_set(&reader->write, 0);
Steven Rostedt778c55d2009-05-01 18:44:45 -04004605 local_set(&reader->entries, 0);
Steven Rostedtef7a4a12009-03-03 00:27:49 -05004606 reader->read = 0;
Steven Rostedt044fa782008-12-02 23:50:03 -05004607 *data_page = bpage;
Steven Rostedtff0ff842010-03-31 22:11:42 -04004608
4609 /*
4610 * Use the real_end for the data size,
4611 * This gives us a chance to store the lost events
4612 * on the page.
4613 */
4614 if (reader->real_end)
4615 local_set(&bpage->commit, reader->real_end);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004616 }
Lai Jiangshan667d2412009-02-09 14:21:17 +08004617 ret = read;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004618
Steven Rostedt66a8cb92010-03-31 13:21:56 -04004619 cpu_buffer->lost_events = 0;
Steven Rostedt2711ca22010-05-21 13:32:26 -04004620
4621 commit = local_read(&bpage->commit);
Steven Rostedt66a8cb92010-03-31 13:21:56 -04004622 /*
4623 * Set a flag in the commit field if we lost events
4624 */
Steven Rostedtff0ff842010-03-31 22:11:42 -04004625 if (missed_events) {
Steven Rostedtff0ff842010-03-31 22:11:42 -04004626 /* If there is room at the end of the page to save the
4627 * missed events, then record it there.
4628 */
4629 if (BUF_PAGE_SIZE - commit >= sizeof(missed_events)) {
4630 memcpy(&bpage->data[commit], &missed_events,
4631 sizeof(missed_events));
4632 local_add(RB_MISSED_STORED, &bpage->commit);
Steven Rostedt2711ca22010-05-21 13:32:26 -04004633 commit += sizeof(missed_events);
Steven Rostedtff0ff842010-03-31 22:11:42 -04004634 }
Steven Rostedt66a8cb92010-03-31 13:21:56 -04004635 local_add(RB_MISSED_EVENTS, &bpage->commit);
Steven Rostedtff0ff842010-03-31 22:11:42 -04004636 }
Steven Rostedt66a8cb92010-03-31 13:21:56 -04004637
Steven Rostedt2711ca22010-05-21 13:32:26 -04004638 /*
4639 * This page may be off to user land. Zero it out here.
4640 */
4641 if (commit < BUF_PAGE_SIZE)
4642 memset(&bpage->data[commit], 0, BUF_PAGE_SIZE - commit);
4643
Steven Rostedt554f7862009-03-11 22:00:13 -04004644 out_unlock:
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02004645 raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004646
Steven Rostedt554f7862009-03-11 22:00:13 -04004647 out:
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004648 return ret;
4649}
Steven Rostedtd6ce96d2009-05-05 01:15:24 -04004650EXPORT_SYMBOL_GPL(ring_buffer_read_page);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004651
Steven Rostedt59222ef2009-03-12 11:46:03 -04004652#ifdef CONFIG_HOTPLUG_CPU
Frederic Weisbecker09c9e842009-03-21 04:33:36 +01004653static int rb_cpu_notify(struct notifier_block *self,
4654 unsigned long action, void *hcpu)
Steven Rostedt554f7862009-03-11 22:00:13 -04004655{
4656 struct ring_buffer *buffer =
4657 container_of(self, struct ring_buffer, cpu_notify);
4658 long cpu = (long)hcpu;
Steven Rostedt (Red Hat)9b94a8f2016-05-12 11:01:24 -04004659 long nr_pages_same;
4660 int cpu_i;
4661 unsigned long nr_pages;
Steven Rostedt554f7862009-03-11 22:00:13 -04004662
4663 switch (action) {
4664 case CPU_UP_PREPARE:
4665 case CPU_UP_PREPARE_FROZEN:
Rusty Russell3f237a72009-06-12 21:15:30 +09304666 if (cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt554f7862009-03-11 22:00:13 -04004667 return NOTIFY_OK;
4668
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08004669 nr_pages = 0;
4670 nr_pages_same = 1;
4671 /* check if all cpu sizes are same */
4672 for_each_buffer_cpu(buffer, cpu_i) {
4673 /* fill in the size from first enabled cpu */
4674 if (nr_pages == 0)
4675 nr_pages = buffer->buffers[cpu_i]->nr_pages;
4676 if (nr_pages != buffer->buffers[cpu_i]->nr_pages) {
4677 nr_pages_same = 0;
4678 break;
4679 }
4680 }
4681 /* allocate minimum pages, user can later expand it */
4682 if (!nr_pages_same)
4683 nr_pages = 2;
Steven Rostedt554f7862009-03-11 22:00:13 -04004684 buffer->buffers[cpu] =
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08004685 rb_allocate_cpu_buffer(buffer, nr_pages, cpu);
Steven Rostedt554f7862009-03-11 22:00:13 -04004686 if (!buffer->buffers[cpu]) {
4687 WARN(1, "failed to allocate ring buffer on CPU %ld\n",
4688 cpu);
4689 return NOTIFY_OK;
4690 }
4691 smp_wmb();
Rusty Russell3f237a72009-06-12 21:15:30 +09304692 cpumask_set_cpu(cpu, buffer->cpumask);
Steven Rostedt554f7862009-03-11 22:00:13 -04004693 break;
4694 case CPU_DOWN_PREPARE:
4695 case CPU_DOWN_PREPARE_FROZEN:
4696 /*
4697 * Do nothing.
4698 * If we were to free the buffer, then the user would
4699 * lose any trace that was in the buffer.
4700 */
4701 break;
4702 default:
4703 break;
4704 }
4705 return NOTIFY_OK;
4706}
4707#endif
Steven Rostedt (Red Hat)6c43e552013-03-15 11:32:53 -04004708
4709#ifdef CONFIG_RING_BUFFER_STARTUP_TEST
4710/*
4711 * This is a basic integrity check of the ring buffer.
4712 * Late in the boot cycle this test will run when configured in.
4713 * It will kick off a thread per CPU that will go into a loop
4714 * writing to the per cpu ring buffer various sizes of data.
4715 * Some of the data will be large items, some small.
4716 *
4717 * Another thread is created that goes into a spin, sending out
4718 * IPIs to the other CPUs to also write into the ring buffer.
4719 * this is to test the nesting ability of the buffer.
4720 *
4721 * Basic stats are recorded and reported. If something in the
4722 * ring buffer should happen that's not expected, a big warning
4723 * is displayed and all ring buffers are disabled.
4724 */
4725static struct task_struct *rb_threads[NR_CPUS] __initdata;
4726
4727struct rb_test_data {
4728 struct ring_buffer *buffer;
4729 unsigned long events;
4730 unsigned long bytes_written;
4731 unsigned long bytes_alloc;
4732 unsigned long bytes_dropped;
4733 unsigned long events_nested;
4734 unsigned long bytes_written_nested;
4735 unsigned long bytes_alloc_nested;
4736 unsigned long bytes_dropped_nested;
4737 int min_size_nested;
4738 int max_size_nested;
4739 int max_size;
4740 int min_size;
4741 int cpu;
4742 int cnt;
4743};
4744
4745static struct rb_test_data rb_data[NR_CPUS] __initdata;
4746
4747/* 1 meg per cpu */
4748#define RB_TEST_BUFFER_SIZE 1048576
4749
4750static char rb_string[] __initdata =
4751 "abcdefghijklmnopqrstuvwxyz1234567890!@#$%^&*()?+\\"
4752 "?+|:';\",.<>/?abcdefghijklmnopqrstuvwxyz1234567890"
4753 "!@#$%^&*()?+\\?+|:';\",.<>/?abcdefghijklmnopqrstuv";
4754
4755static bool rb_test_started __initdata;
4756
4757struct rb_item {
4758 int size;
4759 char str[];
4760};
4761
4762static __init int rb_write_something(struct rb_test_data *data, bool nested)
4763{
4764 struct ring_buffer_event *event;
4765 struct rb_item *item;
4766 bool started;
4767 int event_len;
4768 int size;
4769 int len;
4770 int cnt;
4771
4772 /* Have nested writes different that what is written */
4773 cnt = data->cnt + (nested ? 27 : 0);
4774
4775 /* Multiply cnt by ~e, to make some unique increment */
4776 size = (data->cnt * 68 / 25) % (sizeof(rb_string) - 1);
4777
4778 len = size + sizeof(struct rb_item);
4779
4780 started = rb_test_started;
4781 /* read rb_test_started before checking buffer enabled */
4782 smp_rmb();
4783
4784 event = ring_buffer_lock_reserve(data->buffer, len);
4785 if (!event) {
4786 /* Ignore dropped events before test starts. */
4787 if (started) {
4788 if (nested)
4789 data->bytes_dropped += len;
4790 else
4791 data->bytes_dropped_nested += len;
4792 }
4793 return len;
4794 }
4795
4796 event_len = ring_buffer_event_length(event);
4797
4798 if (RB_WARN_ON(data->buffer, event_len < len))
4799 goto out;
4800
4801 item = ring_buffer_event_data(event);
4802 item->size = size;
4803 memcpy(item->str, rb_string, size);
4804
4805 if (nested) {
4806 data->bytes_alloc_nested += event_len;
4807 data->bytes_written_nested += len;
4808 data->events_nested++;
4809 if (!data->min_size_nested || len < data->min_size_nested)
4810 data->min_size_nested = len;
4811 if (len > data->max_size_nested)
4812 data->max_size_nested = len;
4813 } else {
4814 data->bytes_alloc += event_len;
4815 data->bytes_written += len;
4816 data->events++;
4817 if (!data->min_size || len < data->min_size)
4818 data->max_size = len;
4819 if (len > data->max_size)
4820 data->max_size = len;
4821 }
4822
4823 out:
4824 ring_buffer_unlock_commit(data->buffer, event);
4825
4826 return 0;
4827}
4828
4829static __init int rb_test(void *arg)
4830{
4831 struct rb_test_data *data = arg;
4832
4833 while (!kthread_should_stop()) {
4834 rb_write_something(data, false);
4835 data->cnt++;
4836
4837 set_current_state(TASK_INTERRUPTIBLE);
4838 /* Now sleep between a min of 100-300us and a max of 1ms */
4839 usleep_range(((data->cnt % 3) + 1) * 100, 1000);
4840 }
4841
4842 return 0;
4843}
4844
4845static __init void rb_ipi(void *ignore)
4846{
4847 struct rb_test_data *data;
4848 int cpu = smp_processor_id();
4849
4850 data = &rb_data[cpu];
4851 rb_write_something(data, true);
4852}
4853
4854static __init int rb_hammer_test(void *arg)
4855{
4856 while (!kthread_should_stop()) {
4857
4858 /* Send an IPI to all cpus to write data! */
4859 smp_call_function(rb_ipi, NULL, 1);
4860 /* No sleep, but for non preempt, let others run */
4861 schedule();
4862 }
4863
4864 return 0;
4865}
4866
4867static __init int test_ringbuffer(void)
4868{
4869 struct task_struct *rb_hammer;
4870 struct ring_buffer *buffer;
4871 int cpu;
4872 int ret = 0;
4873
4874 pr_info("Running ring buffer tests...\n");
4875
4876 buffer = ring_buffer_alloc(RB_TEST_BUFFER_SIZE, RB_FL_OVERWRITE);
4877 if (WARN_ON(!buffer))
4878 return 0;
4879
4880 /* Disable buffer so that threads can't write to it yet */
4881 ring_buffer_record_off(buffer);
4882
4883 for_each_online_cpu(cpu) {
4884 rb_data[cpu].buffer = buffer;
4885 rb_data[cpu].cpu = cpu;
4886 rb_data[cpu].cnt = cpu;
4887 rb_threads[cpu] = kthread_create(rb_test, &rb_data[cpu],
4888 "rbtester/%d", cpu);
Wei Yongjun703cebf2016-06-17 17:33:59 +00004889 if (WARN_ON(IS_ERR(rb_threads[cpu]))) {
Steven Rostedt (Red Hat)6c43e552013-03-15 11:32:53 -04004890 pr_cont("FAILED\n");
Wei Yongjun703cebf2016-06-17 17:33:59 +00004891 ret = PTR_ERR(rb_threads[cpu]);
Steven Rostedt (Red Hat)6c43e552013-03-15 11:32:53 -04004892 goto out_free;
4893 }
4894
4895 kthread_bind(rb_threads[cpu], cpu);
4896 wake_up_process(rb_threads[cpu]);
4897 }
4898
4899 /* Now create the rb hammer! */
4900 rb_hammer = kthread_run(rb_hammer_test, NULL, "rbhammer");
Wei Yongjun703cebf2016-06-17 17:33:59 +00004901 if (WARN_ON(IS_ERR(rb_hammer))) {
Steven Rostedt (Red Hat)6c43e552013-03-15 11:32:53 -04004902 pr_cont("FAILED\n");
Wei Yongjun703cebf2016-06-17 17:33:59 +00004903 ret = PTR_ERR(rb_hammer);
Steven Rostedt (Red Hat)6c43e552013-03-15 11:32:53 -04004904 goto out_free;
4905 }
4906
4907 ring_buffer_record_on(buffer);
4908 /*
4909 * Show buffer is enabled before setting rb_test_started.
4910 * Yes there's a small race window where events could be
4911 * dropped and the thread wont catch it. But when a ring
4912 * buffer gets enabled, there will always be some kind of
4913 * delay before other CPUs see it. Thus, we don't care about
4914 * those dropped events. We care about events dropped after
4915 * the threads see that the buffer is active.
4916 */
4917 smp_wmb();
4918 rb_test_started = true;
4919
4920 set_current_state(TASK_INTERRUPTIBLE);
4921 /* Just run for 10 seconds */;
4922 schedule_timeout(10 * HZ);
4923
4924 kthread_stop(rb_hammer);
4925
4926 out_free:
4927 for_each_online_cpu(cpu) {
4928 if (!rb_threads[cpu])
4929 break;
4930 kthread_stop(rb_threads[cpu]);
4931 }
4932 if (ret) {
4933 ring_buffer_free(buffer);
4934 return ret;
4935 }
4936
4937 /* Report! */
4938 pr_info("finished\n");
4939 for_each_online_cpu(cpu) {
4940 struct ring_buffer_event *event;
4941 struct rb_test_data *data = &rb_data[cpu];
4942 struct rb_item *item;
4943 unsigned long total_events;
4944 unsigned long total_dropped;
4945 unsigned long total_written;
4946 unsigned long total_alloc;
4947 unsigned long total_read = 0;
4948 unsigned long total_size = 0;
4949 unsigned long total_len = 0;
4950 unsigned long total_lost = 0;
4951 unsigned long lost;
4952 int big_event_size;
4953 int small_event_size;
4954
4955 ret = -1;
4956
4957 total_events = data->events + data->events_nested;
4958 total_written = data->bytes_written + data->bytes_written_nested;
4959 total_alloc = data->bytes_alloc + data->bytes_alloc_nested;
4960 total_dropped = data->bytes_dropped + data->bytes_dropped_nested;
4961
4962 big_event_size = data->max_size + data->max_size_nested;
4963 small_event_size = data->min_size + data->min_size_nested;
4964
4965 pr_info("CPU %d:\n", cpu);
4966 pr_info(" events: %ld\n", total_events);
4967 pr_info(" dropped bytes: %ld\n", total_dropped);
4968 pr_info(" alloced bytes: %ld\n", total_alloc);
4969 pr_info(" written bytes: %ld\n", total_written);
4970 pr_info(" biggest event: %d\n", big_event_size);
4971 pr_info(" smallest event: %d\n", small_event_size);
4972
4973 if (RB_WARN_ON(buffer, total_dropped))
4974 break;
4975
4976 ret = 0;
4977
4978 while ((event = ring_buffer_consume(buffer, cpu, NULL, &lost))) {
4979 total_lost += lost;
4980 item = ring_buffer_event_data(event);
4981 total_len += ring_buffer_event_length(event);
4982 total_size += item->size + sizeof(struct rb_item);
4983 if (memcmp(&item->str[0], rb_string, item->size) != 0) {
4984 pr_info("FAILED!\n");
4985 pr_info("buffer had: %.*s\n", item->size, item->str);
4986 pr_info("expected: %.*s\n", item->size, rb_string);
4987 RB_WARN_ON(buffer, 1);
4988 ret = -1;
4989 break;
4990 }
4991 total_read++;
4992 }
4993 if (ret)
4994 break;
4995
4996 ret = -1;
4997
4998 pr_info(" read events: %ld\n", total_read);
4999 pr_info(" lost events: %ld\n", total_lost);
5000 pr_info(" total events: %ld\n", total_lost + total_read);
5001 pr_info(" recorded len bytes: %ld\n", total_len);
5002 pr_info(" recorded size bytes: %ld\n", total_size);
5003 if (total_lost)
5004 pr_info(" With dropped events, record len and size may not match\n"
5005 " alloced and written from above\n");
5006 if (!total_lost) {
5007 if (RB_WARN_ON(buffer, total_len != total_alloc ||
5008 total_size != total_written))
5009 break;
5010 }
5011 if (RB_WARN_ON(buffer, total_lost + total_read != total_events))
5012 break;
5013
5014 ret = 0;
5015 }
5016 if (!ret)
5017 pr_info("Ring buffer PASSED!\n");
5018
5019 ring_buffer_free(buffer);
5020 return 0;
5021}
5022
5023late_initcall(test_ringbuffer);
5024#endif /* CONFIG_RING_BUFFER_STARTUP_TEST */