blob: ed1941304f69809428ab79fd71f5607c7df6a562 [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
Nicholas Pigginb23d7a5f2020-06-25 15:34:03 +1000273#define for_each_online_buffer_cpu(buffer, cpu) \
274 for_each_cpu_and(cpu, buffer->cpumask, cpu_online_mask)
275
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400276#define TS_SHIFT 27
277#define TS_MASK ((1ULL << TS_SHIFT) - 1)
278#define TS_DELTA_TEST (~TS_MASK)
279
Tom Zanussidc4e2802018-01-15 20:51:40 -0600280/**
281 * ring_buffer_event_time_stamp - return the event's extended timestamp
282 * @event: the event to get the timestamp of
283 *
284 * Returns the extended timestamp associated with a data event.
285 * An extended time_stamp is a 64-bit timestamp represented
286 * internally in a special way that makes the best use of space
287 * contained within a ring buffer event. This function decodes
288 * it and maps it to a straight u64 value.
289 */
290u64 ring_buffer_event_time_stamp(struct ring_buffer_event *event)
291{
292 u64 ts;
293
294 ts = event->array[0];
295 ts <<= TS_SHIFT;
296 ts += event->time_delta;
297
298 return ts;
299}
300
Steven Rostedt66a8cb92010-03-31 13:21:56 -0400301/* Flag when events were overwritten */
302#define RB_MISSED_EVENTS (1 << 31)
Steven Rostedtff0ff842010-03-31 22:11:42 -0400303/* Missed count stored at end */
304#define RB_MISSED_STORED (1 << 30)
Steven Rostedt66a8cb92010-03-31 13:21:56 -0400305
Steven Rostedtabc9b562008-12-02 15:34:06 -0500306struct buffer_data_page {
Steven Rostedte4c2ce82008-10-01 11:14:54 -0400307 u64 time_stamp; /* page time stamp */
Wenji Huangc3706f02009-02-10 01:03:18 -0500308 local_t commit; /* write committed index */
James Hogan649508f2012-05-30 12:11:19 +0100309 unsigned char data[] RB_ALIGN_DATA; /* data of buffer page */
Steven Rostedtabc9b562008-12-02 15:34:06 -0500310};
311
Steven Rostedt77ae3652009-03-27 11:00:29 -0400312/*
313 * Note, the buffer_page list must be first. The buffer pages
314 * are allocated in cache lines, which means that each buffer
315 * page will be at the beginning of a cache line, and thus
316 * the least significant bits will be zero. We use this to
317 * add flags in the list struct pointers, to make the ring buffer
318 * lockless.
319 */
Steven Rostedtabc9b562008-12-02 15:34:06 -0500320struct buffer_page {
Steven Rostedt778c55d2009-05-01 18:44:45 -0400321 struct list_head list; /* list of buffer pages */
Steven Rostedtabc9b562008-12-02 15:34:06 -0500322 local_t write; /* index for next write */
Steven Rostedt6f807ac2008-10-04 02:00:58 -0400323 unsigned read; /* index for next read */
Steven Rostedt778c55d2009-05-01 18:44:45 -0400324 local_t entries; /* entries on this page */
Steven Rostedtff0ff842010-03-31 22:11:42 -0400325 unsigned long real_end; /* real end of data */
Steven Rostedtabc9b562008-12-02 15:34:06 -0500326 struct buffer_data_page *page; /* Actual data page */
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400327};
328
Steven Rostedt77ae3652009-03-27 11:00:29 -0400329/*
330 * The buffer page counters, write and entries, must be reset
331 * atomically when crossing page boundaries. To synchronize this
332 * update, two counters are inserted into the number. One is
333 * the actual counter for the write position or count on the page.
334 *
335 * The other is a counter of updaters. Before an update happens
336 * the update partition of the counter is incremented. This will
337 * allow the updater to update the counter atomically.
338 *
339 * The counter is 20 bits, and the state data is 12.
340 */
341#define RB_WRITE_MASK 0xfffff
342#define RB_WRITE_INTCNT (1 << 20)
343
Steven Rostedt044fa782008-12-02 23:50:03 -0500344static void rb_init_page(struct buffer_data_page *bpage)
Steven Rostedtabc9b562008-12-02 15:34:06 -0500345{
Steven Rostedt044fa782008-12-02 23:50:03 -0500346 local_set(&bpage->commit, 0);
Steven Rostedtabc9b562008-12-02 15:34:06 -0500347}
348
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400349/*
Steven Rostedted568292008-09-29 23:02:40 -0400350 * Also stolen from mm/slob.c. Thanks to Mathieu Desnoyers for pointing
351 * this issue out.
352 */
Andrew Morton34a148b2009-01-09 12:27:09 -0800353static void free_buffer_page(struct buffer_page *bpage)
Steven Rostedted568292008-09-29 23:02:40 -0400354{
Andrew Morton34a148b2009-01-09 12:27:09 -0800355 free_page((unsigned long)bpage->page);
Steven Rostedte4c2ce82008-10-01 11:14:54 -0400356 kfree(bpage);
Steven Rostedted568292008-09-29 23:02:40 -0400357}
358
359/*
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400360 * We need to fit the time_stamp delta into 27 bits.
361 */
362static inline int test_time_stamp(u64 delta)
363{
364 if (delta & TS_DELTA_TEST)
365 return 1;
366 return 0;
367}
368
Steven Rostedt474d32b2009-03-03 19:51:40 -0500369#define BUF_PAGE_SIZE (PAGE_SIZE - BUF_PAGE_HDR_SIZE)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400370
Steven Rostedtbe957c42009-05-11 14:42:53 -0400371/* Max payload is BUF_PAGE_SIZE - header (8bytes) */
372#define BUF_MAX_DATA_SIZE (BUF_PAGE_SIZE - (sizeof(u32) * 2))
373
Steven Rostedtd1b182a2009-04-15 16:53:47 -0400374int ring_buffer_print_page_header(struct trace_seq *s)
375{
376 struct buffer_data_page field;
Steven Rostedtd1b182a2009-04-15 16:53:47 -0400377
Steven Rostedt (Red Hat)c0cd93a2014-11-12 11:49:00 -0500378 trace_seq_printf(s, "\tfield: u64 timestamp;\t"
379 "offset:0;\tsize:%u;\tsigned:%u;\n",
380 (unsigned int)sizeof(field.time_stamp),
381 (unsigned int)is_signed_type(u64));
Steven Rostedtd1b182a2009-04-15 16:53:47 -0400382
Steven Rostedt (Red Hat)c0cd93a2014-11-12 11:49:00 -0500383 trace_seq_printf(s, "\tfield: local_t commit;\t"
384 "offset:%u;\tsize:%u;\tsigned:%u;\n",
385 (unsigned int)offsetof(typeof(field), commit),
386 (unsigned int)sizeof(field.commit),
387 (unsigned int)is_signed_type(long));
Steven Rostedtd1b182a2009-04-15 16:53:47 -0400388
Steven Rostedt (Red Hat)c0cd93a2014-11-12 11:49:00 -0500389 trace_seq_printf(s, "\tfield: int overwrite;\t"
390 "offset:%u;\tsize:%u;\tsigned:%u;\n",
391 (unsigned int)offsetof(typeof(field), commit),
392 1,
393 (unsigned int)is_signed_type(long));
Steven Rostedt66a8cb92010-03-31 13:21:56 -0400394
Steven Rostedt (Red Hat)c0cd93a2014-11-12 11:49:00 -0500395 trace_seq_printf(s, "\tfield: char data;\t"
396 "offset:%u;\tsize:%u;\tsigned:%u;\n",
397 (unsigned int)offsetof(typeof(field), data),
398 (unsigned int)BUF_PAGE_SIZE,
399 (unsigned int)is_signed_type(char));
Steven Rostedtd1b182a2009-04-15 16:53:47 -0400400
Steven Rostedt (Red Hat)c0cd93a2014-11-12 11:49:00 -0500401 return !trace_seq_has_overflowed(s);
Steven Rostedtd1b182a2009-04-15 16:53:47 -0400402}
403
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -0500404struct rb_irq_work {
405 struct irq_work work;
406 wait_queue_head_t waiters;
Steven Rostedt (Red Hat)1e0d6712015-02-10 22:14:53 -0500407 wait_queue_head_t full_waiters;
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -0500408 bool waiters_pending;
Steven Rostedt (Red Hat)1e0d6712015-02-10 22:14:53 -0500409 bool full_waiters_pending;
410 bool wakeup_full;
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -0500411};
412
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400413/*
Steven Rostedt (Red Hat)fcc742e2015-05-28 17:13:14 -0400414 * Structure to hold event state and handle nested events.
415 */
416struct rb_event_info {
417 u64 ts;
418 u64 delta;
419 unsigned long length;
420 struct buffer_page *tail_page;
421 int add_timestamp;
422};
423
424/*
Steven Rostedt (VMware)a389d862020-06-28 22:52:25 -0400425 * Used for the add_timestamp
426 * NONE
Steven Rostedt (VMware)7c4b4a52020-06-28 22:52:26 -0400427 * EXTEND - wants a time extend
428 * ABSOLUTE - the buffer requests all events to have absolute time stamps
Steven Rostedt (VMware)a389d862020-06-28 22:52:25 -0400429 * FORCE - force a full time stamp.
430 */
431enum {
Steven Rostedt (VMware)7c4b4a52020-06-28 22:52:26 -0400432 RB_ADD_STAMP_NONE = 0,
433 RB_ADD_STAMP_EXTEND = BIT(1),
434 RB_ADD_STAMP_ABSOLUTE = BIT(2),
435 RB_ADD_STAMP_FORCE = BIT(3)
Steven Rostedt (VMware)a389d862020-06-28 22:52:25 -0400436};
437/*
Steven Rostedt (Red Hat)a497adb2015-05-29 10:32:28 -0400438 * Used for which event context the event is in.
439 * NMI = 0
440 * IRQ = 1
441 * SOFTIRQ = 2
442 * NORMAL = 3
443 *
444 * See trace_recursive_lock() comment below for more details.
445 */
446enum {
447 RB_CTX_NMI,
448 RB_CTX_IRQ,
449 RB_CTX_SOFTIRQ,
450 RB_CTX_NORMAL,
451 RB_CTX_MAX
452};
453
Steven Rostedt (VMware)10464b42020-06-28 22:52:27 -0400454#if BITS_PER_LONG == 32
455#define RB_TIME_32
456#endif
457
458/* To test on 64 bit machines */
459//#define RB_TIME_32
460
461#ifdef RB_TIME_32
462
463struct rb_time_struct {
464 local_t cnt;
465 local_t top;
466 local_t bottom;
467};
468#else
469#include <asm/local64.h>
470struct rb_time_struct {
471 local64_t time;
472};
473#endif
474typedef struct rb_time_struct rb_time_t;
475
Steven Rostedt (Red Hat)a497adb2015-05-29 10:32:28 -0400476/*
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400477 * head_page == tail_page && head == tail then buffer is empty.
478 */
479struct ring_buffer_per_cpu {
480 int cpu;
Richard Kennedy985023d2010-03-25 11:27:36 +0000481 atomic_t record_disabled;
Steven Rostedt (VMware)07b8b102020-03-27 16:21:22 -0400482 atomic_t resize_disabled;
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -0500483 struct trace_buffer *buffer;
Thomas Gleixner5389f6f2009-07-25 17:13:33 +0200484 raw_spinlock_t reader_lock; /* serialize readers */
Thomas Gleixner445c8952009-12-02 19:49:50 +0100485 arch_spinlock_t lock;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400486 struct lock_class_key lock_key;
Steven Rostedt (VMware)73a757e2017-05-01 09:35:09 -0400487 struct buffer_data_page *free_page;
Steven Rostedt (Red Hat)9b94a8f2016-05-12 11:01:24 -0400488 unsigned long nr_pages;
Steven Rostedt (Red Hat)58a09ec2015-05-27 10:27:47 -0400489 unsigned int current_context;
Steven Rostedt3adc54f2009-03-30 15:32:01 -0400490 struct list_head *pages;
Steven Rostedt6f807ac2008-10-04 02:00:58 -0400491 struct buffer_page *head_page; /* read from head */
492 struct buffer_page *tail_page; /* write to tail */
Wenji Huangc3706f02009-02-10 01:03:18 -0500493 struct buffer_page *commit_page; /* committed pages */
Steven Rostedtd7690412008-10-01 00:29:53 -0400494 struct buffer_page *reader_page;
Steven Rostedt66a8cb92010-03-31 13:21:56 -0400495 unsigned long lost_events;
496 unsigned long last_overrun;
Steven Rostedt (VMware)8e012062018-02-07 17:26:32 -0500497 unsigned long nest;
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -0700498 local_t entries_bytes;
Steven Rostedte4906ef2009-04-30 20:49:44 -0400499 local_t entries;
Slava Pestov884bfe82011-07-15 14:23:58 -0700500 local_t overrun;
501 local_t commit_overrun;
502 local_t dropped_events;
Steven Rostedtfa743952009-06-16 12:37:57 -0400503 local_t committing;
504 local_t commits;
Steven Rostedt (VMware)2c2b0a72018-11-29 20:32:26 -0500505 local_t pages_touched;
506 local_t pages_read;
Steven Rostedt (VMware)03329f92018-11-29 21:38:42 -0500507 long last_pages_touch;
Steven Rostedt (VMware)2c2b0a72018-11-29 20:32:26 -0500508 size_t shortest_full;
Steven Rostedt77ae3652009-03-27 11:00:29 -0400509 unsigned long read;
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -0700510 unsigned long read_bytes;
Steven Rostedt (VMware)10464b42020-06-28 22:52:27 -0400511 rb_time_t write_stamp;
512 rb_time_t before_stamp;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400513 u64 read_stamp;
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -0800514 /* ring buffer pages to update, > 0 to add, < 0 to remove */
Steven Rostedt (Red Hat)9b94a8f2016-05-12 11:01:24 -0400515 long nr_pages_to_update;
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -0800516 struct list_head new_pages; /* new pages to add */
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -0700517 struct work_struct update_pages_work;
Vaibhav Nagarnaik05fdd702012-05-18 13:29:51 -0700518 struct completion update_done;
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -0500519
520 struct rb_irq_work irq_work;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400521};
522
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -0500523struct trace_buffer {
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400524 unsigned flags;
525 int cpus;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400526 atomic_t record_disabled;
Arnaldo Carvalho de Melo00f62f62009-02-09 17:04:06 -0200527 cpumask_var_t cpumask;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400528
Peter Zijlstra1f8a6a12009-06-08 18:18:39 +0200529 struct lock_class_key *reader_lock_key;
530
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400531 struct mutex mutex;
532
533 struct ring_buffer_per_cpu **buffers;
Steven Rostedt554f7862009-03-11 22:00:13 -0400534
Sebastian Andrzej Siewiorb32614c2016-11-27 00:13:34 +0100535 struct hlist_node node;
Steven Rostedt37886f62009-03-17 17:22:06 -0400536 u64 (*clock)(void);
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -0500537
538 struct rb_irq_work irq_work;
Tom Zanussi00b41452018-01-15 20:51:39 -0600539 bool time_stamp_abs;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400540};
541
542struct ring_buffer_iter {
543 struct ring_buffer_per_cpu *cpu_buffer;
544 unsigned long head;
Steven Rostedt (VMware)785888c2020-03-17 17:32:27 -0400545 unsigned long next_event;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400546 struct buffer_page *head_page;
Steven Rostedt492a74f2010-01-25 15:17:47 -0500547 struct buffer_page *cache_reader_page;
548 unsigned long cache_read;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400549 u64 read_stamp;
Steven Rostedt (VMware)28e3fc52020-03-17 17:32:26 -0400550 u64 page_stamp;
Steven Rostedt (VMware)785888c2020-03-17 17:32:27 -0400551 struct ring_buffer_event *event;
Steven Rostedt (VMware)c9b7a4a2020-03-17 17:32:32 -0400552 int missed_events;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400553};
554
Steven Rostedt (VMware)10464b42020-06-28 22:52:27 -0400555#ifdef RB_TIME_32
556
557/*
558 * On 32 bit machines, local64_t is very expensive. As the ring
559 * buffer doesn't need all the features of a true 64 bit atomic,
560 * on 32 bit, it uses these functions (64 still uses local64_t).
561 *
562 * For the ring buffer, 64 bit required operations for the time is
563 * the following:
564 *
565 * - Only need 59 bits (uses 60 to make it even).
566 * - Reads may fail if it interrupted a modification of the time stamp.
567 * It will succeed if it did not interrupt another write even if
568 * the read itself is interrupted by a write.
569 * It returns whether it was successful or not.
570 *
571 * - Writes always succeed and will overwrite other writes and writes
572 * that were done by events interrupting the current write.
573 *
574 * - A write followed by a read of the same time stamp will always succeed,
575 * but may not contain the same value.
576 *
577 * - A cmpxchg will fail if it interrupted another write or cmpxchg.
578 * Other than that, it acts like a normal cmpxchg.
579 *
580 * The 60 bit time stamp is broken up by 30 bits in a top and bottom half
581 * (bottom being the least significant 30 bits of the 60 bit time stamp).
582 *
583 * The two most significant bits of each half holds a 2 bit counter (0-3).
584 * Each update will increment this counter by one.
585 * When reading the top and bottom, if the two counter bits match then the
586 * top and bottom together make a valid 60 bit number.
587 */
588#define RB_TIME_SHIFT 30
589#define RB_TIME_VAL_MASK ((1 << RB_TIME_SHIFT) - 1)
590
591static inline int rb_time_cnt(unsigned long val)
592{
593 return (val >> RB_TIME_SHIFT) & 3;
594}
595
596static inline u64 rb_time_val(unsigned long top, unsigned long bottom)
597{
598 u64 val;
599
600 val = top & RB_TIME_VAL_MASK;
601 val <<= RB_TIME_SHIFT;
602 val |= bottom & RB_TIME_VAL_MASK;
603
604 return val;
605}
606
607static inline bool __rb_time_read(rb_time_t *t, u64 *ret, unsigned long *cnt)
608{
609 unsigned long top, bottom;
610 unsigned long c;
611
612 /*
613 * If the read is interrupted by a write, then the cnt will
614 * be different. Loop until both top and bottom have been read
615 * without interruption.
616 */
617 do {
618 c = local_read(&t->cnt);
619 top = local_read(&t->top);
620 bottom = local_read(&t->bottom);
621 } while (c != local_read(&t->cnt));
622
623 *cnt = rb_time_cnt(top);
624
625 /* If top and bottom counts don't match, this interrupted a write */
626 if (*cnt != rb_time_cnt(bottom))
627 return false;
628
629 *ret = rb_time_val(top, bottom);
630 return true;
631}
632
633static bool rb_time_read(rb_time_t *t, u64 *ret)
634{
635 unsigned long cnt;
636
637 return __rb_time_read(t, ret, &cnt);
638}
639
640static inline unsigned long rb_time_val_cnt(unsigned long val, unsigned long cnt)
641{
642 return (val & RB_TIME_VAL_MASK) | ((cnt & 3) << RB_TIME_SHIFT);
643}
644
645static inline void rb_time_split(u64 val, unsigned long *top, unsigned long *bottom)
646{
647 *top = (unsigned long)((val >> RB_TIME_SHIFT) & RB_TIME_VAL_MASK);
648 *bottom = (unsigned long)(val & RB_TIME_VAL_MASK);
649}
650
651static inline void rb_time_val_set(local_t *t, unsigned long val, unsigned long cnt)
652{
653 val = rb_time_val_cnt(val, cnt);
654 local_set(t, val);
655}
656
657static void rb_time_set(rb_time_t *t, u64 val)
658{
659 unsigned long cnt, top, bottom;
660
661 rb_time_split(val, &top, &bottom);
662
663 /* Writes always succeed with a valid number even if it gets interrupted. */
664 do {
665 cnt = local_inc_return(&t->cnt);
666 rb_time_val_set(&t->top, top, cnt);
667 rb_time_val_set(&t->bottom, bottom, cnt);
668 } while (cnt != local_read(&t->cnt));
669}
670
671static inline bool
672rb_time_read_cmpxchg(local_t *l, unsigned long expect, unsigned long set)
673{
674 unsigned long ret;
675
676 ret = local_cmpxchg(l, expect, set);
677 return ret == expect;
678}
679
680static int rb_time_cmpxchg(rb_time_t *t, u64 expect, u64 set)
681{
682 unsigned long cnt, top, bottom;
683 unsigned long cnt2, top2, bottom2;
684 u64 val;
685
686 /* The cmpxchg always fails if it interrupted an update */
687 if (!__rb_time_read(t, &val, &cnt2))
688 return false;
689
690 if (val != expect)
691 return false;
692
693 cnt = local_read(&t->cnt);
694 if ((cnt & 3) != cnt2)
695 return false;
696
697 cnt2 = cnt + 1;
698
699 rb_time_split(val, &top, &bottom);
700 top = rb_time_val_cnt(top, cnt);
701 bottom = rb_time_val_cnt(bottom, cnt);
702
703 rb_time_split(set, &top2, &bottom2);
704 top2 = rb_time_val_cnt(top2, cnt2);
705 bottom2 = rb_time_val_cnt(bottom2, cnt2);
706
707 if (!rb_time_read_cmpxchg(&t->cnt, cnt, cnt2))
708 return false;
709 if (!rb_time_read_cmpxchg(&t->top, top, top2))
710 return false;
711 if (!rb_time_read_cmpxchg(&t->bottom, bottom, bottom2))
712 return false;
713 return true;
714}
715
716#else /* 64 bits */
717
718/* local64_t always succeeds */
719
720static inline bool rb_time_read(rb_time_t *t, u64 *ret)
721{
722 *ret = local64_read(&t->time);
723 return true;
724}
725static void rb_time_set(rb_time_t *t, u64 val)
726{
727 local64_set(&t->time, val);
728}
729
730static bool rb_time_cmpxchg(rb_time_t *t, u64 expect, u64 set)
731{
732 u64 val;
733 val = local64_cmpxchg(&t->time, expect, set);
734 return val == expect;
735}
736#endif
737
Steven Rostedt (VMware)2c2b0a72018-11-29 20:32:26 -0500738/**
739 * ring_buffer_nr_pages - get the number of buffer pages in the ring buffer
740 * @buffer: The ring_buffer to get the number of pages from
741 * @cpu: The cpu of the ring_buffer to get the number of pages from
742 *
743 * Returns the number of pages used by a per_cpu buffer of the ring buffer.
744 */
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -0500745size_t ring_buffer_nr_pages(struct trace_buffer *buffer, int cpu)
Steven Rostedt (VMware)2c2b0a72018-11-29 20:32:26 -0500746{
747 return buffer->buffers[cpu]->nr_pages;
748}
749
750/**
751 * ring_buffer_nr_pages_dirty - get the number of used pages in the ring buffer
752 * @buffer: The ring_buffer to get the number of pages from
753 * @cpu: The cpu of the ring_buffer to get the number of pages from
754 *
755 * Returns the number of pages that have content in the ring buffer.
756 */
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -0500757size_t ring_buffer_nr_dirty_pages(struct trace_buffer *buffer, int cpu)
Steven Rostedt (VMware)2c2b0a72018-11-29 20:32:26 -0500758{
759 size_t read;
760 size_t cnt;
761
762 read = local_read(&buffer->buffers[cpu]->pages_read);
763 cnt = local_read(&buffer->buffers[cpu]->pages_touched);
764 /* The reader can read an empty page, but not more than that */
765 if (cnt < read) {
766 WARN_ON_ONCE(read > cnt + 1);
767 return 0;
768 }
769
770 return cnt - read;
771}
772
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -0500773/*
774 * rb_wake_up_waiters - wake up tasks waiting for ring buffer input
775 *
776 * Schedules a delayed work to wake up any task that is blocked on the
777 * ring buffer waiters queue.
778 */
779static void rb_wake_up_waiters(struct irq_work *work)
780{
781 struct rb_irq_work *rbwork = container_of(work, struct rb_irq_work, work);
782
783 wake_up_all(&rbwork->waiters);
Steven Rostedt (Red Hat)1e0d6712015-02-10 22:14:53 -0500784 if (rbwork->wakeup_full) {
785 rbwork->wakeup_full = false;
786 wake_up_all(&rbwork->full_waiters);
787 }
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -0500788}
789
790/**
791 * ring_buffer_wait - wait for input to the ring buffer
792 * @buffer: buffer to wait on
793 * @cpu: the cpu buffer to wait on
Rabin Vincente30f53a2014-11-10 19:46:34 +0100794 * @full: wait until a full page is available, if @cpu != RING_BUFFER_ALL_CPUS
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -0500795 *
796 * If @cpu == RING_BUFFER_ALL_CPUS then the task will wake up as soon
797 * as data is added to any of the @buffer's cpu buffers. Otherwise
798 * it will wait for data to be added to a specific cpu buffer.
799 */
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -0500800int ring_buffer_wait(struct trace_buffer *buffer, int cpu, int full)
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -0500801{
Rabin Vincente30f53a2014-11-10 19:46:34 +0100802 struct ring_buffer_per_cpu *uninitialized_var(cpu_buffer);
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -0500803 DEFINE_WAIT(wait);
804 struct rb_irq_work *work;
Rabin Vincente30f53a2014-11-10 19:46:34 +0100805 int ret = 0;
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -0500806
807 /*
808 * Depending on what the caller is waiting for, either any
809 * data in any cpu buffer, or a specific buffer, put the
810 * caller on the appropriate wait queue.
811 */
Steven Rostedt (Red Hat)1e0d6712015-02-10 22:14:53 -0500812 if (cpu == RING_BUFFER_ALL_CPUS) {
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -0500813 work = &buffer->irq_work;
Steven Rostedt (Red Hat)1e0d6712015-02-10 22:14:53 -0500814 /* Full only makes sense on per cpu reads */
Steven Rostedt (VMware)2c2b0a72018-11-29 20:32:26 -0500815 full = 0;
Steven Rostedt (Red Hat)1e0d6712015-02-10 22:14:53 -0500816 } else {
Steven Rostedt (Red Hat)8b8b3682014-06-10 09:46:00 -0400817 if (!cpumask_test_cpu(cpu, buffer->cpumask))
818 return -ENODEV;
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -0500819 cpu_buffer = buffer->buffers[cpu];
820 work = &cpu_buffer->irq_work;
821 }
822
823
Rabin Vincente30f53a2014-11-10 19:46:34 +0100824 while (true) {
Steven Rostedt (Red Hat)1e0d6712015-02-10 22:14:53 -0500825 if (full)
826 prepare_to_wait(&work->full_waiters, &wait, TASK_INTERRUPTIBLE);
827 else
828 prepare_to_wait(&work->waiters, &wait, TASK_INTERRUPTIBLE);
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -0500829
Rabin Vincente30f53a2014-11-10 19:46:34 +0100830 /*
831 * The events can happen in critical sections where
832 * checking a work queue can cause deadlocks.
833 * After adding a task to the queue, this flag is set
834 * only to notify events to try to wake up the queue
835 * using irq_work.
836 *
837 * We don't clear it even if the buffer is no longer
838 * empty. The flag only causes the next event to run
839 * irq_work to do the work queue wake up. The worse
840 * that can happen if we race with !trace_empty() is that
841 * an event will cause an irq_work to try to wake up
842 * an empty queue.
843 *
844 * There's no reason to protect this flag either, as
845 * the work queue and irq_work logic will do the necessary
846 * synchronization for the wake ups. The only thing
847 * that is necessary is that the wake up happens after
848 * a task has been queued. It's OK for spurious wake ups.
849 */
Steven Rostedt (Red Hat)1e0d6712015-02-10 22:14:53 -0500850 if (full)
851 work->full_waiters_pending = true;
852 else
853 work->waiters_pending = true;
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -0500854
Rabin Vincente30f53a2014-11-10 19:46:34 +0100855 if (signal_pending(current)) {
856 ret = -EINTR;
857 break;
858 }
859
860 if (cpu == RING_BUFFER_ALL_CPUS && !ring_buffer_empty(buffer))
861 break;
862
863 if (cpu != RING_BUFFER_ALL_CPUS &&
864 !ring_buffer_empty_cpu(buffer, cpu)) {
865 unsigned long flags;
866 bool pagebusy;
Steven Rostedt (VMware)2c2b0a72018-11-29 20:32:26 -0500867 size_t nr_pages;
868 size_t dirty;
Rabin Vincente30f53a2014-11-10 19:46:34 +0100869
870 if (!full)
871 break;
872
873 raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
874 pagebusy = cpu_buffer->reader_page == cpu_buffer->commit_page;
Steven Rostedt (VMware)2c2b0a72018-11-29 20:32:26 -0500875 nr_pages = cpu_buffer->nr_pages;
876 dirty = ring_buffer_nr_dirty_pages(buffer, cpu);
877 if (!cpu_buffer->shortest_full ||
878 cpu_buffer->shortest_full < full)
879 cpu_buffer->shortest_full = full;
Rabin Vincente30f53a2014-11-10 19:46:34 +0100880 raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
Steven Rostedt (VMware)2c2b0a72018-11-29 20:32:26 -0500881 if (!pagebusy &&
882 (!nr_pages || (dirty * 100) > full * nr_pages))
Rabin Vincente30f53a2014-11-10 19:46:34 +0100883 break;
884 }
885
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -0500886 schedule();
Rabin Vincente30f53a2014-11-10 19:46:34 +0100887 }
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -0500888
Steven Rostedt (Red Hat)1e0d6712015-02-10 22:14:53 -0500889 if (full)
890 finish_wait(&work->full_waiters, &wait);
891 else
892 finish_wait(&work->waiters, &wait);
Rabin Vincente30f53a2014-11-10 19:46:34 +0100893
894 return ret;
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -0500895}
896
897/**
898 * ring_buffer_poll_wait - poll on buffer input
899 * @buffer: buffer to wait on
900 * @cpu: the cpu buffer to wait on
901 * @filp: the file descriptor
902 * @poll_table: The poll descriptor
903 *
904 * If @cpu == RING_BUFFER_ALL_CPUS then the task will wake up as soon
905 * as data is added to any of the @buffer's cpu buffers. Otherwise
906 * it will wait for data to be added to a specific cpu buffer.
907 *
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800908 * Returns EPOLLIN | EPOLLRDNORM if data exists in the buffers,
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -0500909 * zero otherwise.
910 */
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -0500911__poll_t ring_buffer_poll_wait(struct trace_buffer *buffer, int cpu,
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -0500912 struct file *filp, poll_table *poll_table)
913{
914 struct ring_buffer_per_cpu *cpu_buffer;
915 struct rb_irq_work *work;
916
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -0500917 if (cpu == RING_BUFFER_ALL_CPUS)
918 work = &buffer->irq_work;
919 else {
Steven Rostedt (Red Hat)6721cb62013-05-23 14:21:36 -0400920 if (!cpumask_test_cpu(cpu, buffer->cpumask))
921 return -EINVAL;
922
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -0500923 cpu_buffer = buffer->buffers[cpu];
924 work = &cpu_buffer->irq_work;
925 }
926
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -0500927 poll_wait(filp, &work->waiters, poll_table);
Josef Bacik4ce97db2014-08-25 13:59:41 -0400928 work->waiters_pending = true;
929 /*
930 * There's a tight race between setting the waiters_pending and
931 * checking if the ring buffer is empty. Once the waiters_pending bit
932 * is set, the next event will wake the task up, but we can get stuck
933 * if there's only a single event in.
934 *
935 * FIXME: Ideally, we need a memory barrier on the writer side as well,
936 * but adding a memory barrier to all events will cause too much of a
937 * performance hit in the fast path. We only need a memory barrier when
938 * the buffer goes from empty to having content. But as this race is
939 * extremely small, and it's not a problem if another event comes in, we
940 * will fix it later.
941 */
942 smp_mb();
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -0500943
944 if ((cpu == RING_BUFFER_ALL_CPUS && !ring_buffer_empty(buffer)) ||
945 (cpu != RING_BUFFER_ALL_CPUS && !ring_buffer_empty_cpu(buffer, cpu)))
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800946 return EPOLLIN | EPOLLRDNORM;
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -0500947 return 0;
948}
949
Steven Rostedtf536aaf2008-11-10 23:07:30 -0500950/* buffer may be either ring_buffer or ring_buffer_per_cpu */
Steven Rostedt077c5402009-09-03 19:53:46 -0400951#define RB_WARN_ON(b, cond) \
952 ({ \
953 int _____ret = unlikely(cond); \
954 if (_____ret) { \
955 if (__same_type(*(b), struct ring_buffer_per_cpu)) { \
956 struct ring_buffer_per_cpu *__b = \
957 (void *)b; \
958 atomic_inc(&__b->buffer->record_disabled); \
959 } else \
960 atomic_inc(&b->record_disabled); \
961 WARN_ON(1); \
962 } \
963 _____ret; \
Steven Rostedt3e89c7bb2008-11-11 15:28:41 -0500964 })
Steven Rostedtf536aaf2008-11-10 23:07:30 -0500965
Steven Rostedt37886f62009-03-17 17:22:06 -0400966/* Up this if you want to test the TIME_EXTENTS and normalization */
967#define DEBUG_SHIFT 0
968
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -0500969static inline u64 rb_time_stamp(struct trace_buffer *buffer)
Steven Rostedt88eb0122009-05-11 16:28:23 -0400970{
971 /* shift to debug/test normalization and TIME_EXTENTS */
972 return buffer->clock() << DEBUG_SHIFT;
973}
974
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -0500975u64 ring_buffer_time_stamp(struct trace_buffer *buffer, int cpu)
Steven Rostedt37886f62009-03-17 17:22:06 -0400976{
977 u64 time;
978
979 preempt_disable_notrace();
Jiri Olsa6d3f1e12009-10-23 19:36:19 -0400980 time = rb_time_stamp(buffer);
Peter Zijlstrad6097c92019-04-23 22:03:18 +0200981 preempt_enable_notrace();
Steven Rostedt37886f62009-03-17 17:22:06 -0400982
983 return time;
984}
985EXPORT_SYMBOL_GPL(ring_buffer_time_stamp);
986
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -0500987void ring_buffer_normalize_time_stamp(struct trace_buffer *buffer,
Steven Rostedt37886f62009-03-17 17:22:06 -0400988 int cpu, u64 *ts)
989{
990 /* Just stupid testing the normalize function and deltas */
991 *ts >>= DEBUG_SHIFT;
992}
993EXPORT_SYMBOL_GPL(ring_buffer_normalize_time_stamp);
994
Steven Rostedt77ae3652009-03-27 11:00:29 -0400995/*
996 * Making the ring buffer lockless makes things tricky.
997 * Although writes only happen on the CPU that they are on,
998 * and they only need to worry about interrupts. Reads can
999 * happen on any CPU.
1000 *
1001 * The reader page is always off the ring buffer, but when the
1002 * reader finishes with a page, it needs to swap its page with
1003 * a new one from the buffer. The reader needs to take from
1004 * the head (writes go to the tail). But if a writer is in overwrite
1005 * mode and wraps, it must push the head page forward.
1006 *
1007 * Here lies the problem.
1008 *
1009 * The reader must be careful to replace only the head page, and
1010 * not another one. As described at the top of the file in the
1011 * ASCII art, the reader sets its old page to point to the next
1012 * page after head. It then sets the page after head to point to
1013 * the old reader page. But if the writer moves the head page
1014 * during this operation, the reader could end up with the tail.
1015 *
1016 * We use cmpxchg to help prevent this race. We also do something
1017 * special with the page before head. We set the LSB to 1.
1018 *
1019 * When the writer must push the page forward, it will clear the
1020 * bit that points to the head page, move the head, and then set
1021 * the bit that points to the new head page.
1022 *
1023 * We also don't want an interrupt coming in and moving the head
1024 * page on another writer. Thus we use the second LSB to catch
1025 * that too. Thus:
1026 *
1027 * head->list->prev->next bit 1 bit 0
1028 * ------- -------
1029 * Normal page 0 0
1030 * Points to head page 0 1
1031 * New head page 1 0
1032 *
1033 * Note we can not trust the prev pointer of the head page, because:
1034 *
1035 * +----+ +-----+ +-----+
1036 * | |------>| T |---X--->| N |
1037 * | |<------| | | |
1038 * +----+ +-----+ +-----+
1039 * ^ ^ |
1040 * | +-----+ | |
1041 * +----------| R |----------+ |
1042 * | |<-----------+
1043 * +-----+
1044 *
1045 * Key: ---X--> HEAD flag set in pointer
1046 * T Tail page
1047 * R Reader page
1048 * N Next page
1049 *
1050 * (see __rb_reserve_next() to see where this happens)
1051 *
1052 * What the above shows is that the reader just swapped out
1053 * the reader page with a page in the buffer, but before it
1054 * could make the new header point back to the new page added
1055 * it was preempted by a writer. The writer moved forward onto
1056 * the new page added by the reader and is about to move forward
1057 * again.
1058 *
1059 * You can see, it is legitimate for the previous pointer of
1060 * the head (or any page) not to point back to itself. But only
Steven Rostedt (VMware)6167c202018-05-16 11:17:06 -04001061 * temporarily.
Steven Rostedt77ae3652009-03-27 11:00:29 -04001062 */
1063
1064#define RB_PAGE_NORMAL 0UL
1065#define RB_PAGE_HEAD 1UL
1066#define RB_PAGE_UPDATE 2UL
1067
1068
1069#define RB_FLAG_MASK 3UL
1070
1071/* PAGE_MOVED is not part of the mask */
1072#define RB_PAGE_MOVED 4UL
1073
1074/*
1075 * rb_list_head - remove any bit
1076 */
1077static struct list_head *rb_list_head(struct list_head *list)
1078{
1079 unsigned long val = (unsigned long)list;
1080
1081 return (struct list_head *)(val & ~RB_FLAG_MASK);
1082}
1083
1084/*
Jiri Olsa6d3f1e12009-10-23 19:36:19 -04001085 * rb_is_head_page - test if the given page is the head page
Steven Rostedt77ae3652009-03-27 11:00:29 -04001086 *
1087 * Because the reader may move the head_page pointer, we can
1088 * not trust what the head page is (it may be pointing to
1089 * the reader page). But if the next page is a header page,
1090 * its flags will be non zero.
1091 */
Jesper Juhl42b16b32011-01-17 00:09:38 +01001092static inline int
Steven Rostedt77ae3652009-03-27 11:00:29 -04001093rb_is_head_page(struct ring_buffer_per_cpu *cpu_buffer,
1094 struct buffer_page *page, struct list_head *list)
1095{
1096 unsigned long val;
1097
1098 val = (unsigned long)list->next;
1099
1100 if ((val & ~RB_FLAG_MASK) != (unsigned long)&page->list)
1101 return RB_PAGE_MOVED;
1102
1103 return val & RB_FLAG_MASK;
1104}
1105
1106/*
1107 * rb_is_reader_page
1108 *
1109 * The unique thing about the reader page, is that, if the
1110 * writer is ever on it, the previous pointer never points
1111 * back to the reader page.
1112 */
Yaowei Bai06ca3202015-09-29 22:43:31 +08001113static bool rb_is_reader_page(struct buffer_page *page)
Steven Rostedt77ae3652009-03-27 11:00:29 -04001114{
1115 struct list_head *list = page->list.prev;
1116
1117 return rb_list_head(list->next) != &page->list;
1118}
1119
1120/*
1121 * rb_set_list_to_head - set a list_head to be pointing to head.
1122 */
1123static void rb_set_list_to_head(struct ring_buffer_per_cpu *cpu_buffer,
1124 struct list_head *list)
1125{
1126 unsigned long *ptr;
1127
1128 ptr = (unsigned long *)&list->next;
1129 *ptr |= RB_PAGE_HEAD;
1130 *ptr &= ~RB_PAGE_UPDATE;
1131}
1132
1133/*
1134 * rb_head_page_activate - sets up head page
1135 */
1136static void rb_head_page_activate(struct ring_buffer_per_cpu *cpu_buffer)
1137{
1138 struct buffer_page *head;
1139
1140 head = cpu_buffer->head_page;
1141 if (!head)
1142 return;
1143
1144 /*
1145 * Set the previous list pointer to have the HEAD flag.
1146 */
1147 rb_set_list_to_head(cpu_buffer, head->list.prev);
1148}
1149
1150static void rb_list_head_clear(struct list_head *list)
1151{
1152 unsigned long *ptr = (unsigned long *)&list->next;
1153
1154 *ptr &= ~RB_FLAG_MASK;
1155}
1156
1157/*
Steven Rostedt (VMware)6167c202018-05-16 11:17:06 -04001158 * rb_head_page_deactivate - clears head page ptr (for free list)
Steven Rostedt77ae3652009-03-27 11:00:29 -04001159 */
1160static void
1161rb_head_page_deactivate(struct ring_buffer_per_cpu *cpu_buffer)
1162{
1163 struct list_head *hd;
1164
1165 /* Go through the whole list and clear any pointers found. */
1166 rb_list_head_clear(cpu_buffer->pages);
1167
1168 list_for_each(hd, cpu_buffer->pages)
1169 rb_list_head_clear(hd);
1170}
1171
1172static int rb_head_page_set(struct ring_buffer_per_cpu *cpu_buffer,
1173 struct buffer_page *head,
1174 struct buffer_page *prev,
1175 int old_flag, int new_flag)
1176{
1177 struct list_head *list;
1178 unsigned long val = (unsigned long)&head->list;
1179 unsigned long ret;
1180
1181 list = &prev->list;
1182
1183 val &= ~RB_FLAG_MASK;
1184
Steven Rostedt08a40812009-09-14 09:31:35 -04001185 ret = cmpxchg((unsigned long *)&list->next,
1186 val | old_flag, val | new_flag);
Steven Rostedt77ae3652009-03-27 11:00:29 -04001187
1188 /* check if the reader took the page */
1189 if ((ret & ~RB_FLAG_MASK) != val)
1190 return RB_PAGE_MOVED;
1191
1192 return ret & RB_FLAG_MASK;
1193}
1194
1195static int rb_head_page_set_update(struct ring_buffer_per_cpu *cpu_buffer,
1196 struct buffer_page *head,
1197 struct buffer_page *prev,
1198 int old_flag)
1199{
1200 return rb_head_page_set(cpu_buffer, head, prev,
1201 old_flag, RB_PAGE_UPDATE);
1202}
1203
1204static int rb_head_page_set_head(struct ring_buffer_per_cpu *cpu_buffer,
1205 struct buffer_page *head,
1206 struct buffer_page *prev,
1207 int old_flag)
1208{
1209 return rb_head_page_set(cpu_buffer, head, prev,
1210 old_flag, RB_PAGE_HEAD);
1211}
1212
1213static int rb_head_page_set_normal(struct ring_buffer_per_cpu *cpu_buffer,
1214 struct buffer_page *head,
1215 struct buffer_page *prev,
1216 int old_flag)
1217{
1218 return rb_head_page_set(cpu_buffer, head, prev,
1219 old_flag, RB_PAGE_NORMAL);
1220}
1221
1222static inline void rb_inc_page(struct ring_buffer_per_cpu *cpu_buffer,
1223 struct buffer_page **bpage)
1224{
1225 struct list_head *p = rb_list_head((*bpage)->list.next);
1226
1227 *bpage = list_entry(p, struct buffer_page, list);
1228}
1229
1230static struct buffer_page *
1231rb_set_head_page(struct ring_buffer_per_cpu *cpu_buffer)
1232{
1233 struct buffer_page *head;
1234 struct buffer_page *page;
1235 struct list_head *list;
1236 int i;
1237
1238 if (RB_WARN_ON(cpu_buffer, !cpu_buffer->head_page))
1239 return NULL;
1240
1241 /* sanity check */
1242 list = cpu_buffer->pages;
1243 if (RB_WARN_ON(cpu_buffer, rb_list_head(list->prev->next) != list))
1244 return NULL;
1245
1246 page = head = cpu_buffer->head_page;
1247 /*
1248 * It is possible that the writer moves the header behind
1249 * where we started, and we miss in one loop.
1250 * A second loop should grab the header, but we'll do
1251 * three loops just because I'm paranoid.
1252 */
1253 for (i = 0; i < 3; i++) {
1254 do {
1255 if (rb_is_head_page(cpu_buffer, page, page->list.prev)) {
1256 cpu_buffer->head_page = page;
1257 return page;
1258 }
1259 rb_inc_page(cpu_buffer, &page);
1260 } while (page != head);
1261 }
1262
1263 RB_WARN_ON(cpu_buffer, 1);
1264
1265 return NULL;
1266}
1267
1268static int rb_head_page_replace(struct buffer_page *old,
1269 struct buffer_page *new)
1270{
1271 unsigned long *ptr = (unsigned long *)&old->list.prev->next;
1272 unsigned long val;
1273 unsigned long ret;
1274
1275 val = *ptr & ~RB_FLAG_MASK;
1276 val |= RB_PAGE_HEAD;
1277
Steven Rostedt08a40812009-09-14 09:31:35 -04001278 ret = cmpxchg(ptr, val, (unsigned long)&new->list);
Steven Rostedt77ae3652009-03-27 11:00:29 -04001279
1280 return ret == val;
1281}
1282
1283/*
1284 * rb_tail_page_update - move the tail page forward
Steven Rostedt77ae3652009-03-27 11:00:29 -04001285 */
Steven Rostedt (Red Hat)70004982015-11-17 15:15:19 -05001286static void rb_tail_page_update(struct ring_buffer_per_cpu *cpu_buffer,
Steven Rostedt77ae3652009-03-27 11:00:29 -04001287 struct buffer_page *tail_page,
1288 struct buffer_page *next_page)
1289{
Steven Rostedt77ae3652009-03-27 11:00:29 -04001290 unsigned long old_entries;
1291 unsigned long old_write;
Steven Rostedt77ae3652009-03-27 11:00:29 -04001292
1293 /*
1294 * The tail page now needs to be moved forward.
1295 *
1296 * We need to reset the tail page, but without messing
1297 * with possible erasing of data brought in by interrupts
1298 * that have moved the tail page and are currently on it.
1299 *
1300 * We add a counter to the write field to denote this.
1301 */
1302 old_write = local_add_return(RB_WRITE_INTCNT, &next_page->write);
1303 old_entries = local_add_return(RB_WRITE_INTCNT, &next_page->entries);
1304
Steven Rostedt (VMware)2c2b0a72018-11-29 20:32:26 -05001305 local_inc(&cpu_buffer->pages_touched);
Steven Rostedt77ae3652009-03-27 11:00:29 -04001306 /*
1307 * Just make sure we have seen our old_write and synchronize
1308 * with any interrupts that come in.
1309 */
1310 barrier();
1311
1312 /*
1313 * If the tail page is still the same as what we think
1314 * it is, then it is up to us to update the tail
1315 * pointer.
1316 */
Steven Rostedt (Red Hat)85736362015-11-17 14:03:11 -05001317 if (tail_page == READ_ONCE(cpu_buffer->tail_page)) {
Steven Rostedt77ae3652009-03-27 11:00:29 -04001318 /* Zero the write counter */
1319 unsigned long val = old_write & ~RB_WRITE_MASK;
1320 unsigned long eval = old_entries & ~RB_WRITE_MASK;
1321
1322 /*
1323 * This will only succeed if an interrupt did
1324 * not come in and change it. In which case, we
1325 * do not want to modify it.
Lai Jiangshanda706d82009-07-15 16:27:30 +08001326 *
1327 * We add (void) to let the compiler know that we do not care
1328 * about the return value of these functions. We use the
1329 * cmpxchg to only update if an interrupt did not already
1330 * do it for us. If the cmpxchg fails, we don't care.
Steven Rostedt77ae3652009-03-27 11:00:29 -04001331 */
Lai Jiangshanda706d82009-07-15 16:27:30 +08001332 (void)local_cmpxchg(&next_page->write, old_write, val);
1333 (void)local_cmpxchg(&next_page->entries, old_entries, eval);
Steven Rostedt77ae3652009-03-27 11:00:29 -04001334
1335 /*
1336 * No need to worry about races with clearing out the commit.
1337 * it only can increment when a commit takes place. But that
1338 * only happens in the outer most nested commit.
1339 */
1340 local_set(&next_page->page->commit, 0);
1341
Steven Rostedt (Red Hat)70004982015-11-17 15:15:19 -05001342 /* Again, either we update tail_page or an interrupt does */
1343 (void)cmpxchg(&cpu_buffer->tail_page, tail_page, next_page);
Steven Rostedt77ae3652009-03-27 11:00:29 -04001344 }
Steven Rostedt77ae3652009-03-27 11:00:29 -04001345}
1346
1347static int rb_check_bpage(struct ring_buffer_per_cpu *cpu_buffer,
1348 struct buffer_page *bpage)
1349{
1350 unsigned long val = (unsigned long)bpage;
1351
1352 if (RB_WARN_ON(cpu_buffer, val & RB_FLAG_MASK))
1353 return 1;
1354
1355 return 0;
1356}
1357
1358/**
1359 * rb_check_list - make sure a pointer to a list has the last bits zero
1360 */
1361static int rb_check_list(struct ring_buffer_per_cpu *cpu_buffer,
1362 struct list_head *list)
1363{
1364 if (RB_WARN_ON(cpu_buffer, rb_list_head(list->prev) != list->prev))
1365 return 1;
1366 if (RB_WARN_ON(cpu_buffer, rb_list_head(list->next) != list->next))
1367 return 1;
1368 return 0;
1369}
1370
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001371/**
zhangwei(Jovi)d6118512013-07-15 16:32:50 +08001372 * rb_check_pages - integrity check of buffer pages
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001373 * @cpu_buffer: CPU buffer with pages to test
1374 *
Wenji Huangc3706f02009-02-10 01:03:18 -05001375 * As a safety measure we check to make sure the data pages have not
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001376 * been corrupted.
1377 */
1378static int rb_check_pages(struct ring_buffer_per_cpu *cpu_buffer)
1379{
Steven Rostedt3adc54f2009-03-30 15:32:01 -04001380 struct list_head *head = cpu_buffer->pages;
Steven Rostedt044fa782008-12-02 23:50:03 -05001381 struct buffer_page *bpage, *tmp;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001382
Steven Rostedt308f7ee2012-05-16 19:46:32 -04001383 /* Reset the head page if it exists */
1384 if (cpu_buffer->head_page)
1385 rb_set_head_page(cpu_buffer);
1386
Steven Rostedt77ae3652009-03-27 11:00:29 -04001387 rb_head_page_deactivate(cpu_buffer);
1388
Steven Rostedt3e89c7bb2008-11-11 15:28:41 -05001389 if (RB_WARN_ON(cpu_buffer, head->next->prev != head))
1390 return -1;
1391 if (RB_WARN_ON(cpu_buffer, head->prev->next != head))
1392 return -1;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001393
Steven Rostedt77ae3652009-03-27 11:00:29 -04001394 if (rb_check_list(cpu_buffer, head))
1395 return -1;
1396
Steven Rostedt044fa782008-12-02 23:50:03 -05001397 list_for_each_entry_safe(bpage, tmp, head, list) {
Steven Rostedt3e89c7bb2008-11-11 15:28:41 -05001398 if (RB_WARN_ON(cpu_buffer,
Steven Rostedt044fa782008-12-02 23:50:03 -05001399 bpage->list.next->prev != &bpage->list))
Steven Rostedt3e89c7bb2008-11-11 15:28:41 -05001400 return -1;
1401 if (RB_WARN_ON(cpu_buffer,
Steven Rostedt044fa782008-12-02 23:50:03 -05001402 bpage->list.prev->next != &bpage->list))
Steven Rostedt3e89c7bb2008-11-11 15:28:41 -05001403 return -1;
Steven Rostedt77ae3652009-03-27 11:00:29 -04001404 if (rb_check_list(cpu_buffer, &bpage->list))
1405 return -1;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001406 }
1407
Steven Rostedt77ae3652009-03-27 11:00:29 -04001408 rb_head_page_activate(cpu_buffer);
1409
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001410 return 0;
1411}
1412
Steven Rostedt (Red Hat)9b94a8f2016-05-12 11:01:24 -04001413static int __rb_allocate_pages(long nr_pages, struct list_head *pages, int cpu)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001414{
Steven Rostedt044fa782008-12-02 23:50:03 -05001415 struct buffer_page *bpage, *tmp;
Steven Rostedt (VMware)927e56d2018-04-04 11:29:57 -04001416 bool user_thread = current->mm != NULL;
1417 gfp_t mflags;
Steven Rostedt (Red Hat)9b94a8f2016-05-12 11:01:24 -04001418 long i;
Steven Rostedt3adc54f2009-03-30 15:32:01 -04001419
Steven Rostedt (VMware)927e56d2018-04-04 11:29:57 -04001420 /*
1421 * Check if the available memory is there first.
1422 * Note, si_mem_available() only gives us a rough estimate of available
1423 * memory. It may not be accurate. But we don't care, we just want
1424 * to prevent doing any allocation when it is obvious that it is
1425 * not going to succeed.
1426 */
Steven Rostedt (VMware)2a872fa2018-04-02 10:33:56 -04001427 i = si_mem_available();
1428 if (i < nr_pages)
1429 return -ENOMEM;
1430
Steven Rostedt (VMware)927e56d2018-04-04 11:29:57 -04001431 /*
1432 * __GFP_RETRY_MAYFAIL flag makes sure that the allocation fails
1433 * gracefully without invoking oom-killer and the system is not
1434 * destabilized.
1435 */
1436 mflags = GFP_KERNEL | __GFP_RETRY_MAYFAIL;
1437
1438 /*
1439 * If a user thread allocates too much, and si_mem_available()
1440 * reports there's enough memory, even though there is not.
1441 * Make sure the OOM killer kills this thread. This can happen
1442 * even with RETRY_MAYFAIL because another task may be doing
1443 * an allocation after this task has taken all memory.
1444 * This is the task the OOM killer needs to take out during this
1445 * loop, even if it was triggered by an allocation somewhere else.
1446 */
1447 if (user_thread)
1448 set_current_oom_origin();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001449 for (i = 0; i < nr_pages; i++) {
Vaibhav Nagarnaik7ea59062011-05-03 17:56:42 -07001450 struct page *page;
Steven Rostedt (VMware)927e56d2018-04-04 11:29:57 -04001451
Steven Rostedt044fa782008-12-02 23:50:03 -05001452 bpage = kzalloc_node(ALIGN(sizeof(*bpage), cache_line_size()),
Steven Rostedt (VMware)927e56d2018-04-04 11:29:57 -04001453 mflags, cpu_to_node(cpu));
Steven Rostedt044fa782008-12-02 23:50:03 -05001454 if (!bpage)
Steven Rostedte4c2ce82008-10-01 11:14:54 -04001455 goto free_pages;
Steven Rostedt77ae3652009-03-27 11:00:29 -04001456
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001457 list_add(&bpage->list, pages);
Steven Rostedt77ae3652009-03-27 11:00:29 -04001458
Steven Rostedt (VMware)927e56d2018-04-04 11:29:57 -04001459 page = alloc_pages_node(cpu_to_node(cpu), mflags, 0);
Vaibhav Nagarnaik7ea59062011-05-03 17:56:42 -07001460 if (!page)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001461 goto free_pages;
Vaibhav Nagarnaik7ea59062011-05-03 17:56:42 -07001462 bpage->page = page_address(page);
Steven Rostedt044fa782008-12-02 23:50:03 -05001463 rb_init_page(bpage->page);
Steven Rostedt (VMware)927e56d2018-04-04 11:29:57 -04001464
1465 if (user_thread && fatal_signal_pending(current))
1466 goto free_pages;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001467 }
Steven Rostedt (VMware)927e56d2018-04-04 11:29:57 -04001468 if (user_thread)
1469 clear_current_oom_origin();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001470
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001471 return 0;
1472
1473free_pages:
1474 list_for_each_entry_safe(bpage, tmp, pages, list) {
1475 list_del_init(&bpage->list);
1476 free_buffer_page(bpage);
1477 }
Steven Rostedt (VMware)927e56d2018-04-04 11:29:57 -04001478 if (user_thread)
1479 clear_current_oom_origin();
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001480
1481 return -ENOMEM;
1482}
1483
1484static int rb_allocate_pages(struct ring_buffer_per_cpu *cpu_buffer,
Steven Rostedt (Red Hat)9b94a8f2016-05-12 11:01:24 -04001485 unsigned long nr_pages)
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001486{
1487 LIST_HEAD(pages);
1488
1489 WARN_ON(!nr_pages);
1490
1491 if (__rb_allocate_pages(nr_pages, &pages, cpu_buffer->cpu))
1492 return -ENOMEM;
1493
Steven Rostedt3adc54f2009-03-30 15:32:01 -04001494 /*
1495 * The ring buffer page list is a circular list that does not
1496 * start and end with a list head. All page list items point to
1497 * other pages.
1498 */
1499 cpu_buffer->pages = pages.next;
1500 list_del(&pages);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001501
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001502 cpu_buffer->nr_pages = nr_pages;
1503
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001504 rb_check_pages(cpu_buffer);
1505
1506 return 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001507}
1508
1509static struct ring_buffer_per_cpu *
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05001510rb_allocate_cpu_buffer(struct trace_buffer *buffer, long nr_pages, int cpu)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001511{
1512 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedt044fa782008-12-02 23:50:03 -05001513 struct buffer_page *bpage;
Vaibhav Nagarnaik7ea59062011-05-03 17:56:42 -07001514 struct page *page;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001515 int ret;
1516
1517 cpu_buffer = kzalloc_node(ALIGN(sizeof(*cpu_buffer), cache_line_size()),
1518 GFP_KERNEL, cpu_to_node(cpu));
1519 if (!cpu_buffer)
1520 return NULL;
1521
1522 cpu_buffer->cpu = cpu;
1523 cpu_buffer->buffer = buffer;
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02001524 raw_spin_lock_init(&cpu_buffer->reader_lock);
Peter Zijlstra1f8a6a12009-06-08 18:18:39 +02001525 lockdep_set_class(&cpu_buffer->reader_lock, buffer->reader_lock_key);
Thomas Gleixneredc35bd2009-12-03 12:38:57 +01001526 cpu_buffer->lock = (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001527 INIT_WORK(&cpu_buffer->update_pages_work, update_pages_handler);
Vaibhav Nagarnaik05fdd702012-05-18 13:29:51 -07001528 init_completion(&cpu_buffer->update_done);
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -05001529 init_irq_work(&cpu_buffer->irq_work.work, rb_wake_up_waiters);
Steven Rostedt (Red Hat)f1dc6722013-03-04 17:33:05 -05001530 init_waitqueue_head(&cpu_buffer->irq_work.waiters);
Steven Rostedt (Red Hat)1e0d6712015-02-10 22:14:53 -05001531 init_waitqueue_head(&cpu_buffer->irq_work.full_waiters);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001532
Steven Rostedt044fa782008-12-02 23:50:03 -05001533 bpage = kzalloc_node(ALIGN(sizeof(*bpage), cache_line_size()),
Steven Rostedte4c2ce82008-10-01 11:14:54 -04001534 GFP_KERNEL, cpu_to_node(cpu));
Steven Rostedt044fa782008-12-02 23:50:03 -05001535 if (!bpage)
Steven Rostedte4c2ce82008-10-01 11:14:54 -04001536 goto fail_free_buffer;
1537
Steven Rostedt77ae3652009-03-27 11:00:29 -04001538 rb_check_bpage(cpu_buffer, bpage);
1539
Steven Rostedt044fa782008-12-02 23:50:03 -05001540 cpu_buffer->reader_page = bpage;
Vaibhav Nagarnaik7ea59062011-05-03 17:56:42 -07001541 page = alloc_pages_node(cpu_to_node(cpu), GFP_KERNEL, 0);
1542 if (!page)
Steven Rostedte4c2ce82008-10-01 11:14:54 -04001543 goto fail_free_reader;
Vaibhav Nagarnaik7ea59062011-05-03 17:56:42 -07001544 bpage->page = page_address(page);
Steven Rostedt044fa782008-12-02 23:50:03 -05001545 rb_init_page(bpage->page);
Steven Rostedte4c2ce82008-10-01 11:14:54 -04001546
Steven Rostedtd7690412008-10-01 00:29:53 -04001547 INIT_LIST_HEAD(&cpu_buffer->reader_page->list);
Vaibhav Nagarnaik44b99462012-06-22 11:50:05 -07001548 INIT_LIST_HEAD(&cpu_buffer->new_pages);
Steven Rostedtd7690412008-10-01 00:29:53 -04001549
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001550 ret = rb_allocate_pages(cpu_buffer, nr_pages);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001551 if (ret < 0)
Steven Rostedtd7690412008-10-01 00:29:53 -04001552 goto fail_free_reader;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001553
1554 cpu_buffer->head_page
Steven Rostedt3adc54f2009-03-30 15:32:01 -04001555 = list_entry(cpu_buffer->pages, struct buffer_page, list);
Steven Rostedtbf41a152008-10-04 02:00:59 -04001556 cpu_buffer->tail_page = cpu_buffer->commit_page = cpu_buffer->head_page;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001557
Steven Rostedt77ae3652009-03-27 11:00:29 -04001558 rb_head_page_activate(cpu_buffer);
1559
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001560 return cpu_buffer;
1561
Steven Rostedtd7690412008-10-01 00:29:53 -04001562 fail_free_reader:
1563 free_buffer_page(cpu_buffer->reader_page);
1564
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001565 fail_free_buffer:
1566 kfree(cpu_buffer);
1567 return NULL;
1568}
1569
1570static void rb_free_cpu_buffer(struct ring_buffer_per_cpu *cpu_buffer)
1571{
Steven Rostedt3adc54f2009-03-30 15:32:01 -04001572 struct list_head *head = cpu_buffer->pages;
Steven Rostedt044fa782008-12-02 23:50:03 -05001573 struct buffer_page *bpage, *tmp;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001574
Steven Rostedtd7690412008-10-01 00:29:53 -04001575 free_buffer_page(cpu_buffer->reader_page);
1576
Steven Rostedt77ae3652009-03-27 11:00:29 -04001577 rb_head_page_deactivate(cpu_buffer);
1578
Steven Rostedt3adc54f2009-03-30 15:32:01 -04001579 if (head) {
1580 list_for_each_entry_safe(bpage, tmp, head, list) {
1581 list_del_init(&bpage->list);
1582 free_buffer_page(bpage);
1583 }
1584 bpage = list_entry(head, struct buffer_page, list);
Steven Rostedt044fa782008-12-02 23:50:03 -05001585 free_buffer_page(bpage);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001586 }
Steven Rostedt3adc54f2009-03-30 15:32:01 -04001587
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001588 kfree(cpu_buffer);
1589}
1590
1591/**
zhangwei(Jovi)d6118512013-07-15 16:32:50 +08001592 * __ring_buffer_alloc - allocate a new ring_buffer
Robert Richter68814b52008-11-24 12:24:12 +01001593 * @size: the size in bytes per cpu that is needed.
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001594 * @flags: attributes to set for the ring buffer.
Fabian Frederick59e7cff2014-06-05 20:22:05 +02001595 * @key: ring buffer reader_lock_key.
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001596 *
1597 * Currently the only flag that is available is the RB_FL_OVERWRITE
1598 * flag. This flag means that the buffer will overwrite old data
1599 * when the buffer wraps. If this flag is not set, the buffer will
1600 * drop data when the tail hits the head.
1601 */
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05001602struct trace_buffer *__ring_buffer_alloc(unsigned long size, unsigned flags,
Peter Zijlstra1f8a6a12009-06-08 18:18:39 +02001603 struct lock_class_key *key)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001604{
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05001605 struct trace_buffer *buffer;
Steven Rostedt (Red Hat)9b94a8f2016-05-12 11:01:24 -04001606 long nr_pages;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001607 int bsize;
Steven Rostedt (Red Hat)9b94a8f2016-05-12 11:01:24 -04001608 int cpu;
Sebastian Andrzej Siewiorb32614c2016-11-27 00:13:34 +01001609 int ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001610
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001611 /* keep it in its own cache line */
1612 buffer = kzalloc(ALIGN(sizeof(*buffer), cache_line_size()),
1613 GFP_KERNEL);
1614 if (!buffer)
1615 return NULL;
1616
Sebastian Andrzej Siewiorb18cc3d2016-12-07 14:31:33 +01001617 if (!zalloc_cpumask_var(&buffer->cpumask, GFP_KERNEL))
Rusty Russell9e01c1b2009-01-01 10:12:22 +10301618 goto fail_free_buffer;
1619
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001620 nr_pages = DIV_ROUND_UP(size, BUF_PAGE_SIZE);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001621 buffer->flags = flags;
Steven Rostedt37886f62009-03-17 17:22:06 -04001622 buffer->clock = trace_clock_local;
Peter Zijlstra1f8a6a12009-06-08 18:18:39 +02001623 buffer->reader_lock_key = key;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001624
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -05001625 init_irq_work(&buffer->irq_work.work, rb_wake_up_waiters);
Steven Rostedt (Red Hat)f1dc6722013-03-04 17:33:05 -05001626 init_waitqueue_head(&buffer->irq_work.waiters);
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -05001627
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001628 /* need at least two pages */
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001629 if (nr_pages < 2)
1630 nr_pages = 2;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001631
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001632 buffer->cpus = nr_cpu_ids;
1633
1634 bsize = sizeof(void *) * nr_cpu_ids;
1635 buffer->buffers = kzalloc(ALIGN(bsize, cache_line_size()),
1636 GFP_KERNEL);
1637 if (!buffer->buffers)
Rusty Russell9e01c1b2009-01-01 10:12:22 +10301638 goto fail_free_cpumask;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001639
Sebastian Andrzej Siewiorb32614c2016-11-27 00:13:34 +01001640 cpu = raw_smp_processor_id();
1641 cpumask_set_cpu(cpu, buffer->cpumask);
1642 buffer->buffers[cpu] = rb_allocate_cpu_buffer(buffer, nr_pages, cpu);
1643 if (!buffer->buffers[cpu])
1644 goto fail_free_buffers;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001645
Sebastian Andrzej Siewiorb32614c2016-11-27 00:13:34 +01001646 ret = cpuhp_state_add_instance(CPUHP_TRACE_RB_PREPARE, &buffer->node);
1647 if (ret < 0)
1648 goto fail_free_buffers;
Steven Rostedt554f7862009-03-11 22:00:13 -04001649
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001650 mutex_init(&buffer->mutex);
1651
1652 return buffer;
1653
1654 fail_free_buffers:
1655 for_each_buffer_cpu(buffer, cpu) {
1656 if (buffer->buffers[cpu])
1657 rb_free_cpu_buffer(buffer->buffers[cpu]);
1658 }
1659 kfree(buffer->buffers);
1660
Rusty Russell9e01c1b2009-01-01 10:12:22 +10301661 fail_free_cpumask:
1662 free_cpumask_var(buffer->cpumask);
1663
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001664 fail_free_buffer:
1665 kfree(buffer);
1666 return NULL;
1667}
Peter Zijlstra1f8a6a12009-06-08 18:18:39 +02001668EXPORT_SYMBOL_GPL(__ring_buffer_alloc);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001669
1670/**
1671 * ring_buffer_free - free a ring buffer.
1672 * @buffer: the buffer to free.
1673 */
1674void
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05001675ring_buffer_free(struct trace_buffer *buffer)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001676{
1677 int cpu;
1678
Sebastian Andrzej Siewiorb32614c2016-11-27 00:13:34 +01001679 cpuhp_state_remove_instance(CPUHP_TRACE_RB_PREPARE, &buffer->node);
Steven Rostedt554f7862009-03-11 22:00:13 -04001680
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001681 for_each_buffer_cpu(buffer, cpu)
1682 rb_free_cpu_buffer(buffer->buffers[cpu]);
1683
Eric Dumazetbd3f0222009-08-07 12:49:29 +02001684 kfree(buffer->buffers);
Rusty Russell9e01c1b2009-01-01 10:12:22 +10301685 free_cpumask_var(buffer->cpumask);
1686
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001687 kfree(buffer);
1688}
Robert Richterc4f50182008-12-11 16:49:22 +01001689EXPORT_SYMBOL_GPL(ring_buffer_free);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001690
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05001691void ring_buffer_set_clock(struct trace_buffer *buffer,
Steven Rostedt37886f62009-03-17 17:22:06 -04001692 u64 (*clock)(void))
1693{
1694 buffer->clock = clock;
1695}
1696
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05001697void ring_buffer_set_time_stamp_abs(struct trace_buffer *buffer, bool abs)
Tom Zanussi00b41452018-01-15 20:51:39 -06001698{
1699 buffer->time_stamp_abs = abs;
1700}
1701
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05001702bool ring_buffer_time_stamp_abs(struct trace_buffer *buffer)
Tom Zanussi00b41452018-01-15 20:51:39 -06001703{
1704 return buffer->time_stamp_abs;
1705}
1706
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001707static void rb_reset_cpu(struct ring_buffer_per_cpu *cpu_buffer);
1708
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001709static inline unsigned long rb_page_entries(struct buffer_page *bpage)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001710{
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001711 return local_read(&bpage->entries) & RB_WRITE_MASK;
1712}
1713
1714static inline unsigned long rb_page_write(struct buffer_page *bpage)
1715{
1716 return local_read(&bpage->write) & RB_WRITE_MASK;
1717}
1718
Vaibhav Nagarnaik5040b4b2012-05-03 18:59:51 -07001719static int
Steven Rostedt (Red Hat)9b94a8f2016-05-12 11:01:24 -04001720rb_remove_pages(struct ring_buffer_per_cpu *cpu_buffer, unsigned long nr_pages)
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001721{
1722 struct list_head *tail_page, *to_remove, *next_page;
1723 struct buffer_page *to_remove_page, *tmp_iter_page;
1724 struct buffer_page *last_page, *first_page;
Steven Rostedt (Red Hat)9b94a8f2016-05-12 11:01:24 -04001725 unsigned long nr_removed;
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001726 unsigned long head_bit;
1727 int page_entries;
1728
1729 head_bit = 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001730
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02001731 raw_spin_lock_irq(&cpu_buffer->reader_lock);
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001732 atomic_inc(&cpu_buffer->record_disabled);
1733 /*
1734 * We don't race with the readers since we have acquired the reader
1735 * lock. We also don't race with writers after disabling recording.
1736 * This makes it easy to figure out the first and the last page to be
1737 * removed from the list. We unlink all the pages in between including
1738 * the first and last pages. This is done in a busy loop so that we
1739 * lose the least number of traces.
1740 * The pages are freed after we restart recording and unlock readers.
1741 */
1742 tail_page = &cpu_buffer->tail_page->list;
Steven Rostedt77ae3652009-03-27 11:00:29 -04001743
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001744 /*
1745 * tail page might be on reader page, we remove the next page
1746 * from the ring buffer
1747 */
1748 if (cpu_buffer->tail_page == cpu_buffer->reader_page)
1749 tail_page = rb_list_head(tail_page->next);
1750 to_remove = tail_page;
1751
1752 /* start of pages to remove */
1753 first_page = list_entry(rb_list_head(to_remove->next),
1754 struct buffer_page, list);
1755
1756 for (nr_removed = 0; nr_removed < nr_pages; nr_removed++) {
1757 to_remove = rb_list_head(to_remove)->next;
1758 head_bit |= (unsigned long)to_remove & RB_PAGE_HEAD;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001759 }
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001760
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001761 next_page = rb_list_head(to_remove)->next;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001762
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001763 /*
1764 * Now we remove all pages between tail_page and next_page.
1765 * Make sure that we have head_bit value preserved for the
1766 * next page
1767 */
1768 tail_page->next = (struct list_head *)((unsigned long)next_page |
1769 head_bit);
1770 next_page = rb_list_head(next_page);
1771 next_page->prev = tail_page;
1772
1773 /* make sure pages points to a valid page in the ring buffer */
1774 cpu_buffer->pages = next_page;
1775
1776 /* update head page */
1777 if (head_bit)
1778 cpu_buffer->head_page = list_entry(next_page,
1779 struct buffer_page, list);
1780
1781 /*
1782 * change read pointer to make sure any read iterators reset
1783 * themselves
1784 */
1785 cpu_buffer->read = 0;
1786
1787 /* pages are removed, resume tracing and then free the pages */
1788 atomic_dec(&cpu_buffer->record_disabled);
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02001789 raw_spin_unlock_irq(&cpu_buffer->reader_lock);
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001790
1791 RB_WARN_ON(cpu_buffer, list_empty(cpu_buffer->pages));
1792
1793 /* last buffer page to remove */
1794 last_page = list_entry(rb_list_head(to_remove), struct buffer_page,
1795 list);
1796 tmp_iter_page = first_page;
1797
1798 do {
Vaibhav Nagarnaik83f36552018-09-07 15:31:29 -07001799 cond_resched();
1800
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001801 to_remove_page = tmp_iter_page;
1802 rb_inc_page(cpu_buffer, &tmp_iter_page);
1803
1804 /* update the counters */
1805 page_entries = rb_page_entries(to_remove_page);
1806 if (page_entries) {
1807 /*
1808 * If something was added to this page, it was full
1809 * since it is not the tail page. So we deduct the
1810 * bytes consumed in ring buffer from here.
Vaibhav Nagarnaik48fdc722012-06-29 12:31:41 -07001811 * Increment overrun to account for the lost events.
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001812 */
Vaibhav Nagarnaik48fdc722012-06-29 12:31:41 -07001813 local_add(page_entries, &cpu_buffer->overrun);
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001814 local_sub(BUF_PAGE_SIZE, &cpu_buffer->entries_bytes);
1815 }
1816
1817 /*
1818 * We have already removed references to this list item, just
1819 * free up the buffer_page and its page
1820 */
1821 free_buffer_page(to_remove_page);
1822 nr_removed--;
1823
1824 } while (to_remove_page != last_page);
1825
1826 RB_WARN_ON(cpu_buffer, nr_removed);
Vaibhav Nagarnaik5040b4b2012-05-03 18:59:51 -07001827
1828 return nr_removed == 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001829}
1830
Vaibhav Nagarnaik5040b4b2012-05-03 18:59:51 -07001831static int
1832rb_insert_pages(struct ring_buffer_per_cpu *cpu_buffer)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001833{
Vaibhav Nagarnaik5040b4b2012-05-03 18:59:51 -07001834 struct list_head *pages = &cpu_buffer->new_pages;
1835 int retries, success;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001836
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02001837 raw_spin_lock_irq(&cpu_buffer->reader_lock);
Vaibhav Nagarnaik5040b4b2012-05-03 18:59:51 -07001838 /*
1839 * We are holding the reader lock, so the reader page won't be swapped
1840 * in the ring buffer. Now we are racing with the writer trying to
1841 * move head page and the tail page.
1842 * We are going to adapt the reader page update process where:
1843 * 1. We first splice the start and end of list of new pages between
1844 * the head page and its previous page.
1845 * 2. We cmpxchg the prev_page->next to point from head page to the
1846 * start of new pages list.
1847 * 3. Finally, we update the head->prev to the end of new list.
1848 *
1849 * We will try this process 10 times, to make sure that we don't keep
1850 * spinning.
1851 */
1852 retries = 10;
1853 success = 0;
1854 while (retries--) {
1855 struct list_head *head_page, *prev_page, *r;
1856 struct list_head *last_page, *first_page;
1857 struct list_head *head_page_with_bit;
Steven Rostedt77ae3652009-03-27 11:00:29 -04001858
Vaibhav Nagarnaik5040b4b2012-05-03 18:59:51 -07001859 head_page = &rb_set_head_page(cpu_buffer)->list;
Steven Rostedt54f7be52012-11-29 22:27:22 -05001860 if (!head_page)
1861 break;
Vaibhav Nagarnaik5040b4b2012-05-03 18:59:51 -07001862 prev_page = head_page->prev;
1863
1864 first_page = pages->next;
1865 last_page = pages->prev;
1866
1867 head_page_with_bit = (struct list_head *)
1868 ((unsigned long)head_page | RB_PAGE_HEAD);
1869
1870 last_page->next = head_page_with_bit;
1871 first_page->prev = prev_page;
1872
1873 r = cmpxchg(&prev_page->next, head_page_with_bit, first_page);
1874
1875 if (r == head_page_with_bit) {
1876 /*
1877 * yay, we replaced the page pointer to our new list,
1878 * now, we just have to update to head page's prev
1879 * pointer to point to end of list
1880 */
1881 head_page->prev = last_page;
1882 success = 1;
1883 break;
1884 }
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001885 }
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001886
Vaibhav Nagarnaik5040b4b2012-05-03 18:59:51 -07001887 if (success)
1888 INIT_LIST_HEAD(pages);
1889 /*
1890 * If we weren't successful in adding in new pages, warn and stop
1891 * tracing
1892 */
1893 RB_WARN_ON(cpu_buffer, !success);
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02001894 raw_spin_unlock_irq(&cpu_buffer->reader_lock);
Vaibhav Nagarnaik5040b4b2012-05-03 18:59:51 -07001895
1896 /* free pages if they weren't inserted */
1897 if (!success) {
1898 struct buffer_page *bpage, *tmp;
1899 list_for_each_entry_safe(bpage, tmp, &cpu_buffer->new_pages,
1900 list) {
1901 list_del_init(&bpage->list);
1902 free_buffer_page(bpage);
1903 }
1904 }
1905 return success;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001906}
1907
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001908static void rb_update_pages(struct ring_buffer_per_cpu *cpu_buffer)
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001909{
Vaibhav Nagarnaik5040b4b2012-05-03 18:59:51 -07001910 int success;
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001911
Vaibhav Nagarnaik5040b4b2012-05-03 18:59:51 -07001912 if (cpu_buffer->nr_pages_to_update > 0)
1913 success = rb_insert_pages(cpu_buffer);
1914 else
1915 success = rb_remove_pages(cpu_buffer,
1916 -cpu_buffer->nr_pages_to_update);
1917
1918 if (success)
1919 cpu_buffer->nr_pages += cpu_buffer->nr_pages_to_update;
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001920}
1921
1922static void update_pages_handler(struct work_struct *work)
1923{
1924 struct ring_buffer_per_cpu *cpu_buffer = container_of(work,
1925 struct ring_buffer_per_cpu, update_pages_work);
1926 rb_update_pages(cpu_buffer);
Vaibhav Nagarnaik05fdd702012-05-18 13:29:51 -07001927 complete(&cpu_buffer->update_done);
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001928}
1929
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001930/**
1931 * ring_buffer_resize - resize the ring buffer
1932 * @buffer: the buffer to resize.
1933 * @size: the new size.
zhangwei(Jovi)d6118512013-07-15 16:32:50 +08001934 * @cpu_id: the cpu buffer to resize
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001935 *
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001936 * Minimum size is 2 * BUF_PAGE_SIZE.
1937 *
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001938 * Returns 0 on success and < 0 on failure.
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001939 */
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05001940int ring_buffer_resize(struct trace_buffer *buffer, unsigned long size,
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001941 int cpu_id)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001942{
1943 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedt (Red Hat)9b94a8f2016-05-12 11:01:24 -04001944 unsigned long nr_pages;
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001945 int cpu, err = 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001946
Ingo Molnaree51a1d2008-11-13 14:58:31 +01001947 /*
1948 * Always succeed at resizing a non-existent buffer:
1949 */
1950 if (!buffer)
1951 return size;
1952
Steven Rostedt6a31e1f2012-05-23 15:35:17 -04001953 /* Make sure the requested buffer exists */
1954 if (cpu_id != RING_BUFFER_ALL_CPUS &&
1955 !cpumask_test_cpu(cpu_id, buffer->cpumask))
1956 return size;
1957
Steven Rostedt (Red Hat)59643d12016-05-13 09:34:12 -04001958 nr_pages = DIV_ROUND_UP(size, BUF_PAGE_SIZE);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001959
1960 /* we need a minimum of two pages */
Steven Rostedt (Red Hat)59643d12016-05-13 09:34:12 -04001961 if (nr_pages < 2)
1962 nr_pages = 2;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001963
Steven Rostedt (Red Hat)59643d12016-05-13 09:34:12 -04001964 size = nr_pages * BUF_PAGE_SIZE;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001965
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001966 /* prevent another thread from changing buffer sizes */
1967 mutex_lock(&buffer->mutex);
1968
Steven Rostedt (VMware)07b8b102020-03-27 16:21:22 -04001969
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001970 if (cpu_id == RING_BUFFER_ALL_CPUS) {
Steven Rostedt (VMware)07b8b102020-03-27 16:21:22 -04001971 /*
1972 * Don't succeed if resizing is disabled, as a reader might be
1973 * manipulating the ring buffer and is expecting a sane state while
1974 * this is true.
1975 */
1976 for_each_buffer_cpu(buffer, cpu) {
1977 cpu_buffer = buffer->buffers[cpu];
1978 if (atomic_read(&cpu_buffer->resize_disabled)) {
1979 err = -EBUSY;
1980 goto out_err_unlock;
1981 }
1982 }
1983
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001984 /* calculate the pages to update */
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001985 for_each_buffer_cpu(buffer, cpu) {
1986 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001987
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001988 cpu_buffer->nr_pages_to_update = nr_pages -
1989 cpu_buffer->nr_pages;
Vaibhav Nagarnaikd7ec4bf2011-06-07 17:01:42 -07001990 /*
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001991 * nothing more to do for removing pages or no update
Vaibhav Nagarnaikd7ec4bf2011-06-07 17:01:42 -07001992 */
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001993 if (cpu_buffer->nr_pages_to_update <= 0)
1994 continue;
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001995 /*
1996 * to add pages, make sure all new pages can be
1997 * allocated without receiving ENOMEM
1998 */
1999 INIT_LIST_HEAD(&cpu_buffer->new_pages);
2000 if (__rb_allocate_pages(cpu_buffer->nr_pages_to_update,
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07002001 &cpu_buffer->new_pages, cpu)) {
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08002002 /* not enough memory for new pages */
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07002003 err = -ENOMEM;
2004 goto out_err;
2005 }
2006 }
2007
2008 get_online_cpus();
2009 /*
2010 * Fire off all the required work handlers
Vaibhav Nagarnaik05fdd702012-05-18 13:29:51 -07002011 * We can't schedule on offline CPUs, but it's not necessary
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07002012 * since we can change their buffer sizes without any race.
2013 */
2014 for_each_buffer_cpu(buffer, cpu) {
2015 cpu_buffer = buffer->buffers[cpu];
Vaibhav Nagarnaik05fdd702012-05-18 13:29:51 -07002016 if (!cpu_buffer->nr_pages_to_update)
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07002017 continue;
2018
Corey Minyard021c5b32014-07-16 14:07:13 -05002019 /* Can't run something on an offline CPU. */
2020 if (!cpu_online(cpu)) {
Steven Rostedt (Red Hat)f5eb5582013-03-07 09:27:42 -05002021 rb_update_pages(cpu_buffer);
2022 cpu_buffer->nr_pages_to_update = 0;
2023 } else {
Vaibhav Nagarnaik05fdd702012-05-18 13:29:51 -07002024 schedule_work_on(cpu,
2025 &cpu_buffer->update_pages_work);
Steven Rostedt (Red Hat)f5eb5582013-03-07 09:27:42 -05002026 }
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002027 }
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002028
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08002029 /* wait for all the updates to complete */
2030 for_each_buffer_cpu(buffer, cpu) {
2031 cpu_buffer = buffer->buffers[cpu];
Vaibhav Nagarnaik05fdd702012-05-18 13:29:51 -07002032 if (!cpu_buffer->nr_pages_to_update)
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07002033 continue;
2034
Vaibhav Nagarnaik05fdd702012-05-18 13:29:51 -07002035 if (cpu_online(cpu))
2036 wait_for_completion(&cpu_buffer->update_done);
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07002037 cpu_buffer->nr_pages_to_update = 0;
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08002038 }
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07002039
2040 put_online_cpus();
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08002041 } else {
Steven Rostedt (VMware)6167c202018-05-16 11:17:06 -04002042 /* Make sure this CPU has been initialized */
Vaibhav Nagarnaik8e49f412012-10-10 16:40:27 -07002043 if (!cpumask_test_cpu(cpu_id, buffer->cpumask))
2044 goto out;
2045
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08002046 cpu_buffer = buffer->buffers[cpu_id];
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07002047
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08002048 if (nr_pages == cpu_buffer->nr_pages)
2049 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002050
Steven Rostedt (VMware)07b8b102020-03-27 16:21:22 -04002051 /*
2052 * Don't succeed if resizing is disabled, as a reader might be
2053 * manipulating the ring buffer and is expecting a sane state while
2054 * this is true.
2055 */
2056 if (atomic_read(&cpu_buffer->resize_disabled)) {
2057 err = -EBUSY;
2058 goto out_err_unlock;
2059 }
2060
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08002061 cpu_buffer->nr_pages_to_update = nr_pages -
2062 cpu_buffer->nr_pages;
2063
2064 INIT_LIST_HEAD(&cpu_buffer->new_pages);
2065 if (cpu_buffer->nr_pages_to_update > 0 &&
2066 __rb_allocate_pages(cpu_buffer->nr_pages_to_update,
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07002067 &cpu_buffer->new_pages, cpu_id)) {
2068 err = -ENOMEM;
2069 goto out_err;
2070 }
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08002071
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07002072 get_online_cpus();
2073
Corey Minyard021c5b32014-07-16 14:07:13 -05002074 /* Can't run something on an offline CPU. */
2075 if (!cpu_online(cpu_id))
Steven Rostedt (Red Hat)f5eb5582013-03-07 09:27:42 -05002076 rb_update_pages(cpu_buffer);
2077 else {
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07002078 schedule_work_on(cpu_id,
2079 &cpu_buffer->update_pages_work);
Vaibhav Nagarnaik05fdd702012-05-18 13:29:51 -07002080 wait_for_completion(&cpu_buffer->update_done);
Steven Rostedt (Red Hat)f5eb5582013-03-07 09:27:42 -05002081 }
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07002082
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07002083 cpu_buffer->nr_pages_to_update = 0;
Vaibhav Nagarnaik05fdd702012-05-18 13:29:51 -07002084 put_online_cpus();
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08002085 }
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002086
2087 out:
Steven Rostedt659f4512012-05-14 17:02:33 -04002088 /*
2089 * The ring buffer resize can happen with the ring buffer
2090 * enabled, so that the update disturbs the tracing as little
2091 * as possible. But if the buffer is disabled, we do not need
2092 * to worry about that, and we can take the time to verify
2093 * that the buffer is not corrupt.
2094 */
2095 if (atomic_read(&buffer->record_disabled)) {
2096 atomic_inc(&buffer->record_disabled);
2097 /*
2098 * Even though the buffer was disabled, we must make sure
2099 * that it is truly disabled before calling rb_check_pages.
2100 * There could have been a race between checking
2101 * record_disable and incrementing it.
2102 */
Paul E. McKenney74401722018-11-06 18:44:52 -08002103 synchronize_rcu();
Steven Rostedt659f4512012-05-14 17:02:33 -04002104 for_each_buffer_cpu(buffer, cpu) {
2105 cpu_buffer = buffer->buffers[cpu];
2106 rb_check_pages(cpu_buffer);
2107 }
2108 atomic_dec(&buffer->record_disabled);
2109 }
2110
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002111 mutex_unlock(&buffer->mutex);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002112 return size;
2113
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07002114 out_err:
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08002115 for_each_buffer_cpu(buffer, cpu) {
2116 struct buffer_page *bpage, *tmp;
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07002117
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08002118 cpu_buffer = buffer->buffers[cpu];
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08002119 cpu_buffer->nr_pages_to_update = 0;
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07002120
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08002121 if (list_empty(&cpu_buffer->new_pages))
2122 continue;
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07002123
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08002124 list_for_each_entry_safe(bpage, tmp, &cpu_buffer->new_pages,
2125 list) {
2126 list_del_init(&bpage->list);
2127 free_buffer_page(bpage);
2128 }
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002129 }
Steven Rostedt (VMware)07b8b102020-03-27 16:21:22 -04002130 out_err_unlock:
Vegard Nossum641d2f62008-11-18 19:22:13 +01002131 mutex_unlock(&buffer->mutex);
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07002132 return err;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002133}
Robert Richterc4f50182008-12-11 16:49:22 +01002134EXPORT_SYMBOL_GPL(ring_buffer_resize);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002135
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05002136void ring_buffer_change_overwrite(struct trace_buffer *buffer, int val)
David Sharp750912f2010-12-08 13:46:47 -08002137{
2138 mutex_lock(&buffer->mutex);
2139 if (val)
2140 buffer->flags |= RB_FL_OVERWRITE;
2141 else
2142 buffer->flags &= ~RB_FL_OVERWRITE;
2143 mutex_unlock(&buffer->mutex);
2144}
2145EXPORT_SYMBOL_GPL(ring_buffer_change_overwrite);
2146
Steven Rostedt (Red Hat)2289d562016-11-23 20:35:32 -05002147static __always_inline void *__rb_page_index(struct buffer_page *bpage, unsigned index)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002148{
Steven Rostedt044fa782008-12-02 23:50:03 -05002149 return bpage->page->data + index;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002150}
2151
Steven Rostedt (Red Hat)2289d562016-11-23 20:35:32 -05002152static __always_inline struct ring_buffer_event *
Steven Rostedtd7690412008-10-01 00:29:53 -04002153rb_reader_event(struct ring_buffer_per_cpu *cpu_buffer)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002154{
Steven Rostedt6f807ac2008-10-04 02:00:58 -04002155 return __rb_page_index(cpu_buffer->reader_page,
2156 cpu_buffer->reader_page->read);
2157}
2158
Steven Rostedt (Red Hat)2289d562016-11-23 20:35:32 -05002159static __always_inline unsigned rb_page_commit(struct buffer_page *bpage)
Steven Rostedtbf41a152008-10-04 02:00:59 -04002160{
Steven Rostedtabc9b562008-12-02 15:34:06 -05002161 return local_read(&bpage->page->commit);
Steven Rostedtbf41a152008-10-04 02:00:59 -04002162}
2163
Steven Rostedt (VMware)785888c2020-03-17 17:32:27 -04002164static struct ring_buffer_event *
2165rb_iter_head_event(struct ring_buffer_iter *iter)
2166{
2167 struct ring_buffer_event *event;
2168 struct buffer_page *iter_head_page = iter->head_page;
2169 unsigned long commit;
2170 unsigned length;
2171
Steven Rostedt (VMware)153368c2020-03-17 17:32:29 -04002172 if (iter->head != iter->next_event)
2173 return iter->event;
2174
Steven Rostedt (VMware)785888c2020-03-17 17:32:27 -04002175 /*
2176 * When the writer goes across pages, it issues a cmpxchg which
2177 * is a mb(), which will synchronize with the rmb here.
2178 * (see rb_tail_page_update() and __rb_reserve_next())
2179 */
2180 commit = rb_page_commit(iter_head_page);
2181 smp_rmb();
2182 event = __rb_page_index(iter_head_page, iter->head);
2183 length = rb_event_length(event);
2184
2185 /*
2186 * READ_ONCE() doesn't work on functions and we don't want the
2187 * compiler doing any crazy optimizations with length.
2188 */
2189 barrier();
2190
2191 if ((iter->head + length) > commit || length > BUF_MAX_DATA_SIZE)
2192 /* Writer corrupted the read? */
2193 goto reset;
2194
2195 memcpy(iter->event, event, length);
2196 /*
2197 * If the page stamp is still the same after this rmb() then the
2198 * event was safely copied without the writer entering the page.
2199 */
2200 smp_rmb();
2201
2202 /* Make sure the page didn't change since we read this */
2203 if (iter->page_stamp != iter_head_page->page->time_stamp ||
2204 commit > rb_page_commit(iter_head_page))
2205 goto reset;
2206
2207 iter->next_event = iter->head + length;
2208 return iter->event;
2209 reset:
2210 /* Reset to the beginning */
2211 iter->page_stamp = iter->read_stamp = iter->head_page->page->time_stamp;
2212 iter->head = 0;
2213 iter->next_event = 0;
Steven Rostedt (VMware)c9b7a4a2020-03-17 17:32:32 -04002214 iter->missed_events = 1;
Steven Rostedt (VMware)785888c2020-03-17 17:32:27 -04002215 return NULL;
2216}
2217
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002218/* Size is determined by what has been committed */
Steven Rostedt (Red Hat)2289d562016-11-23 20:35:32 -05002219static __always_inline unsigned rb_page_size(struct buffer_page *bpage)
Steven Rostedtbf41a152008-10-04 02:00:59 -04002220{
2221 return rb_page_commit(bpage);
2222}
2223
Steven Rostedt (Red Hat)2289d562016-11-23 20:35:32 -05002224static __always_inline unsigned
Steven Rostedtbf41a152008-10-04 02:00:59 -04002225rb_commit_index(struct ring_buffer_per_cpu *cpu_buffer)
2226{
2227 return rb_page_commit(cpu_buffer->commit_page);
2228}
2229
Steven Rostedt (Red Hat)2289d562016-11-23 20:35:32 -05002230static __always_inline unsigned
Steven Rostedtbf41a152008-10-04 02:00:59 -04002231rb_event_index(struct ring_buffer_event *event)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002232{
Steven Rostedtbf41a152008-10-04 02:00:59 -04002233 unsigned long addr = (unsigned long)event;
2234
Steven Rostedt22f470f2009-06-11 09:29:58 -04002235 return (addr & ~PAGE_MASK) - BUF_PAGE_HDR_SIZE;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002236}
2237
Andrew Morton34a148b2009-01-09 12:27:09 -08002238static void rb_inc_iter(struct ring_buffer_iter *iter)
Steven Rostedtd7690412008-10-01 00:29:53 -04002239{
2240 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
2241
2242 /*
2243 * The iterator could be on the reader page (it starts there).
2244 * But the head could have moved, since the reader was
2245 * found. Check for this case and assign the iterator
2246 * to the head page instead of next.
2247 */
2248 if (iter->head_page == cpu_buffer->reader_page)
Steven Rostedt77ae3652009-03-27 11:00:29 -04002249 iter->head_page = rb_set_head_page(cpu_buffer);
Steven Rostedtd7690412008-10-01 00:29:53 -04002250 else
2251 rb_inc_page(cpu_buffer, &iter->head_page);
2252
Steven Rostedt (VMware)28e3fc52020-03-17 17:32:26 -04002253 iter->page_stamp = iter->read_stamp = iter->head_page->page->time_stamp;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002254 iter->head = 0;
Steven Rostedt (VMware)785888c2020-03-17 17:32:27 -04002255 iter->next_event = 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002256}
2257
Steven Rostedt77ae3652009-03-27 11:00:29 -04002258/*
2259 * rb_handle_head_page - writer hit the head page
2260 *
2261 * Returns: +1 to retry page
2262 * 0 to continue
2263 * -1 on error
2264 */
2265static int
2266rb_handle_head_page(struct ring_buffer_per_cpu *cpu_buffer,
2267 struct buffer_page *tail_page,
2268 struct buffer_page *next_page)
2269{
2270 struct buffer_page *new_head;
2271 int entries;
2272 int type;
2273 int ret;
2274
2275 entries = rb_page_entries(next_page);
2276
2277 /*
2278 * The hard part is here. We need to move the head
2279 * forward, and protect against both readers on
2280 * other CPUs and writers coming in via interrupts.
2281 */
2282 type = rb_head_page_set_update(cpu_buffer, next_page, tail_page,
2283 RB_PAGE_HEAD);
2284
2285 /*
2286 * type can be one of four:
2287 * NORMAL - an interrupt already moved it for us
2288 * HEAD - we are the first to get here.
2289 * UPDATE - we are the interrupt interrupting
2290 * a current move.
2291 * MOVED - a reader on another CPU moved the next
2292 * pointer to its reader page. Give up
2293 * and try again.
2294 */
2295
2296 switch (type) {
2297 case RB_PAGE_HEAD:
2298 /*
2299 * We changed the head to UPDATE, thus
2300 * it is our responsibility to update
2301 * the counters.
2302 */
2303 local_add(entries, &cpu_buffer->overrun);
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -07002304 local_sub(BUF_PAGE_SIZE, &cpu_buffer->entries_bytes);
Steven Rostedt77ae3652009-03-27 11:00:29 -04002305
2306 /*
2307 * The entries will be zeroed out when we move the
2308 * tail page.
2309 */
2310
2311 /* still more to do */
2312 break;
2313
2314 case RB_PAGE_UPDATE:
2315 /*
2316 * This is an interrupt that interrupt the
2317 * previous update. Still more to do.
2318 */
2319 break;
2320 case RB_PAGE_NORMAL:
2321 /*
2322 * An interrupt came in before the update
2323 * and processed this for us.
2324 * Nothing left to do.
2325 */
2326 return 1;
2327 case RB_PAGE_MOVED:
2328 /*
2329 * The reader is on another CPU and just did
2330 * a swap with our next_page.
2331 * Try again.
2332 */
2333 return 1;
2334 default:
2335 RB_WARN_ON(cpu_buffer, 1); /* WTF??? */
2336 return -1;
2337 }
2338
2339 /*
2340 * Now that we are here, the old head pointer is
2341 * set to UPDATE. This will keep the reader from
2342 * swapping the head page with the reader page.
2343 * The reader (on another CPU) will spin till
2344 * we are finished.
2345 *
2346 * We just need to protect against interrupts
2347 * doing the job. We will set the next pointer
2348 * to HEAD. After that, we set the old pointer
2349 * to NORMAL, but only if it was HEAD before.
2350 * otherwise we are an interrupt, and only
2351 * want the outer most commit to reset it.
2352 */
2353 new_head = next_page;
2354 rb_inc_page(cpu_buffer, &new_head);
2355
2356 ret = rb_head_page_set_head(cpu_buffer, new_head, next_page,
2357 RB_PAGE_NORMAL);
2358
2359 /*
2360 * Valid returns are:
2361 * HEAD - an interrupt came in and already set it.
2362 * NORMAL - One of two things:
2363 * 1) We really set it.
2364 * 2) A bunch of interrupts came in and moved
2365 * the page forward again.
2366 */
2367 switch (ret) {
2368 case RB_PAGE_HEAD:
2369 case RB_PAGE_NORMAL:
2370 /* OK */
2371 break;
2372 default:
2373 RB_WARN_ON(cpu_buffer, 1);
2374 return -1;
2375 }
2376
2377 /*
2378 * It is possible that an interrupt came in,
2379 * set the head up, then more interrupts came in
2380 * and moved it again. When we get back here,
2381 * the page would have been set to NORMAL but we
2382 * just set it back to HEAD.
2383 *
2384 * How do you detect this? Well, if that happened
2385 * the tail page would have moved.
2386 */
2387 if (ret == RB_PAGE_NORMAL) {
Steven Rostedt (Red Hat)85736362015-11-17 14:03:11 -05002388 struct buffer_page *buffer_tail_page;
2389
2390 buffer_tail_page = READ_ONCE(cpu_buffer->tail_page);
Steven Rostedt77ae3652009-03-27 11:00:29 -04002391 /*
2392 * If the tail had moved passed next, then we need
2393 * to reset the pointer.
2394 */
Steven Rostedt (Red Hat)85736362015-11-17 14:03:11 -05002395 if (buffer_tail_page != tail_page &&
2396 buffer_tail_page != next_page)
Steven Rostedt77ae3652009-03-27 11:00:29 -04002397 rb_head_page_set_normal(cpu_buffer, new_head,
2398 next_page,
2399 RB_PAGE_HEAD);
2400 }
2401
2402 /*
2403 * If this was the outer most commit (the one that
2404 * changed the original pointer from HEAD to UPDATE),
2405 * then it is up to us to reset it to NORMAL.
2406 */
2407 if (type == RB_PAGE_HEAD) {
2408 ret = rb_head_page_set_normal(cpu_buffer, next_page,
2409 tail_page,
2410 RB_PAGE_UPDATE);
2411 if (RB_WARN_ON(cpu_buffer,
2412 ret != RB_PAGE_UPDATE))
2413 return -1;
2414 }
2415
2416 return 0;
2417}
2418
Steven Rostedtc7b09302009-06-11 11:12:00 -04002419static inline void
2420rb_reset_tail(struct ring_buffer_per_cpu *cpu_buffer,
Steven Rostedt (Red Hat)fcc742e2015-05-28 17:13:14 -04002421 unsigned long tail, struct rb_event_info *info)
Steven Rostedtc7b09302009-06-11 11:12:00 -04002422{
Steven Rostedt (Red Hat)fcc742e2015-05-28 17:13:14 -04002423 struct buffer_page *tail_page = info->tail_page;
Steven Rostedtc7b09302009-06-11 11:12:00 -04002424 struct ring_buffer_event *event;
Steven Rostedt (Red Hat)fcc742e2015-05-28 17:13:14 -04002425 unsigned long length = info->length;
Steven Rostedtc7b09302009-06-11 11:12:00 -04002426
2427 /*
2428 * Only the event that crossed the page boundary
2429 * must fill the old tail_page with padding.
2430 */
2431 if (tail >= BUF_PAGE_SIZE) {
Steven Rostedtb3230c82010-05-21 11:55:21 -04002432 /*
2433 * If the page was filled, then we still need
2434 * to update the real_end. Reset it to zero
2435 * and the reader will ignore it.
2436 */
2437 if (tail == BUF_PAGE_SIZE)
2438 tail_page->real_end = 0;
2439
Steven Rostedtc7b09302009-06-11 11:12:00 -04002440 local_sub(length, &tail_page->write);
2441 return;
2442 }
2443
2444 event = __rb_page_index(tail_page, tail);
2445
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -07002446 /* account for padding bytes */
2447 local_add(BUF_PAGE_SIZE - tail, &cpu_buffer->entries_bytes);
2448
Steven Rostedtc7b09302009-06-11 11:12:00 -04002449 /*
Steven Rostedtff0ff842010-03-31 22:11:42 -04002450 * Save the original length to the meta data.
2451 * This will be used by the reader to add lost event
2452 * counter.
2453 */
2454 tail_page->real_end = tail;
2455
2456 /*
Steven Rostedtc7b09302009-06-11 11:12:00 -04002457 * If this event is bigger than the minimum size, then
2458 * we need to be careful that we don't subtract the
2459 * write counter enough to allow another writer to slip
2460 * in on this page.
2461 * We put in a discarded commit instead, to make sure
2462 * that this space is not used again.
2463 *
2464 * If we are less than the minimum size, we don't need to
2465 * worry about it.
2466 */
2467 if (tail > (BUF_PAGE_SIZE - RB_EVNT_MIN_SIZE)) {
2468 /* No room for any events */
2469
2470 /* Mark the rest of the page with padding */
2471 rb_event_set_padding(event);
2472
2473 /* Set the write back to the previous setting */
2474 local_sub(length, &tail_page->write);
2475 return;
2476 }
2477
2478 /* Put in a discarded event */
2479 event->array[0] = (BUF_PAGE_SIZE - tail) - RB_EVNT_HDR_SIZE;
2480 event->type_len = RINGBUF_TYPE_PADDING;
2481 /* time delta must be non zero */
2482 event->time_delta = 1;
Steven Rostedtc7b09302009-06-11 11:12:00 -04002483
2484 /* Set write to end of buffer */
2485 length = (tail + length) - BUF_PAGE_SIZE;
2486 local_sub(length, &tail_page->write);
2487}
Steven Rostedt6634ff22009-05-06 15:30:07 -04002488
Steven Rostedt (Red Hat)4239c382015-11-17 16:36:06 -05002489static inline void rb_end_commit(struct ring_buffer_per_cpu *cpu_buffer);
2490
Steven Rostedt747e94a2010-10-08 13:51:48 -04002491/*
2492 * This is the slow path, force gcc not to inline it.
2493 */
2494static noinline struct ring_buffer_event *
Steven Rostedt6634ff22009-05-06 15:30:07 -04002495rb_move_tail(struct ring_buffer_per_cpu *cpu_buffer,
Steven Rostedt (Red Hat)fcc742e2015-05-28 17:13:14 -04002496 unsigned long tail, struct rb_event_info *info)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002497{
Steven Rostedt (Red Hat)fcc742e2015-05-28 17:13:14 -04002498 struct buffer_page *tail_page = info->tail_page;
Steven Rostedt5a50e332009-11-17 08:43:01 -05002499 struct buffer_page *commit_page = cpu_buffer->commit_page;
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05002500 struct trace_buffer *buffer = cpu_buffer->buffer;
Steven Rostedt77ae3652009-03-27 11:00:29 -04002501 struct buffer_page *next_page;
2502 int ret;
Steven Rostedtaa20ae82009-05-05 21:16:11 -04002503
2504 next_page = tail_page;
2505
Steven Rostedtaa20ae82009-05-05 21:16:11 -04002506 rb_inc_page(cpu_buffer, &next_page);
2507
Steven Rostedtaa20ae82009-05-05 21:16:11 -04002508 /*
2509 * If for some reason, we had an interrupt storm that made
2510 * it all the way around the buffer, bail, and warn
2511 * about it.
2512 */
2513 if (unlikely(next_page == commit_page)) {
Steven Rostedt77ae3652009-03-27 11:00:29 -04002514 local_inc(&cpu_buffer->commit_overrun);
Steven Rostedtaa20ae82009-05-05 21:16:11 -04002515 goto out_reset;
2516 }
2517
Steven Rostedt77ae3652009-03-27 11:00:29 -04002518 /*
2519 * This is where the fun begins!
2520 *
2521 * We are fighting against races between a reader that
2522 * could be on another CPU trying to swap its reader
2523 * page with the buffer head.
2524 *
2525 * We are also fighting against interrupts coming in and
2526 * moving the head or tail on us as well.
2527 *
2528 * If the next page is the head page then we have filled
2529 * the buffer, unless the commit page is still on the
2530 * reader page.
2531 */
2532 if (rb_is_head_page(cpu_buffer, next_page, &tail_page->list)) {
Steven Rostedtaa20ae82009-05-05 21:16:11 -04002533
Steven Rostedt77ae3652009-03-27 11:00:29 -04002534 /*
2535 * If the commit is not on the reader page, then
2536 * move the header page.
2537 */
2538 if (!rb_is_reader_page(cpu_buffer->commit_page)) {
2539 /*
2540 * If we are not in overwrite mode,
2541 * this is easy, just stop here.
2542 */
Slava Pestov884bfe82011-07-15 14:23:58 -07002543 if (!(buffer->flags & RB_FL_OVERWRITE)) {
2544 local_inc(&cpu_buffer->dropped_events);
Steven Rostedt77ae3652009-03-27 11:00:29 -04002545 goto out_reset;
Slava Pestov884bfe82011-07-15 14:23:58 -07002546 }
Steven Rostedtaa20ae82009-05-05 21:16:11 -04002547
Steven Rostedt77ae3652009-03-27 11:00:29 -04002548 ret = rb_handle_head_page(cpu_buffer,
2549 tail_page,
2550 next_page);
2551 if (ret < 0)
2552 goto out_reset;
2553 if (ret)
2554 goto out_again;
2555 } else {
2556 /*
2557 * We need to be careful here too. The
2558 * commit page could still be on the reader
2559 * page. We could have a small buffer, and
2560 * have filled up the buffer with events
2561 * from interrupts and such, and wrapped.
2562 *
2563 * Note, if the tail page is also the on the
2564 * reader_page, we let it move out.
2565 */
2566 if (unlikely((cpu_buffer->commit_page !=
2567 cpu_buffer->tail_page) &&
2568 (cpu_buffer->commit_page ==
2569 cpu_buffer->reader_page))) {
2570 local_inc(&cpu_buffer->commit_overrun);
2571 goto out_reset;
2572 }
Steven Rostedtaa20ae82009-05-05 21:16:11 -04002573 }
2574 }
2575
Steven Rostedt (Red Hat)70004982015-11-17 15:15:19 -05002576 rb_tail_page_update(cpu_buffer, tail_page, next_page);
Steven Rostedtaa20ae82009-05-05 21:16:11 -04002577
Steven Rostedt77ae3652009-03-27 11:00:29 -04002578 out_again:
Steven Rostedtaa20ae82009-05-05 21:16:11 -04002579
Steven Rostedt (Red Hat)fcc742e2015-05-28 17:13:14 -04002580 rb_reset_tail(cpu_buffer, tail, info);
Steven Rostedtaa20ae82009-05-05 21:16:11 -04002581
Steven Rostedt (Red Hat)4239c382015-11-17 16:36:06 -05002582 /* Commit what we have for now. */
2583 rb_end_commit(cpu_buffer);
2584 /* rb_end_commit() decs committing */
2585 local_inc(&cpu_buffer->committing);
2586
Steven Rostedtaa20ae82009-05-05 21:16:11 -04002587 /* fail and let the caller try again */
2588 return ERR_PTR(-EAGAIN);
2589
Steven Rostedt45141d42009-02-12 13:19:48 -05002590 out_reset:
Lai Jiangshan6f3b3442009-01-12 11:06:18 +08002591 /* reset write */
Steven Rostedt (Red Hat)fcc742e2015-05-28 17:13:14 -04002592 rb_reset_tail(cpu_buffer, tail, info);
Lai Jiangshan6f3b3442009-01-12 11:06:18 +08002593
Steven Rostedtbf41a152008-10-04 02:00:59 -04002594 return NULL;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002595}
2596
Steven Rostedt (Red Hat)d90fd772015-05-29 12:12:27 -04002597/* Slow path, do not inline */
2598static noinline struct ring_buffer_event *
Tom Zanussidc4e2802018-01-15 20:51:40 -06002599rb_add_time_stamp(struct ring_buffer_event *event, u64 delta, bool abs)
Steven Rostedt (Red Hat)d90fd772015-05-29 12:12:27 -04002600{
Tom Zanussidc4e2802018-01-15 20:51:40 -06002601 if (abs)
2602 event->type_len = RINGBUF_TYPE_TIME_STAMP;
2603 else
2604 event->type_len = RINGBUF_TYPE_TIME_EXTEND;
Steven Rostedt (Red Hat)d90fd772015-05-29 12:12:27 -04002605
Tom Zanussidc4e2802018-01-15 20:51:40 -06002606 /* Not the first event on the page, or not delta? */
2607 if (abs || rb_event_index(event)) {
Steven Rostedt (Red Hat)d90fd772015-05-29 12:12:27 -04002608 event->time_delta = delta & TS_MASK;
2609 event->array[0] = delta >> TS_SHIFT;
2610 } else {
2611 /* nope, just zero it */
2612 event->time_delta = 0;
2613 event->array[0] = 0;
2614 }
2615
2616 return skip_time_extend(event);
2617}
2618
Yaowei Baicdb2a0a92015-09-29 22:43:34 +08002619static inline bool rb_event_is_commit(struct ring_buffer_per_cpu *cpu_buffer,
Steven Rostedt (Red Hat)b7dc42f2015-09-03 08:57:12 -04002620 struct ring_buffer_event *event);
2621
Steven Rostedt (Red Hat)d90fd772015-05-29 12:12:27 -04002622/**
2623 * rb_update_event - update event type and data
Steven Rostedt (VMware)cfc585a2020-01-14 16:27:51 -05002624 * @cpu_buffer: The per cpu buffer of the @event
Steven Rostedt (Red Hat)d90fd772015-05-29 12:12:27 -04002625 * @event: the event to update
Steven Rostedt (VMware)cfc585a2020-01-14 16:27:51 -05002626 * @info: The info to update the @event with (contains length and delta)
Steven Rostedt (Red Hat)d90fd772015-05-29 12:12:27 -04002627 *
Steven Rostedt (VMware)cfc585a2020-01-14 16:27:51 -05002628 * Update the type and data fields of the @event. The length
Steven Rostedt (Red Hat)d90fd772015-05-29 12:12:27 -04002629 * is the actual size that is written to the ring buffer,
2630 * and with this, we can determine what to place into the
2631 * data field.
2632 */
Steven Rostedt (Red Hat)b7dc42f2015-09-03 08:57:12 -04002633static void
Steven Rostedt (Red Hat)d90fd772015-05-29 12:12:27 -04002634rb_update_event(struct ring_buffer_per_cpu *cpu_buffer,
2635 struct ring_buffer_event *event,
2636 struct rb_event_info *info)
2637{
2638 unsigned length = info->length;
2639 u64 delta = info->delta;
2640
2641 /*
2642 * If we need to add a timestamp, then we
Steven Rostedt (VMware)6167c202018-05-16 11:17:06 -04002643 * add it to the start of the reserved space.
Steven Rostedt (Red Hat)d90fd772015-05-29 12:12:27 -04002644 */
2645 if (unlikely(info->add_timestamp)) {
Steven Rostedt (VMware)7c4b4a52020-06-28 22:52:26 -04002646 bool abs = info->add_timestamp &
2647 (RB_ADD_STAMP_FORCE | RB_ADD_STAMP_ABSOLUTE);
Tom Zanussidc4e2802018-01-15 20:51:40 -06002648
Steven Rostedt (VMware)097350d2020-06-22 15:18:15 -04002649 event = rb_add_time_stamp(event, abs ? info->delta : delta, abs);
Steven Rostedt (Red Hat)d90fd772015-05-29 12:12:27 -04002650 length -= RB_LEN_TIME_EXTEND;
2651 delta = 0;
2652 }
2653
2654 event->time_delta = delta;
2655 length -= RB_EVNT_HDR_SIZE;
Steven Rostedt (VMware)86b3de62019-05-28 09:36:19 -04002656 if (length > RB_MAX_SMALL_DATA) {
Steven Rostedt (Red Hat)d90fd772015-05-29 12:12:27 -04002657 event->type_len = 0;
2658 event->array[0] = length;
2659 } else
2660 event->type_len = DIV_ROUND_UP(length, RB_ALIGNMENT);
2661}
2662
2663static unsigned rb_calculate_event_length(unsigned length)
2664{
2665 struct ring_buffer_event event; /* Used only for sizeof array */
2666
2667 /* zero length can cause confusions */
2668 if (!length)
2669 length++;
2670
Steven Rostedt (VMware)86b3de62019-05-28 09:36:19 -04002671 if (length > RB_MAX_SMALL_DATA)
Steven Rostedt (Red Hat)d90fd772015-05-29 12:12:27 -04002672 length += sizeof(event.array[0]);
2673
2674 length += RB_EVNT_HDR_SIZE;
Steven Rostedt (VMware)86b3de62019-05-28 09:36:19 -04002675 length = ALIGN(length, RB_ALIGNMENT);
Steven Rostedt (Red Hat)d90fd772015-05-29 12:12:27 -04002676
2677 /*
2678 * In case the time delta is larger than the 27 bits for it
2679 * in the header, we need to add a timestamp. If another
2680 * event comes in when trying to discard this one to increase
2681 * the length, then the timestamp will be added in the allocated
2682 * space of this event. If length is bigger than the size needed
2683 * for the TIME_EXTEND, then padding has to be used. The events
2684 * length must be either RB_LEN_TIME_EXTEND, or greater than or equal
2685 * to RB_LEN_TIME_EXTEND + 8, as 8 is the minimum size for padding.
2686 * As length is a multiple of 4, we only need to worry if it
2687 * is 12 (RB_LEN_TIME_EXTEND + 4).
2688 */
2689 if (length == RB_LEN_TIME_EXTEND + RB_ALIGNMENT)
2690 length += RB_ALIGNMENT;
2691
2692 return length;
2693}
2694
Steven Rostedt (Red Hat)9826b272015-05-28 17:36:45 -04002695#ifndef CONFIG_HAVE_UNSTABLE_SCHED_CLOCK
2696static inline bool sched_clock_stable(void)
2697{
2698 return true;
2699}
2700#endif
2701
Steven Rostedt (VMware)a389d862020-06-28 22:52:25 -04002702static __always_inline bool
2703rb_event_is_commit(struct ring_buffer_per_cpu *cpu_buffer,
2704 struct ring_buffer_event *event)
2705{
2706 unsigned long addr = (unsigned long)event;
2707 unsigned long index;
2708
2709 index = rb_event_index(event);
2710 addr &= PAGE_MASK;
2711
2712 return cpu_buffer->commit_page->page == (void *)addr &&
2713 rb_commit_index(cpu_buffer) == index;
2714}
2715
2716static u64 rb_time_delta(struct ring_buffer_event *event)
2717{
2718 switch (event->type_len) {
2719 case RINGBUF_TYPE_PADDING:
2720 return 0;
2721
2722 case RINGBUF_TYPE_TIME_EXTEND:
2723 return ring_buffer_event_time_stamp(event);
2724
2725 case RINGBUF_TYPE_TIME_STAMP:
2726 return 0;
2727
2728 case RINGBUF_TYPE_DATA:
2729 return event->time_delta;
2730 default:
2731 return 0;
2732 }
2733}
2734
Steven Rostedt (Red Hat)a4543a22015-05-29 09:40:18 -04002735static inline int
2736rb_try_to_discard(struct ring_buffer_per_cpu *cpu_buffer,
Steven Rostedt (Red Hat)d90fd772015-05-29 12:12:27 -04002737 struct ring_buffer_event *event)
2738{
2739 unsigned long new_index, old_index;
2740 struct buffer_page *bpage;
2741 unsigned long index;
2742 unsigned long addr;
Steven Rostedt (VMware)a389d862020-06-28 22:52:25 -04002743 u64 write_stamp;
2744 u64 delta;
Steven Rostedt (Red Hat)d90fd772015-05-29 12:12:27 -04002745
2746 new_index = rb_event_index(event);
2747 old_index = new_index + rb_event_ts_length(event);
2748 addr = (unsigned long)event;
2749 addr &= PAGE_MASK;
2750
Steven Rostedt (Red Hat)85736362015-11-17 14:03:11 -05002751 bpage = READ_ONCE(cpu_buffer->tail_page);
Steven Rostedt (Red Hat)d90fd772015-05-29 12:12:27 -04002752
Steven Rostedt (VMware)a389d862020-06-28 22:52:25 -04002753 delta = rb_time_delta(event);
2754
Steven Rostedt (VMware)10464b42020-06-28 22:52:27 -04002755 if (!rb_time_read(&cpu_buffer->write_stamp, &write_stamp))
2756 return 0;
Steven Rostedt (VMware)a389d862020-06-28 22:52:25 -04002757
2758 /* Make sure the write stamp is read before testing the location */
2759 barrier();
2760
Steven Rostedt (Red Hat)d90fd772015-05-29 12:12:27 -04002761 if (bpage->page == (void *)addr && rb_page_write(bpage) == old_index) {
2762 unsigned long write_mask =
2763 local_read(&bpage->write) & ~RB_WRITE_MASK;
2764 unsigned long event_length = rb_event_length(event);
Steven Rostedt (VMware)a389d862020-06-28 22:52:25 -04002765
Steven Rostedt (VMware)a389d862020-06-28 22:52:25 -04002766 /* Something came in, can't discard */
Steven Rostedt (VMware)10464b42020-06-28 22:52:27 -04002767 if (!rb_time_cmpxchg(&cpu_buffer->write_stamp,
2768 write_stamp, write_stamp - delta))
Steven Rostedt (VMware)a389d862020-06-28 22:52:25 -04002769 return 0;
2770
2771 /*
2772 * If an event were to come in now, it would see that the
2773 * write_stamp and the before_stamp are different, and assume
2774 * that this event just added itself before updating
2775 * the write stamp. The interrupting event will fix the
2776 * write stamp for us, and use the before stamp as its delta.
2777 */
2778
Steven Rostedt (Red Hat)d90fd772015-05-29 12:12:27 -04002779 /*
2780 * This is on the tail page. It is possible that
2781 * a write could come in and move the tail page
2782 * and write to the next page. That is fine
2783 * because we just shorten what is on this page.
2784 */
2785 old_index += write_mask;
2786 new_index += write_mask;
2787 index = local_cmpxchg(&bpage->write, old_index, new_index);
2788 if (index == old_index) {
2789 /* update counters */
2790 local_sub(event_length, &cpu_buffer->entries_bytes);
2791 return 1;
2792 }
2793 }
2794
2795 /* could not discard */
2796 return 0;
2797}
2798
2799static void rb_start_commit(struct ring_buffer_per_cpu *cpu_buffer)
2800{
2801 local_inc(&cpu_buffer->committing);
2802 local_inc(&cpu_buffer->commits);
2803}
2804
Steven Rostedt (Red Hat)38e11df2016-11-23 20:42:31 -05002805static __always_inline void
Steven Rostedt (Red Hat)d90fd772015-05-29 12:12:27 -04002806rb_set_commit_to_write(struct ring_buffer_per_cpu *cpu_buffer)
2807{
2808 unsigned long max_count;
2809
2810 /*
2811 * We only race with interrupts and NMIs on this CPU.
2812 * If we own the commit event, then we can commit
2813 * all others that interrupted us, since the interruptions
2814 * are in stack format (they finish before they come
2815 * back to us). This allows us to do a simple loop to
2816 * assign the commit to the tail.
2817 */
2818 again:
2819 max_count = cpu_buffer->nr_pages * 100;
2820
Steven Rostedt (Red Hat)85736362015-11-17 14:03:11 -05002821 while (cpu_buffer->commit_page != READ_ONCE(cpu_buffer->tail_page)) {
Steven Rostedt (Red Hat)d90fd772015-05-29 12:12:27 -04002822 if (RB_WARN_ON(cpu_buffer, !(--max_count)))
2823 return;
2824 if (RB_WARN_ON(cpu_buffer,
2825 rb_is_reader_page(cpu_buffer->tail_page)))
2826 return;
2827 local_set(&cpu_buffer->commit_page->page->commit,
2828 rb_page_write(cpu_buffer->commit_page));
2829 rb_inc_page(cpu_buffer, &cpu_buffer->commit_page);
Steven Rostedt (Red Hat)d90fd772015-05-29 12:12:27 -04002830 /* add barrier to keep gcc from optimizing too much */
2831 barrier();
2832 }
2833 while (rb_commit_index(cpu_buffer) !=
2834 rb_page_write(cpu_buffer->commit_page)) {
2835
2836 local_set(&cpu_buffer->commit_page->page->commit,
2837 rb_page_write(cpu_buffer->commit_page));
2838 RB_WARN_ON(cpu_buffer,
2839 local_read(&cpu_buffer->commit_page->page->commit) &
2840 ~RB_WRITE_MASK);
2841 barrier();
2842 }
2843
2844 /* again, keep gcc from optimizing */
2845 barrier();
2846
2847 /*
2848 * If an interrupt came in just after the first while loop
2849 * and pushed the tail page forward, we will be left with
2850 * a dangling commit that will never go forward.
2851 */
Steven Rostedt (Red Hat)85736362015-11-17 14:03:11 -05002852 if (unlikely(cpu_buffer->commit_page != READ_ONCE(cpu_buffer->tail_page)))
Steven Rostedt (Red Hat)d90fd772015-05-29 12:12:27 -04002853 goto again;
2854}
2855
Steven Rostedt (Red Hat)38e11df2016-11-23 20:42:31 -05002856static __always_inline void rb_end_commit(struct ring_buffer_per_cpu *cpu_buffer)
Steven Rostedt (Red Hat)d90fd772015-05-29 12:12:27 -04002857{
2858 unsigned long commits;
2859
2860 if (RB_WARN_ON(cpu_buffer,
2861 !local_read(&cpu_buffer->committing)))
2862 return;
2863
2864 again:
2865 commits = local_read(&cpu_buffer->commits);
2866 /* synchronize with interrupts */
2867 barrier();
2868 if (local_read(&cpu_buffer->committing) == 1)
2869 rb_set_commit_to_write(cpu_buffer);
2870
2871 local_dec(&cpu_buffer->committing);
2872
2873 /* synchronize with interrupts */
2874 barrier();
2875
2876 /*
2877 * Need to account for interrupts coming in between the
2878 * updating of the commit page and the clearing of the
2879 * committing counter.
2880 */
2881 if (unlikely(local_read(&cpu_buffer->commits) != commits) &&
2882 !local_read(&cpu_buffer->committing)) {
2883 local_inc(&cpu_buffer->committing);
2884 goto again;
2885 }
2886}
2887
2888static inline void rb_event_discard(struct ring_buffer_event *event)
2889{
Tom Zanussidc4e2802018-01-15 20:51:40 -06002890 if (extended_time(event))
Steven Rostedt (Red Hat)d90fd772015-05-29 12:12:27 -04002891 event = skip_time_extend(event);
2892
2893 /* array[0] holds the actual length for the discarded event */
2894 event->array[0] = rb_event_data_length(event) - RB_EVNT_HDR_SIZE;
2895 event->type_len = RINGBUF_TYPE_PADDING;
2896 /* time delta must be non zero */
2897 if (!event->time_delta)
2898 event->time_delta = 1;
2899}
2900
Steven Rostedt (Red Hat)d90fd772015-05-29 12:12:27 -04002901static void rb_commit(struct ring_buffer_per_cpu *cpu_buffer,
2902 struct ring_buffer_event *event)
2903{
2904 local_inc(&cpu_buffer->entries);
Steven Rostedt (Red Hat)d90fd772015-05-29 12:12:27 -04002905 rb_end_commit(cpu_buffer);
2906}
2907
2908static __always_inline void
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05002909rb_wakeups(struct trace_buffer *buffer, struct ring_buffer_per_cpu *cpu_buffer)
Steven Rostedt (Red Hat)d90fd772015-05-29 12:12:27 -04002910{
Steven Rostedt (VMware)03329f92018-11-29 21:38:42 -05002911 size_t nr_pages;
2912 size_t dirty;
2913 size_t full;
Steven Rostedt (Red Hat)d90fd772015-05-29 12:12:27 -04002914
2915 if (buffer->irq_work.waiters_pending) {
2916 buffer->irq_work.waiters_pending = false;
2917 /* irq_work_queue() supplies it's own memory barriers */
2918 irq_work_queue(&buffer->irq_work.work);
2919 }
2920
2921 if (cpu_buffer->irq_work.waiters_pending) {
2922 cpu_buffer->irq_work.waiters_pending = false;
2923 /* irq_work_queue() supplies it's own memory barriers */
2924 irq_work_queue(&cpu_buffer->irq_work.work);
2925 }
2926
Steven Rostedt (VMware)03329f92018-11-29 21:38:42 -05002927 if (cpu_buffer->last_pages_touch == local_read(&cpu_buffer->pages_touched))
2928 return;
Steven Rostedt (Red Hat)d90fd772015-05-29 12:12:27 -04002929
Steven Rostedt (VMware)03329f92018-11-29 21:38:42 -05002930 if (cpu_buffer->reader_page == cpu_buffer->commit_page)
2931 return;
Steven Rostedt (VMware)2c2b0a72018-11-29 20:32:26 -05002932
Steven Rostedt (VMware)03329f92018-11-29 21:38:42 -05002933 if (!cpu_buffer->irq_work.full_waiters_pending)
2934 return;
Steven Rostedt (VMware)2c2b0a72018-11-29 20:32:26 -05002935
Steven Rostedt (VMware)03329f92018-11-29 21:38:42 -05002936 cpu_buffer->last_pages_touch = local_read(&cpu_buffer->pages_touched);
2937
2938 full = cpu_buffer->shortest_full;
2939 nr_pages = cpu_buffer->nr_pages;
2940 dirty = ring_buffer_nr_dirty_pages(buffer, cpu_buffer->cpu);
2941 if (full && nr_pages && (dirty * 100) <= full * nr_pages)
2942 return;
2943
2944 cpu_buffer->irq_work.wakeup_full = true;
2945 cpu_buffer->irq_work.full_waiters_pending = false;
2946 /* irq_work_queue() supplies it's own memory barriers */
2947 irq_work_queue(&cpu_buffer->irq_work.work);
Steven Rostedt (Red Hat)d90fd772015-05-29 12:12:27 -04002948}
2949
2950/*
2951 * The lock and unlock are done within a preempt disable section.
2952 * The current_context per_cpu variable can only be modified
2953 * by the current task between lock and unlock. But it can
Steven Rostedt (VMware)a0e3a182018-01-15 10:47:09 -05002954 * be modified more than once via an interrupt. To pass this
2955 * information from the lock to the unlock without having to
2956 * access the 'in_interrupt()' functions again (which do show
2957 * a bit of overhead in something as critical as function tracing,
2958 * we use a bitmask trick.
Steven Rostedt (Red Hat)d90fd772015-05-29 12:12:27 -04002959 *
Steven Rostedt (VMware)a0e3a182018-01-15 10:47:09 -05002960 * bit 0 = NMI context
2961 * bit 1 = IRQ context
2962 * bit 2 = SoftIRQ context
2963 * bit 3 = normal context.
Steven Rostedt (Red Hat)d90fd772015-05-29 12:12:27 -04002964 *
Steven Rostedt (VMware)a0e3a182018-01-15 10:47:09 -05002965 * This works because this is the order of contexts that can
2966 * preempt other contexts. A SoftIRQ never preempts an IRQ
2967 * context.
2968 *
2969 * When the context is determined, the corresponding bit is
2970 * checked and set (if it was set, then a recursion of that context
2971 * happened).
2972 *
2973 * On unlock, we need to clear this bit. To do so, just subtract
2974 * 1 from the current_context and AND it to itself.
2975 *
2976 * (binary)
2977 * 101 - 1 = 100
2978 * 101 & 100 = 100 (clearing bit zero)
2979 *
2980 * 1010 - 1 = 1001
2981 * 1010 & 1001 = 1000 (clearing bit 1)
2982 *
2983 * The least significant bit can be cleared this way, and it
2984 * just so happens that it is the same bit corresponding to
2985 * the current context.
Steven Rostedt (Red Hat)d90fd772015-05-29 12:12:27 -04002986 */
2987
2988static __always_inline int
2989trace_recursive_lock(struct ring_buffer_per_cpu *cpu_buffer)
2990{
Steven Rostedt (VMware)a0e3a182018-01-15 10:47:09 -05002991 unsigned int val = cpu_buffer->current_context;
2992 unsigned long pc = preempt_count();
2993 int bit;
2994
2995 if (!(pc & (NMI_MASK | HARDIRQ_MASK | SOFTIRQ_OFFSET)))
2996 bit = RB_CTX_NORMAL;
2997 else
2998 bit = pc & NMI_MASK ? RB_CTX_NMI :
Steven Rostedt (VMware)0164e0d2018-01-18 15:42:09 -05002999 pc & HARDIRQ_MASK ? RB_CTX_IRQ : RB_CTX_SOFTIRQ;
Steven Rostedt (VMware)a0e3a182018-01-15 10:47:09 -05003000
Steven Rostedt (VMware)8e012062018-02-07 17:26:32 -05003001 if (unlikely(val & (1 << (bit + cpu_buffer->nest))))
Steven Rostedt (Red Hat)d90fd772015-05-29 12:12:27 -04003002 return 1;
3003
Steven Rostedt (VMware)8e012062018-02-07 17:26:32 -05003004 val |= (1 << (bit + cpu_buffer->nest));
Steven Rostedt (VMware)a0e3a182018-01-15 10:47:09 -05003005 cpu_buffer->current_context = val;
Steven Rostedt (Red Hat)d90fd772015-05-29 12:12:27 -04003006
3007 return 0;
3008}
3009
3010static __always_inline void
3011trace_recursive_unlock(struct ring_buffer_per_cpu *cpu_buffer)
3012{
Steven Rostedt (VMware)8e012062018-02-07 17:26:32 -05003013 cpu_buffer->current_context &=
3014 cpu_buffer->current_context - (1 << cpu_buffer->nest);
3015}
3016
3017/* The recursive locking above uses 4 bits */
3018#define NESTED_BITS 4
3019
3020/**
3021 * ring_buffer_nest_start - Allow to trace while nested
3022 * @buffer: The ring buffer to modify
3023 *
Steven Rostedt (VMware)6167c202018-05-16 11:17:06 -04003024 * The ring buffer has a safety mechanism to prevent recursion.
Steven Rostedt (VMware)8e012062018-02-07 17:26:32 -05003025 * But there may be a case where a trace needs to be done while
3026 * tracing something else. In this case, calling this function
3027 * will allow this function to nest within a currently active
3028 * ring_buffer_lock_reserve().
3029 *
3030 * Call this function before calling another ring_buffer_lock_reserve() and
3031 * call ring_buffer_nest_end() after the nested ring_buffer_unlock_commit().
3032 */
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05003033void ring_buffer_nest_start(struct trace_buffer *buffer)
Steven Rostedt (VMware)8e012062018-02-07 17:26:32 -05003034{
3035 struct ring_buffer_per_cpu *cpu_buffer;
3036 int cpu;
3037
3038 /* Enabled by ring_buffer_nest_end() */
3039 preempt_disable_notrace();
3040 cpu = raw_smp_processor_id();
3041 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt (VMware)6167c202018-05-16 11:17:06 -04003042 /* This is the shift value for the above recursive locking */
Steven Rostedt (VMware)8e012062018-02-07 17:26:32 -05003043 cpu_buffer->nest += NESTED_BITS;
3044}
3045
3046/**
3047 * ring_buffer_nest_end - Allow to trace while nested
3048 * @buffer: The ring buffer to modify
3049 *
3050 * Must be called after ring_buffer_nest_start() and after the
3051 * ring_buffer_unlock_commit().
3052 */
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05003053void ring_buffer_nest_end(struct trace_buffer *buffer)
Steven Rostedt (VMware)8e012062018-02-07 17:26:32 -05003054{
3055 struct ring_buffer_per_cpu *cpu_buffer;
3056 int cpu;
3057
3058 /* disabled by ring_buffer_nest_start() */
3059 cpu = raw_smp_processor_id();
3060 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt (VMware)6167c202018-05-16 11:17:06 -04003061 /* This is the shift value for the above recursive locking */
Steven Rostedt (VMware)8e012062018-02-07 17:26:32 -05003062 cpu_buffer->nest -= NESTED_BITS;
3063 preempt_enable_notrace();
Steven Rostedt (Red Hat)d90fd772015-05-29 12:12:27 -04003064}
3065
3066/**
3067 * ring_buffer_unlock_commit - commit a reserved
3068 * @buffer: The buffer to commit to
3069 * @event: The event pointer to commit.
3070 *
3071 * This commits the data to the ring buffer, and releases any locks held.
3072 *
3073 * Must be paired with ring_buffer_lock_reserve.
3074 */
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05003075int ring_buffer_unlock_commit(struct trace_buffer *buffer,
Steven Rostedt (Red Hat)d90fd772015-05-29 12:12:27 -04003076 struct ring_buffer_event *event)
3077{
3078 struct ring_buffer_per_cpu *cpu_buffer;
3079 int cpu = raw_smp_processor_id();
3080
3081 cpu_buffer = buffer->buffers[cpu];
3082
3083 rb_commit(cpu_buffer, event);
3084
3085 rb_wakeups(buffer, cpu_buffer);
3086
3087 trace_recursive_unlock(cpu_buffer);
3088
3089 preempt_enable_notrace();
3090
3091 return 0;
3092}
3093EXPORT_SYMBOL_GPL(ring_buffer_unlock_commit);
Steven Rostedt (Red Hat)a4543a22015-05-29 09:40:18 -04003094
Steven Rostedt (Red Hat)9826b272015-05-28 17:36:45 -04003095static noinline void
Steven Rostedt (VMware)7c4b4a52020-06-28 22:52:26 -04003096rb_check_timestamp(struct ring_buffer_per_cpu *cpu_buffer,
3097 struct rb_event_info *info)
Steven Rostedt (Red Hat)9826b272015-05-28 17:36:45 -04003098{
Steven Rostedt (VMware)10464b42020-06-28 22:52:27 -04003099 u64 write_stamp;
3100
Steven Rostedt (Red Hat)9826b272015-05-28 17:36:45 -04003101 WARN_ONCE(info->delta > (1ULL << 59),
3102 KERN_WARNING "Delta way too big! %llu ts=%llu write stamp = %llu\n%s",
3103 (unsigned long long)info->delta,
3104 (unsigned long long)info->ts,
Steven Rostedt (VMware)10464b42020-06-28 22:52:27 -04003105 (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 -04003106 sched_clock_stable() ? "" :
3107 "If you just came from a suspend/resume,\n"
3108 "please switch to the trace global clock:\n"
Chris Wilson913ea4d2018-03-30 16:01:32 +01003109 " echo global > /sys/kernel/debug/tracing/trace_clock\n"
3110 "or add trace_clock=global to the kernel command line\n");
Steven Rostedt (Red Hat)9826b272015-05-28 17:36:45 -04003111}
3112
Steven Rostedt6634ff22009-05-06 15:30:07 -04003113static struct ring_buffer_event *
3114__rb_reserve_next(struct ring_buffer_per_cpu *cpu_buffer,
Steven Rostedt (Red Hat)fcc742e2015-05-28 17:13:14 -04003115 struct rb_event_info *info)
Steven Rostedt6634ff22009-05-06 15:30:07 -04003116{
Steven Rostedt6634ff22009-05-06 15:30:07 -04003117 struct ring_buffer_event *event;
Steven Rostedt (Red Hat)fcc742e2015-05-28 17:13:14 -04003118 struct buffer_page *tail_page;
Steven Rostedt (VMware)a389d862020-06-28 22:52:25 -04003119 unsigned long tail, write, w;
3120 u64 before, after;
Steven Rostedt (VMware)10464b42020-06-28 22:52:27 -04003121 bool a_ok;
3122 bool b_ok;
Steven Rostedt (Red Hat)b7dc42f2015-09-03 08:57:12 -04003123
Steven Rostedt (VMware)a389d862020-06-28 22:52:25 -04003124 /* Don't let the compiler play games with cpu_buffer->tail_page */
3125 tail_page = info->tail_page = READ_ONCE(cpu_buffer->tail_page);
3126
3127 /*A*/ w = local_read(&tail_page->write) & RB_WRITE_MASK;
3128 barrier();
Steven Rostedt (VMware)10464b42020-06-28 22:52:27 -04003129 b_ok = rb_time_read(&cpu_buffer->before_stamp, &before);
3130 a_ok = rb_time_read(&cpu_buffer->write_stamp, &after);
Steven Rostedt (VMware)a389d862020-06-28 22:52:25 -04003131 barrier();
3132 info->ts = rb_time_stamp(cpu_buffer->buffer);
3133
3134 if (ring_buffer_time_stamp_abs(cpu_buffer->buffer)) {
3135 info->delta = info->ts;
Steven Rostedt (VMware)7c4b4a52020-06-28 22:52:26 -04003136 info->add_timestamp = RB_ADD_STAMP_ABSOLUTE;
Steven Rostedt (VMware)a389d862020-06-28 22:52:25 -04003137 } else {
3138 info->delta = info->ts - after;
3139 }
3140
Steven Rostedt (VMware)10464b42020-06-28 22:52:27 -04003141 if (likely(a_ok && b_ok)) {
3142 if (unlikely(test_time_stamp(info->delta))) {
3143 rb_check_timestamp(cpu_buffer, info);
3144 info->add_timestamp |= RB_ADD_STAMP_EXTEND;
3145 }
Steven Rostedt (VMware)7c4b4a52020-06-28 22:52:26 -04003146 }
Steven Rostedt (VMware)a389d862020-06-28 22:52:25 -04003147
3148 /*
3149 * If interrupting an event time update, we may need an absolute timestamp.
3150 * Don't bother if this is the start of a new page (w == 0).
3151 */
Steven Rostedt (VMware)10464b42020-06-28 22:52:27 -04003152 if (unlikely(!a_ok || !b_ok || (before != after && w)))
Steven Rostedt (VMware)7c4b4a52020-06-28 22:52:26 -04003153 info->add_timestamp |= RB_ADD_STAMP_FORCE | RB_ADD_STAMP_EXTEND;
3154
Steven Rostedt (Red Hat)b7dc42f2015-09-03 08:57:12 -04003155 /*
3156 * If the time delta since the last event is too big to
3157 * hold in the time field of the event, then we append a
3158 * TIME EXTEND event ahead of the data event.
3159 */
3160 if (unlikely(info->add_timestamp))
3161 info->length += RB_LEN_TIME_EXTEND;
Steven Rostedt69d1b832010-10-07 18:18:05 -04003162
Steven Rostedt (VMware)10464b42020-06-28 22:52:27 -04003163 /*B*/ rb_time_set(&cpu_buffer->before_stamp, info->ts);
Steven Rostedt (VMware)a389d862020-06-28 22:52:25 -04003164
3165 /*C*/ write = local_add_return(info->length, &tail_page->write);
Steven Rostedt77ae3652009-03-27 11:00:29 -04003166
3167 /* set write to only the index of the write */
3168 write &= RB_WRITE_MASK;
Steven Rostedt (VMware)a389d862020-06-28 22:52:25 -04003169
Steven Rostedt (Red Hat)fcc742e2015-05-28 17:13:14 -04003170 tail = write - info->length;
Steven Rostedt6634ff22009-05-06 15:30:07 -04003171
Steven Rostedt (VMware)a389d862020-06-28 22:52:25 -04003172 /* See if we shot pass the end of this buffer page */
3173 if (unlikely(write > BUF_PAGE_SIZE)) {
3174 if (tail != w) {
3175 /* before and after may now different, fix it up*/
Steven Rostedt (VMware)10464b42020-06-28 22:52:27 -04003176 b_ok = rb_time_read(&cpu_buffer->before_stamp, &before);
3177 a_ok = rb_time_read(&cpu_buffer->write_stamp, &after);
3178 if (a_ok && b_ok && before != after)
3179 (void)rb_time_cmpxchg(&cpu_buffer->before_stamp, before, after);
Steven Rostedt (VMware)a389d862020-06-28 22:52:25 -04003180 }
3181 return rb_move_tail(cpu_buffer, tail, info);
3182 }
3183
3184 if (likely(tail == w)) {
3185 u64 save_before;
Steven Rostedt (VMware)10464b42020-06-28 22:52:27 -04003186 bool s_ok;
Steven Rostedt (VMware)a389d862020-06-28 22:52:25 -04003187
3188 /* Nothing interrupted us between A and C */
Steven Rostedt (VMware)10464b42020-06-28 22:52:27 -04003189 /*D*/ rb_time_set(&cpu_buffer->write_stamp, info->ts);
Steven Rostedt (VMware)a389d862020-06-28 22:52:25 -04003190 barrier();
Steven Rostedt (VMware)10464b42020-06-28 22:52:27 -04003191 /*E*/ s_ok = rb_time_read(&cpu_buffer->before_stamp, &save_before);
3192 RB_WARN_ON(cpu_buffer, !s_ok);
Steven Rostedt (VMware)7c4b4a52020-06-28 22:52:26 -04003193 if (likely(!(info->add_timestamp &
3194 (RB_ADD_STAMP_FORCE | RB_ADD_STAMP_ABSOLUTE))))
Steven Rostedt (VMware)a389d862020-06-28 22:52:25 -04003195 /* This did not interrupt any time update */
3196 info->delta = info->ts - after;
3197 else
3198 /* Just use full timestamp for inerrupting event */
3199 info->delta = info->ts;
3200 barrier();
3201 if (unlikely(info->ts != save_before)) {
3202 /* SLOW PATH - Interrupted between C and E */
3203
Steven Rostedt (VMware)10464b42020-06-28 22:52:27 -04003204 a_ok = rb_time_read(&cpu_buffer->write_stamp, &after);
3205 RB_WARN_ON(cpu_buffer, !a_ok);
3206
Steven Rostedt (VMware)a389d862020-06-28 22:52:25 -04003207 /* Write stamp must only go forward */
3208 if (save_before > after) {
3209 /*
3210 * We do not care about the result, only that
3211 * it gets updated atomically.
3212 */
Steven Rostedt (VMware)10464b42020-06-28 22:52:27 -04003213 (void)rb_time_cmpxchg(&cpu_buffer->write_stamp, after, save_before);
Steven Rostedt (VMware)a389d862020-06-28 22:52:25 -04003214 }
3215 }
3216 } else {
3217 u64 ts;
3218 /* SLOW PATH - Interrupted between A and C */
Steven Rostedt (VMware)10464b42020-06-28 22:52:27 -04003219 a_ok = rb_time_read(&cpu_buffer->write_stamp, &after);
3220 /* Was interrupted before here, write_stamp must be valid */
3221 RB_WARN_ON(cpu_buffer, !a_ok);
Steven Rostedt (VMware)a389d862020-06-28 22:52:25 -04003222 ts = rb_time_stamp(cpu_buffer->buffer);
3223 barrier();
3224 /*E*/ if (write == (local_read(&tail_page->write) & RB_WRITE_MASK) &&
3225 after < ts) {
3226 /* Nothing came after this event between C and E */
3227 info->delta = ts - after;
Steven Rostedt (VMware)10464b42020-06-28 22:52:27 -04003228 (void)rb_time_cmpxchg(&cpu_buffer->write_stamp, after, info->ts);
Steven Rostedt (VMware)a389d862020-06-28 22:52:25 -04003229 info->ts = ts;
3230 } else {
3231 /*
3232 * Interrupted beween C and E:
3233 * Lost the previous events time stamp. Just set the
3234 * delta to zero, and this will be the same time as
3235 * the event this event interrupted. And the events that
3236 * came after this will still be correct (as they would
3237 * have built their delta on the previous event.
3238 */
3239 info->delta = 0;
3240 }
Steven Rostedt (VMware)7c4b4a52020-06-28 22:52:26 -04003241 info->add_timestamp &= ~RB_ADD_STAMP_FORCE;
Steven Rostedt (VMware)a389d862020-06-28 22:52:25 -04003242 }
3243
Steven Rostedt (Red Hat)b7dc42f2015-09-03 08:57:12 -04003244 /*
3245 * If this is the first commit on the page, then it has the same
3246 * timestamp as the page itself.
3247 */
Steven Rostedt (VMware)7c4b4a52020-06-28 22:52:26 -04003248 if (unlikely(!tail && !(info->add_timestamp &
3249 (RB_ADD_STAMP_FORCE | RB_ADD_STAMP_ABSOLUTE))))
Steven Rostedt (Red Hat)b7dc42f2015-09-03 08:57:12 -04003250 info->delta = 0;
3251
Steven Rostedt6634ff22009-05-06 15:30:07 -04003252 /* We reserved something on the buffer */
Steven Rostedt (Red Hat)b7dc42f2015-09-03 08:57:12 -04003253
Steven Rostedt6634ff22009-05-06 15:30:07 -04003254 event = __rb_page_index(tail_page, tail);
Steven Rostedt (Red Hat)fcc742e2015-05-28 17:13:14 -04003255 rb_update_event(cpu_buffer, event, info);
Steven Rostedt6634ff22009-05-06 15:30:07 -04003256
Steven Rostedt69d1b832010-10-07 18:18:05 -04003257 local_inc(&tail_page->entries);
Steven Rostedt6634ff22009-05-06 15:30:07 -04003258
Steven Rostedt (Red Hat)b7dc42f2015-09-03 08:57:12 -04003259 /*
3260 * If this is the first commit on the page, then update
3261 * its timestamp.
3262 */
3263 if (!tail)
3264 tail_page->page->time_stamp = info->ts;
3265
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -07003266 /* account for these added bytes */
Steven Rostedt (Red Hat)fcc742e2015-05-28 17:13:14 -04003267 local_add(info->length, &cpu_buffer->entries_bytes);
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -07003268
Steven Rostedt6634ff22009-05-06 15:30:07 -04003269 return event;
3270}
3271
Steven Rostedt (Red Hat)fa7ffb32016-11-23 11:36:30 -05003272static __always_inline struct ring_buffer_event *
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05003273rb_reserve_next_event(struct trace_buffer *buffer,
Steven Rostedt62f0b3e2009-09-04 14:11:34 -04003274 struct ring_buffer_per_cpu *cpu_buffer,
Steven Rostedt1cd8d732009-05-11 14:08:09 -04003275 unsigned long length)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003276{
3277 struct ring_buffer_event *event;
Steven Rostedt (Red Hat)fcc742e2015-05-28 17:13:14 -04003278 struct rb_event_info info;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003279 int nr_loops = 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003280
Steven Rostedtfa743952009-06-16 12:37:57 -04003281 rb_start_commit(cpu_buffer);
Steven Rostedt (VMware)a389d862020-06-28 22:52:25 -04003282 /* The commit page can not change after this */
Steven Rostedtfa743952009-06-16 12:37:57 -04003283
Steven Rostedt85bac322009-09-04 14:24:40 -04003284#ifdef CONFIG_RING_BUFFER_ALLOW_SWAP
Steven Rostedt62f0b3e2009-09-04 14:11:34 -04003285 /*
3286 * Due to the ability to swap a cpu buffer from a buffer
3287 * it is possible it was swapped before we committed.
3288 * (committing stops a swap). We check for it here and
3289 * if it happened, we have to fail the write.
3290 */
3291 barrier();
Mark Rutland6aa7de02017-10-23 14:07:29 -07003292 if (unlikely(READ_ONCE(cpu_buffer->buffer) != buffer)) {
Steven Rostedt62f0b3e2009-09-04 14:11:34 -04003293 local_dec(&cpu_buffer->committing);
3294 local_dec(&cpu_buffer->commits);
3295 return NULL;
3296 }
Steven Rostedt85bac322009-09-04 14:24:40 -04003297#endif
Steven Rostedt (Red Hat)b7dc42f2015-09-03 08:57:12 -04003298
Steven Rostedt (Red Hat)fcc742e2015-05-28 17:13:14 -04003299 info.length = rb_calculate_event_length(length);
Steven Rostedt (Red Hat)a4543a22015-05-29 09:40:18 -04003300 again:
Steven Rostedt (VMware)a389d862020-06-28 22:52:25 -04003301 info.add_timestamp = RB_ADD_STAMP_NONE;
Steven Rostedt (Red Hat)b7dc42f2015-09-03 08:57:12 -04003302 info.delta = 0;
3303
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003304 /*
3305 * We allow for interrupts to reenter here and do a trace.
3306 * If one does, it will cause this original code to loop
3307 * back here. Even with heavy interrupts happening, this
3308 * should only happen a few times in a row. If this happens
3309 * 1000 times in a row, there must be either an interrupt
3310 * storm or we have something buggy.
3311 * Bail!
3312 */
Steven Rostedt3e89c7bb2008-11-11 15:28:41 -05003313 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 1000))
Steven Rostedtfa743952009-06-16 12:37:57 -04003314 goto out_fail;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003315
Steven Rostedt (Red Hat)fcc742e2015-05-28 17:13:14 -04003316 event = __rb_reserve_next(cpu_buffer, &info);
3317
Steven Rostedt (Red Hat)bd1b7cd2015-11-23 17:35:24 -05003318 if (unlikely(PTR_ERR(event) == -EAGAIN)) {
3319 if (info.add_timestamp)
3320 info.length -= RB_LEN_TIME_EXTEND;
Steven Rostedtbf41a152008-10-04 02:00:59 -04003321 goto again;
Steven Rostedt (Red Hat)bd1b7cd2015-11-23 17:35:24 -05003322 }
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003323
Steven Rostedt (VMware)a389d862020-06-28 22:52:25 -04003324 if (likely(event))
3325 return event;
Steven Rostedtfa743952009-06-16 12:37:57 -04003326 out_fail:
3327 rb_end_commit(cpu_buffer);
3328 return NULL;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003329}
3330
3331/**
3332 * ring_buffer_lock_reserve - reserve a part of the buffer
3333 * @buffer: the ring buffer to reserve from
3334 * @length: the length of the data to reserve (excluding event header)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003335 *
Steven Rostedt (VMware)6167c202018-05-16 11:17:06 -04003336 * Returns a reserved event on the ring buffer to copy directly to.
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003337 * The user of this interface will need to get the body to write into
3338 * and can use the ring_buffer_event_data() interface.
3339 *
3340 * The length is the length of the data needed, not the event length
3341 * which also includes the event header.
3342 *
3343 * Must be paired with ring_buffer_unlock_commit, unless NULL is returned.
3344 * If NULL is returned, then nothing has been allocated or locked.
3345 */
3346struct ring_buffer_event *
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05003347ring_buffer_lock_reserve(struct trace_buffer *buffer, unsigned long length)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003348{
3349 struct ring_buffer_per_cpu *cpu_buffer;
3350 struct ring_buffer_event *event;
Steven Rostedt5168ae52010-06-03 09:36:50 -04003351 int cpu;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003352
Steven Rostedtbf41a152008-10-04 02:00:59 -04003353 /* If we are tracing schedule, we don't want to recurse */
Steven Rostedt5168ae52010-06-03 09:36:50 -04003354 preempt_disable_notrace();
Steven Rostedtbf41a152008-10-04 02:00:59 -04003355
Steven Rostedt (Red Hat)3205f802015-05-21 17:39:29 -04003356 if (unlikely(atomic_read(&buffer->record_disabled)))
Steven Rostedt (Red Hat)58a09ec2015-05-27 10:27:47 -04003357 goto out;
Steven Rostedt261842b2009-04-16 21:41:52 -04003358
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003359 cpu = raw_smp_processor_id();
3360
Steven Rostedt (Red Hat)3205f802015-05-21 17:39:29 -04003361 if (unlikely(!cpumask_test_cpu(cpu, buffer->cpumask)))
Steven Rostedtd7690412008-10-01 00:29:53 -04003362 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003363
3364 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003365
Steven Rostedt (Red Hat)3205f802015-05-21 17:39:29 -04003366 if (unlikely(atomic_read(&cpu_buffer->record_disabled)))
Steven Rostedtd7690412008-10-01 00:29:53 -04003367 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003368
Steven Rostedt (Red Hat)3205f802015-05-21 17:39:29 -04003369 if (unlikely(length > BUF_MAX_DATA_SIZE))
Steven Rostedtbf41a152008-10-04 02:00:59 -04003370 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003371
Steven Rostedt (Red Hat)58a09ec2015-05-27 10:27:47 -04003372 if (unlikely(trace_recursive_lock(cpu_buffer)))
3373 goto out;
3374
Steven Rostedt62f0b3e2009-09-04 14:11:34 -04003375 event = rb_reserve_next_event(buffer, cpu_buffer, length);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003376 if (!event)
Steven Rostedt (Red Hat)58a09ec2015-05-27 10:27:47 -04003377 goto out_unlock;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003378
3379 return event;
3380
Steven Rostedt (Red Hat)58a09ec2015-05-27 10:27:47 -04003381 out_unlock:
3382 trace_recursive_unlock(cpu_buffer);
Steven Rostedtd7690412008-10-01 00:29:53 -04003383 out:
Steven Rostedt5168ae52010-06-03 09:36:50 -04003384 preempt_enable_notrace();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003385 return NULL;
3386}
Robert Richterc4f50182008-12-11 16:49:22 +01003387EXPORT_SYMBOL_GPL(ring_buffer_lock_reserve);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003388
Steven Rostedta1863c22009-09-03 10:23:58 -04003389/*
3390 * Decrement the entries to the page that an event is on.
3391 * The event does not even need to exist, only the pointer
3392 * to the page it is on. This may only be called before the commit
3393 * takes place.
3394 */
3395static inline void
3396rb_decrement_entry(struct ring_buffer_per_cpu *cpu_buffer,
3397 struct ring_buffer_event *event)
3398{
3399 unsigned long addr = (unsigned long)event;
3400 struct buffer_page *bpage = cpu_buffer->commit_page;
3401 struct buffer_page *start;
3402
3403 addr &= PAGE_MASK;
3404
3405 /* Do the likely case first */
3406 if (likely(bpage->page == (void *)addr)) {
3407 local_dec(&bpage->entries);
3408 return;
3409 }
3410
3411 /*
3412 * Because the commit page may be on the reader page we
3413 * start with the next page and check the end loop there.
3414 */
3415 rb_inc_page(cpu_buffer, &bpage);
3416 start = bpage;
3417 do {
3418 if (bpage->page == (void *)addr) {
3419 local_dec(&bpage->entries);
3420 return;
3421 }
3422 rb_inc_page(cpu_buffer, &bpage);
3423 } while (bpage != start);
3424
3425 /* commit not part of this buffer?? */
3426 RB_WARN_ON(cpu_buffer, 1);
3427}
3428
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003429/**
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04003430 * ring_buffer_commit_discard - discard an event that has not been committed
3431 * @buffer: the ring buffer
3432 * @event: non committed event to discard
3433 *
Steven Rostedtdc892f72009-09-03 15:33:41 -04003434 * Sometimes an event that is in the ring buffer needs to be ignored.
3435 * This function lets the user discard an event in the ring buffer
3436 * and then that event will not be read later.
3437 *
Steven Rostedt (VMware)6167c202018-05-16 11:17:06 -04003438 * This function only works if it is called before the item has been
Steven Rostedtdc892f72009-09-03 15:33:41 -04003439 * committed. It will try to free the event from the ring buffer
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04003440 * if another event has not been added behind it.
3441 *
3442 * If another event has been added behind it, it will set the event
3443 * up as discarded, and perform the commit.
3444 *
3445 * If this function is called, do not call ring_buffer_unlock_commit on
3446 * the event.
3447 */
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05003448void ring_buffer_discard_commit(struct trace_buffer *buffer,
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04003449 struct ring_buffer_event *event)
3450{
3451 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04003452 int cpu;
3453
3454 /* The event is discarded regardless */
Frederic Weisbeckerf3b9aae2009-04-19 23:39:33 +02003455 rb_event_discard(event);
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04003456
Steven Rostedtfa743952009-06-16 12:37:57 -04003457 cpu = smp_processor_id();
3458 cpu_buffer = buffer->buffers[cpu];
3459
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04003460 /*
3461 * This must only be called if the event has not been
3462 * committed yet. Thus we can assume that preemption
3463 * is still disabled.
3464 */
Steven Rostedtfa743952009-06-16 12:37:57 -04003465 RB_WARN_ON(buffer, !local_read(&cpu_buffer->committing));
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04003466
Steven Rostedta1863c22009-09-03 10:23:58 -04003467 rb_decrement_entry(cpu_buffer, event);
Steven Rostedt0f2541d2009-08-05 12:02:48 -04003468 if (rb_try_to_discard(cpu_buffer, event))
Steven Rostedtedd813bf2009-06-02 23:00:53 -04003469 goto out;
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04003470
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04003471 out:
Steven Rostedtfa743952009-06-16 12:37:57 -04003472 rb_end_commit(cpu_buffer);
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04003473
Steven Rostedt (Red Hat)58a09ec2015-05-27 10:27:47 -04003474 trace_recursive_unlock(cpu_buffer);
Frederic Weisbeckerf3b9aae2009-04-19 23:39:33 +02003475
Steven Rostedt5168ae52010-06-03 09:36:50 -04003476 preempt_enable_notrace();
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04003477
3478}
3479EXPORT_SYMBOL_GPL(ring_buffer_discard_commit);
3480
3481/**
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003482 * ring_buffer_write - write data to the buffer without reserving
3483 * @buffer: The ring buffer to write to.
3484 * @length: The length of the data being written (excluding the event header)
3485 * @data: The data to write to the buffer.
3486 *
3487 * This is like ring_buffer_lock_reserve and ring_buffer_unlock_commit as
3488 * one function. If you already have the data to write to the buffer, it
3489 * may be easier to simply call this function.
3490 *
3491 * Note, like ring_buffer_lock_reserve, the length is the length of the data
3492 * and not the length of the event which would hold the header.
3493 */
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05003494int ring_buffer_write(struct trace_buffer *buffer,
David Sharp01e3e712012-06-07 16:46:24 -07003495 unsigned long length,
3496 void *data)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003497{
3498 struct ring_buffer_per_cpu *cpu_buffer;
3499 struct ring_buffer_event *event;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003500 void *body;
3501 int ret = -EBUSY;
Steven Rostedt5168ae52010-06-03 09:36:50 -04003502 int cpu;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003503
Steven Rostedt5168ae52010-06-03 09:36:50 -04003504 preempt_disable_notrace();
Steven Rostedtbf41a152008-10-04 02:00:59 -04003505
Lai Jiangshan52fbe9c2010-03-08 14:50:43 +08003506 if (atomic_read(&buffer->record_disabled))
3507 goto out;
3508
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003509 cpu = raw_smp_processor_id();
3510
Rusty Russell9e01c1b2009-01-01 10:12:22 +10303511 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedtd7690412008-10-01 00:29:53 -04003512 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003513
3514 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003515
3516 if (atomic_read(&cpu_buffer->record_disabled))
3517 goto out;
3518
Steven Rostedtbe957c42009-05-11 14:42:53 -04003519 if (length > BUF_MAX_DATA_SIZE)
3520 goto out;
3521
Steven Rostedt (Red Hat)985e8712015-05-27 10:48:56 -04003522 if (unlikely(trace_recursive_lock(cpu_buffer)))
3523 goto out;
3524
Steven Rostedt62f0b3e2009-09-04 14:11:34 -04003525 event = rb_reserve_next_event(buffer, cpu_buffer, length);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003526 if (!event)
Steven Rostedt (Red Hat)985e8712015-05-27 10:48:56 -04003527 goto out_unlock;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003528
3529 body = rb_event_data(event);
3530
3531 memcpy(body, data, length);
3532
3533 rb_commit(cpu_buffer, event);
3534
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -05003535 rb_wakeups(buffer, cpu_buffer);
3536
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003537 ret = 0;
Steven Rostedt (Red Hat)985e8712015-05-27 10:48:56 -04003538
3539 out_unlock:
3540 trace_recursive_unlock(cpu_buffer);
3541
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003542 out:
Steven Rostedt5168ae52010-06-03 09:36:50 -04003543 preempt_enable_notrace();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003544
3545 return ret;
3546}
Robert Richterc4f50182008-12-11 16:49:22 +01003547EXPORT_SYMBOL_GPL(ring_buffer_write);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003548
Yaowei Baida588342015-09-29 22:43:33 +08003549static bool rb_per_cpu_empty(struct ring_buffer_per_cpu *cpu_buffer)
Steven Rostedtbf41a152008-10-04 02:00:59 -04003550{
3551 struct buffer_page *reader = cpu_buffer->reader_page;
Steven Rostedt77ae3652009-03-27 11:00:29 -04003552 struct buffer_page *head = rb_set_head_page(cpu_buffer);
Steven Rostedtbf41a152008-10-04 02:00:59 -04003553 struct buffer_page *commit = cpu_buffer->commit_page;
3554
Steven Rostedt77ae3652009-03-27 11:00:29 -04003555 /* In case of error, head will be NULL */
3556 if (unlikely(!head))
Yaowei Baida588342015-09-29 22:43:33 +08003557 return true;
Steven Rostedt77ae3652009-03-27 11:00:29 -04003558
Steven Rostedtbf41a152008-10-04 02:00:59 -04003559 return reader->read == rb_page_commit(reader) &&
3560 (commit == reader ||
3561 (commit == head &&
3562 head->read == rb_page_commit(commit)));
3563}
3564
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003565/**
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003566 * ring_buffer_record_disable - stop all writes into the buffer
3567 * @buffer: The ring buffer to stop writes to.
3568 *
3569 * This prevents all writes to the buffer. Any attempt to write
3570 * to the buffer after this will fail and return NULL.
3571 *
Paul E. McKenney74401722018-11-06 18:44:52 -08003572 * The caller should call synchronize_rcu() after this.
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003573 */
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05003574void ring_buffer_record_disable(struct trace_buffer *buffer)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003575{
3576 atomic_inc(&buffer->record_disabled);
3577}
Robert Richterc4f50182008-12-11 16:49:22 +01003578EXPORT_SYMBOL_GPL(ring_buffer_record_disable);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003579
3580/**
3581 * ring_buffer_record_enable - enable writes to the buffer
3582 * @buffer: The ring buffer to enable writes
3583 *
3584 * Note, multiple disables will need the same number of enables
Adam Buchbinderc41b20e2009-12-11 16:35:39 -05003585 * to truly enable the writing (much like preempt_disable).
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003586 */
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05003587void ring_buffer_record_enable(struct trace_buffer *buffer)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003588{
3589 atomic_dec(&buffer->record_disabled);
3590}
Robert Richterc4f50182008-12-11 16:49:22 +01003591EXPORT_SYMBOL_GPL(ring_buffer_record_enable);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003592
3593/**
Steven Rostedt499e5472012-02-22 15:50:28 -05003594 * ring_buffer_record_off - stop all writes into the buffer
3595 * @buffer: The ring buffer to stop writes to.
3596 *
3597 * This prevents all writes to the buffer. Any attempt to write
3598 * to the buffer after this will fail and return NULL.
3599 *
3600 * This is different than ring_buffer_record_disable() as
Wang Tianhong87abb3b2012-08-02 14:02:00 +08003601 * it works like an on/off switch, where as the disable() version
Steven Rostedt499e5472012-02-22 15:50:28 -05003602 * must be paired with a enable().
3603 */
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05003604void ring_buffer_record_off(struct trace_buffer *buffer)
Steven Rostedt499e5472012-02-22 15:50:28 -05003605{
3606 unsigned int rd;
3607 unsigned int new_rd;
3608
3609 do {
3610 rd = atomic_read(&buffer->record_disabled);
3611 new_rd = rd | RB_BUFFER_OFF;
3612 } while (atomic_cmpxchg(&buffer->record_disabled, rd, new_rd) != rd);
3613}
3614EXPORT_SYMBOL_GPL(ring_buffer_record_off);
3615
3616/**
3617 * ring_buffer_record_on - restart writes into the buffer
3618 * @buffer: The ring buffer to start writes to.
3619 *
3620 * This enables all writes to the buffer that was disabled by
3621 * ring_buffer_record_off().
3622 *
3623 * This is different than ring_buffer_record_enable() as
Wang Tianhong87abb3b2012-08-02 14:02:00 +08003624 * it works like an on/off switch, where as the enable() version
Steven Rostedt499e5472012-02-22 15:50:28 -05003625 * must be paired with a disable().
3626 */
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05003627void ring_buffer_record_on(struct trace_buffer *buffer)
Steven Rostedt499e5472012-02-22 15:50:28 -05003628{
3629 unsigned int rd;
3630 unsigned int new_rd;
3631
3632 do {
3633 rd = atomic_read(&buffer->record_disabled);
3634 new_rd = rd & ~RB_BUFFER_OFF;
3635 } while (atomic_cmpxchg(&buffer->record_disabled, rd, new_rd) != rd);
3636}
3637EXPORT_SYMBOL_GPL(ring_buffer_record_on);
3638
3639/**
3640 * ring_buffer_record_is_on - return true if the ring buffer can write
3641 * @buffer: The ring buffer to see if write is enabled
3642 *
3643 * Returns true if the ring buffer is in a state that it accepts writes.
3644 */
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05003645bool ring_buffer_record_is_on(struct trace_buffer *buffer)
Steven Rostedt499e5472012-02-22 15:50:28 -05003646{
3647 return !atomic_read(&buffer->record_disabled);
3648}
3649
3650/**
Masami Hiramatsu73c8d892018-07-14 01:28:15 +09003651 * ring_buffer_record_is_set_on - return true if the ring buffer is set writable
3652 * @buffer: The ring buffer to see if write is set enabled
3653 *
3654 * Returns true if the ring buffer is set writable by ring_buffer_record_on().
3655 * Note that this does NOT mean it is in a writable state.
3656 *
3657 * It may return true when the ring buffer has been disabled by
3658 * ring_buffer_record_disable(), as that is a temporary disabling of
3659 * the ring buffer.
3660 */
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05003661bool ring_buffer_record_is_set_on(struct trace_buffer *buffer)
Masami Hiramatsu73c8d892018-07-14 01:28:15 +09003662{
3663 return !(atomic_read(&buffer->record_disabled) & RB_BUFFER_OFF);
3664}
3665
3666/**
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003667 * ring_buffer_record_disable_cpu - stop all writes into the cpu_buffer
3668 * @buffer: The ring buffer to stop writes to.
3669 * @cpu: The CPU buffer to stop
3670 *
3671 * This prevents all writes to the buffer. Any attempt to write
3672 * to the buffer after this will fail and return NULL.
3673 *
Paul E. McKenney74401722018-11-06 18:44:52 -08003674 * The caller should call synchronize_rcu() after this.
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003675 */
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05003676void ring_buffer_record_disable_cpu(struct trace_buffer *buffer, int cpu)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003677{
3678 struct ring_buffer_per_cpu *cpu_buffer;
3679
Rusty Russell9e01c1b2009-01-01 10:12:22 +10303680 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04003681 return;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003682
3683 cpu_buffer = buffer->buffers[cpu];
3684 atomic_inc(&cpu_buffer->record_disabled);
3685}
Robert Richterc4f50182008-12-11 16:49:22 +01003686EXPORT_SYMBOL_GPL(ring_buffer_record_disable_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003687
3688/**
3689 * ring_buffer_record_enable_cpu - enable writes to the buffer
3690 * @buffer: The ring buffer to enable writes
3691 * @cpu: The CPU to enable.
3692 *
3693 * Note, multiple disables will need the same number of enables
Adam Buchbinderc41b20e2009-12-11 16:35:39 -05003694 * to truly enable the writing (much like preempt_disable).
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003695 */
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05003696void ring_buffer_record_enable_cpu(struct trace_buffer *buffer, int cpu)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003697{
3698 struct ring_buffer_per_cpu *cpu_buffer;
3699
Rusty Russell9e01c1b2009-01-01 10:12:22 +10303700 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04003701 return;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003702
3703 cpu_buffer = buffer->buffers[cpu];
3704 atomic_dec(&cpu_buffer->record_disabled);
3705}
Robert Richterc4f50182008-12-11 16:49:22 +01003706EXPORT_SYMBOL_GPL(ring_buffer_record_enable_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003707
Steven Rostedtf6195aa2010-09-01 12:23:12 -04003708/*
3709 * The total entries in the ring buffer is the running counter
3710 * of entries entered into the ring buffer, minus the sum of
3711 * the entries read from the ring buffer and the number of
3712 * entries that were overwritten.
3713 */
3714static inline unsigned long
3715rb_num_of_entries(struct ring_buffer_per_cpu *cpu_buffer)
3716{
3717 return local_read(&cpu_buffer->entries) -
3718 (local_read(&cpu_buffer->overrun) + cpu_buffer->read);
3719}
3720
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003721/**
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -07003722 * ring_buffer_oldest_event_ts - get the oldest event timestamp from the buffer
3723 * @buffer: The ring buffer
3724 * @cpu: The per CPU buffer to read from.
3725 */
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05003726u64 ring_buffer_oldest_event_ts(struct trace_buffer *buffer, int cpu)
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -07003727{
3728 unsigned long flags;
3729 struct ring_buffer_per_cpu *cpu_buffer;
3730 struct buffer_page *bpage;
Linus Torvaldsda830e52012-12-11 18:18:58 -08003731 u64 ret = 0;
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -07003732
3733 if (!cpumask_test_cpu(cpu, buffer->cpumask))
3734 return 0;
3735
3736 cpu_buffer = buffer->buffers[cpu];
Linus Torvalds7115e3f2011-10-26 17:03:38 +02003737 raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -07003738 /*
3739 * if the tail is on reader_page, oldest time stamp is on the reader
3740 * page
3741 */
3742 if (cpu_buffer->tail_page == cpu_buffer->reader_page)
3743 bpage = cpu_buffer->reader_page;
3744 else
3745 bpage = rb_set_head_page(cpu_buffer);
Steven Rostedt54f7be52012-11-29 22:27:22 -05003746 if (bpage)
3747 ret = bpage->page->time_stamp;
Linus Torvalds7115e3f2011-10-26 17:03:38 +02003748 raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -07003749
3750 return ret;
3751}
3752EXPORT_SYMBOL_GPL(ring_buffer_oldest_event_ts);
3753
3754/**
3755 * ring_buffer_bytes_cpu - get the number of bytes consumed in a cpu buffer
3756 * @buffer: The ring buffer
3757 * @cpu: The per CPU buffer to read from.
3758 */
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05003759unsigned long ring_buffer_bytes_cpu(struct trace_buffer *buffer, int cpu)
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -07003760{
3761 struct ring_buffer_per_cpu *cpu_buffer;
3762 unsigned long ret;
3763
3764 if (!cpumask_test_cpu(cpu, buffer->cpumask))
3765 return 0;
3766
3767 cpu_buffer = buffer->buffers[cpu];
3768 ret = local_read(&cpu_buffer->entries_bytes) - cpu_buffer->read_bytes;
3769
3770 return ret;
3771}
3772EXPORT_SYMBOL_GPL(ring_buffer_bytes_cpu);
3773
3774/**
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003775 * ring_buffer_entries_cpu - get the number of entries in a cpu buffer
3776 * @buffer: The ring buffer
3777 * @cpu: The per CPU buffer to get the entries from.
3778 */
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05003779unsigned long ring_buffer_entries_cpu(struct trace_buffer *buffer, int cpu)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003780{
3781 struct ring_buffer_per_cpu *cpu_buffer;
3782
Rusty Russell9e01c1b2009-01-01 10:12:22 +10303783 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04003784 return 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003785
3786 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt554f7862009-03-11 22:00:13 -04003787
Steven Rostedtf6195aa2010-09-01 12:23:12 -04003788 return rb_num_of_entries(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003789}
Robert Richterc4f50182008-12-11 16:49:22 +01003790EXPORT_SYMBOL_GPL(ring_buffer_entries_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003791
3792/**
Slava Pestov884bfe82011-07-15 14:23:58 -07003793 * ring_buffer_overrun_cpu - get the number of overruns caused by the ring
3794 * buffer wrapping around (only if RB_FL_OVERWRITE is on).
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003795 * @buffer: The ring buffer
3796 * @cpu: The per CPU buffer to get the number of overruns from
3797 */
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05003798unsigned long ring_buffer_overrun_cpu(struct trace_buffer *buffer, int cpu)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003799{
3800 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedt8aabee52009-03-12 13:13:49 -04003801 unsigned long ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003802
Rusty Russell9e01c1b2009-01-01 10:12:22 +10303803 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04003804 return 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003805
3806 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt77ae3652009-03-27 11:00:29 -04003807 ret = local_read(&cpu_buffer->overrun);
Steven Rostedt554f7862009-03-11 22:00:13 -04003808
3809 return ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003810}
Robert Richterc4f50182008-12-11 16:49:22 +01003811EXPORT_SYMBOL_GPL(ring_buffer_overrun_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003812
3813/**
Slava Pestov884bfe82011-07-15 14:23:58 -07003814 * ring_buffer_commit_overrun_cpu - get the number of overruns caused by
3815 * commits failing due to the buffer wrapping around while there are uncommitted
3816 * events, such as during an interrupt storm.
Steven Rostedtf0d2c682009-04-29 13:43:37 -04003817 * @buffer: The ring buffer
3818 * @cpu: The per CPU buffer to get the number of overruns from
3819 */
3820unsigned long
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05003821ring_buffer_commit_overrun_cpu(struct trace_buffer *buffer, int cpu)
Steven Rostedtf0d2c682009-04-29 13:43:37 -04003822{
3823 struct ring_buffer_per_cpu *cpu_buffer;
3824 unsigned long ret;
3825
3826 if (!cpumask_test_cpu(cpu, buffer->cpumask))
3827 return 0;
3828
3829 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt77ae3652009-03-27 11:00:29 -04003830 ret = local_read(&cpu_buffer->commit_overrun);
Steven Rostedtf0d2c682009-04-29 13:43:37 -04003831
3832 return ret;
3833}
3834EXPORT_SYMBOL_GPL(ring_buffer_commit_overrun_cpu);
3835
3836/**
Slava Pestov884bfe82011-07-15 14:23:58 -07003837 * ring_buffer_dropped_events_cpu - get the number of dropped events caused by
3838 * the ring buffer filling up (only if RB_FL_OVERWRITE is off).
3839 * @buffer: The ring buffer
3840 * @cpu: The per CPU buffer to get the number of overruns from
3841 */
3842unsigned long
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05003843ring_buffer_dropped_events_cpu(struct trace_buffer *buffer, int cpu)
Slava Pestov884bfe82011-07-15 14:23:58 -07003844{
3845 struct ring_buffer_per_cpu *cpu_buffer;
3846 unsigned long ret;
3847
3848 if (!cpumask_test_cpu(cpu, buffer->cpumask))
3849 return 0;
3850
3851 cpu_buffer = buffer->buffers[cpu];
3852 ret = local_read(&cpu_buffer->dropped_events);
3853
3854 return ret;
3855}
3856EXPORT_SYMBOL_GPL(ring_buffer_dropped_events_cpu);
3857
3858/**
Steven Rostedt (Red Hat)ad964702013-01-29 17:45:49 -05003859 * ring_buffer_read_events_cpu - get the number of events successfully read
3860 * @buffer: The ring buffer
3861 * @cpu: The per CPU buffer to get the number of events read
3862 */
3863unsigned long
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05003864ring_buffer_read_events_cpu(struct trace_buffer *buffer, int cpu)
Steven Rostedt (Red Hat)ad964702013-01-29 17:45:49 -05003865{
3866 struct ring_buffer_per_cpu *cpu_buffer;
3867
3868 if (!cpumask_test_cpu(cpu, buffer->cpumask))
3869 return 0;
3870
3871 cpu_buffer = buffer->buffers[cpu];
3872 return cpu_buffer->read;
3873}
3874EXPORT_SYMBOL_GPL(ring_buffer_read_events_cpu);
3875
3876/**
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003877 * ring_buffer_entries - get the number of entries in a buffer
3878 * @buffer: The ring buffer
3879 *
3880 * Returns the total number of entries in the ring buffer
3881 * (all CPU entries)
3882 */
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05003883unsigned long ring_buffer_entries(struct trace_buffer *buffer)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003884{
3885 struct ring_buffer_per_cpu *cpu_buffer;
3886 unsigned long entries = 0;
3887 int cpu;
3888
3889 /* if you care about this being correct, lock the buffer */
3890 for_each_buffer_cpu(buffer, cpu) {
3891 cpu_buffer = buffer->buffers[cpu];
Steven Rostedtf6195aa2010-09-01 12:23:12 -04003892 entries += rb_num_of_entries(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003893 }
3894
3895 return entries;
3896}
Robert Richterc4f50182008-12-11 16:49:22 +01003897EXPORT_SYMBOL_GPL(ring_buffer_entries);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003898
3899/**
Jiri Olsa67b394f2009-10-23 19:36:18 -04003900 * ring_buffer_overruns - get the number of overruns in buffer
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003901 * @buffer: The ring buffer
3902 *
3903 * Returns the total number of overruns in the ring buffer
3904 * (all CPU entries)
3905 */
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05003906unsigned long ring_buffer_overruns(struct trace_buffer *buffer)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003907{
3908 struct ring_buffer_per_cpu *cpu_buffer;
3909 unsigned long overruns = 0;
3910 int cpu;
3911
3912 /* if you care about this being correct, lock the buffer */
3913 for_each_buffer_cpu(buffer, cpu) {
3914 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt77ae3652009-03-27 11:00:29 -04003915 overruns += local_read(&cpu_buffer->overrun);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003916 }
3917
3918 return overruns;
3919}
Robert Richterc4f50182008-12-11 16:49:22 +01003920EXPORT_SYMBOL_GPL(ring_buffer_overruns);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003921
Steven Rostedt642edba2008-11-12 00:01:26 -05003922static void rb_iter_reset(struct ring_buffer_iter *iter)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003923{
3924 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
3925
Steven Rostedtd7690412008-10-01 00:29:53 -04003926 /* Iterator usage is expected to have record disabled */
Steven Rostedt (Red Hat)651e22f2014-08-06 14:11:33 -04003927 iter->head_page = cpu_buffer->reader_page;
3928 iter->head = cpu_buffer->reader_page->read;
Steven Rostedt (VMware)785888c2020-03-17 17:32:27 -04003929 iter->next_event = iter->head;
Steven Rostedt (Red Hat)651e22f2014-08-06 14:11:33 -04003930
3931 iter->cache_reader_page = iter->head_page;
Steven Rostedt (Red Hat)24607f12014-10-02 16:51:18 -04003932 iter->cache_read = cpu_buffer->read;
Steven Rostedt (Red Hat)651e22f2014-08-06 14:11:33 -04003933
Steven Rostedt (VMware)28e3fc52020-03-17 17:32:26 -04003934 if (iter->head) {
Steven Rostedtd7690412008-10-01 00:29:53 -04003935 iter->read_stamp = cpu_buffer->read_stamp;
Steven Rostedt (VMware)28e3fc52020-03-17 17:32:26 -04003936 iter->page_stamp = cpu_buffer->reader_page->page->time_stamp;
3937 } else {
Steven Rostedtabc9b562008-12-02 15:34:06 -05003938 iter->read_stamp = iter->head_page->page->time_stamp;
Steven Rostedt (VMware)28e3fc52020-03-17 17:32:26 -04003939 iter->page_stamp = iter->read_stamp;
3940 }
Steven Rostedt642edba2008-11-12 00:01:26 -05003941}
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003942
Steven Rostedt642edba2008-11-12 00:01:26 -05003943/**
3944 * ring_buffer_iter_reset - reset an iterator
3945 * @iter: The iterator to reset
3946 *
3947 * Resets the iterator, so that it will start from the beginning
3948 * again.
3949 */
3950void ring_buffer_iter_reset(struct ring_buffer_iter *iter)
3951{
Steven Rostedt554f7862009-03-11 22:00:13 -04003952 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedt642edba2008-11-12 00:01:26 -05003953 unsigned long flags;
3954
Steven Rostedt554f7862009-03-11 22:00:13 -04003955 if (!iter)
3956 return;
3957
3958 cpu_buffer = iter->cpu_buffer;
3959
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02003960 raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
Steven Rostedt642edba2008-11-12 00:01:26 -05003961 rb_iter_reset(iter);
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02003962 raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003963}
Robert Richterc4f50182008-12-11 16:49:22 +01003964EXPORT_SYMBOL_GPL(ring_buffer_iter_reset);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003965
3966/**
3967 * ring_buffer_iter_empty - check if an iterator has no more to read
3968 * @iter: The iterator to check
3969 */
3970int ring_buffer_iter_empty(struct ring_buffer_iter *iter)
3971{
3972 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedt (VMware)78f7a452017-04-19 14:29:46 -04003973 struct buffer_page *reader;
3974 struct buffer_page *head_page;
3975 struct buffer_page *commit_page;
Steven Rostedt (VMware)ead6ecf2020-03-17 17:32:24 -04003976 struct buffer_page *curr_commit_page;
Steven Rostedt (VMware)78f7a452017-04-19 14:29:46 -04003977 unsigned commit;
Steven Rostedt (VMware)ead6ecf2020-03-17 17:32:24 -04003978 u64 curr_commit_ts;
3979 u64 commit_ts;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003980
3981 cpu_buffer = iter->cpu_buffer;
Steven Rostedt (VMware)78f7a452017-04-19 14:29:46 -04003982 reader = cpu_buffer->reader_page;
3983 head_page = cpu_buffer->head_page;
3984 commit_page = cpu_buffer->commit_page;
Steven Rostedt (VMware)ead6ecf2020-03-17 17:32:24 -04003985 commit_ts = commit_page->page->time_stamp;
Steven Rostedt (VMware)78f7a452017-04-19 14:29:46 -04003986
Steven Rostedt (VMware)ead6ecf2020-03-17 17:32:24 -04003987 /*
3988 * When the writer goes across pages, it issues a cmpxchg which
3989 * is a mb(), which will synchronize with the rmb here.
3990 * (see rb_tail_page_update())
3991 */
3992 smp_rmb();
3993 commit = rb_page_commit(commit_page);
3994 /* We want to make sure that the commit page doesn't change */
3995 smp_rmb();
3996
3997 /* Make sure commit page didn't change */
3998 curr_commit_page = READ_ONCE(cpu_buffer->commit_page);
3999 curr_commit_ts = READ_ONCE(curr_commit_page->page->time_stamp);
4000
4001 /* If the commit page changed, then there's more data */
4002 if (curr_commit_page != commit_page ||
4003 curr_commit_ts != commit_ts)
4004 return 0;
4005
4006 /* Still racy, as it may return a false positive, but that's OK */
Steven Rostedt (VMware)785888c2020-03-17 17:32:27 -04004007 return ((iter->head_page == commit_page && iter->head >= commit) ||
Steven Rostedt (VMware)78f7a452017-04-19 14:29:46 -04004008 (iter->head_page == reader && commit_page == head_page &&
4009 head_page->read == commit &&
4010 iter->head == rb_page_commit(cpu_buffer->reader_page)));
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004011}
Robert Richterc4f50182008-12-11 16:49:22 +01004012EXPORT_SYMBOL_GPL(ring_buffer_iter_empty);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004013
4014static void
4015rb_update_read_stamp(struct ring_buffer_per_cpu *cpu_buffer,
4016 struct ring_buffer_event *event)
4017{
4018 u64 delta;
4019
Lai Jiangshan334d4162009-04-24 11:27:05 +08004020 switch (event->type_len) {
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004021 case RINGBUF_TYPE_PADDING:
4022 return;
4023
4024 case RINGBUF_TYPE_TIME_EXTEND:
Tom Zanussidc4e2802018-01-15 20:51:40 -06004025 delta = ring_buffer_event_time_stamp(event);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004026 cpu_buffer->read_stamp += delta;
4027 return;
4028
4029 case RINGBUF_TYPE_TIME_STAMP:
Tom Zanussidc4e2802018-01-15 20:51:40 -06004030 delta = ring_buffer_event_time_stamp(event);
4031 cpu_buffer->read_stamp = delta;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004032 return;
4033
4034 case RINGBUF_TYPE_DATA:
4035 cpu_buffer->read_stamp += event->time_delta;
4036 return;
4037
4038 default:
Steven Rostedt (VMware)da4d4012020-05-13 15:36:22 -04004039 RB_WARN_ON(cpu_buffer, 1);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004040 }
4041 return;
4042}
4043
4044static void
4045rb_update_iter_read_stamp(struct ring_buffer_iter *iter,
4046 struct ring_buffer_event *event)
4047{
4048 u64 delta;
4049
Lai Jiangshan334d4162009-04-24 11:27:05 +08004050 switch (event->type_len) {
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004051 case RINGBUF_TYPE_PADDING:
4052 return;
4053
4054 case RINGBUF_TYPE_TIME_EXTEND:
Tom Zanussidc4e2802018-01-15 20:51:40 -06004055 delta = ring_buffer_event_time_stamp(event);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004056 iter->read_stamp += delta;
4057 return;
4058
4059 case RINGBUF_TYPE_TIME_STAMP:
Tom Zanussidc4e2802018-01-15 20:51:40 -06004060 delta = ring_buffer_event_time_stamp(event);
4061 iter->read_stamp = delta;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004062 return;
4063
4064 case RINGBUF_TYPE_DATA:
4065 iter->read_stamp += event->time_delta;
4066 return;
4067
4068 default:
Steven Rostedt (VMware)da4d4012020-05-13 15:36:22 -04004069 RB_WARN_ON(iter->cpu_buffer, 1);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004070 }
4071 return;
4072}
4073
Steven Rostedtd7690412008-10-01 00:29:53 -04004074static struct buffer_page *
4075rb_get_reader_page(struct ring_buffer_per_cpu *cpu_buffer)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004076{
Steven Rostedtd7690412008-10-01 00:29:53 -04004077 struct buffer_page *reader = NULL;
Steven Rostedt66a8cb92010-03-31 13:21:56 -04004078 unsigned long overwrite;
Steven Rostedtd7690412008-10-01 00:29:53 -04004079 unsigned long flags;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04004080 int nr_loops = 0;
Steven Rostedt77ae3652009-03-27 11:00:29 -04004081 int ret;
Steven Rostedtd7690412008-10-01 00:29:53 -04004082
Steven Rostedt3e03fb72008-11-06 00:09:43 -05004083 local_irq_save(flags);
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01004084 arch_spin_lock(&cpu_buffer->lock);
Steven Rostedtd7690412008-10-01 00:29:53 -04004085
4086 again:
Steven Rostedt818e3dd2008-10-31 09:58:35 -04004087 /*
4088 * This should normally only loop twice. But because the
4089 * start of the reader inserts an empty page, it causes
4090 * a case where we will loop three times. There should be no
4091 * reason to loop four times (that I know of).
4092 */
Steven Rostedt3e89c7bb2008-11-11 15:28:41 -05004093 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 3)) {
Steven Rostedt818e3dd2008-10-31 09:58:35 -04004094 reader = NULL;
4095 goto out;
4096 }
4097
Steven Rostedtd7690412008-10-01 00:29:53 -04004098 reader = cpu_buffer->reader_page;
4099
4100 /* If there's more to read, return this page */
Steven Rostedtbf41a152008-10-04 02:00:59 -04004101 if (cpu_buffer->reader_page->read < rb_page_size(reader))
Steven Rostedtd7690412008-10-01 00:29:53 -04004102 goto out;
4103
4104 /* Never should we have an index greater than the size */
Steven Rostedt3e89c7bb2008-11-11 15:28:41 -05004105 if (RB_WARN_ON(cpu_buffer,
4106 cpu_buffer->reader_page->read > rb_page_size(reader)))
4107 goto out;
Steven Rostedtd7690412008-10-01 00:29:53 -04004108
4109 /* check if we caught up to the tail */
4110 reader = NULL;
Steven Rostedtbf41a152008-10-04 02:00:59 -04004111 if (cpu_buffer->commit_page == cpu_buffer->reader_page)
Steven Rostedtd7690412008-10-01 00:29:53 -04004112 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004113
Steven Rostedta5fb8332012-06-28 13:35:04 -04004114 /* Don't bother swapping if the ring buffer is empty */
4115 if (rb_num_of_entries(cpu_buffer) == 0)
4116 goto out;
4117
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004118 /*
Steven Rostedtd7690412008-10-01 00:29:53 -04004119 * Reset the reader page to size zero.
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004120 */
Steven Rostedt77ae3652009-03-27 11:00:29 -04004121 local_set(&cpu_buffer->reader_page->write, 0);
4122 local_set(&cpu_buffer->reader_page->entries, 0);
4123 local_set(&cpu_buffer->reader_page->page->commit, 0);
Steven Rostedtff0ff842010-03-31 22:11:42 -04004124 cpu_buffer->reader_page->real_end = 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004125
Steven Rostedt77ae3652009-03-27 11:00:29 -04004126 spin:
4127 /*
4128 * Splice the empty reader page into the list around the head.
4129 */
4130 reader = rb_set_head_page(cpu_buffer);
Steven Rostedt54f7be52012-11-29 22:27:22 -05004131 if (!reader)
4132 goto out;
Steven Rostedt0e1ff5d2010-01-06 20:40:44 -05004133 cpu_buffer->reader_page->list.next = rb_list_head(reader->list.next);
Steven Rostedtd7690412008-10-01 00:29:53 -04004134 cpu_buffer->reader_page->list.prev = reader->list.prev;
Steven Rostedtbf41a152008-10-04 02:00:59 -04004135
Steven Rostedt3adc54f2009-03-30 15:32:01 -04004136 /*
4137 * cpu_buffer->pages just needs to point to the buffer, it
4138 * has no specific buffer page to point to. Lets move it out
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004139 * of our way so we don't accidentally swap it.
Steven Rostedt3adc54f2009-03-30 15:32:01 -04004140 */
4141 cpu_buffer->pages = reader->list.prev;
4142
Steven Rostedt77ae3652009-03-27 11:00:29 -04004143 /* The reader page will be pointing to the new head */
4144 rb_set_list_to_head(cpu_buffer, &cpu_buffer->reader_page->list);
Steven Rostedtd7690412008-10-01 00:29:53 -04004145
4146 /*
Steven Rostedt66a8cb92010-03-31 13:21:56 -04004147 * We want to make sure we read the overruns after we set up our
4148 * pointers to the next object. The writer side does a
4149 * cmpxchg to cross pages which acts as the mb on the writer
4150 * side. Note, the reader will constantly fail the swap
4151 * while the writer is updating the pointers, so this
4152 * guarantees that the overwrite recorded here is the one we
4153 * want to compare with the last_overrun.
4154 */
4155 smp_mb();
4156 overwrite = local_read(&(cpu_buffer->overrun));
4157
4158 /*
Steven Rostedt77ae3652009-03-27 11:00:29 -04004159 * Here's the tricky part.
4160 *
4161 * We need to move the pointer past the header page.
4162 * But we can only do that if a writer is not currently
4163 * moving it. The page before the header page has the
4164 * flag bit '1' set if it is pointing to the page we want.
4165 * but if the writer is in the process of moving it
4166 * than it will be '2' or already moved '0'.
Steven Rostedtd7690412008-10-01 00:29:53 -04004167 */
Steven Rostedtd7690412008-10-01 00:29:53 -04004168
Steven Rostedt77ae3652009-03-27 11:00:29 -04004169 ret = rb_head_page_replace(reader, cpu_buffer->reader_page);
4170
4171 /*
4172 * If we did not convert it, then we must try again.
4173 */
4174 if (!ret)
4175 goto spin;
4176
4177 /*
Steven Rostedt (VMware)2c2b0a72018-11-29 20:32:26 -05004178 * Yay! We succeeded in replacing the page.
Steven Rostedt77ae3652009-03-27 11:00:29 -04004179 *
4180 * Now make the new head point back to the reader page.
4181 */
David Sharp5ded3dc62010-01-06 17:12:07 -08004182 rb_list_head(reader->list.next)->prev = &cpu_buffer->reader_page->list;
Steven Rostedt77ae3652009-03-27 11:00:29 -04004183 rb_inc_page(cpu_buffer, &cpu_buffer->head_page);
Steven Rostedtd7690412008-10-01 00:29:53 -04004184
Steven Rostedt (VMware)2c2b0a72018-11-29 20:32:26 -05004185 local_inc(&cpu_buffer->pages_read);
4186
Steven Rostedtd7690412008-10-01 00:29:53 -04004187 /* Finally update the reader page to the new head */
4188 cpu_buffer->reader_page = reader;
Steven Rostedt (Red Hat)b81f4722015-11-23 10:35:36 -05004189 cpu_buffer->reader_page->read = 0;
Steven Rostedtd7690412008-10-01 00:29:53 -04004190
Steven Rostedt66a8cb92010-03-31 13:21:56 -04004191 if (overwrite != cpu_buffer->last_overrun) {
4192 cpu_buffer->lost_events = overwrite - cpu_buffer->last_overrun;
4193 cpu_buffer->last_overrun = overwrite;
4194 }
4195
Steven Rostedtd7690412008-10-01 00:29:53 -04004196 goto again;
4197
4198 out:
Steven Rostedt (Red Hat)b81f4722015-11-23 10:35:36 -05004199 /* Update the read_stamp on the first event */
4200 if (reader && reader->read == 0)
4201 cpu_buffer->read_stamp = reader->page->time_stamp;
4202
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01004203 arch_spin_unlock(&cpu_buffer->lock);
Steven Rostedt3e03fb72008-11-06 00:09:43 -05004204 local_irq_restore(flags);
Steven Rostedtd7690412008-10-01 00:29:53 -04004205
4206 return reader;
4207}
4208
4209static void rb_advance_reader(struct ring_buffer_per_cpu *cpu_buffer)
4210{
4211 struct ring_buffer_event *event;
4212 struct buffer_page *reader;
4213 unsigned length;
4214
4215 reader = rb_get_reader_page(cpu_buffer);
4216
4217 /* This function should not be called when buffer is empty */
Steven Rostedt3e89c7bb2008-11-11 15:28:41 -05004218 if (RB_WARN_ON(cpu_buffer, !reader))
4219 return;
Steven Rostedtd7690412008-10-01 00:29:53 -04004220
4221 event = rb_reader_event(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004222
Steven Rostedta1863c22009-09-03 10:23:58 -04004223 if (event->type_len <= RINGBUF_TYPE_DATA_TYPE_LEN_MAX)
Steven Rostedte4906ef2009-04-30 20:49:44 -04004224 cpu_buffer->read++;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004225
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004226 rb_update_read_stamp(cpu_buffer, event);
4227
Steven Rostedtd7690412008-10-01 00:29:53 -04004228 length = rb_event_length(event);
Steven Rostedt6f807ac2008-10-04 02:00:58 -04004229 cpu_buffer->reader_page->read += length;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004230}
4231
4232static void rb_advance_iter(struct ring_buffer_iter *iter)
4233{
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004234 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004235
4236 cpu_buffer = iter->cpu_buffer;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004237
Steven Rostedt (VMware)785888c2020-03-17 17:32:27 -04004238 /* If head == next_event then we need to jump to the next event */
4239 if (iter->head == iter->next_event) {
4240 /* If the event gets overwritten again, there's nothing to do */
4241 if (rb_iter_head_event(iter) == NULL)
4242 return;
4243 }
4244
4245 iter->head = iter->next_event;
4246
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004247 /*
4248 * Check if we are at the end of the buffer.
4249 */
Steven Rostedt (VMware)785888c2020-03-17 17:32:27 -04004250 if (iter->next_event >= rb_page_size(iter->head_page)) {
Steven Rostedtea05b572009-06-03 09:30:10 -04004251 /* discarded commits can make the page empty */
4252 if (iter->head_page == cpu_buffer->commit_page)
Steven Rostedt3e89c7bb2008-11-11 15:28:41 -05004253 return;
Steven Rostedtd7690412008-10-01 00:29:53 -04004254 rb_inc_iter(iter);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004255 return;
4256 }
4257
Steven Rostedt (VMware)785888c2020-03-17 17:32:27 -04004258 rb_update_iter_read_stamp(iter, iter->event);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004259}
4260
Steven Rostedt66a8cb92010-03-31 13:21:56 -04004261static int rb_lost_events(struct ring_buffer_per_cpu *cpu_buffer)
4262{
4263 return cpu_buffer->lost_events;
4264}
4265
Steven Rostedtf83c9d02008-11-11 18:47:44 +01004266static struct ring_buffer_event *
Steven Rostedt66a8cb92010-03-31 13:21:56 -04004267rb_buffer_peek(struct ring_buffer_per_cpu *cpu_buffer, u64 *ts,
4268 unsigned long *lost_events)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004269{
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004270 struct ring_buffer_event *event;
Steven Rostedtd7690412008-10-01 00:29:53 -04004271 struct buffer_page *reader;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04004272 int nr_loops = 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004273
Tom Zanussidc4e2802018-01-15 20:51:40 -06004274 if (ts)
4275 *ts = 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004276 again:
Steven Rostedt818e3dd2008-10-31 09:58:35 -04004277 /*
Steven Rostedt69d1b832010-10-07 18:18:05 -04004278 * We repeat when a time extend is encountered.
4279 * Since the time extend is always attached to a data event,
4280 * we should never loop more than once.
4281 * (We never hit the following condition more than twice).
Steven Rostedt818e3dd2008-10-31 09:58:35 -04004282 */
Steven Rostedt69d1b832010-10-07 18:18:05 -04004283 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 2))
Steven Rostedt818e3dd2008-10-31 09:58:35 -04004284 return NULL;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04004285
Steven Rostedtd7690412008-10-01 00:29:53 -04004286 reader = rb_get_reader_page(cpu_buffer);
4287 if (!reader)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004288 return NULL;
4289
Steven Rostedtd7690412008-10-01 00:29:53 -04004290 event = rb_reader_event(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004291
Lai Jiangshan334d4162009-04-24 11:27:05 +08004292 switch (event->type_len) {
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004293 case RINGBUF_TYPE_PADDING:
Tom Zanussi2d622712009-03-22 03:30:49 -05004294 if (rb_null_event(event))
4295 RB_WARN_ON(cpu_buffer, 1);
4296 /*
4297 * Because the writer could be discarding every
4298 * event it creates (which would probably be bad)
4299 * if we were to go back to "again" then we may never
4300 * catch up, and will trigger the warn on, or lock
4301 * the box. Return the padding, and we will release
4302 * the current locks, and try again.
4303 */
Tom Zanussi2d622712009-03-22 03:30:49 -05004304 return event;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004305
4306 case RINGBUF_TYPE_TIME_EXTEND:
4307 /* Internal data, OK to advance */
Steven Rostedtd7690412008-10-01 00:29:53 -04004308 rb_advance_reader(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004309 goto again;
4310
4311 case RINGBUF_TYPE_TIME_STAMP:
Tom Zanussidc4e2802018-01-15 20:51:40 -06004312 if (ts) {
4313 *ts = ring_buffer_event_time_stamp(event);
4314 ring_buffer_normalize_time_stamp(cpu_buffer->buffer,
4315 cpu_buffer->cpu, ts);
4316 }
4317 /* Internal data, OK to advance */
Steven Rostedtd7690412008-10-01 00:29:53 -04004318 rb_advance_reader(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004319 goto again;
4320
4321 case RINGBUF_TYPE_DATA:
Tom Zanussidc4e2802018-01-15 20:51:40 -06004322 if (ts && !(*ts)) {
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004323 *ts = cpu_buffer->read_stamp + event->time_delta;
Robert Richterd8eeb2d2009-07-31 14:58:04 +02004324 ring_buffer_normalize_time_stamp(cpu_buffer->buffer,
Steven Rostedt37886f62009-03-17 17:22:06 -04004325 cpu_buffer->cpu, ts);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004326 }
Steven Rostedt66a8cb92010-03-31 13:21:56 -04004327 if (lost_events)
4328 *lost_events = rb_lost_events(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004329 return event;
4330
4331 default:
Steven Rostedt (VMware)da4d4012020-05-13 15:36:22 -04004332 RB_WARN_ON(cpu_buffer, 1);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004333 }
4334
4335 return NULL;
4336}
Robert Richterc4f50182008-12-11 16:49:22 +01004337EXPORT_SYMBOL_GPL(ring_buffer_peek);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004338
Steven Rostedtf83c9d02008-11-11 18:47:44 +01004339static struct ring_buffer_event *
4340rb_iter_peek(struct ring_buffer_iter *iter, u64 *ts)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004341{
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05004342 struct trace_buffer *buffer;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004343 struct ring_buffer_per_cpu *cpu_buffer;
4344 struct ring_buffer_event *event;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04004345 int nr_loops = 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004346
Tom Zanussidc4e2802018-01-15 20:51:40 -06004347 if (ts)
4348 *ts = 0;
4349
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004350 cpu_buffer = iter->cpu_buffer;
4351 buffer = cpu_buffer->buffer;
4352
Steven Rostedt492a74f2010-01-25 15:17:47 -05004353 /*
4354 * Check if someone performed a consuming read to
4355 * the buffer. A consuming read invalidates the iterator
4356 * and we need to reset the iterator in this case.
4357 */
4358 if (unlikely(iter->cache_read != cpu_buffer->read ||
4359 iter->cache_reader_page != cpu_buffer->reader_page))
4360 rb_iter_reset(iter);
4361
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004362 again:
Steven Rostedt3c05d742010-01-26 16:14:08 -05004363 if (ring_buffer_iter_empty(iter))
4364 return NULL;
4365
Steven Rostedt818e3dd2008-10-31 09:58:35 -04004366 /*
Steven Rostedt (VMware)3d2353d2020-05-13 15:18:01 -04004367 * As the writer can mess with what the iterator is trying
4368 * to read, just give up if we fail to get an event after
4369 * three tries. The iterator is not as reliable when reading
4370 * the ring buffer with an active write as the consumer is.
4371 * Do not warn if the three failures is reached.
Steven Rostedt818e3dd2008-10-31 09:58:35 -04004372 */
Steven Rostedt (VMware)3d2353d2020-05-13 15:18:01 -04004373 if (++nr_loops > 3)
Steven Rostedt818e3dd2008-10-31 09:58:35 -04004374 return NULL;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04004375
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004376 if (rb_per_cpu_empty(cpu_buffer))
4377 return NULL;
4378
Steven Rostedt (Red Hat)10e83fd2014-07-23 19:45:12 -04004379 if (iter->head >= rb_page_size(iter->head_page)) {
Steven Rostedt3c05d742010-01-26 16:14:08 -05004380 rb_inc_iter(iter);
4381 goto again;
4382 }
4383
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004384 event = rb_iter_head_event(iter);
Steven Rostedt (VMware)3d2353d2020-05-13 15:18:01 -04004385 if (!event)
Steven Rostedt (VMware)785888c2020-03-17 17:32:27 -04004386 goto again;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004387
Lai Jiangshan334d4162009-04-24 11:27:05 +08004388 switch (event->type_len) {
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004389 case RINGBUF_TYPE_PADDING:
Tom Zanussi2d622712009-03-22 03:30:49 -05004390 if (rb_null_event(event)) {
4391 rb_inc_iter(iter);
4392 goto again;
4393 }
4394 rb_advance_iter(iter);
4395 return event;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004396
4397 case RINGBUF_TYPE_TIME_EXTEND:
4398 /* Internal data, OK to advance */
4399 rb_advance_iter(iter);
4400 goto again;
4401
4402 case RINGBUF_TYPE_TIME_STAMP:
Tom Zanussidc4e2802018-01-15 20:51:40 -06004403 if (ts) {
4404 *ts = ring_buffer_event_time_stamp(event);
4405 ring_buffer_normalize_time_stamp(cpu_buffer->buffer,
4406 cpu_buffer->cpu, ts);
4407 }
4408 /* Internal data, OK to advance */
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004409 rb_advance_iter(iter);
4410 goto again;
4411
4412 case RINGBUF_TYPE_DATA:
Tom Zanussidc4e2802018-01-15 20:51:40 -06004413 if (ts && !(*ts)) {
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004414 *ts = iter->read_stamp + event->time_delta;
Steven Rostedt37886f62009-03-17 17:22:06 -04004415 ring_buffer_normalize_time_stamp(buffer,
4416 cpu_buffer->cpu, ts);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004417 }
4418 return event;
4419
4420 default:
Steven Rostedt (VMware)da4d4012020-05-13 15:36:22 -04004421 RB_WARN_ON(cpu_buffer, 1);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004422 }
4423
4424 return NULL;
4425}
Robert Richterc4f50182008-12-11 16:49:22 +01004426EXPORT_SYMBOL_GPL(ring_buffer_iter_peek);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004427
Steven Rostedt (Red Hat)289a5a22015-05-28 13:14:51 -04004428static inline bool rb_reader_lock(struct ring_buffer_per_cpu *cpu_buffer)
Steven Rostedt8d707e82009-06-16 21:22:48 -04004429{
Steven Rostedt (Red Hat)289a5a22015-05-28 13:14:51 -04004430 if (likely(!in_nmi())) {
4431 raw_spin_lock(&cpu_buffer->reader_lock);
4432 return true;
4433 }
4434
Steven Rostedt8d707e82009-06-16 21:22:48 -04004435 /*
4436 * If an NMI die dumps out the content of the ring buffer
Steven Rostedt (Red Hat)289a5a22015-05-28 13:14:51 -04004437 * trylock must be used to prevent a deadlock if the NMI
4438 * preempted a task that holds the ring buffer locks. If
4439 * we get the lock then all is fine, if not, then continue
4440 * to do the read, but this can corrupt the ring buffer,
4441 * so it must be permanently disabled from future writes.
4442 * Reading from NMI is a oneshot deal.
Steven Rostedt8d707e82009-06-16 21:22:48 -04004443 */
Steven Rostedt (Red Hat)289a5a22015-05-28 13:14:51 -04004444 if (raw_spin_trylock(&cpu_buffer->reader_lock))
4445 return true;
Steven Rostedt8d707e82009-06-16 21:22:48 -04004446
Steven Rostedt (Red Hat)289a5a22015-05-28 13:14:51 -04004447 /* Continue without locking, but disable the ring buffer */
4448 atomic_inc(&cpu_buffer->record_disabled);
4449 return false;
4450}
4451
4452static inline void
4453rb_reader_unlock(struct ring_buffer_per_cpu *cpu_buffer, bool locked)
4454{
4455 if (likely(locked))
4456 raw_spin_unlock(&cpu_buffer->reader_lock);
4457 return;
Steven Rostedt8d707e82009-06-16 21:22:48 -04004458}
4459
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004460/**
Steven Rostedtf83c9d02008-11-11 18:47:44 +01004461 * ring_buffer_peek - peek at the next event to be read
4462 * @buffer: The ring buffer to read
4463 * @cpu: The cpu to peak at
4464 * @ts: The timestamp counter of this event.
Steven Rostedt66a8cb92010-03-31 13:21:56 -04004465 * @lost_events: a variable to store if events were lost (may be NULL)
Steven Rostedtf83c9d02008-11-11 18:47:44 +01004466 *
4467 * This will return the event that will be read next, but does
4468 * not consume the data.
4469 */
4470struct ring_buffer_event *
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05004471ring_buffer_peek(struct trace_buffer *buffer, int cpu, u64 *ts,
Steven Rostedt66a8cb92010-03-31 13:21:56 -04004472 unsigned long *lost_events)
Steven Rostedtf83c9d02008-11-11 18:47:44 +01004473{
4474 struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
Steven Rostedt8aabee52009-03-12 13:13:49 -04004475 struct ring_buffer_event *event;
Steven Rostedtf83c9d02008-11-11 18:47:44 +01004476 unsigned long flags;
Steven Rostedt (Red Hat)289a5a22015-05-28 13:14:51 -04004477 bool dolock;
Steven Rostedtf83c9d02008-11-11 18:47:44 +01004478
Steven Rostedt554f7862009-03-11 22:00:13 -04004479 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04004480 return NULL;
Steven Rostedt554f7862009-03-11 22:00:13 -04004481
Tom Zanussi2d622712009-03-22 03:30:49 -05004482 again:
Steven Rostedt8d707e82009-06-16 21:22:48 -04004483 local_irq_save(flags);
Steven Rostedt (Red Hat)289a5a22015-05-28 13:14:51 -04004484 dolock = rb_reader_lock(cpu_buffer);
Steven Rostedt66a8cb92010-03-31 13:21:56 -04004485 event = rb_buffer_peek(cpu_buffer, ts, lost_events);
Robert Richter469535a2009-07-30 19:19:18 +02004486 if (event && event->type_len == RINGBUF_TYPE_PADDING)
4487 rb_advance_reader(cpu_buffer);
Steven Rostedt (Red Hat)289a5a22015-05-28 13:14:51 -04004488 rb_reader_unlock(cpu_buffer, dolock);
Steven Rostedt8d707e82009-06-16 21:22:48 -04004489 local_irq_restore(flags);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01004490
Steven Rostedt1b959e12009-09-03 10:12:13 -04004491 if (event && event->type_len == RINGBUF_TYPE_PADDING)
Tom Zanussi2d622712009-03-22 03:30:49 -05004492 goto again;
Tom Zanussi2d622712009-03-22 03:30:49 -05004493
Steven Rostedtf83c9d02008-11-11 18:47:44 +01004494 return event;
4495}
4496
Steven Rostedt (VMware)c9b7a4a2020-03-17 17:32:32 -04004497/** ring_buffer_iter_dropped - report if there are dropped events
4498 * @iter: The ring buffer iterator
4499 *
4500 * Returns true if there was dropped events since the last peek.
4501 */
4502bool ring_buffer_iter_dropped(struct ring_buffer_iter *iter)
4503{
4504 bool ret = iter->missed_events != 0;
4505
4506 iter->missed_events = 0;
4507 return ret;
4508}
4509EXPORT_SYMBOL_GPL(ring_buffer_iter_dropped);
4510
Steven Rostedtf83c9d02008-11-11 18:47:44 +01004511/**
4512 * ring_buffer_iter_peek - peek at the next event to be read
4513 * @iter: The ring buffer iterator
4514 * @ts: The timestamp counter of this event.
4515 *
4516 * This will return the event that will be read next, but does
4517 * not increment the iterator.
4518 */
4519struct ring_buffer_event *
4520ring_buffer_iter_peek(struct ring_buffer_iter *iter, u64 *ts)
4521{
4522 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
4523 struct ring_buffer_event *event;
4524 unsigned long flags;
4525
Tom Zanussi2d622712009-03-22 03:30:49 -05004526 again:
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02004527 raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01004528 event = rb_iter_peek(iter, ts);
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02004529 raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01004530
Steven Rostedt1b959e12009-09-03 10:12:13 -04004531 if (event && event->type_len == RINGBUF_TYPE_PADDING)
Tom Zanussi2d622712009-03-22 03:30:49 -05004532 goto again;
Tom Zanussi2d622712009-03-22 03:30:49 -05004533
Steven Rostedtf83c9d02008-11-11 18:47:44 +01004534 return event;
4535}
4536
4537/**
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004538 * ring_buffer_consume - return an event and consume it
4539 * @buffer: The ring buffer to get the next event from
Steven Rostedt66a8cb92010-03-31 13:21:56 -04004540 * @cpu: the cpu to read the buffer from
4541 * @ts: a variable to store the timestamp (may be NULL)
4542 * @lost_events: a variable to store if events were lost (may be NULL)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004543 *
4544 * Returns the next event in the ring buffer, and that event is consumed.
4545 * Meaning, that sequential reads will keep returning a different event,
4546 * and eventually empty the ring buffer if the producer is slower.
4547 */
4548struct ring_buffer_event *
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05004549ring_buffer_consume(struct trace_buffer *buffer, int cpu, u64 *ts,
Steven Rostedt66a8cb92010-03-31 13:21:56 -04004550 unsigned long *lost_events)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004551{
Steven Rostedt554f7862009-03-11 22:00:13 -04004552 struct ring_buffer_per_cpu *cpu_buffer;
4553 struct ring_buffer_event *event = NULL;
Steven Rostedtf83c9d02008-11-11 18:47:44 +01004554 unsigned long flags;
Steven Rostedt (Red Hat)289a5a22015-05-28 13:14:51 -04004555 bool dolock;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004556
Tom Zanussi2d622712009-03-22 03:30:49 -05004557 again:
Steven Rostedt554f7862009-03-11 22:00:13 -04004558 /* might be called in atomic */
4559 preempt_disable();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004560
Steven Rostedt554f7862009-03-11 22:00:13 -04004561 if (!cpumask_test_cpu(cpu, buffer->cpumask))
4562 goto out;
4563
4564 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt8d707e82009-06-16 21:22:48 -04004565 local_irq_save(flags);
Steven Rostedt (Red Hat)289a5a22015-05-28 13:14:51 -04004566 dolock = rb_reader_lock(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004567
Steven Rostedt66a8cb92010-03-31 13:21:56 -04004568 event = rb_buffer_peek(cpu_buffer, ts, lost_events);
4569 if (event) {
4570 cpu_buffer->lost_events = 0;
Robert Richter469535a2009-07-30 19:19:18 +02004571 rb_advance_reader(cpu_buffer);
Steven Rostedt66a8cb92010-03-31 13:21:56 -04004572 }
Steven Rostedtf83c9d02008-11-11 18:47:44 +01004573
Steven Rostedt (Red Hat)289a5a22015-05-28 13:14:51 -04004574 rb_reader_unlock(cpu_buffer, dolock);
Steven Rostedt8d707e82009-06-16 21:22:48 -04004575 local_irq_restore(flags);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01004576
Steven Rostedt554f7862009-03-11 22:00:13 -04004577 out:
4578 preempt_enable();
4579
Steven Rostedt1b959e12009-09-03 10:12:13 -04004580 if (event && event->type_len == RINGBUF_TYPE_PADDING)
Tom Zanussi2d622712009-03-22 03:30:49 -05004581 goto again;
Tom Zanussi2d622712009-03-22 03:30:49 -05004582
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004583 return event;
4584}
Robert Richterc4f50182008-12-11 16:49:22 +01004585EXPORT_SYMBOL_GPL(ring_buffer_consume);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004586
4587/**
David Miller72c9ddf2010-04-20 15:47:11 -07004588 * ring_buffer_read_prepare - Prepare for a non consuming read of the buffer
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004589 * @buffer: The ring buffer to read from
4590 * @cpu: The cpu buffer to iterate over
Douglas Anderson31b265b2019-03-08 11:32:04 -08004591 * @flags: gfp flags to use for memory allocation
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004592 *
David Miller72c9ddf2010-04-20 15:47:11 -07004593 * This performs the initial preparations necessary to iterate
4594 * through the buffer. Memory is allocated, buffer recording
4595 * is disabled, and the iterator pointer is returned to the caller.
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004596 *
Steven Rostedt (VMware)6167c202018-05-16 11:17:06 -04004597 * Disabling buffer recording prevents the reading from being
David Miller72c9ddf2010-04-20 15:47:11 -07004598 * corrupted. This is not a consuming read, so a producer is not
4599 * expected.
4600 *
4601 * After a sequence of ring_buffer_read_prepare calls, the user is
zhangwei(Jovi)d6118512013-07-15 16:32:50 +08004602 * expected to make at least one call to ring_buffer_read_prepare_sync.
David Miller72c9ddf2010-04-20 15:47:11 -07004603 * Afterwards, ring_buffer_read_start is invoked to get things going
4604 * for real.
4605 *
zhangwei(Jovi)d6118512013-07-15 16:32:50 +08004606 * This overall must be paired with ring_buffer_read_finish.
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004607 */
4608struct ring_buffer_iter *
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05004609ring_buffer_read_prepare(struct trace_buffer *buffer, int cpu, gfp_t flags)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004610{
4611 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedt8aabee52009-03-12 13:13:49 -04004612 struct ring_buffer_iter *iter;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004613
Rusty Russell9e01c1b2009-01-01 10:12:22 +10304614 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04004615 return NULL;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004616
Steven Rostedt (VMware)785888c2020-03-17 17:32:27 -04004617 iter = kzalloc(sizeof(*iter), flags);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004618 if (!iter)
Steven Rostedt8aabee52009-03-12 13:13:49 -04004619 return NULL;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004620
Steven Rostedt (VMware)785888c2020-03-17 17:32:27 -04004621 iter->event = kmalloc(BUF_MAX_DATA_SIZE, flags);
4622 if (!iter->event) {
4623 kfree(iter);
4624 return NULL;
4625 }
4626
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004627 cpu_buffer = buffer->buffers[cpu];
4628
4629 iter->cpu_buffer = cpu_buffer;
4630
Steven Rostedt (VMware)07b8b102020-03-27 16:21:22 -04004631 atomic_inc(&cpu_buffer->resize_disabled);
David Miller72c9ddf2010-04-20 15:47:11 -07004632
4633 return iter;
4634}
4635EXPORT_SYMBOL_GPL(ring_buffer_read_prepare);
4636
4637/**
4638 * ring_buffer_read_prepare_sync - Synchronize a set of prepare calls
4639 *
4640 * All previously invoked ring_buffer_read_prepare calls to prepare
4641 * iterators will be synchronized. Afterwards, read_buffer_read_start
4642 * calls on those iterators are allowed.
4643 */
4644void
4645ring_buffer_read_prepare_sync(void)
4646{
Paul E. McKenney74401722018-11-06 18:44:52 -08004647 synchronize_rcu();
David Miller72c9ddf2010-04-20 15:47:11 -07004648}
4649EXPORT_SYMBOL_GPL(ring_buffer_read_prepare_sync);
4650
4651/**
4652 * ring_buffer_read_start - start a non consuming read of the buffer
4653 * @iter: The iterator returned by ring_buffer_read_prepare
4654 *
4655 * This finalizes the startup of an iteration through the buffer.
4656 * The iterator comes from a call to ring_buffer_read_prepare and
4657 * an intervening ring_buffer_read_prepare_sync must have been
4658 * performed.
4659 *
zhangwei(Jovi)d6118512013-07-15 16:32:50 +08004660 * Must be paired with ring_buffer_read_finish.
David Miller72c9ddf2010-04-20 15:47:11 -07004661 */
4662void
4663ring_buffer_read_start(struct ring_buffer_iter *iter)
4664{
4665 struct ring_buffer_per_cpu *cpu_buffer;
4666 unsigned long flags;
4667
4668 if (!iter)
4669 return;
4670
4671 cpu_buffer = iter->cpu_buffer;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004672
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02004673 raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01004674 arch_spin_lock(&cpu_buffer->lock);
Steven Rostedt642edba2008-11-12 00:01:26 -05004675 rb_iter_reset(iter);
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01004676 arch_spin_unlock(&cpu_buffer->lock);
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02004677 raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004678}
Robert Richterc4f50182008-12-11 16:49:22 +01004679EXPORT_SYMBOL_GPL(ring_buffer_read_start);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004680
4681/**
zhangwei(Jovi)d6118512013-07-15 16:32:50 +08004682 * ring_buffer_read_finish - finish reading the iterator of the buffer
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004683 * @iter: The iterator retrieved by ring_buffer_start
4684 *
4685 * This re-enables the recording to the buffer, and frees the
4686 * iterator.
4687 */
4688void
4689ring_buffer_read_finish(struct ring_buffer_iter *iter)
4690{
4691 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
Steven Rostedt9366c1b2012-11-29 22:31:16 -05004692 unsigned long flags;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004693
Steven Rostedt659f4512012-05-14 17:02:33 -04004694 /*
4695 * Ring buffer is disabled from recording, here's a good place
Steven Rostedt9366c1b2012-11-29 22:31:16 -05004696 * to check the integrity of the ring buffer.
4697 * Must prevent readers from trying to read, as the check
4698 * clears the HEAD page and readers require it.
Steven Rostedt659f4512012-05-14 17:02:33 -04004699 */
Steven Rostedt9366c1b2012-11-29 22:31:16 -05004700 raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
Steven Rostedt659f4512012-05-14 17:02:33 -04004701 rb_check_pages(cpu_buffer);
Steven Rostedt9366c1b2012-11-29 22:31:16 -05004702 raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
Steven Rostedt659f4512012-05-14 17:02:33 -04004703
Steven Rostedt (VMware)07b8b102020-03-27 16:21:22 -04004704 atomic_dec(&cpu_buffer->resize_disabled);
Steven Rostedt (VMware)785888c2020-03-17 17:32:27 -04004705 kfree(iter->event);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004706 kfree(iter);
4707}
Robert Richterc4f50182008-12-11 16:49:22 +01004708EXPORT_SYMBOL_GPL(ring_buffer_read_finish);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004709
4710/**
Steven Rostedt (VMware)bc1a72a2020-03-17 17:32:25 -04004711 * ring_buffer_iter_advance - advance the iterator to the next location
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004712 * @iter: The ring buffer iterator
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004713 *
Steven Rostedt (VMware)bc1a72a2020-03-17 17:32:25 -04004714 * Move the location of the iterator such that the next read will
4715 * be the next location of the iterator.
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004716 */
Steven Rostedt (VMware)bc1a72a2020-03-17 17:32:25 -04004717void ring_buffer_iter_advance(struct ring_buffer_iter *iter)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004718{
Steven Rostedtf83c9d02008-11-11 18:47:44 +01004719 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
4720 unsigned long flags;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004721
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02004722 raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
Steven Rostedt7e9391c2009-09-03 10:02:09 -04004723
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004724 rb_advance_iter(iter);
4725
Steven Rostedt (VMware)bc1a72a2020-03-17 17:32:25 -04004726 raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004727}
Steven Rostedt (VMware)bc1a72a2020-03-17 17:32:25 -04004728EXPORT_SYMBOL_GPL(ring_buffer_iter_advance);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004729
4730/**
4731 * ring_buffer_size - return the size of the ring buffer (in bytes)
4732 * @buffer: The ring buffer.
Fabian Frederick59e7cff2014-06-05 20:22:05 +02004733 * @cpu: The CPU to get ring buffer size from.
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004734 */
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05004735unsigned long ring_buffer_size(struct trace_buffer *buffer, int cpu)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004736{
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08004737 /*
4738 * Earlier, this method returned
4739 * BUF_PAGE_SIZE * buffer->nr_pages
4740 * Since the nr_pages field is now removed, we have converted this to
4741 * return the per cpu buffer value.
4742 */
4743 if (!cpumask_test_cpu(cpu, buffer->cpumask))
4744 return 0;
4745
4746 return BUF_PAGE_SIZE * buffer->buffers[cpu]->nr_pages;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004747}
Robert Richterc4f50182008-12-11 16:49:22 +01004748EXPORT_SYMBOL_GPL(ring_buffer_size);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004749
4750static void
4751rb_reset_cpu(struct ring_buffer_per_cpu *cpu_buffer)
4752{
Steven Rostedt77ae3652009-03-27 11:00:29 -04004753 rb_head_page_deactivate(cpu_buffer);
4754
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004755 cpu_buffer->head_page
Steven Rostedt3adc54f2009-03-30 15:32:01 -04004756 = list_entry(cpu_buffer->pages, struct buffer_page, list);
Steven Rostedtbf41a152008-10-04 02:00:59 -04004757 local_set(&cpu_buffer->head_page->write, 0);
Steven Rostedt778c55d2009-05-01 18:44:45 -04004758 local_set(&cpu_buffer->head_page->entries, 0);
Steven Rostedtabc9b562008-12-02 15:34:06 -05004759 local_set(&cpu_buffer->head_page->page->commit, 0);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004760
Steven Rostedt6f807ac2008-10-04 02:00:58 -04004761 cpu_buffer->head_page->read = 0;
Steven Rostedtbf41a152008-10-04 02:00:59 -04004762
4763 cpu_buffer->tail_page = cpu_buffer->head_page;
4764 cpu_buffer->commit_page = cpu_buffer->head_page;
4765
4766 INIT_LIST_HEAD(&cpu_buffer->reader_page->list);
Vaibhav Nagarnaik5040b4b2012-05-03 18:59:51 -07004767 INIT_LIST_HEAD(&cpu_buffer->new_pages);
Steven Rostedtbf41a152008-10-04 02:00:59 -04004768 local_set(&cpu_buffer->reader_page->write, 0);
Steven Rostedt778c55d2009-05-01 18:44:45 -04004769 local_set(&cpu_buffer->reader_page->entries, 0);
Steven Rostedtabc9b562008-12-02 15:34:06 -05004770 local_set(&cpu_buffer->reader_page->page->commit, 0);
Steven Rostedt6f807ac2008-10-04 02:00:58 -04004771 cpu_buffer->reader_page->read = 0;
Steven Rostedtd7690412008-10-01 00:29:53 -04004772
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -07004773 local_set(&cpu_buffer->entries_bytes, 0);
Steven Rostedt77ae3652009-03-27 11:00:29 -04004774 local_set(&cpu_buffer->overrun, 0);
Slava Pestov884bfe82011-07-15 14:23:58 -07004775 local_set(&cpu_buffer->commit_overrun, 0);
4776 local_set(&cpu_buffer->dropped_events, 0);
Steven Rostedte4906ef2009-04-30 20:49:44 -04004777 local_set(&cpu_buffer->entries, 0);
Steven Rostedtfa743952009-06-16 12:37:57 -04004778 local_set(&cpu_buffer->committing, 0);
4779 local_set(&cpu_buffer->commits, 0);
Steven Rostedt (VMware)2c2b0a72018-11-29 20:32:26 -05004780 local_set(&cpu_buffer->pages_touched, 0);
4781 local_set(&cpu_buffer->pages_read, 0);
Steven Rostedt (VMware)03329f92018-11-29 21:38:42 -05004782 cpu_buffer->last_pages_touch = 0;
Steven Rostedt (VMware)2c2b0a72018-11-29 20:32:26 -05004783 cpu_buffer->shortest_full = 0;
Steven Rostedt77ae3652009-03-27 11:00:29 -04004784 cpu_buffer->read = 0;
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -07004785 cpu_buffer->read_bytes = 0;
Steven Rostedt69507c02009-01-21 18:45:57 -05004786
Steven Rostedt (VMware)10464b42020-06-28 22:52:27 -04004787 rb_time_set(&cpu_buffer->write_stamp, 0);
4788 rb_time_set(&cpu_buffer->before_stamp, 0);
Steven Rostedt77ae3652009-03-27 11:00:29 -04004789
Steven Rostedt66a8cb92010-03-31 13:21:56 -04004790 cpu_buffer->lost_events = 0;
4791 cpu_buffer->last_overrun = 0;
4792
Steven Rostedt77ae3652009-03-27 11:00:29 -04004793 rb_head_page_activate(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004794}
4795
Nicholas Pigginb23d7a5f2020-06-25 15:34:03 +10004796/* Must have disabled the cpu buffer then done a synchronize_rcu */
4797static void reset_disabled_cpu_buffer(struct ring_buffer_per_cpu *cpu_buffer)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004798{
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004799 unsigned long flags;
4800
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02004801 raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01004802
Steven Rostedt41b6a952009-09-02 09:59:48 -04004803 if (RB_WARN_ON(cpu_buffer, local_read(&cpu_buffer->committing)))
4804 goto out;
4805
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01004806 arch_spin_lock(&cpu_buffer->lock);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004807
4808 rb_reset_cpu(cpu_buffer);
4809
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01004810 arch_spin_unlock(&cpu_buffer->lock);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01004811
Steven Rostedt41b6a952009-09-02 09:59:48 -04004812 out:
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02004813 raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
Nicholas Pigginb23d7a5f2020-06-25 15:34:03 +10004814}
4815
4816/**
4817 * ring_buffer_reset_cpu - reset a ring buffer per CPU buffer
4818 * @buffer: The ring buffer to reset a per cpu buffer of
4819 * @cpu: The CPU buffer to be reset
4820 */
4821void ring_buffer_reset_cpu(struct trace_buffer *buffer, int cpu)
4822{
4823 struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
4824
4825 if (!cpumask_test_cpu(cpu, buffer->cpumask))
4826 return;
4827
4828 atomic_inc(&cpu_buffer->resize_disabled);
4829 atomic_inc(&cpu_buffer->record_disabled);
4830
4831 /* Make sure all commits have finished */
4832 synchronize_rcu();
4833
4834 reset_disabled_cpu_buffer(cpu_buffer);
Steven Rostedt41ede232009-05-01 20:26:54 -04004835
4836 atomic_dec(&cpu_buffer->record_disabled);
Steven Rostedt (VMware)07b8b102020-03-27 16:21:22 -04004837 atomic_dec(&cpu_buffer->resize_disabled);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004838}
Robert Richterc4f50182008-12-11 16:49:22 +01004839EXPORT_SYMBOL_GPL(ring_buffer_reset_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004840
4841/**
Nicholas Pigginb23d7a5f2020-06-25 15:34:03 +10004842 * ring_buffer_reset_cpu - reset a ring buffer per CPU buffer
4843 * @buffer: The ring buffer to reset a per cpu buffer of
4844 * @cpu: The CPU buffer to be reset
4845 */
4846void ring_buffer_reset_online_cpus(struct trace_buffer *buffer)
4847{
4848 struct ring_buffer_per_cpu *cpu_buffer;
4849 int cpu;
4850
4851 for_each_online_buffer_cpu(buffer, cpu) {
4852 cpu_buffer = buffer->buffers[cpu];
4853
4854 atomic_inc(&cpu_buffer->resize_disabled);
4855 atomic_inc(&cpu_buffer->record_disabled);
4856 }
4857
4858 /* Make sure all commits have finished */
4859 synchronize_rcu();
4860
4861 for_each_online_buffer_cpu(buffer, cpu) {
4862 cpu_buffer = buffer->buffers[cpu];
4863
4864 reset_disabled_cpu_buffer(cpu_buffer);
4865
4866 atomic_dec(&cpu_buffer->record_disabled);
4867 atomic_dec(&cpu_buffer->resize_disabled);
4868 }
4869}
4870
4871/**
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004872 * ring_buffer_reset - reset a ring buffer
4873 * @buffer: The ring buffer to reset all cpu buffers
4874 */
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05004875void ring_buffer_reset(struct trace_buffer *buffer)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004876{
Nicholas Pigginb23d7a5f2020-06-25 15:34:03 +10004877 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004878 int cpu;
4879
Nicholas Pigginb23d7a5f2020-06-25 15:34:03 +10004880 for_each_buffer_cpu(buffer, cpu) {
4881 cpu_buffer = buffer->buffers[cpu];
4882
4883 atomic_inc(&cpu_buffer->resize_disabled);
4884 atomic_inc(&cpu_buffer->record_disabled);
4885 }
4886
4887 /* Make sure all commits have finished */
4888 synchronize_rcu();
4889
4890 for_each_buffer_cpu(buffer, cpu) {
4891 cpu_buffer = buffer->buffers[cpu];
4892
4893 reset_disabled_cpu_buffer(cpu_buffer);
4894
4895 atomic_dec(&cpu_buffer->record_disabled);
4896 atomic_dec(&cpu_buffer->resize_disabled);
4897 }
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004898}
Robert Richterc4f50182008-12-11 16:49:22 +01004899EXPORT_SYMBOL_GPL(ring_buffer_reset);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004900
4901/**
4902 * rind_buffer_empty - is the ring buffer empty?
4903 * @buffer: The ring buffer to test
4904 */
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05004905bool ring_buffer_empty(struct trace_buffer *buffer)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004906{
4907 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedtd4788202009-06-17 00:39:43 -04004908 unsigned long flags;
Steven Rostedt (Red Hat)289a5a22015-05-28 13:14:51 -04004909 bool dolock;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004910 int cpu;
Steven Rostedtd4788202009-06-17 00:39:43 -04004911 int ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004912
4913 /* yes this is racy, but if you don't like the race, lock the buffer */
4914 for_each_buffer_cpu(buffer, cpu) {
4915 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt8d707e82009-06-16 21:22:48 -04004916 local_irq_save(flags);
Steven Rostedt (Red Hat)289a5a22015-05-28 13:14:51 -04004917 dolock = rb_reader_lock(cpu_buffer);
Steven Rostedtd4788202009-06-17 00:39:43 -04004918 ret = rb_per_cpu_empty(cpu_buffer);
Steven Rostedt (Red Hat)289a5a22015-05-28 13:14:51 -04004919 rb_reader_unlock(cpu_buffer, dolock);
Steven Rostedt8d707e82009-06-16 21:22:48 -04004920 local_irq_restore(flags);
4921
Steven Rostedtd4788202009-06-17 00:39:43 -04004922 if (!ret)
Yaowei Bai3d4e2042015-09-29 22:43:32 +08004923 return false;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004924 }
Steven Rostedt554f7862009-03-11 22:00:13 -04004925
Yaowei Bai3d4e2042015-09-29 22:43:32 +08004926 return true;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004927}
Robert Richterc4f50182008-12-11 16:49:22 +01004928EXPORT_SYMBOL_GPL(ring_buffer_empty);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004929
4930/**
4931 * ring_buffer_empty_cpu - is a cpu buffer of a ring buffer empty?
4932 * @buffer: The ring buffer
4933 * @cpu: The CPU buffer to test
4934 */
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05004935bool ring_buffer_empty_cpu(struct trace_buffer *buffer, int cpu)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004936{
4937 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedtd4788202009-06-17 00:39:43 -04004938 unsigned long flags;
Steven Rostedt (Red Hat)289a5a22015-05-28 13:14:51 -04004939 bool dolock;
Steven Rostedt8aabee52009-03-12 13:13:49 -04004940 int ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004941
Rusty Russell9e01c1b2009-01-01 10:12:22 +10304942 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Yaowei Bai3d4e2042015-09-29 22:43:32 +08004943 return true;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004944
4945 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt8d707e82009-06-16 21:22:48 -04004946 local_irq_save(flags);
Steven Rostedt (Red Hat)289a5a22015-05-28 13:14:51 -04004947 dolock = rb_reader_lock(cpu_buffer);
Steven Rostedt554f7862009-03-11 22:00:13 -04004948 ret = rb_per_cpu_empty(cpu_buffer);
Steven Rostedt (Red Hat)289a5a22015-05-28 13:14:51 -04004949 rb_reader_unlock(cpu_buffer, dolock);
Steven Rostedt8d707e82009-06-16 21:22:48 -04004950 local_irq_restore(flags);
Steven Rostedt554f7862009-03-11 22:00:13 -04004951
4952 return ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004953}
Robert Richterc4f50182008-12-11 16:49:22 +01004954EXPORT_SYMBOL_GPL(ring_buffer_empty_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004955
Steven Rostedt85bac322009-09-04 14:24:40 -04004956#ifdef CONFIG_RING_BUFFER_ALLOW_SWAP
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004957/**
4958 * ring_buffer_swap_cpu - swap a CPU buffer between two ring buffers
4959 * @buffer_a: One buffer to swap with
4960 * @buffer_b: The other buffer to swap with
Fabian Frederick59e7cff2014-06-05 20:22:05 +02004961 * @cpu: the CPU of the buffers to swap
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004962 *
4963 * This function is useful for tracers that want to take a "snapshot"
4964 * of a CPU buffer and has another back up buffer lying around.
4965 * it is expected that the tracer handles the cpu buffer not being
4966 * used at the moment.
4967 */
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05004968int ring_buffer_swap_cpu(struct trace_buffer *buffer_a,
4969 struct trace_buffer *buffer_b, int cpu)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004970{
4971 struct ring_buffer_per_cpu *cpu_buffer_a;
4972 struct ring_buffer_per_cpu *cpu_buffer_b;
Steven Rostedt554f7862009-03-11 22:00:13 -04004973 int ret = -EINVAL;
4974
Rusty Russell9e01c1b2009-01-01 10:12:22 +10304975 if (!cpumask_test_cpu(cpu, buffer_a->cpumask) ||
4976 !cpumask_test_cpu(cpu, buffer_b->cpumask))
Steven Rostedt554f7862009-03-11 22:00:13 -04004977 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004978
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08004979 cpu_buffer_a = buffer_a->buffers[cpu];
4980 cpu_buffer_b = buffer_b->buffers[cpu];
4981
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004982 /* At least make sure the two buffers are somewhat the same */
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08004983 if (cpu_buffer_a->nr_pages != cpu_buffer_b->nr_pages)
Steven Rostedt554f7862009-03-11 22:00:13 -04004984 goto out;
4985
4986 ret = -EAGAIN;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004987
Steven Rostedt97b17ef2009-01-21 15:24:56 -05004988 if (atomic_read(&buffer_a->record_disabled))
Steven Rostedt554f7862009-03-11 22:00:13 -04004989 goto out;
Steven Rostedt97b17ef2009-01-21 15:24:56 -05004990
4991 if (atomic_read(&buffer_b->record_disabled))
Steven Rostedt554f7862009-03-11 22:00:13 -04004992 goto out;
Steven Rostedt97b17ef2009-01-21 15:24:56 -05004993
Steven Rostedt97b17ef2009-01-21 15:24:56 -05004994 if (atomic_read(&cpu_buffer_a->record_disabled))
Steven Rostedt554f7862009-03-11 22:00:13 -04004995 goto out;
Steven Rostedt97b17ef2009-01-21 15:24:56 -05004996
4997 if (atomic_read(&cpu_buffer_b->record_disabled))
Steven Rostedt554f7862009-03-11 22:00:13 -04004998 goto out;
Steven Rostedt97b17ef2009-01-21 15:24:56 -05004999
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04005000 /*
Paul E. McKenney74401722018-11-06 18:44:52 -08005001 * We can't do a synchronize_rcu here because this
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04005002 * function can be called in atomic context.
5003 * Normally this will be called from the same CPU as cpu.
5004 * If not it's up to the caller to protect this.
5005 */
5006 atomic_inc(&cpu_buffer_a->record_disabled);
5007 atomic_inc(&cpu_buffer_b->record_disabled);
5008
Steven Rostedt98277992009-09-02 10:56:15 -04005009 ret = -EBUSY;
5010 if (local_read(&cpu_buffer_a->committing))
5011 goto out_dec;
5012 if (local_read(&cpu_buffer_b->committing))
5013 goto out_dec;
5014
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04005015 buffer_a->buffers[cpu] = cpu_buffer_b;
5016 buffer_b->buffers[cpu] = cpu_buffer_a;
5017
5018 cpu_buffer_b->buffer = buffer_a;
5019 cpu_buffer_a->buffer = buffer_b;
5020
Steven Rostedt98277992009-09-02 10:56:15 -04005021 ret = 0;
5022
5023out_dec:
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04005024 atomic_dec(&cpu_buffer_a->record_disabled);
5025 atomic_dec(&cpu_buffer_b->record_disabled);
Steven Rostedt554f7862009-03-11 22:00:13 -04005026out:
Steven Rostedt554f7862009-03-11 22:00:13 -04005027 return ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04005028}
Robert Richterc4f50182008-12-11 16:49:22 +01005029EXPORT_SYMBOL_GPL(ring_buffer_swap_cpu);
Steven Rostedt85bac322009-09-04 14:24:40 -04005030#endif /* CONFIG_RING_BUFFER_ALLOW_SWAP */
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04005031
Steven Rostedt8789a9e2008-12-02 15:34:07 -05005032/**
5033 * ring_buffer_alloc_read_page - allocate a page to read from buffer
5034 * @buffer: the buffer to allocate for.
zhangwei(Jovi)d6118512013-07-15 16:32:50 +08005035 * @cpu: the cpu buffer to allocate.
Steven Rostedt8789a9e2008-12-02 15:34:07 -05005036 *
5037 * This function is used in conjunction with ring_buffer_read_page.
5038 * When reading a full page from the ring buffer, these functions
5039 * can be used to speed up the process. The calling function should
5040 * allocate a few pages first with this function. Then when it
5041 * needs to get pages from the ring buffer, it passes the result
5042 * of this function into ring_buffer_read_page, which will swap
5043 * the page that was allocated, with the read page of the buffer.
5044 *
5045 * Returns:
Steven Rostedt (VMware)a7e52ad2017-08-02 14:20:54 -04005046 * The page allocated, or ERR_PTR
Steven Rostedt8789a9e2008-12-02 15:34:07 -05005047 */
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05005048void *ring_buffer_alloc_read_page(struct trace_buffer *buffer, int cpu)
Steven Rostedt8789a9e2008-12-02 15:34:07 -05005049{
Steven Rostedt (VMware)a7e52ad2017-08-02 14:20:54 -04005050 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedt (VMware)73a757e2017-05-01 09:35:09 -04005051 struct buffer_data_page *bpage = NULL;
5052 unsigned long flags;
Vaibhav Nagarnaik7ea59062011-05-03 17:56:42 -07005053 struct page *page;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05005054
Steven Rostedt (VMware)a7e52ad2017-08-02 14:20:54 -04005055 if (!cpumask_test_cpu(cpu, buffer->cpumask))
5056 return ERR_PTR(-ENODEV);
5057
5058 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt (VMware)73a757e2017-05-01 09:35:09 -04005059 local_irq_save(flags);
5060 arch_spin_lock(&cpu_buffer->lock);
5061
5062 if (cpu_buffer->free_page) {
5063 bpage = cpu_buffer->free_page;
5064 cpu_buffer->free_page = NULL;
5065 }
5066
5067 arch_spin_unlock(&cpu_buffer->lock);
5068 local_irq_restore(flags);
5069
5070 if (bpage)
5071 goto out;
5072
Vaibhav Nagarnaikd7ec4bf2011-06-07 17:01:42 -07005073 page = alloc_pages_node(cpu_to_node(cpu),
5074 GFP_KERNEL | __GFP_NORETRY, 0);
Vaibhav Nagarnaik7ea59062011-05-03 17:56:42 -07005075 if (!page)
Steven Rostedt (VMware)a7e52ad2017-08-02 14:20:54 -04005076 return ERR_PTR(-ENOMEM);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05005077
Vaibhav Nagarnaik7ea59062011-05-03 17:56:42 -07005078 bpage = page_address(page);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05005079
Steven Rostedt (VMware)73a757e2017-05-01 09:35:09 -04005080 out:
Steven Rostedtef7a4a12009-03-03 00:27:49 -05005081 rb_init_page(bpage);
5082
Steven Rostedt044fa782008-12-02 23:50:03 -05005083 return bpage;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05005084}
Steven Rostedtd6ce96d2009-05-05 01:15:24 -04005085EXPORT_SYMBOL_GPL(ring_buffer_alloc_read_page);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05005086
5087/**
5088 * ring_buffer_free_read_page - free an allocated read page
5089 * @buffer: the buffer the page was allocate for
Steven Rostedt (VMware)73a757e2017-05-01 09:35:09 -04005090 * @cpu: the cpu buffer the page came from
Steven Rostedt8789a9e2008-12-02 15:34:07 -05005091 * @data: the page to free
5092 *
5093 * Free a page allocated from ring_buffer_alloc_read_page.
5094 */
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05005095void ring_buffer_free_read_page(struct trace_buffer *buffer, int cpu, void *data)
Steven Rostedt8789a9e2008-12-02 15:34:07 -05005096{
Steven Rostedt (VMware)73a757e2017-05-01 09:35:09 -04005097 struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
5098 struct buffer_data_page *bpage = data;
Steven Rostedt (VMware)ae415fa2017-12-22 21:19:29 -05005099 struct page *page = virt_to_page(bpage);
Steven Rostedt (VMware)73a757e2017-05-01 09:35:09 -04005100 unsigned long flags;
5101
Steven Rostedt (VMware)ae415fa2017-12-22 21:19:29 -05005102 /* If the page is still in use someplace else, we can't reuse it */
5103 if (page_ref_count(page) > 1)
5104 goto out;
5105
Steven Rostedt (VMware)73a757e2017-05-01 09:35:09 -04005106 local_irq_save(flags);
5107 arch_spin_lock(&cpu_buffer->lock);
5108
5109 if (!cpu_buffer->free_page) {
5110 cpu_buffer->free_page = bpage;
5111 bpage = NULL;
5112 }
5113
5114 arch_spin_unlock(&cpu_buffer->lock);
5115 local_irq_restore(flags);
5116
Steven Rostedt (VMware)ae415fa2017-12-22 21:19:29 -05005117 out:
Steven Rostedt (VMware)73a757e2017-05-01 09:35:09 -04005118 free_page((unsigned long)bpage);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05005119}
Steven Rostedtd6ce96d2009-05-05 01:15:24 -04005120EXPORT_SYMBOL_GPL(ring_buffer_free_read_page);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05005121
5122/**
5123 * ring_buffer_read_page - extract a page from the ring buffer
5124 * @buffer: buffer to extract from
5125 * @data_page: the page to use allocated from ring_buffer_alloc_read_page
Steven Rostedtef7a4a12009-03-03 00:27:49 -05005126 * @len: amount to extract
Steven Rostedt8789a9e2008-12-02 15:34:07 -05005127 * @cpu: the cpu of the buffer to extract
5128 * @full: should the extraction only happen when the page is full.
5129 *
5130 * This function will pull out a page from the ring buffer and consume it.
5131 * @data_page must be the address of the variable that was returned
5132 * from ring_buffer_alloc_read_page. This is because the page might be used
5133 * to swap with a page in the ring buffer.
5134 *
5135 * for example:
zhangwei(Jovi)d6118512013-07-15 16:32:50 +08005136 * rpage = ring_buffer_alloc_read_page(buffer, cpu);
Steven Rostedt (VMware)a7e52ad2017-08-02 14:20:54 -04005137 * if (IS_ERR(rpage))
5138 * return PTR_ERR(rpage);
Steven Rostedtef7a4a12009-03-03 00:27:49 -05005139 * ret = ring_buffer_read_page(buffer, &rpage, len, cpu, 0);
Lai Jiangshan667d2412009-02-09 14:21:17 +08005140 * if (ret >= 0)
5141 * process_page(rpage, ret);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05005142 *
5143 * When @full is set, the function will not return true unless
5144 * the writer is off the reader page.
5145 *
5146 * Note: it is up to the calling functions to handle sleeps and wakeups.
5147 * The ring buffer can be used anywhere in the kernel and can not
5148 * blindly call wake_up. The layer that uses the ring buffer must be
5149 * responsible for that.
5150 *
5151 * Returns:
Lai Jiangshan667d2412009-02-09 14:21:17 +08005152 * >=0 if data has been transferred, returns the offset of consumed data.
5153 * <0 if no data has been transferred.
Steven Rostedt8789a9e2008-12-02 15:34:07 -05005154 */
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05005155int ring_buffer_read_page(struct trace_buffer *buffer,
Steven Rostedtef7a4a12009-03-03 00:27:49 -05005156 void **data_page, size_t len, int cpu, int full)
Steven Rostedt8789a9e2008-12-02 15:34:07 -05005157{
5158 struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
5159 struct ring_buffer_event *event;
Steven Rostedt044fa782008-12-02 23:50:03 -05005160 struct buffer_data_page *bpage;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05005161 struct buffer_page *reader;
Steven Rostedtff0ff842010-03-31 22:11:42 -04005162 unsigned long missed_events;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05005163 unsigned long flags;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05005164 unsigned int commit;
Lai Jiangshan667d2412009-02-09 14:21:17 +08005165 unsigned int read;
Steven Rostedt4f3640f2009-03-03 23:52:42 -05005166 u64 save_timestamp;
Lai Jiangshan667d2412009-02-09 14:21:17 +08005167 int ret = -1;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05005168
Steven Rostedt554f7862009-03-11 22:00:13 -04005169 if (!cpumask_test_cpu(cpu, buffer->cpumask))
5170 goto out;
5171
Steven Rostedt474d32b2009-03-03 19:51:40 -05005172 /*
5173 * If len is not big enough to hold the page header, then
5174 * we can not copy anything.
5175 */
5176 if (len <= BUF_PAGE_HDR_SIZE)
Steven Rostedt554f7862009-03-11 22:00:13 -04005177 goto out;
Steven Rostedt474d32b2009-03-03 19:51:40 -05005178
5179 len -= BUF_PAGE_HDR_SIZE;
5180
Steven Rostedt8789a9e2008-12-02 15:34:07 -05005181 if (!data_page)
Steven Rostedt554f7862009-03-11 22:00:13 -04005182 goto out;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05005183
Steven Rostedt044fa782008-12-02 23:50:03 -05005184 bpage = *data_page;
5185 if (!bpage)
Steven Rostedt554f7862009-03-11 22:00:13 -04005186 goto out;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05005187
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02005188 raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05005189
Steven Rostedtef7a4a12009-03-03 00:27:49 -05005190 reader = rb_get_reader_page(cpu_buffer);
5191 if (!reader)
Steven Rostedt554f7862009-03-11 22:00:13 -04005192 goto out_unlock;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05005193
Steven Rostedtef7a4a12009-03-03 00:27:49 -05005194 event = rb_reader_event(cpu_buffer);
Lai Jiangshan667d2412009-02-09 14:21:17 +08005195
Steven Rostedtef7a4a12009-03-03 00:27:49 -05005196 read = reader->read;
5197 commit = rb_page_commit(reader);
5198
Steven Rostedt66a8cb92010-03-31 13:21:56 -04005199 /* Check if any events were dropped */
Steven Rostedtff0ff842010-03-31 22:11:42 -04005200 missed_events = cpu_buffer->lost_events;
Steven Rostedt66a8cb92010-03-31 13:21:56 -04005201
Steven Rostedt8789a9e2008-12-02 15:34:07 -05005202 /*
Steven Rostedt474d32b2009-03-03 19:51:40 -05005203 * If this page has been partially read or
5204 * if len is not big enough to read the rest of the page or
5205 * a writer is still on the page, then
5206 * we must copy the data from the page to the buffer.
5207 * Otherwise, we can simply swap the page with the one passed in.
Steven Rostedt8789a9e2008-12-02 15:34:07 -05005208 */
Steven Rostedt474d32b2009-03-03 19:51:40 -05005209 if (read || (len < (commit - read)) ||
Steven Rostedtef7a4a12009-03-03 00:27:49 -05005210 cpu_buffer->reader_page == cpu_buffer->commit_page) {
Lai Jiangshan667d2412009-02-09 14:21:17 +08005211 struct buffer_data_page *rpage = cpu_buffer->reader_page->page;
Steven Rostedt474d32b2009-03-03 19:51:40 -05005212 unsigned int rpos = read;
5213 unsigned int pos = 0;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05005214 unsigned int size;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05005215
5216 if (full)
Steven Rostedt554f7862009-03-11 22:00:13 -04005217 goto out_unlock;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05005218
Steven Rostedtef7a4a12009-03-03 00:27:49 -05005219 if (len > (commit - read))
5220 len = (commit - read);
5221
Steven Rostedt69d1b832010-10-07 18:18:05 -04005222 /* Always keep the time extend and data together */
5223 size = rb_event_ts_length(event);
Steven Rostedtef7a4a12009-03-03 00:27:49 -05005224
5225 if (len < size)
Steven Rostedt554f7862009-03-11 22:00:13 -04005226 goto out_unlock;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05005227
Steven Rostedt4f3640f2009-03-03 23:52:42 -05005228 /* save the current timestamp, since the user will need it */
5229 save_timestamp = cpu_buffer->read_stamp;
5230
Steven Rostedtef7a4a12009-03-03 00:27:49 -05005231 /* Need to copy one event at a time */
5232 do {
David Sharpe1e35922010-12-22 16:38:24 -08005233 /* We need the size of one event, because
5234 * rb_advance_reader only advances by one event,
5235 * whereas rb_event_ts_length may include the size of
5236 * one or two events.
5237 * We have already ensured there's enough space if this
5238 * is a time extend. */
5239 size = rb_event_length(event);
Steven Rostedt474d32b2009-03-03 19:51:40 -05005240 memcpy(bpage->data + pos, rpage->data + rpos, size);
Steven Rostedtef7a4a12009-03-03 00:27:49 -05005241
5242 len -= size;
5243
5244 rb_advance_reader(cpu_buffer);
Steven Rostedt474d32b2009-03-03 19:51:40 -05005245 rpos = reader->read;
5246 pos += size;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05005247
Huang Ying18fab912010-07-28 14:14:01 +08005248 if (rpos >= commit)
5249 break;
5250
Steven Rostedtef7a4a12009-03-03 00:27:49 -05005251 event = rb_reader_event(cpu_buffer);
Steven Rostedt69d1b832010-10-07 18:18:05 -04005252 /* Always keep the time extend and data together */
5253 size = rb_event_ts_length(event);
David Sharpe1e35922010-12-22 16:38:24 -08005254 } while (len >= size);
Lai Jiangshan667d2412009-02-09 14:21:17 +08005255
5256 /* update bpage */
Steven Rostedtef7a4a12009-03-03 00:27:49 -05005257 local_set(&bpage->commit, pos);
Steven Rostedt4f3640f2009-03-03 23:52:42 -05005258 bpage->time_stamp = save_timestamp;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05005259
Steven Rostedt474d32b2009-03-03 19:51:40 -05005260 /* we copied everything to the beginning */
5261 read = 0;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05005262 } else {
Steven Rostedtafbab762009-05-01 19:40:05 -04005263 /* update the entry counter */
Steven Rostedt77ae3652009-03-27 11:00:29 -04005264 cpu_buffer->read += rb_page_entries(reader);
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -07005265 cpu_buffer->read_bytes += BUF_PAGE_SIZE;
Steven Rostedtafbab762009-05-01 19:40:05 -04005266
Steven Rostedt8789a9e2008-12-02 15:34:07 -05005267 /* swap the pages */
Steven Rostedt044fa782008-12-02 23:50:03 -05005268 rb_init_page(bpage);
Steven Rostedtef7a4a12009-03-03 00:27:49 -05005269 bpage = reader->page;
5270 reader->page = *data_page;
5271 local_set(&reader->write, 0);
Steven Rostedt778c55d2009-05-01 18:44:45 -04005272 local_set(&reader->entries, 0);
Steven Rostedtef7a4a12009-03-03 00:27:49 -05005273 reader->read = 0;
Steven Rostedt044fa782008-12-02 23:50:03 -05005274 *data_page = bpage;
Steven Rostedtff0ff842010-03-31 22:11:42 -04005275
5276 /*
5277 * Use the real_end for the data size,
5278 * This gives us a chance to store the lost events
5279 * on the page.
5280 */
5281 if (reader->real_end)
5282 local_set(&bpage->commit, reader->real_end);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05005283 }
Lai Jiangshan667d2412009-02-09 14:21:17 +08005284 ret = read;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05005285
Steven Rostedt66a8cb92010-03-31 13:21:56 -04005286 cpu_buffer->lost_events = 0;
Steven Rostedt2711ca22010-05-21 13:32:26 -04005287
5288 commit = local_read(&bpage->commit);
Steven Rostedt66a8cb92010-03-31 13:21:56 -04005289 /*
5290 * Set a flag in the commit field if we lost events
5291 */
Steven Rostedtff0ff842010-03-31 22:11:42 -04005292 if (missed_events) {
Steven Rostedtff0ff842010-03-31 22:11:42 -04005293 /* If there is room at the end of the page to save the
5294 * missed events, then record it there.
5295 */
5296 if (BUF_PAGE_SIZE - commit >= sizeof(missed_events)) {
5297 memcpy(&bpage->data[commit], &missed_events,
5298 sizeof(missed_events));
5299 local_add(RB_MISSED_STORED, &bpage->commit);
Steven Rostedt2711ca22010-05-21 13:32:26 -04005300 commit += sizeof(missed_events);
Steven Rostedtff0ff842010-03-31 22:11:42 -04005301 }
Steven Rostedt66a8cb92010-03-31 13:21:56 -04005302 local_add(RB_MISSED_EVENTS, &bpage->commit);
Steven Rostedtff0ff842010-03-31 22:11:42 -04005303 }
Steven Rostedt66a8cb92010-03-31 13:21:56 -04005304
Steven Rostedt2711ca22010-05-21 13:32:26 -04005305 /*
5306 * This page may be off to user land. Zero it out here.
5307 */
5308 if (commit < BUF_PAGE_SIZE)
5309 memset(&bpage->data[commit], 0, BUF_PAGE_SIZE - commit);
5310
Steven Rostedt554f7862009-03-11 22:00:13 -04005311 out_unlock:
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02005312 raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05005313
Steven Rostedt554f7862009-03-11 22:00:13 -04005314 out:
Steven Rostedt8789a9e2008-12-02 15:34:07 -05005315 return ret;
5316}
Steven Rostedtd6ce96d2009-05-05 01:15:24 -04005317EXPORT_SYMBOL_GPL(ring_buffer_read_page);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05005318
Sebastian Andrzej Siewiorb32614c2016-11-27 00:13:34 +01005319/*
5320 * We only allocate new buffers, never free them if the CPU goes down.
5321 * If we were to free the buffer, then the user would lose any trace that was in
5322 * the buffer.
5323 */
5324int trace_rb_cpu_prepare(unsigned int cpu, struct hlist_node *node)
Steven Rostedt554f7862009-03-11 22:00:13 -04005325{
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05005326 struct trace_buffer *buffer;
Steven Rostedt (Red Hat)9b94a8f2016-05-12 11:01:24 -04005327 long nr_pages_same;
5328 int cpu_i;
5329 unsigned long nr_pages;
Steven Rostedt554f7862009-03-11 22:00:13 -04005330
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05005331 buffer = container_of(node, struct trace_buffer, node);
Sebastian Andrzej Siewiorb32614c2016-11-27 00:13:34 +01005332 if (cpumask_test_cpu(cpu, buffer->cpumask))
5333 return 0;
Steven Rostedt554f7862009-03-11 22:00:13 -04005334
Sebastian Andrzej Siewiorb32614c2016-11-27 00:13:34 +01005335 nr_pages = 0;
5336 nr_pages_same = 1;
5337 /* check if all cpu sizes are same */
5338 for_each_buffer_cpu(buffer, cpu_i) {
5339 /* fill in the size from first enabled cpu */
5340 if (nr_pages == 0)
5341 nr_pages = buffer->buffers[cpu_i]->nr_pages;
5342 if (nr_pages != buffer->buffers[cpu_i]->nr_pages) {
5343 nr_pages_same = 0;
5344 break;
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08005345 }
Steven Rostedt554f7862009-03-11 22:00:13 -04005346 }
Sebastian Andrzej Siewiorb32614c2016-11-27 00:13:34 +01005347 /* allocate minimum pages, user can later expand it */
5348 if (!nr_pages_same)
5349 nr_pages = 2;
5350 buffer->buffers[cpu] =
5351 rb_allocate_cpu_buffer(buffer, nr_pages, cpu);
5352 if (!buffer->buffers[cpu]) {
5353 WARN(1, "failed to allocate ring buffer on CPU %u\n",
5354 cpu);
5355 return -ENOMEM;
5356 }
5357 smp_wmb();
5358 cpumask_set_cpu(cpu, buffer->cpumask);
5359 return 0;
Steven Rostedt554f7862009-03-11 22:00:13 -04005360}
Steven Rostedt (Red Hat)6c43e552013-03-15 11:32:53 -04005361
5362#ifdef CONFIG_RING_BUFFER_STARTUP_TEST
5363/*
5364 * This is a basic integrity check of the ring buffer.
5365 * Late in the boot cycle this test will run when configured in.
5366 * It will kick off a thread per CPU that will go into a loop
5367 * writing to the per cpu ring buffer various sizes of data.
5368 * Some of the data will be large items, some small.
5369 *
5370 * Another thread is created that goes into a spin, sending out
5371 * IPIs to the other CPUs to also write into the ring buffer.
5372 * this is to test the nesting ability of the buffer.
5373 *
5374 * Basic stats are recorded and reported. If something in the
5375 * ring buffer should happen that's not expected, a big warning
5376 * is displayed and all ring buffers are disabled.
5377 */
5378static struct task_struct *rb_threads[NR_CPUS] __initdata;
5379
5380struct rb_test_data {
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05005381 struct trace_buffer *buffer;
Steven Rostedt (Red Hat)6c43e552013-03-15 11:32:53 -04005382 unsigned long events;
5383 unsigned long bytes_written;
5384 unsigned long bytes_alloc;
5385 unsigned long bytes_dropped;
5386 unsigned long events_nested;
5387 unsigned long bytes_written_nested;
5388 unsigned long bytes_alloc_nested;
5389 unsigned long bytes_dropped_nested;
5390 int min_size_nested;
5391 int max_size_nested;
5392 int max_size;
5393 int min_size;
5394 int cpu;
5395 int cnt;
5396};
5397
5398static struct rb_test_data rb_data[NR_CPUS] __initdata;
5399
5400/* 1 meg per cpu */
5401#define RB_TEST_BUFFER_SIZE 1048576
5402
5403static char rb_string[] __initdata =
5404 "abcdefghijklmnopqrstuvwxyz1234567890!@#$%^&*()?+\\"
5405 "?+|:';\",.<>/?abcdefghijklmnopqrstuvwxyz1234567890"
5406 "!@#$%^&*()?+\\?+|:';\",.<>/?abcdefghijklmnopqrstuv";
5407
5408static bool rb_test_started __initdata;
5409
5410struct rb_item {
5411 int size;
5412 char str[];
5413};
5414
5415static __init int rb_write_something(struct rb_test_data *data, bool nested)
5416{
5417 struct ring_buffer_event *event;
5418 struct rb_item *item;
5419 bool started;
5420 int event_len;
5421 int size;
5422 int len;
5423 int cnt;
5424
5425 /* Have nested writes different that what is written */
5426 cnt = data->cnt + (nested ? 27 : 0);
5427
5428 /* Multiply cnt by ~e, to make some unique increment */
YueHaibing40ed29b2018-09-23 12:11:33 +00005429 size = (cnt * 68 / 25) % (sizeof(rb_string) - 1);
Steven Rostedt (Red Hat)6c43e552013-03-15 11:32:53 -04005430
5431 len = size + sizeof(struct rb_item);
5432
5433 started = rb_test_started;
5434 /* read rb_test_started before checking buffer enabled */
5435 smp_rmb();
5436
5437 event = ring_buffer_lock_reserve(data->buffer, len);
5438 if (!event) {
5439 /* Ignore dropped events before test starts. */
5440 if (started) {
5441 if (nested)
5442 data->bytes_dropped += len;
5443 else
5444 data->bytes_dropped_nested += len;
5445 }
5446 return len;
5447 }
5448
5449 event_len = ring_buffer_event_length(event);
5450
5451 if (RB_WARN_ON(data->buffer, event_len < len))
5452 goto out;
5453
5454 item = ring_buffer_event_data(event);
5455 item->size = size;
5456 memcpy(item->str, rb_string, size);
5457
5458 if (nested) {
5459 data->bytes_alloc_nested += event_len;
5460 data->bytes_written_nested += len;
5461 data->events_nested++;
5462 if (!data->min_size_nested || len < data->min_size_nested)
5463 data->min_size_nested = len;
5464 if (len > data->max_size_nested)
5465 data->max_size_nested = len;
5466 } else {
5467 data->bytes_alloc += event_len;
5468 data->bytes_written += len;
5469 data->events++;
5470 if (!data->min_size || len < data->min_size)
5471 data->max_size = len;
5472 if (len > data->max_size)
5473 data->max_size = len;
5474 }
5475
5476 out:
5477 ring_buffer_unlock_commit(data->buffer, event);
5478
5479 return 0;
5480}
5481
5482static __init int rb_test(void *arg)
5483{
5484 struct rb_test_data *data = arg;
5485
5486 while (!kthread_should_stop()) {
5487 rb_write_something(data, false);
5488 data->cnt++;
5489
5490 set_current_state(TASK_INTERRUPTIBLE);
5491 /* Now sleep between a min of 100-300us and a max of 1ms */
5492 usleep_range(((data->cnt % 3) + 1) * 100, 1000);
5493 }
5494
5495 return 0;
5496}
5497
5498static __init void rb_ipi(void *ignore)
5499{
5500 struct rb_test_data *data;
5501 int cpu = smp_processor_id();
5502
5503 data = &rb_data[cpu];
5504 rb_write_something(data, true);
5505}
5506
5507static __init int rb_hammer_test(void *arg)
5508{
5509 while (!kthread_should_stop()) {
5510
5511 /* Send an IPI to all cpus to write data! */
5512 smp_call_function(rb_ipi, NULL, 1);
5513 /* No sleep, but for non preempt, let others run */
5514 schedule();
5515 }
5516
5517 return 0;
5518}
5519
5520static __init int test_ringbuffer(void)
5521{
5522 struct task_struct *rb_hammer;
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05005523 struct trace_buffer *buffer;
Steven Rostedt (Red Hat)6c43e552013-03-15 11:32:53 -04005524 int cpu;
5525 int ret = 0;
5526
Steven Rostedt (VMware)a3566462019-12-02 16:25:27 -05005527 if (security_locked_down(LOCKDOWN_TRACEFS)) {
Stephen Rothwellee195452019-12-06 09:25:03 +11005528 pr_warn("Lockdown is enabled, skipping ring buffer tests\n");
Steven Rostedt (VMware)a3566462019-12-02 16:25:27 -05005529 return 0;
5530 }
5531
Steven Rostedt (Red Hat)6c43e552013-03-15 11:32:53 -04005532 pr_info("Running ring buffer tests...\n");
5533
5534 buffer = ring_buffer_alloc(RB_TEST_BUFFER_SIZE, RB_FL_OVERWRITE);
5535 if (WARN_ON(!buffer))
5536 return 0;
5537
5538 /* Disable buffer so that threads can't write to it yet */
5539 ring_buffer_record_off(buffer);
5540
5541 for_each_online_cpu(cpu) {
5542 rb_data[cpu].buffer = buffer;
5543 rb_data[cpu].cpu = cpu;
5544 rb_data[cpu].cnt = cpu;
5545 rb_threads[cpu] = kthread_create(rb_test, &rb_data[cpu],
5546 "rbtester/%d", cpu);
Wei Yongjun62277de2016-06-17 17:33:59 +00005547 if (WARN_ON(IS_ERR(rb_threads[cpu]))) {
Steven Rostedt (Red Hat)6c43e552013-03-15 11:32:53 -04005548 pr_cont("FAILED\n");
Wei Yongjun62277de2016-06-17 17:33:59 +00005549 ret = PTR_ERR(rb_threads[cpu]);
Steven Rostedt (Red Hat)6c43e552013-03-15 11:32:53 -04005550 goto out_free;
5551 }
5552
5553 kthread_bind(rb_threads[cpu], cpu);
5554 wake_up_process(rb_threads[cpu]);
5555 }
5556
5557 /* Now create the rb hammer! */
5558 rb_hammer = kthread_run(rb_hammer_test, NULL, "rbhammer");
Wei Yongjun62277de2016-06-17 17:33:59 +00005559 if (WARN_ON(IS_ERR(rb_hammer))) {
Steven Rostedt (Red Hat)6c43e552013-03-15 11:32:53 -04005560 pr_cont("FAILED\n");
Wei Yongjun62277de2016-06-17 17:33:59 +00005561 ret = PTR_ERR(rb_hammer);
Steven Rostedt (Red Hat)6c43e552013-03-15 11:32:53 -04005562 goto out_free;
5563 }
5564
5565 ring_buffer_record_on(buffer);
5566 /*
5567 * Show buffer is enabled before setting rb_test_started.
5568 * Yes there's a small race window where events could be
5569 * dropped and the thread wont catch it. But when a ring
5570 * buffer gets enabled, there will always be some kind of
5571 * delay before other CPUs see it. Thus, we don't care about
5572 * those dropped events. We care about events dropped after
5573 * the threads see that the buffer is active.
5574 */
5575 smp_wmb();
5576 rb_test_started = true;
5577
5578 set_current_state(TASK_INTERRUPTIBLE);
5579 /* Just run for 10 seconds */;
5580 schedule_timeout(10 * HZ);
5581
5582 kthread_stop(rb_hammer);
5583
5584 out_free:
5585 for_each_online_cpu(cpu) {
5586 if (!rb_threads[cpu])
5587 break;
5588 kthread_stop(rb_threads[cpu]);
5589 }
5590 if (ret) {
5591 ring_buffer_free(buffer);
5592 return ret;
5593 }
5594
5595 /* Report! */
5596 pr_info("finished\n");
5597 for_each_online_cpu(cpu) {
5598 struct ring_buffer_event *event;
5599 struct rb_test_data *data = &rb_data[cpu];
5600 struct rb_item *item;
5601 unsigned long total_events;
5602 unsigned long total_dropped;
5603 unsigned long total_written;
5604 unsigned long total_alloc;
5605 unsigned long total_read = 0;
5606 unsigned long total_size = 0;
5607 unsigned long total_len = 0;
5608 unsigned long total_lost = 0;
5609 unsigned long lost;
5610 int big_event_size;
5611 int small_event_size;
5612
5613 ret = -1;
5614
5615 total_events = data->events + data->events_nested;
5616 total_written = data->bytes_written + data->bytes_written_nested;
5617 total_alloc = data->bytes_alloc + data->bytes_alloc_nested;
5618 total_dropped = data->bytes_dropped + data->bytes_dropped_nested;
5619
5620 big_event_size = data->max_size + data->max_size_nested;
5621 small_event_size = data->min_size + data->min_size_nested;
5622
5623 pr_info("CPU %d:\n", cpu);
5624 pr_info(" events: %ld\n", total_events);
5625 pr_info(" dropped bytes: %ld\n", total_dropped);
5626 pr_info(" alloced bytes: %ld\n", total_alloc);
5627 pr_info(" written bytes: %ld\n", total_written);
5628 pr_info(" biggest event: %d\n", big_event_size);
5629 pr_info(" smallest event: %d\n", small_event_size);
5630
5631 if (RB_WARN_ON(buffer, total_dropped))
5632 break;
5633
5634 ret = 0;
5635
5636 while ((event = ring_buffer_consume(buffer, cpu, NULL, &lost))) {
5637 total_lost += lost;
5638 item = ring_buffer_event_data(event);
5639 total_len += ring_buffer_event_length(event);
5640 total_size += item->size + sizeof(struct rb_item);
5641 if (memcmp(&item->str[0], rb_string, item->size) != 0) {
5642 pr_info("FAILED!\n");
5643 pr_info("buffer had: %.*s\n", item->size, item->str);
5644 pr_info("expected: %.*s\n", item->size, rb_string);
5645 RB_WARN_ON(buffer, 1);
5646 ret = -1;
5647 break;
5648 }
5649 total_read++;
5650 }
5651 if (ret)
5652 break;
5653
5654 ret = -1;
5655
5656 pr_info(" read events: %ld\n", total_read);
5657 pr_info(" lost events: %ld\n", total_lost);
5658 pr_info(" total events: %ld\n", total_lost + total_read);
5659 pr_info(" recorded len bytes: %ld\n", total_len);
5660 pr_info(" recorded size bytes: %ld\n", total_size);
5661 if (total_lost)
5662 pr_info(" With dropped events, record len and size may not match\n"
5663 " alloced and written from above\n");
5664 if (!total_lost) {
5665 if (RB_WARN_ON(buffer, total_len != total_alloc ||
5666 total_size != total_written))
5667 break;
5668 }
5669 if (RB_WARN_ON(buffer, total_lost + total_read != total_events))
5670 break;
5671
5672 ret = 0;
5673 }
5674 if (!ret)
5675 pr_info("Ring buffer PASSED!\n");
5676
5677 ring_buffer_free(buffer);
5678 return 0;
5679}
5680
5681late_initcall(test_ringbuffer);
5682#endif /* CONFIG_RING_BUFFER_STARTUP_TEST */