blob: 342f49c3ccc5dbe3f804f5c7e6b985f388b1ad93 [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 (VMware)28575c62020-11-02 14:43:10 -05007#include <linux/trace_recursion.h>
Steven Rostedt (Red Hat)af658dc2015-04-29 14:36:05 -04008#include <linux/trace_events.h>
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04009#include <linux/ring_buffer.h>
Ingo Molnar14131f22009-02-26 18:47:11 +010010#include <linux/trace_clock.h>
Ingo Molnare6017572017-02-01 16:36:40 +010011#include <linux/sched/clock.h>
Steven Rostedt0b074362013-01-22 16:58:30 -050012#include <linux/trace_seq.h>
Steven Rostedt7a8e76a2008-09-29 23:02:38 -040013#include <linux/spinlock.h>
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -050014#include <linux/irq_work.h>
Steven Rostedt (VMware)a3566462019-12-02 16:25:27 -050015#include <linux/security.h>
Steven Rostedt7a8e76a2008-09-29 23:02:38 -040016#include <linux/uaccess.h>
Steven Rostedta81bd802009-02-06 01:45:16 -050017#include <linux/hardirq.h>
Steven Rostedt (Red Hat)6c43e552013-03-15 11:32:53 -040018#include <linux/kthread.h> /* for self test */
Steven Rostedt7a8e76a2008-09-29 23:02:38 -040019#include <linux/module.h>
20#include <linux/percpu.h>
21#include <linux/mutex.h>
Steven Rostedt (Red Hat)6c43e552013-03-15 11:32:53 -040022#include <linux/delay.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090023#include <linux/slab.h>
Steven Rostedt7a8e76a2008-09-29 23:02:38 -040024#include <linux/init.h>
25#include <linux/hash.h>
26#include <linux/list.h>
Steven Rostedt554f7862009-03-11 22:00:13 -040027#include <linux/cpu.h>
Steven Rostedt (VMware)927e56d2018-04-04 11:29:57 -040028#include <linux/oom.h>
Steven Rostedt7a8e76a2008-09-29 23:02:38 -040029
Christoph Lameter79615762010-01-05 15:34:50 +090030#include <asm/local.h>
Steven Rostedt182e9f52008-11-03 23:15:56 -050031
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -070032static void update_pages_handler(struct work_struct *work);
33
Steven Rostedt033601a2008-11-21 12:41:55 -050034/*
Steven Rostedtd1b182a2009-04-15 16:53:47 -040035 * The ring buffer header is special. We must manually up keep it.
36 */
37int ring_buffer_print_entry_header(struct trace_seq *s)
38{
Steven Rostedt (Red Hat)c0cd93a2014-11-12 11:49:00 -050039 trace_seq_puts(s, "# compressed entry header\n");
40 trace_seq_puts(s, "\ttype_len : 5 bits\n");
41 trace_seq_puts(s, "\ttime_delta : 27 bits\n");
42 trace_seq_puts(s, "\tarray : 32 bits\n");
43 trace_seq_putc(s, '\n');
44 trace_seq_printf(s, "\tpadding : type == %d\n",
45 RINGBUF_TYPE_PADDING);
46 trace_seq_printf(s, "\ttime_extend : type == %d\n",
47 RINGBUF_TYPE_TIME_EXTEND);
Tom Zanussidc4e2802018-01-15 20:51:40 -060048 trace_seq_printf(s, "\ttime_stamp : type == %d\n",
49 RINGBUF_TYPE_TIME_STAMP);
Steven Rostedt (Red Hat)c0cd93a2014-11-12 11:49:00 -050050 trace_seq_printf(s, "\tdata max type_len == %d\n",
51 RINGBUF_TYPE_DATA_TYPE_LEN_MAX);
Steven Rostedtd1b182a2009-04-15 16:53:47 -040052
Steven Rostedt (Red Hat)c0cd93a2014-11-12 11:49:00 -050053 return !trace_seq_has_overflowed(s);
Steven Rostedtd1b182a2009-04-15 16:53:47 -040054}
55
56/*
Steven Rostedt5cc98542009-03-12 22:24:17 -040057 * The ring buffer is made up of a list of pages. A separate list of pages is
58 * allocated for each CPU. A writer may only write to a buffer that is
59 * associated with the CPU it is currently executing on. A reader may read
60 * from any per cpu buffer.
61 *
62 * The reader is special. For each per cpu buffer, the reader has its own
63 * reader page. When a reader has read the entire reader page, this reader
64 * page is swapped with another page in the ring buffer.
65 *
66 * Now, as long as the writer is off the reader page, the reader can do what
67 * ever it wants with that page. The writer will never write to that page
68 * again (as long as it is out of the ring buffer).
69 *
70 * Here's some silly ASCII art.
71 *
72 * +------+
73 * |reader| RING BUFFER
74 * |page |
75 * +------+ +---+ +---+ +---+
76 * | |-->| |-->| |
77 * +---+ +---+ +---+
78 * ^ |
79 * | |
80 * +---------------+
81 *
82 *
83 * +------+
84 * |reader| RING BUFFER
85 * |page |------------------v
86 * +------+ +---+ +---+ +---+
87 * | |-->| |-->| |
88 * +---+ +---+ +---+
89 * ^ |
90 * | |
91 * +---------------+
92 *
93 *
94 * +------+
95 * |reader| RING BUFFER
96 * |page |------------------v
97 * +------+ +---+ +---+ +---+
98 * ^ | |-->| |-->| |
99 * | +---+ +---+ +---+
100 * | |
101 * | |
102 * +------------------------------+
103 *
104 *
105 * +------+
106 * |buffer| RING BUFFER
107 * |page |------------------v
108 * +------+ +---+ +---+ +---+
109 * ^ | | | |-->| |
110 * | New +---+ +---+ +---+
111 * | Reader------^ |
112 * | page |
113 * +------------------------------+
114 *
115 *
116 * After we make this swap, the reader can hand this page off to the splice
117 * code and be done with it. It can even allocate a new page if it needs to
118 * and swap that into the ring buffer.
119 *
120 * We will be using cmpxchg soon to make all this lockless.
121 *
122 */
123
Steven Rostedt499e5472012-02-22 15:50:28 -0500124/* Used for individual buffers (after the counter) */
125#define RB_BUFFER_OFF (1 << 20)
126
Steven Rostedt474d32b2009-03-03 19:51:40 -0500127#define BUF_PAGE_HDR_SIZE offsetof(struct buffer_data_page, data)
128
Steven Rostedte3d6bf02009-03-03 13:53:07 -0500129#define RB_EVNT_HDR_SIZE (offsetof(struct ring_buffer_event, array))
Andrew Morton67d34722009-01-09 12:27:09 -0800130#define RB_ALIGNMENT 4U
Lai Jiangshan334d4162009-04-24 11:27:05 +0800131#define RB_MAX_SMALL_DATA (RB_ALIGNMENT * RINGBUF_TYPE_DATA_TYPE_LEN_MAX)
Steven Rostedtc7b09302009-06-11 11:12:00 -0400132#define RB_EVNT_MIN_SIZE 8U /* two 32bit words */
Steven Rostedt (VMware)adab66b2020-12-14 12:33:51 -0500133
134#ifndef CONFIG_HAVE_64BIT_ALIGNED_ACCESS
135# define RB_FORCE_8BYTE_ALIGNMENT 0
136# define RB_ARCH_ALIGNMENT RB_ALIGNMENT
137#else
138# define RB_FORCE_8BYTE_ALIGNMENT 1
139# define RB_ARCH_ALIGNMENT 8U
140#endif
141
142#define RB_ALIGN_DATA __aligned(RB_ARCH_ALIGNMENT)
James Hogan649508f2012-05-30 12:11:19 +0100143
Lai Jiangshan334d4162009-04-24 11:27:05 +0800144/* define RINGBUF_TYPE_DATA for 'case RINGBUF_TYPE_DATA:' */
145#define RINGBUF_TYPE_DATA 0 ... RINGBUF_TYPE_DATA_TYPE_LEN_MAX
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400146
147enum {
148 RB_LEN_TIME_EXTEND = 8,
Tom Zanussidc4e2802018-01-15 20:51:40 -0600149 RB_LEN_TIME_STAMP = 8,
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400150};
151
Steven Rostedt69d1b832010-10-07 18:18:05 -0400152#define skip_time_extend(event) \
153 ((struct ring_buffer_event *)((char *)event + RB_LEN_TIME_EXTEND))
154
Tom Zanussidc4e2802018-01-15 20:51:40 -0600155#define extended_time(event) \
156 (event->type_len >= RINGBUF_TYPE_TIME_EXTEND)
157
Tom Zanussi2d622712009-03-22 03:30:49 -0500158static inline int rb_null_event(struct ring_buffer_event *event)
159{
Steven Rostedta1863c22009-09-03 10:23:58 -0400160 return event->type_len == RINGBUF_TYPE_PADDING && !event->time_delta;
Tom Zanussi2d622712009-03-22 03:30:49 -0500161}
162
163static void rb_event_set_padding(struct ring_buffer_event *event)
164{
Steven Rostedta1863c22009-09-03 10:23:58 -0400165 /* padding has a NULL time_delta */
Lai Jiangshan334d4162009-04-24 11:27:05 +0800166 event->type_len = RINGBUF_TYPE_PADDING;
Tom Zanussi2d622712009-03-22 03:30:49 -0500167 event->time_delta = 0;
168}
169
Tom Zanussi2d622712009-03-22 03:30:49 -0500170static unsigned
171rb_event_data_length(struct ring_buffer_event *event)
172{
173 unsigned length;
174
Lai Jiangshan334d4162009-04-24 11:27:05 +0800175 if (event->type_len)
176 length = event->type_len * RB_ALIGNMENT;
Tom Zanussi2d622712009-03-22 03:30:49 -0500177 else
178 length = event->array[0];
179 return length + RB_EVNT_HDR_SIZE;
180}
181
Steven Rostedt69d1b832010-10-07 18:18:05 -0400182/*
183 * Return the length of the given event. Will return
184 * the length of the time extend if the event is a
185 * time extend.
186 */
187static inline unsigned
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400188rb_event_length(struct ring_buffer_event *event)
189{
Lai Jiangshan334d4162009-04-24 11:27:05 +0800190 switch (event->type_len) {
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400191 case RINGBUF_TYPE_PADDING:
Tom Zanussi2d622712009-03-22 03:30:49 -0500192 if (rb_null_event(event))
193 /* undefined */
194 return -1;
Lai Jiangshan334d4162009-04-24 11:27:05 +0800195 return event->array[0] + RB_EVNT_HDR_SIZE;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400196
197 case RINGBUF_TYPE_TIME_EXTEND:
198 return RB_LEN_TIME_EXTEND;
199
200 case RINGBUF_TYPE_TIME_STAMP:
201 return RB_LEN_TIME_STAMP;
202
203 case RINGBUF_TYPE_DATA:
Tom Zanussi2d622712009-03-22 03:30:49 -0500204 return rb_event_data_length(event);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400205 default:
Steven Rostedt (VMware)da4d4012020-05-13 15:36:22 -0400206 WARN_ON_ONCE(1);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400207 }
208 /* not hit */
209 return 0;
210}
211
Steven Rostedt69d1b832010-10-07 18:18:05 -0400212/*
213 * Return total length of time extend and data,
214 * or just the event length for all other events.
215 */
216static inline unsigned
217rb_event_ts_length(struct ring_buffer_event *event)
218{
219 unsigned len = 0;
220
Tom Zanussidc4e2802018-01-15 20:51:40 -0600221 if (extended_time(event)) {
Steven Rostedt69d1b832010-10-07 18:18:05 -0400222 /* time extends include the data event after it */
223 len = RB_LEN_TIME_EXTEND;
224 event = skip_time_extend(event);
225 }
226 return len + rb_event_length(event);
227}
228
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400229/**
230 * ring_buffer_event_length - return the length of the event
231 * @event: the event to get the length of
Steven Rostedt69d1b832010-10-07 18:18:05 -0400232 *
233 * Returns the size of the data load of a data event.
234 * If the event is something other than a data event, it
235 * returns the size of the event itself. With the exception
236 * of a TIME EXTEND, where it still returns the size of the
237 * data load of the data event after it.
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400238 */
239unsigned ring_buffer_event_length(struct ring_buffer_event *event)
240{
Steven Rostedt69d1b832010-10-07 18:18:05 -0400241 unsigned length;
242
Tom Zanussidc4e2802018-01-15 20:51:40 -0600243 if (extended_time(event))
Steven Rostedt69d1b832010-10-07 18:18:05 -0400244 event = skip_time_extend(event);
245
246 length = rb_event_length(event);
Lai Jiangshan334d4162009-04-24 11:27:05 +0800247 if (event->type_len > RINGBUF_TYPE_DATA_TYPE_LEN_MAX)
Robert Richter465634a2009-01-07 15:32:11 +0100248 return length;
249 length -= RB_EVNT_HDR_SIZE;
250 if (length > RB_MAX_SMALL_DATA + sizeof(event->array[0]))
251 length -= sizeof(event->array[0]);
252 return length;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400253}
Robert Richterc4f50182008-12-11 16:49:22 +0100254EXPORT_SYMBOL_GPL(ring_buffer_event_length);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400255
256/* inline for ring buffer fast paths */
Steven Rostedt (Red Hat)929ddbf2016-11-23 11:40:34 -0500257static __always_inline void *
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400258rb_event_data(struct ring_buffer_event *event)
259{
Tom Zanussidc4e2802018-01-15 20:51:40 -0600260 if (extended_time(event))
Steven Rostedt69d1b832010-10-07 18:18:05 -0400261 event = skip_time_extend(event);
Steven Rostedt (VMware)da4d4012020-05-13 15:36:22 -0400262 WARN_ON_ONCE(event->type_len > RINGBUF_TYPE_DATA_TYPE_LEN_MAX);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400263 /* If length is in len field, then array[0] has the data */
Lai Jiangshan334d4162009-04-24 11:27:05 +0800264 if (event->type_len)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400265 return (void *)&event->array[0];
266 /* Otherwise length is in array[0] and array[1] has the data */
267 return (void *)&event->array[1];
268}
269
270/**
271 * ring_buffer_event_data - return the data of the event
272 * @event: the event to get the data from
273 */
274void *ring_buffer_event_data(struct ring_buffer_event *event)
275{
276 return rb_event_data(event);
277}
Robert Richterc4f50182008-12-11 16:49:22 +0100278EXPORT_SYMBOL_GPL(ring_buffer_event_data);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400279
280#define for_each_buffer_cpu(buffer, cpu) \
Rusty Russell9e01c1b2009-01-01 10:12:22 +1030281 for_each_cpu(cpu, buffer->cpumask)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400282
Nicholas Pigginb23d7a5f2020-06-25 15:34:03 +1000283#define for_each_online_buffer_cpu(buffer, cpu) \
284 for_each_cpu_and(cpu, buffer->cpumask, cpu_online_mask)
285
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400286#define TS_SHIFT 27
287#define TS_MASK ((1ULL << TS_SHIFT) - 1)
288#define TS_DELTA_TEST (~TS_MASK)
289
Tom Zanussidc4e2802018-01-15 20:51:40 -0600290/**
291 * ring_buffer_event_time_stamp - return the event's extended timestamp
292 * @event: the event to get the timestamp of
293 *
294 * Returns the extended timestamp associated with a data event.
295 * An extended time_stamp is a 64-bit timestamp represented
296 * internally in a special way that makes the best use of space
297 * contained within a ring buffer event. This function decodes
298 * it and maps it to a straight u64 value.
299 */
300u64 ring_buffer_event_time_stamp(struct ring_buffer_event *event)
301{
302 u64 ts;
303
304 ts = event->array[0];
305 ts <<= TS_SHIFT;
306 ts += event->time_delta;
307
308 return ts;
309}
310
Steven Rostedt66a8cb92010-03-31 13:21:56 -0400311/* Flag when events were overwritten */
312#define RB_MISSED_EVENTS (1 << 31)
Steven Rostedtff0ff842010-03-31 22:11:42 -0400313/* Missed count stored at end */
314#define RB_MISSED_STORED (1 << 30)
Steven Rostedt66a8cb92010-03-31 13:21:56 -0400315
Steven Rostedtabc9b562008-12-02 15:34:06 -0500316struct buffer_data_page {
Steven Rostedte4c2ce82008-10-01 11:14:54 -0400317 u64 time_stamp; /* page time stamp */
Wenji Huangc3706f02009-02-10 01:03:18 -0500318 local_t commit; /* write committed index */
James Hogan649508f2012-05-30 12:11:19 +0100319 unsigned char data[] RB_ALIGN_DATA; /* data of buffer page */
Steven Rostedtabc9b562008-12-02 15:34:06 -0500320};
321
Steven Rostedt77ae3652009-03-27 11:00:29 -0400322/*
323 * Note, the buffer_page list must be first. The buffer pages
324 * are allocated in cache lines, which means that each buffer
325 * page will be at the beginning of a cache line, and thus
326 * the least significant bits will be zero. We use this to
327 * add flags in the list struct pointers, to make the ring buffer
328 * lockless.
329 */
Steven Rostedtabc9b562008-12-02 15:34:06 -0500330struct buffer_page {
Steven Rostedt778c55d2009-05-01 18:44:45 -0400331 struct list_head list; /* list of buffer pages */
Steven Rostedtabc9b562008-12-02 15:34:06 -0500332 local_t write; /* index for next write */
Steven Rostedt6f807ac2008-10-04 02:00:58 -0400333 unsigned read; /* index for next read */
Steven Rostedt778c55d2009-05-01 18:44:45 -0400334 local_t entries; /* entries on this page */
Steven Rostedtff0ff842010-03-31 22:11:42 -0400335 unsigned long real_end; /* real end of data */
Steven Rostedtabc9b562008-12-02 15:34:06 -0500336 struct buffer_data_page *page; /* Actual data page */
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400337};
338
Steven Rostedt77ae3652009-03-27 11:00:29 -0400339/*
340 * The buffer page counters, write and entries, must be reset
341 * atomically when crossing page boundaries. To synchronize this
342 * update, two counters are inserted into the number. One is
343 * the actual counter for the write position or count on the page.
344 *
345 * The other is a counter of updaters. Before an update happens
346 * the update partition of the counter is incremented. This will
347 * allow the updater to update the counter atomically.
348 *
349 * The counter is 20 bits, and the state data is 12.
350 */
351#define RB_WRITE_MASK 0xfffff
352#define RB_WRITE_INTCNT (1 << 20)
353
Steven Rostedt044fa782008-12-02 23:50:03 -0500354static void rb_init_page(struct buffer_data_page *bpage)
Steven Rostedtabc9b562008-12-02 15:34:06 -0500355{
Steven Rostedt044fa782008-12-02 23:50:03 -0500356 local_set(&bpage->commit, 0);
Steven Rostedtabc9b562008-12-02 15:34:06 -0500357}
358
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400359/*
Steven Rostedted568292008-09-29 23:02:40 -0400360 * Also stolen from mm/slob.c. Thanks to Mathieu Desnoyers for pointing
361 * this issue out.
362 */
Andrew Morton34a148b2009-01-09 12:27:09 -0800363static void free_buffer_page(struct buffer_page *bpage)
Steven Rostedted568292008-09-29 23:02:40 -0400364{
Andrew Morton34a148b2009-01-09 12:27:09 -0800365 free_page((unsigned long)bpage->page);
Steven Rostedte4c2ce82008-10-01 11:14:54 -0400366 kfree(bpage);
Steven Rostedted568292008-09-29 23:02:40 -0400367}
368
369/*
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400370 * We need to fit the time_stamp delta into 27 bits.
371 */
372static inline int test_time_stamp(u64 delta)
373{
374 if (delta & TS_DELTA_TEST)
375 return 1;
376 return 0;
377}
378
Steven Rostedt474d32b2009-03-03 19:51:40 -0500379#define BUF_PAGE_SIZE (PAGE_SIZE - BUF_PAGE_HDR_SIZE)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400380
Steven Rostedtbe957c42009-05-11 14:42:53 -0400381/* Max payload is BUF_PAGE_SIZE - header (8bytes) */
382#define BUF_MAX_DATA_SIZE (BUF_PAGE_SIZE - (sizeof(u32) * 2))
383
Steven Rostedtd1b182a2009-04-15 16:53:47 -0400384int ring_buffer_print_page_header(struct trace_seq *s)
385{
386 struct buffer_data_page field;
Steven Rostedtd1b182a2009-04-15 16:53:47 -0400387
Steven Rostedt (Red Hat)c0cd93a2014-11-12 11:49:00 -0500388 trace_seq_printf(s, "\tfield: u64 timestamp;\t"
389 "offset:0;\tsize:%u;\tsigned:%u;\n",
390 (unsigned int)sizeof(field.time_stamp),
391 (unsigned int)is_signed_type(u64));
Steven Rostedtd1b182a2009-04-15 16:53:47 -0400392
Steven Rostedt (Red Hat)c0cd93a2014-11-12 11:49:00 -0500393 trace_seq_printf(s, "\tfield: local_t commit;\t"
394 "offset:%u;\tsize:%u;\tsigned:%u;\n",
395 (unsigned int)offsetof(typeof(field), commit),
396 (unsigned int)sizeof(field.commit),
397 (unsigned int)is_signed_type(long));
Steven Rostedtd1b182a2009-04-15 16:53:47 -0400398
Steven Rostedt (Red Hat)c0cd93a2014-11-12 11:49:00 -0500399 trace_seq_printf(s, "\tfield: int overwrite;\t"
400 "offset:%u;\tsize:%u;\tsigned:%u;\n",
401 (unsigned int)offsetof(typeof(field), commit),
402 1,
403 (unsigned int)is_signed_type(long));
Steven Rostedt66a8cb92010-03-31 13:21:56 -0400404
Steven Rostedt (Red Hat)c0cd93a2014-11-12 11:49:00 -0500405 trace_seq_printf(s, "\tfield: char data;\t"
406 "offset:%u;\tsize:%u;\tsigned:%u;\n",
407 (unsigned int)offsetof(typeof(field), data),
408 (unsigned int)BUF_PAGE_SIZE,
409 (unsigned int)is_signed_type(char));
Steven Rostedtd1b182a2009-04-15 16:53:47 -0400410
Steven Rostedt (Red Hat)c0cd93a2014-11-12 11:49:00 -0500411 return !trace_seq_has_overflowed(s);
Steven Rostedtd1b182a2009-04-15 16:53:47 -0400412}
413
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -0500414struct rb_irq_work {
415 struct irq_work work;
416 wait_queue_head_t waiters;
Steven Rostedt (Red Hat)1e0d6712015-02-10 22:14:53 -0500417 wait_queue_head_t full_waiters;
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -0500418 bool waiters_pending;
Steven Rostedt (Red Hat)1e0d6712015-02-10 22:14:53 -0500419 bool full_waiters_pending;
420 bool wakeup_full;
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -0500421};
422
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400423/*
Steven Rostedt (Red Hat)fcc742e2015-05-28 17:13:14 -0400424 * Structure to hold event state and handle nested events.
425 */
426struct rb_event_info {
427 u64 ts;
428 u64 delta;
Steven Rostedt (VMware)58fbc3c2020-06-30 08:59:26 -0400429 u64 before;
430 u64 after;
Steven Rostedt (Red Hat)fcc742e2015-05-28 17:13:14 -0400431 unsigned long length;
432 struct buffer_page *tail_page;
433 int add_timestamp;
434};
435
436/*
Steven Rostedt (VMware)a389d862020-06-28 22:52:25 -0400437 * Used for the add_timestamp
438 * NONE
Steven Rostedt (VMware)7c4b4a52020-06-28 22:52:26 -0400439 * EXTEND - wants a time extend
440 * ABSOLUTE - the buffer requests all events to have absolute time stamps
Steven Rostedt (VMware)a389d862020-06-28 22:52:25 -0400441 * FORCE - force a full time stamp.
442 */
443enum {
Steven Rostedt (VMware)7c4b4a52020-06-28 22:52:26 -0400444 RB_ADD_STAMP_NONE = 0,
445 RB_ADD_STAMP_EXTEND = BIT(1),
446 RB_ADD_STAMP_ABSOLUTE = BIT(2),
447 RB_ADD_STAMP_FORCE = BIT(3)
Steven Rostedt (VMware)a389d862020-06-28 22:52:25 -0400448};
449/*
Steven Rostedt (Red Hat)a497adb2015-05-29 10:32:28 -0400450 * Used for which event context the event is in.
Steven Rostedt (VMware)b02414c2020-11-02 15:31:27 -0500451 * TRANSITION = 0
452 * NMI = 1
453 * IRQ = 2
454 * SOFTIRQ = 3
455 * NORMAL = 4
Steven Rostedt (Red Hat)a497adb2015-05-29 10:32:28 -0400456 *
457 * See trace_recursive_lock() comment below for more details.
458 */
459enum {
Steven Rostedt (VMware)b02414c2020-11-02 15:31:27 -0500460 RB_CTX_TRANSITION,
Steven Rostedt (Red Hat)a497adb2015-05-29 10:32:28 -0400461 RB_CTX_NMI,
462 RB_CTX_IRQ,
463 RB_CTX_SOFTIRQ,
464 RB_CTX_NORMAL,
465 RB_CTX_MAX
466};
467
Steven Rostedt (VMware)10464b42020-06-28 22:52:27 -0400468#if BITS_PER_LONG == 32
469#define RB_TIME_32
470#endif
471
472/* To test on 64 bit machines */
473//#define RB_TIME_32
474
475#ifdef RB_TIME_32
476
477struct rb_time_struct {
478 local_t cnt;
479 local_t top;
480 local_t bottom;
481};
482#else
483#include <asm/local64.h>
484struct rb_time_struct {
485 local64_t time;
486};
487#endif
488typedef struct rb_time_struct rb_time_t;
489
Steven Rostedt (Red Hat)a497adb2015-05-29 10:32:28 -0400490/*
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400491 * head_page == tail_page && head == tail then buffer is empty.
492 */
493struct ring_buffer_per_cpu {
494 int cpu;
Richard Kennedy985023d2010-03-25 11:27:36 +0000495 atomic_t record_disabled;
Steven Rostedt (VMware)07b8b102020-03-27 16:21:22 -0400496 atomic_t resize_disabled;
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -0500497 struct trace_buffer *buffer;
Thomas Gleixner5389f6f2009-07-25 17:13:33 +0200498 raw_spinlock_t reader_lock; /* serialize readers */
Thomas Gleixner445c8952009-12-02 19:49:50 +0100499 arch_spinlock_t lock;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400500 struct lock_class_key lock_key;
Steven Rostedt (VMware)73a757e2017-05-01 09:35:09 -0400501 struct buffer_data_page *free_page;
Steven Rostedt (Red Hat)9b94a8f2016-05-12 11:01:24 -0400502 unsigned long nr_pages;
Steven Rostedt (Red Hat)58a09ec2015-05-27 10:27:47 -0400503 unsigned int current_context;
Steven Rostedt3adc54f2009-03-30 15:32:01 -0400504 struct list_head *pages;
Steven Rostedt6f807ac2008-10-04 02:00:58 -0400505 struct buffer_page *head_page; /* read from head */
506 struct buffer_page *tail_page; /* write to tail */
Wenji Huangc3706f02009-02-10 01:03:18 -0500507 struct buffer_page *commit_page; /* committed pages */
Steven Rostedtd7690412008-10-01 00:29:53 -0400508 struct buffer_page *reader_page;
Steven Rostedt66a8cb92010-03-31 13:21:56 -0400509 unsigned long lost_events;
510 unsigned long last_overrun;
Steven Rostedt (VMware)8e012062018-02-07 17:26:32 -0500511 unsigned long nest;
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -0700512 local_t entries_bytes;
Steven Rostedte4906ef2009-04-30 20:49:44 -0400513 local_t entries;
Slava Pestov884bfe82011-07-15 14:23:58 -0700514 local_t overrun;
515 local_t commit_overrun;
516 local_t dropped_events;
Steven Rostedtfa743952009-06-16 12:37:57 -0400517 local_t committing;
518 local_t commits;
Steven Rostedt (VMware)2c2b0a72018-11-29 20:32:26 -0500519 local_t pages_touched;
520 local_t pages_read;
Steven Rostedt (VMware)03329f92018-11-29 21:38:42 -0500521 long last_pages_touch;
Steven Rostedt (VMware)2c2b0a72018-11-29 20:32:26 -0500522 size_t shortest_full;
Steven Rostedt77ae3652009-03-27 11:00:29 -0400523 unsigned long read;
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -0700524 unsigned long read_bytes;
Steven Rostedt (VMware)10464b42020-06-28 22:52:27 -0400525 rb_time_t write_stamp;
526 rb_time_t before_stamp;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400527 u64 read_stamp;
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -0800528 /* ring buffer pages to update, > 0 to add, < 0 to remove */
Steven Rostedt (Red Hat)9b94a8f2016-05-12 11:01:24 -0400529 long nr_pages_to_update;
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -0800530 struct list_head new_pages; /* new pages to add */
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -0700531 struct work_struct update_pages_work;
Vaibhav Nagarnaik05fdd702012-05-18 13:29:51 -0700532 struct completion update_done;
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -0500533
534 struct rb_irq_work irq_work;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400535};
536
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -0500537struct trace_buffer {
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400538 unsigned flags;
539 int cpus;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400540 atomic_t record_disabled;
Arnaldo Carvalho de Melo00f62f62009-02-09 17:04:06 -0200541 cpumask_var_t cpumask;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400542
Peter Zijlstra1f8a6a12009-06-08 18:18:39 +0200543 struct lock_class_key *reader_lock_key;
544
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400545 struct mutex mutex;
546
547 struct ring_buffer_per_cpu **buffers;
Steven Rostedt554f7862009-03-11 22:00:13 -0400548
Sebastian Andrzej Siewiorb32614c2016-11-27 00:13:34 +0100549 struct hlist_node node;
Steven Rostedt37886f62009-03-17 17:22:06 -0400550 u64 (*clock)(void);
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -0500551
552 struct rb_irq_work irq_work;
Tom Zanussi00b41452018-01-15 20:51:39 -0600553 bool time_stamp_abs;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400554};
555
556struct ring_buffer_iter {
557 struct ring_buffer_per_cpu *cpu_buffer;
558 unsigned long head;
Steven Rostedt (VMware)785888c2020-03-17 17:32:27 -0400559 unsigned long next_event;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400560 struct buffer_page *head_page;
Steven Rostedt492a74f2010-01-25 15:17:47 -0500561 struct buffer_page *cache_reader_page;
562 unsigned long cache_read;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400563 u64 read_stamp;
Steven Rostedt (VMware)28e3fc52020-03-17 17:32:26 -0400564 u64 page_stamp;
Steven Rostedt (VMware)785888c2020-03-17 17:32:27 -0400565 struct ring_buffer_event *event;
Steven Rostedt (VMware)c9b7a4a2020-03-17 17:32:32 -0400566 int missed_events;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400567};
568
Steven Rostedt (VMware)10464b42020-06-28 22:52:27 -0400569#ifdef RB_TIME_32
570
571/*
572 * On 32 bit machines, local64_t is very expensive. As the ring
573 * buffer doesn't need all the features of a true 64 bit atomic,
574 * on 32 bit, it uses these functions (64 still uses local64_t).
575 *
576 * For the ring buffer, 64 bit required operations for the time is
577 * the following:
578 *
579 * - Only need 59 bits (uses 60 to make it even).
580 * - Reads may fail if it interrupted a modification of the time stamp.
581 * It will succeed if it did not interrupt another write even if
582 * the read itself is interrupted by a write.
583 * It returns whether it was successful or not.
584 *
585 * - Writes always succeed and will overwrite other writes and writes
586 * that were done by events interrupting the current write.
587 *
588 * - A write followed by a read of the same time stamp will always succeed,
589 * but may not contain the same value.
590 *
591 * - A cmpxchg will fail if it interrupted another write or cmpxchg.
592 * Other than that, it acts like a normal cmpxchg.
593 *
594 * The 60 bit time stamp is broken up by 30 bits in a top and bottom half
595 * (bottom being the least significant 30 bits of the 60 bit time stamp).
596 *
597 * The two most significant bits of each half holds a 2 bit counter (0-3).
598 * Each update will increment this counter by one.
599 * When reading the top and bottom, if the two counter bits match then the
600 * top and bottom together make a valid 60 bit number.
601 */
602#define RB_TIME_SHIFT 30
603#define RB_TIME_VAL_MASK ((1 << RB_TIME_SHIFT) - 1)
604
605static inline int rb_time_cnt(unsigned long val)
606{
607 return (val >> RB_TIME_SHIFT) & 3;
608}
609
610static inline u64 rb_time_val(unsigned long top, unsigned long bottom)
611{
612 u64 val;
613
614 val = top & RB_TIME_VAL_MASK;
615 val <<= RB_TIME_SHIFT;
616 val |= bottom & RB_TIME_VAL_MASK;
617
618 return val;
619}
620
621static inline bool __rb_time_read(rb_time_t *t, u64 *ret, unsigned long *cnt)
622{
623 unsigned long top, bottom;
624 unsigned long c;
625
626 /*
627 * If the read is interrupted by a write, then the cnt will
628 * be different. Loop until both top and bottom have been read
629 * without interruption.
630 */
631 do {
632 c = local_read(&t->cnt);
633 top = local_read(&t->top);
634 bottom = local_read(&t->bottom);
635 } while (c != local_read(&t->cnt));
636
637 *cnt = rb_time_cnt(top);
638
639 /* If top and bottom counts don't match, this interrupted a write */
640 if (*cnt != rb_time_cnt(bottom))
641 return false;
642
643 *ret = rb_time_val(top, bottom);
644 return true;
645}
646
647static bool rb_time_read(rb_time_t *t, u64 *ret)
648{
649 unsigned long cnt;
650
651 return __rb_time_read(t, ret, &cnt);
652}
653
654static inline unsigned long rb_time_val_cnt(unsigned long val, unsigned long cnt)
655{
656 return (val & RB_TIME_VAL_MASK) | ((cnt & 3) << RB_TIME_SHIFT);
657}
658
659static inline void rb_time_split(u64 val, unsigned long *top, unsigned long *bottom)
660{
661 *top = (unsigned long)((val >> RB_TIME_SHIFT) & RB_TIME_VAL_MASK);
662 *bottom = (unsigned long)(val & RB_TIME_VAL_MASK);
663}
664
665static inline void rb_time_val_set(local_t *t, unsigned long val, unsigned long cnt)
666{
667 val = rb_time_val_cnt(val, cnt);
668 local_set(t, val);
669}
670
671static void rb_time_set(rb_time_t *t, u64 val)
672{
673 unsigned long cnt, top, bottom;
674
675 rb_time_split(val, &top, &bottom);
676
677 /* Writes always succeed with a valid number even if it gets interrupted. */
678 do {
679 cnt = local_inc_return(&t->cnt);
680 rb_time_val_set(&t->top, top, cnt);
681 rb_time_val_set(&t->bottom, bottom, cnt);
682 } while (cnt != local_read(&t->cnt));
683}
684
685static inline bool
686rb_time_read_cmpxchg(local_t *l, unsigned long expect, unsigned long set)
687{
688 unsigned long ret;
689
690 ret = local_cmpxchg(l, expect, set);
691 return ret == expect;
692}
693
694static int rb_time_cmpxchg(rb_time_t *t, u64 expect, u64 set)
695{
696 unsigned long cnt, top, bottom;
697 unsigned long cnt2, top2, bottom2;
698 u64 val;
699
700 /* The cmpxchg always fails if it interrupted an update */
701 if (!__rb_time_read(t, &val, &cnt2))
702 return false;
703
704 if (val != expect)
705 return false;
706
707 cnt = local_read(&t->cnt);
708 if ((cnt & 3) != cnt2)
709 return false;
710
711 cnt2 = cnt + 1;
712
713 rb_time_split(val, &top, &bottom);
714 top = rb_time_val_cnt(top, cnt);
715 bottom = rb_time_val_cnt(bottom, cnt);
716
717 rb_time_split(set, &top2, &bottom2);
718 top2 = rb_time_val_cnt(top2, cnt2);
719 bottom2 = rb_time_val_cnt(bottom2, cnt2);
720
721 if (!rb_time_read_cmpxchg(&t->cnt, cnt, cnt2))
722 return false;
723 if (!rb_time_read_cmpxchg(&t->top, top, top2))
724 return false;
725 if (!rb_time_read_cmpxchg(&t->bottom, bottom, bottom2))
726 return false;
727 return true;
728}
729
730#else /* 64 bits */
731
732/* local64_t always succeeds */
733
734static inline bool rb_time_read(rb_time_t *t, u64 *ret)
735{
736 *ret = local64_read(&t->time);
737 return true;
738}
739static void rb_time_set(rb_time_t *t, u64 val)
740{
741 local64_set(&t->time, val);
742}
743
744static bool rb_time_cmpxchg(rb_time_t *t, u64 expect, u64 set)
745{
746 u64 val;
747 val = local64_cmpxchg(&t->time, expect, set);
748 return val == expect;
749}
750#endif
751
Steven Rostedt (VMware)2c2b0a72018-11-29 20:32:26 -0500752/**
753 * ring_buffer_nr_pages - get the number of buffer pages in the ring buffer
754 * @buffer: The ring_buffer to get the number of pages from
755 * @cpu: The cpu of the ring_buffer to get the number of pages from
756 *
757 * Returns the number of pages used by a per_cpu buffer of the ring buffer.
758 */
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -0500759size_t ring_buffer_nr_pages(struct trace_buffer *buffer, int cpu)
Steven Rostedt (VMware)2c2b0a72018-11-29 20:32:26 -0500760{
761 return buffer->buffers[cpu]->nr_pages;
762}
763
764/**
765 * ring_buffer_nr_pages_dirty - get the number of used pages in the ring buffer
766 * @buffer: The ring_buffer to get the number of pages from
767 * @cpu: The cpu of the ring_buffer to get the number of pages from
768 *
769 * Returns the number of pages that have content in the ring buffer.
770 */
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -0500771size_t ring_buffer_nr_dirty_pages(struct trace_buffer *buffer, int cpu)
Steven Rostedt (VMware)2c2b0a72018-11-29 20:32:26 -0500772{
773 size_t read;
774 size_t cnt;
775
776 read = local_read(&buffer->buffers[cpu]->pages_read);
777 cnt = local_read(&buffer->buffers[cpu]->pages_touched);
778 /* The reader can read an empty page, but not more than that */
779 if (cnt < read) {
780 WARN_ON_ONCE(read > cnt + 1);
781 return 0;
782 }
783
784 return cnt - read;
785}
786
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -0500787/*
788 * rb_wake_up_waiters - wake up tasks waiting for ring buffer input
789 *
790 * Schedules a delayed work to wake up any task that is blocked on the
791 * ring buffer waiters queue.
792 */
793static void rb_wake_up_waiters(struct irq_work *work)
794{
795 struct rb_irq_work *rbwork = container_of(work, struct rb_irq_work, work);
796
797 wake_up_all(&rbwork->waiters);
Steven Rostedt (Red Hat)1e0d6712015-02-10 22:14:53 -0500798 if (rbwork->wakeup_full) {
799 rbwork->wakeup_full = false;
800 wake_up_all(&rbwork->full_waiters);
801 }
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -0500802}
803
804/**
805 * ring_buffer_wait - wait for input to the ring buffer
806 * @buffer: buffer to wait on
807 * @cpu: the cpu buffer to wait on
Qiujun Huange1981f72020-10-17 17:52:46 +0800808 * @full: wait until the percentage of pages are available, if @cpu != RING_BUFFER_ALL_CPUS
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -0500809 *
810 * If @cpu == RING_BUFFER_ALL_CPUS then the task will wake up as soon
811 * as data is added to any of the @buffer's cpu buffers. Otherwise
812 * it will wait for data to be added to a specific cpu buffer.
813 */
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -0500814int ring_buffer_wait(struct trace_buffer *buffer, int cpu, int full)
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -0500815{
Kees Cook3f649ab2020-06-03 13:09:38 -0700816 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -0500817 DEFINE_WAIT(wait);
818 struct rb_irq_work *work;
Rabin Vincente30f53a2014-11-10 19:46:34 +0100819 int ret = 0;
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -0500820
821 /*
822 * Depending on what the caller is waiting for, either any
823 * data in any cpu buffer, or a specific buffer, put the
824 * caller on the appropriate wait queue.
825 */
Steven Rostedt (Red Hat)1e0d6712015-02-10 22:14:53 -0500826 if (cpu == RING_BUFFER_ALL_CPUS) {
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -0500827 work = &buffer->irq_work;
Steven Rostedt (Red Hat)1e0d6712015-02-10 22:14:53 -0500828 /* Full only makes sense on per cpu reads */
Steven Rostedt (VMware)2c2b0a72018-11-29 20:32:26 -0500829 full = 0;
Steven Rostedt (Red Hat)1e0d6712015-02-10 22:14:53 -0500830 } else {
Steven Rostedt (Red Hat)8b8b3682014-06-10 09:46:00 -0400831 if (!cpumask_test_cpu(cpu, buffer->cpumask))
832 return -ENODEV;
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -0500833 cpu_buffer = buffer->buffers[cpu];
834 work = &cpu_buffer->irq_work;
835 }
836
837
Rabin Vincente30f53a2014-11-10 19:46:34 +0100838 while (true) {
Steven Rostedt (Red Hat)1e0d6712015-02-10 22:14:53 -0500839 if (full)
840 prepare_to_wait(&work->full_waiters, &wait, TASK_INTERRUPTIBLE);
841 else
842 prepare_to_wait(&work->waiters, &wait, TASK_INTERRUPTIBLE);
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -0500843
Rabin Vincente30f53a2014-11-10 19:46:34 +0100844 /*
845 * The events can happen in critical sections where
846 * checking a work queue can cause deadlocks.
847 * After adding a task to the queue, this flag is set
848 * only to notify events to try to wake up the queue
849 * using irq_work.
850 *
851 * We don't clear it even if the buffer is no longer
852 * empty. The flag only causes the next event to run
853 * irq_work to do the work queue wake up. The worse
854 * that can happen if we race with !trace_empty() is that
855 * an event will cause an irq_work to try to wake up
856 * an empty queue.
857 *
858 * There's no reason to protect this flag either, as
859 * the work queue and irq_work logic will do the necessary
860 * synchronization for the wake ups. The only thing
861 * that is necessary is that the wake up happens after
862 * a task has been queued. It's OK for spurious wake ups.
863 */
Steven Rostedt (Red Hat)1e0d6712015-02-10 22:14:53 -0500864 if (full)
865 work->full_waiters_pending = true;
866 else
867 work->waiters_pending = true;
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -0500868
Rabin Vincente30f53a2014-11-10 19:46:34 +0100869 if (signal_pending(current)) {
870 ret = -EINTR;
871 break;
872 }
873
874 if (cpu == RING_BUFFER_ALL_CPUS && !ring_buffer_empty(buffer))
875 break;
876
877 if (cpu != RING_BUFFER_ALL_CPUS &&
878 !ring_buffer_empty_cpu(buffer, cpu)) {
879 unsigned long flags;
880 bool pagebusy;
Steven Rostedt (VMware)2c2b0a72018-11-29 20:32:26 -0500881 size_t nr_pages;
882 size_t dirty;
Rabin Vincente30f53a2014-11-10 19:46:34 +0100883
884 if (!full)
885 break;
886
887 raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
888 pagebusy = cpu_buffer->reader_page == cpu_buffer->commit_page;
Steven Rostedt (VMware)2c2b0a72018-11-29 20:32:26 -0500889 nr_pages = cpu_buffer->nr_pages;
890 dirty = ring_buffer_nr_dirty_pages(buffer, cpu);
891 if (!cpu_buffer->shortest_full ||
892 cpu_buffer->shortest_full < full)
893 cpu_buffer->shortest_full = full;
Rabin Vincente30f53a2014-11-10 19:46:34 +0100894 raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
Steven Rostedt (VMware)2c2b0a72018-11-29 20:32:26 -0500895 if (!pagebusy &&
896 (!nr_pages || (dirty * 100) > full * nr_pages))
Rabin Vincente30f53a2014-11-10 19:46:34 +0100897 break;
898 }
899
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -0500900 schedule();
Rabin Vincente30f53a2014-11-10 19:46:34 +0100901 }
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -0500902
Steven Rostedt (Red Hat)1e0d6712015-02-10 22:14:53 -0500903 if (full)
904 finish_wait(&work->full_waiters, &wait);
905 else
906 finish_wait(&work->waiters, &wait);
Rabin Vincente30f53a2014-11-10 19:46:34 +0100907
908 return ret;
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -0500909}
910
911/**
912 * ring_buffer_poll_wait - poll on buffer input
913 * @buffer: buffer to wait on
914 * @cpu: the cpu buffer to wait on
915 * @filp: the file descriptor
916 * @poll_table: The poll descriptor
917 *
918 * If @cpu == RING_BUFFER_ALL_CPUS then the task will wake up as soon
919 * as data is added to any of the @buffer's cpu buffers. Otherwise
920 * it will wait for data to be added to a specific cpu buffer.
921 *
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800922 * Returns EPOLLIN | EPOLLRDNORM if data exists in the buffers,
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -0500923 * zero otherwise.
924 */
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -0500925__poll_t ring_buffer_poll_wait(struct trace_buffer *buffer, int cpu,
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -0500926 struct file *filp, poll_table *poll_table)
927{
928 struct ring_buffer_per_cpu *cpu_buffer;
929 struct rb_irq_work *work;
930
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -0500931 if (cpu == RING_BUFFER_ALL_CPUS)
932 work = &buffer->irq_work;
933 else {
Steven Rostedt (Red Hat)6721cb62013-05-23 14:21:36 -0400934 if (!cpumask_test_cpu(cpu, buffer->cpumask))
935 return -EINVAL;
936
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -0500937 cpu_buffer = buffer->buffers[cpu];
938 work = &cpu_buffer->irq_work;
939 }
940
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -0500941 poll_wait(filp, &work->waiters, poll_table);
Josef Bacik4ce97db2014-08-25 13:59:41 -0400942 work->waiters_pending = true;
943 /*
944 * There's a tight race between setting the waiters_pending and
945 * checking if the ring buffer is empty. Once the waiters_pending bit
946 * is set, the next event will wake the task up, but we can get stuck
947 * if there's only a single event in.
948 *
949 * FIXME: Ideally, we need a memory barrier on the writer side as well,
950 * but adding a memory barrier to all events will cause too much of a
951 * performance hit in the fast path. We only need a memory barrier when
952 * the buffer goes from empty to having content. But as this race is
953 * extremely small, and it's not a problem if another event comes in, we
954 * will fix it later.
955 */
956 smp_mb();
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -0500957
958 if ((cpu == RING_BUFFER_ALL_CPUS && !ring_buffer_empty(buffer)) ||
959 (cpu != RING_BUFFER_ALL_CPUS && !ring_buffer_empty_cpu(buffer, cpu)))
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800960 return EPOLLIN | EPOLLRDNORM;
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -0500961 return 0;
962}
963
Steven Rostedtf536aaf2008-11-10 23:07:30 -0500964/* buffer may be either ring_buffer or ring_buffer_per_cpu */
Steven Rostedt077c5402009-09-03 19:53:46 -0400965#define RB_WARN_ON(b, cond) \
966 ({ \
967 int _____ret = unlikely(cond); \
968 if (_____ret) { \
969 if (__same_type(*(b), struct ring_buffer_per_cpu)) { \
970 struct ring_buffer_per_cpu *__b = \
971 (void *)b; \
972 atomic_inc(&__b->buffer->record_disabled); \
973 } else \
974 atomic_inc(&b->record_disabled); \
975 WARN_ON(1); \
976 } \
977 _____ret; \
Steven Rostedt3e89c7bb2008-11-11 15:28:41 -0500978 })
Steven Rostedtf536aaf2008-11-10 23:07:30 -0500979
Steven Rostedt37886f62009-03-17 17:22:06 -0400980/* Up this if you want to test the TIME_EXTENTS and normalization */
981#define DEBUG_SHIFT 0
982
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -0500983static inline u64 rb_time_stamp(struct trace_buffer *buffer)
Steven Rostedt88eb0122009-05-11 16:28:23 -0400984{
Steven Rostedt (VMware)bbeba3e2020-06-30 13:05:29 -0400985 u64 ts;
986
987 /* Skip retpolines :-( */
988 if (IS_ENABLED(CONFIG_RETPOLINE) && likely(buffer->clock == trace_clock_local))
989 ts = trace_clock_local();
990 else
991 ts = buffer->clock();
992
Steven Rostedt88eb0122009-05-11 16:28:23 -0400993 /* shift to debug/test normalization and TIME_EXTENTS */
Steven Rostedt (VMware)bbeba3e2020-06-30 13:05:29 -0400994 return ts << DEBUG_SHIFT;
Steven Rostedt88eb0122009-05-11 16:28:23 -0400995}
996
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -0500997u64 ring_buffer_time_stamp(struct trace_buffer *buffer, int cpu)
Steven Rostedt37886f62009-03-17 17:22:06 -0400998{
999 u64 time;
1000
1001 preempt_disable_notrace();
Jiri Olsa6d3f1e12009-10-23 19:36:19 -04001002 time = rb_time_stamp(buffer);
Peter Zijlstrad6097c92019-04-23 22:03:18 +02001003 preempt_enable_notrace();
Steven Rostedt37886f62009-03-17 17:22:06 -04001004
1005 return time;
1006}
1007EXPORT_SYMBOL_GPL(ring_buffer_time_stamp);
1008
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05001009void ring_buffer_normalize_time_stamp(struct trace_buffer *buffer,
Steven Rostedt37886f62009-03-17 17:22:06 -04001010 int cpu, u64 *ts)
1011{
1012 /* Just stupid testing the normalize function and deltas */
1013 *ts >>= DEBUG_SHIFT;
1014}
1015EXPORT_SYMBOL_GPL(ring_buffer_normalize_time_stamp);
1016
Steven Rostedt77ae3652009-03-27 11:00:29 -04001017/*
1018 * Making the ring buffer lockless makes things tricky.
1019 * Although writes only happen on the CPU that they are on,
1020 * and they only need to worry about interrupts. Reads can
1021 * happen on any CPU.
1022 *
1023 * The reader page is always off the ring buffer, but when the
1024 * reader finishes with a page, it needs to swap its page with
1025 * a new one from the buffer. The reader needs to take from
1026 * the head (writes go to the tail). But if a writer is in overwrite
1027 * mode and wraps, it must push the head page forward.
1028 *
1029 * Here lies the problem.
1030 *
1031 * The reader must be careful to replace only the head page, and
1032 * not another one. As described at the top of the file in the
1033 * ASCII art, the reader sets its old page to point to the next
1034 * page after head. It then sets the page after head to point to
1035 * the old reader page. But if the writer moves the head page
1036 * during this operation, the reader could end up with the tail.
1037 *
1038 * We use cmpxchg to help prevent this race. We also do something
1039 * special with the page before head. We set the LSB to 1.
1040 *
1041 * When the writer must push the page forward, it will clear the
1042 * bit that points to the head page, move the head, and then set
1043 * the bit that points to the new head page.
1044 *
1045 * We also don't want an interrupt coming in and moving the head
1046 * page on another writer. Thus we use the second LSB to catch
1047 * that too. Thus:
1048 *
1049 * head->list->prev->next bit 1 bit 0
1050 * ------- -------
1051 * Normal page 0 0
1052 * Points to head page 0 1
1053 * New head page 1 0
1054 *
1055 * Note we can not trust the prev pointer of the head page, because:
1056 *
1057 * +----+ +-----+ +-----+
1058 * | |------>| T |---X--->| N |
1059 * | |<------| | | |
1060 * +----+ +-----+ +-----+
1061 * ^ ^ |
1062 * | +-----+ | |
1063 * +----------| R |----------+ |
1064 * | |<-----------+
1065 * +-----+
1066 *
1067 * Key: ---X--> HEAD flag set in pointer
1068 * T Tail page
1069 * R Reader page
1070 * N Next page
1071 *
1072 * (see __rb_reserve_next() to see where this happens)
1073 *
1074 * What the above shows is that the reader just swapped out
1075 * the reader page with a page in the buffer, but before it
1076 * could make the new header point back to the new page added
1077 * it was preempted by a writer. The writer moved forward onto
1078 * the new page added by the reader and is about to move forward
1079 * again.
1080 *
1081 * You can see, it is legitimate for the previous pointer of
1082 * the head (or any page) not to point back to itself. But only
Steven Rostedt (VMware)6167c202018-05-16 11:17:06 -04001083 * temporarily.
Steven Rostedt77ae3652009-03-27 11:00:29 -04001084 */
1085
1086#define RB_PAGE_NORMAL 0UL
1087#define RB_PAGE_HEAD 1UL
1088#define RB_PAGE_UPDATE 2UL
1089
1090
1091#define RB_FLAG_MASK 3UL
1092
1093/* PAGE_MOVED is not part of the mask */
1094#define RB_PAGE_MOVED 4UL
1095
1096/*
1097 * rb_list_head - remove any bit
1098 */
1099static struct list_head *rb_list_head(struct list_head *list)
1100{
1101 unsigned long val = (unsigned long)list;
1102
1103 return (struct list_head *)(val & ~RB_FLAG_MASK);
1104}
1105
1106/*
Jiri Olsa6d3f1e12009-10-23 19:36:19 -04001107 * rb_is_head_page - test if the given page is the head page
Steven Rostedt77ae3652009-03-27 11:00:29 -04001108 *
1109 * Because the reader may move the head_page pointer, we can
1110 * not trust what the head page is (it may be pointing to
1111 * the reader page). But if the next page is a header page,
1112 * its flags will be non zero.
1113 */
Jesper Juhl42b16b32011-01-17 00:09:38 +01001114static inline int
Qiujun Huang6689bed2020-12-25 22:03:56 +08001115rb_is_head_page(struct buffer_page *page, struct list_head *list)
Steven Rostedt77ae3652009-03-27 11:00:29 -04001116{
1117 unsigned long val;
1118
1119 val = (unsigned long)list->next;
1120
1121 if ((val & ~RB_FLAG_MASK) != (unsigned long)&page->list)
1122 return RB_PAGE_MOVED;
1123
1124 return val & RB_FLAG_MASK;
1125}
1126
1127/*
1128 * rb_is_reader_page
1129 *
1130 * The unique thing about the reader page, is that, if the
1131 * writer is ever on it, the previous pointer never points
1132 * back to the reader page.
1133 */
Yaowei Bai06ca3202015-09-29 22:43:31 +08001134static bool rb_is_reader_page(struct buffer_page *page)
Steven Rostedt77ae3652009-03-27 11:00:29 -04001135{
1136 struct list_head *list = page->list.prev;
1137
1138 return rb_list_head(list->next) != &page->list;
1139}
1140
1141/*
1142 * rb_set_list_to_head - set a list_head to be pointing to head.
1143 */
Qiujun Huang6689bed2020-12-25 22:03:56 +08001144static void rb_set_list_to_head(struct list_head *list)
Steven Rostedt77ae3652009-03-27 11:00:29 -04001145{
1146 unsigned long *ptr;
1147
1148 ptr = (unsigned long *)&list->next;
1149 *ptr |= RB_PAGE_HEAD;
1150 *ptr &= ~RB_PAGE_UPDATE;
1151}
1152
1153/*
1154 * rb_head_page_activate - sets up head page
1155 */
1156static void rb_head_page_activate(struct ring_buffer_per_cpu *cpu_buffer)
1157{
1158 struct buffer_page *head;
1159
1160 head = cpu_buffer->head_page;
1161 if (!head)
1162 return;
1163
1164 /*
1165 * Set the previous list pointer to have the HEAD flag.
1166 */
Qiujun Huang6689bed2020-12-25 22:03:56 +08001167 rb_set_list_to_head(head->list.prev);
Steven Rostedt77ae3652009-03-27 11:00:29 -04001168}
1169
1170static void rb_list_head_clear(struct list_head *list)
1171{
1172 unsigned long *ptr = (unsigned long *)&list->next;
1173
1174 *ptr &= ~RB_FLAG_MASK;
1175}
1176
1177/*
Steven Rostedt (VMware)6167c202018-05-16 11:17:06 -04001178 * rb_head_page_deactivate - clears head page ptr (for free list)
Steven Rostedt77ae3652009-03-27 11:00:29 -04001179 */
1180static void
1181rb_head_page_deactivate(struct ring_buffer_per_cpu *cpu_buffer)
1182{
1183 struct list_head *hd;
1184
1185 /* Go through the whole list and clear any pointers found. */
1186 rb_list_head_clear(cpu_buffer->pages);
1187
1188 list_for_each(hd, cpu_buffer->pages)
1189 rb_list_head_clear(hd);
1190}
1191
1192static int rb_head_page_set(struct ring_buffer_per_cpu *cpu_buffer,
1193 struct buffer_page *head,
1194 struct buffer_page *prev,
1195 int old_flag, int new_flag)
1196{
1197 struct list_head *list;
1198 unsigned long val = (unsigned long)&head->list;
1199 unsigned long ret;
1200
1201 list = &prev->list;
1202
1203 val &= ~RB_FLAG_MASK;
1204
Steven Rostedt08a40812009-09-14 09:31:35 -04001205 ret = cmpxchg((unsigned long *)&list->next,
1206 val | old_flag, val | new_flag);
Steven Rostedt77ae3652009-03-27 11:00:29 -04001207
1208 /* check if the reader took the page */
1209 if ((ret & ~RB_FLAG_MASK) != val)
1210 return RB_PAGE_MOVED;
1211
1212 return ret & RB_FLAG_MASK;
1213}
1214
1215static int rb_head_page_set_update(struct ring_buffer_per_cpu *cpu_buffer,
1216 struct buffer_page *head,
1217 struct buffer_page *prev,
1218 int old_flag)
1219{
1220 return rb_head_page_set(cpu_buffer, head, prev,
1221 old_flag, RB_PAGE_UPDATE);
1222}
1223
1224static int rb_head_page_set_head(struct ring_buffer_per_cpu *cpu_buffer,
1225 struct buffer_page *head,
1226 struct buffer_page *prev,
1227 int old_flag)
1228{
1229 return rb_head_page_set(cpu_buffer, head, prev,
1230 old_flag, RB_PAGE_HEAD);
1231}
1232
1233static int rb_head_page_set_normal(struct ring_buffer_per_cpu *cpu_buffer,
1234 struct buffer_page *head,
1235 struct buffer_page *prev,
1236 int old_flag)
1237{
1238 return rb_head_page_set(cpu_buffer, head, prev,
1239 old_flag, RB_PAGE_NORMAL);
1240}
1241
Qiujun Huang6689bed2020-12-25 22:03:56 +08001242static inline void rb_inc_page(struct buffer_page **bpage)
Steven Rostedt77ae3652009-03-27 11:00:29 -04001243{
1244 struct list_head *p = rb_list_head((*bpage)->list.next);
1245
1246 *bpage = list_entry(p, struct buffer_page, list);
1247}
1248
1249static struct buffer_page *
1250rb_set_head_page(struct ring_buffer_per_cpu *cpu_buffer)
1251{
1252 struct buffer_page *head;
1253 struct buffer_page *page;
1254 struct list_head *list;
1255 int i;
1256
1257 if (RB_WARN_ON(cpu_buffer, !cpu_buffer->head_page))
1258 return NULL;
1259
1260 /* sanity check */
1261 list = cpu_buffer->pages;
1262 if (RB_WARN_ON(cpu_buffer, rb_list_head(list->prev->next) != list))
1263 return NULL;
1264
1265 page = head = cpu_buffer->head_page;
1266 /*
1267 * It is possible that the writer moves the header behind
1268 * where we started, and we miss in one loop.
1269 * A second loop should grab the header, but we'll do
1270 * three loops just because I'm paranoid.
1271 */
1272 for (i = 0; i < 3; i++) {
1273 do {
Qiujun Huang6689bed2020-12-25 22:03:56 +08001274 if (rb_is_head_page(page, page->list.prev)) {
Steven Rostedt77ae3652009-03-27 11:00:29 -04001275 cpu_buffer->head_page = page;
1276 return page;
1277 }
Qiujun Huang6689bed2020-12-25 22:03:56 +08001278 rb_inc_page(&page);
Steven Rostedt77ae3652009-03-27 11:00:29 -04001279 } while (page != head);
1280 }
1281
1282 RB_WARN_ON(cpu_buffer, 1);
1283
1284 return NULL;
1285}
1286
1287static int rb_head_page_replace(struct buffer_page *old,
1288 struct buffer_page *new)
1289{
1290 unsigned long *ptr = (unsigned long *)&old->list.prev->next;
1291 unsigned long val;
1292 unsigned long ret;
1293
1294 val = *ptr & ~RB_FLAG_MASK;
1295 val |= RB_PAGE_HEAD;
1296
Steven Rostedt08a40812009-09-14 09:31:35 -04001297 ret = cmpxchg(ptr, val, (unsigned long)&new->list);
Steven Rostedt77ae3652009-03-27 11:00:29 -04001298
1299 return ret == val;
1300}
1301
1302/*
1303 * rb_tail_page_update - move the tail page forward
Steven Rostedt77ae3652009-03-27 11:00:29 -04001304 */
Steven Rostedt (Red Hat)70004982015-11-17 15:15:19 -05001305static void rb_tail_page_update(struct ring_buffer_per_cpu *cpu_buffer,
Steven Rostedt77ae3652009-03-27 11:00:29 -04001306 struct buffer_page *tail_page,
1307 struct buffer_page *next_page)
1308{
Steven Rostedt77ae3652009-03-27 11:00:29 -04001309 unsigned long old_entries;
1310 unsigned long old_write;
Steven Rostedt77ae3652009-03-27 11:00:29 -04001311
1312 /*
1313 * The tail page now needs to be moved forward.
1314 *
1315 * We need to reset the tail page, but without messing
1316 * with possible erasing of data brought in by interrupts
1317 * that have moved the tail page and are currently on it.
1318 *
1319 * We add a counter to the write field to denote this.
1320 */
1321 old_write = local_add_return(RB_WRITE_INTCNT, &next_page->write);
1322 old_entries = local_add_return(RB_WRITE_INTCNT, &next_page->entries);
1323
Steven Rostedt (VMware)2c2b0a72018-11-29 20:32:26 -05001324 local_inc(&cpu_buffer->pages_touched);
Steven Rostedt77ae3652009-03-27 11:00:29 -04001325 /*
1326 * Just make sure we have seen our old_write and synchronize
1327 * with any interrupts that come in.
1328 */
1329 barrier();
1330
1331 /*
1332 * If the tail page is still the same as what we think
1333 * it is, then it is up to us to update the tail
1334 * pointer.
1335 */
Steven Rostedt (Red Hat)85736362015-11-17 14:03:11 -05001336 if (tail_page == READ_ONCE(cpu_buffer->tail_page)) {
Steven Rostedt77ae3652009-03-27 11:00:29 -04001337 /* Zero the write counter */
1338 unsigned long val = old_write & ~RB_WRITE_MASK;
1339 unsigned long eval = old_entries & ~RB_WRITE_MASK;
1340
1341 /*
1342 * This will only succeed if an interrupt did
1343 * not come in and change it. In which case, we
1344 * do not want to modify it.
Lai Jiangshanda706d82009-07-15 16:27:30 +08001345 *
1346 * We add (void) to let the compiler know that we do not care
1347 * about the return value of these functions. We use the
1348 * cmpxchg to only update if an interrupt did not already
1349 * do it for us. If the cmpxchg fails, we don't care.
Steven Rostedt77ae3652009-03-27 11:00:29 -04001350 */
Lai Jiangshanda706d82009-07-15 16:27:30 +08001351 (void)local_cmpxchg(&next_page->write, old_write, val);
1352 (void)local_cmpxchg(&next_page->entries, old_entries, eval);
Steven Rostedt77ae3652009-03-27 11:00:29 -04001353
1354 /*
1355 * No need to worry about races with clearing out the commit.
1356 * it only can increment when a commit takes place. But that
1357 * only happens in the outer most nested commit.
1358 */
1359 local_set(&next_page->page->commit, 0);
1360
Steven Rostedt (Red Hat)70004982015-11-17 15:15:19 -05001361 /* Again, either we update tail_page or an interrupt does */
1362 (void)cmpxchg(&cpu_buffer->tail_page, tail_page, next_page);
Steven Rostedt77ae3652009-03-27 11:00:29 -04001363 }
Steven Rostedt77ae3652009-03-27 11:00:29 -04001364}
1365
1366static int rb_check_bpage(struct ring_buffer_per_cpu *cpu_buffer,
1367 struct buffer_page *bpage)
1368{
1369 unsigned long val = (unsigned long)bpage;
1370
1371 if (RB_WARN_ON(cpu_buffer, val & RB_FLAG_MASK))
1372 return 1;
1373
1374 return 0;
1375}
1376
1377/**
1378 * rb_check_list - make sure a pointer to a list has the last bits zero
1379 */
1380static int rb_check_list(struct ring_buffer_per_cpu *cpu_buffer,
1381 struct list_head *list)
1382{
1383 if (RB_WARN_ON(cpu_buffer, rb_list_head(list->prev) != list->prev))
1384 return 1;
1385 if (RB_WARN_ON(cpu_buffer, rb_list_head(list->next) != list->next))
1386 return 1;
1387 return 0;
1388}
1389
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001390/**
zhangwei(Jovi)d6118512013-07-15 16:32:50 +08001391 * rb_check_pages - integrity check of buffer pages
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001392 * @cpu_buffer: CPU buffer with pages to test
1393 *
Wenji Huangc3706f02009-02-10 01:03:18 -05001394 * As a safety measure we check to make sure the data pages have not
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001395 * been corrupted.
1396 */
1397static int rb_check_pages(struct ring_buffer_per_cpu *cpu_buffer)
1398{
Steven Rostedt3adc54f2009-03-30 15:32:01 -04001399 struct list_head *head = cpu_buffer->pages;
Steven Rostedt044fa782008-12-02 23:50:03 -05001400 struct buffer_page *bpage, *tmp;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001401
Steven Rostedt308f7ee2012-05-16 19:46:32 -04001402 /* Reset the head page if it exists */
1403 if (cpu_buffer->head_page)
1404 rb_set_head_page(cpu_buffer);
1405
Steven Rostedt77ae3652009-03-27 11:00:29 -04001406 rb_head_page_deactivate(cpu_buffer);
1407
Steven Rostedt3e89c7bb2008-11-11 15:28:41 -05001408 if (RB_WARN_ON(cpu_buffer, head->next->prev != head))
1409 return -1;
1410 if (RB_WARN_ON(cpu_buffer, head->prev->next != head))
1411 return -1;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001412
Steven Rostedt77ae3652009-03-27 11:00:29 -04001413 if (rb_check_list(cpu_buffer, head))
1414 return -1;
1415
Steven Rostedt044fa782008-12-02 23:50:03 -05001416 list_for_each_entry_safe(bpage, tmp, head, list) {
Steven Rostedt3e89c7bb2008-11-11 15:28:41 -05001417 if (RB_WARN_ON(cpu_buffer,
Steven Rostedt044fa782008-12-02 23:50:03 -05001418 bpage->list.next->prev != &bpage->list))
Steven Rostedt3e89c7bb2008-11-11 15:28:41 -05001419 return -1;
1420 if (RB_WARN_ON(cpu_buffer,
Steven Rostedt044fa782008-12-02 23:50:03 -05001421 bpage->list.prev->next != &bpage->list))
Steven Rostedt3e89c7bb2008-11-11 15:28:41 -05001422 return -1;
Steven Rostedt77ae3652009-03-27 11:00:29 -04001423 if (rb_check_list(cpu_buffer, &bpage->list))
1424 return -1;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001425 }
1426
Steven Rostedt77ae3652009-03-27 11:00:29 -04001427 rb_head_page_activate(cpu_buffer);
1428
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001429 return 0;
1430}
1431
Qiujun Huang74e2afc2020-10-15 19:38:42 +08001432static int __rb_allocate_pages(struct ring_buffer_per_cpu *cpu_buffer,
1433 long nr_pages, struct list_head *pages)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001434{
Steven Rostedt044fa782008-12-02 23:50:03 -05001435 struct buffer_page *bpage, *tmp;
Steven Rostedt (VMware)927e56d2018-04-04 11:29:57 -04001436 bool user_thread = current->mm != NULL;
1437 gfp_t mflags;
Steven Rostedt (Red Hat)9b94a8f2016-05-12 11:01:24 -04001438 long i;
Steven Rostedt3adc54f2009-03-30 15:32:01 -04001439
Steven Rostedt (VMware)927e56d2018-04-04 11:29:57 -04001440 /*
1441 * Check if the available memory is there first.
1442 * Note, si_mem_available() only gives us a rough estimate of available
1443 * memory. It may not be accurate. But we don't care, we just want
1444 * to prevent doing any allocation when it is obvious that it is
1445 * not going to succeed.
1446 */
Steven Rostedt (VMware)2a872fa2018-04-02 10:33:56 -04001447 i = si_mem_available();
1448 if (i < nr_pages)
1449 return -ENOMEM;
1450
Steven Rostedt (VMware)927e56d2018-04-04 11:29:57 -04001451 /*
1452 * __GFP_RETRY_MAYFAIL flag makes sure that the allocation fails
1453 * gracefully without invoking oom-killer and the system is not
1454 * destabilized.
1455 */
1456 mflags = GFP_KERNEL | __GFP_RETRY_MAYFAIL;
1457
1458 /*
1459 * If a user thread allocates too much, and si_mem_available()
1460 * reports there's enough memory, even though there is not.
1461 * Make sure the OOM killer kills this thread. This can happen
1462 * even with RETRY_MAYFAIL because another task may be doing
1463 * an allocation after this task has taken all memory.
1464 * This is the task the OOM killer needs to take out during this
1465 * loop, even if it was triggered by an allocation somewhere else.
1466 */
1467 if (user_thread)
1468 set_current_oom_origin();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001469 for (i = 0; i < nr_pages; i++) {
Vaibhav Nagarnaik7ea59062011-05-03 17:56:42 -07001470 struct page *page;
Steven Rostedt (VMware)927e56d2018-04-04 11:29:57 -04001471
Steven Rostedt044fa782008-12-02 23:50:03 -05001472 bpage = kzalloc_node(ALIGN(sizeof(*bpage), cache_line_size()),
Qiujun Huang74e2afc2020-10-15 19:38:42 +08001473 mflags, cpu_to_node(cpu_buffer->cpu));
Steven Rostedt044fa782008-12-02 23:50:03 -05001474 if (!bpage)
Steven Rostedte4c2ce82008-10-01 11:14:54 -04001475 goto free_pages;
Steven Rostedt77ae3652009-03-27 11:00:29 -04001476
Qiujun Huang74e2afc2020-10-15 19:38:42 +08001477 rb_check_bpage(cpu_buffer, bpage);
1478
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001479 list_add(&bpage->list, pages);
Steven Rostedt77ae3652009-03-27 11:00:29 -04001480
Qiujun Huang74e2afc2020-10-15 19:38:42 +08001481 page = alloc_pages_node(cpu_to_node(cpu_buffer->cpu), mflags, 0);
Vaibhav Nagarnaik7ea59062011-05-03 17:56:42 -07001482 if (!page)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001483 goto free_pages;
Vaibhav Nagarnaik7ea59062011-05-03 17:56:42 -07001484 bpage->page = page_address(page);
Steven Rostedt044fa782008-12-02 23:50:03 -05001485 rb_init_page(bpage->page);
Steven Rostedt (VMware)927e56d2018-04-04 11:29:57 -04001486
1487 if (user_thread && fatal_signal_pending(current))
1488 goto free_pages;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001489 }
Steven Rostedt (VMware)927e56d2018-04-04 11:29:57 -04001490 if (user_thread)
1491 clear_current_oom_origin();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001492
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001493 return 0;
1494
1495free_pages:
1496 list_for_each_entry_safe(bpage, tmp, pages, list) {
1497 list_del_init(&bpage->list);
1498 free_buffer_page(bpage);
1499 }
Steven Rostedt (VMware)927e56d2018-04-04 11:29:57 -04001500 if (user_thread)
1501 clear_current_oom_origin();
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001502
1503 return -ENOMEM;
1504}
1505
1506static int rb_allocate_pages(struct ring_buffer_per_cpu *cpu_buffer,
Steven Rostedt (Red Hat)9b94a8f2016-05-12 11:01:24 -04001507 unsigned long nr_pages)
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001508{
1509 LIST_HEAD(pages);
1510
1511 WARN_ON(!nr_pages);
1512
Qiujun Huang74e2afc2020-10-15 19:38:42 +08001513 if (__rb_allocate_pages(cpu_buffer, nr_pages, &pages))
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001514 return -ENOMEM;
1515
Steven Rostedt3adc54f2009-03-30 15:32:01 -04001516 /*
1517 * The ring buffer page list is a circular list that does not
1518 * start and end with a list head. All page list items point to
1519 * other pages.
1520 */
1521 cpu_buffer->pages = pages.next;
1522 list_del(&pages);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001523
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001524 cpu_buffer->nr_pages = nr_pages;
1525
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001526 rb_check_pages(cpu_buffer);
1527
1528 return 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001529}
1530
1531static struct ring_buffer_per_cpu *
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05001532rb_allocate_cpu_buffer(struct trace_buffer *buffer, long nr_pages, int cpu)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001533{
1534 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedt044fa782008-12-02 23:50:03 -05001535 struct buffer_page *bpage;
Vaibhav Nagarnaik7ea59062011-05-03 17:56:42 -07001536 struct page *page;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001537 int ret;
1538
1539 cpu_buffer = kzalloc_node(ALIGN(sizeof(*cpu_buffer), cache_line_size()),
1540 GFP_KERNEL, cpu_to_node(cpu));
1541 if (!cpu_buffer)
1542 return NULL;
1543
1544 cpu_buffer->cpu = cpu;
1545 cpu_buffer->buffer = buffer;
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02001546 raw_spin_lock_init(&cpu_buffer->reader_lock);
Peter Zijlstra1f8a6a12009-06-08 18:18:39 +02001547 lockdep_set_class(&cpu_buffer->reader_lock, buffer->reader_lock_key);
Thomas Gleixneredc35bd2009-12-03 12:38:57 +01001548 cpu_buffer->lock = (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001549 INIT_WORK(&cpu_buffer->update_pages_work, update_pages_handler);
Vaibhav Nagarnaik05fdd702012-05-18 13:29:51 -07001550 init_completion(&cpu_buffer->update_done);
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -05001551 init_irq_work(&cpu_buffer->irq_work.work, rb_wake_up_waiters);
Steven Rostedt (Red Hat)f1dc6722013-03-04 17:33:05 -05001552 init_waitqueue_head(&cpu_buffer->irq_work.waiters);
Steven Rostedt (Red Hat)1e0d6712015-02-10 22:14:53 -05001553 init_waitqueue_head(&cpu_buffer->irq_work.full_waiters);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001554
Steven Rostedt044fa782008-12-02 23:50:03 -05001555 bpage = kzalloc_node(ALIGN(sizeof(*bpage), cache_line_size()),
Steven Rostedte4c2ce82008-10-01 11:14:54 -04001556 GFP_KERNEL, cpu_to_node(cpu));
Steven Rostedt044fa782008-12-02 23:50:03 -05001557 if (!bpage)
Steven Rostedte4c2ce82008-10-01 11:14:54 -04001558 goto fail_free_buffer;
1559
Steven Rostedt77ae3652009-03-27 11:00:29 -04001560 rb_check_bpage(cpu_buffer, bpage);
1561
Steven Rostedt044fa782008-12-02 23:50:03 -05001562 cpu_buffer->reader_page = bpage;
Vaibhav Nagarnaik7ea59062011-05-03 17:56:42 -07001563 page = alloc_pages_node(cpu_to_node(cpu), GFP_KERNEL, 0);
1564 if (!page)
Steven Rostedte4c2ce82008-10-01 11:14:54 -04001565 goto fail_free_reader;
Vaibhav Nagarnaik7ea59062011-05-03 17:56:42 -07001566 bpage->page = page_address(page);
Steven Rostedt044fa782008-12-02 23:50:03 -05001567 rb_init_page(bpage->page);
Steven Rostedte4c2ce82008-10-01 11:14:54 -04001568
Steven Rostedtd7690412008-10-01 00:29:53 -04001569 INIT_LIST_HEAD(&cpu_buffer->reader_page->list);
Vaibhav Nagarnaik44b99462012-06-22 11:50:05 -07001570 INIT_LIST_HEAD(&cpu_buffer->new_pages);
Steven Rostedtd7690412008-10-01 00:29:53 -04001571
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001572 ret = rb_allocate_pages(cpu_buffer, nr_pages);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001573 if (ret < 0)
Steven Rostedtd7690412008-10-01 00:29:53 -04001574 goto fail_free_reader;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001575
1576 cpu_buffer->head_page
Steven Rostedt3adc54f2009-03-30 15:32:01 -04001577 = list_entry(cpu_buffer->pages, struct buffer_page, list);
Steven Rostedtbf41a152008-10-04 02:00:59 -04001578 cpu_buffer->tail_page = cpu_buffer->commit_page = cpu_buffer->head_page;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001579
Steven Rostedt77ae3652009-03-27 11:00:29 -04001580 rb_head_page_activate(cpu_buffer);
1581
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001582 return cpu_buffer;
1583
Steven Rostedtd7690412008-10-01 00:29:53 -04001584 fail_free_reader:
1585 free_buffer_page(cpu_buffer->reader_page);
1586
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001587 fail_free_buffer:
1588 kfree(cpu_buffer);
1589 return NULL;
1590}
1591
1592static void rb_free_cpu_buffer(struct ring_buffer_per_cpu *cpu_buffer)
1593{
Steven Rostedt3adc54f2009-03-30 15:32:01 -04001594 struct list_head *head = cpu_buffer->pages;
Steven Rostedt044fa782008-12-02 23:50:03 -05001595 struct buffer_page *bpage, *tmp;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001596
Steven Rostedtd7690412008-10-01 00:29:53 -04001597 free_buffer_page(cpu_buffer->reader_page);
1598
Steven Rostedt77ae3652009-03-27 11:00:29 -04001599 rb_head_page_deactivate(cpu_buffer);
1600
Steven Rostedt3adc54f2009-03-30 15:32:01 -04001601 if (head) {
1602 list_for_each_entry_safe(bpage, tmp, head, list) {
1603 list_del_init(&bpage->list);
1604 free_buffer_page(bpage);
1605 }
1606 bpage = list_entry(head, struct buffer_page, list);
Steven Rostedt044fa782008-12-02 23:50:03 -05001607 free_buffer_page(bpage);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001608 }
Steven Rostedt3adc54f2009-03-30 15:32:01 -04001609
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001610 kfree(cpu_buffer);
1611}
1612
1613/**
zhangwei(Jovi)d6118512013-07-15 16:32:50 +08001614 * __ring_buffer_alloc - allocate a new ring_buffer
Robert Richter68814b52008-11-24 12:24:12 +01001615 * @size: the size in bytes per cpu that is needed.
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001616 * @flags: attributes to set for the ring buffer.
Fabian Frederick59e7cff2014-06-05 20:22:05 +02001617 * @key: ring buffer reader_lock_key.
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001618 *
1619 * Currently the only flag that is available is the RB_FL_OVERWRITE
1620 * flag. This flag means that the buffer will overwrite old data
1621 * when the buffer wraps. If this flag is not set, the buffer will
1622 * drop data when the tail hits the head.
1623 */
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05001624struct trace_buffer *__ring_buffer_alloc(unsigned long size, unsigned flags,
Peter Zijlstra1f8a6a12009-06-08 18:18:39 +02001625 struct lock_class_key *key)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001626{
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05001627 struct trace_buffer *buffer;
Steven Rostedt (Red Hat)9b94a8f2016-05-12 11:01:24 -04001628 long nr_pages;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001629 int bsize;
Steven Rostedt (Red Hat)9b94a8f2016-05-12 11:01:24 -04001630 int cpu;
Sebastian Andrzej Siewiorb32614c2016-11-27 00:13:34 +01001631 int ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001632
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001633 /* keep it in its own cache line */
1634 buffer = kzalloc(ALIGN(sizeof(*buffer), cache_line_size()),
1635 GFP_KERNEL);
1636 if (!buffer)
1637 return NULL;
1638
Sebastian Andrzej Siewiorb18cc3d2016-12-07 14:31:33 +01001639 if (!zalloc_cpumask_var(&buffer->cpumask, GFP_KERNEL))
Rusty Russell9e01c1b2009-01-01 10:12:22 +10301640 goto fail_free_buffer;
1641
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001642 nr_pages = DIV_ROUND_UP(size, BUF_PAGE_SIZE);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001643 buffer->flags = flags;
Steven Rostedt37886f62009-03-17 17:22:06 -04001644 buffer->clock = trace_clock_local;
Peter Zijlstra1f8a6a12009-06-08 18:18:39 +02001645 buffer->reader_lock_key = key;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001646
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -05001647 init_irq_work(&buffer->irq_work.work, rb_wake_up_waiters);
Steven Rostedt (Red Hat)f1dc6722013-03-04 17:33:05 -05001648 init_waitqueue_head(&buffer->irq_work.waiters);
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -05001649
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001650 /* need at least two pages */
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001651 if (nr_pages < 2)
1652 nr_pages = 2;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001653
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001654 buffer->cpus = nr_cpu_ids;
1655
1656 bsize = sizeof(void *) * nr_cpu_ids;
1657 buffer->buffers = kzalloc(ALIGN(bsize, cache_line_size()),
1658 GFP_KERNEL);
1659 if (!buffer->buffers)
Rusty Russell9e01c1b2009-01-01 10:12:22 +10301660 goto fail_free_cpumask;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001661
Sebastian Andrzej Siewiorb32614c2016-11-27 00:13:34 +01001662 cpu = raw_smp_processor_id();
1663 cpumask_set_cpu(cpu, buffer->cpumask);
1664 buffer->buffers[cpu] = rb_allocate_cpu_buffer(buffer, nr_pages, cpu);
1665 if (!buffer->buffers[cpu])
1666 goto fail_free_buffers;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001667
Sebastian Andrzej Siewiorb32614c2016-11-27 00:13:34 +01001668 ret = cpuhp_state_add_instance(CPUHP_TRACE_RB_PREPARE, &buffer->node);
1669 if (ret < 0)
1670 goto fail_free_buffers;
Steven Rostedt554f7862009-03-11 22:00:13 -04001671
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001672 mutex_init(&buffer->mutex);
1673
1674 return buffer;
1675
1676 fail_free_buffers:
1677 for_each_buffer_cpu(buffer, cpu) {
1678 if (buffer->buffers[cpu])
1679 rb_free_cpu_buffer(buffer->buffers[cpu]);
1680 }
1681 kfree(buffer->buffers);
1682
Rusty Russell9e01c1b2009-01-01 10:12:22 +10301683 fail_free_cpumask:
1684 free_cpumask_var(buffer->cpumask);
1685
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001686 fail_free_buffer:
1687 kfree(buffer);
1688 return NULL;
1689}
Peter Zijlstra1f8a6a12009-06-08 18:18:39 +02001690EXPORT_SYMBOL_GPL(__ring_buffer_alloc);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001691
1692/**
1693 * ring_buffer_free - free a ring buffer.
1694 * @buffer: the buffer to free.
1695 */
1696void
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05001697ring_buffer_free(struct trace_buffer *buffer)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001698{
1699 int cpu;
1700
Sebastian Andrzej Siewiorb32614c2016-11-27 00:13:34 +01001701 cpuhp_state_remove_instance(CPUHP_TRACE_RB_PREPARE, &buffer->node);
Steven Rostedt554f7862009-03-11 22:00:13 -04001702
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001703 for_each_buffer_cpu(buffer, cpu)
1704 rb_free_cpu_buffer(buffer->buffers[cpu]);
1705
Eric Dumazetbd3f0222009-08-07 12:49:29 +02001706 kfree(buffer->buffers);
Rusty Russell9e01c1b2009-01-01 10:12:22 +10301707 free_cpumask_var(buffer->cpumask);
1708
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001709 kfree(buffer);
1710}
Robert Richterc4f50182008-12-11 16:49:22 +01001711EXPORT_SYMBOL_GPL(ring_buffer_free);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001712
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05001713void ring_buffer_set_clock(struct trace_buffer *buffer,
Steven Rostedt37886f62009-03-17 17:22:06 -04001714 u64 (*clock)(void))
1715{
1716 buffer->clock = clock;
1717}
1718
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05001719void ring_buffer_set_time_stamp_abs(struct trace_buffer *buffer, bool abs)
Tom Zanussi00b41452018-01-15 20:51:39 -06001720{
1721 buffer->time_stamp_abs = abs;
1722}
1723
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05001724bool ring_buffer_time_stamp_abs(struct trace_buffer *buffer)
Tom Zanussi00b41452018-01-15 20:51:39 -06001725{
1726 return buffer->time_stamp_abs;
1727}
1728
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001729static void rb_reset_cpu(struct ring_buffer_per_cpu *cpu_buffer);
1730
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001731static inline unsigned long rb_page_entries(struct buffer_page *bpage)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001732{
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001733 return local_read(&bpage->entries) & RB_WRITE_MASK;
1734}
1735
1736static inline unsigned long rb_page_write(struct buffer_page *bpage)
1737{
1738 return local_read(&bpage->write) & RB_WRITE_MASK;
1739}
1740
Vaibhav Nagarnaik5040b4b2012-05-03 18:59:51 -07001741static int
Steven Rostedt (Red Hat)9b94a8f2016-05-12 11:01:24 -04001742rb_remove_pages(struct ring_buffer_per_cpu *cpu_buffer, unsigned long nr_pages)
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001743{
1744 struct list_head *tail_page, *to_remove, *next_page;
1745 struct buffer_page *to_remove_page, *tmp_iter_page;
1746 struct buffer_page *last_page, *first_page;
Steven Rostedt (Red Hat)9b94a8f2016-05-12 11:01:24 -04001747 unsigned long nr_removed;
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001748 unsigned long head_bit;
1749 int page_entries;
1750
1751 head_bit = 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001752
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02001753 raw_spin_lock_irq(&cpu_buffer->reader_lock);
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001754 atomic_inc(&cpu_buffer->record_disabled);
1755 /*
1756 * We don't race with the readers since we have acquired the reader
1757 * lock. We also don't race with writers after disabling recording.
1758 * This makes it easy to figure out the first and the last page to be
1759 * removed from the list. We unlink all the pages in between including
1760 * the first and last pages. This is done in a busy loop so that we
1761 * lose the least number of traces.
1762 * The pages are freed after we restart recording and unlock readers.
1763 */
1764 tail_page = &cpu_buffer->tail_page->list;
Steven Rostedt77ae3652009-03-27 11:00:29 -04001765
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001766 /*
1767 * tail page might be on reader page, we remove the next page
1768 * from the ring buffer
1769 */
1770 if (cpu_buffer->tail_page == cpu_buffer->reader_page)
1771 tail_page = rb_list_head(tail_page->next);
1772 to_remove = tail_page;
1773
1774 /* start of pages to remove */
1775 first_page = list_entry(rb_list_head(to_remove->next),
1776 struct buffer_page, list);
1777
1778 for (nr_removed = 0; nr_removed < nr_pages; nr_removed++) {
1779 to_remove = rb_list_head(to_remove)->next;
1780 head_bit |= (unsigned long)to_remove & RB_PAGE_HEAD;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001781 }
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001782
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001783 next_page = rb_list_head(to_remove)->next;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001784
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001785 /*
1786 * Now we remove all pages between tail_page and next_page.
1787 * Make sure that we have head_bit value preserved for the
1788 * next page
1789 */
1790 tail_page->next = (struct list_head *)((unsigned long)next_page |
1791 head_bit);
1792 next_page = rb_list_head(next_page);
1793 next_page->prev = tail_page;
1794
1795 /* make sure pages points to a valid page in the ring buffer */
1796 cpu_buffer->pages = next_page;
1797
1798 /* update head page */
1799 if (head_bit)
1800 cpu_buffer->head_page = list_entry(next_page,
1801 struct buffer_page, list);
1802
1803 /*
1804 * change read pointer to make sure any read iterators reset
1805 * themselves
1806 */
1807 cpu_buffer->read = 0;
1808
1809 /* pages are removed, resume tracing and then free the pages */
1810 atomic_dec(&cpu_buffer->record_disabled);
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02001811 raw_spin_unlock_irq(&cpu_buffer->reader_lock);
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001812
1813 RB_WARN_ON(cpu_buffer, list_empty(cpu_buffer->pages));
1814
1815 /* last buffer page to remove */
1816 last_page = list_entry(rb_list_head(to_remove), struct buffer_page,
1817 list);
1818 tmp_iter_page = first_page;
1819
1820 do {
Vaibhav Nagarnaik83f36552018-09-07 15:31:29 -07001821 cond_resched();
1822
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001823 to_remove_page = tmp_iter_page;
Qiujun Huang6689bed2020-12-25 22:03:56 +08001824 rb_inc_page(&tmp_iter_page);
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001825
1826 /* update the counters */
1827 page_entries = rb_page_entries(to_remove_page);
1828 if (page_entries) {
1829 /*
1830 * If something was added to this page, it was full
1831 * since it is not the tail page. So we deduct the
1832 * bytes consumed in ring buffer from here.
Vaibhav Nagarnaik48fdc722012-06-29 12:31:41 -07001833 * Increment overrun to account for the lost events.
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001834 */
Vaibhav Nagarnaik48fdc722012-06-29 12:31:41 -07001835 local_add(page_entries, &cpu_buffer->overrun);
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001836 local_sub(BUF_PAGE_SIZE, &cpu_buffer->entries_bytes);
1837 }
1838
1839 /*
1840 * We have already removed references to this list item, just
1841 * free up the buffer_page and its page
1842 */
1843 free_buffer_page(to_remove_page);
1844 nr_removed--;
1845
1846 } while (to_remove_page != last_page);
1847
1848 RB_WARN_ON(cpu_buffer, nr_removed);
Vaibhav Nagarnaik5040b4b2012-05-03 18:59:51 -07001849
1850 return nr_removed == 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001851}
1852
Vaibhav Nagarnaik5040b4b2012-05-03 18:59:51 -07001853static int
1854rb_insert_pages(struct ring_buffer_per_cpu *cpu_buffer)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001855{
Vaibhav Nagarnaik5040b4b2012-05-03 18:59:51 -07001856 struct list_head *pages = &cpu_buffer->new_pages;
1857 int retries, success;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001858
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02001859 raw_spin_lock_irq(&cpu_buffer->reader_lock);
Vaibhav Nagarnaik5040b4b2012-05-03 18:59:51 -07001860 /*
1861 * We are holding the reader lock, so the reader page won't be swapped
1862 * in the ring buffer. Now we are racing with the writer trying to
1863 * move head page and the tail page.
1864 * We are going to adapt the reader page update process where:
1865 * 1. We first splice the start and end of list of new pages between
1866 * the head page and its previous page.
1867 * 2. We cmpxchg the prev_page->next to point from head page to the
1868 * start of new pages list.
1869 * 3. Finally, we update the head->prev to the end of new list.
1870 *
1871 * We will try this process 10 times, to make sure that we don't keep
1872 * spinning.
1873 */
1874 retries = 10;
1875 success = 0;
1876 while (retries--) {
1877 struct list_head *head_page, *prev_page, *r;
1878 struct list_head *last_page, *first_page;
1879 struct list_head *head_page_with_bit;
Steven Rostedt77ae3652009-03-27 11:00:29 -04001880
Vaibhav Nagarnaik5040b4b2012-05-03 18:59:51 -07001881 head_page = &rb_set_head_page(cpu_buffer)->list;
Steven Rostedt54f7be52012-11-29 22:27:22 -05001882 if (!head_page)
1883 break;
Vaibhav Nagarnaik5040b4b2012-05-03 18:59:51 -07001884 prev_page = head_page->prev;
1885
1886 first_page = pages->next;
1887 last_page = pages->prev;
1888
1889 head_page_with_bit = (struct list_head *)
1890 ((unsigned long)head_page | RB_PAGE_HEAD);
1891
1892 last_page->next = head_page_with_bit;
1893 first_page->prev = prev_page;
1894
1895 r = cmpxchg(&prev_page->next, head_page_with_bit, first_page);
1896
1897 if (r == head_page_with_bit) {
1898 /*
1899 * yay, we replaced the page pointer to our new list,
1900 * now, we just have to update to head page's prev
1901 * pointer to point to end of list
1902 */
1903 head_page->prev = last_page;
1904 success = 1;
1905 break;
1906 }
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001907 }
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001908
Vaibhav Nagarnaik5040b4b2012-05-03 18:59:51 -07001909 if (success)
1910 INIT_LIST_HEAD(pages);
1911 /*
1912 * If we weren't successful in adding in new pages, warn and stop
1913 * tracing
1914 */
1915 RB_WARN_ON(cpu_buffer, !success);
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02001916 raw_spin_unlock_irq(&cpu_buffer->reader_lock);
Vaibhav Nagarnaik5040b4b2012-05-03 18:59:51 -07001917
1918 /* free pages if they weren't inserted */
1919 if (!success) {
1920 struct buffer_page *bpage, *tmp;
1921 list_for_each_entry_safe(bpage, tmp, &cpu_buffer->new_pages,
1922 list) {
1923 list_del_init(&bpage->list);
1924 free_buffer_page(bpage);
1925 }
1926 }
1927 return success;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001928}
1929
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001930static void rb_update_pages(struct ring_buffer_per_cpu *cpu_buffer)
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001931{
Vaibhav Nagarnaik5040b4b2012-05-03 18:59:51 -07001932 int success;
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001933
Vaibhav Nagarnaik5040b4b2012-05-03 18:59:51 -07001934 if (cpu_buffer->nr_pages_to_update > 0)
1935 success = rb_insert_pages(cpu_buffer);
1936 else
1937 success = rb_remove_pages(cpu_buffer,
1938 -cpu_buffer->nr_pages_to_update);
1939
1940 if (success)
1941 cpu_buffer->nr_pages += cpu_buffer->nr_pages_to_update;
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001942}
1943
1944static void update_pages_handler(struct work_struct *work)
1945{
1946 struct ring_buffer_per_cpu *cpu_buffer = container_of(work,
1947 struct ring_buffer_per_cpu, update_pages_work);
1948 rb_update_pages(cpu_buffer);
Vaibhav Nagarnaik05fdd702012-05-18 13:29:51 -07001949 complete(&cpu_buffer->update_done);
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001950}
1951
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001952/**
1953 * ring_buffer_resize - resize the ring buffer
1954 * @buffer: the buffer to resize.
1955 * @size: the new size.
zhangwei(Jovi)d6118512013-07-15 16:32:50 +08001956 * @cpu_id: the cpu buffer to resize
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001957 *
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001958 * Minimum size is 2 * BUF_PAGE_SIZE.
1959 *
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001960 * Returns 0 on success and < 0 on failure.
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001961 */
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05001962int ring_buffer_resize(struct trace_buffer *buffer, unsigned long size,
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001963 int cpu_id)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001964{
1965 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedt (Red Hat)9b94a8f2016-05-12 11:01:24 -04001966 unsigned long nr_pages;
Qiujun Huang0a1754b2020-10-19 22:22:42 +08001967 int cpu, err;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001968
Ingo Molnaree51a1d2008-11-13 14:58:31 +01001969 /*
1970 * Always succeed at resizing a non-existent buffer:
1971 */
1972 if (!buffer)
Qiujun Huang0a1754b2020-10-19 22:22:42 +08001973 return 0;
Ingo Molnaree51a1d2008-11-13 14:58:31 +01001974
Steven Rostedt6a31e1f2012-05-23 15:35:17 -04001975 /* Make sure the requested buffer exists */
1976 if (cpu_id != RING_BUFFER_ALL_CPUS &&
1977 !cpumask_test_cpu(cpu_id, buffer->cpumask))
Qiujun Huang0a1754b2020-10-19 22:22:42 +08001978 return 0;
Steven Rostedt6a31e1f2012-05-23 15:35:17 -04001979
Steven Rostedt (Red Hat)59643d12016-05-13 09:34:12 -04001980 nr_pages = DIV_ROUND_UP(size, BUF_PAGE_SIZE);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001981
1982 /* we need a minimum of two pages */
Steven Rostedt (Red Hat)59643d12016-05-13 09:34:12 -04001983 if (nr_pages < 2)
1984 nr_pages = 2;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001985
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001986 /* prevent another thread from changing buffer sizes */
1987 mutex_lock(&buffer->mutex);
1988
Steven Rostedt (VMware)07b8b102020-03-27 16:21:22 -04001989
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001990 if (cpu_id == RING_BUFFER_ALL_CPUS) {
Steven Rostedt (VMware)07b8b102020-03-27 16:21:22 -04001991 /*
1992 * Don't succeed if resizing is disabled, as a reader might be
1993 * manipulating the ring buffer and is expecting a sane state while
1994 * this is true.
1995 */
1996 for_each_buffer_cpu(buffer, cpu) {
1997 cpu_buffer = buffer->buffers[cpu];
1998 if (atomic_read(&cpu_buffer->resize_disabled)) {
1999 err = -EBUSY;
2000 goto out_err_unlock;
2001 }
2002 }
2003
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08002004 /* calculate the pages to update */
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002005 for_each_buffer_cpu(buffer, cpu) {
2006 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002007
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08002008 cpu_buffer->nr_pages_to_update = nr_pages -
2009 cpu_buffer->nr_pages;
Vaibhav Nagarnaikd7ec4bf2011-06-07 17:01:42 -07002010 /*
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08002011 * nothing more to do for removing pages or no update
Vaibhav Nagarnaikd7ec4bf2011-06-07 17:01:42 -07002012 */
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08002013 if (cpu_buffer->nr_pages_to_update <= 0)
2014 continue;
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08002015 /*
2016 * to add pages, make sure all new pages can be
2017 * allocated without receiving ENOMEM
2018 */
2019 INIT_LIST_HEAD(&cpu_buffer->new_pages);
Qiujun Huang74e2afc2020-10-15 19:38:42 +08002020 if (__rb_allocate_pages(cpu_buffer, cpu_buffer->nr_pages_to_update,
2021 &cpu_buffer->new_pages)) {
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08002022 /* not enough memory for new pages */
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07002023 err = -ENOMEM;
2024 goto out_err;
2025 }
2026 }
2027
2028 get_online_cpus();
2029 /*
2030 * Fire off all the required work handlers
Vaibhav Nagarnaik05fdd702012-05-18 13:29:51 -07002031 * We can't schedule on offline CPUs, but it's not necessary
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07002032 * since we can change their buffer sizes without any race.
2033 */
2034 for_each_buffer_cpu(buffer, cpu) {
2035 cpu_buffer = buffer->buffers[cpu];
Vaibhav Nagarnaik05fdd702012-05-18 13:29:51 -07002036 if (!cpu_buffer->nr_pages_to_update)
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07002037 continue;
2038
Corey Minyard021c5b32014-07-16 14:07:13 -05002039 /* Can't run something on an offline CPU. */
2040 if (!cpu_online(cpu)) {
Steven Rostedt (Red Hat)f5eb5582013-03-07 09:27:42 -05002041 rb_update_pages(cpu_buffer);
2042 cpu_buffer->nr_pages_to_update = 0;
2043 } else {
Vaibhav Nagarnaik05fdd702012-05-18 13:29:51 -07002044 schedule_work_on(cpu,
2045 &cpu_buffer->update_pages_work);
Steven Rostedt (Red Hat)f5eb5582013-03-07 09:27:42 -05002046 }
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002047 }
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002048
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08002049 /* wait for all the updates to complete */
2050 for_each_buffer_cpu(buffer, cpu) {
2051 cpu_buffer = buffer->buffers[cpu];
Vaibhav Nagarnaik05fdd702012-05-18 13:29:51 -07002052 if (!cpu_buffer->nr_pages_to_update)
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07002053 continue;
2054
Vaibhav Nagarnaik05fdd702012-05-18 13:29:51 -07002055 if (cpu_online(cpu))
2056 wait_for_completion(&cpu_buffer->update_done);
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07002057 cpu_buffer->nr_pages_to_update = 0;
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08002058 }
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07002059
2060 put_online_cpus();
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08002061 } else {
2062 cpu_buffer = buffer->buffers[cpu_id];
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07002063
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08002064 if (nr_pages == cpu_buffer->nr_pages)
2065 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002066
Steven Rostedt (VMware)07b8b102020-03-27 16:21:22 -04002067 /*
2068 * Don't succeed if resizing is disabled, as a reader might be
2069 * manipulating the ring buffer and is expecting a sane state while
2070 * this is true.
2071 */
2072 if (atomic_read(&cpu_buffer->resize_disabled)) {
2073 err = -EBUSY;
2074 goto out_err_unlock;
2075 }
2076
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08002077 cpu_buffer->nr_pages_to_update = nr_pages -
2078 cpu_buffer->nr_pages;
2079
2080 INIT_LIST_HEAD(&cpu_buffer->new_pages);
2081 if (cpu_buffer->nr_pages_to_update > 0 &&
Qiujun Huang74e2afc2020-10-15 19:38:42 +08002082 __rb_allocate_pages(cpu_buffer, cpu_buffer->nr_pages_to_update,
2083 &cpu_buffer->new_pages)) {
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07002084 err = -ENOMEM;
2085 goto out_err;
2086 }
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08002087
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07002088 get_online_cpus();
2089
Corey Minyard021c5b32014-07-16 14:07:13 -05002090 /* Can't run something on an offline CPU. */
2091 if (!cpu_online(cpu_id))
Steven Rostedt (Red Hat)f5eb5582013-03-07 09:27:42 -05002092 rb_update_pages(cpu_buffer);
2093 else {
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07002094 schedule_work_on(cpu_id,
2095 &cpu_buffer->update_pages_work);
Vaibhav Nagarnaik05fdd702012-05-18 13:29:51 -07002096 wait_for_completion(&cpu_buffer->update_done);
Steven Rostedt (Red Hat)f5eb5582013-03-07 09:27:42 -05002097 }
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07002098
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07002099 cpu_buffer->nr_pages_to_update = 0;
Vaibhav Nagarnaik05fdd702012-05-18 13:29:51 -07002100 put_online_cpus();
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08002101 }
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002102
2103 out:
Steven Rostedt659f4512012-05-14 17:02:33 -04002104 /*
2105 * The ring buffer resize can happen with the ring buffer
2106 * enabled, so that the update disturbs the tracing as little
2107 * as possible. But if the buffer is disabled, we do not need
2108 * to worry about that, and we can take the time to verify
2109 * that the buffer is not corrupt.
2110 */
2111 if (atomic_read(&buffer->record_disabled)) {
2112 atomic_inc(&buffer->record_disabled);
2113 /*
2114 * Even though the buffer was disabled, we must make sure
2115 * that it is truly disabled before calling rb_check_pages.
2116 * There could have been a race between checking
2117 * record_disable and incrementing it.
2118 */
Paul E. McKenney74401722018-11-06 18:44:52 -08002119 synchronize_rcu();
Steven Rostedt659f4512012-05-14 17:02:33 -04002120 for_each_buffer_cpu(buffer, cpu) {
2121 cpu_buffer = buffer->buffers[cpu];
2122 rb_check_pages(cpu_buffer);
2123 }
2124 atomic_dec(&buffer->record_disabled);
2125 }
2126
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002127 mutex_unlock(&buffer->mutex);
Qiujun Huang0a1754b2020-10-19 22:22:42 +08002128 return 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002129
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07002130 out_err:
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08002131 for_each_buffer_cpu(buffer, cpu) {
2132 struct buffer_page *bpage, *tmp;
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07002133
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08002134 cpu_buffer = buffer->buffers[cpu];
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08002135 cpu_buffer->nr_pages_to_update = 0;
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07002136
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08002137 if (list_empty(&cpu_buffer->new_pages))
2138 continue;
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07002139
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08002140 list_for_each_entry_safe(bpage, tmp, &cpu_buffer->new_pages,
2141 list) {
2142 list_del_init(&bpage->list);
2143 free_buffer_page(bpage);
2144 }
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002145 }
Steven Rostedt (VMware)07b8b102020-03-27 16:21:22 -04002146 out_err_unlock:
Vegard Nossum641d2f62008-11-18 19:22:13 +01002147 mutex_unlock(&buffer->mutex);
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07002148 return err;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002149}
Robert Richterc4f50182008-12-11 16:49:22 +01002150EXPORT_SYMBOL_GPL(ring_buffer_resize);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002151
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05002152void ring_buffer_change_overwrite(struct trace_buffer *buffer, int val)
David Sharp750912f2010-12-08 13:46:47 -08002153{
2154 mutex_lock(&buffer->mutex);
2155 if (val)
2156 buffer->flags |= RB_FL_OVERWRITE;
2157 else
2158 buffer->flags &= ~RB_FL_OVERWRITE;
2159 mutex_unlock(&buffer->mutex);
2160}
2161EXPORT_SYMBOL_GPL(ring_buffer_change_overwrite);
2162
Steven Rostedt (Red Hat)2289d562016-11-23 20:35:32 -05002163static __always_inline void *__rb_page_index(struct buffer_page *bpage, unsigned index)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002164{
Steven Rostedt044fa782008-12-02 23:50:03 -05002165 return bpage->page->data + index;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002166}
2167
Steven Rostedt (Red Hat)2289d562016-11-23 20:35:32 -05002168static __always_inline struct ring_buffer_event *
Steven Rostedtd7690412008-10-01 00:29:53 -04002169rb_reader_event(struct ring_buffer_per_cpu *cpu_buffer)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002170{
Steven Rostedt6f807ac2008-10-04 02:00:58 -04002171 return __rb_page_index(cpu_buffer->reader_page,
2172 cpu_buffer->reader_page->read);
2173}
2174
Steven Rostedt (Red Hat)2289d562016-11-23 20:35:32 -05002175static __always_inline unsigned rb_page_commit(struct buffer_page *bpage)
Steven Rostedtbf41a152008-10-04 02:00:59 -04002176{
Steven Rostedtabc9b562008-12-02 15:34:06 -05002177 return local_read(&bpage->page->commit);
Steven Rostedtbf41a152008-10-04 02:00:59 -04002178}
2179
Steven Rostedt (VMware)785888c2020-03-17 17:32:27 -04002180static struct ring_buffer_event *
2181rb_iter_head_event(struct ring_buffer_iter *iter)
2182{
2183 struct ring_buffer_event *event;
2184 struct buffer_page *iter_head_page = iter->head_page;
2185 unsigned long commit;
2186 unsigned length;
2187
Steven Rostedt (VMware)153368c2020-03-17 17:32:29 -04002188 if (iter->head != iter->next_event)
2189 return iter->event;
2190
Steven Rostedt (VMware)785888c2020-03-17 17:32:27 -04002191 /*
2192 * When the writer goes across pages, it issues a cmpxchg which
2193 * is a mb(), which will synchronize with the rmb here.
2194 * (see rb_tail_page_update() and __rb_reserve_next())
2195 */
2196 commit = rb_page_commit(iter_head_page);
2197 smp_rmb();
2198 event = __rb_page_index(iter_head_page, iter->head);
2199 length = rb_event_length(event);
2200
2201 /*
2202 * READ_ONCE() doesn't work on functions and we don't want the
2203 * compiler doing any crazy optimizations with length.
2204 */
2205 barrier();
2206
2207 if ((iter->head + length) > commit || length > BUF_MAX_DATA_SIZE)
2208 /* Writer corrupted the read? */
2209 goto reset;
2210
2211 memcpy(iter->event, event, length);
2212 /*
2213 * If the page stamp is still the same after this rmb() then the
2214 * event was safely copied without the writer entering the page.
2215 */
2216 smp_rmb();
2217
2218 /* Make sure the page didn't change since we read this */
2219 if (iter->page_stamp != iter_head_page->page->time_stamp ||
2220 commit > rb_page_commit(iter_head_page))
2221 goto reset;
2222
2223 iter->next_event = iter->head + length;
2224 return iter->event;
2225 reset:
2226 /* Reset to the beginning */
2227 iter->page_stamp = iter->read_stamp = iter->head_page->page->time_stamp;
2228 iter->head = 0;
2229 iter->next_event = 0;
Steven Rostedt (VMware)c9b7a4a2020-03-17 17:32:32 -04002230 iter->missed_events = 1;
Steven Rostedt (VMware)785888c2020-03-17 17:32:27 -04002231 return NULL;
2232}
2233
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002234/* Size is determined by what has been committed */
Steven Rostedt (Red Hat)2289d562016-11-23 20:35:32 -05002235static __always_inline unsigned rb_page_size(struct buffer_page *bpage)
Steven Rostedtbf41a152008-10-04 02:00:59 -04002236{
2237 return rb_page_commit(bpage);
2238}
2239
Steven Rostedt (Red Hat)2289d562016-11-23 20:35:32 -05002240static __always_inline unsigned
Steven Rostedtbf41a152008-10-04 02:00:59 -04002241rb_commit_index(struct ring_buffer_per_cpu *cpu_buffer)
2242{
2243 return rb_page_commit(cpu_buffer->commit_page);
2244}
2245
Steven Rostedt (Red Hat)2289d562016-11-23 20:35:32 -05002246static __always_inline unsigned
Steven Rostedtbf41a152008-10-04 02:00:59 -04002247rb_event_index(struct ring_buffer_event *event)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002248{
Steven Rostedtbf41a152008-10-04 02:00:59 -04002249 unsigned long addr = (unsigned long)event;
2250
Steven Rostedt22f470f2009-06-11 09:29:58 -04002251 return (addr & ~PAGE_MASK) - BUF_PAGE_HDR_SIZE;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002252}
2253
Andrew Morton34a148b2009-01-09 12:27:09 -08002254static void rb_inc_iter(struct ring_buffer_iter *iter)
Steven Rostedtd7690412008-10-01 00:29:53 -04002255{
2256 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
2257
2258 /*
2259 * The iterator could be on the reader page (it starts there).
2260 * But the head could have moved, since the reader was
2261 * found. Check for this case and assign the iterator
2262 * to the head page instead of next.
2263 */
2264 if (iter->head_page == cpu_buffer->reader_page)
Steven Rostedt77ae3652009-03-27 11:00:29 -04002265 iter->head_page = rb_set_head_page(cpu_buffer);
Steven Rostedtd7690412008-10-01 00:29:53 -04002266 else
Qiujun Huang6689bed2020-12-25 22:03:56 +08002267 rb_inc_page(&iter->head_page);
Steven Rostedtd7690412008-10-01 00:29:53 -04002268
Steven Rostedt (VMware)28e3fc52020-03-17 17:32:26 -04002269 iter->page_stamp = iter->read_stamp = iter->head_page->page->time_stamp;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002270 iter->head = 0;
Steven Rostedt (VMware)785888c2020-03-17 17:32:27 -04002271 iter->next_event = 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002272}
2273
Steven Rostedt77ae3652009-03-27 11:00:29 -04002274/*
2275 * rb_handle_head_page - writer hit the head page
2276 *
2277 * Returns: +1 to retry page
2278 * 0 to continue
2279 * -1 on error
2280 */
2281static int
2282rb_handle_head_page(struct ring_buffer_per_cpu *cpu_buffer,
2283 struct buffer_page *tail_page,
2284 struct buffer_page *next_page)
2285{
2286 struct buffer_page *new_head;
2287 int entries;
2288 int type;
2289 int ret;
2290
2291 entries = rb_page_entries(next_page);
2292
2293 /*
2294 * The hard part is here. We need to move the head
2295 * forward, and protect against both readers on
2296 * other CPUs and writers coming in via interrupts.
2297 */
2298 type = rb_head_page_set_update(cpu_buffer, next_page, tail_page,
2299 RB_PAGE_HEAD);
2300
2301 /*
2302 * type can be one of four:
2303 * NORMAL - an interrupt already moved it for us
2304 * HEAD - we are the first to get here.
2305 * UPDATE - we are the interrupt interrupting
2306 * a current move.
2307 * MOVED - a reader on another CPU moved the next
2308 * pointer to its reader page. Give up
2309 * and try again.
2310 */
2311
2312 switch (type) {
2313 case RB_PAGE_HEAD:
2314 /*
2315 * We changed the head to UPDATE, thus
2316 * it is our responsibility to update
2317 * the counters.
2318 */
2319 local_add(entries, &cpu_buffer->overrun);
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -07002320 local_sub(BUF_PAGE_SIZE, &cpu_buffer->entries_bytes);
Steven Rostedt77ae3652009-03-27 11:00:29 -04002321
2322 /*
2323 * The entries will be zeroed out when we move the
2324 * tail page.
2325 */
2326
2327 /* still more to do */
2328 break;
2329
2330 case RB_PAGE_UPDATE:
2331 /*
2332 * This is an interrupt that interrupt the
2333 * previous update. Still more to do.
2334 */
2335 break;
2336 case RB_PAGE_NORMAL:
2337 /*
2338 * An interrupt came in before the update
2339 * and processed this for us.
2340 * Nothing left to do.
2341 */
2342 return 1;
2343 case RB_PAGE_MOVED:
2344 /*
2345 * The reader is on another CPU and just did
2346 * a swap with our next_page.
2347 * Try again.
2348 */
2349 return 1;
2350 default:
2351 RB_WARN_ON(cpu_buffer, 1); /* WTF??? */
2352 return -1;
2353 }
2354
2355 /*
2356 * Now that we are here, the old head pointer is
2357 * set to UPDATE. This will keep the reader from
2358 * swapping the head page with the reader page.
2359 * The reader (on another CPU) will spin till
2360 * we are finished.
2361 *
2362 * We just need to protect against interrupts
2363 * doing the job. We will set the next pointer
2364 * to HEAD. After that, we set the old pointer
2365 * to NORMAL, but only if it was HEAD before.
2366 * otherwise we are an interrupt, and only
2367 * want the outer most commit to reset it.
2368 */
2369 new_head = next_page;
Qiujun Huang6689bed2020-12-25 22:03:56 +08002370 rb_inc_page(&new_head);
Steven Rostedt77ae3652009-03-27 11:00:29 -04002371
2372 ret = rb_head_page_set_head(cpu_buffer, new_head, next_page,
2373 RB_PAGE_NORMAL);
2374
2375 /*
2376 * Valid returns are:
2377 * HEAD - an interrupt came in and already set it.
2378 * NORMAL - One of two things:
2379 * 1) We really set it.
2380 * 2) A bunch of interrupts came in and moved
2381 * the page forward again.
2382 */
2383 switch (ret) {
2384 case RB_PAGE_HEAD:
2385 case RB_PAGE_NORMAL:
2386 /* OK */
2387 break;
2388 default:
2389 RB_WARN_ON(cpu_buffer, 1);
2390 return -1;
2391 }
2392
2393 /*
2394 * It is possible that an interrupt came in,
2395 * set the head up, then more interrupts came in
2396 * and moved it again. When we get back here,
2397 * the page would have been set to NORMAL but we
2398 * just set it back to HEAD.
2399 *
2400 * How do you detect this? Well, if that happened
2401 * the tail page would have moved.
2402 */
2403 if (ret == RB_PAGE_NORMAL) {
Steven Rostedt (Red Hat)85736362015-11-17 14:03:11 -05002404 struct buffer_page *buffer_tail_page;
2405
2406 buffer_tail_page = READ_ONCE(cpu_buffer->tail_page);
Steven Rostedt77ae3652009-03-27 11:00:29 -04002407 /*
2408 * If the tail had moved passed next, then we need
2409 * to reset the pointer.
2410 */
Steven Rostedt (Red Hat)85736362015-11-17 14:03:11 -05002411 if (buffer_tail_page != tail_page &&
2412 buffer_tail_page != next_page)
Steven Rostedt77ae3652009-03-27 11:00:29 -04002413 rb_head_page_set_normal(cpu_buffer, new_head,
2414 next_page,
2415 RB_PAGE_HEAD);
2416 }
2417
2418 /*
2419 * If this was the outer most commit (the one that
2420 * changed the original pointer from HEAD to UPDATE),
2421 * then it is up to us to reset it to NORMAL.
2422 */
2423 if (type == RB_PAGE_HEAD) {
2424 ret = rb_head_page_set_normal(cpu_buffer, next_page,
2425 tail_page,
2426 RB_PAGE_UPDATE);
2427 if (RB_WARN_ON(cpu_buffer,
2428 ret != RB_PAGE_UPDATE))
2429 return -1;
2430 }
2431
2432 return 0;
2433}
2434
Steven Rostedtc7b09302009-06-11 11:12:00 -04002435static inline void
2436rb_reset_tail(struct ring_buffer_per_cpu *cpu_buffer,
Steven Rostedt (Red Hat)fcc742e2015-05-28 17:13:14 -04002437 unsigned long tail, struct rb_event_info *info)
Steven Rostedtc7b09302009-06-11 11:12:00 -04002438{
Steven Rostedt (Red Hat)fcc742e2015-05-28 17:13:14 -04002439 struct buffer_page *tail_page = info->tail_page;
Steven Rostedtc7b09302009-06-11 11:12:00 -04002440 struct ring_buffer_event *event;
Steven Rostedt (Red Hat)fcc742e2015-05-28 17:13:14 -04002441 unsigned long length = info->length;
Steven Rostedtc7b09302009-06-11 11:12:00 -04002442
2443 /*
2444 * Only the event that crossed the page boundary
2445 * must fill the old tail_page with padding.
2446 */
2447 if (tail >= BUF_PAGE_SIZE) {
Steven Rostedtb3230c82010-05-21 11:55:21 -04002448 /*
2449 * If the page was filled, then we still need
2450 * to update the real_end. Reset it to zero
2451 * and the reader will ignore it.
2452 */
2453 if (tail == BUF_PAGE_SIZE)
2454 tail_page->real_end = 0;
2455
Steven Rostedtc7b09302009-06-11 11:12:00 -04002456 local_sub(length, &tail_page->write);
2457 return;
2458 }
2459
2460 event = __rb_page_index(tail_page, tail);
2461
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -07002462 /* account for padding bytes */
2463 local_add(BUF_PAGE_SIZE - tail, &cpu_buffer->entries_bytes);
2464
Steven Rostedtc7b09302009-06-11 11:12:00 -04002465 /*
Steven Rostedtff0ff842010-03-31 22:11:42 -04002466 * Save the original length to the meta data.
2467 * This will be used by the reader to add lost event
2468 * counter.
2469 */
2470 tail_page->real_end = tail;
2471
2472 /*
Steven Rostedtc7b09302009-06-11 11:12:00 -04002473 * If this event is bigger than the minimum size, then
2474 * we need to be careful that we don't subtract the
2475 * write counter enough to allow another writer to slip
2476 * in on this page.
2477 * We put in a discarded commit instead, to make sure
2478 * that this space is not used again.
2479 *
2480 * If we are less than the minimum size, we don't need to
2481 * worry about it.
2482 */
2483 if (tail > (BUF_PAGE_SIZE - RB_EVNT_MIN_SIZE)) {
2484 /* No room for any events */
2485
2486 /* Mark the rest of the page with padding */
2487 rb_event_set_padding(event);
2488
2489 /* Set the write back to the previous setting */
2490 local_sub(length, &tail_page->write);
2491 return;
2492 }
2493
2494 /* Put in a discarded event */
2495 event->array[0] = (BUF_PAGE_SIZE - tail) - RB_EVNT_HDR_SIZE;
2496 event->type_len = RINGBUF_TYPE_PADDING;
2497 /* time delta must be non zero */
2498 event->time_delta = 1;
Steven Rostedtc7b09302009-06-11 11:12:00 -04002499
2500 /* Set write to end of buffer */
2501 length = (tail + length) - BUF_PAGE_SIZE;
2502 local_sub(length, &tail_page->write);
2503}
Steven Rostedt6634ff22009-05-06 15:30:07 -04002504
Steven Rostedt (Red Hat)4239c382015-11-17 16:36:06 -05002505static inline void rb_end_commit(struct ring_buffer_per_cpu *cpu_buffer);
2506
Steven Rostedt747e94a2010-10-08 13:51:48 -04002507/*
2508 * This is the slow path, force gcc not to inline it.
2509 */
2510static noinline struct ring_buffer_event *
Steven Rostedt6634ff22009-05-06 15:30:07 -04002511rb_move_tail(struct ring_buffer_per_cpu *cpu_buffer,
Steven Rostedt (Red Hat)fcc742e2015-05-28 17:13:14 -04002512 unsigned long tail, struct rb_event_info *info)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002513{
Steven Rostedt (Red Hat)fcc742e2015-05-28 17:13:14 -04002514 struct buffer_page *tail_page = info->tail_page;
Steven Rostedt5a50e332009-11-17 08:43:01 -05002515 struct buffer_page *commit_page = cpu_buffer->commit_page;
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05002516 struct trace_buffer *buffer = cpu_buffer->buffer;
Steven Rostedt77ae3652009-03-27 11:00:29 -04002517 struct buffer_page *next_page;
2518 int ret;
Steven Rostedtaa20ae82009-05-05 21:16:11 -04002519
2520 next_page = tail_page;
2521
Qiujun Huang6689bed2020-12-25 22:03:56 +08002522 rb_inc_page(&next_page);
Steven Rostedtaa20ae82009-05-05 21:16:11 -04002523
Steven Rostedtaa20ae82009-05-05 21:16:11 -04002524 /*
2525 * If for some reason, we had an interrupt storm that made
2526 * it all the way around the buffer, bail, and warn
2527 * about it.
2528 */
2529 if (unlikely(next_page == commit_page)) {
Steven Rostedt77ae3652009-03-27 11:00:29 -04002530 local_inc(&cpu_buffer->commit_overrun);
Steven Rostedtaa20ae82009-05-05 21:16:11 -04002531 goto out_reset;
2532 }
2533
Steven Rostedt77ae3652009-03-27 11:00:29 -04002534 /*
2535 * This is where the fun begins!
2536 *
2537 * We are fighting against races between a reader that
2538 * could be on another CPU trying to swap its reader
2539 * page with the buffer head.
2540 *
2541 * We are also fighting against interrupts coming in and
2542 * moving the head or tail on us as well.
2543 *
2544 * If the next page is the head page then we have filled
2545 * the buffer, unless the commit page is still on the
2546 * reader page.
2547 */
Qiujun Huang6689bed2020-12-25 22:03:56 +08002548 if (rb_is_head_page(next_page, &tail_page->list)) {
Steven Rostedtaa20ae82009-05-05 21:16:11 -04002549
Steven Rostedt77ae3652009-03-27 11:00:29 -04002550 /*
2551 * If the commit is not on the reader page, then
2552 * move the header page.
2553 */
2554 if (!rb_is_reader_page(cpu_buffer->commit_page)) {
2555 /*
2556 * If we are not in overwrite mode,
2557 * this is easy, just stop here.
2558 */
Slava Pestov884bfe82011-07-15 14:23:58 -07002559 if (!(buffer->flags & RB_FL_OVERWRITE)) {
2560 local_inc(&cpu_buffer->dropped_events);
Steven Rostedt77ae3652009-03-27 11:00:29 -04002561 goto out_reset;
Slava Pestov884bfe82011-07-15 14:23:58 -07002562 }
Steven Rostedtaa20ae82009-05-05 21:16:11 -04002563
Steven Rostedt77ae3652009-03-27 11:00:29 -04002564 ret = rb_handle_head_page(cpu_buffer,
2565 tail_page,
2566 next_page);
2567 if (ret < 0)
2568 goto out_reset;
2569 if (ret)
2570 goto out_again;
2571 } else {
2572 /*
2573 * We need to be careful here too. The
2574 * commit page could still be on the reader
2575 * page. We could have a small buffer, and
2576 * have filled up the buffer with events
2577 * from interrupts and such, and wrapped.
2578 *
Qiujun Huangc6358ba2020-12-24 22:46:34 +08002579 * Note, if the tail page is also on the
Steven Rostedt77ae3652009-03-27 11:00:29 -04002580 * reader_page, we let it move out.
2581 */
2582 if (unlikely((cpu_buffer->commit_page !=
2583 cpu_buffer->tail_page) &&
2584 (cpu_buffer->commit_page ==
2585 cpu_buffer->reader_page))) {
2586 local_inc(&cpu_buffer->commit_overrun);
2587 goto out_reset;
2588 }
Steven Rostedtaa20ae82009-05-05 21:16:11 -04002589 }
2590 }
2591
Steven Rostedt (Red Hat)70004982015-11-17 15:15:19 -05002592 rb_tail_page_update(cpu_buffer, tail_page, next_page);
Steven Rostedtaa20ae82009-05-05 21:16:11 -04002593
Steven Rostedt77ae3652009-03-27 11:00:29 -04002594 out_again:
Steven Rostedtaa20ae82009-05-05 21:16:11 -04002595
Steven Rostedt (Red Hat)fcc742e2015-05-28 17:13:14 -04002596 rb_reset_tail(cpu_buffer, tail, info);
Steven Rostedtaa20ae82009-05-05 21:16:11 -04002597
Steven Rostedt (Red Hat)4239c382015-11-17 16:36:06 -05002598 /* Commit what we have for now. */
2599 rb_end_commit(cpu_buffer);
2600 /* rb_end_commit() decs committing */
2601 local_inc(&cpu_buffer->committing);
2602
Steven Rostedtaa20ae82009-05-05 21:16:11 -04002603 /* fail and let the caller try again */
2604 return ERR_PTR(-EAGAIN);
2605
Steven Rostedt45141d42009-02-12 13:19:48 -05002606 out_reset:
Lai Jiangshan6f3b3442009-01-12 11:06:18 +08002607 /* reset write */
Steven Rostedt (Red Hat)fcc742e2015-05-28 17:13:14 -04002608 rb_reset_tail(cpu_buffer, tail, info);
Lai Jiangshan6f3b3442009-01-12 11:06:18 +08002609
Steven Rostedtbf41a152008-10-04 02:00:59 -04002610 return NULL;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002611}
2612
Steven Rostedt (VMware)74e87932020-06-30 12:47:56 -04002613/* Slow path */
2614static struct ring_buffer_event *
Tom Zanussidc4e2802018-01-15 20:51:40 -06002615rb_add_time_stamp(struct ring_buffer_event *event, u64 delta, bool abs)
Steven Rostedt (Red Hat)d90fd772015-05-29 12:12:27 -04002616{
Tom Zanussidc4e2802018-01-15 20:51:40 -06002617 if (abs)
2618 event->type_len = RINGBUF_TYPE_TIME_STAMP;
2619 else
2620 event->type_len = RINGBUF_TYPE_TIME_EXTEND;
Steven Rostedt (Red Hat)d90fd772015-05-29 12:12:27 -04002621
Tom Zanussidc4e2802018-01-15 20:51:40 -06002622 /* Not the first event on the page, or not delta? */
2623 if (abs || rb_event_index(event)) {
Steven Rostedt (Red Hat)d90fd772015-05-29 12:12:27 -04002624 event->time_delta = delta & TS_MASK;
2625 event->array[0] = delta >> TS_SHIFT;
2626 } else {
2627 /* nope, just zero it */
2628 event->time_delta = 0;
2629 event->array[0] = 0;
2630 }
2631
2632 return skip_time_extend(event);
2633}
2634
Steven Rostedt (VMware)58fbc3c2020-06-30 08:59:26 -04002635#ifndef CONFIG_HAVE_UNSTABLE_SCHED_CLOCK
2636static inline bool sched_clock_stable(void)
2637{
2638 return true;
2639}
2640#endif
2641
Steven Rostedt (VMware)74e87932020-06-30 12:47:56 -04002642static void
Steven Rostedt (VMware)58fbc3c2020-06-30 08:59:26 -04002643rb_check_timestamp(struct ring_buffer_per_cpu *cpu_buffer,
2644 struct rb_event_info *info)
2645{
2646 u64 write_stamp;
2647
Steven Rostedt (VMware)29ce2452020-07-01 13:10:19 -04002648 WARN_ONCE(1, "Delta way too big! %llu ts=%llu before=%llu after=%llu write stamp=%llu\n%s",
Steven Rostedt (VMware)58fbc3c2020-06-30 08:59:26 -04002649 (unsigned long long)info->delta,
2650 (unsigned long long)info->ts,
2651 (unsigned long long)info->before,
2652 (unsigned long long)info->after,
2653 (unsigned long long)(rb_time_read(&cpu_buffer->write_stamp, &write_stamp) ? write_stamp : 0),
2654 sched_clock_stable() ? "" :
2655 "If you just came from a suspend/resume,\n"
2656 "please switch to the trace global clock:\n"
2657 " echo global > /sys/kernel/debug/tracing/trace_clock\n"
2658 "or add trace_clock=global to the kernel command line\n");
2659}
2660
Steven Rostedt (VMware)74e87932020-06-30 12:47:56 -04002661static void rb_add_timestamp(struct ring_buffer_per_cpu *cpu_buffer,
2662 struct ring_buffer_event **event,
2663 struct rb_event_info *info,
2664 u64 *delta,
2665 unsigned int *length)
2666{
2667 bool abs = info->add_timestamp &
2668 (RB_ADD_STAMP_FORCE | RB_ADD_STAMP_ABSOLUTE);
2669
Steven Rostedt (VMware)29ce2452020-07-01 13:10:19 -04002670 if (unlikely(info->delta > (1ULL << 59))) {
2671 /* did the clock go backwards */
2672 if (info->before == info->after && info->before > info->ts) {
2673 /* not interrupted */
2674 static int once;
2675
2676 /*
2677 * This is possible with a recalibrating of the TSC.
2678 * Do not produce a call stack, but just report it.
2679 */
2680 if (!once) {
2681 once++;
2682 pr_warn("Ring buffer clock went backwards: %llu -> %llu\n",
2683 info->before, info->ts);
2684 }
2685 } else
2686 rb_check_timestamp(cpu_buffer, info);
2687 if (!abs)
2688 info->delta = 0;
2689 }
Steven Rostedt (VMware)74e87932020-06-30 12:47:56 -04002690 *event = rb_add_time_stamp(*event, info->delta, abs);
2691 *length -= RB_LEN_TIME_EXTEND;
2692 *delta = 0;
2693}
2694
Steven Rostedt (Red Hat)d90fd772015-05-29 12:12:27 -04002695/**
2696 * rb_update_event - update event type and data
Steven Rostedt (VMware)cfc585a2020-01-14 16:27:51 -05002697 * @cpu_buffer: The per cpu buffer of the @event
Steven Rostedt (Red Hat)d90fd772015-05-29 12:12:27 -04002698 * @event: the event to update
Steven Rostedt (VMware)cfc585a2020-01-14 16:27:51 -05002699 * @info: The info to update the @event with (contains length and delta)
Steven Rostedt (Red Hat)d90fd772015-05-29 12:12:27 -04002700 *
Steven Rostedt (VMware)cfc585a2020-01-14 16:27:51 -05002701 * Update the type and data fields of the @event. The length
Steven Rostedt (Red Hat)d90fd772015-05-29 12:12:27 -04002702 * is the actual size that is written to the ring buffer,
2703 * and with this, we can determine what to place into the
2704 * data field.
2705 */
Steven Rostedt (Red Hat)b7dc42f2015-09-03 08:57:12 -04002706static void
Steven Rostedt (Red Hat)d90fd772015-05-29 12:12:27 -04002707rb_update_event(struct ring_buffer_per_cpu *cpu_buffer,
2708 struct ring_buffer_event *event,
2709 struct rb_event_info *info)
2710{
2711 unsigned length = info->length;
2712 u64 delta = info->delta;
2713
2714 /*
2715 * If we need to add a timestamp, then we
Steven Rostedt (VMware)6167c202018-05-16 11:17:06 -04002716 * add it to the start of the reserved space.
Steven Rostedt (Red Hat)d90fd772015-05-29 12:12:27 -04002717 */
Steven Rostedt (VMware)74e87932020-06-30 12:47:56 -04002718 if (unlikely(info->add_timestamp))
2719 rb_add_timestamp(cpu_buffer, &event, info, &delta, &length);
Steven Rostedt (Red Hat)d90fd772015-05-29 12:12:27 -04002720
2721 event->time_delta = delta;
2722 length -= RB_EVNT_HDR_SIZE;
Steven Rostedt (VMware)adab66b2020-12-14 12:33:51 -05002723 if (length > RB_MAX_SMALL_DATA || RB_FORCE_8BYTE_ALIGNMENT) {
Steven Rostedt (Red Hat)d90fd772015-05-29 12:12:27 -04002724 event->type_len = 0;
2725 event->array[0] = length;
2726 } else
2727 event->type_len = DIV_ROUND_UP(length, RB_ALIGNMENT);
2728}
2729
2730static unsigned rb_calculate_event_length(unsigned length)
2731{
2732 struct ring_buffer_event event; /* Used only for sizeof array */
2733
2734 /* zero length can cause confusions */
2735 if (!length)
2736 length++;
2737
Steven Rostedt (VMware)adab66b2020-12-14 12:33:51 -05002738 if (length > RB_MAX_SMALL_DATA || RB_FORCE_8BYTE_ALIGNMENT)
Steven Rostedt (Red Hat)d90fd772015-05-29 12:12:27 -04002739 length += sizeof(event.array[0]);
2740
2741 length += RB_EVNT_HDR_SIZE;
Steven Rostedt (VMware)adab66b2020-12-14 12:33:51 -05002742 length = ALIGN(length, RB_ARCH_ALIGNMENT);
Steven Rostedt (Red Hat)d90fd772015-05-29 12:12:27 -04002743
2744 /*
2745 * In case the time delta is larger than the 27 bits for it
2746 * in the header, we need to add a timestamp. If another
2747 * event comes in when trying to discard this one to increase
2748 * the length, then the timestamp will be added in the allocated
2749 * space of this event. If length is bigger than the size needed
2750 * for the TIME_EXTEND, then padding has to be used. The events
2751 * length must be either RB_LEN_TIME_EXTEND, or greater than or equal
2752 * to RB_LEN_TIME_EXTEND + 8, as 8 is the minimum size for padding.
2753 * As length is a multiple of 4, we only need to worry if it
2754 * is 12 (RB_LEN_TIME_EXTEND + 4).
2755 */
2756 if (length == RB_LEN_TIME_EXTEND + RB_ALIGNMENT)
2757 length += RB_ALIGNMENT;
2758
2759 return length;
2760}
2761
Steven Rostedt (VMware)a389d862020-06-28 22:52:25 -04002762static u64 rb_time_delta(struct ring_buffer_event *event)
2763{
2764 switch (event->type_len) {
2765 case RINGBUF_TYPE_PADDING:
2766 return 0;
2767
2768 case RINGBUF_TYPE_TIME_EXTEND:
2769 return ring_buffer_event_time_stamp(event);
2770
2771 case RINGBUF_TYPE_TIME_STAMP:
2772 return 0;
2773
2774 case RINGBUF_TYPE_DATA:
2775 return event->time_delta;
2776 default:
2777 return 0;
2778 }
2779}
Steven Rostedt (Red Hat)9826b272015-05-28 17:36:45 -04002780
Steven Rostedt (Red Hat)a4543a22015-05-29 09:40:18 -04002781static inline int
2782rb_try_to_discard(struct ring_buffer_per_cpu *cpu_buffer,
Steven Rostedt (Red Hat)d90fd772015-05-29 12:12:27 -04002783 struct ring_buffer_event *event)
2784{
2785 unsigned long new_index, old_index;
2786 struct buffer_page *bpage;
2787 unsigned long index;
2788 unsigned long addr;
Steven Rostedt (VMware)a389d862020-06-28 22:52:25 -04002789 u64 write_stamp;
2790 u64 delta;
Steven Rostedt (Red Hat)d90fd772015-05-29 12:12:27 -04002791
2792 new_index = rb_event_index(event);
2793 old_index = new_index + rb_event_ts_length(event);
2794 addr = (unsigned long)event;
2795 addr &= PAGE_MASK;
2796
Steven Rostedt (Red Hat)85736362015-11-17 14:03:11 -05002797 bpage = READ_ONCE(cpu_buffer->tail_page);
Steven Rostedt (Red Hat)d90fd772015-05-29 12:12:27 -04002798
Steven Rostedt (VMware)a389d862020-06-28 22:52:25 -04002799 delta = rb_time_delta(event);
2800
Steven Rostedt (VMware)10464b42020-06-28 22:52:27 -04002801 if (!rb_time_read(&cpu_buffer->write_stamp, &write_stamp))
2802 return 0;
Steven Rostedt (VMware)a389d862020-06-28 22:52:25 -04002803
2804 /* Make sure the write stamp is read before testing the location */
2805 barrier();
2806
Steven Rostedt (Red Hat)d90fd772015-05-29 12:12:27 -04002807 if (bpage->page == (void *)addr && rb_page_write(bpage) == old_index) {
2808 unsigned long write_mask =
2809 local_read(&bpage->write) & ~RB_WRITE_MASK;
2810 unsigned long event_length = rb_event_length(event);
Steven Rostedt (VMware)a389d862020-06-28 22:52:25 -04002811
Steven Rostedt (VMware)a389d862020-06-28 22:52:25 -04002812 /* Something came in, can't discard */
Steven Rostedt (VMware)10464b42020-06-28 22:52:27 -04002813 if (!rb_time_cmpxchg(&cpu_buffer->write_stamp,
2814 write_stamp, write_stamp - delta))
Steven Rostedt (VMware)a389d862020-06-28 22:52:25 -04002815 return 0;
2816
2817 /*
Steven Rostedt (VMware)6f6be602021-03-03 18:03:52 -05002818 * It's possible that the event time delta is zero
2819 * (has the same time stamp as the previous event)
2820 * in which case write_stamp and before_stamp could
2821 * be the same. In such a case, force before_stamp
2822 * to be different than write_stamp. It doesn't
2823 * matter what it is, as long as its different.
2824 */
2825 if (!delta)
2826 rb_time_set(&cpu_buffer->before_stamp, 0);
2827
2828 /*
Steven Rostedt (VMware)a389d862020-06-28 22:52:25 -04002829 * If an event were to come in now, it would see that the
2830 * write_stamp and the before_stamp are different, and assume
2831 * that this event just added itself before updating
2832 * the write stamp. The interrupting event will fix the
2833 * write stamp for us, and use the before stamp as its delta.
2834 */
2835
Steven Rostedt (Red Hat)d90fd772015-05-29 12:12:27 -04002836 /*
2837 * This is on the tail page. It is possible that
2838 * a write could come in and move the tail page
2839 * and write to the next page. That is fine
2840 * because we just shorten what is on this page.
2841 */
2842 old_index += write_mask;
2843 new_index += write_mask;
2844 index = local_cmpxchg(&bpage->write, old_index, new_index);
2845 if (index == old_index) {
2846 /* update counters */
2847 local_sub(event_length, &cpu_buffer->entries_bytes);
2848 return 1;
2849 }
2850 }
2851
2852 /* could not discard */
2853 return 0;
2854}
2855
2856static void rb_start_commit(struct ring_buffer_per_cpu *cpu_buffer)
2857{
2858 local_inc(&cpu_buffer->committing);
2859 local_inc(&cpu_buffer->commits);
2860}
2861
Steven Rostedt (Red Hat)38e11df2016-11-23 20:42:31 -05002862static __always_inline void
Steven Rostedt (Red Hat)d90fd772015-05-29 12:12:27 -04002863rb_set_commit_to_write(struct ring_buffer_per_cpu *cpu_buffer)
2864{
2865 unsigned long max_count;
2866
2867 /*
2868 * We only race with interrupts and NMIs on this CPU.
2869 * If we own the commit event, then we can commit
2870 * all others that interrupted us, since the interruptions
2871 * are in stack format (they finish before they come
2872 * back to us). This allows us to do a simple loop to
2873 * assign the commit to the tail.
2874 */
2875 again:
2876 max_count = cpu_buffer->nr_pages * 100;
2877
Steven Rostedt (Red Hat)85736362015-11-17 14:03:11 -05002878 while (cpu_buffer->commit_page != READ_ONCE(cpu_buffer->tail_page)) {
Steven Rostedt (Red Hat)d90fd772015-05-29 12:12:27 -04002879 if (RB_WARN_ON(cpu_buffer, !(--max_count)))
2880 return;
2881 if (RB_WARN_ON(cpu_buffer,
2882 rb_is_reader_page(cpu_buffer->tail_page)))
2883 return;
2884 local_set(&cpu_buffer->commit_page->page->commit,
2885 rb_page_write(cpu_buffer->commit_page));
Qiujun Huang6689bed2020-12-25 22:03:56 +08002886 rb_inc_page(&cpu_buffer->commit_page);
Steven Rostedt (Red Hat)d90fd772015-05-29 12:12:27 -04002887 /* add barrier to keep gcc from optimizing too much */
2888 barrier();
2889 }
2890 while (rb_commit_index(cpu_buffer) !=
2891 rb_page_write(cpu_buffer->commit_page)) {
2892
2893 local_set(&cpu_buffer->commit_page->page->commit,
2894 rb_page_write(cpu_buffer->commit_page));
2895 RB_WARN_ON(cpu_buffer,
2896 local_read(&cpu_buffer->commit_page->page->commit) &
2897 ~RB_WRITE_MASK);
2898 barrier();
2899 }
2900
2901 /* again, keep gcc from optimizing */
2902 barrier();
2903
2904 /*
2905 * If an interrupt came in just after the first while loop
2906 * and pushed the tail page forward, we will be left with
2907 * a dangling commit that will never go forward.
2908 */
Steven Rostedt (Red Hat)85736362015-11-17 14:03:11 -05002909 if (unlikely(cpu_buffer->commit_page != READ_ONCE(cpu_buffer->tail_page)))
Steven Rostedt (Red Hat)d90fd772015-05-29 12:12:27 -04002910 goto again;
2911}
2912
Steven Rostedt (Red Hat)38e11df2016-11-23 20:42:31 -05002913static __always_inline void rb_end_commit(struct ring_buffer_per_cpu *cpu_buffer)
Steven Rostedt (Red Hat)d90fd772015-05-29 12:12:27 -04002914{
2915 unsigned long commits;
2916
2917 if (RB_WARN_ON(cpu_buffer,
2918 !local_read(&cpu_buffer->committing)))
2919 return;
2920
2921 again:
2922 commits = local_read(&cpu_buffer->commits);
2923 /* synchronize with interrupts */
2924 barrier();
2925 if (local_read(&cpu_buffer->committing) == 1)
2926 rb_set_commit_to_write(cpu_buffer);
2927
2928 local_dec(&cpu_buffer->committing);
2929
2930 /* synchronize with interrupts */
2931 barrier();
2932
2933 /*
2934 * Need to account for interrupts coming in between the
2935 * updating of the commit page and the clearing of the
2936 * committing counter.
2937 */
2938 if (unlikely(local_read(&cpu_buffer->commits) != commits) &&
2939 !local_read(&cpu_buffer->committing)) {
2940 local_inc(&cpu_buffer->committing);
2941 goto again;
2942 }
2943}
2944
2945static inline void rb_event_discard(struct ring_buffer_event *event)
2946{
Tom Zanussidc4e2802018-01-15 20:51:40 -06002947 if (extended_time(event))
Steven Rostedt (Red Hat)d90fd772015-05-29 12:12:27 -04002948 event = skip_time_extend(event);
2949
2950 /* array[0] holds the actual length for the discarded event */
2951 event->array[0] = rb_event_data_length(event) - RB_EVNT_HDR_SIZE;
2952 event->type_len = RINGBUF_TYPE_PADDING;
2953 /* time delta must be non zero */
2954 if (!event->time_delta)
2955 event->time_delta = 1;
2956}
2957
Steven Rostedt (Red Hat)d90fd772015-05-29 12:12:27 -04002958static void rb_commit(struct ring_buffer_per_cpu *cpu_buffer,
2959 struct ring_buffer_event *event)
2960{
2961 local_inc(&cpu_buffer->entries);
Steven Rostedt (Red Hat)d90fd772015-05-29 12:12:27 -04002962 rb_end_commit(cpu_buffer);
2963}
2964
2965static __always_inline void
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05002966rb_wakeups(struct trace_buffer *buffer, struct ring_buffer_per_cpu *cpu_buffer)
Steven Rostedt (Red Hat)d90fd772015-05-29 12:12:27 -04002967{
Steven Rostedt (VMware)03329f92018-11-29 21:38:42 -05002968 size_t nr_pages;
2969 size_t dirty;
2970 size_t full;
Steven Rostedt (Red Hat)d90fd772015-05-29 12:12:27 -04002971
2972 if (buffer->irq_work.waiters_pending) {
2973 buffer->irq_work.waiters_pending = false;
2974 /* irq_work_queue() supplies it's own memory barriers */
2975 irq_work_queue(&buffer->irq_work.work);
2976 }
2977
2978 if (cpu_buffer->irq_work.waiters_pending) {
2979 cpu_buffer->irq_work.waiters_pending = false;
2980 /* irq_work_queue() supplies it's own memory barriers */
2981 irq_work_queue(&cpu_buffer->irq_work.work);
2982 }
2983
Steven Rostedt (VMware)03329f92018-11-29 21:38:42 -05002984 if (cpu_buffer->last_pages_touch == local_read(&cpu_buffer->pages_touched))
2985 return;
Steven Rostedt (Red Hat)d90fd772015-05-29 12:12:27 -04002986
Steven Rostedt (VMware)03329f92018-11-29 21:38:42 -05002987 if (cpu_buffer->reader_page == cpu_buffer->commit_page)
2988 return;
Steven Rostedt (VMware)2c2b0a72018-11-29 20:32:26 -05002989
Steven Rostedt (VMware)03329f92018-11-29 21:38:42 -05002990 if (!cpu_buffer->irq_work.full_waiters_pending)
2991 return;
Steven Rostedt (VMware)2c2b0a72018-11-29 20:32:26 -05002992
Steven Rostedt (VMware)03329f92018-11-29 21:38:42 -05002993 cpu_buffer->last_pages_touch = local_read(&cpu_buffer->pages_touched);
2994
2995 full = cpu_buffer->shortest_full;
2996 nr_pages = cpu_buffer->nr_pages;
2997 dirty = ring_buffer_nr_dirty_pages(buffer, cpu_buffer->cpu);
2998 if (full && nr_pages && (dirty * 100) <= full * nr_pages)
2999 return;
3000
3001 cpu_buffer->irq_work.wakeup_full = true;
3002 cpu_buffer->irq_work.full_waiters_pending = false;
3003 /* irq_work_queue() supplies it's own memory barriers */
3004 irq_work_queue(&cpu_buffer->irq_work.work);
Steven Rostedt (Red Hat)d90fd772015-05-29 12:12:27 -04003005}
3006
Steven Rostedt (VMware)28575c62020-11-02 14:43:10 -05003007#ifdef CONFIG_RING_BUFFER_RECORD_RECURSION
3008# define do_ring_buffer_record_recursion() \
3009 do_ftrace_record_recursion(_THIS_IP_, _RET_IP_)
3010#else
3011# define do_ring_buffer_record_recursion() do { } while (0)
3012#endif
3013
Steven Rostedt (Red Hat)d90fd772015-05-29 12:12:27 -04003014/*
3015 * The lock and unlock are done within a preempt disable section.
3016 * The current_context per_cpu variable can only be modified
3017 * by the current task between lock and unlock. But it can
Steven Rostedt (VMware)a0e3a182018-01-15 10:47:09 -05003018 * be modified more than once via an interrupt. To pass this
3019 * information from the lock to the unlock without having to
3020 * access the 'in_interrupt()' functions again (which do show
3021 * a bit of overhead in something as critical as function tracing,
3022 * we use a bitmask trick.
Steven Rostedt (Red Hat)d90fd772015-05-29 12:12:27 -04003023 *
Steven Rostedt (VMware)b02414c2020-11-02 15:31:27 -05003024 * bit 1 = NMI context
3025 * bit 2 = IRQ context
3026 * bit 3 = SoftIRQ context
3027 * bit 4 = normal context.
Steven Rostedt (Red Hat)d90fd772015-05-29 12:12:27 -04003028 *
Steven Rostedt (VMware)a0e3a182018-01-15 10:47:09 -05003029 * This works because this is the order of contexts that can
3030 * preempt other contexts. A SoftIRQ never preempts an IRQ
3031 * context.
3032 *
3033 * When the context is determined, the corresponding bit is
3034 * checked and set (if it was set, then a recursion of that context
3035 * happened).
3036 *
3037 * On unlock, we need to clear this bit. To do so, just subtract
3038 * 1 from the current_context and AND it to itself.
3039 *
3040 * (binary)
3041 * 101 - 1 = 100
3042 * 101 & 100 = 100 (clearing bit zero)
3043 *
3044 * 1010 - 1 = 1001
3045 * 1010 & 1001 = 1000 (clearing bit 1)
3046 *
3047 * The least significant bit can be cleared this way, and it
3048 * just so happens that it is the same bit corresponding to
3049 * the current context.
Steven Rostedt (VMware)b02414c2020-11-02 15:31:27 -05003050 *
3051 * Now the TRANSITION bit breaks the above slightly. The TRANSITION bit
3052 * is set when a recursion is detected at the current context, and if
3053 * the TRANSITION bit is already set, it will fail the recursion.
3054 * This is needed because there's a lag between the changing of
3055 * interrupt context and updating the preempt count. In this case,
3056 * a false positive will be found. To handle this, one extra recursion
3057 * is allowed, and this is done by the TRANSITION bit. If the TRANSITION
3058 * bit is already set, then it is considered a recursion and the function
3059 * ends. Otherwise, the TRANSITION bit is set, and that bit is returned.
3060 *
3061 * On the trace_recursive_unlock(), the TRANSITION bit will be the first
3062 * to be cleared. Even if it wasn't the context that set it. That is,
3063 * if an interrupt comes in while NORMAL bit is set and the ring buffer
3064 * is called before preempt_count() is updated, since the check will
3065 * be on the NORMAL bit, the TRANSITION bit will then be set. If an
3066 * NMI then comes in, it will set the NMI bit, but when the NMI code
3067 * does the trace_recursive_unlock() it will clear the TRANSTION bit
3068 * and leave the NMI bit set. But this is fine, because the interrupt
3069 * code that set the TRANSITION bit will then clear the NMI bit when it
3070 * calls trace_recursive_unlock(). If another NMI comes in, it will
3071 * set the TRANSITION bit and continue.
3072 *
3073 * Note: The TRANSITION bit only handles a single transition between context.
Steven Rostedt (Red Hat)d90fd772015-05-29 12:12:27 -04003074 */
3075
3076static __always_inline int
3077trace_recursive_lock(struct ring_buffer_per_cpu *cpu_buffer)
3078{
Steven Rostedt (VMware)a0e3a182018-01-15 10:47:09 -05003079 unsigned int val = cpu_buffer->current_context;
3080 unsigned long pc = preempt_count();
3081 int bit;
3082
3083 if (!(pc & (NMI_MASK | HARDIRQ_MASK | SOFTIRQ_OFFSET)))
3084 bit = RB_CTX_NORMAL;
3085 else
3086 bit = pc & NMI_MASK ? RB_CTX_NMI :
Steven Rostedt (VMware)0164e0d2018-01-18 15:42:09 -05003087 pc & HARDIRQ_MASK ? RB_CTX_IRQ : RB_CTX_SOFTIRQ;
Steven Rostedt (VMware)a0e3a182018-01-15 10:47:09 -05003088
Steven Rostedt (VMware)b02414c2020-11-02 15:31:27 -05003089 if (unlikely(val & (1 << (bit + cpu_buffer->nest)))) {
3090 /*
3091 * It is possible that this was called by transitioning
3092 * between interrupt context, and preempt_count() has not
3093 * been updated yet. In this case, use the TRANSITION bit.
3094 */
3095 bit = RB_CTX_TRANSITION;
Steven Rostedt (VMware)28575c62020-11-02 14:43:10 -05003096 if (val & (1 << (bit + cpu_buffer->nest))) {
3097 do_ring_buffer_record_recursion();
Steven Rostedt (VMware)b02414c2020-11-02 15:31:27 -05003098 return 1;
Steven Rostedt (VMware)28575c62020-11-02 14:43:10 -05003099 }
Steven Rostedt (VMware)b02414c2020-11-02 15:31:27 -05003100 }
Steven Rostedt (Red Hat)d90fd772015-05-29 12:12:27 -04003101
Steven Rostedt (VMware)8e012062018-02-07 17:26:32 -05003102 val |= (1 << (bit + cpu_buffer->nest));
Steven Rostedt (VMware)a0e3a182018-01-15 10:47:09 -05003103 cpu_buffer->current_context = val;
Steven Rostedt (Red Hat)d90fd772015-05-29 12:12:27 -04003104
3105 return 0;
3106}
3107
3108static __always_inline void
3109trace_recursive_unlock(struct ring_buffer_per_cpu *cpu_buffer)
3110{
Steven Rostedt (VMware)8e012062018-02-07 17:26:32 -05003111 cpu_buffer->current_context &=
3112 cpu_buffer->current_context - (1 << cpu_buffer->nest);
3113}
3114
Steven Rostedt (VMware)b02414c2020-11-02 15:31:27 -05003115/* The recursive locking above uses 5 bits */
3116#define NESTED_BITS 5
Steven Rostedt (VMware)8e012062018-02-07 17:26:32 -05003117
3118/**
3119 * ring_buffer_nest_start - Allow to trace while nested
3120 * @buffer: The ring buffer to modify
3121 *
Steven Rostedt (VMware)6167c202018-05-16 11:17:06 -04003122 * The ring buffer has a safety mechanism to prevent recursion.
Steven Rostedt (VMware)8e012062018-02-07 17:26:32 -05003123 * But there may be a case where a trace needs to be done while
3124 * tracing something else. In this case, calling this function
3125 * will allow this function to nest within a currently active
3126 * ring_buffer_lock_reserve().
3127 *
3128 * Call this function before calling another ring_buffer_lock_reserve() and
3129 * call ring_buffer_nest_end() after the nested ring_buffer_unlock_commit().
3130 */
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05003131void ring_buffer_nest_start(struct trace_buffer *buffer)
Steven Rostedt (VMware)8e012062018-02-07 17:26:32 -05003132{
3133 struct ring_buffer_per_cpu *cpu_buffer;
3134 int cpu;
3135
3136 /* Enabled by ring_buffer_nest_end() */
3137 preempt_disable_notrace();
3138 cpu = raw_smp_processor_id();
3139 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt (VMware)6167c202018-05-16 11:17:06 -04003140 /* This is the shift value for the above recursive locking */
Steven Rostedt (VMware)8e012062018-02-07 17:26:32 -05003141 cpu_buffer->nest += NESTED_BITS;
3142}
3143
3144/**
3145 * ring_buffer_nest_end - Allow to trace while nested
3146 * @buffer: The ring buffer to modify
3147 *
3148 * Must be called after ring_buffer_nest_start() and after the
3149 * ring_buffer_unlock_commit().
3150 */
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05003151void ring_buffer_nest_end(struct trace_buffer *buffer)
Steven Rostedt (VMware)8e012062018-02-07 17:26:32 -05003152{
3153 struct ring_buffer_per_cpu *cpu_buffer;
3154 int cpu;
3155
3156 /* disabled by ring_buffer_nest_start() */
3157 cpu = raw_smp_processor_id();
3158 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt (VMware)6167c202018-05-16 11:17:06 -04003159 /* This is the shift value for the above recursive locking */
Steven Rostedt (VMware)8e012062018-02-07 17:26:32 -05003160 cpu_buffer->nest -= NESTED_BITS;
3161 preempt_enable_notrace();
Steven Rostedt (Red Hat)d90fd772015-05-29 12:12:27 -04003162}
3163
3164/**
3165 * ring_buffer_unlock_commit - commit a reserved
3166 * @buffer: The buffer to commit to
3167 * @event: The event pointer to commit.
3168 *
3169 * This commits the data to the ring buffer, and releases any locks held.
3170 *
3171 * Must be paired with ring_buffer_lock_reserve.
3172 */
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05003173int ring_buffer_unlock_commit(struct trace_buffer *buffer,
Steven Rostedt (Red Hat)d90fd772015-05-29 12:12:27 -04003174 struct ring_buffer_event *event)
3175{
3176 struct ring_buffer_per_cpu *cpu_buffer;
3177 int cpu = raw_smp_processor_id();
3178
3179 cpu_buffer = buffer->buffers[cpu];
3180
3181 rb_commit(cpu_buffer, event);
3182
3183 rb_wakeups(buffer, cpu_buffer);
3184
3185 trace_recursive_unlock(cpu_buffer);
3186
3187 preempt_enable_notrace();
3188
3189 return 0;
3190}
3191EXPORT_SYMBOL_GPL(ring_buffer_unlock_commit);
Steven Rostedt (Red Hat)a4543a22015-05-29 09:40:18 -04003192
Steven Rostedt (VMware)5b7be9c2020-11-30 23:37:33 -05003193/* Special value to validate all deltas on a page. */
3194#define CHECK_FULL_PAGE 1L
3195
3196#ifdef CONFIG_RING_BUFFER_VALIDATE_TIME_DELTAS
3197static void dump_buffer_page(struct buffer_data_page *bpage,
3198 struct rb_event_info *info,
3199 unsigned long tail)
3200{
3201 struct ring_buffer_event *event;
3202 u64 ts, delta;
3203 int e;
3204
3205 ts = bpage->time_stamp;
3206 pr_warn(" [%lld] PAGE TIME STAMP\n", ts);
3207
3208 for (e = 0; e < tail; e += rb_event_length(event)) {
3209
3210 event = (struct ring_buffer_event *)(bpage->data + e);
3211
3212 switch (event->type_len) {
3213
3214 case RINGBUF_TYPE_TIME_EXTEND:
3215 delta = ring_buffer_event_time_stamp(event);
3216 ts += delta;
3217 pr_warn(" [%lld] delta:%lld TIME EXTEND\n", ts, delta);
3218 break;
3219
3220 case RINGBUF_TYPE_TIME_STAMP:
3221 delta = ring_buffer_event_time_stamp(event);
3222 ts = delta;
3223 pr_warn(" [%lld] absolute:%lld TIME STAMP\n", ts, delta);
3224 break;
3225
3226 case RINGBUF_TYPE_PADDING:
3227 ts += event->time_delta;
3228 pr_warn(" [%lld] delta:%d PADDING\n", ts, event->time_delta);
3229 break;
3230
3231 case RINGBUF_TYPE_DATA:
3232 ts += event->time_delta;
3233 pr_warn(" [%lld] delta:%d\n", ts, event->time_delta);
3234 break;
3235
3236 default:
3237 break;
3238 }
3239 }
3240}
3241
3242static DEFINE_PER_CPU(atomic_t, checking);
3243static atomic_t ts_dump;
3244
3245/*
3246 * Check if the current event time stamp matches the deltas on
3247 * the buffer page.
3248 */
3249static void check_buffer(struct ring_buffer_per_cpu *cpu_buffer,
3250 struct rb_event_info *info,
3251 unsigned long tail)
3252{
3253 struct ring_buffer_event *event;
3254 struct buffer_data_page *bpage;
3255 u64 ts, delta;
3256 bool full = false;
3257 int e;
3258
3259 bpage = info->tail_page->page;
3260
3261 if (tail == CHECK_FULL_PAGE) {
3262 full = true;
3263 tail = local_read(&bpage->commit);
3264 } else if (info->add_timestamp &
3265 (RB_ADD_STAMP_FORCE | RB_ADD_STAMP_ABSOLUTE)) {
3266 /* Ignore events with absolute time stamps */
3267 return;
3268 }
3269
3270 /*
3271 * Do not check the first event (skip possible extends too).
3272 * Also do not check if previous events have not been committed.
3273 */
3274 if (tail <= 8 || tail > local_read(&bpage->commit))
3275 return;
3276
3277 /*
3278 * If this interrupted another event,
3279 */
3280 if (atomic_inc_return(this_cpu_ptr(&checking)) != 1)
3281 goto out;
3282
3283 ts = bpage->time_stamp;
3284
3285 for (e = 0; e < tail; e += rb_event_length(event)) {
3286
3287 event = (struct ring_buffer_event *)(bpage->data + e);
3288
3289 switch (event->type_len) {
3290
3291 case RINGBUF_TYPE_TIME_EXTEND:
3292 delta = ring_buffer_event_time_stamp(event);
3293 ts += delta;
3294 break;
3295
3296 case RINGBUF_TYPE_TIME_STAMP:
3297 delta = ring_buffer_event_time_stamp(event);
3298 ts = delta;
3299 break;
3300
3301 case RINGBUF_TYPE_PADDING:
3302 if (event->time_delta == 1)
3303 break;
3304 /* fall through */
3305 case RINGBUF_TYPE_DATA:
3306 ts += event->time_delta;
3307 break;
3308
3309 default:
3310 RB_WARN_ON(cpu_buffer, 1);
3311 }
3312 }
3313 if ((full && ts > info->ts) ||
3314 (!full && ts + info->delta != info->ts)) {
3315 /* If another report is happening, ignore this one */
3316 if (atomic_inc_return(&ts_dump) != 1) {
3317 atomic_dec(&ts_dump);
3318 goto out;
3319 }
3320 atomic_inc(&cpu_buffer->record_disabled);
3321 pr_warn("[CPU: %d]TIME DOES NOT MATCH expected:%lld actual:%lld delta:%lld after:%lld\n",
3322 cpu_buffer->cpu,
3323 ts + info->delta, info->ts, info->delta, info->after);
3324 dump_buffer_page(bpage, info, tail);
3325 atomic_dec(&ts_dump);
3326 /* Do not re-enable checking */
3327 return;
3328 }
3329out:
3330 atomic_dec(this_cpu_ptr(&checking));
3331}
3332#else
3333static inline void check_buffer(struct ring_buffer_per_cpu *cpu_buffer,
3334 struct rb_event_info *info,
3335 unsigned long tail)
3336{
3337}
3338#endif /* CONFIG_RING_BUFFER_VALIDATE_TIME_DELTAS */
3339
Steven Rostedt6634ff22009-05-06 15:30:07 -04003340static struct ring_buffer_event *
3341__rb_reserve_next(struct ring_buffer_per_cpu *cpu_buffer,
Steven Rostedt (Red Hat)fcc742e2015-05-28 17:13:14 -04003342 struct rb_event_info *info)
Steven Rostedt6634ff22009-05-06 15:30:07 -04003343{
Steven Rostedt6634ff22009-05-06 15:30:07 -04003344 struct ring_buffer_event *event;
Steven Rostedt (Red Hat)fcc742e2015-05-28 17:13:14 -04003345 struct buffer_page *tail_page;
Steven Rostedt (VMware)a389d862020-06-28 22:52:25 -04003346 unsigned long tail, write, w;
Steven Rostedt (VMware)10464b42020-06-28 22:52:27 -04003347 bool a_ok;
3348 bool b_ok;
Steven Rostedt69d1b832010-10-07 18:18:05 -04003349
Steven Rostedt (Red Hat)85736362015-11-17 14:03:11 -05003350 /* Don't let the compiler play games with cpu_buffer->tail_page */
3351 tail_page = info->tail_page = READ_ONCE(cpu_buffer->tail_page);
Steven Rostedt (VMware)a389d862020-06-28 22:52:25 -04003352
3353 /*A*/ w = local_read(&tail_page->write) & RB_WRITE_MASK;
3354 barrier();
Steven Rostedt (VMware)58fbc3c2020-06-30 08:59:26 -04003355 b_ok = rb_time_read(&cpu_buffer->before_stamp, &info->before);
3356 a_ok = rb_time_read(&cpu_buffer->write_stamp, &info->after);
Steven Rostedt (VMware)a389d862020-06-28 22:52:25 -04003357 barrier();
3358 info->ts = rb_time_stamp(cpu_buffer->buffer);
3359
Steven Rostedt (VMware)58fbc3c2020-06-30 08:59:26 -04003360 if ((info->add_timestamp & RB_ADD_STAMP_ABSOLUTE)) {
Steven Rostedt (VMware)a389d862020-06-28 22:52:25 -04003361 info->delta = info->ts;
Steven Rostedt (VMware)a389d862020-06-28 22:52:25 -04003362 } else {
Steven Rostedt (VMware)58fbc3c2020-06-30 08:59:26 -04003363 /*
3364 * If interrupting an event time update, we may need an
3365 * absolute timestamp.
3366 * Don't bother if this is the start of a new page (w == 0).
3367 */
3368 if (unlikely(!a_ok || !b_ok || (info->before != info->after && w))) {
3369 info->add_timestamp |= RB_ADD_STAMP_FORCE | RB_ADD_STAMP_EXTEND;
3370 info->length += RB_LEN_TIME_EXTEND;
3371 } else {
3372 info->delta = info->ts - info->after;
3373 if (unlikely(test_time_stamp(info->delta))) {
3374 info->add_timestamp |= RB_ADD_STAMP_EXTEND;
3375 info->length += RB_LEN_TIME_EXTEND;
3376 }
Steven Rostedt (VMware)10464b42020-06-28 22:52:27 -04003377 }
Steven Rostedt (VMware)7c4b4a52020-06-28 22:52:26 -04003378 }
Steven Rostedt (VMware)a389d862020-06-28 22:52:25 -04003379
Steven Rostedt (VMware)10464b42020-06-28 22:52:27 -04003380 /*B*/ rb_time_set(&cpu_buffer->before_stamp, info->ts);
Steven Rostedt (VMware)a389d862020-06-28 22:52:25 -04003381
3382 /*C*/ write = local_add_return(info->length, &tail_page->write);
Steven Rostedt77ae3652009-03-27 11:00:29 -04003383
3384 /* set write to only the index of the write */
3385 write &= RB_WRITE_MASK;
Steven Rostedt (VMware)a389d862020-06-28 22:52:25 -04003386
Steven Rostedt (Red Hat)fcc742e2015-05-28 17:13:14 -04003387 tail = write - info->length;
Steven Rostedt6634ff22009-05-06 15:30:07 -04003388
Steven Rostedt (VMware)a389d862020-06-28 22:52:25 -04003389 /* See if we shot pass the end of this buffer page */
3390 if (unlikely(write > BUF_PAGE_SIZE)) {
Steven Rostedt (VMware)68e10d52020-11-30 23:16:03 -05003391 /* before and after may now different, fix it up*/
3392 b_ok = rb_time_read(&cpu_buffer->before_stamp, &info->before);
3393 a_ok = rb_time_read(&cpu_buffer->write_stamp, &info->after);
3394 if (a_ok && b_ok && info->before != info->after)
3395 (void)rb_time_cmpxchg(&cpu_buffer->before_stamp,
3396 info->before, info->after);
Steven Rostedt (VMware)5b7be9c2020-11-30 23:37:33 -05003397 if (a_ok && b_ok)
3398 check_buffer(cpu_buffer, info, CHECK_FULL_PAGE);
Steven Rostedt (VMware)a389d862020-06-28 22:52:25 -04003399 return rb_move_tail(cpu_buffer, tail, info);
3400 }
3401
3402 if (likely(tail == w)) {
3403 u64 save_before;
Steven Rostedt (VMware)10464b42020-06-28 22:52:27 -04003404 bool s_ok;
Steven Rostedt (VMware)a389d862020-06-28 22:52:25 -04003405
3406 /* Nothing interrupted us between A and C */
Steven Rostedt (VMware)10464b42020-06-28 22:52:27 -04003407 /*D*/ rb_time_set(&cpu_buffer->write_stamp, info->ts);
Steven Rostedt (VMware)a389d862020-06-28 22:52:25 -04003408 barrier();
Steven Rostedt (VMware)10464b42020-06-28 22:52:27 -04003409 /*E*/ s_ok = rb_time_read(&cpu_buffer->before_stamp, &save_before);
3410 RB_WARN_ON(cpu_buffer, !s_ok);
Steven Rostedt (VMware)7c4b4a52020-06-28 22:52:26 -04003411 if (likely(!(info->add_timestamp &
3412 (RB_ADD_STAMP_FORCE | RB_ADD_STAMP_ABSOLUTE))))
Steven Rostedt (VMware)a389d862020-06-28 22:52:25 -04003413 /* This did not interrupt any time update */
Steven Rostedt (VMware)58fbc3c2020-06-30 08:59:26 -04003414 info->delta = info->ts - info->after;
Steven Rostedt (VMware)a389d862020-06-28 22:52:25 -04003415 else
Qiujun Huang82db9092020-10-14 23:27:49 +08003416 /* Just use full timestamp for interrupting event */
Steven Rostedt (VMware)a389d862020-06-28 22:52:25 -04003417 info->delta = info->ts;
3418 barrier();
Steven Rostedt (VMware)5b7be9c2020-11-30 23:37:33 -05003419 check_buffer(cpu_buffer, info, tail);
Steven Rostedt (VMware)a389d862020-06-28 22:52:25 -04003420 if (unlikely(info->ts != save_before)) {
3421 /* SLOW PATH - Interrupted between C and E */
3422
Steven Rostedt (VMware)58fbc3c2020-06-30 08:59:26 -04003423 a_ok = rb_time_read(&cpu_buffer->write_stamp, &info->after);
Steven Rostedt (VMware)10464b42020-06-28 22:52:27 -04003424 RB_WARN_ON(cpu_buffer, !a_ok);
3425
Steven Rostedt (VMware)a389d862020-06-28 22:52:25 -04003426 /* Write stamp must only go forward */
Steven Rostedt (VMware)58fbc3c2020-06-30 08:59:26 -04003427 if (save_before > info->after) {
Steven Rostedt (VMware)a389d862020-06-28 22:52:25 -04003428 /*
3429 * We do not care about the result, only that
3430 * it gets updated atomically.
3431 */
Steven Rostedt (VMware)58fbc3c2020-06-30 08:59:26 -04003432 (void)rb_time_cmpxchg(&cpu_buffer->write_stamp,
3433 info->after, save_before);
Steven Rostedt (VMware)a389d862020-06-28 22:52:25 -04003434 }
3435 }
3436 } else {
3437 u64 ts;
3438 /* SLOW PATH - Interrupted between A and C */
Steven Rostedt (VMware)58fbc3c2020-06-30 08:59:26 -04003439 a_ok = rb_time_read(&cpu_buffer->write_stamp, &info->after);
Steven Rostedt (VMware)10464b42020-06-28 22:52:27 -04003440 /* Was interrupted before here, write_stamp must be valid */
3441 RB_WARN_ON(cpu_buffer, !a_ok);
Steven Rostedt (VMware)a389d862020-06-28 22:52:25 -04003442 ts = rb_time_stamp(cpu_buffer->buffer);
3443 barrier();
3444 /*E*/ if (write == (local_read(&tail_page->write) & RB_WRITE_MASK) &&
Andrea Righi8785f512020-11-28 10:15:17 +01003445 info->after < ts &&
3446 rb_time_cmpxchg(&cpu_buffer->write_stamp,
3447 info->after, ts)) {
Steven Rostedt (VMware)a389d862020-06-28 22:52:25 -04003448 /* Nothing came after this event between C and E */
Steven Rostedt (VMware)58fbc3c2020-06-30 08:59:26 -04003449 info->delta = ts - info->after;
Steven Rostedt (VMware)a389d862020-06-28 22:52:25 -04003450 info->ts = ts;
3451 } else {
3452 /*
Qiujun Huang82db9092020-10-14 23:27:49 +08003453 * Interrupted between C and E:
Steven Rostedt (VMware)a389d862020-06-28 22:52:25 -04003454 * Lost the previous events time stamp. Just set the
3455 * delta to zero, and this will be the same time as
3456 * the event this event interrupted. And the events that
3457 * came after this will still be correct (as they would
3458 * have built their delta on the previous event.
3459 */
3460 info->delta = 0;
3461 }
Steven Rostedt (VMware)7c4b4a52020-06-28 22:52:26 -04003462 info->add_timestamp &= ~RB_ADD_STAMP_FORCE;
Steven Rostedt (VMware)a389d862020-06-28 22:52:25 -04003463 }
3464
Steven Rostedt (Red Hat)b7dc42f2015-09-03 08:57:12 -04003465 /*
3466 * If this is the first commit on the page, then it has the same
3467 * timestamp as the page itself.
3468 */
Steven Rostedt (VMware)7c4b4a52020-06-28 22:52:26 -04003469 if (unlikely(!tail && !(info->add_timestamp &
3470 (RB_ADD_STAMP_FORCE | RB_ADD_STAMP_ABSOLUTE))))
Steven Rostedt (Red Hat)b7dc42f2015-09-03 08:57:12 -04003471 info->delta = 0;
3472
Steven Rostedt6634ff22009-05-06 15:30:07 -04003473 /* We reserved something on the buffer */
Steven Rostedt (Red Hat)b7dc42f2015-09-03 08:57:12 -04003474
Steven Rostedt6634ff22009-05-06 15:30:07 -04003475 event = __rb_page_index(tail_page, tail);
Steven Rostedt (Red Hat)fcc742e2015-05-28 17:13:14 -04003476 rb_update_event(cpu_buffer, event, info);
Steven Rostedt6634ff22009-05-06 15:30:07 -04003477
Steven Rostedt69d1b832010-10-07 18:18:05 -04003478 local_inc(&tail_page->entries);
Steven Rostedt6634ff22009-05-06 15:30:07 -04003479
Steven Rostedt (Red Hat)b7dc42f2015-09-03 08:57:12 -04003480 /*
3481 * If this is the first commit on the page, then update
3482 * its timestamp.
3483 */
Steven Rostedt (VMware)75b21c62020-06-30 09:04:35 -04003484 if (unlikely(!tail))
Steven Rostedt (Red Hat)b7dc42f2015-09-03 08:57:12 -04003485 tail_page->page->time_stamp = info->ts;
3486
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -07003487 /* account for these added bytes */
Steven Rostedt (Red Hat)fcc742e2015-05-28 17:13:14 -04003488 local_add(info->length, &cpu_buffer->entries_bytes);
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -07003489
Steven Rostedt6634ff22009-05-06 15:30:07 -04003490 return event;
3491}
3492
Steven Rostedt (Red Hat)fa7ffb32016-11-23 11:36:30 -05003493static __always_inline struct ring_buffer_event *
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05003494rb_reserve_next_event(struct trace_buffer *buffer,
Steven Rostedt62f0b3e2009-09-04 14:11:34 -04003495 struct ring_buffer_per_cpu *cpu_buffer,
Steven Rostedt1cd8d732009-05-11 14:08:09 -04003496 unsigned long length)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003497{
3498 struct ring_buffer_event *event;
Steven Rostedt (Red Hat)fcc742e2015-05-28 17:13:14 -04003499 struct rb_event_info info;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003500 int nr_loops = 0;
Steven Rostedt (VMware)58fbc3c2020-06-30 08:59:26 -04003501 int add_ts_default;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003502
Steven Rostedtfa743952009-06-16 12:37:57 -04003503 rb_start_commit(cpu_buffer);
Steven Rostedt (VMware)a389d862020-06-28 22:52:25 -04003504 /* The commit page can not change after this */
Steven Rostedtfa743952009-06-16 12:37:57 -04003505
Steven Rostedt85bac322009-09-04 14:24:40 -04003506#ifdef CONFIG_RING_BUFFER_ALLOW_SWAP
Steven Rostedt62f0b3e2009-09-04 14:11:34 -04003507 /*
3508 * Due to the ability to swap a cpu buffer from a buffer
3509 * it is possible it was swapped before we committed.
3510 * (committing stops a swap). We check for it here and
3511 * if it happened, we have to fail the write.
3512 */
3513 barrier();
Mark Rutland6aa7de02017-10-23 14:07:29 -07003514 if (unlikely(READ_ONCE(cpu_buffer->buffer) != buffer)) {
Steven Rostedt62f0b3e2009-09-04 14:11:34 -04003515 local_dec(&cpu_buffer->committing);
3516 local_dec(&cpu_buffer->commits);
3517 return NULL;
3518 }
Steven Rostedt85bac322009-09-04 14:24:40 -04003519#endif
Steven Rostedt (Red Hat)b7dc42f2015-09-03 08:57:12 -04003520
Steven Rostedt (Red Hat)fcc742e2015-05-28 17:13:14 -04003521 info.length = rb_calculate_event_length(length);
Steven Rostedt (VMware)58fbc3c2020-06-30 08:59:26 -04003522
3523 if (ring_buffer_time_stamp_abs(cpu_buffer->buffer)) {
3524 add_ts_default = RB_ADD_STAMP_ABSOLUTE;
3525 info.length += RB_LEN_TIME_EXTEND;
3526 } else {
3527 add_ts_default = RB_ADD_STAMP_NONE;
3528 }
3529
Steven Rostedt (Red Hat)a4543a22015-05-29 09:40:18 -04003530 again:
Steven Rostedt (VMware)58fbc3c2020-06-30 08:59:26 -04003531 info.add_timestamp = add_ts_default;
Steven Rostedt (Red Hat)b7dc42f2015-09-03 08:57:12 -04003532 info.delta = 0;
3533
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003534 /*
3535 * We allow for interrupts to reenter here and do a trace.
3536 * If one does, it will cause this original code to loop
3537 * back here. Even with heavy interrupts happening, this
3538 * should only happen a few times in a row. If this happens
3539 * 1000 times in a row, there must be either an interrupt
3540 * storm or we have something buggy.
3541 * Bail!
3542 */
Steven Rostedt3e89c7bb2008-11-11 15:28:41 -05003543 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 1000))
Steven Rostedtfa743952009-06-16 12:37:57 -04003544 goto out_fail;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003545
Steven Rostedt (Red Hat)fcc742e2015-05-28 17:13:14 -04003546 event = __rb_reserve_next(cpu_buffer, &info);
3547
Steven Rostedt (Red Hat)bd1b7cd2015-11-23 17:35:24 -05003548 if (unlikely(PTR_ERR(event) == -EAGAIN)) {
Steven Rostedt (VMware)58fbc3c2020-06-30 08:59:26 -04003549 if (info.add_timestamp & (RB_ADD_STAMP_FORCE | RB_ADD_STAMP_EXTEND))
Steven Rostedt (Red Hat)bd1b7cd2015-11-23 17:35:24 -05003550 info.length -= RB_LEN_TIME_EXTEND;
Steven Rostedtbf41a152008-10-04 02:00:59 -04003551 goto again;
Steven Rostedt (Red Hat)bd1b7cd2015-11-23 17:35:24 -05003552 }
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003553
Steven Rostedt (VMware)a389d862020-06-28 22:52:25 -04003554 if (likely(event))
3555 return event;
Steven Rostedtfa743952009-06-16 12:37:57 -04003556 out_fail:
3557 rb_end_commit(cpu_buffer);
3558 return NULL;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003559}
3560
3561/**
3562 * ring_buffer_lock_reserve - reserve a part of the buffer
3563 * @buffer: the ring buffer to reserve from
3564 * @length: the length of the data to reserve (excluding event header)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003565 *
Steven Rostedt (VMware)6167c202018-05-16 11:17:06 -04003566 * Returns a reserved event on the ring buffer to copy directly to.
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003567 * The user of this interface will need to get the body to write into
3568 * and can use the ring_buffer_event_data() interface.
3569 *
3570 * The length is the length of the data needed, not the event length
3571 * which also includes the event header.
3572 *
3573 * Must be paired with ring_buffer_unlock_commit, unless NULL is returned.
3574 * If NULL is returned, then nothing has been allocated or locked.
3575 */
3576struct ring_buffer_event *
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05003577ring_buffer_lock_reserve(struct trace_buffer *buffer, unsigned long length)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003578{
3579 struct ring_buffer_per_cpu *cpu_buffer;
3580 struct ring_buffer_event *event;
Steven Rostedt5168ae52010-06-03 09:36:50 -04003581 int cpu;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003582
Steven Rostedtbf41a152008-10-04 02:00:59 -04003583 /* If we are tracing schedule, we don't want to recurse */
Steven Rostedt5168ae52010-06-03 09:36:50 -04003584 preempt_disable_notrace();
Steven Rostedtbf41a152008-10-04 02:00:59 -04003585
Steven Rostedt (Red Hat)3205f802015-05-21 17:39:29 -04003586 if (unlikely(atomic_read(&buffer->record_disabled)))
Steven Rostedt (Red Hat)58a09ec2015-05-27 10:27:47 -04003587 goto out;
Steven Rostedt261842b2009-04-16 21:41:52 -04003588
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003589 cpu = raw_smp_processor_id();
3590
Steven Rostedt (Red Hat)3205f802015-05-21 17:39:29 -04003591 if (unlikely(!cpumask_test_cpu(cpu, buffer->cpumask)))
Steven Rostedtd7690412008-10-01 00:29:53 -04003592 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003593
3594 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003595
Steven Rostedt (Red Hat)3205f802015-05-21 17:39:29 -04003596 if (unlikely(atomic_read(&cpu_buffer->record_disabled)))
Steven Rostedtd7690412008-10-01 00:29:53 -04003597 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003598
Steven Rostedt (Red Hat)3205f802015-05-21 17:39:29 -04003599 if (unlikely(length > BUF_MAX_DATA_SIZE))
Steven Rostedtbf41a152008-10-04 02:00:59 -04003600 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003601
Steven Rostedt (Red Hat)58a09ec2015-05-27 10:27:47 -04003602 if (unlikely(trace_recursive_lock(cpu_buffer)))
3603 goto out;
3604
Steven Rostedt62f0b3e2009-09-04 14:11:34 -04003605 event = rb_reserve_next_event(buffer, cpu_buffer, length);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003606 if (!event)
Steven Rostedt (Red Hat)58a09ec2015-05-27 10:27:47 -04003607 goto out_unlock;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003608
3609 return event;
3610
Steven Rostedt (Red Hat)58a09ec2015-05-27 10:27:47 -04003611 out_unlock:
3612 trace_recursive_unlock(cpu_buffer);
Steven Rostedtd7690412008-10-01 00:29:53 -04003613 out:
Steven Rostedt5168ae52010-06-03 09:36:50 -04003614 preempt_enable_notrace();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003615 return NULL;
3616}
Robert Richterc4f50182008-12-11 16:49:22 +01003617EXPORT_SYMBOL_GPL(ring_buffer_lock_reserve);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003618
Steven Rostedta1863c22009-09-03 10:23:58 -04003619/*
3620 * Decrement the entries to the page that an event is on.
3621 * The event does not even need to exist, only the pointer
3622 * to the page it is on. This may only be called before the commit
3623 * takes place.
3624 */
3625static inline void
3626rb_decrement_entry(struct ring_buffer_per_cpu *cpu_buffer,
3627 struct ring_buffer_event *event)
3628{
3629 unsigned long addr = (unsigned long)event;
3630 struct buffer_page *bpage = cpu_buffer->commit_page;
3631 struct buffer_page *start;
3632
3633 addr &= PAGE_MASK;
3634
3635 /* Do the likely case first */
3636 if (likely(bpage->page == (void *)addr)) {
3637 local_dec(&bpage->entries);
3638 return;
3639 }
3640
3641 /*
3642 * Because the commit page may be on the reader page we
3643 * start with the next page and check the end loop there.
3644 */
Qiujun Huang6689bed2020-12-25 22:03:56 +08003645 rb_inc_page(&bpage);
Steven Rostedta1863c22009-09-03 10:23:58 -04003646 start = bpage;
3647 do {
3648 if (bpage->page == (void *)addr) {
3649 local_dec(&bpage->entries);
3650 return;
3651 }
Qiujun Huang6689bed2020-12-25 22:03:56 +08003652 rb_inc_page(&bpage);
Steven Rostedta1863c22009-09-03 10:23:58 -04003653 } while (bpage != start);
3654
3655 /* commit not part of this buffer?? */
3656 RB_WARN_ON(cpu_buffer, 1);
3657}
3658
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003659/**
Qiujun Huang88883492020-11-12 23:18:00 +08003660 * ring_buffer_discard_commit - discard an event that has not been committed
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04003661 * @buffer: the ring buffer
3662 * @event: non committed event to discard
3663 *
Steven Rostedtdc892f72009-09-03 15:33:41 -04003664 * Sometimes an event that is in the ring buffer needs to be ignored.
3665 * This function lets the user discard an event in the ring buffer
3666 * and then that event will not be read later.
3667 *
Steven Rostedt (VMware)6167c202018-05-16 11:17:06 -04003668 * This function only works if it is called before the item has been
Steven Rostedtdc892f72009-09-03 15:33:41 -04003669 * committed. It will try to free the event from the ring buffer
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04003670 * if another event has not been added behind it.
3671 *
3672 * If another event has been added behind it, it will set the event
3673 * up as discarded, and perform the commit.
3674 *
3675 * If this function is called, do not call ring_buffer_unlock_commit on
3676 * the event.
3677 */
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05003678void ring_buffer_discard_commit(struct trace_buffer *buffer,
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04003679 struct ring_buffer_event *event)
3680{
3681 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04003682 int cpu;
3683
3684 /* The event is discarded regardless */
Frederic Weisbeckerf3b9aae2009-04-19 23:39:33 +02003685 rb_event_discard(event);
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04003686
Steven Rostedtfa743952009-06-16 12:37:57 -04003687 cpu = smp_processor_id();
3688 cpu_buffer = buffer->buffers[cpu];
3689
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04003690 /*
3691 * This must only be called if the event has not been
3692 * committed yet. Thus we can assume that preemption
3693 * is still disabled.
3694 */
Steven Rostedtfa743952009-06-16 12:37:57 -04003695 RB_WARN_ON(buffer, !local_read(&cpu_buffer->committing));
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04003696
Steven Rostedta1863c22009-09-03 10:23:58 -04003697 rb_decrement_entry(cpu_buffer, event);
Steven Rostedt0f2541d2009-08-05 12:02:48 -04003698 if (rb_try_to_discard(cpu_buffer, event))
Steven Rostedtedd813bf2009-06-02 23:00:53 -04003699 goto out;
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04003700
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04003701 out:
Steven Rostedtfa743952009-06-16 12:37:57 -04003702 rb_end_commit(cpu_buffer);
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04003703
Steven Rostedt (Red Hat)58a09ec2015-05-27 10:27:47 -04003704 trace_recursive_unlock(cpu_buffer);
Frederic Weisbeckerf3b9aae2009-04-19 23:39:33 +02003705
Steven Rostedt5168ae52010-06-03 09:36:50 -04003706 preempt_enable_notrace();
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04003707
3708}
3709EXPORT_SYMBOL_GPL(ring_buffer_discard_commit);
3710
3711/**
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003712 * ring_buffer_write - write data to the buffer without reserving
3713 * @buffer: The ring buffer to write to.
3714 * @length: The length of the data being written (excluding the event header)
3715 * @data: The data to write to the buffer.
3716 *
3717 * This is like ring_buffer_lock_reserve and ring_buffer_unlock_commit as
3718 * one function. If you already have the data to write to the buffer, it
3719 * may be easier to simply call this function.
3720 *
3721 * Note, like ring_buffer_lock_reserve, the length is the length of the data
3722 * and not the length of the event which would hold the header.
3723 */
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05003724int ring_buffer_write(struct trace_buffer *buffer,
David Sharp01e3e712012-06-07 16:46:24 -07003725 unsigned long length,
3726 void *data)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003727{
3728 struct ring_buffer_per_cpu *cpu_buffer;
3729 struct ring_buffer_event *event;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003730 void *body;
3731 int ret = -EBUSY;
Steven Rostedt5168ae52010-06-03 09:36:50 -04003732 int cpu;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003733
Steven Rostedt5168ae52010-06-03 09:36:50 -04003734 preempt_disable_notrace();
Steven Rostedtbf41a152008-10-04 02:00:59 -04003735
Lai Jiangshan52fbe9c2010-03-08 14:50:43 +08003736 if (atomic_read(&buffer->record_disabled))
3737 goto out;
3738
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003739 cpu = raw_smp_processor_id();
3740
Rusty Russell9e01c1b2009-01-01 10:12:22 +10303741 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedtd7690412008-10-01 00:29:53 -04003742 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003743
3744 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003745
3746 if (atomic_read(&cpu_buffer->record_disabled))
3747 goto out;
3748
Steven Rostedtbe957c42009-05-11 14:42:53 -04003749 if (length > BUF_MAX_DATA_SIZE)
3750 goto out;
3751
Steven Rostedt (Red Hat)985e8712015-05-27 10:48:56 -04003752 if (unlikely(trace_recursive_lock(cpu_buffer)))
3753 goto out;
3754
Steven Rostedt62f0b3e2009-09-04 14:11:34 -04003755 event = rb_reserve_next_event(buffer, cpu_buffer, length);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003756 if (!event)
Steven Rostedt (Red Hat)985e8712015-05-27 10:48:56 -04003757 goto out_unlock;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003758
3759 body = rb_event_data(event);
3760
3761 memcpy(body, data, length);
3762
3763 rb_commit(cpu_buffer, event);
3764
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -05003765 rb_wakeups(buffer, cpu_buffer);
3766
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003767 ret = 0;
Steven Rostedt (Red Hat)985e8712015-05-27 10:48:56 -04003768
3769 out_unlock:
3770 trace_recursive_unlock(cpu_buffer);
3771
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003772 out:
Steven Rostedt5168ae52010-06-03 09:36:50 -04003773 preempt_enable_notrace();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003774
3775 return ret;
3776}
Robert Richterc4f50182008-12-11 16:49:22 +01003777EXPORT_SYMBOL_GPL(ring_buffer_write);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003778
Yaowei Baida588342015-09-29 22:43:33 +08003779static bool rb_per_cpu_empty(struct ring_buffer_per_cpu *cpu_buffer)
Steven Rostedtbf41a152008-10-04 02:00:59 -04003780{
3781 struct buffer_page *reader = cpu_buffer->reader_page;
Steven Rostedt77ae3652009-03-27 11:00:29 -04003782 struct buffer_page *head = rb_set_head_page(cpu_buffer);
Steven Rostedtbf41a152008-10-04 02:00:59 -04003783 struct buffer_page *commit = cpu_buffer->commit_page;
3784
Steven Rostedt77ae3652009-03-27 11:00:29 -04003785 /* In case of error, head will be NULL */
3786 if (unlikely(!head))
Yaowei Baida588342015-09-29 22:43:33 +08003787 return true;
Steven Rostedt77ae3652009-03-27 11:00:29 -04003788
Steven Rostedtbf41a152008-10-04 02:00:59 -04003789 return reader->read == rb_page_commit(reader) &&
3790 (commit == reader ||
3791 (commit == head &&
3792 head->read == rb_page_commit(commit)));
3793}
3794
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003795/**
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003796 * ring_buffer_record_disable - stop all writes into the buffer
3797 * @buffer: The ring buffer to stop writes to.
3798 *
3799 * This prevents all writes to the buffer. Any attempt to write
3800 * to the buffer after this will fail and return NULL.
3801 *
Paul E. McKenney74401722018-11-06 18:44:52 -08003802 * The caller should call synchronize_rcu() after this.
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003803 */
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05003804void ring_buffer_record_disable(struct trace_buffer *buffer)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003805{
3806 atomic_inc(&buffer->record_disabled);
3807}
Robert Richterc4f50182008-12-11 16:49:22 +01003808EXPORT_SYMBOL_GPL(ring_buffer_record_disable);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003809
3810/**
3811 * ring_buffer_record_enable - enable writes to the buffer
3812 * @buffer: The ring buffer to enable writes
3813 *
3814 * Note, multiple disables will need the same number of enables
Adam Buchbinderc41b20e2009-12-11 16:35:39 -05003815 * to truly enable the writing (much like preempt_disable).
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003816 */
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05003817void ring_buffer_record_enable(struct trace_buffer *buffer)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003818{
3819 atomic_dec(&buffer->record_disabled);
3820}
Robert Richterc4f50182008-12-11 16:49:22 +01003821EXPORT_SYMBOL_GPL(ring_buffer_record_enable);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003822
3823/**
Steven Rostedt499e5472012-02-22 15:50:28 -05003824 * ring_buffer_record_off - stop all writes into the buffer
3825 * @buffer: The ring buffer to stop writes to.
3826 *
3827 * This prevents all writes to the buffer. Any attempt to write
3828 * to the buffer after this will fail and return NULL.
3829 *
3830 * This is different than ring_buffer_record_disable() as
Wang Tianhong87abb3b2012-08-02 14:02:00 +08003831 * it works like an on/off switch, where as the disable() version
Steven Rostedt499e5472012-02-22 15:50:28 -05003832 * must be paired with a enable().
3833 */
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05003834void ring_buffer_record_off(struct trace_buffer *buffer)
Steven Rostedt499e5472012-02-22 15:50:28 -05003835{
3836 unsigned int rd;
3837 unsigned int new_rd;
3838
3839 do {
3840 rd = atomic_read(&buffer->record_disabled);
3841 new_rd = rd | RB_BUFFER_OFF;
3842 } while (atomic_cmpxchg(&buffer->record_disabled, rd, new_rd) != rd);
3843}
3844EXPORT_SYMBOL_GPL(ring_buffer_record_off);
3845
3846/**
3847 * ring_buffer_record_on - restart writes into the buffer
3848 * @buffer: The ring buffer to start writes to.
3849 *
3850 * This enables all writes to the buffer that was disabled by
3851 * ring_buffer_record_off().
3852 *
3853 * This is different than ring_buffer_record_enable() as
Wang Tianhong87abb3b2012-08-02 14:02:00 +08003854 * it works like an on/off switch, where as the enable() version
Steven Rostedt499e5472012-02-22 15:50:28 -05003855 * must be paired with a disable().
3856 */
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05003857void ring_buffer_record_on(struct trace_buffer *buffer)
Steven Rostedt499e5472012-02-22 15:50:28 -05003858{
3859 unsigned int rd;
3860 unsigned int new_rd;
3861
3862 do {
3863 rd = atomic_read(&buffer->record_disabled);
3864 new_rd = rd & ~RB_BUFFER_OFF;
3865 } while (atomic_cmpxchg(&buffer->record_disabled, rd, new_rd) != rd);
3866}
3867EXPORT_SYMBOL_GPL(ring_buffer_record_on);
3868
3869/**
3870 * ring_buffer_record_is_on - return true if the ring buffer can write
3871 * @buffer: The ring buffer to see if write is enabled
3872 *
3873 * Returns true if the ring buffer is in a state that it accepts writes.
3874 */
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05003875bool ring_buffer_record_is_on(struct trace_buffer *buffer)
Steven Rostedt499e5472012-02-22 15:50:28 -05003876{
3877 return !atomic_read(&buffer->record_disabled);
3878}
3879
3880/**
Masami Hiramatsu73c8d892018-07-14 01:28:15 +09003881 * ring_buffer_record_is_set_on - return true if the ring buffer is set writable
3882 * @buffer: The ring buffer to see if write is set enabled
3883 *
3884 * Returns true if the ring buffer is set writable by ring_buffer_record_on().
3885 * Note that this does NOT mean it is in a writable state.
3886 *
3887 * It may return true when the ring buffer has been disabled by
3888 * ring_buffer_record_disable(), as that is a temporary disabling of
3889 * the ring buffer.
3890 */
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05003891bool ring_buffer_record_is_set_on(struct trace_buffer *buffer)
Masami Hiramatsu73c8d892018-07-14 01:28:15 +09003892{
3893 return !(atomic_read(&buffer->record_disabled) & RB_BUFFER_OFF);
3894}
3895
3896/**
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003897 * ring_buffer_record_disable_cpu - stop all writes into the cpu_buffer
3898 * @buffer: The ring buffer to stop writes to.
3899 * @cpu: The CPU buffer to stop
3900 *
3901 * This prevents all writes to the buffer. Any attempt to write
3902 * to the buffer after this will fail and return NULL.
3903 *
Paul E. McKenney74401722018-11-06 18:44:52 -08003904 * The caller should call synchronize_rcu() after this.
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003905 */
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05003906void ring_buffer_record_disable_cpu(struct trace_buffer *buffer, int cpu)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003907{
3908 struct ring_buffer_per_cpu *cpu_buffer;
3909
Rusty Russell9e01c1b2009-01-01 10:12:22 +10303910 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04003911 return;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003912
3913 cpu_buffer = buffer->buffers[cpu];
3914 atomic_inc(&cpu_buffer->record_disabled);
3915}
Robert Richterc4f50182008-12-11 16:49:22 +01003916EXPORT_SYMBOL_GPL(ring_buffer_record_disable_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003917
3918/**
3919 * ring_buffer_record_enable_cpu - enable writes to the buffer
3920 * @buffer: The ring buffer to enable writes
3921 * @cpu: The CPU to enable.
3922 *
3923 * Note, multiple disables will need the same number of enables
Adam Buchbinderc41b20e2009-12-11 16:35:39 -05003924 * to truly enable the writing (much like preempt_disable).
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003925 */
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05003926void ring_buffer_record_enable_cpu(struct trace_buffer *buffer, int cpu)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003927{
3928 struct ring_buffer_per_cpu *cpu_buffer;
3929
Rusty Russell9e01c1b2009-01-01 10:12:22 +10303930 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04003931 return;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003932
3933 cpu_buffer = buffer->buffers[cpu];
3934 atomic_dec(&cpu_buffer->record_disabled);
3935}
Robert Richterc4f50182008-12-11 16:49:22 +01003936EXPORT_SYMBOL_GPL(ring_buffer_record_enable_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003937
Steven Rostedtf6195aa2010-09-01 12:23:12 -04003938/*
3939 * The total entries in the ring buffer is the running counter
3940 * of entries entered into the ring buffer, minus the sum of
3941 * the entries read from the ring buffer and the number of
3942 * entries that were overwritten.
3943 */
3944static inline unsigned long
3945rb_num_of_entries(struct ring_buffer_per_cpu *cpu_buffer)
3946{
3947 return local_read(&cpu_buffer->entries) -
3948 (local_read(&cpu_buffer->overrun) + cpu_buffer->read);
3949}
3950
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003951/**
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -07003952 * ring_buffer_oldest_event_ts - get the oldest event timestamp from the buffer
3953 * @buffer: The ring buffer
3954 * @cpu: The per CPU buffer to read from.
3955 */
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05003956u64 ring_buffer_oldest_event_ts(struct trace_buffer *buffer, int cpu)
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -07003957{
3958 unsigned long flags;
3959 struct ring_buffer_per_cpu *cpu_buffer;
3960 struct buffer_page *bpage;
Linus Torvaldsda830e52012-12-11 18:18:58 -08003961 u64 ret = 0;
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -07003962
3963 if (!cpumask_test_cpu(cpu, buffer->cpumask))
3964 return 0;
3965
3966 cpu_buffer = buffer->buffers[cpu];
Linus Torvalds7115e3f2011-10-26 17:03:38 +02003967 raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -07003968 /*
3969 * if the tail is on reader_page, oldest time stamp is on the reader
3970 * page
3971 */
3972 if (cpu_buffer->tail_page == cpu_buffer->reader_page)
3973 bpage = cpu_buffer->reader_page;
3974 else
3975 bpage = rb_set_head_page(cpu_buffer);
Steven Rostedt54f7be52012-11-29 22:27:22 -05003976 if (bpage)
3977 ret = bpage->page->time_stamp;
Linus Torvalds7115e3f2011-10-26 17:03:38 +02003978 raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -07003979
3980 return ret;
3981}
3982EXPORT_SYMBOL_GPL(ring_buffer_oldest_event_ts);
3983
3984/**
3985 * ring_buffer_bytes_cpu - get the number of bytes consumed in a cpu buffer
3986 * @buffer: The ring buffer
3987 * @cpu: The per CPU buffer to read from.
3988 */
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05003989unsigned long ring_buffer_bytes_cpu(struct trace_buffer *buffer, int cpu)
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -07003990{
3991 struct ring_buffer_per_cpu *cpu_buffer;
3992 unsigned long ret;
3993
3994 if (!cpumask_test_cpu(cpu, buffer->cpumask))
3995 return 0;
3996
3997 cpu_buffer = buffer->buffers[cpu];
3998 ret = local_read(&cpu_buffer->entries_bytes) - cpu_buffer->read_bytes;
3999
4000 return ret;
4001}
4002EXPORT_SYMBOL_GPL(ring_buffer_bytes_cpu);
4003
4004/**
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004005 * ring_buffer_entries_cpu - get the number of entries in a cpu buffer
4006 * @buffer: The ring buffer
4007 * @cpu: The per CPU buffer to get the entries from.
4008 */
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05004009unsigned long ring_buffer_entries_cpu(struct trace_buffer *buffer, int cpu)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004010{
4011 struct ring_buffer_per_cpu *cpu_buffer;
4012
Rusty Russell9e01c1b2009-01-01 10:12:22 +10304013 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04004014 return 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004015
4016 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt554f7862009-03-11 22:00:13 -04004017
Steven Rostedtf6195aa2010-09-01 12:23:12 -04004018 return rb_num_of_entries(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004019}
Robert Richterc4f50182008-12-11 16:49:22 +01004020EXPORT_SYMBOL_GPL(ring_buffer_entries_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004021
4022/**
Slava Pestov884bfe82011-07-15 14:23:58 -07004023 * ring_buffer_overrun_cpu - get the number of overruns caused by the ring
4024 * buffer wrapping around (only if RB_FL_OVERWRITE is on).
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004025 * @buffer: The ring buffer
4026 * @cpu: The per CPU buffer to get the number of overruns from
4027 */
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05004028unsigned long ring_buffer_overrun_cpu(struct trace_buffer *buffer, int cpu)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004029{
4030 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedt8aabee52009-03-12 13:13:49 -04004031 unsigned long ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004032
Rusty Russell9e01c1b2009-01-01 10:12:22 +10304033 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04004034 return 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004035
4036 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt77ae3652009-03-27 11:00:29 -04004037 ret = local_read(&cpu_buffer->overrun);
Steven Rostedt554f7862009-03-11 22:00:13 -04004038
4039 return ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004040}
Robert Richterc4f50182008-12-11 16:49:22 +01004041EXPORT_SYMBOL_GPL(ring_buffer_overrun_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004042
4043/**
Slava Pestov884bfe82011-07-15 14:23:58 -07004044 * ring_buffer_commit_overrun_cpu - get the number of overruns caused by
4045 * commits failing due to the buffer wrapping around while there are uncommitted
4046 * events, such as during an interrupt storm.
Steven Rostedtf0d2c682009-04-29 13:43:37 -04004047 * @buffer: The ring buffer
4048 * @cpu: The per CPU buffer to get the number of overruns from
4049 */
4050unsigned long
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05004051ring_buffer_commit_overrun_cpu(struct trace_buffer *buffer, int cpu)
Steven Rostedtf0d2c682009-04-29 13:43:37 -04004052{
4053 struct ring_buffer_per_cpu *cpu_buffer;
4054 unsigned long ret;
4055
4056 if (!cpumask_test_cpu(cpu, buffer->cpumask))
4057 return 0;
4058
4059 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt77ae3652009-03-27 11:00:29 -04004060 ret = local_read(&cpu_buffer->commit_overrun);
Steven Rostedtf0d2c682009-04-29 13:43:37 -04004061
4062 return ret;
4063}
4064EXPORT_SYMBOL_GPL(ring_buffer_commit_overrun_cpu);
4065
4066/**
Slava Pestov884bfe82011-07-15 14:23:58 -07004067 * ring_buffer_dropped_events_cpu - get the number of dropped events caused by
4068 * the ring buffer filling up (only if RB_FL_OVERWRITE is off).
4069 * @buffer: The ring buffer
4070 * @cpu: The per CPU buffer to get the number of overruns from
4071 */
4072unsigned long
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05004073ring_buffer_dropped_events_cpu(struct trace_buffer *buffer, int cpu)
Slava Pestov884bfe82011-07-15 14:23:58 -07004074{
4075 struct ring_buffer_per_cpu *cpu_buffer;
4076 unsigned long ret;
4077
4078 if (!cpumask_test_cpu(cpu, buffer->cpumask))
4079 return 0;
4080
4081 cpu_buffer = buffer->buffers[cpu];
4082 ret = local_read(&cpu_buffer->dropped_events);
4083
4084 return ret;
4085}
4086EXPORT_SYMBOL_GPL(ring_buffer_dropped_events_cpu);
4087
4088/**
Steven Rostedt (Red Hat)ad964702013-01-29 17:45:49 -05004089 * ring_buffer_read_events_cpu - get the number of events successfully read
4090 * @buffer: The ring buffer
4091 * @cpu: The per CPU buffer to get the number of events read
4092 */
4093unsigned long
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05004094ring_buffer_read_events_cpu(struct trace_buffer *buffer, int cpu)
Steven Rostedt (Red Hat)ad964702013-01-29 17:45:49 -05004095{
4096 struct ring_buffer_per_cpu *cpu_buffer;
4097
4098 if (!cpumask_test_cpu(cpu, buffer->cpumask))
4099 return 0;
4100
4101 cpu_buffer = buffer->buffers[cpu];
4102 return cpu_buffer->read;
4103}
4104EXPORT_SYMBOL_GPL(ring_buffer_read_events_cpu);
4105
4106/**
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004107 * ring_buffer_entries - get the number of entries in a buffer
4108 * @buffer: The ring buffer
4109 *
4110 * Returns the total number of entries in the ring buffer
4111 * (all CPU entries)
4112 */
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05004113unsigned long ring_buffer_entries(struct trace_buffer *buffer)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004114{
4115 struct ring_buffer_per_cpu *cpu_buffer;
4116 unsigned long entries = 0;
4117 int cpu;
4118
4119 /* if you care about this being correct, lock the buffer */
4120 for_each_buffer_cpu(buffer, cpu) {
4121 cpu_buffer = buffer->buffers[cpu];
Steven Rostedtf6195aa2010-09-01 12:23:12 -04004122 entries += rb_num_of_entries(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004123 }
4124
4125 return entries;
4126}
Robert Richterc4f50182008-12-11 16:49:22 +01004127EXPORT_SYMBOL_GPL(ring_buffer_entries);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004128
4129/**
Jiri Olsa67b394f2009-10-23 19:36:18 -04004130 * ring_buffer_overruns - get the number of overruns in buffer
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004131 * @buffer: The ring buffer
4132 *
4133 * Returns the total number of overruns in the ring buffer
4134 * (all CPU entries)
4135 */
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05004136unsigned long ring_buffer_overruns(struct trace_buffer *buffer)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004137{
4138 struct ring_buffer_per_cpu *cpu_buffer;
4139 unsigned long overruns = 0;
4140 int cpu;
4141
4142 /* if you care about this being correct, lock the buffer */
4143 for_each_buffer_cpu(buffer, cpu) {
4144 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt77ae3652009-03-27 11:00:29 -04004145 overruns += local_read(&cpu_buffer->overrun);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004146 }
4147
4148 return overruns;
4149}
Robert Richterc4f50182008-12-11 16:49:22 +01004150EXPORT_SYMBOL_GPL(ring_buffer_overruns);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004151
Steven Rostedt642edba2008-11-12 00:01:26 -05004152static void rb_iter_reset(struct ring_buffer_iter *iter)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004153{
4154 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
4155
Steven Rostedtd7690412008-10-01 00:29:53 -04004156 /* Iterator usage is expected to have record disabled */
Steven Rostedt (Red Hat)651e22f2014-08-06 14:11:33 -04004157 iter->head_page = cpu_buffer->reader_page;
4158 iter->head = cpu_buffer->reader_page->read;
Steven Rostedt (VMware)785888c2020-03-17 17:32:27 -04004159 iter->next_event = iter->head;
Steven Rostedt (Red Hat)651e22f2014-08-06 14:11:33 -04004160
4161 iter->cache_reader_page = iter->head_page;
Steven Rostedt (Red Hat)24607f12014-10-02 16:51:18 -04004162 iter->cache_read = cpu_buffer->read;
Steven Rostedt (Red Hat)651e22f2014-08-06 14:11:33 -04004163
Steven Rostedt (VMware)28e3fc52020-03-17 17:32:26 -04004164 if (iter->head) {
Steven Rostedtd7690412008-10-01 00:29:53 -04004165 iter->read_stamp = cpu_buffer->read_stamp;
Steven Rostedt (VMware)28e3fc52020-03-17 17:32:26 -04004166 iter->page_stamp = cpu_buffer->reader_page->page->time_stamp;
4167 } else {
Steven Rostedtabc9b562008-12-02 15:34:06 -05004168 iter->read_stamp = iter->head_page->page->time_stamp;
Steven Rostedt (VMware)28e3fc52020-03-17 17:32:26 -04004169 iter->page_stamp = iter->read_stamp;
4170 }
Steven Rostedt642edba2008-11-12 00:01:26 -05004171}
Steven Rostedtf83c9d02008-11-11 18:47:44 +01004172
Steven Rostedt642edba2008-11-12 00:01:26 -05004173/**
4174 * ring_buffer_iter_reset - reset an iterator
4175 * @iter: The iterator to reset
4176 *
4177 * Resets the iterator, so that it will start from the beginning
4178 * again.
4179 */
4180void ring_buffer_iter_reset(struct ring_buffer_iter *iter)
4181{
Steven Rostedt554f7862009-03-11 22:00:13 -04004182 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedt642edba2008-11-12 00:01:26 -05004183 unsigned long flags;
4184
Steven Rostedt554f7862009-03-11 22:00:13 -04004185 if (!iter)
4186 return;
4187
4188 cpu_buffer = iter->cpu_buffer;
4189
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02004190 raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
Steven Rostedt642edba2008-11-12 00:01:26 -05004191 rb_iter_reset(iter);
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02004192 raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004193}
Robert Richterc4f50182008-12-11 16:49:22 +01004194EXPORT_SYMBOL_GPL(ring_buffer_iter_reset);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004195
4196/**
4197 * ring_buffer_iter_empty - check if an iterator has no more to read
4198 * @iter: The iterator to check
4199 */
4200int ring_buffer_iter_empty(struct ring_buffer_iter *iter)
4201{
4202 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedt (VMware)78f7a452017-04-19 14:29:46 -04004203 struct buffer_page *reader;
4204 struct buffer_page *head_page;
4205 struct buffer_page *commit_page;
Steven Rostedt (VMware)ead6ecf2020-03-17 17:32:24 -04004206 struct buffer_page *curr_commit_page;
Steven Rostedt (VMware)78f7a452017-04-19 14:29:46 -04004207 unsigned commit;
Steven Rostedt (VMware)ead6ecf2020-03-17 17:32:24 -04004208 u64 curr_commit_ts;
4209 u64 commit_ts;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004210
4211 cpu_buffer = iter->cpu_buffer;
Steven Rostedt (VMware)78f7a452017-04-19 14:29:46 -04004212 reader = cpu_buffer->reader_page;
4213 head_page = cpu_buffer->head_page;
4214 commit_page = cpu_buffer->commit_page;
Steven Rostedt (VMware)ead6ecf2020-03-17 17:32:24 -04004215 commit_ts = commit_page->page->time_stamp;
Steven Rostedt (VMware)78f7a452017-04-19 14:29:46 -04004216
Steven Rostedt (VMware)ead6ecf2020-03-17 17:32:24 -04004217 /*
4218 * When the writer goes across pages, it issues a cmpxchg which
4219 * is a mb(), which will synchronize with the rmb here.
4220 * (see rb_tail_page_update())
4221 */
4222 smp_rmb();
4223 commit = rb_page_commit(commit_page);
4224 /* We want to make sure that the commit page doesn't change */
4225 smp_rmb();
4226
4227 /* Make sure commit page didn't change */
4228 curr_commit_page = READ_ONCE(cpu_buffer->commit_page);
4229 curr_commit_ts = READ_ONCE(curr_commit_page->page->time_stamp);
4230
4231 /* If the commit page changed, then there's more data */
4232 if (curr_commit_page != commit_page ||
4233 curr_commit_ts != commit_ts)
4234 return 0;
4235
4236 /* Still racy, as it may return a false positive, but that's OK */
Steven Rostedt (VMware)785888c2020-03-17 17:32:27 -04004237 return ((iter->head_page == commit_page && iter->head >= commit) ||
Steven Rostedt (VMware)78f7a452017-04-19 14:29:46 -04004238 (iter->head_page == reader && commit_page == head_page &&
4239 head_page->read == commit &&
4240 iter->head == rb_page_commit(cpu_buffer->reader_page)));
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004241}
Robert Richterc4f50182008-12-11 16:49:22 +01004242EXPORT_SYMBOL_GPL(ring_buffer_iter_empty);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004243
4244static void
4245rb_update_read_stamp(struct ring_buffer_per_cpu *cpu_buffer,
4246 struct ring_buffer_event *event)
4247{
4248 u64 delta;
4249
Lai Jiangshan334d4162009-04-24 11:27:05 +08004250 switch (event->type_len) {
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004251 case RINGBUF_TYPE_PADDING:
4252 return;
4253
4254 case RINGBUF_TYPE_TIME_EXTEND:
Tom Zanussidc4e2802018-01-15 20:51:40 -06004255 delta = ring_buffer_event_time_stamp(event);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004256 cpu_buffer->read_stamp += delta;
4257 return;
4258
4259 case RINGBUF_TYPE_TIME_STAMP:
Tom Zanussidc4e2802018-01-15 20:51:40 -06004260 delta = ring_buffer_event_time_stamp(event);
4261 cpu_buffer->read_stamp = delta;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004262 return;
4263
4264 case RINGBUF_TYPE_DATA:
4265 cpu_buffer->read_stamp += event->time_delta;
4266 return;
4267
4268 default:
Steven Rostedt (VMware)da4d4012020-05-13 15:36:22 -04004269 RB_WARN_ON(cpu_buffer, 1);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004270 }
4271 return;
4272}
4273
4274static void
4275rb_update_iter_read_stamp(struct ring_buffer_iter *iter,
4276 struct ring_buffer_event *event)
4277{
4278 u64 delta;
4279
Lai Jiangshan334d4162009-04-24 11:27:05 +08004280 switch (event->type_len) {
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004281 case RINGBUF_TYPE_PADDING:
4282 return;
4283
4284 case RINGBUF_TYPE_TIME_EXTEND:
Tom Zanussidc4e2802018-01-15 20:51:40 -06004285 delta = ring_buffer_event_time_stamp(event);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004286 iter->read_stamp += delta;
4287 return;
4288
4289 case RINGBUF_TYPE_TIME_STAMP:
Tom Zanussidc4e2802018-01-15 20:51:40 -06004290 delta = ring_buffer_event_time_stamp(event);
4291 iter->read_stamp = delta;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004292 return;
4293
4294 case RINGBUF_TYPE_DATA:
4295 iter->read_stamp += event->time_delta;
4296 return;
4297
4298 default:
Steven Rostedt (VMware)da4d4012020-05-13 15:36:22 -04004299 RB_WARN_ON(iter->cpu_buffer, 1);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004300 }
4301 return;
4302}
4303
Steven Rostedtd7690412008-10-01 00:29:53 -04004304static struct buffer_page *
4305rb_get_reader_page(struct ring_buffer_per_cpu *cpu_buffer)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004306{
Steven Rostedtd7690412008-10-01 00:29:53 -04004307 struct buffer_page *reader = NULL;
Steven Rostedt66a8cb92010-03-31 13:21:56 -04004308 unsigned long overwrite;
Steven Rostedtd7690412008-10-01 00:29:53 -04004309 unsigned long flags;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04004310 int nr_loops = 0;
Steven Rostedt77ae3652009-03-27 11:00:29 -04004311 int ret;
Steven Rostedtd7690412008-10-01 00:29:53 -04004312
Steven Rostedt3e03fb72008-11-06 00:09:43 -05004313 local_irq_save(flags);
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01004314 arch_spin_lock(&cpu_buffer->lock);
Steven Rostedtd7690412008-10-01 00:29:53 -04004315
4316 again:
Steven Rostedt818e3dd2008-10-31 09:58:35 -04004317 /*
4318 * This should normally only loop twice. But because the
4319 * start of the reader inserts an empty page, it causes
4320 * a case where we will loop three times. There should be no
4321 * reason to loop four times (that I know of).
4322 */
Steven Rostedt3e89c7bb2008-11-11 15:28:41 -05004323 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 3)) {
Steven Rostedt818e3dd2008-10-31 09:58:35 -04004324 reader = NULL;
4325 goto out;
4326 }
4327
Steven Rostedtd7690412008-10-01 00:29:53 -04004328 reader = cpu_buffer->reader_page;
4329
4330 /* If there's more to read, return this page */
Steven Rostedtbf41a152008-10-04 02:00:59 -04004331 if (cpu_buffer->reader_page->read < rb_page_size(reader))
Steven Rostedtd7690412008-10-01 00:29:53 -04004332 goto out;
4333
4334 /* Never should we have an index greater than the size */
Steven Rostedt3e89c7bb2008-11-11 15:28:41 -05004335 if (RB_WARN_ON(cpu_buffer,
4336 cpu_buffer->reader_page->read > rb_page_size(reader)))
4337 goto out;
Steven Rostedtd7690412008-10-01 00:29:53 -04004338
4339 /* check if we caught up to the tail */
4340 reader = NULL;
Steven Rostedtbf41a152008-10-04 02:00:59 -04004341 if (cpu_buffer->commit_page == cpu_buffer->reader_page)
Steven Rostedtd7690412008-10-01 00:29:53 -04004342 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004343
Steven Rostedta5fb8332012-06-28 13:35:04 -04004344 /* Don't bother swapping if the ring buffer is empty */
4345 if (rb_num_of_entries(cpu_buffer) == 0)
4346 goto out;
4347
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004348 /*
Steven Rostedtd7690412008-10-01 00:29:53 -04004349 * Reset the reader page to size zero.
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004350 */
Steven Rostedt77ae3652009-03-27 11:00:29 -04004351 local_set(&cpu_buffer->reader_page->write, 0);
4352 local_set(&cpu_buffer->reader_page->entries, 0);
4353 local_set(&cpu_buffer->reader_page->page->commit, 0);
Steven Rostedtff0ff842010-03-31 22:11:42 -04004354 cpu_buffer->reader_page->real_end = 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004355
Steven Rostedt77ae3652009-03-27 11:00:29 -04004356 spin:
4357 /*
4358 * Splice the empty reader page into the list around the head.
4359 */
4360 reader = rb_set_head_page(cpu_buffer);
Steven Rostedt54f7be52012-11-29 22:27:22 -05004361 if (!reader)
4362 goto out;
Steven Rostedt0e1ff5d2010-01-06 20:40:44 -05004363 cpu_buffer->reader_page->list.next = rb_list_head(reader->list.next);
Steven Rostedtd7690412008-10-01 00:29:53 -04004364 cpu_buffer->reader_page->list.prev = reader->list.prev;
Steven Rostedtbf41a152008-10-04 02:00:59 -04004365
Steven Rostedt3adc54f2009-03-30 15:32:01 -04004366 /*
4367 * cpu_buffer->pages just needs to point to the buffer, it
4368 * has no specific buffer page to point to. Lets move it out
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004369 * of our way so we don't accidentally swap it.
Steven Rostedt3adc54f2009-03-30 15:32:01 -04004370 */
4371 cpu_buffer->pages = reader->list.prev;
4372
Steven Rostedt77ae3652009-03-27 11:00:29 -04004373 /* The reader page will be pointing to the new head */
Qiujun Huang6689bed2020-12-25 22:03:56 +08004374 rb_set_list_to_head(&cpu_buffer->reader_page->list);
Steven Rostedtd7690412008-10-01 00:29:53 -04004375
4376 /*
Steven Rostedt66a8cb92010-03-31 13:21:56 -04004377 * We want to make sure we read the overruns after we set up our
4378 * pointers to the next object. The writer side does a
4379 * cmpxchg to cross pages which acts as the mb on the writer
4380 * side. Note, the reader will constantly fail the swap
4381 * while the writer is updating the pointers, so this
4382 * guarantees that the overwrite recorded here is the one we
4383 * want to compare with the last_overrun.
4384 */
4385 smp_mb();
4386 overwrite = local_read(&(cpu_buffer->overrun));
4387
4388 /*
Steven Rostedt77ae3652009-03-27 11:00:29 -04004389 * Here's the tricky part.
4390 *
4391 * We need to move the pointer past the header page.
4392 * But we can only do that if a writer is not currently
4393 * moving it. The page before the header page has the
4394 * flag bit '1' set if it is pointing to the page we want.
4395 * but if the writer is in the process of moving it
4396 * than it will be '2' or already moved '0'.
Steven Rostedtd7690412008-10-01 00:29:53 -04004397 */
Steven Rostedtd7690412008-10-01 00:29:53 -04004398
Steven Rostedt77ae3652009-03-27 11:00:29 -04004399 ret = rb_head_page_replace(reader, cpu_buffer->reader_page);
4400
4401 /*
4402 * If we did not convert it, then we must try again.
4403 */
4404 if (!ret)
4405 goto spin;
4406
4407 /*
Steven Rostedt (VMware)2c2b0a72018-11-29 20:32:26 -05004408 * Yay! We succeeded in replacing the page.
Steven Rostedt77ae3652009-03-27 11:00:29 -04004409 *
4410 * Now make the new head point back to the reader page.
4411 */
David Sharp5ded3dc62010-01-06 17:12:07 -08004412 rb_list_head(reader->list.next)->prev = &cpu_buffer->reader_page->list;
Qiujun Huang6689bed2020-12-25 22:03:56 +08004413 rb_inc_page(&cpu_buffer->head_page);
Steven Rostedtd7690412008-10-01 00:29:53 -04004414
Steven Rostedt (VMware)2c2b0a72018-11-29 20:32:26 -05004415 local_inc(&cpu_buffer->pages_read);
4416
Steven Rostedtd7690412008-10-01 00:29:53 -04004417 /* Finally update the reader page to the new head */
4418 cpu_buffer->reader_page = reader;
Steven Rostedt (Red Hat)b81f4722015-11-23 10:35:36 -05004419 cpu_buffer->reader_page->read = 0;
Steven Rostedtd7690412008-10-01 00:29:53 -04004420
Steven Rostedt66a8cb92010-03-31 13:21:56 -04004421 if (overwrite != cpu_buffer->last_overrun) {
4422 cpu_buffer->lost_events = overwrite - cpu_buffer->last_overrun;
4423 cpu_buffer->last_overrun = overwrite;
4424 }
4425
Steven Rostedtd7690412008-10-01 00:29:53 -04004426 goto again;
4427
4428 out:
Steven Rostedt (Red Hat)b81f4722015-11-23 10:35:36 -05004429 /* Update the read_stamp on the first event */
4430 if (reader && reader->read == 0)
4431 cpu_buffer->read_stamp = reader->page->time_stamp;
4432
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01004433 arch_spin_unlock(&cpu_buffer->lock);
Steven Rostedt3e03fb72008-11-06 00:09:43 -05004434 local_irq_restore(flags);
Steven Rostedtd7690412008-10-01 00:29:53 -04004435
4436 return reader;
4437}
4438
4439static void rb_advance_reader(struct ring_buffer_per_cpu *cpu_buffer)
4440{
4441 struct ring_buffer_event *event;
4442 struct buffer_page *reader;
4443 unsigned length;
4444
4445 reader = rb_get_reader_page(cpu_buffer);
4446
4447 /* This function should not be called when buffer is empty */
Steven Rostedt3e89c7bb2008-11-11 15:28:41 -05004448 if (RB_WARN_ON(cpu_buffer, !reader))
4449 return;
Steven Rostedtd7690412008-10-01 00:29:53 -04004450
4451 event = rb_reader_event(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004452
Steven Rostedta1863c22009-09-03 10:23:58 -04004453 if (event->type_len <= RINGBUF_TYPE_DATA_TYPE_LEN_MAX)
Steven Rostedte4906ef2009-04-30 20:49:44 -04004454 cpu_buffer->read++;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004455
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004456 rb_update_read_stamp(cpu_buffer, event);
4457
Steven Rostedtd7690412008-10-01 00:29:53 -04004458 length = rb_event_length(event);
Steven Rostedt6f807ac2008-10-04 02:00:58 -04004459 cpu_buffer->reader_page->read += length;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004460}
4461
4462static void rb_advance_iter(struct ring_buffer_iter *iter)
4463{
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004464 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004465
4466 cpu_buffer = iter->cpu_buffer;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004467
Steven Rostedt (VMware)785888c2020-03-17 17:32:27 -04004468 /* If head == next_event then we need to jump to the next event */
4469 if (iter->head == iter->next_event) {
4470 /* If the event gets overwritten again, there's nothing to do */
4471 if (rb_iter_head_event(iter) == NULL)
4472 return;
4473 }
4474
4475 iter->head = iter->next_event;
4476
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004477 /*
4478 * Check if we are at the end of the buffer.
4479 */
Steven Rostedt (VMware)785888c2020-03-17 17:32:27 -04004480 if (iter->next_event >= rb_page_size(iter->head_page)) {
Steven Rostedtea05b572009-06-03 09:30:10 -04004481 /* discarded commits can make the page empty */
4482 if (iter->head_page == cpu_buffer->commit_page)
Steven Rostedt3e89c7bb2008-11-11 15:28:41 -05004483 return;
Steven Rostedtd7690412008-10-01 00:29:53 -04004484 rb_inc_iter(iter);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004485 return;
4486 }
4487
Steven Rostedt (VMware)785888c2020-03-17 17:32:27 -04004488 rb_update_iter_read_stamp(iter, iter->event);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004489}
4490
Steven Rostedt66a8cb92010-03-31 13:21:56 -04004491static int rb_lost_events(struct ring_buffer_per_cpu *cpu_buffer)
4492{
4493 return cpu_buffer->lost_events;
4494}
4495
Steven Rostedtf83c9d02008-11-11 18:47:44 +01004496static struct ring_buffer_event *
Steven Rostedt66a8cb92010-03-31 13:21:56 -04004497rb_buffer_peek(struct ring_buffer_per_cpu *cpu_buffer, u64 *ts,
4498 unsigned long *lost_events)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004499{
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004500 struct ring_buffer_event *event;
Steven Rostedtd7690412008-10-01 00:29:53 -04004501 struct buffer_page *reader;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04004502 int nr_loops = 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004503
Tom Zanussidc4e2802018-01-15 20:51:40 -06004504 if (ts)
4505 *ts = 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004506 again:
Steven Rostedt818e3dd2008-10-31 09:58:35 -04004507 /*
Steven Rostedt69d1b832010-10-07 18:18:05 -04004508 * We repeat when a time extend is encountered.
4509 * Since the time extend is always attached to a data event,
4510 * we should never loop more than once.
4511 * (We never hit the following condition more than twice).
Steven Rostedt818e3dd2008-10-31 09:58:35 -04004512 */
Steven Rostedt69d1b832010-10-07 18:18:05 -04004513 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 2))
Steven Rostedt818e3dd2008-10-31 09:58:35 -04004514 return NULL;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04004515
Steven Rostedtd7690412008-10-01 00:29:53 -04004516 reader = rb_get_reader_page(cpu_buffer);
4517 if (!reader)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004518 return NULL;
4519
Steven Rostedtd7690412008-10-01 00:29:53 -04004520 event = rb_reader_event(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004521
Lai Jiangshan334d4162009-04-24 11:27:05 +08004522 switch (event->type_len) {
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004523 case RINGBUF_TYPE_PADDING:
Tom Zanussi2d622712009-03-22 03:30:49 -05004524 if (rb_null_event(event))
4525 RB_WARN_ON(cpu_buffer, 1);
4526 /*
4527 * Because the writer could be discarding every
4528 * event it creates (which would probably be bad)
4529 * if we were to go back to "again" then we may never
4530 * catch up, and will trigger the warn on, or lock
4531 * the box. Return the padding, and we will release
4532 * the current locks, and try again.
4533 */
Tom Zanussi2d622712009-03-22 03:30:49 -05004534 return event;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004535
4536 case RINGBUF_TYPE_TIME_EXTEND:
4537 /* Internal data, OK to advance */
Steven Rostedtd7690412008-10-01 00:29:53 -04004538 rb_advance_reader(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004539 goto again;
4540
4541 case RINGBUF_TYPE_TIME_STAMP:
Tom Zanussidc4e2802018-01-15 20:51:40 -06004542 if (ts) {
4543 *ts = ring_buffer_event_time_stamp(event);
4544 ring_buffer_normalize_time_stamp(cpu_buffer->buffer,
4545 cpu_buffer->cpu, ts);
4546 }
4547 /* Internal data, OK to advance */
Steven Rostedtd7690412008-10-01 00:29:53 -04004548 rb_advance_reader(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004549 goto again;
4550
4551 case RINGBUF_TYPE_DATA:
Tom Zanussidc4e2802018-01-15 20:51:40 -06004552 if (ts && !(*ts)) {
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004553 *ts = cpu_buffer->read_stamp + event->time_delta;
Robert Richterd8eeb2d2009-07-31 14:58:04 +02004554 ring_buffer_normalize_time_stamp(cpu_buffer->buffer,
Steven Rostedt37886f62009-03-17 17:22:06 -04004555 cpu_buffer->cpu, ts);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004556 }
Steven Rostedt66a8cb92010-03-31 13:21:56 -04004557 if (lost_events)
4558 *lost_events = rb_lost_events(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004559 return event;
4560
4561 default:
Steven Rostedt (VMware)da4d4012020-05-13 15:36:22 -04004562 RB_WARN_ON(cpu_buffer, 1);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004563 }
4564
4565 return NULL;
4566}
Robert Richterc4f50182008-12-11 16:49:22 +01004567EXPORT_SYMBOL_GPL(ring_buffer_peek);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004568
Steven Rostedtf83c9d02008-11-11 18:47:44 +01004569static struct ring_buffer_event *
4570rb_iter_peek(struct ring_buffer_iter *iter, u64 *ts)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004571{
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05004572 struct trace_buffer *buffer;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004573 struct ring_buffer_per_cpu *cpu_buffer;
4574 struct ring_buffer_event *event;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04004575 int nr_loops = 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004576
Tom Zanussidc4e2802018-01-15 20:51:40 -06004577 if (ts)
4578 *ts = 0;
4579
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004580 cpu_buffer = iter->cpu_buffer;
4581 buffer = cpu_buffer->buffer;
4582
Steven Rostedt492a74f2010-01-25 15:17:47 -05004583 /*
4584 * Check if someone performed a consuming read to
4585 * the buffer. A consuming read invalidates the iterator
4586 * and we need to reset the iterator in this case.
4587 */
4588 if (unlikely(iter->cache_read != cpu_buffer->read ||
4589 iter->cache_reader_page != cpu_buffer->reader_page))
4590 rb_iter_reset(iter);
4591
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004592 again:
Steven Rostedt3c05d742010-01-26 16:14:08 -05004593 if (ring_buffer_iter_empty(iter))
4594 return NULL;
4595
Steven Rostedt818e3dd2008-10-31 09:58:35 -04004596 /*
Steven Rostedt (VMware)3d2353d2020-05-13 15:18:01 -04004597 * As the writer can mess with what the iterator is trying
4598 * to read, just give up if we fail to get an event after
4599 * three tries. The iterator is not as reliable when reading
4600 * the ring buffer with an active write as the consumer is.
4601 * Do not warn if the three failures is reached.
Steven Rostedt818e3dd2008-10-31 09:58:35 -04004602 */
Steven Rostedt (VMware)3d2353d2020-05-13 15:18:01 -04004603 if (++nr_loops > 3)
Steven Rostedt818e3dd2008-10-31 09:58:35 -04004604 return NULL;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04004605
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004606 if (rb_per_cpu_empty(cpu_buffer))
4607 return NULL;
4608
Steven Rostedt (Red Hat)10e83fd2014-07-23 19:45:12 -04004609 if (iter->head >= rb_page_size(iter->head_page)) {
Steven Rostedt3c05d742010-01-26 16:14:08 -05004610 rb_inc_iter(iter);
4611 goto again;
4612 }
4613
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004614 event = rb_iter_head_event(iter);
Steven Rostedt (VMware)3d2353d2020-05-13 15:18:01 -04004615 if (!event)
Steven Rostedt (VMware)785888c2020-03-17 17:32:27 -04004616 goto again;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004617
Lai Jiangshan334d4162009-04-24 11:27:05 +08004618 switch (event->type_len) {
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004619 case RINGBUF_TYPE_PADDING:
Tom Zanussi2d622712009-03-22 03:30:49 -05004620 if (rb_null_event(event)) {
4621 rb_inc_iter(iter);
4622 goto again;
4623 }
4624 rb_advance_iter(iter);
4625 return event;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004626
4627 case RINGBUF_TYPE_TIME_EXTEND:
4628 /* Internal data, OK to advance */
4629 rb_advance_iter(iter);
4630 goto again;
4631
4632 case RINGBUF_TYPE_TIME_STAMP:
Tom Zanussidc4e2802018-01-15 20:51:40 -06004633 if (ts) {
4634 *ts = ring_buffer_event_time_stamp(event);
4635 ring_buffer_normalize_time_stamp(cpu_buffer->buffer,
4636 cpu_buffer->cpu, ts);
4637 }
4638 /* Internal data, OK to advance */
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004639 rb_advance_iter(iter);
4640 goto again;
4641
4642 case RINGBUF_TYPE_DATA:
Tom Zanussidc4e2802018-01-15 20:51:40 -06004643 if (ts && !(*ts)) {
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004644 *ts = iter->read_stamp + event->time_delta;
Steven Rostedt37886f62009-03-17 17:22:06 -04004645 ring_buffer_normalize_time_stamp(buffer,
4646 cpu_buffer->cpu, ts);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004647 }
4648 return event;
4649
4650 default:
Steven Rostedt (VMware)da4d4012020-05-13 15:36:22 -04004651 RB_WARN_ON(cpu_buffer, 1);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004652 }
4653
4654 return NULL;
4655}
Robert Richterc4f50182008-12-11 16:49:22 +01004656EXPORT_SYMBOL_GPL(ring_buffer_iter_peek);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004657
Steven Rostedt (Red Hat)289a5a22015-05-28 13:14:51 -04004658static inline bool rb_reader_lock(struct ring_buffer_per_cpu *cpu_buffer)
Steven Rostedt8d707e82009-06-16 21:22:48 -04004659{
Steven Rostedt (Red Hat)289a5a22015-05-28 13:14:51 -04004660 if (likely(!in_nmi())) {
4661 raw_spin_lock(&cpu_buffer->reader_lock);
4662 return true;
4663 }
4664
Steven Rostedt8d707e82009-06-16 21:22:48 -04004665 /*
4666 * If an NMI die dumps out the content of the ring buffer
Steven Rostedt (Red Hat)289a5a22015-05-28 13:14:51 -04004667 * trylock must be used to prevent a deadlock if the NMI
4668 * preempted a task that holds the ring buffer locks. If
4669 * we get the lock then all is fine, if not, then continue
4670 * to do the read, but this can corrupt the ring buffer,
4671 * so it must be permanently disabled from future writes.
4672 * Reading from NMI is a oneshot deal.
Steven Rostedt8d707e82009-06-16 21:22:48 -04004673 */
Steven Rostedt (Red Hat)289a5a22015-05-28 13:14:51 -04004674 if (raw_spin_trylock(&cpu_buffer->reader_lock))
4675 return true;
Steven Rostedt8d707e82009-06-16 21:22:48 -04004676
Steven Rostedt (Red Hat)289a5a22015-05-28 13:14:51 -04004677 /* Continue without locking, but disable the ring buffer */
4678 atomic_inc(&cpu_buffer->record_disabled);
4679 return false;
4680}
4681
4682static inline void
4683rb_reader_unlock(struct ring_buffer_per_cpu *cpu_buffer, bool locked)
4684{
4685 if (likely(locked))
4686 raw_spin_unlock(&cpu_buffer->reader_lock);
4687 return;
Steven Rostedt8d707e82009-06-16 21:22:48 -04004688}
4689
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004690/**
Steven Rostedtf83c9d02008-11-11 18:47:44 +01004691 * ring_buffer_peek - peek at the next event to be read
4692 * @buffer: The ring buffer to read
4693 * @cpu: The cpu to peak at
4694 * @ts: The timestamp counter of this event.
Steven Rostedt66a8cb92010-03-31 13:21:56 -04004695 * @lost_events: a variable to store if events were lost (may be NULL)
Steven Rostedtf83c9d02008-11-11 18:47:44 +01004696 *
4697 * This will return the event that will be read next, but does
4698 * not consume the data.
4699 */
4700struct ring_buffer_event *
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05004701ring_buffer_peek(struct trace_buffer *buffer, int cpu, u64 *ts,
Steven Rostedt66a8cb92010-03-31 13:21:56 -04004702 unsigned long *lost_events)
Steven Rostedtf83c9d02008-11-11 18:47:44 +01004703{
4704 struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
Steven Rostedt8aabee52009-03-12 13:13:49 -04004705 struct ring_buffer_event *event;
Steven Rostedtf83c9d02008-11-11 18:47:44 +01004706 unsigned long flags;
Steven Rostedt (Red Hat)289a5a22015-05-28 13:14:51 -04004707 bool dolock;
Steven Rostedtf83c9d02008-11-11 18:47:44 +01004708
Steven Rostedt554f7862009-03-11 22:00:13 -04004709 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04004710 return NULL;
Steven Rostedt554f7862009-03-11 22:00:13 -04004711
Tom Zanussi2d622712009-03-22 03:30:49 -05004712 again:
Steven Rostedt8d707e82009-06-16 21:22:48 -04004713 local_irq_save(flags);
Steven Rostedt (Red Hat)289a5a22015-05-28 13:14:51 -04004714 dolock = rb_reader_lock(cpu_buffer);
Steven Rostedt66a8cb92010-03-31 13:21:56 -04004715 event = rb_buffer_peek(cpu_buffer, ts, lost_events);
Robert Richter469535a2009-07-30 19:19:18 +02004716 if (event && event->type_len == RINGBUF_TYPE_PADDING)
4717 rb_advance_reader(cpu_buffer);
Steven Rostedt (Red Hat)289a5a22015-05-28 13:14:51 -04004718 rb_reader_unlock(cpu_buffer, dolock);
Steven Rostedt8d707e82009-06-16 21:22:48 -04004719 local_irq_restore(flags);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01004720
Steven Rostedt1b959e12009-09-03 10:12:13 -04004721 if (event && event->type_len == RINGBUF_TYPE_PADDING)
Tom Zanussi2d622712009-03-22 03:30:49 -05004722 goto again;
Tom Zanussi2d622712009-03-22 03:30:49 -05004723
Steven Rostedtf83c9d02008-11-11 18:47:44 +01004724 return event;
4725}
4726
Steven Rostedt (VMware)c9b7a4a2020-03-17 17:32:32 -04004727/** ring_buffer_iter_dropped - report if there are dropped events
4728 * @iter: The ring buffer iterator
4729 *
4730 * Returns true if there was dropped events since the last peek.
4731 */
4732bool ring_buffer_iter_dropped(struct ring_buffer_iter *iter)
4733{
4734 bool ret = iter->missed_events != 0;
4735
4736 iter->missed_events = 0;
4737 return ret;
4738}
4739EXPORT_SYMBOL_GPL(ring_buffer_iter_dropped);
4740
Steven Rostedtf83c9d02008-11-11 18:47:44 +01004741/**
4742 * ring_buffer_iter_peek - peek at the next event to be read
4743 * @iter: The ring buffer iterator
4744 * @ts: The timestamp counter of this event.
4745 *
4746 * This will return the event that will be read next, but does
4747 * not increment the iterator.
4748 */
4749struct ring_buffer_event *
4750ring_buffer_iter_peek(struct ring_buffer_iter *iter, u64 *ts)
4751{
4752 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
4753 struct ring_buffer_event *event;
4754 unsigned long flags;
4755
Tom Zanussi2d622712009-03-22 03:30:49 -05004756 again:
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02004757 raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01004758 event = rb_iter_peek(iter, ts);
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02004759 raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01004760
Steven Rostedt1b959e12009-09-03 10:12:13 -04004761 if (event && event->type_len == RINGBUF_TYPE_PADDING)
Tom Zanussi2d622712009-03-22 03:30:49 -05004762 goto again;
Tom Zanussi2d622712009-03-22 03:30:49 -05004763
Steven Rostedtf83c9d02008-11-11 18:47:44 +01004764 return event;
4765}
4766
4767/**
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004768 * ring_buffer_consume - return an event and consume it
4769 * @buffer: The ring buffer to get the next event from
Steven Rostedt66a8cb92010-03-31 13:21:56 -04004770 * @cpu: the cpu to read the buffer from
4771 * @ts: a variable to store the timestamp (may be NULL)
4772 * @lost_events: a variable to store if events were lost (may be NULL)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004773 *
4774 * Returns the next event in the ring buffer, and that event is consumed.
4775 * Meaning, that sequential reads will keep returning a different event,
4776 * and eventually empty the ring buffer if the producer is slower.
4777 */
4778struct ring_buffer_event *
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05004779ring_buffer_consume(struct trace_buffer *buffer, int cpu, u64 *ts,
Steven Rostedt66a8cb92010-03-31 13:21:56 -04004780 unsigned long *lost_events)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004781{
Steven Rostedt554f7862009-03-11 22:00:13 -04004782 struct ring_buffer_per_cpu *cpu_buffer;
4783 struct ring_buffer_event *event = NULL;
Steven Rostedtf83c9d02008-11-11 18:47:44 +01004784 unsigned long flags;
Steven Rostedt (Red Hat)289a5a22015-05-28 13:14:51 -04004785 bool dolock;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004786
Tom Zanussi2d622712009-03-22 03:30:49 -05004787 again:
Steven Rostedt554f7862009-03-11 22:00:13 -04004788 /* might be called in atomic */
4789 preempt_disable();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004790
Steven Rostedt554f7862009-03-11 22:00:13 -04004791 if (!cpumask_test_cpu(cpu, buffer->cpumask))
4792 goto out;
4793
4794 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt8d707e82009-06-16 21:22:48 -04004795 local_irq_save(flags);
Steven Rostedt (Red Hat)289a5a22015-05-28 13:14:51 -04004796 dolock = rb_reader_lock(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004797
Steven Rostedt66a8cb92010-03-31 13:21:56 -04004798 event = rb_buffer_peek(cpu_buffer, ts, lost_events);
4799 if (event) {
4800 cpu_buffer->lost_events = 0;
Robert Richter469535a2009-07-30 19:19:18 +02004801 rb_advance_reader(cpu_buffer);
Steven Rostedt66a8cb92010-03-31 13:21:56 -04004802 }
Steven Rostedtf83c9d02008-11-11 18:47:44 +01004803
Steven Rostedt (Red Hat)289a5a22015-05-28 13:14:51 -04004804 rb_reader_unlock(cpu_buffer, dolock);
Steven Rostedt8d707e82009-06-16 21:22:48 -04004805 local_irq_restore(flags);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01004806
Steven Rostedt554f7862009-03-11 22:00:13 -04004807 out:
4808 preempt_enable();
4809
Steven Rostedt1b959e12009-09-03 10:12:13 -04004810 if (event && event->type_len == RINGBUF_TYPE_PADDING)
Tom Zanussi2d622712009-03-22 03:30:49 -05004811 goto again;
Tom Zanussi2d622712009-03-22 03:30:49 -05004812
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004813 return event;
4814}
Robert Richterc4f50182008-12-11 16:49:22 +01004815EXPORT_SYMBOL_GPL(ring_buffer_consume);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004816
4817/**
David Miller72c9ddf2010-04-20 15:47:11 -07004818 * ring_buffer_read_prepare - Prepare for a non consuming read of the buffer
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004819 * @buffer: The ring buffer to read from
4820 * @cpu: The cpu buffer to iterate over
Douglas Anderson31b265b2019-03-08 11:32:04 -08004821 * @flags: gfp flags to use for memory allocation
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004822 *
David Miller72c9ddf2010-04-20 15:47:11 -07004823 * This performs the initial preparations necessary to iterate
4824 * through the buffer. Memory is allocated, buffer recording
4825 * is disabled, and the iterator pointer is returned to the caller.
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004826 *
Steven Rostedt (VMware)6167c202018-05-16 11:17:06 -04004827 * Disabling buffer recording prevents the reading from being
David Miller72c9ddf2010-04-20 15:47:11 -07004828 * corrupted. This is not a consuming read, so a producer is not
4829 * expected.
4830 *
4831 * After a sequence of ring_buffer_read_prepare calls, the user is
zhangwei(Jovi)d6118512013-07-15 16:32:50 +08004832 * expected to make at least one call to ring_buffer_read_prepare_sync.
David Miller72c9ddf2010-04-20 15:47:11 -07004833 * Afterwards, ring_buffer_read_start is invoked to get things going
4834 * for real.
4835 *
zhangwei(Jovi)d6118512013-07-15 16:32:50 +08004836 * This overall must be paired with ring_buffer_read_finish.
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004837 */
4838struct ring_buffer_iter *
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05004839ring_buffer_read_prepare(struct trace_buffer *buffer, int cpu, gfp_t flags)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004840{
4841 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedt8aabee52009-03-12 13:13:49 -04004842 struct ring_buffer_iter *iter;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004843
Rusty Russell9e01c1b2009-01-01 10:12:22 +10304844 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04004845 return NULL;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004846
Steven Rostedt (VMware)785888c2020-03-17 17:32:27 -04004847 iter = kzalloc(sizeof(*iter), flags);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004848 if (!iter)
Steven Rostedt8aabee52009-03-12 13:13:49 -04004849 return NULL;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004850
Steven Rostedt (VMware)785888c2020-03-17 17:32:27 -04004851 iter->event = kmalloc(BUF_MAX_DATA_SIZE, flags);
4852 if (!iter->event) {
4853 kfree(iter);
4854 return NULL;
4855 }
4856
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004857 cpu_buffer = buffer->buffers[cpu];
4858
4859 iter->cpu_buffer = cpu_buffer;
4860
Steven Rostedt (VMware)07b8b102020-03-27 16:21:22 -04004861 atomic_inc(&cpu_buffer->resize_disabled);
David Miller72c9ddf2010-04-20 15:47:11 -07004862
4863 return iter;
4864}
4865EXPORT_SYMBOL_GPL(ring_buffer_read_prepare);
4866
4867/**
4868 * ring_buffer_read_prepare_sync - Synchronize a set of prepare calls
4869 *
4870 * All previously invoked ring_buffer_read_prepare calls to prepare
4871 * iterators will be synchronized. Afterwards, read_buffer_read_start
4872 * calls on those iterators are allowed.
4873 */
4874void
4875ring_buffer_read_prepare_sync(void)
4876{
Paul E. McKenney74401722018-11-06 18:44:52 -08004877 synchronize_rcu();
David Miller72c9ddf2010-04-20 15:47:11 -07004878}
4879EXPORT_SYMBOL_GPL(ring_buffer_read_prepare_sync);
4880
4881/**
4882 * ring_buffer_read_start - start a non consuming read of the buffer
4883 * @iter: The iterator returned by ring_buffer_read_prepare
4884 *
4885 * This finalizes the startup of an iteration through the buffer.
4886 * The iterator comes from a call to ring_buffer_read_prepare and
4887 * an intervening ring_buffer_read_prepare_sync must have been
4888 * performed.
4889 *
zhangwei(Jovi)d6118512013-07-15 16:32:50 +08004890 * Must be paired with ring_buffer_read_finish.
David Miller72c9ddf2010-04-20 15:47:11 -07004891 */
4892void
4893ring_buffer_read_start(struct ring_buffer_iter *iter)
4894{
4895 struct ring_buffer_per_cpu *cpu_buffer;
4896 unsigned long flags;
4897
4898 if (!iter)
4899 return;
4900
4901 cpu_buffer = iter->cpu_buffer;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004902
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02004903 raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01004904 arch_spin_lock(&cpu_buffer->lock);
Steven Rostedt642edba2008-11-12 00:01:26 -05004905 rb_iter_reset(iter);
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01004906 arch_spin_unlock(&cpu_buffer->lock);
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02004907 raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004908}
Robert Richterc4f50182008-12-11 16:49:22 +01004909EXPORT_SYMBOL_GPL(ring_buffer_read_start);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004910
4911/**
zhangwei(Jovi)d6118512013-07-15 16:32:50 +08004912 * ring_buffer_read_finish - finish reading the iterator of the buffer
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004913 * @iter: The iterator retrieved by ring_buffer_start
4914 *
4915 * This re-enables the recording to the buffer, and frees the
4916 * iterator.
4917 */
4918void
4919ring_buffer_read_finish(struct ring_buffer_iter *iter)
4920{
4921 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
Steven Rostedt9366c1b2012-11-29 22:31:16 -05004922 unsigned long flags;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004923
Steven Rostedt659f4512012-05-14 17:02:33 -04004924 /*
4925 * Ring buffer is disabled from recording, here's a good place
Steven Rostedt9366c1b2012-11-29 22:31:16 -05004926 * to check the integrity of the ring buffer.
4927 * Must prevent readers from trying to read, as the check
4928 * clears the HEAD page and readers require it.
Steven Rostedt659f4512012-05-14 17:02:33 -04004929 */
Steven Rostedt9366c1b2012-11-29 22:31:16 -05004930 raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
Steven Rostedt659f4512012-05-14 17:02:33 -04004931 rb_check_pages(cpu_buffer);
Steven Rostedt9366c1b2012-11-29 22:31:16 -05004932 raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
Steven Rostedt659f4512012-05-14 17:02:33 -04004933
Steven Rostedt (VMware)07b8b102020-03-27 16:21:22 -04004934 atomic_dec(&cpu_buffer->resize_disabled);
Steven Rostedt (VMware)785888c2020-03-17 17:32:27 -04004935 kfree(iter->event);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004936 kfree(iter);
4937}
Robert Richterc4f50182008-12-11 16:49:22 +01004938EXPORT_SYMBOL_GPL(ring_buffer_read_finish);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004939
4940/**
Steven Rostedt (VMware)bc1a72a2020-03-17 17:32:25 -04004941 * ring_buffer_iter_advance - advance the iterator to the next location
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004942 * @iter: The ring buffer iterator
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004943 *
Steven Rostedt (VMware)bc1a72a2020-03-17 17:32:25 -04004944 * Move the location of the iterator such that the next read will
4945 * be the next location of the iterator.
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004946 */
Steven Rostedt (VMware)bc1a72a2020-03-17 17:32:25 -04004947void ring_buffer_iter_advance(struct ring_buffer_iter *iter)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004948{
Steven Rostedtf83c9d02008-11-11 18:47:44 +01004949 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
4950 unsigned long flags;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004951
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02004952 raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
Steven Rostedt7e9391c2009-09-03 10:02:09 -04004953
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004954 rb_advance_iter(iter);
4955
Steven Rostedt (VMware)bc1a72a2020-03-17 17:32:25 -04004956 raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004957}
Steven Rostedt (VMware)bc1a72a2020-03-17 17:32:25 -04004958EXPORT_SYMBOL_GPL(ring_buffer_iter_advance);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004959
4960/**
4961 * ring_buffer_size - return the size of the ring buffer (in bytes)
4962 * @buffer: The ring buffer.
Fabian Frederick59e7cff2014-06-05 20:22:05 +02004963 * @cpu: The CPU to get ring buffer size from.
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004964 */
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05004965unsigned long ring_buffer_size(struct trace_buffer *buffer, int cpu)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004966{
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08004967 /*
4968 * Earlier, this method returned
4969 * BUF_PAGE_SIZE * buffer->nr_pages
4970 * Since the nr_pages field is now removed, we have converted this to
4971 * return the per cpu buffer value.
4972 */
4973 if (!cpumask_test_cpu(cpu, buffer->cpumask))
4974 return 0;
4975
4976 return BUF_PAGE_SIZE * buffer->buffers[cpu]->nr_pages;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004977}
Robert Richterc4f50182008-12-11 16:49:22 +01004978EXPORT_SYMBOL_GPL(ring_buffer_size);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004979
4980static void
4981rb_reset_cpu(struct ring_buffer_per_cpu *cpu_buffer)
4982{
Steven Rostedt77ae3652009-03-27 11:00:29 -04004983 rb_head_page_deactivate(cpu_buffer);
4984
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004985 cpu_buffer->head_page
Steven Rostedt3adc54f2009-03-30 15:32:01 -04004986 = list_entry(cpu_buffer->pages, struct buffer_page, list);
Steven Rostedtbf41a152008-10-04 02:00:59 -04004987 local_set(&cpu_buffer->head_page->write, 0);
Steven Rostedt778c55d2009-05-01 18:44:45 -04004988 local_set(&cpu_buffer->head_page->entries, 0);
Steven Rostedtabc9b562008-12-02 15:34:06 -05004989 local_set(&cpu_buffer->head_page->page->commit, 0);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004990
Steven Rostedt6f807ac2008-10-04 02:00:58 -04004991 cpu_buffer->head_page->read = 0;
Steven Rostedtbf41a152008-10-04 02:00:59 -04004992
4993 cpu_buffer->tail_page = cpu_buffer->head_page;
4994 cpu_buffer->commit_page = cpu_buffer->head_page;
4995
4996 INIT_LIST_HEAD(&cpu_buffer->reader_page->list);
Vaibhav Nagarnaik5040b4b2012-05-03 18:59:51 -07004997 INIT_LIST_HEAD(&cpu_buffer->new_pages);
Steven Rostedtbf41a152008-10-04 02:00:59 -04004998 local_set(&cpu_buffer->reader_page->write, 0);
Steven Rostedt778c55d2009-05-01 18:44:45 -04004999 local_set(&cpu_buffer->reader_page->entries, 0);
Steven Rostedtabc9b562008-12-02 15:34:06 -05005000 local_set(&cpu_buffer->reader_page->page->commit, 0);
Steven Rostedt6f807ac2008-10-04 02:00:58 -04005001 cpu_buffer->reader_page->read = 0;
Steven Rostedtd7690412008-10-01 00:29:53 -04005002
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -07005003 local_set(&cpu_buffer->entries_bytes, 0);
Steven Rostedt77ae3652009-03-27 11:00:29 -04005004 local_set(&cpu_buffer->overrun, 0);
Slava Pestov884bfe82011-07-15 14:23:58 -07005005 local_set(&cpu_buffer->commit_overrun, 0);
5006 local_set(&cpu_buffer->dropped_events, 0);
Steven Rostedte4906ef2009-04-30 20:49:44 -04005007 local_set(&cpu_buffer->entries, 0);
Steven Rostedtfa743952009-06-16 12:37:57 -04005008 local_set(&cpu_buffer->committing, 0);
5009 local_set(&cpu_buffer->commits, 0);
Steven Rostedt (VMware)2c2b0a72018-11-29 20:32:26 -05005010 local_set(&cpu_buffer->pages_touched, 0);
5011 local_set(&cpu_buffer->pages_read, 0);
Steven Rostedt (VMware)03329f92018-11-29 21:38:42 -05005012 cpu_buffer->last_pages_touch = 0;
Steven Rostedt (VMware)2c2b0a72018-11-29 20:32:26 -05005013 cpu_buffer->shortest_full = 0;
Steven Rostedt77ae3652009-03-27 11:00:29 -04005014 cpu_buffer->read = 0;
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -07005015 cpu_buffer->read_bytes = 0;
Steven Rostedt69507c02009-01-21 18:45:57 -05005016
Steven Rostedt (VMware)10464b42020-06-28 22:52:27 -04005017 rb_time_set(&cpu_buffer->write_stamp, 0);
5018 rb_time_set(&cpu_buffer->before_stamp, 0);
Steven Rostedt77ae3652009-03-27 11:00:29 -04005019
Steven Rostedt66a8cb92010-03-31 13:21:56 -04005020 cpu_buffer->lost_events = 0;
5021 cpu_buffer->last_overrun = 0;
5022
Steven Rostedt77ae3652009-03-27 11:00:29 -04005023 rb_head_page_activate(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04005024}
5025
Nicholas Pigginb23d7a5f2020-06-25 15:34:03 +10005026/* Must have disabled the cpu buffer then done a synchronize_rcu */
5027static void reset_disabled_cpu_buffer(struct ring_buffer_per_cpu *cpu_buffer)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04005028{
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04005029 unsigned long flags;
5030
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02005031 raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01005032
Steven Rostedt41b6a952009-09-02 09:59:48 -04005033 if (RB_WARN_ON(cpu_buffer, local_read(&cpu_buffer->committing)))
5034 goto out;
5035
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01005036 arch_spin_lock(&cpu_buffer->lock);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04005037
5038 rb_reset_cpu(cpu_buffer);
5039
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01005040 arch_spin_unlock(&cpu_buffer->lock);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01005041
Steven Rostedt41b6a952009-09-02 09:59:48 -04005042 out:
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02005043 raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
Nicholas Pigginb23d7a5f2020-06-25 15:34:03 +10005044}
5045
5046/**
5047 * ring_buffer_reset_cpu - reset a ring buffer per CPU buffer
5048 * @buffer: The ring buffer to reset a per cpu buffer of
5049 * @cpu: The CPU buffer to be reset
5050 */
5051void ring_buffer_reset_cpu(struct trace_buffer *buffer, int cpu)
5052{
5053 struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
5054
5055 if (!cpumask_test_cpu(cpu, buffer->cpumask))
5056 return;
5057
Gaurav Kohlibbeb9742020-10-06 15:03:53 +05305058 /* prevent another thread from changing buffer sizes */
5059 mutex_lock(&buffer->mutex);
5060
Nicholas Pigginb23d7a5f2020-06-25 15:34:03 +10005061 atomic_inc(&cpu_buffer->resize_disabled);
5062 atomic_inc(&cpu_buffer->record_disabled);
5063
5064 /* Make sure all commits have finished */
5065 synchronize_rcu();
5066
5067 reset_disabled_cpu_buffer(cpu_buffer);
Steven Rostedt41ede232009-05-01 20:26:54 -04005068
5069 atomic_dec(&cpu_buffer->record_disabled);
Steven Rostedt (VMware)07b8b102020-03-27 16:21:22 -04005070 atomic_dec(&cpu_buffer->resize_disabled);
Gaurav Kohlibbeb9742020-10-06 15:03:53 +05305071
5072 mutex_unlock(&buffer->mutex);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04005073}
Robert Richterc4f50182008-12-11 16:49:22 +01005074EXPORT_SYMBOL_GPL(ring_buffer_reset_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04005075
5076/**
Nicholas Pigginb23d7a5f2020-06-25 15:34:03 +10005077 * ring_buffer_reset_cpu - reset a ring buffer per CPU buffer
5078 * @buffer: The ring buffer to reset a per cpu buffer of
5079 * @cpu: The CPU buffer to be reset
5080 */
5081void ring_buffer_reset_online_cpus(struct trace_buffer *buffer)
5082{
5083 struct ring_buffer_per_cpu *cpu_buffer;
5084 int cpu;
5085
Gaurav Kohlibbeb9742020-10-06 15:03:53 +05305086 /* prevent another thread from changing buffer sizes */
5087 mutex_lock(&buffer->mutex);
5088
Nicholas Pigginb23d7a5f2020-06-25 15:34:03 +10005089 for_each_online_buffer_cpu(buffer, cpu) {
5090 cpu_buffer = buffer->buffers[cpu];
5091
5092 atomic_inc(&cpu_buffer->resize_disabled);
5093 atomic_inc(&cpu_buffer->record_disabled);
5094 }
5095
5096 /* Make sure all commits have finished */
5097 synchronize_rcu();
5098
5099 for_each_online_buffer_cpu(buffer, cpu) {
5100 cpu_buffer = buffer->buffers[cpu];
5101
5102 reset_disabled_cpu_buffer(cpu_buffer);
5103
5104 atomic_dec(&cpu_buffer->record_disabled);
5105 atomic_dec(&cpu_buffer->resize_disabled);
5106 }
Gaurav Kohlibbeb9742020-10-06 15:03:53 +05305107
5108 mutex_unlock(&buffer->mutex);
Nicholas Pigginb23d7a5f2020-06-25 15:34:03 +10005109}
5110
5111/**
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04005112 * ring_buffer_reset - reset a ring buffer
5113 * @buffer: The ring buffer to reset all cpu buffers
5114 */
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05005115void ring_buffer_reset(struct trace_buffer *buffer)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04005116{
Nicholas Pigginb23d7a5f2020-06-25 15:34:03 +10005117 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04005118 int cpu;
5119
Nicholas Pigginb23d7a5f2020-06-25 15:34:03 +10005120 for_each_buffer_cpu(buffer, cpu) {
5121 cpu_buffer = buffer->buffers[cpu];
5122
5123 atomic_inc(&cpu_buffer->resize_disabled);
5124 atomic_inc(&cpu_buffer->record_disabled);
5125 }
5126
5127 /* Make sure all commits have finished */
5128 synchronize_rcu();
5129
5130 for_each_buffer_cpu(buffer, cpu) {
5131 cpu_buffer = buffer->buffers[cpu];
5132
5133 reset_disabled_cpu_buffer(cpu_buffer);
5134
5135 atomic_dec(&cpu_buffer->record_disabled);
5136 atomic_dec(&cpu_buffer->resize_disabled);
5137 }
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04005138}
Robert Richterc4f50182008-12-11 16:49:22 +01005139EXPORT_SYMBOL_GPL(ring_buffer_reset);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04005140
5141/**
5142 * rind_buffer_empty - is the ring buffer empty?
5143 * @buffer: The ring buffer to test
5144 */
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05005145bool ring_buffer_empty(struct trace_buffer *buffer)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04005146{
5147 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedtd4788202009-06-17 00:39:43 -04005148 unsigned long flags;
Steven Rostedt (Red Hat)289a5a22015-05-28 13:14:51 -04005149 bool dolock;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04005150 int cpu;
Steven Rostedtd4788202009-06-17 00:39:43 -04005151 int ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04005152
5153 /* yes this is racy, but if you don't like the race, lock the buffer */
5154 for_each_buffer_cpu(buffer, cpu) {
5155 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt8d707e82009-06-16 21:22:48 -04005156 local_irq_save(flags);
Steven Rostedt (Red Hat)289a5a22015-05-28 13:14:51 -04005157 dolock = rb_reader_lock(cpu_buffer);
Steven Rostedtd4788202009-06-17 00:39:43 -04005158 ret = rb_per_cpu_empty(cpu_buffer);
Steven Rostedt (Red Hat)289a5a22015-05-28 13:14:51 -04005159 rb_reader_unlock(cpu_buffer, dolock);
Steven Rostedt8d707e82009-06-16 21:22:48 -04005160 local_irq_restore(flags);
5161
Steven Rostedtd4788202009-06-17 00:39:43 -04005162 if (!ret)
Yaowei Bai3d4e2042015-09-29 22:43:32 +08005163 return false;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04005164 }
Steven Rostedt554f7862009-03-11 22:00:13 -04005165
Yaowei Bai3d4e2042015-09-29 22:43:32 +08005166 return true;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04005167}
Robert Richterc4f50182008-12-11 16:49:22 +01005168EXPORT_SYMBOL_GPL(ring_buffer_empty);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04005169
5170/**
5171 * ring_buffer_empty_cpu - is a cpu buffer of a ring buffer empty?
5172 * @buffer: The ring buffer
5173 * @cpu: The CPU buffer to test
5174 */
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05005175bool ring_buffer_empty_cpu(struct trace_buffer *buffer, int cpu)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04005176{
5177 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedtd4788202009-06-17 00:39:43 -04005178 unsigned long flags;
Steven Rostedt (Red Hat)289a5a22015-05-28 13:14:51 -04005179 bool dolock;
Steven Rostedt8aabee52009-03-12 13:13:49 -04005180 int ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04005181
Rusty Russell9e01c1b2009-01-01 10:12:22 +10305182 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Yaowei Bai3d4e2042015-09-29 22:43:32 +08005183 return true;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04005184
5185 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt8d707e82009-06-16 21:22:48 -04005186 local_irq_save(flags);
Steven Rostedt (Red Hat)289a5a22015-05-28 13:14:51 -04005187 dolock = rb_reader_lock(cpu_buffer);
Steven Rostedt554f7862009-03-11 22:00:13 -04005188 ret = rb_per_cpu_empty(cpu_buffer);
Steven Rostedt (Red Hat)289a5a22015-05-28 13:14:51 -04005189 rb_reader_unlock(cpu_buffer, dolock);
Steven Rostedt8d707e82009-06-16 21:22:48 -04005190 local_irq_restore(flags);
Steven Rostedt554f7862009-03-11 22:00:13 -04005191
5192 return ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04005193}
Robert Richterc4f50182008-12-11 16:49:22 +01005194EXPORT_SYMBOL_GPL(ring_buffer_empty_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04005195
Steven Rostedt85bac322009-09-04 14:24:40 -04005196#ifdef CONFIG_RING_BUFFER_ALLOW_SWAP
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04005197/**
5198 * ring_buffer_swap_cpu - swap a CPU buffer between two ring buffers
5199 * @buffer_a: One buffer to swap with
5200 * @buffer_b: The other buffer to swap with
Fabian Frederick59e7cff2014-06-05 20:22:05 +02005201 * @cpu: the CPU of the buffers to swap
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04005202 *
5203 * This function is useful for tracers that want to take a "snapshot"
5204 * of a CPU buffer and has another back up buffer lying around.
5205 * it is expected that the tracer handles the cpu buffer not being
5206 * used at the moment.
5207 */
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05005208int ring_buffer_swap_cpu(struct trace_buffer *buffer_a,
5209 struct trace_buffer *buffer_b, int cpu)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04005210{
5211 struct ring_buffer_per_cpu *cpu_buffer_a;
5212 struct ring_buffer_per_cpu *cpu_buffer_b;
Steven Rostedt554f7862009-03-11 22:00:13 -04005213 int ret = -EINVAL;
5214
Rusty Russell9e01c1b2009-01-01 10:12:22 +10305215 if (!cpumask_test_cpu(cpu, buffer_a->cpumask) ||
5216 !cpumask_test_cpu(cpu, buffer_b->cpumask))
Steven Rostedt554f7862009-03-11 22:00:13 -04005217 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04005218
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08005219 cpu_buffer_a = buffer_a->buffers[cpu];
5220 cpu_buffer_b = buffer_b->buffers[cpu];
5221
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04005222 /* At least make sure the two buffers are somewhat the same */
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08005223 if (cpu_buffer_a->nr_pages != cpu_buffer_b->nr_pages)
Steven Rostedt554f7862009-03-11 22:00:13 -04005224 goto out;
5225
5226 ret = -EAGAIN;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04005227
Steven Rostedt97b17ef2009-01-21 15:24:56 -05005228 if (atomic_read(&buffer_a->record_disabled))
Steven Rostedt554f7862009-03-11 22:00:13 -04005229 goto out;
Steven Rostedt97b17ef2009-01-21 15:24:56 -05005230
5231 if (atomic_read(&buffer_b->record_disabled))
Steven Rostedt554f7862009-03-11 22:00:13 -04005232 goto out;
Steven Rostedt97b17ef2009-01-21 15:24:56 -05005233
Steven Rostedt97b17ef2009-01-21 15:24:56 -05005234 if (atomic_read(&cpu_buffer_a->record_disabled))
Steven Rostedt554f7862009-03-11 22:00:13 -04005235 goto out;
Steven Rostedt97b17ef2009-01-21 15:24:56 -05005236
5237 if (atomic_read(&cpu_buffer_b->record_disabled))
Steven Rostedt554f7862009-03-11 22:00:13 -04005238 goto out;
Steven Rostedt97b17ef2009-01-21 15:24:56 -05005239
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04005240 /*
Paul E. McKenney74401722018-11-06 18:44:52 -08005241 * We can't do a synchronize_rcu here because this
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04005242 * function can be called in atomic context.
5243 * Normally this will be called from the same CPU as cpu.
5244 * If not it's up to the caller to protect this.
5245 */
5246 atomic_inc(&cpu_buffer_a->record_disabled);
5247 atomic_inc(&cpu_buffer_b->record_disabled);
5248
Steven Rostedt98277992009-09-02 10:56:15 -04005249 ret = -EBUSY;
5250 if (local_read(&cpu_buffer_a->committing))
5251 goto out_dec;
5252 if (local_read(&cpu_buffer_b->committing))
5253 goto out_dec;
5254
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04005255 buffer_a->buffers[cpu] = cpu_buffer_b;
5256 buffer_b->buffers[cpu] = cpu_buffer_a;
5257
5258 cpu_buffer_b->buffer = buffer_a;
5259 cpu_buffer_a->buffer = buffer_b;
5260
Steven Rostedt98277992009-09-02 10:56:15 -04005261 ret = 0;
5262
5263out_dec:
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04005264 atomic_dec(&cpu_buffer_a->record_disabled);
5265 atomic_dec(&cpu_buffer_b->record_disabled);
Steven Rostedt554f7862009-03-11 22:00:13 -04005266out:
Steven Rostedt554f7862009-03-11 22:00:13 -04005267 return ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04005268}
Robert Richterc4f50182008-12-11 16:49:22 +01005269EXPORT_SYMBOL_GPL(ring_buffer_swap_cpu);
Steven Rostedt85bac322009-09-04 14:24:40 -04005270#endif /* CONFIG_RING_BUFFER_ALLOW_SWAP */
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04005271
Steven Rostedt8789a9e2008-12-02 15:34:07 -05005272/**
5273 * ring_buffer_alloc_read_page - allocate a page to read from buffer
5274 * @buffer: the buffer to allocate for.
zhangwei(Jovi)d6118512013-07-15 16:32:50 +08005275 * @cpu: the cpu buffer to allocate.
Steven Rostedt8789a9e2008-12-02 15:34:07 -05005276 *
5277 * This function is used in conjunction with ring_buffer_read_page.
5278 * When reading a full page from the ring buffer, these functions
5279 * can be used to speed up the process. The calling function should
5280 * allocate a few pages first with this function. Then when it
5281 * needs to get pages from the ring buffer, it passes the result
5282 * of this function into ring_buffer_read_page, which will swap
5283 * the page that was allocated, with the read page of the buffer.
5284 *
5285 * Returns:
Steven Rostedt (VMware)a7e52ad2017-08-02 14:20:54 -04005286 * The page allocated, or ERR_PTR
Steven Rostedt8789a9e2008-12-02 15:34:07 -05005287 */
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05005288void *ring_buffer_alloc_read_page(struct trace_buffer *buffer, int cpu)
Steven Rostedt8789a9e2008-12-02 15:34:07 -05005289{
Steven Rostedt (VMware)a7e52ad2017-08-02 14:20:54 -04005290 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedt (VMware)73a757e2017-05-01 09:35:09 -04005291 struct buffer_data_page *bpage = NULL;
5292 unsigned long flags;
Vaibhav Nagarnaik7ea59062011-05-03 17:56:42 -07005293 struct page *page;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05005294
Steven Rostedt (VMware)a7e52ad2017-08-02 14:20:54 -04005295 if (!cpumask_test_cpu(cpu, buffer->cpumask))
5296 return ERR_PTR(-ENODEV);
5297
5298 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt (VMware)73a757e2017-05-01 09:35:09 -04005299 local_irq_save(flags);
5300 arch_spin_lock(&cpu_buffer->lock);
5301
5302 if (cpu_buffer->free_page) {
5303 bpage = cpu_buffer->free_page;
5304 cpu_buffer->free_page = NULL;
5305 }
5306
5307 arch_spin_unlock(&cpu_buffer->lock);
5308 local_irq_restore(flags);
5309
5310 if (bpage)
5311 goto out;
5312
Vaibhav Nagarnaikd7ec4bf2011-06-07 17:01:42 -07005313 page = alloc_pages_node(cpu_to_node(cpu),
5314 GFP_KERNEL | __GFP_NORETRY, 0);
Vaibhav Nagarnaik7ea59062011-05-03 17:56:42 -07005315 if (!page)
Steven Rostedt (VMware)a7e52ad2017-08-02 14:20:54 -04005316 return ERR_PTR(-ENOMEM);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05005317
Vaibhav Nagarnaik7ea59062011-05-03 17:56:42 -07005318 bpage = page_address(page);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05005319
Steven Rostedt (VMware)73a757e2017-05-01 09:35:09 -04005320 out:
Steven Rostedtef7a4a12009-03-03 00:27:49 -05005321 rb_init_page(bpage);
5322
Steven Rostedt044fa782008-12-02 23:50:03 -05005323 return bpage;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05005324}
Steven Rostedtd6ce96d2009-05-05 01:15:24 -04005325EXPORT_SYMBOL_GPL(ring_buffer_alloc_read_page);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05005326
5327/**
5328 * ring_buffer_free_read_page - free an allocated read page
5329 * @buffer: the buffer the page was allocate for
Steven Rostedt (VMware)73a757e2017-05-01 09:35:09 -04005330 * @cpu: the cpu buffer the page came from
Steven Rostedt8789a9e2008-12-02 15:34:07 -05005331 * @data: the page to free
5332 *
5333 * Free a page allocated from ring_buffer_alloc_read_page.
5334 */
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05005335void ring_buffer_free_read_page(struct trace_buffer *buffer, int cpu, void *data)
Steven Rostedt8789a9e2008-12-02 15:34:07 -05005336{
Steven Rostedt (VMware)73a757e2017-05-01 09:35:09 -04005337 struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
5338 struct buffer_data_page *bpage = data;
Steven Rostedt (VMware)ae415fa2017-12-22 21:19:29 -05005339 struct page *page = virt_to_page(bpage);
Steven Rostedt (VMware)73a757e2017-05-01 09:35:09 -04005340 unsigned long flags;
5341
Steven Rostedt (VMware)ae415fa2017-12-22 21:19:29 -05005342 /* If the page is still in use someplace else, we can't reuse it */
5343 if (page_ref_count(page) > 1)
5344 goto out;
5345
Steven Rostedt (VMware)73a757e2017-05-01 09:35:09 -04005346 local_irq_save(flags);
5347 arch_spin_lock(&cpu_buffer->lock);
5348
5349 if (!cpu_buffer->free_page) {
5350 cpu_buffer->free_page = bpage;
5351 bpage = NULL;
5352 }
5353
5354 arch_spin_unlock(&cpu_buffer->lock);
5355 local_irq_restore(flags);
5356
Steven Rostedt (VMware)ae415fa2017-12-22 21:19:29 -05005357 out:
Steven Rostedt (VMware)73a757e2017-05-01 09:35:09 -04005358 free_page((unsigned long)bpage);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05005359}
Steven Rostedtd6ce96d2009-05-05 01:15:24 -04005360EXPORT_SYMBOL_GPL(ring_buffer_free_read_page);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05005361
5362/**
5363 * ring_buffer_read_page - extract a page from the ring buffer
5364 * @buffer: buffer to extract from
5365 * @data_page: the page to use allocated from ring_buffer_alloc_read_page
Steven Rostedtef7a4a12009-03-03 00:27:49 -05005366 * @len: amount to extract
Steven Rostedt8789a9e2008-12-02 15:34:07 -05005367 * @cpu: the cpu of the buffer to extract
5368 * @full: should the extraction only happen when the page is full.
5369 *
5370 * This function will pull out a page from the ring buffer and consume it.
5371 * @data_page must be the address of the variable that was returned
5372 * from ring_buffer_alloc_read_page. This is because the page might be used
5373 * to swap with a page in the ring buffer.
5374 *
5375 * for example:
zhangwei(Jovi)d6118512013-07-15 16:32:50 +08005376 * rpage = ring_buffer_alloc_read_page(buffer, cpu);
Steven Rostedt (VMware)a7e52ad2017-08-02 14:20:54 -04005377 * if (IS_ERR(rpage))
5378 * return PTR_ERR(rpage);
Steven Rostedtef7a4a12009-03-03 00:27:49 -05005379 * ret = ring_buffer_read_page(buffer, &rpage, len, cpu, 0);
Lai Jiangshan667d2412009-02-09 14:21:17 +08005380 * if (ret >= 0)
5381 * process_page(rpage, ret);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05005382 *
5383 * When @full is set, the function will not return true unless
5384 * the writer is off the reader page.
5385 *
5386 * Note: it is up to the calling functions to handle sleeps and wakeups.
5387 * The ring buffer can be used anywhere in the kernel and can not
5388 * blindly call wake_up. The layer that uses the ring buffer must be
5389 * responsible for that.
5390 *
5391 * Returns:
Lai Jiangshan667d2412009-02-09 14:21:17 +08005392 * >=0 if data has been transferred, returns the offset of consumed data.
5393 * <0 if no data has been transferred.
Steven Rostedt8789a9e2008-12-02 15:34:07 -05005394 */
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05005395int ring_buffer_read_page(struct trace_buffer *buffer,
Steven Rostedtef7a4a12009-03-03 00:27:49 -05005396 void **data_page, size_t len, int cpu, int full)
Steven Rostedt8789a9e2008-12-02 15:34:07 -05005397{
5398 struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
5399 struct ring_buffer_event *event;
Steven Rostedt044fa782008-12-02 23:50:03 -05005400 struct buffer_data_page *bpage;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05005401 struct buffer_page *reader;
Steven Rostedtff0ff842010-03-31 22:11:42 -04005402 unsigned long missed_events;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05005403 unsigned long flags;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05005404 unsigned int commit;
Lai Jiangshan667d2412009-02-09 14:21:17 +08005405 unsigned int read;
Steven Rostedt4f3640f2009-03-03 23:52:42 -05005406 u64 save_timestamp;
Lai Jiangshan667d2412009-02-09 14:21:17 +08005407 int ret = -1;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05005408
Steven Rostedt554f7862009-03-11 22:00:13 -04005409 if (!cpumask_test_cpu(cpu, buffer->cpumask))
5410 goto out;
5411
Steven Rostedt474d32b2009-03-03 19:51:40 -05005412 /*
5413 * If len is not big enough to hold the page header, then
5414 * we can not copy anything.
5415 */
5416 if (len <= BUF_PAGE_HDR_SIZE)
Steven Rostedt554f7862009-03-11 22:00:13 -04005417 goto out;
Steven Rostedt474d32b2009-03-03 19:51:40 -05005418
5419 len -= BUF_PAGE_HDR_SIZE;
5420
Steven Rostedt8789a9e2008-12-02 15:34:07 -05005421 if (!data_page)
Steven Rostedt554f7862009-03-11 22:00:13 -04005422 goto out;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05005423
Steven Rostedt044fa782008-12-02 23:50:03 -05005424 bpage = *data_page;
5425 if (!bpage)
Steven Rostedt554f7862009-03-11 22:00:13 -04005426 goto out;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05005427
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02005428 raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05005429
Steven Rostedtef7a4a12009-03-03 00:27:49 -05005430 reader = rb_get_reader_page(cpu_buffer);
5431 if (!reader)
Steven Rostedt554f7862009-03-11 22:00:13 -04005432 goto out_unlock;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05005433
Steven Rostedtef7a4a12009-03-03 00:27:49 -05005434 event = rb_reader_event(cpu_buffer);
Lai Jiangshan667d2412009-02-09 14:21:17 +08005435
Steven Rostedtef7a4a12009-03-03 00:27:49 -05005436 read = reader->read;
5437 commit = rb_page_commit(reader);
5438
Steven Rostedt66a8cb92010-03-31 13:21:56 -04005439 /* Check if any events were dropped */
Steven Rostedtff0ff842010-03-31 22:11:42 -04005440 missed_events = cpu_buffer->lost_events;
Steven Rostedt66a8cb92010-03-31 13:21:56 -04005441
Steven Rostedt8789a9e2008-12-02 15:34:07 -05005442 /*
Steven Rostedt474d32b2009-03-03 19:51:40 -05005443 * If this page has been partially read or
5444 * if len is not big enough to read the rest of the page or
5445 * a writer is still on the page, then
5446 * we must copy the data from the page to the buffer.
5447 * Otherwise, we can simply swap the page with the one passed in.
Steven Rostedt8789a9e2008-12-02 15:34:07 -05005448 */
Steven Rostedt474d32b2009-03-03 19:51:40 -05005449 if (read || (len < (commit - read)) ||
Steven Rostedtef7a4a12009-03-03 00:27:49 -05005450 cpu_buffer->reader_page == cpu_buffer->commit_page) {
Lai Jiangshan667d2412009-02-09 14:21:17 +08005451 struct buffer_data_page *rpage = cpu_buffer->reader_page->page;
Steven Rostedt474d32b2009-03-03 19:51:40 -05005452 unsigned int rpos = read;
5453 unsigned int pos = 0;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05005454 unsigned int size;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05005455
5456 if (full)
Steven Rostedt554f7862009-03-11 22:00:13 -04005457 goto out_unlock;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05005458
Steven Rostedtef7a4a12009-03-03 00:27:49 -05005459 if (len > (commit - read))
5460 len = (commit - read);
5461
Steven Rostedt69d1b832010-10-07 18:18:05 -04005462 /* Always keep the time extend and data together */
5463 size = rb_event_ts_length(event);
Steven Rostedtef7a4a12009-03-03 00:27:49 -05005464
5465 if (len < size)
Steven Rostedt554f7862009-03-11 22:00:13 -04005466 goto out_unlock;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05005467
Steven Rostedt4f3640f2009-03-03 23:52:42 -05005468 /* save the current timestamp, since the user will need it */
5469 save_timestamp = cpu_buffer->read_stamp;
5470
Steven Rostedtef7a4a12009-03-03 00:27:49 -05005471 /* Need to copy one event at a time */
5472 do {
David Sharpe1e35922010-12-22 16:38:24 -08005473 /* We need the size of one event, because
5474 * rb_advance_reader only advances by one event,
5475 * whereas rb_event_ts_length may include the size of
5476 * one or two events.
5477 * We have already ensured there's enough space if this
5478 * is a time extend. */
5479 size = rb_event_length(event);
Steven Rostedt474d32b2009-03-03 19:51:40 -05005480 memcpy(bpage->data + pos, rpage->data + rpos, size);
Steven Rostedtef7a4a12009-03-03 00:27:49 -05005481
5482 len -= size;
5483
5484 rb_advance_reader(cpu_buffer);
Steven Rostedt474d32b2009-03-03 19:51:40 -05005485 rpos = reader->read;
5486 pos += size;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05005487
Huang Ying18fab912010-07-28 14:14:01 +08005488 if (rpos >= commit)
5489 break;
5490
Steven Rostedtef7a4a12009-03-03 00:27:49 -05005491 event = rb_reader_event(cpu_buffer);
Steven Rostedt69d1b832010-10-07 18:18:05 -04005492 /* Always keep the time extend and data together */
5493 size = rb_event_ts_length(event);
David Sharpe1e35922010-12-22 16:38:24 -08005494 } while (len >= size);
Lai Jiangshan667d2412009-02-09 14:21:17 +08005495
5496 /* update bpage */
Steven Rostedtef7a4a12009-03-03 00:27:49 -05005497 local_set(&bpage->commit, pos);
Steven Rostedt4f3640f2009-03-03 23:52:42 -05005498 bpage->time_stamp = save_timestamp;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05005499
Steven Rostedt474d32b2009-03-03 19:51:40 -05005500 /* we copied everything to the beginning */
5501 read = 0;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05005502 } else {
Steven Rostedtafbab762009-05-01 19:40:05 -04005503 /* update the entry counter */
Steven Rostedt77ae3652009-03-27 11:00:29 -04005504 cpu_buffer->read += rb_page_entries(reader);
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -07005505 cpu_buffer->read_bytes += BUF_PAGE_SIZE;
Steven Rostedtafbab762009-05-01 19:40:05 -04005506
Steven Rostedt8789a9e2008-12-02 15:34:07 -05005507 /* swap the pages */
Steven Rostedt044fa782008-12-02 23:50:03 -05005508 rb_init_page(bpage);
Steven Rostedtef7a4a12009-03-03 00:27:49 -05005509 bpage = reader->page;
5510 reader->page = *data_page;
5511 local_set(&reader->write, 0);
Steven Rostedt778c55d2009-05-01 18:44:45 -04005512 local_set(&reader->entries, 0);
Steven Rostedtef7a4a12009-03-03 00:27:49 -05005513 reader->read = 0;
Steven Rostedt044fa782008-12-02 23:50:03 -05005514 *data_page = bpage;
Steven Rostedtff0ff842010-03-31 22:11:42 -04005515
5516 /*
5517 * Use the real_end for the data size,
5518 * This gives us a chance to store the lost events
5519 * on the page.
5520 */
5521 if (reader->real_end)
5522 local_set(&bpage->commit, reader->real_end);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05005523 }
Lai Jiangshan667d2412009-02-09 14:21:17 +08005524 ret = read;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05005525
Steven Rostedt66a8cb92010-03-31 13:21:56 -04005526 cpu_buffer->lost_events = 0;
Steven Rostedt2711ca22010-05-21 13:32:26 -04005527
5528 commit = local_read(&bpage->commit);
Steven Rostedt66a8cb92010-03-31 13:21:56 -04005529 /*
5530 * Set a flag in the commit field if we lost events
5531 */
Steven Rostedtff0ff842010-03-31 22:11:42 -04005532 if (missed_events) {
Steven Rostedtff0ff842010-03-31 22:11:42 -04005533 /* If there is room at the end of the page to save the
5534 * missed events, then record it there.
5535 */
5536 if (BUF_PAGE_SIZE - commit >= sizeof(missed_events)) {
5537 memcpy(&bpage->data[commit], &missed_events,
5538 sizeof(missed_events));
5539 local_add(RB_MISSED_STORED, &bpage->commit);
Steven Rostedt2711ca22010-05-21 13:32:26 -04005540 commit += sizeof(missed_events);
Steven Rostedtff0ff842010-03-31 22:11:42 -04005541 }
Steven Rostedt66a8cb92010-03-31 13:21:56 -04005542 local_add(RB_MISSED_EVENTS, &bpage->commit);
Steven Rostedtff0ff842010-03-31 22:11:42 -04005543 }
Steven Rostedt66a8cb92010-03-31 13:21:56 -04005544
Steven Rostedt2711ca22010-05-21 13:32:26 -04005545 /*
5546 * This page may be off to user land. Zero it out here.
5547 */
5548 if (commit < BUF_PAGE_SIZE)
5549 memset(&bpage->data[commit], 0, BUF_PAGE_SIZE - commit);
5550
Steven Rostedt554f7862009-03-11 22:00:13 -04005551 out_unlock:
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02005552 raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05005553
Steven Rostedt554f7862009-03-11 22:00:13 -04005554 out:
Steven Rostedt8789a9e2008-12-02 15:34:07 -05005555 return ret;
5556}
Steven Rostedtd6ce96d2009-05-05 01:15:24 -04005557EXPORT_SYMBOL_GPL(ring_buffer_read_page);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05005558
Sebastian Andrzej Siewiorb32614c2016-11-27 00:13:34 +01005559/*
5560 * We only allocate new buffers, never free them if the CPU goes down.
5561 * If we were to free the buffer, then the user would lose any trace that was in
5562 * the buffer.
5563 */
5564int trace_rb_cpu_prepare(unsigned int cpu, struct hlist_node *node)
Steven Rostedt554f7862009-03-11 22:00:13 -04005565{
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05005566 struct trace_buffer *buffer;
Steven Rostedt (Red Hat)9b94a8f2016-05-12 11:01:24 -04005567 long nr_pages_same;
5568 int cpu_i;
5569 unsigned long nr_pages;
Steven Rostedt554f7862009-03-11 22:00:13 -04005570
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05005571 buffer = container_of(node, struct trace_buffer, node);
Sebastian Andrzej Siewiorb32614c2016-11-27 00:13:34 +01005572 if (cpumask_test_cpu(cpu, buffer->cpumask))
5573 return 0;
Steven Rostedt554f7862009-03-11 22:00:13 -04005574
Sebastian Andrzej Siewiorb32614c2016-11-27 00:13:34 +01005575 nr_pages = 0;
5576 nr_pages_same = 1;
5577 /* check if all cpu sizes are same */
5578 for_each_buffer_cpu(buffer, cpu_i) {
5579 /* fill in the size from first enabled cpu */
5580 if (nr_pages == 0)
5581 nr_pages = buffer->buffers[cpu_i]->nr_pages;
5582 if (nr_pages != buffer->buffers[cpu_i]->nr_pages) {
5583 nr_pages_same = 0;
5584 break;
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08005585 }
Steven Rostedt554f7862009-03-11 22:00:13 -04005586 }
Sebastian Andrzej Siewiorb32614c2016-11-27 00:13:34 +01005587 /* allocate minimum pages, user can later expand it */
5588 if (!nr_pages_same)
5589 nr_pages = 2;
5590 buffer->buffers[cpu] =
5591 rb_allocate_cpu_buffer(buffer, nr_pages, cpu);
5592 if (!buffer->buffers[cpu]) {
5593 WARN(1, "failed to allocate ring buffer on CPU %u\n",
5594 cpu);
5595 return -ENOMEM;
5596 }
5597 smp_wmb();
5598 cpumask_set_cpu(cpu, buffer->cpumask);
5599 return 0;
Steven Rostedt554f7862009-03-11 22:00:13 -04005600}
Steven Rostedt (Red Hat)6c43e552013-03-15 11:32:53 -04005601
5602#ifdef CONFIG_RING_BUFFER_STARTUP_TEST
5603/*
5604 * This is a basic integrity check of the ring buffer.
5605 * Late in the boot cycle this test will run when configured in.
5606 * It will kick off a thread per CPU that will go into a loop
5607 * writing to the per cpu ring buffer various sizes of data.
5608 * Some of the data will be large items, some small.
5609 *
5610 * Another thread is created that goes into a spin, sending out
5611 * IPIs to the other CPUs to also write into the ring buffer.
5612 * this is to test the nesting ability of the buffer.
5613 *
5614 * Basic stats are recorded and reported. If something in the
5615 * ring buffer should happen that's not expected, a big warning
5616 * is displayed and all ring buffers are disabled.
5617 */
5618static struct task_struct *rb_threads[NR_CPUS] __initdata;
5619
5620struct rb_test_data {
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05005621 struct trace_buffer *buffer;
Steven Rostedt (Red Hat)6c43e552013-03-15 11:32:53 -04005622 unsigned long events;
5623 unsigned long bytes_written;
5624 unsigned long bytes_alloc;
5625 unsigned long bytes_dropped;
5626 unsigned long events_nested;
5627 unsigned long bytes_written_nested;
5628 unsigned long bytes_alloc_nested;
5629 unsigned long bytes_dropped_nested;
5630 int min_size_nested;
5631 int max_size_nested;
5632 int max_size;
5633 int min_size;
5634 int cpu;
5635 int cnt;
5636};
5637
5638static struct rb_test_data rb_data[NR_CPUS] __initdata;
5639
5640/* 1 meg per cpu */
5641#define RB_TEST_BUFFER_SIZE 1048576
5642
5643static char rb_string[] __initdata =
5644 "abcdefghijklmnopqrstuvwxyz1234567890!@#$%^&*()?+\\"
5645 "?+|:';\",.<>/?abcdefghijklmnopqrstuvwxyz1234567890"
5646 "!@#$%^&*()?+\\?+|:';\",.<>/?abcdefghijklmnopqrstuv";
5647
5648static bool rb_test_started __initdata;
5649
5650struct rb_item {
5651 int size;
5652 char str[];
5653};
5654
5655static __init int rb_write_something(struct rb_test_data *data, bool nested)
5656{
5657 struct ring_buffer_event *event;
5658 struct rb_item *item;
5659 bool started;
5660 int event_len;
5661 int size;
5662 int len;
5663 int cnt;
5664
5665 /* Have nested writes different that what is written */
5666 cnt = data->cnt + (nested ? 27 : 0);
5667
5668 /* Multiply cnt by ~e, to make some unique increment */
YueHaibing40ed29b2018-09-23 12:11:33 +00005669 size = (cnt * 68 / 25) % (sizeof(rb_string) - 1);
Steven Rostedt (Red Hat)6c43e552013-03-15 11:32:53 -04005670
5671 len = size + sizeof(struct rb_item);
5672
5673 started = rb_test_started;
5674 /* read rb_test_started before checking buffer enabled */
5675 smp_rmb();
5676
5677 event = ring_buffer_lock_reserve(data->buffer, len);
5678 if (!event) {
5679 /* Ignore dropped events before test starts. */
5680 if (started) {
5681 if (nested)
5682 data->bytes_dropped += len;
5683 else
5684 data->bytes_dropped_nested += len;
5685 }
5686 return len;
5687 }
5688
5689 event_len = ring_buffer_event_length(event);
5690
5691 if (RB_WARN_ON(data->buffer, event_len < len))
5692 goto out;
5693
5694 item = ring_buffer_event_data(event);
5695 item->size = size;
5696 memcpy(item->str, rb_string, size);
5697
5698 if (nested) {
5699 data->bytes_alloc_nested += event_len;
5700 data->bytes_written_nested += len;
5701 data->events_nested++;
5702 if (!data->min_size_nested || len < data->min_size_nested)
5703 data->min_size_nested = len;
5704 if (len > data->max_size_nested)
5705 data->max_size_nested = len;
5706 } else {
5707 data->bytes_alloc += event_len;
5708 data->bytes_written += len;
5709 data->events++;
5710 if (!data->min_size || len < data->min_size)
5711 data->max_size = len;
5712 if (len > data->max_size)
5713 data->max_size = len;
5714 }
5715
5716 out:
5717 ring_buffer_unlock_commit(data->buffer, event);
5718
5719 return 0;
5720}
5721
5722static __init int rb_test(void *arg)
5723{
5724 struct rb_test_data *data = arg;
5725
5726 while (!kthread_should_stop()) {
5727 rb_write_something(data, false);
5728 data->cnt++;
5729
5730 set_current_state(TASK_INTERRUPTIBLE);
5731 /* Now sleep between a min of 100-300us and a max of 1ms */
5732 usleep_range(((data->cnt % 3) + 1) * 100, 1000);
5733 }
5734
5735 return 0;
5736}
5737
5738static __init void rb_ipi(void *ignore)
5739{
5740 struct rb_test_data *data;
5741 int cpu = smp_processor_id();
5742
5743 data = &rb_data[cpu];
5744 rb_write_something(data, true);
5745}
5746
5747static __init int rb_hammer_test(void *arg)
5748{
5749 while (!kthread_should_stop()) {
5750
5751 /* Send an IPI to all cpus to write data! */
5752 smp_call_function(rb_ipi, NULL, 1);
5753 /* No sleep, but for non preempt, let others run */
5754 schedule();
5755 }
5756
5757 return 0;
5758}
5759
5760static __init int test_ringbuffer(void)
5761{
5762 struct task_struct *rb_hammer;
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05005763 struct trace_buffer *buffer;
Steven Rostedt (Red Hat)6c43e552013-03-15 11:32:53 -04005764 int cpu;
5765 int ret = 0;
5766
Steven Rostedt (VMware)a3566462019-12-02 16:25:27 -05005767 if (security_locked_down(LOCKDOWN_TRACEFS)) {
Stephen Rothwellee195452019-12-06 09:25:03 +11005768 pr_warn("Lockdown is enabled, skipping ring buffer tests\n");
Steven Rostedt (VMware)a3566462019-12-02 16:25:27 -05005769 return 0;
5770 }
5771
Steven Rostedt (Red Hat)6c43e552013-03-15 11:32:53 -04005772 pr_info("Running ring buffer tests...\n");
5773
5774 buffer = ring_buffer_alloc(RB_TEST_BUFFER_SIZE, RB_FL_OVERWRITE);
5775 if (WARN_ON(!buffer))
5776 return 0;
5777
5778 /* Disable buffer so that threads can't write to it yet */
5779 ring_buffer_record_off(buffer);
5780
5781 for_each_online_cpu(cpu) {
5782 rb_data[cpu].buffer = buffer;
5783 rb_data[cpu].cpu = cpu;
5784 rb_data[cpu].cnt = cpu;
5785 rb_threads[cpu] = kthread_create(rb_test, &rb_data[cpu],
5786 "rbtester/%d", cpu);
Wei Yongjun62277de2016-06-17 17:33:59 +00005787 if (WARN_ON(IS_ERR(rb_threads[cpu]))) {
Steven Rostedt (Red Hat)6c43e552013-03-15 11:32:53 -04005788 pr_cont("FAILED\n");
Wei Yongjun62277de2016-06-17 17:33:59 +00005789 ret = PTR_ERR(rb_threads[cpu]);
Steven Rostedt (Red Hat)6c43e552013-03-15 11:32:53 -04005790 goto out_free;
5791 }
5792
5793 kthread_bind(rb_threads[cpu], cpu);
5794 wake_up_process(rb_threads[cpu]);
5795 }
5796
5797 /* Now create the rb hammer! */
5798 rb_hammer = kthread_run(rb_hammer_test, NULL, "rbhammer");
Wei Yongjun62277de2016-06-17 17:33:59 +00005799 if (WARN_ON(IS_ERR(rb_hammer))) {
Steven Rostedt (Red Hat)6c43e552013-03-15 11:32:53 -04005800 pr_cont("FAILED\n");
Wei Yongjun62277de2016-06-17 17:33:59 +00005801 ret = PTR_ERR(rb_hammer);
Steven Rostedt (Red Hat)6c43e552013-03-15 11:32:53 -04005802 goto out_free;
5803 }
5804
5805 ring_buffer_record_on(buffer);
5806 /*
5807 * Show buffer is enabled before setting rb_test_started.
5808 * Yes there's a small race window where events could be
5809 * dropped and the thread wont catch it. But when a ring
5810 * buffer gets enabled, there will always be some kind of
5811 * delay before other CPUs see it. Thus, we don't care about
5812 * those dropped events. We care about events dropped after
5813 * the threads see that the buffer is active.
5814 */
5815 smp_wmb();
5816 rb_test_started = true;
5817
5818 set_current_state(TASK_INTERRUPTIBLE);
5819 /* Just run for 10 seconds */;
5820 schedule_timeout(10 * HZ);
5821
5822 kthread_stop(rb_hammer);
5823
5824 out_free:
5825 for_each_online_cpu(cpu) {
5826 if (!rb_threads[cpu])
5827 break;
5828 kthread_stop(rb_threads[cpu]);
5829 }
5830 if (ret) {
5831 ring_buffer_free(buffer);
5832 return ret;
5833 }
5834
5835 /* Report! */
5836 pr_info("finished\n");
5837 for_each_online_cpu(cpu) {
5838 struct ring_buffer_event *event;
5839 struct rb_test_data *data = &rb_data[cpu];
5840 struct rb_item *item;
5841 unsigned long total_events;
5842 unsigned long total_dropped;
5843 unsigned long total_written;
5844 unsigned long total_alloc;
5845 unsigned long total_read = 0;
5846 unsigned long total_size = 0;
5847 unsigned long total_len = 0;
5848 unsigned long total_lost = 0;
5849 unsigned long lost;
5850 int big_event_size;
5851 int small_event_size;
5852
5853 ret = -1;
5854
5855 total_events = data->events + data->events_nested;
5856 total_written = data->bytes_written + data->bytes_written_nested;
5857 total_alloc = data->bytes_alloc + data->bytes_alloc_nested;
5858 total_dropped = data->bytes_dropped + data->bytes_dropped_nested;
5859
5860 big_event_size = data->max_size + data->max_size_nested;
5861 small_event_size = data->min_size + data->min_size_nested;
5862
5863 pr_info("CPU %d:\n", cpu);
5864 pr_info(" events: %ld\n", total_events);
5865 pr_info(" dropped bytes: %ld\n", total_dropped);
5866 pr_info(" alloced bytes: %ld\n", total_alloc);
5867 pr_info(" written bytes: %ld\n", total_written);
5868 pr_info(" biggest event: %d\n", big_event_size);
5869 pr_info(" smallest event: %d\n", small_event_size);
5870
5871 if (RB_WARN_ON(buffer, total_dropped))
5872 break;
5873
5874 ret = 0;
5875
5876 while ((event = ring_buffer_consume(buffer, cpu, NULL, &lost))) {
5877 total_lost += lost;
5878 item = ring_buffer_event_data(event);
5879 total_len += ring_buffer_event_length(event);
5880 total_size += item->size + sizeof(struct rb_item);
5881 if (memcmp(&item->str[0], rb_string, item->size) != 0) {
5882 pr_info("FAILED!\n");
5883 pr_info("buffer had: %.*s\n", item->size, item->str);
5884 pr_info("expected: %.*s\n", item->size, rb_string);
5885 RB_WARN_ON(buffer, 1);
5886 ret = -1;
5887 break;
5888 }
5889 total_read++;
5890 }
5891 if (ret)
5892 break;
5893
5894 ret = -1;
5895
5896 pr_info(" read events: %ld\n", total_read);
5897 pr_info(" lost events: %ld\n", total_lost);
5898 pr_info(" total events: %ld\n", total_lost + total_read);
5899 pr_info(" recorded len bytes: %ld\n", total_len);
5900 pr_info(" recorded size bytes: %ld\n", total_size);
5901 if (total_lost)
5902 pr_info(" With dropped events, record len and size may not match\n"
5903 " alloced and written from above\n");
5904 if (!total_lost) {
5905 if (RB_WARN_ON(buffer, total_len != total_alloc ||
5906 total_size != total_written))
5907 break;
5908 }
5909 if (RB_WARN_ON(buffer, total_lost + total_read != total_events))
5910 break;
5911
5912 ret = 0;
5913 }
5914 if (!ret)
5915 pr_info("Ring buffer PASSED!\n");
5916
5917 ring_buffer_free(buffer);
5918 return 0;
5919}
5920
5921late_initcall(test_ringbuffer);
5922#endif /* CONFIG_RING_BUFFER_STARTUP_TEST */