blob: 802bb38d9c81ef294daf5411ba391c1d9d5bff0d [file] [log] [blame]
Steven Rostedt (VMware)bcea3f92018-08-16 11:23:53 -04001// SPDX-License-Identifier: GPL-2.0
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002/*
3 * Generic ring buffer
4 *
5 * Copyright (C) 2008 Steven Rostedt <srostedt@redhat.com>
6 */
Steven Rostedt (Red Hat)af658dc2015-04-29 14:36:05 -04007#include <linux/trace_events.h>
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04008#include <linux/ring_buffer.h>
Ingo Molnar14131f22009-02-26 18:47:11 +01009#include <linux/trace_clock.h>
Ingo Molnare6017572017-02-01 16:36:40 +010010#include <linux/sched/clock.h>
Steven Rostedt0b074362013-01-22 16:58:30 -050011#include <linux/trace_seq.h>
Steven Rostedt7a8e76a2008-09-29 23:02:38 -040012#include <linux/spinlock.h>
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -050013#include <linux/irq_work.h>
Steven Rostedt (VMware)a3566462019-12-02 16:25:27 -050014#include <linux/security.h>
Steven Rostedt7a8e76a2008-09-29 23:02:38 -040015#include <linux/uaccess.h>
Steven Rostedta81bd802009-02-06 01:45:16 -050016#include <linux/hardirq.h>
Steven Rostedt (Red Hat)6c43e552013-03-15 11:32:53 -040017#include <linux/kthread.h> /* for self test */
Steven Rostedt7a8e76a2008-09-29 23:02:38 -040018#include <linux/module.h>
19#include <linux/percpu.h>
20#include <linux/mutex.h>
Steven Rostedt (Red Hat)6c43e552013-03-15 11:32:53 -040021#include <linux/delay.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090022#include <linux/slab.h>
Steven Rostedt7a8e76a2008-09-29 23:02:38 -040023#include <linux/init.h>
24#include <linux/hash.h>
25#include <linux/list.h>
Steven Rostedt554f7862009-03-11 22:00:13 -040026#include <linux/cpu.h>
Steven Rostedt (VMware)927e56d2018-04-04 11:29:57 -040027#include <linux/oom.h>
Steven Rostedt7a8e76a2008-09-29 23:02:38 -040028
Christoph Lameter79615762010-01-05 15:34:50 +090029#include <asm/local.h>
Steven Rostedt182e9f52008-11-03 23:15:56 -050030
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -070031static void update_pages_handler(struct work_struct *work);
32
Steven Rostedt033601a2008-11-21 12:41:55 -050033/*
Steven Rostedtd1b182a2009-04-15 16:53:47 -040034 * The ring buffer header is special. We must manually up keep it.
35 */
36int ring_buffer_print_entry_header(struct trace_seq *s)
37{
Steven Rostedt (Red Hat)c0cd93a2014-11-12 11:49:00 -050038 trace_seq_puts(s, "# compressed entry header\n");
39 trace_seq_puts(s, "\ttype_len : 5 bits\n");
40 trace_seq_puts(s, "\ttime_delta : 27 bits\n");
41 trace_seq_puts(s, "\tarray : 32 bits\n");
42 trace_seq_putc(s, '\n');
43 trace_seq_printf(s, "\tpadding : type == %d\n",
44 RINGBUF_TYPE_PADDING);
45 trace_seq_printf(s, "\ttime_extend : type == %d\n",
46 RINGBUF_TYPE_TIME_EXTEND);
Tom Zanussidc4e2802018-01-15 20:51:40 -060047 trace_seq_printf(s, "\ttime_stamp : type == %d\n",
48 RINGBUF_TYPE_TIME_STAMP);
Steven Rostedt (Red Hat)c0cd93a2014-11-12 11:49:00 -050049 trace_seq_printf(s, "\tdata max type_len == %d\n",
50 RINGBUF_TYPE_DATA_TYPE_LEN_MAX);
Steven Rostedtd1b182a2009-04-15 16:53:47 -040051
Steven Rostedt (Red Hat)c0cd93a2014-11-12 11:49:00 -050052 return !trace_seq_has_overflowed(s);
Steven Rostedtd1b182a2009-04-15 16:53:47 -040053}
54
55/*
Steven Rostedt5cc98542009-03-12 22:24:17 -040056 * The ring buffer is made up of a list of pages. A separate list of pages is
57 * allocated for each CPU. A writer may only write to a buffer that is
58 * associated with the CPU it is currently executing on. A reader may read
59 * from any per cpu buffer.
60 *
61 * The reader is special. For each per cpu buffer, the reader has its own
62 * reader page. When a reader has read the entire reader page, this reader
63 * page is swapped with another page in the ring buffer.
64 *
65 * Now, as long as the writer is off the reader page, the reader can do what
66 * ever it wants with that page. The writer will never write to that page
67 * again (as long as it is out of the ring buffer).
68 *
69 * Here's some silly ASCII art.
70 *
71 * +------+
72 * |reader| RING BUFFER
73 * |page |
74 * +------+ +---+ +---+ +---+
75 * | |-->| |-->| |
76 * +---+ +---+ +---+
77 * ^ |
78 * | |
79 * +---------------+
80 *
81 *
82 * +------+
83 * |reader| RING BUFFER
84 * |page |------------------v
85 * +------+ +---+ +---+ +---+
86 * | |-->| |-->| |
87 * +---+ +---+ +---+
88 * ^ |
89 * | |
90 * +---------------+
91 *
92 *
93 * +------+
94 * |reader| RING BUFFER
95 * |page |------------------v
96 * +------+ +---+ +---+ +---+
97 * ^ | |-->| |-->| |
98 * | +---+ +---+ +---+
99 * | |
100 * | |
101 * +------------------------------+
102 *
103 *
104 * +------+
105 * |buffer| RING BUFFER
106 * |page |------------------v
107 * +------+ +---+ +---+ +---+
108 * ^ | | | |-->| |
109 * | New +---+ +---+ +---+
110 * | Reader------^ |
111 * | page |
112 * +------------------------------+
113 *
114 *
115 * After we make this swap, the reader can hand this page off to the splice
116 * code and be done with it. It can even allocate a new page if it needs to
117 * and swap that into the ring buffer.
118 *
119 * We will be using cmpxchg soon to make all this lockless.
120 *
121 */
122
Steven Rostedt499e5472012-02-22 15:50:28 -0500123/* Used for individual buffers (after the counter) */
124#define RB_BUFFER_OFF (1 << 20)
125
Steven Rostedt474d32b2009-03-03 19:51:40 -0500126#define BUF_PAGE_HDR_SIZE offsetof(struct buffer_data_page, data)
127
Steven Rostedte3d6bf02009-03-03 13:53:07 -0500128#define RB_EVNT_HDR_SIZE (offsetof(struct ring_buffer_event, array))
Andrew Morton67d34722009-01-09 12:27:09 -0800129#define RB_ALIGNMENT 4U
Lai Jiangshan334d4162009-04-24 11:27:05 +0800130#define RB_MAX_SMALL_DATA (RB_ALIGNMENT * RINGBUF_TYPE_DATA_TYPE_LEN_MAX)
Steven Rostedtc7b09302009-06-11 11:12:00 -0400131#define RB_EVNT_MIN_SIZE 8U /* two 32bit words */
Steven Rostedt (VMware)86b3de62019-05-28 09:36:19 -0400132#define RB_ALIGN_DATA __aligned(RB_ALIGNMENT)
James Hogan649508f2012-05-30 12:11:19 +0100133
Lai Jiangshan334d4162009-04-24 11:27:05 +0800134/* define RINGBUF_TYPE_DATA for 'case RINGBUF_TYPE_DATA:' */
135#define RINGBUF_TYPE_DATA 0 ... RINGBUF_TYPE_DATA_TYPE_LEN_MAX
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400136
137enum {
138 RB_LEN_TIME_EXTEND = 8,
Tom Zanussidc4e2802018-01-15 20:51:40 -0600139 RB_LEN_TIME_STAMP = 8,
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400140};
141
Steven Rostedt69d1b832010-10-07 18:18:05 -0400142#define skip_time_extend(event) \
143 ((struct ring_buffer_event *)((char *)event + RB_LEN_TIME_EXTEND))
144
Tom Zanussidc4e2802018-01-15 20:51:40 -0600145#define extended_time(event) \
146 (event->type_len >= RINGBUF_TYPE_TIME_EXTEND)
147
Tom Zanussi2d622712009-03-22 03:30:49 -0500148static inline int rb_null_event(struct ring_buffer_event *event)
149{
Steven Rostedta1863c22009-09-03 10:23:58 -0400150 return event->type_len == RINGBUF_TYPE_PADDING && !event->time_delta;
Tom Zanussi2d622712009-03-22 03:30:49 -0500151}
152
153static void rb_event_set_padding(struct ring_buffer_event *event)
154{
Steven Rostedta1863c22009-09-03 10:23:58 -0400155 /* padding has a NULL time_delta */
Lai Jiangshan334d4162009-04-24 11:27:05 +0800156 event->type_len = RINGBUF_TYPE_PADDING;
Tom Zanussi2d622712009-03-22 03:30:49 -0500157 event->time_delta = 0;
158}
159
Tom Zanussi2d622712009-03-22 03:30:49 -0500160static unsigned
161rb_event_data_length(struct ring_buffer_event *event)
162{
163 unsigned length;
164
Lai Jiangshan334d4162009-04-24 11:27:05 +0800165 if (event->type_len)
166 length = event->type_len * RB_ALIGNMENT;
Tom Zanussi2d622712009-03-22 03:30:49 -0500167 else
168 length = event->array[0];
169 return length + RB_EVNT_HDR_SIZE;
170}
171
Steven Rostedt69d1b832010-10-07 18:18:05 -0400172/*
173 * Return the length of the given event. Will return
174 * the length of the time extend if the event is a
175 * time extend.
176 */
177static inline unsigned
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400178rb_event_length(struct ring_buffer_event *event)
179{
Lai Jiangshan334d4162009-04-24 11:27:05 +0800180 switch (event->type_len) {
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400181 case RINGBUF_TYPE_PADDING:
Tom Zanussi2d622712009-03-22 03:30:49 -0500182 if (rb_null_event(event))
183 /* undefined */
184 return -1;
Lai Jiangshan334d4162009-04-24 11:27:05 +0800185 return event->array[0] + RB_EVNT_HDR_SIZE;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400186
187 case RINGBUF_TYPE_TIME_EXTEND:
188 return RB_LEN_TIME_EXTEND;
189
190 case RINGBUF_TYPE_TIME_STAMP:
191 return RB_LEN_TIME_STAMP;
192
193 case RINGBUF_TYPE_DATA:
Tom Zanussi2d622712009-03-22 03:30:49 -0500194 return rb_event_data_length(event);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400195 default:
Steven Rostedt (VMware)da4d4012020-05-13 15:36:22 -0400196 WARN_ON_ONCE(1);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400197 }
198 /* not hit */
199 return 0;
200}
201
Steven Rostedt69d1b832010-10-07 18:18:05 -0400202/*
203 * Return total length of time extend and data,
204 * or just the event length for all other events.
205 */
206static inline unsigned
207rb_event_ts_length(struct ring_buffer_event *event)
208{
209 unsigned len = 0;
210
Tom Zanussidc4e2802018-01-15 20:51:40 -0600211 if (extended_time(event)) {
Steven Rostedt69d1b832010-10-07 18:18:05 -0400212 /* time extends include the data event after it */
213 len = RB_LEN_TIME_EXTEND;
214 event = skip_time_extend(event);
215 }
216 return len + rb_event_length(event);
217}
218
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400219/**
220 * ring_buffer_event_length - return the length of the event
221 * @event: the event to get the length of
Steven Rostedt69d1b832010-10-07 18:18:05 -0400222 *
223 * Returns the size of the data load of a data event.
224 * If the event is something other than a data event, it
225 * returns the size of the event itself. With the exception
226 * of a TIME EXTEND, where it still returns the size of the
227 * data load of the data event after it.
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400228 */
229unsigned ring_buffer_event_length(struct ring_buffer_event *event)
230{
Steven Rostedt69d1b832010-10-07 18:18:05 -0400231 unsigned length;
232
Tom Zanussidc4e2802018-01-15 20:51:40 -0600233 if (extended_time(event))
Steven Rostedt69d1b832010-10-07 18:18:05 -0400234 event = skip_time_extend(event);
235
236 length = rb_event_length(event);
Lai Jiangshan334d4162009-04-24 11:27:05 +0800237 if (event->type_len > RINGBUF_TYPE_DATA_TYPE_LEN_MAX)
Robert Richter465634a2009-01-07 15:32:11 +0100238 return length;
239 length -= RB_EVNT_HDR_SIZE;
240 if (length > RB_MAX_SMALL_DATA + sizeof(event->array[0]))
241 length -= sizeof(event->array[0]);
242 return length;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400243}
Robert Richterc4f50182008-12-11 16:49:22 +0100244EXPORT_SYMBOL_GPL(ring_buffer_event_length);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400245
246/* inline for ring buffer fast paths */
Steven Rostedt (Red Hat)929ddbf2016-11-23 11:40:34 -0500247static __always_inline void *
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400248rb_event_data(struct ring_buffer_event *event)
249{
Tom Zanussidc4e2802018-01-15 20:51:40 -0600250 if (extended_time(event))
Steven Rostedt69d1b832010-10-07 18:18:05 -0400251 event = skip_time_extend(event);
Steven Rostedt (VMware)da4d4012020-05-13 15:36:22 -0400252 WARN_ON_ONCE(event->type_len > RINGBUF_TYPE_DATA_TYPE_LEN_MAX);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400253 /* If length is in len field, then array[0] has the data */
Lai Jiangshan334d4162009-04-24 11:27:05 +0800254 if (event->type_len)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400255 return (void *)&event->array[0];
256 /* Otherwise length is in array[0] and array[1] has the data */
257 return (void *)&event->array[1];
258}
259
260/**
261 * ring_buffer_event_data - return the data of the event
262 * @event: the event to get the data from
263 */
264void *ring_buffer_event_data(struct ring_buffer_event *event)
265{
266 return rb_event_data(event);
267}
Robert Richterc4f50182008-12-11 16:49:22 +0100268EXPORT_SYMBOL_GPL(ring_buffer_event_data);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400269
270#define for_each_buffer_cpu(buffer, cpu) \
Rusty Russell9e01c1b2009-01-01 10:12:22 +1030271 for_each_cpu(cpu, buffer->cpumask)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400272
273#define TS_SHIFT 27
274#define TS_MASK ((1ULL << TS_SHIFT) - 1)
275#define TS_DELTA_TEST (~TS_MASK)
276
Tom Zanussidc4e2802018-01-15 20:51:40 -0600277/**
278 * ring_buffer_event_time_stamp - return the event's extended timestamp
279 * @event: the event to get the timestamp of
280 *
281 * Returns the extended timestamp associated with a data event.
282 * An extended time_stamp is a 64-bit timestamp represented
283 * internally in a special way that makes the best use of space
284 * contained within a ring buffer event. This function decodes
285 * it and maps it to a straight u64 value.
286 */
287u64 ring_buffer_event_time_stamp(struct ring_buffer_event *event)
288{
289 u64 ts;
290
291 ts = event->array[0];
292 ts <<= TS_SHIFT;
293 ts += event->time_delta;
294
295 return ts;
296}
297
Steven Rostedt66a8cb92010-03-31 13:21:56 -0400298/* Flag when events were overwritten */
299#define RB_MISSED_EVENTS (1 << 31)
Steven Rostedtff0ff842010-03-31 22:11:42 -0400300/* Missed count stored at end */
301#define RB_MISSED_STORED (1 << 30)
Steven Rostedt66a8cb92010-03-31 13:21:56 -0400302
Steven Rostedtabc9b562008-12-02 15:34:06 -0500303struct buffer_data_page {
Steven Rostedte4c2ce82008-10-01 11:14:54 -0400304 u64 time_stamp; /* page time stamp */
Wenji Huangc3706f02009-02-10 01:03:18 -0500305 local_t commit; /* write committed index */
James Hogan649508f2012-05-30 12:11:19 +0100306 unsigned char data[] RB_ALIGN_DATA; /* data of buffer page */
Steven Rostedtabc9b562008-12-02 15:34:06 -0500307};
308
Steven Rostedt77ae3652009-03-27 11:00:29 -0400309/*
310 * Note, the buffer_page list must be first. The buffer pages
311 * are allocated in cache lines, which means that each buffer
312 * page will be at the beginning of a cache line, and thus
313 * the least significant bits will be zero. We use this to
314 * add flags in the list struct pointers, to make the ring buffer
315 * lockless.
316 */
Steven Rostedtabc9b562008-12-02 15:34:06 -0500317struct buffer_page {
Steven Rostedt778c55d2009-05-01 18:44:45 -0400318 struct list_head list; /* list of buffer pages */
Steven Rostedtabc9b562008-12-02 15:34:06 -0500319 local_t write; /* index for next write */
Steven Rostedt6f807ac2008-10-04 02:00:58 -0400320 unsigned read; /* index for next read */
Steven Rostedt778c55d2009-05-01 18:44:45 -0400321 local_t entries; /* entries on this page */
Steven Rostedtff0ff842010-03-31 22:11:42 -0400322 unsigned long real_end; /* real end of data */
Steven Rostedtabc9b562008-12-02 15:34:06 -0500323 struct buffer_data_page *page; /* Actual data page */
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400324};
325
Steven Rostedt77ae3652009-03-27 11:00:29 -0400326/*
327 * The buffer page counters, write and entries, must be reset
328 * atomically when crossing page boundaries. To synchronize this
329 * update, two counters are inserted into the number. One is
330 * the actual counter for the write position or count on the page.
331 *
332 * The other is a counter of updaters. Before an update happens
333 * the update partition of the counter is incremented. This will
334 * allow the updater to update the counter atomically.
335 *
336 * The counter is 20 bits, and the state data is 12.
337 */
338#define RB_WRITE_MASK 0xfffff
339#define RB_WRITE_INTCNT (1 << 20)
340
Steven Rostedt044fa782008-12-02 23:50:03 -0500341static void rb_init_page(struct buffer_data_page *bpage)
Steven Rostedtabc9b562008-12-02 15:34:06 -0500342{
Steven Rostedt044fa782008-12-02 23:50:03 -0500343 local_set(&bpage->commit, 0);
Steven Rostedtabc9b562008-12-02 15:34:06 -0500344}
345
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400346/*
Steven Rostedted568292008-09-29 23:02:40 -0400347 * Also stolen from mm/slob.c. Thanks to Mathieu Desnoyers for pointing
348 * this issue out.
349 */
Andrew Morton34a148b2009-01-09 12:27:09 -0800350static void free_buffer_page(struct buffer_page *bpage)
Steven Rostedted568292008-09-29 23:02:40 -0400351{
Andrew Morton34a148b2009-01-09 12:27:09 -0800352 free_page((unsigned long)bpage->page);
Steven Rostedte4c2ce82008-10-01 11:14:54 -0400353 kfree(bpage);
Steven Rostedted568292008-09-29 23:02:40 -0400354}
355
356/*
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400357 * We need to fit the time_stamp delta into 27 bits.
358 */
359static inline int test_time_stamp(u64 delta)
360{
361 if (delta & TS_DELTA_TEST)
362 return 1;
363 return 0;
364}
365
Steven Rostedt474d32b2009-03-03 19:51:40 -0500366#define BUF_PAGE_SIZE (PAGE_SIZE - BUF_PAGE_HDR_SIZE)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400367
Steven Rostedtbe957c42009-05-11 14:42:53 -0400368/* Max payload is BUF_PAGE_SIZE - header (8bytes) */
369#define BUF_MAX_DATA_SIZE (BUF_PAGE_SIZE - (sizeof(u32) * 2))
370
Steven Rostedtd1b182a2009-04-15 16:53:47 -0400371int ring_buffer_print_page_header(struct trace_seq *s)
372{
373 struct buffer_data_page field;
Steven Rostedtd1b182a2009-04-15 16:53:47 -0400374
Steven Rostedt (Red Hat)c0cd93a2014-11-12 11:49:00 -0500375 trace_seq_printf(s, "\tfield: u64 timestamp;\t"
376 "offset:0;\tsize:%u;\tsigned:%u;\n",
377 (unsigned int)sizeof(field.time_stamp),
378 (unsigned int)is_signed_type(u64));
Steven Rostedtd1b182a2009-04-15 16:53:47 -0400379
Steven Rostedt (Red Hat)c0cd93a2014-11-12 11:49:00 -0500380 trace_seq_printf(s, "\tfield: local_t commit;\t"
381 "offset:%u;\tsize:%u;\tsigned:%u;\n",
382 (unsigned int)offsetof(typeof(field), commit),
383 (unsigned int)sizeof(field.commit),
384 (unsigned int)is_signed_type(long));
Steven Rostedtd1b182a2009-04-15 16:53:47 -0400385
Steven Rostedt (Red Hat)c0cd93a2014-11-12 11:49:00 -0500386 trace_seq_printf(s, "\tfield: int overwrite;\t"
387 "offset:%u;\tsize:%u;\tsigned:%u;\n",
388 (unsigned int)offsetof(typeof(field), commit),
389 1,
390 (unsigned int)is_signed_type(long));
Steven Rostedt66a8cb92010-03-31 13:21:56 -0400391
Steven Rostedt (Red Hat)c0cd93a2014-11-12 11:49:00 -0500392 trace_seq_printf(s, "\tfield: char data;\t"
393 "offset:%u;\tsize:%u;\tsigned:%u;\n",
394 (unsigned int)offsetof(typeof(field), data),
395 (unsigned int)BUF_PAGE_SIZE,
396 (unsigned int)is_signed_type(char));
Steven Rostedtd1b182a2009-04-15 16:53:47 -0400397
Steven Rostedt (Red Hat)c0cd93a2014-11-12 11:49:00 -0500398 return !trace_seq_has_overflowed(s);
Steven Rostedtd1b182a2009-04-15 16:53:47 -0400399}
400
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -0500401struct rb_irq_work {
402 struct irq_work work;
403 wait_queue_head_t waiters;
Steven Rostedt (Red Hat)1e0d6712015-02-10 22:14:53 -0500404 wait_queue_head_t full_waiters;
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -0500405 bool waiters_pending;
Steven Rostedt (Red Hat)1e0d6712015-02-10 22:14:53 -0500406 bool full_waiters_pending;
407 bool wakeup_full;
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -0500408};
409
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400410/*
Steven Rostedt (Red Hat)fcc742e2015-05-28 17:13:14 -0400411 * Structure to hold event state and handle nested events.
412 */
413struct rb_event_info {
414 u64 ts;
415 u64 delta;
416 unsigned long length;
417 struct buffer_page *tail_page;
418 int add_timestamp;
419};
420
421/*
Steven Rostedt (VMware)a389d862020-06-28 22:52:25 -0400422 * Used for the add_timestamp
423 * NONE
Steven Rostedt (VMware)7c4b4a52020-06-28 22:52:26 -0400424 * EXTEND - wants a time extend
425 * ABSOLUTE - the buffer requests all events to have absolute time stamps
Steven Rostedt (VMware)a389d862020-06-28 22:52:25 -0400426 * FORCE - force a full time stamp.
427 */
428enum {
Steven Rostedt (VMware)7c4b4a52020-06-28 22:52:26 -0400429 RB_ADD_STAMP_NONE = 0,
430 RB_ADD_STAMP_EXTEND = BIT(1),
431 RB_ADD_STAMP_ABSOLUTE = BIT(2),
432 RB_ADD_STAMP_FORCE = BIT(3)
Steven Rostedt (VMware)a389d862020-06-28 22:52:25 -0400433};
434/*
Steven Rostedt (Red Hat)a497adb2015-05-29 10:32:28 -0400435 * Used for which event context the event is in.
436 * NMI = 0
437 * IRQ = 1
438 * SOFTIRQ = 2
439 * NORMAL = 3
440 *
441 * See trace_recursive_lock() comment below for more details.
442 */
443enum {
444 RB_CTX_NMI,
445 RB_CTX_IRQ,
446 RB_CTX_SOFTIRQ,
447 RB_CTX_NORMAL,
448 RB_CTX_MAX
449};
450
Steven Rostedt (VMware)10464b42020-06-28 22:52:27 -0400451#if BITS_PER_LONG == 32
452#define RB_TIME_32
453#endif
454
455/* To test on 64 bit machines */
456//#define RB_TIME_32
457
458#ifdef RB_TIME_32
459
460struct rb_time_struct {
461 local_t cnt;
462 local_t top;
463 local_t bottom;
464};
465#else
466#include <asm/local64.h>
467struct rb_time_struct {
468 local64_t time;
469};
470#endif
471typedef struct rb_time_struct rb_time_t;
472
Steven Rostedt (Red Hat)a497adb2015-05-29 10:32:28 -0400473/*
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400474 * head_page == tail_page && head == tail then buffer is empty.
475 */
476struct ring_buffer_per_cpu {
477 int cpu;
Richard Kennedy985023d2010-03-25 11:27:36 +0000478 atomic_t record_disabled;
Steven Rostedt (VMware)07b8b102020-03-27 16:21:22 -0400479 atomic_t resize_disabled;
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -0500480 struct trace_buffer *buffer;
Thomas Gleixner5389f6f2009-07-25 17:13:33 +0200481 raw_spinlock_t reader_lock; /* serialize readers */
Thomas Gleixner445c8952009-12-02 19:49:50 +0100482 arch_spinlock_t lock;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400483 struct lock_class_key lock_key;
Steven Rostedt (VMware)73a757e2017-05-01 09:35:09 -0400484 struct buffer_data_page *free_page;
Steven Rostedt (Red Hat)9b94a8f2016-05-12 11:01:24 -0400485 unsigned long nr_pages;
Steven Rostedt (Red Hat)58a09ec2015-05-27 10:27:47 -0400486 unsigned int current_context;
Steven Rostedt3adc54f2009-03-30 15:32:01 -0400487 struct list_head *pages;
Steven Rostedt6f807ac2008-10-04 02:00:58 -0400488 struct buffer_page *head_page; /* read from head */
489 struct buffer_page *tail_page; /* write to tail */
Wenji Huangc3706f02009-02-10 01:03:18 -0500490 struct buffer_page *commit_page; /* committed pages */
Steven Rostedtd7690412008-10-01 00:29:53 -0400491 struct buffer_page *reader_page;
Steven Rostedt66a8cb92010-03-31 13:21:56 -0400492 unsigned long lost_events;
493 unsigned long last_overrun;
Steven Rostedt (VMware)8e012062018-02-07 17:26:32 -0500494 unsigned long nest;
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -0700495 local_t entries_bytes;
Steven Rostedte4906ef2009-04-30 20:49:44 -0400496 local_t entries;
Slava Pestov884bfe82011-07-15 14:23:58 -0700497 local_t overrun;
498 local_t commit_overrun;
499 local_t dropped_events;
Steven Rostedtfa743952009-06-16 12:37:57 -0400500 local_t committing;
501 local_t commits;
Steven Rostedt (VMware)2c2b0a72018-11-29 20:32:26 -0500502 local_t pages_touched;
503 local_t pages_read;
Steven Rostedt (VMware)03329f92018-11-29 21:38:42 -0500504 long last_pages_touch;
Steven Rostedt (VMware)2c2b0a72018-11-29 20:32:26 -0500505 size_t shortest_full;
Steven Rostedt77ae3652009-03-27 11:00:29 -0400506 unsigned long read;
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -0700507 unsigned long read_bytes;
Steven Rostedt (VMware)10464b42020-06-28 22:52:27 -0400508 rb_time_t write_stamp;
509 rb_time_t before_stamp;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400510 u64 read_stamp;
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -0800511 /* ring buffer pages to update, > 0 to add, < 0 to remove */
Steven Rostedt (Red Hat)9b94a8f2016-05-12 11:01:24 -0400512 long nr_pages_to_update;
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -0800513 struct list_head new_pages; /* new pages to add */
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -0700514 struct work_struct update_pages_work;
Vaibhav Nagarnaik05fdd702012-05-18 13:29:51 -0700515 struct completion update_done;
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -0500516
517 struct rb_irq_work irq_work;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400518};
519
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -0500520struct trace_buffer {
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400521 unsigned flags;
522 int cpus;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400523 atomic_t record_disabled;
Arnaldo Carvalho de Melo00f62f62009-02-09 17:04:06 -0200524 cpumask_var_t cpumask;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400525
Peter Zijlstra1f8a6a12009-06-08 18:18:39 +0200526 struct lock_class_key *reader_lock_key;
527
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400528 struct mutex mutex;
529
530 struct ring_buffer_per_cpu **buffers;
Steven Rostedt554f7862009-03-11 22:00:13 -0400531
Sebastian Andrzej Siewiorb32614c2016-11-27 00:13:34 +0100532 struct hlist_node node;
Steven Rostedt37886f62009-03-17 17:22:06 -0400533 u64 (*clock)(void);
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -0500534
535 struct rb_irq_work irq_work;
Tom Zanussi00b41452018-01-15 20:51:39 -0600536 bool time_stamp_abs;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400537};
538
539struct ring_buffer_iter {
540 struct ring_buffer_per_cpu *cpu_buffer;
541 unsigned long head;
Steven Rostedt (VMware)785888c2020-03-17 17:32:27 -0400542 unsigned long next_event;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400543 struct buffer_page *head_page;
Steven Rostedt492a74f2010-01-25 15:17:47 -0500544 struct buffer_page *cache_reader_page;
545 unsigned long cache_read;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400546 u64 read_stamp;
Steven Rostedt (VMware)28e3fc52020-03-17 17:32:26 -0400547 u64 page_stamp;
Steven Rostedt (VMware)785888c2020-03-17 17:32:27 -0400548 struct ring_buffer_event *event;
Steven Rostedt (VMware)c9b7a4a2020-03-17 17:32:32 -0400549 int missed_events;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400550};
551
Steven Rostedt (VMware)10464b42020-06-28 22:52:27 -0400552#ifdef RB_TIME_32
553
554/*
555 * On 32 bit machines, local64_t is very expensive. As the ring
556 * buffer doesn't need all the features of a true 64 bit atomic,
557 * on 32 bit, it uses these functions (64 still uses local64_t).
558 *
559 * For the ring buffer, 64 bit required operations for the time is
560 * the following:
561 *
562 * - Only need 59 bits (uses 60 to make it even).
563 * - Reads may fail if it interrupted a modification of the time stamp.
564 * It will succeed if it did not interrupt another write even if
565 * the read itself is interrupted by a write.
566 * It returns whether it was successful or not.
567 *
568 * - Writes always succeed and will overwrite other writes and writes
569 * that were done by events interrupting the current write.
570 *
571 * - A write followed by a read of the same time stamp will always succeed,
572 * but may not contain the same value.
573 *
574 * - A cmpxchg will fail if it interrupted another write or cmpxchg.
575 * Other than that, it acts like a normal cmpxchg.
576 *
577 * The 60 bit time stamp is broken up by 30 bits in a top and bottom half
578 * (bottom being the least significant 30 bits of the 60 bit time stamp).
579 *
580 * The two most significant bits of each half holds a 2 bit counter (0-3).
581 * Each update will increment this counter by one.
582 * When reading the top and bottom, if the two counter bits match then the
583 * top and bottom together make a valid 60 bit number.
584 */
585#define RB_TIME_SHIFT 30
586#define RB_TIME_VAL_MASK ((1 << RB_TIME_SHIFT) - 1)
587
588static inline int rb_time_cnt(unsigned long val)
589{
590 return (val >> RB_TIME_SHIFT) & 3;
591}
592
593static inline u64 rb_time_val(unsigned long top, unsigned long bottom)
594{
595 u64 val;
596
597 val = top & RB_TIME_VAL_MASK;
598 val <<= RB_TIME_SHIFT;
599 val |= bottom & RB_TIME_VAL_MASK;
600
601 return val;
602}
603
604static inline bool __rb_time_read(rb_time_t *t, u64 *ret, unsigned long *cnt)
605{
606 unsigned long top, bottom;
607 unsigned long c;
608
609 /*
610 * If the read is interrupted by a write, then the cnt will
611 * be different. Loop until both top and bottom have been read
612 * without interruption.
613 */
614 do {
615 c = local_read(&t->cnt);
616 top = local_read(&t->top);
617 bottom = local_read(&t->bottom);
618 } while (c != local_read(&t->cnt));
619
620 *cnt = rb_time_cnt(top);
621
622 /* If top and bottom counts don't match, this interrupted a write */
623 if (*cnt != rb_time_cnt(bottom))
624 return false;
625
626 *ret = rb_time_val(top, bottom);
627 return true;
628}
629
630static bool rb_time_read(rb_time_t *t, u64 *ret)
631{
632 unsigned long cnt;
633
634 return __rb_time_read(t, ret, &cnt);
635}
636
637static inline unsigned long rb_time_val_cnt(unsigned long val, unsigned long cnt)
638{
639 return (val & RB_TIME_VAL_MASK) | ((cnt & 3) << RB_TIME_SHIFT);
640}
641
642static inline void rb_time_split(u64 val, unsigned long *top, unsigned long *bottom)
643{
644 *top = (unsigned long)((val >> RB_TIME_SHIFT) & RB_TIME_VAL_MASK);
645 *bottom = (unsigned long)(val & RB_TIME_VAL_MASK);
646}
647
648static inline void rb_time_val_set(local_t *t, unsigned long val, unsigned long cnt)
649{
650 val = rb_time_val_cnt(val, cnt);
651 local_set(t, val);
652}
653
654static void rb_time_set(rb_time_t *t, u64 val)
655{
656 unsigned long cnt, top, bottom;
657
658 rb_time_split(val, &top, &bottom);
659
660 /* Writes always succeed with a valid number even if it gets interrupted. */
661 do {
662 cnt = local_inc_return(&t->cnt);
663 rb_time_val_set(&t->top, top, cnt);
664 rb_time_val_set(&t->bottom, bottom, cnt);
665 } while (cnt != local_read(&t->cnt));
666}
667
668static inline bool
669rb_time_read_cmpxchg(local_t *l, unsigned long expect, unsigned long set)
670{
671 unsigned long ret;
672
673 ret = local_cmpxchg(l, expect, set);
674 return ret == expect;
675}
676
677static int rb_time_cmpxchg(rb_time_t *t, u64 expect, u64 set)
678{
679 unsigned long cnt, top, bottom;
680 unsigned long cnt2, top2, bottom2;
681 u64 val;
682
683 /* The cmpxchg always fails if it interrupted an update */
684 if (!__rb_time_read(t, &val, &cnt2))
685 return false;
686
687 if (val != expect)
688 return false;
689
690 cnt = local_read(&t->cnt);
691 if ((cnt & 3) != cnt2)
692 return false;
693
694 cnt2 = cnt + 1;
695
696 rb_time_split(val, &top, &bottom);
697 top = rb_time_val_cnt(top, cnt);
698 bottom = rb_time_val_cnt(bottom, cnt);
699
700 rb_time_split(set, &top2, &bottom2);
701 top2 = rb_time_val_cnt(top2, cnt2);
702 bottom2 = rb_time_val_cnt(bottom2, cnt2);
703
704 if (!rb_time_read_cmpxchg(&t->cnt, cnt, cnt2))
705 return false;
706 if (!rb_time_read_cmpxchg(&t->top, top, top2))
707 return false;
708 if (!rb_time_read_cmpxchg(&t->bottom, bottom, bottom2))
709 return false;
710 return true;
711}
712
713#else /* 64 bits */
714
715/* local64_t always succeeds */
716
717static inline bool rb_time_read(rb_time_t *t, u64 *ret)
718{
719 *ret = local64_read(&t->time);
720 return true;
721}
722static void rb_time_set(rb_time_t *t, u64 val)
723{
724 local64_set(&t->time, val);
725}
726
727static bool rb_time_cmpxchg(rb_time_t *t, u64 expect, u64 set)
728{
729 u64 val;
730 val = local64_cmpxchg(&t->time, expect, set);
731 return val == expect;
732}
733#endif
734
Steven Rostedt (VMware)2c2b0a72018-11-29 20:32:26 -0500735/**
736 * ring_buffer_nr_pages - get the number of buffer pages in the ring buffer
737 * @buffer: The ring_buffer to get the number of pages from
738 * @cpu: The cpu of the ring_buffer to get the number of pages from
739 *
740 * Returns the number of pages used by a per_cpu buffer of the ring buffer.
741 */
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -0500742size_t ring_buffer_nr_pages(struct trace_buffer *buffer, int cpu)
Steven Rostedt (VMware)2c2b0a72018-11-29 20:32:26 -0500743{
744 return buffer->buffers[cpu]->nr_pages;
745}
746
747/**
748 * ring_buffer_nr_pages_dirty - get the number of used pages in the ring buffer
749 * @buffer: The ring_buffer to get the number of pages from
750 * @cpu: The cpu of the ring_buffer to get the number of pages from
751 *
752 * Returns the number of pages that have content in the ring buffer.
753 */
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -0500754size_t ring_buffer_nr_dirty_pages(struct trace_buffer *buffer, int cpu)
Steven Rostedt (VMware)2c2b0a72018-11-29 20:32:26 -0500755{
756 size_t read;
757 size_t cnt;
758
759 read = local_read(&buffer->buffers[cpu]->pages_read);
760 cnt = local_read(&buffer->buffers[cpu]->pages_touched);
761 /* The reader can read an empty page, but not more than that */
762 if (cnt < read) {
763 WARN_ON_ONCE(read > cnt + 1);
764 return 0;
765 }
766
767 return cnt - read;
768}
769
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -0500770/*
771 * rb_wake_up_waiters - wake up tasks waiting for ring buffer input
772 *
773 * Schedules a delayed work to wake up any task that is blocked on the
774 * ring buffer waiters queue.
775 */
776static void rb_wake_up_waiters(struct irq_work *work)
777{
778 struct rb_irq_work *rbwork = container_of(work, struct rb_irq_work, work);
779
780 wake_up_all(&rbwork->waiters);
Steven Rostedt (Red Hat)1e0d6712015-02-10 22:14:53 -0500781 if (rbwork->wakeup_full) {
782 rbwork->wakeup_full = false;
783 wake_up_all(&rbwork->full_waiters);
784 }
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -0500785}
786
787/**
788 * ring_buffer_wait - wait for input to the ring buffer
789 * @buffer: buffer to wait on
790 * @cpu: the cpu buffer to wait on
Rabin Vincente30f53a2014-11-10 19:46:34 +0100791 * @full: wait until a full page is available, if @cpu != RING_BUFFER_ALL_CPUS
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -0500792 *
793 * If @cpu == RING_BUFFER_ALL_CPUS then the task will wake up as soon
794 * as data is added to any of the @buffer's cpu buffers. Otherwise
795 * it will wait for data to be added to a specific cpu buffer.
796 */
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -0500797int ring_buffer_wait(struct trace_buffer *buffer, int cpu, int full)
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -0500798{
Rabin Vincente30f53a2014-11-10 19:46:34 +0100799 struct ring_buffer_per_cpu *uninitialized_var(cpu_buffer);
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -0500800 DEFINE_WAIT(wait);
801 struct rb_irq_work *work;
Rabin Vincente30f53a2014-11-10 19:46:34 +0100802 int ret = 0;
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -0500803
804 /*
805 * Depending on what the caller is waiting for, either any
806 * data in any cpu buffer, or a specific buffer, put the
807 * caller on the appropriate wait queue.
808 */
Steven Rostedt (Red Hat)1e0d6712015-02-10 22:14:53 -0500809 if (cpu == RING_BUFFER_ALL_CPUS) {
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -0500810 work = &buffer->irq_work;
Steven Rostedt (Red Hat)1e0d6712015-02-10 22:14:53 -0500811 /* Full only makes sense on per cpu reads */
Steven Rostedt (VMware)2c2b0a72018-11-29 20:32:26 -0500812 full = 0;
Steven Rostedt (Red Hat)1e0d6712015-02-10 22:14:53 -0500813 } else {
Steven Rostedt (Red Hat)8b8b3682014-06-10 09:46:00 -0400814 if (!cpumask_test_cpu(cpu, buffer->cpumask))
815 return -ENODEV;
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -0500816 cpu_buffer = buffer->buffers[cpu];
817 work = &cpu_buffer->irq_work;
818 }
819
820
Rabin Vincente30f53a2014-11-10 19:46:34 +0100821 while (true) {
Steven Rostedt (Red Hat)1e0d6712015-02-10 22:14:53 -0500822 if (full)
823 prepare_to_wait(&work->full_waiters, &wait, TASK_INTERRUPTIBLE);
824 else
825 prepare_to_wait(&work->waiters, &wait, TASK_INTERRUPTIBLE);
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -0500826
Rabin Vincente30f53a2014-11-10 19:46:34 +0100827 /*
828 * The events can happen in critical sections where
829 * checking a work queue can cause deadlocks.
830 * After adding a task to the queue, this flag is set
831 * only to notify events to try to wake up the queue
832 * using irq_work.
833 *
834 * We don't clear it even if the buffer is no longer
835 * empty. The flag only causes the next event to run
836 * irq_work to do the work queue wake up. The worse
837 * that can happen if we race with !trace_empty() is that
838 * an event will cause an irq_work to try to wake up
839 * an empty queue.
840 *
841 * There's no reason to protect this flag either, as
842 * the work queue and irq_work logic will do the necessary
843 * synchronization for the wake ups. The only thing
844 * that is necessary is that the wake up happens after
845 * a task has been queued. It's OK for spurious wake ups.
846 */
Steven Rostedt (Red Hat)1e0d6712015-02-10 22:14:53 -0500847 if (full)
848 work->full_waiters_pending = true;
849 else
850 work->waiters_pending = true;
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -0500851
Rabin Vincente30f53a2014-11-10 19:46:34 +0100852 if (signal_pending(current)) {
853 ret = -EINTR;
854 break;
855 }
856
857 if (cpu == RING_BUFFER_ALL_CPUS && !ring_buffer_empty(buffer))
858 break;
859
860 if (cpu != RING_BUFFER_ALL_CPUS &&
861 !ring_buffer_empty_cpu(buffer, cpu)) {
862 unsigned long flags;
863 bool pagebusy;
Steven Rostedt (VMware)2c2b0a72018-11-29 20:32:26 -0500864 size_t nr_pages;
865 size_t dirty;
Rabin Vincente30f53a2014-11-10 19:46:34 +0100866
867 if (!full)
868 break;
869
870 raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
871 pagebusy = cpu_buffer->reader_page == cpu_buffer->commit_page;
Steven Rostedt (VMware)2c2b0a72018-11-29 20:32:26 -0500872 nr_pages = cpu_buffer->nr_pages;
873 dirty = ring_buffer_nr_dirty_pages(buffer, cpu);
874 if (!cpu_buffer->shortest_full ||
875 cpu_buffer->shortest_full < full)
876 cpu_buffer->shortest_full = full;
Rabin Vincente30f53a2014-11-10 19:46:34 +0100877 raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
Steven Rostedt (VMware)2c2b0a72018-11-29 20:32:26 -0500878 if (!pagebusy &&
879 (!nr_pages || (dirty * 100) > full * nr_pages))
Rabin Vincente30f53a2014-11-10 19:46:34 +0100880 break;
881 }
882
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -0500883 schedule();
Rabin Vincente30f53a2014-11-10 19:46:34 +0100884 }
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -0500885
Steven Rostedt (Red Hat)1e0d6712015-02-10 22:14:53 -0500886 if (full)
887 finish_wait(&work->full_waiters, &wait);
888 else
889 finish_wait(&work->waiters, &wait);
Rabin Vincente30f53a2014-11-10 19:46:34 +0100890
891 return ret;
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -0500892}
893
894/**
895 * ring_buffer_poll_wait - poll on buffer input
896 * @buffer: buffer to wait on
897 * @cpu: the cpu buffer to wait on
898 * @filp: the file descriptor
899 * @poll_table: The poll descriptor
900 *
901 * If @cpu == RING_BUFFER_ALL_CPUS then the task will wake up as soon
902 * as data is added to any of the @buffer's cpu buffers. Otherwise
903 * it will wait for data to be added to a specific cpu buffer.
904 *
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800905 * Returns EPOLLIN | EPOLLRDNORM if data exists in the buffers,
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -0500906 * zero otherwise.
907 */
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -0500908__poll_t ring_buffer_poll_wait(struct trace_buffer *buffer, int cpu,
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -0500909 struct file *filp, poll_table *poll_table)
910{
911 struct ring_buffer_per_cpu *cpu_buffer;
912 struct rb_irq_work *work;
913
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -0500914 if (cpu == RING_BUFFER_ALL_CPUS)
915 work = &buffer->irq_work;
916 else {
Steven Rostedt (Red Hat)6721cb62013-05-23 14:21:36 -0400917 if (!cpumask_test_cpu(cpu, buffer->cpumask))
918 return -EINVAL;
919
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -0500920 cpu_buffer = buffer->buffers[cpu];
921 work = &cpu_buffer->irq_work;
922 }
923
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -0500924 poll_wait(filp, &work->waiters, poll_table);
Josef Bacik4ce97db2014-08-25 13:59:41 -0400925 work->waiters_pending = true;
926 /*
927 * There's a tight race between setting the waiters_pending and
928 * checking if the ring buffer is empty. Once the waiters_pending bit
929 * is set, the next event will wake the task up, but we can get stuck
930 * if there's only a single event in.
931 *
932 * FIXME: Ideally, we need a memory barrier on the writer side as well,
933 * but adding a memory barrier to all events will cause too much of a
934 * performance hit in the fast path. We only need a memory barrier when
935 * the buffer goes from empty to having content. But as this race is
936 * extremely small, and it's not a problem if another event comes in, we
937 * will fix it later.
938 */
939 smp_mb();
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -0500940
941 if ((cpu == RING_BUFFER_ALL_CPUS && !ring_buffer_empty(buffer)) ||
942 (cpu != RING_BUFFER_ALL_CPUS && !ring_buffer_empty_cpu(buffer, cpu)))
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800943 return EPOLLIN | EPOLLRDNORM;
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -0500944 return 0;
945}
946
Steven Rostedtf536aaf2008-11-10 23:07:30 -0500947/* buffer may be either ring_buffer or ring_buffer_per_cpu */
Steven Rostedt077c5402009-09-03 19:53:46 -0400948#define RB_WARN_ON(b, cond) \
949 ({ \
950 int _____ret = unlikely(cond); \
951 if (_____ret) { \
952 if (__same_type(*(b), struct ring_buffer_per_cpu)) { \
953 struct ring_buffer_per_cpu *__b = \
954 (void *)b; \
955 atomic_inc(&__b->buffer->record_disabled); \
956 } else \
957 atomic_inc(&b->record_disabled); \
958 WARN_ON(1); \
959 } \
960 _____ret; \
Steven Rostedt3e89c7bb2008-11-11 15:28:41 -0500961 })
Steven Rostedtf536aaf2008-11-10 23:07:30 -0500962
Steven Rostedt37886f62009-03-17 17:22:06 -0400963/* Up this if you want to test the TIME_EXTENTS and normalization */
964#define DEBUG_SHIFT 0
965
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -0500966static inline u64 rb_time_stamp(struct trace_buffer *buffer)
Steven Rostedt88eb0122009-05-11 16:28:23 -0400967{
968 /* shift to debug/test normalization and TIME_EXTENTS */
969 return buffer->clock() << DEBUG_SHIFT;
970}
971
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -0500972u64 ring_buffer_time_stamp(struct trace_buffer *buffer, int cpu)
Steven Rostedt37886f62009-03-17 17:22:06 -0400973{
974 u64 time;
975
976 preempt_disable_notrace();
Jiri Olsa6d3f1e12009-10-23 19:36:19 -0400977 time = rb_time_stamp(buffer);
Peter Zijlstrad6097c92019-04-23 22:03:18 +0200978 preempt_enable_notrace();
Steven Rostedt37886f62009-03-17 17:22:06 -0400979
980 return time;
981}
982EXPORT_SYMBOL_GPL(ring_buffer_time_stamp);
983
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -0500984void ring_buffer_normalize_time_stamp(struct trace_buffer *buffer,
Steven Rostedt37886f62009-03-17 17:22:06 -0400985 int cpu, u64 *ts)
986{
987 /* Just stupid testing the normalize function and deltas */
988 *ts >>= DEBUG_SHIFT;
989}
990EXPORT_SYMBOL_GPL(ring_buffer_normalize_time_stamp);
991
Steven Rostedt77ae3652009-03-27 11:00:29 -0400992/*
993 * Making the ring buffer lockless makes things tricky.
994 * Although writes only happen on the CPU that they are on,
995 * and they only need to worry about interrupts. Reads can
996 * happen on any CPU.
997 *
998 * The reader page is always off the ring buffer, but when the
999 * reader finishes with a page, it needs to swap its page with
1000 * a new one from the buffer. The reader needs to take from
1001 * the head (writes go to the tail). But if a writer is in overwrite
1002 * mode and wraps, it must push the head page forward.
1003 *
1004 * Here lies the problem.
1005 *
1006 * The reader must be careful to replace only the head page, and
1007 * not another one. As described at the top of the file in the
1008 * ASCII art, the reader sets its old page to point to the next
1009 * page after head. It then sets the page after head to point to
1010 * the old reader page. But if the writer moves the head page
1011 * during this operation, the reader could end up with the tail.
1012 *
1013 * We use cmpxchg to help prevent this race. We also do something
1014 * special with the page before head. We set the LSB to 1.
1015 *
1016 * When the writer must push the page forward, it will clear the
1017 * bit that points to the head page, move the head, and then set
1018 * the bit that points to the new head page.
1019 *
1020 * We also don't want an interrupt coming in and moving the head
1021 * page on another writer. Thus we use the second LSB to catch
1022 * that too. Thus:
1023 *
1024 * head->list->prev->next bit 1 bit 0
1025 * ------- -------
1026 * Normal page 0 0
1027 * Points to head page 0 1
1028 * New head page 1 0
1029 *
1030 * Note we can not trust the prev pointer of the head page, because:
1031 *
1032 * +----+ +-----+ +-----+
1033 * | |------>| T |---X--->| N |
1034 * | |<------| | | |
1035 * +----+ +-----+ +-----+
1036 * ^ ^ |
1037 * | +-----+ | |
1038 * +----------| R |----------+ |
1039 * | |<-----------+
1040 * +-----+
1041 *
1042 * Key: ---X--> HEAD flag set in pointer
1043 * T Tail page
1044 * R Reader page
1045 * N Next page
1046 *
1047 * (see __rb_reserve_next() to see where this happens)
1048 *
1049 * What the above shows is that the reader just swapped out
1050 * the reader page with a page in the buffer, but before it
1051 * could make the new header point back to the new page added
1052 * it was preempted by a writer. The writer moved forward onto
1053 * the new page added by the reader and is about to move forward
1054 * again.
1055 *
1056 * You can see, it is legitimate for the previous pointer of
1057 * the head (or any page) not to point back to itself. But only
Steven Rostedt (VMware)6167c202018-05-16 11:17:06 -04001058 * temporarily.
Steven Rostedt77ae3652009-03-27 11:00:29 -04001059 */
1060
1061#define RB_PAGE_NORMAL 0UL
1062#define RB_PAGE_HEAD 1UL
1063#define RB_PAGE_UPDATE 2UL
1064
1065
1066#define RB_FLAG_MASK 3UL
1067
1068/* PAGE_MOVED is not part of the mask */
1069#define RB_PAGE_MOVED 4UL
1070
1071/*
1072 * rb_list_head - remove any bit
1073 */
1074static struct list_head *rb_list_head(struct list_head *list)
1075{
1076 unsigned long val = (unsigned long)list;
1077
1078 return (struct list_head *)(val & ~RB_FLAG_MASK);
1079}
1080
1081/*
Jiri Olsa6d3f1e12009-10-23 19:36:19 -04001082 * rb_is_head_page - test if the given page is the head page
Steven Rostedt77ae3652009-03-27 11:00:29 -04001083 *
1084 * Because the reader may move the head_page pointer, we can
1085 * not trust what the head page is (it may be pointing to
1086 * the reader page). But if the next page is a header page,
1087 * its flags will be non zero.
1088 */
Jesper Juhl42b16b32011-01-17 00:09:38 +01001089static inline int
Steven Rostedt77ae3652009-03-27 11:00:29 -04001090rb_is_head_page(struct ring_buffer_per_cpu *cpu_buffer,
1091 struct buffer_page *page, struct list_head *list)
1092{
1093 unsigned long val;
1094
1095 val = (unsigned long)list->next;
1096
1097 if ((val & ~RB_FLAG_MASK) != (unsigned long)&page->list)
1098 return RB_PAGE_MOVED;
1099
1100 return val & RB_FLAG_MASK;
1101}
1102
1103/*
1104 * rb_is_reader_page
1105 *
1106 * The unique thing about the reader page, is that, if the
1107 * writer is ever on it, the previous pointer never points
1108 * back to the reader page.
1109 */
Yaowei Bai06ca3202015-09-29 22:43:31 +08001110static bool rb_is_reader_page(struct buffer_page *page)
Steven Rostedt77ae3652009-03-27 11:00:29 -04001111{
1112 struct list_head *list = page->list.prev;
1113
1114 return rb_list_head(list->next) != &page->list;
1115}
1116
1117/*
1118 * rb_set_list_to_head - set a list_head to be pointing to head.
1119 */
1120static void rb_set_list_to_head(struct ring_buffer_per_cpu *cpu_buffer,
1121 struct list_head *list)
1122{
1123 unsigned long *ptr;
1124
1125 ptr = (unsigned long *)&list->next;
1126 *ptr |= RB_PAGE_HEAD;
1127 *ptr &= ~RB_PAGE_UPDATE;
1128}
1129
1130/*
1131 * rb_head_page_activate - sets up head page
1132 */
1133static void rb_head_page_activate(struct ring_buffer_per_cpu *cpu_buffer)
1134{
1135 struct buffer_page *head;
1136
1137 head = cpu_buffer->head_page;
1138 if (!head)
1139 return;
1140
1141 /*
1142 * Set the previous list pointer to have the HEAD flag.
1143 */
1144 rb_set_list_to_head(cpu_buffer, head->list.prev);
1145}
1146
1147static void rb_list_head_clear(struct list_head *list)
1148{
1149 unsigned long *ptr = (unsigned long *)&list->next;
1150
1151 *ptr &= ~RB_FLAG_MASK;
1152}
1153
1154/*
Steven Rostedt (VMware)6167c202018-05-16 11:17:06 -04001155 * rb_head_page_deactivate - clears head page ptr (for free list)
Steven Rostedt77ae3652009-03-27 11:00:29 -04001156 */
1157static void
1158rb_head_page_deactivate(struct ring_buffer_per_cpu *cpu_buffer)
1159{
1160 struct list_head *hd;
1161
1162 /* Go through the whole list and clear any pointers found. */
1163 rb_list_head_clear(cpu_buffer->pages);
1164
1165 list_for_each(hd, cpu_buffer->pages)
1166 rb_list_head_clear(hd);
1167}
1168
1169static int rb_head_page_set(struct ring_buffer_per_cpu *cpu_buffer,
1170 struct buffer_page *head,
1171 struct buffer_page *prev,
1172 int old_flag, int new_flag)
1173{
1174 struct list_head *list;
1175 unsigned long val = (unsigned long)&head->list;
1176 unsigned long ret;
1177
1178 list = &prev->list;
1179
1180 val &= ~RB_FLAG_MASK;
1181
Steven Rostedt08a40812009-09-14 09:31:35 -04001182 ret = cmpxchg((unsigned long *)&list->next,
1183 val | old_flag, val | new_flag);
Steven Rostedt77ae3652009-03-27 11:00:29 -04001184
1185 /* check if the reader took the page */
1186 if ((ret & ~RB_FLAG_MASK) != val)
1187 return RB_PAGE_MOVED;
1188
1189 return ret & RB_FLAG_MASK;
1190}
1191
1192static int rb_head_page_set_update(struct ring_buffer_per_cpu *cpu_buffer,
1193 struct buffer_page *head,
1194 struct buffer_page *prev,
1195 int old_flag)
1196{
1197 return rb_head_page_set(cpu_buffer, head, prev,
1198 old_flag, RB_PAGE_UPDATE);
1199}
1200
1201static int rb_head_page_set_head(struct ring_buffer_per_cpu *cpu_buffer,
1202 struct buffer_page *head,
1203 struct buffer_page *prev,
1204 int old_flag)
1205{
1206 return rb_head_page_set(cpu_buffer, head, prev,
1207 old_flag, RB_PAGE_HEAD);
1208}
1209
1210static int rb_head_page_set_normal(struct ring_buffer_per_cpu *cpu_buffer,
1211 struct buffer_page *head,
1212 struct buffer_page *prev,
1213 int old_flag)
1214{
1215 return rb_head_page_set(cpu_buffer, head, prev,
1216 old_flag, RB_PAGE_NORMAL);
1217}
1218
1219static inline void rb_inc_page(struct ring_buffer_per_cpu *cpu_buffer,
1220 struct buffer_page **bpage)
1221{
1222 struct list_head *p = rb_list_head((*bpage)->list.next);
1223
1224 *bpage = list_entry(p, struct buffer_page, list);
1225}
1226
1227static struct buffer_page *
1228rb_set_head_page(struct ring_buffer_per_cpu *cpu_buffer)
1229{
1230 struct buffer_page *head;
1231 struct buffer_page *page;
1232 struct list_head *list;
1233 int i;
1234
1235 if (RB_WARN_ON(cpu_buffer, !cpu_buffer->head_page))
1236 return NULL;
1237
1238 /* sanity check */
1239 list = cpu_buffer->pages;
1240 if (RB_WARN_ON(cpu_buffer, rb_list_head(list->prev->next) != list))
1241 return NULL;
1242
1243 page = head = cpu_buffer->head_page;
1244 /*
1245 * It is possible that the writer moves the header behind
1246 * where we started, and we miss in one loop.
1247 * A second loop should grab the header, but we'll do
1248 * three loops just because I'm paranoid.
1249 */
1250 for (i = 0; i < 3; i++) {
1251 do {
1252 if (rb_is_head_page(cpu_buffer, page, page->list.prev)) {
1253 cpu_buffer->head_page = page;
1254 return page;
1255 }
1256 rb_inc_page(cpu_buffer, &page);
1257 } while (page != head);
1258 }
1259
1260 RB_WARN_ON(cpu_buffer, 1);
1261
1262 return NULL;
1263}
1264
1265static int rb_head_page_replace(struct buffer_page *old,
1266 struct buffer_page *new)
1267{
1268 unsigned long *ptr = (unsigned long *)&old->list.prev->next;
1269 unsigned long val;
1270 unsigned long ret;
1271
1272 val = *ptr & ~RB_FLAG_MASK;
1273 val |= RB_PAGE_HEAD;
1274
Steven Rostedt08a40812009-09-14 09:31:35 -04001275 ret = cmpxchg(ptr, val, (unsigned long)&new->list);
Steven Rostedt77ae3652009-03-27 11:00:29 -04001276
1277 return ret == val;
1278}
1279
1280/*
1281 * rb_tail_page_update - move the tail page forward
Steven Rostedt77ae3652009-03-27 11:00:29 -04001282 */
Steven Rostedt (Red Hat)70004982015-11-17 15:15:19 -05001283static void rb_tail_page_update(struct ring_buffer_per_cpu *cpu_buffer,
Steven Rostedt77ae3652009-03-27 11:00:29 -04001284 struct buffer_page *tail_page,
1285 struct buffer_page *next_page)
1286{
Steven Rostedt77ae3652009-03-27 11:00:29 -04001287 unsigned long old_entries;
1288 unsigned long old_write;
Steven Rostedt77ae3652009-03-27 11:00:29 -04001289
1290 /*
1291 * The tail page now needs to be moved forward.
1292 *
1293 * We need to reset the tail page, but without messing
1294 * with possible erasing of data brought in by interrupts
1295 * that have moved the tail page and are currently on it.
1296 *
1297 * We add a counter to the write field to denote this.
1298 */
1299 old_write = local_add_return(RB_WRITE_INTCNT, &next_page->write);
1300 old_entries = local_add_return(RB_WRITE_INTCNT, &next_page->entries);
1301
Steven Rostedt (VMware)2c2b0a72018-11-29 20:32:26 -05001302 local_inc(&cpu_buffer->pages_touched);
Steven Rostedt77ae3652009-03-27 11:00:29 -04001303 /*
1304 * Just make sure we have seen our old_write and synchronize
1305 * with any interrupts that come in.
1306 */
1307 barrier();
1308
1309 /*
1310 * If the tail page is still the same as what we think
1311 * it is, then it is up to us to update the tail
1312 * pointer.
1313 */
Steven Rostedt (Red Hat)85736362015-11-17 14:03:11 -05001314 if (tail_page == READ_ONCE(cpu_buffer->tail_page)) {
Steven Rostedt77ae3652009-03-27 11:00:29 -04001315 /* Zero the write counter */
1316 unsigned long val = old_write & ~RB_WRITE_MASK;
1317 unsigned long eval = old_entries & ~RB_WRITE_MASK;
1318
1319 /*
1320 * This will only succeed if an interrupt did
1321 * not come in and change it. In which case, we
1322 * do not want to modify it.
Lai Jiangshanda706d82009-07-15 16:27:30 +08001323 *
1324 * We add (void) to let the compiler know that we do not care
1325 * about the return value of these functions. We use the
1326 * cmpxchg to only update if an interrupt did not already
1327 * do it for us. If the cmpxchg fails, we don't care.
Steven Rostedt77ae3652009-03-27 11:00:29 -04001328 */
Lai Jiangshanda706d82009-07-15 16:27:30 +08001329 (void)local_cmpxchg(&next_page->write, old_write, val);
1330 (void)local_cmpxchg(&next_page->entries, old_entries, eval);
Steven Rostedt77ae3652009-03-27 11:00:29 -04001331
1332 /*
1333 * No need to worry about races with clearing out the commit.
1334 * it only can increment when a commit takes place. But that
1335 * only happens in the outer most nested commit.
1336 */
1337 local_set(&next_page->page->commit, 0);
1338
Steven Rostedt (Red Hat)70004982015-11-17 15:15:19 -05001339 /* Again, either we update tail_page or an interrupt does */
1340 (void)cmpxchg(&cpu_buffer->tail_page, tail_page, next_page);
Steven Rostedt77ae3652009-03-27 11:00:29 -04001341 }
Steven Rostedt77ae3652009-03-27 11:00:29 -04001342}
1343
1344static int rb_check_bpage(struct ring_buffer_per_cpu *cpu_buffer,
1345 struct buffer_page *bpage)
1346{
1347 unsigned long val = (unsigned long)bpage;
1348
1349 if (RB_WARN_ON(cpu_buffer, val & RB_FLAG_MASK))
1350 return 1;
1351
1352 return 0;
1353}
1354
1355/**
1356 * rb_check_list - make sure a pointer to a list has the last bits zero
1357 */
1358static int rb_check_list(struct ring_buffer_per_cpu *cpu_buffer,
1359 struct list_head *list)
1360{
1361 if (RB_WARN_ON(cpu_buffer, rb_list_head(list->prev) != list->prev))
1362 return 1;
1363 if (RB_WARN_ON(cpu_buffer, rb_list_head(list->next) != list->next))
1364 return 1;
1365 return 0;
1366}
1367
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001368/**
zhangwei(Jovi)d6118512013-07-15 16:32:50 +08001369 * rb_check_pages - integrity check of buffer pages
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001370 * @cpu_buffer: CPU buffer with pages to test
1371 *
Wenji Huangc3706f02009-02-10 01:03:18 -05001372 * As a safety measure we check to make sure the data pages have not
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001373 * been corrupted.
1374 */
1375static int rb_check_pages(struct ring_buffer_per_cpu *cpu_buffer)
1376{
Steven Rostedt3adc54f2009-03-30 15:32:01 -04001377 struct list_head *head = cpu_buffer->pages;
Steven Rostedt044fa782008-12-02 23:50:03 -05001378 struct buffer_page *bpage, *tmp;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001379
Steven Rostedt308f7ee2012-05-16 19:46:32 -04001380 /* Reset the head page if it exists */
1381 if (cpu_buffer->head_page)
1382 rb_set_head_page(cpu_buffer);
1383
Steven Rostedt77ae3652009-03-27 11:00:29 -04001384 rb_head_page_deactivate(cpu_buffer);
1385
Steven Rostedt3e89c7bb2008-11-11 15:28:41 -05001386 if (RB_WARN_ON(cpu_buffer, head->next->prev != head))
1387 return -1;
1388 if (RB_WARN_ON(cpu_buffer, head->prev->next != head))
1389 return -1;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001390
Steven Rostedt77ae3652009-03-27 11:00:29 -04001391 if (rb_check_list(cpu_buffer, head))
1392 return -1;
1393
Steven Rostedt044fa782008-12-02 23:50:03 -05001394 list_for_each_entry_safe(bpage, tmp, head, list) {
Steven Rostedt3e89c7bb2008-11-11 15:28:41 -05001395 if (RB_WARN_ON(cpu_buffer,
Steven Rostedt044fa782008-12-02 23:50:03 -05001396 bpage->list.next->prev != &bpage->list))
Steven Rostedt3e89c7bb2008-11-11 15:28:41 -05001397 return -1;
1398 if (RB_WARN_ON(cpu_buffer,
Steven Rostedt044fa782008-12-02 23:50:03 -05001399 bpage->list.prev->next != &bpage->list))
Steven Rostedt3e89c7bb2008-11-11 15:28:41 -05001400 return -1;
Steven Rostedt77ae3652009-03-27 11:00:29 -04001401 if (rb_check_list(cpu_buffer, &bpage->list))
1402 return -1;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001403 }
1404
Steven Rostedt77ae3652009-03-27 11:00:29 -04001405 rb_head_page_activate(cpu_buffer);
1406
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001407 return 0;
1408}
1409
Steven Rostedt (Red Hat)9b94a8f2016-05-12 11:01:24 -04001410static int __rb_allocate_pages(long nr_pages, struct list_head *pages, int cpu)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001411{
Steven Rostedt044fa782008-12-02 23:50:03 -05001412 struct buffer_page *bpage, *tmp;
Steven Rostedt (VMware)927e56d2018-04-04 11:29:57 -04001413 bool user_thread = current->mm != NULL;
1414 gfp_t mflags;
Steven Rostedt (Red Hat)9b94a8f2016-05-12 11:01:24 -04001415 long i;
Steven Rostedt3adc54f2009-03-30 15:32:01 -04001416
Steven Rostedt (VMware)927e56d2018-04-04 11:29:57 -04001417 /*
1418 * Check if the available memory is there first.
1419 * Note, si_mem_available() only gives us a rough estimate of available
1420 * memory. It may not be accurate. But we don't care, we just want
1421 * to prevent doing any allocation when it is obvious that it is
1422 * not going to succeed.
1423 */
Steven Rostedt (VMware)2a872fa2018-04-02 10:33:56 -04001424 i = si_mem_available();
1425 if (i < nr_pages)
1426 return -ENOMEM;
1427
Steven Rostedt (VMware)927e56d2018-04-04 11:29:57 -04001428 /*
1429 * __GFP_RETRY_MAYFAIL flag makes sure that the allocation fails
1430 * gracefully without invoking oom-killer and the system is not
1431 * destabilized.
1432 */
1433 mflags = GFP_KERNEL | __GFP_RETRY_MAYFAIL;
1434
1435 /*
1436 * If a user thread allocates too much, and si_mem_available()
1437 * reports there's enough memory, even though there is not.
1438 * Make sure the OOM killer kills this thread. This can happen
1439 * even with RETRY_MAYFAIL because another task may be doing
1440 * an allocation after this task has taken all memory.
1441 * This is the task the OOM killer needs to take out during this
1442 * loop, even if it was triggered by an allocation somewhere else.
1443 */
1444 if (user_thread)
1445 set_current_oom_origin();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001446 for (i = 0; i < nr_pages; i++) {
Vaibhav Nagarnaik7ea59062011-05-03 17:56:42 -07001447 struct page *page;
Steven Rostedt (VMware)927e56d2018-04-04 11:29:57 -04001448
Steven Rostedt044fa782008-12-02 23:50:03 -05001449 bpage = kzalloc_node(ALIGN(sizeof(*bpage), cache_line_size()),
Steven Rostedt (VMware)927e56d2018-04-04 11:29:57 -04001450 mflags, cpu_to_node(cpu));
Steven Rostedt044fa782008-12-02 23:50:03 -05001451 if (!bpage)
Steven Rostedte4c2ce82008-10-01 11:14:54 -04001452 goto free_pages;
Steven Rostedt77ae3652009-03-27 11:00:29 -04001453
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001454 list_add(&bpage->list, pages);
Steven Rostedt77ae3652009-03-27 11:00:29 -04001455
Steven Rostedt (VMware)927e56d2018-04-04 11:29:57 -04001456 page = alloc_pages_node(cpu_to_node(cpu), mflags, 0);
Vaibhav Nagarnaik7ea59062011-05-03 17:56:42 -07001457 if (!page)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001458 goto free_pages;
Vaibhav Nagarnaik7ea59062011-05-03 17:56:42 -07001459 bpage->page = page_address(page);
Steven Rostedt044fa782008-12-02 23:50:03 -05001460 rb_init_page(bpage->page);
Steven Rostedt (VMware)927e56d2018-04-04 11:29:57 -04001461
1462 if (user_thread && fatal_signal_pending(current))
1463 goto free_pages;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001464 }
Steven Rostedt (VMware)927e56d2018-04-04 11:29:57 -04001465 if (user_thread)
1466 clear_current_oom_origin();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001467
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001468 return 0;
1469
1470free_pages:
1471 list_for_each_entry_safe(bpage, tmp, pages, list) {
1472 list_del_init(&bpage->list);
1473 free_buffer_page(bpage);
1474 }
Steven Rostedt (VMware)927e56d2018-04-04 11:29:57 -04001475 if (user_thread)
1476 clear_current_oom_origin();
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001477
1478 return -ENOMEM;
1479}
1480
1481static int rb_allocate_pages(struct ring_buffer_per_cpu *cpu_buffer,
Steven Rostedt (Red Hat)9b94a8f2016-05-12 11:01:24 -04001482 unsigned long nr_pages)
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001483{
1484 LIST_HEAD(pages);
1485
1486 WARN_ON(!nr_pages);
1487
1488 if (__rb_allocate_pages(nr_pages, &pages, cpu_buffer->cpu))
1489 return -ENOMEM;
1490
Steven Rostedt3adc54f2009-03-30 15:32:01 -04001491 /*
1492 * The ring buffer page list is a circular list that does not
1493 * start and end with a list head. All page list items point to
1494 * other pages.
1495 */
1496 cpu_buffer->pages = pages.next;
1497 list_del(&pages);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001498
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001499 cpu_buffer->nr_pages = nr_pages;
1500
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001501 rb_check_pages(cpu_buffer);
1502
1503 return 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001504}
1505
1506static struct ring_buffer_per_cpu *
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05001507rb_allocate_cpu_buffer(struct trace_buffer *buffer, long nr_pages, int cpu)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001508{
1509 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedt044fa782008-12-02 23:50:03 -05001510 struct buffer_page *bpage;
Vaibhav Nagarnaik7ea59062011-05-03 17:56:42 -07001511 struct page *page;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001512 int ret;
1513
1514 cpu_buffer = kzalloc_node(ALIGN(sizeof(*cpu_buffer), cache_line_size()),
1515 GFP_KERNEL, cpu_to_node(cpu));
1516 if (!cpu_buffer)
1517 return NULL;
1518
1519 cpu_buffer->cpu = cpu;
1520 cpu_buffer->buffer = buffer;
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02001521 raw_spin_lock_init(&cpu_buffer->reader_lock);
Peter Zijlstra1f8a6a12009-06-08 18:18:39 +02001522 lockdep_set_class(&cpu_buffer->reader_lock, buffer->reader_lock_key);
Thomas Gleixneredc35bd2009-12-03 12:38:57 +01001523 cpu_buffer->lock = (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001524 INIT_WORK(&cpu_buffer->update_pages_work, update_pages_handler);
Vaibhav Nagarnaik05fdd702012-05-18 13:29:51 -07001525 init_completion(&cpu_buffer->update_done);
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -05001526 init_irq_work(&cpu_buffer->irq_work.work, rb_wake_up_waiters);
Steven Rostedt (Red Hat)f1dc6722013-03-04 17:33:05 -05001527 init_waitqueue_head(&cpu_buffer->irq_work.waiters);
Steven Rostedt (Red Hat)1e0d6712015-02-10 22:14:53 -05001528 init_waitqueue_head(&cpu_buffer->irq_work.full_waiters);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001529
Steven Rostedt044fa782008-12-02 23:50:03 -05001530 bpage = kzalloc_node(ALIGN(sizeof(*bpage), cache_line_size()),
Steven Rostedte4c2ce82008-10-01 11:14:54 -04001531 GFP_KERNEL, cpu_to_node(cpu));
Steven Rostedt044fa782008-12-02 23:50:03 -05001532 if (!bpage)
Steven Rostedte4c2ce82008-10-01 11:14:54 -04001533 goto fail_free_buffer;
1534
Steven Rostedt77ae3652009-03-27 11:00:29 -04001535 rb_check_bpage(cpu_buffer, bpage);
1536
Steven Rostedt044fa782008-12-02 23:50:03 -05001537 cpu_buffer->reader_page = bpage;
Vaibhav Nagarnaik7ea59062011-05-03 17:56:42 -07001538 page = alloc_pages_node(cpu_to_node(cpu), GFP_KERNEL, 0);
1539 if (!page)
Steven Rostedte4c2ce82008-10-01 11:14:54 -04001540 goto fail_free_reader;
Vaibhav Nagarnaik7ea59062011-05-03 17:56:42 -07001541 bpage->page = page_address(page);
Steven Rostedt044fa782008-12-02 23:50:03 -05001542 rb_init_page(bpage->page);
Steven Rostedte4c2ce82008-10-01 11:14:54 -04001543
Steven Rostedtd7690412008-10-01 00:29:53 -04001544 INIT_LIST_HEAD(&cpu_buffer->reader_page->list);
Vaibhav Nagarnaik44b99462012-06-22 11:50:05 -07001545 INIT_LIST_HEAD(&cpu_buffer->new_pages);
Steven Rostedtd7690412008-10-01 00:29:53 -04001546
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001547 ret = rb_allocate_pages(cpu_buffer, nr_pages);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001548 if (ret < 0)
Steven Rostedtd7690412008-10-01 00:29:53 -04001549 goto fail_free_reader;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001550
1551 cpu_buffer->head_page
Steven Rostedt3adc54f2009-03-30 15:32:01 -04001552 = list_entry(cpu_buffer->pages, struct buffer_page, list);
Steven Rostedtbf41a152008-10-04 02:00:59 -04001553 cpu_buffer->tail_page = cpu_buffer->commit_page = cpu_buffer->head_page;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001554
Steven Rostedt77ae3652009-03-27 11:00:29 -04001555 rb_head_page_activate(cpu_buffer);
1556
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001557 return cpu_buffer;
1558
Steven Rostedtd7690412008-10-01 00:29:53 -04001559 fail_free_reader:
1560 free_buffer_page(cpu_buffer->reader_page);
1561
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001562 fail_free_buffer:
1563 kfree(cpu_buffer);
1564 return NULL;
1565}
1566
1567static void rb_free_cpu_buffer(struct ring_buffer_per_cpu *cpu_buffer)
1568{
Steven Rostedt3adc54f2009-03-30 15:32:01 -04001569 struct list_head *head = cpu_buffer->pages;
Steven Rostedt044fa782008-12-02 23:50:03 -05001570 struct buffer_page *bpage, *tmp;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001571
Steven Rostedtd7690412008-10-01 00:29:53 -04001572 free_buffer_page(cpu_buffer->reader_page);
1573
Steven Rostedt77ae3652009-03-27 11:00:29 -04001574 rb_head_page_deactivate(cpu_buffer);
1575
Steven Rostedt3adc54f2009-03-30 15:32:01 -04001576 if (head) {
1577 list_for_each_entry_safe(bpage, tmp, head, list) {
1578 list_del_init(&bpage->list);
1579 free_buffer_page(bpage);
1580 }
1581 bpage = list_entry(head, struct buffer_page, list);
Steven Rostedt044fa782008-12-02 23:50:03 -05001582 free_buffer_page(bpage);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001583 }
Steven Rostedt3adc54f2009-03-30 15:32:01 -04001584
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001585 kfree(cpu_buffer);
1586}
1587
1588/**
zhangwei(Jovi)d6118512013-07-15 16:32:50 +08001589 * __ring_buffer_alloc - allocate a new ring_buffer
Robert Richter68814b52008-11-24 12:24:12 +01001590 * @size: the size in bytes per cpu that is needed.
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001591 * @flags: attributes to set for the ring buffer.
Fabian Frederick59e7cff2014-06-05 20:22:05 +02001592 * @key: ring buffer reader_lock_key.
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001593 *
1594 * Currently the only flag that is available is the RB_FL_OVERWRITE
1595 * flag. This flag means that the buffer will overwrite old data
1596 * when the buffer wraps. If this flag is not set, the buffer will
1597 * drop data when the tail hits the head.
1598 */
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05001599struct trace_buffer *__ring_buffer_alloc(unsigned long size, unsigned flags,
Peter Zijlstra1f8a6a12009-06-08 18:18:39 +02001600 struct lock_class_key *key)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001601{
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05001602 struct trace_buffer *buffer;
Steven Rostedt (Red Hat)9b94a8f2016-05-12 11:01:24 -04001603 long nr_pages;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001604 int bsize;
Steven Rostedt (Red Hat)9b94a8f2016-05-12 11:01:24 -04001605 int cpu;
Sebastian Andrzej Siewiorb32614c2016-11-27 00:13:34 +01001606 int ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001607
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001608 /* keep it in its own cache line */
1609 buffer = kzalloc(ALIGN(sizeof(*buffer), cache_line_size()),
1610 GFP_KERNEL);
1611 if (!buffer)
1612 return NULL;
1613
Sebastian Andrzej Siewiorb18cc3d2016-12-07 14:31:33 +01001614 if (!zalloc_cpumask_var(&buffer->cpumask, GFP_KERNEL))
Rusty Russell9e01c1b2009-01-01 10:12:22 +10301615 goto fail_free_buffer;
1616
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001617 nr_pages = DIV_ROUND_UP(size, BUF_PAGE_SIZE);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001618 buffer->flags = flags;
Steven Rostedt37886f62009-03-17 17:22:06 -04001619 buffer->clock = trace_clock_local;
Peter Zijlstra1f8a6a12009-06-08 18:18:39 +02001620 buffer->reader_lock_key = key;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001621
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -05001622 init_irq_work(&buffer->irq_work.work, rb_wake_up_waiters);
Steven Rostedt (Red Hat)f1dc6722013-03-04 17:33:05 -05001623 init_waitqueue_head(&buffer->irq_work.waiters);
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -05001624
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001625 /* need at least two pages */
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001626 if (nr_pages < 2)
1627 nr_pages = 2;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001628
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001629 buffer->cpus = nr_cpu_ids;
1630
1631 bsize = sizeof(void *) * nr_cpu_ids;
1632 buffer->buffers = kzalloc(ALIGN(bsize, cache_line_size()),
1633 GFP_KERNEL);
1634 if (!buffer->buffers)
Rusty Russell9e01c1b2009-01-01 10:12:22 +10301635 goto fail_free_cpumask;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001636
Sebastian Andrzej Siewiorb32614c2016-11-27 00:13:34 +01001637 cpu = raw_smp_processor_id();
1638 cpumask_set_cpu(cpu, buffer->cpumask);
1639 buffer->buffers[cpu] = rb_allocate_cpu_buffer(buffer, nr_pages, cpu);
1640 if (!buffer->buffers[cpu])
1641 goto fail_free_buffers;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001642
Sebastian Andrzej Siewiorb32614c2016-11-27 00:13:34 +01001643 ret = cpuhp_state_add_instance(CPUHP_TRACE_RB_PREPARE, &buffer->node);
1644 if (ret < 0)
1645 goto fail_free_buffers;
Steven Rostedt554f7862009-03-11 22:00:13 -04001646
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001647 mutex_init(&buffer->mutex);
1648
1649 return buffer;
1650
1651 fail_free_buffers:
1652 for_each_buffer_cpu(buffer, cpu) {
1653 if (buffer->buffers[cpu])
1654 rb_free_cpu_buffer(buffer->buffers[cpu]);
1655 }
1656 kfree(buffer->buffers);
1657
Rusty Russell9e01c1b2009-01-01 10:12:22 +10301658 fail_free_cpumask:
1659 free_cpumask_var(buffer->cpumask);
1660
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001661 fail_free_buffer:
1662 kfree(buffer);
1663 return NULL;
1664}
Peter Zijlstra1f8a6a12009-06-08 18:18:39 +02001665EXPORT_SYMBOL_GPL(__ring_buffer_alloc);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001666
1667/**
1668 * ring_buffer_free - free a ring buffer.
1669 * @buffer: the buffer to free.
1670 */
1671void
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05001672ring_buffer_free(struct trace_buffer *buffer)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001673{
1674 int cpu;
1675
Sebastian Andrzej Siewiorb32614c2016-11-27 00:13:34 +01001676 cpuhp_state_remove_instance(CPUHP_TRACE_RB_PREPARE, &buffer->node);
Steven Rostedt554f7862009-03-11 22:00:13 -04001677
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001678 for_each_buffer_cpu(buffer, cpu)
1679 rb_free_cpu_buffer(buffer->buffers[cpu]);
1680
Eric Dumazetbd3f0222009-08-07 12:49:29 +02001681 kfree(buffer->buffers);
Rusty Russell9e01c1b2009-01-01 10:12:22 +10301682 free_cpumask_var(buffer->cpumask);
1683
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001684 kfree(buffer);
1685}
Robert Richterc4f50182008-12-11 16:49:22 +01001686EXPORT_SYMBOL_GPL(ring_buffer_free);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001687
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05001688void ring_buffer_set_clock(struct trace_buffer *buffer,
Steven Rostedt37886f62009-03-17 17:22:06 -04001689 u64 (*clock)(void))
1690{
1691 buffer->clock = clock;
1692}
1693
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05001694void ring_buffer_set_time_stamp_abs(struct trace_buffer *buffer, bool abs)
Tom Zanussi00b41452018-01-15 20:51:39 -06001695{
1696 buffer->time_stamp_abs = abs;
1697}
1698
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05001699bool ring_buffer_time_stamp_abs(struct trace_buffer *buffer)
Tom Zanussi00b41452018-01-15 20:51:39 -06001700{
1701 return buffer->time_stamp_abs;
1702}
1703
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001704static void rb_reset_cpu(struct ring_buffer_per_cpu *cpu_buffer);
1705
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001706static inline unsigned long rb_page_entries(struct buffer_page *bpage)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001707{
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001708 return local_read(&bpage->entries) & RB_WRITE_MASK;
1709}
1710
1711static inline unsigned long rb_page_write(struct buffer_page *bpage)
1712{
1713 return local_read(&bpage->write) & RB_WRITE_MASK;
1714}
1715
Vaibhav Nagarnaik5040b4b2012-05-03 18:59:51 -07001716static int
Steven Rostedt (Red Hat)9b94a8f2016-05-12 11:01:24 -04001717rb_remove_pages(struct ring_buffer_per_cpu *cpu_buffer, unsigned long nr_pages)
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001718{
1719 struct list_head *tail_page, *to_remove, *next_page;
1720 struct buffer_page *to_remove_page, *tmp_iter_page;
1721 struct buffer_page *last_page, *first_page;
Steven Rostedt (Red Hat)9b94a8f2016-05-12 11:01:24 -04001722 unsigned long nr_removed;
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001723 unsigned long head_bit;
1724 int page_entries;
1725
1726 head_bit = 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001727
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02001728 raw_spin_lock_irq(&cpu_buffer->reader_lock);
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001729 atomic_inc(&cpu_buffer->record_disabled);
1730 /*
1731 * We don't race with the readers since we have acquired the reader
1732 * lock. We also don't race with writers after disabling recording.
1733 * This makes it easy to figure out the first and the last page to be
1734 * removed from the list. We unlink all the pages in between including
1735 * the first and last pages. This is done in a busy loop so that we
1736 * lose the least number of traces.
1737 * The pages are freed after we restart recording and unlock readers.
1738 */
1739 tail_page = &cpu_buffer->tail_page->list;
Steven Rostedt77ae3652009-03-27 11:00:29 -04001740
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001741 /*
1742 * tail page might be on reader page, we remove the next page
1743 * from the ring buffer
1744 */
1745 if (cpu_buffer->tail_page == cpu_buffer->reader_page)
1746 tail_page = rb_list_head(tail_page->next);
1747 to_remove = tail_page;
1748
1749 /* start of pages to remove */
1750 first_page = list_entry(rb_list_head(to_remove->next),
1751 struct buffer_page, list);
1752
1753 for (nr_removed = 0; nr_removed < nr_pages; nr_removed++) {
1754 to_remove = rb_list_head(to_remove)->next;
1755 head_bit |= (unsigned long)to_remove & RB_PAGE_HEAD;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001756 }
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001757
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001758 next_page = rb_list_head(to_remove)->next;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001759
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001760 /*
1761 * Now we remove all pages between tail_page and next_page.
1762 * Make sure that we have head_bit value preserved for the
1763 * next page
1764 */
1765 tail_page->next = (struct list_head *)((unsigned long)next_page |
1766 head_bit);
1767 next_page = rb_list_head(next_page);
1768 next_page->prev = tail_page;
1769
1770 /* make sure pages points to a valid page in the ring buffer */
1771 cpu_buffer->pages = next_page;
1772
1773 /* update head page */
1774 if (head_bit)
1775 cpu_buffer->head_page = list_entry(next_page,
1776 struct buffer_page, list);
1777
1778 /*
1779 * change read pointer to make sure any read iterators reset
1780 * themselves
1781 */
1782 cpu_buffer->read = 0;
1783
1784 /* pages are removed, resume tracing and then free the pages */
1785 atomic_dec(&cpu_buffer->record_disabled);
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02001786 raw_spin_unlock_irq(&cpu_buffer->reader_lock);
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001787
1788 RB_WARN_ON(cpu_buffer, list_empty(cpu_buffer->pages));
1789
1790 /* last buffer page to remove */
1791 last_page = list_entry(rb_list_head(to_remove), struct buffer_page,
1792 list);
1793 tmp_iter_page = first_page;
1794
1795 do {
Vaibhav Nagarnaik83f36552018-09-07 15:31:29 -07001796 cond_resched();
1797
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001798 to_remove_page = tmp_iter_page;
1799 rb_inc_page(cpu_buffer, &tmp_iter_page);
1800
1801 /* update the counters */
1802 page_entries = rb_page_entries(to_remove_page);
1803 if (page_entries) {
1804 /*
1805 * If something was added to this page, it was full
1806 * since it is not the tail page. So we deduct the
1807 * bytes consumed in ring buffer from here.
Vaibhav Nagarnaik48fdc722012-06-29 12:31:41 -07001808 * Increment overrun to account for the lost events.
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001809 */
Vaibhav Nagarnaik48fdc722012-06-29 12:31:41 -07001810 local_add(page_entries, &cpu_buffer->overrun);
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001811 local_sub(BUF_PAGE_SIZE, &cpu_buffer->entries_bytes);
1812 }
1813
1814 /*
1815 * We have already removed references to this list item, just
1816 * free up the buffer_page and its page
1817 */
1818 free_buffer_page(to_remove_page);
1819 nr_removed--;
1820
1821 } while (to_remove_page != last_page);
1822
1823 RB_WARN_ON(cpu_buffer, nr_removed);
Vaibhav Nagarnaik5040b4b2012-05-03 18:59:51 -07001824
1825 return nr_removed == 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001826}
1827
Vaibhav Nagarnaik5040b4b2012-05-03 18:59:51 -07001828static int
1829rb_insert_pages(struct ring_buffer_per_cpu *cpu_buffer)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001830{
Vaibhav Nagarnaik5040b4b2012-05-03 18:59:51 -07001831 struct list_head *pages = &cpu_buffer->new_pages;
1832 int retries, success;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001833
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02001834 raw_spin_lock_irq(&cpu_buffer->reader_lock);
Vaibhav Nagarnaik5040b4b2012-05-03 18:59:51 -07001835 /*
1836 * We are holding the reader lock, so the reader page won't be swapped
1837 * in the ring buffer. Now we are racing with the writer trying to
1838 * move head page and the tail page.
1839 * We are going to adapt the reader page update process where:
1840 * 1. We first splice the start and end of list of new pages between
1841 * the head page and its previous page.
1842 * 2. We cmpxchg the prev_page->next to point from head page to the
1843 * start of new pages list.
1844 * 3. Finally, we update the head->prev to the end of new list.
1845 *
1846 * We will try this process 10 times, to make sure that we don't keep
1847 * spinning.
1848 */
1849 retries = 10;
1850 success = 0;
1851 while (retries--) {
1852 struct list_head *head_page, *prev_page, *r;
1853 struct list_head *last_page, *first_page;
1854 struct list_head *head_page_with_bit;
Steven Rostedt77ae3652009-03-27 11:00:29 -04001855
Vaibhav Nagarnaik5040b4b2012-05-03 18:59:51 -07001856 head_page = &rb_set_head_page(cpu_buffer)->list;
Steven Rostedt54f7be52012-11-29 22:27:22 -05001857 if (!head_page)
1858 break;
Vaibhav Nagarnaik5040b4b2012-05-03 18:59:51 -07001859 prev_page = head_page->prev;
1860
1861 first_page = pages->next;
1862 last_page = pages->prev;
1863
1864 head_page_with_bit = (struct list_head *)
1865 ((unsigned long)head_page | RB_PAGE_HEAD);
1866
1867 last_page->next = head_page_with_bit;
1868 first_page->prev = prev_page;
1869
1870 r = cmpxchg(&prev_page->next, head_page_with_bit, first_page);
1871
1872 if (r == head_page_with_bit) {
1873 /*
1874 * yay, we replaced the page pointer to our new list,
1875 * now, we just have to update to head page's prev
1876 * pointer to point to end of list
1877 */
1878 head_page->prev = last_page;
1879 success = 1;
1880 break;
1881 }
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001882 }
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001883
Vaibhav Nagarnaik5040b4b2012-05-03 18:59:51 -07001884 if (success)
1885 INIT_LIST_HEAD(pages);
1886 /*
1887 * If we weren't successful in adding in new pages, warn and stop
1888 * tracing
1889 */
1890 RB_WARN_ON(cpu_buffer, !success);
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02001891 raw_spin_unlock_irq(&cpu_buffer->reader_lock);
Vaibhav Nagarnaik5040b4b2012-05-03 18:59:51 -07001892
1893 /* free pages if they weren't inserted */
1894 if (!success) {
1895 struct buffer_page *bpage, *tmp;
1896 list_for_each_entry_safe(bpage, tmp, &cpu_buffer->new_pages,
1897 list) {
1898 list_del_init(&bpage->list);
1899 free_buffer_page(bpage);
1900 }
1901 }
1902 return success;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001903}
1904
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001905static void rb_update_pages(struct ring_buffer_per_cpu *cpu_buffer)
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001906{
Vaibhav Nagarnaik5040b4b2012-05-03 18:59:51 -07001907 int success;
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001908
Vaibhav Nagarnaik5040b4b2012-05-03 18:59:51 -07001909 if (cpu_buffer->nr_pages_to_update > 0)
1910 success = rb_insert_pages(cpu_buffer);
1911 else
1912 success = rb_remove_pages(cpu_buffer,
1913 -cpu_buffer->nr_pages_to_update);
1914
1915 if (success)
1916 cpu_buffer->nr_pages += cpu_buffer->nr_pages_to_update;
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001917}
1918
1919static void update_pages_handler(struct work_struct *work)
1920{
1921 struct ring_buffer_per_cpu *cpu_buffer = container_of(work,
1922 struct ring_buffer_per_cpu, update_pages_work);
1923 rb_update_pages(cpu_buffer);
Vaibhav Nagarnaik05fdd702012-05-18 13:29:51 -07001924 complete(&cpu_buffer->update_done);
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001925}
1926
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001927/**
1928 * ring_buffer_resize - resize the ring buffer
1929 * @buffer: the buffer to resize.
1930 * @size: the new size.
zhangwei(Jovi)d6118512013-07-15 16:32:50 +08001931 * @cpu_id: the cpu buffer to resize
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001932 *
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001933 * Minimum size is 2 * BUF_PAGE_SIZE.
1934 *
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001935 * Returns 0 on success and < 0 on failure.
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001936 */
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05001937int ring_buffer_resize(struct trace_buffer *buffer, unsigned long size,
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001938 int cpu_id)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001939{
1940 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedt (Red Hat)9b94a8f2016-05-12 11:01:24 -04001941 unsigned long nr_pages;
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001942 int cpu, err = 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001943
Ingo Molnaree51a1d2008-11-13 14:58:31 +01001944 /*
1945 * Always succeed at resizing a non-existent buffer:
1946 */
1947 if (!buffer)
1948 return size;
1949
Steven Rostedt6a31e1f2012-05-23 15:35:17 -04001950 /* Make sure the requested buffer exists */
1951 if (cpu_id != RING_BUFFER_ALL_CPUS &&
1952 !cpumask_test_cpu(cpu_id, buffer->cpumask))
1953 return size;
1954
Steven Rostedt (Red Hat)59643d12016-05-13 09:34:12 -04001955 nr_pages = DIV_ROUND_UP(size, BUF_PAGE_SIZE);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001956
1957 /* we need a minimum of two pages */
Steven Rostedt (Red Hat)59643d12016-05-13 09:34:12 -04001958 if (nr_pages < 2)
1959 nr_pages = 2;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001960
Steven Rostedt (Red Hat)59643d12016-05-13 09:34:12 -04001961 size = nr_pages * BUF_PAGE_SIZE;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001962
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001963 /* prevent another thread from changing buffer sizes */
1964 mutex_lock(&buffer->mutex);
1965
Steven Rostedt (VMware)07b8b102020-03-27 16:21:22 -04001966
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001967 if (cpu_id == RING_BUFFER_ALL_CPUS) {
Steven Rostedt (VMware)07b8b102020-03-27 16:21:22 -04001968 /*
1969 * Don't succeed if resizing is disabled, as a reader might be
1970 * manipulating the ring buffer and is expecting a sane state while
1971 * this is true.
1972 */
1973 for_each_buffer_cpu(buffer, cpu) {
1974 cpu_buffer = buffer->buffers[cpu];
1975 if (atomic_read(&cpu_buffer->resize_disabled)) {
1976 err = -EBUSY;
1977 goto out_err_unlock;
1978 }
1979 }
1980
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001981 /* calculate the pages to update */
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001982 for_each_buffer_cpu(buffer, cpu) {
1983 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001984
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001985 cpu_buffer->nr_pages_to_update = nr_pages -
1986 cpu_buffer->nr_pages;
Vaibhav Nagarnaikd7ec4bf2011-06-07 17:01:42 -07001987 /*
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001988 * nothing more to do for removing pages or no update
Vaibhav Nagarnaikd7ec4bf2011-06-07 17:01:42 -07001989 */
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001990 if (cpu_buffer->nr_pages_to_update <= 0)
1991 continue;
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001992 /*
1993 * to add pages, make sure all new pages can be
1994 * allocated without receiving ENOMEM
1995 */
1996 INIT_LIST_HEAD(&cpu_buffer->new_pages);
1997 if (__rb_allocate_pages(cpu_buffer->nr_pages_to_update,
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001998 &cpu_buffer->new_pages, cpu)) {
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001999 /* not enough memory for new pages */
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07002000 err = -ENOMEM;
2001 goto out_err;
2002 }
2003 }
2004
2005 get_online_cpus();
2006 /*
2007 * Fire off all the required work handlers
Vaibhav Nagarnaik05fdd702012-05-18 13:29:51 -07002008 * We can't schedule on offline CPUs, but it's not necessary
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07002009 * since we can change their buffer sizes without any race.
2010 */
2011 for_each_buffer_cpu(buffer, cpu) {
2012 cpu_buffer = buffer->buffers[cpu];
Vaibhav Nagarnaik05fdd702012-05-18 13:29:51 -07002013 if (!cpu_buffer->nr_pages_to_update)
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07002014 continue;
2015
Corey Minyard021c5b32014-07-16 14:07:13 -05002016 /* Can't run something on an offline CPU. */
2017 if (!cpu_online(cpu)) {
Steven Rostedt (Red Hat)f5eb5582013-03-07 09:27:42 -05002018 rb_update_pages(cpu_buffer);
2019 cpu_buffer->nr_pages_to_update = 0;
2020 } else {
Vaibhav Nagarnaik05fdd702012-05-18 13:29:51 -07002021 schedule_work_on(cpu,
2022 &cpu_buffer->update_pages_work);
Steven Rostedt (Red Hat)f5eb5582013-03-07 09:27:42 -05002023 }
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002024 }
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002025
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08002026 /* wait for all the updates to complete */
2027 for_each_buffer_cpu(buffer, cpu) {
2028 cpu_buffer = buffer->buffers[cpu];
Vaibhav Nagarnaik05fdd702012-05-18 13:29:51 -07002029 if (!cpu_buffer->nr_pages_to_update)
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07002030 continue;
2031
Vaibhav Nagarnaik05fdd702012-05-18 13:29:51 -07002032 if (cpu_online(cpu))
2033 wait_for_completion(&cpu_buffer->update_done);
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07002034 cpu_buffer->nr_pages_to_update = 0;
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08002035 }
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07002036
2037 put_online_cpus();
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08002038 } else {
Steven Rostedt (VMware)6167c202018-05-16 11:17:06 -04002039 /* Make sure this CPU has been initialized */
Vaibhav Nagarnaik8e49f412012-10-10 16:40:27 -07002040 if (!cpumask_test_cpu(cpu_id, buffer->cpumask))
2041 goto out;
2042
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08002043 cpu_buffer = buffer->buffers[cpu_id];
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07002044
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08002045 if (nr_pages == cpu_buffer->nr_pages)
2046 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002047
Steven Rostedt (VMware)07b8b102020-03-27 16:21:22 -04002048 /*
2049 * Don't succeed if resizing is disabled, as a reader might be
2050 * manipulating the ring buffer and is expecting a sane state while
2051 * this is true.
2052 */
2053 if (atomic_read(&cpu_buffer->resize_disabled)) {
2054 err = -EBUSY;
2055 goto out_err_unlock;
2056 }
2057
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08002058 cpu_buffer->nr_pages_to_update = nr_pages -
2059 cpu_buffer->nr_pages;
2060
2061 INIT_LIST_HEAD(&cpu_buffer->new_pages);
2062 if (cpu_buffer->nr_pages_to_update > 0 &&
2063 __rb_allocate_pages(cpu_buffer->nr_pages_to_update,
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07002064 &cpu_buffer->new_pages, cpu_id)) {
2065 err = -ENOMEM;
2066 goto out_err;
2067 }
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08002068
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07002069 get_online_cpus();
2070
Corey Minyard021c5b32014-07-16 14:07:13 -05002071 /* Can't run something on an offline CPU. */
2072 if (!cpu_online(cpu_id))
Steven Rostedt (Red Hat)f5eb5582013-03-07 09:27:42 -05002073 rb_update_pages(cpu_buffer);
2074 else {
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07002075 schedule_work_on(cpu_id,
2076 &cpu_buffer->update_pages_work);
Vaibhav Nagarnaik05fdd702012-05-18 13:29:51 -07002077 wait_for_completion(&cpu_buffer->update_done);
Steven Rostedt (Red Hat)f5eb5582013-03-07 09:27:42 -05002078 }
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07002079
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07002080 cpu_buffer->nr_pages_to_update = 0;
Vaibhav Nagarnaik05fdd702012-05-18 13:29:51 -07002081 put_online_cpus();
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08002082 }
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002083
2084 out:
Steven Rostedt659f4512012-05-14 17:02:33 -04002085 /*
2086 * The ring buffer resize can happen with the ring buffer
2087 * enabled, so that the update disturbs the tracing as little
2088 * as possible. But if the buffer is disabled, we do not need
2089 * to worry about that, and we can take the time to verify
2090 * that the buffer is not corrupt.
2091 */
2092 if (atomic_read(&buffer->record_disabled)) {
2093 atomic_inc(&buffer->record_disabled);
2094 /*
2095 * Even though the buffer was disabled, we must make sure
2096 * that it is truly disabled before calling rb_check_pages.
2097 * There could have been a race between checking
2098 * record_disable and incrementing it.
2099 */
Paul E. McKenney74401722018-11-06 18:44:52 -08002100 synchronize_rcu();
Steven Rostedt659f4512012-05-14 17:02:33 -04002101 for_each_buffer_cpu(buffer, cpu) {
2102 cpu_buffer = buffer->buffers[cpu];
2103 rb_check_pages(cpu_buffer);
2104 }
2105 atomic_dec(&buffer->record_disabled);
2106 }
2107
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002108 mutex_unlock(&buffer->mutex);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002109 return size;
2110
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07002111 out_err:
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08002112 for_each_buffer_cpu(buffer, cpu) {
2113 struct buffer_page *bpage, *tmp;
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07002114
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08002115 cpu_buffer = buffer->buffers[cpu];
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08002116 cpu_buffer->nr_pages_to_update = 0;
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07002117
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08002118 if (list_empty(&cpu_buffer->new_pages))
2119 continue;
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07002120
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08002121 list_for_each_entry_safe(bpage, tmp, &cpu_buffer->new_pages,
2122 list) {
2123 list_del_init(&bpage->list);
2124 free_buffer_page(bpage);
2125 }
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002126 }
Steven Rostedt (VMware)07b8b102020-03-27 16:21:22 -04002127 out_err_unlock:
Vegard Nossum641d2f62008-11-18 19:22:13 +01002128 mutex_unlock(&buffer->mutex);
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07002129 return err;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002130}
Robert Richterc4f50182008-12-11 16:49:22 +01002131EXPORT_SYMBOL_GPL(ring_buffer_resize);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002132
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05002133void ring_buffer_change_overwrite(struct trace_buffer *buffer, int val)
David Sharp750912f2010-12-08 13:46:47 -08002134{
2135 mutex_lock(&buffer->mutex);
2136 if (val)
2137 buffer->flags |= RB_FL_OVERWRITE;
2138 else
2139 buffer->flags &= ~RB_FL_OVERWRITE;
2140 mutex_unlock(&buffer->mutex);
2141}
2142EXPORT_SYMBOL_GPL(ring_buffer_change_overwrite);
2143
Steven Rostedt (Red Hat)2289d562016-11-23 20:35:32 -05002144static __always_inline void *__rb_page_index(struct buffer_page *bpage, unsigned index)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002145{
Steven Rostedt044fa782008-12-02 23:50:03 -05002146 return bpage->page->data + index;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002147}
2148
Steven Rostedt (Red Hat)2289d562016-11-23 20:35:32 -05002149static __always_inline struct ring_buffer_event *
Steven Rostedtd7690412008-10-01 00:29:53 -04002150rb_reader_event(struct ring_buffer_per_cpu *cpu_buffer)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002151{
Steven Rostedt6f807ac2008-10-04 02:00:58 -04002152 return __rb_page_index(cpu_buffer->reader_page,
2153 cpu_buffer->reader_page->read);
2154}
2155
Steven Rostedt (Red Hat)2289d562016-11-23 20:35:32 -05002156static __always_inline unsigned rb_page_commit(struct buffer_page *bpage)
Steven Rostedtbf41a152008-10-04 02:00:59 -04002157{
Steven Rostedtabc9b562008-12-02 15:34:06 -05002158 return local_read(&bpage->page->commit);
Steven Rostedtbf41a152008-10-04 02:00:59 -04002159}
2160
Steven Rostedt (VMware)785888c2020-03-17 17:32:27 -04002161static struct ring_buffer_event *
2162rb_iter_head_event(struct ring_buffer_iter *iter)
2163{
2164 struct ring_buffer_event *event;
2165 struct buffer_page *iter_head_page = iter->head_page;
2166 unsigned long commit;
2167 unsigned length;
2168
Steven Rostedt (VMware)153368c2020-03-17 17:32:29 -04002169 if (iter->head != iter->next_event)
2170 return iter->event;
2171
Steven Rostedt (VMware)785888c2020-03-17 17:32:27 -04002172 /*
2173 * When the writer goes across pages, it issues a cmpxchg which
2174 * is a mb(), which will synchronize with the rmb here.
2175 * (see rb_tail_page_update() and __rb_reserve_next())
2176 */
2177 commit = rb_page_commit(iter_head_page);
2178 smp_rmb();
2179 event = __rb_page_index(iter_head_page, iter->head);
2180 length = rb_event_length(event);
2181
2182 /*
2183 * READ_ONCE() doesn't work on functions and we don't want the
2184 * compiler doing any crazy optimizations with length.
2185 */
2186 barrier();
2187
2188 if ((iter->head + length) > commit || length > BUF_MAX_DATA_SIZE)
2189 /* Writer corrupted the read? */
2190 goto reset;
2191
2192 memcpy(iter->event, event, length);
2193 /*
2194 * If the page stamp is still the same after this rmb() then the
2195 * event was safely copied without the writer entering the page.
2196 */
2197 smp_rmb();
2198
2199 /* Make sure the page didn't change since we read this */
2200 if (iter->page_stamp != iter_head_page->page->time_stamp ||
2201 commit > rb_page_commit(iter_head_page))
2202 goto reset;
2203
2204 iter->next_event = iter->head + length;
2205 return iter->event;
2206 reset:
2207 /* Reset to the beginning */
2208 iter->page_stamp = iter->read_stamp = iter->head_page->page->time_stamp;
2209 iter->head = 0;
2210 iter->next_event = 0;
Steven Rostedt (VMware)c9b7a4a2020-03-17 17:32:32 -04002211 iter->missed_events = 1;
Steven Rostedt (VMware)785888c2020-03-17 17:32:27 -04002212 return NULL;
2213}
2214
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002215/* Size is determined by what has been committed */
Steven Rostedt (Red Hat)2289d562016-11-23 20:35:32 -05002216static __always_inline unsigned rb_page_size(struct buffer_page *bpage)
Steven Rostedtbf41a152008-10-04 02:00:59 -04002217{
2218 return rb_page_commit(bpage);
2219}
2220
Steven Rostedt (Red Hat)2289d562016-11-23 20:35:32 -05002221static __always_inline unsigned
Steven Rostedtbf41a152008-10-04 02:00:59 -04002222rb_commit_index(struct ring_buffer_per_cpu *cpu_buffer)
2223{
2224 return rb_page_commit(cpu_buffer->commit_page);
2225}
2226
Steven Rostedt (Red Hat)2289d562016-11-23 20:35:32 -05002227static __always_inline unsigned
Steven Rostedtbf41a152008-10-04 02:00:59 -04002228rb_event_index(struct ring_buffer_event *event)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002229{
Steven Rostedtbf41a152008-10-04 02:00:59 -04002230 unsigned long addr = (unsigned long)event;
2231
Steven Rostedt22f470f2009-06-11 09:29:58 -04002232 return (addr & ~PAGE_MASK) - BUF_PAGE_HDR_SIZE;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002233}
2234
Andrew Morton34a148b2009-01-09 12:27:09 -08002235static void rb_inc_iter(struct ring_buffer_iter *iter)
Steven Rostedtd7690412008-10-01 00:29:53 -04002236{
2237 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
2238
2239 /*
2240 * The iterator could be on the reader page (it starts there).
2241 * But the head could have moved, since the reader was
2242 * found. Check for this case and assign the iterator
2243 * to the head page instead of next.
2244 */
2245 if (iter->head_page == cpu_buffer->reader_page)
Steven Rostedt77ae3652009-03-27 11:00:29 -04002246 iter->head_page = rb_set_head_page(cpu_buffer);
Steven Rostedtd7690412008-10-01 00:29:53 -04002247 else
2248 rb_inc_page(cpu_buffer, &iter->head_page);
2249
Steven Rostedt (VMware)28e3fc52020-03-17 17:32:26 -04002250 iter->page_stamp = iter->read_stamp = iter->head_page->page->time_stamp;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002251 iter->head = 0;
Steven Rostedt (VMware)785888c2020-03-17 17:32:27 -04002252 iter->next_event = 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002253}
2254
Steven Rostedt77ae3652009-03-27 11:00:29 -04002255/*
2256 * rb_handle_head_page - writer hit the head page
2257 *
2258 * Returns: +1 to retry page
2259 * 0 to continue
2260 * -1 on error
2261 */
2262static int
2263rb_handle_head_page(struct ring_buffer_per_cpu *cpu_buffer,
2264 struct buffer_page *tail_page,
2265 struct buffer_page *next_page)
2266{
2267 struct buffer_page *new_head;
2268 int entries;
2269 int type;
2270 int ret;
2271
2272 entries = rb_page_entries(next_page);
2273
2274 /*
2275 * The hard part is here. We need to move the head
2276 * forward, and protect against both readers on
2277 * other CPUs and writers coming in via interrupts.
2278 */
2279 type = rb_head_page_set_update(cpu_buffer, next_page, tail_page,
2280 RB_PAGE_HEAD);
2281
2282 /*
2283 * type can be one of four:
2284 * NORMAL - an interrupt already moved it for us
2285 * HEAD - we are the first to get here.
2286 * UPDATE - we are the interrupt interrupting
2287 * a current move.
2288 * MOVED - a reader on another CPU moved the next
2289 * pointer to its reader page. Give up
2290 * and try again.
2291 */
2292
2293 switch (type) {
2294 case RB_PAGE_HEAD:
2295 /*
2296 * We changed the head to UPDATE, thus
2297 * it is our responsibility to update
2298 * the counters.
2299 */
2300 local_add(entries, &cpu_buffer->overrun);
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -07002301 local_sub(BUF_PAGE_SIZE, &cpu_buffer->entries_bytes);
Steven Rostedt77ae3652009-03-27 11:00:29 -04002302
2303 /*
2304 * The entries will be zeroed out when we move the
2305 * tail page.
2306 */
2307
2308 /* still more to do */
2309 break;
2310
2311 case RB_PAGE_UPDATE:
2312 /*
2313 * This is an interrupt that interrupt the
2314 * previous update. Still more to do.
2315 */
2316 break;
2317 case RB_PAGE_NORMAL:
2318 /*
2319 * An interrupt came in before the update
2320 * and processed this for us.
2321 * Nothing left to do.
2322 */
2323 return 1;
2324 case RB_PAGE_MOVED:
2325 /*
2326 * The reader is on another CPU and just did
2327 * a swap with our next_page.
2328 * Try again.
2329 */
2330 return 1;
2331 default:
2332 RB_WARN_ON(cpu_buffer, 1); /* WTF??? */
2333 return -1;
2334 }
2335
2336 /*
2337 * Now that we are here, the old head pointer is
2338 * set to UPDATE. This will keep the reader from
2339 * swapping the head page with the reader page.
2340 * The reader (on another CPU) will spin till
2341 * we are finished.
2342 *
2343 * We just need to protect against interrupts
2344 * doing the job. We will set the next pointer
2345 * to HEAD. After that, we set the old pointer
2346 * to NORMAL, but only if it was HEAD before.
2347 * otherwise we are an interrupt, and only
2348 * want the outer most commit to reset it.
2349 */
2350 new_head = next_page;
2351 rb_inc_page(cpu_buffer, &new_head);
2352
2353 ret = rb_head_page_set_head(cpu_buffer, new_head, next_page,
2354 RB_PAGE_NORMAL);
2355
2356 /*
2357 * Valid returns are:
2358 * HEAD - an interrupt came in and already set it.
2359 * NORMAL - One of two things:
2360 * 1) We really set it.
2361 * 2) A bunch of interrupts came in and moved
2362 * the page forward again.
2363 */
2364 switch (ret) {
2365 case RB_PAGE_HEAD:
2366 case RB_PAGE_NORMAL:
2367 /* OK */
2368 break;
2369 default:
2370 RB_WARN_ON(cpu_buffer, 1);
2371 return -1;
2372 }
2373
2374 /*
2375 * It is possible that an interrupt came in,
2376 * set the head up, then more interrupts came in
2377 * and moved it again. When we get back here,
2378 * the page would have been set to NORMAL but we
2379 * just set it back to HEAD.
2380 *
2381 * How do you detect this? Well, if that happened
2382 * the tail page would have moved.
2383 */
2384 if (ret == RB_PAGE_NORMAL) {
Steven Rostedt (Red Hat)85736362015-11-17 14:03:11 -05002385 struct buffer_page *buffer_tail_page;
2386
2387 buffer_tail_page = READ_ONCE(cpu_buffer->tail_page);
Steven Rostedt77ae3652009-03-27 11:00:29 -04002388 /*
2389 * If the tail had moved passed next, then we need
2390 * to reset the pointer.
2391 */
Steven Rostedt (Red Hat)85736362015-11-17 14:03:11 -05002392 if (buffer_tail_page != tail_page &&
2393 buffer_tail_page != next_page)
Steven Rostedt77ae3652009-03-27 11:00:29 -04002394 rb_head_page_set_normal(cpu_buffer, new_head,
2395 next_page,
2396 RB_PAGE_HEAD);
2397 }
2398
2399 /*
2400 * If this was the outer most commit (the one that
2401 * changed the original pointer from HEAD to UPDATE),
2402 * then it is up to us to reset it to NORMAL.
2403 */
2404 if (type == RB_PAGE_HEAD) {
2405 ret = rb_head_page_set_normal(cpu_buffer, next_page,
2406 tail_page,
2407 RB_PAGE_UPDATE);
2408 if (RB_WARN_ON(cpu_buffer,
2409 ret != RB_PAGE_UPDATE))
2410 return -1;
2411 }
2412
2413 return 0;
2414}
2415
Steven Rostedtc7b09302009-06-11 11:12:00 -04002416static inline void
2417rb_reset_tail(struct ring_buffer_per_cpu *cpu_buffer,
Steven Rostedt (Red Hat)fcc742e2015-05-28 17:13:14 -04002418 unsigned long tail, struct rb_event_info *info)
Steven Rostedtc7b09302009-06-11 11:12:00 -04002419{
Steven Rostedt (Red Hat)fcc742e2015-05-28 17:13:14 -04002420 struct buffer_page *tail_page = info->tail_page;
Steven Rostedtc7b09302009-06-11 11:12:00 -04002421 struct ring_buffer_event *event;
Steven Rostedt (Red Hat)fcc742e2015-05-28 17:13:14 -04002422 unsigned long length = info->length;
Steven Rostedtc7b09302009-06-11 11:12:00 -04002423
2424 /*
2425 * Only the event that crossed the page boundary
2426 * must fill the old tail_page with padding.
2427 */
2428 if (tail >= BUF_PAGE_SIZE) {
Steven Rostedtb3230c82010-05-21 11:55:21 -04002429 /*
2430 * If the page was filled, then we still need
2431 * to update the real_end. Reset it to zero
2432 * and the reader will ignore it.
2433 */
2434 if (tail == BUF_PAGE_SIZE)
2435 tail_page->real_end = 0;
2436
Steven Rostedtc7b09302009-06-11 11:12:00 -04002437 local_sub(length, &tail_page->write);
2438 return;
2439 }
2440
2441 event = __rb_page_index(tail_page, tail);
2442
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -07002443 /* account for padding bytes */
2444 local_add(BUF_PAGE_SIZE - tail, &cpu_buffer->entries_bytes);
2445
Steven Rostedtc7b09302009-06-11 11:12:00 -04002446 /*
Steven Rostedtff0ff842010-03-31 22:11:42 -04002447 * Save the original length to the meta data.
2448 * This will be used by the reader to add lost event
2449 * counter.
2450 */
2451 tail_page->real_end = tail;
2452
2453 /*
Steven Rostedtc7b09302009-06-11 11:12:00 -04002454 * If this event is bigger than the minimum size, then
2455 * we need to be careful that we don't subtract the
2456 * write counter enough to allow another writer to slip
2457 * in on this page.
2458 * We put in a discarded commit instead, to make sure
2459 * that this space is not used again.
2460 *
2461 * If we are less than the minimum size, we don't need to
2462 * worry about it.
2463 */
2464 if (tail > (BUF_PAGE_SIZE - RB_EVNT_MIN_SIZE)) {
2465 /* No room for any events */
2466
2467 /* Mark the rest of the page with padding */
2468 rb_event_set_padding(event);
2469
2470 /* Set the write back to the previous setting */
2471 local_sub(length, &tail_page->write);
2472 return;
2473 }
2474
2475 /* Put in a discarded event */
2476 event->array[0] = (BUF_PAGE_SIZE - tail) - RB_EVNT_HDR_SIZE;
2477 event->type_len = RINGBUF_TYPE_PADDING;
2478 /* time delta must be non zero */
2479 event->time_delta = 1;
Steven Rostedtc7b09302009-06-11 11:12:00 -04002480
2481 /* Set write to end of buffer */
2482 length = (tail + length) - BUF_PAGE_SIZE;
2483 local_sub(length, &tail_page->write);
2484}
Steven Rostedt6634ff22009-05-06 15:30:07 -04002485
Steven Rostedt (Red Hat)4239c382015-11-17 16:36:06 -05002486static inline void rb_end_commit(struct ring_buffer_per_cpu *cpu_buffer);
2487
Steven Rostedt747e94a2010-10-08 13:51:48 -04002488/*
2489 * This is the slow path, force gcc not to inline it.
2490 */
2491static noinline struct ring_buffer_event *
Steven Rostedt6634ff22009-05-06 15:30:07 -04002492rb_move_tail(struct ring_buffer_per_cpu *cpu_buffer,
Steven Rostedt (Red Hat)fcc742e2015-05-28 17:13:14 -04002493 unsigned long tail, struct rb_event_info *info)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002494{
Steven Rostedt (Red Hat)fcc742e2015-05-28 17:13:14 -04002495 struct buffer_page *tail_page = info->tail_page;
Steven Rostedt5a50e332009-11-17 08:43:01 -05002496 struct buffer_page *commit_page = cpu_buffer->commit_page;
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05002497 struct trace_buffer *buffer = cpu_buffer->buffer;
Steven Rostedt77ae3652009-03-27 11:00:29 -04002498 struct buffer_page *next_page;
2499 int ret;
Steven Rostedtaa20ae82009-05-05 21:16:11 -04002500
2501 next_page = tail_page;
2502
Steven Rostedtaa20ae82009-05-05 21:16:11 -04002503 rb_inc_page(cpu_buffer, &next_page);
2504
Steven Rostedtaa20ae82009-05-05 21:16:11 -04002505 /*
2506 * If for some reason, we had an interrupt storm that made
2507 * it all the way around the buffer, bail, and warn
2508 * about it.
2509 */
2510 if (unlikely(next_page == commit_page)) {
Steven Rostedt77ae3652009-03-27 11:00:29 -04002511 local_inc(&cpu_buffer->commit_overrun);
Steven Rostedtaa20ae82009-05-05 21:16:11 -04002512 goto out_reset;
2513 }
2514
Steven Rostedt77ae3652009-03-27 11:00:29 -04002515 /*
2516 * This is where the fun begins!
2517 *
2518 * We are fighting against races between a reader that
2519 * could be on another CPU trying to swap its reader
2520 * page with the buffer head.
2521 *
2522 * We are also fighting against interrupts coming in and
2523 * moving the head or tail on us as well.
2524 *
2525 * If the next page is the head page then we have filled
2526 * the buffer, unless the commit page is still on the
2527 * reader page.
2528 */
2529 if (rb_is_head_page(cpu_buffer, next_page, &tail_page->list)) {
Steven Rostedtaa20ae82009-05-05 21:16:11 -04002530
Steven Rostedt77ae3652009-03-27 11:00:29 -04002531 /*
2532 * If the commit is not on the reader page, then
2533 * move the header page.
2534 */
2535 if (!rb_is_reader_page(cpu_buffer->commit_page)) {
2536 /*
2537 * If we are not in overwrite mode,
2538 * this is easy, just stop here.
2539 */
Slava Pestov884bfe82011-07-15 14:23:58 -07002540 if (!(buffer->flags & RB_FL_OVERWRITE)) {
2541 local_inc(&cpu_buffer->dropped_events);
Steven Rostedt77ae3652009-03-27 11:00:29 -04002542 goto out_reset;
Slava Pestov884bfe82011-07-15 14:23:58 -07002543 }
Steven Rostedtaa20ae82009-05-05 21:16:11 -04002544
Steven Rostedt77ae3652009-03-27 11:00:29 -04002545 ret = rb_handle_head_page(cpu_buffer,
2546 tail_page,
2547 next_page);
2548 if (ret < 0)
2549 goto out_reset;
2550 if (ret)
2551 goto out_again;
2552 } else {
2553 /*
2554 * We need to be careful here too. The
2555 * commit page could still be on the reader
2556 * page. We could have a small buffer, and
2557 * have filled up the buffer with events
2558 * from interrupts and such, and wrapped.
2559 *
2560 * Note, if the tail page is also the on the
2561 * reader_page, we let it move out.
2562 */
2563 if (unlikely((cpu_buffer->commit_page !=
2564 cpu_buffer->tail_page) &&
2565 (cpu_buffer->commit_page ==
2566 cpu_buffer->reader_page))) {
2567 local_inc(&cpu_buffer->commit_overrun);
2568 goto out_reset;
2569 }
Steven Rostedtaa20ae82009-05-05 21:16:11 -04002570 }
2571 }
2572
Steven Rostedt (Red Hat)70004982015-11-17 15:15:19 -05002573 rb_tail_page_update(cpu_buffer, tail_page, next_page);
Steven Rostedtaa20ae82009-05-05 21:16:11 -04002574
Steven Rostedt77ae3652009-03-27 11:00:29 -04002575 out_again:
Steven Rostedtaa20ae82009-05-05 21:16:11 -04002576
Steven Rostedt (Red Hat)fcc742e2015-05-28 17:13:14 -04002577 rb_reset_tail(cpu_buffer, tail, info);
Steven Rostedtaa20ae82009-05-05 21:16:11 -04002578
Steven Rostedt (Red Hat)4239c382015-11-17 16:36:06 -05002579 /* Commit what we have for now. */
2580 rb_end_commit(cpu_buffer);
2581 /* rb_end_commit() decs committing */
2582 local_inc(&cpu_buffer->committing);
2583
Steven Rostedtaa20ae82009-05-05 21:16:11 -04002584 /* fail and let the caller try again */
2585 return ERR_PTR(-EAGAIN);
2586
Steven Rostedt45141d42009-02-12 13:19:48 -05002587 out_reset:
Lai Jiangshan6f3b3442009-01-12 11:06:18 +08002588 /* reset write */
Steven Rostedt (Red Hat)fcc742e2015-05-28 17:13:14 -04002589 rb_reset_tail(cpu_buffer, tail, info);
Lai Jiangshan6f3b3442009-01-12 11:06:18 +08002590
Steven Rostedtbf41a152008-10-04 02:00:59 -04002591 return NULL;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002592}
2593
Steven Rostedt (Red Hat)d90fd772015-05-29 12:12:27 -04002594/* Slow path, do not inline */
2595static noinline struct ring_buffer_event *
Tom Zanussidc4e2802018-01-15 20:51:40 -06002596rb_add_time_stamp(struct ring_buffer_event *event, u64 delta, bool abs)
Steven Rostedt (Red Hat)d90fd772015-05-29 12:12:27 -04002597{
Tom Zanussidc4e2802018-01-15 20:51:40 -06002598 if (abs)
2599 event->type_len = RINGBUF_TYPE_TIME_STAMP;
2600 else
2601 event->type_len = RINGBUF_TYPE_TIME_EXTEND;
Steven Rostedt (Red Hat)d90fd772015-05-29 12:12:27 -04002602
Tom Zanussidc4e2802018-01-15 20:51:40 -06002603 /* Not the first event on the page, or not delta? */
2604 if (abs || rb_event_index(event)) {
Steven Rostedt (Red Hat)d90fd772015-05-29 12:12:27 -04002605 event->time_delta = delta & TS_MASK;
2606 event->array[0] = delta >> TS_SHIFT;
2607 } else {
2608 /* nope, just zero it */
2609 event->time_delta = 0;
2610 event->array[0] = 0;
2611 }
2612
2613 return skip_time_extend(event);
2614}
2615
Yaowei Baicdb2a0a92015-09-29 22:43:34 +08002616static inline bool rb_event_is_commit(struct ring_buffer_per_cpu *cpu_buffer,
Steven Rostedt (Red Hat)b7dc42f2015-09-03 08:57:12 -04002617 struct ring_buffer_event *event);
2618
Steven Rostedt (Red Hat)d90fd772015-05-29 12:12:27 -04002619/**
2620 * rb_update_event - update event type and data
Steven Rostedt (VMware)cfc585a2020-01-14 16:27:51 -05002621 * @cpu_buffer: The per cpu buffer of the @event
Steven Rostedt (Red Hat)d90fd772015-05-29 12:12:27 -04002622 * @event: the event to update
Steven Rostedt (VMware)cfc585a2020-01-14 16:27:51 -05002623 * @info: The info to update the @event with (contains length and delta)
Steven Rostedt (Red Hat)d90fd772015-05-29 12:12:27 -04002624 *
Steven Rostedt (VMware)cfc585a2020-01-14 16:27:51 -05002625 * Update the type and data fields of the @event. The length
Steven Rostedt (Red Hat)d90fd772015-05-29 12:12:27 -04002626 * is the actual size that is written to the ring buffer,
2627 * and with this, we can determine what to place into the
2628 * data field.
2629 */
Steven Rostedt (Red Hat)b7dc42f2015-09-03 08:57:12 -04002630static void
Steven Rostedt (Red Hat)d90fd772015-05-29 12:12:27 -04002631rb_update_event(struct ring_buffer_per_cpu *cpu_buffer,
2632 struct ring_buffer_event *event,
2633 struct rb_event_info *info)
2634{
2635 unsigned length = info->length;
2636 u64 delta = info->delta;
2637
2638 /*
2639 * If we need to add a timestamp, then we
Steven Rostedt (VMware)6167c202018-05-16 11:17:06 -04002640 * add it to the start of the reserved space.
Steven Rostedt (Red Hat)d90fd772015-05-29 12:12:27 -04002641 */
2642 if (unlikely(info->add_timestamp)) {
Steven Rostedt (VMware)7c4b4a52020-06-28 22:52:26 -04002643 bool abs = info->add_timestamp &
2644 (RB_ADD_STAMP_FORCE | RB_ADD_STAMP_ABSOLUTE);
Tom Zanussidc4e2802018-01-15 20:51:40 -06002645
Steven Rostedt (VMware)097350d2020-06-22 15:18:15 -04002646 event = rb_add_time_stamp(event, abs ? info->delta : delta, abs);
Steven Rostedt (Red Hat)d90fd772015-05-29 12:12:27 -04002647 length -= RB_LEN_TIME_EXTEND;
2648 delta = 0;
2649 }
2650
2651 event->time_delta = delta;
2652 length -= RB_EVNT_HDR_SIZE;
Steven Rostedt (VMware)86b3de62019-05-28 09:36:19 -04002653 if (length > RB_MAX_SMALL_DATA) {
Steven Rostedt (Red Hat)d90fd772015-05-29 12:12:27 -04002654 event->type_len = 0;
2655 event->array[0] = length;
2656 } else
2657 event->type_len = DIV_ROUND_UP(length, RB_ALIGNMENT);
2658}
2659
2660static unsigned rb_calculate_event_length(unsigned length)
2661{
2662 struct ring_buffer_event event; /* Used only for sizeof array */
2663
2664 /* zero length can cause confusions */
2665 if (!length)
2666 length++;
2667
Steven Rostedt (VMware)86b3de62019-05-28 09:36:19 -04002668 if (length > RB_MAX_SMALL_DATA)
Steven Rostedt (Red Hat)d90fd772015-05-29 12:12:27 -04002669 length += sizeof(event.array[0]);
2670
2671 length += RB_EVNT_HDR_SIZE;
Steven Rostedt (VMware)86b3de62019-05-28 09:36:19 -04002672 length = ALIGN(length, RB_ALIGNMENT);
Steven Rostedt (Red Hat)d90fd772015-05-29 12:12:27 -04002673
2674 /*
2675 * In case the time delta is larger than the 27 bits for it
2676 * in the header, we need to add a timestamp. If another
2677 * event comes in when trying to discard this one to increase
2678 * the length, then the timestamp will be added in the allocated
2679 * space of this event. If length is bigger than the size needed
2680 * for the TIME_EXTEND, then padding has to be used. The events
2681 * length must be either RB_LEN_TIME_EXTEND, or greater than or equal
2682 * to RB_LEN_TIME_EXTEND + 8, as 8 is the minimum size for padding.
2683 * As length is a multiple of 4, we only need to worry if it
2684 * is 12 (RB_LEN_TIME_EXTEND + 4).
2685 */
2686 if (length == RB_LEN_TIME_EXTEND + RB_ALIGNMENT)
2687 length += RB_ALIGNMENT;
2688
2689 return length;
2690}
2691
Steven Rostedt (Red Hat)9826b272015-05-28 17:36:45 -04002692#ifndef CONFIG_HAVE_UNSTABLE_SCHED_CLOCK
2693static inline bool sched_clock_stable(void)
2694{
2695 return true;
2696}
2697#endif
2698
Steven Rostedt (VMware)a389d862020-06-28 22:52:25 -04002699static __always_inline bool
2700rb_event_is_commit(struct ring_buffer_per_cpu *cpu_buffer,
2701 struct ring_buffer_event *event)
2702{
2703 unsigned long addr = (unsigned long)event;
2704 unsigned long index;
2705
2706 index = rb_event_index(event);
2707 addr &= PAGE_MASK;
2708
2709 return cpu_buffer->commit_page->page == (void *)addr &&
2710 rb_commit_index(cpu_buffer) == index;
2711}
2712
2713static u64 rb_time_delta(struct ring_buffer_event *event)
2714{
2715 switch (event->type_len) {
2716 case RINGBUF_TYPE_PADDING:
2717 return 0;
2718
2719 case RINGBUF_TYPE_TIME_EXTEND:
2720 return ring_buffer_event_time_stamp(event);
2721
2722 case RINGBUF_TYPE_TIME_STAMP:
2723 return 0;
2724
2725 case RINGBUF_TYPE_DATA:
2726 return event->time_delta;
2727 default:
2728 return 0;
2729 }
2730}
2731
Steven Rostedt (Red Hat)a4543a22015-05-29 09:40:18 -04002732static inline int
2733rb_try_to_discard(struct ring_buffer_per_cpu *cpu_buffer,
Steven Rostedt (Red Hat)d90fd772015-05-29 12:12:27 -04002734 struct ring_buffer_event *event)
2735{
2736 unsigned long new_index, old_index;
2737 struct buffer_page *bpage;
2738 unsigned long index;
2739 unsigned long addr;
Steven Rostedt (VMware)a389d862020-06-28 22:52:25 -04002740 u64 write_stamp;
2741 u64 delta;
Steven Rostedt (Red Hat)d90fd772015-05-29 12:12:27 -04002742
2743 new_index = rb_event_index(event);
2744 old_index = new_index + rb_event_ts_length(event);
2745 addr = (unsigned long)event;
2746 addr &= PAGE_MASK;
2747
Steven Rostedt (Red Hat)85736362015-11-17 14:03:11 -05002748 bpage = READ_ONCE(cpu_buffer->tail_page);
Steven Rostedt (Red Hat)d90fd772015-05-29 12:12:27 -04002749
Steven Rostedt (VMware)a389d862020-06-28 22:52:25 -04002750 delta = rb_time_delta(event);
2751
Steven Rostedt (VMware)10464b42020-06-28 22:52:27 -04002752 if (!rb_time_read(&cpu_buffer->write_stamp, &write_stamp))
2753 return 0;
Steven Rostedt (VMware)a389d862020-06-28 22:52:25 -04002754
2755 /* Make sure the write stamp is read before testing the location */
2756 barrier();
2757
Steven Rostedt (Red Hat)d90fd772015-05-29 12:12:27 -04002758 if (bpage->page == (void *)addr && rb_page_write(bpage) == old_index) {
2759 unsigned long write_mask =
2760 local_read(&bpage->write) & ~RB_WRITE_MASK;
2761 unsigned long event_length = rb_event_length(event);
Steven Rostedt (VMware)a389d862020-06-28 22:52:25 -04002762
Steven Rostedt (VMware)a389d862020-06-28 22:52:25 -04002763 /* Something came in, can't discard */
Steven Rostedt (VMware)10464b42020-06-28 22:52:27 -04002764 if (!rb_time_cmpxchg(&cpu_buffer->write_stamp,
2765 write_stamp, write_stamp - delta))
Steven Rostedt (VMware)a389d862020-06-28 22:52:25 -04002766 return 0;
2767
2768 /*
2769 * If an event were to come in now, it would see that the
2770 * write_stamp and the before_stamp are different, and assume
2771 * that this event just added itself before updating
2772 * the write stamp. The interrupting event will fix the
2773 * write stamp for us, and use the before stamp as its delta.
2774 */
2775
Steven Rostedt (Red Hat)d90fd772015-05-29 12:12:27 -04002776 /*
2777 * This is on the tail page. It is possible that
2778 * a write could come in and move the tail page
2779 * and write to the next page. That is fine
2780 * because we just shorten what is on this page.
2781 */
2782 old_index += write_mask;
2783 new_index += write_mask;
2784 index = local_cmpxchg(&bpage->write, old_index, new_index);
2785 if (index == old_index) {
2786 /* update counters */
2787 local_sub(event_length, &cpu_buffer->entries_bytes);
2788 return 1;
2789 }
2790 }
2791
2792 /* could not discard */
2793 return 0;
2794}
2795
2796static void rb_start_commit(struct ring_buffer_per_cpu *cpu_buffer)
2797{
2798 local_inc(&cpu_buffer->committing);
2799 local_inc(&cpu_buffer->commits);
2800}
2801
Steven Rostedt (Red Hat)38e11df2016-11-23 20:42:31 -05002802static __always_inline void
Steven Rostedt (Red Hat)d90fd772015-05-29 12:12:27 -04002803rb_set_commit_to_write(struct ring_buffer_per_cpu *cpu_buffer)
2804{
2805 unsigned long max_count;
2806
2807 /*
2808 * We only race with interrupts and NMIs on this CPU.
2809 * If we own the commit event, then we can commit
2810 * all others that interrupted us, since the interruptions
2811 * are in stack format (they finish before they come
2812 * back to us). This allows us to do a simple loop to
2813 * assign the commit to the tail.
2814 */
2815 again:
2816 max_count = cpu_buffer->nr_pages * 100;
2817
Steven Rostedt (Red Hat)85736362015-11-17 14:03:11 -05002818 while (cpu_buffer->commit_page != READ_ONCE(cpu_buffer->tail_page)) {
Steven Rostedt (Red Hat)d90fd772015-05-29 12:12:27 -04002819 if (RB_WARN_ON(cpu_buffer, !(--max_count)))
2820 return;
2821 if (RB_WARN_ON(cpu_buffer,
2822 rb_is_reader_page(cpu_buffer->tail_page)))
2823 return;
2824 local_set(&cpu_buffer->commit_page->page->commit,
2825 rb_page_write(cpu_buffer->commit_page));
2826 rb_inc_page(cpu_buffer, &cpu_buffer->commit_page);
Steven Rostedt (Red Hat)d90fd772015-05-29 12:12:27 -04002827 /* add barrier to keep gcc from optimizing too much */
2828 barrier();
2829 }
2830 while (rb_commit_index(cpu_buffer) !=
2831 rb_page_write(cpu_buffer->commit_page)) {
2832
2833 local_set(&cpu_buffer->commit_page->page->commit,
2834 rb_page_write(cpu_buffer->commit_page));
2835 RB_WARN_ON(cpu_buffer,
2836 local_read(&cpu_buffer->commit_page->page->commit) &
2837 ~RB_WRITE_MASK);
2838 barrier();
2839 }
2840
2841 /* again, keep gcc from optimizing */
2842 barrier();
2843
2844 /*
2845 * If an interrupt came in just after the first while loop
2846 * and pushed the tail page forward, we will be left with
2847 * a dangling commit that will never go forward.
2848 */
Steven Rostedt (Red Hat)85736362015-11-17 14:03:11 -05002849 if (unlikely(cpu_buffer->commit_page != READ_ONCE(cpu_buffer->tail_page)))
Steven Rostedt (Red Hat)d90fd772015-05-29 12:12:27 -04002850 goto again;
2851}
2852
Steven Rostedt (Red Hat)38e11df2016-11-23 20:42:31 -05002853static __always_inline void rb_end_commit(struct ring_buffer_per_cpu *cpu_buffer)
Steven Rostedt (Red Hat)d90fd772015-05-29 12:12:27 -04002854{
2855 unsigned long commits;
2856
2857 if (RB_WARN_ON(cpu_buffer,
2858 !local_read(&cpu_buffer->committing)))
2859 return;
2860
2861 again:
2862 commits = local_read(&cpu_buffer->commits);
2863 /* synchronize with interrupts */
2864 barrier();
2865 if (local_read(&cpu_buffer->committing) == 1)
2866 rb_set_commit_to_write(cpu_buffer);
2867
2868 local_dec(&cpu_buffer->committing);
2869
2870 /* synchronize with interrupts */
2871 barrier();
2872
2873 /*
2874 * Need to account for interrupts coming in between the
2875 * updating of the commit page and the clearing of the
2876 * committing counter.
2877 */
2878 if (unlikely(local_read(&cpu_buffer->commits) != commits) &&
2879 !local_read(&cpu_buffer->committing)) {
2880 local_inc(&cpu_buffer->committing);
2881 goto again;
2882 }
2883}
2884
2885static inline void rb_event_discard(struct ring_buffer_event *event)
2886{
Tom Zanussidc4e2802018-01-15 20:51:40 -06002887 if (extended_time(event))
Steven Rostedt (Red Hat)d90fd772015-05-29 12:12:27 -04002888 event = skip_time_extend(event);
2889
2890 /* array[0] holds the actual length for the discarded event */
2891 event->array[0] = rb_event_data_length(event) - RB_EVNT_HDR_SIZE;
2892 event->type_len = RINGBUF_TYPE_PADDING;
2893 /* time delta must be non zero */
2894 if (!event->time_delta)
2895 event->time_delta = 1;
2896}
2897
Steven Rostedt (Red Hat)d90fd772015-05-29 12:12:27 -04002898static void rb_commit(struct ring_buffer_per_cpu *cpu_buffer,
2899 struct ring_buffer_event *event)
2900{
2901 local_inc(&cpu_buffer->entries);
Steven Rostedt (Red Hat)d90fd772015-05-29 12:12:27 -04002902 rb_end_commit(cpu_buffer);
2903}
2904
2905static __always_inline void
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05002906rb_wakeups(struct trace_buffer *buffer, struct ring_buffer_per_cpu *cpu_buffer)
Steven Rostedt (Red Hat)d90fd772015-05-29 12:12:27 -04002907{
Steven Rostedt (VMware)03329f92018-11-29 21:38:42 -05002908 size_t nr_pages;
2909 size_t dirty;
2910 size_t full;
Steven Rostedt (Red Hat)d90fd772015-05-29 12:12:27 -04002911
2912 if (buffer->irq_work.waiters_pending) {
2913 buffer->irq_work.waiters_pending = false;
2914 /* irq_work_queue() supplies it's own memory barriers */
2915 irq_work_queue(&buffer->irq_work.work);
2916 }
2917
2918 if (cpu_buffer->irq_work.waiters_pending) {
2919 cpu_buffer->irq_work.waiters_pending = false;
2920 /* irq_work_queue() supplies it's own memory barriers */
2921 irq_work_queue(&cpu_buffer->irq_work.work);
2922 }
2923
Steven Rostedt (VMware)03329f92018-11-29 21:38:42 -05002924 if (cpu_buffer->last_pages_touch == local_read(&cpu_buffer->pages_touched))
2925 return;
Steven Rostedt (Red Hat)d90fd772015-05-29 12:12:27 -04002926
Steven Rostedt (VMware)03329f92018-11-29 21:38:42 -05002927 if (cpu_buffer->reader_page == cpu_buffer->commit_page)
2928 return;
Steven Rostedt (VMware)2c2b0a72018-11-29 20:32:26 -05002929
Steven Rostedt (VMware)03329f92018-11-29 21:38:42 -05002930 if (!cpu_buffer->irq_work.full_waiters_pending)
2931 return;
Steven Rostedt (VMware)2c2b0a72018-11-29 20:32:26 -05002932
Steven Rostedt (VMware)03329f92018-11-29 21:38:42 -05002933 cpu_buffer->last_pages_touch = local_read(&cpu_buffer->pages_touched);
2934
2935 full = cpu_buffer->shortest_full;
2936 nr_pages = cpu_buffer->nr_pages;
2937 dirty = ring_buffer_nr_dirty_pages(buffer, cpu_buffer->cpu);
2938 if (full && nr_pages && (dirty * 100) <= full * nr_pages)
2939 return;
2940
2941 cpu_buffer->irq_work.wakeup_full = true;
2942 cpu_buffer->irq_work.full_waiters_pending = false;
2943 /* irq_work_queue() supplies it's own memory barriers */
2944 irq_work_queue(&cpu_buffer->irq_work.work);
Steven Rostedt (Red Hat)d90fd772015-05-29 12:12:27 -04002945}
2946
2947/*
2948 * The lock and unlock are done within a preempt disable section.
2949 * The current_context per_cpu variable can only be modified
2950 * by the current task between lock and unlock. But it can
Steven Rostedt (VMware)a0e3a182018-01-15 10:47:09 -05002951 * be modified more than once via an interrupt. To pass this
2952 * information from the lock to the unlock without having to
2953 * access the 'in_interrupt()' functions again (which do show
2954 * a bit of overhead in something as critical as function tracing,
2955 * we use a bitmask trick.
Steven Rostedt (Red Hat)d90fd772015-05-29 12:12:27 -04002956 *
Steven Rostedt (VMware)a0e3a182018-01-15 10:47:09 -05002957 * bit 0 = NMI context
2958 * bit 1 = IRQ context
2959 * bit 2 = SoftIRQ context
2960 * bit 3 = normal context.
Steven Rostedt (Red Hat)d90fd772015-05-29 12:12:27 -04002961 *
Steven Rostedt (VMware)a0e3a182018-01-15 10:47:09 -05002962 * This works because this is the order of contexts that can
2963 * preempt other contexts. A SoftIRQ never preempts an IRQ
2964 * context.
2965 *
2966 * When the context is determined, the corresponding bit is
2967 * checked and set (if it was set, then a recursion of that context
2968 * happened).
2969 *
2970 * On unlock, we need to clear this bit. To do so, just subtract
2971 * 1 from the current_context and AND it to itself.
2972 *
2973 * (binary)
2974 * 101 - 1 = 100
2975 * 101 & 100 = 100 (clearing bit zero)
2976 *
2977 * 1010 - 1 = 1001
2978 * 1010 & 1001 = 1000 (clearing bit 1)
2979 *
2980 * The least significant bit can be cleared this way, and it
2981 * just so happens that it is the same bit corresponding to
2982 * the current context.
Steven Rostedt (Red Hat)d90fd772015-05-29 12:12:27 -04002983 */
2984
2985static __always_inline int
2986trace_recursive_lock(struct ring_buffer_per_cpu *cpu_buffer)
2987{
Steven Rostedt (VMware)a0e3a182018-01-15 10:47:09 -05002988 unsigned int val = cpu_buffer->current_context;
2989 unsigned long pc = preempt_count();
2990 int bit;
2991
2992 if (!(pc & (NMI_MASK | HARDIRQ_MASK | SOFTIRQ_OFFSET)))
2993 bit = RB_CTX_NORMAL;
2994 else
2995 bit = pc & NMI_MASK ? RB_CTX_NMI :
Steven Rostedt (VMware)0164e0d2018-01-18 15:42:09 -05002996 pc & HARDIRQ_MASK ? RB_CTX_IRQ : RB_CTX_SOFTIRQ;
Steven Rostedt (VMware)a0e3a182018-01-15 10:47:09 -05002997
Steven Rostedt (VMware)8e012062018-02-07 17:26:32 -05002998 if (unlikely(val & (1 << (bit + cpu_buffer->nest))))
Steven Rostedt (Red Hat)d90fd772015-05-29 12:12:27 -04002999 return 1;
3000
Steven Rostedt (VMware)8e012062018-02-07 17:26:32 -05003001 val |= (1 << (bit + cpu_buffer->nest));
Steven Rostedt (VMware)a0e3a182018-01-15 10:47:09 -05003002 cpu_buffer->current_context = val;
Steven Rostedt (Red Hat)d90fd772015-05-29 12:12:27 -04003003
3004 return 0;
3005}
3006
3007static __always_inline void
3008trace_recursive_unlock(struct ring_buffer_per_cpu *cpu_buffer)
3009{
Steven Rostedt (VMware)8e012062018-02-07 17:26:32 -05003010 cpu_buffer->current_context &=
3011 cpu_buffer->current_context - (1 << cpu_buffer->nest);
3012}
3013
3014/* The recursive locking above uses 4 bits */
3015#define NESTED_BITS 4
3016
3017/**
3018 * ring_buffer_nest_start - Allow to trace while nested
3019 * @buffer: The ring buffer to modify
3020 *
Steven Rostedt (VMware)6167c202018-05-16 11:17:06 -04003021 * The ring buffer has a safety mechanism to prevent recursion.
Steven Rostedt (VMware)8e012062018-02-07 17:26:32 -05003022 * But there may be a case where a trace needs to be done while
3023 * tracing something else. In this case, calling this function
3024 * will allow this function to nest within a currently active
3025 * ring_buffer_lock_reserve().
3026 *
3027 * Call this function before calling another ring_buffer_lock_reserve() and
3028 * call ring_buffer_nest_end() after the nested ring_buffer_unlock_commit().
3029 */
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05003030void ring_buffer_nest_start(struct trace_buffer *buffer)
Steven Rostedt (VMware)8e012062018-02-07 17:26:32 -05003031{
3032 struct ring_buffer_per_cpu *cpu_buffer;
3033 int cpu;
3034
3035 /* Enabled by ring_buffer_nest_end() */
3036 preempt_disable_notrace();
3037 cpu = raw_smp_processor_id();
3038 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt (VMware)6167c202018-05-16 11:17:06 -04003039 /* This is the shift value for the above recursive locking */
Steven Rostedt (VMware)8e012062018-02-07 17:26:32 -05003040 cpu_buffer->nest += NESTED_BITS;
3041}
3042
3043/**
3044 * ring_buffer_nest_end - Allow to trace while nested
3045 * @buffer: The ring buffer to modify
3046 *
3047 * Must be called after ring_buffer_nest_start() and after the
3048 * ring_buffer_unlock_commit().
3049 */
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05003050void ring_buffer_nest_end(struct trace_buffer *buffer)
Steven Rostedt (VMware)8e012062018-02-07 17:26:32 -05003051{
3052 struct ring_buffer_per_cpu *cpu_buffer;
3053 int cpu;
3054
3055 /* disabled by ring_buffer_nest_start() */
3056 cpu = raw_smp_processor_id();
3057 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt (VMware)6167c202018-05-16 11:17:06 -04003058 /* This is the shift value for the above recursive locking */
Steven Rostedt (VMware)8e012062018-02-07 17:26:32 -05003059 cpu_buffer->nest -= NESTED_BITS;
3060 preempt_enable_notrace();
Steven Rostedt (Red Hat)d90fd772015-05-29 12:12:27 -04003061}
3062
3063/**
3064 * ring_buffer_unlock_commit - commit a reserved
3065 * @buffer: The buffer to commit to
3066 * @event: The event pointer to commit.
3067 *
3068 * This commits the data to the ring buffer, and releases any locks held.
3069 *
3070 * Must be paired with ring_buffer_lock_reserve.
3071 */
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05003072int ring_buffer_unlock_commit(struct trace_buffer *buffer,
Steven Rostedt (Red Hat)d90fd772015-05-29 12:12:27 -04003073 struct ring_buffer_event *event)
3074{
3075 struct ring_buffer_per_cpu *cpu_buffer;
3076 int cpu = raw_smp_processor_id();
3077
3078 cpu_buffer = buffer->buffers[cpu];
3079
3080 rb_commit(cpu_buffer, event);
3081
3082 rb_wakeups(buffer, cpu_buffer);
3083
3084 trace_recursive_unlock(cpu_buffer);
3085
3086 preempt_enable_notrace();
3087
3088 return 0;
3089}
3090EXPORT_SYMBOL_GPL(ring_buffer_unlock_commit);
Steven Rostedt (Red Hat)a4543a22015-05-29 09:40:18 -04003091
Steven Rostedt (Red Hat)9826b272015-05-28 17:36:45 -04003092static noinline void
Steven Rostedt (VMware)7c4b4a52020-06-28 22:52:26 -04003093rb_check_timestamp(struct ring_buffer_per_cpu *cpu_buffer,
3094 struct rb_event_info *info)
Steven Rostedt (Red Hat)9826b272015-05-28 17:36:45 -04003095{
Steven Rostedt (VMware)10464b42020-06-28 22:52:27 -04003096 u64 write_stamp;
3097
Steven Rostedt (Red Hat)9826b272015-05-28 17:36:45 -04003098 WARN_ONCE(info->delta > (1ULL << 59),
3099 KERN_WARNING "Delta way too big! %llu ts=%llu write stamp = %llu\n%s",
3100 (unsigned long long)info->delta,
3101 (unsigned long long)info->ts,
Steven Rostedt (VMware)10464b42020-06-28 22:52:27 -04003102 (unsigned long long)(rb_time_read(&cpu_buffer->write_stamp, &write_stamp) ? write_stamp : 0),
Steven Rostedt (Red Hat)9826b272015-05-28 17:36:45 -04003103 sched_clock_stable() ? "" :
3104 "If you just came from a suspend/resume,\n"
3105 "please switch to the trace global clock:\n"
Chris Wilson913ea4d2018-03-30 16:01:32 +01003106 " echo global > /sys/kernel/debug/tracing/trace_clock\n"
3107 "or add trace_clock=global to the kernel command line\n");
Steven Rostedt (Red Hat)9826b272015-05-28 17:36:45 -04003108}
3109
Steven Rostedt6634ff22009-05-06 15:30:07 -04003110static struct ring_buffer_event *
3111__rb_reserve_next(struct ring_buffer_per_cpu *cpu_buffer,
Steven Rostedt (Red Hat)fcc742e2015-05-28 17:13:14 -04003112 struct rb_event_info *info)
Steven Rostedt6634ff22009-05-06 15:30:07 -04003113{
Steven Rostedt6634ff22009-05-06 15:30:07 -04003114 struct ring_buffer_event *event;
Steven Rostedt (Red Hat)fcc742e2015-05-28 17:13:14 -04003115 struct buffer_page *tail_page;
Steven Rostedt (VMware)a389d862020-06-28 22:52:25 -04003116 unsigned long tail, write, w;
3117 u64 before, after;
Steven Rostedt (VMware)10464b42020-06-28 22:52:27 -04003118 bool a_ok;
3119 bool b_ok;
Steven Rostedt (Red Hat)b7dc42f2015-09-03 08:57:12 -04003120
Steven Rostedt (VMware)a389d862020-06-28 22:52:25 -04003121 /* Don't let the compiler play games with cpu_buffer->tail_page */
3122 tail_page = info->tail_page = READ_ONCE(cpu_buffer->tail_page);
3123
3124 /*A*/ w = local_read(&tail_page->write) & RB_WRITE_MASK;
3125 barrier();
Steven Rostedt (VMware)10464b42020-06-28 22:52:27 -04003126 b_ok = rb_time_read(&cpu_buffer->before_stamp, &before);
3127 a_ok = rb_time_read(&cpu_buffer->write_stamp, &after);
Steven Rostedt (VMware)a389d862020-06-28 22:52:25 -04003128 barrier();
3129 info->ts = rb_time_stamp(cpu_buffer->buffer);
3130
3131 if (ring_buffer_time_stamp_abs(cpu_buffer->buffer)) {
3132 info->delta = info->ts;
Steven Rostedt (VMware)7c4b4a52020-06-28 22:52:26 -04003133 info->add_timestamp = RB_ADD_STAMP_ABSOLUTE;
Steven Rostedt (VMware)a389d862020-06-28 22:52:25 -04003134 } else {
3135 info->delta = info->ts - after;
3136 }
3137
Steven Rostedt (VMware)10464b42020-06-28 22:52:27 -04003138 if (likely(a_ok && b_ok)) {
3139 if (unlikely(test_time_stamp(info->delta))) {
3140 rb_check_timestamp(cpu_buffer, info);
3141 info->add_timestamp |= RB_ADD_STAMP_EXTEND;
3142 }
Steven Rostedt (VMware)7c4b4a52020-06-28 22:52:26 -04003143 }
Steven Rostedt (VMware)a389d862020-06-28 22:52:25 -04003144
3145 /*
3146 * If interrupting an event time update, we may need an absolute timestamp.
3147 * Don't bother if this is the start of a new page (w == 0).
3148 */
Steven Rostedt (VMware)10464b42020-06-28 22:52:27 -04003149 if (unlikely(!a_ok || !b_ok || (before != after && w)))
Steven Rostedt (VMware)7c4b4a52020-06-28 22:52:26 -04003150 info->add_timestamp |= RB_ADD_STAMP_FORCE | RB_ADD_STAMP_EXTEND;
3151
Steven Rostedt (Red Hat)b7dc42f2015-09-03 08:57:12 -04003152 /*
3153 * If the time delta since the last event is too big to
3154 * hold in the time field of the event, then we append a
3155 * TIME EXTEND event ahead of the data event.
3156 */
3157 if (unlikely(info->add_timestamp))
3158 info->length += RB_LEN_TIME_EXTEND;
Steven Rostedt69d1b832010-10-07 18:18:05 -04003159
Steven Rostedt (VMware)10464b42020-06-28 22:52:27 -04003160 /*B*/ rb_time_set(&cpu_buffer->before_stamp, info->ts);
Steven Rostedt (VMware)a389d862020-06-28 22:52:25 -04003161
3162 /*C*/ write = local_add_return(info->length, &tail_page->write);
Steven Rostedt77ae3652009-03-27 11:00:29 -04003163
3164 /* set write to only the index of the write */
3165 write &= RB_WRITE_MASK;
Steven Rostedt (VMware)a389d862020-06-28 22:52:25 -04003166
Steven Rostedt (Red Hat)fcc742e2015-05-28 17:13:14 -04003167 tail = write - info->length;
Steven Rostedt6634ff22009-05-06 15:30:07 -04003168
Steven Rostedt (VMware)a389d862020-06-28 22:52:25 -04003169 /* See if we shot pass the end of this buffer page */
3170 if (unlikely(write > BUF_PAGE_SIZE)) {
3171 if (tail != w) {
3172 /* before and after may now different, fix it up*/
Steven Rostedt (VMware)10464b42020-06-28 22:52:27 -04003173 b_ok = rb_time_read(&cpu_buffer->before_stamp, &before);
3174 a_ok = rb_time_read(&cpu_buffer->write_stamp, &after);
3175 if (a_ok && b_ok && before != after)
3176 (void)rb_time_cmpxchg(&cpu_buffer->before_stamp, before, after);
Steven Rostedt (VMware)a389d862020-06-28 22:52:25 -04003177 }
3178 return rb_move_tail(cpu_buffer, tail, info);
3179 }
3180
3181 if (likely(tail == w)) {
3182 u64 save_before;
Steven Rostedt (VMware)10464b42020-06-28 22:52:27 -04003183 bool s_ok;
Steven Rostedt (VMware)a389d862020-06-28 22:52:25 -04003184
3185 /* Nothing interrupted us between A and C */
Steven Rostedt (VMware)10464b42020-06-28 22:52:27 -04003186 /*D*/ rb_time_set(&cpu_buffer->write_stamp, info->ts);
Steven Rostedt (VMware)a389d862020-06-28 22:52:25 -04003187 barrier();
Steven Rostedt (VMware)10464b42020-06-28 22:52:27 -04003188 /*E*/ s_ok = rb_time_read(&cpu_buffer->before_stamp, &save_before);
3189 RB_WARN_ON(cpu_buffer, !s_ok);
Steven Rostedt (VMware)7c4b4a52020-06-28 22:52:26 -04003190 if (likely(!(info->add_timestamp &
3191 (RB_ADD_STAMP_FORCE | RB_ADD_STAMP_ABSOLUTE))))
Steven Rostedt (VMware)a389d862020-06-28 22:52:25 -04003192 /* This did not interrupt any time update */
3193 info->delta = info->ts - after;
3194 else
3195 /* Just use full timestamp for inerrupting event */
3196 info->delta = info->ts;
3197 barrier();
3198 if (unlikely(info->ts != save_before)) {
3199 /* SLOW PATH - Interrupted between C and E */
3200
Steven Rostedt (VMware)10464b42020-06-28 22:52:27 -04003201 a_ok = rb_time_read(&cpu_buffer->write_stamp, &after);
3202 RB_WARN_ON(cpu_buffer, !a_ok);
3203
Steven Rostedt (VMware)a389d862020-06-28 22:52:25 -04003204 /* Write stamp must only go forward */
3205 if (save_before > after) {
3206 /*
3207 * We do not care about the result, only that
3208 * it gets updated atomically.
3209 */
Steven Rostedt (VMware)10464b42020-06-28 22:52:27 -04003210 (void)rb_time_cmpxchg(&cpu_buffer->write_stamp, after, save_before);
Steven Rostedt (VMware)a389d862020-06-28 22:52:25 -04003211 }
3212 }
3213 } else {
3214 u64 ts;
3215 /* SLOW PATH - Interrupted between A and C */
Steven Rostedt (VMware)10464b42020-06-28 22:52:27 -04003216 a_ok = rb_time_read(&cpu_buffer->write_stamp, &after);
3217 /* Was interrupted before here, write_stamp must be valid */
3218 RB_WARN_ON(cpu_buffer, !a_ok);
Steven Rostedt (VMware)a389d862020-06-28 22:52:25 -04003219 ts = rb_time_stamp(cpu_buffer->buffer);
3220 barrier();
3221 /*E*/ if (write == (local_read(&tail_page->write) & RB_WRITE_MASK) &&
3222 after < ts) {
3223 /* Nothing came after this event between C and E */
3224 info->delta = ts - after;
Steven Rostedt (VMware)10464b42020-06-28 22:52:27 -04003225 (void)rb_time_cmpxchg(&cpu_buffer->write_stamp, after, info->ts);
Steven Rostedt (VMware)a389d862020-06-28 22:52:25 -04003226 info->ts = ts;
3227 } else {
3228 /*
3229 * Interrupted beween C and E:
3230 * Lost the previous events time stamp. Just set the
3231 * delta to zero, and this will be the same time as
3232 * the event this event interrupted. And the events that
3233 * came after this will still be correct (as they would
3234 * have built their delta on the previous event.
3235 */
3236 info->delta = 0;
3237 }
Steven Rostedt (VMware)7c4b4a52020-06-28 22:52:26 -04003238 info->add_timestamp &= ~RB_ADD_STAMP_FORCE;
Steven Rostedt (VMware)a389d862020-06-28 22:52:25 -04003239 }
3240
Steven Rostedt (Red Hat)b7dc42f2015-09-03 08:57:12 -04003241 /*
3242 * If this is the first commit on the page, then it has the same
3243 * timestamp as the page itself.
3244 */
Steven Rostedt (VMware)7c4b4a52020-06-28 22:52:26 -04003245 if (unlikely(!tail && !(info->add_timestamp &
3246 (RB_ADD_STAMP_FORCE | RB_ADD_STAMP_ABSOLUTE))))
Steven Rostedt (Red Hat)b7dc42f2015-09-03 08:57:12 -04003247 info->delta = 0;
3248
Steven Rostedt6634ff22009-05-06 15:30:07 -04003249 /* We reserved something on the buffer */
Steven Rostedt (Red Hat)b7dc42f2015-09-03 08:57:12 -04003250
Steven Rostedt6634ff22009-05-06 15:30:07 -04003251 event = __rb_page_index(tail_page, tail);
Steven Rostedt (Red Hat)fcc742e2015-05-28 17:13:14 -04003252 rb_update_event(cpu_buffer, event, info);
Steven Rostedt6634ff22009-05-06 15:30:07 -04003253
Steven Rostedt69d1b832010-10-07 18:18:05 -04003254 local_inc(&tail_page->entries);
Steven Rostedt6634ff22009-05-06 15:30:07 -04003255
Steven Rostedt (Red Hat)b7dc42f2015-09-03 08:57:12 -04003256 /*
3257 * If this is the first commit on the page, then update
3258 * its timestamp.
3259 */
3260 if (!tail)
3261 tail_page->page->time_stamp = info->ts;
3262
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -07003263 /* account for these added bytes */
Steven Rostedt (Red Hat)fcc742e2015-05-28 17:13:14 -04003264 local_add(info->length, &cpu_buffer->entries_bytes);
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -07003265
Steven Rostedt6634ff22009-05-06 15:30:07 -04003266 return event;
3267}
3268
Steven Rostedt (Red Hat)fa7ffb32016-11-23 11:36:30 -05003269static __always_inline struct ring_buffer_event *
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05003270rb_reserve_next_event(struct trace_buffer *buffer,
Steven Rostedt62f0b3e2009-09-04 14:11:34 -04003271 struct ring_buffer_per_cpu *cpu_buffer,
Steven Rostedt1cd8d732009-05-11 14:08:09 -04003272 unsigned long length)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003273{
3274 struct ring_buffer_event *event;
Steven Rostedt (Red Hat)fcc742e2015-05-28 17:13:14 -04003275 struct rb_event_info info;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003276 int nr_loops = 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003277
Steven Rostedtfa743952009-06-16 12:37:57 -04003278 rb_start_commit(cpu_buffer);
Steven Rostedt (VMware)a389d862020-06-28 22:52:25 -04003279 /* The commit page can not change after this */
Steven Rostedtfa743952009-06-16 12:37:57 -04003280
Steven Rostedt85bac322009-09-04 14:24:40 -04003281#ifdef CONFIG_RING_BUFFER_ALLOW_SWAP
Steven Rostedt62f0b3e2009-09-04 14:11:34 -04003282 /*
3283 * Due to the ability to swap a cpu buffer from a buffer
3284 * it is possible it was swapped before we committed.
3285 * (committing stops a swap). We check for it here and
3286 * if it happened, we have to fail the write.
3287 */
3288 barrier();
Mark Rutland6aa7de02017-10-23 14:07:29 -07003289 if (unlikely(READ_ONCE(cpu_buffer->buffer) != buffer)) {
Steven Rostedt62f0b3e2009-09-04 14:11:34 -04003290 local_dec(&cpu_buffer->committing);
3291 local_dec(&cpu_buffer->commits);
3292 return NULL;
3293 }
Steven Rostedt85bac322009-09-04 14:24:40 -04003294#endif
Steven Rostedt (Red Hat)b7dc42f2015-09-03 08:57:12 -04003295
Steven Rostedt (Red Hat)fcc742e2015-05-28 17:13:14 -04003296 info.length = rb_calculate_event_length(length);
Steven Rostedt (Red Hat)a4543a22015-05-29 09:40:18 -04003297 again:
Steven Rostedt (VMware)a389d862020-06-28 22:52:25 -04003298 info.add_timestamp = RB_ADD_STAMP_NONE;
Steven Rostedt (Red Hat)b7dc42f2015-09-03 08:57:12 -04003299 info.delta = 0;
3300
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003301 /*
3302 * We allow for interrupts to reenter here and do a trace.
3303 * If one does, it will cause this original code to loop
3304 * back here. Even with heavy interrupts happening, this
3305 * should only happen a few times in a row. If this happens
3306 * 1000 times in a row, there must be either an interrupt
3307 * storm or we have something buggy.
3308 * Bail!
3309 */
Steven Rostedt3e89c7bb2008-11-11 15:28:41 -05003310 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 1000))
Steven Rostedtfa743952009-06-16 12:37:57 -04003311 goto out_fail;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003312
Steven Rostedt (Red Hat)fcc742e2015-05-28 17:13:14 -04003313 event = __rb_reserve_next(cpu_buffer, &info);
3314
Steven Rostedt (Red Hat)bd1b7cd2015-11-23 17:35:24 -05003315 if (unlikely(PTR_ERR(event) == -EAGAIN)) {
3316 if (info.add_timestamp)
3317 info.length -= RB_LEN_TIME_EXTEND;
Steven Rostedtbf41a152008-10-04 02:00:59 -04003318 goto again;
Steven Rostedt (Red Hat)bd1b7cd2015-11-23 17:35:24 -05003319 }
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003320
Steven Rostedt (VMware)a389d862020-06-28 22:52:25 -04003321 if (likely(event))
3322 return event;
Steven Rostedtfa743952009-06-16 12:37:57 -04003323 out_fail:
3324 rb_end_commit(cpu_buffer);
3325 return NULL;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003326}
3327
3328/**
3329 * ring_buffer_lock_reserve - reserve a part of the buffer
3330 * @buffer: the ring buffer to reserve from
3331 * @length: the length of the data to reserve (excluding event header)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003332 *
Steven Rostedt (VMware)6167c202018-05-16 11:17:06 -04003333 * Returns a reserved event on the ring buffer to copy directly to.
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003334 * The user of this interface will need to get the body to write into
3335 * and can use the ring_buffer_event_data() interface.
3336 *
3337 * The length is the length of the data needed, not the event length
3338 * which also includes the event header.
3339 *
3340 * Must be paired with ring_buffer_unlock_commit, unless NULL is returned.
3341 * If NULL is returned, then nothing has been allocated or locked.
3342 */
3343struct ring_buffer_event *
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05003344ring_buffer_lock_reserve(struct trace_buffer *buffer, unsigned long length)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003345{
3346 struct ring_buffer_per_cpu *cpu_buffer;
3347 struct ring_buffer_event *event;
Steven Rostedt5168ae52010-06-03 09:36:50 -04003348 int cpu;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003349
Steven Rostedtbf41a152008-10-04 02:00:59 -04003350 /* If we are tracing schedule, we don't want to recurse */
Steven Rostedt5168ae52010-06-03 09:36:50 -04003351 preempt_disable_notrace();
Steven Rostedtbf41a152008-10-04 02:00:59 -04003352
Steven Rostedt (Red Hat)3205f802015-05-21 17:39:29 -04003353 if (unlikely(atomic_read(&buffer->record_disabled)))
Steven Rostedt (Red Hat)58a09ec2015-05-27 10:27:47 -04003354 goto out;
Steven Rostedt261842b2009-04-16 21:41:52 -04003355
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003356 cpu = raw_smp_processor_id();
3357
Steven Rostedt (Red Hat)3205f802015-05-21 17:39:29 -04003358 if (unlikely(!cpumask_test_cpu(cpu, buffer->cpumask)))
Steven Rostedtd7690412008-10-01 00:29:53 -04003359 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003360
3361 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003362
Steven Rostedt (Red Hat)3205f802015-05-21 17:39:29 -04003363 if (unlikely(atomic_read(&cpu_buffer->record_disabled)))
Steven Rostedtd7690412008-10-01 00:29:53 -04003364 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003365
Steven Rostedt (Red Hat)3205f802015-05-21 17:39:29 -04003366 if (unlikely(length > BUF_MAX_DATA_SIZE))
Steven Rostedtbf41a152008-10-04 02:00:59 -04003367 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003368
Steven Rostedt (Red Hat)58a09ec2015-05-27 10:27:47 -04003369 if (unlikely(trace_recursive_lock(cpu_buffer)))
3370 goto out;
3371
Steven Rostedt62f0b3e2009-09-04 14:11:34 -04003372 event = rb_reserve_next_event(buffer, cpu_buffer, length);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003373 if (!event)
Steven Rostedt (Red Hat)58a09ec2015-05-27 10:27:47 -04003374 goto out_unlock;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003375
3376 return event;
3377
Steven Rostedt (Red Hat)58a09ec2015-05-27 10:27:47 -04003378 out_unlock:
3379 trace_recursive_unlock(cpu_buffer);
Steven Rostedtd7690412008-10-01 00:29:53 -04003380 out:
Steven Rostedt5168ae52010-06-03 09:36:50 -04003381 preempt_enable_notrace();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003382 return NULL;
3383}
Robert Richterc4f50182008-12-11 16:49:22 +01003384EXPORT_SYMBOL_GPL(ring_buffer_lock_reserve);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003385
Steven Rostedta1863c22009-09-03 10:23:58 -04003386/*
3387 * Decrement the entries to the page that an event is on.
3388 * The event does not even need to exist, only the pointer
3389 * to the page it is on. This may only be called before the commit
3390 * takes place.
3391 */
3392static inline void
3393rb_decrement_entry(struct ring_buffer_per_cpu *cpu_buffer,
3394 struct ring_buffer_event *event)
3395{
3396 unsigned long addr = (unsigned long)event;
3397 struct buffer_page *bpage = cpu_buffer->commit_page;
3398 struct buffer_page *start;
3399
3400 addr &= PAGE_MASK;
3401
3402 /* Do the likely case first */
3403 if (likely(bpage->page == (void *)addr)) {
3404 local_dec(&bpage->entries);
3405 return;
3406 }
3407
3408 /*
3409 * Because the commit page may be on the reader page we
3410 * start with the next page and check the end loop there.
3411 */
3412 rb_inc_page(cpu_buffer, &bpage);
3413 start = bpage;
3414 do {
3415 if (bpage->page == (void *)addr) {
3416 local_dec(&bpage->entries);
3417 return;
3418 }
3419 rb_inc_page(cpu_buffer, &bpage);
3420 } while (bpage != start);
3421
3422 /* commit not part of this buffer?? */
3423 RB_WARN_ON(cpu_buffer, 1);
3424}
3425
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003426/**
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04003427 * ring_buffer_commit_discard - discard an event that has not been committed
3428 * @buffer: the ring buffer
3429 * @event: non committed event to discard
3430 *
Steven Rostedtdc892f72009-09-03 15:33:41 -04003431 * Sometimes an event that is in the ring buffer needs to be ignored.
3432 * This function lets the user discard an event in the ring buffer
3433 * and then that event will not be read later.
3434 *
Steven Rostedt (VMware)6167c202018-05-16 11:17:06 -04003435 * This function only works if it is called before the item has been
Steven Rostedtdc892f72009-09-03 15:33:41 -04003436 * committed. It will try to free the event from the ring buffer
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04003437 * if another event has not been added behind it.
3438 *
3439 * If another event has been added behind it, it will set the event
3440 * up as discarded, and perform the commit.
3441 *
3442 * If this function is called, do not call ring_buffer_unlock_commit on
3443 * the event.
3444 */
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05003445void ring_buffer_discard_commit(struct trace_buffer *buffer,
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04003446 struct ring_buffer_event *event)
3447{
3448 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04003449 int cpu;
3450
3451 /* The event is discarded regardless */
Frederic Weisbeckerf3b9aae2009-04-19 23:39:33 +02003452 rb_event_discard(event);
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04003453
Steven Rostedtfa743952009-06-16 12:37:57 -04003454 cpu = smp_processor_id();
3455 cpu_buffer = buffer->buffers[cpu];
3456
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04003457 /*
3458 * This must only be called if the event has not been
3459 * committed yet. Thus we can assume that preemption
3460 * is still disabled.
3461 */
Steven Rostedtfa743952009-06-16 12:37:57 -04003462 RB_WARN_ON(buffer, !local_read(&cpu_buffer->committing));
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04003463
Steven Rostedta1863c22009-09-03 10:23:58 -04003464 rb_decrement_entry(cpu_buffer, event);
Steven Rostedt0f2541d2009-08-05 12:02:48 -04003465 if (rb_try_to_discard(cpu_buffer, event))
Steven Rostedtedd813bf2009-06-02 23:00:53 -04003466 goto out;
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04003467
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04003468 out:
Steven Rostedtfa743952009-06-16 12:37:57 -04003469 rb_end_commit(cpu_buffer);
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04003470
Steven Rostedt (Red Hat)58a09ec2015-05-27 10:27:47 -04003471 trace_recursive_unlock(cpu_buffer);
Frederic Weisbeckerf3b9aae2009-04-19 23:39:33 +02003472
Steven Rostedt5168ae52010-06-03 09:36:50 -04003473 preempt_enable_notrace();
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04003474
3475}
3476EXPORT_SYMBOL_GPL(ring_buffer_discard_commit);
3477
3478/**
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003479 * ring_buffer_write - write data to the buffer without reserving
3480 * @buffer: The ring buffer to write to.
3481 * @length: The length of the data being written (excluding the event header)
3482 * @data: The data to write to the buffer.
3483 *
3484 * This is like ring_buffer_lock_reserve and ring_buffer_unlock_commit as
3485 * one function. If you already have the data to write to the buffer, it
3486 * may be easier to simply call this function.
3487 *
3488 * Note, like ring_buffer_lock_reserve, the length is the length of the data
3489 * and not the length of the event which would hold the header.
3490 */
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05003491int ring_buffer_write(struct trace_buffer *buffer,
David Sharp01e3e712012-06-07 16:46:24 -07003492 unsigned long length,
3493 void *data)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003494{
3495 struct ring_buffer_per_cpu *cpu_buffer;
3496 struct ring_buffer_event *event;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003497 void *body;
3498 int ret = -EBUSY;
Steven Rostedt5168ae52010-06-03 09:36:50 -04003499 int cpu;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003500
Steven Rostedt5168ae52010-06-03 09:36:50 -04003501 preempt_disable_notrace();
Steven Rostedtbf41a152008-10-04 02:00:59 -04003502
Lai Jiangshan52fbe9c2010-03-08 14:50:43 +08003503 if (atomic_read(&buffer->record_disabled))
3504 goto out;
3505
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003506 cpu = raw_smp_processor_id();
3507
Rusty Russell9e01c1b2009-01-01 10:12:22 +10303508 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedtd7690412008-10-01 00:29:53 -04003509 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003510
3511 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003512
3513 if (atomic_read(&cpu_buffer->record_disabled))
3514 goto out;
3515
Steven Rostedtbe957c42009-05-11 14:42:53 -04003516 if (length > BUF_MAX_DATA_SIZE)
3517 goto out;
3518
Steven Rostedt (Red Hat)985e8712015-05-27 10:48:56 -04003519 if (unlikely(trace_recursive_lock(cpu_buffer)))
3520 goto out;
3521
Steven Rostedt62f0b3e2009-09-04 14:11:34 -04003522 event = rb_reserve_next_event(buffer, cpu_buffer, length);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003523 if (!event)
Steven Rostedt (Red Hat)985e8712015-05-27 10:48:56 -04003524 goto out_unlock;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003525
3526 body = rb_event_data(event);
3527
3528 memcpy(body, data, length);
3529
3530 rb_commit(cpu_buffer, event);
3531
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -05003532 rb_wakeups(buffer, cpu_buffer);
3533
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003534 ret = 0;
Steven Rostedt (Red Hat)985e8712015-05-27 10:48:56 -04003535
3536 out_unlock:
3537 trace_recursive_unlock(cpu_buffer);
3538
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003539 out:
Steven Rostedt5168ae52010-06-03 09:36:50 -04003540 preempt_enable_notrace();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003541
3542 return ret;
3543}
Robert Richterc4f50182008-12-11 16:49:22 +01003544EXPORT_SYMBOL_GPL(ring_buffer_write);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003545
Yaowei Baida588342015-09-29 22:43:33 +08003546static bool rb_per_cpu_empty(struct ring_buffer_per_cpu *cpu_buffer)
Steven Rostedtbf41a152008-10-04 02:00:59 -04003547{
3548 struct buffer_page *reader = cpu_buffer->reader_page;
Steven Rostedt77ae3652009-03-27 11:00:29 -04003549 struct buffer_page *head = rb_set_head_page(cpu_buffer);
Steven Rostedtbf41a152008-10-04 02:00:59 -04003550 struct buffer_page *commit = cpu_buffer->commit_page;
3551
Steven Rostedt77ae3652009-03-27 11:00:29 -04003552 /* In case of error, head will be NULL */
3553 if (unlikely(!head))
Yaowei Baida588342015-09-29 22:43:33 +08003554 return true;
Steven Rostedt77ae3652009-03-27 11:00:29 -04003555
Steven Rostedtbf41a152008-10-04 02:00:59 -04003556 return reader->read == rb_page_commit(reader) &&
3557 (commit == reader ||
3558 (commit == head &&
3559 head->read == rb_page_commit(commit)));
3560}
3561
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003562/**
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003563 * ring_buffer_record_disable - stop all writes into the buffer
3564 * @buffer: The ring buffer to stop writes to.
3565 *
3566 * This prevents all writes to the buffer. Any attempt to write
3567 * to the buffer after this will fail and return NULL.
3568 *
Paul E. McKenney74401722018-11-06 18:44:52 -08003569 * The caller should call synchronize_rcu() after this.
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003570 */
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05003571void ring_buffer_record_disable(struct trace_buffer *buffer)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003572{
3573 atomic_inc(&buffer->record_disabled);
3574}
Robert Richterc4f50182008-12-11 16:49:22 +01003575EXPORT_SYMBOL_GPL(ring_buffer_record_disable);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003576
3577/**
3578 * ring_buffer_record_enable - enable writes to the buffer
3579 * @buffer: The ring buffer to enable writes
3580 *
3581 * Note, multiple disables will need the same number of enables
Adam Buchbinderc41b20e2009-12-11 16:35:39 -05003582 * to truly enable the writing (much like preempt_disable).
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003583 */
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05003584void ring_buffer_record_enable(struct trace_buffer *buffer)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003585{
3586 atomic_dec(&buffer->record_disabled);
3587}
Robert Richterc4f50182008-12-11 16:49:22 +01003588EXPORT_SYMBOL_GPL(ring_buffer_record_enable);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003589
3590/**
Steven Rostedt499e5472012-02-22 15:50:28 -05003591 * ring_buffer_record_off - stop all writes into the buffer
3592 * @buffer: The ring buffer to stop writes to.
3593 *
3594 * This prevents all writes to the buffer. Any attempt to write
3595 * to the buffer after this will fail and return NULL.
3596 *
3597 * This is different than ring_buffer_record_disable() as
Wang Tianhong87abb3b2012-08-02 14:02:00 +08003598 * it works like an on/off switch, where as the disable() version
Steven Rostedt499e5472012-02-22 15:50:28 -05003599 * must be paired with a enable().
3600 */
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05003601void ring_buffer_record_off(struct trace_buffer *buffer)
Steven Rostedt499e5472012-02-22 15:50:28 -05003602{
3603 unsigned int rd;
3604 unsigned int new_rd;
3605
3606 do {
3607 rd = atomic_read(&buffer->record_disabled);
3608 new_rd = rd | RB_BUFFER_OFF;
3609 } while (atomic_cmpxchg(&buffer->record_disabled, rd, new_rd) != rd);
3610}
3611EXPORT_SYMBOL_GPL(ring_buffer_record_off);
3612
3613/**
3614 * ring_buffer_record_on - restart writes into the buffer
3615 * @buffer: The ring buffer to start writes to.
3616 *
3617 * This enables all writes to the buffer that was disabled by
3618 * ring_buffer_record_off().
3619 *
3620 * This is different than ring_buffer_record_enable() as
Wang Tianhong87abb3b2012-08-02 14:02:00 +08003621 * it works like an on/off switch, where as the enable() version
Steven Rostedt499e5472012-02-22 15:50:28 -05003622 * must be paired with a disable().
3623 */
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05003624void ring_buffer_record_on(struct trace_buffer *buffer)
Steven Rostedt499e5472012-02-22 15:50:28 -05003625{
3626 unsigned int rd;
3627 unsigned int new_rd;
3628
3629 do {
3630 rd = atomic_read(&buffer->record_disabled);
3631 new_rd = rd & ~RB_BUFFER_OFF;
3632 } while (atomic_cmpxchg(&buffer->record_disabled, rd, new_rd) != rd);
3633}
3634EXPORT_SYMBOL_GPL(ring_buffer_record_on);
3635
3636/**
3637 * ring_buffer_record_is_on - return true if the ring buffer can write
3638 * @buffer: The ring buffer to see if write is enabled
3639 *
3640 * Returns true if the ring buffer is in a state that it accepts writes.
3641 */
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05003642bool ring_buffer_record_is_on(struct trace_buffer *buffer)
Steven Rostedt499e5472012-02-22 15:50:28 -05003643{
3644 return !atomic_read(&buffer->record_disabled);
3645}
3646
3647/**
Masami Hiramatsu73c8d892018-07-14 01:28:15 +09003648 * ring_buffer_record_is_set_on - return true if the ring buffer is set writable
3649 * @buffer: The ring buffer to see if write is set enabled
3650 *
3651 * Returns true if the ring buffer is set writable by ring_buffer_record_on().
3652 * Note that this does NOT mean it is in a writable state.
3653 *
3654 * It may return true when the ring buffer has been disabled by
3655 * ring_buffer_record_disable(), as that is a temporary disabling of
3656 * the ring buffer.
3657 */
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05003658bool ring_buffer_record_is_set_on(struct trace_buffer *buffer)
Masami Hiramatsu73c8d892018-07-14 01:28:15 +09003659{
3660 return !(atomic_read(&buffer->record_disabled) & RB_BUFFER_OFF);
3661}
3662
3663/**
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003664 * ring_buffer_record_disable_cpu - stop all writes into the cpu_buffer
3665 * @buffer: The ring buffer to stop writes to.
3666 * @cpu: The CPU buffer to stop
3667 *
3668 * This prevents all writes to the buffer. Any attempt to write
3669 * to the buffer after this will fail and return NULL.
3670 *
Paul E. McKenney74401722018-11-06 18:44:52 -08003671 * The caller should call synchronize_rcu() after this.
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003672 */
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05003673void ring_buffer_record_disable_cpu(struct trace_buffer *buffer, int cpu)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003674{
3675 struct ring_buffer_per_cpu *cpu_buffer;
3676
Rusty Russell9e01c1b2009-01-01 10:12:22 +10303677 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04003678 return;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003679
3680 cpu_buffer = buffer->buffers[cpu];
3681 atomic_inc(&cpu_buffer->record_disabled);
3682}
Robert Richterc4f50182008-12-11 16:49:22 +01003683EXPORT_SYMBOL_GPL(ring_buffer_record_disable_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003684
3685/**
3686 * ring_buffer_record_enable_cpu - enable writes to the buffer
3687 * @buffer: The ring buffer to enable writes
3688 * @cpu: The CPU to enable.
3689 *
3690 * Note, multiple disables will need the same number of enables
Adam Buchbinderc41b20e2009-12-11 16:35:39 -05003691 * to truly enable the writing (much like preempt_disable).
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003692 */
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05003693void ring_buffer_record_enable_cpu(struct trace_buffer *buffer, int cpu)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003694{
3695 struct ring_buffer_per_cpu *cpu_buffer;
3696
Rusty Russell9e01c1b2009-01-01 10:12:22 +10303697 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04003698 return;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003699
3700 cpu_buffer = buffer->buffers[cpu];
3701 atomic_dec(&cpu_buffer->record_disabled);
3702}
Robert Richterc4f50182008-12-11 16:49:22 +01003703EXPORT_SYMBOL_GPL(ring_buffer_record_enable_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003704
Steven Rostedtf6195aa2010-09-01 12:23:12 -04003705/*
3706 * The total entries in the ring buffer is the running counter
3707 * of entries entered into the ring buffer, minus the sum of
3708 * the entries read from the ring buffer and the number of
3709 * entries that were overwritten.
3710 */
3711static inline unsigned long
3712rb_num_of_entries(struct ring_buffer_per_cpu *cpu_buffer)
3713{
3714 return local_read(&cpu_buffer->entries) -
3715 (local_read(&cpu_buffer->overrun) + cpu_buffer->read);
3716}
3717
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003718/**
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -07003719 * ring_buffer_oldest_event_ts - get the oldest event timestamp from the buffer
3720 * @buffer: The ring buffer
3721 * @cpu: The per CPU buffer to read from.
3722 */
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05003723u64 ring_buffer_oldest_event_ts(struct trace_buffer *buffer, int cpu)
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -07003724{
3725 unsigned long flags;
3726 struct ring_buffer_per_cpu *cpu_buffer;
3727 struct buffer_page *bpage;
Linus Torvaldsda830e52012-12-11 18:18:58 -08003728 u64 ret = 0;
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -07003729
3730 if (!cpumask_test_cpu(cpu, buffer->cpumask))
3731 return 0;
3732
3733 cpu_buffer = buffer->buffers[cpu];
Linus Torvalds7115e3f2011-10-26 17:03:38 +02003734 raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -07003735 /*
3736 * if the tail is on reader_page, oldest time stamp is on the reader
3737 * page
3738 */
3739 if (cpu_buffer->tail_page == cpu_buffer->reader_page)
3740 bpage = cpu_buffer->reader_page;
3741 else
3742 bpage = rb_set_head_page(cpu_buffer);
Steven Rostedt54f7be52012-11-29 22:27:22 -05003743 if (bpage)
3744 ret = bpage->page->time_stamp;
Linus Torvalds7115e3f2011-10-26 17:03:38 +02003745 raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -07003746
3747 return ret;
3748}
3749EXPORT_SYMBOL_GPL(ring_buffer_oldest_event_ts);
3750
3751/**
3752 * ring_buffer_bytes_cpu - get the number of bytes consumed in a cpu buffer
3753 * @buffer: The ring buffer
3754 * @cpu: The per CPU buffer to read from.
3755 */
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05003756unsigned long ring_buffer_bytes_cpu(struct trace_buffer *buffer, int cpu)
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -07003757{
3758 struct ring_buffer_per_cpu *cpu_buffer;
3759 unsigned long ret;
3760
3761 if (!cpumask_test_cpu(cpu, buffer->cpumask))
3762 return 0;
3763
3764 cpu_buffer = buffer->buffers[cpu];
3765 ret = local_read(&cpu_buffer->entries_bytes) - cpu_buffer->read_bytes;
3766
3767 return ret;
3768}
3769EXPORT_SYMBOL_GPL(ring_buffer_bytes_cpu);
3770
3771/**
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003772 * ring_buffer_entries_cpu - get the number of entries in a cpu buffer
3773 * @buffer: The ring buffer
3774 * @cpu: The per CPU buffer to get the entries from.
3775 */
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05003776unsigned long ring_buffer_entries_cpu(struct trace_buffer *buffer, int cpu)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003777{
3778 struct ring_buffer_per_cpu *cpu_buffer;
3779
Rusty Russell9e01c1b2009-01-01 10:12:22 +10303780 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04003781 return 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003782
3783 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt554f7862009-03-11 22:00:13 -04003784
Steven Rostedtf6195aa2010-09-01 12:23:12 -04003785 return rb_num_of_entries(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003786}
Robert Richterc4f50182008-12-11 16:49:22 +01003787EXPORT_SYMBOL_GPL(ring_buffer_entries_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003788
3789/**
Slava Pestov884bfe82011-07-15 14:23:58 -07003790 * ring_buffer_overrun_cpu - get the number of overruns caused by the ring
3791 * buffer wrapping around (only if RB_FL_OVERWRITE is on).
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003792 * @buffer: The ring buffer
3793 * @cpu: The per CPU buffer to get the number of overruns from
3794 */
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05003795unsigned long ring_buffer_overrun_cpu(struct trace_buffer *buffer, int cpu)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003796{
3797 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedt8aabee52009-03-12 13:13:49 -04003798 unsigned long ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003799
Rusty Russell9e01c1b2009-01-01 10:12:22 +10303800 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04003801 return 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003802
3803 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt77ae3652009-03-27 11:00:29 -04003804 ret = local_read(&cpu_buffer->overrun);
Steven Rostedt554f7862009-03-11 22:00:13 -04003805
3806 return ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003807}
Robert Richterc4f50182008-12-11 16:49:22 +01003808EXPORT_SYMBOL_GPL(ring_buffer_overrun_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003809
3810/**
Slava Pestov884bfe82011-07-15 14:23:58 -07003811 * ring_buffer_commit_overrun_cpu - get the number of overruns caused by
3812 * commits failing due to the buffer wrapping around while there are uncommitted
3813 * events, such as during an interrupt storm.
Steven Rostedtf0d2c682009-04-29 13:43:37 -04003814 * @buffer: The ring buffer
3815 * @cpu: The per CPU buffer to get the number of overruns from
3816 */
3817unsigned long
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05003818ring_buffer_commit_overrun_cpu(struct trace_buffer *buffer, int cpu)
Steven Rostedtf0d2c682009-04-29 13:43:37 -04003819{
3820 struct ring_buffer_per_cpu *cpu_buffer;
3821 unsigned long ret;
3822
3823 if (!cpumask_test_cpu(cpu, buffer->cpumask))
3824 return 0;
3825
3826 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt77ae3652009-03-27 11:00:29 -04003827 ret = local_read(&cpu_buffer->commit_overrun);
Steven Rostedtf0d2c682009-04-29 13:43:37 -04003828
3829 return ret;
3830}
3831EXPORT_SYMBOL_GPL(ring_buffer_commit_overrun_cpu);
3832
3833/**
Slava Pestov884bfe82011-07-15 14:23:58 -07003834 * ring_buffer_dropped_events_cpu - get the number of dropped events caused by
3835 * the ring buffer filling up (only if RB_FL_OVERWRITE is off).
3836 * @buffer: The ring buffer
3837 * @cpu: The per CPU buffer to get the number of overruns from
3838 */
3839unsigned long
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05003840ring_buffer_dropped_events_cpu(struct trace_buffer *buffer, int cpu)
Slava Pestov884bfe82011-07-15 14:23:58 -07003841{
3842 struct ring_buffer_per_cpu *cpu_buffer;
3843 unsigned long ret;
3844
3845 if (!cpumask_test_cpu(cpu, buffer->cpumask))
3846 return 0;
3847
3848 cpu_buffer = buffer->buffers[cpu];
3849 ret = local_read(&cpu_buffer->dropped_events);
3850
3851 return ret;
3852}
3853EXPORT_SYMBOL_GPL(ring_buffer_dropped_events_cpu);
3854
3855/**
Steven Rostedt (Red Hat)ad964702013-01-29 17:45:49 -05003856 * ring_buffer_read_events_cpu - get the number of events successfully read
3857 * @buffer: The ring buffer
3858 * @cpu: The per CPU buffer to get the number of events read
3859 */
3860unsigned long
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05003861ring_buffer_read_events_cpu(struct trace_buffer *buffer, int cpu)
Steven Rostedt (Red Hat)ad964702013-01-29 17:45:49 -05003862{
3863 struct ring_buffer_per_cpu *cpu_buffer;
3864
3865 if (!cpumask_test_cpu(cpu, buffer->cpumask))
3866 return 0;
3867
3868 cpu_buffer = buffer->buffers[cpu];
3869 return cpu_buffer->read;
3870}
3871EXPORT_SYMBOL_GPL(ring_buffer_read_events_cpu);
3872
3873/**
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003874 * ring_buffer_entries - get the number of entries in a buffer
3875 * @buffer: The ring buffer
3876 *
3877 * Returns the total number of entries in the ring buffer
3878 * (all CPU entries)
3879 */
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05003880unsigned long ring_buffer_entries(struct trace_buffer *buffer)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003881{
3882 struct ring_buffer_per_cpu *cpu_buffer;
3883 unsigned long entries = 0;
3884 int cpu;
3885
3886 /* if you care about this being correct, lock the buffer */
3887 for_each_buffer_cpu(buffer, cpu) {
3888 cpu_buffer = buffer->buffers[cpu];
Steven Rostedtf6195aa2010-09-01 12:23:12 -04003889 entries += rb_num_of_entries(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003890 }
3891
3892 return entries;
3893}
Robert Richterc4f50182008-12-11 16:49:22 +01003894EXPORT_SYMBOL_GPL(ring_buffer_entries);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003895
3896/**
Jiri Olsa67b394f2009-10-23 19:36:18 -04003897 * ring_buffer_overruns - get the number of overruns in buffer
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003898 * @buffer: The ring buffer
3899 *
3900 * Returns the total number of overruns in the ring buffer
3901 * (all CPU entries)
3902 */
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05003903unsigned long ring_buffer_overruns(struct trace_buffer *buffer)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003904{
3905 struct ring_buffer_per_cpu *cpu_buffer;
3906 unsigned long overruns = 0;
3907 int cpu;
3908
3909 /* if you care about this being correct, lock the buffer */
3910 for_each_buffer_cpu(buffer, cpu) {
3911 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt77ae3652009-03-27 11:00:29 -04003912 overruns += local_read(&cpu_buffer->overrun);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003913 }
3914
3915 return overruns;
3916}
Robert Richterc4f50182008-12-11 16:49:22 +01003917EXPORT_SYMBOL_GPL(ring_buffer_overruns);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003918
Steven Rostedt642edba2008-11-12 00:01:26 -05003919static void rb_iter_reset(struct ring_buffer_iter *iter)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003920{
3921 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
3922
Steven Rostedtd7690412008-10-01 00:29:53 -04003923 /* Iterator usage is expected to have record disabled */
Steven Rostedt (Red Hat)651e22f2014-08-06 14:11:33 -04003924 iter->head_page = cpu_buffer->reader_page;
3925 iter->head = cpu_buffer->reader_page->read;
Steven Rostedt (VMware)785888c2020-03-17 17:32:27 -04003926 iter->next_event = iter->head;
Steven Rostedt (Red Hat)651e22f2014-08-06 14:11:33 -04003927
3928 iter->cache_reader_page = iter->head_page;
Steven Rostedt (Red Hat)24607f12014-10-02 16:51:18 -04003929 iter->cache_read = cpu_buffer->read;
Steven Rostedt (Red Hat)651e22f2014-08-06 14:11:33 -04003930
Steven Rostedt (VMware)28e3fc52020-03-17 17:32:26 -04003931 if (iter->head) {
Steven Rostedtd7690412008-10-01 00:29:53 -04003932 iter->read_stamp = cpu_buffer->read_stamp;
Steven Rostedt (VMware)28e3fc52020-03-17 17:32:26 -04003933 iter->page_stamp = cpu_buffer->reader_page->page->time_stamp;
3934 } else {
Steven Rostedtabc9b562008-12-02 15:34:06 -05003935 iter->read_stamp = iter->head_page->page->time_stamp;
Steven Rostedt (VMware)28e3fc52020-03-17 17:32:26 -04003936 iter->page_stamp = iter->read_stamp;
3937 }
Steven Rostedt642edba2008-11-12 00:01:26 -05003938}
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003939
Steven Rostedt642edba2008-11-12 00:01:26 -05003940/**
3941 * ring_buffer_iter_reset - reset an iterator
3942 * @iter: The iterator to reset
3943 *
3944 * Resets the iterator, so that it will start from the beginning
3945 * again.
3946 */
3947void ring_buffer_iter_reset(struct ring_buffer_iter *iter)
3948{
Steven Rostedt554f7862009-03-11 22:00:13 -04003949 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedt642edba2008-11-12 00:01:26 -05003950 unsigned long flags;
3951
Steven Rostedt554f7862009-03-11 22:00:13 -04003952 if (!iter)
3953 return;
3954
3955 cpu_buffer = iter->cpu_buffer;
3956
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02003957 raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
Steven Rostedt642edba2008-11-12 00:01:26 -05003958 rb_iter_reset(iter);
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02003959 raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003960}
Robert Richterc4f50182008-12-11 16:49:22 +01003961EXPORT_SYMBOL_GPL(ring_buffer_iter_reset);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003962
3963/**
3964 * ring_buffer_iter_empty - check if an iterator has no more to read
3965 * @iter: The iterator to check
3966 */
3967int ring_buffer_iter_empty(struct ring_buffer_iter *iter)
3968{
3969 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedt (VMware)78f7a452017-04-19 14:29:46 -04003970 struct buffer_page *reader;
3971 struct buffer_page *head_page;
3972 struct buffer_page *commit_page;
Steven Rostedt (VMware)ead6ecf2020-03-17 17:32:24 -04003973 struct buffer_page *curr_commit_page;
Steven Rostedt (VMware)78f7a452017-04-19 14:29:46 -04003974 unsigned commit;
Steven Rostedt (VMware)ead6ecf2020-03-17 17:32:24 -04003975 u64 curr_commit_ts;
3976 u64 commit_ts;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003977
3978 cpu_buffer = iter->cpu_buffer;
Steven Rostedt (VMware)78f7a452017-04-19 14:29:46 -04003979 reader = cpu_buffer->reader_page;
3980 head_page = cpu_buffer->head_page;
3981 commit_page = cpu_buffer->commit_page;
Steven Rostedt (VMware)ead6ecf2020-03-17 17:32:24 -04003982 commit_ts = commit_page->page->time_stamp;
Steven Rostedt (VMware)78f7a452017-04-19 14:29:46 -04003983
Steven Rostedt (VMware)ead6ecf2020-03-17 17:32:24 -04003984 /*
3985 * When the writer goes across pages, it issues a cmpxchg which
3986 * is a mb(), which will synchronize with the rmb here.
3987 * (see rb_tail_page_update())
3988 */
3989 smp_rmb();
3990 commit = rb_page_commit(commit_page);
3991 /* We want to make sure that the commit page doesn't change */
3992 smp_rmb();
3993
3994 /* Make sure commit page didn't change */
3995 curr_commit_page = READ_ONCE(cpu_buffer->commit_page);
3996 curr_commit_ts = READ_ONCE(curr_commit_page->page->time_stamp);
3997
3998 /* If the commit page changed, then there's more data */
3999 if (curr_commit_page != commit_page ||
4000 curr_commit_ts != commit_ts)
4001 return 0;
4002
4003 /* Still racy, as it may return a false positive, but that's OK */
Steven Rostedt (VMware)785888c2020-03-17 17:32:27 -04004004 return ((iter->head_page == commit_page && iter->head >= commit) ||
Steven Rostedt (VMware)78f7a452017-04-19 14:29:46 -04004005 (iter->head_page == reader && commit_page == head_page &&
4006 head_page->read == commit &&
4007 iter->head == rb_page_commit(cpu_buffer->reader_page)));
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004008}
Robert Richterc4f50182008-12-11 16:49:22 +01004009EXPORT_SYMBOL_GPL(ring_buffer_iter_empty);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004010
4011static void
4012rb_update_read_stamp(struct ring_buffer_per_cpu *cpu_buffer,
4013 struct ring_buffer_event *event)
4014{
4015 u64 delta;
4016
Lai Jiangshan334d4162009-04-24 11:27:05 +08004017 switch (event->type_len) {
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004018 case RINGBUF_TYPE_PADDING:
4019 return;
4020
4021 case RINGBUF_TYPE_TIME_EXTEND:
Tom Zanussidc4e2802018-01-15 20:51:40 -06004022 delta = ring_buffer_event_time_stamp(event);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004023 cpu_buffer->read_stamp += delta;
4024 return;
4025
4026 case RINGBUF_TYPE_TIME_STAMP:
Tom Zanussidc4e2802018-01-15 20:51:40 -06004027 delta = ring_buffer_event_time_stamp(event);
4028 cpu_buffer->read_stamp = delta;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004029 return;
4030
4031 case RINGBUF_TYPE_DATA:
4032 cpu_buffer->read_stamp += event->time_delta;
4033 return;
4034
4035 default:
Steven Rostedt (VMware)da4d4012020-05-13 15:36:22 -04004036 RB_WARN_ON(cpu_buffer, 1);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004037 }
4038 return;
4039}
4040
4041static void
4042rb_update_iter_read_stamp(struct ring_buffer_iter *iter,
4043 struct ring_buffer_event *event)
4044{
4045 u64 delta;
4046
Lai Jiangshan334d4162009-04-24 11:27:05 +08004047 switch (event->type_len) {
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004048 case RINGBUF_TYPE_PADDING:
4049 return;
4050
4051 case RINGBUF_TYPE_TIME_EXTEND:
Tom Zanussidc4e2802018-01-15 20:51:40 -06004052 delta = ring_buffer_event_time_stamp(event);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004053 iter->read_stamp += delta;
4054 return;
4055
4056 case RINGBUF_TYPE_TIME_STAMP:
Tom Zanussidc4e2802018-01-15 20:51:40 -06004057 delta = ring_buffer_event_time_stamp(event);
4058 iter->read_stamp = delta;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004059 return;
4060
4061 case RINGBUF_TYPE_DATA:
4062 iter->read_stamp += event->time_delta;
4063 return;
4064
4065 default:
Steven Rostedt (VMware)da4d4012020-05-13 15:36:22 -04004066 RB_WARN_ON(iter->cpu_buffer, 1);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004067 }
4068 return;
4069}
4070
Steven Rostedtd7690412008-10-01 00:29:53 -04004071static struct buffer_page *
4072rb_get_reader_page(struct ring_buffer_per_cpu *cpu_buffer)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004073{
Steven Rostedtd7690412008-10-01 00:29:53 -04004074 struct buffer_page *reader = NULL;
Steven Rostedt66a8cb92010-03-31 13:21:56 -04004075 unsigned long overwrite;
Steven Rostedtd7690412008-10-01 00:29:53 -04004076 unsigned long flags;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04004077 int nr_loops = 0;
Steven Rostedt77ae3652009-03-27 11:00:29 -04004078 int ret;
Steven Rostedtd7690412008-10-01 00:29:53 -04004079
Steven Rostedt3e03fb72008-11-06 00:09:43 -05004080 local_irq_save(flags);
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01004081 arch_spin_lock(&cpu_buffer->lock);
Steven Rostedtd7690412008-10-01 00:29:53 -04004082
4083 again:
Steven Rostedt818e3dd2008-10-31 09:58:35 -04004084 /*
4085 * This should normally only loop twice. But because the
4086 * start of the reader inserts an empty page, it causes
4087 * a case where we will loop three times. There should be no
4088 * reason to loop four times (that I know of).
4089 */
Steven Rostedt3e89c7bb2008-11-11 15:28:41 -05004090 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 3)) {
Steven Rostedt818e3dd2008-10-31 09:58:35 -04004091 reader = NULL;
4092 goto out;
4093 }
4094
Steven Rostedtd7690412008-10-01 00:29:53 -04004095 reader = cpu_buffer->reader_page;
4096
4097 /* If there's more to read, return this page */
Steven Rostedtbf41a152008-10-04 02:00:59 -04004098 if (cpu_buffer->reader_page->read < rb_page_size(reader))
Steven Rostedtd7690412008-10-01 00:29:53 -04004099 goto out;
4100
4101 /* Never should we have an index greater than the size */
Steven Rostedt3e89c7bb2008-11-11 15:28:41 -05004102 if (RB_WARN_ON(cpu_buffer,
4103 cpu_buffer->reader_page->read > rb_page_size(reader)))
4104 goto out;
Steven Rostedtd7690412008-10-01 00:29:53 -04004105
4106 /* check if we caught up to the tail */
4107 reader = NULL;
Steven Rostedtbf41a152008-10-04 02:00:59 -04004108 if (cpu_buffer->commit_page == cpu_buffer->reader_page)
Steven Rostedtd7690412008-10-01 00:29:53 -04004109 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004110
Steven Rostedta5fb8332012-06-28 13:35:04 -04004111 /* Don't bother swapping if the ring buffer is empty */
4112 if (rb_num_of_entries(cpu_buffer) == 0)
4113 goto out;
4114
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004115 /*
Steven Rostedtd7690412008-10-01 00:29:53 -04004116 * Reset the reader page to size zero.
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004117 */
Steven Rostedt77ae3652009-03-27 11:00:29 -04004118 local_set(&cpu_buffer->reader_page->write, 0);
4119 local_set(&cpu_buffer->reader_page->entries, 0);
4120 local_set(&cpu_buffer->reader_page->page->commit, 0);
Steven Rostedtff0ff842010-03-31 22:11:42 -04004121 cpu_buffer->reader_page->real_end = 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004122
Steven Rostedt77ae3652009-03-27 11:00:29 -04004123 spin:
4124 /*
4125 * Splice the empty reader page into the list around the head.
4126 */
4127 reader = rb_set_head_page(cpu_buffer);
Steven Rostedt54f7be52012-11-29 22:27:22 -05004128 if (!reader)
4129 goto out;
Steven Rostedt0e1ff5d2010-01-06 20:40:44 -05004130 cpu_buffer->reader_page->list.next = rb_list_head(reader->list.next);
Steven Rostedtd7690412008-10-01 00:29:53 -04004131 cpu_buffer->reader_page->list.prev = reader->list.prev;
Steven Rostedtbf41a152008-10-04 02:00:59 -04004132
Steven Rostedt3adc54f2009-03-30 15:32:01 -04004133 /*
4134 * cpu_buffer->pages just needs to point to the buffer, it
4135 * has no specific buffer page to point to. Lets move it out
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004136 * of our way so we don't accidentally swap it.
Steven Rostedt3adc54f2009-03-30 15:32:01 -04004137 */
4138 cpu_buffer->pages = reader->list.prev;
4139
Steven Rostedt77ae3652009-03-27 11:00:29 -04004140 /* The reader page will be pointing to the new head */
4141 rb_set_list_to_head(cpu_buffer, &cpu_buffer->reader_page->list);
Steven Rostedtd7690412008-10-01 00:29:53 -04004142
4143 /*
Steven Rostedt66a8cb92010-03-31 13:21:56 -04004144 * We want to make sure we read the overruns after we set up our
4145 * pointers to the next object. The writer side does a
4146 * cmpxchg to cross pages which acts as the mb on the writer
4147 * side. Note, the reader will constantly fail the swap
4148 * while the writer is updating the pointers, so this
4149 * guarantees that the overwrite recorded here is the one we
4150 * want to compare with the last_overrun.
4151 */
4152 smp_mb();
4153 overwrite = local_read(&(cpu_buffer->overrun));
4154
4155 /*
Steven Rostedt77ae3652009-03-27 11:00:29 -04004156 * Here's the tricky part.
4157 *
4158 * We need to move the pointer past the header page.
4159 * But we can only do that if a writer is not currently
4160 * moving it. The page before the header page has the
4161 * flag bit '1' set if it is pointing to the page we want.
4162 * but if the writer is in the process of moving it
4163 * than it will be '2' or already moved '0'.
Steven Rostedtd7690412008-10-01 00:29:53 -04004164 */
Steven Rostedtd7690412008-10-01 00:29:53 -04004165
Steven Rostedt77ae3652009-03-27 11:00:29 -04004166 ret = rb_head_page_replace(reader, cpu_buffer->reader_page);
4167
4168 /*
4169 * If we did not convert it, then we must try again.
4170 */
4171 if (!ret)
4172 goto spin;
4173
4174 /*
Steven Rostedt (VMware)2c2b0a72018-11-29 20:32:26 -05004175 * Yay! We succeeded in replacing the page.
Steven Rostedt77ae3652009-03-27 11:00:29 -04004176 *
4177 * Now make the new head point back to the reader page.
4178 */
David Sharp5ded3dc62010-01-06 17:12:07 -08004179 rb_list_head(reader->list.next)->prev = &cpu_buffer->reader_page->list;
Steven Rostedt77ae3652009-03-27 11:00:29 -04004180 rb_inc_page(cpu_buffer, &cpu_buffer->head_page);
Steven Rostedtd7690412008-10-01 00:29:53 -04004181
Steven Rostedt (VMware)2c2b0a72018-11-29 20:32:26 -05004182 local_inc(&cpu_buffer->pages_read);
4183
Steven Rostedtd7690412008-10-01 00:29:53 -04004184 /* Finally update the reader page to the new head */
4185 cpu_buffer->reader_page = reader;
Steven Rostedt (Red Hat)b81f4722015-11-23 10:35:36 -05004186 cpu_buffer->reader_page->read = 0;
Steven Rostedtd7690412008-10-01 00:29:53 -04004187
Steven Rostedt66a8cb92010-03-31 13:21:56 -04004188 if (overwrite != cpu_buffer->last_overrun) {
4189 cpu_buffer->lost_events = overwrite - cpu_buffer->last_overrun;
4190 cpu_buffer->last_overrun = overwrite;
4191 }
4192
Steven Rostedtd7690412008-10-01 00:29:53 -04004193 goto again;
4194
4195 out:
Steven Rostedt (Red Hat)b81f4722015-11-23 10:35:36 -05004196 /* Update the read_stamp on the first event */
4197 if (reader && reader->read == 0)
4198 cpu_buffer->read_stamp = reader->page->time_stamp;
4199
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01004200 arch_spin_unlock(&cpu_buffer->lock);
Steven Rostedt3e03fb72008-11-06 00:09:43 -05004201 local_irq_restore(flags);
Steven Rostedtd7690412008-10-01 00:29:53 -04004202
4203 return reader;
4204}
4205
4206static void rb_advance_reader(struct ring_buffer_per_cpu *cpu_buffer)
4207{
4208 struct ring_buffer_event *event;
4209 struct buffer_page *reader;
4210 unsigned length;
4211
4212 reader = rb_get_reader_page(cpu_buffer);
4213
4214 /* This function should not be called when buffer is empty */
Steven Rostedt3e89c7bb2008-11-11 15:28:41 -05004215 if (RB_WARN_ON(cpu_buffer, !reader))
4216 return;
Steven Rostedtd7690412008-10-01 00:29:53 -04004217
4218 event = rb_reader_event(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004219
Steven Rostedta1863c22009-09-03 10:23:58 -04004220 if (event->type_len <= RINGBUF_TYPE_DATA_TYPE_LEN_MAX)
Steven Rostedte4906ef2009-04-30 20:49:44 -04004221 cpu_buffer->read++;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004222
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004223 rb_update_read_stamp(cpu_buffer, event);
4224
Steven Rostedtd7690412008-10-01 00:29:53 -04004225 length = rb_event_length(event);
Steven Rostedt6f807ac2008-10-04 02:00:58 -04004226 cpu_buffer->reader_page->read += length;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004227}
4228
4229static void rb_advance_iter(struct ring_buffer_iter *iter)
4230{
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004231 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004232
4233 cpu_buffer = iter->cpu_buffer;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004234
Steven Rostedt (VMware)785888c2020-03-17 17:32:27 -04004235 /* If head == next_event then we need to jump to the next event */
4236 if (iter->head == iter->next_event) {
4237 /* If the event gets overwritten again, there's nothing to do */
4238 if (rb_iter_head_event(iter) == NULL)
4239 return;
4240 }
4241
4242 iter->head = iter->next_event;
4243
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004244 /*
4245 * Check if we are at the end of the buffer.
4246 */
Steven Rostedt (VMware)785888c2020-03-17 17:32:27 -04004247 if (iter->next_event >= rb_page_size(iter->head_page)) {
Steven Rostedtea05b572009-06-03 09:30:10 -04004248 /* discarded commits can make the page empty */
4249 if (iter->head_page == cpu_buffer->commit_page)
Steven Rostedt3e89c7bb2008-11-11 15:28:41 -05004250 return;
Steven Rostedtd7690412008-10-01 00:29:53 -04004251 rb_inc_iter(iter);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004252 return;
4253 }
4254
Steven Rostedt (VMware)785888c2020-03-17 17:32:27 -04004255 rb_update_iter_read_stamp(iter, iter->event);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004256}
4257
Steven Rostedt66a8cb92010-03-31 13:21:56 -04004258static int rb_lost_events(struct ring_buffer_per_cpu *cpu_buffer)
4259{
4260 return cpu_buffer->lost_events;
4261}
4262
Steven Rostedtf83c9d02008-11-11 18:47:44 +01004263static struct ring_buffer_event *
Steven Rostedt66a8cb92010-03-31 13:21:56 -04004264rb_buffer_peek(struct ring_buffer_per_cpu *cpu_buffer, u64 *ts,
4265 unsigned long *lost_events)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004266{
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004267 struct ring_buffer_event *event;
Steven Rostedtd7690412008-10-01 00:29:53 -04004268 struct buffer_page *reader;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04004269 int nr_loops = 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004270
Tom Zanussidc4e2802018-01-15 20:51:40 -06004271 if (ts)
4272 *ts = 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004273 again:
Steven Rostedt818e3dd2008-10-31 09:58:35 -04004274 /*
Steven Rostedt69d1b832010-10-07 18:18:05 -04004275 * We repeat when a time extend is encountered.
4276 * Since the time extend is always attached to a data event,
4277 * we should never loop more than once.
4278 * (We never hit the following condition more than twice).
Steven Rostedt818e3dd2008-10-31 09:58:35 -04004279 */
Steven Rostedt69d1b832010-10-07 18:18:05 -04004280 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 2))
Steven Rostedt818e3dd2008-10-31 09:58:35 -04004281 return NULL;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04004282
Steven Rostedtd7690412008-10-01 00:29:53 -04004283 reader = rb_get_reader_page(cpu_buffer);
4284 if (!reader)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004285 return NULL;
4286
Steven Rostedtd7690412008-10-01 00:29:53 -04004287 event = rb_reader_event(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004288
Lai Jiangshan334d4162009-04-24 11:27:05 +08004289 switch (event->type_len) {
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004290 case RINGBUF_TYPE_PADDING:
Tom Zanussi2d622712009-03-22 03:30:49 -05004291 if (rb_null_event(event))
4292 RB_WARN_ON(cpu_buffer, 1);
4293 /*
4294 * Because the writer could be discarding every
4295 * event it creates (which would probably be bad)
4296 * if we were to go back to "again" then we may never
4297 * catch up, and will trigger the warn on, or lock
4298 * the box. Return the padding, and we will release
4299 * the current locks, and try again.
4300 */
Tom Zanussi2d622712009-03-22 03:30:49 -05004301 return event;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004302
4303 case RINGBUF_TYPE_TIME_EXTEND:
4304 /* Internal data, OK to advance */
Steven Rostedtd7690412008-10-01 00:29:53 -04004305 rb_advance_reader(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004306 goto again;
4307
4308 case RINGBUF_TYPE_TIME_STAMP:
Tom Zanussidc4e2802018-01-15 20:51:40 -06004309 if (ts) {
4310 *ts = ring_buffer_event_time_stamp(event);
4311 ring_buffer_normalize_time_stamp(cpu_buffer->buffer,
4312 cpu_buffer->cpu, ts);
4313 }
4314 /* Internal data, OK to advance */
Steven Rostedtd7690412008-10-01 00:29:53 -04004315 rb_advance_reader(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004316 goto again;
4317
4318 case RINGBUF_TYPE_DATA:
Tom Zanussidc4e2802018-01-15 20:51:40 -06004319 if (ts && !(*ts)) {
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004320 *ts = cpu_buffer->read_stamp + event->time_delta;
Robert Richterd8eeb2d2009-07-31 14:58:04 +02004321 ring_buffer_normalize_time_stamp(cpu_buffer->buffer,
Steven Rostedt37886f62009-03-17 17:22:06 -04004322 cpu_buffer->cpu, ts);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004323 }
Steven Rostedt66a8cb92010-03-31 13:21:56 -04004324 if (lost_events)
4325 *lost_events = rb_lost_events(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004326 return event;
4327
4328 default:
Steven Rostedt (VMware)da4d4012020-05-13 15:36:22 -04004329 RB_WARN_ON(cpu_buffer, 1);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004330 }
4331
4332 return NULL;
4333}
Robert Richterc4f50182008-12-11 16:49:22 +01004334EXPORT_SYMBOL_GPL(ring_buffer_peek);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004335
Steven Rostedtf83c9d02008-11-11 18:47:44 +01004336static struct ring_buffer_event *
4337rb_iter_peek(struct ring_buffer_iter *iter, u64 *ts)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004338{
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05004339 struct trace_buffer *buffer;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004340 struct ring_buffer_per_cpu *cpu_buffer;
4341 struct ring_buffer_event *event;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04004342 int nr_loops = 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004343
Tom Zanussidc4e2802018-01-15 20:51:40 -06004344 if (ts)
4345 *ts = 0;
4346
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004347 cpu_buffer = iter->cpu_buffer;
4348 buffer = cpu_buffer->buffer;
4349
Steven Rostedt492a74f2010-01-25 15:17:47 -05004350 /*
4351 * Check if someone performed a consuming read to
4352 * the buffer. A consuming read invalidates the iterator
4353 * and we need to reset the iterator in this case.
4354 */
4355 if (unlikely(iter->cache_read != cpu_buffer->read ||
4356 iter->cache_reader_page != cpu_buffer->reader_page))
4357 rb_iter_reset(iter);
4358
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004359 again:
Steven Rostedt3c05d742010-01-26 16:14:08 -05004360 if (ring_buffer_iter_empty(iter))
4361 return NULL;
4362
Steven Rostedt818e3dd2008-10-31 09:58:35 -04004363 /*
Steven Rostedt (VMware)3d2353d2020-05-13 15:18:01 -04004364 * As the writer can mess with what the iterator is trying
4365 * to read, just give up if we fail to get an event after
4366 * three tries. The iterator is not as reliable when reading
4367 * the ring buffer with an active write as the consumer is.
4368 * Do not warn if the three failures is reached.
Steven Rostedt818e3dd2008-10-31 09:58:35 -04004369 */
Steven Rostedt (VMware)3d2353d2020-05-13 15:18:01 -04004370 if (++nr_loops > 3)
Steven Rostedt818e3dd2008-10-31 09:58:35 -04004371 return NULL;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04004372
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004373 if (rb_per_cpu_empty(cpu_buffer))
4374 return NULL;
4375
Steven Rostedt (Red Hat)10e83fd2014-07-23 19:45:12 -04004376 if (iter->head >= rb_page_size(iter->head_page)) {
Steven Rostedt3c05d742010-01-26 16:14:08 -05004377 rb_inc_iter(iter);
4378 goto again;
4379 }
4380
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004381 event = rb_iter_head_event(iter);
Steven Rostedt (VMware)3d2353d2020-05-13 15:18:01 -04004382 if (!event)
Steven Rostedt (VMware)785888c2020-03-17 17:32:27 -04004383 goto again;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004384
Lai Jiangshan334d4162009-04-24 11:27:05 +08004385 switch (event->type_len) {
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004386 case RINGBUF_TYPE_PADDING:
Tom Zanussi2d622712009-03-22 03:30:49 -05004387 if (rb_null_event(event)) {
4388 rb_inc_iter(iter);
4389 goto again;
4390 }
4391 rb_advance_iter(iter);
4392 return event;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004393
4394 case RINGBUF_TYPE_TIME_EXTEND:
4395 /* Internal data, OK to advance */
4396 rb_advance_iter(iter);
4397 goto again;
4398
4399 case RINGBUF_TYPE_TIME_STAMP:
Tom Zanussidc4e2802018-01-15 20:51:40 -06004400 if (ts) {
4401 *ts = ring_buffer_event_time_stamp(event);
4402 ring_buffer_normalize_time_stamp(cpu_buffer->buffer,
4403 cpu_buffer->cpu, ts);
4404 }
4405 /* Internal data, OK to advance */
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004406 rb_advance_iter(iter);
4407 goto again;
4408
4409 case RINGBUF_TYPE_DATA:
Tom Zanussidc4e2802018-01-15 20:51:40 -06004410 if (ts && !(*ts)) {
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004411 *ts = iter->read_stamp + event->time_delta;
Steven Rostedt37886f62009-03-17 17:22:06 -04004412 ring_buffer_normalize_time_stamp(buffer,
4413 cpu_buffer->cpu, ts);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004414 }
4415 return event;
4416
4417 default:
Steven Rostedt (VMware)da4d4012020-05-13 15:36:22 -04004418 RB_WARN_ON(cpu_buffer, 1);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004419 }
4420
4421 return NULL;
4422}
Robert Richterc4f50182008-12-11 16:49:22 +01004423EXPORT_SYMBOL_GPL(ring_buffer_iter_peek);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004424
Steven Rostedt (Red Hat)289a5a22015-05-28 13:14:51 -04004425static inline bool rb_reader_lock(struct ring_buffer_per_cpu *cpu_buffer)
Steven Rostedt8d707e82009-06-16 21:22:48 -04004426{
Steven Rostedt (Red Hat)289a5a22015-05-28 13:14:51 -04004427 if (likely(!in_nmi())) {
4428 raw_spin_lock(&cpu_buffer->reader_lock);
4429 return true;
4430 }
4431
Steven Rostedt8d707e82009-06-16 21:22:48 -04004432 /*
4433 * If an NMI die dumps out the content of the ring buffer
Steven Rostedt (Red Hat)289a5a22015-05-28 13:14:51 -04004434 * trylock must be used to prevent a deadlock if the NMI
4435 * preempted a task that holds the ring buffer locks. If
4436 * we get the lock then all is fine, if not, then continue
4437 * to do the read, but this can corrupt the ring buffer,
4438 * so it must be permanently disabled from future writes.
4439 * Reading from NMI is a oneshot deal.
Steven Rostedt8d707e82009-06-16 21:22:48 -04004440 */
Steven Rostedt (Red Hat)289a5a22015-05-28 13:14:51 -04004441 if (raw_spin_trylock(&cpu_buffer->reader_lock))
4442 return true;
Steven Rostedt8d707e82009-06-16 21:22:48 -04004443
Steven Rostedt (Red Hat)289a5a22015-05-28 13:14:51 -04004444 /* Continue without locking, but disable the ring buffer */
4445 atomic_inc(&cpu_buffer->record_disabled);
4446 return false;
4447}
4448
4449static inline void
4450rb_reader_unlock(struct ring_buffer_per_cpu *cpu_buffer, bool locked)
4451{
4452 if (likely(locked))
4453 raw_spin_unlock(&cpu_buffer->reader_lock);
4454 return;
Steven Rostedt8d707e82009-06-16 21:22:48 -04004455}
4456
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004457/**
Steven Rostedtf83c9d02008-11-11 18:47:44 +01004458 * ring_buffer_peek - peek at the next event to be read
4459 * @buffer: The ring buffer to read
4460 * @cpu: The cpu to peak at
4461 * @ts: The timestamp counter of this event.
Steven Rostedt66a8cb92010-03-31 13:21:56 -04004462 * @lost_events: a variable to store if events were lost (may be NULL)
Steven Rostedtf83c9d02008-11-11 18:47:44 +01004463 *
4464 * This will return the event that will be read next, but does
4465 * not consume the data.
4466 */
4467struct ring_buffer_event *
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05004468ring_buffer_peek(struct trace_buffer *buffer, int cpu, u64 *ts,
Steven Rostedt66a8cb92010-03-31 13:21:56 -04004469 unsigned long *lost_events)
Steven Rostedtf83c9d02008-11-11 18:47:44 +01004470{
4471 struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
Steven Rostedt8aabee52009-03-12 13:13:49 -04004472 struct ring_buffer_event *event;
Steven Rostedtf83c9d02008-11-11 18:47:44 +01004473 unsigned long flags;
Steven Rostedt (Red Hat)289a5a22015-05-28 13:14:51 -04004474 bool dolock;
Steven Rostedtf83c9d02008-11-11 18:47:44 +01004475
Steven Rostedt554f7862009-03-11 22:00:13 -04004476 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04004477 return NULL;
Steven Rostedt554f7862009-03-11 22:00:13 -04004478
Tom Zanussi2d622712009-03-22 03:30:49 -05004479 again:
Steven Rostedt8d707e82009-06-16 21:22:48 -04004480 local_irq_save(flags);
Steven Rostedt (Red Hat)289a5a22015-05-28 13:14:51 -04004481 dolock = rb_reader_lock(cpu_buffer);
Steven Rostedt66a8cb92010-03-31 13:21:56 -04004482 event = rb_buffer_peek(cpu_buffer, ts, lost_events);
Robert Richter469535a2009-07-30 19:19:18 +02004483 if (event && event->type_len == RINGBUF_TYPE_PADDING)
4484 rb_advance_reader(cpu_buffer);
Steven Rostedt (Red Hat)289a5a22015-05-28 13:14:51 -04004485 rb_reader_unlock(cpu_buffer, dolock);
Steven Rostedt8d707e82009-06-16 21:22:48 -04004486 local_irq_restore(flags);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01004487
Steven Rostedt1b959e12009-09-03 10:12:13 -04004488 if (event && event->type_len == RINGBUF_TYPE_PADDING)
Tom Zanussi2d622712009-03-22 03:30:49 -05004489 goto again;
Tom Zanussi2d622712009-03-22 03:30:49 -05004490
Steven Rostedtf83c9d02008-11-11 18:47:44 +01004491 return event;
4492}
4493
Steven Rostedt (VMware)c9b7a4a2020-03-17 17:32:32 -04004494/** ring_buffer_iter_dropped - report if there are dropped events
4495 * @iter: The ring buffer iterator
4496 *
4497 * Returns true if there was dropped events since the last peek.
4498 */
4499bool ring_buffer_iter_dropped(struct ring_buffer_iter *iter)
4500{
4501 bool ret = iter->missed_events != 0;
4502
4503 iter->missed_events = 0;
4504 return ret;
4505}
4506EXPORT_SYMBOL_GPL(ring_buffer_iter_dropped);
4507
Steven Rostedtf83c9d02008-11-11 18:47:44 +01004508/**
4509 * ring_buffer_iter_peek - peek at the next event to be read
4510 * @iter: The ring buffer iterator
4511 * @ts: The timestamp counter of this event.
4512 *
4513 * This will return the event that will be read next, but does
4514 * not increment the iterator.
4515 */
4516struct ring_buffer_event *
4517ring_buffer_iter_peek(struct ring_buffer_iter *iter, u64 *ts)
4518{
4519 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
4520 struct ring_buffer_event *event;
4521 unsigned long flags;
4522
Tom Zanussi2d622712009-03-22 03:30:49 -05004523 again:
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02004524 raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01004525 event = rb_iter_peek(iter, ts);
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02004526 raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01004527
Steven Rostedt1b959e12009-09-03 10:12:13 -04004528 if (event && event->type_len == RINGBUF_TYPE_PADDING)
Tom Zanussi2d622712009-03-22 03:30:49 -05004529 goto again;
Tom Zanussi2d622712009-03-22 03:30:49 -05004530
Steven Rostedtf83c9d02008-11-11 18:47:44 +01004531 return event;
4532}
4533
4534/**
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004535 * ring_buffer_consume - return an event and consume it
4536 * @buffer: The ring buffer to get the next event from
Steven Rostedt66a8cb92010-03-31 13:21:56 -04004537 * @cpu: the cpu to read the buffer from
4538 * @ts: a variable to store the timestamp (may be NULL)
4539 * @lost_events: a variable to store if events were lost (may be NULL)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004540 *
4541 * Returns the next event in the ring buffer, and that event is consumed.
4542 * Meaning, that sequential reads will keep returning a different event,
4543 * and eventually empty the ring buffer if the producer is slower.
4544 */
4545struct ring_buffer_event *
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05004546ring_buffer_consume(struct trace_buffer *buffer, int cpu, u64 *ts,
Steven Rostedt66a8cb92010-03-31 13:21:56 -04004547 unsigned long *lost_events)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004548{
Steven Rostedt554f7862009-03-11 22:00:13 -04004549 struct ring_buffer_per_cpu *cpu_buffer;
4550 struct ring_buffer_event *event = NULL;
Steven Rostedtf83c9d02008-11-11 18:47:44 +01004551 unsigned long flags;
Steven Rostedt (Red Hat)289a5a22015-05-28 13:14:51 -04004552 bool dolock;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004553
Tom Zanussi2d622712009-03-22 03:30:49 -05004554 again:
Steven Rostedt554f7862009-03-11 22:00:13 -04004555 /* might be called in atomic */
4556 preempt_disable();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004557
Steven Rostedt554f7862009-03-11 22:00:13 -04004558 if (!cpumask_test_cpu(cpu, buffer->cpumask))
4559 goto out;
4560
4561 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt8d707e82009-06-16 21:22:48 -04004562 local_irq_save(flags);
Steven Rostedt (Red Hat)289a5a22015-05-28 13:14:51 -04004563 dolock = rb_reader_lock(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004564
Steven Rostedt66a8cb92010-03-31 13:21:56 -04004565 event = rb_buffer_peek(cpu_buffer, ts, lost_events);
4566 if (event) {
4567 cpu_buffer->lost_events = 0;
Robert Richter469535a2009-07-30 19:19:18 +02004568 rb_advance_reader(cpu_buffer);
Steven Rostedt66a8cb92010-03-31 13:21:56 -04004569 }
Steven Rostedtf83c9d02008-11-11 18:47:44 +01004570
Steven Rostedt (Red Hat)289a5a22015-05-28 13:14:51 -04004571 rb_reader_unlock(cpu_buffer, dolock);
Steven Rostedt8d707e82009-06-16 21:22:48 -04004572 local_irq_restore(flags);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01004573
Steven Rostedt554f7862009-03-11 22:00:13 -04004574 out:
4575 preempt_enable();
4576
Steven Rostedt1b959e12009-09-03 10:12:13 -04004577 if (event && event->type_len == RINGBUF_TYPE_PADDING)
Tom Zanussi2d622712009-03-22 03:30:49 -05004578 goto again;
Tom Zanussi2d622712009-03-22 03:30:49 -05004579
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004580 return event;
4581}
Robert Richterc4f50182008-12-11 16:49:22 +01004582EXPORT_SYMBOL_GPL(ring_buffer_consume);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004583
4584/**
David Miller72c9ddf2010-04-20 15:47:11 -07004585 * ring_buffer_read_prepare - Prepare for a non consuming read of the buffer
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004586 * @buffer: The ring buffer to read from
4587 * @cpu: The cpu buffer to iterate over
Douglas Anderson31b265b2019-03-08 11:32:04 -08004588 * @flags: gfp flags to use for memory allocation
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004589 *
David Miller72c9ddf2010-04-20 15:47:11 -07004590 * This performs the initial preparations necessary to iterate
4591 * through the buffer. Memory is allocated, buffer recording
4592 * is disabled, and the iterator pointer is returned to the caller.
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004593 *
Steven Rostedt (VMware)6167c202018-05-16 11:17:06 -04004594 * Disabling buffer recording prevents the reading from being
David Miller72c9ddf2010-04-20 15:47:11 -07004595 * corrupted. This is not a consuming read, so a producer is not
4596 * expected.
4597 *
4598 * After a sequence of ring_buffer_read_prepare calls, the user is
zhangwei(Jovi)d6118512013-07-15 16:32:50 +08004599 * expected to make at least one call to ring_buffer_read_prepare_sync.
David Miller72c9ddf2010-04-20 15:47:11 -07004600 * Afterwards, ring_buffer_read_start is invoked to get things going
4601 * for real.
4602 *
zhangwei(Jovi)d6118512013-07-15 16:32:50 +08004603 * This overall must be paired with ring_buffer_read_finish.
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004604 */
4605struct ring_buffer_iter *
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05004606ring_buffer_read_prepare(struct trace_buffer *buffer, int cpu, gfp_t flags)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004607{
4608 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedt8aabee52009-03-12 13:13:49 -04004609 struct ring_buffer_iter *iter;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004610
Rusty Russell9e01c1b2009-01-01 10:12:22 +10304611 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04004612 return NULL;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004613
Steven Rostedt (VMware)785888c2020-03-17 17:32:27 -04004614 iter = kzalloc(sizeof(*iter), flags);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004615 if (!iter)
Steven Rostedt8aabee52009-03-12 13:13:49 -04004616 return NULL;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004617
Steven Rostedt (VMware)785888c2020-03-17 17:32:27 -04004618 iter->event = kmalloc(BUF_MAX_DATA_SIZE, flags);
4619 if (!iter->event) {
4620 kfree(iter);
4621 return NULL;
4622 }
4623
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004624 cpu_buffer = buffer->buffers[cpu];
4625
4626 iter->cpu_buffer = cpu_buffer;
4627
Steven Rostedt (VMware)07b8b102020-03-27 16:21:22 -04004628 atomic_inc(&cpu_buffer->resize_disabled);
David Miller72c9ddf2010-04-20 15:47:11 -07004629
4630 return iter;
4631}
4632EXPORT_SYMBOL_GPL(ring_buffer_read_prepare);
4633
4634/**
4635 * ring_buffer_read_prepare_sync - Synchronize a set of prepare calls
4636 *
4637 * All previously invoked ring_buffer_read_prepare calls to prepare
4638 * iterators will be synchronized. Afterwards, read_buffer_read_start
4639 * calls on those iterators are allowed.
4640 */
4641void
4642ring_buffer_read_prepare_sync(void)
4643{
Paul E. McKenney74401722018-11-06 18:44:52 -08004644 synchronize_rcu();
David Miller72c9ddf2010-04-20 15:47:11 -07004645}
4646EXPORT_SYMBOL_GPL(ring_buffer_read_prepare_sync);
4647
4648/**
4649 * ring_buffer_read_start - start a non consuming read of the buffer
4650 * @iter: The iterator returned by ring_buffer_read_prepare
4651 *
4652 * This finalizes the startup of an iteration through the buffer.
4653 * The iterator comes from a call to ring_buffer_read_prepare and
4654 * an intervening ring_buffer_read_prepare_sync must have been
4655 * performed.
4656 *
zhangwei(Jovi)d6118512013-07-15 16:32:50 +08004657 * Must be paired with ring_buffer_read_finish.
David Miller72c9ddf2010-04-20 15:47:11 -07004658 */
4659void
4660ring_buffer_read_start(struct ring_buffer_iter *iter)
4661{
4662 struct ring_buffer_per_cpu *cpu_buffer;
4663 unsigned long flags;
4664
4665 if (!iter)
4666 return;
4667
4668 cpu_buffer = iter->cpu_buffer;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004669
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02004670 raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01004671 arch_spin_lock(&cpu_buffer->lock);
Steven Rostedt642edba2008-11-12 00:01:26 -05004672 rb_iter_reset(iter);
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01004673 arch_spin_unlock(&cpu_buffer->lock);
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02004674 raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004675}
Robert Richterc4f50182008-12-11 16:49:22 +01004676EXPORT_SYMBOL_GPL(ring_buffer_read_start);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004677
4678/**
zhangwei(Jovi)d6118512013-07-15 16:32:50 +08004679 * ring_buffer_read_finish - finish reading the iterator of the buffer
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004680 * @iter: The iterator retrieved by ring_buffer_start
4681 *
4682 * This re-enables the recording to the buffer, and frees the
4683 * iterator.
4684 */
4685void
4686ring_buffer_read_finish(struct ring_buffer_iter *iter)
4687{
4688 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
Steven Rostedt9366c1b2012-11-29 22:31:16 -05004689 unsigned long flags;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004690
Steven Rostedt659f4512012-05-14 17:02:33 -04004691 /*
4692 * Ring buffer is disabled from recording, here's a good place
Steven Rostedt9366c1b2012-11-29 22:31:16 -05004693 * to check the integrity of the ring buffer.
4694 * Must prevent readers from trying to read, as the check
4695 * clears the HEAD page and readers require it.
Steven Rostedt659f4512012-05-14 17:02:33 -04004696 */
Steven Rostedt9366c1b2012-11-29 22:31:16 -05004697 raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
Steven Rostedt659f4512012-05-14 17:02:33 -04004698 rb_check_pages(cpu_buffer);
Steven Rostedt9366c1b2012-11-29 22:31:16 -05004699 raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
Steven Rostedt659f4512012-05-14 17:02:33 -04004700
Steven Rostedt (VMware)07b8b102020-03-27 16:21:22 -04004701 atomic_dec(&cpu_buffer->resize_disabled);
Steven Rostedt (VMware)785888c2020-03-17 17:32:27 -04004702 kfree(iter->event);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004703 kfree(iter);
4704}
Robert Richterc4f50182008-12-11 16:49:22 +01004705EXPORT_SYMBOL_GPL(ring_buffer_read_finish);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004706
4707/**
Steven Rostedt (VMware)bc1a72a2020-03-17 17:32:25 -04004708 * ring_buffer_iter_advance - advance the iterator to the next location
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004709 * @iter: The ring buffer iterator
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004710 *
Steven Rostedt (VMware)bc1a72a2020-03-17 17:32:25 -04004711 * Move the location of the iterator such that the next read will
4712 * be the next location of the iterator.
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004713 */
Steven Rostedt (VMware)bc1a72a2020-03-17 17:32:25 -04004714void ring_buffer_iter_advance(struct ring_buffer_iter *iter)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004715{
Steven Rostedtf83c9d02008-11-11 18:47:44 +01004716 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
4717 unsigned long flags;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004718
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02004719 raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
Steven Rostedt7e9391c2009-09-03 10:02:09 -04004720
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004721 rb_advance_iter(iter);
4722
Steven Rostedt (VMware)bc1a72a2020-03-17 17:32:25 -04004723 raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004724}
Steven Rostedt (VMware)bc1a72a2020-03-17 17:32:25 -04004725EXPORT_SYMBOL_GPL(ring_buffer_iter_advance);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004726
4727/**
4728 * ring_buffer_size - return the size of the ring buffer (in bytes)
4729 * @buffer: The ring buffer.
Fabian Frederick59e7cff2014-06-05 20:22:05 +02004730 * @cpu: The CPU to get ring buffer size from.
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004731 */
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05004732unsigned long ring_buffer_size(struct trace_buffer *buffer, int cpu)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004733{
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08004734 /*
4735 * Earlier, this method returned
4736 * BUF_PAGE_SIZE * buffer->nr_pages
4737 * Since the nr_pages field is now removed, we have converted this to
4738 * return the per cpu buffer value.
4739 */
4740 if (!cpumask_test_cpu(cpu, buffer->cpumask))
4741 return 0;
4742
4743 return BUF_PAGE_SIZE * buffer->buffers[cpu]->nr_pages;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004744}
Robert Richterc4f50182008-12-11 16:49:22 +01004745EXPORT_SYMBOL_GPL(ring_buffer_size);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004746
4747static void
4748rb_reset_cpu(struct ring_buffer_per_cpu *cpu_buffer)
4749{
Steven Rostedt77ae3652009-03-27 11:00:29 -04004750 rb_head_page_deactivate(cpu_buffer);
4751
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004752 cpu_buffer->head_page
Steven Rostedt3adc54f2009-03-30 15:32:01 -04004753 = list_entry(cpu_buffer->pages, struct buffer_page, list);
Steven Rostedtbf41a152008-10-04 02:00:59 -04004754 local_set(&cpu_buffer->head_page->write, 0);
Steven Rostedt778c55d2009-05-01 18:44:45 -04004755 local_set(&cpu_buffer->head_page->entries, 0);
Steven Rostedtabc9b562008-12-02 15:34:06 -05004756 local_set(&cpu_buffer->head_page->page->commit, 0);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004757
Steven Rostedt6f807ac2008-10-04 02:00:58 -04004758 cpu_buffer->head_page->read = 0;
Steven Rostedtbf41a152008-10-04 02:00:59 -04004759
4760 cpu_buffer->tail_page = cpu_buffer->head_page;
4761 cpu_buffer->commit_page = cpu_buffer->head_page;
4762
4763 INIT_LIST_HEAD(&cpu_buffer->reader_page->list);
Vaibhav Nagarnaik5040b4b2012-05-03 18:59:51 -07004764 INIT_LIST_HEAD(&cpu_buffer->new_pages);
Steven Rostedtbf41a152008-10-04 02:00:59 -04004765 local_set(&cpu_buffer->reader_page->write, 0);
Steven Rostedt778c55d2009-05-01 18:44:45 -04004766 local_set(&cpu_buffer->reader_page->entries, 0);
Steven Rostedtabc9b562008-12-02 15:34:06 -05004767 local_set(&cpu_buffer->reader_page->page->commit, 0);
Steven Rostedt6f807ac2008-10-04 02:00:58 -04004768 cpu_buffer->reader_page->read = 0;
Steven Rostedtd7690412008-10-01 00:29:53 -04004769
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -07004770 local_set(&cpu_buffer->entries_bytes, 0);
Steven Rostedt77ae3652009-03-27 11:00:29 -04004771 local_set(&cpu_buffer->overrun, 0);
Slava Pestov884bfe82011-07-15 14:23:58 -07004772 local_set(&cpu_buffer->commit_overrun, 0);
4773 local_set(&cpu_buffer->dropped_events, 0);
Steven Rostedte4906ef2009-04-30 20:49:44 -04004774 local_set(&cpu_buffer->entries, 0);
Steven Rostedtfa743952009-06-16 12:37:57 -04004775 local_set(&cpu_buffer->committing, 0);
4776 local_set(&cpu_buffer->commits, 0);
Steven Rostedt (VMware)2c2b0a72018-11-29 20:32:26 -05004777 local_set(&cpu_buffer->pages_touched, 0);
4778 local_set(&cpu_buffer->pages_read, 0);
Steven Rostedt (VMware)03329f92018-11-29 21:38:42 -05004779 cpu_buffer->last_pages_touch = 0;
Steven Rostedt (VMware)2c2b0a72018-11-29 20:32:26 -05004780 cpu_buffer->shortest_full = 0;
Steven Rostedt77ae3652009-03-27 11:00:29 -04004781 cpu_buffer->read = 0;
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -07004782 cpu_buffer->read_bytes = 0;
Steven Rostedt69507c02009-01-21 18:45:57 -05004783
Steven Rostedt (VMware)10464b42020-06-28 22:52:27 -04004784 rb_time_set(&cpu_buffer->write_stamp, 0);
4785 rb_time_set(&cpu_buffer->before_stamp, 0);
Steven Rostedt77ae3652009-03-27 11:00:29 -04004786
Steven Rostedt66a8cb92010-03-31 13:21:56 -04004787 cpu_buffer->lost_events = 0;
4788 cpu_buffer->last_overrun = 0;
4789
Steven Rostedt77ae3652009-03-27 11:00:29 -04004790 rb_head_page_activate(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004791}
4792
4793/**
4794 * ring_buffer_reset_cpu - reset a ring buffer per CPU buffer
4795 * @buffer: The ring buffer to reset a per cpu buffer of
4796 * @cpu: The CPU buffer to be reset
4797 */
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05004798void ring_buffer_reset_cpu(struct trace_buffer *buffer, int cpu)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004799{
4800 struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
4801 unsigned long flags;
4802
Rusty Russell9e01c1b2009-01-01 10:12:22 +10304803 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04004804 return;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004805
Steven Rostedt (VMware)07b8b102020-03-27 16:21:22 -04004806 atomic_inc(&cpu_buffer->resize_disabled);
Steven Rostedt41ede232009-05-01 20:26:54 -04004807 atomic_inc(&cpu_buffer->record_disabled);
4808
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07004809 /* Make sure all commits have finished */
Paul E. McKenney74401722018-11-06 18:44:52 -08004810 synchronize_rcu();
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07004811
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02004812 raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01004813
Steven Rostedt41b6a952009-09-02 09:59:48 -04004814 if (RB_WARN_ON(cpu_buffer, local_read(&cpu_buffer->committing)))
4815 goto out;
4816
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01004817 arch_spin_lock(&cpu_buffer->lock);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004818
4819 rb_reset_cpu(cpu_buffer);
4820
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01004821 arch_spin_unlock(&cpu_buffer->lock);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01004822
Steven Rostedt41b6a952009-09-02 09:59:48 -04004823 out:
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02004824 raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
Steven Rostedt41ede232009-05-01 20:26:54 -04004825
4826 atomic_dec(&cpu_buffer->record_disabled);
Steven Rostedt (VMware)07b8b102020-03-27 16:21:22 -04004827 atomic_dec(&cpu_buffer->resize_disabled);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004828}
Robert Richterc4f50182008-12-11 16:49:22 +01004829EXPORT_SYMBOL_GPL(ring_buffer_reset_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004830
4831/**
4832 * ring_buffer_reset - reset a ring buffer
4833 * @buffer: The ring buffer to reset all cpu buffers
4834 */
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05004835void ring_buffer_reset(struct trace_buffer *buffer)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004836{
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004837 int cpu;
4838
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004839 for_each_buffer_cpu(buffer, cpu)
Steven Rostedtd7690412008-10-01 00:29:53 -04004840 ring_buffer_reset_cpu(buffer, cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004841}
Robert Richterc4f50182008-12-11 16:49:22 +01004842EXPORT_SYMBOL_GPL(ring_buffer_reset);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004843
4844/**
4845 * rind_buffer_empty - is the ring buffer empty?
4846 * @buffer: The ring buffer to test
4847 */
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05004848bool ring_buffer_empty(struct trace_buffer *buffer)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004849{
4850 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedtd4788202009-06-17 00:39:43 -04004851 unsigned long flags;
Steven Rostedt (Red Hat)289a5a22015-05-28 13:14:51 -04004852 bool dolock;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004853 int cpu;
Steven Rostedtd4788202009-06-17 00:39:43 -04004854 int ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004855
4856 /* yes this is racy, but if you don't like the race, lock the buffer */
4857 for_each_buffer_cpu(buffer, cpu) {
4858 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt8d707e82009-06-16 21:22:48 -04004859 local_irq_save(flags);
Steven Rostedt (Red Hat)289a5a22015-05-28 13:14:51 -04004860 dolock = rb_reader_lock(cpu_buffer);
Steven Rostedtd4788202009-06-17 00:39:43 -04004861 ret = rb_per_cpu_empty(cpu_buffer);
Steven Rostedt (Red Hat)289a5a22015-05-28 13:14:51 -04004862 rb_reader_unlock(cpu_buffer, dolock);
Steven Rostedt8d707e82009-06-16 21:22:48 -04004863 local_irq_restore(flags);
4864
Steven Rostedtd4788202009-06-17 00:39:43 -04004865 if (!ret)
Yaowei Bai3d4e2042015-09-29 22:43:32 +08004866 return false;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004867 }
Steven Rostedt554f7862009-03-11 22:00:13 -04004868
Yaowei Bai3d4e2042015-09-29 22:43:32 +08004869 return true;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004870}
Robert Richterc4f50182008-12-11 16:49:22 +01004871EXPORT_SYMBOL_GPL(ring_buffer_empty);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004872
4873/**
4874 * ring_buffer_empty_cpu - is a cpu buffer of a ring buffer empty?
4875 * @buffer: The ring buffer
4876 * @cpu: The CPU buffer to test
4877 */
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05004878bool ring_buffer_empty_cpu(struct trace_buffer *buffer, int cpu)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004879{
4880 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedtd4788202009-06-17 00:39:43 -04004881 unsigned long flags;
Steven Rostedt (Red Hat)289a5a22015-05-28 13:14:51 -04004882 bool dolock;
Steven Rostedt8aabee52009-03-12 13:13:49 -04004883 int ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004884
Rusty Russell9e01c1b2009-01-01 10:12:22 +10304885 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Yaowei Bai3d4e2042015-09-29 22:43:32 +08004886 return true;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004887
4888 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt8d707e82009-06-16 21:22:48 -04004889 local_irq_save(flags);
Steven Rostedt (Red Hat)289a5a22015-05-28 13:14:51 -04004890 dolock = rb_reader_lock(cpu_buffer);
Steven Rostedt554f7862009-03-11 22:00:13 -04004891 ret = rb_per_cpu_empty(cpu_buffer);
Steven Rostedt (Red Hat)289a5a22015-05-28 13:14:51 -04004892 rb_reader_unlock(cpu_buffer, dolock);
Steven Rostedt8d707e82009-06-16 21:22:48 -04004893 local_irq_restore(flags);
Steven Rostedt554f7862009-03-11 22:00:13 -04004894
4895 return ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004896}
Robert Richterc4f50182008-12-11 16:49:22 +01004897EXPORT_SYMBOL_GPL(ring_buffer_empty_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004898
Steven Rostedt85bac322009-09-04 14:24:40 -04004899#ifdef CONFIG_RING_BUFFER_ALLOW_SWAP
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004900/**
4901 * ring_buffer_swap_cpu - swap a CPU buffer between two ring buffers
4902 * @buffer_a: One buffer to swap with
4903 * @buffer_b: The other buffer to swap with
Fabian Frederick59e7cff2014-06-05 20:22:05 +02004904 * @cpu: the CPU of the buffers to swap
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004905 *
4906 * This function is useful for tracers that want to take a "snapshot"
4907 * of a CPU buffer and has another back up buffer lying around.
4908 * it is expected that the tracer handles the cpu buffer not being
4909 * used at the moment.
4910 */
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05004911int ring_buffer_swap_cpu(struct trace_buffer *buffer_a,
4912 struct trace_buffer *buffer_b, int cpu)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004913{
4914 struct ring_buffer_per_cpu *cpu_buffer_a;
4915 struct ring_buffer_per_cpu *cpu_buffer_b;
Steven Rostedt554f7862009-03-11 22:00:13 -04004916 int ret = -EINVAL;
4917
Rusty Russell9e01c1b2009-01-01 10:12:22 +10304918 if (!cpumask_test_cpu(cpu, buffer_a->cpumask) ||
4919 !cpumask_test_cpu(cpu, buffer_b->cpumask))
Steven Rostedt554f7862009-03-11 22:00:13 -04004920 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004921
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08004922 cpu_buffer_a = buffer_a->buffers[cpu];
4923 cpu_buffer_b = buffer_b->buffers[cpu];
4924
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004925 /* At least make sure the two buffers are somewhat the same */
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08004926 if (cpu_buffer_a->nr_pages != cpu_buffer_b->nr_pages)
Steven Rostedt554f7862009-03-11 22:00:13 -04004927 goto out;
4928
4929 ret = -EAGAIN;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004930
Steven Rostedt97b17ef2009-01-21 15:24:56 -05004931 if (atomic_read(&buffer_a->record_disabled))
Steven Rostedt554f7862009-03-11 22:00:13 -04004932 goto out;
Steven Rostedt97b17ef2009-01-21 15:24:56 -05004933
4934 if (atomic_read(&buffer_b->record_disabled))
Steven Rostedt554f7862009-03-11 22:00:13 -04004935 goto out;
Steven Rostedt97b17ef2009-01-21 15:24:56 -05004936
Steven Rostedt97b17ef2009-01-21 15:24:56 -05004937 if (atomic_read(&cpu_buffer_a->record_disabled))
Steven Rostedt554f7862009-03-11 22:00:13 -04004938 goto out;
Steven Rostedt97b17ef2009-01-21 15:24:56 -05004939
4940 if (atomic_read(&cpu_buffer_b->record_disabled))
Steven Rostedt554f7862009-03-11 22:00:13 -04004941 goto out;
Steven Rostedt97b17ef2009-01-21 15:24:56 -05004942
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004943 /*
Paul E. McKenney74401722018-11-06 18:44:52 -08004944 * We can't do a synchronize_rcu here because this
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004945 * function can be called in atomic context.
4946 * Normally this will be called from the same CPU as cpu.
4947 * If not it's up to the caller to protect this.
4948 */
4949 atomic_inc(&cpu_buffer_a->record_disabled);
4950 atomic_inc(&cpu_buffer_b->record_disabled);
4951
Steven Rostedt98277992009-09-02 10:56:15 -04004952 ret = -EBUSY;
4953 if (local_read(&cpu_buffer_a->committing))
4954 goto out_dec;
4955 if (local_read(&cpu_buffer_b->committing))
4956 goto out_dec;
4957
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004958 buffer_a->buffers[cpu] = cpu_buffer_b;
4959 buffer_b->buffers[cpu] = cpu_buffer_a;
4960
4961 cpu_buffer_b->buffer = buffer_a;
4962 cpu_buffer_a->buffer = buffer_b;
4963
Steven Rostedt98277992009-09-02 10:56:15 -04004964 ret = 0;
4965
4966out_dec:
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004967 atomic_dec(&cpu_buffer_a->record_disabled);
4968 atomic_dec(&cpu_buffer_b->record_disabled);
Steven Rostedt554f7862009-03-11 22:00:13 -04004969out:
Steven Rostedt554f7862009-03-11 22:00:13 -04004970 return ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004971}
Robert Richterc4f50182008-12-11 16:49:22 +01004972EXPORT_SYMBOL_GPL(ring_buffer_swap_cpu);
Steven Rostedt85bac322009-09-04 14:24:40 -04004973#endif /* CONFIG_RING_BUFFER_ALLOW_SWAP */
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004974
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004975/**
4976 * ring_buffer_alloc_read_page - allocate a page to read from buffer
4977 * @buffer: the buffer to allocate for.
zhangwei(Jovi)d6118512013-07-15 16:32:50 +08004978 * @cpu: the cpu buffer to allocate.
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004979 *
4980 * This function is used in conjunction with ring_buffer_read_page.
4981 * When reading a full page from the ring buffer, these functions
4982 * can be used to speed up the process. The calling function should
4983 * allocate a few pages first with this function. Then when it
4984 * needs to get pages from the ring buffer, it passes the result
4985 * of this function into ring_buffer_read_page, which will swap
4986 * the page that was allocated, with the read page of the buffer.
4987 *
4988 * Returns:
Steven Rostedt (VMware)a7e52ad2017-08-02 14:20:54 -04004989 * The page allocated, or ERR_PTR
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004990 */
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05004991void *ring_buffer_alloc_read_page(struct trace_buffer *buffer, int cpu)
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004992{
Steven Rostedt (VMware)a7e52ad2017-08-02 14:20:54 -04004993 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedt (VMware)73a757e2017-05-01 09:35:09 -04004994 struct buffer_data_page *bpage = NULL;
4995 unsigned long flags;
Vaibhav Nagarnaik7ea59062011-05-03 17:56:42 -07004996 struct page *page;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004997
Steven Rostedt (VMware)a7e52ad2017-08-02 14:20:54 -04004998 if (!cpumask_test_cpu(cpu, buffer->cpumask))
4999 return ERR_PTR(-ENODEV);
5000
5001 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt (VMware)73a757e2017-05-01 09:35:09 -04005002 local_irq_save(flags);
5003 arch_spin_lock(&cpu_buffer->lock);
5004
5005 if (cpu_buffer->free_page) {
5006 bpage = cpu_buffer->free_page;
5007 cpu_buffer->free_page = NULL;
5008 }
5009
5010 arch_spin_unlock(&cpu_buffer->lock);
5011 local_irq_restore(flags);
5012
5013 if (bpage)
5014 goto out;
5015
Vaibhav Nagarnaikd7ec4bf2011-06-07 17:01:42 -07005016 page = alloc_pages_node(cpu_to_node(cpu),
5017 GFP_KERNEL | __GFP_NORETRY, 0);
Vaibhav Nagarnaik7ea59062011-05-03 17:56:42 -07005018 if (!page)
Steven Rostedt (VMware)a7e52ad2017-08-02 14:20:54 -04005019 return ERR_PTR(-ENOMEM);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05005020
Vaibhav Nagarnaik7ea59062011-05-03 17:56:42 -07005021 bpage = page_address(page);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05005022
Steven Rostedt (VMware)73a757e2017-05-01 09:35:09 -04005023 out:
Steven Rostedtef7a4a12009-03-03 00:27:49 -05005024 rb_init_page(bpage);
5025
Steven Rostedt044fa782008-12-02 23:50:03 -05005026 return bpage;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05005027}
Steven Rostedtd6ce96d2009-05-05 01:15:24 -04005028EXPORT_SYMBOL_GPL(ring_buffer_alloc_read_page);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05005029
5030/**
5031 * ring_buffer_free_read_page - free an allocated read page
5032 * @buffer: the buffer the page was allocate for
Steven Rostedt (VMware)73a757e2017-05-01 09:35:09 -04005033 * @cpu: the cpu buffer the page came from
Steven Rostedt8789a9e2008-12-02 15:34:07 -05005034 * @data: the page to free
5035 *
5036 * Free a page allocated from ring_buffer_alloc_read_page.
5037 */
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05005038void ring_buffer_free_read_page(struct trace_buffer *buffer, int cpu, void *data)
Steven Rostedt8789a9e2008-12-02 15:34:07 -05005039{
Steven Rostedt (VMware)73a757e2017-05-01 09:35:09 -04005040 struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
5041 struct buffer_data_page *bpage = data;
Steven Rostedt (VMware)ae415fa2017-12-22 21:19:29 -05005042 struct page *page = virt_to_page(bpage);
Steven Rostedt (VMware)73a757e2017-05-01 09:35:09 -04005043 unsigned long flags;
5044
Steven Rostedt (VMware)ae415fa2017-12-22 21:19:29 -05005045 /* If the page is still in use someplace else, we can't reuse it */
5046 if (page_ref_count(page) > 1)
5047 goto out;
5048
Steven Rostedt (VMware)73a757e2017-05-01 09:35:09 -04005049 local_irq_save(flags);
5050 arch_spin_lock(&cpu_buffer->lock);
5051
5052 if (!cpu_buffer->free_page) {
5053 cpu_buffer->free_page = bpage;
5054 bpage = NULL;
5055 }
5056
5057 arch_spin_unlock(&cpu_buffer->lock);
5058 local_irq_restore(flags);
5059
Steven Rostedt (VMware)ae415fa2017-12-22 21:19:29 -05005060 out:
Steven Rostedt (VMware)73a757e2017-05-01 09:35:09 -04005061 free_page((unsigned long)bpage);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05005062}
Steven Rostedtd6ce96d2009-05-05 01:15:24 -04005063EXPORT_SYMBOL_GPL(ring_buffer_free_read_page);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05005064
5065/**
5066 * ring_buffer_read_page - extract a page from the ring buffer
5067 * @buffer: buffer to extract from
5068 * @data_page: the page to use allocated from ring_buffer_alloc_read_page
Steven Rostedtef7a4a12009-03-03 00:27:49 -05005069 * @len: amount to extract
Steven Rostedt8789a9e2008-12-02 15:34:07 -05005070 * @cpu: the cpu of the buffer to extract
5071 * @full: should the extraction only happen when the page is full.
5072 *
5073 * This function will pull out a page from the ring buffer and consume it.
5074 * @data_page must be the address of the variable that was returned
5075 * from ring_buffer_alloc_read_page. This is because the page might be used
5076 * to swap with a page in the ring buffer.
5077 *
5078 * for example:
zhangwei(Jovi)d6118512013-07-15 16:32:50 +08005079 * rpage = ring_buffer_alloc_read_page(buffer, cpu);
Steven Rostedt (VMware)a7e52ad2017-08-02 14:20:54 -04005080 * if (IS_ERR(rpage))
5081 * return PTR_ERR(rpage);
Steven Rostedtef7a4a12009-03-03 00:27:49 -05005082 * ret = ring_buffer_read_page(buffer, &rpage, len, cpu, 0);
Lai Jiangshan667d2412009-02-09 14:21:17 +08005083 * if (ret >= 0)
5084 * process_page(rpage, ret);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05005085 *
5086 * When @full is set, the function will not return true unless
5087 * the writer is off the reader page.
5088 *
5089 * Note: it is up to the calling functions to handle sleeps and wakeups.
5090 * The ring buffer can be used anywhere in the kernel and can not
5091 * blindly call wake_up. The layer that uses the ring buffer must be
5092 * responsible for that.
5093 *
5094 * Returns:
Lai Jiangshan667d2412009-02-09 14:21:17 +08005095 * >=0 if data has been transferred, returns the offset of consumed data.
5096 * <0 if no data has been transferred.
Steven Rostedt8789a9e2008-12-02 15:34:07 -05005097 */
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05005098int ring_buffer_read_page(struct trace_buffer *buffer,
Steven Rostedtef7a4a12009-03-03 00:27:49 -05005099 void **data_page, size_t len, int cpu, int full)
Steven Rostedt8789a9e2008-12-02 15:34:07 -05005100{
5101 struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
5102 struct ring_buffer_event *event;
Steven Rostedt044fa782008-12-02 23:50:03 -05005103 struct buffer_data_page *bpage;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05005104 struct buffer_page *reader;
Steven Rostedtff0ff842010-03-31 22:11:42 -04005105 unsigned long missed_events;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05005106 unsigned long flags;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05005107 unsigned int commit;
Lai Jiangshan667d2412009-02-09 14:21:17 +08005108 unsigned int read;
Steven Rostedt4f3640f2009-03-03 23:52:42 -05005109 u64 save_timestamp;
Lai Jiangshan667d2412009-02-09 14:21:17 +08005110 int ret = -1;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05005111
Steven Rostedt554f7862009-03-11 22:00:13 -04005112 if (!cpumask_test_cpu(cpu, buffer->cpumask))
5113 goto out;
5114
Steven Rostedt474d32b2009-03-03 19:51:40 -05005115 /*
5116 * If len is not big enough to hold the page header, then
5117 * we can not copy anything.
5118 */
5119 if (len <= BUF_PAGE_HDR_SIZE)
Steven Rostedt554f7862009-03-11 22:00:13 -04005120 goto out;
Steven Rostedt474d32b2009-03-03 19:51:40 -05005121
5122 len -= BUF_PAGE_HDR_SIZE;
5123
Steven Rostedt8789a9e2008-12-02 15:34:07 -05005124 if (!data_page)
Steven Rostedt554f7862009-03-11 22:00:13 -04005125 goto out;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05005126
Steven Rostedt044fa782008-12-02 23:50:03 -05005127 bpage = *data_page;
5128 if (!bpage)
Steven Rostedt554f7862009-03-11 22:00:13 -04005129 goto out;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05005130
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02005131 raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05005132
Steven Rostedtef7a4a12009-03-03 00:27:49 -05005133 reader = rb_get_reader_page(cpu_buffer);
5134 if (!reader)
Steven Rostedt554f7862009-03-11 22:00:13 -04005135 goto out_unlock;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05005136
Steven Rostedtef7a4a12009-03-03 00:27:49 -05005137 event = rb_reader_event(cpu_buffer);
Lai Jiangshan667d2412009-02-09 14:21:17 +08005138
Steven Rostedtef7a4a12009-03-03 00:27:49 -05005139 read = reader->read;
5140 commit = rb_page_commit(reader);
5141
Steven Rostedt66a8cb92010-03-31 13:21:56 -04005142 /* Check if any events were dropped */
Steven Rostedtff0ff842010-03-31 22:11:42 -04005143 missed_events = cpu_buffer->lost_events;
Steven Rostedt66a8cb92010-03-31 13:21:56 -04005144
Steven Rostedt8789a9e2008-12-02 15:34:07 -05005145 /*
Steven Rostedt474d32b2009-03-03 19:51:40 -05005146 * If this page has been partially read or
5147 * if len is not big enough to read the rest of the page or
5148 * a writer is still on the page, then
5149 * we must copy the data from the page to the buffer.
5150 * Otherwise, we can simply swap the page with the one passed in.
Steven Rostedt8789a9e2008-12-02 15:34:07 -05005151 */
Steven Rostedt474d32b2009-03-03 19:51:40 -05005152 if (read || (len < (commit - read)) ||
Steven Rostedtef7a4a12009-03-03 00:27:49 -05005153 cpu_buffer->reader_page == cpu_buffer->commit_page) {
Lai Jiangshan667d2412009-02-09 14:21:17 +08005154 struct buffer_data_page *rpage = cpu_buffer->reader_page->page;
Steven Rostedt474d32b2009-03-03 19:51:40 -05005155 unsigned int rpos = read;
5156 unsigned int pos = 0;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05005157 unsigned int size;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05005158
5159 if (full)
Steven Rostedt554f7862009-03-11 22:00:13 -04005160 goto out_unlock;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05005161
Steven Rostedtef7a4a12009-03-03 00:27:49 -05005162 if (len > (commit - read))
5163 len = (commit - read);
5164
Steven Rostedt69d1b832010-10-07 18:18:05 -04005165 /* Always keep the time extend and data together */
5166 size = rb_event_ts_length(event);
Steven Rostedtef7a4a12009-03-03 00:27:49 -05005167
5168 if (len < size)
Steven Rostedt554f7862009-03-11 22:00:13 -04005169 goto out_unlock;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05005170
Steven Rostedt4f3640f2009-03-03 23:52:42 -05005171 /* save the current timestamp, since the user will need it */
5172 save_timestamp = cpu_buffer->read_stamp;
5173
Steven Rostedtef7a4a12009-03-03 00:27:49 -05005174 /* Need to copy one event at a time */
5175 do {
David Sharpe1e35922010-12-22 16:38:24 -08005176 /* We need the size of one event, because
5177 * rb_advance_reader only advances by one event,
5178 * whereas rb_event_ts_length may include the size of
5179 * one or two events.
5180 * We have already ensured there's enough space if this
5181 * is a time extend. */
5182 size = rb_event_length(event);
Steven Rostedt474d32b2009-03-03 19:51:40 -05005183 memcpy(bpage->data + pos, rpage->data + rpos, size);
Steven Rostedtef7a4a12009-03-03 00:27:49 -05005184
5185 len -= size;
5186
5187 rb_advance_reader(cpu_buffer);
Steven Rostedt474d32b2009-03-03 19:51:40 -05005188 rpos = reader->read;
5189 pos += size;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05005190
Huang Ying18fab912010-07-28 14:14:01 +08005191 if (rpos >= commit)
5192 break;
5193
Steven Rostedtef7a4a12009-03-03 00:27:49 -05005194 event = rb_reader_event(cpu_buffer);
Steven Rostedt69d1b832010-10-07 18:18:05 -04005195 /* Always keep the time extend and data together */
5196 size = rb_event_ts_length(event);
David Sharpe1e35922010-12-22 16:38:24 -08005197 } while (len >= size);
Lai Jiangshan667d2412009-02-09 14:21:17 +08005198
5199 /* update bpage */
Steven Rostedtef7a4a12009-03-03 00:27:49 -05005200 local_set(&bpage->commit, pos);
Steven Rostedt4f3640f2009-03-03 23:52:42 -05005201 bpage->time_stamp = save_timestamp;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05005202
Steven Rostedt474d32b2009-03-03 19:51:40 -05005203 /* we copied everything to the beginning */
5204 read = 0;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05005205 } else {
Steven Rostedtafbab762009-05-01 19:40:05 -04005206 /* update the entry counter */
Steven Rostedt77ae3652009-03-27 11:00:29 -04005207 cpu_buffer->read += rb_page_entries(reader);
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -07005208 cpu_buffer->read_bytes += BUF_PAGE_SIZE;
Steven Rostedtafbab762009-05-01 19:40:05 -04005209
Steven Rostedt8789a9e2008-12-02 15:34:07 -05005210 /* swap the pages */
Steven Rostedt044fa782008-12-02 23:50:03 -05005211 rb_init_page(bpage);
Steven Rostedtef7a4a12009-03-03 00:27:49 -05005212 bpage = reader->page;
5213 reader->page = *data_page;
5214 local_set(&reader->write, 0);
Steven Rostedt778c55d2009-05-01 18:44:45 -04005215 local_set(&reader->entries, 0);
Steven Rostedtef7a4a12009-03-03 00:27:49 -05005216 reader->read = 0;
Steven Rostedt044fa782008-12-02 23:50:03 -05005217 *data_page = bpage;
Steven Rostedtff0ff842010-03-31 22:11:42 -04005218
5219 /*
5220 * Use the real_end for the data size,
5221 * This gives us a chance to store the lost events
5222 * on the page.
5223 */
5224 if (reader->real_end)
5225 local_set(&bpage->commit, reader->real_end);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05005226 }
Lai Jiangshan667d2412009-02-09 14:21:17 +08005227 ret = read;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05005228
Steven Rostedt66a8cb92010-03-31 13:21:56 -04005229 cpu_buffer->lost_events = 0;
Steven Rostedt2711ca22010-05-21 13:32:26 -04005230
5231 commit = local_read(&bpage->commit);
Steven Rostedt66a8cb92010-03-31 13:21:56 -04005232 /*
5233 * Set a flag in the commit field if we lost events
5234 */
Steven Rostedtff0ff842010-03-31 22:11:42 -04005235 if (missed_events) {
Steven Rostedtff0ff842010-03-31 22:11:42 -04005236 /* If there is room at the end of the page to save the
5237 * missed events, then record it there.
5238 */
5239 if (BUF_PAGE_SIZE - commit >= sizeof(missed_events)) {
5240 memcpy(&bpage->data[commit], &missed_events,
5241 sizeof(missed_events));
5242 local_add(RB_MISSED_STORED, &bpage->commit);
Steven Rostedt2711ca22010-05-21 13:32:26 -04005243 commit += sizeof(missed_events);
Steven Rostedtff0ff842010-03-31 22:11:42 -04005244 }
Steven Rostedt66a8cb92010-03-31 13:21:56 -04005245 local_add(RB_MISSED_EVENTS, &bpage->commit);
Steven Rostedtff0ff842010-03-31 22:11:42 -04005246 }
Steven Rostedt66a8cb92010-03-31 13:21:56 -04005247
Steven Rostedt2711ca22010-05-21 13:32:26 -04005248 /*
5249 * This page may be off to user land. Zero it out here.
5250 */
5251 if (commit < BUF_PAGE_SIZE)
5252 memset(&bpage->data[commit], 0, BUF_PAGE_SIZE - commit);
5253
Steven Rostedt554f7862009-03-11 22:00:13 -04005254 out_unlock:
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02005255 raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05005256
Steven Rostedt554f7862009-03-11 22:00:13 -04005257 out:
Steven Rostedt8789a9e2008-12-02 15:34:07 -05005258 return ret;
5259}
Steven Rostedtd6ce96d2009-05-05 01:15:24 -04005260EXPORT_SYMBOL_GPL(ring_buffer_read_page);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05005261
Sebastian Andrzej Siewiorb32614c2016-11-27 00:13:34 +01005262/*
5263 * We only allocate new buffers, never free them if the CPU goes down.
5264 * If we were to free the buffer, then the user would lose any trace that was in
5265 * the buffer.
5266 */
5267int trace_rb_cpu_prepare(unsigned int cpu, struct hlist_node *node)
Steven Rostedt554f7862009-03-11 22:00:13 -04005268{
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05005269 struct trace_buffer *buffer;
Steven Rostedt (Red Hat)9b94a8f2016-05-12 11:01:24 -04005270 long nr_pages_same;
5271 int cpu_i;
5272 unsigned long nr_pages;
Steven Rostedt554f7862009-03-11 22:00:13 -04005273
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05005274 buffer = container_of(node, struct trace_buffer, node);
Sebastian Andrzej Siewiorb32614c2016-11-27 00:13:34 +01005275 if (cpumask_test_cpu(cpu, buffer->cpumask))
5276 return 0;
Steven Rostedt554f7862009-03-11 22:00:13 -04005277
Sebastian Andrzej Siewiorb32614c2016-11-27 00:13:34 +01005278 nr_pages = 0;
5279 nr_pages_same = 1;
5280 /* check if all cpu sizes are same */
5281 for_each_buffer_cpu(buffer, cpu_i) {
5282 /* fill in the size from first enabled cpu */
5283 if (nr_pages == 0)
5284 nr_pages = buffer->buffers[cpu_i]->nr_pages;
5285 if (nr_pages != buffer->buffers[cpu_i]->nr_pages) {
5286 nr_pages_same = 0;
5287 break;
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08005288 }
Steven Rostedt554f7862009-03-11 22:00:13 -04005289 }
Sebastian Andrzej Siewiorb32614c2016-11-27 00:13:34 +01005290 /* allocate minimum pages, user can later expand it */
5291 if (!nr_pages_same)
5292 nr_pages = 2;
5293 buffer->buffers[cpu] =
5294 rb_allocate_cpu_buffer(buffer, nr_pages, cpu);
5295 if (!buffer->buffers[cpu]) {
5296 WARN(1, "failed to allocate ring buffer on CPU %u\n",
5297 cpu);
5298 return -ENOMEM;
5299 }
5300 smp_wmb();
5301 cpumask_set_cpu(cpu, buffer->cpumask);
5302 return 0;
Steven Rostedt554f7862009-03-11 22:00:13 -04005303}
Steven Rostedt (Red Hat)6c43e552013-03-15 11:32:53 -04005304
5305#ifdef CONFIG_RING_BUFFER_STARTUP_TEST
5306/*
5307 * This is a basic integrity check of the ring buffer.
5308 * Late in the boot cycle this test will run when configured in.
5309 * It will kick off a thread per CPU that will go into a loop
5310 * writing to the per cpu ring buffer various sizes of data.
5311 * Some of the data will be large items, some small.
5312 *
5313 * Another thread is created that goes into a spin, sending out
5314 * IPIs to the other CPUs to also write into the ring buffer.
5315 * this is to test the nesting ability of the buffer.
5316 *
5317 * Basic stats are recorded and reported. If something in the
5318 * ring buffer should happen that's not expected, a big warning
5319 * is displayed and all ring buffers are disabled.
5320 */
5321static struct task_struct *rb_threads[NR_CPUS] __initdata;
5322
5323struct rb_test_data {
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05005324 struct trace_buffer *buffer;
Steven Rostedt (Red Hat)6c43e552013-03-15 11:32:53 -04005325 unsigned long events;
5326 unsigned long bytes_written;
5327 unsigned long bytes_alloc;
5328 unsigned long bytes_dropped;
5329 unsigned long events_nested;
5330 unsigned long bytes_written_nested;
5331 unsigned long bytes_alloc_nested;
5332 unsigned long bytes_dropped_nested;
5333 int min_size_nested;
5334 int max_size_nested;
5335 int max_size;
5336 int min_size;
5337 int cpu;
5338 int cnt;
5339};
5340
5341static struct rb_test_data rb_data[NR_CPUS] __initdata;
5342
5343/* 1 meg per cpu */
5344#define RB_TEST_BUFFER_SIZE 1048576
5345
5346static char rb_string[] __initdata =
5347 "abcdefghijklmnopqrstuvwxyz1234567890!@#$%^&*()?+\\"
5348 "?+|:';\",.<>/?abcdefghijklmnopqrstuvwxyz1234567890"
5349 "!@#$%^&*()?+\\?+|:';\",.<>/?abcdefghijklmnopqrstuv";
5350
5351static bool rb_test_started __initdata;
5352
5353struct rb_item {
5354 int size;
5355 char str[];
5356};
5357
5358static __init int rb_write_something(struct rb_test_data *data, bool nested)
5359{
5360 struct ring_buffer_event *event;
5361 struct rb_item *item;
5362 bool started;
5363 int event_len;
5364 int size;
5365 int len;
5366 int cnt;
5367
5368 /* Have nested writes different that what is written */
5369 cnt = data->cnt + (nested ? 27 : 0);
5370
5371 /* Multiply cnt by ~e, to make some unique increment */
YueHaibing40ed29b2018-09-23 12:11:33 +00005372 size = (cnt * 68 / 25) % (sizeof(rb_string) - 1);
Steven Rostedt (Red Hat)6c43e552013-03-15 11:32:53 -04005373
5374 len = size + sizeof(struct rb_item);
5375
5376 started = rb_test_started;
5377 /* read rb_test_started before checking buffer enabled */
5378 smp_rmb();
5379
5380 event = ring_buffer_lock_reserve(data->buffer, len);
5381 if (!event) {
5382 /* Ignore dropped events before test starts. */
5383 if (started) {
5384 if (nested)
5385 data->bytes_dropped += len;
5386 else
5387 data->bytes_dropped_nested += len;
5388 }
5389 return len;
5390 }
5391
5392 event_len = ring_buffer_event_length(event);
5393
5394 if (RB_WARN_ON(data->buffer, event_len < len))
5395 goto out;
5396
5397 item = ring_buffer_event_data(event);
5398 item->size = size;
5399 memcpy(item->str, rb_string, size);
5400
5401 if (nested) {
5402 data->bytes_alloc_nested += event_len;
5403 data->bytes_written_nested += len;
5404 data->events_nested++;
5405 if (!data->min_size_nested || len < data->min_size_nested)
5406 data->min_size_nested = len;
5407 if (len > data->max_size_nested)
5408 data->max_size_nested = len;
5409 } else {
5410 data->bytes_alloc += event_len;
5411 data->bytes_written += len;
5412 data->events++;
5413 if (!data->min_size || len < data->min_size)
5414 data->max_size = len;
5415 if (len > data->max_size)
5416 data->max_size = len;
5417 }
5418
5419 out:
5420 ring_buffer_unlock_commit(data->buffer, event);
5421
5422 return 0;
5423}
5424
5425static __init int rb_test(void *arg)
5426{
5427 struct rb_test_data *data = arg;
5428
5429 while (!kthread_should_stop()) {
5430 rb_write_something(data, false);
5431 data->cnt++;
5432
5433 set_current_state(TASK_INTERRUPTIBLE);
5434 /* Now sleep between a min of 100-300us and a max of 1ms */
5435 usleep_range(((data->cnt % 3) + 1) * 100, 1000);
5436 }
5437
5438 return 0;
5439}
5440
5441static __init void rb_ipi(void *ignore)
5442{
5443 struct rb_test_data *data;
5444 int cpu = smp_processor_id();
5445
5446 data = &rb_data[cpu];
5447 rb_write_something(data, true);
5448}
5449
5450static __init int rb_hammer_test(void *arg)
5451{
5452 while (!kthread_should_stop()) {
5453
5454 /* Send an IPI to all cpus to write data! */
5455 smp_call_function(rb_ipi, NULL, 1);
5456 /* No sleep, but for non preempt, let others run */
5457 schedule();
5458 }
5459
5460 return 0;
5461}
5462
5463static __init int test_ringbuffer(void)
5464{
5465 struct task_struct *rb_hammer;
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05005466 struct trace_buffer *buffer;
Steven Rostedt (Red Hat)6c43e552013-03-15 11:32:53 -04005467 int cpu;
5468 int ret = 0;
5469
Steven Rostedt (VMware)a3566462019-12-02 16:25:27 -05005470 if (security_locked_down(LOCKDOWN_TRACEFS)) {
Stephen Rothwellee195452019-12-06 09:25:03 +11005471 pr_warn("Lockdown is enabled, skipping ring buffer tests\n");
Steven Rostedt (VMware)a3566462019-12-02 16:25:27 -05005472 return 0;
5473 }
5474
Steven Rostedt (Red Hat)6c43e552013-03-15 11:32:53 -04005475 pr_info("Running ring buffer tests...\n");
5476
5477 buffer = ring_buffer_alloc(RB_TEST_BUFFER_SIZE, RB_FL_OVERWRITE);
5478 if (WARN_ON(!buffer))
5479 return 0;
5480
5481 /* Disable buffer so that threads can't write to it yet */
5482 ring_buffer_record_off(buffer);
5483
5484 for_each_online_cpu(cpu) {
5485 rb_data[cpu].buffer = buffer;
5486 rb_data[cpu].cpu = cpu;
5487 rb_data[cpu].cnt = cpu;
5488 rb_threads[cpu] = kthread_create(rb_test, &rb_data[cpu],
5489 "rbtester/%d", cpu);
Wei Yongjun62277de2016-06-17 17:33:59 +00005490 if (WARN_ON(IS_ERR(rb_threads[cpu]))) {
Steven Rostedt (Red Hat)6c43e552013-03-15 11:32:53 -04005491 pr_cont("FAILED\n");
Wei Yongjun62277de2016-06-17 17:33:59 +00005492 ret = PTR_ERR(rb_threads[cpu]);
Steven Rostedt (Red Hat)6c43e552013-03-15 11:32:53 -04005493 goto out_free;
5494 }
5495
5496 kthread_bind(rb_threads[cpu], cpu);
5497 wake_up_process(rb_threads[cpu]);
5498 }
5499
5500 /* Now create the rb hammer! */
5501 rb_hammer = kthread_run(rb_hammer_test, NULL, "rbhammer");
Wei Yongjun62277de2016-06-17 17:33:59 +00005502 if (WARN_ON(IS_ERR(rb_hammer))) {
Steven Rostedt (Red Hat)6c43e552013-03-15 11:32:53 -04005503 pr_cont("FAILED\n");
Wei Yongjun62277de2016-06-17 17:33:59 +00005504 ret = PTR_ERR(rb_hammer);
Steven Rostedt (Red Hat)6c43e552013-03-15 11:32:53 -04005505 goto out_free;
5506 }
5507
5508 ring_buffer_record_on(buffer);
5509 /*
5510 * Show buffer is enabled before setting rb_test_started.
5511 * Yes there's a small race window where events could be
5512 * dropped and the thread wont catch it. But when a ring
5513 * buffer gets enabled, there will always be some kind of
5514 * delay before other CPUs see it. Thus, we don't care about
5515 * those dropped events. We care about events dropped after
5516 * the threads see that the buffer is active.
5517 */
5518 smp_wmb();
5519 rb_test_started = true;
5520
5521 set_current_state(TASK_INTERRUPTIBLE);
5522 /* Just run for 10 seconds */;
5523 schedule_timeout(10 * HZ);
5524
5525 kthread_stop(rb_hammer);
5526
5527 out_free:
5528 for_each_online_cpu(cpu) {
5529 if (!rb_threads[cpu])
5530 break;
5531 kthread_stop(rb_threads[cpu]);
5532 }
5533 if (ret) {
5534 ring_buffer_free(buffer);
5535 return ret;
5536 }
5537
5538 /* Report! */
5539 pr_info("finished\n");
5540 for_each_online_cpu(cpu) {
5541 struct ring_buffer_event *event;
5542 struct rb_test_data *data = &rb_data[cpu];
5543 struct rb_item *item;
5544 unsigned long total_events;
5545 unsigned long total_dropped;
5546 unsigned long total_written;
5547 unsigned long total_alloc;
5548 unsigned long total_read = 0;
5549 unsigned long total_size = 0;
5550 unsigned long total_len = 0;
5551 unsigned long total_lost = 0;
5552 unsigned long lost;
5553 int big_event_size;
5554 int small_event_size;
5555
5556 ret = -1;
5557
5558 total_events = data->events + data->events_nested;
5559 total_written = data->bytes_written + data->bytes_written_nested;
5560 total_alloc = data->bytes_alloc + data->bytes_alloc_nested;
5561 total_dropped = data->bytes_dropped + data->bytes_dropped_nested;
5562
5563 big_event_size = data->max_size + data->max_size_nested;
5564 small_event_size = data->min_size + data->min_size_nested;
5565
5566 pr_info("CPU %d:\n", cpu);
5567 pr_info(" events: %ld\n", total_events);
5568 pr_info(" dropped bytes: %ld\n", total_dropped);
5569 pr_info(" alloced bytes: %ld\n", total_alloc);
5570 pr_info(" written bytes: %ld\n", total_written);
5571 pr_info(" biggest event: %d\n", big_event_size);
5572 pr_info(" smallest event: %d\n", small_event_size);
5573
5574 if (RB_WARN_ON(buffer, total_dropped))
5575 break;
5576
5577 ret = 0;
5578
5579 while ((event = ring_buffer_consume(buffer, cpu, NULL, &lost))) {
5580 total_lost += lost;
5581 item = ring_buffer_event_data(event);
5582 total_len += ring_buffer_event_length(event);
5583 total_size += item->size + sizeof(struct rb_item);
5584 if (memcmp(&item->str[0], rb_string, item->size) != 0) {
5585 pr_info("FAILED!\n");
5586 pr_info("buffer had: %.*s\n", item->size, item->str);
5587 pr_info("expected: %.*s\n", item->size, rb_string);
5588 RB_WARN_ON(buffer, 1);
5589 ret = -1;
5590 break;
5591 }
5592 total_read++;
5593 }
5594 if (ret)
5595 break;
5596
5597 ret = -1;
5598
5599 pr_info(" read events: %ld\n", total_read);
5600 pr_info(" lost events: %ld\n", total_lost);
5601 pr_info(" total events: %ld\n", total_lost + total_read);
5602 pr_info(" recorded len bytes: %ld\n", total_len);
5603 pr_info(" recorded size bytes: %ld\n", total_size);
5604 if (total_lost)
5605 pr_info(" With dropped events, record len and size may not match\n"
5606 " alloced and written from above\n");
5607 if (!total_lost) {
5608 if (RB_WARN_ON(buffer, total_len != total_alloc ||
5609 total_size != total_written))
5610 break;
5611 }
5612 if (RB_WARN_ON(buffer, total_lost + total_read != total_events))
5613 break;
5614
5615 ret = 0;
5616 }
5617 if (!ret)
5618 pr_info("Ring buffer PASSED!\n");
5619
5620 ring_buffer_free(buffer);
5621 return 0;
5622}
5623
5624late_initcall(test_ringbuffer);
5625#endif /* CONFIG_RING_BUFFER_STARTUP_TEST */