blob: 2cfe11e1190baae304b28ae9b0efd1fe23635e78 [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);
Peter Zijlstra56f9da82019-04-23 22:03:18 +0200704 preempt_enable_notrace();
Steven Rostedt37886f62009-03-17 17:22:06 -0400705
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 {
Vaibhav Nagarnaikbe910e72018-09-07 15:31:29 -07001507 cond_resched();
1508
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001509 to_remove_page = tmp_iter_page;
1510 rb_inc_page(cpu_buffer, &tmp_iter_page);
1511
1512 /* update the counters */
1513 page_entries = rb_page_entries(to_remove_page);
1514 if (page_entries) {
1515 /*
1516 * If something was added to this page, it was full
1517 * since it is not the tail page. So we deduct the
1518 * bytes consumed in ring buffer from here.
Vaibhav Nagarnaik48fdc722012-06-29 12:31:41 -07001519 * Increment overrun to account for the lost events.
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001520 */
Vaibhav Nagarnaik48fdc722012-06-29 12:31:41 -07001521 local_add(page_entries, &cpu_buffer->overrun);
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001522 local_sub(BUF_PAGE_SIZE, &cpu_buffer->entries_bytes);
1523 }
1524
1525 /*
1526 * We have already removed references to this list item, just
1527 * free up the buffer_page and its page
1528 */
1529 free_buffer_page(to_remove_page);
1530 nr_removed--;
1531
1532 } while (to_remove_page != last_page);
1533
1534 RB_WARN_ON(cpu_buffer, nr_removed);
Vaibhav Nagarnaik5040b4b2012-05-03 18:59:51 -07001535
1536 return nr_removed == 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001537}
1538
Vaibhav Nagarnaik5040b4b2012-05-03 18:59:51 -07001539static int
1540rb_insert_pages(struct ring_buffer_per_cpu *cpu_buffer)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001541{
Vaibhav Nagarnaik5040b4b2012-05-03 18:59:51 -07001542 struct list_head *pages = &cpu_buffer->new_pages;
1543 int retries, success;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001544
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02001545 raw_spin_lock_irq(&cpu_buffer->reader_lock);
Vaibhav Nagarnaik5040b4b2012-05-03 18:59:51 -07001546 /*
1547 * We are holding the reader lock, so the reader page won't be swapped
1548 * in the ring buffer. Now we are racing with the writer trying to
1549 * move head page and the tail page.
1550 * We are going to adapt the reader page update process where:
1551 * 1. We first splice the start and end of list of new pages between
1552 * the head page and its previous page.
1553 * 2. We cmpxchg the prev_page->next to point from head page to the
1554 * start of new pages list.
1555 * 3. Finally, we update the head->prev to the end of new list.
1556 *
1557 * We will try this process 10 times, to make sure that we don't keep
1558 * spinning.
1559 */
1560 retries = 10;
1561 success = 0;
1562 while (retries--) {
1563 struct list_head *head_page, *prev_page, *r;
1564 struct list_head *last_page, *first_page;
1565 struct list_head *head_page_with_bit;
Steven Rostedt77ae3652009-03-27 11:00:29 -04001566
Vaibhav Nagarnaik5040b4b2012-05-03 18:59:51 -07001567 head_page = &rb_set_head_page(cpu_buffer)->list;
Steven Rostedt54f7be52012-11-29 22:27:22 -05001568 if (!head_page)
1569 break;
Vaibhav Nagarnaik5040b4b2012-05-03 18:59:51 -07001570 prev_page = head_page->prev;
1571
1572 first_page = pages->next;
1573 last_page = pages->prev;
1574
1575 head_page_with_bit = (struct list_head *)
1576 ((unsigned long)head_page | RB_PAGE_HEAD);
1577
1578 last_page->next = head_page_with_bit;
1579 first_page->prev = prev_page;
1580
1581 r = cmpxchg(&prev_page->next, head_page_with_bit, first_page);
1582
1583 if (r == head_page_with_bit) {
1584 /*
1585 * yay, we replaced the page pointer to our new list,
1586 * now, we just have to update to head page's prev
1587 * pointer to point to end of list
1588 */
1589 head_page->prev = last_page;
1590 success = 1;
1591 break;
1592 }
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001593 }
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001594
Vaibhav Nagarnaik5040b4b2012-05-03 18:59:51 -07001595 if (success)
1596 INIT_LIST_HEAD(pages);
1597 /*
1598 * If we weren't successful in adding in new pages, warn and stop
1599 * tracing
1600 */
1601 RB_WARN_ON(cpu_buffer, !success);
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02001602 raw_spin_unlock_irq(&cpu_buffer->reader_lock);
Vaibhav Nagarnaik5040b4b2012-05-03 18:59:51 -07001603
1604 /* free pages if they weren't inserted */
1605 if (!success) {
1606 struct buffer_page *bpage, *tmp;
1607 list_for_each_entry_safe(bpage, tmp, &cpu_buffer->new_pages,
1608 list) {
1609 list_del_init(&bpage->list);
1610 free_buffer_page(bpage);
1611 }
1612 }
1613 return success;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001614}
1615
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001616static void rb_update_pages(struct ring_buffer_per_cpu *cpu_buffer)
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001617{
Vaibhav Nagarnaik5040b4b2012-05-03 18:59:51 -07001618 int success;
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001619
Vaibhav Nagarnaik5040b4b2012-05-03 18:59:51 -07001620 if (cpu_buffer->nr_pages_to_update > 0)
1621 success = rb_insert_pages(cpu_buffer);
1622 else
1623 success = rb_remove_pages(cpu_buffer,
1624 -cpu_buffer->nr_pages_to_update);
1625
1626 if (success)
1627 cpu_buffer->nr_pages += cpu_buffer->nr_pages_to_update;
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001628}
1629
1630static void update_pages_handler(struct work_struct *work)
1631{
1632 struct ring_buffer_per_cpu *cpu_buffer = container_of(work,
1633 struct ring_buffer_per_cpu, update_pages_work);
1634 rb_update_pages(cpu_buffer);
Vaibhav Nagarnaik05fdd702012-05-18 13:29:51 -07001635 complete(&cpu_buffer->update_done);
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001636}
1637
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001638/**
1639 * ring_buffer_resize - resize the ring buffer
1640 * @buffer: the buffer to resize.
1641 * @size: the new size.
zhangwei(Jovi)d6118512013-07-15 16:32:50 +08001642 * @cpu_id: the cpu buffer to resize
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001643 *
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001644 * Minimum size is 2 * BUF_PAGE_SIZE.
1645 *
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001646 * Returns 0 on success and < 0 on failure.
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001647 */
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001648int ring_buffer_resize(struct ring_buffer *buffer, unsigned long size,
1649 int cpu_id)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001650{
1651 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedt (Red Hat)9b94a8f2016-05-12 11:01:24 -04001652 unsigned long nr_pages;
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001653 int cpu, err = 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001654
Ingo Molnaree51a1d2008-11-13 14:58:31 +01001655 /*
1656 * Always succeed at resizing a non-existent buffer:
1657 */
1658 if (!buffer)
1659 return size;
1660
Steven Rostedt6a31e1f2012-05-23 15:35:17 -04001661 /* Make sure the requested buffer exists */
1662 if (cpu_id != RING_BUFFER_ALL_CPUS &&
1663 !cpumask_test_cpu(cpu_id, buffer->cpumask))
1664 return size;
1665
Steven Rostedt (Red Hat)59643d12016-05-13 09:34:12 -04001666 nr_pages = DIV_ROUND_UP(size, BUF_PAGE_SIZE);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001667
1668 /* we need a minimum of two pages */
Steven Rostedt (Red Hat)59643d12016-05-13 09:34:12 -04001669 if (nr_pages < 2)
1670 nr_pages = 2;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001671
Steven Rostedt (Red Hat)59643d12016-05-13 09:34:12 -04001672 size = nr_pages * BUF_PAGE_SIZE;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001673
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001674 /*
1675 * Don't succeed if resizing is disabled, as a reader might be
1676 * manipulating the ring buffer and is expecting a sane state while
1677 * this is true.
1678 */
1679 if (atomic_read(&buffer->resize_disabled))
1680 return -EBUSY;
1681
1682 /* prevent another thread from changing buffer sizes */
1683 mutex_lock(&buffer->mutex);
1684
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001685 if (cpu_id == RING_BUFFER_ALL_CPUS) {
1686 /* calculate the pages to update */
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001687 for_each_buffer_cpu(buffer, cpu) {
1688 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001689
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001690 cpu_buffer->nr_pages_to_update = nr_pages -
1691 cpu_buffer->nr_pages;
Vaibhav Nagarnaikd7ec4bf2011-06-07 17:01:42 -07001692 /*
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001693 * nothing more to do for removing pages or no update
Vaibhav Nagarnaikd7ec4bf2011-06-07 17:01:42 -07001694 */
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001695 if (cpu_buffer->nr_pages_to_update <= 0)
1696 continue;
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001697 /*
1698 * to add pages, make sure all new pages can be
1699 * allocated without receiving ENOMEM
1700 */
1701 INIT_LIST_HEAD(&cpu_buffer->new_pages);
1702 if (__rb_allocate_pages(cpu_buffer->nr_pages_to_update,
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001703 &cpu_buffer->new_pages, cpu)) {
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001704 /* not enough memory for new pages */
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001705 err = -ENOMEM;
1706 goto out_err;
1707 }
1708 }
1709
1710 get_online_cpus();
1711 /*
1712 * Fire off all the required work handlers
Vaibhav Nagarnaik05fdd702012-05-18 13:29:51 -07001713 * We can't schedule on offline CPUs, but it's not necessary
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001714 * since we can change their buffer sizes without any race.
1715 */
1716 for_each_buffer_cpu(buffer, cpu) {
1717 cpu_buffer = buffer->buffers[cpu];
Vaibhav Nagarnaik05fdd702012-05-18 13:29:51 -07001718 if (!cpu_buffer->nr_pages_to_update)
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001719 continue;
1720
Corey Minyard021c5b32014-07-16 14:07:13 -05001721 /* Can't run something on an offline CPU. */
1722 if (!cpu_online(cpu)) {
Steven Rostedt (Red Hat)f5eb5582013-03-07 09:27:42 -05001723 rb_update_pages(cpu_buffer);
1724 cpu_buffer->nr_pages_to_update = 0;
1725 } else {
Vaibhav Nagarnaik05fdd702012-05-18 13:29:51 -07001726 schedule_work_on(cpu,
1727 &cpu_buffer->update_pages_work);
Steven Rostedt (Red Hat)f5eb5582013-03-07 09:27:42 -05001728 }
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001729 }
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001730
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001731 /* wait for all the updates to complete */
1732 for_each_buffer_cpu(buffer, cpu) {
1733 cpu_buffer = buffer->buffers[cpu];
Vaibhav Nagarnaik05fdd702012-05-18 13:29:51 -07001734 if (!cpu_buffer->nr_pages_to_update)
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001735 continue;
1736
Vaibhav Nagarnaik05fdd702012-05-18 13:29:51 -07001737 if (cpu_online(cpu))
1738 wait_for_completion(&cpu_buffer->update_done);
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001739 cpu_buffer->nr_pages_to_update = 0;
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001740 }
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001741
1742 put_online_cpus();
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001743 } else {
Vaibhav Nagarnaik8e49f412012-10-10 16:40:27 -07001744 /* Make sure this CPU has been intitialized */
1745 if (!cpumask_test_cpu(cpu_id, buffer->cpumask))
1746 goto out;
1747
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001748 cpu_buffer = buffer->buffers[cpu_id];
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001749
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001750 if (nr_pages == cpu_buffer->nr_pages)
1751 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001752
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001753 cpu_buffer->nr_pages_to_update = nr_pages -
1754 cpu_buffer->nr_pages;
1755
1756 INIT_LIST_HEAD(&cpu_buffer->new_pages);
1757 if (cpu_buffer->nr_pages_to_update > 0 &&
1758 __rb_allocate_pages(cpu_buffer->nr_pages_to_update,
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001759 &cpu_buffer->new_pages, cpu_id)) {
1760 err = -ENOMEM;
1761 goto out_err;
1762 }
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001763
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001764 get_online_cpus();
1765
Corey Minyard021c5b32014-07-16 14:07:13 -05001766 /* Can't run something on an offline CPU. */
1767 if (!cpu_online(cpu_id))
Steven Rostedt (Red Hat)f5eb5582013-03-07 09:27:42 -05001768 rb_update_pages(cpu_buffer);
1769 else {
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001770 schedule_work_on(cpu_id,
1771 &cpu_buffer->update_pages_work);
Vaibhav Nagarnaik05fdd702012-05-18 13:29:51 -07001772 wait_for_completion(&cpu_buffer->update_done);
Steven Rostedt (Red Hat)f5eb5582013-03-07 09:27:42 -05001773 }
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001774
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001775 cpu_buffer->nr_pages_to_update = 0;
Vaibhav Nagarnaik05fdd702012-05-18 13:29:51 -07001776 put_online_cpus();
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001777 }
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001778
1779 out:
Steven Rostedt659f4512012-05-14 17:02:33 -04001780 /*
1781 * The ring buffer resize can happen with the ring buffer
1782 * enabled, so that the update disturbs the tracing as little
1783 * as possible. But if the buffer is disabled, we do not need
1784 * to worry about that, and we can take the time to verify
1785 * that the buffer is not corrupt.
1786 */
1787 if (atomic_read(&buffer->record_disabled)) {
1788 atomic_inc(&buffer->record_disabled);
1789 /*
1790 * Even though the buffer was disabled, we must make sure
1791 * that it is truly disabled before calling rb_check_pages.
1792 * There could have been a race between checking
1793 * record_disable and incrementing it.
1794 */
1795 synchronize_sched();
1796 for_each_buffer_cpu(buffer, cpu) {
1797 cpu_buffer = buffer->buffers[cpu];
1798 rb_check_pages(cpu_buffer);
1799 }
1800 atomic_dec(&buffer->record_disabled);
1801 }
1802
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001803 mutex_unlock(&buffer->mutex);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001804 return size;
1805
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001806 out_err:
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001807 for_each_buffer_cpu(buffer, cpu) {
1808 struct buffer_page *bpage, *tmp;
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001809
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001810 cpu_buffer = buffer->buffers[cpu];
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001811 cpu_buffer->nr_pages_to_update = 0;
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001812
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001813 if (list_empty(&cpu_buffer->new_pages))
1814 continue;
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001815
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001816 list_for_each_entry_safe(bpage, tmp, &cpu_buffer->new_pages,
1817 list) {
1818 list_del_init(&bpage->list);
1819 free_buffer_page(bpage);
1820 }
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001821 }
Vegard Nossum641d2f62008-11-18 19:22:13 +01001822 mutex_unlock(&buffer->mutex);
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001823 return err;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001824}
Robert Richterc4f50182008-12-11 16:49:22 +01001825EXPORT_SYMBOL_GPL(ring_buffer_resize);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001826
David Sharp750912f2010-12-08 13:46:47 -08001827void ring_buffer_change_overwrite(struct ring_buffer *buffer, int val)
1828{
1829 mutex_lock(&buffer->mutex);
1830 if (val)
1831 buffer->flags |= RB_FL_OVERWRITE;
1832 else
1833 buffer->flags &= ~RB_FL_OVERWRITE;
1834 mutex_unlock(&buffer->mutex);
1835}
1836EXPORT_SYMBOL_GPL(ring_buffer_change_overwrite);
1837
Steven Rostedt8789a9e2008-12-02 15:34:07 -05001838static inline void *
Steven Rostedt044fa782008-12-02 23:50:03 -05001839__rb_data_page_index(struct buffer_data_page *bpage, unsigned index)
Steven Rostedt8789a9e2008-12-02 15:34:07 -05001840{
Steven Rostedt044fa782008-12-02 23:50:03 -05001841 return bpage->data + index;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05001842}
1843
Steven Rostedt044fa782008-12-02 23:50:03 -05001844static inline void *__rb_page_index(struct buffer_page *bpage, unsigned index)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001845{
Steven Rostedt044fa782008-12-02 23:50:03 -05001846 return bpage->page->data + index;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001847}
1848
1849static inline struct ring_buffer_event *
Steven Rostedtd7690412008-10-01 00:29:53 -04001850rb_reader_event(struct ring_buffer_per_cpu *cpu_buffer)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001851{
Steven Rostedt6f807ac2008-10-04 02:00:58 -04001852 return __rb_page_index(cpu_buffer->reader_page,
1853 cpu_buffer->reader_page->read);
1854}
1855
1856static inline struct ring_buffer_event *
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001857rb_iter_head_event(struct ring_buffer_iter *iter)
1858{
Steven Rostedt6f807ac2008-10-04 02:00:58 -04001859 return __rb_page_index(iter->head_page, iter->head);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001860}
1861
Steven Rostedtbf41a152008-10-04 02:00:59 -04001862static inline unsigned rb_page_commit(struct buffer_page *bpage)
1863{
Steven Rostedtabc9b562008-12-02 15:34:06 -05001864 return local_read(&bpage->page->commit);
Steven Rostedtbf41a152008-10-04 02:00:59 -04001865}
1866
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001867/* Size is determined by what has been committed */
Steven Rostedtbf41a152008-10-04 02:00:59 -04001868static inline unsigned rb_page_size(struct buffer_page *bpage)
1869{
1870 return rb_page_commit(bpage);
1871}
1872
1873static inline unsigned
1874rb_commit_index(struct ring_buffer_per_cpu *cpu_buffer)
1875{
1876 return rb_page_commit(cpu_buffer->commit_page);
1877}
1878
Steven Rostedtbf41a152008-10-04 02:00:59 -04001879static inline unsigned
1880rb_event_index(struct ring_buffer_event *event)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001881{
Steven Rostedtbf41a152008-10-04 02:00:59 -04001882 unsigned long addr = (unsigned long)event;
1883
Steven Rostedt22f470f2009-06-11 09:29:58 -04001884 return (addr & ~PAGE_MASK) - BUF_PAGE_HDR_SIZE;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001885}
1886
Andrew Morton34a148b2009-01-09 12:27:09 -08001887static void rb_inc_iter(struct ring_buffer_iter *iter)
Steven Rostedtd7690412008-10-01 00:29:53 -04001888{
1889 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
1890
1891 /*
1892 * The iterator could be on the reader page (it starts there).
1893 * But the head could have moved, since the reader was
1894 * found. Check for this case and assign the iterator
1895 * to the head page instead of next.
1896 */
1897 if (iter->head_page == cpu_buffer->reader_page)
Steven Rostedt77ae3652009-03-27 11:00:29 -04001898 iter->head_page = rb_set_head_page(cpu_buffer);
Steven Rostedtd7690412008-10-01 00:29:53 -04001899 else
1900 rb_inc_page(cpu_buffer, &iter->head_page);
1901
Steven Rostedtabc9b562008-12-02 15:34:06 -05001902 iter->read_stamp = iter->head_page->page->time_stamp;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001903 iter->head = 0;
1904}
1905
Steven Rostedt77ae3652009-03-27 11:00:29 -04001906/*
1907 * rb_handle_head_page - writer hit the head page
1908 *
1909 * Returns: +1 to retry page
1910 * 0 to continue
1911 * -1 on error
1912 */
1913static int
1914rb_handle_head_page(struct ring_buffer_per_cpu *cpu_buffer,
1915 struct buffer_page *tail_page,
1916 struct buffer_page *next_page)
1917{
1918 struct buffer_page *new_head;
1919 int entries;
1920 int type;
1921 int ret;
1922
1923 entries = rb_page_entries(next_page);
1924
1925 /*
1926 * The hard part is here. We need to move the head
1927 * forward, and protect against both readers on
1928 * other CPUs and writers coming in via interrupts.
1929 */
1930 type = rb_head_page_set_update(cpu_buffer, next_page, tail_page,
1931 RB_PAGE_HEAD);
1932
1933 /*
1934 * type can be one of four:
1935 * NORMAL - an interrupt already moved it for us
1936 * HEAD - we are the first to get here.
1937 * UPDATE - we are the interrupt interrupting
1938 * a current move.
1939 * MOVED - a reader on another CPU moved the next
1940 * pointer to its reader page. Give up
1941 * and try again.
1942 */
1943
1944 switch (type) {
1945 case RB_PAGE_HEAD:
1946 /*
1947 * We changed the head to UPDATE, thus
1948 * it is our responsibility to update
1949 * the counters.
1950 */
1951 local_add(entries, &cpu_buffer->overrun);
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -07001952 local_sub(BUF_PAGE_SIZE, &cpu_buffer->entries_bytes);
Steven Rostedt77ae3652009-03-27 11:00:29 -04001953
1954 /*
1955 * The entries will be zeroed out when we move the
1956 * tail page.
1957 */
1958
1959 /* still more to do */
1960 break;
1961
1962 case RB_PAGE_UPDATE:
1963 /*
1964 * This is an interrupt that interrupt the
1965 * previous update. Still more to do.
1966 */
1967 break;
1968 case RB_PAGE_NORMAL:
1969 /*
1970 * An interrupt came in before the update
1971 * and processed this for us.
1972 * Nothing left to do.
1973 */
1974 return 1;
1975 case RB_PAGE_MOVED:
1976 /*
1977 * The reader is on another CPU and just did
1978 * a swap with our next_page.
1979 * Try again.
1980 */
1981 return 1;
1982 default:
1983 RB_WARN_ON(cpu_buffer, 1); /* WTF??? */
1984 return -1;
1985 }
1986
1987 /*
1988 * Now that we are here, the old head pointer is
1989 * set to UPDATE. This will keep the reader from
1990 * swapping the head page with the reader page.
1991 * The reader (on another CPU) will spin till
1992 * we are finished.
1993 *
1994 * We just need to protect against interrupts
1995 * doing the job. We will set the next pointer
1996 * to HEAD. After that, we set the old pointer
1997 * to NORMAL, but only if it was HEAD before.
1998 * otherwise we are an interrupt, and only
1999 * want the outer most commit to reset it.
2000 */
2001 new_head = next_page;
2002 rb_inc_page(cpu_buffer, &new_head);
2003
2004 ret = rb_head_page_set_head(cpu_buffer, new_head, next_page,
2005 RB_PAGE_NORMAL);
2006
2007 /*
2008 * Valid returns are:
2009 * HEAD - an interrupt came in and already set it.
2010 * NORMAL - One of two things:
2011 * 1) We really set it.
2012 * 2) A bunch of interrupts came in and moved
2013 * the page forward again.
2014 */
2015 switch (ret) {
2016 case RB_PAGE_HEAD:
2017 case RB_PAGE_NORMAL:
2018 /* OK */
2019 break;
2020 default:
2021 RB_WARN_ON(cpu_buffer, 1);
2022 return -1;
2023 }
2024
2025 /*
2026 * It is possible that an interrupt came in,
2027 * set the head up, then more interrupts came in
2028 * and moved it again. When we get back here,
2029 * the page would have been set to NORMAL but we
2030 * just set it back to HEAD.
2031 *
2032 * How do you detect this? Well, if that happened
2033 * the tail page would have moved.
2034 */
2035 if (ret == RB_PAGE_NORMAL) {
Steven Rostedt (Red Hat)85736362015-11-17 14:03:11 -05002036 struct buffer_page *buffer_tail_page;
2037
2038 buffer_tail_page = READ_ONCE(cpu_buffer->tail_page);
Steven Rostedt77ae3652009-03-27 11:00:29 -04002039 /*
2040 * If the tail had moved passed next, then we need
2041 * to reset the pointer.
2042 */
Steven Rostedt (Red Hat)85736362015-11-17 14:03:11 -05002043 if (buffer_tail_page != tail_page &&
2044 buffer_tail_page != next_page)
Steven Rostedt77ae3652009-03-27 11:00:29 -04002045 rb_head_page_set_normal(cpu_buffer, new_head,
2046 next_page,
2047 RB_PAGE_HEAD);
2048 }
2049
2050 /*
2051 * If this was the outer most commit (the one that
2052 * changed the original pointer from HEAD to UPDATE),
2053 * then it is up to us to reset it to NORMAL.
2054 */
2055 if (type == RB_PAGE_HEAD) {
2056 ret = rb_head_page_set_normal(cpu_buffer, next_page,
2057 tail_page,
2058 RB_PAGE_UPDATE);
2059 if (RB_WARN_ON(cpu_buffer,
2060 ret != RB_PAGE_UPDATE))
2061 return -1;
2062 }
2063
2064 return 0;
2065}
2066
Steven Rostedtc7b09302009-06-11 11:12:00 -04002067static inline void
2068rb_reset_tail(struct ring_buffer_per_cpu *cpu_buffer,
Steven Rostedt (Red Hat)fcc742e2015-05-28 17:13:14 -04002069 unsigned long tail, struct rb_event_info *info)
Steven Rostedtc7b09302009-06-11 11:12:00 -04002070{
Steven Rostedt (Red Hat)fcc742e2015-05-28 17:13:14 -04002071 struct buffer_page *tail_page = info->tail_page;
Steven Rostedtc7b09302009-06-11 11:12:00 -04002072 struct ring_buffer_event *event;
Steven Rostedt (Red Hat)fcc742e2015-05-28 17:13:14 -04002073 unsigned long length = info->length;
Steven Rostedtc7b09302009-06-11 11:12:00 -04002074
2075 /*
2076 * Only the event that crossed the page boundary
2077 * must fill the old tail_page with padding.
2078 */
2079 if (tail >= BUF_PAGE_SIZE) {
Steven Rostedtb3230c82010-05-21 11:55:21 -04002080 /*
2081 * If the page was filled, then we still need
2082 * to update the real_end. Reset it to zero
2083 * and the reader will ignore it.
2084 */
2085 if (tail == BUF_PAGE_SIZE)
2086 tail_page->real_end = 0;
2087
Steven Rostedtc7b09302009-06-11 11:12:00 -04002088 local_sub(length, &tail_page->write);
2089 return;
2090 }
2091
2092 event = __rb_page_index(tail_page, tail);
Linus Torvaldsb0b70652009-06-20 10:56:46 -07002093 kmemcheck_annotate_bitfield(event, bitfield);
Steven Rostedtc7b09302009-06-11 11:12:00 -04002094
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -07002095 /* account for padding bytes */
2096 local_add(BUF_PAGE_SIZE - tail, &cpu_buffer->entries_bytes);
2097
Steven Rostedtc7b09302009-06-11 11:12:00 -04002098 /*
Steven Rostedtff0ff842010-03-31 22:11:42 -04002099 * Save the original length to the meta data.
2100 * This will be used by the reader to add lost event
2101 * counter.
2102 */
2103 tail_page->real_end = tail;
2104
2105 /*
Steven Rostedtc7b09302009-06-11 11:12:00 -04002106 * If this event is bigger than the minimum size, then
2107 * we need to be careful that we don't subtract the
2108 * write counter enough to allow another writer to slip
2109 * in on this page.
2110 * We put in a discarded commit instead, to make sure
2111 * that this space is not used again.
2112 *
2113 * If we are less than the minimum size, we don't need to
2114 * worry about it.
2115 */
2116 if (tail > (BUF_PAGE_SIZE - RB_EVNT_MIN_SIZE)) {
2117 /* No room for any events */
2118
2119 /* Mark the rest of the page with padding */
2120 rb_event_set_padding(event);
2121
2122 /* Set the write back to the previous setting */
2123 local_sub(length, &tail_page->write);
2124 return;
2125 }
2126
2127 /* Put in a discarded event */
2128 event->array[0] = (BUF_PAGE_SIZE - tail) - RB_EVNT_HDR_SIZE;
2129 event->type_len = RINGBUF_TYPE_PADDING;
2130 /* time delta must be non zero */
2131 event->time_delta = 1;
Steven Rostedtc7b09302009-06-11 11:12:00 -04002132
2133 /* Set write to end of buffer */
2134 length = (tail + length) - BUF_PAGE_SIZE;
2135 local_sub(length, &tail_page->write);
2136}
Steven Rostedt6634ff22009-05-06 15:30:07 -04002137
Steven Rostedt (Red Hat)4239c382015-11-17 16:36:06 -05002138static inline void rb_end_commit(struct ring_buffer_per_cpu *cpu_buffer);
2139
Steven Rostedt747e94a2010-10-08 13:51:48 -04002140/*
2141 * This is the slow path, force gcc not to inline it.
2142 */
2143static noinline struct ring_buffer_event *
Steven Rostedt6634ff22009-05-06 15:30:07 -04002144rb_move_tail(struct ring_buffer_per_cpu *cpu_buffer,
Steven Rostedt (Red Hat)fcc742e2015-05-28 17:13:14 -04002145 unsigned long tail, struct rb_event_info *info)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002146{
Steven Rostedt (Red Hat)fcc742e2015-05-28 17:13:14 -04002147 struct buffer_page *tail_page = info->tail_page;
Steven Rostedt5a50e332009-11-17 08:43:01 -05002148 struct buffer_page *commit_page = cpu_buffer->commit_page;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002149 struct ring_buffer *buffer = cpu_buffer->buffer;
Steven Rostedt77ae3652009-03-27 11:00:29 -04002150 struct buffer_page *next_page;
2151 int ret;
Steven Rostedtaa20ae82009-05-05 21:16:11 -04002152
2153 next_page = tail_page;
2154
Steven Rostedtaa20ae82009-05-05 21:16:11 -04002155 rb_inc_page(cpu_buffer, &next_page);
2156
Steven Rostedtaa20ae82009-05-05 21:16:11 -04002157 /*
2158 * If for some reason, we had an interrupt storm that made
2159 * it all the way around the buffer, bail, and warn
2160 * about it.
2161 */
2162 if (unlikely(next_page == commit_page)) {
Steven Rostedt77ae3652009-03-27 11:00:29 -04002163 local_inc(&cpu_buffer->commit_overrun);
Steven Rostedtaa20ae82009-05-05 21:16:11 -04002164 goto out_reset;
2165 }
2166
Steven Rostedt77ae3652009-03-27 11:00:29 -04002167 /*
2168 * This is where the fun begins!
2169 *
2170 * We are fighting against races between a reader that
2171 * could be on another CPU trying to swap its reader
2172 * page with the buffer head.
2173 *
2174 * We are also fighting against interrupts coming in and
2175 * moving the head or tail on us as well.
2176 *
2177 * If the next page is the head page then we have filled
2178 * the buffer, unless the commit page is still on the
2179 * reader page.
2180 */
2181 if (rb_is_head_page(cpu_buffer, next_page, &tail_page->list)) {
Steven Rostedtaa20ae82009-05-05 21:16:11 -04002182
Steven Rostedt77ae3652009-03-27 11:00:29 -04002183 /*
2184 * If the commit is not on the reader page, then
2185 * move the header page.
2186 */
2187 if (!rb_is_reader_page(cpu_buffer->commit_page)) {
2188 /*
2189 * If we are not in overwrite mode,
2190 * this is easy, just stop here.
2191 */
Slava Pestov884bfe82011-07-15 14:23:58 -07002192 if (!(buffer->flags & RB_FL_OVERWRITE)) {
2193 local_inc(&cpu_buffer->dropped_events);
Steven Rostedt77ae3652009-03-27 11:00:29 -04002194 goto out_reset;
Slava Pestov884bfe82011-07-15 14:23:58 -07002195 }
Steven Rostedtaa20ae82009-05-05 21:16:11 -04002196
Steven Rostedt77ae3652009-03-27 11:00:29 -04002197 ret = rb_handle_head_page(cpu_buffer,
2198 tail_page,
2199 next_page);
2200 if (ret < 0)
2201 goto out_reset;
2202 if (ret)
2203 goto out_again;
2204 } else {
2205 /*
2206 * We need to be careful here too. The
2207 * commit page could still be on the reader
2208 * page. We could have a small buffer, and
2209 * have filled up the buffer with events
2210 * from interrupts and such, and wrapped.
2211 *
2212 * Note, if the tail page is also the on the
2213 * reader_page, we let it move out.
2214 */
2215 if (unlikely((cpu_buffer->commit_page !=
2216 cpu_buffer->tail_page) &&
2217 (cpu_buffer->commit_page ==
2218 cpu_buffer->reader_page))) {
2219 local_inc(&cpu_buffer->commit_overrun);
2220 goto out_reset;
2221 }
Steven Rostedtaa20ae82009-05-05 21:16:11 -04002222 }
2223 }
2224
Steven Rostedt (Red Hat)70004982015-11-17 15:15:19 -05002225 rb_tail_page_update(cpu_buffer, tail_page, next_page);
Steven Rostedtaa20ae82009-05-05 21:16:11 -04002226
Steven Rostedt77ae3652009-03-27 11:00:29 -04002227 out_again:
Steven Rostedtaa20ae82009-05-05 21:16:11 -04002228
Steven Rostedt (Red Hat)fcc742e2015-05-28 17:13:14 -04002229 rb_reset_tail(cpu_buffer, tail, info);
Steven Rostedtaa20ae82009-05-05 21:16:11 -04002230
Steven Rostedt (Red Hat)4239c382015-11-17 16:36:06 -05002231 /* Commit what we have for now. */
2232 rb_end_commit(cpu_buffer);
2233 /* rb_end_commit() decs committing */
2234 local_inc(&cpu_buffer->committing);
2235
Steven Rostedtaa20ae82009-05-05 21:16:11 -04002236 /* fail and let the caller try again */
2237 return ERR_PTR(-EAGAIN);
2238
Steven Rostedt45141d42009-02-12 13:19:48 -05002239 out_reset:
Lai Jiangshan6f3b3442009-01-12 11:06:18 +08002240 /* reset write */
Steven Rostedt (Red Hat)fcc742e2015-05-28 17:13:14 -04002241 rb_reset_tail(cpu_buffer, tail, info);
Lai Jiangshan6f3b3442009-01-12 11:06:18 +08002242
Steven Rostedtbf41a152008-10-04 02:00:59 -04002243 return NULL;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002244}
2245
Steven Rostedt (Red Hat)d90fd772015-05-29 12:12:27 -04002246/* Slow path, do not inline */
2247static noinline struct ring_buffer_event *
2248rb_add_time_stamp(struct ring_buffer_event *event, u64 delta)
2249{
2250 event->type_len = RINGBUF_TYPE_TIME_EXTEND;
2251
2252 /* Not the first event on the page? */
2253 if (rb_event_index(event)) {
2254 event->time_delta = delta & TS_MASK;
2255 event->array[0] = delta >> TS_SHIFT;
2256 } else {
2257 /* nope, just zero it */
2258 event->time_delta = 0;
2259 event->array[0] = 0;
2260 }
2261
2262 return skip_time_extend(event);
2263}
2264
Yaowei Baicdb2a0a2015-09-29 22:43:34 +08002265static inline bool rb_event_is_commit(struct ring_buffer_per_cpu *cpu_buffer,
Steven Rostedt (Red Hat)b7dc42f2015-09-03 08:57:12 -04002266 struct ring_buffer_event *event);
2267
Steven Rostedt (Red Hat)d90fd772015-05-29 12:12:27 -04002268/**
2269 * rb_update_event - update event type and data
2270 * @event: the event to update
2271 * @type: the type of event
2272 * @length: the size of the event field in the ring buffer
2273 *
2274 * Update the type and data fields of the event. The length
2275 * is the actual size that is written to the ring buffer,
2276 * and with this, we can determine what to place into the
2277 * data field.
2278 */
Steven Rostedt (Red Hat)b7dc42f2015-09-03 08:57:12 -04002279static void
Steven Rostedt (Red Hat)d90fd772015-05-29 12:12:27 -04002280rb_update_event(struct ring_buffer_per_cpu *cpu_buffer,
2281 struct ring_buffer_event *event,
2282 struct rb_event_info *info)
2283{
2284 unsigned length = info->length;
2285 u64 delta = info->delta;
2286
Steven Rostedt (Red Hat)b7dc42f2015-09-03 08:57:12 -04002287 /* Only a commit updates the timestamp */
2288 if (unlikely(!rb_event_is_commit(cpu_buffer, event)))
2289 delta = 0;
2290
Steven Rostedt (Red Hat)d90fd772015-05-29 12:12:27 -04002291 /*
2292 * If we need to add a timestamp, then we
2293 * add it to the start of the resevered space.
2294 */
2295 if (unlikely(info->add_timestamp)) {
2296 event = rb_add_time_stamp(event, delta);
2297 length -= RB_LEN_TIME_EXTEND;
2298 delta = 0;
2299 }
2300
2301 event->time_delta = delta;
2302 length -= RB_EVNT_HDR_SIZE;
2303 if (length > RB_MAX_SMALL_DATA || RB_FORCE_8BYTE_ALIGNMENT) {
2304 event->type_len = 0;
2305 event->array[0] = length;
2306 } else
2307 event->type_len = DIV_ROUND_UP(length, RB_ALIGNMENT);
2308}
2309
2310static unsigned rb_calculate_event_length(unsigned length)
2311{
2312 struct ring_buffer_event event; /* Used only for sizeof array */
2313
2314 /* zero length can cause confusions */
2315 if (!length)
2316 length++;
2317
2318 if (length > RB_MAX_SMALL_DATA || RB_FORCE_8BYTE_ALIGNMENT)
2319 length += sizeof(event.array[0]);
2320
2321 length += RB_EVNT_HDR_SIZE;
2322 length = ALIGN(length, RB_ARCH_ALIGNMENT);
2323
2324 /*
2325 * In case the time delta is larger than the 27 bits for it
2326 * in the header, we need to add a timestamp. If another
2327 * event comes in when trying to discard this one to increase
2328 * the length, then the timestamp will be added in the allocated
2329 * space of this event. If length is bigger than the size needed
2330 * for the TIME_EXTEND, then padding has to be used. The events
2331 * length must be either RB_LEN_TIME_EXTEND, or greater than or equal
2332 * to RB_LEN_TIME_EXTEND + 8, as 8 is the minimum size for padding.
2333 * As length is a multiple of 4, we only need to worry if it
2334 * is 12 (RB_LEN_TIME_EXTEND + 4).
2335 */
2336 if (length == RB_LEN_TIME_EXTEND + RB_ALIGNMENT)
2337 length += RB_ALIGNMENT;
2338
2339 return length;
2340}
2341
Steven Rostedt (Red Hat)9826b272015-05-28 17:36:45 -04002342#ifndef CONFIG_HAVE_UNSTABLE_SCHED_CLOCK
2343static inline bool sched_clock_stable(void)
2344{
2345 return true;
2346}
2347#endif
2348
Steven Rostedt (Red Hat)a4543a22015-05-29 09:40:18 -04002349static inline int
2350rb_try_to_discard(struct ring_buffer_per_cpu *cpu_buffer,
Steven Rostedt (Red Hat)d90fd772015-05-29 12:12:27 -04002351 struct ring_buffer_event *event)
2352{
2353 unsigned long new_index, old_index;
2354 struct buffer_page *bpage;
2355 unsigned long index;
2356 unsigned long addr;
2357
2358 new_index = rb_event_index(event);
2359 old_index = new_index + rb_event_ts_length(event);
2360 addr = (unsigned long)event;
2361 addr &= PAGE_MASK;
2362
Steven Rostedt (Red Hat)85736362015-11-17 14:03:11 -05002363 bpage = READ_ONCE(cpu_buffer->tail_page);
Steven Rostedt (Red Hat)d90fd772015-05-29 12:12:27 -04002364
2365 if (bpage->page == (void *)addr && rb_page_write(bpage) == old_index) {
2366 unsigned long write_mask =
2367 local_read(&bpage->write) & ~RB_WRITE_MASK;
2368 unsigned long event_length = rb_event_length(event);
2369 /*
2370 * This is on the tail page. It is possible that
2371 * a write could come in and move the tail page
2372 * and write to the next page. That is fine
2373 * because we just shorten what is on this page.
2374 */
2375 old_index += write_mask;
2376 new_index += write_mask;
2377 index = local_cmpxchg(&bpage->write, old_index, new_index);
2378 if (index == old_index) {
2379 /* update counters */
2380 local_sub(event_length, &cpu_buffer->entries_bytes);
2381 return 1;
2382 }
2383 }
2384
2385 /* could not discard */
2386 return 0;
2387}
2388
2389static void rb_start_commit(struct ring_buffer_per_cpu *cpu_buffer)
2390{
2391 local_inc(&cpu_buffer->committing);
2392 local_inc(&cpu_buffer->commits);
2393}
2394
2395static void
2396rb_set_commit_to_write(struct ring_buffer_per_cpu *cpu_buffer)
2397{
2398 unsigned long max_count;
2399
2400 /*
2401 * We only race with interrupts and NMIs on this CPU.
2402 * If we own the commit event, then we can commit
2403 * all others that interrupted us, since the interruptions
2404 * are in stack format (they finish before they come
2405 * back to us). This allows us to do a simple loop to
2406 * assign the commit to the tail.
2407 */
2408 again:
2409 max_count = cpu_buffer->nr_pages * 100;
2410
Steven Rostedt (Red Hat)85736362015-11-17 14:03:11 -05002411 while (cpu_buffer->commit_page != READ_ONCE(cpu_buffer->tail_page)) {
Steven Rostedt (Red Hat)d90fd772015-05-29 12:12:27 -04002412 if (RB_WARN_ON(cpu_buffer, !(--max_count)))
2413 return;
2414 if (RB_WARN_ON(cpu_buffer,
2415 rb_is_reader_page(cpu_buffer->tail_page)))
2416 return;
2417 local_set(&cpu_buffer->commit_page->page->commit,
2418 rb_page_write(cpu_buffer->commit_page));
2419 rb_inc_page(cpu_buffer, &cpu_buffer->commit_page);
Steven Rostedt (Red Hat)70004982015-11-17 15:15:19 -05002420 /* Only update the write stamp if the page has an event */
2421 if (rb_page_write(cpu_buffer->commit_page))
2422 cpu_buffer->write_stamp =
2423 cpu_buffer->commit_page->page->time_stamp;
Steven Rostedt (Red Hat)d90fd772015-05-29 12:12:27 -04002424 /* add barrier to keep gcc from optimizing too much */
2425 barrier();
2426 }
2427 while (rb_commit_index(cpu_buffer) !=
2428 rb_page_write(cpu_buffer->commit_page)) {
2429
2430 local_set(&cpu_buffer->commit_page->page->commit,
2431 rb_page_write(cpu_buffer->commit_page));
2432 RB_WARN_ON(cpu_buffer,
2433 local_read(&cpu_buffer->commit_page->page->commit) &
2434 ~RB_WRITE_MASK);
2435 barrier();
2436 }
2437
2438 /* again, keep gcc from optimizing */
2439 barrier();
2440
2441 /*
2442 * If an interrupt came in just after the first while loop
2443 * and pushed the tail page forward, we will be left with
2444 * a dangling commit that will never go forward.
2445 */
Steven Rostedt (Red Hat)85736362015-11-17 14:03:11 -05002446 if (unlikely(cpu_buffer->commit_page != READ_ONCE(cpu_buffer->tail_page)))
Steven Rostedt (Red Hat)d90fd772015-05-29 12:12:27 -04002447 goto again;
2448}
2449
2450static inline void rb_end_commit(struct ring_buffer_per_cpu *cpu_buffer)
2451{
2452 unsigned long commits;
2453
2454 if (RB_WARN_ON(cpu_buffer,
2455 !local_read(&cpu_buffer->committing)))
2456 return;
2457
2458 again:
2459 commits = local_read(&cpu_buffer->commits);
2460 /* synchronize with interrupts */
2461 barrier();
2462 if (local_read(&cpu_buffer->committing) == 1)
2463 rb_set_commit_to_write(cpu_buffer);
2464
2465 local_dec(&cpu_buffer->committing);
2466
2467 /* synchronize with interrupts */
2468 barrier();
2469
2470 /*
2471 * Need to account for interrupts coming in between the
2472 * updating of the commit page and the clearing of the
2473 * committing counter.
2474 */
2475 if (unlikely(local_read(&cpu_buffer->commits) != commits) &&
2476 !local_read(&cpu_buffer->committing)) {
2477 local_inc(&cpu_buffer->committing);
2478 goto again;
2479 }
2480}
2481
2482static inline void rb_event_discard(struct ring_buffer_event *event)
2483{
2484 if (event->type_len == RINGBUF_TYPE_TIME_EXTEND)
2485 event = skip_time_extend(event);
2486
2487 /* array[0] holds the actual length for the discarded event */
2488 event->array[0] = rb_event_data_length(event) - RB_EVNT_HDR_SIZE;
2489 event->type_len = RINGBUF_TYPE_PADDING;
2490 /* time delta must be non zero */
2491 if (!event->time_delta)
2492 event->time_delta = 1;
2493}
2494
Yaowei Baicdb2a0a2015-09-29 22:43:34 +08002495static inline bool
Steven Rostedt (Red Hat)d90fd772015-05-29 12:12:27 -04002496rb_event_is_commit(struct ring_buffer_per_cpu *cpu_buffer,
2497 struct ring_buffer_event *event)
2498{
2499 unsigned long addr = (unsigned long)event;
2500 unsigned long index;
2501
2502 index = rb_event_index(event);
2503 addr &= PAGE_MASK;
2504
2505 return cpu_buffer->commit_page->page == (void *)addr &&
2506 rb_commit_index(cpu_buffer) == index;
2507}
2508
Steven Rostedt (Red Hat)a4543a22015-05-29 09:40:18 -04002509static void
2510rb_update_write_stamp(struct ring_buffer_per_cpu *cpu_buffer,
Steven Rostedt (Red Hat)d90fd772015-05-29 12:12:27 -04002511 struct ring_buffer_event *event)
2512{
2513 u64 delta;
2514
2515 /*
2516 * The event first in the commit queue updates the
2517 * time stamp.
2518 */
2519 if (rb_event_is_commit(cpu_buffer, event)) {
2520 /*
2521 * A commit event that is first on a page
2522 * updates the write timestamp with the page stamp
2523 */
2524 if (!rb_event_index(event))
2525 cpu_buffer->write_stamp =
2526 cpu_buffer->commit_page->page->time_stamp;
2527 else if (event->type_len == RINGBUF_TYPE_TIME_EXTEND) {
2528 delta = event->array[0];
2529 delta <<= TS_SHIFT;
2530 delta += event->time_delta;
2531 cpu_buffer->write_stamp += delta;
2532 } else
2533 cpu_buffer->write_stamp += event->time_delta;
2534 }
2535}
2536
2537static void rb_commit(struct ring_buffer_per_cpu *cpu_buffer,
2538 struct ring_buffer_event *event)
2539{
2540 local_inc(&cpu_buffer->entries);
2541 rb_update_write_stamp(cpu_buffer, event);
2542 rb_end_commit(cpu_buffer);
2543}
2544
2545static __always_inline void
2546rb_wakeups(struct ring_buffer *buffer, struct ring_buffer_per_cpu *cpu_buffer)
2547{
2548 bool pagebusy;
2549
2550 if (buffer->irq_work.waiters_pending) {
2551 buffer->irq_work.waiters_pending = false;
2552 /* irq_work_queue() supplies it's own memory barriers */
2553 irq_work_queue(&buffer->irq_work.work);
2554 }
2555
2556 if (cpu_buffer->irq_work.waiters_pending) {
2557 cpu_buffer->irq_work.waiters_pending = false;
2558 /* irq_work_queue() supplies it's own memory barriers */
2559 irq_work_queue(&cpu_buffer->irq_work.work);
2560 }
2561
2562 pagebusy = cpu_buffer->reader_page == cpu_buffer->commit_page;
2563
2564 if (!pagebusy && cpu_buffer->irq_work.full_waiters_pending) {
2565 cpu_buffer->irq_work.wakeup_full = true;
2566 cpu_buffer->irq_work.full_waiters_pending = false;
2567 /* irq_work_queue() supplies it's own memory barriers */
2568 irq_work_queue(&cpu_buffer->irq_work.work);
2569 }
2570}
2571
2572/*
2573 * The lock and unlock are done within a preempt disable section.
2574 * The current_context per_cpu variable can only be modified
2575 * by the current task between lock and unlock. But it can
2576 * be modified more than once via an interrupt. To pass this
2577 * information from the lock to the unlock without having to
2578 * access the 'in_interrupt()' functions again (which do show
2579 * a bit of overhead in something as critical as function tracing,
2580 * we use a bitmask trick.
2581 *
2582 * bit 0 = NMI context
2583 * bit 1 = IRQ context
2584 * bit 2 = SoftIRQ context
2585 * bit 3 = normal context.
2586 *
2587 * This works because this is the order of contexts that can
2588 * preempt other contexts. A SoftIRQ never preempts an IRQ
2589 * context.
2590 *
2591 * When the context is determined, the corresponding bit is
2592 * checked and set (if it was set, then a recursion of that context
2593 * happened).
2594 *
2595 * On unlock, we need to clear this bit. To do so, just subtract
2596 * 1 from the current_context and AND it to itself.
2597 *
2598 * (binary)
2599 * 101 - 1 = 100
2600 * 101 & 100 = 100 (clearing bit zero)
2601 *
2602 * 1010 - 1 = 1001
2603 * 1010 & 1001 = 1000 (clearing bit 1)
2604 *
2605 * The least significant bit can be cleared this way, and it
2606 * just so happens that it is the same bit corresponding to
2607 * the current context.
2608 */
2609
2610static __always_inline int
2611trace_recursive_lock(struct ring_buffer_per_cpu *cpu_buffer)
2612{
2613 unsigned int val = cpu_buffer->current_context;
2614 int bit;
2615
2616 if (in_interrupt()) {
2617 if (in_nmi())
2618 bit = RB_CTX_NMI;
2619 else if (in_irq())
2620 bit = RB_CTX_IRQ;
2621 else
2622 bit = RB_CTX_SOFTIRQ;
2623 } else
2624 bit = RB_CTX_NORMAL;
2625
2626 if (unlikely(val & (1 << bit)))
2627 return 1;
2628
2629 val |= (1 << bit);
2630 cpu_buffer->current_context = val;
2631
2632 return 0;
2633}
2634
2635static __always_inline void
2636trace_recursive_unlock(struct ring_buffer_per_cpu *cpu_buffer)
2637{
2638 cpu_buffer->current_context &= cpu_buffer->current_context - 1;
2639}
2640
2641/**
2642 * ring_buffer_unlock_commit - commit a reserved
2643 * @buffer: The buffer to commit to
2644 * @event: The event pointer to commit.
2645 *
2646 * This commits the data to the ring buffer, and releases any locks held.
2647 *
2648 * Must be paired with ring_buffer_lock_reserve.
2649 */
2650int ring_buffer_unlock_commit(struct ring_buffer *buffer,
2651 struct ring_buffer_event *event)
2652{
2653 struct ring_buffer_per_cpu *cpu_buffer;
2654 int cpu = raw_smp_processor_id();
2655
2656 cpu_buffer = buffer->buffers[cpu];
2657
2658 rb_commit(cpu_buffer, event);
2659
2660 rb_wakeups(buffer, cpu_buffer);
2661
2662 trace_recursive_unlock(cpu_buffer);
2663
2664 preempt_enable_notrace();
2665
2666 return 0;
2667}
2668EXPORT_SYMBOL_GPL(ring_buffer_unlock_commit);
Steven Rostedt (Red Hat)a4543a22015-05-29 09:40:18 -04002669
Steven Rostedt (Red Hat)9826b272015-05-28 17:36:45 -04002670static noinline void
2671rb_handle_timestamp(struct ring_buffer_per_cpu *cpu_buffer,
2672 struct rb_event_info *info)
2673{
2674 WARN_ONCE(info->delta > (1ULL << 59),
2675 KERN_WARNING "Delta way too big! %llu ts=%llu write stamp = %llu\n%s",
2676 (unsigned long long)info->delta,
2677 (unsigned long long)info->ts,
2678 (unsigned long long)cpu_buffer->write_stamp,
2679 sched_clock_stable() ? "" :
2680 "If you just came from a suspend/resume,\n"
2681 "please switch to the trace global clock:\n"
2682 " echo global > /sys/kernel/debug/tracing/trace_clock\n");
Steven Rostedt (Red Hat)b7dc42f2015-09-03 08:57:12 -04002683 info->add_timestamp = 1;
Steven Rostedt (Red Hat)9826b272015-05-28 17:36:45 -04002684}
2685
Steven Rostedt6634ff22009-05-06 15:30:07 -04002686static struct ring_buffer_event *
2687__rb_reserve_next(struct ring_buffer_per_cpu *cpu_buffer,
Steven Rostedt (Red Hat)fcc742e2015-05-28 17:13:14 -04002688 struct rb_event_info *info)
Steven Rostedt6634ff22009-05-06 15:30:07 -04002689{
Steven Rostedt6634ff22009-05-06 15:30:07 -04002690 struct ring_buffer_event *event;
Steven Rostedt (Red Hat)fcc742e2015-05-28 17:13:14 -04002691 struct buffer_page *tail_page;
Steven Rostedt6634ff22009-05-06 15:30:07 -04002692 unsigned long tail, write;
Steven Rostedt (Red Hat)b7dc42f2015-09-03 08:57:12 -04002693
2694 /*
2695 * If the time delta since the last event is too big to
2696 * hold in the time field of the event, then we append a
2697 * TIME EXTEND event ahead of the data event.
2698 */
2699 if (unlikely(info->add_timestamp))
2700 info->length += RB_LEN_TIME_EXTEND;
Steven Rostedt69d1b832010-10-07 18:18:05 -04002701
Steven Rostedt (Red Hat)85736362015-11-17 14:03:11 -05002702 /* Don't let the compiler play games with cpu_buffer->tail_page */
2703 tail_page = info->tail_page = READ_ONCE(cpu_buffer->tail_page);
Steven Rostedt (Red Hat)fcc742e2015-05-28 17:13:14 -04002704 write = local_add_return(info->length, &tail_page->write);
Steven Rostedt77ae3652009-03-27 11:00:29 -04002705
2706 /* set write to only the index of the write */
2707 write &= RB_WRITE_MASK;
Steven Rostedt (Red Hat)fcc742e2015-05-28 17:13:14 -04002708 tail = write - info->length;
Steven Rostedt6634ff22009-05-06 15:30:07 -04002709
Steven Rostedt (Red Hat)b7dc42f2015-09-03 08:57:12 -04002710 /*
2711 * If this is the first commit on the page, then it has the same
2712 * timestamp as the page itself.
2713 */
2714 if (!tail)
2715 info->delta = 0;
2716
Steven Rostedt6634ff22009-05-06 15:30:07 -04002717 /* See if we shot pass the end of this buffer page */
Steven Rostedt747e94a2010-10-08 13:51:48 -04002718 if (unlikely(write > BUF_PAGE_SIZE))
Steven Rostedt (Red Hat)fcc742e2015-05-28 17:13:14 -04002719 return rb_move_tail(cpu_buffer, tail, info);
Steven Rostedt6634ff22009-05-06 15:30:07 -04002720
2721 /* We reserved something on the buffer */
Steven Rostedt (Red Hat)b7dc42f2015-09-03 08:57:12 -04002722
Steven Rostedt6634ff22009-05-06 15:30:07 -04002723 event = __rb_page_index(tail_page, tail);
Vegard Nossum1744a212009-02-28 08:29:44 +01002724 kmemcheck_annotate_bitfield(event, bitfield);
Steven Rostedt (Red Hat)fcc742e2015-05-28 17:13:14 -04002725 rb_update_event(cpu_buffer, event, info);
Steven Rostedt6634ff22009-05-06 15:30:07 -04002726
Steven Rostedt69d1b832010-10-07 18:18:05 -04002727 local_inc(&tail_page->entries);
Steven Rostedt6634ff22009-05-06 15:30:07 -04002728
Steven Rostedt (Red Hat)b7dc42f2015-09-03 08:57:12 -04002729 /*
2730 * If this is the first commit on the page, then update
2731 * its timestamp.
2732 */
2733 if (!tail)
2734 tail_page->page->time_stamp = info->ts;
2735
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -07002736 /* account for these added bytes */
Steven Rostedt (Red Hat)fcc742e2015-05-28 17:13:14 -04002737 local_add(info->length, &cpu_buffer->entries_bytes);
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -07002738
Steven Rostedt6634ff22009-05-06 15:30:07 -04002739 return event;
2740}
2741
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002742static struct ring_buffer_event *
Steven Rostedt62f0b3e2009-09-04 14:11:34 -04002743rb_reserve_next_event(struct ring_buffer *buffer,
2744 struct ring_buffer_per_cpu *cpu_buffer,
Steven Rostedt1cd8d732009-05-11 14:08:09 -04002745 unsigned long length)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002746{
2747 struct ring_buffer_event *event;
Steven Rostedt (Red Hat)fcc742e2015-05-28 17:13:14 -04002748 struct rb_event_info info;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002749 int nr_loops = 0;
Steven Rostedt (Red Hat)b7dc42f2015-09-03 08:57:12 -04002750 u64 diff;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002751
Steven Rostedtfa743952009-06-16 12:37:57 -04002752 rb_start_commit(cpu_buffer);
2753
Steven Rostedt85bac322009-09-04 14:24:40 -04002754#ifdef CONFIG_RING_BUFFER_ALLOW_SWAP
Steven Rostedt62f0b3e2009-09-04 14:11:34 -04002755 /*
2756 * Due to the ability to swap a cpu buffer from a buffer
2757 * it is possible it was swapped before we committed.
2758 * (committing stops a swap). We check for it here and
2759 * if it happened, we have to fail the write.
2760 */
2761 barrier();
2762 if (unlikely(ACCESS_ONCE(cpu_buffer->buffer) != buffer)) {
2763 local_dec(&cpu_buffer->committing);
2764 local_dec(&cpu_buffer->commits);
2765 return NULL;
2766 }
Steven Rostedt85bac322009-09-04 14:24:40 -04002767#endif
Steven Rostedt (Red Hat)b7dc42f2015-09-03 08:57:12 -04002768
Steven Rostedt (Red Hat)fcc742e2015-05-28 17:13:14 -04002769 info.length = rb_calculate_event_length(length);
Steven Rostedt (Red Hat)a4543a22015-05-29 09:40:18 -04002770 again:
Steven Rostedt (Red Hat)b7dc42f2015-09-03 08:57:12 -04002771 info.add_timestamp = 0;
2772 info.delta = 0;
2773
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002774 /*
2775 * We allow for interrupts to reenter here and do a trace.
2776 * If one does, it will cause this original code to loop
2777 * back here. Even with heavy interrupts happening, this
2778 * should only happen a few times in a row. If this happens
2779 * 1000 times in a row, there must be either an interrupt
2780 * storm or we have something buggy.
2781 * Bail!
2782 */
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05002783 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 1000))
Steven Rostedtfa743952009-06-16 12:37:57 -04002784 goto out_fail;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002785
Steven Rostedt (Red Hat)b7dc42f2015-09-03 08:57:12 -04002786 info.ts = rb_time_stamp(cpu_buffer->buffer);
2787 diff = info.ts - cpu_buffer->write_stamp;
2788
2789 /* make sure this diff is calculated here */
2790 barrier();
2791
2792 /* Did the write stamp get updated already? */
2793 if (likely(info.ts >= cpu_buffer->write_stamp)) {
2794 info.delta = diff;
2795 if (unlikely(test_time_stamp(info.delta)))
2796 rb_handle_timestamp(cpu_buffer, &info);
2797 }
2798
Steven Rostedt (Red Hat)fcc742e2015-05-28 17:13:14 -04002799 event = __rb_reserve_next(cpu_buffer, &info);
2800
Steven Rostedt (Red Hat)bd1b7cd2015-11-23 17:35:24 -05002801 if (unlikely(PTR_ERR(event) == -EAGAIN)) {
2802 if (info.add_timestamp)
2803 info.length -= RB_LEN_TIME_EXTEND;
Steven Rostedtbf41a152008-10-04 02:00:59 -04002804 goto again;
Steven Rostedt (Red Hat)bd1b7cd2015-11-23 17:35:24 -05002805 }
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002806
Steven Rostedtfa743952009-06-16 12:37:57 -04002807 if (!event)
2808 goto out_fail;
Steven Rostedtbf41a152008-10-04 02:00:59 -04002809
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002810 return event;
Steven Rostedtfa743952009-06-16 12:37:57 -04002811
2812 out_fail:
2813 rb_end_commit(cpu_buffer);
2814 return NULL;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002815}
2816
2817/**
2818 * ring_buffer_lock_reserve - reserve a part of the buffer
2819 * @buffer: the ring buffer to reserve from
2820 * @length: the length of the data to reserve (excluding event header)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002821 *
2822 * Returns a reseverd event on the ring buffer to copy directly to.
2823 * The user of this interface will need to get the body to write into
2824 * and can use the ring_buffer_event_data() interface.
2825 *
2826 * The length is the length of the data needed, not the event length
2827 * which also includes the event header.
2828 *
2829 * Must be paired with ring_buffer_unlock_commit, unless NULL is returned.
2830 * If NULL is returned, then nothing has been allocated or locked.
2831 */
2832struct ring_buffer_event *
Arnaldo Carvalho de Melo0a987752009-02-05 16:12:56 -02002833ring_buffer_lock_reserve(struct ring_buffer *buffer, unsigned long length)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002834{
2835 struct ring_buffer_per_cpu *cpu_buffer;
2836 struct ring_buffer_event *event;
Steven Rostedt5168ae52010-06-03 09:36:50 -04002837 int cpu;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002838
Steven Rostedtbf41a152008-10-04 02:00:59 -04002839 /* If we are tracing schedule, we don't want to recurse */
Steven Rostedt5168ae52010-06-03 09:36:50 -04002840 preempt_disable_notrace();
Steven Rostedtbf41a152008-10-04 02:00:59 -04002841
Steven Rostedt (Red Hat)3205f802015-05-21 17:39:29 -04002842 if (unlikely(atomic_read(&buffer->record_disabled)))
Steven Rostedt (Red Hat)58a09ec2015-05-27 10:27:47 -04002843 goto out;
Steven Rostedt261842b2009-04-16 21:41:52 -04002844
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002845 cpu = raw_smp_processor_id();
2846
Steven Rostedt (Red Hat)3205f802015-05-21 17:39:29 -04002847 if (unlikely(!cpumask_test_cpu(cpu, buffer->cpumask)))
Steven Rostedtd7690412008-10-01 00:29:53 -04002848 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002849
2850 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002851
Steven Rostedt (Red Hat)3205f802015-05-21 17:39:29 -04002852 if (unlikely(atomic_read(&cpu_buffer->record_disabled)))
Steven Rostedtd7690412008-10-01 00:29:53 -04002853 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002854
Steven Rostedt (Red Hat)3205f802015-05-21 17:39:29 -04002855 if (unlikely(length > BUF_MAX_DATA_SIZE))
Steven Rostedtbf41a152008-10-04 02:00:59 -04002856 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002857
Steven Rostedt (Red Hat)58a09ec2015-05-27 10:27:47 -04002858 if (unlikely(trace_recursive_lock(cpu_buffer)))
2859 goto out;
2860
Steven Rostedt62f0b3e2009-09-04 14:11:34 -04002861 event = rb_reserve_next_event(buffer, cpu_buffer, length);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002862 if (!event)
Steven Rostedt (Red Hat)58a09ec2015-05-27 10:27:47 -04002863 goto out_unlock;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002864
2865 return event;
2866
Steven Rostedt (Red Hat)58a09ec2015-05-27 10:27:47 -04002867 out_unlock:
2868 trace_recursive_unlock(cpu_buffer);
Steven Rostedtd7690412008-10-01 00:29:53 -04002869 out:
Steven Rostedt5168ae52010-06-03 09:36:50 -04002870 preempt_enable_notrace();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002871 return NULL;
2872}
Robert Richterc4f50182008-12-11 16:49:22 +01002873EXPORT_SYMBOL_GPL(ring_buffer_lock_reserve);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002874
Steven Rostedta1863c22009-09-03 10:23:58 -04002875/*
2876 * Decrement the entries to the page that an event is on.
2877 * The event does not even need to exist, only the pointer
2878 * to the page it is on. This may only be called before the commit
2879 * takes place.
2880 */
2881static inline void
2882rb_decrement_entry(struct ring_buffer_per_cpu *cpu_buffer,
2883 struct ring_buffer_event *event)
2884{
2885 unsigned long addr = (unsigned long)event;
2886 struct buffer_page *bpage = cpu_buffer->commit_page;
2887 struct buffer_page *start;
2888
2889 addr &= PAGE_MASK;
2890
2891 /* Do the likely case first */
2892 if (likely(bpage->page == (void *)addr)) {
2893 local_dec(&bpage->entries);
2894 return;
2895 }
2896
2897 /*
2898 * Because the commit page may be on the reader page we
2899 * start with the next page and check the end loop there.
2900 */
2901 rb_inc_page(cpu_buffer, &bpage);
2902 start = bpage;
2903 do {
2904 if (bpage->page == (void *)addr) {
2905 local_dec(&bpage->entries);
2906 return;
2907 }
2908 rb_inc_page(cpu_buffer, &bpage);
2909 } while (bpage != start);
2910
2911 /* commit not part of this buffer?? */
2912 RB_WARN_ON(cpu_buffer, 1);
2913}
2914
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002915/**
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002916 * ring_buffer_commit_discard - discard an event that has not been committed
2917 * @buffer: the ring buffer
2918 * @event: non committed event to discard
2919 *
Steven Rostedtdc892f72009-09-03 15:33:41 -04002920 * Sometimes an event that is in the ring buffer needs to be ignored.
2921 * This function lets the user discard an event in the ring buffer
2922 * and then that event will not be read later.
2923 *
2924 * This function only works if it is called before the the item has been
2925 * committed. It will try to free the event from the ring buffer
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002926 * if another event has not been added behind it.
2927 *
2928 * If another event has been added behind it, it will set the event
2929 * up as discarded, and perform the commit.
2930 *
2931 * If this function is called, do not call ring_buffer_unlock_commit on
2932 * the event.
2933 */
2934void ring_buffer_discard_commit(struct ring_buffer *buffer,
2935 struct ring_buffer_event *event)
2936{
2937 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002938 int cpu;
2939
2940 /* The event is discarded regardless */
Frederic Weisbeckerf3b9aae2009-04-19 23:39:33 +02002941 rb_event_discard(event);
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002942
Steven Rostedtfa743952009-06-16 12:37:57 -04002943 cpu = smp_processor_id();
2944 cpu_buffer = buffer->buffers[cpu];
2945
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002946 /*
2947 * This must only be called if the event has not been
2948 * committed yet. Thus we can assume that preemption
2949 * is still disabled.
2950 */
Steven Rostedtfa743952009-06-16 12:37:57 -04002951 RB_WARN_ON(buffer, !local_read(&cpu_buffer->committing));
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002952
Steven Rostedta1863c22009-09-03 10:23:58 -04002953 rb_decrement_entry(cpu_buffer, event);
Steven Rostedt0f2541d2009-08-05 12:02:48 -04002954 if (rb_try_to_discard(cpu_buffer, event))
Steven Rostedtedd813bf2009-06-02 23:00:53 -04002955 goto out;
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002956
2957 /*
2958 * The commit is still visible by the reader, so we
Steven Rostedta1863c22009-09-03 10:23:58 -04002959 * must still update the timestamp.
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002960 */
Steven Rostedta1863c22009-09-03 10:23:58 -04002961 rb_update_write_stamp(cpu_buffer, event);
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002962 out:
Steven Rostedtfa743952009-06-16 12:37:57 -04002963 rb_end_commit(cpu_buffer);
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002964
Steven Rostedt (Red Hat)58a09ec2015-05-27 10:27:47 -04002965 trace_recursive_unlock(cpu_buffer);
Frederic Weisbeckerf3b9aae2009-04-19 23:39:33 +02002966
Steven Rostedt5168ae52010-06-03 09:36:50 -04002967 preempt_enable_notrace();
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002968
2969}
2970EXPORT_SYMBOL_GPL(ring_buffer_discard_commit);
2971
2972/**
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002973 * ring_buffer_write - write data to the buffer without reserving
2974 * @buffer: The ring buffer to write to.
2975 * @length: The length of the data being written (excluding the event header)
2976 * @data: The data to write to the buffer.
2977 *
2978 * This is like ring_buffer_lock_reserve and ring_buffer_unlock_commit as
2979 * one function. If you already have the data to write to the buffer, it
2980 * may be easier to simply call this function.
2981 *
2982 * Note, like ring_buffer_lock_reserve, the length is the length of the data
2983 * and not the length of the event which would hold the header.
2984 */
2985int ring_buffer_write(struct ring_buffer *buffer,
David Sharp01e3e712012-06-07 16:46:24 -07002986 unsigned long length,
2987 void *data)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002988{
2989 struct ring_buffer_per_cpu *cpu_buffer;
2990 struct ring_buffer_event *event;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002991 void *body;
2992 int ret = -EBUSY;
Steven Rostedt5168ae52010-06-03 09:36:50 -04002993 int cpu;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002994
Steven Rostedt5168ae52010-06-03 09:36:50 -04002995 preempt_disable_notrace();
Steven Rostedtbf41a152008-10-04 02:00:59 -04002996
Lai Jiangshan52fbe9c2010-03-08 14:50:43 +08002997 if (atomic_read(&buffer->record_disabled))
2998 goto out;
2999
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003000 cpu = raw_smp_processor_id();
3001
Rusty Russell9e01c1b2009-01-01 10:12:22 +10303002 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedtd7690412008-10-01 00:29:53 -04003003 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003004
3005 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003006
3007 if (atomic_read(&cpu_buffer->record_disabled))
3008 goto out;
3009
Steven Rostedtbe957c42009-05-11 14:42:53 -04003010 if (length > BUF_MAX_DATA_SIZE)
3011 goto out;
3012
Steven Rostedt (Red Hat)985e8712015-05-27 10:48:56 -04003013 if (unlikely(trace_recursive_lock(cpu_buffer)))
3014 goto out;
3015
Steven Rostedt62f0b3e2009-09-04 14:11:34 -04003016 event = rb_reserve_next_event(buffer, cpu_buffer, length);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003017 if (!event)
Steven Rostedt (Red Hat)985e8712015-05-27 10:48:56 -04003018 goto out_unlock;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003019
3020 body = rb_event_data(event);
3021
3022 memcpy(body, data, length);
3023
3024 rb_commit(cpu_buffer, event);
3025
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -05003026 rb_wakeups(buffer, cpu_buffer);
3027
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003028 ret = 0;
Steven Rostedt (Red Hat)985e8712015-05-27 10:48:56 -04003029
3030 out_unlock:
3031 trace_recursive_unlock(cpu_buffer);
3032
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003033 out:
Steven Rostedt5168ae52010-06-03 09:36:50 -04003034 preempt_enable_notrace();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003035
3036 return ret;
3037}
Robert Richterc4f50182008-12-11 16:49:22 +01003038EXPORT_SYMBOL_GPL(ring_buffer_write);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003039
Yaowei Baida588342015-09-29 22:43:33 +08003040static bool rb_per_cpu_empty(struct ring_buffer_per_cpu *cpu_buffer)
Steven Rostedtbf41a152008-10-04 02:00:59 -04003041{
3042 struct buffer_page *reader = cpu_buffer->reader_page;
Steven Rostedt77ae3652009-03-27 11:00:29 -04003043 struct buffer_page *head = rb_set_head_page(cpu_buffer);
Steven Rostedtbf41a152008-10-04 02:00:59 -04003044 struct buffer_page *commit = cpu_buffer->commit_page;
3045
Steven Rostedt77ae3652009-03-27 11:00:29 -04003046 /* In case of error, head will be NULL */
3047 if (unlikely(!head))
Yaowei Baida588342015-09-29 22:43:33 +08003048 return true;
Steven Rostedt77ae3652009-03-27 11:00:29 -04003049
Steven Rostedtbf41a152008-10-04 02:00:59 -04003050 return reader->read == rb_page_commit(reader) &&
3051 (commit == reader ||
3052 (commit == head &&
3053 head->read == rb_page_commit(commit)));
3054}
3055
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003056/**
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003057 * ring_buffer_record_disable - stop all writes into the buffer
3058 * @buffer: The ring buffer to stop writes to.
3059 *
3060 * This prevents all writes to the buffer. Any attempt to write
3061 * to the buffer after this will fail and return NULL.
3062 *
3063 * The caller should call synchronize_sched() after this.
3064 */
3065void ring_buffer_record_disable(struct ring_buffer *buffer)
3066{
3067 atomic_inc(&buffer->record_disabled);
3068}
Robert Richterc4f50182008-12-11 16:49:22 +01003069EXPORT_SYMBOL_GPL(ring_buffer_record_disable);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003070
3071/**
3072 * ring_buffer_record_enable - enable writes to the buffer
3073 * @buffer: The ring buffer to enable writes
3074 *
3075 * Note, multiple disables will need the same number of enables
Adam Buchbinderc41b20e2009-12-11 16:35:39 -05003076 * to truly enable the writing (much like preempt_disable).
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003077 */
3078void ring_buffer_record_enable(struct ring_buffer *buffer)
3079{
3080 atomic_dec(&buffer->record_disabled);
3081}
Robert Richterc4f50182008-12-11 16:49:22 +01003082EXPORT_SYMBOL_GPL(ring_buffer_record_enable);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003083
3084/**
Steven Rostedt499e5472012-02-22 15:50:28 -05003085 * ring_buffer_record_off - stop all writes into the buffer
3086 * @buffer: The ring buffer to stop writes to.
3087 *
3088 * This prevents all writes to the buffer. Any attempt to write
3089 * to the buffer after this will fail and return NULL.
3090 *
3091 * This is different than ring_buffer_record_disable() as
Wang Tianhong87abb3b2012-08-02 14:02:00 +08003092 * it works like an on/off switch, where as the disable() version
Steven Rostedt499e5472012-02-22 15:50:28 -05003093 * must be paired with a enable().
3094 */
3095void ring_buffer_record_off(struct ring_buffer *buffer)
3096{
3097 unsigned int rd;
3098 unsigned int new_rd;
3099
3100 do {
3101 rd = atomic_read(&buffer->record_disabled);
3102 new_rd = rd | RB_BUFFER_OFF;
3103 } while (atomic_cmpxchg(&buffer->record_disabled, rd, new_rd) != rd);
3104}
3105EXPORT_SYMBOL_GPL(ring_buffer_record_off);
3106
3107/**
3108 * ring_buffer_record_on - restart writes into the buffer
3109 * @buffer: The ring buffer to start writes to.
3110 *
3111 * This enables all writes to the buffer that was disabled by
3112 * ring_buffer_record_off().
3113 *
3114 * This is different than ring_buffer_record_enable() as
Wang Tianhong87abb3b2012-08-02 14:02:00 +08003115 * it works like an on/off switch, where as the enable() version
Steven Rostedt499e5472012-02-22 15:50:28 -05003116 * must be paired with a disable().
3117 */
3118void ring_buffer_record_on(struct ring_buffer *buffer)
3119{
3120 unsigned int rd;
3121 unsigned int new_rd;
3122
3123 do {
3124 rd = atomic_read(&buffer->record_disabled);
3125 new_rd = rd & ~RB_BUFFER_OFF;
3126 } while (atomic_cmpxchg(&buffer->record_disabled, rd, new_rd) != rd);
3127}
3128EXPORT_SYMBOL_GPL(ring_buffer_record_on);
3129
3130/**
3131 * ring_buffer_record_is_on - return true if the ring buffer can write
3132 * @buffer: The ring buffer to see if write is enabled
3133 *
3134 * Returns true if the ring buffer is in a state that it accepts writes.
3135 */
3136int ring_buffer_record_is_on(struct ring_buffer *buffer)
3137{
3138 return !atomic_read(&buffer->record_disabled);
3139}
3140
3141/**
Masami Hiramatsua26030a2018-07-14 01:28:15 +09003142 * ring_buffer_record_is_set_on - return true if the ring buffer is set writable
3143 * @buffer: The ring buffer to see if write is set enabled
3144 *
3145 * Returns true if the ring buffer is set writable by ring_buffer_record_on().
3146 * Note that this does NOT mean it is in a writable state.
3147 *
3148 * It may return true when the ring buffer has been disabled by
3149 * ring_buffer_record_disable(), as that is a temporary disabling of
3150 * the ring buffer.
3151 */
3152int ring_buffer_record_is_set_on(struct ring_buffer *buffer)
3153{
3154 return !(atomic_read(&buffer->record_disabled) & RB_BUFFER_OFF);
3155}
3156
3157/**
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003158 * ring_buffer_record_disable_cpu - stop all writes into the cpu_buffer
3159 * @buffer: The ring buffer to stop writes to.
3160 * @cpu: The CPU buffer to stop
3161 *
3162 * This prevents all writes to the buffer. Any attempt to write
3163 * to the buffer after this will fail and return NULL.
3164 *
3165 * The caller should call synchronize_sched() after this.
3166 */
3167void ring_buffer_record_disable_cpu(struct ring_buffer *buffer, int cpu)
3168{
3169 struct ring_buffer_per_cpu *cpu_buffer;
3170
Rusty Russell9e01c1b2009-01-01 10:12:22 +10303171 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04003172 return;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003173
3174 cpu_buffer = buffer->buffers[cpu];
3175 atomic_inc(&cpu_buffer->record_disabled);
3176}
Robert Richterc4f50182008-12-11 16:49:22 +01003177EXPORT_SYMBOL_GPL(ring_buffer_record_disable_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003178
3179/**
3180 * ring_buffer_record_enable_cpu - enable writes to the buffer
3181 * @buffer: The ring buffer to enable writes
3182 * @cpu: The CPU to enable.
3183 *
3184 * Note, multiple disables will need the same number of enables
Adam Buchbinderc41b20e2009-12-11 16:35:39 -05003185 * to truly enable the writing (much like preempt_disable).
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003186 */
3187void ring_buffer_record_enable_cpu(struct ring_buffer *buffer, int cpu)
3188{
3189 struct ring_buffer_per_cpu *cpu_buffer;
3190
Rusty Russell9e01c1b2009-01-01 10:12:22 +10303191 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04003192 return;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003193
3194 cpu_buffer = buffer->buffers[cpu];
3195 atomic_dec(&cpu_buffer->record_disabled);
3196}
Robert Richterc4f50182008-12-11 16:49:22 +01003197EXPORT_SYMBOL_GPL(ring_buffer_record_enable_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003198
Steven Rostedtf6195aa2010-09-01 12:23:12 -04003199/*
3200 * The total entries in the ring buffer is the running counter
3201 * of entries entered into the ring buffer, minus the sum of
3202 * the entries read from the ring buffer and the number of
3203 * entries that were overwritten.
3204 */
3205static inline unsigned long
3206rb_num_of_entries(struct ring_buffer_per_cpu *cpu_buffer)
3207{
3208 return local_read(&cpu_buffer->entries) -
3209 (local_read(&cpu_buffer->overrun) + cpu_buffer->read);
3210}
3211
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003212/**
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -07003213 * ring_buffer_oldest_event_ts - get the oldest event timestamp from the buffer
3214 * @buffer: The ring buffer
3215 * @cpu: The per CPU buffer to read from.
3216 */
Yoshihiro YUNOMAE50ecf2c2012-10-11 16:27:54 -07003217u64 ring_buffer_oldest_event_ts(struct ring_buffer *buffer, int cpu)
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -07003218{
3219 unsigned long flags;
3220 struct ring_buffer_per_cpu *cpu_buffer;
3221 struct buffer_page *bpage;
Linus Torvaldsda830e52012-12-11 18:18:58 -08003222 u64 ret = 0;
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -07003223
3224 if (!cpumask_test_cpu(cpu, buffer->cpumask))
3225 return 0;
3226
3227 cpu_buffer = buffer->buffers[cpu];
Linus Torvalds7115e3f2011-10-26 17:03:38 +02003228 raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -07003229 /*
3230 * if the tail is on reader_page, oldest time stamp is on the reader
3231 * page
3232 */
3233 if (cpu_buffer->tail_page == cpu_buffer->reader_page)
3234 bpage = cpu_buffer->reader_page;
3235 else
3236 bpage = rb_set_head_page(cpu_buffer);
Steven Rostedt54f7be52012-11-29 22:27:22 -05003237 if (bpage)
3238 ret = bpage->page->time_stamp;
Linus Torvalds7115e3f2011-10-26 17:03:38 +02003239 raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -07003240
3241 return ret;
3242}
3243EXPORT_SYMBOL_GPL(ring_buffer_oldest_event_ts);
3244
3245/**
3246 * ring_buffer_bytes_cpu - get the number of bytes consumed in a cpu buffer
3247 * @buffer: The ring buffer
3248 * @cpu: The per CPU buffer to read from.
3249 */
3250unsigned long ring_buffer_bytes_cpu(struct ring_buffer *buffer, int cpu)
3251{
3252 struct ring_buffer_per_cpu *cpu_buffer;
3253 unsigned long ret;
3254
3255 if (!cpumask_test_cpu(cpu, buffer->cpumask))
3256 return 0;
3257
3258 cpu_buffer = buffer->buffers[cpu];
3259 ret = local_read(&cpu_buffer->entries_bytes) - cpu_buffer->read_bytes;
3260
3261 return ret;
3262}
3263EXPORT_SYMBOL_GPL(ring_buffer_bytes_cpu);
3264
3265/**
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003266 * ring_buffer_entries_cpu - get the number of entries in a cpu buffer
3267 * @buffer: The ring buffer
3268 * @cpu: The per CPU buffer to get the entries from.
3269 */
3270unsigned long ring_buffer_entries_cpu(struct ring_buffer *buffer, int cpu)
3271{
3272 struct ring_buffer_per_cpu *cpu_buffer;
3273
Rusty Russell9e01c1b2009-01-01 10:12:22 +10303274 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04003275 return 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003276
3277 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt554f7862009-03-11 22:00:13 -04003278
Steven Rostedtf6195aa2010-09-01 12:23:12 -04003279 return rb_num_of_entries(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003280}
Robert Richterc4f50182008-12-11 16:49:22 +01003281EXPORT_SYMBOL_GPL(ring_buffer_entries_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003282
3283/**
Slava Pestov884bfe82011-07-15 14:23:58 -07003284 * ring_buffer_overrun_cpu - get the number of overruns caused by the ring
3285 * buffer wrapping around (only if RB_FL_OVERWRITE is on).
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003286 * @buffer: The ring buffer
3287 * @cpu: The per CPU buffer to get the number of overruns from
3288 */
3289unsigned long ring_buffer_overrun_cpu(struct ring_buffer *buffer, int cpu)
3290{
3291 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedt8aabee52009-03-12 13:13:49 -04003292 unsigned long ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003293
Rusty Russell9e01c1b2009-01-01 10:12:22 +10303294 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04003295 return 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003296
3297 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt77ae3652009-03-27 11:00:29 -04003298 ret = local_read(&cpu_buffer->overrun);
Steven Rostedt554f7862009-03-11 22:00:13 -04003299
3300 return ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003301}
Robert Richterc4f50182008-12-11 16:49:22 +01003302EXPORT_SYMBOL_GPL(ring_buffer_overrun_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003303
3304/**
Slava Pestov884bfe82011-07-15 14:23:58 -07003305 * ring_buffer_commit_overrun_cpu - get the number of overruns caused by
3306 * commits failing due to the buffer wrapping around while there are uncommitted
3307 * events, such as during an interrupt storm.
Steven Rostedtf0d2c682009-04-29 13:43:37 -04003308 * @buffer: The ring buffer
3309 * @cpu: The per CPU buffer to get the number of overruns from
3310 */
3311unsigned long
3312ring_buffer_commit_overrun_cpu(struct ring_buffer *buffer, int cpu)
3313{
3314 struct ring_buffer_per_cpu *cpu_buffer;
3315 unsigned long ret;
3316
3317 if (!cpumask_test_cpu(cpu, buffer->cpumask))
3318 return 0;
3319
3320 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt77ae3652009-03-27 11:00:29 -04003321 ret = local_read(&cpu_buffer->commit_overrun);
Steven Rostedtf0d2c682009-04-29 13:43:37 -04003322
3323 return ret;
3324}
3325EXPORT_SYMBOL_GPL(ring_buffer_commit_overrun_cpu);
3326
3327/**
Slava Pestov884bfe82011-07-15 14:23:58 -07003328 * ring_buffer_dropped_events_cpu - get the number of dropped events caused by
3329 * the ring buffer filling up (only if RB_FL_OVERWRITE is off).
3330 * @buffer: The ring buffer
3331 * @cpu: The per CPU buffer to get the number of overruns from
3332 */
3333unsigned long
3334ring_buffer_dropped_events_cpu(struct ring_buffer *buffer, int cpu)
3335{
3336 struct ring_buffer_per_cpu *cpu_buffer;
3337 unsigned long ret;
3338
3339 if (!cpumask_test_cpu(cpu, buffer->cpumask))
3340 return 0;
3341
3342 cpu_buffer = buffer->buffers[cpu];
3343 ret = local_read(&cpu_buffer->dropped_events);
3344
3345 return ret;
3346}
3347EXPORT_SYMBOL_GPL(ring_buffer_dropped_events_cpu);
3348
3349/**
Steven Rostedt (Red Hat)ad964702013-01-29 17:45:49 -05003350 * ring_buffer_read_events_cpu - get the number of events successfully read
3351 * @buffer: The ring buffer
3352 * @cpu: The per CPU buffer to get the number of events read
3353 */
3354unsigned long
3355ring_buffer_read_events_cpu(struct ring_buffer *buffer, int cpu)
3356{
3357 struct ring_buffer_per_cpu *cpu_buffer;
3358
3359 if (!cpumask_test_cpu(cpu, buffer->cpumask))
3360 return 0;
3361
3362 cpu_buffer = buffer->buffers[cpu];
3363 return cpu_buffer->read;
3364}
3365EXPORT_SYMBOL_GPL(ring_buffer_read_events_cpu);
3366
3367/**
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003368 * ring_buffer_entries - get the number of entries in a buffer
3369 * @buffer: The ring buffer
3370 *
3371 * Returns the total number of entries in the ring buffer
3372 * (all CPU entries)
3373 */
3374unsigned long ring_buffer_entries(struct ring_buffer *buffer)
3375{
3376 struct ring_buffer_per_cpu *cpu_buffer;
3377 unsigned long entries = 0;
3378 int cpu;
3379
3380 /* if you care about this being correct, lock the buffer */
3381 for_each_buffer_cpu(buffer, cpu) {
3382 cpu_buffer = buffer->buffers[cpu];
Steven Rostedtf6195aa2010-09-01 12:23:12 -04003383 entries += rb_num_of_entries(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003384 }
3385
3386 return entries;
3387}
Robert Richterc4f50182008-12-11 16:49:22 +01003388EXPORT_SYMBOL_GPL(ring_buffer_entries);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003389
3390/**
Jiri Olsa67b394f2009-10-23 19:36:18 -04003391 * ring_buffer_overruns - get the number of overruns in buffer
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003392 * @buffer: The ring buffer
3393 *
3394 * Returns the total number of overruns in the ring buffer
3395 * (all CPU entries)
3396 */
3397unsigned long ring_buffer_overruns(struct ring_buffer *buffer)
3398{
3399 struct ring_buffer_per_cpu *cpu_buffer;
3400 unsigned long overruns = 0;
3401 int cpu;
3402
3403 /* if you care about this being correct, lock the buffer */
3404 for_each_buffer_cpu(buffer, cpu) {
3405 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt77ae3652009-03-27 11:00:29 -04003406 overruns += local_read(&cpu_buffer->overrun);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003407 }
3408
3409 return overruns;
3410}
Robert Richterc4f50182008-12-11 16:49:22 +01003411EXPORT_SYMBOL_GPL(ring_buffer_overruns);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003412
Steven Rostedt642edba2008-11-12 00:01:26 -05003413static void rb_iter_reset(struct ring_buffer_iter *iter)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003414{
3415 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
3416
Steven Rostedtd7690412008-10-01 00:29:53 -04003417 /* Iterator usage is expected to have record disabled */
Steven Rostedt (Red Hat)651e22f2014-08-06 14:11:33 -04003418 iter->head_page = cpu_buffer->reader_page;
3419 iter->head = cpu_buffer->reader_page->read;
3420
3421 iter->cache_reader_page = iter->head_page;
Steven Rostedt (Red Hat)24607f12014-10-02 16:51:18 -04003422 iter->cache_read = cpu_buffer->read;
Steven Rostedt (Red Hat)651e22f2014-08-06 14:11:33 -04003423
Steven Rostedtd7690412008-10-01 00:29:53 -04003424 if (iter->head)
3425 iter->read_stamp = cpu_buffer->read_stamp;
3426 else
Steven Rostedtabc9b562008-12-02 15:34:06 -05003427 iter->read_stamp = iter->head_page->page->time_stamp;
Steven Rostedt642edba2008-11-12 00:01:26 -05003428}
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003429
Steven Rostedt642edba2008-11-12 00:01:26 -05003430/**
3431 * ring_buffer_iter_reset - reset an iterator
3432 * @iter: The iterator to reset
3433 *
3434 * Resets the iterator, so that it will start from the beginning
3435 * again.
3436 */
3437void ring_buffer_iter_reset(struct ring_buffer_iter *iter)
3438{
Steven Rostedt554f7862009-03-11 22:00:13 -04003439 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedt642edba2008-11-12 00:01:26 -05003440 unsigned long flags;
3441
Steven Rostedt554f7862009-03-11 22:00:13 -04003442 if (!iter)
3443 return;
3444
3445 cpu_buffer = iter->cpu_buffer;
3446
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02003447 raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
Steven Rostedt642edba2008-11-12 00:01:26 -05003448 rb_iter_reset(iter);
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02003449 raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003450}
Robert Richterc4f50182008-12-11 16:49:22 +01003451EXPORT_SYMBOL_GPL(ring_buffer_iter_reset);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003452
3453/**
3454 * ring_buffer_iter_empty - check if an iterator has no more to read
3455 * @iter: The iterator to check
3456 */
3457int ring_buffer_iter_empty(struct ring_buffer_iter *iter)
3458{
3459 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedt (VMware)d80e90712017-04-19 14:29:46 -04003460 struct buffer_page *reader;
3461 struct buffer_page *head_page;
3462 struct buffer_page *commit_page;
3463 unsigned commit;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003464
3465 cpu_buffer = iter->cpu_buffer;
3466
Steven Rostedt (VMware)d80e90712017-04-19 14:29:46 -04003467 /* Remember, trace recording is off when iterator is in use */
3468 reader = cpu_buffer->reader_page;
3469 head_page = cpu_buffer->head_page;
3470 commit_page = cpu_buffer->commit_page;
3471 commit = rb_page_commit(commit_page);
3472
3473 return ((iter->head_page == commit_page && iter->head == commit) ||
3474 (iter->head_page == reader && commit_page == head_page &&
3475 head_page->read == commit &&
3476 iter->head == rb_page_commit(cpu_buffer->reader_page)));
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003477}
Robert Richterc4f50182008-12-11 16:49:22 +01003478EXPORT_SYMBOL_GPL(ring_buffer_iter_empty);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003479
3480static void
3481rb_update_read_stamp(struct ring_buffer_per_cpu *cpu_buffer,
3482 struct ring_buffer_event *event)
3483{
3484 u64 delta;
3485
Lai Jiangshan334d4162009-04-24 11:27:05 +08003486 switch (event->type_len) {
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003487 case RINGBUF_TYPE_PADDING:
3488 return;
3489
3490 case RINGBUF_TYPE_TIME_EXTEND:
3491 delta = event->array[0];
3492 delta <<= TS_SHIFT;
3493 delta += event->time_delta;
3494 cpu_buffer->read_stamp += delta;
3495 return;
3496
3497 case RINGBUF_TYPE_TIME_STAMP:
3498 /* FIXME: not implemented */
3499 return;
3500
3501 case RINGBUF_TYPE_DATA:
3502 cpu_buffer->read_stamp += event->time_delta;
3503 return;
3504
3505 default:
3506 BUG();
3507 }
3508 return;
3509}
3510
3511static void
3512rb_update_iter_read_stamp(struct ring_buffer_iter *iter,
3513 struct ring_buffer_event *event)
3514{
3515 u64 delta;
3516
Lai Jiangshan334d4162009-04-24 11:27:05 +08003517 switch (event->type_len) {
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003518 case RINGBUF_TYPE_PADDING:
3519 return;
3520
3521 case RINGBUF_TYPE_TIME_EXTEND:
3522 delta = event->array[0];
3523 delta <<= TS_SHIFT;
3524 delta += event->time_delta;
3525 iter->read_stamp += delta;
3526 return;
3527
3528 case RINGBUF_TYPE_TIME_STAMP:
3529 /* FIXME: not implemented */
3530 return;
3531
3532 case RINGBUF_TYPE_DATA:
3533 iter->read_stamp += event->time_delta;
3534 return;
3535
3536 default:
3537 BUG();
3538 }
3539 return;
3540}
3541
Steven Rostedtd7690412008-10-01 00:29:53 -04003542static struct buffer_page *
3543rb_get_reader_page(struct ring_buffer_per_cpu *cpu_buffer)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003544{
Steven Rostedtd7690412008-10-01 00:29:53 -04003545 struct buffer_page *reader = NULL;
Steven Rostedt66a8cb92010-03-31 13:21:56 -04003546 unsigned long overwrite;
Steven Rostedtd7690412008-10-01 00:29:53 -04003547 unsigned long flags;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003548 int nr_loops = 0;
Steven Rostedt77ae3652009-03-27 11:00:29 -04003549 int ret;
Steven Rostedtd7690412008-10-01 00:29:53 -04003550
Steven Rostedt3e03fb72008-11-06 00:09:43 -05003551 local_irq_save(flags);
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01003552 arch_spin_lock(&cpu_buffer->lock);
Steven Rostedtd7690412008-10-01 00:29:53 -04003553
3554 again:
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003555 /*
3556 * This should normally only loop twice. But because the
3557 * start of the reader inserts an empty page, it causes
3558 * a case where we will loop three times. There should be no
3559 * reason to loop four times (that I know of).
3560 */
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05003561 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 3)) {
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003562 reader = NULL;
3563 goto out;
3564 }
3565
Steven Rostedtd7690412008-10-01 00:29:53 -04003566 reader = cpu_buffer->reader_page;
3567
3568 /* If there's more to read, return this page */
Steven Rostedtbf41a152008-10-04 02:00:59 -04003569 if (cpu_buffer->reader_page->read < rb_page_size(reader))
Steven Rostedtd7690412008-10-01 00:29:53 -04003570 goto out;
3571
3572 /* Never should we have an index greater than the size */
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05003573 if (RB_WARN_ON(cpu_buffer,
3574 cpu_buffer->reader_page->read > rb_page_size(reader)))
3575 goto out;
Steven Rostedtd7690412008-10-01 00:29:53 -04003576
3577 /* check if we caught up to the tail */
3578 reader = NULL;
Steven Rostedtbf41a152008-10-04 02:00:59 -04003579 if (cpu_buffer->commit_page == cpu_buffer->reader_page)
Steven Rostedtd7690412008-10-01 00:29:53 -04003580 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003581
Steven Rostedta5fb8332012-06-28 13:35:04 -04003582 /* Don't bother swapping if the ring buffer is empty */
3583 if (rb_num_of_entries(cpu_buffer) == 0)
3584 goto out;
3585
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003586 /*
Steven Rostedtd7690412008-10-01 00:29:53 -04003587 * Reset the reader page to size zero.
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003588 */
Steven Rostedt77ae3652009-03-27 11:00:29 -04003589 local_set(&cpu_buffer->reader_page->write, 0);
3590 local_set(&cpu_buffer->reader_page->entries, 0);
3591 local_set(&cpu_buffer->reader_page->page->commit, 0);
Steven Rostedtff0ff842010-03-31 22:11:42 -04003592 cpu_buffer->reader_page->real_end = 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003593
Steven Rostedt77ae3652009-03-27 11:00:29 -04003594 spin:
3595 /*
3596 * Splice the empty reader page into the list around the head.
3597 */
3598 reader = rb_set_head_page(cpu_buffer);
Steven Rostedt54f7be52012-11-29 22:27:22 -05003599 if (!reader)
3600 goto out;
Steven Rostedt0e1ff5d2010-01-06 20:40:44 -05003601 cpu_buffer->reader_page->list.next = rb_list_head(reader->list.next);
Steven Rostedtd7690412008-10-01 00:29:53 -04003602 cpu_buffer->reader_page->list.prev = reader->list.prev;
Steven Rostedtbf41a152008-10-04 02:00:59 -04003603
Steven Rostedt3adc54f2009-03-30 15:32:01 -04003604 /*
3605 * cpu_buffer->pages just needs to point to the buffer, it
3606 * has no specific buffer page to point to. Lets move it out
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003607 * of our way so we don't accidentally swap it.
Steven Rostedt3adc54f2009-03-30 15:32:01 -04003608 */
3609 cpu_buffer->pages = reader->list.prev;
3610
Steven Rostedt77ae3652009-03-27 11:00:29 -04003611 /* The reader page will be pointing to the new head */
3612 rb_set_list_to_head(cpu_buffer, &cpu_buffer->reader_page->list);
Steven Rostedtd7690412008-10-01 00:29:53 -04003613
3614 /*
Steven Rostedt66a8cb92010-03-31 13:21:56 -04003615 * We want to make sure we read the overruns after we set up our
3616 * pointers to the next object. The writer side does a
3617 * cmpxchg to cross pages which acts as the mb on the writer
3618 * side. Note, the reader will constantly fail the swap
3619 * while the writer is updating the pointers, so this
3620 * guarantees that the overwrite recorded here is the one we
3621 * want to compare with the last_overrun.
3622 */
3623 smp_mb();
3624 overwrite = local_read(&(cpu_buffer->overrun));
3625
3626 /*
Steven Rostedt77ae3652009-03-27 11:00:29 -04003627 * Here's the tricky part.
3628 *
3629 * We need to move the pointer past the header page.
3630 * But we can only do that if a writer is not currently
3631 * moving it. The page before the header page has the
3632 * flag bit '1' set if it is pointing to the page we want.
3633 * but if the writer is in the process of moving it
3634 * than it will be '2' or already moved '0'.
Steven Rostedtd7690412008-10-01 00:29:53 -04003635 */
Steven Rostedtd7690412008-10-01 00:29:53 -04003636
Steven Rostedt77ae3652009-03-27 11:00:29 -04003637 ret = rb_head_page_replace(reader, cpu_buffer->reader_page);
3638
3639 /*
3640 * If we did not convert it, then we must try again.
3641 */
3642 if (!ret)
3643 goto spin;
3644
3645 /*
3646 * Yeah! We succeeded in replacing the page.
3647 *
3648 * Now make the new head point back to the reader page.
3649 */
David Sharp5ded3dc62010-01-06 17:12:07 -08003650 rb_list_head(reader->list.next)->prev = &cpu_buffer->reader_page->list;
Steven Rostedt77ae3652009-03-27 11:00:29 -04003651 rb_inc_page(cpu_buffer, &cpu_buffer->head_page);
Steven Rostedtd7690412008-10-01 00:29:53 -04003652
3653 /* Finally update the reader page to the new head */
3654 cpu_buffer->reader_page = reader;
Steven Rostedt (Red Hat)b81f4722015-11-23 10:35:36 -05003655 cpu_buffer->reader_page->read = 0;
Steven Rostedtd7690412008-10-01 00:29:53 -04003656
Steven Rostedt66a8cb92010-03-31 13:21:56 -04003657 if (overwrite != cpu_buffer->last_overrun) {
3658 cpu_buffer->lost_events = overwrite - cpu_buffer->last_overrun;
3659 cpu_buffer->last_overrun = overwrite;
3660 }
3661
Steven Rostedtd7690412008-10-01 00:29:53 -04003662 goto again;
3663
3664 out:
Steven Rostedt (Red Hat)b81f4722015-11-23 10:35:36 -05003665 /* Update the read_stamp on the first event */
3666 if (reader && reader->read == 0)
3667 cpu_buffer->read_stamp = reader->page->time_stamp;
3668
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01003669 arch_spin_unlock(&cpu_buffer->lock);
Steven Rostedt3e03fb72008-11-06 00:09:43 -05003670 local_irq_restore(flags);
Steven Rostedtd7690412008-10-01 00:29:53 -04003671
3672 return reader;
3673}
3674
3675static void rb_advance_reader(struct ring_buffer_per_cpu *cpu_buffer)
3676{
3677 struct ring_buffer_event *event;
3678 struct buffer_page *reader;
3679 unsigned length;
3680
3681 reader = rb_get_reader_page(cpu_buffer);
3682
3683 /* This function should not be called when buffer is empty */
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05003684 if (RB_WARN_ON(cpu_buffer, !reader))
3685 return;
Steven Rostedtd7690412008-10-01 00:29:53 -04003686
3687 event = rb_reader_event(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003688
Steven Rostedta1863c22009-09-03 10:23:58 -04003689 if (event->type_len <= RINGBUF_TYPE_DATA_TYPE_LEN_MAX)
Steven Rostedte4906ef2009-04-30 20:49:44 -04003690 cpu_buffer->read++;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003691
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003692 rb_update_read_stamp(cpu_buffer, event);
3693
Steven Rostedtd7690412008-10-01 00:29:53 -04003694 length = rb_event_length(event);
Steven Rostedt6f807ac2008-10-04 02:00:58 -04003695 cpu_buffer->reader_page->read += length;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003696}
3697
3698static void rb_advance_iter(struct ring_buffer_iter *iter)
3699{
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003700 struct ring_buffer_per_cpu *cpu_buffer;
3701 struct ring_buffer_event *event;
3702 unsigned length;
3703
3704 cpu_buffer = iter->cpu_buffer;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003705
3706 /*
3707 * Check if we are at the end of the buffer.
3708 */
Steven Rostedtbf41a152008-10-04 02:00:59 -04003709 if (iter->head >= rb_page_size(iter->head_page)) {
Steven Rostedtea05b572009-06-03 09:30:10 -04003710 /* discarded commits can make the page empty */
3711 if (iter->head_page == cpu_buffer->commit_page)
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05003712 return;
Steven Rostedtd7690412008-10-01 00:29:53 -04003713 rb_inc_iter(iter);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003714 return;
3715 }
3716
3717 event = rb_iter_head_event(iter);
3718
3719 length = rb_event_length(event);
3720
3721 /*
3722 * This should not be called to advance the header if we are
3723 * at the tail of the buffer.
3724 */
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05003725 if (RB_WARN_ON(cpu_buffer,
Steven Rostedtf536aaf2008-11-10 23:07:30 -05003726 (iter->head_page == cpu_buffer->commit_page) &&
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05003727 (iter->head + length > rb_commit_index(cpu_buffer))))
3728 return;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003729
3730 rb_update_iter_read_stamp(iter, event);
3731
3732 iter->head += length;
3733
3734 /* check for end of page padding */
Steven Rostedtbf41a152008-10-04 02:00:59 -04003735 if ((iter->head >= rb_page_size(iter->head_page)) &&
3736 (iter->head_page != cpu_buffer->commit_page))
Steven Rostedt771e0382012-11-30 10:41:57 -05003737 rb_inc_iter(iter);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003738}
3739
Steven Rostedt66a8cb92010-03-31 13:21:56 -04003740static int rb_lost_events(struct ring_buffer_per_cpu *cpu_buffer)
3741{
3742 return cpu_buffer->lost_events;
3743}
3744
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003745static struct ring_buffer_event *
Steven Rostedt66a8cb92010-03-31 13:21:56 -04003746rb_buffer_peek(struct ring_buffer_per_cpu *cpu_buffer, u64 *ts,
3747 unsigned long *lost_events)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003748{
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003749 struct ring_buffer_event *event;
Steven Rostedtd7690412008-10-01 00:29:53 -04003750 struct buffer_page *reader;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003751 int nr_loops = 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003752
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003753 again:
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003754 /*
Steven Rostedt69d1b832010-10-07 18:18:05 -04003755 * We repeat when a time extend is encountered.
3756 * Since the time extend is always attached to a data event,
3757 * we should never loop more than once.
3758 * (We never hit the following condition more than twice).
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003759 */
Steven Rostedt69d1b832010-10-07 18:18:05 -04003760 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 2))
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003761 return NULL;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003762
Steven Rostedtd7690412008-10-01 00:29:53 -04003763 reader = rb_get_reader_page(cpu_buffer);
3764 if (!reader)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003765 return NULL;
3766
Steven Rostedtd7690412008-10-01 00:29:53 -04003767 event = rb_reader_event(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003768
Lai Jiangshan334d4162009-04-24 11:27:05 +08003769 switch (event->type_len) {
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003770 case RINGBUF_TYPE_PADDING:
Tom Zanussi2d622712009-03-22 03:30:49 -05003771 if (rb_null_event(event))
3772 RB_WARN_ON(cpu_buffer, 1);
3773 /*
3774 * Because the writer could be discarding every
3775 * event it creates (which would probably be bad)
3776 * if we were to go back to "again" then we may never
3777 * catch up, and will trigger the warn on, or lock
3778 * the box. Return the padding, and we will release
3779 * the current locks, and try again.
3780 */
Tom Zanussi2d622712009-03-22 03:30:49 -05003781 return event;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003782
3783 case RINGBUF_TYPE_TIME_EXTEND:
3784 /* Internal data, OK to advance */
Steven Rostedtd7690412008-10-01 00:29:53 -04003785 rb_advance_reader(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003786 goto again;
3787
3788 case RINGBUF_TYPE_TIME_STAMP:
3789 /* FIXME: not implemented */
Steven Rostedtd7690412008-10-01 00:29:53 -04003790 rb_advance_reader(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003791 goto again;
3792
3793 case RINGBUF_TYPE_DATA:
3794 if (ts) {
3795 *ts = cpu_buffer->read_stamp + event->time_delta;
Robert Richterd8eeb2d2009-07-31 14:58:04 +02003796 ring_buffer_normalize_time_stamp(cpu_buffer->buffer,
Steven Rostedt37886f62009-03-17 17:22:06 -04003797 cpu_buffer->cpu, ts);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003798 }
Steven Rostedt66a8cb92010-03-31 13:21:56 -04003799 if (lost_events)
3800 *lost_events = rb_lost_events(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003801 return event;
3802
3803 default:
3804 BUG();
3805 }
3806
3807 return NULL;
3808}
Robert Richterc4f50182008-12-11 16:49:22 +01003809EXPORT_SYMBOL_GPL(ring_buffer_peek);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003810
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003811static struct ring_buffer_event *
3812rb_iter_peek(struct ring_buffer_iter *iter, u64 *ts)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003813{
3814 struct ring_buffer *buffer;
3815 struct ring_buffer_per_cpu *cpu_buffer;
3816 struct ring_buffer_event *event;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003817 int nr_loops = 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003818
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003819 cpu_buffer = iter->cpu_buffer;
3820 buffer = cpu_buffer->buffer;
3821
Steven Rostedt492a74f2010-01-25 15:17:47 -05003822 /*
3823 * Check if someone performed a consuming read to
3824 * the buffer. A consuming read invalidates the iterator
3825 * and we need to reset the iterator in this case.
3826 */
3827 if (unlikely(iter->cache_read != cpu_buffer->read ||
3828 iter->cache_reader_page != cpu_buffer->reader_page))
3829 rb_iter_reset(iter);
3830
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003831 again:
Steven Rostedt3c05d742010-01-26 16:14:08 -05003832 if (ring_buffer_iter_empty(iter))
3833 return NULL;
3834
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003835 /*
Steven Rostedt (Red Hat)021de3d2014-08-06 15:36:31 -04003836 * We repeat when a time extend is encountered or we hit
3837 * the end of the page. Since the time extend is always attached
3838 * to a data event, we should never loop more than three times.
3839 * Once for going to next page, once on time extend, and
3840 * finally once to get the event.
3841 * (We never hit the following condition more than thrice).
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003842 */
Steven Rostedt (Red Hat)021de3d2014-08-06 15:36:31 -04003843 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 3))
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003844 return NULL;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003845
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003846 if (rb_per_cpu_empty(cpu_buffer))
3847 return NULL;
3848
Steven Rostedt (Red Hat)10e83fd2014-07-23 19:45:12 -04003849 if (iter->head >= rb_page_size(iter->head_page)) {
Steven Rostedt3c05d742010-01-26 16:14:08 -05003850 rb_inc_iter(iter);
3851 goto again;
3852 }
3853
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003854 event = rb_iter_head_event(iter);
3855
Lai Jiangshan334d4162009-04-24 11:27:05 +08003856 switch (event->type_len) {
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003857 case RINGBUF_TYPE_PADDING:
Tom Zanussi2d622712009-03-22 03:30:49 -05003858 if (rb_null_event(event)) {
3859 rb_inc_iter(iter);
3860 goto again;
3861 }
3862 rb_advance_iter(iter);
3863 return event;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003864
3865 case RINGBUF_TYPE_TIME_EXTEND:
3866 /* Internal data, OK to advance */
3867 rb_advance_iter(iter);
3868 goto again;
3869
3870 case RINGBUF_TYPE_TIME_STAMP:
3871 /* FIXME: not implemented */
3872 rb_advance_iter(iter);
3873 goto again;
3874
3875 case RINGBUF_TYPE_DATA:
3876 if (ts) {
3877 *ts = iter->read_stamp + event->time_delta;
Steven Rostedt37886f62009-03-17 17:22:06 -04003878 ring_buffer_normalize_time_stamp(buffer,
3879 cpu_buffer->cpu, ts);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003880 }
3881 return event;
3882
3883 default:
3884 BUG();
3885 }
3886
3887 return NULL;
3888}
Robert Richterc4f50182008-12-11 16:49:22 +01003889EXPORT_SYMBOL_GPL(ring_buffer_iter_peek);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003890
Steven Rostedt (Red Hat)289a5a22015-05-28 13:14:51 -04003891static inline bool rb_reader_lock(struct ring_buffer_per_cpu *cpu_buffer)
Steven Rostedt8d707e82009-06-16 21:22:48 -04003892{
Steven Rostedt (Red Hat)289a5a22015-05-28 13:14:51 -04003893 if (likely(!in_nmi())) {
3894 raw_spin_lock(&cpu_buffer->reader_lock);
3895 return true;
3896 }
3897
Steven Rostedt8d707e82009-06-16 21:22:48 -04003898 /*
3899 * If an NMI die dumps out the content of the ring buffer
Steven Rostedt (Red Hat)289a5a22015-05-28 13:14:51 -04003900 * trylock must be used to prevent a deadlock if the NMI
3901 * preempted a task that holds the ring buffer locks. If
3902 * we get the lock then all is fine, if not, then continue
3903 * to do the read, but this can corrupt the ring buffer,
3904 * so it must be permanently disabled from future writes.
3905 * Reading from NMI is a oneshot deal.
Steven Rostedt8d707e82009-06-16 21:22:48 -04003906 */
Steven Rostedt (Red Hat)289a5a22015-05-28 13:14:51 -04003907 if (raw_spin_trylock(&cpu_buffer->reader_lock))
3908 return true;
Steven Rostedt8d707e82009-06-16 21:22:48 -04003909
Steven Rostedt (Red Hat)289a5a22015-05-28 13:14:51 -04003910 /* Continue without locking, but disable the ring buffer */
3911 atomic_inc(&cpu_buffer->record_disabled);
3912 return false;
3913}
3914
3915static inline void
3916rb_reader_unlock(struct ring_buffer_per_cpu *cpu_buffer, bool locked)
3917{
3918 if (likely(locked))
3919 raw_spin_unlock(&cpu_buffer->reader_lock);
3920 return;
Steven Rostedt8d707e82009-06-16 21:22:48 -04003921}
3922
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003923/**
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003924 * ring_buffer_peek - peek at the next event to be read
3925 * @buffer: The ring buffer to read
3926 * @cpu: The cpu to peak at
3927 * @ts: The timestamp counter of this event.
Steven Rostedt66a8cb92010-03-31 13:21:56 -04003928 * @lost_events: a variable to store if events were lost (may be NULL)
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003929 *
3930 * This will return the event that will be read next, but does
3931 * not consume the data.
3932 */
3933struct ring_buffer_event *
Steven Rostedt66a8cb92010-03-31 13:21:56 -04003934ring_buffer_peek(struct ring_buffer *buffer, int cpu, u64 *ts,
3935 unsigned long *lost_events)
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003936{
3937 struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
Steven Rostedt8aabee52009-03-12 13:13:49 -04003938 struct ring_buffer_event *event;
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003939 unsigned long flags;
Steven Rostedt (Red Hat)289a5a22015-05-28 13:14:51 -04003940 bool dolock;
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003941
Steven Rostedt554f7862009-03-11 22:00:13 -04003942 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04003943 return NULL;
Steven Rostedt554f7862009-03-11 22:00:13 -04003944
Tom Zanussi2d622712009-03-22 03:30:49 -05003945 again:
Steven Rostedt8d707e82009-06-16 21:22:48 -04003946 local_irq_save(flags);
Steven Rostedt (Red Hat)289a5a22015-05-28 13:14:51 -04003947 dolock = rb_reader_lock(cpu_buffer);
Steven Rostedt66a8cb92010-03-31 13:21:56 -04003948 event = rb_buffer_peek(cpu_buffer, ts, lost_events);
Robert Richter469535a2009-07-30 19:19:18 +02003949 if (event && event->type_len == RINGBUF_TYPE_PADDING)
3950 rb_advance_reader(cpu_buffer);
Steven Rostedt (Red Hat)289a5a22015-05-28 13:14:51 -04003951 rb_reader_unlock(cpu_buffer, dolock);
Steven Rostedt8d707e82009-06-16 21:22:48 -04003952 local_irq_restore(flags);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003953
Steven Rostedt1b959e12009-09-03 10:12:13 -04003954 if (event && event->type_len == RINGBUF_TYPE_PADDING)
Tom Zanussi2d622712009-03-22 03:30:49 -05003955 goto again;
Tom Zanussi2d622712009-03-22 03:30:49 -05003956
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003957 return event;
3958}
3959
3960/**
3961 * ring_buffer_iter_peek - peek at the next event to be read
3962 * @iter: The ring buffer iterator
3963 * @ts: The timestamp counter of this event.
3964 *
3965 * This will return the event that will be read next, but does
3966 * not increment the iterator.
3967 */
3968struct ring_buffer_event *
3969ring_buffer_iter_peek(struct ring_buffer_iter *iter, u64 *ts)
3970{
3971 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
3972 struct ring_buffer_event *event;
3973 unsigned long flags;
3974
Tom Zanussi2d622712009-03-22 03:30:49 -05003975 again:
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02003976 raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003977 event = rb_iter_peek(iter, ts);
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02003978 raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003979
Steven Rostedt1b959e12009-09-03 10:12:13 -04003980 if (event && event->type_len == RINGBUF_TYPE_PADDING)
Tom Zanussi2d622712009-03-22 03:30:49 -05003981 goto again;
Tom Zanussi2d622712009-03-22 03:30:49 -05003982
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003983 return event;
3984}
3985
3986/**
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003987 * ring_buffer_consume - return an event and consume it
3988 * @buffer: The ring buffer to get the next event from
Steven Rostedt66a8cb92010-03-31 13:21:56 -04003989 * @cpu: the cpu to read the buffer from
3990 * @ts: a variable to store the timestamp (may be NULL)
3991 * @lost_events: a variable to store if events were lost (may be NULL)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003992 *
3993 * Returns the next event in the ring buffer, and that event is consumed.
3994 * Meaning, that sequential reads will keep returning a different event,
3995 * and eventually empty the ring buffer if the producer is slower.
3996 */
3997struct ring_buffer_event *
Steven Rostedt66a8cb92010-03-31 13:21:56 -04003998ring_buffer_consume(struct ring_buffer *buffer, int cpu, u64 *ts,
3999 unsigned long *lost_events)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004000{
Steven Rostedt554f7862009-03-11 22:00:13 -04004001 struct ring_buffer_per_cpu *cpu_buffer;
4002 struct ring_buffer_event *event = NULL;
Steven Rostedtf83c9d02008-11-11 18:47:44 +01004003 unsigned long flags;
Steven Rostedt (Red Hat)289a5a22015-05-28 13:14:51 -04004004 bool dolock;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004005
Tom Zanussi2d622712009-03-22 03:30:49 -05004006 again:
Steven Rostedt554f7862009-03-11 22:00:13 -04004007 /* might be called in atomic */
4008 preempt_disable();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004009
Steven Rostedt554f7862009-03-11 22:00:13 -04004010 if (!cpumask_test_cpu(cpu, buffer->cpumask))
4011 goto out;
4012
4013 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt8d707e82009-06-16 21:22:48 -04004014 local_irq_save(flags);
Steven Rostedt (Red Hat)289a5a22015-05-28 13:14:51 -04004015 dolock = rb_reader_lock(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004016
Steven Rostedt66a8cb92010-03-31 13:21:56 -04004017 event = rb_buffer_peek(cpu_buffer, ts, lost_events);
4018 if (event) {
4019 cpu_buffer->lost_events = 0;
Robert Richter469535a2009-07-30 19:19:18 +02004020 rb_advance_reader(cpu_buffer);
Steven Rostedt66a8cb92010-03-31 13:21:56 -04004021 }
Steven Rostedtf83c9d02008-11-11 18:47:44 +01004022
Steven Rostedt (Red Hat)289a5a22015-05-28 13:14:51 -04004023 rb_reader_unlock(cpu_buffer, dolock);
Steven Rostedt8d707e82009-06-16 21:22:48 -04004024 local_irq_restore(flags);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01004025
Steven Rostedt554f7862009-03-11 22:00:13 -04004026 out:
4027 preempt_enable();
4028
Steven Rostedt1b959e12009-09-03 10:12:13 -04004029 if (event && event->type_len == RINGBUF_TYPE_PADDING)
Tom Zanussi2d622712009-03-22 03:30:49 -05004030 goto again;
Tom Zanussi2d622712009-03-22 03:30:49 -05004031
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004032 return event;
4033}
Robert Richterc4f50182008-12-11 16:49:22 +01004034EXPORT_SYMBOL_GPL(ring_buffer_consume);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004035
4036/**
David Miller72c9ddf2010-04-20 15:47:11 -07004037 * ring_buffer_read_prepare - Prepare for a non consuming read of the buffer
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004038 * @buffer: The ring buffer to read from
4039 * @cpu: The cpu buffer to iterate over
Douglas Anderson3085d412019-03-08 11:32:04 -08004040 * @flags: gfp flags to use for memory allocation
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004041 *
David Miller72c9ddf2010-04-20 15:47:11 -07004042 * This performs the initial preparations necessary to iterate
4043 * through the buffer. Memory is allocated, buffer recording
4044 * is disabled, and the iterator pointer is returned to the caller.
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004045 *
David Miller72c9ddf2010-04-20 15:47:11 -07004046 * Disabling buffer recordng prevents the reading from being
4047 * corrupted. This is not a consuming read, so a producer is not
4048 * expected.
4049 *
4050 * After a sequence of ring_buffer_read_prepare calls, the user is
zhangwei(Jovi)d6118512013-07-15 16:32:50 +08004051 * expected to make at least one call to ring_buffer_read_prepare_sync.
David Miller72c9ddf2010-04-20 15:47:11 -07004052 * Afterwards, ring_buffer_read_start is invoked to get things going
4053 * for real.
4054 *
zhangwei(Jovi)d6118512013-07-15 16:32:50 +08004055 * This overall must be paired with ring_buffer_read_finish.
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004056 */
4057struct ring_buffer_iter *
Douglas Anderson3085d412019-03-08 11:32:04 -08004058ring_buffer_read_prepare(struct ring_buffer *buffer, int cpu, gfp_t flags)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004059{
4060 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedt8aabee52009-03-12 13:13:49 -04004061 struct ring_buffer_iter *iter;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004062
Rusty Russell9e01c1b2009-01-01 10:12:22 +10304063 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04004064 return NULL;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004065
Douglas Anderson3085d412019-03-08 11:32:04 -08004066 iter = kmalloc(sizeof(*iter), flags);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004067 if (!iter)
Steven Rostedt8aabee52009-03-12 13:13:49 -04004068 return NULL;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004069
4070 cpu_buffer = buffer->buffers[cpu];
4071
4072 iter->cpu_buffer = cpu_buffer;
4073
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07004074 atomic_inc(&buffer->resize_disabled);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004075 atomic_inc(&cpu_buffer->record_disabled);
David Miller72c9ddf2010-04-20 15:47:11 -07004076
4077 return iter;
4078}
4079EXPORT_SYMBOL_GPL(ring_buffer_read_prepare);
4080
4081/**
4082 * ring_buffer_read_prepare_sync - Synchronize a set of prepare calls
4083 *
4084 * All previously invoked ring_buffer_read_prepare calls to prepare
4085 * iterators will be synchronized. Afterwards, read_buffer_read_start
4086 * calls on those iterators are allowed.
4087 */
4088void
4089ring_buffer_read_prepare_sync(void)
4090{
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004091 synchronize_sched();
David Miller72c9ddf2010-04-20 15:47:11 -07004092}
4093EXPORT_SYMBOL_GPL(ring_buffer_read_prepare_sync);
4094
4095/**
4096 * ring_buffer_read_start - start a non consuming read of the buffer
4097 * @iter: The iterator returned by ring_buffer_read_prepare
4098 *
4099 * This finalizes the startup of an iteration through the buffer.
4100 * The iterator comes from a call to ring_buffer_read_prepare and
4101 * an intervening ring_buffer_read_prepare_sync must have been
4102 * performed.
4103 *
zhangwei(Jovi)d6118512013-07-15 16:32:50 +08004104 * Must be paired with ring_buffer_read_finish.
David Miller72c9ddf2010-04-20 15:47:11 -07004105 */
4106void
4107ring_buffer_read_start(struct ring_buffer_iter *iter)
4108{
4109 struct ring_buffer_per_cpu *cpu_buffer;
4110 unsigned long flags;
4111
4112 if (!iter)
4113 return;
4114
4115 cpu_buffer = iter->cpu_buffer;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004116
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02004117 raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01004118 arch_spin_lock(&cpu_buffer->lock);
Steven Rostedt642edba2008-11-12 00:01:26 -05004119 rb_iter_reset(iter);
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01004120 arch_spin_unlock(&cpu_buffer->lock);
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02004121 raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004122}
Robert Richterc4f50182008-12-11 16:49:22 +01004123EXPORT_SYMBOL_GPL(ring_buffer_read_start);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004124
4125/**
zhangwei(Jovi)d6118512013-07-15 16:32:50 +08004126 * ring_buffer_read_finish - finish reading the iterator of the buffer
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004127 * @iter: The iterator retrieved by ring_buffer_start
4128 *
4129 * This re-enables the recording to the buffer, and frees the
4130 * iterator.
4131 */
4132void
4133ring_buffer_read_finish(struct ring_buffer_iter *iter)
4134{
4135 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
Steven Rostedt9366c1b2012-11-29 22:31:16 -05004136 unsigned long flags;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004137
Steven Rostedt659f4512012-05-14 17:02:33 -04004138 /*
4139 * Ring buffer is disabled from recording, here's a good place
Steven Rostedt9366c1b2012-11-29 22:31:16 -05004140 * to check the integrity of the ring buffer.
4141 * Must prevent readers from trying to read, as the check
4142 * clears the HEAD page and readers require it.
Steven Rostedt659f4512012-05-14 17:02:33 -04004143 */
Steven Rostedt9366c1b2012-11-29 22:31:16 -05004144 raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
Steven Rostedt659f4512012-05-14 17:02:33 -04004145 rb_check_pages(cpu_buffer);
Steven Rostedt9366c1b2012-11-29 22:31:16 -05004146 raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
Steven Rostedt659f4512012-05-14 17:02:33 -04004147
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004148 atomic_dec(&cpu_buffer->record_disabled);
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07004149 atomic_dec(&cpu_buffer->buffer->resize_disabled);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004150 kfree(iter);
4151}
Robert Richterc4f50182008-12-11 16:49:22 +01004152EXPORT_SYMBOL_GPL(ring_buffer_read_finish);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004153
4154/**
4155 * ring_buffer_read - read the next item in the ring buffer by the iterator
4156 * @iter: The ring buffer iterator
4157 * @ts: The time stamp of the event read.
4158 *
4159 * This reads the next event in the ring buffer and increments the iterator.
4160 */
4161struct ring_buffer_event *
4162ring_buffer_read(struct ring_buffer_iter *iter, u64 *ts)
4163{
4164 struct ring_buffer_event *event;
Steven Rostedtf83c9d02008-11-11 18:47:44 +01004165 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
4166 unsigned long flags;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004167
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02004168 raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
Steven Rostedt7e9391c2009-09-03 10:02:09 -04004169 again:
Steven Rostedtf83c9d02008-11-11 18:47:44 +01004170 event = rb_iter_peek(iter, ts);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004171 if (!event)
Steven Rostedtf83c9d02008-11-11 18:47:44 +01004172 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004173
Steven Rostedt7e9391c2009-09-03 10:02:09 -04004174 if (event->type_len == RINGBUF_TYPE_PADDING)
4175 goto again;
4176
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004177 rb_advance_iter(iter);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01004178 out:
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02004179 raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004180
4181 return event;
4182}
Robert Richterc4f50182008-12-11 16:49:22 +01004183EXPORT_SYMBOL_GPL(ring_buffer_read);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004184
4185/**
4186 * ring_buffer_size - return the size of the ring buffer (in bytes)
4187 * @buffer: The ring buffer.
4188 */
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08004189unsigned long ring_buffer_size(struct ring_buffer *buffer, int cpu)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004190{
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08004191 /*
4192 * Earlier, this method returned
4193 * BUF_PAGE_SIZE * buffer->nr_pages
4194 * Since the nr_pages field is now removed, we have converted this to
4195 * return the per cpu buffer value.
4196 */
4197 if (!cpumask_test_cpu(cpu, buffer->cpumask))
4198 return 0;
4199
4200 return BUF_PAGE_SIZE * buffer->buffers[cpu]->nr_pages;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004201}
Robert Richterc4f50182008-12-11 16:49:22 +01004202EXPORT_SYMBOL_GPL(ring_buffer_size);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004203
4204static void
4205rb_reset_cpu(struct ring_buffer_per_cpu *cpu_buffer)
4206{
Steven Rostedt77ae3652009-03-27 11:00:29 -04004207 rb_head_page_deactivate(cpu_buffer);
4208
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004209 cpu_buffer->head_page
Steven Rostedt3adc54f2009-03-30 15:32:01 -04004210 = list_entry(cpu_buffer->pages, struct buffer_page, list);
Steven Rostedtbf41a152008-10-04 02:00:59 -04004211 local_set(&cpu_buffer->head_page->write, 0);
Steven Rostedt778c55d2009-05-01 18:44:45 -04004212 local_set(&cpu_buffer->head_page->entries, 0);
Steven Rostedtabc9b562008-12-02 15:34:06 -05004213 local_set(&cpu_buffer->head_page->page->commit, 0);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004214
Steven Rostedt6f807ac2008-10-04 02:00:58 -04004215 cpu_buffer->head_page->read = 0;
Steven Rostedtbf41a152008-10-04 02:00:59 -04004216
4217 cpu_buffer->tail_page = cpu_buffer->head_page;
4218 cpu_buffer->commit_page = cpu_buffer->head_page;
4219
4220 INIT_LIST_HEAD(&cpu_buffer->reader_page->list);
Vaibhav Nagarnaik5040b4b2012-05-03 18:59:51 -07004221 INIT_LIST_HEAD(&cpu_buffer->new_pages);
Steven Rostedtbf41a152008-10-04 02:00:59 -04004222 local_set(&cpu_buffer->reader_page->write, 0);
Steven Rostedt778c55d2009-05-01 18:44:45 -04004223 local_set(&cpu_buffer->reader_page->entries, 0);
Steven Rostedtabc9b562008-12-02 15:34:06 -05004224 local_set(&cpu_buffer->reader_page->page->commit, 0);
Steven Rostedt6f807ac2008-10-04 02:00:58 -04004225 cpu_buffer->reader_page->read = 0;
Steven Rostedtd7690412008-10-01 00:29:53 -04004226
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -07004227 local_set(&cpu_buffer->entries_bytes, 0);
Steven Rostedt77ae3652009-03-27 11:00:29 -04004228 local_set(&cpu_buffer->overrun, 0);
Slava Pestov884bfe82011-07-15 14:23:58 -07004229 local_set(&cpu_buffer->commit_overrun, 0);
4230 local_set(&cpu_buffer->dropped_events, 0);
Steven Rostedte4906ef2009-04-30 20:49:44 -04004231 local_set(&cpu_buffer->entries, 0);
Steven Rostedtfa743952009-06-16 12:37:57 -04004232 local_set(&cpu_buffer->committing, 0);
4233 local_set(&cpu_buffer->commits, 0);
Steven Rostedt77ae3652009-03-27 11:00:29 -04004234 cpu_buffer->read = 0;
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -07004235 cpu_buffer->read_bytes = 0;
Steven Rostedt69507c02009-01-21 18:45:57 -05004236
4237 cpu_buffer->write_stamp = 0;
4238 cpu_buffer->read_stamp = 0;
Steven Rostedt77ae3652009-03-27 11:00:29 -04004239
Steven Rostedt66a8cb92010-03-31 13:21:56 -04004240 cpu_buffer->lost_events = 0;
4241 cpu_buffer->last_overrun = 0;
4242
Steven Rostedt77ae3652009-03-27 11:00:29 -04004243 rb_head_page_activate(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004244}
4245
4246/**
4247 * ring_buffer_reset_cpu - reset a ring buffer per CPU buffer
4248 * @buffer: The ring buffer to reset a per cpu buffer of
4249 * @cpu: The CPU buffer to be reset
4250 */
4251void ring_buffer_reset_cpu(struct ring_buffer *buffer, int cpu)
4252{
4253 struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
4254 unsigned long flags;
4255
Rusty Russell9e01c1b2009-01-01 10:12:22 +10304256 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04004257 return;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004258
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07004259 atomic_inc(&buffer->resize_disabled);
Steven Rostedt41ede232009-05-01 20:26:54 -04004260 atomic_inc(&cpu_buffer->record_disabled);
4261
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07004262 /* Make sure all commits have finished */
4263 synchronize_sched();
4264
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02004265 raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01004266
Steven Rostedt41b6a952009-09-02 09:59:48 -04004267 if (RB_WARN_ON(cpu_buffer, local_read(&cpu_buffer->committing)))
4268 goto out;
4269
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01004270 arch_spin_lock(&cpu_buffer->lock);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004271
4272 rb_reset_cpu(cpu_buffer);
4273
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01004274 arch_spin_unlock(&cpu_buffer->lock);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01004275
Steven Rostedt41b6a952009-09-02 09:59:48 -04004276 out:
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02004277 raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
Steven Rostedt41ede232009-05-01 20:26:54 -04004278
4279 atomic_dec(&cpu_buffer->record_disabled);
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07004280 atomic_dec(&buffer->resize_disabled);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004281}
Robert Richterc4f50182008-12-11 16:49:22 +01004282EXPORT_SYMBOL_GPL(ring_buffer_reset_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004283
4284/**
4285 * ring_buffer_reset - reset a ring buffer
4286 * @buffer: The ring buffer to reset all cpu buffers
4287 */
4288void ring_buffer_reset(struct ring_buffer *buffer)
4289{
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004290 int cpu;
4291
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004292 for_each_buffer_cpu(buffer, cpu)
Steven Rostedtd7690412008-10-01 00:29:53 -04004293 ring_buffer_reset_cpu(buffer, cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004294}
Robert Richterc4f50182008-12-11 16:49:22 +01004295EXPORT_SYMBOL_GPL(ring_buffer_reset);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004296
4297/**
4298 * rind_buffer_empty - is the ring buffer empty?
4299 * @buffer: The ring buffer to test
4300 */
Yaowei Bai3d4e2042015-09-29 22:43:32 +08004301bool ring_buffer_empty(struct ring_buffer *buffer)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004302{
4303 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedtd4788202009-06-17 00:39:43 -04004304 unsigned long flags;
Steven Rostedt (Red Hat)289a5a22015-05-28 13:14:51 -04004305 bool dolock;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004306 int cpu;
Steven Rostedtd4788202009-06-17 00:39:43 -04004307 int ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004308
4309 /* yes this is racy, but if you don't like the race, lock the buffer */
4310 for_each_buffer_cpu(buffer, cpu) {
4311 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt8d707e82009-06-16 21:22:48 -04004312 local_irq_save(flags);
Steven Rostedt (Red Hat)289a5a22015-05-28 13:14:51 -04004313 dolock = rb_reader_lock(cpu_buffer);
Steven Rostedtd4788202009-06-17 00:39:43 -04004314 ret = rb_per_cpu_empty(cpu_buffer);
Steven Rostedt (Red Hat)289a5a22015-05-28 13:14:51 -04004315 rb_reader_unlock(cpu_buffer, dolock);
Steven Rostedt8d707e82009-06-16 21:22:48 -04004316 local_irq_restore(flags);
4317
Steven Rostedtd4788202009-06-17 00:39:43 -04004318 if (!ret)
Yaowei Bai3d4e2042015-09-29 22:43:32 +08004319 return false;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004320 }
Steven Rostedt554f7862009-03-11 22:00:13 -04004321
Yaowei Bai3d4e2042015-09-29 22:43:32 +08004322 return true;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004323}
Robert Richterc4f50182008-12-11 16:49:22 +01004324EXPORT_SYMBOL_GPL(ring_buffer_empty);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004325
4326/**
4327 * ring_buffer_empty_cpu - is a cpu buffer of a ring buffer empty?
4328 * @buffer: The ring buffer
4329 * @cpu: The CPU buffer to test
4330 */
Yaowei Bai3d4e2042015-09-29 22:43:32 +08004331bool ring_buffer_empty_cpu(struct ring_buffer *buffer, int cpu)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004332{
4333 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedtd4788202009-06-17 00:39:43 -04004334 unsigned long flags;
Steven Rostedt (Red Hat)289a5a22015-05-28 13:14:51 -04004335 bool dolock;
Steven Rostedt8aabee52009-03-12 13:13:49 -04004336 int ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004337
Rusty Russell9e01c1b2009-01-01 10:12:22 +10304338 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Yaowei Bai3d4e2042015-09-29 22:43:32 +08004339 return true;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004340
4341 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt8d707e82009-06-16 21:22:48 -04004342 local_irq_save(flags);
Steven Rostedt (Red Hat)289a5a22015-05-28 13:14:51 -04004343 dolock = rb_reader_lock(cpu_buffer);
Steven Rostedt554f7862009-03-11 22:00:13 -04004344 ret = rb_per_cpu_empty(cpu_buffer);
Steven Rostedt (Red Hat)289a5a22015-05-28 13:14:51 -04004345 rb_reader_unlock(cpu_buffer, dolock);
Steven Rostedt8d707e82009-06-16 21:22:48 -04004346 local_irq_restore(flags);
Steven Rostedt554f7862009-03-11 22:00:13 -04004347
4348 return ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004349}
Robert Richterc4f50182008-12-11 16:49:22 +01004350EXPORT_SYMBOL_GPL(ring_buffer_empty_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004351
Steven Rostedt85bac322009-09-04 14:24:40 -04004352#ifdef CONFIG_RING_BUFFER_ALLOW_SWAP
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004353/**
4354 * ring_buffer_swap_cpu - swap a CPU buffer between two ring buffers
4355 * @buffer_a: One buffer to swap with
4356 * @buffer_b: The other buffer to swap with
4357 *
4358 * This function is useful for tracers that want to take a "snapshot"
4359 * of a CPU buffer and has another back up buffer lying around.
4360 * it is expected that the tracer handles the cpu buffer not being
4361 * used at the moment.
4362 */
4363int ring_buffer_swap_cpu(struct ring_buffer *buffer_a,
4364 struct ring_buffer *buffer_b, int cpu)
4365{
4366 struct ring_buffer_per_cpu *cpu_buffer_a;
4367 struct ring_buffer_per_cpu *cpu_buffer_b;
Steven Rostedt554f7862009-03-11 22:00:13 -04004368 int ret = -EINVAL;
4369
Rusty Russell9e01c1b2009-01-01 10:12:22 +10304370 if (!cpumask_test_cpu(cpu, buffer_a->cpumask) ||
4371 !cpumask_test_cpu(cpu, buffer_b->cpumask))
Steven Rostedt554f7862009-03-11 22:00:13 -04004372 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004373
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08004374 cpu_buffer_a = buffer_a->buffers[cpu];
4375 cpu_buffer_b = buffer_b->buffers[cpu];
4376
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004377 /* At least make sure the two buffers are somewhat the same */
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08004378 if (cpu_buffer_a->nr_pages != cpu_buffer_b->nr_pages)
Steven Rostedt554f7862009-03-11 22:00:13 -04004379 goto out;
4380
4381 ret = -EAGAIN;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004382
Steven Rostedt97b17ef2009-01-21 15:24:56 -05004383 if (atomic_read(&buffer_a->record_disabled))
Steven Rostedt554f7862009-03-11 22:00:13 -04004384 goto out;
Steven Rostedt97b17ef2009-01-21 15:24:56 -05004385
4386 if (atomic_read(&buffer_b->record_disabled))
Steven Rostedt554f7862009-03-11 22:00:13 -04004387 goto out;
Steven Rostedt97b17ef2009-01-21 15:24:56 -05004388
Steven Rostedt97b17ef2009-01-21 15:24:56 -05004389 if (atomic_read(&cpu_buffer_a->record_disabled))
Steven Rostedt554f7862009-03-11 22:00:13 -04004390 goto out;
Steven Rostedt97b17ef2009-01-21 15:24:56 -05004391
4392 if (atomic_read(&cpu_buffer_b->record_disabled))
Steven Rostedt554f7862009-03-11 22:00:13 -04004393 goto out;
Steven Rostedt97b17ef2009-01-21 15:24:56 -05004394
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004395 /*
4396 * We can't do a synchronize_sched here because this
4397 * function can be called in atomic context.
4398 * Normally this will be called from the same CPU as cpu.
4399 * If not it's up to the caller to protect this.
4400 */
4401 atomic_inc(&cpu_buffer_a->record_disabled);
4402 atomic_inc(&cpu_buffer_b->record_disabled);
4403
Steven Rostedt98277992009-09-02 10:56:15 -04004404 ret = -EBUSY;
4405 if (local_read(&cpu_buffer_a->committing))
4406 goto out_dec;
4407 if (local_read(&cpu_buffer_b->committing))
4408 goto out_dec;
4409
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004410 buffer_a->buffers[cpu] = cpu_buffer_b;
4411 buffer_b->buffers[cpu] = cpu_buffer_a;
4412
4413 cpu_buffer_b->buffer = buffer_a;
4414 cpu_buffer_a->buffer = buffer_b;
4415
Steven Rostedt98277992009-09-02 10:56:15 -04004416 ret = 0;
4417
4418out_dec:
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004419 atomic_dec(&cpu_buffer_a->record_disabled);
4420 atomic_dec(&cpu_buffer_b->record_disabled);
Steven Rostedt554f7862009-03-11 22:00:13 -04004421out:
Steven Rostedt554f7862009-03-11 22:00:13 -04004422 return ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004423}
Robert Richterc4f50182008-12-11 16:49:22 +01004424EXPORT_SYMBOL_GPL(ring_buffer_swap_cpu);
Steven Rostedt85bac322009-09-04 14:24:40 -04004425#endif /* CONFIG_RING_BUFFER_ALLOW_SWAP */
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004426
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004427/**
4428 * ring_buffer_alloc_read_page - allocate a page to read from buffer
4429 * @buffer: the buffer to allocate for.
zhangwei(Jovi)d6118512013-07-15 16:32:50 +08004430 * @cpu: the cpu buffer to allocate.
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004431 *
4432 * This function is used in conjunction with ring_buffer_read_page.
4433 * When reading a full page from the ring buffer, these functions
4434 * can be used to speed up the process. The calling function should
4435 * allocate a few pages first with this function. Then when it
4436 * needs to get pages from the ring buffer, it passes the result
4437 * of this function into ring_buffer_read_page, which will swap
4438 * the page that was allocated, with the read page of the buffer.
4439 *
4440 * Returns:
4441 * The page allocated, or NULL on error.
4442 */
Vaibhav Nagarnaik7ea59062011-05-03 17:56:42 -07004443void *ring_buffer_alloc_read_page(struct ring_buffer *buffer, int cpu)
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004444{
Steven Rostedt044fa782008-12-02 23:50:03 -05004445 struct buffer_data_page *bpage;
Vaibhav Nagarnaik7ea59062011-05-03 17:56:42 -07004446 struct page *page;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004447
Vaibhav Nagarnaikd7ec4bf2011-06-07 17:01:42 -07004448 page = alloc_pages_node(cpu_to_node(cpu),
4449 GFP_KERNEL | __GFP_NORETRY, 0);
Vaibhav Nagarnaik7ea59062011-05-03 17:56:42 -07004450 if (!page)
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004451 return NULL;
4452
Vaibhav Nagarnaik7ea59062011-05-03 17:56:42 -07004453 bpage = page_address(page);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004454
Steven Rostedtef7a4a12009-03-03 00:27:49 -05004455 rb_init_page(bpage);
4456
Steven Rostedt044fa782008-12-02 23:50:03 -05004457 return bpage;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004458}
Steven Rostedtd6ce96d2009-05-05 01:15:24 -04004459EXPORT_SYMBOL_GPL(ring_buffer_alloc_read_page);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004460
4461/**
4462 * ring_buffer_free_read_page - free an allocated read page
4463 * @buffer: the buffer the page was allocate for
4464 * @data: the page to free
4465 *
4466 * Free a page allocated from ring_buffer_alloc_read_page.
4467 */
4468void ring_buffer_free_read_page(struct ring_buffer *buffer, void *data)
4469{
4470 free_page((unsigned long)data);
4471}
Steven Rostedtd6ce96d2009-05-05 01:15:24 -04004472EXPORT_SYMBOL_GPL(ring_buffer_free_read_page);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004473
4474/**
4475 * ring_buffer_read_page - extract a page from the ring buffer
4476 * @buffer: buffer to extract from
4477 * @data_page: the page to use allocated from ring_buffer_alloc_read_page
Steven Rostedtef7a4a12009-03-03 00:27:49 -05004478 * @len: amount to extract
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004479 * @cpu: the cpu of the buffer to extract
4480 * @full: should the extraction only happen when the page is full.
4481 *
4482 * This function will pull out a page from the ring buffer and consume it.
4483 * @data_page must be the address of the variable that was returned
4484 * from ring_buffer_alloc_read_page. This is because the page might be used
4485 * to swap with a page in the ring buffer.
4486 *
4487 * for example:
zhangwei(Jovi)d6118512013-07-15 16:32:50 +08004488 * rpage = ring_buffer_alloc_read_page(buffer, cpu);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004489 * if (!rpage)
4490 * return error;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05004491 * ret = ring_buffer_read_page(buffer, &rpage, len, cpu, 0);
Lai Jiangshan667d2412009-02-09 14:21:17 +08004492 * if (ret >= 0)
4493 * process_page(rpage, ret);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004494 *
4495 * When @full is set, the function will not return true unless
4496 * the writer is off the reader page.
4497 *
4498 * Note: it is up to the calling functions to handle sleeps and wakeups.
4499 * The ring buffer can be used anywhere in the kernel and can not
4500 * blindly call wake_up. The layer that uses the ring buffer must be
4501 * responsible for that.
4502 *
4503 * Returns:
Lai Jiangshan667d2412009-02-09 14:21:17 +08004504 * >=0 if data has been transferred, returns the offset of consumed data.
4505 * <0 if no data has been transferred.
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004506 */
4507int ring_buffer_read_page(struct ring_buffer *buffer,
Steven Rostedtef7a4a12009-03-03 00:27:49 -05004508 void **data_page, size_t len, int cpu, int full)
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004509{
4510 struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
4511 struct ring_buffer_event *event;
Steven Rostedt044fa782008-12-02 23:50:03 -05004512 struct buffer_data_page *bpage;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05004513 struct buffer_page *reader;
Steven Rostedtff0ff842010-03-31 22:11:42 -04004514 unsigned long missed_events;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004515 unsigned long flags;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05004516 unsigned int commit;
Lai Jiangshan667d2412009-02-09 14:21:17 +08004517 unsigned int read;
Steven Rostedt4f3640f2009-03-03 23:52:42 -05004518 u64 save_timestamp;
Lai Jiangshan667d2412009-02-09 14:21:17 +08004519 int ret = -1;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004520
Steven Rostedt554f7862009-03-11 22:00:13 -04004521 if (!cpumask_test_cpu(cpu, buffer->cpumask))
4522 goto out;
4523
Steven Rostedt474d32b2009-03-03 19:51:40 -05004524 /*
4525 * If len is not big enough to hold the page header, then
4526 * we can not copy anything.
4527 */
4528 if (len <= BUF_PAGE_HDR_SIZE)
Steven Rostedt554f7862009-03-11 22:00:13 -04004529 goto out;
Steven Rostedt474d32b2009-03-03 19:51:40 -05004530
4531 len -= BUF_PAGE_HDR_SIZE;
4532
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004533 if (!data_page)
Steven Rostedt554f7862009-03-11 22:00:13 -04004534 goto out;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004535
Steven Rostedt044fa782008-12-02 23:50:03 -05004536 bpage = *data_page;
4537 if (!bpage)
Steven Rostedt554f7862009-03-11 22:00:13 -04004538 goto out;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004539
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02004540 raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004541
Steven Rostedtef7a4a12009-03-03 00:27:49 -05004542 reader = rb_get_reader_page(cpu_buffer);
4543 if (!reader)
Steven Rostedt554f7862009-03-11 22:00:13 -04004544 goto out_unlock;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004545
Steven Rostedtef7a4a12009-03-03 00:27:49 -05004546 event = rb_reader_event(cpu_buffer);
Lai Jiangshan667d2412009-02-09 14:21:17 +08004547
Steven Rostedtef7a4a12009-03-03 00:27:49 -05004548 read = reader->read;
4549 commit = rb_page_commit(reader);
4550
Steven Rostedt66a8cb92010-03-31 13:21:56 -04004551 /* Check if any events were dropped */
Steven Rostedtff0ff842010-03-31 22:11:42 -04004552 missed_events = cpu_buffer->lost_events;
Steven Rostedt66a8cb92010-03-31 13:21:56 -04004553
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004554 /*
Steven Rostedt474d32b2009-03-03 19:51:40 -05004555 * If this page has been partially read or
4556 * if len is not big enough to read the rest of the page or
4557 * a writer is still on the page, then
4558 * we must copy the data from the page to the buffer.
4559 * Otherwise, we can simply swap the page with the one passed in.
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004560 */
Steven Rostedt474d32b2009-03-03 19:51:40 -05004561 if (read || (len < (commit - read)) ||
Steven Rostedtef7a4a12009-03-03 00:27:49 -05004562 cpu_buffer->reader_page == cpu_buffer->commit_page) {
Lai Jiangshan667d2412009-02-09 14:21:17 +08004563 struct buffer_data_page *rpage = cpu_buffer->reader_page->page;
Steven Rostedt474d32b2009-03-03 19:51:40 -05004564 unsigned int rpos = read;
4565 unsigned int pos = 0;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05004566 unsigned int size;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004567
4568 if (full)
Steven Rostedt554f7862009-03-11 22:00:13 -04004569 goto out_unlock;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004570
Steven Rostedtef7a4a12009-03-03 00:27:49 -05004571 if (len > (commit - read))
4572 len = (commit - read);
4573
Steven Rostedt69d1b832010-10-07 18:18:05 -04004574 /* Always keep the time extend and data together */
4575 size = rb_event_ts_length(event);
Steven Rostedtef7a4a12009-03-03 00:27:49 -05004576
4577 if (len < size)
Steven Rostedt554f7862009-03-11 22:00:13 -04004578 goto out_unlock;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05004579
Steven Rostedt4f3640f2009-03-03 23:52:42 -05004580 /* save the current timestamp, since the user will need it */
4581 save_timestamp = cpu_buffer->read_stamp;
4582
Steven Rostedtef7a4a12009-03-03 00:27:49 -05004583 /* Need to copy one event at a time */
4584 do {
David Sharpe1e35922010-12-22 16:38:24 -08004585 /* We need the size of one event, because
4586 * rb_advance_reader only advances by one event,
4587 * whereas rb_event_ts_length may include the size of
4588 * one or two events.
4589 * We have already ensured there's enough space if this
4590 * is a time extend. */
4591 size = rb_event_length(event);
Steven Rostedt474d32b2009-03-03 19:51:40 -05004592 memcpy(bpage->data + pos, rpage->data + rpos, size);
Steven Rostedtef7a4a12009-03-03 00:27:49 -05004593
4594 len -= size;
4595
4596 rb_advance_reader(cpu_buffer);
Steven Rostedt474d32b2009-03-03 19:51:40 -05004597 rpos = reader->read;
4598 pos += size;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05004599
Huang Ying18fab912010-07-28 14:14:01 +08004600 if (rpos >= commit)
4601 break;
4602
Steven Rostedtef7a4a12009-03-03 00:27:49 -05004603 event = rb_reader_event(cpu_buffer);
Steven Rostedt69d1b832010-10-07 18:18:05 -04004604 /* Always keep the time extend and data together */
4605 size = rb_event_ts_length(event);
David Sharpe1e35922010-12-22 16:38:24 -08004606 } while (len >= size);
Lai Jiangshan667d2412009-02-09 14:21:17 +08004607
4608 /* update bpage */
Steven Rostedtef7a4a12009-03-03 00:27:49 -05004609 local_set(&bpage->commit, pos);
Steven Rostedt4f3640f2009-03-03 23:52:42 -05004610 bpage->time_stamp = save_timestamp;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05004611
Steven Rostedt474d32b2009-03-03 19:51:40 -05004612 /* we copied everything to the beginning */
4613 read = 0;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004614 } else {
Steven Rostedtafbab762009-05-01 19:40:05 -04004615 /* update the entry counter */
Steven Rostedt77ae3652009-03-27 11:00:29 -04004616 cpu_buffer->read += rb_page_entries(reader);
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -07004617 cpu_buffer->read_bytes += BUF_PAGE_SIZE;
Steven Rostedtafbab762009-05-01 19:40:05 -04004618
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004619 /* swap the pages */
Steven Rostedt044fa782008-12-02 23:50:03 -05004620 rb_init_page(bpage);
Steven Rostedtef7a4a12009-03-03 00:27:49 -05004621 bpage = reader->page;
4622 reader->page = *data_page;
4623 local_set(&reader->write, 0);
Steven Rostedt778c55d2009-05-01 18:44:45 -04004624 local_set(&reader->entries, 0);
Steven Rostedtef7a4a12009-03-03 00:27:49 -05004625 reader->read = 0;
Steven Rostedt044fa782008-12-02 23:50:03 -05004626 *data_page = bpage;
Steven Rostedtff0ff842010-03-31 22:11:42 -04004627
4628 /*
4629 * Use the real_end for the data size,
4630 * This gives us a chance to store the lost events
4631 * on the page.
4632 */
4633 if (reader->real_end)
4634 local_set(&bpage->commit, reader->real_end);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004635 }
Lai Jiangshan667d2412009-02-09 14:21:17 +08004636 ret = read;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004637
Steven Rostedt66a8cb92010-03-31 13:21:56 -04004638 cpu_buffer->lost_events = 0;
Steven Rostedt2711ca22010-05-21 13:32:26 -04004639
4640 commit = local_read(&bpage->commit);
Steven Rostedt66a8cb92010-03-31 13:21:56 -04004641 /*
4642 * Set a flag in the commit field if we lost events
4643 */
Steven Rostedtff0ff842010-03-31 22:11:42 -04004644 if (missed_events) {
Steven Rostedtff0ff842010-03-31 22:11:42 -04004645 /* If there is room at the end of the page to save the
4646 * missed events, then record it there.
4647 */
4648 if (BUF_PAGE_SIZE - commit >= sizeof(missed_events)) {
4649 memcpy(&bpage->data[commit], &missed_events,
4650 sizeof(missed_events));
4651 local_add(RB_MISSED_STORED, &bpage->commit);
Steven Rostedt2711ca22010-05-21 13:32:26 -04004652 commit += sizeof(missed_events);
Steven Rostedtff0ff842010-03-31 22:11:42 -04004653 }
Steven Rostedt66a8cb92010-03-31 13:21:56 -04004654 local_add(RB_MISSED_EVENTS, &bpage->commit);
Steven Rostedtff0ff842010-03-31 22:11:42 -04004655 }
Steven Rostedt66a8cb92010-03-31 13:21:56 -04004656
Steven Rostedt2711ca22010-05-21 13:32:26 -04004657 /*
4658 * This page may be off to user land. Zero it out here.
4659 */
4660 if (commit < BUF_PAGE_SIZE)
4661 memset(&bpage->data[commit], 0, BUF_PAGE_SIZE - commit);
4662
Steven Rostedt554f7862009-03-11 22:00:13 -04004663 out_unlock:
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02004664 raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004665
Steven Rostedt554f7862009-03-11 22:00:13 -04004666 out:
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004667 return ret;
4668}
Steven Rostedtd6ce96d2009-05-05 01:15:24 -04004669EXPORT_SYMBOL_GPL(ring_buffer_read_page);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004670
Steven Rostedt59222ef2009-03-12 11:46:03 -04004671#ifdef CONFIG_HOTPLUG_CPU
Frederic Weisbecker09c9e842009-03-21 04:33:36 +01004672static int rb_cpu_notify(struct notifier_block *self,
4673 unsigned long action, void *hcpu)
Steven Rostedt554f7862009-03-11 22:00:13 -04004674{
4675 struct ring_buffer *buffer =
4676 container_of(self, struct ring_buffer, cpu_notify);
4677 long cpu = (long)hcpu;
Steven Rostedt (Red Hat)9b94a8f2016-05-12 11:01:24 -04004678 long nr_pages_same;
4679 int cpu_i;
4680 unsigned long nr_pages;
Steven Rostedt554f7862009-03-11 22:00:13 -04004681
4682 switch (action) {
4683 case CPU_UP_PREPARE:
4684 case CPU_UP_PREPARE_FROZEN:
Rusty Russell3f237a72009-06-12 21:15:30 +09304685 if (cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt554f7862009-03-11 22:00:13 -04004686 return NOTIFY_OK;
4687
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08004688 nr_pages = 0;
4689 nr_pages_same = 1;
4690 /* check if all cpu sizes are same */
4691 for_each_buffer_cpu(buffer, cpu_i) {
4692 /* fill in the size from first enabled cpu */
4693 if (nr_pages == 0)
4694 nr_pages = buffer->buffers[cpu_i]->nr_pages;
4695 if (nr_pages != buffer->buffers[cpu_i]->nr_pages) {
4696 nr_pages_same = 0;
4697 break;
4698 }
4699 }
4700 /* allocate minimum pages, user can later expand it */
4701 if (!nr_pages_same)
4702 nr_pages = 2;
Steven Rostedt554f7862009-03-11 22:00:13 -04004703 buffer->buffers[cpu] =
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08004704 rb_allocate_cpu_buffer(buffer, nr_pages, cpu);
Steven Rostedt554f7862009-03-11 22:00:13 -04004705 if (!buffer->buffers[cpu]) {
4706 WARN(1, "failed to allocate ring buffer on CPU %ld\n",
4707 cpu);
4708 return NOTIFY_OK;
4709 }
4710 smp_wmb();
Rusty Russell3f237a72009-06-12 21:15:30 +09304711 cpumask_set_cpu(cpu, buffer->cpumask);
Steven Rostedt554f7862009-03-11 22:00:13 -04004712 break;
4713 case CPU_DOWN_PREPARE:
4714 case CPU_DOWN_PREPARE_FROZEN:
4715 /*
4716 * Do nothing.
4717 * If we were to free the buffer, then the user would
4718 * lose any trace that was in the buffer.
4719 */
4720 break;
4721 default:
4722 break;
4723 }
4724 return NOTIFY_OK;
4725}
4726#endif
Steven Rostedt (Red Hat)6c43e552013-03-15 11:32:53 -04004727
4728#ifdef CONFIG_RING_BUFFER_STARTUP_TEST
4729/*
4730 * This is a basic integrity check of the ring buffer.
4731 * Late in the boot cycle this test will run when configured in.
4732 * It will kick off a thread per CPU that will go into a loop
4733 * writing to the per cpu ring buffer various sizes of data.
4734 * Some of the data will be large items, some small.
4735 *
4736 * Another thread is created that goes into a spin, sending out
4737 * IPIs to the other CPUs to also write into the ring buffer.
4738 * this is to test the nesting ability of the buffer.
4739 *
4740 * Basic stats are recorded and reported. If something in the
4741 * ring buffer should happen that's not expected, a big warning
4742 * is displayed and all ring buffers are disabled.
4743 */
4744static struct task_struct *rb_threads[NR_CPUS] __initdata;
4745
4746struct rb_test_data {
4747 struct ring_buffer *buffer;
4748 unsigned long events;
4749 unsigned long bytes_written;
4750 unsigned long bytes_alloc;
4751 unsigned long bytes_dropped;
4752 unsigned long events_nested;
4753 unsigned long bytes_written_nested;
4754 unsigned long bytes_alloc_nested;
4755 unsigned long bytes_dropped_nested;
4756 int min_size_nested;
4757 int max_size_nested;
4758 int max_size;
4759 int min_size;
4760 int cpu;
4761 int cnt;
4762};
4763
4764static struct rb_test_data rb_data[NR_CPUS] __initdata;
4765
4766/* 1 meg per cpu */
4767#define RB_TEST_BUFFER_SIZE 1048576
4768
4769static char rb_string[] __initdata =
4770 "abcdefghijklmnopqrstuvwxyz1234567890!@#$%^&*()?+\\"
4771 "?+|:';\",.<>/?abcdefghijklmnopqrstuvwxyz1234567890"
4772 "!@#$%^&*()?+\\?+|:';\",.<>/?abcdefghijklmnopqrstuv";
4773
4774static bool rb_test_started __initdata;
4775
4776struct rb_item {
4777 int size;
4778 char str[];
4779};
4780
4781static __init int rb_write_something(struct rb_test_data *data, bool nested)
4782{
4783 struct ring_buffer_event *event;
4784 struct rb_item *item;
4785 bool started;
4786 int event_len;
4787 int size;
4788 int len;
4789 int cnt;
4790
4791 /* Have nested writes different that what is written */
4792 cnt = data->cnt + (nested ? 27 : 0);
4793
4794 /* Multiply cnt by ~e, to make some unique increment */
4795 size = (data->cnt * 68 / 25) % (sizeof(rb_string) - 1);
4796
4797 len = size + sizeof(struct rb_item);
4798
4799 started = rb_test_started;
4800 /* read rb_test_started before checking buffer enabled */
4801 smp_rmb();
4802
4803 event = ring_buffer_lock_reserve(data->buffer, len);
4804 if (!event) {
4805 /* Ignore dropped events before test starts. */
4806 if (started) {
4807 if (nested)
4808 data->bytes_dropped += len;
4809 else
4810 data->bytes_dropped_nested += len;
4811 }
4812 return len;
4813 }
4814
4815 event_len = ring_buffer_event_length(event);
4816
4817 if (RB_WARN_ON(data->buffer, event_len < len))
4818 goto out;
4819
4820 item = ring_buffer_event_data(event);
4821 item->size = size;
4822 memcpy(item->str, rb_string, size);
4823
4824 if (nested) {
4825 data->bytes_alloc_nested += event_len;
4826 data->bytes_written_nested += len;
4827 data->events_nested++;
4828 if (!data->min_size_nested || len < data->min_size_nested)
4829 data->min_size_nested = len;
4830 if (len > data->max_size_nested)
4831 data->max_size_nested = len;
4832 } else {
4833 data->bytes_alloc += event_len;
4834 data->bytes_written += len;
4835 data->events++;
4836 if (!data->min_size || len < data->min_size)
4837 data->max_size = len;
4838 if (len > data->max_size)
4839 data->max_size = len;
4840 }
4841
4842 out:
4843 ring_buffer_unlock_commit(data->buffer, event);
4844
4845 return 0;
4846}
4847
4848static __init int rb_test(void *arg)
4849{
4850 struct rb_test_data *data = arg;
4851
4852 while (!kthread_should_stop()) {
4853 rb_write_something(data, false);
4854 data->cnt++;
4855
4856 set_current_state(TASK_INTERRUPTIBLE);
4857 /* Now sleep between a min of 100-300us and a max of 1ms */
4858 usleep_range(((data->cnt % 3) + 1) * 100, 1000);
4859 }
4860
4861 return 0;
4862}
4863
4864static __init void rb_ipi(void *ignore)
4865{
4866 struct rb_test_data *data;
4867 int cpu = smp_processor_id();
4868
4869 data = &rb_data[cpu];
4870 rb_write_something(data, true);
4871}
4872
4873static __init int rb_hammer_test(void *arg)
4874{
4875 while (!kthread_should_stop()) {
4876
4877 /* Send an IPI to all cpus to write data! */
4878 smp_call_function(rb_ipi, NULL, 1);
4879 /* No sleep, but for non preempt, let others run */
4880 schedule();
4881 }
4882
4883 return 0;
4884}
4885
4886static __init int test_ringbuffer(void)
4887{
4888 struct task_struct *rb_hammer;
4889 struct ring_buffer *buffer;
4890 int cpu;
4891 int ret = 0;
4892
4893 pr_info("Running ring buffer tests...\n");
4894
4895 buffer = ring_buffer_alloc(RB_TEST_BUFFER_SIZE, RB_FL_OVERWRITE);
4896 if (WARN_ON(!buffer))
4897 return 0;
4898
4899 /* Disable buffer so that threads can't write to it yet */
4900 ring_buffer_record_off(buffer);
4901
4902 for_each_online_cpu(cpu) {
4903 rb_data[cpu].buffer = buffer;
4904 rb_data[cpu].cpu = cpu;
4905 rb_data[cpu].cnt = cpu;
4906 rb_threads[cpu] = kthread_create(rb_test, &rb_data[cpu],
4907 "rbtester/%d", cpu);
Wei Yongjun703cebf2016-06-17 17:33:59 +00004908 if (WARN_ON(IS_ERR(rb_threads[cpu]))) {
Steven Rostedt (Red Hat)6c43e552013-03-15 11:32:53 -04004909 pr_cont("FAILED\n");
Wei Yongjun703cebf2016-06-17 17:33:59 +00004910 ret = PTR_ERR(rb_threads[cpu]);
Steven Rostedt (Red Hat)6c43e552013-03-15 11:32:53 -04004911 goto out_free;
4912 }
4913
4914 kthread_bind(rb_threads[cpu], cpu);
4915 wake_up_process(rb_threads[cpu]);
4916 }
4917
4918 /* Now create the rb hammer! */
4919 rb_hammer = kthread_run(rb_hammer_test, NULL, "rbhammer");
Wei Yongjun703cebf2016-06-17 17:33:59 +00004920 if (WARN_ON(IS_ERR(rb_hammer))) {
Steven Rostedt (Red Hat)6c43e552013-03-15 11:32:53 -04004921 pr_cont("FAILED\n");
Wei Yongjun703cebf2016-06-17 17:33:59 +00004922 ret = PTR_ERR(rb_hammer);
Steven Rostedt (Red Hat)6c43e552013-03-15 11:32:53 -04004923 goto out_free;
4924 }
4925
4926 ring_buffer_record_on(buffer);
4927 /*
4928 * Show buffer is enabled before setting rb_test_started.
4929 * Yes there's a small race window where events could be
4930 * dropped and the thread wont catch it. But when a ring
4931 * buffer gets enabled, there will always be some kind of
4932 * delay before other CPUs see it. Thus, we don't care about
4933 * those dropped events. We care about events dropped after
4934 * the threads see that the buffer is active.
4935 */
4936 smp_wmb();
4937 rb_test_started = true;
4938
4939 set_current_state(TASK_INTERRUPTIBLE);
4940 /* Just run for 10 seconds */;
4941 schedule_timeout(10 * HZ);
4942
4943 kthread_stop(rb_hammer);
4944
4945 out_free:
4946 for_each_online_cpu(cpu) {
4947 if (!rb_threads[cpu])
4948 break;
4949 kthread_stop(rb_threads[cpu]);
4950 }
4951 if (ret) {
4952 ring_buffer_free(buffer);
4953 return ret;
4954 }
4955
4956 /* Report! */
4957 pr_info("finished\n");
4958 for_each_online_cpu(cpu) {
4959 struct ring_buffer_event *event;
4960 struct rb_test_data *data = &rb_data[cpu];
4961 struct rb_item *item;
4962 unsigned long total_events;
4963 unsigned long total_dropped;
4964 unsigned long total_written;
4965 unsigned long total_alloc;
4966 unsigned long total_read = 0;
4967 unsigned long total_size = 0;
4968 unsigned long total_len = 0;
4969 unsigned long total_lost = 0;
4970 unsigned long lost;
4971 int big_event_size;
4972 int small_event_size;
4973
4974 ret = -1;
4975
4976 total_events = data->events + data->events_nested;
4977 total_written = data->bytes_written + data->bytes_written_nested;
4978 total_alloc = data->bytes_alloc + data->bytes_alloc_nested;
4979 total_dropped = data->bytes_dropped + data->bytes_dropped_nested;
4980
4981 big_event_size = data->max_size + data->max_size_nested;
4982 small_event_size = data->min_size + data->min_size_nested;
4983
4984 pr_info("CPU %d:\n", cpu);
4985 pr_info(" events: %ld\n", total_events);
4986 pr_info(" dropped bytes: %ld\n", total_dropped);
4987 pr_info(" alloced bytes: %ld\n", total_alloc);
4988 pr_info(" written bytes: %ld\n", total_written);
4989 pr_info(" biggest event: %d\n", big_event_size);
4990 pr_info(" smallest event: %d\n", small_event_size);
4991
4992 if (RB_WARN_ON(buffer, total_dropped))
4993 break;
4994
4995 ret = 0;
4996
4997 while ((event = ring_buffer_consume(buffer, cpu, NULL, &lost))) {
4998 total_lost += lost;
4999 item = ring_buffer_event_data(event);
5000 total_len += ring_buffer_event_length(event);
5001 total_size += item->size + sizeof(struct rb_item);
5002 if (memcmp(&item->str[0], rb_string, item->size) != 0) {
5003 pr_info("FAILED!\n");
5004 pr_info("buffer had: %.*s\n", item->size, item->str);
5005 pr_info("expected: %.*s\n", item->size, rb_string);
5006 RB_WARN_ON(buffer, 1);
5007 ret = -1;
5008 break;
5009 }
5010 total_read++;
5011 }
5012 if (ret)
5013 break;
5014
5015 ret = -1;
5016
5017 pr_info(" read events: %ld\n", total_read);
5018 pr_info(" lost events: %ld\n", total_lost);
5019 pr_info(" total events: %ld\n", total_lost + total_read);
5020 pr_info(" recorded len bytes: %ld\n", total_len);
5021 pr_info(" recorded size bytes: %ld\n", total_size);
5022 if (total_lost)
5023 pr_info(" With dropped events, record len and size may not match\n"
5024 " alloced and written from above\n");
5025 if (!total_lost) {
5026 if (RB_WARN_ON(buffer, total_len != total_alloc ||
5027 total_size != total_written))
5028 break;
5029 }
5030 if (RB_WARN_ON(buffer, total_lost + total_read != total_events))
5031 break;
5032
5033 ret = 0;
5034 }
5035 if (!ret)
5036 pr_info("Ring buffer PASSED!\n");
5037
5038 ring_buffer_free(buffer);
5039 return 0;
5040}
5041
5042late_initcall(test_ringbuffer);
5043#endif /* CONFIG_RING_BUFFER_STARTUP_TEST */