blob: 8fa2a84f714fe78903f4a43c2ea5cc36ca79b90c [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
Steven Rostedt (VMware)e20044f2021-03-16 12:41:01 -0400290static u64 rb_event_time_stamp(struct ring_buffer_event *event)
291{
292 u64 ts;
293
294 ts = event->array[0];
295 ts <<= TS_SHIFT;
296 ts += event->time_delta;
297
298 return ts;
299}
300
Steven Rostedt66a8cb92010-03-31 13:21:56 -0400301/* Flag when events were overwritten */
302#define RB_MISSED_EVENTS (1 << 31)
Steven Rostedtff0ff842010-03-31 22:11:42 -0400303/* Missed count stored at end */
304#define RB_MISSED_STORED (1 << 30)
Steven Rostedt66a8cb92010-03-31 13:21:56 -0400305
Steven Rostedtabc9b562008-12-02 15:34:06 -0500306struct buffer_data_page {
Steven Rostedte4c2ce82008-10-01 11:14:54 -0400307 u64 time_stamp; /* page time stamp */
Wenji Huangc3706f02009-02-10 01:03:18 -0500308 local_t commit; /* write committed index */
James Hogan649508f2012-05-30 12:11:19 +0100309 unsigned char data[] RB_ALIGN_DATA; /* data of buffer page */
Steven Rostedtabc9b562008-12-02 15:34:06 -0500310};
311
Steven Rostedt77ae3652009-03-27 11:00:29 -0400312/*
313 * Note, the buffer_page list must be first. The buffer pages
314 * are allocated in cache lines, which means that each buffer
315 * page will be at the beginning of a cache line, and thus
316 * the least significant bits will be zero. We use this to
317 * add flags in the list struct pointers, to make the ring buffer
318 * lockless.
319 */
Steven Rostedtabc9b562008-12-02 15:34:06 -0500320struct buffer_page {
Steven Rostedt778c55d2009-05-01 18:44:45 -0400321 struct list_head list; /* list of buffer pages */
Steven Rostedtabc9b562008-12-02 15:34:06 -0500322 local_t write; /* index for next write */
Steven Rostedt6f807ac2008-10-04 02:00:58 -0400323 unsigned read; /* index for next read */
Steven Rostedt778c55d2009-05-01 18:44:45 -0400324 local_t entries; /* entries on this page */
Steven Rostedtff0ff842010-03-31 22:11:42 -0400325 unsigned long real_end; /* real end of data */
Steven Rostedtabc9b562008-12-02 15:34:06 -0500326 struct buffer_data_page *page; /* Actual data page */
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400327};
328
Steven Rostedt77ae3652009-03-27 11:00:29 -0400329/*
330 * The buffer page counters, write and entries, must be reset
331 * atomically when crossing page boundaries. To synchronize this
332 * update, two counters are inserted into the number. One is
333 * the actual counter for the write position or count on the page.
334 *
335 * The other is a counter of updaters. Before an update happens
336 * the update partition of the counter is incremented. This will
337 * allow the updater to update the counter atomically.
338 *
339 * The counter is 20 bits, and the state data is 12.
340 */
341#define RB_WRITE_MASK 0xfffff
342#define RB_WRITE_INTCNT (1 << 20)
343
Steven Rostedt044fa782008-12-02 23:50:03 -0500344static void rb_init_page(struct buffer_data_page *bpage)
Steven Rostedtabc9b562008-12-02 15:34:06 -0500345{
Steven Rostedt044fa782008-12-02 23:50:03 -0500346 local_set(&bpage->commit, 0);
Steven Rostedtabc9b562008-12-02 15:34:06 -0500347}
348
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400349/*
Steven Rostedted568292008-09-29 23:02:40 -0400350 * Also stolen from mm/slob.c. Thanks to Mathieu Desnoyers for pointing
351 * this issue out.
352 */
Andrew Morton34a148b2009-01-09 12:27:09 -0800353static void free_buffer_page(struct buffer_page *bpage)
Steven Rostedted568292008-09-29 23:02:40 -0400354{
Andrew Morton34a148b2009-01-09 12:27:09 -0800355 free_page((unsigned long)bpage->page);
Steven Rostedte4c2ce82008-10-01 11:14:54 -0400356 kfree(bpage);
Steven Rostedted568292008-09-29 23:02:40 -0400357}
358
359/*
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400360 * We need to fit the time_stamp delta into 27 bits.
361 */
362static inline int test_time_stamp(u64 delta)
363{
364 if (delta & TS_DELTA_TEST)
365 return 1;
366 return 0;
367}
368
Steven Rostedt474d32b2009-03-03 19:51:40 -0500369#define BUF_PAGE_SIZE (PAGE_SIZE - BUF_PAGE_HDR_SIZE)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400370
Steven Rostedtbe957c42009-05-11 14:42:53 -0400371/* Max payload is BUF_PAGE_SIZE - header (8bytes) */
372#define BUF_MAX_DATA_SIZE (BUF_PAGE_SIZE - (sizeof(u32) * 2))
373
Steven Rostedtd1b182a2009-04-15 16:53:47 -0400374int ring_buffer_print_page_header(struct trace_seq *s)
375{
376 struct buffer_data_page field;
Steven Rostedtd1b182a2009-04-15 16:53:47 -0400377
Steven Rostedt (Red Hat)c0cd93a2014-11-12 11:49:00 -0500378 trace_seq_printf(s, "\tfield: u64 timestamp;\t"
379 "offset:0;\tsize:%u;\tsigned:%u;\n",
380 (unsigned int)sizeof(field.time_stamp),
381 (unsigned int)is_signed_type(u64));
Steven Rostedtd1b182a2009-04-15 16:53:47 -0400382
Steven Rostedt (Red Hat)c0cd93a2014-11-12 11:49:00 -0500383 trace_seq_printf(s, "\tfield: local_t commit;\t"
384 "offset:%u;\tsize:%u;\tsigned:%u;\n",
385 (unsigned int)offsetof(typeof(field), commit),
386 (unsigned int)sizeof(field.commit),
387 (unsigned int)is_signed_type(long));
Steven Rostedtd1b182a2009-04-15 16:53:47 -0400388
Steven Rostedt (Red Hat)c0cd93a2014-11-12 11:49:00 -0500389 trace_seq_printf(s, "\tfield: int overwrite;\t"
390 "offset:%u;\tsize:%u;\tsigned:%u;\n",
391 (unsigned int)offsetof(typeof(field), commit),
392 1,
393 (unsigned int)is_signed_type(long));
Steven Rostedt66a8cb92010-03-31 13:21:56 -0400394
Steven Rostedt (Red Hat)c0cd93a2014-11-12 11:49:00 -0500395 trace_seq_printf(s, "\tfield: char data;\t"
396 "offset:%u;\tsize:%u;\tsigned:%u;\n",
397 (unsigned int)offsetof(typeof(field), data),
398 (unsigned int)BUF_PAGE_SIZE,
399 (unsigned int)is_signed_type(char));
Steven Rostedtd1b182a2009-04-15 16:53:47 -0400400
Steven Rostedt (Red Hat)c0cd93a2014-11-12 11:49:00 -0500401 return !trace_seq_has_overflowed(s);
Steven Rostedtd1b182a2009-04-15 16:53:47 -0400402}
403
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -0500404struct rb_irq_work {
405 struct irq_work work;
406 wait_queue_head_t waiters;
Steven Rostedt (Red Hat)1e0d6712015-02-10 22:14:53 -0500407 wait_queue_head_t full_waiters;
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -0500408 bool waiters_pending;
Steven Rostedt (Red Hat)1e0d6712015-02-10 22:14:53 -0500409 bool full_waiters_pending;
410 bool wakeup_full;
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -0500411};
412
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400413/*
Steven Rostedt (Red Hat)fcc742e2015-05-28 17:13:14 -0400414 * Structure to hold event state and handle nested events.
415 */
416struct rb_event_info {
417 u64 ts;
418 u64 delta;
Steven Rostedt (VMware)58fbc3c2020-06-30 08:59:26 -0400419 u64 before;
420 u64 after;
Steven Rostedt (Red Hat)fcc742e2015-05-28 17:13:14 -0400421 unsigned long length;
422 struct buffer_page *tail_page;
423 int add_timestamp;
424};
425
426/*
Steven Rostedt (VMware)a389d862020-06-28 22:52:25 -0400427 * Used for the add_timestamp
428 * NONE
Steven Rostedt (VMware)7c4b4a52020-06-28 22:52:26 -0400429 * EXTEND - wants a time extend
430 * ABSOLUTE - the buffer requests all events to have absolute time stamps
Steven Rostedt (VMware)a389d862020-06-28 22:52:25 -0400431 * FORCE - force a full time stamp.
432 */
433enum {
Steven Rostedt (VMware)7c4b4a52020-06-28 22:52:26 -0400434 RB_ADD_STAMP_NONE = 0,
435 RB_ADD_STAMP_EXTEND = BIT(1),
436 RB_ADD_STAMP_ABSOLUTE = BIT(2),
437 RB_ADD_STAMP_FORCE = BIT(3)
Steven Rostedt (VMware)a389d862020-06-28 22:52:25 -0400438};
439/*
Steven Rostedt (Red Hat)a497adb2015-05-29 10:32:28 -0400440 * Used for which event context the event is in.
Steven Rostedt (VMware)b02414c2020-11-02 15:31:27 -0500441 * TRANSITION = 0
442 * NMI = 1
443 * IRQ = 2
444 * SOFTIRQ = 3
445 * NORMAL = 4
Steven Rostedt (Red Hat)a497adb2015-05-29 10:32:28 -0400446 *
447 * See trace_recursive_lock() comment below for more details.
448 */
449enum {
Steven Rostedt (VMware)b02414c2020-11-02 15:31:27 -0500450 RB_CTX_TRANSITION,
Steven Rostedt (Red Hat)a497adb2015-05-29 10:32:28 -0400451 RB_CTX_NMI,
452 RB_CTX_IRQ,
453 RB_CTX_SOFTIRQ,
454 RB_CTX_NORMAL,
455 RB_CTX_MAX
456};
457
Steven Rostedt (VMware)10464b42020-06-28 22:52:27 -0400458#if BITS_PER_LONG == 32
459#define RB_TIME_32
460#endif
461
462/* To test on 64 bit machines */
463//#define RB_TIME_32
464
465#ifdef RB_TIME_32
466
467struct rb_time_struct {
468 local_t cnt;
469 local_t top;
470 local_t bottom;
471};
472#else
473#include <asm/local64.h>
474struct rb_time_struct {
475 local64_t time;
476};
477#endif
478typedef struct rb_time_struct rb_time_t;
479
Steven Rostedt (VMware)8672e4942021-03-16 12:41:02 -0400480#define MAX_NEST 5
481
Steven Rostedt (Red Hat)a497adb2015-05-29 10:32:28 -0400482/*
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400483 * head_page == tail_page && head == tail then buffer is empty.
484 */
485struct ring_buffer_per_cpu {
486 int cpu;
Richard Kennedy985023d2010-03-25 11:27:36 +0000487 atomic_t record_disabled;
Steven Rostedt (VMware)07b8b102020-03-27 16:21:22 -0400488 atomic_t resize_disabled;
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -0500489 struct trace_buffer *buffer;
Thomas Gleixner5389f6f2009-07-25 17:13:33 +0200490 raw_spinlock_t reader_lock; /* serialize readers */
Thomas Gleixner445c8952009-12-02 19:49:50 +0100491 arch_spinlock_t lock;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400492 struct lock_class_key lock_key;
Steven Rostedt (VMware)73a757e2017-05-01 09:35:09 -0400493 struct buffer_data_page *free_page;
Steven Rostedt (Red Hat)9b94a8f2016-05-12 11:01:24 -0400494 unsigned long nr_pages;
Steven Rostedt (Red Hat)58a09ec2015-05-27 10:27:47 -0400495 unsigned int current_context;
Steven Rostedt3adc54f2009-03-30 15:32:01 -0400496 struct list_head *pages;
Steven Rostedt6f807ac2008-10-04 02:00:58 -0400497 struct buffer_page *head_page; /* read from head */
498 struct buffer_page *tail_page; /* write to tail */
Wenji Huangc3706f02009-02-10 01:03:18 -0500499 struct buffer_page *commit_page; /* committed pages */
Steven Rostedtd7690412008-10-01 00:29:53 -0400500 struct buffer_page *reader_page;
Steven Rostedt66a8cb92010-03-31 13:21:56 -0400501 unsigned long lost_events;
502 unsigned long last_overrun;
Steven Rostedt (VMware)8e012062018-02-07 17:26:32 -0500503 unsigned long nest;
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -0700504 local_t entries_bytes;
Steven Rostedte4906ef2009-04-30 20:49:44 -0400505 local_t entries;
Slava Pestov884bfe82011-07-15 14:23:58 -0700506 local_t overrun;
507 local_t commit_overrun;
508 local_t dropped_events;
Steven Rostedtfa743952009-06-16 12:37:57 -0400509 local_t committing;
510 local_t commits;
Steven Rostedt (VMware)2c2b0a72018-11-29 20:32:26 -0500511 local_t pages_touched;
512 local_t pages_read;
Steven Rostedt (VMware)03329f92018-11-29 21:38:42 -0500513 long last_pages_touch;
Steven Rostedt (VMware)2c2b0a72018-11-29 20:32:26 -0500514 size_t shortest_full;
Steven Rostedt77ae3652009-03-27 11:00:29 -0400515 unsigned long read;
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -0700516 unsigned long read_bytes;
Steven Rostedt (VMware)10464b42020-06-28 22:52:27 -0400517 rb_time_t write_stamp;
518 rb_time_t before_stamp;
Steven Rostedt (VMware)8672e4942021-03-16 12:41:02 -0400519 u64 event_stamp[MAX_NEST];
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400520 u64 read_stamp;
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -0800521 /* ring buffer pages to update, > 0 to add, < 0 to remove */
Steven Rostedt (Red Hat)9b94a8f2016-05-12 11:01:24 -0400522 long nr_pages_to_update;
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -0800523 struct list_head new_pages; /* new pages to add */
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -0700524 struct work_struct update_pages_work;
Vaibhav Nagarnaik05fdd702012-05-18 13:29:51 -0700525 struct completion update_done;
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -0500526
527 struct rb_irq_work irq_work;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400528};
529
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -0500530struct trace_buffer {
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400531 unsigned flags;
532 int cpus;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400533 atomic_t record_disabled;
Arnaldo Carvalho de Melo00f62f62009-02-09 17:04:06 -0200534 cpumask_var_t cpumask;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400535
Peter Zijlstra1f8a6a12009-06-08 18:18:39 +0200536 struct lock_class_key *reader_lock_key;
537
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400538 struct mutex mutex;
539
540 struct ring_buffer_per_cpu **buffers;
Steven Rostedt554f7862009-03-11 22:00:13 -0400541
Sebastian Andrzej Siewiorb32614c2016-11-27 00:13:34 +0100542 struct hlist_node node;
Steven Rostedt37886f62009-03-17 17:22:06 -0400543 u64 (*clock)(void);
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -0500544
545 struct rb_irq_work irq_work;
Tom Zanussi00b41452018-01-15 20:51:39 -0600546 bool time_stamp_abs;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400547};
548
549struct ring_buffer_iter {
550 struct ring_buffer_per_cpu *cpu_buffer;
551 unsigned long head;
Steven Rostedt (VMware)785888c2020-03-17 17:32:27 -0400552 unsigned long next_event;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400553 struct buffer_page *head_page;
Steven Rostedt492a74f2010-01-25 15:17:47 -0500554 struct buffer_page *cache_reader_page;
555 unsigned long cache_read;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400556 u64 read_stamp;
Steven Rostedt (VMware)28e3fc52020-03-17 17:32:26 -0400557 u64 page_stamp;
Steven Rostedt (VMware)785888c2020-03-17 17:32:27 -0400558 struct ring_buffer_event *event;
Steven Rostedt (VMware)c9b7a4a2020-03-17 17:32:32 -0400559 int missed_events;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400560};
561
Steven Rostedt (VMware)10464b42020-06-28 22:52:27 -0400562#ifdef RB_TIME_32
563
564/*
565 * On 32 bit machines, local64_t is very expensive. As the ring
566 * buffer doesn't need all the features of a true 64 bit atomic,
567 * on 32 bit, it uses these functions (64 still uses local64_t).
568 *
569 * For the ring buffer, 64 bit required operations for the time is
570 * the following:
571 *
572 * - Only need 59 bits (uses 60 to make it even).
573 * - Reads may fail if it interrupted a modification of the time stamp.
574 * It will succeed if it did not interrupt another write even if
575 * the read itself is interrupted by a write.
576 * It returns whether it was successful or not.
577 *
578 * - Writes always succeed and will overwrite other writes and writes
579 * that were done by events interrupting the current write.
580 *
581 * - A write followed by a read of the same time stamp will always succeed,
582 * but may not contain the same value.
583 *
584 * - A cmpxchg will fail if it interrupted another write or cmpxchg.
585 * Other than that, it acts like a normal cmpxchg.
586 *
587 * The 60 bit time stamp is broken up by 30 bits in a top and bottom half
588 * (bottom being the least significant 30 bits of the 60 bit time stamp).
589 *
590 * The two most significant bits of each half holds a 2 bit counter (0-3).
591 * Each update will increment this counter by one.
592 * When reading the top and bottom, if the two counter bits match then the
593 * top and bottom together make a valid 60 bit number.
594 */
595#define RB_TIME_SHIFT 30
596#define RB_TIME_VAL_MASK ((1 << RB_TIME_SHIFT) - 1)
597
598static inline int rb_time_cnt(unsigned long val)
599{
600 return (val >> RB_TIME_SHIFT) & 3;
601}
602
603static inline u64 rb_time_val(unsigned long top, unsigned long bottom)
604{
605 u64 val;
606
607 val = top & RB_TIME_VAL_MASK;
608 val <<= RB_TIME_SHIFT;
609 val |= bottom & RB_TIME_VAL_MASK;
610
611 return val;
612}
613
614static inline bool __rb_time_read(rb_time_t *t, u64 *ret, unsigned long *cnt)
615{
616 unsigned long top, bottom;
617 unsigned long c;
618
619 /*
620 * If the read is interrupted by a write, then the cnt will
621 * be different. Loop until both top and bottom have been read
622 * without interruption.
623 */
624 do {
625 c = local_read(&t->cnt);
626 top = local_read(&t->top);
627 bottom = local_read(&t->bottom);
628 } while (c != local_read(&t->cnt));
629
630 *cnt = rb_time_cnt(top);
631
632 /* If top and bottom counts don't match, this interrupted a write */
633 if (*cnt != rb_time_cnt(bottom))
634 return false;
635
636 *ret = rb_time_val(top, bottom);
637 return true;
638}
639
640static bool rb_time_read(rb_time_t *t, u64 *ret)
641{
642 unsigned long cnt;
643
644 return __rb_time_read(t, ret, &cnt);
645}
646
647static inline unsigned long rb_time_val_cnt(unsigned long val, unsigned long cnt)
648{
649 return (val & RB_TIME_VAL_MASK) | ((cnt & 3) << RB_TIME_SHIFT);
650}
651
652static inline void rb_time_split(u64 val, unsigned long *top, unsigned long *bottom)
653{
654 *top = (unsigned long)((val >> RB_TIME_SHIFT) & RB_TIME_VAL_MASK);
655 *bottom = (unsigned long)(val & RB_TIME_VAL_MASK);
656}
657
658static inline void rb_time_val_set(local_t *t, unsigned long val, unsigned long cnt)
659{
660 val = rb_time_val_cnt(val, cnt);
661 local_set(t, val);
662}
663
664static void rb_time_set(rb_time_t *t, u64 val)
665{
666 unsigned long cnt, top, bottom;
667
668 rb_time_split(val, &top, &bottom);
669
670 /* Writes always succeed with a valid number even if it gets interrupted. */
671 do {
672 cnt = local_inc_return(&t->cnt);
673 rb_time_val_set(&t->top, top, cnt);
674 rb_time_val_set(&t->bottom, bottom, cnt);
675 } while (cnt != local_read(&t->cnt));
676}
677
678static inline bool
679rb_time_read_cmpxchg(local_t *l, unsigned long expect, unsigned long set)
680{
681 unsigned long ret;
682
683 ret = local_cmpxchg(l, expect, set);
684 return ret == expect;
685}
686
687static int rb_time_cmpxchg(rb_time_t *t, u64 expect, u64 set)
688{
689 unsigned long cnt, top, bottom;
690 unsigned long cnt2, top2, bottom2;
691 u64 val;
692
693 /* The cmpxchg always fails if it interrupted an update */
694 if (!__rb_time_read(t, &val, &cnt2))
695 return false;
696
697 if (val != expect)
698 return false;
699
700 cnt = local_read(&t->cnt);
701 if ((cnt & 3) != cnt2)
702 return false;
703
704 cnt2 = cnt + 1;
705
706 rb_time_split(val, &top, &bottom);
707 top = rb_time_val_cnt(top, cnt);
708 bottom = rb_time_val_cnt(bottom, cnt);
709
710 rb_time_split(set, &top2, &bottom2);
711 top2 = rb_time_val_cnt(top2, cnt2);
712 bottom2 = rb_time_val_cnt(bottom2, cnt2);
713
714 if (!rb_time_read_cmpxchg(&t->cnt, cnt, cnt2))
715 return false;
716 if (!rb_time_read_cmpxchg(&t->top, top, top2))
717 return false;
718 if (!rb_time_read_cmpxchg(&t->bottom, bottom, bottom2))
719 return false;
720 return true;
721}
722
723#else /* 64 bits */
724
725/* local64_t always succeeds */
726
727static inline bool rb_time_read(rb_time_t *t, u64 *ret)
728{
729 *ret = local64_read(&t->time);
730 return true;
731}
732static void rb_time_set(rb_time_t *t, u64 val)
733{
734 local64_set(&t->time, val);
735}
736
737static bool rb_time_cmpxchg(rb_time_t *t, u64 expect, u64 set)
738{
739 u64 val;
740 val = local64_cmpxchg(&t->time, expect, set);
741 return val == expect;
742}
743#endif
744
Steven Rostedt (VMware)efe61962021-03-16 12:41:04 -0400745static inline u64 rb_time_stamp(struct trace_buffer *buffer);
746
747/**
748 * ring_buffer_event_time_stamp - return the event's current time stamp
749 * @buffer: The buffer that the event is on
750 * @event: the event to get the time stamp of
751 *
752 * Note, this must be called after @event is reserved, and before it is
753 * committed to the ring buffer. And must be called from the same
754 * context where the event was reserved (normal, softirq, irq, etc).
755 *
756 * Returns the time stamp associated with the current event.
757 * If the event has an extended time stamp, then that is used as
758 * the time stamp to return.
759 * In the highly unlikely case that the event was nested more than
760 * the max nesting, then the write_stamp of the buffer is returned,
761 * otherwise current time is returned, but that really neither of
762 * the last two cases should ever happen.
763 */
764u64 ring_buffer_event_time_stamp(struct trace_buffer *buffer,
765 struct ring_buffer_event *event)
766{
767 struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[smp_processor_id()];
768 unsigned int nest;
769 u64 ts;
770
771 /* If the event includes an absolute time, then just use that */
772 if (event->type_len == RINGBUF_TYPE_TIME_STAMP)
773 return rb_event_time_stamp(event);
774
775 /* Read the current saved nesting level time stamp */
776 nest = local_read(&cpu_buffer->committing) - 1;
777 if (likely(nest < MAX_NEST))
778 return cpu_buffer->event_stamp[nest];
779
780 WARN_ON_ONCE(1);
781
782 /* Can only fail on 32 bit */
783 if (!rb_time_read(&cpu_buffer->write_stamp, &ts))
784 /* Screw it, just read the current time */
785 ts = rb_time_stamp(cpu_buffer->buffer);
786
787 return ts;
788}
789
Steven Rostedt (VMware)2c2b0a72018-11-29 20:32:26 -0500790/**
791 * ring_buffer_nr_pages - get the number of buffer pages in the ring buffer
792 * @buffer: The ring_buffer to get the number of pages from
793 * @cpu: The cpu of the ring_buffer to get the number of pages from
794 *
795 * Returns the number of pages used by a per_cpu buffer of the ring buffer.
796 */
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -0500797size_t ring_buffer_nr_pages(struct trace_buffer *buffer, int cpu)
Steven Rostedt (VMware)2c2b0a72018-11-29 20:32:26 -0500798{
799 return buffer->buffers[cpu]->nr_pages;
800}
801
802/**
803 * ring_buffer_nr_pages_dirty - get the number of used pages in the ring buffer
804 * @buffer: The ring_buffer to get the number of pages from
805 * @cpu: The cpu of the ring_buffer to get the number of pages from
806 *
807 * Returns the number of pages that have content in the ring buffer.
808 */
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -0500809size_t ring_buffer_nr_dirty_pages(struct trace_buffer *buffer, int cpu)
Steven Rostedt (VMware)2c2b0a72018-11-29 20:32:26 -0500810{
811 size_t read;
812 size_t cnt;
813
814 read = local_read(&buffer->buffers[cpu]->pages_read);
815 cnt = local_read(&buffer->buffers[cpu]->pages_touched);
816 /* The reader can read an empty page, but not more than that */
817 if (cnt < read) {
818 WARN_ON_ONCE(read > cnt + 1);
819 return 0;
820 }
821
822 return cnt - read;
823}
824
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -0500825/*
826 * rb_wake_up_waiters - wake up tasks waiting for ring buffer input
827 *
828 * Schedules a delayed work to wake up any task that is blocked on the
829 * ring buffer waiters queue.
830 */
831static void rb_wake_up_waiters(struct irq_work *work)
832{
833 struct rb_irq_work *rbwork = container_of(work, struct rb_irq_work, work);
834
835 wake_up_all(&rbwork->waiters);
Steven Rostedt (Red Hat)1e0d6712015-02-10 22:14:53 -0500836 if (rbwork->wakeup_full) {
837 rbwork->wakeup_full = false;
838 wake_up_all(&rbwork->full_waiters);
839 }
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -0500840}
841
842/**
843 * ring_buffer_wait - wait for input to the ring buffer
844 * @buffer: buffer to wait on
845 * @cpu: the cpu buffer to wait on
Qiujun Huange1981f72020-10-17 17:52:46 +0800846 * @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 -0500847 *
848 * If @cpu == RING_BUFFER_ALL_CPUS then the task will wake up as soon
849 * as data is added to any of the @buffer's cpu buffers. Otherwise
850 * it will wait for data to be added to a specific cpu buffer.
851 */
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -0500852int ring_buffer_wait(struct trace_buffer *buffer, int cpu, int full)
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -0500853{
Kees Cook3f649ab2020-06-03 13:09:38 -0700854 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -0500855 DEFINE_WAIT(wait);
856 struct rb_irq_work *work;
Rabin Vincente30f53a2014-11-10 19:46:34 +0100857 int ret = 0;
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -0500858
859 /*
860 * Depending on what the caller is waiting for, either any
861 * data in any cpu buffer, or a specific buffer, put the
862 * caller on the appropriate wait queue.
863 */
Steven Rostedt (Red Hat)1e0d6712015-02-10 22:14:53 -0500864 if (cpu == RING_BUFFER_ALL_CPUS) {
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -0500865 work = &buffer->irq_work;
Steven Rostedt (Red Hat)1e0d6712015-02-10 22:14:53 -0500866 /* Full only makes sense on per cpu reads */
Steven Rostedt (VMware)2c2b0a72018-11-29 20:32:26 -0500867 full = 0;
Steven Rostedt (Red Hat)1e0d6712015-02-10 22:14:53 -0500868 } else {
Steven Rostedt (Red Hat)8b8b3682014-06-10 09:46:00 -0400869 if (!cpumask_test_cpu(cpu, buffer->cpumask))
870 return -ENODEV;
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -0500871 cpu_buffer = buffer->buffers[cpu];
872 work = &cpu_buffer->irq_work;
873 }
874
875
Rabin Vincente30f53a2014-11-10 19:46:34 +0100876 while (true) {
Steven Rostedt (Red Hat)1e0d6712015-02-10 22:14:53 -0500877 if (full)
878 prepare_to_wait(&work->full_waiters, &wait, TASK_INTERRUPTIBLE);
879 else
880 prepare_to_wait(&work->waiters, &wait, TASK_INTERRUPTIBLE);
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -0500881
Rabin Vincente30f53a2014-11-10 19:46:34 +0100882 /*
883 * The events can happen in critical sections where
884 * checking a work queue can cause deadlocks.
885 * After adding a task to the queue, this flag is set
886 * only to notify events to try to wake up the queue
887 * using irq_work.
888 *
889 * We don't clear it even if the buffer is no longer
890 * empty. The flag only causes the next event to run
891 * irq_work to do the work queue wake up. The worse
892 * that can happen if we race with !trace_empty() is that
893 * an event will cause an irq_work to try to wake up
894 * an empty queue.
895 *
896 * There's no reason to protect this flag either, as
897 * the work queue and irq_work logic will do the necessary
898 * synchronization for the wake ups. The only thing
899 * that is necessary is that the wake up happens after
900 * a task has been queued. It's OK for spurious wake ups.
901 */
Steven Rostedt (Red Hat)1e0d6712015-02-10 22:14:53 -0500902 if (full)
903 work->full_waiters_pending = true;
904 else
905 work->waiters_pending = true;
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -0500906
Rabin Vincente30f53a2014-11-10 19:46:34 +0100907 if (signal_pending(current)) {
908 ret = -EINTR;
909 break;
910 }
911
912 if (cpu == RING_BUFFER_ALL_CPUS && !ring_buffer_empty(buffer))
913 break;
914
915 if (cpu != RING_BUFFER_ALL_CPUS &&
916 !ring_buffer_empty_cpu(buffer, cpu)) {
917 unsigned long flags;
918 bool pagebusy;
Steven Rostedt (VMware)2c2b0a72018-11-29 20:32:26 -0500919 size_t nr_pages;
920 size_t dirty;
Rabin Vincente30f53a2014-11-10 19:46:34 +0100921
922 if (!full)
923 break;
924
925 raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
926 pagebusy = cpu_buffer->reader_page == cpu_buffer->commit_page;
Steven Rostedt (VMware)2c2b0a72018-11-29 20:32:26 -0500927 nr_pages = cpu_buffer->nr_pages;
928 dirty = ring_buffer_nr_dirty_pages(buffer, cpu);
929 if (!cpu_buffer->shortest_full ||
930 cpu_buffer->shortest_full < full)
931 cpu_buffer->shortest_full = full;
Rabin Vincente30f53a2014-11-10 19:46:34 +0100932 raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
Steven Rostedt (VMware)2c2b0a72018-11-29 20:32:26 -0500933 if (!pagebusy &&
934 (!nr_pages || (dirty * 100) > full * nr_pages))
Rabin Vincente30f53a2014-11-10 19:46:34 +0100935 break;
936 }
937
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -0500938 schedule();
Rabin Vincente30f53a2014-11-10 19:46:34 +0100939 }
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -0500940
Steven Rostedt (Red Hat)1e0d6712015-02-10 22:14:53 -0500941 if (full)
942 finish_wait(&work->full_waiters, &wait);
943 else
944 finish_wait(&work->waiters, &wait);
Rabin Vincente30f53a2014-11-10 19:46:34 +0100945
946 return ret;
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -0500947}
948
949/**
950 * ring_buffer_poll_wait - poll on buffer input
951 * @buffer: buffer to wait on
952 * @cpu: the cpu buffer to wait on
953 * @filp: the file descriptor
954 * @poll_table: The poll descriptor
955 *
956 * If @cpu == RING_BUFFER_ALL_CPUS then the task will wake up as soon
957 * as data is added to any of the @buffer's cpu buffers. Otherwise
958 * it will wait for data to be added to a specific cpu buffer.
959 *
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800960 * Returns EPOLLIN | EPOLLRDNORM if data exists in the buffers,
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -0500961 * zero otherwise.
962 */
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -0500963__poll_t ring_buffer_poll_wait(struct trace_buffer *buffer, int cpu,
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -0500964 struct file *filp, poll_table *poll_table)
965{
966 struct ring_buffer_per_cpu *cpu_buffer;
967 struct rb_irq_work *work;
968
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -0500969 if (cpu == RING_BUFFER_ALL_CPUS)
970 work = &buffer->irq_work;
971 else {
Steven Rostedt (Red Hat)6721cb62013-05-23 14:21:36 -0400972 if (!cpumask_test_cpu(cpu, buffer->cpumask))
973 return -EINVAL;
974
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -0500975 cpu_buffer = buffer->buffers[cpu];
976 work = &cpu_buffer->irq_work;
977 }
978
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -0500979 poll_wait(filp, &work->waiters, poll_table);
Josef Bacik4ce97db2014-08-25 13:59:41 -0400980 work->waiters_pending = true;
981 /*
982 * There's a tight race between setting the waiters_pending and
983 * checking if the ring buffer is empty. Once the waiters_pending bit
984 * is set, the next event will wake the task up, but we can get stuck
985 * if there's only a single event in.
986 *
987 * FIXME: Ideally, we need a memory barrier on the writer side as well,
988 * but adding a memory barrier to all events will cause too much of a
989 * performance hit in the fast path. We only need a memory barrier when
990 * the buffer goes from empty to having content. But as this race is
991 * extremely small, and it's not a problem if another event comes in, we
992 * will fix it later.
993 */
994 smp_mb();
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -0500995
996 if ((cpu == RING_BUFFER_ALL_CPUS && !ring_buffer_empty(buffer)) ||
997 (cpu != RING_BUFFER_ALL_CPUS && !ring_buffer_empty_cpu(buffer, cpu)))
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800998 return EPOLLIN | EPOLLRDNORM;
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -0500999 return 0;
1000}
1001
Steven Rostedtf536aaf2008-11-10 23:07:30 -05001002/* buffer may be either ring_buffer or ring_buffer_per_cpu */
Steven Rostedt077c5402009-09-03 19:53:46 -04001003#define RB_WARN_ON(b, cond) \
1004 ({ \
1005 int _____ret = unlikely(cond); \
1006 if (_____ret) { \
1007 if (__same_type(*(b), struct ring_buffer_per_cpu)) { \
1008 struct ring_buffer_per_cpu *__b = \
1009 (void *)b; \
1010 atomic_inc(&__b->buffer->record_disabled); \
1011 } else \
1012 atomic_inc(&b->record_disabled); \
1013 WARN_ON(1); \
1014 } \
1015 _____ret; \
Steven Rostedt3e89c7bb2008-11-11 15:28:41 -05001016 })
Steven Rostedtf536aaf2008-11-10 23:07:30 -05001017
Steven Rostedt37886f62009-03-17 17:22:06 -04001018/* Up this if you want to test the TIME_EXTENTS and normalization */
1019#define DEBUG_SHIFT 0
1020
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05001021static inline u64 rb_time_stamp(struct trace_buffer *buffer)
Steven Rostedt88eb0122009-05-11 16:28:23 -04001022{
Steven Rostedt (VMware)bbeba3e2020-06-30 13:05:29 -04001023 u64 ts;
1024
1025 /* Skip retpolines :-( */
1026 if (IS_ENABLED(CONFIG_RETPOLINE) && likely(buffer->clock == trace_clock_local))
1027 ts = trace_clock_local();
1028 else
1029 ts = buffer->clock();
1030
Steven Rostedt88eb0122009-05-11 16:28:23 -04001031 /* shift to debug/test normalization and TIME_EXTENTS */
Steven Rostedt (VMware)bbeba3e2020-06-30 13:05:29 -04001032 return ts << DEBUG_SHIFT;
Steven Rostedt88eb0122009-05-11 16:28:23 -04001033}
1034
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05001035u64 ring_buffer_time_stamp(struct trace_buffer *buffer, int cpu)
Steven Rostedt37886f62009-03-17 17:22:06 -04001036{
1037 u64 time;
1038
1039 preempt_disable_notrace();
Jiri Olsa6d3f1e12009-10-23 19:36:19 -04001040 time = rb_time_stamp(buffer);
Peter Zijlstrad6097c92019-04-23 22:03:18 +02001041 preempt_enable_notrace();
Steven Rostedt37886f62009-03-17 17:22:06 -04001042
1043 return time;
1044}
1045EXPORT_SYMBOL_GPL(ring_buffer_time_stamp);
1046
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05001047void ring_buffer_normalize_time_stamp(struct trace_buffer *buffer,
Steven Rostedt37886f62009-03-17 17:22:06 -04001048 int cpu, u64 *ts)
1049{
1050 /* Just stupid testing the normalize function and deltas */
1051 *ts >>= DEBUG_SHIFT;
1052}
1053EXPORT_SYMBOL_GPL(ring_buffer_normalize_time_stamp);
1054
Steven Rostedt77ae3652009-03-27 11:00:29 -04001055/*
1056 * Making the ring buffer lockless makes things tricky.
1057 * Although writes only happen on the CPU that they are on,
1058 * and they only need to worry about interrupts. Reads can
1059 * happen on any CPU.
1060 *
1061 * The reader page is always off the ring buffer, but when the
1062 * reader finishes with a page, it needs to swap its page with
1063 * a new one from the buffer. The reader needs to take from
1064 * the head (writes go to the tail). But if a writer is in overwrite
1065 * mode and wraps, it must push the head page forward.
1066 *
1067 * Here lies the problem.
1068 *
1069 * The reader must be careful to replace only the head page, and
1070 * not another one. As described at the top of the file in the
1071 * ASCII art, the reader sets its old page to point to the next
1072 * page after head. It then sets the page after head to point to
1073 * the old reader page. But if the writer moves the head page
1074 * during this operation, the reader could end up with the tail.
1075 *
1076 * We use cmpxchg to help prevent this race. We also do something
1077 * special with the page before head. We set the LSB to 1.
1078 *
1079 * When the writer must push the page forward, it will clear the
1080 * bit that points to the head page, move the head, and then set
1081 * the bit that points to the new head page.
1082 *
1083 * We also don't want an interrupt coming in and moving the head
1084 * page on another writer. Thus we use the second LSB to catch
1085 * that too. Thus:
1086 *
1087 * head->list->prev->next bit 1 bit 0
1088 * ------- -------
1089 * Normal page 0 0
1090 * Points to head page 0 1
1091 * New head page 1 0
1092 *
1093 * Note we can not trust the prev pointer of the head page, because:
1094 *
1095 * +----+ +-----+ +-----+
1096 * | |------>| T |---X--->| N |
1097 * | |<------| | | |
1098 * +----+ +-----+ +-----+
1099 * ^ ^ |
1100 * | +-----+ | |
1101 * +----------| R |----------+ |
1102 * | |<-----------+
1103 * +-----+
1104 *
1105 * Key: ---X--> HEAD flag set in pointer
1106 * T Tail page
1107 * R Reader page
1108 * N Next page
1109 *
1110 * (see __rb_reserve_next() to see where this happens)
1111 *
1112 * What the above shows is that the reader just swapped out
1113 * the reader page with a page in the buffer, but before it
1114 * could make the new header point back to the new page added
1115 * it was preempted by a writer. The writer moved forward onto
1116 * the new page added by the reader and is about to move forward
1117 * again.
1118 *
1119 * You can see, it is legitimate for the previous pointer of
1120 * the head (or any page) not to point back to itself. But only
Steven Rostedt (VMware)6167c202018-05-16 11:17:06 -04001121 * temporarily.
Steven Rostedt77ae3652009-03-27 11:00:29 -04001122 */
1123
1124#define RB_PAGE_NORMAL 0UL
1125#define RB_PAGE_HEAD 1UL
1126#define RB_PAGE_UPDATE 2UL
1127
1128
1129#define RB_FLAG_MASK 3UL
1130
1131/* PAGE_MOVED is not part of the mask */
1132#define RB_PAGE_MOVED 4UL
1133
1134/*
1135 * rb_list_head - remove any bit
1136 */
1137static struct list_head *rb_list_head(struct list_head *list)
1138{
1139 unsigned long val = (unsigned long)list;
1140
1141 return (struct list_head *)(val & ~RB_FLAG_MASK);
1142}
1143
1144/*
Jiri Olsa6d3f1e12009-10-23 19:36:19 -04001145 * rb_is_head_page - test if the given page is the head page
Steven Rostedt77ae3652009-03-27 11:00:29 -04001146 *
1147 * Because the reader may move the head_page pointer, we can
1148 * not trust what the head page is (it may be pointing to
1149 * the reader page). But if the next page is a header page,
1150 * its flags will be non zero.
1151 */
Jesper Juhl42b16b32011-01-17 00:09:38 +01001152static inline int
Qiujun Huang6689bed2020-12-25 22:03:56 +08001153rb_is_head_page(struct buffer_page *page, struct list_head *list)
Steven Rostedt77ae3652009-03-27 11:00:29 -04001154{
1155 unsigned long val;
1156
1157 val = (unsigned long)list->next;
1158
1159 if ((val & ~RB_FLAG_MASK) != (unsigned long)&page->list)
1160 return RB_PAGE_MOVED;
1161
1162 return val & RB_FLAG_MASK;
1163}
1164
1165/*
1166 * rb_is_reader_page
1167 *
1168 * The unique thing about the reader page, is that, if the
1169 * writer is ever on it, the previous pointer never points
1170 * back to the reader page.
1171 */
Yaowei Bai06ca3202015-09-29 22:43:31 +08001172static bool rb_is_reader_page(struct buffer_page *page)
Steven Rostedt77ae3652009-03-27 11:00:29 -04001173{
1174 struct list_head *list = page->list.prev;
1175
1176 return rb_list_head(list->next) != &page->list;
1177}
1178
1179/*
1180 * rb_set_list_to_head - set a list_head to be pointing to head.
1181 */
Qiujun Huang6689bed2020-12-25 22:03:56 +08001182static void rb_set_list_to_head(struct list_head *list)
Steven Rostedt77ae3652009-03-27 11:00:29 -04001183{
1184 unsigned long *ptr;
1185
1186 ptr = (unsigned long *)&list->next;
1187 *ptr |= RB_PAGE_HEAD;
1188 *ptr &= ~RB_PAGE_UPDATE;
1189}
1190
1191/*
1192 * rb_head_page_activate - sets up head page
1193 */
1194static void rb_head_page_activate(struct ring_buffer_per_cpu *cpu_buffer)
1195{
1196 struct buffer_page *head;
1197
1198 head = cpu_buffer->head_page;
1199 if (!head)
1200 return;
1201
1202 /*
1203 * Set the previous list pointer to have the HEAD flag.
1204 */
Qiujun Huang6689bed2020-12-25 22:03:56 +08001205 rb_set_list_to_head(head->list.prev);
Steven Rostedt77ae3652009-03-27 11:00:29 -04001206}
1207
1208static void rb_list_head_clear(struct list_head *list)
1209{
1210 unsigned long *ptr = (unsigned long *)&list->next;
1211
1212 *ptr &= ~RB_FLAG_MASK;
1213}
1214
1215/*
Steven Rostedt (VMware)6167c202018-05-16 11:17:06 -04001216 * rb_head_page_deactivate - clears head page ptr (for free list)
Steven Rostedt77ae3652009-03-27 11:00:29 -04001217 */
1218static void
1219rb_head_page_deactivate(struct ring_buffer_per_cpu *cpu_buffer)
1220{
1221 struct list_head *hd;
1222
1223 /* Go through the whole list and clear any pointers found. */
1224 rb_list_head_clear(cpu_buffer->pages);
1225
1226 list_for_each(hd, cpu_buffer->pages)
1227 rb_list_head_clear(hd);
1228}
1229
1230static int rb_head_page_set(struct ring_buffer_per_cpu *cpu_buffer,
1231 struct buffer_page *head,
1232 struct buffer_page *prev,
1233 int old_flag, int new_flag)
1234{
1235 struct list_head *list;
1236 unsigned long val = (unsigned long)&head->list;
1237 unsigned long ret;
1238
1239 list = &prev->list;
1240
1241 val &= ~RB_FLAG_MASK;
1242
Steven Rostedt08a40812009-09-14 09:31:35 -04001243 ret = cmpxchg((unsigned long *)&list->next,
1244 val | old_flag, val | new_flag);
Steven Rostedt77ae3652009-03-27 11:00:29 -04001245
1246 /* check if the reader took the page */
1247 if ((ret & ~RB_FLAG_MASK) != val)
1248 return RB_PAGE_MOVED;
1249
1250 return ret & RB_FLAG_MASK;
1251}
1252
1253static int rb_head_page_set_update(struct ring_buffer_per_cpu *cpu_buffer,
1254 struct buffer_page *head,
1255 struct buffer_page *prev,
1256 int old_flag)
1257{
1258 return rb_head_page_set(cpu_buffer, head, prev,
1259 old_flag, RB_PAGE_UPDATE);
1260}
1261
1262static int rb_head_page_set_head(struct ring_buffer_per_cpu *cpu_buffer,
1263 struct buffer_page *head,
1264 struct buffer_page *prev,
1265 int old_flag)
1266{
1267 return rb_head_page_set(cpu_buffer, head, prev,
1268 old_flag, RB_PAGE_HEAD);
1269}
1270
1271static int rb_head_page_set_normal(struct ring_buffer_per_cpu *cpu_buffer,
1272 struct buffer_page *head,
1273 struct buffer_page *prev,
1274 int old_flag)
1275{
1276 return rb_head_page_set(cpu_buffer, head, prev,
1277 old_flag, RB_PAGE_NORMAL);
1278}
1279
Qiujun Huang6689bed2020-12-25 22:03:56 +08001280static inline void rb_inc_page(struct buffer_page **bpage)
Steven Rostedt77ae3652009-03-27 11:00:29 -04001281{
1282 struct list_head *p = rb_list_head((*bpage)->list.next);
1283
1284 *bpage = list_entry(p, struct buffer_page, list);
1285}
1286
1287static struct buffer_page *
1288rb_set_head_page(struct ring_buffer_per_cpu *cpu_buffer)
1289{
1290 struct buffer_page *head;
1291 struct buffer_page *page;
1292 struct list_head *list;
1293 int i;
1294
1295 if (RB_WARN_ON(cpu_buffer, !cpu_buffer->head_page))
1296 return NULL;
1297
1298 /* sanity check */
1299 list = cpu_buffer->pages;
1300 if (RB_WARN_ON(cpu_buffer, rb_list_head(list->prev->next) != list))
1301 return NULL;
1302
1303 page = head = cpu_buffer->head_page;
1304 /*
1305 * It is possible that the writer moves the header behind
1306 * where we started, and we miss in one loop.
1307 * A second loop should grab the header, but we'll do
1308 * three loops just because I'm paranoid.
1309 */
1310 for (i = 0; i < 3; i++) {
1311 do {
Qiujun Huang6689bed2020-12-25 22:03:56 +08001312 if (rb_is_head_page(page, page->list.prev)) {
Steven Rostedt77ae3652009-03-27 11:00:29 -04001313 cpu_buffer->head_page = page;
1314 return page;
1315 }
Qiujun Huang6689bed2020-12-25 22:03:56 +08001316 rb_inc_page(&page);
Steven Rostedt77ae3652009-03-27 11:00:29 -04001317 } while (page != head);
1318 }
1319
1320 RB_WARN_ON(cpu_buffer, 1);
1321
1322 return NULL;
1323}
1324
1325static int rb_head_page_replace(struct buffer_page *old,
1326 struct buffer_page *new)
1327{
1328 unsigned long *ptr = (unsigned long *)&old->list.prev->next;
1329 unsigned long val;
1330 unsigned long ret;
1331
1332 val = *ptr & ~RB_FLAG_MASK;
1333 val |= RB_PAGE_HEAD;
1334
Steven Rostedt08a40812009-09-14 09:31:35 -04001335 ret = cmpxchg(ptr, val, (unsigned long)&new->list);
Steven Rostedt77ae3652009-03-27 11:00:29 -04001336
1337 return ret == val;
1338}
1339
1340/*
1341 * rb_tail_page_update - move the tail page forward
Steven Rostedt77ae3652009-03-27 11:00:29 -04001342 */
Steven Rostedt (Red Hat)70004982015-11-17 15:15:19 -05001343static void rb_tail_page_update(struct ring_buffer_per_cpu *cpu_buffer,
Steven Rostedt77ae3652009-03-27 11:00:29 -04001344 struct buffer_page *tail_page,
1345 struct buffer_page *next_page)
1346{
Steven Rostedt77ae3652009-03-27 11:00:29 -04001347 unsigned long old_entries;
1348 unsigned long old_write;
Steven Rostedt77ae3652009-03-27 11:00:29 -04001349
1350 /*
1351 * The tail page now needs to be moved forward.
1352 *
1353 * We need to reset the tail page, but without messing
1354 * with possible erasing of data brought in by interrupts
1355 * that have moved the tail page and are currently on it.
1356 *
1357 * We add a counter to the write field to denote this.
1358 */
1359 old_write = local_add_return(RB_WRITE_INTCNT, &next_page->write);
1360 old_entries = local_add_return(RB_WRITE_INTCNT, &next_page->entries);
1361
Steven Rostedt (VMware)2c2b0a72018-11-29 20:32:26 -05001362 local_inc(&cpu_buffer->pages_touched);
Steven Rostedt77ae3652009-03-27 11:00:29 -04001363 /*
1364 * Just make sure we have seen our old_write and synchronize
1365 * with any interrupts that come in.
1366 */
1367 barrier();
1368
1369 /*
1370 * If the tail page is still the same as what we think
1371 * it is, then it is up to us to update the tail
1372 * pointer.
1373 */
Steven Rostedt (Red Hat)85736362015-11-17 14:03:11 -05001374 if (tail_page == READ_ONCE(cpu_buffer->tail_page)) {
Steven Rostedt77ae3652009-03-27 11:00:29 -04001375 /* Zero the write counter */
1376 unsigned long val = old_write & ~RB_WRITE_MASK;
1377 unsigned long eval = old_entries & ~RB_WRITE_MASK;
1378
1379 /*
1380 * This will only succeed if an interrupt did
1381 * not come in and change it. In which case, we
1382 * do not want to modify it.
Lai Jiangshanda706d82009-07-15 16:27:30 +08001383 *
1384 * We add (void) to let the compiler know that we do not care
1385 * about the return value of these functions. We use the
1386 * cmpxchg to only update if an interrupt did not already
1387 * do it for us. If the cmpxchg fails, we don't care.
Steven Rostedt77ae3652009-03-27 11:00:29 -04001388 */
Lai Jiangshanda706d82009-07-15 16:27:30 +08001389 (void)local_cmpxchg(&next_page->write, old_write, val);
1390 (void)local_cmpxchg(&next_page->entries, old_entries, eval);
Steven Rostedt77ae3652009-03-27 11:00:29 -04001391
1392 /*
1393 * No need to worry about races with clearing out the commit.
1394 * it only can increment when a commit takes place. But that
1395 * only happens in the outer most nested commit.
1396 */
1397 local_set(&next_page->page->commit, 0);
1398
Steven Rostedt (Red Hat)70004982015-11-17 15:15:19 -05001399 /* Again, either we update tail_page or an interrupt does */
1400 (void)cmpxchg(&cpu_buffer->tail_page, tail_page, next_page);
Steven Rostedt77ae3652009-03-27 11:00:29 -04001401 }
Steven Rostedt77ae3652009-03-27 11:00:29 -04001402}
1403
1404static int rb_check_bpage(struct ring_buffer_per_cpu *cpu_buffer,
1405 struct buffer_page *bpage)
1406{
1407 unsigned long val = (unsigned long)bpage;
1408
1409 if (RB_WARN_ON(cpu_buffer, val & RB_FLAG_MASK))
1410 return 1;
1411
1412 return 0;
1413}
1414
1415/**
1416 * rb_check_list - make sure a pointer to a list has the last bits zero
1417 */
1418static int rb_check_list(struct ring_buffer_per_cpu *cpu_buffer,
1419 struct list_head *list)
1420{
1421 if (RB_WARN_ON(cpu_buffer, rb_list_head(list->prev) != list->prev))
1422 return 1;
1423 if (RB_WARN_ON(cpu_buffer, rb_list_head(list->next) != list->next))
1424 return 1;
1425 return 0;
1426}
1427
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001428/**
zhangwei(Jovi)d6118512013-07-15 16:32:50 +08001429 * rb_check_pages - integrity check of buffer pages
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001430 * @cpu_buffer: CPU buffer with pages to test
1431 *
Wenji Huangc3706f02009-02-10 01:03:18 -05001432 * As a safety measure we check to make sure the data pages have not
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001433 * been corrupted.
1434 */
1435static int rb_check_pages(struct ring_buffer_per_cpu *cpu_buffer)
1436{
Steven Rostedt3adc54f2009-03-30 15:32:01 -04001437 struct list_head *head = cpu_buffer->pages;
Steven Rostedt044fa782008-12-02 23:50:03 -05001438 struct buffer_page *bpage, *tmp;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001439
Steven Rostedt308f7ee2012-05-16 19:46:32 -04001440 /* Reset the head page if it exists */
1441 if (cpu_buffer->head_page)
1442 rb_set_head_page(cpu_buffer);
1443
Steven Rostedt77ae3652009-03-27 11:00:29 -04001444 rb_head_page_deactivate(cpu_buffer);
1445
Steven Rostedt3e89c7bb2008-11-11 15:28:41 -05001446 if (RB_WARN_ON(cpu_buffer, head->next->prev != head))
1447 return -1;
1448 if (RB_WARN_ON(cpu_buffer, head->prev->next != head))
1449 return -1;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001450
Steven Rostedt77ae3652009-03-27 11:00:29 -04001451 if (rb_check_list(cpu_buffer, head))
1452 return -1;
1453
Steven Rostedt044fa782008-12-02 23:50:03 -05001454 list_for_each_entry_safe(bpage, tmp, head, list) {
Steven Rostedt3e89c7bb2008-11-11 15:28:41 -05001455 if (RB_WARN_ON(cpu_buffer,
Steven Rostedt044fa782008-12-02 23:50:03 -05001456 bpage->list.next->prev != &bpage->list))
Steven Rostedt3e89c7bb2008-11-11 15:28:41 -05001457 return -1;
1458 if (RB_WARN_ON(cpu_buffer,
Steven Rostedt044fa782008-12-02 23:50:03 -05001459 bpage->list.prev->next != &bpage->list))
Steven Rostedt3e89c7bb2008-11-11 15:28:41 -05001460 return -1;
Steven Rostedt77ae3652009-03-27 11:00:29 -04001461 if (rb_check_list(cpu_buffer, &bpage->list))
1462 return -1;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001463 }
1464
Steven Rostedt77ae3652009-03-27 11:00:29 -04001465 rb_head_page_activate(cpu_buffer);
1466
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001467 return 0;
1468}
1469
Qiujun Huang74e2afc2020-10-15 19:38:42 +08001470static int __rb_allocate_pages(struct ring_buffer_per_cpu *cpu_buffer,
1471 long nr_pages, struct list_head *pages)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001472{
Steven Rostedt044fa782008-12-02 23:50:03 -05001473 struct buffer_page *bpage, *tmp;
Steven Rostedt (VMware)927e56d2018-04-04 11:29:57 -04001474 bool user_thread = current->mm != NULL;
1475 gfp_t mflags;
Steven Rostedt (Red Hat)9b94a8f2016-05-12 11:01:24 -04001476 long i;
Steven Rostedt3adc54f2009-03-30 15:32:01 -04001477
Steven Rostedt (VMware)927e56d2018-04-04 11:29:57 -04001478 /*
1479 * Check if the available memory is there first.
1480 * Note, si_mem_available() only gives us a rough estimate of available
1481 * memory. It may not be accurate. But we don't care, we just want
1482 * to prevent doing any allocation when it is obvious that it is
1483 * not going to succeed.
1484 */
Steven Rostedt (VMware)2a872fa2018-04-02 10:33:56 -04001485 i = si_mem_available();
1486 if (i < nr_pages)
1487 return -ENOMEM;
1488
Steven Rostedt (VMware)927e56d2018-04-04 11:29:57 -04001489 /*
1490 * __GFP_RETRY_MAYFAIL flag makes sure that the allocation fails
1491 * gracefully without invoking oom-killer and the system is not
1492 * destabilized.
1493 */
1494 mflags = GFP_KERNEL | __GFP_RETRY_MAYFAIL;
1495
1496 /*
1497 * If a user thread allocates too much, and si_mem_available()
1498 * reports there's enough memory, even though there is not.
1499 * Make sure the OOM killer kills this thread. This can happen
1500 * even with RETRY_MAYFAIL because another task may be doing
1501 * an allocation after this task has taken all memory.
1502 * This is the task the OOM killer needs to take out during this
1503 * loop, even if it was triggered by an allocation somewhere else.
1504 */
1505 if (user_thread)
1506 set_current_oom_origin();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001507 for (i = 0; i < nr_pages; i++) {
Vaibhav Nagarnaik7ea59062011-05-03 17:56:42 -07001508 struct page *page;
Steven Rostedt (VMware)927e56d2018-04-04 11:29:57 -04001509
Steven Rostedt044fa782008-12-02 23:50:03 -05001510 bpage = kzalloc_node(ALIGN(sizeof(*bpage), cache_line_size()),
Qiujun Huang74e2afc2020-10-15 19:38:42 +08001511 mflags, cpu_to_node(cpu_buffer->cpu));
Steven Rostedt044fa782008-12-02 23:50:03 -05001512 if (!bpage)
Steven Rostedte4c2ce82008-10-01 11:14:54 -04001513 goto free_pages;
Steven Rostedt77ae3652009-03-27 11:00:29 -04001514
Qiujun Huang74e2afc2020-10-15 19:38:42 +08001515 rb_check_bpage(cpu_buffer, bpage);
1516
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001517 list_add(&bpage->list, pages);
Steven Rostedt77ae3652009-03-27 11:00:29 -04001518
Qiujun Huang74e2afc2020-10-15 19:38:42 +08001519 page = alloc_pages_node(cpu_to_node(cpu_buffer->cpu), mflags, 0);
Vaibhav Nagarnaik7ea59062011-05-03 17:56:42 -07001520 if (!page)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001521 goto free_pages;
Vaibhav Nagarnaik7ea59062011-05-03 17:56:42 -07001522 bpage->page = page_address(page);
Steven Rostedt044fa782008-12-02 23:50:03 -05001523 rb_init_page(bpage->page);
Steven Rostedt (VMware)927e56d2018-04-04 11:29:57 -04001524
1525 if (user_thread && fatal_signal_pending(current))
1526 goto free_pages;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001527 }
Steven Rostedt (VMware)927e56d2018-04-04 11:29:57 -04001528 if (user_thread)
1529 clear_current_oom_origin();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001530
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001531 return 0;
1532
1533free_pages:
1534 list_for_each_entry_safe(bpage, tmp, pages, list) {
1535 list_del_init(&bpage->list);
1536 free_buffer_page(bpage);
1537 }
Steven Rostedt (VMware)927e56d2018-04-04 11:29:57 -04001538 if (user_thread)
1539 clear_current_oom_origin();
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001540
1541 return -ENOMEM;
1542}
1543
1544static int rb_allocate_pages(struct ring_buffer_per_cpu *cpu_buffer,
Steven Rostedt (Red Hat)9b94a8f2016-05-12 11:01:24 -04001545 unsigned long nr_pages)
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001546{
1547 LIST_HEAD(pages);
1548
1549 WARN_ON(!nr_pages);
1550
Qiujun Huang74e2afc2020-10-15 19:38:42 +08001551 if (__rb_allocate_pages(cpu_buffer, nr_pages, &pages))
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001552 return -ENOMEM;
1553
Steven Rostedt3adc54f2009-03-30 15:32:01 -04001554 /*
1555 * The ring buffer page list is a circular list that does not
1556 * start and end with a list head. All page list items point to
1557 * other pages.
1558 */
1559 cpu_buffer->pages = pages.next;
1560 list_del(&pages);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001561
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001562 cpu_buffer->nr_pages = nr_pages;
1563
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001564 rb_check_pages(cpu_buffer);
1565
1566 return 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001567}
1568
1569static struct ring_buffer_per_cpu *
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05001570rb_allocate_cpu_buffer(struct trace_buffer *buffer, long nr_pages, int cpu)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001571{
1572 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedt044fa782008-12-02 23:50:03 -05001573 struct buffer_page *bpage;
Vaibhav Nagarnaik7ea59062011-05-03 17:56:42 -07001574 struct page *page;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001575 int ret;
1576
1577 cpu_buffer = kzalloc_node(ALIGN(sizeof(*cpu_buffer), cache_line_size()),
1578 GFP_KERNEL, cpu_to_node(cpu));
1579 if (!cpu_buffer)
1580 return NULL;
1581
1582 cpu_buffer->cpu = cpu;
1583 cpu_buffer->buffer = buffer;
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02001584 raw_spin_lock_init(&cpu_buffer->reader_lock);
Peter Zijlstra1f8a6a12009-06-08 18:18:39 +02001585 lockdep_set_class(&cpu_buffer->reader_lock, buffer->reader_lock_key);
Thomas Gleixneredc35bd2009-12-03 12:38:57 +01001586 cpu_buffer->lock = (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001587 INIT_WORK(&cpu_buffer->update_pages_work, update_pages_handler);
Vaibhav Nagarnaik05fdd702012-05-18 13:29:51 -07001588 init_completion(&cpu_buffer->update_done);
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -05001589 init_irq_work(&cpu_buffer->irq_work.work, rb_wake_up_waiters);
Steven Rostedt (Red Hat)f1dc6722013-03-04 17:33:05 -05001590 init_waitqueue_head(&cpu_buffer->irq_work.waiters);
Steven Rostedt (Red Hat)1e0d6712015-02-10 22:14:53 -05001591 init_waitqueue_head(&cpu_buffer->irq_work.full_waiters);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001592
Steven Rostedt044fa782008-12-02 23:50:03 -05001593 bpage = kzalloc_node(ALIGN(sizeof(*bpage), cache_line_size()),
Steven Rostedte4c2ce82008-10-01 11:14:54 -04001594 GFP_KERNEL, cpu_to_node(cpu));
Steven Rostedt044fa782008-12-02 23:50:03 -05001595 if (!bpage)
Steven Rostedte4c2ce82008-10-01 11:14:54 -04001596 goto fail_free_buffer;
1597
Steven Rostedt77ae3652009-03-27 11:00:29 -04001598 rb_check_bpage(cpu_buffer, bpage);
1599
Steven Rostedt044fa782008-12-02 23:50:03 -05001600 cpu_buffer->reader_page = bpage;
Vaibhav Nagarnaik7ea59062011-05-03 17:56:42 -07001601 page = alloc_pages_node(cpu_to_node(cpu), GFP_KERNEL, 0);
1602 if (!page)
Steven Rostedte4c2ce82008-10-01 11:14:54 -04001603 goto fail_free_reader;
Vaibhav Nagarnaik7ea59062011-05-03 17:56:42 -07001604 bpage->page = page_address(page);
Steven Rostedt044fa782008-12-02 23:50:03 -05001605 rb_init_page(bpage->page);
Steven Rostedte4c2ce82008-10-01 11:14:54 -04001606
Steven Rostedtd7690412008-10-01 00:29:53 -04001607 INIT_LIST_HEAD(&cpu_buffer->reader_page->list);
Vaibhav Nagarnaik44b99462012-06-22 11:50:05 -07001608 INIT_LIST_HEAD(&cpu_buffer->new_pages);
Steven Rostedtd7690412008-10-01 00:29:53 -04001609
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001610 ret = rb_allocate_pages(cpu_buffer, nr_pages);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001611 if (ret < 0)
Steven Rostedtd7690412008-10-01 00:29:53 -04001612 goto fail_free_reader;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001613
1614 cpu_buffer->head_page
Steven Rostedt3adc54f2009-03-30 15:32:01 -04001615 = list_entry(cpu_buffer->pages, struct buffer_page, list);
Steven Rostedtbf41a152008-10-04 02:00:59 -04001616 cpu_buffer->tail_page = cpu_buffer->commit_page = cpu_buffer->head_page;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001617
Steven Rostedt77ae3652009-03-27 11:00:29 -04001618 rb_head_page_activate(cpu_buffer);
1619
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001620 return cpu_buffer;
1621
Steven Rostedtd7690412008-10-01 00:29:53 -04001622 fail_free_reader:
1623 free_buffer_page(cpu_buffer->reader_page);
1624
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001625 fail_free_buffer:
1626 kfree(cpu_buffer);
1627 return NULL;
1628}
1629
1630static void rb_free_cpu_buffer(struct ring_buffer_per_cpu *cpu_buffer)
1631{
Steven Rostedt3adc54f2009-03-30 15:32:01 -04001632 struct list_head *head = cpu_buffer->pages;
Steven Rostedt044fa782008-12-02 23:50:03 -05001633 struct buffer_page *bpage, *tmp;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001634
Steven Rostedtd7690412008-10-01 00:29:53 -04001635 free_buffer_page(cpu_buffer->reader_page);
1636
Steven Rostedt77ae3652009-03-27 11:00:29 -04001637 rb_head_page_deactivate(cpu_buffer);
1638
Steven Rostedt3adc54f2009-03-30 15:32:01 -04001639 if (head) {
1640 list_for_each_entry_safe(bpage, tmp, head, list) {
1641 list_del_init(&bpage->list);
1642 free_buffer_page(bpage);
1643 }
1644 bpage = list_entry(head, struct buffer_page, list);
Steven Rostedt044fa782008-12-02 23:50:03 -05001645 free_buffer_page(bpage);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001646 }
Steven Rostedt3adc54f2009-03-30 15:32:01 -04001647
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001648 kfree(cpu_buffer);
1649}
1650
1651/**
zhangwei(Jovi)d6118512013-07-15 16:32:50 +08001652 * __ring_buffer_alloc - allocate a new ring_buffer
Robert Richter68814b52008-11-24 12:24:12 +01001653 * @size: the size in bytes per cpu that is needed.
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001654 * @flags: attributes to set for the ring buffer.
Fabian Frederick59e7cff2014-06-05 20:22:05 +02001655 * @key: ring buffer reader_lock_key.
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001656 *
1657 * Currently the only flag that is available is the RB_FL_OVERWRITE
1658 * flag. This flag means that the buffer will overwrite old data
1659 * when the buffer wraps. If this flag is not set, the buffer will
1660 * drop data when the tail hits the head.
1661 */
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05001662struct trace_buffer *__ring_buffer_alloc(unsigned long size, unsigned flags,
Peter Zijlstra1f8a6a12009-06-08 18:18:39 +02001663 struct lock_class_key *key)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001664{
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05001665 struct trace_buffer *buffer;
Steven Rostedt (Red Hat)9b94a8f2016-05-12 11:01:24 -04001666 long nr_pages;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001667 int bsize;
Steven Rostedt (Red Hat)9b94a8f2016-05-12 11:01:24 -04001668 int cpu;
Sebastian Andrzej Siewiorb32614c2016-11-27 00:13:34 +01001669 int ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001670
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001671 /* keep it in its own cache line */
1672 buffer = kzalloc(ALIGN(sizeof(*buffer), cache_line_size()),
1673 GFP_KERNEL);
1674 if (!buffer)
1675 return NULL;
1676
Sebastian Andrzej Siewiorb18cc3d2016-12-07 14:31:33 +01001677 if (!zalloc_cpumask_var(&buffer->cpumask, GFP_KERNEL))
Rusty Russell9e01c1b2009-01-01 10:12:22 +10301678 goto fail_free_buffer;
1679
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001680 nr_pages = DIV_ROUND_UP(size, BUF_PAGE_SIZE);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001681 buffer->flags = flags;
Steven Rostedt37886f62009-03-17 17:22:06 -04001682 buffer->clock = trace_clock_local;
Peter Zijlstra1f8a6a12009-06-08 18:18:39 +02001683 buffer->reader_lock_key = key;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001684
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -05001685 init_irq_work(&buffer->irq_work.work, rb_wake_up_waiters);
Steven Rostedt (Red Hat)f1dc6722013-03-04 17:33:05 -05001686 init_waitqueue_head(&buffer->irq_work.waiters);
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -05001687
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001688 /* need at least two pages */
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001689 if (nr_pages < 2)
1690 nr_pages = 2;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001691
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001692 buffer->cpus = nr_cpu_ids;
1693
1694 bsize = sizeof(void *) * nr_cpu_ids;
1695 buffer->buffers = kzalloc(ALIGN(bsize, cache_line_size()),
1696 GFP_KERNEL);
1697 if (!buffer->buffers)
Rusty Russell9e01c1b2009-01-01 10:12:22 +10301698 goto fail_free_cpumask;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001699
Sebastian Andrzej Siewiorb32614c2016-11-27 00:13:34 +01001700 cpu = raw_smp_processor_id();
1701 cpumask_set_cpu(cpu, buffer->cpumask);
1702 buffer->buffers[cpu] = rb_allocate_cpu_buffer(buffer, nr_pages, cpu);
1703 if (!buffer->buffers[cpu])
1704 goto fail_free_buffers;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001705
Sebastian Andrzej Siewiorb32614c2016-11-27 00:13:34 +01001706 ret = cpuhp_state_add_instance(CPUHP_TRACE_RB_PREPARE, &buffer->node);
1707 if (ret < 0)
1708 goto fail_free_buffers;
Steven Rostedt554f7862009-03-11 22:00:13 -04001709
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001710 mutex_init(&buffer->mutex);
1711
1712 return buffer;
1713
1714 fail_free_buffers:
1715 for_each_buffer_cpu(buffer, cpu) {
1716 if (buffer->buffers[cpu])
1717 rb_free_cpu_buffer(buffer->buffers[cpu]);
1718 }
1719 kfree(buffer->buffers);
1720
Rusty Russell9e01c1b2009-01-01 10:12:22 +10301721 fail_free_cpumask:
1722 free_cpumask_var(buffer->cpumask);
1723
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001724 fail_free_buffer:
1725 kfree(buffer);
1726 return NULL;
1727}
Peter Zijlstra1f8a6a12009-06-08 18:18:39 +02001728EXPORT_SYMBOL_GPL(__ring_buffer_alloc);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001729
1730/**
1731 * ring_buffer_free - free a ring buffer.
1732 * @buffer: the buffer to free.
1733 */
1734void
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05001735ring_buffer_free(struct trace_buffer *buffer)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001736{
1737 int cpu;
1738
Sebastian Andrzej Siewiorb32614c2016-11-27 00:13:34 +01001739 cpuhp_state_remove_instance(CPUHP_TRACE_RB_PREPARE, &buffer->node);
Steven Rostedt554f7862009-03-11 22:00:13 -04001740
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001741 for_each_buffer_cpu(buffer, cpu)
1742 rb_free_cpu_buffer(buffer->buffers[cpu]);
1743
Eric Dumazetbd3f0222009-08-07 12:49:29 +02001744 kfree(buffer->buffers);
Rusty Russell9e01c1b2009-01-01 10:12:22 +10301745 free_cpumask_var(buffer->cpumask);
1746
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001747 kfree(buffer);
1748}
Robert Richterc4f50182008-12-11 16:49:22 +01001749EXPORT_SYMBOL_GPL(ring_buffer_free);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001750
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05001751void ring_buffer_set_clock(struct trace_buffer *buffer,
Steven Rostedt37886f62009-03-17 17:22:06 -04001752 u64 (*clock)(void))
1753{
1754 buffer->clock = clock;
1755}
1756
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05001757void ring_buffer_set_time_stamp_abs(struct trace_buffer *buffer, bool abs)
Tom Zanussi00b41452018-01-15 20:51:39 -06001758{
1759 buffer->time_stamp_abs = abs;
1760}
1761
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05001762bool ring_buffer_time_stamp_abs(struct trace_buffer *buffer)
Tom Zanussi00b41452018-01-15 20:51:39 -06001763{
1764 return buffer->time_stamp_abs;
1765}
1766
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001767static void rb_reset_cpu(struct ring_buffer_per_cpu *cpu_buffer);
1768
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001769static inline unsigned long rb_page_entries(struct buffer_page *bpage)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001770{
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001771 return local_read(&bpage->entries) & RB_WRITE_MASK;
1772}
1773
1774static inline unsigned long rb_page_write(struct buffer_page *bpage)
1775{
1776 return local_read(&bpage->write) & RB_WRITE_MASK;
1777}
1778
Vaibhav Nagarnaik5040b4b2012-05-03 18:59:51 -07001779static int
Steven Rostedt (Red Hat)9b94a8f2016-05-12 11:01:24 -04001780rb_remove_pages(struct ring_buffer_per_cpu *cpu_buffer, unsigned long nr_pages)
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001781{
1782 struct list_head *tail_page, *to_remove, *next_page;
1783 struct buffer_page *to_remove_page, *tmp_iter_page;
1784 struct buffer_page *last_page, *first_page;
Steven Rostedt (Red Hat)9b94a8f2016-05-12 11:01:24 -04001785 unsigned long nr_removed;
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001786 unsigned long head_bit;
1787 int page_entries;
1788
1789 head_bit = 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001790
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02001791 raw_spin_lock_irq(&cpu_buffer->reader_lock);
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001792 atomic_inc(&cpu_buffer->record_disabled);
1793 /*
1794 * We don't race with the readers since we have acquired the reader
1795 * lock. We also don't race with writers after disabling recording.
1796 * This makes it easy to figure out the first and the last page to be
1797 * removed from the list. We unlink all the pages in between including
1798 * the first and last pages. This is done in a busy loop so that we
1799 * lose the least number of traces.
1800 * The pages are freed after we restart recording and unlock readers.
1801 */
1802 tail_page = &cpu_buffer->tail_page->list;
Steven Rostedt77ae3652009-03-27 11:00:29 -04001803
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001804 /*
1805 * tail page might be on reader page, we remove the next page
1806 * from the ring buffer
1807 */
1808 if (cpu_buffer->tail_page == cpu_buffer->reader_page)
1809 tail_page = rb_list_head(tail_page->next);
1810 to_remove = tail_page;
1811
1812 /* start of pages to remove */
1813 first_page = list_entry(rb_list_head(to_remove->next),
1814 struct buffer_page, list);
1815
1816 for (nr_removed = 0; nr_removed < nr_pages; nr_removed++) {
1817 to_remove = rb_list_head(to_remove)->next;
1818 head_bit |= (unsigned long)to_remove & RB_PAGE_HEAD;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001819 }
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001820
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001821 next_page = rb_list_head(to_remove)->next;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001822
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001823 /*
1824 * Now we remove all pages between tail_page and next_page.
1825 * Make sure that we have head_bit value preserved for the
1826 * next page
1827 */
1828 tail_page->next = (struct list_head *)((unsigned long)next_page |
1829 head_bit);
1830 next_page = rb_list_head(next_page);
1831 next_page->prev = tail_page;
1832
1833 /* make sure pages points to a valid page in the ring buffer */
1834 cpu_buffer->pages = next_page;
1835
1836 /* update head page */
1837 if (head_bit)
1838 cpu_buffer->head_page = list_entry(next_page,
1839 struct buffer_page, list);
1840
1841 /*
1842 * change read pointer to make sure any read iterators reset
1843 * themselves
1844 */
1845 cpu_buffer->read = 0;
1846
1847 /* pages are removed, resume tracing and then free the pages */
1848 atomic_dec(&cpu_buffer->record_disabled);
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02001849 raw_spin_unlock_irq(&cpu_buffer->reader_lock);
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001850
1851 RB_WARN_ON(cpu_buffer, list_empty(cpu_buffer->pages));
1852
1853 /* last buffer page to remove */
1854 last_page = list_entry(rb_list_head(to_remove), struct buffer_page,
1855 list);
1856 tmp_iter_page = first_page;
1857
1858 do {
Vaibhav Nagarnaik83f36552018-09-07 15:31:29 -07001859 cond_resched();
1860
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001861 to_remove_page = tmp_iter_page;
Qiujun Huang6689bed2020-12-25 22:03:56 +08001862 rb_inc_page(&tmp_iter_page);
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001863
1864 /* update the counters */
1865 page_entries = rb_page_entries(to_remove_page);
1866 if (page_entries) {
1867 /*
1868 * If something was added to this page, it was full
1869 * since it is not the tail page. So we deduct the
1870 * bytes consumed in ring buffer from here.
Vaibhav Nagarnaik48fdc722012-06-29 12:31:41 -07001871 * Increment overrun to account for the lost events.
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001872 */
Vaibhav Nagarnaik48fdc722012-06-29 12:31:41 -07001873 local_add(page_entries, &cpu_buffer->overrun);
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001874 local_sub(BUF_PAGE_SIZE, &cpu_buffer->entries_bytes);
1875 }
1876
1877 /*
1878 * We have already removed references to this list item, just
1879 * free up the buffer_page and its page
1880 */
1881 free_buffer_page(to_remove_page);
1882 nr_removed--;
1883
1884 } while (to_remove_page != last_page);
1885
1886 RB_WARN_ON(cpu_buffer, nr_removed);
Vaibhav Nagarnaik5040b4b2012-05-03 18:59:51 -07001887
1888 return nr_removed == 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001889}
1890
Vaibhav Nagarnaik5040b4b2012-05-03 18:59:51 -07001891static int
1892rb_insert_pages(struct ring_buffer_per_cpu *cpu_buffer)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001893{
Vaibhav Nagarnaik5040b4b2012-05-03 18:59:51 -07001894 struct list_head *pages = &cpu_buffer->new_pages;
1895 int retries, success;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001896
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02001897 raw_spin_lock_irq(&cpu_buffer->reader_lock);
Vaibhav Nagarnaik5040b4b2012-05-03 18:59:51 -07001898 /*
1899 * We are holding the reader lock, so the reader page won't be swapped
1900 * in the ring buffer. Now we are racing with the writer trying to
1901 * move head page and the tail page.
1902 * We are going to adapt the reader page update process where:
1903 * 1. We first splice the start and end of list of new pages between
1904 * the head page and its previous page.
1905 * 2. We cmpxchg the prev_page->next to point from head page to the
1906 * start of new pages list.
1907 * 3. Finally, we update the head->prev to the end of new list.
1908 *
1909 * We will try this process 10 times, to make sure that we don't keep
1910 * spinning.
1911 */
1912 retries = 10;
1913 success = 0;
1914 while (retries--) {
1915 struct list_head *head_page, *prev_page, *r;
1916 struct list_head *last_page, *first_page;
1917 struct list_head *head_page_with_bit;
Steven Rostedt77ae3652009-03-27 11:00:29 -04001918
Vaibhav Nagarnaik5040b4b2012-05-03 18:59:51 -07001919 head_page = &rb_set_head_page(cpu_buffer)->list;
Steven Rostedt54f7be52012-11-29 22:27:22 -05001920 if (!head_page)
1921 break;
Vaibhav Nagarnaik5040b4b2012-05-03 18:59:51 -07001922 prev_page = head_page->prev;
1923
1924 first_page = pages->next;
1925 last_page = pages->prev;
1926
1927 head_page_with_bit = (struct list_head *)
1928 ((unsigned long)head_page | RB_PAGE_HEAD);
1929
1930 last_page->next = head_page_with_bit;
1931 first_page->prev = prev_page;
1932
1933 r = cmpxchg(&prev_page->next, head_page_with_bit, first_page);
1934
1935 if (r == head_page_with_bit) {
1936 /*
1937 * yay, we replaced the page pointer to our new list,
1938 * now, we just have to update to head page's prev
1939 * pointer to point to end of list
1940 */
1941 head_page->prev = last_page;
1942 success = 1;
1943 break;
1944 }
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001945 }
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001946
Vaibhav Nagarnaik5040b4b2012-05-03 18:59:51 -07001947 if (success)
1948 INIT_LIST_HEAD(pages);
1949 /*
1950 * If we weren't successful in adding in new pages, warn and stop
1951 * tracing
1952 */
1953 RB_WARN_ON(cpu_buffer, !success);
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02001954 raw_spin_unlock_irq(&cpu_buffer->reader_lock);
Vaibhav Nagarnaik5040b4b2012-05-03 18:59:51 -07001955
1956 /* free pages if they weren't inserted */
1957 if (!success) {
1958 struct buffer_page *bpage, *tmp;
1959 list_for_each_entry_safe(bpage, tmp, &cpu_buffer->new_pages,
1960 list) {
1961 list_del_init(&bpage->list);
1962 free_buffer_page(bpage);
1963 }
1964 }
1965 return success;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001966}
1967
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001968static void rb_update_pages(struct ring_buffer_per_cpu *cpu_buffer)
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001969{
Vaibhav Nagarnaik5040b4b2012-05-03 18:59:51 -07001970 int success;
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001971
Vaibhav Nagarnaik5040b4b2012-05-03 18:59:51 -07001972 if (cpu_buffer->nr_pages_to_update > 0)
1973 success = rb_insert_pages(cpu_buffer);
1974 else
1975 success = rb_remove_pages(cpu_buffer,
1976 -cpu_buffer->nr_pages_to_update);
1977
1978 if (success)
1979 cpu_buffer->nr_pages += cpu_buffer->nr_pages_to_update;
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001980}
1981
1982static void update_pages_handler(struct work_struct *work)
1983{
1984 struct ring_buffer_per_cpu *cpu_buffer = container_of(work,
1985 struct ring_buffer_per_cpu, update_pages_work);
1986 rb_update_pages(cpu_buffer);
Vaibhav Nagarnaik05fdd702012-05-18 13:29:51 -07001987 complete(&cpu_buffer->update_done);
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001988}
1989
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001990/**
1991 * ring_buffer_resize - resize the ring buffer
1992 * @buffer: the buffer to resize.
1993 * @size: the new size.
zhangwei(Jovi)d6118512013-07-15 16:32:50 +08001994 * @cpu_id: the cpu buffer to resize
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001995 *
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001996 * Minimum size is 2 * BUF_PAGE_SIZE.
1997 *
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001998 * Returns 0 on success and < 0 on failure.
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001999 */
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05002000int ring_buffer_resize(struct trace_buffer *buffer, unsigned long size,
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08002001 int cpu_id)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002002{
2003 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedt (Red Hat)9b94a8f2016-05-12 11:01:24 -04002004 unsigned long nr_pages;
Qiujun Huang0a1754b2020-10-19 22:22:42 +08002005 int cpu, err;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002006
Ingo Molnaree51a1d2008-11-13 14:58:31 +01002007 /*
2008 * Always succeed at resizing a non-existent buffer:
2009 */
2010 if (!buffer)
Qiujun Huang0a1754b2020-10-19 22:22:42 +08002011 return 0;
Ingo Molnaree51a1d2008-11-13 14:58:31 +01002012
Steven Rostedt6a31e1f2012-05-23 15:35:17 -04002013 /* Make sure the requested buffer exists */
2014 if (cpu_id != RING_BUFFER_ALL_CPUS &&
2015 !cpumask_test_cpu(cpu_id, buffer->cpumask))
Qiujun Huang0a1754b2020-10-19 22:22:42 +08002016 return 0;
Steven Rostedt6a31e1f2012-05-23 15:35:17 -04002017
Steven Rostedt (Red Hat)59643d12016-05-13 09:34:12 -04002018 nr_pages = DIV_ROUND_UP(size, BUF_PAGE_SIZE);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002019
2020 /* we need a minimum of two pages */
Steven Rostedt (Red Hat)59643d12016-05-13 09:34:12 -04002021 if (nr_pages < 2)
2022 nr_pages = 2;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002023
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07002024 /* prevent another thread from changing buffer sizes */
2025 mutex_lock(&buffer->mutex);
2026
Steven Rostedt (VMware)07b8b102020-03-27 16:21:22 -04002027
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08002028 if (cpu_id == RING_BUFFER_ALL_CPUS) {
Steven Rostedt (VMware)07b8b102020-03-27 16:21:22 -04002029 /*
2030 * Don't succeed if resizing is disabled, as a reader might be
2031 * manipulating the ring buffer and is expecting a sane state while
2032 * this is true.
2033 */
2034 for_each_buffer_cpu(buffer, cpu) {
2035 cpu_buffer = buffer->buffers[cpu];
2036 if (atomic_read(&cpu_buffer->resize_disabled)) {
2037 err = -EBUSY;
2038 goto out_err_unlock;
2039 }
2040 }
2041
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08002042 /* calculate the pages to update */
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002043 for_each_buffer_cpu(buffer, cpu) {
2044 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002045
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08002046 cpu_buffer->nr_pages_to_update = nr_pages -
2047 cpu_buffer->nr_pages;
Vaibhav Nagarnaikd7ec4bf2011-06-07 17:01:42 -07002048 /*
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08002049 * nothing more to do for removing pages or no update
Vaibhav Nagarnaikd7ec4bf2011-06-07 17:01:42 -07002050 */
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08002051 if (cpu_buffer->nr_pages_to_update <= 0)
2052 continue;
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08002053 /*
2054 * to add pages, make sure all new pages can be
2055 * allocated without receiving ENOMEM
2056 */
2057 INIT_LIST_HEAD(&cpu_buffer->new_pages);
Qiujun Huang74e2afc2020-10-15 19:38:42 +08002058 if (__rb_allocate_pages(cpu_buffer, cpu_buffer->nr_pages_to_update,
2059 &cpu_buffer->new_pages)) {
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08002060 /* not enough memory for new pages */
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07002061 err = -ENOMEM;
2062 goto out_err;
2063 }
2064 }
2065
2066 get_online_cpus();
2067 /*
2068 * Fire off all the required work handlers
Vaibhav Nagarnaik05fdd702012-05-18 13:29:51 -07002069 * We can't schedule on offline CPUs, but it's not necessary
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07002070 * since we can change their buffer sizes without any race.
2071 */
2072 for_each_buffer_cpu(buffer, cpu) {
2073 cpu_buffer = buffer->buffers[cpu];
Vaibhav Nagarnaik05fdd702012-05-18 13:29:51 -07002074 if (!cpu_buffer->nr_pages_to_update)
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07002075 continue;
2076
Corey Minyard021c5b32014-07-16 14:07:13 -05002077 /* Can't run something on an offline CPU. */
2078 if (!cpu_online(cpu)) {
Steven Rostedt (Red Hat)f5eb5582013-03-07 09:27:42 -05002079 rb_update_pages(cpu_buffer);
2080 cpu_buffer->nr_pages_to_update = 0;
2081 } else {
Vaibhav Nagarnaik05fdd702012-05-18 13:29:51 -07002082 schedule_work_on(cpu,
2083 &cpu_buffer->update_pages_work);
Steven Rostedt (Red Hat)f5eb5582013-03-07 09:27:42 -05002084 }
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002085 }
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002086
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08002087 /* wait for all the updates to complete */
2088 for_each_buffer_cpu(buffer, cpu) {
2089 cpu_buffer = buffer->buffers[cpu];
Vaibhav Nagarnaik05fdd702012-05-18 13:29:51 -07002090 if (!cpu_buffer->nr_pages_to_update)
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07002091 continue;
2092
Vaibhav Nagarnaik05fdd702012-05-18 13:29:51 -07002093 if (cpu_online(cpu))
2094 wait_for_completion(&cpu_buffer->update_done);
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07002095 cpu_buffer->nr_pages_to_update = 0;
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08002096 }
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07002097
2098 put_online_cpus();
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08002099 } else {
2100 cpu_buffer = buffer->buffers[cpu_id];
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07002101
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08002102 if (nr_pages == cpu_buffer->nr_pages)
2103 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002104
Steven Rostedt (VMware)07b8b102020-03-27 16:21:22 -04002105 /*
2106 * Don't succeed if resizing is disabled, as a reader might be
2107 * manipulating the ring buffer and is expecting a sane state while
2108 * this is true.
2109 */
2110 if (atomic_read(&cpu_buffer->resize_disabled)) {
2111 err = -EBUSY;
2112 goto out_err_unlock;
2113 }
2114
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08002115 cpu_buffer->nr_pages_to_update = nr_pages -
2116 cpu_buffer->nr_pages;
2117
2118 INIT_LIST_HEAD(&cpu_buffer->new_pages);
2119 if (cpu_buffer->nr_pages_to_update > 0 &&
Qiujun Huang74e2afc2020-10-15 19:38:42 +08002120 __rb_allocate_pages(cpu_buffer, cpu_buffer->nr_pages_to_update,
2121 &cpu_buffer->new_pages)) {
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07002122 err = -ENOMEM;
2123 goto out_err;
2124 }
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08002125
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07002126 get_online_cpus();
2127
Corey Minyard021c5b32014-07-16 14:07:13 -05002128 /* Can't run something on an offline CPU. */
2129 if (!cpu_online(cpu_id))
Steven Rostedt (Red Hat)f5eb5582013-03-07 09:27:42 -05002130 rb_update_pages(cpu_buffer);
2131 else {
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07002132 schedule_work_on(cpu_id,
2133 &cpu_buffer->update_pages_work);
Vaibhav Nagarnaik05fdd702012-05-18 13:29:51 -07002134 wait_for_completion(&cpu_buffer->update_done);
Steven Rostedt (Red Hat)f5eb5582013-03-07 09:27:42 -05002135 }
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07002136
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07002137 cpu_buffer->nr_pages_to_update = 0;
Vaibhav Nagarnaik05fdd702012-05-18 13:29:51 -07002138 put_online_cpus();
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08002139 }
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002140
2141 out:
Steven Rostedt659f4512012-05-14 17:02:33 -04002142 /*
2143 * The ring buffer resize can happen with the ring buffer
2144 * enabled, so that the update disturbs the tracing as little
2145 * as possible. But if the buffer is disabled, we do not need
2146 * to worry about that, and we can take the time to verify
2147 * that the buffer is not corrupt.
2148 */
2149 if (atomic_read(&buffer->record_disabled)) {
2150 atomic_inc(&buffer->record_disabled);
2151 /*
2152 * Even though the buffer was disabled, we must make sure
2153 * that it is truly disabled before calling rb_check_pages.
2154 * There could have been a race between checking
2155 * record_disable and incrementing it.
2156 */
Paul E. McKenney74401722018-11-06 18:44:52 -08002157 synchronize_rcu();
Steven Rostedt659f4512012-05-14 17:02:33 -04002158 for_each_buffer_cpu(buffer, cpu) {
2159 cpu_buffer = buffer->buffers[cpu];
2160 rb_check_pages(cpu_buffer);
2161 }
2162 atomic_dec(&buffer->record_disabled);
2163 }
2164
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002165 mutex_unlock(&buffer->mutex);
Qiujun Huang0a1754b2020-10-19 22:22:42 +08002166 return 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002167
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07002168 out_err:
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08002169 for_each_buffer_cpu(buffer, cpu) {
2170 struct buffer_page *bpage, *tmp;
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07002171
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08002172 cpu_buffer = buffer->buffers[cpu];
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08002173 cpu_buffer->nr_pages_to_update = 0;
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07002174
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08002175 if (list_empty(&cpu_buffer->new_pages))
2176 continue;
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07002177
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08002178 list_for_each_entry_safe(bpage, tmp, &cpu_buffer->new_pages,
2179 list) {
2180 list_del_init(&bpage->list);
2181 free_buffer_page(bpage);
2182 }
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002183 }
Steven Rostedt (VMware)07b8b102020-03-27 16:21:22 -04002184 out_err_unlock:
Vegard Nossum641d2f62008-11-18 19:22:13 +01002185 mutex_unlock(&buffer->mutex);
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07002186 return err;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002187}
Robert Richterc4f50182008-12-11 16:49:22 +01002188EXPORT_SYMBOL_GPL(ring_buffer_resize);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002189
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05002190void ring_buffer_change_overwrite(struct trace_buffer *buffer, int val)
David Sharp750912f2010-12-08 13:46:47 -08002191{
2192 mutex_lock(&buffer->mutex);
2193 if (val)
2194 buffer->flags |= RB_FL_OVERWRITE;
2195 else
2196 buffer->flags &= ~RB_FL_OVERWRITE;
2197 mutex_unlock(&buffer->mutex);
2198}
2199EXPORT_SYMBOL_GPL(ring_buffer_change_overwrite);
2200
Steven Rostedt (Red Hat)2289d562016-11-23 20:35:32 -05002201static __always_inline void *__rb_page_index(struct buffer_page *bpage, unsigned index)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002202{
Steven Rostedt044fa782008-12-02 23:50:03 -05002203 return bpage->page->data + index;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002204}
2205
Steven Rostedt (Red Hat)2289d562016-11-23 20:35:32 -05002206static __always_inline struct ring_buffer_event *
Steven Rostedtd7690412008-10-01 00:29:53 -04002207rb_reader_event(struct ring_buffer_per_cpu *cpu_buffer)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002208{
Steven Rostedt6f807ac2008-10-04 02:00:58 -04002209 return __rb_page_index(cpu_buffer->reader_page,
2210 cpu_buffer->reader_page->read);
2211}
2212
Steven Rostedt (Red Hat)2289d562016-11-23 20:35:32 -05002213static __always_inline unsigned rb_page_commit(struct buffer_page *bpage)
Steven Rostedtbf41a152008-10-04 02:00:59 -04002214{
Steven Rostedtabc9b562008-12-02 15:34:06 -05002215 return local_read(&bpage->page->commit);
Steven Rostedtbf41a152008-10-04 02:00:59 -04002216}
2217
Steven Rostedt (VMware)785888c2020-03-17 17:32:27 -04002218static struct ring_buffer_event *
2219rb_iter_head_event(struct ring_buffer_iter *iter)
2220{
2221 struct ring_buffer_event *event;
2222 struct buffer_page *iter_head_page = iter->head_page;
2223 unsigned long commit;
2224 unsigned length;
2225
Steven Rostedt (VMware)153368c2020-03-17 17:32:29 -04002226 if (iter->head != iter->next_event)
2227 return iter->event;
2228
Steven Rostedt (VMware)785888c2020-03-17 17:32:27 -04002229 /*
2230 * When the writer goes across pages, it issues a cmpxchg which
2231 * is a mb(), which will synchronize with the rmb here.
2232 * (see rb_tail_page_update() and __rb_reserve_next())
2233 */
2234 commit = rb_page_commit(iter_head_page);
2235 smp_rmb();
2236 event = __rb_page_index(iter_head_page, iter->head);
2237 length = rb_event_length(event);
2238
2239 /*
2240 * READ_ONCE() doesn't work on functions and we don't want the
2241 * compiler doing any crazy optimizations with length.
2242 */
2243 barrier();
2244
2245 if ((iter->head + length) > commit || length > BUF_MAX_DATA_SIZE)
2246 /* Writer corrupted the read? */
2247 goto reset;
2248
2249 memcpy(iter->event, event, length);
2250 /*
2251 * If the page stamp is still the same after this rmb() then the
2252 * event was safely copied without the writer entering the page.
2253 */
2254 smp_rmb();
2255
2256 /* Make sure the page didn't change since we read this */
2257 if (iter->page_stamp != iter_head_page->page->time_stamp ||
2258 commit > rb_page_commit(iter_head_page))
2259 goto reset;
2260
2261 iter->next_event = iter->head + length;
2262 return iter->event;
2263 reset:
2264 /* Reset to the beginning */
2265 iter->page_stamp = iter->read_stamp = iter->head_page->page->time_stamp;
2266 iter->head = 0;
2267 iter->next_event = 0;
Steven Rostedt (VMware)c9b7a4a2020-03-17 17:32:32 -04002268 iter->missed_events = 1;
Steven Rostedt (VMware)785888c2020-03-17 17:32:27 -04002269 return NULL;
2270}
2271
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002272/* Size is determined by what has been committed */
Steven Rostedt (Red Hat)2289d562016-11-23 20:35:32 -05002273static __always_inline unsigned rb_page_size(struct buffer_page *bpage)
Steven Rostedtbf41a152008-10-04 02:00:59 -04002274{
2275 return rb_page_commit(bpage);
2276}
2277
Steven Rostedt (Red Hat)2289d562016-11-23 20:35:32 -05002278static __always_inline unsigned
Steven Rostedtbf41a152008-10-04 02:00:59 -04002279rb_commit_index(struct ring_buffer_per_cpu *cpu_buffer)
2280{
2281 return rb_page_commit(cpu_buffer->commit_page);
2282}
2283
Steven Rostedt (Red Hat)2289d562016-11-23 20:35:32 -05002284static __always_inline unsigned
Steven Rostedtbf41a152008-10-04 02:00:59 -04002285rb_event_index(struct ring_buffer_event *event)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002286{
Steven Rostedtbf41a152008-10-04 02:00:59 -04002287 unsigned long addr = (unsigned long)event;
2288
Steven Rostedt22f470f2009-06-11 09:29:58 -04002289 return (addr & ~PAGE_MASK) - BUF_PAGE_HDR_SIZE;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002290}
2291
Andrew Morton34a148b2009-01-09 12:27:09 -08002292static void rb_inc_iter(struct ring_buffer_iter *iter)
Steven Rostedtd7690412008-10-01 00:29:53 -04002293{
2294 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
2295
2296 /*
2297 * The iterator could be on the reader page (it starts there).
2298 * But the head could have moved, since the reader was
2299 * found. Check for this case and assign the iterator
2300 * to the head page instead of next.
2301 */
2302 if (iter->head_page == cpu_buffer->reader_page)
Steven Rostedt77ae3652009-03-27 11:00:29 -04002303 iter->head_page = rb_set_head_page(cpu_buffer);
Steven Rostedtd7690412008-10-01 00:29:53 -04002304 else
Qiujun Huang6689bed2020-12-25 22:03:56 +08002305 rb_inc_page(&iter->head_page);
Steven Rostedtd7690412008-10-01 00:29:53 -04002306
Steven Rostedt (VMware)28e3fc52020-03-17 17:32:26 -04002307 iter->page_stamp = iter->read_stamp = iter->head_page->page->time_stamp;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002308 iter->head = 0;
Steven Rostedt (VMware)785888c2020-03-17 17:32:27 -04002309 iter->next_event = 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002310}
2311
Steven Rostedt77ae3652009-03-27 11:00:29 -04002312/*
2313 * rb_handle_head_page - writer hit the head page
2314 *
2315 * Returns: +1 to retry page
2316 * 0 to continue
2317 * -1 on error
2318 */
2319static int
2320rb_handle_head_page(struct ring_buffer_per_cpu *cpu_buffer,
2321 struct buffer_page *tail_page,
2322 struct buffer_page *next_page)
2323{
2324 struct buffer_page *new_head;
2325 int entries;
2326 int type;
2327 int ret;
2328
2329 entries = rb_page_entries(next_page);
2330
2331 /*
2332 * The hard part is here. We need to move the head
2333 * forward, and protect against both readers on
2334 * other CPUs and writers coming in via interrupts.
2335 */
2336 type = rb_head_page_set_update(cpu_buffer, next_page, tail_page,
2337 RB_PAGE_HEAD);
2338
2339 /*
2340 * type can be one of four:
2341 * NORMAL - an interrupt already moved it for us
2342 * HEAD - we are the first to get here.
2343 * UPDATE - we are the interrupt interrupting
2344 * a current move.
2345 * MOVED - a reader on another CPU moved the next
2346 * pointer to its reader page. Give up
2347 * and try again.
2348 */
2349
2350 switch (type) {
2351 case RB_PAGE_HEAD:
2352 /*
2353 * We changed the head to UPDATE, thus
2354 * it is our responsibility to update
2355 * the counters.
2356 */
2357 local_add(entries, &cpu_buffer->overrun);
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -07002358 local_sub(BUF_PAGE_SIZE, &cpu_buffer->entries_bytes);
Steven Rostedt77ae3652009-03-27 11:00:29 -04002359
2360 /*
2361 * The entries will be zeroed out when we move the
2362 * tail page.
2363 */
2364
2365 /* still more to do */
2366 break;
2367
2368 case RB_PAGE_UPDATE:
2369 /*
2370 * This is an interrupt that interrupt the
2371 * previous update. Still more to do.
2372 */
2373 break;
2374 case RB_PAGE_NORMAL:
2375 /*
2376 * An interrupt came in before the update
2377 * and processed this for us.
2378 * Nothing left to do.
2379 */
2380 return 1;
2381 case RB_PAGE_MOVED:
2382 /*
2383 * The reader is on another CPU and just did
2384 * a swap with our next_page.
2385 * Try again.
2386 */
2387 return 1;
2388 default:
2389 RB_WARN_ON(cpu_buffer, 1); /* WTF??? */
2390 return -1;
2391 }
2392
2393 /*
2394 * Now that we are here, the old head pointer is
2395 * set to UPDATE. This will keep the reader from
2396 * swapping the head page with the reader page.
2397 * The reader (on another CPU) will spin till
2398 * we are finished.
2399 *
2400 * We just need to protect against interrupts
2401 * doing the job. We will set the next pointer
2402 * to HEAD. After that, we set the old pointer
2403 * to NORMAL, but only if it was HEAD before.
2404 * otherwise we are an interrupt, and only
2405 * want the outer most commit to reset it.
2406 */
2407 new_head = next_page;
Qiujun Huang6689bed2020-12-25 22:03:56 +08002408 rb_inc_page(&new_head);
Steven Rostedt77ae3652009-03-27 11:00:29 -04002409
2410 ret = rb_head_page_set_head(cpu_buffer, new_head, next_page,
2411 RB_PAGE_NORMAL);
2412
2413 /*
2414 * Valid returns are:
2415 * HEAD - an interrupt came in and already set it.
2416 * NORMAL - One of two things:
2417 * 1) We really set it.
2418 * 2) A bunch of interrupts came in and moved
2419 * the page forward again.
2420 */
2421 switch (ret) {
2422 case RB_PAGE_HEAD:
2423 case RB_PAGE_NORMAL:
2424 /* OK */
2425 break;
2426 default:
2427 RB_WARN_ON(cpu_buffer, 1);
2428 return -1;
2429 }
2430
2431 /*
2432 * It is possible that an interrupt came in,
2433 * set the head up, then more interrupts came in
2434 * and moved it again. When we get back here,
2435 * the page would have been set to NORMAL but we
2436 * just set it back to HEAD.
2437 *
2438 * How do you detect this? Well, if that happened
2439 * the tail page would have moved.
2440 */
2441 if (ret == RB_PAGE_NORMAL) {
Steven Rostedt (Red Hat)85736362015-11-17 14:03:11 -05002442 struct buffer_page *buffer_tail_page;
2443
2444 buffer_tail_page = READ_ONCE(cpu_buffer->tail_page);
Steven Rostedt77ae3652009-03-27 11:00:29 -04002445 /*
2446 * If the tail had moved passed next, then we need
2447 * to reset the pointer.
2448 */
Steven Rostedt (Red Hat)85736362015-11-17 14:03:11 -05002449 if (buffer_tail_page != tail_page &&
2450 buffer_tail_page != next_page)
Steven Rostedt77ae3652009-03-27 11:00:29 -04002451 rb_head_page_set_normal(cpu_buffer, new_head,
2452 next_page,
2453 RB_PAGE_HEAD);
2454 }
2455
2456 /*
2457 * If this was the outer most commit (the one that
2458 * changed the original pointer from HEAD to UPDATE),
2459 * then it is up to us to reset it to NORMAL.
2460 */
2461 if (type == RB_PAGE_HEAD) {
2462 ret = rb_head_page_set_normal(cpu_buffer, next_page,
2463 tail_page,
2464 RB_PAGE_UPDATE);
2465 if (RB_WARN_ON(cpu_buffer,
2466 ret != RB_PAGE_UPDATE))
2467 return -1;
2468 }
2469
2470 return 0;
2471}
2472
Steven Rostedtc7b09302009-06-11 11:12:00 -04002473static inline void
2474rb_reset_tail(struct ring_buffer_per_cpu *cpu_buffer,
Steven Rostedt (Red Hat)fcc742e2015-05-28 17:13:14 -04002475 unsigned long tail, struct rb_event_info *info)
Steven Rostedtc7b09302009-06-11 11:12:00 -04002476{
Steven Rostedt (Red Hat)fcc742e2015-05-28 17:13:14 -04002477 struct buffer_page *tail_page = info->tail_page;
Steven Rostedtc7b09302009-06-11 11:12:00 -04002478 struct ring_buffer_event *event;
Steven Rostedt (Red Hat)fcc742e2015-05-28 17:13:14 -04002479 unsigned long length = info->length;
Steven Rostedtc7b09302009-06-11 11:12:00 -04002480
2481 /*
2482 * Only the event that crossed the page boundary
2483 * must fill the old tail_page with padding.
2484 */
2485 if (tail >= BUF_PAGE_SIZE) {
Steven Rostedtb3230c82010-05-21 11:55:21 -04002486 /*
2487 * If the page was filled, then we still need
2488 * to update the real_end. Reset it to zero
2489 * and the reader will ignore it.
2490 */
2491 if (tail == BUF_PAGE_SIZE)
2492 tail_page->real_end = 0;
2493
Steven Rostedtc7b09302009-06-11 11:12:00 -04002494 local_sub(length, &tail_page->write);
2495 return;
2496 }
2497
2498 event = __rb_page_index(tail_page, tail);
2499
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -07002500 /* account for padding bytes */
2501 local_add(BUF_PAGE_SIZE - tail, &cpu_buffer->entries_bytes);
2502
Steven Rostedtc7b09302009-06-11 11:12:00 -04002503 /*
Steven Rostedtff0ff842010-03-31 22:11:42 -04002504 * Save the original length to the meta data.
2505 * This will be used by the reader to add lost event
2506 * counter.
2507 */
2508 tail_page->real_end = tail;
2509
2510 /*
Steven Rostedtc7b09302009-06-11 11:12:00 -04002511 * If this event is bigger than the minimum size, then
2512 * we need to be careful that we don't subtract the
2513 * write counter enough to allow another writer to slip
2514 * in on this page.
2515 * We put in a discarded commit instead, to make sure
2516 * that this space is not used again.
2517 *
2518 * If we are less than the minimum size, we don't need to
2519 * worry about it.
2520 */
2521 if (tail > (BUF_PAGE_SIZE - RB_EVNT_MIN_SIZE)) {
2522 /* No room for any events */
2523
2524 /* Mark the rest of the page with padding */
2525 rb_event_set_padding(event);
2526
2527 /* Set the write back to the previous setting */
2528 local_sub(length, &tail_page->write);
2529 return;
2530 }
2531
2532 /* Put in a discarded event */
2533 event->array[0] = (BUF_PAGE_SIZE - tail) - RB_EVNT_HDR_SIZE;
2534 event->type_len = RINGBUF_TYPE_PADDING;
2535 /* time delta must be non zero */
2536 event->time_delta = 1;
Steven Rostedtc7b09302009-06-11 11:12:00 -04002537
2538 /* Set write to end of buffer */
2539 length = (tail + length) - BUF_PAGE_SIZE;
2540 local_sub(length, &tail_page->write);
2541}
Steven Rostedt6634ff22009-05-06 15:30:07 -04002542
Steven Rostedt (Red Hat)4239c382015-11-17 16:36:06 -05002543static inline void rb_end_commit(struct ring_buffer_per_cpu *cpu_buffer);
2544
Steven Rostedt747e94a2010-10-08 13:51:48 -04002545/*
2546 * This is the slow path, force gcc not to inline it.
2547 */
2548static noinline struct ring_buffer_event *
Steven Rostedt6634ff22009-05-06 15:30:07 -04002549rb_move_tail(struct ring_buffer_per_cpu *cpu_buffer,
Steven Rostedt (Red Hat)fcc742e2015-05-28 17:13:14 -04002550 unsigned long tail, struct rb_event_info *info)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002551{
Steven Rostedt (Red Hat)fcc742e2015-05-28 17:13:14 -04002552 struct buffer_page *tail_page = info->tail_page;
Steven Rostedt5a50e332009-11-17 08:43:01 -05002553 struct buffer_page *commit_page = cpu_buffer->commit_page;
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05002554 struct trace_buffer *buffer = cpu_buffer->buffer;
Steven Rostedt77ae3652009-03-27 11:00:29 -04002555 struct buffer_page *next_page;
2556 int ret;
Steven Rostedtaa20ae82009-05-05 21:16:11 -04002557
2558 next_page = tail_page;
2559
Qiujun Huang6689bed2020-12-25 22:03:56 +08002560 rb_inc_page(&next_page);
Steven Rostedtaa20ae82009-05-05 21:16:11 -04002561
Steven Rostedtaa20ae82009-05-05 21:16:11 -04002562 /*
2563 * If for some reason, we had an interrupt storm that made
2564 * it all the way around the buffer, bail, and warn
2565 * about it.
2566 */
2567 if (unlikely(next_page == commit_page)) {
Steven Rostedt77ae3652009-03-27 11:00:29 -04002568 local_inc(&cpu_buffer->commit_overrun);
Steven Rostedtaa20ae82009-05-05 21:16:11 -04002569 goto out_reset;
2570 }
2571
Steven Rostedt77ae3652009-03-27 11:00:29 -04002572 /*
2573 * This is where the fun begins!
2574 *
2575 * We are fighting against races between a reader that
2576 * could be on another CPU trying to swap its reader
2577 * page with the buffer head.
2578 *
2579 * We are also fighting against interrupts coming in and
2580 * moving the head or tail on us as well.
2581 *
2582 * If the next page is the head page then we have filled
2583 * the buffer, unless the commit page is still on the
2584 * reader page.
2585 */
Qiujun Huang6689bed2020-12-25 22:03:56 +08002586 if (rb_is_head_page(next_page, &tail_page->list)) {
Steven Rostedtaa20ae82009-05-05 21:16:11 -04002587
Steven Rostedt77ae3652009-03-27 11:00:29 -04002588 /*
2589 * If the commit is not on the reader page, then
2590 * move the header page.
2591 */
2592 if (!rb_is_reader_page(cpu_buffer->commit_page)) {
2593 /*
2594 * If we are not in overwrite mode,
2595 * this is easy, just stop here.
2596 */
Slava Pestov884bfe82011-07-15 14:23:58 -07002597 if (!(buffer->flags & RB_FL_OVERWRITE)) {
2598 local_inc(&cpu_buffer->dropped_events);
Steven Rostedt77ae3652009-03-27 11:00:29 -04002599 goto out_reset;
Slava Pestov884bfe82011-07-15 14:23:58 -07002600 }
Steven Rostedtaa20ae82009-05-05 21:16:11 -04002601
Steven Rostedt77ae3652009-03-27 11:00:29 -04002602 ret = rb_handle_head_page(cpu_buffer,
2603 tail_page,
2604 next_page);
2605 if (ret < 0)
2606 goto out_reset;
2607 if (ret)
2608 goto out_again;
2609 } else {
2610 /*
2611 * We need to be careful here too. The
2612 * commit page could still be on the reader
2613 * page. We could have a small buffer, and
2614 * have filled up the buffer with events
2615 * from interrupts and such, and wrapped.
2616 *
Qiujun Huangc6358ba2020-12-24 22:46:34 +08002617 * Note, if the tail page is also on the
Steven Rostedt77ae3652009-03-27 11:00:29 -04002618 * reader_page, we let it move out.
2619 */
2620 if (unlikely((cpu_buffer->commit_page !=
2621 cpu_buffer->tail_page) &&
2622 (cpu_buffer->commit_page ==
2623 cpu_buffer->reader_page))) {
2624 local_inc(&cpu_buffer->commit_overrun);
2625 goto out_reset;
2626 }
Steven Rostedtaa20ae82009-05-05 21:16:11 -04002627 }
2628 }
2629
Steven Rostedt (Red Hat)70004982015-11-17 15:15:19 -05002630 rb_tail_page_update(cpu_buffer, tail_page, next_page);
Steven Rostedtaa20ae82009-05-05 21:16:11 -04002631
Steven Rostedt77ae3652009-03-27 11:00:29 -04002632 out_again:
Steven Rostedtaa20ae82009-05-05 21:16:11 -04002633
Steven Rostedt (Red Hat)fcc742e2015-05-28 17:13:14 -04002634 rb_reset_tail(cpu_buffer, tail, info);
Steven Rostedtaa20ae82009-05-05 21:16:11 -04002635
Steven Rostedt (Red Hat)4239c382015-11-17 16:36:06 -05002636 /* Commit what we have for now. */
2637 rb_end_commit(cpu_buffer);
2638 /* rb_end_commit() decs committing */
2639 local_inc(&cpu_buffer->committing);
2640
Steven Rostedtaa20ae82009-05-05 21:16:11 -04002641 /* fail and let the caller try again */
2642 return ERR_PTR(-EAGAIN);
2643
Steven Rostedt45141d42009-02-12 13:19:48 -05002644 out_reset:
Lai Jiangshan6f3b3442009-01-12 11:06:18 +08002645 /* reset write */
Steven Rostedt (Red Hat)fcc742e2015-05-28 17:13:14 -04002646 rb_reset_tail(cpu_buffer, tail, info);
Lai Jiangshan6f3b3442009-01-12 11:06:18 +08002647
Steven Rostedtbf41a152008-10-04 02:00:59 -04002648 return NULL;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002649}
2650
Steven Rostedt (VMware)74e87932020-06-30 12:47:56 -04002651/* Slow path */
2652static struct ring_buffer_event *
Tom Zanussidc4e2802018-01-15 20:51:40 -06002653rb_add_time_stamp(struct ring_buffer_event *event, u64 delta, bool abs)
Steven Rostedt (Red Hat)d90fd772015-05-29 12:12:27 -04002654{
Tom Zanussidc4e2802018-01-15 20:51:40 -06002655 if (abs)
2656 event->type_len = RINGBUF_TYPE_TIME_STAMP;
2657 else
2658 event->type_len = RINGBUF_TYPE_TIME_EXTEND;
Steven Rostedt (Red Hat)d90fd772015-05-29 12:12:27 -04002659
Tom Zanussidc4e2802018-01-15 20:51:40 -06002660 /* Not the first event on the page, or not delta? */
2661 if (abs || rb_event_index(event)) {
Steven Rostedt (Red Hat)d90fd772015-05-29 12:12:27 -04002662 event->time_delta = delta & TS_MASK;
2663 event->array[0] = delta >> TS_SHIFT;
2664 } else {
2665 /* nope, just zero it */
2666 event->time_delta = 0;
2667 event->array[0] = 0;
2668 }
2669
2670 return skip_time_extend(event);
2671}
2672
Steven Rostedt (VMware)58fbc3c2020-06-30 08:59:26 -04002673#ifndef CONFIG_HAVE_UNSTABLE_SCHED_CLOCK
2674static inline bool sched_clock_stable(void)
2675{
2676 return true;
2677}
2678#endif
2679
Steven Rostedt (VMware)74e87932020-06-30 12:47:56 -04002680static void
Steven Rostedt (VMware)58fbc3c2020-06-30 08:59:26 -04002681rb_check_timestamp(struct ring_buffer_per_cpu *cpu_buffer,
2682 struct rb_event_info *info)
2683{
2684 u64 write_stamp;
2685
Steven Rostedt (VMware)29ce2452020-07-01 13:10:19 -04002686 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 -04002687 (unsigned long long)info->delta,
2688 (unsigned long long)info->ts,
2689 (unsigned long long)info->before,
2690 (unsigned long long)info->after,
2691 (unsigned long long)(rb_time_read(&cpu_buffer->write_stamp, &write_stamp) ? write_stamp : 0),
2692 sched_clock_stable() ? "" :
2693 "If you just came from a suspend/resume,\n"
2694 "please switch to the trace global clock:\n"
2695 " echo global > /sys/kernel/debug/tracing/trace_clock\n"
2696 "or add trace_clock=global to the kernel command line\n");
2697}
2698
Steven Rostedt (VMware)74e87932020-06-30 12:47:56 -04002699static void rb_add_timestamp(struct ring_buffer_per_cpu *cpu_buffer,
2700 struct ring_buffer_event **event,
2701 struct rb_event_info *info,
2702 u64 *delta,
2703 unsigned int *length)
2704{
2705 bool abs = info->add_timestamp &
2706 (RB_ADD_STAMP_FORCE | RB_ADD_STAMP_ABSOLUTE);
2707
Steven Rostedt (VMware)29ce2452020-07-01 13:10:19 -04002708 if (unlikely(info->delta > (1ULL << 59))) {
2709 /* did the clock go backwards */
2710 if (info->before == info->after && info->before > info->ts) {
2711 /* not interrupted */
2712 static int once;
2713
2714 /*
2715 * This is possible with a recalibrating of the TSC.
2716 * Do not produce a call stack, but just report it.
2717 */
2718 if (!once) {
2719 once++;
2720 pr_warn("Ring buffer clock went backwards: %llu -> %llu\n",
2721 info->before, info->ts);
2722 }
2723 } else
2724 rb_check_timestamp(cpu_buffer, info);
2725 if (!abs)
2726 info->delta = 0;
2727 }
Steven Rostedt (VMware)74e87932020-06-30 12:47:56 -04002728 *event = rb_add_time_stamp(*event, info->delta, abs);
2729 *length -= RB_LEN_TIME_EXTEND;
2730 *delta = 0;
2731}
2732
Steven Rostedt (Red Hat)d90fd772015-05-29 12:12:27 -04002733/**
2734 * rb_update_event - update event type and data
Steven Rostedt (VMware)cfc585a2020-01-14 16:27:51 -05002735 * @cpu_buffer: The per cpu buffer of the @event
Steven Rostedt (Red Hat)d90fd772015-05-29 12:12:27 -04002736 * @event: the event to update
Steven Rostedt (VMware)cfc585a2020-01-14 16:27:51 -05002737 * @info: The info to update the @event with (contains length and delta)
Steven Rostedt (Red Hat)d90fd772015-05-29 12:12:27 -04002738 *
Steven Rostedt (VMware)cfc585a2020-01-14 16:27:51 -05002739 * Update the type and data fields of the @event. The length
Steven Rostedt (Red Hat)d90fd772015-05-29 12:12:27 -04002740 * is the actual size that is written to the ring buffer,
2741 * and with this, we can determine what to place into the
2742 * data field.
2743 */
Steven Rostedt (Red Hat)b7dc42f2015-09-03 08:57:12 -04002744static void
Steven Rostedt (Red Hat)d90fd772015-05-29 12:12:27 -04002745rb_update_event(struct ring_buffer_per_cpu *cpu_buffer,
2746 struct ring_buffer_event *event,
2747 struct rb_event_info *info)
2748{
2749 unsigned length = info->length;
2750 u64 delta = info->delta;
Steven Rostedt (VMware)8672e4942021-03-16 12:41:02 -04002751 unsigned int nest = local_read(&cpu_buffer->committing) - 1;
2752
2753 if (nest < MAX_NEST)
2754 cpu_buffer->event_stamp[nest] = info->ts;
Steven Rostedt (Red Hat)d90fd772015-05-29 12:12:27 -04002755
2756 /*
2757 * If we need to add a timestamp, then we
Steven Rostedt (VMware)6167c202018-05-16 11:17:06 -04002758 * add it to the start of the reserved space.
Steven Rostedt (Red Hat)d90fd772015-05-29 12:12:27 -04002759 */
Steven Rostedt (VMware)74e87932020-06-30 12:47:56 -04002760 if (unlikely(info->add_timestamp))
2761 rb_add_timestamp(cpu_buffer, &event, info, &delta, &length);
Steven Rostedt (Red Hat)d90fd772015-05-29 12:12:27 -04002762
2763 event->time_delta = delta;
2764 length -= RB_EVNT_HDR_SIZE;
Steven Rostedt (VMware)adab66b2020-12-14 12:33:51 -05002765 if (length > RB_MAX_SMALL_DATA || RB_FORCE_8BYTE_ALIGNMENT) {
Steven Rostedt (Red Hat)d90fd772015-05-29 12:12:27 -04002766 event->type_len = 0;
2767 event->array[0] = length;
2768 } else
2769 event->type_len = DIV_ROUND_UP(length, RB_ALIGNMENT);
2770}
2771
2772static unsigned rb_calculate_event_length(unsigned length)
2773{
2774 struct ring_buffer_event event; /* Used only for sizeof array */
2775
2776 /* zero length can cause confusions */
2777 if (!length)
2778 length++;
2779
Steven Rostedt (VMware)adab66b2020-12-14 12:33:51 -05002780 if (length > RB_MAX_SMALL_DATA || RB_FORCE_8BYTE_ALIGNMENT)
Steven Rostedt (Red Hat)d90fd772015-05-29 12:12:27 -04002781 length += sizeof(event.array[0]);
2782
2783 length += RB_EVNT_HDR_SIZE;
Steven Rostedt (VMware)adab66b2020-12-14 12:33:51 -05002784 length = ALIGN(length, RB_ARCH_ALIGNMENT);
Steven Rostedt (Red Hat)d90fd772015-05-29 12:12:27 -04002785
2786 /*
2787 * In case the time delta is larger than the 27 bits for it
2788 * in the header, we need to add a timestamp. If another
2789 * event comes in when trying to discard this one to increase
2790 * the length, then the timestamp will be added in the allocated
2791 * space of this event. If length is bigger than the size needed
2792 * for the TIME_EXTEND, then padding has to be used. The events
2793 * length must be either RB_LEN_TIME_EXTEND, or greater than or equal
2794 * to RB_LEN_TIME_EXTEND + 8, as 8 is the minimum size for padding.
2795 * As length is a multiple of 4, we only need to worry if it
2796 * is 12 (RB_LEN_TIME_EXTEND + 4).
2797 */
2798 if (length == RB_LEN_TIME_EXTEND + RB_ALIGNMENT)
2799 length += RB_ALIGNMENT;
2800
2801 return length;
2802}
2803
Steven Rostedt (VMware)a389d862020-06-28 22:52:25 -04002804static u64 rb_time_delta(struct ring_buffer_event *event)
2805{
2806 switch (event->type_len) {
2807 case RINGBUF_TYPE_PADDING:
2808 return 0;
2809
2810 case RINGBUF_TYPE_TIME_EXTEND:
Steven Rostedt (VMware)e20044f2021-03-16 12:41:01 -04002811 return rb_event_time_stamp(event);
Steven Rostedt (VMware)a389d862020-06-28 22:52:25 -04002812
2813 case RINGBUF_TYPE_TIME_STAMP:
2814 return 0;
2815
2816 case RINGBUF_TYPE_DATA:
2817 return event->time_delta;
2818 default:
2819 return 0;
2820 }
2821}
Steven Rostedt (Red Hat)9826b272015-05-28 17:36:45 -04002822
Steven Rostedt (Red Hat)a4543a22015-05-29 09:40:18 -04002823static inline int
2824rb_try_to_discard(struct ring_buffer_per_cpu *cpu_buffer,
Steven Rostedt (Red Hat)d90fd772015-05-29 12:12:27 -04002825 struct ring_buffer_event *event)
2826{
2827 unsigned long new_index, old_index;
2828 struct buffer_page *bpage;
2829 unsigned long index;
2830 unsigned long addr;
Steven Rostedt (VMware)a389d862020-06-28 22:52:25 -04002831 u64 write_stamp;
2832 u64 delta;
Steven Rostedt (Red Hat)d90fd772015-05-29 12:12:27 -04002833
2834 new_index = rb_event_index(event);
2835 old_index = new_index + rb_event_ts_length(event);
2836 addr = (unsigned long)event;
2837 addr &= PAGE_MASK;
2838
Steven Rostedt (Red Hat)85736362015-11-17 14:03:11 -05002839 bpage = READ_ONCE(cpu_buffer->tail_page);
Steven Rostedt (Red Hat)d90fd772015-05-29 12:12:27 -04002840
Steven Rostedt (VMware)a389d862020-06-28 22:52:25 -04002841 delta = rb_time_delta(event);
2842
Steven Rostedt (VMware)10464b42020-06-28 22:52:27 -04002843 if (!rb_time_read(&cpu_buffer->write_stamp, &write_stamp))
2844 return 0;
Steven Rostedt (VMware)a389d862020-06-28 22:52:25 -04002845
2846 /* Make sure the write stamp is read before testing the location */
2847 barrier();
2848
Steven Rostedt (Red Hat)d90fd772015-05-29 12:12:27 -04002849 if (bpage->page == (void *)addr && rb_page_write(bpage) == old_index) {
2850 unsigned long write_mask =
2851 local_read(&bpage->write) & ~RB_WRITE_MASK;
2852 unsigned long event_length = rb_event_length(event);
Steven Rostedt (VMware)a389d862020-06-28 22:52:25 -04002853
Steven Rostedt (VMware)a389d862020-06-28 22:52:25 -04002854 /* Something came in, can't discard */
Steven Rostedt (VMware)10464b42020-06-28 22:52:27 -04002855 if (!rb_time_cmpxchg(&cpu_buffer->write_stamp,
2856 write_stamp, write_stamp - delta))
Steven Rostedt (VMware)a389d862020-06-28 22:52:25 -04002857 return 0;
2858
2859 /*
Steven Rostedt (VMware)6f6be602021-03-03 18:03:52 -05002860 * It's possible that the event time delta is zero
2861 * (has the same time stamp as the previous event)
2862 * in which case write_stamp and before_stamp could
2863 * be the same. In such a case, force before_stamp
2864 * to be different than write_stamp. It doesn't
2865 * matter what it is, as long as its different.
2866 */
2867 if (!delta)
2868 rb_time_set(&cpu_buffer->before_stamp, 0);
2869
2870 /*
Steven Rostedt (VMware)a389d862020-06-28 22:52:25 -04002871 * If an event were to come in now, it would see that the
2872 * write_stamp and the before_stamp are different, and assume
2873 * that this event just added itself before updating
2874 * the write stamp. The interrupting event will fix the
2875 * write stamp for us, and use the before stamp as its delta.
2876 */
2877
Steven Rostedt (Red Hat)d90fd772015-05-29 12:12:27 -04002878 /*
2879 * This is on the tail page. It is possible that
2880 * a write could come in and move the tail page
2881 * and write to the next page. That is fine
2882 * because we just shorten what is on this page.
2883 */
2884 old_index += write_mask;
2885 new_index += write_mask;
2886 index = local_cmpxchg(&bpage->write, old_index, new_index);
2887 if (index == old_index) {
2888 /* update counters */
2889 local_sub(event_length, &cpu_buffer->entries_bytes);
2890 return 1;
2891 }
2892 }
2893
2894 /* could not discard */
2895 return 0;
2896}
2897
2898static void rb_start_commit(struct ring_buffer_per_cpu *cpu_buffer)
2899{
2900 local_inc(&cpu_buffer->committing);
2901 local_inc(&cpu_buffer->commits);
2902}
2903
Steven Rostedt (Red Hat)38e11df2016-11-23 20:42:31 -05002904static __always_inline void
Steven Rostedt (Red Hat)d90fd772015-05-29 12:12:27 -04002905rb_set_commit_to_write(struct ring_buffer_per_cpu *cpu_buffer)
2906{
2907 unsigned long max_count;
2908
2909 /*
2910 * We only race with interrupts and NMIs on this CPU.
2911 * If we own the commit event, then we can commit
2912 * all others that interrupted us, since the interruptions
2913 * are in stack format (they finish before they come
2914 * back to us). This allows us to do a simple loop to
2915 * assign the commit to the tail.
2916 */
2917 again:
2918 max_count = cpu_buffer->nr_pages * 100;
2919
Steven Rostedt (Red Hat)85736362015-11-17 14:03:11 -05002920 while (cpu_buffer->commit_page != READ_ONCE(cpu_buffer->tail_page)) {
Steven Rostedt (Red Hat)d90fd772015-05-29 12:12:27 -04002921 if (RB_WARN_ON(cpu_buffer, !(--max_count)))
2922 return;
2923 if (RB_WARN_ON(cpu_buffer,
2924 rb_is_reader_page(cpu_buffer->tail_page)))
2925 return;
2926 local_set(&cpu_buffer->commit_page->page->commit,
2927 rb_page_write(cpu_buffer->commit_page));
Qiujun Huang6689bed2020-12-25 22:03:56 +08002928 rb_inc_page(&cpu_buffer->commit_page);
Steven Rostedt (Red Hat)d90fd772015-05-29 12:12:27 -04002929 /* add barrier to keep gcc from optimizing too much */
2930 barrier();
2931 }
2932 while (rb_commit_index(cpu_buffer) !=
2933 rb_page_write(cpu_buffer->commit_page)) {
2934
2935 local_set(&cpu_buffer->commit_page->page->commit,
2936 rb_page_write(cpu_buffer->commit_page));
2937 RB_WARN_ON(cpu_buffer,
2938 local_read(&cpu_buffer->commit_page->page->commit) &
2939 ~RB_WRITE_MASK);
2940 barrier();
2941 }
2942
2943 /* again, keep gcc from optimizing */
2944 barrier();
2945
2946 /*
2947 * If an interrupt came in just after the first while loop
2948 * and pushed the tail page forward, we will be left with
2949 * a dangling commit that will never go forward.
2950 */
Steven Rostedt (Red Hat)85736362015-11-17 14:03:11 -05002951 if (unlikely(cpu_buffer->commit_page != READ_ONCE(cpu_buffer->tail_page)))
Steven Rostedt (Red Hat)d90fd772015-05-29 12:12:27 -04002952 goto again;
2953}
2954
Steven Rostedt (Red Hat)38e11df2016-11-23 20:42:31 -05002955static __always_inline void rb_end_commit(struct ring_buffer_per_cpu *cpu_buffer)
Steven Rostedt (Red Hat)d90fd772015-05-29 12:12:27 -04002956{
2957 unsigned long commits;
2958
2959 if (RB_WARN_ON(cpu_buffer,
2960 !local_read(&cpu_buffer->committing)))
2961 return;
2962
2963 again:
2964 commits = local_read(&cpu_buffer->commits);
2965 /* synchronize with interrupts */
2966 barrier();
2967 if (local_read(&cpu_buffer->committing) == 1)
2968 rb_set_commit_to_write(cpu_buffer);
2969
2970 local_dec(&cpu_buffer->committing);
2971
2972 /* synchronize with interrupts */
2973 barrier();
2974
2975 /*
2976 * Need to account for interrupts coming in between the
2977 * updating of the commit page and the clearing of the
2978 * committing counter.
2979 */
2980 if (unlikely(local_read(&cpu_buffer->commits) != commits) &&
2981 !local_read(&cpu_buffer->committing)) {
2982 local_inc(&cpu_buffer->committing);
2983 goto again;
2984 }
2985}
2986
2987static inline void rb_event_discard(struct ring_buffer_event *event)
2988{
Tom Zanussidc4e2802018-01-15 20:51:40 -06002989 if (extended_time(event))
Steven Rostedt (Red Hat)d90fd772015-05-29 12:12:27 -04002990 event = skip_time_extend(event);
2991
2992 /* array[0] holds the actual length for the discarded event */
2993 event->array[0] = rb_event_data_length(event) - RB_EVNT_HDR_SIZE;
2994 event->type_len = RINGBUF_TYPE_PADDING;
2995 /* time delta must be non zero */
2996 if (!event->time_delta)
2997 event->time_delta = 1;
2998}
2999
Steven Rostedt (Red Hat)d90fd772015-05-29 12:12:27 -04003000static void rb_commit(struct ring_buffer_per_cpu *cpu_buffer,
3001 struct ring_buffer_event *event)
3002{
3003 local_inc(&cpu_buffer->entries);
Steven Rostedt (Red Hat)d90fd772015-05-29 12:12:27 -04003004 rb_end_commit(cpu_buffer);
3005}
3006
3007static __always_inline void
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05003008rb_wakeups(struct trace_buffer *buffer, struct ring_buffer_per_cpu *cpu_buffer)
Steven Rostedt (Red Hat)d90fd772015-05-29 12:12:27 -04003009{
Steven Rostedt (VMware)03329f92018-11-29 21:38:42 -05003010 size_t nr_pages;
3011 size_t dirty;
3012 size_t full;
Steven Rostedt (Red Hat)d90fd772015-05-29 12:12:27 -04003013
3014 if (buffer->irq_work.waiters_pending) {
3015 buffer->irq_work.waiters_pending = false;
3016 /* irq_work_queue() supplies it's own memory barriers */
3017 irq_work_queue(&buffer->irq_work.work);
3018 }
3019
3020 if (cpu_buffer->irq_work.waiters_pending) {
3021 cpu_buffer->irq_work.waiters_pending = false;
3022 /* irq_work_queue() supplies it's own memory barriers */
3023 irq_work_queue(&cpu_buffer->irq_work.work);
3024 }
3025
Steven Rostedt (VMware)03329f92018-11-29 21:38:42 -05003026 if (cpu_buffer->last_pages_touch == local_read(&cpu_buffer->pages_touched))
3027 return;
Steven Rostedt (Red Hat)d90fd772015-05-29 12:12:27 -04003028
Steven Rostedt (VMware)03329f92018-11-29 21:38:42 -05003029 if (cpu_buffer->reader_page == cpu_buffer->commit_page)
3030 return;
Steven Rostedt (VMware)2c2b0a72018-11-29 20:32:26 -05003031
Steven Rostedt (VMware)03329f92018-11-29 21:38:42 -05003032 if (!cpu_buffer->irq_work.full_waiters_pending)
3033 return;
Steven Rostedt (VMware)2c2b0a72018-11-29 20:32:26 -05003034
Steven Rostedt (VMware)03329f92018-11-29 21:38:42 -05003035 cpu_buffer->last_pages_touch = local_read(&cpu_buffer->pages_touched);
3036
3037 full = cpu_buffer->shortest_full;
3038 nr_pages = cpu_buffer->nr_pages;
3039 dirty = ring_buffer_nr_dirty_pages(buffer, cpu_buffer->cpu);
3040 if (full && nr_pages && (dirty * 100) <= full * nr_pages)
3041 return;
3042
3043 cpu_buffer->irq_work.wakeup_full = true;
3044 cpu_buffer->irq_work.full_waiters_pending = false;
3045 /* irq_work_queue() supplies it's own memory barriers */
3046 irq_work_queue(&cpu_buffer->irq_work.work);
Steven Rostedt (Red Hat)d90fd772015-05-29 12:12:27 -04003047}
3048
Steven Rostedt (VMware)28575c62020-11-02 14:43:10 -05003049#ifdef CONFIG_RING_BUFFER_RECORD_RECURSION
3050# define do_ring_buffer_record_recursion() \
3051 do_ftrace_record_recursion(_THIS_IP_, _RET_IP_)
3052#else
3053# define do_ring_buffer_record_recursion() do { } while (0)
3054#endif
3055
Steven Rostedt (Red Hat)d90fd772015-05-29 12:12:27 -04003056/*
3057 * The lock and unlock are done within a preempt disable section.
3058 * The current_context per_cpu variable can only be modified
3059 * by the current task between lock and unlock. But it can
Steven Rostedt (VMware)a0e3a182018-01-15 10:47:09 -05003060 * be modified more than once via an interrupt. To pass this
3061 * information from the lock to the unlock without having to
3062 * access the 'in_interrupt()' functions again (which do show
3063 * a bit of overhead in something as critical as function tracing,
3064 * we use a bitmask trick.
Steven Rostedt (Red Hat)d90fd772015-05-29 12:12:27 -04003065 *
Steven Rostedt (VMware)b02414c2020-11-02 15:31:27 -05003066 * bit 1 = NMI context
3067 * bit 2 = IRQ context
3068 * bit 3 = SoftIRQ context
3069 * bit 4 = normal context.
Steven Rostedt (Red Hat)d90fd772015-05-29 12:12:27 -04003070 *
Steven Rostedt (VMware)a0e3a182018-01-15 10:47:09 -05003071 * This works because this is the order of contexts that can
3072 * preempt other contexts. A SoftIRQ never preempts an IRQ
3073 * context.
3074 *
3075 * When the context is determined, the corresponding bit is
3076 * checked and set (if it was set, then a recursion of that context
3077 * happened).
3078 *
3079 * On unlock, we need to clear this bit. To do so, just subtract
3080 * 1 from the current_context and AND it to itself.
3081 *
3082 * (binary)
3083 * 101 - 1 = 100
3084 * 101 & 100 = 100 (clearing bit zero)
3085 *
3086 * 1010 - 1 = 1001
3087 * 1010 & 1001 = 1000 (clearing bit 1)
3088 *
3089 * The least significant bit can be cleared this way, and it
3090 * just so happens that it is the same bit corresponding to
3091 * the current context.
Steven Rostedt (VMware)b02414c2020-11-02 15:31:27 -05003092 *
3093 * Now the TRANSITION bit breaks the above slightly. The TRANSITION bit
3094 * is set when a recursion is detected at the current context, and if
3095 * the TRANSITION bit is already set, it will fail the recursion.
3096 * This is needed because there's a lag between the changing of
3097 * interrupt context and updating the preempt count. In this case,
3098 * a false positive will be found. To handle this, one extra recursion
3099 * is allowed, and this is done by the TRANSITION bit. If the TRANSITION
3100 * bit is already set, then it is considered a recursion and the function
3101 * ends. Otherwise, the TRANSITION bit is set, and that bit is returned.
3102 *
3103 * On the trace_recursive_unlock(), the TRANSITION bit will be the first
3104 * to be cleared. Even if it wasn't the context that set it. That is,
3105 * if an interrupt comes in while NORMAL bit is set and the ring buffer
3106 * is called before preempt_count() is updated, since the check will
3107 * be on the NORMAL bit, the TRANSITION bit will then be set. If an
3108 * NMI then comes in, it will set the NMI bit, but when the NMI code
3109 * does the trace_recursive_unlock() it will clear the TRANSTION bit
3110 * and leave the NMI bit set. But this is fine, because the interrupt
3111 * code that set the TRANSITION bit will then clear the NMI bit when it
3112 * calls trace_recursive_unlock(). If another NMI comes in, it will
3113 * set the TRANSITION bit and continue.
3114 *
3115 * Note: The TRANSITION bit only handles a single transition between context.
Steven Rostedt (Red Hat)d90fd772015-05-29 12:12:27 -04003116 */
3117
3118static __always_inline int
3119trace_recursive_lock(struct ring_buffer_per_cpu *cpu_buffer)
3120{
Steven Rostedt (VMware)a0e3a182018-01-15 10:47:09 -05003121 unsigned int val = cpu_buffer->current_context;
3122 unsigned long pc = preempt_count();
3123 int bit;
3124
3125 if (!(pc & (NMI_MASK | HARDIRQ_MASK | SOFTIRQ_OFFSET)))
3126 bit = RB_CTX_NORMAL;
3127 else
3128 bit = pc & NMI_MASK ? RB_CTX_NMI :
Steven Rostedt (VMware)0164e0d2018-01-18 15:42:09 -05003129 pc & HARDIRQ_MASK ? RB_CTX_IRQ : RB_CTX_SOFTIRQ;
Steven Rostedt (VMware)a0e3a182018-01-15 10:47:09 -05003130
Steven Rostedt (VMware)b02414c2020-11-02 15:31:27 -05003131 if (unlikely(val & (1 << (bit + cpu_buffer->nest)))) {
3132 /*
3133 * It is possible that this was called by transitioning
3134 * between interrupt context, and preempt_count() has not
3135 * been updated yet. In this case, use the TRANSITION bit.
3136 */
3137 bit = RB_CTX_TRANSITION;
Steven Rostedt (VMware)28575c62020-11-02 14:43:10 -05003138 if (val & (1 << (bit + cpu_buffer->nest))) {
3139 do_ring_buffer_record_recursion();
Steven Rostedt (VMware)b02414c2020-11-02 15:31:27 -05003140 return 1;
Steven Rostedt (VMware)28575c62020-11-02 14:43:10 -05003141 }
Steven Rostedt (VMware)b02414c2020-11-02 15:31:27 -05003142 }
Steven Rostedt (Red Hat)d90fd772015-05-29 12:12:27 -04003143
Steven Rostedt (VMware)8e012062018-02-07 17:26:32 -05003144 val |= (1 << (bit + cpu_buffer->nest));
Steven Rostedt (VMware)a0e3a182018-01-15 10:47:09 -05003145 cpu_buffer->current_context = val;
Steven Rostedt (Red Hat)d90fd772015-05-29 12:12:27 -04003146
3147 return 0;
3148}
3149
3150static __always_inline void
3151trace_recursive_unlock(struct ring_buffer_per_cpu *cpu_buffer)
3152{
Steven Rostedt (VMware)8e012062018-02-07 17:26:32 -05003153 cpu_buffer->current_context &=
3154 cpu_buffer->current_context - (1 << cpu_buffer->nest);
3155}
3156
Steven Rostedt (VMware)b02414c2020-11-02 15:31:27 -05003157/* The recursive locking above uses 5 bits */
3158#define NESTED_BITS 5
Steven Rostedt (VMware)8e012062018-02-07 17:26:32 -05003159
3160/**
3161 * ring_buffer_nest_start - Allow to trace while nested
3162 * @buffer: The ring buffer to modify
3163 *
Steven Rostedt (VMware)6167c202018-05-16 11:17:06 -04003164 * The ring buffer has a safety mechanism to prevent recursion.
Steven Rostedt (VMware)8e012062018-02-07 17:26:32 -05003165 * But there may be a case where a trace needs to be done while
3166 * tracing something else. In this case, calling this function
3167 * will allow this function to nest within a currently active
3168 * ring_buffer_lock_reserve().
3169 *
3170 * Call this function before calling another ring_buffer_lock_reserve() and
3171 * call ring_buffer_nest_end() after the nested ring_buffer_unlock_commit().
3172 */
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05003173void ring_buffer_nest_start(struct trace_buffer *buffer)
Steven Rostedt (VMware)8e012062018-02-07 17:26:32 -05003174{
3175 struct ring_buffer_per_cpu *cpu_buffer;
3176 int cpu;
3177
3178 /* Enabled by ring_buffer_nest_end() */
3179 preempt_disable_notrace();
3180 cpu = raw_smp_processor_id();
3181 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt (VMware)6167c202018-05-16 11:17:06 -04003182 /* This is the shift value for the above recursive locking */
Steven Rostedt (VMware)8e012062018-02-07 17:26:32 -05003183 cpu_buffer->nest += NESTED_BITS;
3184}
3185
3186/**
3187 * ring_buffer_nest_end - Allow to trace while nested
3188 * @buffer: The ring buffer to modify
3189 *
3190 * Must be called after ring_buffer_nest_start() and after the
3191 * ring_buffer_unlock_commit().
3192 */
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05003193void ring_buffer_nest_end(struct trace_buffer *buffer)
Steven Rostedt (VMware)8e012062018-02-07 17:26:32 -05003194{
3195 struct ring_buffer_per_cpu *cpu_buffer;
3196 int cpu;
3197
3198 /* disabled by ring_buffer_nest_start() */
3199 cpu = raw_smp_processor_id();
3200 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt (VMware)6167c202018-05-16 11:17:06 -04003201 /* This is the shift value for the above recursive locking */
Steven Rostedt (VMware)8e012062018-02-07 17:26:32 -05003202 cpu_buffer->nest -= NESTED_BITS;
3203 preempt_enable_notrace();
Steven Rostedt (Red Hat)d90fd772015-05-29 12:12:27 -04003204}
3205
3206/**
3207 * ring_buffer_unlock_commit - commit a reserved
3208 * @buffer: The buffer to commit to
3209 * @event: The event pointer to commit.
3210 *
3211 * This commits the data to the ring buffer, and releases any locks held.
3212 *
3213 * Must be paired with ring_buffer_lock_reserve.
3214 */
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05003215int ring_buffer_unlock_commit(struct trace_buffer *buffer,
Steven Rostedt (Red Hat)d90fd772015-05-29 12:12:27 -04003216 struct ring_buffer_event *event)
3217{
3218 struct ring_buffer_per_cpu *cpu_buffer;
3219 int cpu = raw_smp_processor_id();
3220
3221 cpu_buffer = buffer->buffers[cpu];
3222
3223 rb_commit(cpu_buffer, event);
3224
3225 rb_wakeups(buffer, cpu_buffer);
3226
3227 trace_recursive_unlock(cpu_buffer);
3228
3229 preempt_enable_notrace();
3230
3231 return 0;
3232}
3233EXPORT_SYMBOL_GPL(ring_buffer_unlock_commit);
Steven Rostedt (Red Hat)a4543a22015-05-29 09:40:18 -04003234
Steven Rostedt (VMware)5b7be9c2020-11-30 23:37:33 -05003235/* Special value to validate all deltas on a page. */
3236#define CHECK_FULL_PAGE 1L
3237
3238#ifdef CONFIG_RING_BUFFER_VALIDATE_TIME_DELTAS
3239static void dump_buffer_page(struct buffer_data_page *bpage,
3240 struct rb_event_info *info,
3241 unsigned long tail)
3242{
3243 struct ring_buffer_event *event;
3244 u64 ts, delta;
3245 int e;
3246
3247 ts = bpage->time_stamp;
3248 pr_warn(" [%lld] PAGE TIME STAMP\n", ts);
3249
3250 for (e = 0; e < tail; e += rb_event_length(event)) {
3251
3252 event = (struct ring_buffer_event *)(bpage->data + e);
3253
3254 switch (event->type_len) {
3255
3256 case RINGBUF_TYPE_TIME_EXTEND:
Steven Rostedt (VMware)e20044f2021-03-16 12:41:01 -04003257 delta = rb_event_time_stamp(event);
Steven Rostedt (VMware)5b7be9c2020-11-30 23:37:33 -05003258 ts += delta;
3259 pr_warn(" [%lld] delta:%lld TIME EXTEND\n", ts, delta);
3260 break;
3261
3262 case RINGBUF_TYPE_TIME_STAMP:
Steven Rostedt (VMware)e20044f2021-03-16 12:41:01 -04003263 delta = rb_event_time_stamp(event);
Steven Rostedt (VMware)5b7be9c2020-11-30 23:37:33 -05003264 ts = delta;
3265 pr_warn(" [%lld] absolute:%lld TIME STAMP\n", ts, delta);
3266 break;
3267
3268 case RINGBUF_TYPE_PADDING:
3269 ts += event->time_delta;
3270 pr_warn(" [%lld] delta:%d PADDING\n", ts, event->time_delta);
3271 break;
3272
3273 case RINGBUF_TYPE_DATA:
3274 ts += event->time_delta;
3275 pr_warn(" [%lld] delta:%d\n", ts, event->time_delta);
3276 break;
3277
3278 default:
3279 break;
3280 }
3281 }
3282}
3283
3284static DEFINE_PER_CPU(atomic_t, checking);
3285static atomic_t ts_dump;
3286
3287/*
3288 * Check if the current event time stamp matches the deltas on
3289 * the buffer page.
3290 */
3291static void check_buffer(struct ring_buffer_per_cpu *cpu_buffer,
3292 struct rb_event_info *info,
3293 unsigned long tail)
3294{
3295 struct ring_buffer_event *event;
3296 struct buffer_data_page *bpage;
3297 u64 ts, delta;
3298 bool full = false;
3299 int e;
3300
3301 bpage = info->tail_page->page;
3302
3303 if (tail == CHECK_FULL_PAGE) {
3304 full = true;
3305 tail = local_read(&bpage->commit);
3306 } else if (info->add_timestamp &
3307 (RB_ADD_STAMP_FORCE | RB_ADD_STAMP_ABSOLUTE)) {
3308 /* Ignore events with absolute time stamps */
3309 return;
3310 }
3311
3312 /*
3313 * Do not check the first event (skip possible extends too).
3314 * Also do not check if previous events have not been committed.
3315 */
3316 if (tail <= 8 || tail > local_read(&bpage->commit))
3317 return;
3318
3319 /*
3320 * If this interrupted another event,
3321 */
3322 if (atomic_inc_return(this_cpu_ptr(&checking)) != 1)
3323 goto out;
3324
3325 ts = bpage->time_stamp;
3326
3327 for (e = 0; e < tail; e += rb_event_length(event)) {
3328
3329 event = (struct ring_buffer_event *)(bpage->data + e);
3330
3331 switch (event->type_len) {
3332
3333 case RINGBUF_TYPE_TIME_EXTEND:
Steven Rostedt (VMware)e20044f2021-03-16 12:41:01 -04003334 delta = rb_event_time_stamp(event);
Steven Rostedt (VMware)5b7be9c2020-11-30 23:37:33 -05003335 ts += delta;
3336 break;
3337
3338 case RINGBUF_TYPE_TIME_STAMP:
Steven Rostedt (VMware)e20044f2021-03-16 12:41:01 -04003339 delta = rb_event_time_stamp(event);
Steven Rostedt (VMware)5b7be9c2020-11-30 23:37:33 -05003340 ts = delta;
3341 break;
3342
3343 case RINGBUF_TYPE_PADDING:
3344 if (event->time_delta == 1)
3345 break;
3346 /* fall through */
3347 case RINGBUF_TYPE_DATA:
3348 ts += event->time_delta;
3349 break;
3350
3351 default:
3352 RB_WARN_ON(cpu_buffer, 1);
3353 }
3354 }
3355 if ((full && ts > info->ts) ||
3356 (!full && ts + info->delta != info->ts)) {
3357 /* If another report is happening, ignore this one */
3358 if (atomic_inc_return(&ts_dump) != 1) {
3359 atomic_dec(&ts_dump);
3360 goto out;
3361 }
3362 atomic_inc(&cpu_buffer->record_disabled);
Steven Rostedt (VMware)6549de12021-03-03 18:23:40 -05003363 /* There's some cases in boot up that this can happen */
3364 WARN_ON_ONCE(system_state != SYSTEM_BOOTING);
3365 pr_warn("[CPU: %d]TIME DOES NOT MATCH expected:%lld actual:%lld delta:%lld before:%lld after:%lld%s\n",
3366 cpu_buffer->cpu,
3367 ts + info->delta, info->ts, info->delta,
3368 info->before, info->after,
3369 full ? " (full)" : "");
Steven Rostedt (VMware)5b7be9c2020-11-30 23:37:33 -05003370 dump_buffer_page(bpage, info, tail);
3371 atomic_dec(&ts_dump);
3372 /* Do not re-enable checking */
3373 return;
3374 }
3375out:
3376 atomic_dec(this_cpu_ptr(&checking));
3377}
3378#else
3379static inline void check_buffer(struct ring_buffer_per_cpu *cpu_buffer,
3380 struct rb_event_info *info,
3381 unsigned long tail)
3382{
3383}
3384#endif /* CONFIG_RING_BUFFER_VALIDATE_TIME_DELTAS */
3385
Steven Rostedt6634ff22009-05-06 15:30:07 -04003386static struct ring_buffer_event *
3387__rb_reserve_next(struct ring_buffer_per_cpu *cpu_buffer,
Steven Rostedt (Red Hat)fcc742e2015-05-28 17:13:14 -04003388 struct rb_event_info *info)
Steven Rostedt6634ff22009-05-06 15:30:07 -04003389{
Steven Rostedt6634ff22009-05-06 15:30:07 -04003390 struct ring_buffer_event *event;
Steven Rostedt (Red Hat)fcc742e2015-05-28 17:13:14 -04003391 struct buffer_page *tail_page;
Steven Rostedt (VMware)a389d862020-06-28 22:52:25 -04003392 unsigned long tail, write, w;
Steven Rostedt (VMware)10464b42020-06-28 22:52:27 -04003393 bool a_ok;
3394 bool b_ok;
Steven Rostedt69d1b832010-10-07 18:18:05 -04003395
Steven Rostedt (Red Hat)85736362015-11-17 14:03:11 -05003396 /* Don't let the compiler play games with cpu_buffer->tail_page */
3397 tail_page = info->tail_page = READ_ONCE(cpu_buffer->tail_page);
Steven Rostedt (VMware)a389d862020-06-28 22:52:25 -04003398
3399 /*A*/ w = local_read(&tail_page->write) & RB_WRITE_MASK;
3400 barrier();
Steven Rostedt (VMware)58fbc3c2020-06-30 08:59:26 -04003401 b_ok = rb_time_read(&cpu_buffer->before_stamp, &info->before);
3402 a_ok = rb_time_read(&cpu_buffer->write_stamp, &info->after);
Steven Rostedt (VMware)a389d862020-06-28 22:52:25 -04003403 barrier();
3404 info->ts = rb_time_stamp(cpu_buffer->buffer);
3405
Steven Rostedt (VMware)58fbc3c2020-06-30 08:59:26 -04003406 if ((info->add_timestamp & RB_ADD_STAMP_ABSOLUTE)) {
Steven Rostedt (VMware)a389d862020-06-28 22:52:25 -04003407 info->delta = info->ts;
Steven Rostedt (VMware)a389d862020-06-28 22:52:25 -04003408 } else {
Steven Rostedt (VMware)58fbc3c2020-06-30 08:59:26 -04003409 /*
3410 * If interrupting an event time update, we may need an
3411 * absolute timestamp.
3412 * Don't bother if this is the start of a new page (w == 0).
3413 */
3414 if (unlikely(!a_ok || !b_ok || (info->before != info->after && w))) {
3415 info->add_timestamp |= RB_ADD_STAMP_FORCE | RB_ADD_STAMP_EXTEND;
3416 info->length += RB_LEN_TIME_EXTEND;
3417 } else {
3418 info->delta = info->ts - info->after;
3419 if (unlikely(test_time_stamp(info->delta))) {
3420 info->add_timestamp |= RB_ADD_STAMP_EXTEND;
3421 info->length += RB_LEN_TIME_EXTEND;
3422 }
Steven Rostedt (VMware)10464b42020-06-28 22:52:27 -04003423 }
Steven Rostedt (VMware)7c4b4a52020-06-28 22:52:26 -04003424 }
Steven Rostedt (VMware)a389d862020-06-28 22:52:25 -04003425
Steven Rostedt (VMware)10464b42020-06-28 22:52:27 -04003426 /*B*/ rb_time_set(&cpu_buffer->before_stamp, info->ts);
Steven Rostedt (VMware)a389d862020-06-28 22:52:25 -04003427
3428 /*C*/ write = local_add_return(info->length, &tail_page->write);
Steven Rostedt77ae3652009-03-27 11:00:29 -04003429
3430 /* set write to only the index of the write */
3431 write &= RB_WRITE_MASK;
Steven Rostedt (VMware)a389d862020-06-28 22:52:25 -04003432
Steven Rostedt (Red Hat)fcc742e2015-05-28 17:13:14 -04003433 tail = write - info->length;
Steven Rostedt6634ff22009-05-06 15:30:07 -04003434
Steven Rostedt (VMware)a389d862020-06-28 22:52:25 -04003435 /* See if we shot pass the end of this buffer page */
3436 if (unlikely(write > BUF_PAGE_SIZE)) {
Steven Rostedt (VMware)68e10d52020-11-30 23:16:03 -05003437 /* before and after may now different, fix it up*/
3438 b_ok = rb_time_read(&cpu_buffer->before_stamp, &info->before);
3439 a_ok = rb_time_read(&cpu_buffer->write_stamp, &info->after);
3440 if (a_ok && b_ok && info->before != info->after)
3441 (void)rb_time_cmpxchg(&cpu_buffer->before_stamp,
3442 info->before, info->after);
Steven Rostedt (VMware)5b7be9c2020-11-30 23:37:33 -05003443 if (a_ok && b_ok)
3444 check_buffer(cpu_buffer, info, CHECK_FULL_PAGE);
Steven Rostedt (VMware)a389d862020-06-28 22:52:25 -04003445 return rb_move_tail(cpu_buffer, tail, info);
3446 }
3447
3448 if (likely(tail == w)) {
3449 u64 save_before;
Steven Rostedt (VMware)10464b42020-06-28 22:52:27 -04003450 bool s_ok;
Steven Rostedt (VMware)a389d862020-06-28 22:52:25 -04003451
3452 /* Nothing interrupted us between A and C */
Steven Rostedt (VMware)10464b42020-06-28 22:52:27 -04003453 /*D*/ rb_time_set(&cpu_buffer->write_stamp, info->ts);
Steven Rostedt (VMware)a389d862020-06-28 22:52:25 -04003454 barrier();
Steven Rostedt (VMware)10464b42020-06-28 22:52:27 -04003455 /*E*/ s_ok = rb_time_read(&cpu_buffer->before_stamp, &save_before);
3456 RB_WARN_ON(cpu_buffer, !s_ok);
Steven Rostedt (VMware)7c4b4a52020-06-28 22:52:26 -04003457 if (likely(!(info->add_timestamp &
3458 (RB_ADD_STAMP_FORCE | RB_ADD_STAMP_ABSOLUTE))))
Steven Rostedt (VMware)a389d862020-06-28 22:52:25 -04003459 /* This did not interrupt any time update */
Steven Rostedt (VMware)58fbc3c2020-06-30 08:59:26 -04003460 info->delta = info->ts - info->after;
Steven Rostedt (VMware)a389d862020-06-28 22:52:25 -04003461 else
Qiujun Huang82db9092020-10-14 23:27:49 +08003462 /* Just use full timestamp for interrupting event */
Steven Rostedt (VMware)a389d862020-06-28 22:52:25 -04003463 info->delta = info->ts;
3464 barrier();
Steven Rostedt (VMware)5b7be9c2020-11-30 23:37:33 -05003465 check_buffer(cpu_buffer, info, tail);
Steven Rostedt (VMware)a389d862020-06-28 22:52:25 -04003466 if (unlikely(info->ts != save_before)) {
3467 /* SLOW PATH - Interrupted between C and E */
3468
Steven Rostedt (VMware)58fbc3c2020-06-30 08:59:26 -04003469 a_ok = rb_time_read(&cpu_buffer->write_stamp, &info->after);
Steven Rostedt (VMware)10464b42020-06-28 22:52:27 -04003470 RB_WARN_ON(cpu_buffer, !a_ok);
3471
Steven Rostedt (VMware)a389d862020-06-28 22:52:25 -04003472 /* Write stamp must only go forward */
Steven Rostedt (VMware)58fbc3c2020-06-30 08:59:26 -04003473 if (save_before > info->after) {
Steven Rostedt (VMware)a389d862020-06-28 22:52:25 -04003474 /*
3475 * We do not care about the result, only that
3476 * it gets updated atomically.
3477 */
Steven Rostedt (VMware)58fbc3c2020-06-30 08:59:26 -04003478 (void)rb_time_cmpxchg(&cpu_buffer->write_stamp,
3479 info->after, save_before);
Steven Rostedt (VMware)a389d862020-06-28 22:52:25 -04003480 }
3481 }
3482 } else {
3483 u64 ts;
3484 /* SLOW PATH - Interrupted between A and C */
Steven Rostedt (VMware)58fbc3c2020-06-30 08:59:26 -04003485 a_ok = rb_time_read(&cpu_buffer->write_stamp, &info->after);
Steven Rostedt (VMware)10464b42020-06-28 22:52:27 -04003486 /* Was interrupted before here, write_stamp must be valid */
3487 RB_WARN_ON(cpu_buffer, !a_ok);
Steven Rostedt (VMware)a389d862020-06-28 22:52:25 -04003488 ts = rb_time_stamp(cpu_buffer->buffer);
3489 barrier();
3490 /*E*/ if (write == (local_read(&tail_page->write) & RB_WRITE_MASK) &&
Andrea Righi8785f512020-11-28 10:15:17 +01003491 info->after < ts &&
3492 rb_time_cmpxchg(&cpu_buffer->write_stamp,
3493 info->after, ts)) {
Steven Rostedt (VMware)a389d862020-06-28 22:52:25 -04003494 /* Nothing came after this event between C and E */
Steven Rostedt (VMware)58fbc3c2020-06-30 08:59:26 -04003495 info->delta = ts - info->after;
Steven Rostedt (VMware)a389d862020-06-28 22:52:25 -04003496 } else {
3497 /*
Qiujun Huang82db9092020-10-14 23:27:49 +08003498 * Interrupted between C and E:
Steven Rostedt (VMware)a389d862020-06-28 22:52:25 -04003499 * Lost the previous events time stamp. Just set the
3500 * delta to zero, and this will be the same time as
3501 * the event this event interrupted. And the events that
3502 * came after this will still be correct (as they would
3503 * have built their delta on the previous event.
3504 */
3505 info->delta = 0;
3506 }
Steven Rostedt (VMware)8672e4942021-03-16 12:41:02 -04003507 info->ts = ts;
Steven Rostedt (VMware)7c4b4a52020-06-28 22:52:26 -04003508 info->add_timestamp &= ~RB_ADD_STAMP_FORCE;
Steven Rostedt (VMware)a389d862020-06-28 22:52:25 -04003509 }
3510
Steven Rostedt (Red Hat)b7dc42f2015-09-03 08:57:12 -04003511 /*
3512 * If this is the first commit on the page, then it has the same
3513 * timestamp as the page itself.
3514 */
Steven Rostedt (VMware)7c4b4a52020-06-28 22:52:26 -04003515 if (unlikely(!tail && !(info->add_timestamp &
3516 (RB_ADD_STAMP_FORCE | RB_ADD_STAMP_ABSOLUTE))))
Steven Rostedt (Red Hat)b7dc42f2015-09-03 08:57:12 -04003517 info->delta = 0;
3518
Steven Rostedt6634ff22009-05-06 15:30:07 -04003519 /* We reserved something on the buffer */
Steven Rostedt (Red Hat)b7dc42f2015-09-03 08:57:12 -04003520
Steven Rostedt6634ff22009-05-06 15:30:07 -04003521 event = __rb_page_index(tail_page, tail);
Steven Rostedt (Red Hat)fcc742e2015-05-28 17:13:14 -04003522 rb_update_event(cpu_buffer, event, info);
Steven Rostedt6634ff22009-05-06 15:30:07 -04003523
Steven Rostedt69d1b832010-10-07 18:18:05 -04003524 local_inc(&tail_page->entries);
Steven Rostedt6634ff22009-05-06 15:30:07 -04003525
Steven Rostedt (Red Hat)b7dc42f2015-09-03 08:57:12 -04003526 /*
3527 * If this is the first commit on the page, then update
3528 * its timestamp.
3529 */
Steven Rostedt (VMware)75b21c62020-06-30 09:04:35 -04003530 if (unlikely(!tail))
Steven Rostedt (Red Hat)b7dc42f2015-09-03 08:57:12 -04003531 tail_page->page->time_stamp = info->ts;
3532
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -07003533 /* account for these added bytes */
Steven Rostedt (Red Hat)fcc742e2015-05-28 17:13:14 -04003534 local_add(info->length, &cpu_buffer->entries_bytes);
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -07003535
Steven Rostedt6634ff22009-05-06 15:30:07 -04003536 return event;
3537}
3538
Steven Rostedt (Red Hat)fa7ffb32016-11-23 11:36:30 -05003539static __always_inline struct ring_buffer_event *
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05003540rb_reserve_next_event(struct trace_buffer *buffer,
Steven Rostedt62f0b3e2009-09-04 14:11:34 -04003541 struct ring_buffer_per_cpu *cpu_buffer,
Steven Rostedt1cd8d732009-05-11 14:08:09 -04003542 unsigned long length)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003543{
3544 struct ring_buffer_event *event;
Steven Rostedt (Red Hat)fcc742e2015-05-28 17:13:14 -04003545 struct rb_event_info info;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003546 int nr_loops = 0;
Steven Rostedt (VMware)58fbc3c2020-06-30 08:59:26 -04003547 int add_ts_default;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003548
Steven Rostedtfa743952009-06-16 12:37:57 -04003549 rb_start_commit(cpu_buffer);
Steven Rostedt (VMware)a389d862020-06-28 22:52:25 -04003550 /* The commit page can not change after this */
Steven Rostedtfa743952009-06-16 12:37:57 -04003551
Steven Rostedt85bac322009-09-04 14:24:40 -04003552#ifdef CONFIG_RING_BUFFER_ALLOW_SWAP
Steven Rostedt62f0b3e2009-09-04 14:11:34 -04003553 /*
3554 * Due to the ability to swap a cpu buffer from a buffer
3555 * it is possible it was swapped before we committed.
3556 * (committing stops a swap). We check for it here and
3557 * if it happened, we have to fail the write.
3558 */
3559 barrier();
Mark Rutland6aa7de02017-10-23 14:07:29 -07003560 if (unlikely(READ_ONCE(cpu_buffer->buffer) != buffer)) {
Steven Rostedt62f0b3e2009-09-04 14:11:34 -04003561 local_dec(&cpu_buffer->committing);
3562 local_dec(&cpu_buffer->commits);
3563 return NULL;
3564 }
Steven Rostedt85bac322009-09-04 14:24:40 -04003565#endif
Steven Rostedt (Red Hat)b7dc42f2015-09-03 08:57:12 -04003566
Steven Rostedt (Red Hat)fcc742e2015-05-28 17:13:14 -04003567 info.length = rb_calculate_event_length(length);
Steven Rostedt (VMware)58fbc3c2020-06-30 08:59:26 -04003568
3569 if (ring_buffer_time_stamp_abs(cpu_buffer->buffer)) {
3570 add_ts_default = RB_ADD_STAMP_ABSOLUTE;
3571 info.length += RB_LEN_TIME_EXTEND;
3572 } else {
3573 add_ts_default = RB_ADD_STAMP_NONE;
3574 }
3575
Steven Rostedt (Red Hat)a4543a22015-05-29 09:40:18 -04003576 again:
Steven Rostedt (VMware)58fbc3c2020-06-30 08:59:26 -04003577 info.add_timestamp = add_ts_default;
Steven Rostedt (Red Hat)b7dc42f2015-09-03 08:57:12 -04003578 info.delta = 0;
3579
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003580 /*
3581 * We allow for interrupts to reenter here and do a trace.
3582 * If one does, it will cause this original code to loop
3583 * back here. Even with heavy interrupts happening, this
3584 * should only happen a few times in a row. If this happens
3585 * 1000 times in a row, there must be either an interrupt
3586 * storm or we have something buggy.
3587 * Bail!
3588 */
Steven Rostedt3e89c7bb2008-11-11 15:28:41 -05003589 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 1000))
Steven Rostedtfa743952009-06-16 12:37:57 -04003590 goto out_fail;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003591
Steven Rostedt (Red Hat)fcc742e2015-05-28 17:13:14 -04003592 event = __rb_reserve_next(cpu_buffer, &info);
3593
Steven Rostedt (Red Hat)bd1b7cd2015-11-23 17:35:24 -05003594 if (unlikely(PTR_ERR(event) == -EAGAIN)) {
Steven Rostedt (VMware)58fbc3c2020-06-30 08:59:26 -04003595 if (info.add_timestamp & (RB_ADD_STAMP_FORCE | RB_ADD_STAMP_EXTEND))
Steven Rostedt (Red Hat)bd1b7cd2015-11-23 17:35:24 -05003596 info.length -= RB_LEN_TIME_EXTEND;
Steven Rostedtbf41a152008-10-04 02:00:59 -04003597 goto again;
Steven Rostedt (Red Hat)bd1b7cd2015-11-23 17:35:24 -05003598 }
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003599
Steven Rostedt (VMware)a389d862020-06-28 22:52:25 -04003600 if (likely(event))
3601 return event;
Steven Rostedtfa743952009-06-16 12:37:57 -04003602 out_fail:
3603 rb_end_commit(cpu_buffer);
3604 return NULL;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003605}
3606
3607/**
3608 * ring_buffer_lock_reserve - reserve a part of the buffer
3609 * @buffer: the ring buffer to reserve from
3610 * @length: the length of the data to reserve (excluding event header)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003611 *
Steven Rostedt (VMware)6167c202018-05-16 11:17:06 -04003612 * Returns a reserved event on the ring buffer to copy directly to.
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003613 * The user of this interface will need to get the body to write into
3614 * and can use the ring_buffer_event_data() interface.
3615 *
3616 * The length is the length of the data needed, not the event length
3617 * which also includes the event header.
3618 *
3619 * Must be paired with ring_buffer_unlock_commit, unless NULL is returned.
3620 * If NULL is returned, then nothing has been allocated or locked.
3621 */
3622struct ring_buffer_event *
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05003623ring_buffer_lock_reserve(struct trace_buffer *buffer, unsigned long length)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003624{
3625 struct ring_buffer_per_cpu *cpu_buffer;
3626 struct ring_buffer_event *event;
Steven Rostedt5168ae52010-06-03 09:36:50 -04003627 int cpu;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003628
Steven Rostedtbf41a152008-10-04 02:00:59 -04003629 /* If we are tracing schedule, we don't want to recurse */
Steven Rostedt5168ae52010-06-03 09:36:50 -04003630 preempt_disable_notrace();
Steven Rostedtbf41a152008-10-04 02:00:59 -04003631
Steven Rostedt (Red Hat)3205f802015-05-21 17:39:29 -04003632 if (unlikely(atomic_read(&buffer->record_disabled)))
Steven Rostedt (Red Hat)58a09ec2015-05-27 10:27:47 -04003633 goto out;
Steven Rostedt261842b2009-04-16 21:41:52 -04003634
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003635 cpu = raw_smp_processor_id();
3636
Steven Rostedt (Red Hat)3205f802015-05-21 17:39:29 -04003637 if (unlikely(!cpumask_test_cpu(cpu, buffer->cpumask)))
Steven Rostedtd7690412008-10-01 00:29:53 -04003638 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003639
3640 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003641
Steven Rostedt (Red Hat)3205f802015-05-21 17:39:29 -04003642 if (unlikely(atomic_read(&cpu_buffer->record_disabled)))
Steven Rostedtd7690412008-10-01 00:29:53 -04003643 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003644
Steven Rostedt (Red Hat)3205f802015-05-21 17:39:29 -04003645 if (unlikely(length > BUF_MAX_DATA_SIZE))
Steven Rostedtbf41a152008-10-04 02:00:59 -04003646 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003647
Steven Rostedt (Red Hat)58a09ec2015-05-27 10:27:47 -04003648 if (unlikely(trace_recursive_lock(cpu_buffer)))
3649 goto out;
3650
Steven Rostedt62f0b3e2009-09-04 14:11:34 -04003651 event = rb_reserve_next_event(buffer, cpu_buffer, length);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003652 if (!event)
Steven Rostedt (Red Hat)58a09ec2015-05-27 10:27:47 -04003653 goto out_unlock;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003654
3655 return event;
3656
Steven Rostedt (Red Hat)58a09ec2015-05-27 10:27:47 -04003657 out_unlock:
3658 trace_recursive_unlock(cpu_buffer);
Steven Rostedtd7690412008-10-01 00:29:53 -04003659 out:
Steven Rostedt5168ae52010-06-03 09:36:50 -04003660 preempt_enable_notrace();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003661 return NULL;
3662}
Robert Richterc4f50182008-12-11 16:49:22 +01003663EXPORT_SYMBOL_GPL(ring_buffer_lock_reserve);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003664
Steven Rostedta1863c22009-09-03 10:23:58 -04003665/*
3666 * Decrement the entries to the page that an event is on.
3667 * The event does not even need to exist, only the pointer
3668 * to the page it is on. This may only be called before the commit
3669 * takes place.
3670 */
3671static inline void
3672rb_decrement_entry(struct ring_buffer_per_cpu *cpu_buffer,
3673 struct ring_buffer_event *event)
3674{
3675 unsigned long addr = (unsigned long)event;
3676 struct buffer_page *bpage = cpu_buffer->commit_page;
3677 struct buffer_page *start;
3678
3679 addr &= PAGE_MASK;
3680
3681 /* Do the likely case first */
3682 if (likely(bpage->page == (void *)addr)) {
3683 local_dec(&bpage->entries);
3684 return;
3685 }
3686
3687 /*
3688 * Because the commit page may be on the reader page we
3689 * start with the next page and check the end loop there.
3690 */
Qiujun Huang6689bed2020-12-25 22:03:56 +08003691 rb_inc_page(&bpage);
Steven Rostedta1863c22009-09-03 10:23:58 -04003692 start = bpage;
3693 do {
3694 if (bpage->page == (void *)addr) {
3695 local_dec(&bpage->entries);
3696 return;
3697 }
Qiujun Huang6689bed2020-12-25 22:03:56 +08003698 rb_inc_page(&bpage);
Steven Rostedta1863c22009-09-03 10:23:58 -04003699 } while (bpage != start);
3700
3701 /* commit not part of this buffer?? */
3702 RB_WARN_ON(cpu_buffer, 1);
3703}
3704
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003705/**
Qiujun Huang88883492020-11-12 23:18:00 +08003706 * ring_buffer_discard_commit - discard an event that has not been committed
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04003707 * @buffer: the ring buffer
3708 * @event: non committed event to discard
3709 *
Steven Rostedtdc892f72009-09-03 15:33:41 -04003710 * Sometimes an event that is in the ring buffer needs to be ignored.
3711 * This function lets the user discard an event in the ring buffer
3712 * and then that event will not be read later.
3713 *
Steven Rostedt (VMware)6167c202018-05-16 11:17:06 -04003714 * This function only works if it is called before the item has been
Steven Rostedtdc892f72009-09-03 15:33:41 -04003715 * committed. It will try to free the event from the ring buffer
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04003716 * if another event has not been added behind it.
3717 *
3718 * If another event has been added behind it, it will set the event
3719 * up as discarded, and perform the commit.
3720 *
3721 * If this function is called, do not call ring_buffer_unlock_commit on
3722 * the event.
3723 */
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05003724void ring_buffer_discard_commit(struct trace_buffer *buffer,
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04003725 struct ring_buffer_event *event)
3726{
3727 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04003728 int cpu;
3729
3730 /* The event is discarded regardless */
Frederic Weisbeckerf3b9aae2009-04-19 23:39:33 +02003731 rb_event_discard(event);
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04003732
Steven Rostedtfa743952009-06-16 12:37:57 -04003733 cpu = smp_processor_id();
3734 cpu_buffer = buffer->buffers[cpu];
3735
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04003736 /*
3737 * This must only be called if the event has not been
3738 * committed yet. Thus we can assume that preemption
3739 * is still disabled.
3740 */
Steven Rostedtfa743952009-06-16 12:37:57 -04003741 RB_WARN_ON(buffer, !local_read(&cpu_buffer->committing));
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04003742
Steven Rostedta1863c22009-09-03 10:23:58 -04003743 rb_decrement_entry(cpu_buffer, event);
Steven Rostedt0f2541d2009-08-05 12:02:48 -04003744 if (rb_try_to_discard(cpu_buffer, event))
Steven Rostedtedd813bf2009-06-02 23:00:53 -04003745 goto out;
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04003746
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04003747 out:
Steven Rostedtfa743952009-06-16 12:37:57 -04003748 rb_end_commit(cpu_buffer);
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04003749
Steven Rostedt (Red Hat)58a09ec2015-05-27 10:27:47 -04003750 trace_recursive_unlock(cpu_buffer);
Frederic Weisbeckerf3b9aae2009-04-19 23:39:33 +02003751
Steven Rostedt5168ae52010-06-03 09:36:50 -04003752 preempt_enable_notrace();
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04003753
3754}
3755EXPORT_SYMBOL_GPL(ring_buffer_discard_commit);
3756
3757/**
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003758 * ring_buffer_write - write data to the buffer without reserving
3759 * @buffer: The ring buffer to write to.
3760 * @length: The length of the data being written (excluding the event header)
3761 * @data: The data to write to the buffer.
3762 *
3763 * This is like ring_buffer_lock_reserve and ring_buffer_unlock_commit as
3764 * one function. If you already have the data to write to the buffer, it
3765 * may be easier to simply call this function.
3766 *
3767 * Note, like ring_buffer_lock_reserve, the length is the length of the data
3768 * and not the length of the event which would hold the header.
3769 */
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05003770int ring_buffer_write(struct trace_buffer *buffer,
David Sharp01e3e712012-06-07 16:46:24 -07003771 unsigned long length,
3772 void *data)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003773{
3774 struct ring_buffer_per_cpu *cpu_buffer;
3775 struct ring_buffer_event *event;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003776 void *body;
3777 int ret = -EBUSY;
Steven Rostedt5168ae52010-06-03 09:36:50 -04003778 int cpu;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003779
Steven Rostedt5168ae52010-06-03 09:36:50 -04003780 preempt_disable_notrace();
Steven Rostedtbf41a152008-10-04 02:00:59 -04003781
Lai Jiangshan52fbe9c2010-03-08 14:50:43 +08003782 if (atomic_read(&buffer->record_disabled))
3783 goto out;
3784
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003785 cpu = raw_smp_processor_id();
3786
Rusty Russell9e01c1b2009-01-01 10:12:22 +10303787 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedtd7690412008-10-01 00:29:53 -04003788 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003789
3790 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003791
3792 if (atomic_read(&cpu_buffer->record_disabled))
3793 goto out;
3794
Steven Rostedtbe957c42009-05-11 14:42:53 -04003795 if (length > BUF_MAX_DATA_SIZE)
3796 goto out;
3797
Steven Rostedt (Red Hat)985e8712015-05-27 10:48:56 -04003798 if (unlikely(trace_recursive_lock(cpu_buffer)))
3799 goto out;
3800
Steven Rostedt62f0b3e2009-09-04 14:11:34 -04003801 event = rb_reserve_next_event(buffer, cpu_buffer, length);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003802 if (!event)
Steven Rostedt (Red Hat)985e8712015-05-27 10:48:56 -04003803 goto out_unlock;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003804
3805 body = rb_event_data(event);
3806
3807 memcpy(body, data, length);
3808
3809 rb_commit(cpu_buffer, event);
3810
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -05003811 rb_wakeups(buffer, cpu_buffer);
3812
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003813 ret = 0;
Steven Rostedt (Red Hat)985e8712015-05-27 10:48:56 -04003814
3815 out_unlock:
3816 trace_recursive_unlock(cpu_buffer);
3817
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003818 out:
Steven Rostedt5168ae52010-06-03 09:36:50 -04003819 preempt_enable_notrace();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003820
3821 return ret;
3822}
Robert Richterc4f50182008-12-11 16:49:22 +01003823EXPORT_SYMBOL_GPL(ring_buffer_write);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003824
Yaowei Baida588342015-09-29 22:43:33 +08003825static bool rb_per_cpu_empty(struct ring_buffer_per_cpu *cpu_buffer)
Steven Rostedtbf41a152008-10-04 02:00:59 -04003826{
3827 struct buffer_page *reader = cpu_buffer->reader_page;
Steven Rostedt77ae3652009-03-27 11:00:29 -04003828 struct buffer_page *head = rb_set_head_page(cpu_buffer);
Steven Rostedtbf41a152008-10-04 02:00:59 -04003829 struct buffer_page *commit = cpu_buffer->commit_page;
3830
Steven Rostedt77ae3652009-03-27 11:00:29 -04003831 /* In case of error, head will be NULL */
3832 if (unlikely(!head))
Yaowei Baida588342015-09-29 22:43:33 +08003833 return true;
Steven Rostedt77ae3652009-03-27 11:00:29 -04003834
Steven Rostedtbf41a152008-10-04 02:00:59 -04003835 return reader->read == rb_page_commit(reader) &&
3836 (commit == reader ||
3837 (commit == head &&
3838 head->read == rb_page_commit(commit)));
3839}
3840
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003841/**
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003842 * ring_buffer_record_disable - stop all writes into the buffer
3843 * @buffer: The ring buffer to stop writes to.
3844 *
3845 * This prevents all writes to the buffer. Any attempt to write
3846 * to the buffer after this will fail and return NULL.
3847 *
Paul E. McKenney74401722018-11-06 18:44:52 -08003848 * The caller should call synchronize_rcu() after this.
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003849 */
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05003850void ring_buffer_record_disable(struct trace_buffer *buffer)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003851{
3852 atomic_inc(&buffer->record_disabled);
3853}
Robert Richterc4f50182008-12-11 16:49:22 +01003854EXPORT_SYMBOL_GPL(ring_buffer_record_disable);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003855
3856/**
3857 * ring_buffer_record_enable - enable writes to the buffer
3858 * @buffer: The ring buffer to enable writes
3859 *
3860 * Note, multiple disables will need the same number of enables
Adam Buchbinderc41b20e2009-12-11 16:35:39 -05003861 * to truly enable the writing (much like preempt_disable).
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003862 */
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05003863void ring_buffer_record_enable(struct trace_buffer *buffer)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003864{
3865 atomic_dec(&buffer->record_disabled);
3866}
Robert Richterc4f50182008-12-11 16:49:22 +01003867EXPORT_SYMBOL_GPL(ring_buffer_record_enable);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003868
3869/**
Steven Rostedt499e5472012-02-22 15:50:28 -05003870 * ring_buffer_record_off - stop all writes into the buffer
3871 * @buffer: The ring buffer to stop writes to.
3872 *
3873 * This prevents all writes to the buffer. Any attempt to write
3874 * to the buffer after this will fail and return NULL.
3875 *
3876 * This is different than ring_buffer_record_disable() as
Wang Tianhong87abb3b2012-08-02 14:02:00 +08003877 * it works like an on/off switch, where as the disable() version
Steven Rostedt499e5472012-02-22 15:50:28 -05003878 * must be paired with a enable().
3879 */
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05003880void ring_buffer_record_off(struct trace_buffer *buffer)
Steven Rostedt499e5472012-02-22 15:50:28 -05003881{
3882 unsigned int rd;
3883 unsigned int new_rd;
3884
3885 do {
3886 rd = atomic_read(&buffer->record_disabled);
3887 new_rd = rd | RB_BUFFER_OFF;
3888 } while (atomic_cmpxchg(&buffer->record_disabled, rd, new_rd) != rd);
3889}
3890EXPORT_SYMBOL_GPL(ring_buffer_record_off);
3891
3892/**
3893 * ring_buffer_record_on - restart writes into the buffer
3894 * @buffer: The ring buffer to start writes to.
3895 *
3896 * This enables all writes to the buffer that was disabled by
3897 * ring_buffer_record_off().
3898 *
3899 * This is different than ring_buffer_record_enable() as
Wang Tianhong87abb3b2012-08-02 14:02:00 +08003900 * it works like an on/off switch, where as the enable() version
Steven Rostedt499e5472012-02-22 15:50:28 -05003901 * must be paired with a disable().
3902 */
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05003903void ring_buffer_record_on(struct trace_buffer *buffer)
Steven Rostedt499e5472012-02-22 15:50:28 -05003904{
3905 unsigned int rd;
3906 unsigned int new_rd;
3907
3908 do {
3909 rd = atomic_read(&buffer->record_disabled);
3910 new_rd = rd & ~RB_BUFFER_OFF;
3911 } while (atomic_cmpxchg(&buffer->record_disabled, rd, new_rd) != rd);
3912}
3913EXPORT_SYMBOL_GPL(ring_buffer_record_on);
3914
3915/**
3916 * ring_buffer_record_is_on - return true if the ring buffer can write
3917 * @buffer: The ring buffer to see if write is enabled
3918 *
3919 * Returns true if the ring buffer is in a state that it accepts writes.
3920 */
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05003921bool ring_buffer_record_is_on(struct trace_buffer *buffer)
Steven Rostedt499e5472012-02-22 15:50:28 -05003922{
3923 return !atomic_read(&buffer->record_disabled);
3924}
3925
3926/**
Masami Hiramatsu73c8d892018-07-14 01:28:15 +09003927 * ring_buffer_record_is_set_on - return true if the ring buffer is set writable
3928 * @buffer: The ring buffer to see if write is set enabled
3929 *
3930 * Returns true if the ring buffer is set writable by ring_buffer_record_on().
3931 * Note that this does NOT mean it is in a writable state.
3932 *
3933 * It may return true when the ring buffer has been disabled by
3934 * ring_buffer_record_disable(), as that is a temporary disabling of
3935 * the ring buffer.
3936 */
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05003937bool ring_buffer_record_is_set_on(struct trace_buffer *buffer)
Masami Hiramatsu73c8d892018-07-14 01:28:15 +09003938{
3939 return !(atomic_read(&buffer->record_disabled) & RB_BUFFER_OFF);
3940}
3941
3942/**
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003943 * ring_buffer_record_disable_cpu - stop all writes into the cpu_buffer
3944 * @buffer: The ring buffer to stop writes to.
3945 * @cpu: The CPU buffer to stop
3946 *
3947 * This prevents all writes to the buffer. Any attempt to write
3948 * to the buffer after this will fail and return NULL.
3949 *
Paul E. McKenney74401722018-11-06 18:44:52 -08003950 * The caller should call synchronize_rcu() after this.
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003951 */
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05003952void ring_buffer_record_disable_cpu(struct trace_buffer *buffer, int cpu)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003953{
3954 struct ring_buffer_per_cpu *cpu_buffer;
3955
Rusty Russell9e01c1b2009-01-01 10:12:22 +10303956 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04003957 return;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003958
3959 cpu_buffer = buffer->buffers[cpu];
3960 atomic_inc(&cpu_buffer->record_disabled);
3961}
Robert Richterc4f50182008-12-11 16:49:22 +01003962EXPORT_SYMBOL_GPL(ring_buffer_record_disable_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003963
3964/**
3965 * ring_buffer_record_enable_cpu - enable writes to the buffer
3966 * @buffer: The ring buffer to enable writes
3967 * @cpu: The CPU to enable.
3968 *
3969 * Note, multiple disables will need the same number of enables
Adam Buchbinderc41b20e2009-12-11 16:35:39 -05003970 * to truly enable the writing (much like preempt_disable).
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003971 */
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05003972void ring_buffer_record_enable_cpu(struct trace_buffer *buffer, int cpu)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003973{
3974 struct ring_buffer_per_cpu *cpu_buffer;
3975
Rusty Russell9e01c1b2009-01-01 10:12:22 +10303976 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04003977 return;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003978
3979 cpu_buffer = buffer->buffers[cpu];
3980 atomic_dec(&cpu_buffer->record_disabled);
3981}
Robert Richterc4f50182008-12-11 16:49:22 +01003982EXPORT_SYMBOL_GPL(ring_buffer_record_enable_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003983
Steven Rostedtf6195aa2010-09-01 12:23:12 -04003984/*
3985 * The total entries in the ring buffer is the running counter
3986 * of entries entered into the ring buffer, minus the sum of
3987 * the entries read from the ring buffer and the number of
3988 * entries that were overwritten.
3989 */
3990static inline unsigned long
3991rb_num_of_entries(struct ring_buffer_per_cpu *cpu_buffer)
3992{
3993 return local_read(&cpu_buffer->entries) -
3994 (local_read(&cpu_buffer->overrun) + cpu_buffer->read);
3995}
3996
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003997/**
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -07003998 * ring_buffer_oldest_event_ts - get the oldest event timestamp from the buffer
3999 * @buffer: The ring buffer
4000 * @cpu: The per CPU buffer to read from.
4001 */
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05004002u64 ring_buffer_oldest_event_ts(struct trace_buffer *buffer, int cpu)
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -07004003{
4004 unsigned long flags;
4005 struct ring_buffer_per_cpu *cpu_buffer;
4006 struct buffer_page *bpage;
Linus Torvaldsda830e52012-12-11 18:18:58 -08004007 u64 ret = 0;
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -07004008
4009 if (!cpumask_test_cpu(cpu, buffer->cpumask))
4010 return 0;
4011
4012 cpu_buffer = buffer->buffers[cpu];
Linus Torvalds7115e3f2011-10-26 17:03:38 +02004013 raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -07004014 /*
4015 * if the tail is on reader_page, oldest time stamp is on the reader
4016 * page
4017 */
4018 if (cpu_buffer->tail_page == cpu_buffer->reader_page)
4019 bpage = cpu_buffer->reader_page;
4020 else
4021 bpage = rb_set_head_page(cpu_buffer);
Steven Rostedt54f7be52012-11-29 22:27:22 -05004022 if (bpage)
4023 ret = bpage->page->time_stamp;
Linus Torvalds7115e3f2011-10-26 17:03:38 +02004024 raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -07004025
4026 return ret;
4027}
4028EXPORT_SYMBOL_GPL(ring_buffer_oldest_event_ts);
4029
4030/**
4031 * ring_buffer_bytes_cpu - get the number of bytes consumed in a cpu buffer
4032 * @buffer: The ring buffer
4033 * @cpu: The per CPU buffer to read from.
4034 */
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05004035unsigned long ring_buffer_bytes_cpu(struct trace_buffer *buffer, int cpu)
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -07004036{
4037 struct ring_buffer_per_cpu *cpu_buffer;
4038 unsigned long ret;
4039
4040 if (!cpumask_test_cpu(cpu, buffer->cpumask))
4041 return 0;
4042
4043 cpu_buffer = buffer->buffers[cpu];
4044 ret = local_read(&cpu_buffer->entries_bytes) - cpu_buffer->read_bytes;
4045
4046 return ret;
4047}
4048EXPORT_SYMBOL_GPL(ring_buffer_bytes_cpu);
4049
4050/**
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004051 * ring_buffer_entries_cpu - get the number of entries in a cpu buffer
4052 * @buffer: The ring buffer
4053 * @cpu: The per CPU buffer to get the entries from.
4054 */
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05004055unsigned long ring_buffer_entries_cpu(struct trace_buffer *buffer, int cpu)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004056{
4057 struct ring_buffer_per_cpu *cpu_buffer;
4058
Rusty Russell9e01c1b2009-01-01 10:12:22 +10304059 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04004060 return 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004061
4062 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt554f7862009-03-11 22:00:13 -04004063
Steven Rostedtf6195aa2010-09-01 12:23:12 -04004064 return rb_num_of_entries(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004065}
Robert Richterc4f50182008-12-11 16:49:22 +01004066EXPORT_SYMBOL_GPL(ring_buffer_entries_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004067
4068/**
Slava Pestov884bfe82011-07-15 14:23:58 -07004069 * ring_buffer_overrun_cpu - get the number of overruns caused by the ring
4070 * buffer wrapping around (only if RB_FL_OVERWRITE is on).
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004071 * @buffer: The ring buffer
4072 * @cpu: The per CPU buffer to get the number of overruns from
4073 */
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05004074unsigned long ring_buffer_overrun_cpu(struct trace_buffer *buffer, int cpu)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004075{
4076 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedt8aabee52009-03-12 13:13:49 -04004077 unsigned long ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004078
Rusty Russell9e01c1b2009-01-01 10:12:22 +10304079 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04004080 return 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004081
4082 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt77ae3652009-03-27 11:00:29 -04004083 ret = local_read(&cpu_buffer->overrun);
Steven Rostedt554f7862009-03-11 22:00:13 -04004084
4085 return ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004086}
Robert Richterc4f50182008-12-11 16:49:22 +01004087EXPORT_SYMBOL_GPL(ring_buffer_overrun_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004088
4089/**
Slava Pestov884bfe82011-07-15 14:23:58 -07004090 * ring_buffer_commit_overrun_cpu - get the number of overruns caused by
4091 * commits failing due to the buffer wrapping around while there are uncommitted
4092 * events, such as during an interrupt storm.
Steven Rostedtf0d2c682009-04-29 13:43:37 -04004093 * @buffer: The ring buffer
4094 * @cpu: The per CPU buffer to get the number of overruns from
4095 */
4096unsigned long
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05004097ring_buffer_commit_overrun_cpu(struct trace_buffer *buffer, int cpu)
Steven Rostedtf0d2c682009-04-29 13:43:37 -04004098{
4099 struct ring_buffer_per_cpu *cpu_buffer;
4100 unsigned long ret;
4101
4102 if (!cpumask_test_cpu(cpu, buffer->cpumask))
4103 return 0;
4104
4105 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt77ae3652009-03-27 11:00:29 -04004106 ret = local_read(&cpu_buffer->commit_overrun);
Steven Rostedtf0d2c682009-04-29 13:43:37 -04004107
4108 return ret;
4109}
4110EXPORT_SYMBOL_GPL(ring_buffer_commit_overrun_cpu);
4111
4112/**
Slava Pestov884bfe82011-07-15 14:23:58 -07004113 * ring_buffer_dropped_events_cpu - get the number of dropped events caused by
4114 * the ring buffer filling up (only if RB_FL_OVERWRITE is off).
4115 * @buffer: The ring buffer
4116 * @cpu: The per CPU buffer to get the number of overruns from
4117 */
4118unsigned long
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05004119ring_buffer_dropped_events_cpu(struct trace_buffer *buffer, int cpu)
Slava Pestov884bfe82011-07-15 14:23:58 -07004120{
4121 struct ring_buffer_per_cpu *cpu_buffer;
4122 unsigned long ret;
4123
4124 if (!cpumask_test_cpu(cpu, buffer->cpumask))
4125 return 0;
4126
4127 cpu_buffer = buffer->buffers[cpu];
4128 ret = local_read(&cpu_buffer->dropped_events);
4129
4130 return ret;
4131}
4132EXPORT_SYMBOL_GPL(ring_buffer_dropped_events_cpu);
4133
4134/**
Steven Rostedt (Red Hat)ad964702013-01-29 17:45:49 -05004135 * ring_buffer_read_events_cpu - get the number of events successfully read
4136 * @buffer: The ring buffer
4137 * @cpu: The per CPU buffer to get the number of events read
4138 */
4139unsigned long
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05004140ring_buffer_read_events_cpu(struct trace_buffer *buffer, int cpu)
Steven Rostedt (Red Hat)ad964702013-01-29 17:45:49 -05004141{
4142 struct ring_buffer_per_cpu *cpu_buffer;
4143
4144 if (!cpumask_test_cpu(cpu, buffer->cpumask))
4145 return 0;
4146
4147 cpu_buffer = buffer->buffers[cpu];
4148 return cpu_buffer->read;
4149}
4150EXPORT_SYMBOL_GPL(ring_buffer_read_events_cpu);
4151
4152/**
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004153 * ring_buffer_entries - get the number of entries in a buffer
4154 * @buffer: The ring buffer
4155 *
4156 * Returns the total number of entries in the ring buffer
4157 * (all CPU entries)
4158 */
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05004159unsigned long ring_buffer_entries(struct trace_buffer *buffer)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004160{
4161 struct ring_buffer_per_cpu *cpu_buffer;
4162 unsigned long entries = 0;
4163 int cpu;
4164
4165 /* if you care about this being correct, lock the buffer */
4166 for_each_buffer_cpu(buffer, cpu) {
4167 cpu_buffer = buffer->buffers[cpu];
Steven Rostedtf6195aa2010-09-01 12:23:12 -04004168 entries += rb_num_of_entries(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004169 }
4170
4171 return entries;
4172}
Robert Richterc4f50182008-12-11 16:49:22 +01004173EXPORT_SYMBOL_GPL(ring_buffer_entries);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004174
4175/**
Jiri Olsa67b394f2009-10-23 19:36:18 -04004176 * ring_buffer_overruns - get the number of overruns in buffer
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004177 * @buffer: The ring buffer
4178 *
4179 * Returns the total number of overruns in the ring buffer
4180 * (all CPU entries)
4181 */
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05004182unsigned long ring_buffer_overruns(struct trace_buffer *buffer)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004183{
4184 struct ring_buffer_per_cpu *cpu_buffer;
4185 unsigned long overruns = 0;
4186 int cpu;
4187
4188 /* if you care about this being correct, lock the buffer */
4189 for_each_buffer_cpu(buffer, cpu) {
4190 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt77ae3652009-03-27 11:00:29 -04004191 overruns += local_read(&cpu_buffer->overrun);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004192 }
4193
4194 return overruns;
4195}
Robert Richterc4f50182008-12-11 16:49:22 +01004196EXPORT_SYMBOL_GPL(ring_buffer_overruns);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004197
Steven Rostedt642edba2008-11-12 00:01:26 -05004198static void rb_iter_reset(struct ring_buffer_iter *iter)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004199{
4200 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
4201
Steven Rostedtd7690412008-10-01 00:29:53 -04004202 /* Iterator usage is expected to have record disabled */
Steven Rostedt (Red Hat)651e22f2014-08-06 14:11:33 -04004203 iter->head_page = cpu_buffer->reader_page;
4204 iter->head = cpu_buffer->reader_page->read;
Steven Rostedt (VMware)785888c2020-03-17 17:32:27 -04004205 iter->next_event = iter->head;
Steven Rostedt (Red Hat)651e22f2014-08-06 14:11:33 -04004206
4207 iter->cache_reader_page = iter->head_page;
Steven Rostedt (Red Hat)24607f12014-10-02 16:51:18 -04004208 iter->cache_read = cpu_buffer->read;
Steven Rostedt (Red Hat)651e22f2014-08-06 14:11:33 -04004209
Steven Rostedt (VMware)28e3fc52020-03-17 17:32:26 -04004210 if (iter->head) {
Steven Rostedtd7690412008-10-01 00:29:53 -04004211 iter->read_stamp = cpu_buffer->read_stamp;
Steven Rostedt (VMware)28e3fc52020-03-17 17:32:26 -04004212 iter->page_stamp = cpu_buffer->reader_page->page->time_stamp;
4213 } else {
Steven Rostedtabc9b562008-12-02 15:34:06 -05004214 iter->read_stamp = iter->head_page->page->time_stamp;
Steven Rostedt (VMware)28e3fc52020-03-17 17:32:26 -04004215 iter->page_stamp = iter->read_stamp;
4216 }
Steven Rostedt642edba2008-11-12 00:01:26 -05004217}
Steven Rostedtf83c9d02008-11-11 18:47:44 +01004218
Steven Rostedt642edba2008-11-12 00:01:26 -05004219/**
4220 * ring_buffer_iter_reset - reset an iterator
4221 * @iter: The iterator to reset
4222 *
4223 * Resets the iterator, so that it will start from the beginning
4224 * again.
4225 */
4226void ring_buffer_iter_reset(struct ring_buffer_iter *iter)
4227{
Steven Rostedt554f7862009-03-11 22:00:13 -04004228 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedt642edba2008-11-12 00:01:26 -05004229 unsigned long flags;
4230
Steven Rostedt554f7862009-03-11 22:00:13 -04004231 if (!iter)
4232 return;
4233
4234 cpu_buffer = iter->cpu_buffer;
4235
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02004236 raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
Steven Rostedt642edba2008-11-12 00:01:26 -05004237 rb_iter_reset(iter);
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02004238 raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004239}
Robert Richterc4f50182008-12-11 16:49:22 +01004240EXPORT_SYMBOL_GPL(ring_buffer_iter_reset);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004241
4242/**
4243 * ring_buffer_iter_empty - check if an iterator has no more to read
4244 * @iter: The iterator to check
4245 */
4246int ring_buffer_iter_empty(struct ring_buffer_iter *iter)
4247{
4248 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedt (VMware)78f7a452017-04-19 14:29:46 -04004249 struct buffer_page *reader;
4250 struct buffer_page *head_page;
4251 struct buffer_page *commit_page;
Steven Rostedt (VMware)ead6ecf2020-03-17 17:32:24 -04004252 struct buffer_page *curr_commit_page;
Steven Rostedt (VMware)78f7a452017-04-19 14:29:46 -04004253 unsigned commit;
Steven Rostedt (VMware)ead6ecf2020-03-17 17:32:24 -04004254 u64 curr_commit_ts;
4255 u64 commit_ts;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004256
4257 cpu_buffer = iter->cpu_buffer;
Steven Rostedt (VMware)78f7a452017-04-19 14:29:46 -04004258 reader = cpu_buffer->reader_page;
4259 head_page = cpu_buffer->head_page;
4260 commit_page = cpu_buffer->commit_page;
Steven Rostedt (VMware)ead6ecf2020-03-17 17:32:24 -04004261 commit_ts = commit_page->page->time_stamp;
Steven Rostedt (VMware)78f7a452017-04-19 14:29:46 -04004262
Steven Rostedt (VMware)ead6ecf2020-03-17 17:32:24 -04004263 /*
4264 * When the writer goes across pages, it issues a cmpxchg which
4265 * is a mb(), which will synchronize with the rmb here.
4266 * (see rb_tail_page_update())
4267 */
4268 smp_rmb();
4269 commit = rb_page_commit(commit_page);
4270 /* We want to make sure that the commit page doesn't change */
4271 smp_rmb();
4272
4273 /* Make sure commit page didn't change */
4274 curr_commit_page = READ_ONCE(cpu_buffer->commit_page);
4275 curr_commit_ts = READ_ONCE(curr_commit_page->page->time_stamp);
4276
4277 /* If the commit page changed, then there's more data */
4278 if (curr_commit_page != commit_page ||
4279 curr_commit_ts != commit_ts)
4280 return 0;
4281
4282 /* Still racy, as it may return a false positive, but that's OK */
Steven Rostedt (VMware)785888c2020-03-17 17:32:27 -04004283 return ((iter->head_page == commit_page && iter->head >= commit) ||
Steven Rostedt (VMware)78f7a452017-04-19 14:29:46 -04004284 (iter->head_page == reader && commit_page == head_page &&
4285 head_page->read == commit &&
4286 iter->head == rb_page_commit(cpu_buffer->reader_page)));
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004287}
Robert Richterc4f50182008-12-11 16:49:22 +01004288EXPORT_SYMBOL_GPL(ring_buffer_iter_empty);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004289
4290static void
4291rb_update_read_stamp(struct ring_buffer_per_cpu *cpu_buffer,
4292 struct ring_buffer_event *event)
4293{
4294 u64 delta;
4295
Lai Jiangshan334d4162009-04-24 11:27:05 +08004296 switch (event->type_len) {
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004297 case RINGBUF_TYPE_PADDING:
4298 return;
4299
4300 case RINGBUF_TYPE_TIME_EXTEND:
Steven Rostedt (VMware)e20044f2021-03-16 12:41:01 -04004301 delta = rb_event_time_stamp(event);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004302 cpu_buffer->read_stamp += delta;
4303 return;
4304
4305 case RINGBUF_TYPE_TIME_STAMP:
Steven Rostedt (VMware)e20044f2021-03-16 12:41:01 -04004306 delta = rb_event_time_stamp(event);
Tom Zanussidc4e2802018-01-15 20:51:40 -06004307 cpu_buffer->read_stamp = delta;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004308 return;
4309
4310 case RINGBUF_TYPE_DATA:
4311 cpu_buffer->read_stamp += event->time_delta;
4312 return;
4313
4314 default:
Steven Rostedt (VMware)da4d4012020-05-13 15:36:22 -04004315 RB_WARN_ON(cpu_buffer, 1);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004316 }
4317 return;
4318}
4319
4320static void
4321rb_update_iter_read_stamp(struct ring_buffer_iter *iter,
4322 struct ring_buffer_event *event)
4323{
4324 u64 delta;
4325
Lai Jiangshan334d4162009-04-24 11:27:05 +08004326 switch (event->type_len) {
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004327 case RINGBUF_TYPE_PADDING:
4328 return;
4329
4330 case RINGBUF_TYPE_TIME_EXTEND:
Steven Rostedt (VMware)e20044f2021-03-16 12:41:01 -04004331 delta = rb_event_time_stamp(event);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004332 iter->read_stamp += delta;
4333 return;
4334
4335 case RINGBUF_TYPE_TIME_STAMP:
Steven Rostedt (VMware)e20044f2021-03-16 12:41:01 -04004336 delta = rb_event_time_stamp(event);
Tom Zanussidc4e2802018-01-15 20:51:40 -06004337 iter->read_stamp = delta;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004338 return;
4339
4340 case RINGBUF_TYPE_DATA:
4341 iter->read_stamp += event->time_delta;
4342 return;
4343
4344 default:
Steven Rostedt (VMware)da4d4012020-05-13 15:36:22 -04004345 RB_WARN_ON(iter->cpu_buffer, 1);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004346 }
4347 return;
4348}
4349
Steven Rostedtd7690412008-10-01 00:29:53 -04004350static struct buffer_page *
4351rb_get_reader_page(struct ring_buffer_per_cpu *cpu_buffer)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004352{
Steven Rostedtd7690412008-10-01 00:29:53 -04004353 struct buffer_page *reader = NULL;
Steven Rostedt66a8cb92010-03-31 13:21:56 -04004354 unsigned long overwrite;
Steven Rostedtd7690412008-10-01 00:29:53 -04004355 unsigned long flags;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04004356 int nr_loops = 0;
Steven Rostedt77ae3652009-03-27 11:00:29 -04004357 int ret;
Steven Rostedtd7690412008-10-01 00:29:53 -04004358
Steven Rostedt3e03fb72008-11-06 00:09:43 -05004359 local_irq_save(flags);
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01004360 arch_spin_lock(&cpu_buffer->lock);
Steven Rostedtd7690412008-10-01 00:29:53 -04004361
4362 again:
Steven Rostedt818e3dd2008-10-31 09:58:35 -04004363 /*
4364 * This should normally only loop twice. But because the
4365 * start of the reader inserts an empty page, it causes
4366 * a case where we will loop three times. There should be no
4367 * reason to loop four times (that I know of).
4368 */
Steven Rostedt3e89c7bb2008-11-11 15:28:41 -05004369 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 3)) {
Steven Rostedt818e3dd2008-10-31 09:58:35 -04004370 reader = NULL;
4371 goto out;
4372 }
4373
Steven Rostedtd7690412008-10-01 00:29:53 -04004374 reader = cpu_buffer->reader_page;
4375
4376 /* If there's more to read, return this page */
Steven Rostedtbf41a152008-10-04 02:00:59 -04004377 if (cpu_buffer->reader_page->read < rb_page_size(reader))
Steven Rostedtd7690412008-10-01 00:29:53 -04004378 goto out;
4379
4380 /* Never should we have an index greater than the size */
Steven Rostedt3e89c7bb2008-11-11 15:28:41 -05004381 if (RB_WARN_ON(cpu_buffer,
4382 cpu_buffer->reader_page->read > rb_page_size(reader)))
4383 goto out;
Steven Rostedtd7690412008-10-01 00:29:53 -04004384
4385 /* check if we caught up to the tail */
4386 reader = NULL;
Steven Rostedtbf41a152008-10-04 02:00:59 -04004387 if (cpu_buffer->commit_page == cpu_buffer->reader_page)
Steven Rostedtd7690412008-10-01 00:29:53 -04004388 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004389
Steven Rostedta5fb8332012-06-28 13:35:04 -04004390 /* Don't bother swapping if the ring buffer is empty */
4391 if (rb_num_of_entries(cpu_buffer) == 0)
4392 goto out;
4393
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004394 /*
Steven Rostedtd7690412008-10-01 00:29:53 -04004395 * Reset the reader page to size zero.
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004396 */
Steven Rostedt77ae3652009-03-27 11:00:29 -04004397 local_set(&cpu_buffer->reader_page->write, 0);
4398 local_set(&cpu_buffer->reader_page->entries, 0);
4399 local_set(&cpu_buffer->reader_page->page->commit, 0);
Steven Rostedtff0ff842010-03-31 22:11:42 -04004400 cpu_buffer->reader_page->real_end = 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004401
Steven Rostedt77ae3652009-03-27 11:00:29 -04004402 spin:
4403 /*
4404 * Splice the empty reader page into the list around the head.
4405 */
4406 reader = rb_set_head_page(cpu_buffer);
Steven Rostedt54f7be52012-11-29 22:27:22 -05004407 if (!reader)
4408 goto out;
Steven Rostedt0e1ff5d2010-01-06 20:40:44 -05004409 cpu_buffer->reader_page->list.next = rb_list_head(reader->list.next);
Steven Rostedtd7690412008-10-01 00:29:53 -04004410 cpu_buffer->reader_page->list.prev = reader->list.prev;
Steven Rostedtbf41a152008-10-04 02:00:59 -04004411
Steven Rostedt3adc54f2009-03-30 15:32:01 -04004412 /*
4413 * cpu_buffer->pages just needs to point to the buffer, it
4414 * has no specific buffer page to point to. Lets move it out
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004415 * of our way so we don't accidentally swap it.
Steven Rostedt3adc54f2009-03-30 15:32:01 -04004416 */
4417 cpu_buffer->pages = reader->list.prev;
4418
Steven Rostedt77ae3652009-03-27 11:00:29 -04004419 /* The reader page will be pointing to the new head */
Qiujun Huang6689bed2020-12-25 22:03:56 +08004420 rb_set_list_to_head(&cpu_buffer->reader_page->list);
Steven Rostedtd7690412008-10-01 00:29:53 -04004421
4422 /*
Steven Rostedt66a8cb92010-03-31 13:21:56 -04004423 * We want to make sure we read the overruns after we set up our
4424 * pointers to the next object. The writer side does a
4425 * cmpxchg to cross pages which acts as the mb on the writer
4426 * side. Note, the reader will constantly fail the swap
4427 * while the writer is updating the pointers, so this
4428 * guarantees that the overwrite recorded here is the one we
4429 * want to compare with the last_overrun.
4430 */
4431 smp_mb();
4432 overwrite = local_read(&(cpu_buffer->overrun));
4433
4434 /*
Steven Rostedt77ae3652009-03-27 11:00:29 -04004435 * Here's the tricky part.
4436 *
4437 * We need to move the pointer past the header page.
4438 * But we can only do that if a writer is not currently
4439 * moving it. The page before the header page has the
4440 * flag bit '1' set if it is pointing to the page we want.
4441 * but if the writer is in the process of moving it
4442 * than it will be '2' or already moved '0'.
Steven Rostedtd7690412008-10-01 00:29:53 -04004443 */
Steven Rostedtd7690412008-10-01 00:29:53 -04004444
Steven Rostedt77ae3652009-03-27 11:00:29 -04004445 ret = rb_head_page_replace(reader, cpu_buffer->reader_page);
4446
4447 /*
4448 * If we did not convert it, then we must try again.
4449 */
4450 if (!ret)
4451 goto spin;
4452
4453 /*
Steven Rostedt (VMware)2c2b0a72018-11-29 20:32:26 -05004454 * Yay! We succeeded in replacing the page.
Steven Rostedt77ae3652009-03-27 11:00:29 -04004455 *
4456 * Now make the new head point back to the reader page.
4457 */
David Sharp5ded3dc62010-01-06 17:12:07 -08004458 rb_list_head(reader->list.next)->prev = &cpu_buffer->reader_page->list;
Qiujun Huang6689bed2020-12-25 22:03:56 +08004459 rb_inc_page(&cpu_buffer->head_page);
Steven Rostedtd7690412008-10-01 00:29:53 -04004460
Steven Rostedt (VMware)2c2b0a72018-11-29 20:32:26 -05004461 local_inc(&cpu_buffer->pages_read);
4462
Steven Rostedtd7690412008-10-01 00:29:53 -04004463 /* Finally update the reader page to the new head */
4464 cpu_buffer->reader_page = reader;
Steven Rostedt (Red Hat)b81f4722015-11-23 10:35:36 -05004465 cpu_buffer->reader_page->read = 0;
Steven Rostedtd7690412008-10-01 00:29:53 -04004466
Steven Rostedt66a8cb92010-03-31 13:21:56 -04004467 if (overwrite != cpu_buffer->last_overrun) {
4468 cpu_buffer->lost_events = overwrite - cpu_buffer->last_overrun;
4469 cpu_buffer->last_overrun = overwrite;
4470 }
4471
Steven Rostedtd7690412008-10-01 00:29:53 -04004472 goto again;
4473
4474 out:
Steven Rostedt (Red Hat)b81f4722015-11-23 10:35:36 -05004475 /* Update the read_stamp on the first event */
4476 if (reader && reader->read == 0)
4477 cpu_buffer->read_stamp = reader->page->time_stamp;
4478
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01004479 arch_spin_unlock(&cpu_buffer->lock);
Steven Rostedt3e03fb72008-11-06 00:09:43 -05004480 local_irq_restore(flags);
Steven Rostedtd7690412008-10-01 00:29:53 -04004481
4482 return reader;
4483}
4484
4485static void rb_advance_reader(struct ring_buffer_per_cpu *cpu_buffer)
4486{
4487 struct ring_buffer_event *event;
4488 struct buffer_page *reader;
4489 unsigned length;
4490
4491 reader = rb_get_reader_page(cpu_buffer);
4492
4493 /* This function should not be called when buffer is empty */
Steven Rostedt3e89c7bb2008-11-11 15:28:41 -05004494 if (RB_WARN_ON(cpu_buffer, !reader))
4495 return;
Steven Rostedtd7690412008-10-01 00:29:53 -04004496
4497 event = rb_reader_event(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004498
Steven Rostedta1863c22009-09-03 10:23:58 -04004499 if (event->type_len <= RINGBUF_TYPE_DATA_TYPE_LEN_MAX)
Steven Rostedte4906ef2009-04-30 20:49:44 -04004500 cpu_buffer->read++;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004501
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004502 rb_update_read_stamp(cpu_buffer, event);
4503
Steven Rostedtd7690412008-10-01 00:29:53 -04004504 length = rb_event_length(event);
Steven Rostedt6f807ac2008-10-04 02:00:58 -04004505 cpu_buffer->reader_page->read += length;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004506}
4507
4508static void rb_advance_iter(struct ring_buffer_iter *iter)
4509{
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004510 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004511
4512 cpu_buffer = iter->cpu_buffer;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004513
Steven Rostedt (VMware)785888c2020-03-17 17:32:27 -04004514 /* If head == next_event then we need to jump to the next event */
4515 if (iter->head == iter->next_event) {
4516 /* If the event gets overwritten again, there's nothing to do */
4517 if (rb_iter_head_event(iter) == NULL)
4518 return;
4519 }
4520
4521 iter->head = iter->next_event;
4522
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004523 /*
4524 * Check if we are at the end of the buffer.
4525 */
Steven Rostedt (VMware)785888c2020-03-17 17:32:27 -04004526 if (iter->next_event >= rb_page_size(iter->head_page)) {
Steven Rostedtea05b572009-06-03 09:30:10 -04004527 /* discarded commits can make the page empty */
4528 if (iter->head_page == cpu_buffer->commit_page)
Steven Rostedt3e89c7bb2008-11-11 15:28:41 -05004529 return;
Steven Rostedtd7690412008-10-01 00:29:53 -04004530 rb_inc_iter(iter);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004531 return;
4532 }
4533
Steven Rostedt (VMware)785888c2020-03-17 17:32:27 -04004534 rb_update_iter_read_stamp(iter, iter->event);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004535}
4536
Steven Rostedt66a8cb92010-03-31 13:21:56 -04004537static int rb_lost_events(struct ring_buffer_per_cpu *cpu_buffer)
4538{
4539 return cpu_buffer->lost_events;
4540}
4541
Steven Rostedtf83c9d02008-11-11 18:47:44 +01004542static struct ring_buffer_event *
Steven Rostedt66a8cb92010-03-31 13:21:56 -04004543rb_buffer_peek(struct ring_buffer_per_cpu *cpu_buffer, u64 *ts,
4544 unsigned long *lost_events)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004545{
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004546 struct ring_buffer_event *event;
Steven Rostedtd7690412008-10-01 00:29:53 -04004547 struct buffer_page *reader;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04004548 int nr_loops = 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004549
Tom Zanussidc4e2802018-01-15 20:51:40 -06004550 if (ts)
4551 *ts = 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004552 again:
Steven Rostedt818e3dd2008-10-31 09:58:35 -04004553 /*
Steven Rostedt69d1b832010-10-07 18:18:05 -04004554 * We repeat when a time extend is encountered.
4555 * Since the time extend is always attached to a data event,
4556 * we should never loop more than once.
4557 * (We never hit the following condition more than twice).
Steven Rostedt818e3dd2008-10-31 09:58:35 -04004558 */
Steven Rostedt69d1b832010-10-07 18:18:05 -04004559 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 2))
Steven Rostedt818e3dd2008-10-31 09:58:35 -04004560 return NULL;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04004561
Steven Rostedtd7690412008-10-01 00:29:53 -04004562 reader = rb_get_reader_page(cpu_buffer);
4563 if (!reader)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004564 return NULL;
4565
Steven Rostedtd7690412008-10-01 00:29:53 -04004566 event = rb_reader_event(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004567
Lai Jiangshan334d4162009-04-24 11:27:05 +08004568 switch (event->type_len) {
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004569 case RINGBUF_TYPE_PADDING:
Tom Zanussi2d622712009-03-22 03:30:49 -05004570 if (rb_null_event(event))
4571 RB_WARN_ON(cpu_buffer, 1);
4572 /*
4573 * Because the writer could be discarding every
4574 * event it creates (which would probably be bad)
4575 * if we were to go back to "again" then we may never
4576 * catch up, and will trigger the warn on, or lock
4577 * the box. Return the padding, and we will release
4578 * the current locks, and try again.
4579 */
Tom Zanussi2d622712009-03-22 03:30:49 -05004580 return event;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004581
4582 case RINGBUF_TYPE_TIME_EXTEND:
4583 /* Internal data, OK to advance */
Steven Rostedtd7690412008-10-01 00:29:53 -04004584 rb_advance_reader(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004585 goto again;
4586
4587 case RINGBUF_TYPE_TIME_STAMP:
Tom Zanussidc4e2802018-01-15 20:51:40 -06004588 if (ts) {
Steven Rostedt (VMware)e20044f2021-03-16 12:41:01 -04004589 *ts = rb_event_time_stamp(event);
Tom Zanussidc4e2802018-01-15 20:51:40 -06004590 ring_buffer_normalize_time_stamp(cpu_buffer->buffer,
4591 cpu_buffer->cpu, ts);
4592 }
4593 /* Internal data, OK to advance */
Steven Rostedtd7690412008-10-01 00:29:53 -04004594 rb_advance_reader(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004595 goto again;
4596
4597 case RINGBUF_TYPE_DATA:
Tom Zanussidc4e2802018-01-15 20:51:40 -06004598 if (ts && !(*ts)) {
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004599 *ts = cpu_buffer->read_stamp + event->time_delta;
Robert Richterd8eeb2d2009-07-31 14:58:04 +02004600 ring_buffer_normalize_time_stamp(cpu_buffer->buffer,
Steven Rostedt37886f62009-03-17 17:22:06 -04004601 cpu_buffer->cpu, ts);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004602 }
Steven Rostedt66a8cb92010-03-31 13:21:56 -04004603 if (lost_events)
4604 *lost_events = rb_lost_events(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004605 return event;
4606
4607 default:
Steven Rostedt (VMware)da4d4012020-05-13 15:36:22 -04004608 RB_WARN_ON(cpu_buffer, 1);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004609 }
4610
4611 return NULL;
4612}
Robert Richterc4f50182008-12-11 16:49:22 +01004613EXPORT_SYMBOL_GPL(ring_buffer_peek);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004614
Steven Rostedtf83c9d02008-11-11 18:47:44 +01004615static struct ring_buffer_event *
4616rb_iter_peek(struct ring_buffer_iter *iter, u64 *ts)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004617{
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05004618 struct trace_buffer *buffer;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004619 struct ring_buffer_per_cpu *cpu_buffer;
4620 struct ring_buffer_event *event;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04004621 int nr_loops = 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004622
Tom Zanussidc4e2802018-01-15 20:51:40 -06004623 if (ts)
4624 *ts = 0;
4625
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004626 cpu_buffer = iter->cpu_buffer;
4627 buffer = cpu_buffer->buffer;
4628
Steven Rostedt492a74f2010-01-25 15:17:47 -05004629 /*
4630 * Check if someone performed a consuming read to
4631 * the buffer. A consuming read invalidates the iterator
4632 * and we need to reset the iterator in this case.
4633 */
4634 if (unlikely(iter->cache_read != cpu_buffer->read ||
4635 iter->cache_reader_page != cpu_buffer->reader_page))
4636 rb_iter_reset(iter);
4637
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004638 again:
Steven Rostedt3c05d742010-01-26 16:14:08 -05004639 if (ring_buffer_iter_empty(iter))
4640 return NULL;
4641
Steven Rostedt818e3dd2008-10-31 09:58:35 -04004642 /*
Steven Rostedt (VMware)3d2353d2020-05-13 15:18:01 -04004643 * As the writer can mess with what the iterator is trying
4644 * to read, just give up if we fail to get an event after
4645 * three tries. The iterator is not as reliable when reading
4646 * the ring buffer with an active write as the consumer is.
4647 * Do not warn if the three failures is reached.
Steven Rostedt818e3dd2008-10-31 09:58:35 -04004648 */
Steven Rostedt (VMware)3d2353d2020-05-13 15:18:01 -04004649 if (++nr_loops > 3)
Steven Rostedt818e3dd2008-10-31 09:58:35 -04004650 return NULL;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04004651
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004652 if (rb_per_cpu_empty(cpu_buffer))
4653 return NULL;
4654
Steven Rostedt (Red Hat)10e83fd2014-07-23 19:45:12 -04004655 if (iter->head >= rb_page_size(iter->head_page)) {
Steven Rostedt3c05d742010-01-26 16:14:08 -05004656 rb_inc_iter(iter);
4657 goto again;
4658 }
4659
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004660 event = rb_iter_head_event(iter);
Steven Rostedt (VMware)3d2353d2020-05-13 15:18:01 -04004661 if (!event)
Steven Rostedt (VMware)785888c2020-03-17 17:32:27 -04004662 goto again;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004663
Lai Jiangshan334d4162009-04-24 11:27:05 +08004664 switch (event->type_len) {
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004665 case RINGBUF_TYPE_PADDING:
Tom Zanussi2d622712009-03-22 03:30:49 -05004666 if (rb_null_event(event)) {
4667 rb_inc_iter(iter);
4668 goto again;
4669 }
4670 rb_advance_iter(iter);
4671 return event;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004672
4673 case RINGBUF_TYPE_TIME_EXTEND:
4674 /* Internal data, OK to advance */
4675 rb_advance_iter(iter);
4676 goto again;
4677
4678 case RINGBUF_TYPE_TIME_STAMP:
Tom Zanussidc4e2802018-01-15 20:51:40 -06004679 if (ts) {
Steven Rostedt (VMware)e20044f2021-03-16 12:41:01 -04004680 *ts = rb_event_time_stamp(event);
Tom Zanussidc4e2802018-01-15 20:51:40 -06004681 ring_buffer_normalize_time_stamp(cpu_buffer->buffer,
4682 cpu_buffer->cpu, ts);
4683 }
4684 /* Internal data, OK to advance */
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004685 rb_advance_iter(iter);
4686 goto again;
4687
4688 case RINGBUF_TYPE_DATA:
Tom Zanussidc4e2802018-01-15 20:51:40 -06004689 if (ts && !(*ts)) {
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004690 *ts = iter->read_stamp + event->time_delta;
Steven Rostedt37886f62009-03-17 17:22:06 -04004691 ring_buffer_normalize_time_stamp(buffer,
4692 cpu_buffer->cpu, ts);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004693 }
4694 return event;
4695
4696 default:
Steven Rostedt (VMware)da4d4012020-05-13 15:36:22 -04004697 RB_WARN_ON(cpu_buffer, 1);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004698 }
4699
4700 return NULL;
4701}
Robert Richterc4f50182008-12-11 16:49:22 +01004702EXPORT_SYMBOL_GPL(ring_buffer_iter_peek);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004703
Steven Rostedt (Red Hat)289a5a22015-05-28 13:14:51 -04004704static inline bool rb_reader_lock(struct ring_buffer_per_cpu *cpu_buffer)
Steven Rostedt8d707e82009-06-16 21:22:48 -04004705{
Steven Rostedt (Red Hat)289a5a22015-05-28 13:14:51 -04004706 if (likely(!in_nmi())) {
4707 raw_spin_lock(&cpu_buffer->reader_lock);
4708 return true;
4709 }
4710
Steven Rostedt8d707e82009-06-16 21:22:48 -04004711 /*
4712 * If an NMI die dumps out the content of the ring buffer
Steven Rostedt (Red Hat)289a5a22015-05-28 13:14:51 -04004713 * trylock must be used to prevent a deadlock if the NMI
4714 * preempted a task that holds the ring buffer locks. If
4715 * we get the lock then all is fine, if not, then continue
4716 * to do the read, but this can corrupt the ring buffer,
4717 * so it must be permanently disabled from future writes.
4718 * Reading from NMI is a oneshot deal.
Steven Rostedt8d707e82009-06-16 21:22:48 -04004719 */
Steven Rostedt (Red Hat)289a5a22015-05-28 13:14:51 -04004720 if (raw_spin_trylock(&cpu_buffer->reader_lock))
4721 return true;
Steven Rostedt8d707e82009-06-16 21:22:48 -04004722
Steven Rostedt (Red Hat)289a5a22015-05-28 13:14:51 -04004723 /* Continue without locking, but disable the ring buffer */
4724 atomic_inc(&cpu_buffer->record_disabled);
4725 return false;
4726}
4727
4728static inline void
4729rb_reader_unlock(struct ring_buffer_per_cpu *cpu_buffer, bool locked)
4730{
4731 if (likely(locked))
4732 raw_spin_unlock(&cpu_buffer->reader_lock);
4733 return;
Steven Rostedt8d707e82009-06-16 21:22:48 -04004734}
4735
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004736/**
Steven Rostedtf83c9d02008-11-11 18:47:44 +01004737 * ring_buffer_peek - peek at the next event to be read
4738 * @buffer: The ring buffer to read
4739 * @cpu: The cpu to peak at
4740 * @ts: The timestamp counter of this event.
Steven Rostedt66a8cb92010-03-31 13:21:56 -04004741 * @lost_events: a variable to store if events were lost (may be NULL)
Steven Rostedtf83c9d02008-11-11 18:47:44 +01004742 *
4743 * This will return the event that will be read next, but does
4744 * not consume the data.
4745 */
4746struct ring_buffer_event *
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05004747ring_buffer_peek(struct trace_buffer *buffer, int cpu, u64 *ts,
Steven Rostedt66a8cb92010-03-31 13:21:56 -04004748 unsigned long *lost_events)
Steven Rostedtf83c9d02008-11-11 18:47:44 +01004749{
4750 struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
Steven Rostedt8aabee52009-03-12 13:13:49 -04004751 struct ring_buffer_event *event;
Steven Rostedtf83c9d02008-11-11 18:47:44 +01004752 unsigned long flags;
Steven Rostedt (Red Hat)289a5a22015-05-28 13:14:51 -04004753 bool dolock;
Steven Rostedtf83c9d02008-11-11 18:47:44 +01004754
Steven Rostedt554f7862009-03-11 22:00:13 -04004755 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04004756 return NULL;
Steven Rostedt554f7862009-03-11 22:00:13 -04004757
Tom Zanussi2d622712009-03-22 03:30:49 -05004758 again:
Steven Rostedt8d707e82009-06-16 21:22:48 -04004759 local_irq_save(flags);
Steven Rostedt (Red Hat)289a5a22015-05-28 13:14:51 -04004760 dolock = rb_reader_lock(cpu_buffer);
Steven Rostedt66a8cb92010-03-31 13:21:56 -04004761 event = rb_buffer_peek(cpu_buffer, ts, lost_events);
Robert Richter469535a2009-07-30 19:19:18 +02004762 if (event && event->type_len == RINGBUF_TYPE_PADDING)
4763 rb_advance_reader(cpu_buffer);
Steven Rostedt (Red Hat)289a5a22015-05-28 13:14:51 -04004764 rb_reader_unlock(cpu_buffer, dolock);
Steven Rostedt8d707e82009-06-16 21:22:48 -04004765 local_irq_restore(flags);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01004766
Steven Rostedt1b959e12009-09-03 10:12:13 -04004767 if (event && event->type_len == RINGBUF_TYPE_PADDING)
Tom Zanussi2d622712009-03-22 03:30:49 -05004768 goto again;
Tom Zanussi2d622712009-03-22 03:30:49 -05004769
Steven Rostedtf83c9d02008-11-11 18:47:44 +01004770 return event;
4771}
4772
Steven Rostedt (VMware)c9b7a4a2020-03-17 17:32:32 -04004773/** ring_buffer_iter_dropped - report if there are dropped events
4774 * @iter: The ring buffer iterator
4775 *
4776 * Returns true if there was dropped events since the last peek.
4777 */
4778bool ring_buffer_iter_dropped(struct ring_buffer_iter *iter)
4779{
4780 bool ret = iter->missed_events != 0;
4781
4782 iter->missed_events = 0;
4783 return ret;
4784}
4785EXPORT_SYMBOL_GPL(ring_buffer_iter_dropped);
4786
Steven Rostedtf83c9d02008-11-11 18:47:44 +01004787/**
4788 * ring_buffer_iter_peek - peek at the next event to be read
4789 * @iter: The ring buffer iterator
4790 * @ts: The timestamp counter of this event.
4791 *
4792 * This will return the event that will be read next, but does
4793 * not increment the iterator.
4794 */
4795struct ring_buffer_event *
4796ring_buffer_iter_peek(struct ring_buffer_iter *iter, u64 *ts)
4797{
4798 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
4799 struct ring_buffer_event *event;
4800 unsigned long flags;
4801
Tom Zanussi2d622712009-03-22 03:30:49 -05004802 again:
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02004803 raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01004804 event = rb_iter_peek(iter, ts);
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02004805 raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01004806
Steven Rostedt1b959e12009-09-03 10:12:13 -04004807 if (event && event->type_len == RINGBUF_TYPE_PADDING)
Tom Zanussi2d622712009-03-22 03:30:49 -05004808 goto again;
Tom Zanussi2d622712009-03-22 03:30:49 -05004809
Steven Rostedtf83c9d02008-11-11 18:47:44 +01004810 return event;
4811}
4812
4813/**
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004814 * ring_buffer_consume - return an event and consume it
4815 * @buffer: The ring buffer to get the next event from
Steven Rostedt66a8cb92010-03-31 13:21:56 -04004816 * @cpu: the cpu to read the buffer from
4817 * @ts: a variable to store the timestamp (may be NULL)
4818 * @lost_events: a variable to store if events were lost (may be NULL)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004819 *
4820 * Returns the next event in the ring buffer, and that event is consumed.
4821 * Meaning, that sequential reads will keep returning a different event,
4822 * and eventually empty the ring buffer if the producer is slower.
4823 */
4824struct ring_buffer_event *
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05004825ring_buffer_consume(struct trace_buffer *buffer, int cpu, u64 *ts,
Steven Rostedt66a8cb92010-03-31 13:21:56 -04004826 unsigned long *lost_events)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004827{
Steven Rostedt554f7862009-03-11 22:00:13 -04004828 struct ring_buffer_per_cpu *cpu_buffer;
4829 struct ring_buffer_event *event = NULL;
Steven Rostedtf83c9d02008-11-11 18:47:44 +01004830 unsigned long flags;
Steven Rostedt (Red Hat)289a5a22015-05-28 13:14:51 -04004831 bool dolock;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004832
Tom Zanussi2d622712009-03-22 03:30:49 -05004833 again:
Steven Rostedt554f7862009-03-11 22:00:13 -04004834 /* might be called in atomic */
4835 preempt_disable();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004836
Steven Rostedt554f7862009-03-11 22:00:13 -04004837 if (!cpumask_test_cpu(cpu, buffer->cpumask))
4838 goto out;
4839
4840 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt8d707e82009-06-16 21:22:48 -04004841 local_irq_save(flags);
Steven Rostedt (Red Hat)289a5a22015-05-28 13:14:51 -04004842 dolock = rb_reader_lock(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004843
Steven Rostedt66a8cb92010-03-31 13:21:56 -04004844 event = rb_buffer_peek(cpu_buffer, ts, lost_events);
4845 if (event) {
4846 cpu_buffer->lost_events = 0;
Robert Richter469535a2009-07-30 19:19:18 +02004847 rb_advance_reader(cpu_buffer);
Steven Rostedt66a8cb92010-03-31 13:21:56 -04004848 }
Steven Rostedtf83c9d02008-11-11 18:47:44 +01004849
Steven Rostedt (Red Hat)289a5a22015-05-28 13:14:51 -04004850 rb_reader_unlock(cpu_buffer, dolock);
Steven Rostedt8d707e82009-06-16 21:22:48 -04004851 local_irq_restore(flags);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01004852
Steven Rostedt554f7862009-03-11 22:00:13 -04004853 out:
4854 preempt_enable();
4855
Steven Rostedt1b959e12009-09-03 10:12:13 -04004856 if (event && event->type_len == RINGBUF_TYPE_PADDING)
Tom Zanussi2d622712009-03-22 03:30:49 -05004857 goto again;
Tom Zanussi2d622712009-03-22 03:30:49 -05004858
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004859 return event;
4860}
Robert Richterc4f50182008-12-11 16:49:22 +01004861EXPORT_SYMBOL_GPL(ring_buffer_consume);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004862
4863/**
David Miller72c9ddf2010-04-20 15:47:11 -07004864 * ring_buffer_read_prepare - Prepare for a non consuming read of the buffer
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004865 * @buffer: The ring buffer to read from
4866 * @cpu: The cpu buffer to iterate over
Douglas Anderson31b265b2019-03-08 11:32:04 -08004867 * @flags: gfp flags to use for memory allocation
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004868 *
David Miller72c9ddf2010-04-20 15:47:11 -07004869 * This performs the initial preparations necessary to iterate
4870 * through the buffer. Memory is allocated, buffer recording
4871 * is disabled, and the iterator pointer is returned to the caller.
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004872 *
Steven Rostedt (VMware)6167c202018-05-16 11:17:06 -04004873 * Disabling buffer recording prevents the reading from being
David Miller72c9ddf2010-04-20 15:47:11 -07004874 * corrupted. This is not a consuming read, so a producer is not
4875 * expected.
4876 *
4877 * After a sequence of ring_buffer_read_prepare calls, the user is
zhangwei(Jovi)d6118512013-07-15 16:32:50 +08004878 * expected to make at least one call to ring_buffer_read_prepare_sync.
David Miller72c9ddf2010-04-20 15:47:11 -07004879 * Afterwards, ring_buffer_read_start is invoked to get things going
4880 * for real.
4881 *
zhangwei(Jovi)d6118512013-07-15 16:32:50 +08004882 * This overall must be paired with ring_buffer_read_finish.
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004883 */
4884struct ring_buffer_iter *
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05004885ring_buffer_read_prepare(struct trace_buffer *buffer, int cpu, gfp_t flags)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004886{
4887 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedt8aabee52009-03-12 13:13:49 -04004888 struct ring_buffer_iter *iter;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004889
Rusty Russell9e01c1b2009-01-01 10:12:22 +10304890 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04004891 return NULL;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004892
Steven Rostedt (VMware)785888c2020-03-17 17:32:27 -04004893 iter = kzalloc(sizeof(*iter), flags);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004894 if (!iter)
Steven Rostedt8aabee52009-03-12 13:13:49 -04004895 return NULL;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004896
Steven Rostedt (VMware)785888c2020-03-17 17:32:27 -04004897 iter->event = kmalloc(BUF_MAX_DATA_SIZE, flags);
4898 if (!iter->event) {
4899 kfree(iter);
4900 return NULL;
4901 }
4902
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004903 cpu_buffer = buffer->buffers[cpu];
4904
4905 iter->cpu_buffer = cpu_buffer;
4906
Steven Rostedt (VMware)07b8b102020-03-27 16:21:22 -04004907 atomic_inc(&cpu_buffer->resize_disabled);
David Miller72c9ddf2010-04-20 15:47:11 -07004908
4909 return iter;
4910}
4911EXPORT_SYMBOL_GPL(ring_buffer_read_prepare);
4912
4913/**
4914 * ring_buffer_read_prepare_sync - Synchronize a set of prepare calls
4915 *
4916 * All previously invoked ring_buffer_read_prepare calls to prepare
4917 * iterators will be synchronized. Afterwards, read_buffer_read_start
4918 * calls on those iterators are allowed.
4919 */
4920void
4921ring_buffer_read_prepare_sync(void)
4922{
Paul E. McKenney74401722018-11-06 18:44:52 -08004923 synchronize_rcu();
David Miller72c9ddf2010-04-20 15:47:11 -07004924}
4925EXPORT_SYMBOL_GPL(ring_buffer_read_prepare_sync);
4926
4927/**
4928 * ring_buffer_read_start - start a non consuming read of the buffer
4929 * @iter: The iterator returned by ring_buffer_read_prepare
4930 *
4931 * This finalizes the startup of an iteration through the buffer.
4932 * The iterator comes from a call to ring_buffer_read_prepare and
4933 * an intervening ring_buffer_read_prepare_sync must have been
4934 * performed.
4935 *
zhangwei(Jovi)d6118512013-07-15 16:32:50 +08004936 * Must be paired with ring_buffer_read_finish.
David Miller72c9ddf2010-04-20 15:47:11 -07004937 */
4938void
4939ring_buffer_read_start(struct ring_buffer_iter *iter)
4940{
4941 struct ring_buffer_per_cpu *cpu_buffer;
4942 unsigned long flags;
4943
4944 if (!iter)
4945 return;
4946
4947 cpu_buffer = iter->cpu_buffer;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004948
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02004949 raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01004950 arch_spin_lock(&cpu_buffer->lock);
Steven Rostedt642edba2008-11-12 00:01:26 -05004951 rb_iter_reset(iter);
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01004952 arch_spin_unlock(&cpu_buffer->lock);
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02004953 raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004954}
Robert Richterc4f50182008-12-11 16:49:22 +01004955EXPORT_SYMBOL_GPL(ring_buffer_read_start);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004956
4957/**
zhangwei(Jovi)d6118512013-07-15 16:32:50 +08004958 * ring_buffer_read_finish - finish reading the iterator of the buffer
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004959 * @iter: The iterator retrieved by ring_buffer_start
4960 *
4961 * This re-enables the recording to the buffer, and frees the
4962 * iterator.
4963 */
4964void
4965ring_buffer_read_finish(struct ring_buffer_iter *iter)
4966{
4967 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
Steven Rostedt9366c1b2012-11-29 22:31:16 -05004968 unsigned long flags;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004969
Steven Rostedt659f4512012-05-14 17:02:33 -04004970 /*
4971 * Ring buffer is disabled from recording, here's a good place
Steven Rostedt9366c1b2012-11-29 22:31:16 -05004972 * to check the integrity of the ring buffer.
4973 * Must prevent readers from trying to read, as the check
4974 * clears the HEAD page and readers require it.
Steven Rostedt659f4512012-05-14 17:02:33 -04004975 */
Steven Rostedt9366c1b2012-11-29 22:31:16 -05004976 raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
Steven Rostedt659f4512012-05-14 17:02:33 -04004977 rb_check_pages(cpu_buffer);
Steven Rostedt9366c1b2012-11-29 22:31:16 -05004978 raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
Steven Rostedt659f4512012-05-14 17:02:33 -04004979
Steven Rostedt (VMware)07b8b102020-03-27 16:21:22 -04004980 atomic_dec(&cpu_buffer->resize_disabled);
Steven Rostedt (VMware)785888c2020-03-17 17:32:27 -04004981 kfree(iter->event);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004982 kfree(iter);
4983}
Robert Richterc4f50182008-12-11 16:49:22 +01004984EXPORT_SYMBOL_GPL(ring_buffer_read_finish);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004985
4986/**
Steven Rostedt (VMware)bc1a72a2020-03-17 17:32:25 -04004987 * ring_buffer_iter_advance - advance the iterator to the next location
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004988 * @iter: The ring buffer iterator
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004989 *
Steven Rostedt (VMware)bc1a72a2020-03-17 17:32:25 -04004990 * Move the location of the iterator such that the next read will
4991 * be the next location of the iterator.
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004992 */
Steven Rostedt (VMware)bc1a72a2020-03-17 17:32:25 -04004993void ring_buffer_iter_advance(struct ring_buffer_iter *iter)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004994{
Steven Rostedtf83c9d02008-11-11 18:47:44 +01004995 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
4996 unsigned long flags;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004997
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02004998 raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
Steven Rostedt7e9391c2009-09-03 10:02:09 -04004999
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04005000 rb_advance_iter(iter);
5001
Steven Rostedt (VMware)bc1a72a2020-03-17 17:32:25 -04005002 raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04005003}
Steven Rostedt (VMware)bc1a72a2020-03-17 17:32:25 -04005004EXPORT_SYMBOL_GPL(ring_buffer_iter_advance);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04005005
5006/**
5007 * ring_buffer_size - return the size of the ring buffer (in bytes)
5008 * @buffer: The ring buffer.
Fabian Frederick59e7cff2014-06-05 20:22:05 +02005009 * @cpu: The CPU to get ring buffer size from.
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04005010 */
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05005011unsigned long ring_buffer_size(struct trace_buffer *buffer, int cpu)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04005012{
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08005013 /*
5014 * Earlier, this method returned
5015 * BUF_PAGE_SIZE * buffer->nr_pages
5016 * Since the nr_pages field is now removed, we have converted this to
5017 * return the per cpu buffer value.
5018 */
5019 if (!cpumask_test_cpu(cpu, buffer->cpumask))
5020 return 0;
5021
5022 return BUF_PAGE_SIZE * buffer->buffers[cpu]->nr_pages;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04005023}
Robert Richterc4f50182008-12-11 16:49:22 +01005024EXPORT_SYMBOL_GPL(ring_buffer_size);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04005025
5026static void
5027rb_reset_cpu(struct ring_buffer_per_cpu *cpu_buffer)
5028{
Steven Rostedt77ae3652009-03-27 11:00:29 -04005029 rb_head_page_deactivate(cpu_buffer);
5030
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04005031 cpu_buffer->head_page
Steven Rostedt3adc54f2009-03-30 15:32:01 -04005032 = list_entry(cpu_buffer->pages, struct buffer_page, list);
Steven Rostedtbf41a152008-10-04 02:00:59 -04005033 local_set(&cpu_buffer->head_page->write, 0);
Steven Rostedt778c55d2009-05-01 18:44:45 -04005034 local_set(&cpu_buffer->head_page->entries, 0);
Steven Rostedtabc9b562008-12-02 15:34:06 -05005035 local_set(&cpu_buffer->head_page->page->commit, 0);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04005036
Steven Rostedt6f807ac2008-10-04 02:00:58 -04005037 cpu_buffer->head_page->read = 0;
Steven Rostedtbf41a152008-10-04 02:00:59 -04005038
5039 cpu_buffer->tail_page = cpu_buffer->head_page;
5040 cpu_buffer->commit_page = cpu_buffer->head_page;
5041
5042 INIT_LIST_HEAD(&cpu_buffer->reader_page->list);
Vaibhav Nagarnaik5040b4b2012-05-03 18:59:51 -07005043 INIT_LIST_HEAD(&cpu_buffer->new_pages);
Steven Rostedtbf41a152008-10-04 02:00:59 -04005044 local_set(&cpu_buffer->reader_page->write, 0);
Steven Rostedt778c55d2009-05-01 18:44:45 -04005045 local_set(&cpu_buffer->reader_page->entries, 0);
Steven Rostedtabc9b562008-12-02 15:34:06 -05005046 local_set(&cpu_buffer->reader_page->page->commit, 0);
Steven Rostedt6f807ac2008-10-04 02:00:58 -04005047 cpu_buffer->reader_page->read = 0;
Steven Rostedtd7690412008-10-01 00:29:53 -04005048
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -07005049 local_set(&cpu_buffer->entries_bytes, 0);
Steven Rostedt77ae3652009-03-27 11:00:29 -04005050 local_set(&cpu_buffer->overrun, 0);
Slava Pestov884bfe82011-07-15 14:23:58 -07005051 local_set(&cpu_buffer->commit_overrun, 0);
5052 local_set(&cpu_buffer->dropped_events, 0);
Steven Rostedte4906ef2009-04-30 20:49:44 -04005053 local_set(&cpu_buffer->entries, 0);
Steven Rostedtfa743952009-06-16 12:37:57 -04005054 local_set(&cpu_buffer->committing, 0);
5055 local_set(&cpu_buffer->commits, 0);
Steven Rostedt (VMware)2c2b0a72018-11-29 20:32:26 -05005056 local_set(&cpu_buffer->pages_touched, 0);
5057 local_set(&cpu_buffer->pages_read, 0);
Steven Rostedt (VMware)03329f92018-11-29 21:38:42 -05005058 cpu_buffer->last_pages_touch = 0;
Steven Rostedt (VMware)2c2b0a72018-11-29 20:32:26 -05005059 cpu_buffer->shortest_full = 0;
Steven Rostedt77ae3652009-03-27 11:00:29 -04005060 cpu_buffer->read = 0;
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -07005061 cpu_buffer->read_bytes = 0;
Steven Rostedt69507c02009-01-21 18:45:57 -05005062
Steven Rostedt (VMware)10464b42020-06-28 22:52:27 -04005063 rb_time_set(&cpu_buffer->write_stamp, 0);
5064 rb_time_set(&cpu_buffer->before_stamp, 0);
Steven Rostedt77ae3652009-03-27 11:00:29 -04005065
Steven Rostedt (VMware)8672e4942021-03-16 12:41:02 -04005066 memset(cpu_buffer->event_stamp, 0, sizeof(cpu_buffer->event_stamp));
5067
Steven Rostedt66a8cb92010-03-31 13:21:56 -04005068 cpu_buffer->lost_events = 0;
5069 cpu_buffer->last_overrun = 0;
5070
Steven Rostedt77ae3652009-03-27 11:00:29 -04005071 rb_head_page_activate(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04005072}
5073
Nicholas Pigginb23d7a5f2020-06-25 15:34:03 +10005074/* Must have disabled the cpu buffer then done a synchronize_rcu */
5075static void reset_disabled_cpu_buffer(struct ring_buffer_per_cpu *cpu_buffer)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04005076{
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04005077 unsigned long flags;
5078
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02005079 raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01005080
Steven Rostedt41b6a952009-09-02 09:59:48 -04005081 if (RB_WARN_ON(cpu_buffer, local_read(&cpu_buffer->committing)))
5082 goto out;
5083
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01005084 arch_spin_lock(&cpu_buffer->lock);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04005085
5086 rb_reset_cpu(cpu_buffer);
5087
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01005088 arch_spin_unlock(&cpu_buffer->lock);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01005089
Steven Rostedt41b6a952009-09-02 09:59:48 -04005090 out:
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02005091 raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
Nicholas Pigginb23d7a5f2020-06-25 15:34:03 +10005092}
5093
5094/**
5095 * ring_buffer_reset_cpu - reset a ring buffer per CPU buffer
5096 * @buffer: The ring buffer to reset a per cpu buffer of
5097 * @cpu: The CPU buffer to be reset
5098 */
5099void ring_buffer_reset_cpu(struct trace_buffer *buffer, int cpu)
5100{
5101 struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
5102
5103 if (!cpumask_test_cpu(cpu, buffer->cpumask))
5104 return;
5105
Gaurav Kohlibbeb9742020-10-06 15:03:53 +05305106 /* prevent another thread from changing buffer sizes */
5107 mutex_lock(&buffer->mutex);
5108
Nicholas Pigginb23d7a5f2020-06-25 15:34:03 +10005109 atomic_inc(&cpu_buffer->resize_disabled);
5110 atomic_inc(&cpu_buffer->record_disabled);
5111
5112 /* Make sure all commits have finished */
5113 synchronize_rcu();
5114
5115 reset_disabled_cpu_buffer(cpu_buffer);
Steven Rostedt41ede232009-05-01 20:26:54 -04005116
5117 atomic_dec(&cpu_buffer->record_disabled);
Steven Rostedt (VMware)07b8b102020-03-27 16:21:22 -04005118 atomic_dec(&cpu_buffer->resize_disabled);
Gaurav Kohlibbeb9742020-10-06 15:03:53 +05305119
5120 mutex_unlock(&buffer->mutex);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04005121}
Robert Richterc4f50182008-12-11 16:49:22 +01005122EXPORT_SYMBOL_GPL(ring_buffer_reset_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04005123
5124/**
Nicholas Pigginb23d7a5f2020-06-25 15:34:03 +10005125 * ring_buffer_reset_cpu - reset a ring buffer per CPU buffer
5126 * @buffer: The ring buffer to reset a per cpu buffer of
5127 * @cpu: The CPU buffer to be reset
5128 */
5129void ring_buffer_reset_online_cpus(struct trace_buffer *buffer)
5130{
5131 struct ring_buffer_per_cpu *cpu_buffer;
5132 int cpu;
5133
Gaurav Kohlibbeb9742020-10-06 15:03:53 +05305134 /* prevent another thread from changing buffer sizes */
5135 mutex_lock(&buffer->mutex);
5136
Nicholas Pigginb23d7a5f2020-06-25 15:34:03 +10005137 for_each_online_buffer_cpu(buffer, cpu) {
5138 cpu_buffer = buffer->buffers[cpu];
5139
5140 atomic_inc(&cpu_buffer->resize_disabled);
5141 atomic_inc(&cpu_buffer->record_disabled);
5142 }
5143
5144 /* Make sure all commits have finished */
5145 synchronize_rcu();
5146
5147 for_each_online_buffer_cpu(buffer, cpu) {
5148 cpu_buffer = buffer->buffers[cpu];
5149
5150 reset_disabled_cpu_buffer(cpu_buffer);
5151
5152 atomic_dec(&cpu_buffer->record_disabled);
5153 atomic_dec(&cpu_buffer->resize_disabled);
5154 }
Gaurav Kohlibbeb9742020-10-06 15:03:53 +05305155
5156 mutex_unlock(&buffer->mutex);
Nicholas Pigginb23d7a5f2020-06-25 15:34:03 +10005157}
5158
5159/**
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04005160 * ring_buffer_reset - reset a ring buffer
5161 * @buffer: The ring buffer to reset all cpu buffers
5162 */
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05005163void ring_buffer_reset(struct trace_buffer *buffer)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04005164{
Nicholas Pigginb23d7a5f2020-06-25 15:34:03 +10005165 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04005166 int cpu;
5167
Nicholas Pigginb23d7a5f2020-06-25 15:34:03 +10005168 for_each_buffer_cpu(buffer, cpu) {
5169 cpu_buffer = buffer->buffers[cpu];
5170
5171 atomic_inc(&cpu_buffer->resize_disabled);
5172 atomic_inc(&cpu_buffer->record_disabled);
5173 }
5174
5175 /* Make sure all commits have finished */
5176 synchronize_rcu();
5177
5178 for_each_buffer_cpu(buffer, cpu) {
5179 cpu_buffer = buffer->buffers[cpu];
5180
5181 reset_disabled_cpu_buffer(cpu_buffer);
5182
5183 atomic_dec(&cpu_buffer->record_disabled);
5184 atomic_dec(&cpu_buffer->resize_disabled);
5185 }
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04005186}
Robert Richterc4f50182008-12-11 16:49:22 +01005187EXPORT_SYMBOL_GPL(ring_buffer_reset);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04005188
5189/**
5190 * rind_buffer_empty - is the ring buffer empty?
5191 * @buffer: The ring buffer to test
5192 */
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05005193bool ring_buffer_empty(struct trace_buffer *buffer)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04005194{
5195 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedtd4788202009-06-17 00:39:43 -04005196 unsigned long flags;
Steven Rostedt (Red Hat)289a5a22015-05-28 13:14:51 -04005197 bool dolock;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04005198 int cpu;
Steven Rostedtd4788202009-06-17 00:39:43 -04005199 int ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04005200
5201 /* yes this is racy, but if you don't like the race, lock the buffer */
5202 for_each_buffer_cpu(buffer, cpu) {
5203 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt8d707e82009-06-16 21:22:48 -04005204 local_irq_save(flags);
Steven Rostedt (Red Hat)289a5a22015-05-28 13:14:51 -04005205 dolock = rb_reader_lock(cpu_buffer);
Steven Rostedtd4788202009-06-17 00:39:43 -04005206 ret = rb_per_cpu_empty(cpu_buffer);
Steven Rostedt (Red Hat)289a5a22015-05-28 13:14:51 -04005207 rb_reader_unlock(cpu_buffer, dolock);
Steven Rostedt8d707e82009-06-16 21:22:48 -04005208 local_irq_restore(flags);
5209
Steven Rostedtd4788202009-06-17 00:39:43 -04005210 if (!ret)
Yaowei Bai3d4e2042015-09-29 22:43:32 +08005211 return false;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04005212 }
Steven Rostedt554f7862009-03-11 22:00:13 -04005213
Yaowei Bai3d4e2042015-09-29 22:43:32 +08005214 return true;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04005215}
Robert Richterc4f50182008-12-11 16:49:22 +01005216EXPORT_SYMBOL_GPL(ring_buffer_empty);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04005217
5218/**
5219 * ring_buffer_empty_cpu - is a cpu buffer of a ring buffer empty?
5220 * @buffer: The ring buffer
5221 * @cpu: The CPU buffer to test
5222 */
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05005223bool ring_buffer_empty_cpu(struct trace_buffer *buffer, int cpu)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04005224{
5225 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedtd4788202009-06-17 00:39:43 -04005226 unsigned long flags;
Steven Rostedt (Red Hat)289a5a22015-05-28 13:14:51 -04005227 bool dolock;
Steven Rostedt8aabee52009-03-12 13:13:49 -04005228 int ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04005229
Rusty Russell9e01c1b2009-01-01 10:12:22 +10305230 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Yaowei Bai3d4e2042015-09-29 22:43:32 +08005231 return true;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04005232
5233 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt8d707e82009-06-16 21:22:48 -04005234 local_irq_save(flags);
Steven Rostedt (Red Hat)289a5a22015-05-28 13:14:51 -04005235 dolock = rb_reader_lock(cpu_buffer);
Steven Rostedt554f7862009-03-11 22:00:13 -04005236 ret = rb_per_cpu_empty(cpu_buffer);
Steven Rostedt (Red Hat)289a5a22015-05-28 13:14:51 -04005237 rb_reader_unlock(cpu_buffer, dolock);
Steven Rostedt8d707e82009-06-16 21:22:48 -04005238 local_irq_restore(flags);
Steven Rostedt554f7862009-03-11 22:00:13 -04005239
5240 return ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04005241}
Robert Richterc4f50182008-12-11 16:49:22 +01005242EXPORT_SYMBOL_GPL(ring_buffer_empty_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04005243
Steven Rostedt85bac322009-09-04 14:24:40 -04005244#ifdef CONFIG_RING_BUFFER_ALLOW_SWAP
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04005245/**
5246 * ring_buffer_swap_cpu - swap a CPU buffer between two ring buffers
5247 * @buffer_a: One buffer to swap with
5248 * @buffer_b: The other buffer to swap with
Fabian Frederick59e7cff2014-06-05 20:22:05 +02005249 * @cpu: the CPU of the buffers to swap
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04005250 *
5251 * This function is useful for tracers that want to take a "snapshot"
5252 * of a CPU buffer and has another back up buffer lying around.
5253 * it is expected that the tracer handles the cpu buffer not being
5254 * used at the moment.
5255 */
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05005256int ring_buffer_swap_cpu(struct trace_buffer *buffer_a,
5257 struct trace_buffer *buffer_b, int cpu)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04005258{
5259 struct ring_buffer_per_cpu *cpu_buffer_a;
5260 struct ring_buffer_per_cpu *cpu_buffer_b;
Steven Rostedt554f7862009-03-11 22:00:13 -04005261 int ret = -EINVAL;
5262
Rusty Russell9e01c1b2009-01-01 10:12:22 +10305263 if (!cpumask_test_cpu(cpu, buffer_a->cpumask) ||
5264 !cpumask_test_cpu(cpu, buffer_b->cpumask))
Steven Rostedt554f7862009-03-11 22:00:13 -04005265 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04005266
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08005267 cpu_buffer_a = buffer_a->buffers[cpu];
5268 cpu_buffer_b = buffer_b->buffers[cpu];
5269
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04005270 /* At least make sure the two buffers are somewhat the same */
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08005271 if (cpu_buffer_a->nr_pages != cpu_buffer_b->nr_pages)
Steven Rostedt554f7862009-03-11 22:00:13 -04005272 goto out;
5273
5274 ret = -EAGAIN;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04005275
Steven Rostedt97b17ef2009-01-21 15:24:56 -05005276 if (atomic_read(&buffer_a->record_disabled))
Steven Rostedt554f7862009-03-11 22:00:13 -04005277 goto out;
Steven Rostedt97b17ef2009-01-21 15:24:56 -05005278
5279 if (atomic_read(&buffer_b->record_disabled))
Steven Rostedt554f7862009-03-11 22:00:13 -04005280 goto out;
Steven Rostedt97b17ef2009-01-21 15:24:56 -05005281
Steven Rostedt97b17ef2009-01-21 15:24:56 -05005282 if (atomic_read(&cpu_buffer_a->record_disabled))
Steven Rostedt554f7862009-03-11 22:00:13 -04005283 goto out;
Steven Rostedt97b17ef2009-01-21 15:24:56 -05005284
5285 if (atomic_read(&cpu_buffer_b->record_disabled))
Steven Rostedt554f7862009-03-11 22:00:13 -04005286 goto out;
Steven Rostedt97b17ef2009-01-21 15:24:56 -05005287
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04005288 /*
Paul E. McKenney74401722018-11-06 18:44:52 -08005289 * We can't do a synchronize_rcu here because this
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04005290 * function can be called in atomic context.
5291 * Normally this will be called from the same CPU as cpu.
5292 * If not it's up to the caller to protect this.
5293 */
5294 atomic_inc(&cpu_buffer_a->record_disabled);
5295 atomic_inc(&cpu_buffer_b->record_disabled);
5296
Steven Rostedt98277992009-09-02 10:56:15 -04005297 ret = -EBUSY;
5298 if (local_read(&cpu_buffer_a->committing))
5299 goto out_dec;
5300 if (local_read(&cpu_buffer_b->committing))
5301 goto out_dec;
5302
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04005303 buffer_a->buffers[cpu] = cpu_buffer_b;
5304 buffer_b->buffers[cpu] = cpu_buffer_a;
5305
5306 cpu_buffer_b->buffer = buffer_a;
5307 cpu_buffer_a->buffer = buffer_b;
5308
Steven Rostedt98277992009-09-02 10:56:15 -04005309 ret = 0;
5310
5311out_dec:
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04005312 atomic_dec(&cpu_buffer_a->record_disabled);
5313 atomic_dec(&cpu_buffer_b->record_disabled);
Steven Rostedt554f7862009-03-11 22:00:13 -04005314out:
Steven Rostedt554f7862009-03-11 22:00:13 -04005315 return ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04005316}
Robert Richterc4f50182008-12-11 16:49:22 +01005317EXPORT_SYMBOL_GPL(ring_buffer_swap_cpu);
Steven Rostedt85bac322009-09-04 14:24:40 -04005318#endif /* CONFIG_RING_BUFFER_ALLOW_SWAP */
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04005319
Steven Rostedt8789a9e2008-12-02 15:34:07 -05005320/**
5321 * ring_buffer_alloc_read_page - allocate a page to read from buffer
5322 * @buffer: the buffer to allocate for.
zhangwei(Jovi)d6118512013-07-15 16:32:50 +08005323 * @cpu: the cpu buffer to allocate.
Steven Rostedt8789a9e2008-12-02 15:34:07 -05005324 *
5325 * This function is used in conjunction with ring_buffer_read_page.
5326 * When reading a full page from the ring buffer, these functions
5327 * can be used to speed up the process. The calling function should
5328 * allocate a few pages first with this function. Then when it
5329 * needs to get pages from the ring buffer, it passes the result
5330 * of this function into ring_buffer_read_page, which will swap
5331 * the page that was allocated, with the read page of the buffer.
5332 *
5333 * Returns:
Steven Rostedt (VMware)a7e52ad2017-08-02 14:20:54 -04005334 * The page allocated, or ERR_PTR
Steven Rostedt8789a9e2008-12-02 15:34:07 -05005335 */
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05005336void *ring_buffer_alloc_read_page(struct trace_buffer *buffer, int cpu)
Steven Rostedt8789a9e2008-12-02 15:34:07 -05005337{
Steven Rostedt (VMware)a7e52ad2017-08-02 14:20:54 -04005338 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedt (VMware)73a757e2017-05-01 09:35:09 -04005339 struct buffer_data_page *bpage = NULL;
5340 unsigned long flags;
Vaibhav Nagarnaik7ea59062011-05-03 17:56:42 -07005341 struct page *page;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05005342
Steven Rostedt (VMware)a7e52ad2017-08-02 14:20:54 -04005343 if (!cpumask_test_cpu(cpu, buffer->cpumask))
5344 return ERR_PTR(-ENODEV);
5345
5346 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt (VMware)73a757e2017-05-01 09:35:09 -04005347 local_irq_save(flags);
5348 arch_spin_lock(&cpu_buffer->lock);
5349
5350 if (cpu_buffer->free_page) {
5351 bpage = cpu_buffer->free_page;
5352 cpu_buffer->free_page = NULL;
5353 }
5354
5355 arch_spin_unlock(&cpu_buffer->lock);
5356 local_irq_restore(flags);
5357
5358 if (bpage)
5359 goto out;
5360
Vaibhav Nagarnaikd7ec4bf2011-06-07 17:01:42 -07005361 page = alloc_pages_node(cpu_to_node(cpu),
5362 GFP_KERNEL | __GFP_NORETRY, 0);
Vaibhav Nagarnaik7ea59062011-05-03 17:56:42 -07005363 if (!page)
Steven Rostedt (VMware)a7e52ad2017-08-02 14:20:54 -04005364 return ERR_PTR(-ENOMEM);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05005365
Vaibhav Nagarnaik7ea59062011-05-03 17:56:42 -07005366 bpage = page_address(page);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05005367
Steven Rostedt (VMware)73a757e2017-05-01 09:35:09 -04005368 out:
Steven Rostedtef7a4a12009-03-03 00:27:49 -05005369 rb_init_page(bpage);
5370
Steven Rostedt044fa782008-12-02 23:50:03 -05005371 return bpage;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05005372}
Steven Rostedtd6ce96d2009-05-05 01:15:24 -04005373EXPORT_SYMBOL_GPL(ring_buffer_alloc_read_page);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05005374
5375/**
5376 * ring_buffer_free_read_page - free an allocated read page
5377 * @buffer: the buffer the page was allocate for
Steven Rostedt (VMware)73a757e2017-05-01 09:35:09 -04005378 * @cpu: the cpu buffer the page came from
Steven Rostedt8789a9e2008-12-02 15:34:07 -05005379 * @data: the page to free
5380 *
5381 * Free a page allocated from ring_buffer_alloc_read_page.
5382 */
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05005383void ring_buffer_free_read_page(struct trace_buffer *buffer, int cpu, void *data)
Steven Rostedt8789a9e2008-12-02 15:34:07 -05005384{
Steven Rostedt (VMware)73a757e2017-05-01 09:35:09 -04005385 struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
5386 struct buffer_data_page *bpage = data;
Steven Rostedt (VMware)ae415fa2017-12-22 21:19:29 -05005387 struct page *page = virt_to_page(bpage);
Steven Rostedt (VMware)73a757e2017-05-01 09:35:09 -04005388 unsigned long flags;
5389
Steven Rostedt (VMware)ae415fa2017-12-22 21:19:29 -05005390 /* If the page is still in use someplace else, we can't reuse it */
5391 if (page_ref_count(page) > 1)
5392 goto out;
5393
Steven Rostedt (VMware)73a757e2017-05-01 09:35:09 -04005394 local_irq_save(flags);
5395 arch_spin_lock(&cpu_buffer->lock);
5396
5397 if (!cpu_buffer->free_page) {
5398 cpu_buffer->free_page = bpage;
5399 bpage = NULL;
5400 }
5401
5402 arch_spin_unlock(&cpu_buffer->lock);
5403 local_irq_restore(flags);
5404
Steven Rostedt (VMware)ae415fa2017-12-22 21:19:29 -05005405 out:
Steven Rostedt (VMware)73a757e2017-05-01 09:35:09 -04005406 free_page((unsigned long)bpage);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05005407}
Steven Rostedtd6ce96d2009-05-05 01:15:24 -04005408EXPORT_SYMBOL_GPL(ring_buffer_free_read_page);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05005409
5410/**
5411 * ring_buffer_read_page - extract a page from the ring buffer
5412 * @buffer: buffer to extract from
5413 * @data_page: the page to use allocated from ring_buffer_alloc_read_page
Steven Rostedtef7a4a12009-03-03 00:27:49 -05005414 * @len: amount to extract
Steven Rostedt8789a9e2008-12-02 15:34:07 -05005415 * @cpu: the cpu of the buffer to extract
5416 * @full: should the extraction only happen when the page is full.
5417 *
5418 * This function will pull out a page from the ring buffer and consume it.
5419 * @data_page must be the address of the variable that was returned
5420 * from ring_buffer_alloc_read_page. This is because the page might be used
5421 * to swap with a page in the ring buffer.
5422 *
5423 * for example:
zhangwei(Jovi)d6118512013-07-15 16:32:50 +08005424 * rpage = ring_buffer_alloc_read_page(buffer, cpu);
Steven Rostedt (VMware)a7e52ad2017-08-02 14:20:54 -04005425 * if (IS_ERR(rpage))
5426 * return PTR_ERR(rpage);
Steven Rostedtef7a4a12009-03-03 00:27:49 -05005427 * ret = ring_buffer_read_page(buffer, &rpage, len, cpu, 0);
Lai Jiangshan667d2412009-02-09 14:21:17 +08005428 * if (ret >= 0)
5429 * process_page(rpage, ret);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05005430 *
5431 * When @full is set, the function will not return true unless
5432 * the writer is off the reader page.
5433 *
5434 * Note: it is up to the calling functions to handle sleeps and wakeups.
5435 * The ring buffer can be used anywhere in the kernel and can not
5436 * blindly call wake_up. The layer that uses the ring buffer must be
5437 * responsible for that.
5438 *
5439 * Returns:
Lai Jiangshan667d2412009-02-09 14:21:17 +08005440 * >=0 if data has been transferred, returns the offset of consumed data.
5441 * <0 if no data has been transferred.
Steven Rostedt8789a9e2008-12-02 15:34:07 -05005442 */
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05005443int ring_buffer_read_page(struct trace_buffer *buffer,
Steven Rostedtef7a4a12009-03-03 00:27:49 -05005444 void **data_page, size_t len, int cpu, int full)
Steven Rostedt8789a9e2008-12-02 15:34:07 -05005445{
5446 struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
5447 struct ring_buffer_event *event;
Steven Rostedt044fa782008-12-02 23:50:03 -05005448 struct buffer_data_page *bpage;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05005449 struct buffer_page *reader;
Steven Rostedtff0ff842010-03-31 22:11:42 -04005450 unsigned long missed_events;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05005451 unsigned long flags;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05005452 unsigned int commit;
Lai Jiangshan667d2412009-02-09 14:21:17 +08005453 unsigned int read;
Steven Rostedt4f3640f2009-03-03 23:52:42 -05005454 u64 save_timestamp;
Lai Jiangshan667d2412009-02-09 14:21:17 +08005455 int ret = -1;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05005456
Steven Rostedt554f7862009-03-11 22:00:13 -04005457 if (!cpumask_test_cpu(cpu, buffer->cpumask))
5458 goto out;
5459
Steven Rostedt474d32b2009-03-03 19:51:40 -05005460 /*
5461 * If len is not big enough to hold the page header, then
5462 * we can not copy anything.
5463 */
5464 if (len <= BUF_PAGE_HDR_SIZE)
Steven Rostedt554f7862009-03-11 22:00:13 -04005465 goto out;
Steven Rostedt474d32b2009-03-03 19:51:40 -05005466
5467 len -= BUF_PAGE_HDR_SIZE;
5468
Steven Rostedt8789a9e2008-12-02 15:34:07 -05005469 if (!data_page)
Steven Rostedt554f7862009-03-11 22:00:13 -04005470 goto out;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05005471
Steven Rostedt044fa782008-12-02 23:50:03 -05005472 bpage = *data_page;
5473 if (!bpage)
Steven Rostedt554f7862009-03-11 22:00:13 -04005474 goto out;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05005475
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02005476 raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05005477
Steven Rostedtef7a4a12009-03-03 00:27:49 -05005478 reader = rb_get_reader_page(cpu_buffer);
5479 if (!reader)
Steven Rostedt554f7862009-03-11 22:00:13 -04005480 goto out_unlock;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05005481
Steven Rostedtef7a4a12009-03-03 00:27:49 -05005482 event = rb_reader_event(cpu_buffer);
Lai Jiangshan667d2412009-02-09 14:21:17 +08005483
Steven Rostedtef7a4a12009-03-03 00:27:49 -05005484 read = reader->read;
5485 commit = rb_page_commit(reader);
5486
Steven Rostedt66a8cb92010-03-31 13:21:56 -04005487 /* Check if any events were dropped */
Steven Rostedtff0ff842010-03-31 22:11:42 -04005488 missed_events = cpu_buffer->lost_events;
Steven Rostedt66a8cb92010-03-31 13:21:56 -04005489
Steven Rostedt8789a9e2008-12-02 15:34:07 -05005490 /*
Steven Rostedt474d32b2009-03-03 19:51:40 -05005491 * If this page has been partially read or
5492 * if len is not big enough to read the rest of the page or
5493 * a writer is still on the page, then
5494 * we must copy the data from the page to the buffer.
5495 * Otherwise, we can simply swap the page with the one passed in.
Steven Rostedt8789a9e2008-12-02 15:34:07 -05005496 */
Steven Rostedt474d32b2009-03-03 19:51:40 -05005497 if (read || (len < (commit - read)) ||
Steven Rostedtef7a4a12009-03-03 00:27:49 -05005498 cpu_buffer->reader_page == cpu_buffer->commit_page) {
Lai Jiangshan667d2412009-02-09 14:21:17 +08005499 struct buffer_data_page *rpage = cpu_buffer->reader_page->page;
Steven Rostedt474d32b2009-03-03 19:51:40 -05005500 unsigned int rpos = read;
5501 unsigned int pos = 0;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05005502 unsigned int size;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05005503
5504 if (full)
Steven Rostedt554f7862009-03-11 22:00:13 -04005505 goto out_unlock;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05005506
Steven Rostedtef7a4a12009-03-03 00:27:49 -05005507 if (len > (commit - read))
5508 len = (commit - read);
5509
Steven Rostedt69d1b832010-10-07 18:18:05 -04005510 /* Always keep the time extend and data together */
5511 size = rb_event_ts_length(event);
Steven Rostedtef7a4a12009-03-03 00:27:49 -05005512
5513 if (len < size)
Steven Rostedt554f7862009-03-11 22:00:13 -04005514 goto out_unlock;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05005515
Steven Rostedt4f3640f2009-03-03 23:52:42 -05005516 /* save the current timestamp, since the user will need it */
5517 save_timestamp = cpu_buffer->read_stamp;
5518
Steven Rostedtef7a4a12009-03-03 00:27:49 -05005519 /* Need to copy one event at a time */
5520 do {
David Sharpe1e35922010-12-22 16:38:24 -08005521 /* We need the size of one event, because
5522 * rb_advance_reader only advances by one event,
5523 * whereas rb_event_ts_length may include the size of
5524 * one or two events.
5525 * We have already ensured there's enough space if this
5526 * is a time extend. */
5527 size = rb_event_length(event);
Steven Rostedt474d32b2009-03-03 19:51:40 -05005528 memcpy(bpage->data + pos, rpage->data + rpos, size);
Steven Rostedtef7a4a12009-03-03 00:27:49 -05005529
5530 len -= size;
5531
5532 rb_advance_reader(cpu_buffer);
Steven Rostedt474d32b2009-03-03 19:51:40 -05005533 rpos = reader->read;
5534 pos += size;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05005535
Huang Ying18fab912010-07-28 14:14:01 +08005536 if (rpos >= commit)
5537 break;
5538
Steven Rostedtef7a4a12009-03-03 00:27:49 -05005539 event = rb_reader_event(cpu_buffer);
Steven Rostedt69d1b832010-10-07 18:18:05 -04005540 /* Always keep the time extend and data together */
5541 size = rb_event_ts_length(event);
David Sharpe1e35922010-12-22 16:38:24 -08005542 } while (len >= size);
Lai Jiangshan667d2412009-02-09 14:21:17 +08005543
5544 /* update bpage */
Steven Rostedtef7a4a12009-03-03 00:27:49 -05005545 local_set(&bpage->commit, pos);
Steven Rostedt4f3640f2009-03-03 23:52:42 -05005546 bpage->time_stamp = save_timestamp;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05005547
Steven Rostedt474d32b2009-03-03 19:51:40 -05005548 /* we copied everything to the beginning */
5549 read = 0;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05005550 } else {
Steven Rostedtafbab762009-05-01 19:40:05 -04005551 /* update the entry counter */
Steven Rostedt77ae3652009-03-27 11:00:29 -04005552 cpu_buffer->read += rb_page_entries(reader);
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -07005553 cpu_buffer->read_bytes += BUF_PAGE_SIZE;
Steven Rostedtafbab762009-05-01 19:40:05 -04005554
Steven Rostedt8789a9e2008-12-02 15:34:07 -05005555 /* swap the pages */
Steven Rostedt044fa782008-12-02 23:50:03 -05005556 rb_init_page(bpage);
Steven Rostedtef7a4a12009-03-03 00:27:49 -05005557 bpage = reader->page;
5558 reader->page = *data_page;
5559 local_set(&reader->write, 0);
Steven Rostedt778c55d2009-05-01 18:44:45 -04005560 local_set(&reader->entries, 0);
Steven Rostedtef7a4a12009-03-03 00:27:49 -05005561 reader->read = 0;
Steven Rostedt044fa782008-12-02 23:50:03 -05005562 *data_page = bpage;
Steven Rostedtff0ff842010-03-31 22:11:42 -04005563
5564 /*
5565 * Use the real_end for the data size,
5566 * This gives us a chance to store the lost events
5567 * on the page.
5568 */
5569 if (reader->real_end)
5570 local_set(&bpage->commit, reader->real_end);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05005571 }
Lai Jiangshan667d2412009-02-09 14:21:17 +08005572 ret = read;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05005573
Steven Rostedt66a8cb92010-03-31 13:21:56 -04005574 cpu_buffer->lost_events = 0;
Steven Rostedt2711ca22010-05-21 13:32:26 -04005575
5576 commit = local_read(&bpage->commit);
Steven Rostedt66a8cb92010-03-31 13:21:56 -04005577 /*
5578 * Set a flag in the commit field if we lost events
5579 */
Steven Rostedtff0ff842010-03-31 22:11:42 -04005580 if (missed_events) {
Steven Rostedtff0ff842010-03-31 22:11:42 -04005581 /* If there is room at the end of the page to save the
5582 * missed events, then record it there.
5583 */
5584 if (BUF_PAGE_SIZE - commit >= sizeof(missed_events)) {
5585 memcpy(&bpage->data[commit], &missed_events,
5586 sizeof(missed_events));
5587 local_add(RB_MISSED_STORED, &bpage->commit);
Steven Rostedt2711ca22010-05-21 13:32:26 -04005588 commit += sizeof(missed_events);
Steven Rostedtff0ff842010-03-31 22:11:42 -04005589 }
Steven Rostedt66a8cb92010-03-31 13:21:56 -04005590 local_add(RB_MISSED_EVENTS, &bpage->commit);
Steven Rostedtff0ff842010-03-31 22:11:42 -04005591 }
Steven Rostedt66a8cb92010-03-31 13:21:56 -04005592
Steven Rostedt2711ca22010-05-21 13:32:26 -04005593 /*
5594 * This page may be off to user land. Zero it out here.
5595 */
5596 if (commit < BUF_PAGE_SIZE)
5597 memset(&bpage->data[commit], 0, BUF_PAGE_SIZE - commit);
5598
Steven Rostedt554f7862009-03-11 22:00:13 -04005599 out_unlock:
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02005600 raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05005601
Steven Rostedt554f7862009-03-11 22:00:13 -04005602 out:
Steven Rostedt8789a9e2008-12-02 15:34:07 -05005603 return ret;
5604}
Steven Rostedtd6ce96d2009-05-05 01:15:24 -04005605EXPORT_SYMBOL_GPL(ring_buffer_read_page);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05005606
Sebastian Andrzej Siewiorb32614c2016-11-27 00:13:34 +01005607/*
5608 * We only allocate new buffers, never free them if the CPU goes down.
5609 * If we were to free the buffer, then the user would lose any trace that was in
5610 * the buffer.
5611 */
5612int trace_rb_cpu_prepare(unsigned int cpu, struct hlist_node *node)
Steven Rostedt554f7862009-03-11 22:00:13 -04005613{
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05005614 struct trace_buffer *buffer;
Steven Rostedt (Red Hat)9b94a8f2016-05-12 11:01:24 -04005615 long nr_pages_same;
5616 int cpu_i;
5617 unsigned long nr_pages;
Steven Rostedt554f7862009-03-11 22:00:13 -04005618
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05005619 buffer = container_of(node, struct trace_buffer, node);
Sebastian Andrzej Siewiorb32614c2016-11-27 00:13:34 +01005620 if (cpumask_test_cpu(cpu, buffer->cpumask))
5621 return 0;
Steven Rostedt554f7862009-03-11 22:00:13 -04005622
Sebastian Andrzej Siewiorb32614c2016-11-27 00:13:34 +01005623 nr_pages = 0;
5624 nr_pages_same = 1;
5625 /* check if all cpu sizes are same */
5626 for_each_buffer_cpu(buffer, cpu_i) {
5627 /* fill in the size from first enabled cpu */
5628 if (nr_pages == 0)
5629 nr_pages = buffer->buffers[cpu_i]->nr_pages;
5630 if (nr_pages != buffer->buffers[cpu_i]->nr_pages) {
5631 nr_pages_same = 0;
5632 break;
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08005633 }
Steven Rostedt554f7862009-03-11 22:00:13 -04005634 }
Sebastian Andrzej Siewiorb32614c2016-11-27 00:13:34 +01005635 /* allocate minimum pages, user can later expand it */
5636 if (!nr_pages_same)
5637 nr_pages = 2;
5638 buffer->buffers[cpu] =
5639 rb_allocate_cpu_buffer(buffer, nr_pages, cpu);
5640 if (!buffer->buffers[cpu]) {
5641 WARN(1, "failed to allocate ring buffer on CPU %u\n",
5642 cpu);
5643 return -ENOMEM;
5644 }
5645 smp_wmb();
5646 cpumask_set_cpu(cpu, buffer->cpumask);
5647 return 0;
Steven Rostedt554f7862009-03-11 22:00:13 -04005648}
Steven Rostedt (Red Hat)6c43e552013-03-15 11:32:53 -04005649
5650#ifdef CONFIG_RING_BUFFER_STARTUP_TEST
5651/*
5652 * This is a basic integrity check of the ring buffer.
5653 * Late in the boot cycle this test will run when configured in.
5654 * It will kick off a thread per CPU that will go into a loop
5655 * writing to the per cpu ring buffer various sizes of data.
5656 * Some of the data will be large items, some small.
5657 *
5658 * Another thread is created that goes into a spin, sending out
5659 * IPIs to the other CPUs to also write into the ring buffer.
5660 * this is to test the nesting ability of the buffer.
5661 *
5662 * Basic stats are recorded and reported. If something in the
5663 * ring buffer should happen that's not expected, a big warning
5664 * is displayed and all ring buffers are disabled.
5665 */
5666static struct task_struct *rb_threads[NR_CPUS] __initdata;
5667
5668struct rb_test_data {
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05005669 struct trace_buffer *buffer;
Steven Rostedt (Red Hat)6c43e552013-03-15 11:32:53 -04005670 unsigned long events;
5671 unsigned long bytes_written;
5672 unsigned long bytes_alloc;
5673 unsigned long bytes_dropped;
5674 unsigned long events_nested;
5675 unsigned long bytes_written_nested;
5676 unsigned long bytes_alloc_nested;
5677 unsigned long bytes_dropped_nested;
5678 int min_size_nested;
5679 int max_size_nested;
5680 int max_size;
5681 int min_size;
5682 int cpu;
5683 int cnt;
5684};
5685
5686static struct rb_test_data rb_data[NR_CPUS] __initdata;
5687
5688/* 1 meg per cpu */
5689#define RB_TEST_BUFFER_SIZE 1048576
5690
5691static char rb_string[] __initdata =
5692 "abcdefghijklmnopqrstuvwxyz1234567890!@#$%^&*()?+\\"
5693 "?+|:';\",.<>/?abcdefghijklmnopqrstuvwxyz1234567890"
5694 "!@#$%^&*()?+\\?+|:';\",.<>/?abcdefghijklmnopqrstuv";
5695
5696static bool rb_test_started __initdata;
5697
5698struct rb_item {
5699 int size;
5700 char str[];
5701};
5702
5703static __init int rb_write_something(struct rb_test_data *data, bool nested)
5704{
5705 struct ring_buffer_event *event;
5706 struct rb_item *item;
5707 bool started;
5708 int event_len;
5709 int size;
5710 int len;
5711 int cnt;
5712
5713 /* Have nested writes different that what is written */
5714 cnt = data->cnt + (nested ? 27 : 0);
5715
5716 /* Multiply cnt by ~e, to make some unique increment */
YueHaibing40ed29b2018-09-23 12:11:33 +00005717 size = (cnt * 68 / 25) % (sizeof(rb_string) - 1);
Steven Rostedt (Red Hat)6c43e552013-03-15 11:32:53 -04005718
5719 len = size + sizeof(struct rb_item);
5720
5721 started = rb_test_started;
5722 /* read rb_test_started before checking buffer enabled */
5723 smp_rmb();
5724
5725 event = ring_buffer_lock_reserve(data->buffer, len);
5726 if (!event) {
5727 /* Ignore dropped events before test starts. */
5728 if (started) {
5729 if (nested)
5730 data->bytes_dropped += len;
5731 else
5732 data->bytes_dropped_nested += len;
5733 }
5734 return len;
5735 }
5736
5737 event_len = ring_buffer_event_length(event);
5738
5739 if (RB_WARN_ON(data->buffer, event_len < len))
5740 goto out;
5741
5742 item = ring_buffer_event_data(event);
5743 item->size = size;
5744 memcpy(item->str, rb_string, size);
5745
5746 if (nested) {
5747 data->bytes_alloc_nested += event_len;
5748 data->bytes_written_nested += len;
5749 data->events_nested++;
5750 if (!data->min_size_nested || len < data->min_size_nested)
5751 data->min_size_nested = len;
5752 if (len > data->max_size_nested)
5753 data->max_size_nested = len;
5754 } else {
5755 data->bytes_alloc += event_len;
5756 data->bytes_written += len;
5757 data->events++;
5758 if (!data->min_size || len < data->min_size)
5759 data->max_size = len;
5760 if (len > data->max_size)
5761 data->max_size = len;
5762 }
5763
5764 out:
5765 ring_buffer_unlock_commit(data->buffer, event);
5766
5767 return 0;
5768}
5769
5770static __init int rb_test(void *arg)
5771{
5772 struct rb_test_data *data = arg;
5773
5774 while (!kthread_should_stop()) {
5775 rb_write_something(data, false);
5776 data->cnt++;
5777
5778 set_current_state(TASK_INTERRUPTIBLE);
5779 /* Now sleep between a min of 100-300us and a max of 1ms */
5780 usleep_range(((data->cnt % 3) + 1) * 100, 1000);
5781 }
5782
5783 return 0;
5784}
5785
5786static __init void rb_ipi(void *ignore)
5787{
5788 struct rb_test_data *data;
5789 int cpu = smp_processor_id();
5790
5791 data = &rb_data[cpu];
5792 rb_write_something(data, true);
5793}
5794
5795static __init int rb_hammer_test(void *arg)
5796{
5797 while (!kthread_should_stop()) {
5798
5799 /* Send an IPI to all cpus to write data! */
5800 smp_call_function(rb_ipi, NULL, 1);
5801 /* No sleep, but for non preempt, let others run */
5802 schedule();
5803 }
5804
5805 return 0;
5806}
5807
5808static __init int test_ringbuffer(void)
5809{
5810 struct task_struct *rb_hammer;
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05005811 struct trace_buffer *buffer;
Steven Rostedt (Red Hat)6c43e552013-03-15 11:32:53 -04005812 int cpu;
5813 int ret = 0;
5814
Steven Rostedt (VMware)a3566462019-12-02 16:25:27 -05005815 if (security_locked_down(LOCKDOWN_TRACEFS)) {
Stephen Rothwellee195452019-12-06 09:25:03 +11005816 pr_warn("Lockdown is enabled, skipping ring buffer tests\n");
Steven Rostedt (VMware)a3566462019-12-02 16:25:27 -05005817 return 0;
5818 }
5819
Steven Rostedt (Red Hat)6c43e552013-03-15 11:32:53 -04005820 pr_info("Running ring buffer tests...\n");
5821
5822 buffer = ring_buffer_alloc(RB_TEST_BUFFER_SIZE, RB_FL_OVERWRITE);
5823 if (WARN_ON(!buffer))
5824 return 0;
5825
5826 /* Disable buffer so that threads can't write to it yet */
5827 ring_buffer_record_off(buffer);
5828
5829 for_each_online_cpu(cpu) {
5830 rb_data[cpu].buffer = buffer;
5831 rb_data[cpu].cpu = cpu;
5832 rb_data[cpu].cnt = cpu;
5833 rb_threads[cpu] = kthread_create(rb_test, &rb_data[cpu],
5834 "rbtester/%d", cpu);
Wei Yongjun62277de2016-06-17 17:33:59 +00005835 if (WARN_ON(IS_ERR(rb_threads[cpu]))) {
Steven Rostedt (Red Hat)6c43e552013-03-15 11:32:53 -04005836 pr_cont("FAILED\n");
Wei Yongjun62277de2016-06-17 17:33:59 +00005837 ret = PTR_ERR(rb_threads[cpu]);
Steven Rostedt (Red Hat)6c43e552013-03-15 11:32:53 -04005838 goto out_free;
5839 }
5840
5841 kthread_bind(rb_threads[cpu], cpu);
5842 wake_up_process(rb_threads[cpu]);
5843 }
5844
5845 /* Now create the rb hammer! */
5846 rb_hammer = kthread_run(rb_hammer_test, NULL, "rbhammer");
Wei Yongjun62277de2016-06-17 17:33:59 +00005847 if (WARN_ON(IS_ERR(rb_hammer))) {
Steven Rostedt (Red Hat)6c43e552013-03-15 11:32:53 -04005848 pr_cont("FAILED\n");
Wei Yongjun62277de2016-06-17 17:33:59 +00005849 ret = PTR_ERR(rb_hammer);
Steven Rostedt (Red Hat)6c43e552013-03-15 11:32:53 -04005850 goto out_free;
5851 }
5852
5853 ring_buffer_record_on(buffer);
5854 /*
5855 * Show buffer is enabled before setting rb_test_started.
5856 * Yes there's a small race window where events could be
5857 * dropped and the thread wont catch it. But when a ring
5858 * buffer gets enabled, there will always be some kind of
5859 * delay before other CPUs see it. Thus, we don't care about
5860 * those dropped events. We care about events dropped after
5861 * the threads see that the buffer is active.
5862 */
5863 smp_wmb();
5864 rb_test_started = true;
5865
5866 set_current_state(TASK_INTERRUPTIBLE);
5867 /* Just run for 10 seconds */;
5868 schedule_timeout(10 * HZ);
5869
5870 kthread_stop(rb_hammer);
5871
5872 out_free:
5873 for_each_online_cpu(cpu) {
5874 if (!rb_threads[cpu])
5875 break;
5876 kthread_stop(rb_threads[cpu]);
5877 }
5878 if (ret) {
5879 ring_buffer_free(buffer);
5880 return ret;
5881 }
5882
5883 /* Report! */
5884 pr_info("finished\n");
5885 for_each_online_cpu(cpu) {
5886 struct ring_buffer_event *event;
5887 struct rb_test_data *data = &rb_data[cpu];
5888 struct rb_item *item;
5889 unsigned long total_events;
5890 unsigned long total_dropped;
5891 unsigned long total_written;
5892 unsigned long total_alloc;
5893 unsigned long total_read = 0;
5894 unsigned long total_size = 0;
5895 unsigned long total_len = 0;
5896 unsigned long total_lost = 0;
5897 unsigned long lost;
5898 int big_event_size;
5899 int small_event_size;
5900
5901 ret = -1;
5902
5903 total_events = data->events + data->events_nested;
5904 total_written = data->bytes_written + data->bytes_written_nested;
5905 total_alloc = data->bytes_alloc + data->bytes_alloc_nested;
5906 total_dropped = data->bytes_dropped + data->bytes_dropped_nested;
5907
5908 big_event_size = data->max_size + data->max_size_nested;
5909 small_event_size = data->min_size + data->min_size_nested;
5910
5911 pr_info("CPU %d:\n", cpu);
5912 pr_info(" events: %ld\n", total_events);
5913 pr_info(" dropped bytes: %ld\n", total_dropped);
5914 pr_info(" alloced bytes: %ld\n", total_alloc);
5915 pr_info(" written bytes: %ld\n", total_written);
5916 pr_info(" biggest event: %d\n", big_event_size);
5917 pr_info(" smallest event: %d\n", small_event_size);
5918
5919 if (RB_WARN_ON(buffer, total_dropped))
5920 break;
5921
5922 ret = 0;
5923
5924 while ((event = ring_buffer_consume(buffer, cpu, NULL, &lost))) {
5925 total_lost += lost;
5926 item = ring_buffer_event_data(event);
5927 total_len += ring_buffer_event_length(event);
5928 total_size += item->size + sizeof(struct rb_item);
5929 if (memcmp(&item->str[0], rb_string, item->size) != 0) {
5930 pr_info("FAILED!\n");
5931 pr_info("buffer had: %.*s\n", item->size, item->str);
5932 pr_info("expected: %.*s\n", item->size, rb_string);
5933 RB_WARN_ON(buffer, 1);
5934 ret = -1;
5935 break;
5936 }
5937 total_read++;
5938 }
5939 if (ret)
5940 break;
5941
5942 ret = -1;
5943
5944 pr_info(" read events: %ld\n", total_read);
5945 pr_info(" lost events: %ld\n", total_lost);
5946 pr_info(" total events: %ld\n", total_lost + total_read);
5947 pr_info(" recorded len bytes: %ld\n", total_len);
5948 pr_info(" recorded size bytes: %ld\n", total_size);
5949 if (total_lost)
5950 pr_info(" With dropped events, record len and size may not match\n"
5951 " alloced and written from above\n");
5952 if (!total_lost) {
5953 if (RB_WARN_ON(buffer, total_len != total_alloc ||
5954 total_size != total_written))
5955 break;
5956 }
5957 if (RB_WARN_ON(buffer, total_lost + total_read != total_events))
5958 break;
5959
5960 ret = 0;
5961 }
5962 if (!ret)
5963 pr_info("Ring buffer PASSED!\n");
5964
5965 ring_buffer_free(buffer);
5966 return 0;
5967}
5968
5969late_initcall(test_ringbuffer);
5970#endif /* CONFIG_RING_BUFFER_STARTUP_TEST */