Merge tag 'sched_ext-for-6.12' of git://git.kernel.org/pub/scm/linux/kernel/git/tj...
[linux-2.6-block.git] / kernel / trace / ring_buffer.c
CommitLineData
bcea3f96 1// SPDX-License-Identifier: GPL-2.0
7a8e76a3
SR
2/*
3 * Generic ring buffer
4 *
5 * Copyright (C) 2008 Steven Rostedt <srostedt@redhat.com>
6 */
28575c61 7#include <linux/trace_recursion.h>
af658dca 8#include <linux/trace_events.h>
7a8e76a3 9#include <linux/ring_buffer.h>
14131f2f 10#include <linux/trace_clock.h>
e6017571 11#include <linux/sched/clock.h>
117c3920 12#include <linux/cacheflush.h>
0b07436d 13#include <linux/trace_seq.h>
7a8e76a3 14#include <linux/spinlock.h>
15693458 15#include <linux/irq_work.h>
a356646a 16#include <linux/security.h>
7a8e76a3 17#include <linux/uaccess.h>
a81bd80a 18#include <linux/hardirq.h>
6c43e554 19#include <linux/kthread.h> /* for self test */
7a8e76a3
SR
20#include <linux/module.h>
21#include <linux/percpu.h>
22#include <linux/mutex.h>
6c43e554 23#include <linux/delay.h>
5a0e3ad6 24#include <linux/slab.h>
7a8e76a3
SR
25#include <linux/init.h>
26#include <linux/hash.h>
27#include <linux/list.h>
554f786e 28#include <linux/cpu.h>
927e56db 29#include <linux/oom.h>
117c3920 30#include <linux/mm.h>
7a8e76a3 31
c84897c0 32#include <asm/local64.h>
79615760 33#include <asm/local.h>
182e9f5f 34
6695da58
SRG
35/*
36 * The "absolute" timestamp in the buffer is only 59 bits.
37 * If a clock has the 5 MSBs set, it needs to be saved and
38 * reinserted.
39 */
40#define TS_MSB (0xf8ULL << 56)
41#define ABS_TS_MASK (~TS_MSB)
42
83f40318
VN
43static void update_pages_handler(struct work_struct *work);
44
d1b182a8
SR
45/*
46 * The ring buffer header is special. We must manually up keep it.
47 */
48int ring_buffer_print_entry_header(struct trace_seq *s)
49{
c0cd93aa
SRRH
50 trace_seq_puts(s, "# compressed entry header\n");
51 trace_seq_puts(s, "\ttype_len : 5 bits\n");
52 trace_seq_puts(s, "\ttime_delta : 27 bits\n");
53 trace_seq_puts(s, "\tarray : 32 bits\n");
54 trace_seq_putc(s, '\n');
55 trace_seq_printf(s, "\tpadding : type == %d\n",
56 RINGBUF_TYPE_PADDING);
57 trace_seq_printf(s, "\ttime_extend : type == %d\n",
58 RINGBUF_TYPE_TIME_EXTEND);
dc4e2801
TZ
59 trace_seq_printf(s, "\ttime_stamp : type == %d\n",
60 RINGBUF_TYPE_TIME_STAMP);
c0cd93aa
SRRH
61 trace_seq_printf(s, "\tdata max type_len == %d\n",
62 RINGBUF_TYPE_DATA_TYPE_LEN_MAX);
63
64 return !trace_seq_has_overflowed(s);
d1b182a8
SR
65}
66
5cc98548
SR
67/*
68 * The ring buffer is made up of a list of pages. A separate list of pages is
69 * allocated for each CPU. A writer may only write to a buffer that is
70 * associated with the CPU it is currently executing on. A reader may read
71 * from any per cpu buffer.
72 *
73 * The reader is special. For each per cpu buffer, the reader has its own
74 * reader page. When a reader has read the entire reader page, this reader
75 * page is swapped with another page in the ring buffer.
76 *
77 * Now, as long as the writer is off the reader page, the reader can do what
78 * ever it wants with that page. The writer will never write to that page
79 * again (as long as it is out of the ring buffer).
80 *
81 * Here's some silly ASCII art.
82 *
83 * +------+
84 * |reader| RING BUFFER
85 * |page |
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 * |reader| RING BUFFER
107 * |page |------------------v
108 * +------+ +---+ +---+ +---+
109 * ^ | |-->| |-->| |
110 * | +---+ +---+ +---+
111 * | |
112 * | |
113 * +------------------------------+
114 *
115 *
116 * +------+
117 * |buffer| RING BUFFER
118 * |page |------------------v
119 * +------+ +---+ +---+ +---+
120 * ^ | | | |-->| |
121 * | New +---+ +---+ +---+
122 * | Reader------^ |
123 * | page |
124 * +------------------------------+
125 *
126 *
127 * After we make this swap, the reader can hand this page off to the splice
128 * code and be done with it. It can even allocate a new page if it needs to
129 * and swap that into the ring buffer.
130 *
131 * We will be using cmpxchg soon to make all this lockless.
132 *
133 */
134
499e5470
SR
135/* Used for individual buffers (after the counter) */
136#define RB_BUFFER_OFF (1 << 20)
a3583244 137
499e5470 138#define BUF_PAGE_HDR_SIZE offsetof(struct buffer_data_page, data)
033601a3 139
e3d6bf0a 140#define RB_EVNT_HDR_SIZE (offsetof(struct ring_buffer_event, array))
67d34724 141#define RB_ALIGNMENT 4U
334d4169 142#define RB_MAX_SMALL_DATA (RB_ALIGNMENT * RINGBUF_TYPE_DATA_TYPE_LEN_MAX)
c7b09308 143#define RB_EVNT_MIN_SIZE 8U /* two 32bit words */
adab66b7
SRV
144
145#ifndef CONFIG_HAVE_64BIT_ALIGNED_ACCESS
146# define RB_FORCE_8BYTE_ALIGNMENT 0
147# define RB_ARCH_ALIGNMENT RB_ALIGNMENT
148#else
149# define RB_FORCE_8BYTE_ALIGNMENT 1
150# define RB_ARCH_ALIGNMENT 8U
151#endif
152
153#define RB_ALIGN_DATA __aligned(RB_ARCH_ALIGNMENT)
649508f6 154
334d4169
LJ
155/* define RINGBUF_TYPE_DATA for 'case RINGBUF_TYPE_DATA:' */
156#define RINGBUF_TYPE_DATA 0 ... RINGBUF_TYPE_DATA_TYPE_LEN_MAX
7a8e76a3
SR
157
158enum {
159 RB_LEN_TIME_EXTEND = 8,
dc4e2801 160 RB_LEN_TIME_STAMP = 8,
7a8e76a3
SR
161};
162
69d1b839
SR
163#define skip_time_extend(event) \
164 ((struct ring_buffer_event *)((char *)event + RB_LEN_TIME_EXTEND))
165
dc4e2801
TZ
166#define extended_time(event) \
167 (event->type_len >= RINGBUF_TYPE_TIME_EXTEND)
168
bc92b956 169static inline bool rb_null_event(struct ring_buffer_event *event)
2d622719 170{
a1863c21 171 return event->type_len == RINGBUF_TYPE_PADDING && !event->time_delta;
2d622719
TZ
172}
173
174static void rb_event_set_padding(struct ring_buffer_event *event)
175{
a1863c21 176 /* padding has a NULL time_delta */
334d4169 177 event->type_len = RINGBUF_TYPE_PADDING;
2d622719
TZ
178 event->time_delta = 0;
179}
180
34a148bf 181static unsigned
2d622719 182rb_event_data_length(struct ring_buffer_event *event)
7a8e76a3
SR
183{
184 unsigned length;
185
334d4169
LJ
186 if (event->type_len)
187 length = event->type_len * RB_ALIGNMENT;
2d622719
TZ
188 else
189 length = event->array[0];
190 return length + RB_EVNT_HDR_SIZE;
191}
192
69d1b839
SR
193/*
194 * Return the length of the given event. Will return
195 * the length of the time extend if the event is a
196 * time extend.
197 */
198static inline unsigned
2d622719
TZ
199rb_event_length(struct ring_buffer_event *event)
200{
334d4169 201 switch (event->type_len) {
7a8e76a3 202 case RINGBUF_TYPE_PADDING:
2d622719
TZ
203 if (rb_null_event(event))
204 /* undefined */
205 return -1;
334d4169 206 return event->array[0] + RB_EVNT_HDR_SIZE;
7a8e76a3
SR
207
208 case RINGBUF_TYPE_TIME_EXTEND:
209 return RB_LEN_TIME_EXTEND;
210
211 case RINGBUF_TYPE_TIME_STAMP:
212 return RB_LEN_TIME_STAMP;
213
214 case RINGBUF_TYPE_DATA:
2d622719 215 return rb_event_data_length(event);
7a8e76a3 216 default:
da4d401a 217 WARN_ON_ONCE(1);
7a8e76a3
SR
218 }
219 /* not hit */
220 return 0;
221}
222
69d1b839
SR
223/*
224 * Return total length of time extend and data,
225 * or just the event length for all other events.
226 */
227static inline unsigned
228rb_event_ts_length(struct ring_buffer_event *event)
229{
230 unsigned len = 0;
231
dc4e2801 232 if (extended_time(event)) {
69d1b839
SR
233 /* time extends include the data event after it */
234 len = RB_LEN_TIME_EXTEND;
235 event = skip_time_extend(event);
236 }
237 return len + rb_event_length(event);
238}
239
7a8e76a3
SR
240/**
241 * ring_buffer_event_length - return the length of the event
242 * @event: the event to get the length of
69d1b839
SR
243 *
244 * Returns the size of the data load of a data event.
245 * If the event is something other than a data event, it
246 * returns the size of the event itself. With the exception
247 * of a TIME EXTEND, where it still returns the size of the
248 * data load of the data event after it.
7a8e76a3
SR
249 */
250unsigned ring_buffer_event_length(struct ring_buffer_event *event)
251{
69d1b839
SR
252 unsigned length;
253
dc4e2801 254 if (extended_time(event))
69d1b839
SR
255 event = skip_time_extend(event);
256
257 length = rb_event_length(event);
334d4169 258 if (event->type_len > RINGBUF_TYPE_DATA_TYPE_LEN_MAX)
465634ad
RR
259 return length;
260 length -= RB_EVNT_HDR_SIZE;
261 if (length > RB_MAX_SMALL_DATA + sizeof(event->array[0]))
262 length -= sizeof(event->array[0]);
263 return length;
7a8e76a3 264}
c4f50183 265EXPORT_SYMBOL_GPL(ring_buffer_event_length);
7a8e76a3
SR
266
267/* inline for ring buffer fast paths */
929ddbf3 268static __always_inline void *
7a8e76a3
SR
269rb_event_data(struct ring_buffer_event *event)
270{
dc4e2801 271 if (extended_time(event))
69d1b839 272 event = skip_time_extend(event);
da4d401a 273 WARN_ON_ONCE(event->type_len > RINGBUF_TYPE_DATA_TYPE_LEN_MAX);
7a8e76a3 274 /* If length is in len field, then array[0] has the data */
334d4169 275 if (event->type_len)
7a8e76a3
SR
276 return (void *)&event->array[0];
277 /* Otherwise length is in array[0] and array[1] has the data */
278 return (void *)&event->array[1];
279}
280
281/**
282 * ring_buffer_event_data - return the data of the event
283 * @event: the event to get the data from
284 */
285void *ring_buffer_event_data(struct ring_buffer_event *event)
286{
287 return rb_event_data(event);
288}
c4f50183 289EXPORT_SYMBOL_GPL(ring_buffer_event_data);
7a8e76a3
SR
290
291#define for_each_buffer_cpu(buffer, cpu) \
9e01c1b7 292 for_each_cpu(cpu, buffer->cpumask)
7a8e76a3 293
b23d7a5f
NP
294#define for_each_online_buffer_cpu(buffer, cpu) \
295 for_each_cpu_and(cpu, buffer->cpumask, cpu_online_mask)
296
7a8e76a3
SR
297#define TS_SHIFT 27
298#define TS_MASK ((1ULL << TS_SHIFT) - 1)
299#define TS_DELTA_TEST (~TS_MASK)
300
e20044f7
SRV
301static u64 rb_event_time_stamp(struct ring_buffer_event *event)
302{
303 u64 ts;
304
305 ts = event->array[0];
306 ts <<= TS_SHIFT;
307 ts += event->time_delta;
308
309 return ts;
310}
311
66a8cb95
SR
312/* Flag when events were overwritten */
313#define RB_MISSED_EVENTS (1 << 31)
ff0ff84a
SR
314/* Missed count stored at end */
315#define RB_MISSED_STORED (1 << 30)
66a8cb95 316
fe832be0
SRG
317#define RB_MISSED_MASK (3 << 30)
318
abc9b56d 319struct buffer_data_page {
e4c2ce82 320 u64 time_stamp; /* page time stamp */
c3706f00 321 local_t commit; /* write committed index */
649508f6 322 unsigned char data[] RB_ALIGN_DATA; /* data of buffer page */
abc9b56d
SR
323};
324
bce761d7
TSV
325struct buffer_data_read_page {
326 unsigned order; /* order of the page */
327 struct buffer_data_page *data; /* actual data, stored in this page */
328};
329
77ae365e
SR
330/*
331 * Note, the buffer_page list must be first. The buffer pages
332 * are allocated in cache lines, which means that each buffer
333 * page will be at the beginning of a cache line, and thus
334 * the least significant bits will be zero. We use this to
335 * add flags in the list struct pointers, to make the ring buffer
336 * lockless.
337 */
abc9b56d 338struct buffer_page {
778c55d4 339 struct list_head list; /* list of buffer pages */
abc9b56d 340 local_t write; /* index for next write */
6f807acd 341 unsigned read; /* index for next read */
778c55d4 342 local_t entries; /* entries on this page */
ff0ff84a 343 unsigned long real_end; /* real end of data */
f9b94daa 344 unsigned order; /* order of the page */
117c3920 345 u32 id; /* ID for external mapping */
abc9b56d 346 struct buffer_data_page *page; /* Actual data page */
7a8e76a3
SR
347};
348
77ae365e
SR
349/*
350 * The buffer page counters, write and entries, must be reset
351 * atomically when crossing page boundaries. To synchronize this
352 * update, two counters are inserted into the number. One is
353 * the actual counter for the write position or count on the page.
354 *
355 * The other is a counter of updaters. Before an update happens
356 * the update partition of the counter is incremented. This will
357 * allow the updater to update the counter atomically.
358 *
359 * The counter is 20 bits, and the state data is 12.
360 */
361#define RB_WRITE_MASK 0xfffff
362#define RB_WRITE_INTCNT (1 << 20)
363
044fa782 364static void rb_init_page(struct buffer_data_page *bpage)
abc9b56d 365{
044fa782 366 local_set(&bpage->commit, 0);
abc9b56d
SR
367}
368
45d99ea4
ZY
369static __always_inline unsigned int rb_page_commit(struct buffer_page *bpage)
370{
371 return local_read(&bpage->page->commit);
372}
373
34a148bf 374static void free_buffer_page(struct buffer_page *bpage)
ed56829c 375{
f9b94daa 376 free_pages((unsigned long)bpage->page, bpage->order);
e4c2ce82 377 kfree(bpage);
ed56829c
SR
378}
379
7a8e76a3
SR
380/*
381 * We need to fit the time_stamp delta into 27 bits.
382 */
bc92b956 383static inline bool test_time_stamp(u64 delta)
7a8e76a3 384{
bc92b956 385 return !!(delta & TS_DELTA_TEST);
7a8e76a3
SR
386}
387
15693458
SRRH
388struct rb_irq_work {
389 struct irq_work work;
390 wait_queue_head_t waiters;
1e0d6714 391 wait_queue_head_t full_waiters;
b70f2938 392 atomic_t seq;
15693458 393 bool waiters_pending;
1e0d6714
SRRH
394 bool full_waiters_pending;
395 bool wakeup_full;
15693458
SRRH
396};
397
fcc742ea
SRRH
398/*
399 * Structure to hold event state and handle nested events.
400 */
401struct rb_event_info {
402 u64 ts;
403 u64 delta;
58fbc3c6
SRV
404 u64 before;
405 u64 after;
fcc742ea
SRRH
406 unsigned long length;
407 struct buffer_page *tail_page;
408 int add_timestamp;
409};
410
a389d86f
SRV
411/*
412 * Used for the add_timestamp
413 * NONE
7c4b4a51
SRV
414 * EXTEND - wants a time extend
415 * ABSOLUTE - the buffer requests all events to have absolute time stamps
a389d86f
SRV
416 * FORCE - force a full time stamp.
417 */
418enum {
7c4b4a51
SRV
419 RB_ADD_STAMP_NONE = 0,
420 RB_ADD_STAMP_EXTEND = BIT(1),
421 RB_ADD_STAMP_ABSOLUTE = BIT(2),
422 RB_ADD_STAMP_FORCE = BIT(3)
a389d86f 423};
a497adb4
SRRH
424/*
425 * Used for which event context the event is in.
b02414c8
SRV
426 * TRANSITION = 0
427 * NMI = 1
428 * IRQ = 2
429 * SOFTIRQ = 3
430 * NORMAL = 4
a497adb4
SRRH
431 *
432 * See trace_recursive_lock() comment below for more details.
433 */
434enum {
b02414c8 435 RB_CTX_TRANSITION,
a497adb4
SRRH
436 RB_CTX_NMI,
437 RB_CTX_IRQ,
438 RB_CTX_SOFTIRQ,
439 RB_CTX_NORMAL,
440 RB_CTX_MAX
441};
442
10464b4a
SRV
443struct rb_time_struct {
444 local64_t time;
445};
10464b4a
SRV
446typedef struct rb_time_struct rb_time_t;
447
8672e494
SRV
448#define MAX_NEST 5
449
7a8e76a3
SR
450/*
451 * head_page == tail_page && head == tail then buffer is empty.
452 */
453struct ring_buffer_per_cpu {
454 int cpu;
985023de 455 atomic_t record_disabled;
07b8b10e 456 atomic_t resize_disabled;
13292494 457 struct trace_buffer *buffer;
5389f6fa 458 raw_spinlock_t reader_lock; /* serialize readers */
445c8951 459 arch_spinlock_t lock;
7a8e76a3 460 struct lock_class_key lock_key;
73a757e6 461 struct buffer_data_page *free_page;
9b94a8fb 462 unsigned long nr_pages;
58a09ec6 463 unsigned int current_context;
3adc54fa 464 struct list_head *pages;
6f807acd
SR
465 struct buffer_page *head_page; /* read from head */
466 struct buffer_page *tail_page; /* write to tail */
c3706f00 467 struct buffer_page *commit_page; /* committed pages */
d769041f 468 struct buffer_page *reader_page;
66a8cb95
SR
469 unsigned long lost_events;
470 unsigned long last_overrun;
8e012066 471 unsigned long nest;
c64e148a 472 local_t entries_bytes;
e4906eff 473 local_t entries;
884bfe89
SP
474 local_t overrun;
475 local_t commit_overrun;
476 local_t dropped_events;
fa743953
SR
477 local_t committing;
478 local_t commits;
2c2b0a78 479 local_t pages_touched;
31029a8b 480 local_t pages_lost;
2c2b0a78 481 local_t pages_read;
03329f99 482 long last_pages_touch;
2c2b0a78 483 size_t shortest_full;
77ae365e 484 unsigned long read;
c64e148a 485 unsigned long read_bytes;
10464b4a
SRV
486 rb_time_t write_stamp;
487 rb_time_t before_stamp;
8672e494 488 u64 event_stamp[MAX_NEST];
7a8e76a3 489 u64 read_stamp;
2d093282
ZY
490 /* pages removed since last reset */
491 unsigned long pages_removed;
117c3920
VD
492
493 unsigned int mapped;
494 struct mutex mapping_lock;
495 unsigned long *subbuf_ids; /* ID to subbuf VA */
496 struct trace_buffer_meta *meta_page;
497
438ced17 498 /* ring buffer pages to update, > 0 to add, < 0 to remove */
9b94a8fb 499 long nr_pages_to_update;
438ced17 500 struct list_head new_pages; /* new pages to add */
83f40318 501 struct work_struct update_pages_work;
05fdd70d 502 struct completion update_done;
15693458
SRRH
503
504 struct rb_irq_work irq_work;
7a8e76a3
SR
505};
506
13292494 507struct trace_buffer {
7a8e76a3
SR
508 unsigned flags;
509 int cpus;
7a8e76a3 510 atomic_t record_disabled;
8a96c028 511 atomic_t resizing;
00f62f61 512 cpumask_var_t cpumask;
7a8e76a3 513
1f8a6a10
PZ
514 struct lock_class_key *reader_lock_key;
515
7a8e76a3
SR
516 struct mutex mutex;
517
518 struct ring_buffer_per_cpu **buffers;
554f786e 519
b32614c0 520 struct hlist_node node;
37886f6a 521 u64 (*clock)(void);
15693458
SRRH
522
523 struct rb_irq_work irq_work;
00b41452 524 bool time_stamp_abs;
139f8400
TSV
525
526 unsigned int subbuf_size;
2808e31e 527 unsigned int subbuf_order;
139f8400 528 unsigned int max_data_size;
7a8e76a3
SR
529};
530
531struct ring_buffer_iter {
532 struct ring_buffer_per_cpu *cpu_buffer;
533 unsigned long head;
785888c5 534 unsigned long next_event;
7a8e76a3 535 struct buffer_page *head_page;
492a74f4
SR
536 struct buffer_page *cache_reader_page;
537 unsigned long cache_read;
2d093282 538 unsigned long cache_pages_removed;
7a8e76a3 539 u64 read_stamp;
28e3fc56 540 u64 page_stamp;
785888c5 541 struct ring_buffer_event *event;
139f8400 542 size_t event_size;
c9b7a4a7 543 int missed_events;
7a8e76a3
SR
544};
545
139f8400 546int ring_buffer_print_page_header(struct trace_buffer *buffer, struct trace_seq *s)
10464b4a 547{
d5cfbdfc 548 struct buffer_data_page field;
10464b4a 549
d5cfbdfc
TSV
550 trace_seq_printf(s, "\tfield: u64 timestamp;\t"
551 "offset:0;\tsize:%u;\tsigned:%u;\n",
552 (unsigned int)sizeof(field.time_stamp),
553 (unsigned int)is_signed_type(u64));
10464b4a 554
d5cfbdfc
TSV
555 trace_seq_printf(s, "\tfield: local_t commit;\t"
556 "offset:%u;\tsize:%u;\tsigned:%u;\n",
557 (unsigned int)offsetof(typeof(field), commit),
558 (unsigned int)sizeof(field.commit),
559 (unsigned int)is_signed_type(long));
10464b4a 560
d5cfbdfc
TSV
561 trace_seq_printf(s, "\tfield: int overwrite;\t"
562 "offset:%u;\tsize:%u;\tsigned:%u;\n",
563 (unsigned int)offsetof(typeof(field), commit),
564 1,
565 (unsigned int)is_signed_type(long));
10464b4a 566
d5cfbdfc
TSV
567 trace_seq_printf(s, "\tfield: char data;\t"
568 "offset:%u;\tsize:%u;\tsigned:%u;\n",
569 (unsigned int)offsetof(typeof(field), data),
139f8400 570 (unsigned int)buffer->subbuf_size,
d5cfbdfc 571 (unsigned int)is_signed_type(char));
10464b4a 572
d5cfbdfc 573 return !trace_seq_has_overflowed(s);
10464b4a
SRV
574}
575
c84897c0 576static inline void rb_time_read(rb_time_t *t, u64 *ret)
10464b4a
SRV
577{
578 *ret = local64_read(&t->time);
10464b4a
SRV
579}
580static void rb_time_set(rb_time_t *t, u64 val)
581{
582 local64_set(&t->time, val);
583}
10464b4a 584
a948c69d
SRV
585/*
586 * Enable this to make sure that the event passed to
587 * ring_buffer_event_time_stamp() is not committed and also
588 * is on the buffer that it passed in.
589 */
590//#define RB_VERIFY_EVENT
591#ifdef RB_VERIFY_EVENT
592static struct list_head *rb_list_head(struct list_head *list);
593static void verify_event(struct ring_buffer_per_cpu *cpu_buffer,
594 void *event)
595{
596 struct buffer_page *page = cpu_buffer->commit_page;
597 struct buffer_page *tail_page = READ_ONCE(cpu_buffer->tail_page);
598 struct list_head *next;
599 long commit, write;
600 unsigned long addr = (unsigned long)event;
601 bool done = false;
602 int stop = 0;
603
604 /* Make sure the event exists and is not committed yet */
605 do {
606 if (page == tail_page || WARN_ON_ONCE(stop++ > 100))
607 done = true;
608 commit = local_read(&page->page->commit);
609 write = local_read(&page->write);
610 if (addr >= (unsigned long)&page->page->data[commit] &&
611 addr < (unsigned long)&page->page->data[write])
612 return;
613
614 next = rb_list_head(page->list.next);
615 page = list_entry(next, struct buffer_page, list);
616 } while (!done);
617 WARN_ON_ONCE(1);
618}
619#else
620static inline void verify_event(struct ring_buffer_per_cpu *cpu_buffer,
621 void *event)
622{
623}
624#endif
625
6695da58
SRG
626/*
627 * The absolute time stamp drops the 5 MSBs and some clocks may
628 * require them. The rb_fix_abs_ts() will take a previous full
629 * time stamp, and add the 5 MSB of that time stamp on to the
630 * saved absolute time stamp. Then they are compared in case of
631 * the unlikely event that the latest time stamp incremented
632 * the 5 MSB.
633 */
634static inline u64 rb_fix_abs_ts(u64 abs, u64 save_ts)
635{
636 if (save_ts & TS_MSB) {
637 abs |= save_ts & TS_MSB;
638 /* Check for overflow */
639 if (unlikely(abs < save_ts))
640 abs += 1ULL << 59;
641 }
642 return abs;
643}
a948c69d 644
efe6196a
SRV
645static inline u64 rb_time_stamp(struct trace_buffer *buffer);
646
647/**
648 * ring_buffer_event_time_stamp - return the event's current time stamp
649 * @buffer: The buffer that the event is on
650 * @event: the event to get the time stamp of
651 *
652 * Note, this must be called after @event is reserved, and before it is
653 * committed to the ring buffer. And must be called from the same
654 * context where the event was reserved (normal, softirq, irq, etc).
655 *
656 * Returns the time stamp associated with the current event.
657 * If the event has an extended time stamp, then that is used as
658 * the time stamp to return.
659 * In the highly unlikely case that the event was nested more than
660 * the max nesting, then the write_stamp of the buffer is returned,
661 * otherwise current time is returned, but that really neither of
662 * the last two cases should ever happen.
663 */
664u64 ring_buffer_event_time_stamp(struct trace_buffer *buffer,
665 struct ring_buffer_event *event)
666{
667 struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[smp_processor_id()];
668 unsigned int nest;
669 u64 ts;
670
671 /* If the event includes an absolute time, then just use that */
6695da58
SRG
672 if (event->type_len == RINGBUF_TYPE_TIME_STAMP) {
673 ts = rb_event_time_stamp(event);
674 return rb_fix_abs_ts(ts, cpu_buffer->tail_page->page->time_stamp);
675 }
efe6196a 676
a948c69d
SRV
677 nest = local_read(&cpu_buffer->committing);
678 verify_event(cpu_buffer, event);
679 if (WARN_ON_ONCE(!nest))
680 goto fail;
681
efe6196a 682 /* Read the current saved nesting level time stamp */
a948c69d 683 if (likely(--nest < MAX_NEST))
efe6196a
SRV
684 return cpu_buffer->event_stamp[nest];
685
a948c69d
SRV
686 /* Shouldn't happen, warn if it does */
687 WARN_ONCE(1, "nest (%d) greater than max", nest);
efe6196a 688
a948c69d 689 fail:
c84897c0 690 rb_time_read(&cpu_buffer->write_stamp, &ts);
efe6196a
SRV
691
692 return ts;
693}
694
2c2b0a78 695/**
b7085b6f 696 * ring_buffer_nr_dirty_pages - get the number of used pages in the ring buffer
2c2b0a78
SRV
697 * @buffer: The ring_buffer to get the number of pages from
698 * @cpu: The cpu of the ring_buffer to get the number of pages from
699 *
700 * Returns the number of pages that have content in the ring buffer.
701 */
13292494 702size_t ring_buffer_nr_dirty_pages(struct trace_buffer *buffer, int cpu)
2c2b0a78
SRV
703{
704 size_t read;
31029a8b 705 size_t lost;
2c2b0a78
SRV
706 size_t cnt;
707
708 read = local_read(&buffer->buffers[cpu]->pages_read);
31029a8b 709 lost = local_read(&buffer->buffers[cpu]->pages_lost);
2c2b0a78 710 cnt = local_read(&buffer->buffers[cpu]->pages_touched);
31029a8b
SRG
711
712 if (WARN_ON_ONCE(cnt < lost))
713 return 0;
714
715 cnt -= lost;
716
2c2b0a78
SRV
717 /* The reader can read an empty page, but not more than that */
718 if (cnt < read) {
719 WARN_ON_ONCE(read > cnt + 1);
720 return 0;
721 }
722
723 return cnt - read;
724}
725
42fb0a1e
SRG
726static __always_inline bool full_hit(struct trace_buffer *buffer, int cpu, int full)
727{
728 struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
729 size_t nr_pages;
730 size_t dirty;
731
732 nr_pages = cpu_buffer->nr_pages;
733 if (!nr_pages || !full)
734 return true;
735
623b1f89
SRG
736 /*
737 * Add one as dirty will never equal nr_pages, as the sub-buffer
738 * that the writer is on is not counted as dirty.
739 * This is needed if "buffer_percent" is set to 100.
740 */
741 dirty = ring_buffer_nr_dirty_pages(buffer, cpu) + 1;
42fb0a1e 742
623b1f89 743 return (dirty * 100) >= (full * nr_pages);
42fb0a1e
SRG
744}
745
15693458
SRRH
746/*
747 * rb_wake_up_waiters - wake up tasks waiting for ring buffer input
748 *
749 * Schedules a delayed work to wake up any task that is blocked on the
750 * ring buffer waiters queue.
751 */
752static void rb_wake_up_waiters(struct irq_work *work)
753{
754 struct rb_irq_work *rbwork = container_of(work, struct rb_irq_work, work);
755
b70f2938
SRG
756 /* For waiters waiting for the first wake up */
757 (void)atomic_fetch_inc_release(&rbwork->seq);
758
15693458 759 wake_up_all(&rbwork->waiters);
ec0bbc5e 760 if (rbwork->full_waiters_pending || rbwork->wakeup_full) {
68282dd9
SRG
761 /* Only cpu_buffer sets the above flags */
762 struct ring_buffer_per_cpu *cpu_buffer =
763 container_of(rbwork, struct ring_buffer_per_cpu, irq_work);
764
765 /* Called from interrupt context */
766 raw_spin_lock(&cpu_buffer->reader_lock);
1e0d6714 767 rbwork->wakeup_full = false;
ec0bbc5e 768 rbwork->full_waiters_pending = false;
68282dd9
SRG
769
770 /* Waking up all waiters, they will reset the shortest full */
771 cpu_buffer->shortest_full = 0;
772 raw_spin_unlock(&cpu_buffer->reader_lock);
773
1e0d6714
SRRH
774 wake_up_all(&rbwork->full_waiters);
775 }
15693458
SRRH
776}
777
7e9fbbb1
SRG
778/**
779 * ring_buffer_wake_waiters - wake up any waiters on this ring buffer
780 * @buffer: The ring buffer to wake waiters on
151e34d1 781 * @cpu: The CPU buffer to wake waiters on
7e9fbbb1
SRG
782 *
783 * In the case of a file that represents a ring buffer is closing,
784 * it is prudent to wake up any waiters that are on this.
785 */
786void ring_buffer_wake_waiters(struct trace_buffer *buffer, int cpu)
787{
788 struct ring_buffer_per_cpu *cpu_buffer;
789 struct rb_irq_work *rbwork;
790
7433632c
SRG
791 if (!buffer)
792 return;
793
7e9fbbb1
SRG
794 if (cpu == RING_BUFFER_ALL_CPUS) {
795
796 /* Wake up individual ones too. One level recursion */
797 for_each_buffer_cpu(buffer, cpu)
798 ring_buffer_wake_waiters(buffer, cpu);
799
800 rbwork = &buffer->irq_work;
801 } else {
7433632c
SRG
802 if (WARN_ON_ONCE(!buffer->buffers))
803 return;
804 if (WARN_ON_ONCE(cpu >= nr_cpu_ids))
805 return;
806
7e9fbbb1 807 cpu_buffer = buffer->buffers[cpu];
7433632c
SRG
808 /* The CPU buffer may not have been initialized yet */
809 if (!cpu_buffer)
810 return;
7e9fbbb1
SRG
811 rbwork = &cpu_buffer->irq_work;
812 }
813
39a7dc23
SRG
814 /* This can be called in any context */
815 irq_work_queue(&rbwork->work);
7e9fbbb1
SRG
816}
817
b3594573
SRG
818static bool rb_watermark_hit(struct trace_buffer *buffer, int cpu, int full)
819{
820 struct ring_buffer_per_cpu *cpu_buffer;
821 bool ret = false;
822
823 /* Reads of all CPUs always waits for any data */
824 if (cpu == RING_BUFFER_ALL_CPUS)
825 return !ring_buffer_empty(buffer);
826
827 cpu_buffer = buffer->buffers[cpu];
828
829 if (!ring_buffer_empty_cpu(buffer, cpu)) {
830 unsigned long flags;
831 bool pagebusy;
832
833 if (!full)
834 return true;
835
836 raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
837 pagebusy = cpu_buffer->reader_page == cpu_buffer->commit_page;
838 ret = !pagebusy && full_hit(buffer, cpu, full);
839
761d9473
SRG
840 if (!ret && (!cpu_buffer->shortest_full ||
841 cpu_buffer->shortest_full > full)) {
842 cpu_buffer->shortest_full = full;
843 }
b3594573
SRG
844 raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
845 }
846 return ret;
847}
848
7af9ded0
SRG
849static inline bool
850rb_wait_cond(struct rb_irq_work *rbwork, struct trace_buffer *buffer,
851 int cpu, int full, ring_buffer_cond_fn cond, void *data)
15693458 852{
7af9ded0
SRG
853 if (rb_watermark_hit(buffer, cpu, full))
854 return true;
15693458 855
7af9ded0
SRG
856 if (cond(data))
857 return true;
b3594573
SRG
858
859 /*
860 * The events can happen in critical sections where
861 * checking a work queue can cause deadlocks.
862 * After adding a task to the queue, this flag is set
863 * only to notify events to try to wake up the queue
864 * using irq_work.
865 *
866 * We don't clear it even if the buffer is no longer
867 * empty. The flag only causes the next event to run
868 * irq_work to do the work queue wake up. The worse
869 * that can happen if we race with !trace_empty() is that
870 * an event will cause an irq_work to try to wake up
871 * an empty queue.
872 *
873 * There's no reason to protect this flag either, as
874 * the work queue and irq_work logic will do the necessary
875 * synchronization for the wake ups. The only thing
876 * that is necessary is that the wake up happens after
877 * a task has been queued. It's OK for spurious wake ups.
878 */
879 if (full)
7af9ded0 880 rbwork->full_waiters_pending = true;
b3594573 881 else
7af9ded0 882 rbwork->waiters_pending = true;
b3594573 883
7af9ded0
SRG
884 return false;
885}
b3594573 886
b70f2938
SRG
887struct rb_wait_data {
888 struct rb_irq_work *irq_work;
889 int seq;
890};
891
7af9ded0
SRG
892/*
893 * The default wait condition for ring_buffer_wait() is to just to exit the
894 * wait loop the first time it is woken up.
895 */
896static bool rb_wait_once(void *data)
897{
b70f2938
SRG
898 struct rb_wait_data *rdata = data;
899 struct rb_irq_work *rbwork = rdata->irq_work;
7af9ded0 900
b70f2938 901 return atomic_read_acquire(&rbwork->seq) != rdata->seq;
7af9ded0 902}
b3594573 903
7af9ded0
SRG
904/**
905 * ring_buffer_wait - wait for input to the ring buffer
906 * @buffer: buffer to wait on
907 * @cpu: the cpu buffer to wait on
908 * @full: wait until the percentage of pages are available, if @cpu != RING_BUFFER_ALL_CPUS
2aa043a5
SRG
909 * @cond: condition function to break out of wait (NULL to run once)
910 * @data: the data to pass to @cond.
7af9ded0
SRG
911 *
912 * If @cpu == RING_BUFFER_ALL_CPUS then the task will wake up as soon
913 * as data is added to any of the @buffer's cpu buffers. Otherwise
914 * it will wait for data to be added to a specific cpu buffer.
915 */
2aa043a5
SRG
916int ring_buffer_wait(struct trace_buffer *buffer, int cpu, int full,
917 ring_buffer_cond_fn cond, void *data)
7af9ded0
SRG
918{
919 struct ring_buffer_per_cpu *cpu_buffer;
920 struct wait_queue_head *waitq;
7af9ded0 921 struct rb_irq_work *rbwork;
b70f2938 922 struct rb_wait_data rdata;
7af9ded0
SRG
923 int ret = 0;
924
7af9ded0
SRG
925 /*
926 * Depending on what the caller is waiting for, either any
927 * data in any cpu buffer, or a specific buffer, put the
928 * caller on the appropriate wait queue.
929 */
930 if (cpu == RING_BUFFER_ALL_CPUS) {
931 rbwork = &buffer->irq_work;
932 /* Full only makes sense on per cpu reads */
933 full = 0;
934 } else {
935 if (!cpumask_test_cpu(cpu, buffer->cpumask))
936 return -ENODEV;
937 cpu_buffer = buffer->buffers[cpu];
938 rbwork = &cpu_buffer->irq_work;
e30f53aa 939 }
15693458 940
1e0d6714 941 if (full)
7af9ded0 942 waitq = &rbwork->full_waiters;
1e0d6714 943 else
7af9ded0 944 waitq = &rbwork->waiters;
e30f53aa 945
b70f2938
SRG
946 /* Set up to exit loop as soon as it is woken */
947 if (!cond) {
948 cond = rb_wait_once;
949 rdata.irq_work = rbwork;
950 rdata.seq = atomic_read_acquire(&rbwork->seq);
951 data = &rdata;
952 }
953
7af9ded0
SRG
954 ret = wait_event_interruptible((*waitq),
955 rb_wait_cond(rbwork, buffer, cpu, full, cond, data));
b3594573 956
e30f53aa 957 return ret;
15693458
SRRH
958}
959
960/**
961 * ring_buffer_poll_wait - poll on buffer input
962 * @buffer: buffer to wait on
963 * @cpu: the cpu buffer to wait on
964 * @filp: the file descriptor
965 * @poll_table: The poll descriptor
42fb0a1e 966 * @full: wait until the percentage of pages are available, if @cpu != RING_BUFFER_ALL_CPUS
15693458
SRRH
967 *
968 * If @cpu == RING_BUFFER_ALL_CPUS then the task will wake up as soon
969 * as data is added to any of the @buffer's cpu buffers. Otherwise
970 * it will wait for data to be added to a specific cpu buffer.
971 *
a9a08845 972 * Returns EPOLLIN | EPOLLRDNORM if data exists in the buffers,
15693458
SRRH
973 * zero otherwise.
974 */
13292494 975__poll_t ring_buffer_poll_wait(struct trace_buffer *buffer, int cpu,
42fb0a1e 976 struct file *filp, poll_table *poll_table, int full)
15693458
SRRH
977{
978 struct ring_buffer_per_cpu *cpu_buffer;
68282dd9 979 struct rb_irq_work *rbwork;
15693458 980
42fb0a1e 981 if (cpu == RING_BUFFER_ALL_CPUS) {
68282dd9 982 rbwork = &buffer->irq_work;
42fb0a1e
SRG
983 full = 0;
984 } else {
6721cb60 985 if (!cpumask_test_cpu(cpu, buffer->cpumask))
66bbea9e 986 return EPOLLERR;
6721cb60 987
15693458 988 cpu_buffer = buffer->buffers[cpu];
68282dd9 989 rbwork = &cpu_buffer->irq_work;
15693458
SRRH
990 }
991
42fb0a1e 992 if (full) {
68282dd9
SRG
993 poll_wait(filp, &rbwork->full_waiters, poll_table);
994
e36f19a6 995 if (rb_watermark_hit(buffer, cpu, full))
8145f1c3
SRG
996 return EPOLLIN | EPOLLRDNORM;
997 /*
998 * Only allow full_waiters_pending update to be seen after
e36f19a6
SRG
999 * the shortest_full is set (in rb_watermark_hit). If the
1000 * writer sees the full_waiters_pending flag set, it will
1001 * compare the amount in the ring buffer to shortest_full.
1002 * If the amount in the ring buffer is greater than the
1003 * shortest_full percent, it will call the irq_work handler
1004 * to wake up this list. The irq_handler will reset shortest_full
8145f1c3
SRG
1005 * back to zero. That's done under the reader_lock, but
1006 * the below smp_mb() makes sure that the update to
1007 * full_waiters_pending doesn't leak up into the above.
1008 */
1009 smp_mb();
68282dd9 1010 rbwork->full_waiters_pending = true;
8145f1c3 1011 return 0;
42fb0a1e
SRG
1012 }
1013
8145f1c3
SRG
1014 poll_wait(filp, &rbwork->waiters, poll_table);
1015 rbwork->waiters_pending = true;
1016
4ce97dbf
JB
1017 /*
1018 * There's a tight race between setting the waiters_pending and
1019 * checking if the ring buffer is empty. Once the waiters_pending bit
1020 * is set, the next event will wake the task up, but we can get stuck
1021 * if there's only a single event in.
1022 *
1023 * FIXME: Ideally, we need a memory barrier on the writer side as well,
1024 * but adding a memory barrier to all events will cause too much of a
1025 * performance hit in the fast path. We only need a memory barrier when
1026 * the buffer goes from empty to having content. But as this race is
1027 * extremely small, and it's not a problem if another event comes in, we
1028 * will fix it later.
1029 */
1030 smp_mb();
15693458
SRRH
1031
1032 if ((cpu == RING_BUFFER_ALL_CPUS && !ring_buffer_empty(buffer)) ||
1033 (cpu != RING_BUFFER_ALL_CPUS && !ring_buffer_empty_cpu(buffer, cpu)))
a9a08845 1034 return EPOLLIN | EPOLLRDNORM;
15693458
SRRH
1035 return 0;
1036}
1037
f536aafc 1038/* buffer may be either ring_buffer or ring_buffer_per_cpu */
077c5407
SR
1039#define RB_WARN_ON(b, cond) \
1040 ({ \
1041 int _____ret = unlikely(cond); \
1042 if (_____ret) { \
1043 if (__same_type(*(b), struct ring_buffer_per_cpu)) { \
1044 struct ring_buffer_per_cpu *__b = \
1045 (void *)b; \
1046 atomic_inc(&__b->buffer->record_disabled); \
1047 } else \
1048 atomic_inc(&b->record_disabled); \
1049 WARN_ON(1); \
1050 } \
1051 _____ret; \
3e89c7bb 1052 })
f536aafc 1053
37886f6a
SR
1054/* Up this if you want to test the TIME_EXTENTS and normalization */
1055#define DEBUG_SHIFT 0
1056
13292494 1057static inline u64 rb_time_stamp(struct trace_buffer *buffer)
88eb0125 1058{
bbeba3e5
SRV
1059 u64 ts;
1060
1061 /* Skip retpolines :-( */
aefb2f2e 1062 if (IS_ENABLED(CONFIG_MITIGATION_RETPOLINE) && likely(buffer->clock == trace_clock_local))
bbeba3e5
SRV
1063 ts = trace_clock_local();
1064 else
1065 ts = buffer->clock();
1066
88eb0125 1067 /* shift to debug/test normalization and TIME_EXTENTS */
bbeba3e5 1068 return ts << DEBUG_SHIFT;
88eb0125
SR
1069}
1070
f3ef7202 1071u64 ring_buffer_time_stamp(struct trace_buffer *buffer)
37886f6a
SR
1072{
1073 u64 time;
1074
1075 preempt_disable_notrace();
6d3f1e12 1076 time = rb_time_stamp(buffer);
d6097c9e 1077 preempt_enable_notrace();
37886f6a
SR
1078
1079 return time;
1080}
1081EXPORT_SYMBOL_GPL(ring_buffer_time_stamp);
1082
13292494 1083void ring_buffer_normalize_time_stamp(struct trace_buffer *buffer,
37886f6a
SR
1084 int cpu, u64 *ts)
1085{
1086 /* Just stupid testing the normalize function and deltas */
1087 *ts >>= DEBUG_SHIFT;
1088}
1089EXPORT_SYMBOL_GPL(ring_buffer_normalize_time_stamp);
1090
77ae365e
SR
1091/*
1092 * Making the ring buffer lockless makes things tricky.
1093 * Although writes only happen on the CPU that they are on,
1094 * and they only need to worry about interrupts. Reads can
1095 * happen on any CPU.
1096 *
1097 * The reader page is always off the ring buffer, but when the
1098 * reader finishes with a page, it needs to swap its page with
1099 * a new one from the buffer. The reader needs to take from
1100 * the head (writes go to the tail). But if a writer is in overwrite
1101 * mode and wraps, it must push the head page forward.
1102 *
1103 * Here lies the problem.
1104 *
1105 * The reader must be careful to replace only the head page, and
1106 * not another one. As described at the top of the file in the
1107 * ASCII art, the reader sets its old page to point to the next
1108 * page after head. It then sets the page after head to point to
1109 * the old reader page. But if the writer moves the head page
1110 * during this operation, the reader could end up with the tail.
1111 *
1112 * We use cmpxchg to help prevent this race. We also do something
1113 * special with the page before head. We set the LSB to 1.
1114 *
1115 * When the writer must push the page forward, it will clear the
1116 * bit that points to the head page, move the head, and then set
1117 * the bit that points to the new head page.
1118 *
1119 * We also don't want an interrupt coming in and moving the head
1120 * page on another writer. Thus we use the second LSB to catch
1121 * that too. Thus:
1122 *
1123 * head->list->prev->next bit 1 bit 0
1124 * ------- -------
1125 * Normal page 0 0
1126 * Points to head page 0 1
1127 * New head page 1 0
1128 *
1129 * Note we can not trust the prev pointer of the head page, because:
1130 *
1131 * +----+ +-----+ +-----+
1132 * | |------>| T |---X--->| N |
1133 * | |<------| | | |
1134 * +----+ +-----+ +-----+
1135 * ^ ^ |
1136 * | +-----+ | |
1137 * +----------| R |----------+ |
1138 * | |<-----------+
1139 * +-----+
1140 *
1141 * Key: ---X--> HEAD flag set in pointer
1142 * T Tail page
1143 * R Reader page
1144 * N Next page
1145 *
1146 * (see __rb_reserve_next() to see where this happens)
1147 *
1148 * What the above shows is that the reader just swapped out
1149 * the reader page with a page in the buffer, but before it
1150 * could make the new header point back to the new page added
1151 * it was preempted by a writer. The writer moved forward onto
1152 * the new page added by the reader and is about to move forward
1153 * again.
1154 *
1155 * You can see, it is legitimate for the previous pointer of
1156 * the head (or any page) not to point back to itself. But only
6167c205 1157 * temporarily.
77ae365e
SR
1158 */
1159
1160#define RB_PAGE_NORMAL 0UL
1161#define RB_PAGE_HEAD 1UL
1162#define RB_PAGE_UPDATE 2UL
1163
1164
1165#define RB_FLAG_MASK 3UL
1166
1167/* PAGE_MOVED is not part of the mask */
1168#define RB_PAGE_MOVED 4UL
1169
1170/*
1171 * rb_list_head - remove any bit
1172 */
1173static struct list_head *rb_list_head(struct list_head *list)
1174{
1175 unsigned long val = (unsigned long)list;
1176
1177 return (struct list_head *)(val & ~RB_FLAG_MASK);
1178}
1179
1180/*
6d3f1e12 1181 * rb_is_head_page - test if the given page is the head page
77ae365e
SR
1182 *
1183 * Because the reader may move the head_page pointer, we can
1184 * not trust what the head page is (it may be pointing to
1185 * the reader page). But if the next page is a header page,
1186 * its flags will be non zero.
1187 */
42b16b3f 1188static inline int
6689bed3 1189rb_is_head_page(struct buffer_page *page, struct list_head *list)
77ae365e
SR
1190{
1191 unsigned long val;
1192
1193 val = (unsigned long)list->next;
1194
1195 if ((val & ~RB_FLAG_MASK) != (unsigned long)&page->list)
1196 return RB_PAGE_MOVED;
1197
1198 return val & RB_FLAG_MASK;
1199}
1200
1201/*
1202 * rb_is_reader_page
1203 *
1204 * The unique thing about the reader page, is that, if the
1205 * writer is ever on it, the previous pointer never points
1206 * back to the reader page.
1207 */
06ca3209 1208static bool rb_is_reader_page(struct buffer_page *page)
77ae365e
SR
1209{
1210 struct list_head *list = page->list.prev;
1211
1212 return rb_list_head(list->next) != &page->list;
1213}
1214
1215/*
1216 * rb_set_list_to_head - set a list_head to be pointing to head.
1217 */
6689bed3 1218static void rb_set_list_to_head(struct list_head *list)
77ae365e
SR
1219{
1220 unsigned long *ptr;
1221
1222 ptr = (unsigned long *)&list->next;
1223 *ptr |= RB_PAGE_HEAD;
1224 *ptr &= ~RB_PAGE_UPDATE;
1225}
1226
1227/*
1228 * rb_head_page_activate - sets up head page
1229 */
1230static void rb_head_page_activate(struct ring_buffer_per_cpu *cpu_buffer)
1231{
1232 struct buffer_page *head;
1233
1234 head = cpu_buffer->head_page;
1235 if (!head)
1236 return;
1237
1238 /*
1239 * Set the previous list pointer to have the HEAD flag.
1240 */
6689bed3 1241 rb_set_list_to_head(head->list.prev);
77ae365e
SR
1242}
1243
1244static void rb_list_head_clear(struct list_head *list)
1245{
1246 unsigned long *ptr = (unsigned long *)&list->next;
1247
1248 *ptr &= ~RB_FLAG_MASK;
1249}
1250
1251/*
6167c205 1252 * rb_head_page_deactivate - clears head page ptr (for free list)
77ae365e
SR
1253 */
1254static void
1255rb_head_page_deactivate(struct ring_buffer_per_cpu *cpu_buffer)
1256{
1257 struct list_head *hd;
1258
1259 /* Go through the whole list and clear any pointers found. */
1260 rb_list_head_clear(cpu_buffer->pages);
1261
1262 list_for_each(hd, cpu_buffer->pages)
1263 rb_list_head_clear(hd);
1264}
1265
1266static int rb_head_page_set(struct ring_buffer_per_cpu *cpu_buffer,
1267 struct buffer_page *head,
1268 struct buffer_page *prev,
1269 int old_flag, int new_flag)
1270{
1271 struct list_head *list;
1272 unsigned long val = (unsigned long)&head->list;
1273 unsigned long ret;
1274
1275 list = &prev->list;
1276
1277 val &= ~RB_FLAG_MASK;
1278
08a40816
SR
1279 ret = cmpxchg((unsigned long *)&list->next,
1280 val | old_flag, val | new_flag);
77ae365e
SR
1281
1282 /* check if the reader took the page */
1283 if ((ret & ~RB_FLAG_MASK) != val)
1284 return RB_PAGE_MOVED;
1285
1286 return ret & RB_FLAG_MASK;
1287}
1288
1289static int rb_head_page_set_update(struct ring_buffer_per_cpu *cpu_buffer,
1290 struct buffer_page *head,
1291 struct buffer_page *prev,
1292 int old_flag)
1293{
1294 return rb_head_page_set(cpu_buffer, head, prev,
1295 old_flag, RB_PAGE_UPDATE);
1296}
1297
1298static int rb_head_page_set_head(struct ring_buffer_per_cpu *cpu_buffer,
1299 struct buffer_page *head,
1300 struct buffer_page *prev,
1301 int old_flag)
1302{
1303 return rb_head_page_set(cpu_buffer, head, prev,
1304 old_flag, RB_PAGE_HEAD);
1305}
1306
1307static int rb_head_page_set_normal(struct ring_buffer_per_cpu *cpu_buffer,
1308 struct buffer_page *head,
1309 struct buffer_page *prev,
1310 int old_flag)
1311{
1312 return rb_head_page_set(cpu_buffer, head, prev,
1313 old_flag, RB_PAGE_NORMAL);
1314}
1315
6689bed3 1316static inline void rb_inc_page(struct buffer_page **bpage)
77ae365e
SR
1317{
1318 struct list_head *p = rb_list_head((*bpage)->list.next);
1319
1320 *bpage = list_entry(p, struct buffer_page, list);
1321}
1322
1323static struct buffer_page *
1324rb_set_head_page(struct ring_buffer_per_cpu *cpu_buffer)
1325{
1326 struct buffer_page *head;
1327 struct buffer_page *page;
1328 struct list_head *list;
1329 int i;
1330
1331 if (RB_WARN_ON(cpu_buffer, !cpu_buffer->head_page))
1332 return NULL;
1333
1334 /* sanity check */
1335 list = cpu_buffer->pages;
1336 if (RB_WARN_ON(cpu_buffer, rb_list_head(list->prev->next) != list))
1337 return NULL;
1338
1339 page = head = cpu_buffer->head_page;
1340 /*
1341 * It is possible that the writer moves the header behind
1342 * where we started, and we miss in one loop.
1343 * A second loop should grab the header, but we'll do
1344 * three loops just because I'm paranoid.
1345 */
1346 for (i = 0; i < 3; i++) {
1347 do {
6689bed3 1348 if (rb_is_head_page(page, page->list.prev)) {
77ae365e
SR
1349 cpu_buffer->head_page = page;
1350 return page;
1351 }
6689bed3 1352 rb_inc_page(&page);
77ae365e
SR
1353 } while (page != head);
1354 }
1355
1356 RB_WARN_ON(cpu_buffer, 1);
1357
1358 return NULL;
1359}
1360
bc92b956 1361static bool rb_head_page_replace(struct buffer_page *old,
77ae365e
SR
1362 struct buffer_page *new)
1363{
1364 unsigned long *ptr = (unsigned long *)&old->list.prev->next;
1365 unsigned long val;
77ae365e
SR
1366
1367 val = *ptr & ~RB_FLAG_MASK;
1368 val |= RB_PAGE_HEAD;
1369
00a8478f 1370 return try_cmpxchg(ptr, &val, (unsigned long)&new->list);
77ae365e
SR
1371}
1372
1373/*
1374 * rb_tail_page_update - move the tail page forward
77ae365e 1375 */
70004986 1376static void rb_tail_page_update(struct ring_buffer_per_cpu *cpu_buffer,
77ae365e
SR
1377 struct buffer_page *tail_page,
1378 struct buffer_page *next_page)
1379{
77ae365e
SR
1380 unsigned long old_entries;
1381 unsigned long old_write;
77ae365e
SR
1382
1383 /*
1384 * The tail page now needs to be moved forward.
1385 *
1386 * We need to reset the tail page, but without messing
1387 * with possible erasing of data brought in by interrupts
1388 * that have moved the tail page and are currently on it.
1389 *
1390 * We add a counter to the write field to denote this.
1391 */
1392 old_write = local_add_return(RB_WRITE_INTCNT, &next_page->write);
1393 old_entries = local_add_return(RB_WRITE_INTCNT, &next_page->entries);
1394
1395 /*
1396 * Just make sure we have seen our old_write and synchronize
1397 * with any interrupts that come in.
1398 */
1399 barrier();
1400
1401 /*
1402 * If the tail page is still the same as what we think
1403 * it is, then it is up to us to update the tail
1404 * pointer.
1405 */
8573636e 1406 if (tail_page == READ_ONCE(cpu_buffer->tail_page)) {
77ae365e
SR
1407 /* Zero the write counter */
1408 unsigned long val = old_write & ~RB_WRITE_MASK;
1409 unsigned long eval = old_entries & ~RB_WRITE_MASK;
1410
1411 /*
1412 * This will only succeed if an interrupt did
1413 * not come in and change it. In which case, we
1414 * do not want to modify it.
da706d8b
LJ
1415 *
1416 * We add (void) to let the compiler know that we do not care
1417 * about the return value of these functions. We use the
1418 * cmpxchg to only update if an interrupt did not already
1419 * do it for us. If the cmpxchg fails, we don't care.
77ae365e 1420 */
da706d8b
LJ
1421 (void)local_cmpxchg(&next_page->write, old_write, val);
1422 (void)local_cmpxchg(&next_page->entries, old_entries, eval);
77ae365e
SR
1423
1424 /*
1425 * No need to worry about races with clearing out the commit.
1426 * it only can increment when a commit takes place. But that
1427 * only happens in the outer most nested commit.
1428 */
1429 local_set(&next_page->page->commit, 0);
1430
ffe3986f
SRG
1431 /* Either we update tail_page or an interrupt does */
1432 if (try_cmpxchg(&cpu_buffer->tail_page, &tail_page, next_page))
1433 local_inc(&cpu_buffer->pages_touched);
77ae365e 1434 }
77ae365e
SR
1435}
1436
b4b55dfd 1437static void rb_check_bpage(struct ring_buffer_per_cpu *cpu_buffer,
77ae365e
SR
1438 struct buffer_page *bpage)
1439{
1440 unsigned long val = (unsigned long)bpage;
1441
b4b55dfd 1442 RB_WARN_ON(cpu_buffer, val & RB_FLAG_MASK);
77ae365e
SR
1443}
1444
7a8e76a3 1445/**
d611851b 1446 * rb_check_pages - integrity check of buffer pages
7a8e76a3
SR
1447 * @cpu_buffer: CPU buffer with pages to test
1448 *
c3706f00 1449 * As a safety measure we check to make sure the data pages have not
7a8e76a3 1450 * been corrupted.
c2274b90
PP
1451 *
1452 * Callers of this function need to guarantee that the list of pages doesn't get
1453 * modified during the check. In particular, if it's possible that the function
1454 * is invoked with concurrent readers which can swap in a new reader page then
1455 * the caller should take cpu_buffer->reader_lock.
7a8e76a3 1456 */
b4b55dfd 1457static void rb_check_pages(struct ring_buffer_per_cpu *cpu_buffer)
7a8e76a3 1458{
8843e06f
MO
1459 struct list_head *head = rb_list_head(cpu_buffer->pages);
1460 struct list_head *tmp;
308f7eeb 1461
8843e06f
MO
1462 if (RB_WARN_ON(cpu_buffer,
1463 rb_list_head(rb_list_head(head->next)->prev) != head))
b4b55dfd 1464 return;
7a8e76a3 1465
8843e06f
MO
1466 if (RB_WARN_ON(cpu_buffer,
1467 rb_list_head(rb_list_head(head->prev)->next) != head))
b4b55dfd 1468 return;
77ae365e 1469
8843e06f 1470 for (tmp = rb_list_head(head->next); tmp != head; tmp = rb_list_head(tmp->next)) {
3e89c7bb 1471 if (RB_WARN_ON(cpu_buffer,
8843e06f 1472 rb_list_head(rb_list_head(tmp->next)->prev) != tmp))
b4b55dfd 1473 return;
8843e06f 1474
3e89c7bb 1475 if (RB_WARN_ON(cpu_buffer,
8843e06f 1476 rb_list_head(rb_list_head(tmp->prev)->next) != tmp))
b4b55dfd 1477 return;
7a8e76a3 1478 }
7a8e76a3
SR
1479}
1480
74e2afc6
QH
1481static int __rb_allocate_pages(struct ring_buffer_per_cpu *cpu_buffer,
1482 long nr_pages, struct list_head *pages)
7a8e76a3 1483{
044fa782 1484 struct buffer_page *bpage, *tmp;
927e56db
SRV
1485 bool user_thread = current->mm != NULL;
1486 gfp_t mflags;
9b94a8fb 1487 long i;
3adc54fa 1488
927e56db
SRV
1489 /*
1490 * Check if the available memory is there first.
1491 * Note, si_mem_available() only gives us a rough estimate of available
1492 * memory. It may not be accurate. But we don't care, we just want
1493 * to prevent doing any allocation when it is obvious that it is
1494 * not going to succeed.
1495 */
2a872fa4
SRV
1496 i = si_mem_available();
1497 if (i < nr_pages)
1498 return -ENOMEM;
1499
927e56db
SRV
1500 /*
1501 * __GFP_RETRY_MAYFAIL flag makes sure that the allocation fails
1502 * gracefully without invoking oom-killer and the system is not
1503 * destabilized.
1504 */
1505 mflags = GFP_KERNEL | __GFP_RETRY_MAYFAIL;
1506
1507 /*
1508 * If a user thread allocates too much, and si_mem_available()
1509 * reports there's enough memory, even though there is not.
1510 * Make sure the OOM killer kills this thread. This can happen
1511 * even with RETRY_MAYFAIL because another task may be doing
1512 * an allocation after this task has taken all memory.
1513 * This is the task the OOM killer needs to take out during this
1514 * loop, even if it was triggered by an allocation somewhere else.
1515 */
1516 if (user_thread)
1517 set_current_oom_origin();
7a8e76a3 1518 for (i = 0; i < nr_pages; i++) {
7ea59064 1519 struct page *page;
927e56db 1520
044fa782 1521 bpage = kzalloc_node(ALIGN(sizeof(*bpage), cache_line_size()),
74e2afc6 1522 mflags, cpu_to_node(cpu_buffer->cpu));
044fa782 1523 if (!bpage)
e4c2ce82 1524 goto free_pages;
77ae365e 1525
74e2afc6
QH
1526 rb_check_bpage(cpu_buffer, bpage);
1527
438ced17 1528 list_add(&bpage->list, pages);
77ae365e 1529
6b76323e 1530 page = alloc_pages_node(cpu_to_node(cpu_buffer->cpu),
c09d4167 1531 mflags | __GFP_COMP | __GFP_ZERO,
f9b94daa 1532 cpu_buffer->buffer->subbuf_order);
7ea59064 1533 if (!page)
7a8e76a3 1534 goto free_pages;
7ea59064 1535 bpage->page = page_address(page);
f9b94daa 1536 bpage->order = cpu_buffer->buffer->subbuf_order;
044fa782 1537 rb_init_page(bpage->page);
927e56db
SRV
1538
1539 if (user_thread && fatal_signal_pending(current))
1540 goto free_pages;
7a8e76a3 1541 }
927e56db
SRV
1542 if (user_thread)
1543 clear_current_oom_origin();
7a8e76a3 1544
438ced17
VN
1545 return 0;
1546
1547free_pages:
1548 list_for_each_entry_safe(bpage, tmp, pages, list) {
1549 list_del_init(&bpage->list);
1550 free_buffer_page(bpage);
1551 }
927e56db
SRV
1552 if (user_thread)
1553 clear_current_oom_origin();
438ced17
VN
1554
1555 return -ENOMEM;
1556}
1557
1558static int rb_allocate_pages(struct ring_buffer_per_cpu *cpu_buffer,
9b94a8fb 1559 unsigned long nr_pages)
438ced17
VN
1560{
1561 LIST_HEAD(pages);
1562
1563 WARN_ON(!nr_pages);
1564
74e2afc6 1565 if (__rb_allocate_pages(cpu_buffer, nr_pages, &pages))
438ced17
VN
1566 return -ENOMEM;
1567
3adc54fa
SR
1568 /*
1569 * The ring buffer page list is a circular list that does not
1570 * start and end with a list head. All page list items point to
1571 * other pages.
1572 */
1573 cpu_buffer->pages = pages.next;
1574 list_del(&pages);
7a8e76a3 1575
438ced17
VN
1576 cpu_buffer->nr_pages = nr_pages;
1577
7a8e76a3
SR
1578 rb_check_pages(cpu_buffer);
1579
1580 return 0;
7a8e76a3
SR
1581}
1582
1583static struct ring_buffer_per_cpu *
13292494 1584rb_allocate_cpu_buffer(struct trace_buffer *buffer, long nr_pages, int cpu)
7a8e76a3
SR
1585{
1586 struct ring_buffer_per_cpu *cpu_buffer;
044fa782 1587 struct buffer_page *bpage;
7ea59064 1588 struct page *page;
7a8e76a3
SR
1589 int ret;
1590
1591 cpu_buffer = kzalloc_node(ALIGN(sizeof(*cpu_buffer), cache_line_size()),
1592 GFP_KERNEL, cpu_to_node(cpu));
1593 if (!cpu_buffer)
1594 return NULL;
1595
1596 cpu_buffer->cpu = cpu;
1597 cpu_buffer->buffer = buffer;
5389f6fa 1598 raw_spin_lock_init(&cpu_buffer->reader_lock);
1f8a6a10 1599 lockdep_set_class(&cpu_buffer->reader_lock, buffer->reader_lock_key);
edc35bd7 1600 cpu_buffer->lock = (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
83f40318 1601 INIT_WORK(&cpu_buffer->update_pages_work, update_pages_handler);
05fdd70d 1602 init_completion(&cpu_buffer->update_done);
15693458 1603 init_irq_work(&cpu_buffer->irq_work.work, rb_wake_up_waiters);
f1dc6725 1604 init_waitqueue_head(&cpu_buffer->irq_work.waiters);
1e0d6714 1605 init_waitqueue_head(&cpu_buffer->irq_work.full_waiters);
117c3920 1606 mutex_init(&cpu_buffer->mapping_lock);
7a8e76a3 1607
044fa782 1608 bpage = kzalloc_node(ALIGN(sizeof(*bpage), cache_line_size()),
e4c2ce82 1609 GFP_KERNEL, cpu_to_node(cpu));
044fa782 1610 if (!bpage)
e4c2ce82
SR
1611 goto fail_free_buffer;
1612
77ae365e
SR
1613 rb_check_bpage(cpu_buffer, bpage);
1614
044fa782 1615 cpu_buffer->reader_page = bpage;
f9b94daa 1616
c09d4167 1617 page = alloc_pages_node(cpu_to_node(cpu), GFP_KERNEL | __GFP_COMP | __GFP_ZERO,
6b76323e 1618 cpu_buffer->buffer->subbuf_order);
7ea59064 1619 if (!page)
e4c2ce82 1620 goto fail_free_reader;
7ea59064 1621 bpage->page = page_address(page);
044fa782 1622 rb_init_page(bpage->page);
e4c2ce82 1623
d769041f 1624 INIT_LIST_HEAD(&cpu_buffer->reader_page->list);
44b99462 1625 INIT_LIST_HEAD(&cpu_buffer->new_pages);
d769041f 1626
438ced17 1627 ret = rb_allocate_pages(cpu_buffer, nr_pages);
7a8e76a3 1628 if (ret < 0)
d769041f 1629 goto fail_free_reader;
7a8e76a3
SR
1630
1631 cpu_buffer->head_page
3adc54fa 1632 = list_entry(cpu_buffer->pages, struct buffer_page, list);
bf41a158 1633 cpu_buffer->tail_page = cpu_buffer->commit_page = cpu_buffer->head_page;
7a8e76a3 1634
77ae365e
SR
1635 rb_head_page_activate(cpu_buffer);
1636
7a8e76a3
SR
1637 return cpu_buffer;
1638
d769041f
SR
1639 fail_free_reader:
1640 free_buffer_page(cpu_buffer->reader_page);
1641
7a8e76a3
SR
1642 fail_free_buffer:
1643 kfree(cpu_buffer);
1644 return NULL;
1645}
1646
1647static void rb_free_cpu_buffer(struct ring_buffer_per_cpu *cpu_buffer)
1648{
3adc54fa 1649 struct list_head *head = cpu_buffer->pages;
044fa782 1650 struct buffer_page *bpage, *tmp;
7a8e76a3 1651
675751bb
JB
1652 irq_work_sync(&cpu_buffer->irq_work.work);
1653
d769041f
SR
1654 free_buffer_page(cpu_buffer->reader_page);
1655
3adc54fa 1656 if (head) {
56f4ca0a
DT
1657 rb_head_page_deactivate(cpu_buffer);
1658
3adc54fa
SR
1659 list_for_each_entry_safe(bpage, tmp, head, list) {
1660 list_del_init(&bpage->list);
1661 free_buffer_page(bpage);
1662 }
1663 bpage = list_entry(head, struct buffer_page, list);
044fa782 1664 free_buffer_page(bpage);
7a8e76a3 1665 }
3adc54fa 1666
17d80175
SRG
1667 free_page((unsigned long)cpu_buffer->free_page);
1668
7a8e76a3
SR
1669 kfree(cpu_buffer);
1670}
1671
1672/**
d611851b 1673 * __ring_buffer_alloc - allocate a new ring_buffer
68814b58 1674 * @size: the size in bytes per cpu that is needed.
7a8e76a3 1675 * @flags: attributes to set for the ring buffer.
59e7cffe 1676 * @key: ring buffer reader_lock_key.
7a8e76a3
SR
1677 *
1678 * Currently the only flag that is available is the RB_FL_OVERWRITE
1679 * flag. This flag means that the buffer will overwrite old data
1680 * when the buffer wraps. If this flag is not set, the buffer will
1681 * drop data when the tail hits the head.
1682 */
13292494 1683struct trace_buffer *__ring_buffer_alloc(unsigned long size, unsigned flags,
1f8a6a10 1684 struct lock_class_key *key)
7a8e76a3 1685{
13292494 1686 struct trace_buffer *buffer;
9b94a8fb 1687 long nr_pages;
7a8e76a3 1688 int bsize;
9b94a8fb 1689 int cpu;
b32614c0 1690 int ret;
7a8e76a3
SR
1691
1692 /* keep it in its own cache line */
1693 buffer = kzalloc(ALIGN(sizeof(*buffer), cache_line_size()),
1694 GFP_KERNEL);
1695 if (!buffer)
1696 return NULL;
1697
b18cc3de 1698 if (!zalloc_cpumask_var(&buffer->cpumask, GFP_KERNEL))
9e01c1b7
RR
1699 goto fail_free_buffer;
1700
139f8400 1701 /* Default buffer page size - one system page */
f9b94daa 1702 buffer->subbuf_order = 0;
139f8400
TSV
1703 buffer->subbuf_size = PAGE_SIZE - BUF_PAGE_HDR_SIZE;
1704
1705 /* Max payload is buffer page size - header (8bytes) */
1706 buffer->max_data_size = buffer->subbuf_size - (sizeof(u32) * 2);
1707
1708 nr_pages = DIV_ROUND_UP(size, buffer->subbuf_size);
7a8e76a3 1709 buffer->flags = flags;
37886f6a 1710 buffer->clock = trace_clock_local;
1f8a6a10 1711 buffer->reader_lock_key = key;
7a8e76a3 1712
15693458 1713 init_irq_work(&buffer->irq_work.work, rb_wake_up_waiters);
f1dc6725 1714 init_waitqueue_head(&buffer->irq_work.waiters);
15693458 1715
7a8e76a3 1716 /* need at least two pages */
438ced17
VN
1717 if (nr_pages < 2)
1718 nr_pages = 2;
7a8e76a3 1719
7a8e76a3
SR
1720 buffer->cpus = nr_cpu_ids;
1721
1722 bsize = sizeof(void *) * nr_cpu_ids;
1723 buffer->buffers = kzalloc(ALIGN(bsize, cache_line_size()),
1724 GFP_KERNEL);
1725 if (!buffer->buffers)
9e01c1b7 1726 goto fail_free_cpumask;
7a8e76a3 1727
b32614c0
SAS
1728 cpu = raw_smp_processor_id();
1729 cpumask_set_cpu(cpu, buffer->cpumask);
1730 buffer->buffers[cpu] = rb_allocate_cpu_buffer(buffer, nr_pages, cpu);
1731 if (!buffer->buffers[cpu])
1732 goto fail_free_buffers;
7a8e76a3 1733
b32614c0
SAS
1734 ret = cpuhp_state_add_instance(CPUHP_TRACE_RB_PREPARE, &buffer->node);
1735 if (ret < 0)
1736 goto fail_free_buffers;
554f786e 1737
7a8e76a3
SR
1738 mutex_init(&buffer->mutex);
1739
1740 return buffer;
1741
1742 fail_free_buffers:
1743 for_each_buffer_cpu(buffer, cpu) {
1744 if (buffer->buffers[cpu])
1745 rb_free_cpu_buffer(buffer->buffers[cpu]);
1746 }
1747 kfree(buffer->buffers);
1748
9e01c1b7
RR
1749 fail_free_cpumask:
1750 free_cpumask_var(buffer->cpumask);
1751
7a8e76a3
SR
1752 fail_free_buffer:
1753 kfree(buffer);
1754 return NULL;
1755}
1f8a6a10 1756EXPORT_SYMBOL_GPL(__ring_buffer_alloc);
7a8e76a3
SR
1757
1758/**
1759 * ring_buffer_free - free a ring buffer.
1760 * @buffer: the buffer to free.
1761 */
1762void
13292494 1763ring_buffer_free(struct trace_buffer *buffer)
7a8e76a3
SR
1764{
1765 int cpu;
1766
b32614c0 1767 cpuhp_state_remove_instance(CPUHP_TRACE_RB_PREPARE, &buffer->node);
554f786e 1768
675751bb
JB
1769 irq_work_sync(&buffer->irq_work.work);
1770
7a8e76a3
SR
1771 for_each_buffer_cpu(buffer, cpu)
1772 rb_free_cpu_buffer(buffer->buffers[cpu]);
1773
bd3f0221 1774 kfree(buffer->buffers);
9e01c1b7
RR
1775 free_cpumask_var(buffer->cpumask);
1776
7a8e76a3
SR
1777 kfree(buffer);
1778}
c4f50183 1779EXPORT_SYMBOL_GPL(ring_buffer_free);
7a8e76a3 1780
13292494 1781void ring_buffer_set_clock(struct trace_buffer *buffer,
37886f6a
SR
1782 u64 (*clock)(void))
1783{
1784 buffer->clock = clock;
1785}
1786
13292494 1787void ring_buffer_set_time_stamp_abs(struct trace_buffer *buffer, bool abs)
00b41452
TZ
1788{
1789 buffer->time_stamp_abs = abs;
1790}
1791
13292494 1792bool ring_buffer_time_stamp_abs(struct trace_buffer *buffer)
00b41452
TZ
1793{
1794 return buffer->time_stamp_abs;
1795}
1796
83f40318
VN
1797static inline unsigned long rb_page_entries(struct buffer_page *bpage)
1798{
1799 return local_read(&bpage->entries) & RB_WRITE_MASK;
1800}
1801
1802static inline unsigned long rb_page_write(struct buffer_page *bpage)
1803{
1804 return local_read(&bpage->write) & RB_WRITE_MASK;
1805}
1806
bc92b956 1807static bool
9b94a8fb 1808rb_remove_pages(struct ring_buffer_per_cpu *cpu_buffer, unsigned long nr_pages)
7a8e76a3 1809{
83f40318
VN
1810 struct list_head *tail_page, *to_remove, *next_page;
1811 struct buffer_page *to_remove_page, *tmp_iter_page;
1812 struct buffer_page *last_page, *first_page;
9b94a8fb 1813 unsigned long nr_removed;
83f40318
VN
1814 unsigned long head_bit;
1815 int page_entries;
1816
1817 head_bit = 0;
7a8e76a3 1818
5389f6fa 1819 raw_spin_lock_irq(&cpu_buffer->reader_lock);
83f40318
VN
1820 atomic_inc(&cpu_buffer->record_disabled);
1821 /*
1822 * We don't race with the readers since we have acquired the reader
1823 * lock. We also don't race with writers after disabling recording.
1824 * This makes it easy to figure out the first and the last page to be
1825 * removed from the list. We unlink all the pages in between including
1826 * the first and last pages. This is done in a busy loop so that we
1827 * lose the least number of traces.
1828 * The pages are freed after we restart recording and unlock readers.
1829 */
1830 tail_page = &cpu_buffer->tail_page->list;
77ae365e 1831
83f40318
VN
1832 /*
1833 * tail page might be on reader page, we remove the next page
1834 * from the ring buffer
1835 */
1836 if (cpu_buffer->tail_page == cpu_buffer->reader_page)
1837 tail_page = rb_list_head(tail_page->next);
1838 to_remove = tail_page;
1839
1840 /* start of pages to remove */
1841 first_page = list_entry(rb_list_head(to_remove->next),
1842 struct buffer_page, list);
1843
1844 for (nr_removed = 0; nr_removed < nr_pages; nr_removed++) {
1845 to_remove = rb_list_head(to_remove)->next;
1846 head_bit |= (unsigned long)to_remove & RB_PAGE_HEAD;
7a8e76a3 1847 }
2d093282
ZY
1848 /* Read iterators need to reset themselves when some pages removed */
1849 cpu_buffer->pages_removed += nr_removed;
7a8e76a3 1850
83f40318 1851 next_page = rb_list_head(to_remove)->next;
7a8e76a3 1852
83f40318
VN
1853 /*
1854 * Now we remove all pages between tail_page and next_page.
1855 * Make sure that we have head_bit value preserved for the
1856 * next page
1857 */
1858 tail_page->next = (struct list_head *)((unsigned long)next_page |
1859 head_bit);
1860 next_page = rb_list_head(next_page);
1861 next_page->prev = tail_page;
1862
1863 /* make sure pages points to a valid page in the ring buffer */
1864 cpu_buffer->pages = next_page;
1865
1866 /* update head page */
1867 if (head_bit)
1868 cpu_buffer->head_page = list_entry(next_page,
1869 struct buffer_page, list);
1870
83f40318
VN
1871 /* pages are removed, resume tracing and then free the pages */
1872 atomic_dec(&cpu_buffer->record_disabled);
5389f6fa 1873 raw_spin_unlock_irq(&cpu_buffer->reader_lock);
83f40318
VN
1874
1875 RB_WARN_ON(cpu_buffer, list_empty(cpu_buffer->pages));
1876
1877 /* last buffer page to remove */
1878 last_page = list_entry(rb_list_head(to_remove), struct buffer_page,
1879 list);
1880 tmp_iter_page = first_page;
1881
1882 do {
83f36555
VN
1883 cond_resched();
1884
83f40318 1885 to_remove_page = tmp_iter_page;
6689bed3 1886 rb_inc_page(&tmp_iter_page);
83f40318
VN
1887
1888 /* update the counters */
1889 page_entries = rb_page_entries(to_remove_page);
1890 if (page_entries) {
1891 /*
1892 * If something was added to this page, it was full
1893 * since it is not the tail page. So we deduct the
1894 * bytes consumed in ring buffer from here.
48fdc72f 1895 * Increment overrun to account for the lost events.
83f40318 1896 */
48fdc72f 1897 local_add(page_entries, &cpu_buffer->overrun);
45d99ea4 1898 local_sub(rb_page_commit(to_remove_page), &cpu_buffer->entries_bytes);
31029a8b 1899 local_inc(&cpu_buffer->pages_lost);
83f40318
VN
1900 }
1901
1902 /*
1903 * We have already removed references to this list item, just
1904 * free up the buffer_page and its page
1905 */
1906 free_buffer_page(to_remove_page);
1907 nr_removed--;
1908
1909 } while (to_remove_page != last_page);
1910
1911 RB_WARN_ON(cpu_buffer, nr_removed);
5040b4b7
VN
1912
1913 return nr_removed == 0;
7a8e76a3
SR
1914}
1915
bc92b956 1916static bool
5040b4b7 1917rb_insert_pages(struct ring_buffer_per_cpu *cpu_buffer)
7a8e76a3 1918{
5040b4b7 1919 struct list_head *pages = &cpu_buffer->new_pages;
88ca6a71 1920 unsigned long flags;
bc92b956
UB
1921 bool success;
1922 int retries;
7a8e76a3 1923
88ca6a71
SR
1924 /* Can be called at early boot up, where interrupts must not been enabled */
1925 raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
5040b4b7
VN
1926 /*
1927 * We are holding the reader lock, so the reader page won't be swapped
1928 * in the ring buffer. Now we are racing with the writer trying to
1929 * move head page and the tail page.
1930 * We are going to adapt the reader page update process where:
1931 * 1. We first splice the start and end of list of new pages between
1932 * the head page and its previous page.
1933 * 2. We cmpxchg the prev_page->next to point from head page to the
1934 * start of new pages list.
1935 * 3. Finally, we update the head->prev to the end of new list.
1936 *
1937 * We will try this process 10 times, to make sure that we don't keep
1938 * spinning.
1939 */
1940 retries = 10;
bc92b956 1941 success = false;
5040b4b7 1942 while (retries--) {
bdf4fb62 1943 struct list_head *head_page, *prev_page;
5040b4b7
VN
1944 struct list_head *last_page, *first_page;
1945 struct list_head *head_page_with_bit;
625ed527 1946 struct buffer_page *hpage = rb_set_head_page(cpu_buffer);
77ae365e 1947
625ed527 1948 if (!hpage)
54f7be5b 1949 break;
625ed527 1950 head_page = &hpage->list;
5040b4b7
VN
1951 prev_page = head_page->prev;
1952
1953 first_page = pages->next;
1954 last_page = pages->prev;
1955
1956 head_page_with_bit = (struct list_head *)
1957 ((unsigned long)head_page | RB_PAGE_HEAD);
1958
1959 last_page->next = head_page_with_bit;
1960 first_page->prev = prev_page;
1961
bdf4fb62
UB
1962 /* caution: head_page_with_bit gets updated on cmpxchg failure */
1963 if (try_cmpxchg(&prev_page->next,
1964 &head_page_with_bit, first_page)) {
5040b4b7
VN
1965 /*
1966 * yay, we replaced the page pointer to our new list,
1967 * now, we just have to update to head page's prev
1968 * pointer to point to end of list
1969 */
1970 head_page->prev = last_page;
bc92b956 1971 success = true;
5040b4b7
VN
1972 break;
1973 }
7a8e76a3 1974 }
7a8e76a3 1975
5040b4b7
VN
1976 if (success)
1977 INIT_LIST_HEAD(pages);
1978 /*
1979 * If we weren't successful in adding in new pages, warn and stop
1980 * tracing
1981 */
1982 RB_WARN_ON(cpu_buffer, !success);
88ca6a71 1983 raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
5040b4b7
VN
1984
1985 /* free pages if they weren't inserted */
1986 if (!success) {
1987 struct buffer_page *bpage, *tmp;
1988 list_for_each_entry_safe(bpage, tmp, &cpu_buffer->new_pages,
1989 list) {
1990 list_del_init(&bpage->list);
1991 free_buffer_page(bpage);
1992 }
1993 }
1994 return success;
7a8e76a3
SR
1995}
1996
83f40318 1997static void rb_update_pages(struct ring_buffer_per_cpu *cpu_buffer)
438ced17 1998{
bc92b956 1999 bool success;
5040b4b7 2000
438ced17 2001 if (cpu_buffer->nr_pages_to_update > 0)
5040b4b7 2002 success = rb_insert_pages(cpu_buffer);
438ced17 2003 else
5040b4b7
VN
2004 success = rb_remove_pages(cpu_buffer,
2005 -cpu_buffer->nr_pages_to_update);
83f40318 2006
5040b4b7
VN
2007 if (success)
2008 cpu_buffer->nr_pages += cpu_buffer->nr_pages_to_update;
83f40318
VN
2009}
2010
2011static void update_pages_handler(struct work_struct *work)
2012{
2013 struct ring_buffer_per_cpu *cpu_buffer = container_of(work,
2014 struct ring_buffer_per_cpu, update_pages_work);
2015 rb_update_pages(cpu_buffer);
05fdd70d 2016 complete(&cpu_buffer->update_done);
438ced17
VN
2017}
2018
7a8e76a3
SR
2019/**
2020 * ring_buffer_resize - resize the ring buffer
2021 * @buffer: the buffer to resize.
2022 * @size: the new size.
d611851b 2023 * @cpu_id: the cpu buffer to resize
7a8e76a3 2024 *
139f8400 2025 * Minimum size is 2 * buffer->subbuf_size.
7a8e76a3 2026 *
83f40318 2027 * Returns 0 on success and < 0 on failure.
7a8e76a3 2028 */
13292494 2029int ring_buffer_resize(struct trace_buffer *buffer, unsigned long size,
438ced17 2030 int cpu_id)
7a8e76a3
SR
2031{
2032 struct ring_buffer_per_cpu *cpu_buffer;
9b94a8fb 2033 unsigned long nr_pages;
0a1754b2 2034 int cpu, err;
7a8e76a3 2035
ee51a1de
IM
2036 /*
2037 * Always succeed at resizing a non-existent buffer:
2038 */
2039 if (!buffer)
0a1754b2 2040 return 0;
ee51a1de 2041
6a31e1f1
SR
2042 /* Make sure the requested buffer exists */
2043 if (cpu_id != RING_BUFFER_ALL_CPUS &&
2044 !cpumask_test_cpu(cpu_id, buffer->cpumask))
0a1754b2 2045 return 0;
6a31e1f1 2046
139f8400 2047 nr_pages = DIV_ROUND_UP(size, buffer->subbuf_size);
7a8e76a3
SR
2048
2049 /* we need a minimum of two pages */
59643d15
SRRH
2050 if (nr_pages < 2)
2051 nr_pages = 2;
7a8e76a3 2052
83f40318 2053 /* prevent another thread from changing buffer sizes */
7a8e76a3 2054 mutex_lock(&buffer->mutex);
8a96c028 2055 atomic_inc(&buffer->resizing);
07b8b10e 2056
438ced17 2057 if (cpu_id == RING_BUFFER_ALL_CPUS) {
07b8b10e
SRV
2058 /*
2059 * Don't succeed if resizing is disabled, as a reader might be
2060 * manipulating the ring buffer and is expecting a sane state while
2061 * this is true.
2062 */
2063 for_each_buffer_cpu(buffer, cpu) {
2064 cpu_buffer = buffer->buffers[cpu];
2065 if (atomic_read(&cpu_buffer->resize_disabled)) {
2066 err = -EBUSY;
2067 goto out_err_unlock;
2068 }
2069 }
2070
438ced17 2071 /* calculate the pages to update */
7a8e76a3
SR
2072 for_each_buffer_cpu(buffer, cpu) {
2073 cpu_buffer = buffer->buffers[cpu];
7a8e76a3 2074
438ced17
VN
2075 cpu_buffer->nr_pages_to_update = nr_pages -
2076 cpu_buffer->nr_pages;
438ced17
VN
2077 /*
2078 * nothing more to do for removing pages or no update
2079 */
2080 if (cpu_buffer->nr_pages_to_update <= 0)
2081 continue;
d7ec4bfe 2082 /*
438ced17
VN
2083 * to add pages, make sure all new pages can be
2084 * allocated without receiving ENOMEM
d7ec4bfe 2085 */
438ced17 2086 INIT_LIST_HEAD(&cpu_buffer->new_pages);
74e2afc6
QH
2087 if (__rb_allocate_pages(cpu_buffer, cpu_buffer->nr_pages_to_update,
2088 &cpu_buffer->new_pages)) {
438ced17 2089 /* not enough memory for new pages */
83f40318
VN
2090 err = -ENOMEM;
2091 goto out_err;
2092 }
f6bd2c92
ZY
2093
2094 cond_resched();
83f40318
VN
2095 }
2096
99c37d1a 2097 cpus_read_lock();
83f40318
VN
2098 /*
2099 * Fire off all the required work handlers
05fdd70d 2100 * We can't schedule on offline CPUs, but it's not necessary
83f40318
VN
2101 * since we can change their buffer sizes without any race.
2102 */
2103 for_each_buffer_cpu(buffer, cpu) {
2104 cpu_buffer = buffer->buffers[cpu];
05fdd70d 2105 if (!cpu_buffer->nr_pages_to_update)
83f40318
VN
2106 continue;
2107
021c5b34
CM
2108 /* Can't run something on an offline CPU. */
2109 if (!cpu_online(cpu)) {
f5eb5588
SRRH
2110 rb_update_pages(cpu_buffer);
2111 cpu_buffer->nr_pages_to_update = 0;
2112 } else {
88ca6a71
SR
2113 /* Run directly if possible. */
2114 migrate_disable();
2115 if (cpu != smp_processor_id()) {
2116 migrate_enable();
2117 schedule_work_on(cpu,
2118 &cpu_buffer->update_pages_work);
2119 } else {
2120 update_pages_handler(&cpu_buffer->update_pages_work);
2121 migrate_enable();
2122 }
f5eb5588 2123 }
7a8e76a3 2124 }
7a8e76a3 2125
438ced17
VN
2126 /* wait for all the updates to complete */
2127 for_each_buffer_cpu(buffer, cpu) {
2128 cpu_buffer = buffer->buffers[cpu];
05fdd70d 2129 if (!cpu_buffer->nr_pages_to_update)
83f40318
VN
2130 continue;
2131
05fdd70d
VN
2132 if (cpu_online(cpu))
2133 wait_for_completion(&cpu_buffer->update_done);
83f40318 2134 cpu_buffer->nr_pages_to_update = 0;
438ced17 2135 }
83f40318 2136
99c37d1a 2137 cpus_read_unlock();
438ced17
VN
2138 } else {
2139 cpu_buffer = buffer->buffers[cpu_id];
83f40318 2140
438ced17
VN
2141 if (nr_pages == cpu_buffer->nr_pages)
2142 goto out;
7a8e76a3 2143
07b8b10e
SRV
2144 /*
2145 * Don't succeed if resizing is disabled, as a reader might be
2146 * manipulating the ring buffer and is expecting a sane state while
2147 * this is true.
2148 */
2149 if (atomic_read(&cpu_buffer->resize_disabled)) {
2150 err = -EBUSY;
2151 goto out_err_unlock;
2152 }
2153
438ced17
VN
2154 cpu_buffer->nr_pages_to_update = nr_pages -
2155 cpu_buffer->nr_pages;
2156
2157 INIT_LIST_HEAD(&cpu_buffer->new_pages);
2158 if (cpu_buffer->nr_pages_to_update > 0 &&
74e2afc6
QH
2159 __rb_allocate_pages(cpu_buffer, cpu_buffer->nr_pages_to_update,
2160 &cpu_buffer->new_pages)) {
83f40318
VN
2161 err = -ENOMEM;
2162 goto out_err;
2163 }
438ced17 2164
99c37d1a 2165 cpus_read_lock();
83f40318 2166
021c5b34
CM
2167 /* Can't run something on an offline CPU. */
2168 if (!cpu_online(cpu_id))
f5eb5588
SRRH
2169 rb_update_pages(cpu_buffer);
2170 else {
88ca6a71
SR
2171 /* Run directly if possible. */
2172 migrate_disable();
2173 if (cpu_id == smp_processor_id()) {
2174 rb_update_pages(cpu_buffer);
2175 migrate_enable();
2176 } else {
2177 migrate_enable();
2178 schedule_work_on(cpu_id,
2179 &cpu_buffer->update_pages_work);
2180 wait_for_completion(&cpu_buffer->update_done);
2181 }
f5eb5588 2182 }
83f40318 2183
83f40318 2184 cpu_buffer->nr_pages_to_update = 0;
99c37d1a 2185 cpus_read_unlock();
438ced17 2186 }
7a8e76a3
SR
2187
2188 out:
659f451f
SR
2189 /*
2190 * The ring buffer resize can happen with the ring buffer
2191 * enabled, so that the update disturbs the tracing as little
2192 * as possible. But if the buffer is disabled, we do not need
2193 * to worry about that, and we can take the time to verify
2194 * that the buffer is not corrupt.
2195 */
2196 if (atomic_read(&buffer->record_disabled)) {
2197 atomic_inc(&buffer->record_disabled);
2198 /*
2199 * Even though the buffer was disabled, we must make sure
2200 * that it is truly disabled before calling rb_check_pages.
2201 * There could have been a race between checking
2202 * record_disable and incrementing it.
2203 */
74401729 2204 synchronize_rcu();
659f451f 2205 for_each_buffer_cpu(buffer, cpu) {
c2274b90
PP
2206 unsigned long flags;
2207
659f451f 2208 cpu_buffer = buffer->buffers[cpu];
c2274b90 2209 raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
659f451f 2210 rb_check_pages(cpu_buffer);
c2274b90 2211 raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
659f451f
SR
2212 }
2213 atomic_dec(&buffer->record_disabled);
2214 }
2215
8a96c028 2216 atomic_dec(&buffer->resizing);
7a8e76a3 2217 mutex_unlock(&buffer->mutex);
0a1754b2 2218 return 0;
7a8e76a3 2219
83f40318 2220 out_err:
438ced17
VN
2221 for_each_buffer_cpu(buffer, cpu) {
2222 struct buffer_page *bpage, *tmp;
83f40318 2223
438ced17 2224 cpu_buffer = buffer->buffers[cpu];
438ced17 2225 cpu_buffer->nr_pages_to_update = 0;
83f40318 2226
438ced17
VN
2227 if (list_empty(&cpu_buffer->new_pages))
2228 continue;
83f40318 2229
438ced17
VN
2230 list_for_each_entry_safe(bpage, tmp, &cpu_buffer->new_pages,
2231 list) {
2232 list_del_init(&bpage->list);
2233 free_buffer_page(bpage);
2234 }
7a8e76a3 2235 }
07b8b10e 2236 out_err_unlock:
8a96c028 2237 atomic_dec(&buffer->resizing);
641d2f63 2238 mutex_unlock(&buffer->mutex);
83f40318 2239 return err;
7a8e76a3 2240}
c4f50183 2241EXPORT_SYMBOL_GPL(ring_buffer_resize);
7a8e76a3 2242
13292494 2243void ring_buffer_change_overwrite(struct trace_buffer *buffer, int val)
750912fa
DS
2244{
2245 mutex_lock(&buffer->mutex);
2246 if (val)
2247 buffer->flags |= RB_FL_OVERWRITE;
2248 else
2249 buffer->flags &= ~RB_FL_OVERWRITE;
2250 mutex_unlock(&buffer->mutex);
2251}
2252EXPORT_SYMBOL_GPL(ring_buffer_change_overwrite);
2253
2289d567 2254static __always_inline void *__rb_page_index(struct buffer_page *bpage, unsigned index)
7a8e76a3 2255{
044fa782 2256 return bpage->page->data + index;
7a8e76a3
SR
2257}
2258
2289d567 2259static __always_inline struct ring_buffer_event *
d769041f 2260rb_reader_event(struct ring_buffer_per_cpu *cpu_buffer)
7a8e76a3 2261{
6f807acd
SR
2262 return __rb_page_index(cpu_buffer->reader_page,
2263 cpu_buffer->reader_page->read);
2264}
2265
785888c5
SRV
2266static struct ring_buffer_event *
2267rb_iter_head_event(struct ring_buffer_iter *iter)
bf41a158 2268{
785888c5
SRV
2269 struct ring_buffer_event *event;
2270 struct buffer_page *iter_head_page = iter->head_page;
2271 unsigned long commit;
2272 unsigned length;
2273
153368ce
SRV
2274 if (iter->head != iter->next_event)
2275 return iter->event;
2276
785888c5
SRV
2277 /*
2278 * When the writer goes across pages, it issues a cmpxchg which
2279 * is a mb(), which will synchronize with the rmb here.
2280 * (see rb_tail_page_update() and __rb_reserve_next())
2281 */
2282 commit = rb_page_commit(iter_head_page);
2283 smp_rmb();
95a404bd
SRG
2284
2285 /* An event needs to be at least 8 bytes in size */
2286 if (iter->head > commit - 8)
2287 goto reset;
2288
785888c5
SRV
2289 event = __rb_page_index(iter_head_page, iter->head);
2290 length = rb_event_length(event);
2291
2292 /*
2293 * READ_ONCE() doesn't work on functions and we don't want the
2294 * compiler doing any crazy optimizations with length.
2295 */
2296 barrier();
2297
139f8400 2298 if ((iter->head + length) > commit || length > iter->event_size)
785888c5
SRV
2299 /* Writer corrupted the read? */
2300 goto reset;
2301
2302 memcpy(iter->event, event, length);
2303 /*
2304 * If the page stamp is still the same after this rmb() then the
2305 * event was safely copied without the writer entering the page.
2306 */
2307 smp_rmb();
2308
2309 /* Make sure the page didn't change since we read this */
2310 if (iter->page_stamp != iter_head_page->page->time_stamp ||
2311 commit > rb_page_commit(iter_head_page))
2312 goto reset;
2313
2314 iter->next_event = iter->head + length;
2315 return iter->event;
2316 reset:
2317 /* Reset to the beginning */
2318 iter->page_stamp = iter->read_stamp = iter->head_page->page->time_stamp;
2319 iter->head = 0;
2320 iter->next_event = 0;
c9b7a4a7 2321 iter->missed_events = 1;
785888c5 2322 return NULL;
bf41a158
SR
2323}
2324
25985edc 2325/* Size is determined by what has been committed */
2289d567 2326static __always_inline unsigned rb_page_size(struct buffer_page *bpage)
bf41a158 2327{
fe832be0 2328 return rb_page_commit(bpage) & ~RB_MISSED_MASK;
bf41a158
SR
2329}
2330
2289d567 2331static __always_inline unsigned
bf41a158
SR
2332rb_commit_index(struct ring_buffer_per_cpu *cpu_buffer)
2333{
2334 return rb_page_commit(cpu_buffer->commit_page);
2335}
2336
2289d567 2337static __always_inline unsigned
3cb30911 2338rb_event_index(struct ring_buffer_per_cpu *cpu_buffer, struct ring_buffer_event *event)
bf41a158
SR
2339{
2340 unsigned long addr = (unsigned long)event;
2341
3cb30911
SRG
2342 addr &= (PAGE_SIZE << cpu_buffer->buffer->subbuf_order) - 1;
2343
2344 return addr - BUF_PAGE_HDR_SIZE;
bf41a158
SR
2345}
2346
34a148bf 2347static void rb_inc_iter(struct ring_buffer_iter *iter)
d769041f
SR
2348{
2349 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
2350
2351 /*
2352 * The iterator could be on the reader page (it starts there).
2353 * But the head could have moved, since the reader was
2354 * found. Check for this case and assign the iterator
2355 * to the head page instead of next.
2356 */
2357 if (iter->head_page == cpu_buffer->reader_page)
77ae365e 2358 iter->head_page = rb_set_head_page(cpu_buffer);
d769041f 2359 else
6689bed3 2360 rb_inc_page(&iter->head_page);
d769041f 2361
28e3fc56 2362 iter->page_stamp = iter->read_stamp = iter->head_page->page->time_stamp;
7a8e76a3 2363 iter->head = 0;
785888c5 2364 iter->next_event = 0;
7a8e76a3
SR
2365}
2366
77ae365e
SR
2367/*
2368 * rb_handle_head_page - writer hit the head page
2369 *
2370 * Returns: +1 to retry page
2371 * 0 to continue
2372 * -1 on error
2373 */
2374static int
2375rb_handle_head_page(struct ring_buffer_per_cpu *cpu_buffer,
2376 struct buffer_page *tail_page,
2377 struct buffer_page *next_page)
2378{
2379 struct buffer_page *new_head;
2380 int entries;
2381 int type;
2382 int ret;
2383
2384 entries = rb_page_entries(next_page);
2385
2386 /*
2387 * The hard part is here. We need to move the head
2388 * forward, and protect against both readers on
2389 * other CPUs and writers coming in via interrupts.
2390 */
2391 type = rb_head_page_set_update(cpu_buffer, next_page, tail_page,
2392 RB_PAGE_HEAD);
2393
2394 /*
2395 * type can be one of four:
2396 * NORMAL - an interrupt already moved it for us
2397 * HEAD - we are the first to get here.
2398 * UPDATE - we are the interrupt interrupting
2399 * a current move.
2400 * MOVED - a reader on another CPU moved the next
2401 * pointer to its reader page. Give up
2402 * and try again.
2403 */
2404
2405 switch (type) {
2406 case RB_PAGE_HEAD:
2407 /*
2408 * We changed the head to UPDATE, thus
2409 * it is our responsibility to update
2410 * the counters.
2411 */
2412 local_add(entries, &cpu_buffer->overrun);
45d99ea4 2413 local_sub(rb_page_commit(next_page), &cpu_buffer->entries_bytes);
31029a8b 2414 local_inc(&cpu_buffer->pages_lost);
77ae365e
SR
2415
2416 /*
2417 * The entries will be zeroed out when we move the
2418 * tail page.
2419 */
2420
2421 /* still more to do */
2422 break;
2423
2424 case RB_PAGE_UPDATE:
2425 /*
2426 * This is an interrupt that interrupt the
2427 * previous update. Still more to do.
2428 */
2429 break;
2430 case RB_PAGE_NORMAL:
2431 /*
2432 * An interrupt came in before the update
2433 * and processed this for us.
2434 * Nothing left to do.
2435 */
2436 return 1;
2437 case RB_PAGE_MOVED:
2438 /*
2439 * The reader is on another CPU and just did
2440 * a swap with our next_page.
2441 * Try again.
2442 */
2443 return 1;
2444 default:
2445 RB_WARN_ON(cpu_buffer, 1); /* WTF??? */
2446 return -1;
2447 }
2448
2449 /*
2450 * Now that we are here, the old head pointer is
2451 * set to UPDATE. This will keep the reader from
2452 * swapping the head page with the reader page.
2453 * The reader (on another CPU) will spin till
2454 * we are finished.
2455 *
2456 * We just need to protect against interrupts
2457 * doing the job. We will set the next pointer
2458 * to HEAD. After that, we set the old pointer
2459 * to NORMAL, but only if it was HEAD before.
2460 * otherwise we are an interrupt, and only
2461 * want the outer most commit to reset it.
2462 */
2463 new_head = next_page;
6689bed3 2464 rb_inc_page(&new_head);
77ae365e
SR
2465
2466 ret = rb_head_page_set_head(cpu_buffer, new_head, next_page,
2467 RB_PAGE_NORMAL);
2468
2469 /*
2470 * Valid returns are:
2471 * HEAD - an interrupt came in and already set it.
2472 * NORMAL - One of two things:
2473 * 1) We really set it.
2474 * 2) A bunch of interrupts came in and moved
2475 * the page forward again.
2476 */
2477 switch (ret) {
2478 case RB_PAGE_HEAD:
2479 case RB_PAGE_NORMAL:
2480 /* OK */
2481 break;
2482 default:
2483 RB_WARN_ON(cpu_buffer, 1);
2484 return -1;
2485 }
2486
2487 /*
2488 * It is possible that an interrupt came in,
2489 * set the head up, then more interrupts came in
2490 * and moved it again. When we get back here,
2491 * the page would have been set to NORMAL but we
2492 * just set it back to HEAD.
2493 *
2494 * How do you detect this? Well, if that happened
2495 * the tail page would have moved.
2496 */
2497 if (ret == RB_PAGE_NORMAL) {
8573636e
SRRH
2498 struct buffer_page *buffer_tail_page;
2499
2500 buffer_tail_page = READ_ONCE(cpu_buffer->tail_page);
77ae365e
SR
2501 /*
2502 * If the tail had moved passed next, then we need
2503 * to reset the pointer.
2504 */
8573636e
SRRH
2505 if (buffer_tail_page != tail_page &&
2506 buffer_tail_page != next_page)
77ae365e
SR
2507 rb_head_page_set_normal(cpu_buffer, new_head,
2508 next_page,
2509 RB_PAGE_HEAD);
2510 }
2511
2512 /*
2513 * If this was the outer most commit (the one that
2514 * changed the original pointer from HEAD to UPDATE),
2515 * then it is up to us to reset it to NORMAL.
2516 */
2517 if (type == RB_PAGE_HEAD) {
2518 ret = rb_head_page_set_normal(cpu_buffer, next_page,
2519 tail_page,
2520 RB_PAGE_UPDATE);
2521 if (RB_WARN_ON(cpu_buffer,
2522 ret != RB_PAGE_UPDATE))
2523 return -1;
2524 }
2525
2526 return 0;
2527}
2528
c7b09308
SR
2529static inline void
2530rb_reset_tail(struct ring_buffer_per_cpu *cpu_buffer,
fcc742ea 2531 unsigned long tail, struct rb_event_info *info)
c7b09308 2532{
139f8400 2533 unsigned long bsize = READ_ONCE(cpu_buffer->buffer->subbuf_size);
fcc742ea 2534 struct buffer_page *tail_page = info->tail_page;
c7b09308 2535 struct ring_buffer_event *event;
fcc742ea 2536 unsigned long length = info->length;
c7b09308
SR
2537
2538 /*
2539 * Only the event that crossed the page boundary
2540 * must fill the old tail_page with padding.
2541 */
139f8400 2542 if (tail >= bsize) {
b3230c8b
SR
2543 /*
2544 * If the page was filled, then we still need
2545 * to update the real_end. Reset it to zero
2546 * and the reader will ignore it.
2547 */
139f8400 2548 if (tail == bsize)
b3230c8b
SR
2549 tail_page->real_end = 0;
2550
c7b09308
SR
2551 local_sub(length, &tail_page->write);
2552 return;
2553 }
2554
2555 event = __rb_page_index(tail_page, tail);
2556
ff0ff84a
SR
2557 /*
2558 * Save the original length to the meta data.
2559 * This will be used by the reader to add lost event
2560 * counter.
2561 */
2562 tail_page->real_end = tail;
2563
c7b09308
SR
2564 /*
2565 * If this event is bigger than the minimum size, then
2566 * we need to be careful that we don't subtract the
2567 * write counter enough to allow another writer to slip
2568 * in on this page.
2569 * We put in a discarded commit instead, to make sure
45d99ea4
ZY
2570 * that this space is not used again, and this space will
2571 * not be accounted into 'entries_bytes'.
c7b09308
SR
2572 *
2573 * If we are less than the minimum size, we don't need to
2574 * worry about it.
2575 */
139f8400 2576 if (tail > (bsize - RB_EVNT_MIN_SIZE)) {
c7b09308
SR
2577 /* No room for any events */
2578
2579 /* Mark the rest of the page with padding */
2580 rb_event_set_padding(event);
2581
a0fcaaed
SRG
2582 /* Make sure the padding is visible before the write update */
2583 smp_wmb();
2584
c7b09308
SR
2585 /* Set the write back to the previous setting */
2586 local_sub(length, &tail_page->write);
2587 return;
2588 }
2589
2590 /* Put in a discarded event */
139f8400 2591 event->array[0] = (bsize - tail) - RB_EVNT_HDR_SIZE;
c7b09308
SR
2592 event->type_len = RINGBUF_TYPE_PADDING;
2593 /* time delta must be non zero */
2594 event->time_delta = 1;
c7b09308 2595
45d99ea4 2596 /* account for padding bytes */
139f8400 2597 local_add(bsize - tail, &cpu_buffer->entries_bytes);
45d99ea4 2598
a0fcaaed
SRG
2599 /* Make sure the padding is visible before the tail_page->write update */
2600 smp_wmb();
2601
c7b09308 2602 /* Set write to end of buffer */
139f8400 2603 length = (tail + length) - bsize;
c7b09308
SR
2604 local_sub(length, &tail_page->write);
2605}
6634ff26 2606
4239c38f
SRRH
2607static inline void rb_end_commit(struct ring_buffer_per_cpu *cpu_buffer);
2608
747e94ae
SR
2609/*
2610 * This is the slow path, force gcc not to inline it.
2611 */
2612static noinline struct ring_buffer_event *
6634ff26 2613rb_move_tail(struct ring_buffer_per_cpu *cpu_buffer,
fcc742ea 2614 unsigned long tail, struct rb_event_info *info)
7a8e76a3 2615{
fcc742ea 2616 struct buffer_page *tail_page = info->tail_page;
5a50e33c 2617 struct buffer_page *commit_page = cpu_buffer->commit_page;
13292494 2618 struct trace_buffer *buffer = cpu_buffer->buffer;
77ae365e
SR
2619 struct buffer_page *next_page;
2620 int ret;
aa20ae84
SR
2621
2622 next_page = tail_page;
2623
6689bed3 2624 rb_inc_page(&next_page);
aa20ae84 2625
aa20ae84
SR
2626 /*
2627 * If for some reason, we had an interrupt storm that made
2628 * it all the way around the buffer, bail, and warn
2629 * about it.
2630 */
2631 if (unlikely(next_page == commit_page)) {
77ae365e 2632 local_inc(&cpu_buffer->commit_overrun);
aa20ae84
SR
2633 goto out_reset;
2634 }
2635
77ae365e
SR
2636 /*
2637 * This is where the fun begins!
2638 *
2639 * We are fighting against races between a reader that
2640 * could be on another CPU trying to swap its reader
2641 * page with the buffer head.
2642 *
2643 * We are also fighting against interrupts coming in and
2644 * moving the head or tail on us as well.
2645 *
2646 * If the next page is the head page then we have filled
2647 * the buffer, unless the commit page is still on the
2648 * reader page.
2649 */
6689bed3 2650 if (rb_is_head_page(next_page, &tail_page->list)) {
aa20ae84 2651
77ae365e
SR
2652 /*
2653 * If the commit is not on the reader page, then
2654 * move the header page.
2655 */
2656 if (!rb_is_reader_page(cpu_buffer->commit_page)) {
2657 /*
2658 * If we are not in overwrite mode,
2659 * this is easy, just stop here.
2660 */
884bfe89
SP
2661 if (!(buffer->flags & RB_FL_OVERWRITE)) {
2662 local_inc(&cpu_buffer->dropped_events);
77ae365e 2663 goto out_reset;
884bfe89 2664 }
77ae365e
SR
2665
2666 ret = rb_handle_head_page(cpu_buffer,
2667 tail_page,
2668 next_page);
2669 if (ret < 0)
2670 goto out_reset;
2671 if (ret)
2672 goto out_again;
2673 } else {
2674 /*
2675 * We need to be careful here too. The
2676 * commit page could still be on the reader
2677 * page. We could have a small buffer, and
2678 * have filled up the buffer with events
2679 * from interrupts and such, and wrapped.
2680 *
c6358bac 2681 * Note, if the tail page is also on the
77ae365e
SR
2682 * reader_page, we let it move out.
2683 */
2684 if (unlikely((cpu_buffer->commit_page !=
2685 cpu_buffer->tail_page) &&
2686 (cpu_buffer->commit_page ==
2687 cpu_buffer->reader_page))) {
2688 local_inc(&cpu_buffer->commit_overrun);
2689 goto out_reset;
2690 }
aa20ae84
SR
2691 }
2692 }
2693
70004986 2694 rb_tail_page_update(cpu_buffer, tail_page, next_page);
aa20ae84 2695
77ae365e 2696 out_again:
aa20ae84 2697
fcc742ea 2698 rb_reset_tail(cpu_buffer, tail, info);
aa20ae84 2699
4239c38f
SRRH
2700 /* Commit what we have for now. */
2701 rb_end_commit(cpu_buffer);
2702 /* rb_end_commit() decs committing */
2703 local_inc(&cpu_buffer->committing);
2704
aa20ae84
SR
2705 /* fail and let the caller try again */
2706 return ERR_PTR(-EAGAIN);
2707
45141d46 2708 out_reset:
6f3b3440 2709 /* reset write */
fcc742ea 2710 rb_reset_tail(cpu_buffer, tail, info);
6f3b3440 2711
bf41a158 2712 return NULL;
7a8e76a3
SR
2713}
2714
74e87937
SRV
2715/* Slow path */
2716static struct ring_buffer_event *
3cb30911
SRG
2717rb_add_time_stamp(struct ring_buffer_per_cpu *cpu_buffer,
2718 struct ring_buffer_event *event, u64 delta, bool abs)
9826b273 2719{
dc4e2801
TZ
2720 if (abs)
2721 event->type_len = RINGBUF_TYPE_TIME_STAMP;
2722 else
2723 event->type_len = RINGBUF_TYPE_TIME_EXTEND;
9826b273 2724
dc4e2801 2725 /* Not the first event on the page, or not delta? */
3cb30911 2726 if (abs || rb_event_index(cpu_buffer, event)) {
d90fd774
SRRH
2727 event->time_delta = delta & TS_MASK;
2728 event->array[0] = delta >> TS_SHIFT;
2729 } else {
2730 /* nope, just zero it */
2731 event->time_delta = 0;
2732 event->array[0] = 0;
2733 }
a4543a2f 2734
d90fd774
SRRH
2735 return skip_time_extend(event);
2736}
a4543a2f 2737
58fbc3c6
SRV
2738#ifndef CONFIG_HAVE_UNSTABLE_SCHED_CLOCK
2739static inline bool sched_clock_stable(void)
2740{
2741 return true;
2742}
2743#endif
2744
74e87937 2745static void
58fbc3c6
SRV
2746rb_check_timestamp(struct ring_buffer_per_cpu *cpu_buffer,
2747 struct rb_event_info *info)
2748{
2749 u64 write_stamp;
2750
29ce2451 2751 WARN_ONCE(1, "Delta way too big! %llu ts=%llu before=%llu after=%llu write stamp=%llu\n%s",
58fbc3c6
SRV
2752 (unsigned long long)info->delta,
2753 (unsigned long long)info->ts,
2754 (unsigned long long)info->before,
2755 (unsigned long long)info->after,
c84897c0 2756 (unsigned long long)({rb_time_read(&cpu_buffer->write_stamp, &write_stamp); write_stamp;}),
58fbc3c6
SRV
2757 sched_clock_stable() ? "" :
2758 "If you just came from a suspend/resume,\n"
2759 "please switch to the trace global clock:\n"
2455f0e1 2760 " echo global > /sys/kernel/tracing/trace_clock\n"
58fbc3c6
SRV
2761 "or add trace_clock=global to the kernel command line\n");
2762}
2763
74e87937
SRV
2764static void rb_add_timestamp(struct ring_buffer_per_cpu *cpu_buffer,
2765 struct ring_buffer_event **event,
2766 struct rb_event_info *info,
2767 u64 *delta,
2768 unsigned int *length)
2769{
2770 bool abs = info->add_timestamp &
2771 (RB_ADD_STAMP_FORCE | RB_ADD_STAMP_ABSOLUTE);
2772
29ce2451 2773 if (unlikely(info->delta > (1ULL << 59))) {
6695da58
SRG
2774 /*
2775 * Some timers can use more than 59 bits, and when a timestamp
2776 * is added to the buffer, it will lose those bits.
2777 */
2778 if (abs && (info->ts & TS_MSB)) {
2779 info->delta &= ABS_TS_MASK;
2780
29ce2451 2781 /* did the clock go backwards */
6695da58 2782 } else if (info->before == info->after && info->before > info->ts) {
29ce2451
SRV
2783 /* not interrupted */
2784 static int once;
2785
2786 /*
2787 * This is possible with a recalibrating of the TSC.
2788 * Do not produce a call stack, but just report it.
2789 */
2790 if (!once) {
2791 once++;
2792 pr_warn("Ring buffer clock went backwards: %llu -> %llu\n",
2793 info->before, info->ts);
2794 }
2795 } else
2796 rb_check_timestamp(cpu_buffer, info);
2797 if (!abs)
2798 info->delta = 0;
2799 }
3cb30911 2800 *event = rb_add_time_stamp(cpu_buffer, *event, info->delta, abs);
74e87937
SRV
2801 *length -= RB_LEN_TIME_EXTEND;
2802 *delta = 0;
2803}
2804
d90fd774
SRRH
2805/**
2806 * rb_update_event - update event type and data
cfc585a4 2807 * @cpu_buffer: The per cpu buffer of the @event
d90fd774 2808 * @event: the event to update
cfc585a4 2809 * @info: The info to update the @event with (contains length and delta)
d90fd774 2810 *
cfc585a4 2811 * Update the type and data fields of the @event. The length
d90fd774
SRRH
2812 * is the actual size that is written to the ring buffer,
2813 * and with this, we can determine what to place into the
2814 * data field.
2815 */
b7dc42fd 2816static void
d90fd774
SRRH
2817rb_update_event(struct ring_buffer_per_cpu *cpu_buffer,
2818 struct ring_buffer_event *event,
2819 struct rb_event_info *info)
2820{
2821 unsigned length = info->length;
2822 u64 delta = info->delta;
8672e494
SRV
2823 unsigned int nest = local_read(&cpu_buffer->committing) - 1;
2824
a948c69d 2825 if (!WARN_ON_ONCE(nest >= MAX_NEST))
8672e494 2826 cpu_buffer->event_stamp[nest] = info->ts;
a4543a2f
SRRH
2827
2828 /*
d90fd774 2829 * If we need to add a timestamp, then we
6167c205 2830 * add it to the start of the reserved space.
a4543a2f 2831 */
74e87937
SRV
2832 if (unlikely(info->add_timestamp))
2833 rb_add_timestamp(cpu_buffer, &event, info, &delta, &length);
a4543a2f 2834
d90fd774
SRRH
2835 event->time_delta = delta;
2836 length -= RB_EVNT_HDR_SIZE;
adab66b7 2837 if (length > RB_MAX_SMALL_DATA || RB_FORCE_8BYTE_ALIGNMENT) {
d90fd774
SRRH
2838 event->type_len = 0;
2839 event->array[0] = length;
2840 } else
2841 event->type_len = DIV_ROUND_UP(length, RB_ALIGNMENT);
2842}
2843
2844static unsigned rb_calculate_event_length(unsigned length)
2845{
2846 struct ring_buffer_event event; /* Used only for sizeof array */
2847
2848 /* zero length can cause confusions */
2849 if (!length)
2850 length++;
2851
adab66b7 2852 if (length > RB_MAX_SMALL_DATA || RB_FORCE_8BYTE_ALIGNMENT)
d90fd774
SRRH
2853 length += sizeof(event.array[0]);
2854
2855 length += RB_EVNT_HDR_SIZE;
adab66b7 2856 length = ALIGN(length, RB_ARCH_ALIGNMENT);
d90fd774
SRRH
2857
2858 /*
2859 * In case the time delta is larger than the 27 bits for it
2860 * in the header, we need to add a timestamp. If another
2861 * event comes in when trying to discard this one to increase
2862 * the length, then the timestamp will be added in the allocated
2863 * space of this event. If length is bigger than the size needed
2864 * for the TIME_EXTEND, then padding has to be used. The events
2865 * length must be either RB_LEN_TIME_EXTEND, or greater than or equal
2866 * to RB_LEN_TIME_EXTEND + 8, as 8 is the minimum size for padding.
2867 * As length is a multiple of 4, we only need to worry if it
2868 * is 12 (RB_LEN_TIME_EXTEND + 4).
2869 */
2870 if (length == RB_LEN_TIME_EXTEND + RB_ALIGNMENT)
2871 length += RB_ALIGNMENT;
2872
2873 return length;
2874}
2875
bc92b956 2876static inline bool
d90fd774
SRRH
2877rb_try_to_discard(struct ring_buffer_per_cpu *cpu_buffer,
2878 struct ring_buffer_event *event)
2879{
2880 unsigned long new_index, old_index;
2881 struct buffer_page *bpage;
d90fd774
SRRH
2882 unsigned long addr;
2883
3cb30911 2884 new_index = rb_event_index(cpu_buffer, event);
d90fd774
SRRH
2885 old_index = new_index + rb_event_ts_length(event);
2886 addr = (unsigned long)event;
3cb30911 2887 addr &= ~((PAGE_SIZE << cpu_buffer->buffer->subbuf_order) - 1);
d90fd774 2888
8573636e 2889 bpage = READ_ONCE(cpu_buffer->tail_page);
d90fd774 2890
083e9f65
SRG
2891 /*
2892 * Make sure the tail_page is still the same and
2893 * the next write location is the end of this event
2894 */
d90fd774
SRRH
2895 if (bpage->page == (void *)addr && rb_page_write(bpage) == old_index) {
2896 unsigned long write_mask =
2897 local_read(&bpage->write) & ~RB_WRITE_MASK;
2898 unsigned long event_length = rb_event_length(event);
a389d86f 2899
b2dd7975
SRG
2900 /*
2901 * For the before_stamp to be different than the write_stamp
2902 * to make sure that the next event adds an absolute
2903 * value and does not rely on the saved write stamp, which
2904 * is now going to be bogus.
083e9f65
SRG
2905 *
2906 * By setting the before_stamp to zero, the next event
2907 * is not going to use the write_stamp and will instead
2908 * create an absolute timestamp. This means there's no
2909 * reason to update the wirte_stamp!
b2dd7975
SRG
2910 */
2911 rb_time_set(&cpu_buffer->before_stamp, 0);
2912
a389d86f
SRV
2913 /*
2914 * If an event were to come in now, it would see that the
2915 * write_stamp and the before_stamp are different, and assume
2916 * that this event just added itself before updating
2917 * the write stamp. The interrupting event will fix the
083e9f65 2918 * write stamp for us, and use an absolute timestamp.
a389d86f
SRV
2919 */
2920
d90fd774
SRRH
2921 /*
2922 * This is on the tail page. It is possible that
2923 * a write could come in and move the tail page
2924 * and write to the next page. That is fine
2925 * because we just shorten what is on this page.
2926 */
2927 old_index += write_mask;
2928 new_index += write_mask;
00a8478f
UB
2929
2930 /* caution: old_index gets updated on cmpxchg failure */
2931 if (local_try_cmpxchg(&bpage->write, &old_index, new_index)) {
d90fd774
SRRH
2932 /* update counters */
2933 local_sub(event_length, &cpu_buffer->entries_bytes);
bc92b956 2934 return true;
d90fd774
SRRH
2935 }
2936 }
2937
2938 /* could not discard */
bc92b956 2939 return false;
d90fd774
SRRH
2940}
2941
2942static void rb_start_commit(struct ring_buffer_per_cpu *cpu_buffer)
2943{
2944 local_inc(&cpu_buffer->committing);
2945 local_inc(&cpu_buffer->commits);
2946}
2947
38e11df1 2948static __always_inline void
d90fd774
SRRH
2949rb_set_commit_to_write(struct ring_buffer_per_cpu *cpu_buffer)
2950{
2951 unsigned long max_count;
2952
2953 /*
2954 * We only race with interrupts and NMIs on this CPU.
2955 * If we own the commit event, then we can commit
2956 * all others that interrupted us, since the interruptions
2957 * are in stack format (they finish before they come
2958 * back to us). This allows us to do a simple loop to
2959 * assign the commit to the tail.
2960 */
2961 again:
2962 max_count = cpu_buffer->nr_pages * 100;
2963
8573636e 2964 while (cpu_buffer->commit_page != READ_ONCE(cpu_buffer->tail_page)) {
d90fd774
SRRH
2965 if (RB_WARN_ON(cpu_buffer, !(--max_count)))
2966 return;
2967 if (RB_WARN_ON(cpu_buffer,
2968 rb_is_reader_page(cpu_buffer->tail_page)))
2969 return;
6455b616
ZY
2970 /*
2971 * No need for a memory barrier here, as the update
2972 * of the tail_page did it for this page.
2973 */
d90fd774
SRRH
2974 local_set(&cpu_buffer->commit_page->page->commit,
2975 rb_page_write(cpu_buffer->commit_page));
6689bed3 2976 rb_inc_page(&cpu_buffer->commit_page);
d90fd774
SRRH
2977 /* add barrier to keep gcc from optimizing too much */
2978 barrier();
2979 }
2980 while (rb_commit_index(cpu_buffer) !=
2981 rb_page_write(cpu_buffer->commit_page)) {
2982
6455b616
ZY
2983 /* Make sure the readers see the content of what is committed. */
2984 smp_wmb();
d90fd774
SRRH
2985 local_set(&cpu_buffer->commit_page->page->commit,
2986 rb_page_write(cpu_buffer->commit_page));
2987 RB_WARN_ON(cpu_buffer,
2988 local_read(&cpu_buffer->commit_page->page->commit) &
2989 ~RB_WRITE_MASK);
2990 barrier();
2991 }
2992
2993 /* again, keep gcc from optimizing */
2994 barrier();
2995
2996 /*
2997 * If an interrupt came in just after the first while loop
2998 * and pushed the tail page forward, we will be left with
2999 * a dangling commit that will never go forward.
3000 */
8573636e 3001 if (unlikely(cpu_buffer->commit_page != READ_ONCE(cpu_buffer->tail_page)))
d90fd774
SRRH
3002 goto again;
3003}
3004
38e11df1 3005static __always_inline void rb_end_commit(struct ring_buffer_per_cpu *cpu_buffer)
d90fd774
SRRH
3006{
3007 unsigned long commits;
3008
3009 if (RB_WARN_ON(cpu_buffer,
3010 !local_read(&cpu_buffer->committing)))
3011 return;
3012
3013 again:
3014 commits = local_read(&cpu_buffer->commits);
3015 /* synchronize with interrupts */
3016 barrier();
3017 if (local_read(&cpu_buffer->committing) == 1)
3018 rb_set_commit_to_write(cpu_buffer);
3019
3020 local_dec(&cpu_buffer->committing);
3021
3022 /* synchronize with interrupts */
3023 barrier();
3024
3025 /*
3026 * Need to account for interrupts coming in between the
3027 * updating of the commit page and the clearing of the
3028 * committing counter.
3029 */
3030 if (unlikely(local_read(&cpu_buffer->commits) != commits) &&
3031 !local_read(&cpu_buffer->committing)) {
3032 local_inc(&cpu_buffer->committing);
3033 goto again;
3034 }
3035}
3036
3037static inline void rb_event_discard(struct ring_buffer_event *event)
3038{
dc4e2801 3039 if (extended_time(event))
d90fd774
SRRH
3040 event = skip_time_extend(event);
3041
3042 /* array[0] holds the actual length for the discarded event */
3043 event->array[0] = rb_event_data_length(event) - RB_EVNT_HDR_SIZE;
3044 event->type_len = RINGBUF_TYPE_PADDING;
3045 /* time delta must be non zero */
3046 if (!event->time_delta)
3047 event->time_delta = 1;
3048}
3049
04aabc32 3050static void rb_commit(struct ring_buffer_per_cpu *cpu_buffer)
d90fd774
SRRH
3051{
3052 local_inc(&cpu_buffer->entries);
d90fd774
SRRH
3053 rb_end_commit(cpu_buffer);
3054}
3055
3056static __always_inline void
13292494 3057rb_wakeups(struct trace_buffer *buffer, struct ring_buffer_per_cpu *cpu_buffer)
d90fd774 3058{
d90fd774
SRRH
3059 if (buffer->irq_work.waiters_pending) {
3060 buffer->irq_work.waiters_pending = false;
3061 /* irq_work_queue() supplies it's own memory barriers */
3062 irq_work_queue(&buffer->irq_work.work);
3063 }
3064
3065 if (cpu_buffer->irq_work.waiters_pending) {
3066 cpu_buffer->irq_work.waiters_pending = false;
3067 /* irq_work_queue() supplies it's own memory barriers */
3068 irq_work_queue(&cpu_buffer->irq_work.work);
3069 }
3070
03329f99
SRV
3071 if (cpu_buffer->last_pages_touch == local_read(&cpu_buffer->pages_touched))
3072 return;
d90fd774 3073
03329f99
SRV
3074 if (cpu_buffer->reader_page == cpu_buffer->commit_page)
3075 return;
2c2b0a78 3076
03329f99
SRV
3077 if (!cpu_buffer->irq_work.full_waiters_pending)
3078 return;
2c2b0a78 3079
03329f99
SRV
3080 cpu_buffer->last_pages_touch = local_read(&cpu_buffer->pages_touched);
3081
42fb0a1e 3082 if (!full_hit(buffer, cpu_buffer->cpu, cpu_buffer->shortest_full))
03329f99
SRV
3083 return;
3084
3085 cpu_buffer->irq_work.wakeup_full = true;
3086 cpu_buffer->irq_work.full_waiters_pending = false;
3087 /* irq_work_queue() supplies it's own memory barriers */
3088 irq_work_queue(&cpu_buffer->irq_work.work);
d90fd774
SRRH
3089}
3090
28575c61
SRV
3091#ifdef CONFIG_RING_BUFFER_RECORD_RECURSION
3092# define do_ring_buffer_record_recursion() \
3093 do_ftrace_record_recursion(_THIS_IP_, _RET_IP_)
3094#else
3095# define do_ring_buffer_record_recursion() do { } while (0)
3096#endif
3097
d90fd774
SRRH
3098/*
3099 * The lock and unlock are done within a preempt disable section.
3100 * The current_context per_cpu variable can only be modified
3101 * by the current task between lock and unlock. But it can
a0e3a18f
SRV
3102 * be modified more than once via an interrupt. To pass this
3103 * information from the lock to the unlock without having to
3104 * access the 'in_interrupt()' functions again (which do show
3105 * a bit of overhead in something as critical as function tracing,
3106 * we use a bitmask trick.
d90fd774 3107 *
b02414c8
SRV
3108 * bit 1 = NMI context
3109 * bit 2 = IRQ context
3110 * bit 3 = SoftIRQ context
3111 * bit 4 = normal context.
d90fd774 3112 *
a0e3a18f
SRV
3113 * This works because this is the order of contexts that can
3114 * preempt other contexts. A SoftIRQ never preempts an IRQ
3115 * context.
3116 *
3117 * When the context is determined, the corresponding bit is
3118 * checked and set (if it was set, then a recursion of that context
3119 * happened).
3120 *
3121 * On unlock, we need to clear this bit. To do so, just subtract
3122 * 1 from the current_context and AND it to itself.
3123 *
3124 * (binary)
3125 * 101 - 1 = 100
3126 * 101 & 100 = 100 (clearing bit zero)
3127 *
3128 * 1010 - 1 = 1001
3129 * 1010 & 1001 = 1000 (clearing bit 1)
3130 *
3131 * The least significant bit can be cleared this way, and it
3132 * just so happens that it is the same bit corresponding to
3133 * the current context.
b02414c8
SRV
3134 *
3135 * Now the TRANSITION bit breaks the above slightly. The TRANSITION bit
3136 * is set when a recursion is detected at the current context, and if
3137 * the TRANSITION bit is already set, it will fail the recursion.
3138 * This is needed because there's a lag between the changing of
3139 * interrupt context and updating the preempt count. In this case,
3140 * a false positive will be found. To handle this, one extra recursion
3141 * is allowed, and this is done by the TRANSITION bit. If the TRANSITION
3142 * bit is already set, then it is considered a recursion and the function
3143 * ends. Otherwise, the TRANSITION bit is set, and that bit is returned.
3144 *
3145 * On the trace_recursive_unlock(), the TRANSITION bit will be the first
3146 * to be cleared. Even if it wasn't the context that set it. That is,
3147 * if an interrupt comes in while NORMAL bit is set and the ring buffer
3148 * is called before preempt_count() is updated, since the check will
3149 * be on the NORMAL bit, the TRANSITION bit will then be set. If an
3150 * NMI then comes in, it will set the NMI bit, but when the NMI code
f2cc020d 3151 * does the trace_recursive_unlock() it will clear the TRANSITION bit
b02414c8
SRV
3152 * and leave the NMI bit set. But this is fine, because the interrupt
3153 * code that set the TRANSITION bit will then clear the NMI bit when it
3154 * calls trace_recursive_unlock(). If another NMI comes in, it will
3155 * set the TRANSITION bit and continue.
3156 *
3157 * Note: The TRANSITION bit only handles a single transition between context.
d90fd774
SRRH
3158 */
3159
bc92b956 3160static __always_inline bool
d90fd774
SRRH
3161trace_recursive_lock(struct ring_buffer_per_cpu *cpu_buffer)
3162{
a0e3a18f 3163 unsigned int val = cpu_buffer->current_context;
91ebe8bc 3164 int bit = interrupt_context_level();
9b84fadc
SRV
3165
3166 bit = RB_CTX_NORMAL - bit;
a0e3a18f 3167
b02414c8
SRV
3168 if (unlikely(val & (1 << (bit + cpu_buffer->nest)))) {
3169 /*
3170 * It is possible that this was called by transitioning
3171 * between interrupt context, and preempt_count() has not
3172 * been updated yet. In this case, use the TRANSITION bit.
3173 */
3174 bit = RB_CTX_TRANSITION;
28575c61
SRV
3175 if (val & (1 << (bit + cpu_buffer->nest))) {
3176 do_ring_buffer_record_recursion();
bc92b956 3177 return true;
28575c61 3178 }
b02414c8 3179 }
d90fd774 3180
8e012066 3181 val |= (1 << (bit + cpu_buffer->nest));
a0e3a18f 3182 cpu_buffer->current_context = val;
d90fd774 3183
bc92b956 3184 return false;
d90fd774
SRRH
3185}
3186
3187static __always_inline void
3188trace_recursive_unlock(struct ring_buffer_per_cpu *cpu_buffer)
3189{
8e012066
SRV
3190 cpu_buffer->current_context &=
3191 cpu_buffer->current_context - (1 << cpu_buffer->nest);
3192}
3193
b02414c8
SRV
3194/* The recursive locking above uses 5 bits */
3195#define NESTED_BITS 5
8e012066
SRV
3196
3197/**
3198 * ring_buffer_nest_start - Allow to trace while nested
3199 * @buffer: The ring buffer to modify
3200 *
6167c205 3201 * The ring buffer has a safety mechanism to prevent recursion.
8e012066
SRV
3202 * But there may be a case where a trace needs to be done while
3203 * tracing something else. In this case, calling this function
3204 * will allow this function to nest within a currently active
3205 * ring_buffer_lock_reserve().
3206 *
3207 * Call this function before calling another ring_buffer_lock_reserve() and
3208 * call ring_buffer_nest_end() after the nested ring_buffer_unlock_commit().
3209 */
13292494 3210void ring_buffer_nest_start(struct trace_buffer *buffer)
8e012066
SRV
3211{
3212 struct ring_buffer_per_cpu *cpu_buffer;
3213 int cpu;
3214
3215 /* Enabled by ring_buffer_nest_end() */
3216 preempt_disable_notrace();
3217 cpu = raw_smp_processor_id();
3218 cpu_buffer = buffer->buffers[cpu];
6167c205 3219 /* This is the shift value for the above recursive locking */
8e012066
SRV
3220 cpu_buffer->nest += NESTED_BITS;
3221}
3222
3223/**
3224 * ring_buffer_nest_end - Allow to trace while nested
3225 * @buffer: The ring buffer to modify
3226 *
3227 * Must be called after ring_buffer_nest_start() and after the
3228 * ring_buffer_unlock_commit().
3229 */
13292494 3230void ring_buffer_nest_end(struct trace_buffer *buffer)
8e012066
SRV
3231{
3232 struct ring_buffer_per_cpu *cpu_buffer;
3233 int cpu;
3234
3235 /* disabled by ring_buffer_nest_start() */
3236 cpu = raw_smp_processor_id();
3237 cpu_buffer = buffer->buffers[cpu];
6167c205 3238 /* This is the shift value for the above recursive locking */
8e012066
SRV
3239 cpu_buffer->nest -= NESTED_BITS;
3240 preempt_enable_notrace();
d90fd774
SRRH
3241}
3242
3243/**
3244 * ring_buffer_unlock_commit - commit a reserved
3245 * @buffer: The buffer to commit to
d90fd774
SRRH
3246 *
3247 * This commits the data to the ring buffer, and releases any locks held.
3248 *
3249 * Must be paired with ring_buffer_lock_reserve.
3250 */
04aabc32 3251int ring_buffer_unlock_commit(struct trace_buffer *buffer)
d90fd774
SRRH
3252{
3253 struct ring_buffer_per_cpu *cpu_buffer;
3254 int cpu = raw_smp_processor_id();
3255
3256 cpu_buffer = buffer->buffers[cpu];
3257
04aabc32 3258 rb_commit(cpu_buffer);
d90fd774
SRRH
3259
3260 rb_wakeups(buffer, cpu_buffer);
3261
3262 trace_recursive_unlock(cpu_buffer);
3263
3264 preempt_enable_notrace();
3265
3266 return 0;
3267}
3268EXPORT_SYMBOL_GPL(ring_buffer_unlock_commit);
3269
5b7be9c7
SRV
3270/* Special value to validate all deltas on a page. */
3271#define CHECK_FULL_PAGE 1L
3272
3273#ifdef CONFIG_RING_BUFFER_VALIDATE_TIME_DELTAS
d40dbb61
SRG
3274
3275static const char *show_irq_str(int bits)
3276{
3277 const char *type[] = {
3278 ".", // 0
3279 "s", // 1
3280 "h", // 2
3281 "Hs", // 3
3282 "n", // 4
3283 "Ns", // 5
3284 "Nh", // 6
3285 "NHs", // 7
3286 };
3287
3288 return type[bits];
3289}
3290
3291/* Assume this is an trace event */
3292static const char *show_flags(struct ring_buffer_event *event)
3293{
3294 struct trace_entry *entry;
3295 int bits = 0;
3296
3297 if (rb_event_data_length(event) - RB_EVNT_HDR_SIZE < sizeof(*entry))
3298 return "X";
3299
3300 entry = ring_buffer_event_data(event);
3301
3302 if (entry->flags & TRACE_FLAG_SOFTIRQ)
3303 bits |= 1;
3304
3305 if (entry->flags & TRACE_FLAG_HARDIRQ)
3306 bits |= 2;
3307
3308 if (entry->flags & TRACE_FLAG_NMI)
3309 bits |= 4;
3310
3311 return show_irq_str(bits);
3312}
3313
3314static const char *show_irq(struct ring_buffer_event *event)
3315{
3316 struct trace_entry *entry;
3317
3318 if (rb_event_data_length(event) - RB_EVNT_HDR_SIZE < sizeof(*entry))
3319 return "";
3320
3321 entry = ring_buffer_event_data(event);
3322 if (entry->flags & TRACE_FLAG_IRQS_OFF)
3323 return "d";
3324 return "";
3325}
3326
3327static const char *show_interrupt_level(void)
3328{
3329 unsigned long pc = preempt_count();
3330 unsigned char level = 0;
3331
3332 if (pc & SOFTIRQ_OFFSET)
3333 level |= 1;
3334
3335 if (pc & HARDIRQ_MASK)
3336 level |= 2;
3337
3338 if (pc & NMI_MASK)
3339 level |= 4;
3340
3341 return show_irq_str(level);
3342}
3343
5b7be9c7
SRV
3344static void dump_buffer_page(struct buffer_data_page *bpage,
3345 struct rb_event_info *info,
3346 unsigned long tail)
3347{
3348 struct ring_buffer_event *event;
3349 u64 ts, delta;
3350 int e;
3351
3352 ts = bpage->time_stamp;
3353 pr_warn(" [%lld] PAGE TIME STAMP\n", ts);
3354
3355 for (e = 0; e < tail; e += rb_event_length(event)) {
3356
3357 event = (struct ring_buffer_event *)(bpage->data + e);
3358
3359 switch (event->type_len) {
3360
3361 case RINGBUF_TYPE_TIME_EXTEND:
e20044f7 3362 delta = rb_event_time_stamp(event);
5b7be9c7 3363 ts += delta;
0b9036ef
SRG
3364 pr_warn(" 0x%x: [%lld] delta:%lld TIME EXTEND\n",
3365 e, ts, delta);
5b7be9c7
SRV
3366 break;
3367
3368 case RINGBUF_TYPE_TIME_STAMP:
e20044f7 3369 delta = rb_event_time_stamp(event);
6695da58 3370 ts = rb_fix_abs_ts(delta, ts);
0b9036ef
SRG
3371 pr_warn(" 0x%x: [%lld] absolute:%lld TIME STAMP\n",
3372 e, ts, delta);
5b7be9c7
SRV
3373 break;
3374
3375 case RINGBUF_TYPE_PADDING:
3376 ts += event->time_delta;
0b9036ef
SRG
3377 pr_warn(" 0x%x: [%lld] delta:%d PADDING\n",
3378 e, ts, event->time_delta);
5b7be9c7
SRV
3379 break;
3380
3381 case RINGBUF_TYPE_DATA:
3382 ts += event->time_delta;
d40dbb61
SRG
3383 pr_warn(" 0x%x: [%lld] delta:%d %s%s\n",
3384 e, ts, event->time_delta,
3385 show_flags(event), show_irq(event));
5b7be9c7
SRV
3386 break;
3387
3388 default:
3389 break;
3390 }
3391 }
0b9036ef 3392 pr_warn("expected end:0x%lx last event actually ended at:0x%x\n", tail, e);
5b7be9c7
SRV
3393}
3394
3395static DEFINE_PER_CPU(atomic_t, checking);
3396static atomic_t ts_dump;
3397
f50345b4
SRG
3398#define buffer_warn_return(fmt, ...) \
3399 do { \
3400 /* If another report is happening, ignore this one */ \
3401 if (atomic_inc_return(&ts_dump) != 1) { \
3402 atomic_dec(&ts_dump); \
3403 goto out; \
3404 } \
3405 atomic_inc(&cpu_buffer->record_disabled); \
3406 pr_warn(fmt, ##__VA_ARGS__); \
3407 dump_buffer_page(bpage, info, tail); \
3408 atomic_dec(&ts_dump); \
3409 /* There's some cases in boot up that this can happen */ \
3410 if (WARN_ON_ONCE(system_state != SYSTEM_BOOTING)) \
3411 /* Do not re-enable checking */ \
3412 return; \
3413 } while (0)
3414
5b7be9c7
SRV
3415/*
3416 * Check if the current event time stamp matches the deltas on
3417 * the buffer page.
3418 */
3419static void check_buffer(struct ring_buffer_per_cpu *cpu_buffer,
3420 struct rb_event_info *info,
3421 unsigned long tail)
3422{
3423 struct ring_buffer_event *event;
3424 struct buffer_data_page *bpage;
3425 u64 ts, delta;
3426 bool full = false;
3427 int e;
3428
3429 bpage = info->tail_page->page;
3430
3431 if (tail == CHECK_FULL_PAGE) {
3432 full = true;
3433 tail = local_read(&bpage->commit);
3434 } else if (info->add_timestamp &
3435 (RB_ADD_STAMP_FORCE | RB_ADD_STAMP_ABSOLUTE)) {
3436 /* Ignore events with absolute time stamps */
3437 return;
3438 }
3439
3440 /*
3441 * Do not check the first event (skip possible extends too).
3442 * Also do not check if previous events have not been committed.
3443 */
3444 if (tail <= 8 || tail > local_read(&bpage->commit))
3445 return;
3446
3447 /*
083e9f65 3448 * If this interrupted another event,
5b7be9c7
SRV
3449 */
3450 if (atomic_inc_return(this_cpu_ptr(&checking)) != 1)
3451 goto out;
3452
3453 ts = bpage->time_stamp;
3454
3455 for (e = 0; e < tail; e += rb_event_length(event)) {
3456
3457 event = (struct ring_buffer_event *)(bpage->data + e);
3458
3459 switch (event->type_len) {
3460
3461 case RINGBUF_TYPE_TIME_EXTEND:
e20044f7 3462 delta = rb_event_time_stamp(event);
5b7be9c7
SRV
3463 ts += delta;
3464 break;
3465
3466 case RINGBUF_TYPE_TIME_STAMP:
e20044f7 3467 delta = rb_event_time_stamp(event);
f50345b4
SRG
3468 delta = rb_fix_abs_ts(delta, ts);
3469 if (delta < ts) {
3470 buffer_warn_return("[CPU: %d]ABSOLUTE TIME WENT BACKWARDS: last ts: %lld absolute ts: %lld\n",
3471 cpu_buffer->cpu, ts, delta);
3472 }
3473 ts = delta;
5b7be9c7
SRV
3474 break;
3475
3476 case RINGBUF_TYPE_PADDING:
3477 if (event->time_delta == 1)
3478 break;
957cdcd9 3479 fallthrough;
5b7be9c7
SRV
3480 case RINGBUF_TYPE_DATA:
3481 ts += event->time_delta;
3482 break;
3483
3484 default:
3485 RB_WARN_ON(cpu_buffer, 1);
3486 }
3487 }
3488 if ((full && ts > info->ts) ||
3489 (!full && ts + info->delta != info->ts)) {
f50345b4
SRG
3490 buffer_warn_return("[CPU: %d]TIME DOES NOT MATCH expected:%lld actual:%lld delta:%lld before:%lld after:%lld%s context:%s\n",
3491 cpu_buffer->cpu,
3492 ts + info->delta, info->ts, info->delta,
3493 info->before, info->after,
3494 full ? " (full)" : "", show_interrupt_level());
5b7be9c7
SRV
3495 }
3496out:
3497 atomic_dec(this_cpu_ptr(&checking));
3498}
3499#else
3500static inline void check_buffer(struct ring_buffer_per_cpu *cpu_buffer,
3501 struct rb_event_info *info,
3502 unsigned long tail)
3503{
3504}
3505#endif /* CONFIG_RING_BUFFER_VALIDATE_TIME_DELTAS */
3506
6634ff26
SR
3507static struct ring_buffer_event *
3508__rb_reserve_next(struct ring_buffer_per_cpu *cpu_buffer,
fcc742ea 3509 struct rb_event_info *info)
6634ff26 3510{
6634ff26 3511 struct ring_buffer_event *event;
fcc742ea 3512 struct buffer_page *tail_page;
a389d86f 3513 unsigned long tail, write, w;
69d1b839 3514
8573636e
SRRH
3515 /* Don't let the compiler play games with cpu_buffer->tail_page */
3516 tail_page = info->tail_page = READ_ONCE(cpu_buffer->tail_page);
a389d86f
SRV
3517
3518 /*A*/ w = local_read(&tail_page->write) & RB_WRITE_MASK;
3519 barrier();
c84897c0
SRG
3520 rb_time_read(&cpu_buffer->before_stamp, &info->before);
3521 rb_time_read(&cpu_buffer->write_stamp, &info->after);
a389d86f
SRV
3522 barrier();
3523 info->ts = rb_time_stamp(cpu_buffer->buffer);
3524
58fbc3c6 3525 if ((info->add_timestamp & RB_ADD_STAMP_ABSOLUTE)) {
a389d86f 3526 info->delta = info->ts;
a389d86f 3527 } else {
58fbc3c6
SRV
3528 /*
3529 * If interrupting an event time update, we may need an
3530 * absolute timestamp.
3531 * Don't bother if this is the start of a new page (w == 0).
3532 */
b3ae7b67
SRG
3533 if (!w) {
3534 /* Use the sub-buffer timestamp */
3535 info->delta = 0;
c84897c0 3536 } else if (unlikely(info->before != info->after)) {
58fbc3c6
SRV
3537 info->add_timestamp |= RB_ADD_STAMP_FORCE | RB_ADD_STAMP_EXTEND;
3538 info->length += RB_LEN_TIME_EXTEND;
3539 } else {
3540 info->delta = info->ts - info->after;
3541 if (unlikely(test_time_stamp(info->delta))) {
3542 info->add_timestamp |= RB_ADD_STAMP_EXTEND;
3543 info->length += RB_LEN_TIME_EXTEND;
3544 }
10464b4a 3545 }
7c4b4a51 3546 }
b7dc42fd 3547
10464b4a 3548 /*B*/ rb_time_set(&cpu_buffer->before_stamp, info->ts);
a389d86f
SRV
3549
3550 /*C*/ write = local_add_return(info->length, &tail_page->write);
77ae365e
SR
3551
3552 /* set write to only the index of the write */
3553 write &= RB_WRITE_MASK;
a389d86f 3554
fcc742ea 3555 tail = write - info->length;
6634ff26 3556
a389d86f 3557 /* See if we shot pass the end of this buffer page */
139f8400 3558 if (unlikely(write > cpu_buffer->buffer->subbuf_size)) {
9e45e39d 3559 check_buffer(cpu_buffer, info, CHECK_FULL_PAGE);
a389d86f
SRV
3560 return rb_move_tail(cpu_buffer, tail, info);
3561 }
3562
3563 if (likely(tail == w)) {
a389d86f 3564 /* Nothing interrupted us between A and C */
10464b4a 3565 /*D*/ rb_time_set(&cpu_buffer->write_stamp, info->ts);
dd939425
SRG
3566 /*
3567 * If something came in between C and D, the write stamp
3568 * may now not be in sync. But that's fine as the before_stamp
3569 * will be different and then next event will just be forced
3570 * to use an absolute timestamp.
3571 */
7c4b4a51
SRV
3572 if (likely(!(info->add_timestamp &
3573 (RB_ADD_STAMP_FORCE | RB_ADD_STAMP_ABSOLUTE))))
a389d86f 3574 /* This did not interrupt any time update */
58fbc3c6 3575 info->delta = info->ts - info->after;
a389d86f 3576 else
82db909e 3577 /* Just use full timestamp for interrupting event */
a389d86f 3578 info->delta = info->ts;
5b7be9c7 3579 check_buffer(cpu_buffer, info, tail);
a389d86f
SRV
3580 } else {
3581 u64 ts;
3582 /* SLOW PATH - Interrupted between A and C */
b803d7c6
SRG
3583
3584 /* Save the old before_stamp */
c84897c0 3585 rb_time_read(&cpu_buffer->before_stamp, &info->before);
b803d7c6
SRG
3586
3587 /*
3588 * Read a new timestamp and update the before_stamp to make
3589 * the next event after this one force using an absolute
3590 * timestamp. This is in case an interrupt were to come in
3591 * between E and F.
3592 */
a389d86f 3593 ts = rb_time_stamp(cpu_buffer->buffer);
b803d7c6
SRG
3594 rb_time_set(&cpu_buffer->before_stamp, ts);
3595
3596 barrier();
c84897c0 3597 /*E*/ rb_time_read(&cpu_buffer->write_stamp, &info->after);
a389d86f 3598 barrier();
b803d7c6
SRG
3599 /*F*/ if (write == (local_read(&tail_page->write) & RB_WRITE_MASK) &&
3600 info->after == info->before && info->after < ts) {
3601 /*
3602 * Nothing came after this event between C and F, it is
3603 * safe to use info->after for the delta as it
3604 * matched info->before and is still valid.
3605 */
58fbc3c6 3606 info->delta = ts - info->after;
a389d86f
SRV
3607 } else {
3608 /*
b803d7c6 3609 * Interrupted between C and F:
a389d86f
SRV
3610 * Lost the previous events time stamp. Just set the
3611 * delta to zero, and this will be the same time as
3612 * the event this event interrupted. And the events that
3613 * came after this will still be correct (as they would
3614 * have built their delta on the previous event.
3615 */
3616 info->delta = 0;
3617 }
8672e494 3618 info->ts = ts;
7c4b4a51 3619 info->add_timestamp &= ~RB_ADD_STAMP_FORCE;
a389d86f
SRV
3620 }
3621
6634ff26 3622 /*
a4543a2f 3623 * If this is the first commit on the page, then it has the same
b7dc42fd 3624 * timestamp as the page itself.
6634ff26 3625 */
7c4b4a51
SRV
3626 if (unlikely(!tail && !(info->add_timestamp &
3627 (RB_ADD_STAMP_FORCE | RB_ADD_STAMP_ABSOLUTE))))
a4543a2f
SRRH
3628 info->delta = 0;
3629
b7dc42fd
SRRH
3630 /* We reserved something on the buffer */
3631
3632 event = __rb_page_index(tail_page, tail);
a4543a2f
SRRH
3633 rb_update_event(cpu_buffer, event, info);
3634
3635 local_inc(&tail_page->entries);
6634ff26 3636
b7dc42fd
SRRH
3637 /*
3638 * If this is the first commit on the page, then update
3639 * its timestamp.
3640 */
75b21c6d 3641 if (unlikely(!tail))
b7dc42fd
SRRH
3642 tail_page->page->time_stamp = info->ts;
3643
c64e148a 3644 /* account for these added bytes */
fcc742ea 3645 local_add(info->length, &cpu_buffer->entries_bytes);
c64e148a 3646
6634ff26
SR
3647 return event;
3648}
3649
fa7ffb39 3650static __always_inline struct ring_buffer_event *
13292494 3651rb_reserve_next_event(struct trace_buffer *buffer,
62f0b3eb 3652 struct ring_buffer_per_cpu *cpu_buffer,
1cd8d735 3653 unsigned long length)
7a8e76a3
SR
3654{
3655 struct ring_buffer_event *event;
fcc742ea 3656 struct rb_event_info info;
818e3dd3 3657 int nr_loops = 0;
58fbc3c6 3658 int add_ts_default;
7a8e76a3 3659
71229230
SRG
3660 /* ring buffer does cmpxchg, make sure it is safe in NMI context */
3661 if (!IS_ENABLED(CONFIG_ARCH_HAVE_NMI_SAFE_CMPXCHG) &&
3662 (unlikely(in_nmi()))) {
3663 return NULL;
3664 }
3665
fa743953 3666 rb_start_commit(cpu_buffer);
a389d86f 3667 /* The commit page can not change after this */
fa743953 3668
85bac32c 3669#ifdef CONFIG_RING_BUFFER_ALLOW_SWAP
62f0b3eb
SR
3670 /*
3671 * Due to the ability to swap a cpu buffer from a buffer
3672 * it is possible it was swapped before we committed.
3673 * (committing stops a swap). We check for it here and
3674 * if it happened, we have to fail the write.
3675 */
3676 barrier();
6aa7de05 3677 if (unlikely(READ_ONCE(cpu_buffer->buffer) != buffer)) {
62f0b3eb
SR
3678 local_dec(&cpu_buffer->committing);
3679 local_dec(&cpu_buffer->commits);
3680 return NULL;
3681 }
85bac32c 3682#endif
b7dc42fd 3683
fcc742ea 3684 info.length = rb_calculate_event_length(length);
58fbc3c6
SRV
3685
3686 if (ring_buffer_time_stamp_abs(cpu_buffer->buffer)) {
3687 add_ts_default = RB_ADD_STAMP_ABSOLUTE;
3688 info.length += RB_LEN_TIME_EXTEND;
139f8400 3689 if (info.length > cpu_buffer->buffer->max_data_size)
b3ae7b67 3690 goto out_fail;
58fbc3c6
SRV
3691 } else {
3692 add_ts_default = RB_ADD_STAMP_NONE;
3693 }
3694
a4543a2f 3695 again:
58fbc3c6 3696 info.add_timestamp = add_ts_default;
b7dc42fd
SRRH
3697 info.delta = 0;
3698
818e3dd3
SR
3699 /*
3700 * We allow for interrupts to reenter here and do a trace.
3701 * If one does, it will cause this original code to loop
3702 * back here. Even with heavy interrupts happening, this
3703 * should only happen a few times in a row. If this happens
3704 * 1000 times in a row, there must be either an interrupt
3705 * storm or we have something buggy.
3706 * Bail!
3707 */
3e89c7bb 3708 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 1000))
fa743953 3709 goto out_fail;
818e3dd3 3710
fcc742ea
SRRH
3711 event = __rb_reserve_next(cpu_buffer, &info);
3712
bd1b7cd3 3713 if (unlikely(PTR_ERR(event) == -EAGAIN)) {
58fbc3c6 3714 if (info.add_timestamp & (RB_ADD_STAMP_FORCE | RB_ADD_STAMP_EXTEND))
bd1b7cd3 3715 info.length -= RB_LEN_TIME_EXTEND;
bf41a158 3716 goto again;
bd1b7cd3 3717 }
bf41a158 3718
a389d86f
SRV
3719 if (likely(event))
3720 return event;
fa743953
SR
3721 out_fail:
3722 rb_end_commit(cpu_buffer);
3723 return NULL;
7a8e76a3
SR
3724}
3725
3726/**
3727 * ring_buffer_lock_reserve - reserve a part of the buffer
3728 * @buffer: the ring buffer to reserve from
3729 * @length: the length of the data to reserve (excluding event header)
7a8e76a3 3730 *
6167c205 3731 * Returns a reserved event on the ring buffer to copy directly to.
7a8e76a3
SR
3732 * The user of this interface will need to get the body to write into
3733 * and can use the ring_buffer_event_data() interface.
3734 *
3735 * The length is the length of the data needed, not the event length
3736 * which also includes the event header.
3737 *
3738 * Must be paired with ring_buffer_unlock_commit, unless NULL is returned.
3739 * If NULL is returned, then nothing has been allocated or locked.
3740 */
3741struct ring_buffer_event *
13292494 3742ring_buffer_lock_reserve(struct trace_buffer *buffer, unsigned long length)
7a8e76a3
SR
3743{
3744 struct ring_buffer_per_cpu *cpu_buffer;
3745 struct ring_buffer_event *event;
5168ae50 3746 int cpu;
7a8e76a3 3747
bf41a158 3748 /* If we are tracing schedule, we don't want to recurse */
5168ae50 3749 preempt_disable_notrace();
bf41a158 3750
3205f806 3751 if (unlikely(atomic_read(&buffer->record_disabled)))
58a09ec6 3752 goto out;
261842b7 3753
7a8e76a3
SR
3754 cpu = raw_smp_processor_id();
3755
3205f806 3756 if (unlikely(!cpumask_test_cpu(cpu, buffer->cpumask)))
d769041f 3757 goto out;
7a8e76a3
SR
3758
3759 cpu_buffer = buffer->buffers[cpu];
7a8e76a3 3760
3205f806 3761 if (unlikely(atomic_read(&cpu_buffer->record_disabled)))
d769041f 3762 goto out;
7a8e76a3 3763
139f8400 3764 if (unlikely(length > buffer->max_data_size))
bf41a158 3765 goto out;
7a8e76a3 3766
58a09ec6
SRRH
3767 if (unlikely(trace_recursive_lock(cpu_buffer)))
3768 goto out;
3769
62f0b3eb 3770 event = rb_reserve_next_event(buffer, cpu_buffer, length);
7a8e76a3 3771 if (!event)
58a09ec6 3772 goto out_unlock;
7a8e76a3
SR
3773
3774 return event;
3775
58a09ec6
SRRH
3776 out_unlock:
3777 trace_recursive_unlock(cpu_buffer);
d769041f 3778 out:
5168ae50 3779 preempt_enable_notrace();
7a8e76a3
SR
3780 return NULL;
3781}
c4f50183 3782EXPORT_SYMBOL_GPL(ring_buffer_lock_reserve);
7a8e76a3 3783
a1863c21
SR
3784/*
3785 * Decrement the entries to the page that an event is on.
3786 * The event does not even need to exist, only the pointer
3787 * to the page it is on. This may only be called before the commit
3788 * takes place.
3789 */
3790static inline void
3791rb_decrement_entry(struct ring_buffer_per_cpu *cpu_buffer,
3792 struct ring_buffer_event *event)
3793{
3794 unsigned long addr = (unsigned long)event;
3795 struct buffer_page *bpage = cpu_buffer->commit_page;
3796 struct buffer_page *start;
3797
3cb30911 3798 addr &= ~((PAGE_SIZE << cpu_buffer->buffer->subbuf_order) - 1);
a1863c21
SR
3799
3800 /* Do the likely case first */
3801 if (likely(bpage->page == (void *)addr)) {
3802 local_dec(&bpage->entries);
3803 return;
3804 }
3805
3806 /*
3807 * Because the commit page may be on the reader page we
3808 * start with the next page and check the end loop there.
3809 */
6689bed3 3810 rb_inc_page(&bpage);
a1863c21
SR
3811 start = bpage;
3812 do {
3813 if (bpage->page == (void *)addr) {
3814 local_dec(&bpage->entries);
3815 return;
3816 }
6689bed3 3817 rb_inc_page(&bpage);
a1863c21
SR
3818 } while (bpage != start);
3819
3820 /* commit not part of this buffer?? */
3821 RB_WARN_ON(cpu_buffer, 1);
3822}
3823
fa1b47dd 3824/**
88883490 3825 * ring_buffer_discard_commit - discard an event that has not been committed
fa1b47dd
SR
3826 * @buffer: the ring buffer
3827 * @event: non committed event to discard
3828 *
dc892f73
SR
3829 * Sometimes an event that is in the ring buffer needs to be ignored.
3830 * This function lets the user discard an event in the ring buffer
3831 * and then that event will not be read later.
3832 *
6167c205 3833 * This function only works if it is called before the item has been
dc892f73 3834 * committed. It will try to free the event from the ring buffer
fa1b47dd
SR
3835 * if another event has not been added behind it.
3836 *
3837 * If another event has been added behind it, it will set the event
3838 * up as discarded, and perform the commit.
3839 *
3840 * If this function is called, do not call ring_buffer_unlock_commit on
3841 * the event.
3842 */
13292494 3843void ring_buffer_discard_commit(struct trace_buffer *buffer,
fa1b47dd
SR
3844 struct ring_buffer_event *event)
3845{
3846 struct ring_buffer_per_cpu *cpu_buffer;
fa1b47dd
SR
3847 int cpu;
3848
3849 /* The event is discarded regardless */
f3b9aae1 3850 rb_event_discard(event);
fa1b47dd 3851
fa743953
SR
3852 cpu = smp_processor_id();
3853 cpu_buffer = buffer->buffers[cpu];
3854
fa1b47dd
SR
3855 /*
3856 * This must only be called if the event has not been
3857 * committed yet. Thus we can assume that preemption
3858 * is still disabled.
3859 */
fa743953 3860 RB_WARN_ON(buffer, !local_read(&cpu_buffer->committing));
fa1b47dd 3861
a1863c21 3862 rb_decrement_entry(cpu_buffer, event);
0f2541d2 3863 if (rb_try_to_discard(cpu_buffer, event))
edd813bf 3864 goto out;
fa1b47dd 3865
fa1b47dd 3866 out:
fa743953 3867 rb_end_commit(cpu_buffer);
fa1b47dd 3868
58a09ec6 3869 trace_recursive_unlock(cpu_buffer);
f3b9aae1 3870
5168ae50 3871 preempt_enable_notrace();
fa1b47dd
SR
3872
3873}
3874EXPORT_SYMBOL_GPL(ring_buffer_discard_commit);
3875
7a8e76a3
SR
3876/**
3877 * ring_buffer_write - write data to the buffer without reserving
3878 * @buffer: The ring buffer to write to.
3879 * @length: The length of the data being written (excluding the event header)
3880 * @data: The data to write to the buffer.
3881 *
3882 * This is like ring_buffer_lock_reserve and ring_buffer_unlock_commit as
3883 * one function. If you already have the data to write to the buffer, it
3884 * may be easier to simply call this function.
3885 *
3886 * Note, like ring_buffer_lock_reserve, the length is the length of the data
3887 * and not the length of the event which would hold the header.
3888 */
13292494 3889int ring_buffer_write(struct trace_buffer *buffer,
01e3e710
DS
3890 unsigned long length,
3891 void *data)
7a8e76a3
SR
3892{
3893 struct ring_buffer_per_cpu *cpu_buffer;
3894 struct ring_buffer_event *event;
7a8e76a3
SR
3895 void *body;
3896 int ret = -EBUSY;
5168ae50 3897 int cpu;
7a8e76a3 3898
5168ae50 3899 preempt_disable_notrace();
bf41a158 3900
52fbe9cd
LJ
3901 if (atomic_read(&buffer->record_disabled))
3902 goto out;
3903
7a8e76a3
SR
3904 cpu = raw_smp_processor_id();
3905
9e01c1b7 3906 if (!cpumask_test_cpu(cpu, buffer->cpumask))
d769041f 3907 goto out;
7a8e76a3
SR
3908
3909 cpu_buffer = buffer->buffers[cpu];
7a8e76a3
SR
3910
3911 if (atomic_read(&cpu_buffer->record_disabled))
3912 goto out;
3913
139f8400 3914 if (length > buffer->max_data_size)
be957c44
SR
3915 goto out;
3916
985e871b
SRRH
3917 if (unlikely(trace_recursive_lock(cpu_buffer)))
3918 goto out;
3919
62f0b3eb 3920 event = rb_reserve_next_event(buffer, cpu_buffer, length);
7a8e76a3 3921 if (!event)
985e871b 3922 goto out_unlock;
7a8e76a3
SR
3923
3924 body = rb_event_data(event);
3925
3926 memcpy(body, data, length);
3927
04aabc32 3928 rb_commit(cpu_buffer);
7a8e76a3 3929
15693458
SRRH
3930 rb_wakeups(buffer, cpu_buffer);
3931
7a8e76a3 3932 ret = 0;
985e871b
SRRH
3933
3934 out_unlock:
3935 trace_recursive_unlock(cpu_buffer);
3936
7a8e76a3 3937 out:
5168ae50 3938 preempt_enable_notrace();
7a8e76a3
SR
3939
3940 return ret;
3941}
c4f50183 3942EXPORT_SYMBOL_GPL(ring_buffer_write);
7a8e76a3 3943
da58834c 3944static bool rb_per_cpu_empty(struct ring_buffer_per_cpu *cpu_buffer)
bf41a158
SR
3945{
3946 struct buffer_page *reader = cpu_buffer->reader_page;
77ae365e 3947 struct buffer_page *head = rb_set_head_page(cpu_buffer);
bf41a158
SR
3948 struct buffer_page *commit = cpu_buffer->commit_page;
3949
77ae365e
SR
3950 /* In case of error, head will be NULL */
3951 if (unlikely(!head))
da58834c 3952 return true;
77ae365e 3953
67f0d6d9 3954 /* Reader should exhaust content in reader page */
fe832be0 3955 if (reader->read != rb_page_size(reader))
67f0d6d9
HL
3956 return false;
3957
3958 /*
3959 * If writers are committing on the reader page, knowing all
3960 * committed content has been read, the ring buffer is empty.
3961 */
3962 if (commit == reader)
3963 return true;
3964
3965 /*
3966 * If writers are committing on a page other than reader page
3967 * and head page, there should always be content to read.
3968 */
3969 if (commit != head)
3970 return false;
3971
3972 /*
3973 * Writers are committing on the head page, we just need
3974 * to care about there're committed data, and the reader will
3975 * swap reader page with head page when it is to read data.
3976 */
3977 return rb_page_commit(commit) == 0;
bf41a158
SR
3978}
3979
7a8e76a3
SR
3980/**
3981 * ring_buffer_record_disable - stop all writes into the buffer
3982 * @buffer: The ring buffer to stop writes to.
3983 *
3984 * This prevents all writes to the buffer. Any attempt to write
3985 * to the buffer after this will fail and return NULL.
3986 *
74401729 3987 * The caller should call synchronize_rcu() after this.
7a8e76a3 3988 */
13292494 3989void ring_buffer_record_disable(struct trace_buffer *buffer)
7a8e76a3
SR
3990{
3991 atomic_inc(&buffer->record_disabled);
3992}
c4f50183 3993EXPORT_SYMBOL_GPL(ring_buffer_record_disable);
7a8e76a3
SR
3994
3995/**
3996 * ring_buffer_record_enable - enable writes to the buffer
3997 * @buffer: The ring buffer to enable writes
3998 *
3999 * Note, multiple disables will need the same number of enables
c41b20e7 4000 * to truly enable the writing (much like preempt_disable).
7a8e76a3 4001 */
13292494 4002void ring_buffer_record_enable(struct trace_buffer *buffer)
7a8e76a3
SR
4003{
4004 atomic_dec(&buffer->record_disabled);
4005}
c4f50183 4006EXPORT_SYMBOL_GPL(ring_buffer_record_enable);
7a8e76a3 4007
499e5470
SR
4008/**
4009 * ring_buffer_record_off - stop all writes into the buffer
4010 * @buffer: The ring buffer to stop writes to.
4011 *
4012 * This prevents all writes to the buffer. Any attempt to write
4013 * to the buffer after this will fail and return NULL.
4014 *
4015 * This is different than ring_buffer_record_disable() as
87abb3b1 4016 * it works like an on/off switch, where as the disable() version
499e5470
SR
4017 * must be paired with a enable().
4018 */
13292494 4019void ring_buffer_record_off(struct trace_buffer *buffer)
499e5470
SR
4020{
4021 unsigned int rd;
4022 unsigned int new_rd;
4023
8328e36d 4024 rd = atomic_read(&buffer->record_disabled);
499e5470 4025 do {
499e5470 4026 new_rd = rd | RB_BUFFER_OFF;
8328e36d 4027 } while (!atomic_try_cmpxchg(&buffer->record_disabled, &rd, new_rd));
499e5470
SR
4028}
4029EXPORT_SYMBOL_GPL(ring_buffer_record_off);
4030
4031/**
4032 * ring_buffer_record_on - restart writes into the buffer
4033 * @buffer: The ring buffer to start writes to.
4034 *
4035 * This enables all writes to the buffer that was disabled by
4036 * ring_buffer_record_off().
4037 *
4038 * This is different than ring_buffer_record_enable() as
87abb3b1 4039 * it works like an on/off switch, where as the enable() version
499e5470
SR
4040 * must be paired with a disable().
4041 */
13292494 4042void ring_buffer_record_on(struct trace_buffer *buffer)
499e5470
SR
4043{
4044 unsigned int rd;
4045 unsigned int new_rd;
4046
8328e36d 4047 rd = atomic_read(&buffer->record_disabled);
499e5470 4048 do {
499e5470 4049 new_rd = rd & ~RB_BUFFER_OFF;
8328e36d 4050 } while (!atomic_try_cmpxchg(&buffer->record_disabled, &rd, new_rd));
499e5470
SR
4051}
4052EXPORT_SYMBOL_GPL(ring_buffer_record_on);
4053
4054/**
4055 * ring_buffer_record_is_on - return true if the ring buffer can write
4056 * @buffer: The ring buffer to see if write is enabled
4057 *
4058 * Returns true if the ring buffer is in a state that it accepts writes.
4059 */
13292494 4060bool ring_buffer_record_is_on(struct trace_buffer *buffer)
499e5470
SR
4061{
4062 return !atomic_read(&buffer->record_disabled);
4063}
4064
73c8d894
MH
4065/**
4066 * ring_buffer_record_is_set_on - return true if the ring buffer is set writable
4067 * @buffer: The ring buffer to see if write is set enabled
4068 *
4069 * Returns true if the ring buffer is set writable by ring_buffer_record_on().
4070 * Note that this does NOT mean it is in a writable state.
4071 *
4072 * It may return true when the ring buffer has been disabled by
4073 * ring_buffer_record_disable(), as that is a temporary disabling of
4074 * the ring buffer.
4075 */
13292494 4076bool ring_buffer_record_is_set_on(struct trace_buffer *buffer)
73c8d894
MH
4077{
4078 return !(atomic_read(&buffer->record_disabled) & RB_BUFFER_OFF);
4079}
4080
7a8e76a3
SR
4081/**
4082 * ring_buffer_record_disable_cpu - stop all writes into the cpu_buffer
4083 * @buffer: The ring buffer to stop writes to.
4084 * @cpu: The CPU buffer to stop
4085 *
4086 * This prevents all writes to the buffer. Any attempt to write
4087 * to the buffer after this will fail and return NULL.
4088 *
74401729 4089 * The caller should call synchronize_rcu() after this.
7a8e76a3 4090 */
13292494 4091void ring_buffer_record_disable_cpu(struct trace_buffer *buffer, int cpu)
7a8e76a3
SR
4092{
4093 struct ring_buffer_per_cpu *cpu_buffer;
4094
9e01c1b7 4095 if (!cpumask_test_cpu(cpu, buffer->cpumask))
8aabee57 4096 return;
7a8e76a3
SR
4097
4098 cpu_buffer = buffer->buffers[cpu];
4099 atomic_inc(&cpu_buffer->record_disabled);
4100}
c4f50183 4101EXPORT_SYMBOL_GPL(ring_buffer_record_disable_cpu);
7a8e76a3
SR
4102
4103/**
4104 * ring_buffer_record_enable_cpu - enable writes to the buffer
4105 * @buffer: The ring buffer to enable writes
4106 * @cpu: The CPU to enable.
4107 *
4108 * Note, multiple disables will need the same number of enables
c41b20e7 4109 * to truly enable the writing (much like preempt_disable).
7a8e76a3 4110 */
13292494 4111void ring_buffer_record_enable_cpu(struct trace_buffer *buffer, int cpu)
7a8e76a3
SR
4112{
4113 struct ring_buffer_per_cpu *cpu_buffer;
4114
9e01c1b7 4115 if (!cpumask_test_cpu(cpu, buffer->cpumask))
8aabee57 4116 return;
7a8e76a3
SR
4117
4118 cpu_buffer = buffer->buffers[cpu];
4119 atomic_dec(&cpu_buffer->record_disabled);
4120}
c4f50183 4121EXPORT_SYMBOL_GPL(ring_buffer_record_enable_cpu);
7a8e76a3 4122
f6195aa0
SR
4123/*
4124 * The total entries in the ring buffer is the running counter
4125 * of entries entered into the ring buffer, minus the sum of
4126 * the entries read from the ring buffer and the number of
4127 * entries that were overwritten.
4128 */
4129static inline unsigned long
4130rb_num_of_entries(struct ring_buffer_per_cpu *cpu_buffer)
4131{
4132 return local_read(&cpu_buffer->entries) -
4133 (local_read(&cpu_buffer->overrun) + cpu_buffer->read);
4134}
4135
c64e148a
VN
4136/**
4137 * ring_buffer_oldest_event_ts - get the oldest event timestamp from the buffer
4138 * @buffer: The ring buffer
4139 * @cpu: The per CPU buffer to read from.
4140 */
13292494 4141u64 ring_buffer_oldest_event_ts(struct trace_buffer *buffer, int cpu)
c64e148a
VN
4142{
4143 unsigned long flags;
4144 struct ring_buffer_per_cpu *cpu_buffer;
4145 struct buffer_page *bpage;
da830e58 4146 u64 ret = 0;
c64e148a
VN
4147
4148 if (!cpumask_test_cpu(cpu, buffer->cpumask))
4149 return 0;
4150
4151 cpu_buffer = buffer->buffers[cpu];
7115e3fc 4152 raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
c64e148a
VN
4153 /*
4154 * if the tail is on reader_page, oldest time stamp is on the reader
4155 * page
4156 */
4157 if (cpu_buffer->tail_page == cpu_buffer->reader_page)
4158 bpage = cpu_buffer->reader_page;
4159 else
4160 bpage = rb_set_head_page(cpu_buffer);
54f7be5b
SR
4161 if (bpage)
4162 ret = bpage->page->time_stamp;
7115e3fc 4163 raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
c64e148a
VN
4164
4165 return ret;
4166}
4167EXPORT_SYMBOL_GPL(ring_buffer_oldest_event_ts);
4168
4169/**
45d99ea4 4170 * ring_buffer_bytes_cpu - get the number of bytes unconsumed in a cpu buffer
c64e148a
VN
4171 * @buffer: The ring buffer
4172 * @cpu: The per CPU buffer to read from.
4173 */
13292494 4174unsigned long ring_buffer_bytes_cpu(struct trace_buffer *buffer, int cpu)
c64e148a
VN
4175{
4176 struct ring_buffer_per_cpu *cpu_buffer;
4177 unsigned long ret;
4178
4179 if (!cpumask_test_cpu(cpu, buffer->cpumask))
4180 return 0;
4181
4182 cpu_buffer = buffer->buffers[cpu];
4183 ret = local_read(&cpu_buffer->entries_bytes) - cpu_buffer->read_bytes;
4184
4185 return ret;
4186}
4187EXPORT_SYMBOL_GPL(ring_buffer_bytes_cpu);
4188
7a8e76a3
SR
4189/**
4190 * ring_buffer_entries_cpu - get the number of entries in a cpu buffer
4191 * @buffer: The ring buffer
4192 * @cpu: The per CPU buffer to get the entries from.
4193 */
13292494 4194unsigned long ring_buffer_entries_cpu(struct trace_buffer *buffer, int cpu)
7a8e76a3
SR
4195{
4196 struct ring_buffer_per_cpu *cpu_buffer;
4197
9e01c1b7 4198 if (!cpumask_test_cpu(cpu, buffer->cpumask))
8aabee57 4199 return 0;
7a8e76a3
SR
4200
4201 cpu_buffer = buffer->buffers[cpu];
554f786e 4202
f6195aa0 4203 return rb_num_of_entries(cpu_buffer);
7a8e76a3 4204}
c4f50183 4205EXPORT_SYMBOL_GPL(ring_buffer_entries_cpu);
7a8e76a3
SR
4206
4207/**
884bfe89
SP
4208 * ring_buffer_overrun_cpu - get the number of overruns caused by the ring
4209 * buffer wrapping around (only if RB_FL_OVERWRITE is on).
7a8e76a3
SR
4210 * @buffer: The ring buffer
4211 * @cpu: The per CPU buffer to get the number of overruns from
4212 */
13292494 4213unsigned long ring_buffer_overrun_cpu(struct trace_buffer *buffer, int cpu)
7a8e76a3
SR
4214{
4215 struct ring_buffer_per_cpu *cpu_buffer;
8aabee57 4216 unsigned long ret;
7a8e76a3 4217
9e01c1b7 4218 if (!cpumask_test_cpu(cpu, buffer->cpumask))
8aabee57 4219 return 0;
7a8e76a3
SR
4220
4221 cpu_buffer = buffer->buffers[cpu];
77ae365e 4222 ret = local_read(&cpu_buffer->overrun);
554f786e
SR
4223
4224 return ret;
7a8e76a3 4225}
c4f50183 4226EXPORT_SYMBOL_GPL(ring_buffer_overrun_cpu);
7a8e76a3 4227
f0d2c681 4228/**
884bfe89
SP
4229 * ring_buffer_commit_overrun_cpu - get the number of overruns caused by
4230 * commits failing due to the buffer wrapping around while there are uncommitted
4231 * events, such as during an interrupt storm.
f0d2c681
SR
4232 * @buffer: The ring buffer
4233 * @cpu: The per CPU buffer to get the number of overruns from
4234 */
4235unsigned long
13292494 4236ring_buffer_commit_overrun_cpu(struct trace_buffer *buffer, int cpu)
f0d2c681
SR
4237{
4238 struct ring_buffer_per_cpu *cpu_buffer;
4239 unsigned long ret;
4240
4241 if (!cpumask_test_cpu(cpu, buffer->cpumask))
4242 return 0;
4243
4244 cpu_buffer = buffer->buffers[cpu];
77ae365e 4245 ret = local_read(&cpu_buffer->commit_overrun);
f0d2c681
SR
4246
4247 return ret;
4248}
4249EXPORT_SYMBOL_GPL(ring_buffer_commit_overrun_cpu);
4250
884bfe89
SP
4251/**
4252 * ring_buffer_dropped_events_cpu - get the number of dropped events caused by
4253 * the ring buffer filling up (only if RB_FL_OVERWRITE is off).
4254 * @buffer: The ring buffer
4255 * @cpu: The per CPU buffer to get the number of overruns from
4256 */
4257unsigned long
13292494 4258ring_buffer_dropped_events_cpu(struct trace_buffer *buffer, int cpu)
884bfe89
SP
4259{
4260 struct ring_buffer_per_cpu *cpu_buffer;
4261 unsigned long ret;
4262
4263 if (!cpumask_test_cpu(cpu, buffer->cpumask))
4264 return 0;
4265
4266 cpu_buffer = buffer->buffers[cpu];
4267 ret = local_read(&cpu_buffer->dropped_events);
4268
4269 return ret;
4270}
4271EXPORT_SYMBOL_GPL(ring_buffer_dropped_events_cpu);
4272
ad964704
SRRH
4273/**
4274 * ring_buffer_read_events_cpu - get the number of events successfully read
4275 * @buffer: The ring buffer
4276 * @cpu: The per CPU buffer to get the number of events read
4277 */
4278unsigned long
13292494 4279ring_buffer_read_events_cpu(struct trace_buffer *buffer, int cpu)
ad964704
SRRH
4280{
4281 struct ring_buffer_per_cpu *cpu_buffer;
4282
4283 if (!cpumask_test_cpu(cpu, buffer->cpumask))
4284 return 0;
4285
4286 cpu_buffer = buffer->buffers[cpu];
4287 return cpu_buffer->read;
4288}
4289EXPORT_SYMBOL_GPL(ring_buffer_read_events_cpu);
4290
7a8e76a3
SR
4291/**
4292 * ring_buffer_entries - get the number of entries in a buffer
4293 * @buffer: The ring buffer
4294 *
4295 * Returns the total number of entries in the ring buffer
4296 * (all CPU entries)
4297 */
13292494 4298unsigned long ring_buffer_entries(struct trace_buffer *buffer)
7a8e76a3
SR
4299{
4300 struct ring_buffer_per_cpu *cpu_buffer;
4301 unsigned long entries = 0;
4302 int cpu;
4303
4304 /* if you care about this being correct, lock the buffer */
4305 for_each_buffer_cpu(buffer, cpu) {
4306 cpu_buffer = buffer->buffers[cpu];
f6195aa0 4307 entries += rb_num_of_entries(cpu_buffer);
7a8e76a3
SR
4308 }
4309
4310 return entries;
4311}
c4f50183 4312EXPORT_SYMBOL_GPL(ring_buffer_entries);
7a8e76a3
SR
4313
4314/**
67b394f7 4315 * ring_buffer_overruns - get the number of overruns in buffer
7a8e76a3
SR
4316 * @buffer: The ring buffer
4317 *
4318 * Returns the total number of overruns in the ring buffer
4319 * (all CPU entries)
4320 */
13292494 4321unsigned long ring_buffer_overruns(struct trace_buffer *buffer)
7a8e76a3
SR
4322{
4323 struct ring_buffer_per_cpu *cpu_buffer;
4324 unsigned long overruns = 0;
4325 int cpu;
4326
4327 /* if you care about this being correct, lock the buffer */
4328 for_each_buffer_cpu(buffer, cpu) {
4329 cpu_buffer = buffer->buffers[cpu];
77ae365e 4330 overruns += local_read(&cpu_buffer->overrun);
7a8e76a3
SR
4331 }
4332
4333 return overruns;
4334}
c4f50183 4335EXPORT_SYMBOL_GPL(ring_buffer_overruns);
7a8e76a3 4336
642edba5 4337static void rb_iter_reset(struct ring_buffer_iter *iter)
7a8e76a3
SR
4338{
4339 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
4340
d769041f 4341 /* Iterator usage is expected to have record disabled */
651e22f2
SRRH
4342 iter->head_page = cpu_buffer->reader_page;
4343 iter->head = cpu_buffer->reader_page->read;
785888c5 4344 iter->next_event = iter->head;
651e22f2
SRRH
4345
4346 iter->cache_reader_page = iter->head_page;
24607f11 4347 iter->cache_read = cpu_buffer->read;
2d093282 4348 iter->cache_pages_removed = cpu_buffer->pages_removed;
651e22f2 4349
28e3fc56 4350 if (iter->head) {
d769041f 4351 iter->read_stamp = cpu_buffer->read_stamp;
28e3fc56
SRV
4352 iter->page_stamp = cpu_buffer->reader_page->page->time_stamp;
4353 } else {
abc9b56d 4354 iter->read_stamp = iter->head_page->page->time_stamp;
28e3fc56
SRV
4355 iter->page_stamp = iter->read_stamp;
4356 }
642edba5 4357}
f83c9d0f 4358
642edba5
SR
4359/**
4360 * ring_buffer_iter_reset - reset an iterator
4361 * @iter: The iterator to reset
4362 *
4363 * Resets the iterator, so that it will start from the beginning
4364 * again.
4365 */
4366void ring_buffer_iter_reset(struct ring_buffer_iter *iter)
4367{
554f786e 4368 struct ring_buffer_per_cpu *cpu_buffer;
642edba5
SR
4369 unsigned long flags;
4370
554f786e
SR
4371 if (!iter)
4372 return;
4373
4374 cpu_buffer = iter->cpu_buffer;
4375
5389f6fa 4376 raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
642edba5 4377 rb_iter_reset(iter);
5389f6fa 4378 raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
7a8e76a3 4379}
c4f50183 4380EXPORT_SYMBOL_GPL(ring_buffer_iter_reset);
7a8e76a3
SR
4381
4382/**
4383 * ring_buffer_iter_empty - check if an iterator has no more to read
4384 * @iter: The iterator to check
4385 */
4386int ring_buffer_iter_empty(struct ring_buffer_iter *iter)
4387{
4388 struct ring_buffer_per_cpu *cpu_buffer;
78f7a45d
SRV
4389 struct buffer_page *reader;
4390 struct buffer_page *head_page;
4391 struct buffer_page *commit_page;
ead6ecfd 4392 struct buffer_page *curr_commit_page;
78f7a45d 4393 unsigned commit;
ead6ecfd
SRV
4394 u64 curr_commit_ts;
4395 u64 commit_ts;
7a8e76a3
SR
4396
4397 cpu_buffer = iter->cpu_buffer;
78f7a45d
SRV
4398 reader = cpu_buffer->reader_page;
4399 head_page = cpu_buffer->head_page;
f1e30cb6 4400 commit_page = READ_ONCE(cpu_buffer->commit_page);
ead6ecfd
SRV
4401 commit_ts = commit_page->page->time_stamp;
4402
4403 /*
4404 * When the writer goes across pages, it issues a cmpxchg which
4405 * is a mb(), which will synchronize with the rmb here.
4406 * (see rb_tail_page_update())
4407 */
4408 smp_rmb();
78f7a45d 4409 commit = rb_page_commit(commit_page);
ead6ecfd
SRV
4410 /* We want to make sure that the commit page doesn't change */
4411 smp_rmb();
4412
4413 /* Make sure commit page didn't change */
4414 curr_commit_page = READ_ONCE(cpu_buffer->commit_page);
4415 curr_commit_ts = READ_ONCE(curr_commit_page->page->time_stamp);
4416
4417 /* If the commit page changed, then there's more data */
4418 if (curr_commit_page != commit_page ||
4419 curr_commit_ts != commit_ts)
4420 return 0;
78f7a45d 4421
ead6ecfd 4422 /* Still racy, as it may return a false positive, but that's OK */
785888c5 4423 return ((iter->head_page == commit_page && iter->head >= commit) ||
78f7a45d
SRV
4424 (iter->head_page == reader && commit_page == head_page &&
4425 head_page->read == commit &&
fe832be0 4426 iter->head == rb_page_size(cpu_buffer->reader_page)));
7a8e76a3 4427}
c4f50183 4428EXPORT_SYMBOL_GPL(ring_buffer_iter_empty);
7a8e76a3
SR
4429
4430static void
4431rb_update_read_stamp(struct ring_buffer_per_cpu *cpu_buffer,
4432 struct ring_buffer_event *event)
4433{
4434 u64 delta;
4435
334d4169 4436 switch (event->type_len) {
7a8e76a3
SR
4437 case RINGBUF_TYPE_PADDING:
4438 return;
4439
4440 case RINGBUF_TYPE_TIME_EXTEND:
e20044f7 4441 delta = rb_event_time_stamp(event);
7a8e76a3
SR
4442 cpu_buffer->read_stamp += delta;
4443 return;
4444
4445 case RINGBUF_TYPE_TIME_STAMP:
e20044f7 4446 delta = rb_event_time_stamp(event);
6695da58 4447 delta = rb_fix_abs_ts(delta, cpu_buffer->read_stamp);
dc4e2801 4448 cpu_buffer->read_stamp = delta;
7a8e76a3
SR
4449 return;
4450
4451 case RINGBUF_TYPE_DATA:
4452 cpu_buffer->read_stamp += event->time_delta;
4453 return;
4454
4455 default:
da4d401a 4456 RB_WARN_ON(cpu_buffer, 1);
7a8e76a3 4457 }
7a8e76a3
SR
4458}
4459
4460static void
4461rb_update_iter_read_stamp(struct ring_buffer_iter *iter,
4462 struct ring_buffer_event *event)
4463{
4464 u64 delta;
4465
334d4169 4466 switch (event->type_len) {
7a8e76a3
SR
4467 case RINGBUF_TYPE_PADDING:
4468 return;
4469
4470 case RINGBUF_TYPE_TIME_EXTEND:
e20044f7 4471 delta = rb_event_time_stamp(event);
7a8e76a3
SR
4472 iter->read_stamp += delta;
4473 return;
4474
4475 case RINGBUF_TYPE_TIME_STAMP:
e20044f7 4476 delta = rb_event_time_stamp(event);
6695da58 4477 delta = rb_fix_abs_ts(delta, iter->read_stamp);
dc4e2801 4478 iter->read_stamp = delta;
7a8e76a3
SR
4479 return;
4480
4481 case RINGBUF_TYPE_DATA:
4482 iter->read_stamp += event->time_delta;
4483 return;
4484
4485 default:
da4d401a 4486 RB_WARN_ON(iter->cpu_buffer, 1);
7a8e76a3 4487 }
7a8e76a3
SR
4488}
4489
d769041f
SR
4490static struct buffer_page *
4491rb_get_reader_page(struct ring_buffer_per_cpu *cpu_buffer)
7a8e76a3 4492{
d769041f 4493 struct buffer_page *reader = NULL;
139f8400 4494 unsigned long bsize = READ_ONCE(cpu_buffer->buffer->subbuf_size);
66a8cb95 4495 unsigned long overwrite;
d769041f 4496 unsigned long flags;
818e3dd3 4497 int nr_loops = 0;
bc92b956 4498 bool ret;
d769041f 4499
3e03fb7f 4500 local_irq_save(flags);
0199c4e6 4501 arch_spin_lock(&cpu_buffer->lock);
d769041f
SR
4502
4503 again:
818e3dd3
SR
4504 /*
4505 * This should normally only loop twice. But because the
4506 * start of the reader inserts an empty page, it causes
4507 * a case where we will loop three times. There should be no
4508 * reason to loop four times (that I know of).
4509 */
3e89c7bb 4510 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 3)) {
818e3dd3
SR
4511 reader = NULL;
4512 goto out;
4513 }
4514
d769041f
SR
4515 reader = cpu_buffer->reader_page;
4516
4517 /* If there's more to read, return this page */
bf41a158 4518 if (cpu_buffer->reader_page->read < rb_page_size(reader))
d769041f
SR
4519 goto out;
4520
4521 /* Never should we have an index greater than the size */
3e89c7bb
SR
4522 if (RB_WARN_ON(cpu_buffer,
4523 cpu_buffer->reader_page->read > rb_page_size(reader)))
4524 goto out;
d769041f
SR
4525
4526 /* check if we caught up to the tail */
4527 reader = NULL;
bf41a158 4528 if (cpu_buffer->commit_page == cpu_buffer->reader_page)
d769041f 4529 goto out;
7a8e76a3 4530
a5fb8331
SR
4531 /* Don't bother swapping if the ring buffer is empty */
4532 if (rb_num_of_entries(cpu_buffer) == 0)
4533 goto out;
4534
7a8e76a3 4535 /*
d769041f 4536 * Reset the reader page to size zero.
7a8e76a3 4537 */
77ae365e
SR
4538 local_set(&cpu_buffer->reader_page->write, 0);
4539 local_set(&cpu_buffer->reader_page->entries, 0);
4540 local_set(&cpu_buffer->reader_page->page->commit, 0);
ff0ff84a 4541 cpu_buffer->reader_page->real_end = 0;
7a8e76a3 4542
77ae365e
SR
4543 spin:
4544 /*
4545 * Splice the empty reader page into the list around the head.
4546 */
4547 reader = rb_set_head_page(cpu_buffer);
54f7be5b
SR
4548 if (!reader)
4549 goto out;
0e1ff5d7 4550 cpu_buffer->reader_page->list.next = rb_list_head(reader->list.next);
d769041f 4551 cpu_buffer->reader_page->list.prev = reader->list.prev;
bf41a158 4552
3adc54fa
SR
4553 /*
4554 * cpu_buffer->pages just needs to point to the buffer, it
4555 * has no specific buffer page to point to. Lets move it out
25985edc 4556 * of our way so we don't accidentally swap it.
3adc54fa
SR
4557 */
4558 cpu_buffer->pages = reader->list.prev;
4559
77ae365e 4560 /* The reader page will be pointing to the new head */
6689bed3 4561 rb_set_list_to_head(&cpu_buffer->reader_page->list);
7a8e76a3 4562
66a8cb95
SR
4563 /*
4564 * We want to make sure we read the overruns after we set up our
4565 * pointers to the next object. The writer side does a
4566 * cmpxchg to cross pages which acts as the mb on the writer
4567 * side. Note, the reader will constantly fail the swap
4568 * while the writer is updating the pointers, so this
4569 * guarantees that the overwrite recorded here is the one we
4570 * want to compare with the last_overrun.
4571 */
4572 smp_mb();
4573 overwrite = local_read(&(cpu_buffer->overrun));
4574
77ae365e
SR
4575 /*
4576 * Here's the tricky part.
4577 *
4578 * We need to move the pointer past the header page.
4579 * But we can only do that if a writer is not currently
4580 * moving it. The page before the header page has the
4581 * flag bit '1' set if it is pointing to the page we want.
4582 * but if the writer is in the process of moving it
4583 * than it will be '2' or already moved '0'.
4584 */
4585
4586 ret = rb_head_page_replace(reader, cpu_buffer->reader_page);
7a8e76a3
SR
4587
4588 /*
77ae365e 4589 * If we did not convert it, then we must try again.
7a8e76a3 4590 */
77ae365e
SR
4591 if (!ret)
4592 goto spin;
7a8e76a3 4593
77ae365e 4594 /*
2c2b0a78 4595 * Yay! We succeeded in replacing the page.
77ae365e
SR
4596 *
4597 * Now make the new head point back to the reader page.
4598 */
5ded3dc6 4599 rb_list_head(reader->list.next)->prev = &cpu_buffer->reader_page->list;
6689bed3 4600 rb_inc_page(&cpu_buffer->head_page);
d769041f 4601
2c2b0a78
SRV
4602 local_inc(&cpu_buffer->pages_read);
4603
d769041f
SR
4604 /* Finally update the reader page to the new head */
4605 cpu_buffer->reader_page = reader;
b81f472a 4606 cpu_buffer->reader_page->read = 0;
d769041f 4607
66a8cb95
SR
4608 if (overwrite != cpu_buffer->last_overrun) {
4609 cpu_buffer->lost_events = overwrite - cpu_buffer->last_overrun;
4610 cpu_buffer->last_overrun = overwrite;
4611 }
4612
d769041f
SR
4613 goto again;
4614
4615 out:
b81f472a
SRRH
4616 /* Update the read_stamp on the first event */
4617 if (reader && reader->read == 0)
4618 cpu_buffer->read_stamp = reader->page->time_stamp;
4619
0199c4e6 4620 arch_spin_unlock(&cpu_buffer->lock);
3e03fb7f 4621 local_irq_restore(flags);
d769041f 4622
a0fcaaed
SRG
4623 /*
4624 * The writer has preempt disable, wait for it. But not forever
4625 * Although, 1 second is pretty much "forever"
4626 */
4627#define USECS_WAIT 1000000
4628 for (nr_loops = 0; nr_loops < USECS_WAIT; nr_loops++) {
4629 /* If the write is past the end of page, a writer is still updating it */
139f8400 4630 if (likely(!reader || rb_page_write(reader) <= bsize))
a0fcaaed
SRG
4631 break;
4632
4633 udelay(1);
4634
4635 /* Get the latest version of the reader write value */
4636 smp_rmb();
4637 }
4638
4639 /* The writer is not moving forward? Something is wrong */
4640 if (RB_WARN_ON(cpu_buffer, nr_loops == USECS_WAIT))
4641 reader = NULL;
4642
4643 /*
4644 * Make sure we see any padding after the write update
6455b616
ZY
4645 * (see rb_reset_tail()).
4646 *
4647 * In addition, a writer may be writing on the reader page
4648 * if the page has not been fully filled, so the read barrier
4649 * is also needed to make sure we see the content of what is
4650 * committed by the writer (see rb_set_commit_to_write()).
a0fcaaed
SRG
4651 */
4652 smp_rmb();
4653
4654
d769041f
SR
4655 return reader;
4656}
4657
4658static void rb_advance_reader(struct ring_buffer_per_cpu *cpu_buffer)
4659{
4660 struct ring_buffer_event *event;
4661 struct buffer_page *reader;
4662 unsigned length;
4663
4664 reader = rb_get_reader_page(cpu_buffer);
7a8e76a3 4665
d769041f 4666 /* This function should not be called when buffer is empty */
3e89c7bb
SR
4667 if (RB_WARN_ON(cpu_buffer, !reader))
4668 return;
7a8e76a3 4669
d769041f
SR
4670 event = rb_reader_event(cpu_buffer);
4671
a1863c21 4672 if (event->type_len <= RINGBUF_TYPE_DATA_TYPE_LEN_MAX)
e4906eff 4673 cpu_buffer->read++;
d769041f
SR
4674
4675 rb_update_read_stamp(cpu_buffer, event);
4676
4677 length = rb_event_length(event);
6f807acd 4678 cpu_buffer->reader_page->read += length;
45d99ea4 4679 cpu_buffer->read_bytes += length;
7a8e76a3
SR
4680}
4681
4682static void rb_advance_iter(struct ring_buffer_iter *iter)
4683{
7a8e76a3 4684 struct ring_buffer_per_cpu *cpu_buffer;
7a8e76a3
SR
4685
4686 cpu_buffer = iter->cpu_buffer;
7a8e76a3 4687
785888c5
SRV
4688 /* If head == next_event then we need to jump to the next event */
4689 if (iter->head == iter->next_event) {
4690 /* If the event gets overwritten again, there's nothing to do */
4691 if (rb_iter_head_event(iter) == NULL)
4692 return;
4693 }
4694
4695 iter->head = iter->next_event;
4696
7a8e76a3
SR
4697 /*
4698 * Check if we are at the end of the buffer.
4699 */
785888c5 4700 if (iter->next_event >= rb_page_size(iter->head_page)) {
ea05b57c
SR
4701 /* discarded commits can make the page empty */
4702 if (iter->head_page == cpu_buffer->commit_page)
3e89c7bb 4703 return;
d769041f 4704 rb_inc_iter(iter);
7a8e76a3
SR
4705 return;
4706 }
4707
785888c5 4708 rb_update_iter_read_stamp(iter, iter->event);
7a8e76a3
SR
4709}
4710
66a8cb95
SR
4711static int rb_lost_events(struct ring_buffer_per_cpu *cpu_buffer)
4712{
4713 return cpu_buffer->lost_events;
4714}
4715
f83c9d0f 4716static struct ring_buffer_event *
66a8cb95
SR
4717rb_buffer_peek(struct ring_buffer_per_cpu *cpu_buffer, u64 *ts,
4718 unsigned long *lost_events)
7a8e76a3 4719{
7a8e76a3 4720 struct ring_buffer_event *event;
d769041f 4721 struct buffer_page *reader;
818e3dd3 4722 int nr_loops = 0;
7a8e76a3 4723
dc4e2801
TZ
4724 if (ts)
4725 *ts = 0;
7a8e76a3 4726 again:
818e3dd3 4727 /*
69d1b839
SR
4728 * We repeat when a time extend is encountered.
4729 * Since the time extend is always attached to a data event,
4730 * we should never loop more than once.
4731 * (We never hit the following condition more than twice).
818e3dd3 4732 */
69d1b839 4733 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 2))
818e3dd3 4734 return NULL;
818e3dd3 4735
d769041f
SR
4736 reader = rb_get_reader_page(cpu_buffer);
4737 if (!reader)
7a8e76a3
SR
4738 return NULL;
4739
d769041f 4740 event = rb_reader_event(cpu_buffer);
7a8e76a3 4741
334d4169 4742 switch (event->type_len) {
7a8e76a3 4743 case RINGBUF_TYPE_PADDING:
2d622719
TZ
4744 if (rb_null_event(event))
4745 RB_WARN_ON(cpu_buffer, 1);
4746 /*
4747 * Because the writer could be discarding every
4748 * event it creates (which would probably be bad)
4749 * if we were to go back to "again" then we may never
4750 * catch up, and will trigger the warn on, or lock
4751 * the box. Return the padding, and we will release
4752 * the current locks, and try again.
4753 */
2d622719 4754 return event;
7a8e76a3
SR
4755
4756 case RINGBUF_TYPE_TIME_EXTEND:
4757 /* Internal data, OK to advance */
d769041f 4758 rb_advance_reader(cpu_buffer);
7a8e76a3
SR
4759 goto again;
4760
4761 case RINGBUF_TYPE_TIME_STAMP:
dc4e2801 4762 if (ts) {
e20044f7 4763 *ts = rb_event_time_stamp(event);
6695da58 4764 *ts = rb_fix_abs_ts(*ts, reader->page->time_stamp);
dc4e2801
TZ
4765 ring_buffer_normalize_time_stamp(cpu_buffer->buffer,
4766 cpu_buffer->cpu, ts);
4767 }
4768 /* Internal data, OK to advance */
d769041f 4769 rb_advance_reader(cpu_buffer);
7a8e76a3
SR
4770 goto again;
4771
4772 case RINGBUF_TYPE_DATA:
dc4e2801 4773 if (ts && !(*ts)) {
7a8e76a3 4774 *ts = cpu_buffer->read_stamp + event->time_delta;
d8eeb2d3 4775 ring_buffer_normalize_time_stamp(cpu_buffer->buffer,
37886f6a 4776 cpu_buffer->cpu, ts);
7a8e76a3 4777 }
66a8cb95
SR
4778 if (lost_events)
4779 *lost_events = rb_lost_events(cpu_buffer);
7a8e76a3
SR
4780 return event;
4781
4782 default:
da4d401a 4783 RB_WARN_ON(cpu_buffer, 1);
7a8e76a3
SR
4784 }
4785
4786 return NULL;
4787}
c4f50183 4788EXPORT_SYMBOL_GPL(ring_buffer_peek);
7a8e76a3 4789
f83c9d0f
SR
4790static struct ring_buffer_event *
4791rb_iter_peek(struct ring_buffer_iter *iter, u64 *ts)
7a8e76a3 4792{
13292494 4793 struct trace_buffer *buffer;
7a8e76a3
SR
4794 struct ring_buffer_per_cpu *cpu_buffer;
4795 struct ring_buffer_event *event;
818e3dd3 4796 int nr_loops = 0;
7a8e76a3 4797
dc4e2801
TZ
4798 if (ts)
4799 *ts = 0;
4800
7a8e76a3
SR
4801 cpu_buffer = iter->cpu_buffer;
4802 buffer = cpu_buffer->buffer;
4803
492a74f4 4804 /*
2d093282
ZY
4805 * Check if someone performed a consuming read to the buffer
4806 * or removed some pages from the buffer. In these cases,
4807 * iterator was invalidated and we need to reset it.
492a74f4
SR
4808 */
4809 if (unlikely(iter->cache_read != cpu_buffer->read ||
2d093282
ZY
4810 iter->cache_reader_page != cpu_buffer->reader_page ||
4811 iter->cache_pages_removed != cpu_buffer->pages_removed))
492a74f4
SR
4812 rb_iter_reset(iter);
4813
7a8e76a3 4814 again:
3c05d748
SR
4815 if (ring_buffer_iter_empty(iter))
4816 return NULL;
4817
818e3dd3 4818 /*
3d2353de
SRV
4819 * As the writer can mess with what the iterator is trying
4820 * to read, just give up if we fail to get an event after
4821 * three tries. The iterator is not as reliable when reading
4822 * the ring buffer with an active write as the consumer is.
4823 * Do not warn if the three failures is reached.
818e3dd3 4824 */
3d2353de 4825 if (++nr_loops > 3)
818e3dd3 4826 return NULL;
818e3dd3 4827
7a8e76a3
SR
4828 if (rb_per_cpu_empty(cpu_buffer))
4829 return NULL;
4830
10e83fd0 4831 if (iter->head >= rb_page_size(iter->head_page)) {
3c05d748
SR
4832 rb_inc_iter(iter);
4833 goto again;
4834 }
4835
7a8e76a3 4836 event = rb_iter_head_event(iter);
3d2353de 4837 if (!event)
785888c5 4838 goto again;
7a8e76a3 4839
334d4169 4840 switch (event->type_len) {
7a8e76a3 4841 case RINGBUF_TYPE_PADDING:
2d622719
TZ
4842 if (rb_null_event(event)) {
4843 rb_inc_iter(iter);
4844 goto again;
4845 }
4846 rb_advance_iter(iter);
4847 return event;
7a8e76a3
SR
4848
4849 case RINGBUF_TYPE_TIME_EXTEND:
4850 /* Internal data, OK to advance */
4851 rb_advance_iter(iter);
4852 goto again;
4853
4854 case RINGBUF_TYPE_TIME_STAMP:
dc4e2801 4855 if (ts) {
e20044f7 4856 *ts = rb_event_time_stamp(event);
6695da58 4857 *ts = rb_fix_abs_ts(*ts, iter->head_page->page->time_stamp);
dc4e2801
TZ
4858 ring_buffer_normalize_time_stamp(cpu_buffer->buffer,
4859 cpu_buffer->cpu, ts);
4860 }
4861 /* Internal data, OK to advance */
7a8e76a3
SR
4862 rb_advance_iter(iter);
4863 goto again;
4864
4865 case RINGBUF_TYPE_DATA:
dc4e2801 4866 if (ts && !(*ts)) {
7a8e76a3 4867 *ts = iter->read_stamp + event->time_delta;
37886f6a
SR
4868 ring_buffer_normalize_time_stamp(buffer,
4869 cpu_buffer->cpu, ts);
7a8e76a3
SR
4870 }
4871 return event;
4872
4873 default:
da4d401a 4874 RB_WARN_ON(cpu_buffer, 1);
7a8e76a3
SR
4875 }
4876
4877 return NULL;
4878}
c4f50183 4879EXPORT_SYMBOL_GPL(ring_buffer_iter_peek);
7a8e76a3 4880
289a5a25 4881static inline bool rb_reader_lock(struct ring_buffer_per_cpu *cpu_buffer)
8d707e8e 4882{
289a5a25
SRRH
4883 if (likely(!in_nmi())) {
4884 raw_spin_lock(&cpu_buffer->reader_lock);
4885 return true;
4886 }
4887
8d707e8e
SR
4888 /*
4889 * If an NMI die dumps out the content of the ring buffer
289a5a25
SRRH
4890 * trylock must be used to prevent a deadlock if the NMI
4891 * preempted a task that holds the ring buffer locks. If
4892 * we get the lock then all is fine, if not, then continue
4893 * to do the read, but this can corrupt the ring buffer,
4894 * so it must be permanently disabled from future writes.
4895 * Reading from NMI is a oneshot deal.
8d707e8e 4896 */
289a5a25
SRRH
4897 if (raw_spin_trylock(&cpu_buffer->reader_lock))
4898 return true;
8d707e8e 4899
289a5a25
SRRH
4900 /* Continue without locking, but disable the ring buffer */
4901 atomic_inc(&cpu_buffer->record_disabled);
4902 return false;
4903}
4904
4905static inline void
4906rb_reader_unlock(struct ring_buffer_per_cpu *cpu_buffer, bool locked)
4907{
4908 if (likely(locked))
4909 raw_spin_unlock(&cpu_buffer->reader_lock);
8d707e8e
SR
4910}
4911
f83c9d0f
SR
4912/**
4913 * ring_buffer_peek - peek at the next event to be read
4914 * @buffer: The ring buffer to read
4915 * @cpu: The cpu to peak at
4916 * @ts: The timestamp counter of this event.
66a8cb95 4917 * @lost_events: a variable to store if events were lost (may be NULL)
f83c9d0f
SR
4918 *
4919 * This will return the event that will be read next, but does
4920 * not consume the data.
4921 */
4922struct ring_buffer_event *
13292494 4923ring_buffer_peek(struct trace_buffer *buffer, int cpu, u64 *ts,
66a8cb95 4924 unsigned long *lost_events)
f83c9d0f
SR
4925{
4926 struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
8aabee57 4927 struct ring_buffer_event *event;
f83c9d0f 4928 unsigned long flags;
289a5a25 4929 bool dolock;
f83c9d0f 4930
554f786e 4931 if (!cpumask_test_cpu(cpu, buffer->cpumask))
8aabee57 4932 return NULL;
554f786e 4933
2d622719 4934 again:
8d707e8e 4935 local_irq_save(flags);
289a5a25 4936 dolock = rb_reader_lock(cpu_buffer);
66a8cb95 4937 event = rb_buffer_peek(cpu_buffer, ts, lost_events);
469535a5
RR
4938 if (event && event->type_len == RINGBUF_TYPE_PADDING)
4939 rb_advance_reader(cpu_buffer);
289a5a25 4940 rb_reader_unlock(cpu_buffer, dolock);
8d707e8e 4941 local_irq_restore(flags);
f83c9d0f 4942
1b959e18 4943 if (event && event->type_len == RINGBUF_TYPE_PADDING)
2d622719 4944 goto again;
2d622719 4945
f83c9d0f
SR
4946 return event;
4947}
4948
c9b7a4a7
SRV
4949/** ring_buffer_iter_dropped - report if there are dropped events
4950 * @iter: The ring buffer iterator
4951 *
4952 * Returns true if there was dropped events since the last peek.
4953 */
4954bool ring_buffer_iter_dropped(struct ring_buffer_iter *iter)
4955{
4956 bool ret = iter->missed_events != 0;
4957
4958 iter->missed_events = 0;
4959 return ret;
4960}
4961EXPORT_SYMBOL_GPL(ring_buffer_iter_dropped);
4962
f83c9d0f
SR
4963/**
4964 * ring_buffer_iter_peek - peek at the next event to be read
4965 * @iter: The ring buffer iterator
4966 * @ts: The timestamp counter of this event.
4967 *
4968 * This will return the event that will be read next, but does
4969 * not increment the iterator.
4970 */
4971struct ring_buffer_event *
4972ring_buffer_iter_peek(struct ring_buffer_iter *iter, u64 *ts)
4973{
4974 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
4975 struct ring_buffer_event *event;
4976 unsigned long flags;
4977
2d622719 4978 again:
5389f6fa 4979 raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
f83c9d0f 4980 event = rb_iter_peek(iter, ts);
5389f6fa 4981 raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
f83c9d0f 4982
1b959e18 4983 if (event && event->type_len == RINGBUF_TYPE_PADDING)
2d622719 4984 goto again;
2d622719 4985
f83c9d0f
SR
4986 return event;
4987}
4988
7a8e76a3
SR
4989/**
4990 * ring_buffer_consume - return an event and consume it
4991 * @buffer: The ring buffer to get the next event from
66a8cb95
SR
4992 * @cpu: the cpu to read the buffer from
4993 * @ts: a variable to store the timestamp (may be NULL)
4994 * @lost_events: a variable to store if events were lost (may be NULL)
7a8e76a3
SR
4995 *
4996 * Returns the next event in the ring buffer, and that event is consumed.
4997 * Meaning, that sequential reads will keep returning a different event,
4998 * and eventually empty the ring buffer if the producer is slower.
4999 */
5000struct ring_buffer_event *
13292494 5001ring_buffer_consume(struct trace_buffer *buffer, int cpu, u64 *ts,
66a8cb95 5002 unsigned long *lost_events)
7a8e76a3 5003{
554f786e
SR
5004 struct ring_buffer_per_cpu *cpu_buffer;
5005 struct ring_buffer_event *event = NULL;
f83c9d0f 5006 unsigned long flags;
289a5a25 5007 bool dolock;
7a8e76a3 5008
2d622719 5009 again:
554f786e
SR
5010 /* might be called in atomic */
5011 preempt_disable();
5012
9e01c1b7 5013 if (!cpumask_test_cpu(cpu, buffer->cpumask))
554f786e 5014 goto out;
7a8e76a3 5015
554f786e 5016 cpu_buffer = buffer->buffers[cpu];
8d707e8e 5017 local_irq_save(flags);
289a5a25 5018 dolock = rb_reader_lock(cpu_buffer);
f83c9d0f 5019
66a8cb95
SR
5020 event = rb_buffer_peek(cpu_buffer, ts, lost_events);
5021 if (event) {
5022 cpu_buffer->lost_events = 0;
469535a5 5023 rb_advance_reader(cpu_buffer);
66a8cb95 5024 }
7a8e76a3 5025
289a5a25 5026 rb_reader_unlock(cpu_buffer, dolock);
8d707e8e 5027 local_irq_restore(flags);
f83c9d0f 5028
554f786e
SR
5029 out:
5030 preempt_enable();
5031
1b959e18 5032 if (event && event->type_len == RINGBUF_TYPE_PADDING)
2d622719 5033 goto again;
2d622719 5034
7a8e76a3
SR
5035 return event;
5036}
c4f50183 5037EXPORT_SYMBOL_GPL(ring_buffer_consume);
7a8e76a3
SR
5038
5039/**
72c9ddfd 5040 * ring_buffer_read_prepare - Prepare for a non consuming read of the buffer
7a8e76a3
SR
5041 * @buffer: The ring buffer to read from
5042 * @cpu: The cpu buffer to iterate over
31b265b3 5043 * @flags: gfp flags to use for memory allocation
7a8e76a3 5044 *
72c9ddfd 5045 * This performs the initial preparations necessary to iterate
ea70a962 5046 * through the buffer. Memory is allocated, buffer resizing
72c9ddfd 5047 * is disabled, and the iterator pointer is returned to the caller.
7a8e76a3 5048 *
72c9ddfd 5049 * After a sequence of ring_buffer_read_prepare calls, the user is
d611851b 5050 * expected to make at least one call to ring_buffer_read_prepare_sync.
72c9ddfd
DM
5051 * Afterwards, ring_buffer_read_start is invoked to get things going
5052 * for real.
5053 *
d611851b 5054 * This overall must be paired with ring_buffer_read_finish.
7a8e76a3
SR
5055 */
5056struct ring_buffer_iter *
13292494 5057ring_buffer_read_prepare(struct trace_buffer *buffer, int cpu, gfp_t flags)
7a8e76a3
SR
5058{
5059 struct ring_buffer_per_cpu *cpu_buffer;
8aabee57 5060 struct ring_buffer_iter *iter;
7a8e76a3 5061
9e01c1b7 5062 if (!cpumask_test_cpu(cpu, buffer->cpumask))
8aabee57 5063 return NULL;
7a8e76a3 5064
785888c5 5065 iter = kzalloc(sizeof(*iter), flags);
7a8e76a3 5066 if (!iter)
8aabee57 5067 return NULL;
7a8e76a3 5068
b0495258 5069 /* Holds the entire event: data and meta data */
139f8400
TSV
5070 iter->event_size = buffer->subbuf_size;
5071 iter->event = kmalloc(iter->event_size, flags);
785888c5
SRV
5072 if (!iter->event) {
5073 kfree(iter);
5074 return NULL;
5075 }
5076
7a8e76a3
SR
5077 cpu_buffer = buffer->buffers[cpu];
5078
5079 iter->cpu_buffer = cpu_buffer;
5080
07b8b10e 5081 atomic_inc(&cpu_buffer->resize_disabled);
72c9ddfd
DM
5082
5083 return iter;
5084}
5085EXPORT_SYMBOL_GPL(ring_buffer_read_prepare);
5086
5087/**
5088 * ring_buffer_read_prepare_sync - Synchronize a set of prepare calls
5089 *
5090 * All previously invoked ring_buffer_read_prepare calls to prepare
5091 * iterators will be synchronized. Afterwards, read_buffer_read_start
5092 * calls on those iterators are allowed.
5093 */
5094void
5095ring_buffer_read_prepare_sync(void)
5096{
74401729 5097 synchronize_rcu();
72c9ddfd
DM
5098}
5099EXPORT_SYMBOL_GPL(ring_buffer_read_prepare_sync);
5100
5101/**
5102 * ring_buffer_read_start - start a non consuming read of the buffer
5103 * @iter: The iterator returned by ring_buffer_read_prepare
5104 *
5105 * This finalizes the startup of an iteration through the buffer.
5106 * The iterator comes from a call to ring_buffer_read_prepare and
5107 * an intervening ring_buffer_read_prepare_sync must have been
5108 * performed.
5109 *
d611851b 5110 * Must be paired with ring_buffer_read_finish.
72c9ddfd
DM
5111 */
5112void
5113ring_buffer_read_start(struct ring_buffer_iter *iter)
5114{
5115 struct ring_buffer_per_cpu *cpu_buffer;
5116 unsigned long flags;
5117
5118 if (!iter)
5119 return;
5120
5121 cpu_buffer = iter->cpu_buffer;
7a8e76a3 5122
5389f6fa 5123 raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
0199c4e6 5124 arch_spin_lock(&cpu_buffer->lock);
642edba5 5125 rb_iter_reset(iter);
0199c4e6 5126 arch_spin_unlock(&cpu_buffer->lock);
5389f6fa 5127 raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
7a8e76a3 5128}
c4f50183 5129EXPORT_SYMBOL_GPL(ring_buffer_read_start);
7a8e76a3
SR
5130
5131/**
d611851b 5132 * ring_buffer_read_finish - finish reading the iterator of the buffer
7a8e76a3
SR
5133 * @iter: The iterator retrieved by ring_buffer_start
5134 *
ea70a962 5135 * This re-enables resizing of the buffer, and frees the iterator.
7a8e76a3
SR
5136 */
5137void
5138ring_buffer_read_finish(struct ring_buffer_iter *iter)
5139{
5140 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
9366c1ba 5141 unsigned long flags;
7a8e76a3 5142
ea70a962 5143 /* Use this opportunity to check the integrity of the ring buffer. */
9366c1ba 5144 raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
659f451f 5145 rb_check_pages(cpu_buffer);
9366c1ba 5146 raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
659f451f 5147
07b8b10e 5148 atomic_dec(&cpu_buffer->resize_disabled);
785888c5 5149 kfree(iter->event);
7a8e76a3
SR
5150 kfree(iter);
5151}
c4f50183 5152EXPORT_SYMBOL_GPL(ring_buffer_read_finish);
7a8e76a3
SR
5153
5154/**
bc1a72af 5155 * ring_buffer_iter_advance - advance the iterator to the next location
7a8e76a3 5156 * @iter: The ring buffer iterator
7a8e76a3 5157 *
bc1a72af
SRV
5158 * Move the location of the iterator such that the next read will
5159 * be the next location of the iterator.
7a8e76a3 5160 */
bc1a72af 5161void ring_buffer_iter_advance(struct ring_buffer_iter *iter)
7a8e76a3 5162{
f83c9d0f
SR
5163 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
5164 unsigned long flags;
7a8e76a3 5165
5389f6fa 5166 raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
7e9391cf 5167
7a8e76a3
SR
5168 rb_advance_iter(iter);
5169
bc1a72af 5170 raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
7a8e76a3 5171}
bc1a72af 5172EXPORT_SYMBOL_GPL(ring_buffer_iter_advance);
7a8e76a3
SR
5173
5174/**
5175 * ring_buffer_size - return the size of the ring buffer (in bytes)
5176 * @buffer: The ring buffer.
59e7cffe 5177 * @cpu: The CPU to get ring buffer size from.
7a8e76a3 5178 */
13292494 5179unsigned long ring_buffer_size(struct trace_buffer *buffer, int cpu)
7a8e76a3 5180{
438ced17
VN
5181 if (!cpumask_test_cpu(cpu, buffer->cpumask))
5182 return 0;
5183
139f8400 5184 return buffer->subbuf_size * buffer->buffers[cpu]->nr_pages;
7a8e76a3 5185}
c4f50183 5186EXPORT_SYMBOL_GPL(ring_buffer_size);
7a8e76a3 5187
8ec90be7
SRG
5188/**
5189 * ring_buffer_max_event_size - return the max data size of an event
5190 * @buffer: The ring buffer.
5191 *
5192 * Returns the maximum size an event can be.
5193 */
5194unsigned long ring_buffer_max_event_size(struct trace_buffer *buffer)
5195{
5196 /* If abs timestamp is requested, events have a timestamp too */
5197 if (ring_buffer_time_stamp_abs(buffer))
139f8400
TSV
5198 return buffer->max_data_size - RB_LEN_TIME_EXTEND;
5199 return buffer->max_data_size;
8ec90be7
SRG
5200}
5201EXPORT_SYMBOL_GPL(ring_buffer_max_event_size);
5202
7e42907f
ZY
5203static void rb_clear_buffer_page(struct buffer_page *page)
5204{
5205 local_set(&page->write, 0);
5206 local_set(&page->entries, 0);
5207 rb_init_page(page->page);
5208 page->read = 0;
5209}
5210
117c3920
VD
5211static void rb_update_meta_page(struct ring_buffer_per_cpu *cpu_buffer)
5212{
5213 struct trace_buffer_meta *meta = cpu_buffer->meta_page;
5214
5215 meta->reader.read = cpu_buffer->reader_page->read;
5216 meta->reader.id = cpu_buffer->reader_page->id;
5217 meta->reader.lost_events = cpu_buffer->lost_events;
5218
5219 meta->entries = local_read(&cpu_buffer->entries);
5220 meta->overrun = local_read(&cpu_buffer->overrun);
5221 meta->read = cpu_buffer->read;
5222
5223 /* Some archs do not have data cache coherency between kernel and user-space */
5224 flush_dcache_folio(virt_to_folio(cpu_buffer->meta_page));
5225}
5226
7a8e76a3
SR
5227static void
5228rb_reset_cpu(struct ring_buffer_per_cpu *cpu_buffer)
5229{
7e42907f
ZY
5230 struct buffer_page *page;
5231
77ae365e
SR
5232 rb_head_page_deactivate(cpu_buffer);
5233
7a8e76a3 5234 cpu_buffer->head_page
3adc54fa 5235 = list_entry(cpu_buffer->pages, struct buffer_page, list);
7e42907f
ZY
5236 rb_clear_buffer_page(cpu_buffer->head_page);
5237 list_for_each_entry(page, cpu_buffer->pages, list) {
5238 rb_clear_buffer_page(page);
5239 }
bf41a158
SR
5240
5241 cpu_buffer->tail_page = cpu_buffer->head_page;
5242 cpu_buffer->commit_page = cpu_buffer->head_page;
5243
5244 INIT_LIST_HEAD(&cpu_buffer->reader_page->list);
5040b4b7 5245 INIT_LIST_HEAD(&cpu_buffer->new_pages);
7e42907f 5246 rb_clear_buffer_page(cpu_buffer->reader_page);
7a8e76a3 5247
c64e148a 5248 local_set(&cpu_buffer->entries_bytes, 0);
77ae365e 5249 local_set(&cpu_buffer->overrun, 0);
884bfe89
SP
5250 local_set(&cpu_buffer->commit_overrun, 0);
5251 local_set(&cpu_buffer->dropped_events, 0);
e4906eff 5252 local_set(&cpu_buffer->entries, 0);
fa743953
SR
5253 local_set(&cpu_buffer->committing, 0);
5254 local_set(&cpu_buffer->commits, 0);
2c2b0a78 5255 local_set(&cpu_buffer->pages_touched, 0);
31029a8b 5256 local_set(&cpu_buffer->pages_lost, 0);
2c2b0a78 5257 local_set(&cpu_buffer->pages_read, 0);
03329f99 5258 cpu_buffer->last_pages_touch = 0;
2c2b0a78 5259 cpu_buffer->shortest_full = 0;
77ae365e 5260 cpu_buffer->read = 0;
c64e148a 5261 cpu_buffer->read_bytes = 0;
69507c06 5262
10464b4a
SRV
5263 rb_time_set(&cpu_buffer->write_stamp, 0);
5264 rb_time_set(&cpu_buffer->before_stamp, 0);
77ae365e 5265
8672e494
SRV
5266 memset(cpu_buffer->event_stamp, 0, sizeof(cpu_buffer->event_stamp));
5267
66a8cb95
SR
5268 cpu_buffer->lost_events = 0;
5269 cpu_buffer->last_overrun = 0;
5270
117c3920
VD
5271 if (cpu_buffer->mapped)
5272 rb_update_meta_page(cpu_buffer);
5273
77ae365e 5274 rb_head_page_activate(cpu_buffer);
2d093282 5275 cpu_buffer->pages_removed = 0;
7a8e76a3
SR
5276}
5277
b23d7a5f
NP
5278/* Must have disabled the cpu buffer then done a synchronize_rcu */
5279static void reset_disabled_cpu_buffer(struct ring_buffer_per_cpu *cpu_buffer)
5280{
5281 unsigned long flags;
5282
5283 raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
5284
5285 if (RB_WARN_ON(cpu_buffer, local_read(&cpu_buffer->committing)))
5286 goto out;
5287
5288 arch_spin_lock(&cpu_buffer->lock);
5289
5290 rb_reset_cpu(cpu_buffer);
5291
5292 arch_spin_unlock(&cpu_buffer->lock);
5293
5294 out:
5295 raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
5296}
5297
7a8e76a3
SR
5298/**
5299 * ring_buffer_reset_cpu - reset a ring buffer per CPU buffer
5300 * @buffer: The ring buffer to reset a per cpu buffer of
5301 * @cpu: The CPU buffer to be reset
5302 */
13292494 5303void ring_buffer_reset_cpu(struct trace_buffer *buffer, int cpu)
7a8e76a3
SR
5304{
5305 struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
7a8e76a3 5306
9e01c1b7 5307 if (!cpumask_test_cpu(cpu, buffer->cpumask))
8aabee57 5308 return;
7a8e76a3 5309
bbeb9746
GK
5310 /* prevent another thread from changing buffer sizes */
5311 mutex_lock(&buffer->mutex);
5312
07b8b10e 5313 atomic_inc(&cpu_buffer->resize_disabled);
41ede23e
SR
5314 atomic_inc(&cpu_buffer->record_disabled);
5315
83f40318 5316 /* Make sure all commits have finished */
74401729 5317 synchronize_rcu();
83f40318 5318
b23d7a5f 5319 reset_disabled_cpu_buffer(cpu_buffer);
f83c9d0f 5320
b23d7a5f
NP
5321 atomic_dec(&cpu_buffer->record_disabled);
5322 atomic_dec(&cpu_buffer->resize_disabled);
bbeb9746
GK
5323
5324 mutex_unlock(&buffer->mutex);
b23d7a5f
NP
5325}
5326EXPORT_SYMBOL_GPL(ring_buffer_reset_cpu);
41b6a95d 5327
7c339fb4
TW
5328/* Flag to ensure proper resetting of atomic variables */
5329#define RESET_BIT (1 << 30)
5330
b23d7a5f 5331/**
b7085b6f 5332 * ring_buffer_reset_online_cpus - reset a ring buffer per CPU buffer
b23d7a5f 5333 * @buffer: The ring buffer to reset a per cpu buffer of
b23d7a5f
NP
5334 */
5335void ring_buffer_reset_online_cpus(struct trace_buffer *buffer)
5336{
5337 struct ring_buffer_per_cpu *cpu_buffer;
5338 int cpu;
7a8e76a3 5339
bbeb9746
GK
5340 /* prevent another thread from changing buffer sizes */
5341 mutex_lock(&buffer->mutex);
5342
b23d7a5f
NP
5343 for_each_online_buffer_cpu(buffer, cpu) {
5344 cpu_buffer = buffer->buffers[cpu];
7a8e76a3 5345
7c339fb4 5346 atomic_add(RESET_BIT, &cpu_buffer->resize_disabled);
b23d7a5f
NP
5347 atomic_inc(&cpu_buffer->record_disabled);
5348 }
f83c9d0f 5349
b23d7a5f
NP
5350 /* Make sure all commits have finished */
5351 synchronize_rcu();
41ede23e 5352
7c339fb4 5353 for_each_buffer_cpu(buffer, cpu) {
b23d7a5f
NP
5354 cpu_buffer = buffer->buffers[cpu];
5355
7c339fb4
TW
5356 /*
5357 * If a CPU came online during the synchronize_rcu(), then
5358 * ignore it.
5359 */
5360 if (!(atomic_read(&cpu_buffer->resize_disabled) & RESET_BIT))
5361 continue;
5362
b23d7a5f
NP
5363 reset_disabled_cpu_buffer(cpu_buffer);
5364
5365 atomic_dec(&cpu_buffer->record_disabled);
7c339fb4 5366 atomic_sub(RESET_BIT, &cpu_buffer->resize_disabled);
b23d7a5f 5367 }
bbeb9746
GK
5368
5369 mutex_unlock(&buffer->mutex);
7a8e76a3
SR
5370}
5371
5372/**
5373 * ring_buffer_reset - reset a ring buffer
5374 * @buffer: The ring buffer to reset all cpu buffers
5375 */
13292494 5376void ring_buffer_reset(struct trace_buffer *buffer)
7a8e76a3 5377{
b23d7a5f 5378 struct ring_buffer_per_cpu *cpu_buffer;
7a8e76a3
SR
5379 int cpu;
5380
51d15794
SRV
5381 /* prevent another thread from changing buffer sizes */
5382 mutex_lock(&buffer->mutex);
5383
b23d7a5f
NP
5384 for_each_buffer_cpu(buffer, cpu) {
5385 cpu_buffer = buffer->buffers[cpu];
5386
5387 atomic_inc(&cpu_buffer->resize_disabled);
5388 atomic_inc(&cpu_buffer->record_disabled);
5389 }
5390
5391 /* Make sure all commits have finished */
5392 synchronize_rcu();
5393
5394 for_each_buffer_cpu(buffer, cpu) {
5395 cpu_buffer = buffer->buffers[cpu];
5396
5397 reset_disabled_cpu_buffer(cpu_buffer);
5398
5399 atomic_dec(&cpu_buffer->record_disabled);
5400 atomic_dec(&cpu_buffer->resize_disabled);
5401 }
51d15794
SRV
5402
5403 mutex_unlock(&buffer->mutex);
7a8e76a3 5404}
c4f50183 5405EXPORT_SYMBOL_GPL(ring_buffer_reset);
7a8e76a3
SR
5406
5407/**
b7085b6f 5408 * ring_buffer_empty - is the ring buffer empty?
7a8e76a3
SR
5409 * @buffer: The ring buffer to test
5410 */
13292494 5411bool ring_buffer_empty(struct trace_buffer *buffer)
7a8e76a3
SR
5412{
5413 struct ring_buffer_per_cpu *cpu_buffer;
d4788207 5414 unsigned long flags;
289a5a25 5415 bool dolock;
bc92b956 5416 bool ret;
7a8e76a3
SR
5417 int cpu;
5418
5419 /* yes this is racy, but if you don't like the race, lock the buffer */
5420 for_each_buffer_cpu(buffer, cpu) {
5421 cpu_buffer = buffer->buffers[cpu];
8d707e8e 5422 local_irq_save(flags);
289a5a25 5423 dolock = rb_reader_lock(cpu_buffer);
d4788207 5424 ret = rb_per_cpu_empty(cpu_buffer);
289a5a25 5425 rb_reader_unlock(cpu_buffer, dolock);
8d707e8e
SR
5426 local_irq_restore(flags);
5427
d4788207 5428 if (!ret)
3d4e204d 5429 return false;
7a8e76a3 5430 }
554f786e 5431
3d4e204d 5432 return true;
7a8e76a3 5433}
c4f50183 5434EXPORT_SYMBOL_GPL(ring_buffer_empty);
7a8e76a3
SR
5435
5436/**
5437 * ring_buffer_empty_cpu - is a cpu buffer of a ring buffer empty?
5438 * @buffer: The ring buffer
5439 * @cpu: The CPU buffer to test
5440 */
13292494 5441bool ring_buffer_empty_cpu(struct trace_buffer *buffer, int cpu)
7a8e76a3
SR
5442{
5443 struct ring_buffer_per_cpu *cpu_buffer;
d4788207 5444 unsigned long flags;
289a5a25 5445 bool dolock;
bc92b956 5446 bool ret;
7a8e76a3 5447
9e01c1b7 5448 if (!cpumask_test_cpu(cpu, buffer->cpumask))
3d4e204d 5449 return true;
7a8e76a3
SR
5450
5451 cpu_buffer = buffer->buffers[cpu];
8d707e8e 5452 local_irq_save(flags);
289a5a25 5453 dolock = rb_reader_lock(cpu_buffer);
554f786e 5454 ret = rb_per_cpu_empty(cpu_buffer);
289a5a25 5455 rb_reader_unlock(cpu_buffer, dolock);
8d707e8e 5456 local_irq_restore(flags);
554f786e
SR
5457
5458 return ret;
7a8e76a3 5459}
c4f50183 5460EXPORT_SYMBOL_GPL(ring_buffer_empty_cpu);
7a8e76a3 5461
85bac32c 5462#ifdef CONFIG_RING_BUFFER_ALLOW_SWAP
7a8e76a3
SR
5463/**
5464 * ring_buffer_swap_cpu - swap a CPU buffer between two ring buffers
5465 * @buffer_a: One buffer to swap with
5466 * @buffer_b: The other buffer to swap with
59e7cffe 5467 * @cpu: the CPU of the buffers to swap
7a8e76a3
SR
5468 *
5469 * This function is useful for tracers that want to take a "snapshot"
5470 * of a CPU buffer and has another back up buffer lying around.
5471 * it is expected that the tracer handles the cpu buffer not being
5472 * used at the moment.
5473 */
13292494
SRV
5474int ring_buffer_swap_cpu(struct trace_buffer *buffer_a,
5475 struct trace_buffer *buffer_b, int cpu)
7a8e76a3
SR
5476{
5477 struct ring_buffer_per_cpu *cpu_buffer_a;
5478 struct ring_buffer_per_cpu *cpu_buffer_b;
554f786e
SR
5479 int ret = -EINVAL;
5480
9e01c1b7
RR
5481 if (!cpumask_test_cpu(cpu, buffer_a->cpumask) ||
5482 !cpumask_test_cpu(cpu, buffer_b->cpumask))
554f786e 5483 goto out;
7a8e76a3 5484
438ced17
VN
5485 cpu_buffer_a = buffer_a->buffers[cpu];
5486 cpu_buffer_b = buffer_b->buffers[cpu];
5487
117c3920
VD
5488 /* It's up to the callers to not try to swap mapped buffers */
5489 if (WARN_ON_ONCE(cpu_buffer_a->mapped || cpu_buffer_b->mapped)) {
5490 ret = -EBUSY;
5491 goto out;
5492 }
5493
7a8e76a3 5494 /* At least make sure the two buffers are somewhat the same */
438ced17 5495 if (cpu_buffer_a->nr_pages != cpu_buffer_b->nr_pages)
554f786e
SR
5496 goto out;
5497
b81e03a2
SRG
5498 if (buffer_a->subbuf_order != buffer_b->subbuf_order)
5499 goto out;
5500
554f786e 5501 ret = -EAGAIN;
7a8e76a3 5502
97b17efe 5503 if (atomic_read(&buffer_a->record_disabled))
554f786e 5504 goto out;
97b17efe
SR
5505
5506 if (atomic_read(&buffer_b->record_disabled))
554f786e 5507 goto out;
97b17efe 5508
97b17efe 5509 if (atomic_read(&cpu_buffer_a->record_disabled))
554f786e 5510 goto out;
97b17efe
SR
5511
5512 if (atomic_read(&cpu_buffer_b->record_disabled))
554f786e 5513 goto out;
97b17efe 5514
7a8e76a3 5515 /*
74401729 5516 * We can't do a synchronize_rcu here because this
7a8e76a3
SR
5517 * function can be called in atomic context.
5518 * Normally this will be called from the same CPU as cpu.
5519 * If not it's up to the caller to protect this.
5520 */
5521 atomic_inc(&cpu_buffer_a->record_disabled);
5522 atomic_inc(&cpu_buffer_b->record_disabled);
5523
98277991
SR
5524 ret = -EBUSY;
5525 if (local_read(&cpu_buffer_a->committing))
5526 goto out_dec;
5527 if (local_read(&cpu_buffer_b->committing))
5528 goto out_dec;
5529
8a96c028
CL
5530 /*
5531 * When resize is in progress, we cannot swap it because
5532 * it will mess the state of the cpu buffer.
5533 */
5534 if (atomic_read(&buffer_a->resizing))
5535 goto out_dec;
5536 if (atomic_read(&buffer_b->resizing))
5537 goto out_dec;
5538
7a8e76a3
SR
5539 buffer_a->buffers[cpu] = cpu_buffer_b;
5540 buffer_b->buffers[cpu] = cpu_buffer_a;
5541
5542 cpu_buffer_b->buffer = buffer_a;
5543 cpu_buffer_a->buffer = buffer_b;
5544
98277991
SR
5545 ret = 0;
5546
5547out_dec:
7a8e76a3
SR
5548 atomic_dec(&cpu_buffer_a->record_disabled);
5549 atomic_dec(&cpu_buffer_b->record_disabled);
554f786e 5550out:
554f786e 5551 return ret;
7a8e76a3 5552}
c4f50183 5553EXPORT_SYMBOL_GPL(ring_buffer_swap_cpu);
85bac32c 5554#endif /* CONFIG_RING_BUFFER_ALLOW_SWAP */
7a8e76a3 5555
8789a9e7
SR
5556/**
5557 * ring_buffer_alloc_read_page - allocate a page to read from buffer
5558 * @buffer: the buffer to allocate for.
d611851b 5559 * @cpu: the cpu buffer to allocate.
8789a9e7
SR
5560 *
5561 * This function is used in conjunction with ring_buffer_read_page.
5562 * When reading a full page from the ring buffer, these functions
5563 * can be used to speed up the process. The calling function should
5564 * allocate a few pages first with this function. Then when it
5565 * needs to get pages from the ring buffer, it passes the result
5566 * of this function into ring_buffer_read_page, which will swap
5567 * the page that was allocated, with the read page of the buffer.
5568 *
5569 * Returns:
a7e52ad7 5570 * The page allocated, or ERR_PTR
8789a9e7 5571 */
bce761d7
TSV
5572struct buffer_data_read_page *
5573ring_buffer_alloc_read_page(struct trace_buffer *buffer, int cpu)
8789a9e7 5574{
a7e52ad7 5575 struct ring_buffer_per_cpu *cpu_buffer;
bce761d7 5576 struct buffer_data_read_page *bpage = NULL;
73a757e6 5577 unsigned long flags;
7ea59064 5578 struct page *page;
8789a9e7 5579
a7e52ad7
SRV
5580 if (!cpumask_test_cpu(cpu, buffer->cpumask))
5581 return ERR_PTR(-ENODEV);
5582
bce761d7
TSV
5583 bpage = kzalloc(sizeof(*bpage), GFP_KERNEL);
5584 if (!bpage)
5585 return ERR_PTR(-ENOMEM);
5586
5587 bpage->order = buffer->subbuf_order;
a7e52ad7 5588 cpu_buffer = buffer->buffers[cpu];
73a757e6
SRV
5589 local_irq_save(flags);
5590 arch_spin_lock(&cpu_buffer->lock);
5591
5592 if (cpu_buffer->free_page) {
bce761d7 5593 bpage->data = cpu_buffer->free_page;
73a757e6
SRV
5594 cpu_buffer->free_page = NULL;
5595 }
5596
5597 arch_spin_unlock(&cpu_buffer->lock);
5598 local_irq_restore(flags);
5599
bce761d7 5600 if (bpage->data)
73a757e6
SRV
5601 goto out;
5602
6b76323e 5603 page = alloc_pages_node(cpu_to_node(cpu),
c09d4167 5604 GFP_KERNEL | __GFP_NORETRY | __GFP_COMP | __GFP_ZERO,
f9b94daa 5605 cpu_buffer->buffer->subbuf_order);
bce761d7
TSV
5606 if (!page) {
5607 kfree(bpage);
a7e52ad7 5608 return ERR_PTR(-ENOMEM);
bce761d7 5609 }
8789a9e7 5610
bce761d7 5611 bpage->data = page_address(page);
8789a9e7 5612
73a757e6 5613 out:
bce761d7 5614 rb_init_page(bpage->data);
ef7a4a16 5615
044fa782 5616 return bpage;
8789a9e7 5617}
d6ce96da 5618EXPORT_SYMBOL_GPL(ring_buffer_alloc_read_page);
8789a9e7
SR
5619
5620/**
5621 * ring_buffer_free_read_page - free an allocated read page
5622 * @buffer: the buffer the page was allocate for
73a757e6 5623 * @cpu: the cpu buffer the page came from
bce761d7 5624 * @data_page: the page to free
8789a9e7
SR
5625 *
5626 * Free a page allocated from ring_buffer_alloc_read_page.
5627 */
bce761d7
TSV
5628void ring_buffer_free_read_page(struct trace_buffer *buffer, int cpu,
5629 struct buffer_data_read_page *data_page)
8789a9e7 5630{
3e4272b9 5631 struct ring_buffer_per_cpu *cpu_buffer;
bce761d7 5632 struct buffer_data_page *bpage = data_page->data;
ae415fa4 5633 struct page *page = virt_to_page(bpage);
73a757e6
SRV
5634 unsigned long flags;
5635
3e4272b9
JJB
5636 if (!buffer || !buffer->buffers || !buffer->buffers[cpu])
5637 return;
5638
5639 cpu_buffer = buffer->buffers[cpu];
5640
bce761d7
TSV
5641 /*
5642 * If the page is still in use someplace else, or order of the page
5643 * is different from the subbuffer order of the buffer -
5644 * we can't reuse it
5645 */
5646 if (page_ref_count(page) > 1 || data_page->order != buffer->subbuf_order)
ae415fa4
SRV
5647 goto out;
5648
73a757e6
SRV
5649 local_irq_save(flags);
5650 arch_spin_lock(&cpu_buffer->lock);
5651
5652 if (!cpu_buffer->free_page) {
5653 cpu_buffer->free_page = bpage;
5654 bpage = NULL;
5655 }
5656
5657 arch_spin_unlock(&cpu_buffer->lock);
5658 local_irq_restore(flags);
5659
ae415fa4 5660 out:
bce761d7
TSV
5661 free_pages((unsigned long)bpage, data_page->order);
5662 kfree(data_page);
8789a9e7 5663}
d6ce96da 5664EXPORT_SYMBOL_GPL(ring_buffer_free_read_page);
8789a9e7
SR
5665
5666/**
5667 * ring_buffer_read_page - extract a page from the ring buffer
5668 * @buffer: buffer to extract from
5669 * @data_page: the page to use allocated from ring_buffer_alloc_read_page
ef7a4a16 5670 * @len: amount to extract
8789a9e7
SR
5671 * @cpu: the cpu of the buffer to extract
5672 * @full: should the extraction only happen when the page is full.
5673 *
5674 * This function will pull out a page from the ring buffer and consume it.
5675 * @data_page must be the address of the variable that was returned
5676 * from ring_buffer_alloc_read_page. This is because the page might be used
5677 * to swap with a page in the ring buffer.
5678 *
5679 * for example:
d611851b 5680 * rpage = ring_buffer_alloc_read_page(buffer, cpu);
a7e52ad7
SRV
5681 * if (IS_ERR(rpage))
5682 * return PTR_ERR(rpage);
bce761d7 5683 * ret = ring_buffer_read_page(buffer, rpage, len, cpu, 0);
667d2412 5684 * if (ret >= 0)
bce761d7
TSV
5685 * process_page(ring_buffer_read_page_data(rpage), ret);
5686 * ring_buffer_free_read_page(buffer, cpu, rpage);
8789a9e7
SR
5687 *
5688 * When @full is set, the function will not return true unless
5689 * the writer is off the reader page.
5690 *
5691 * Note: it is up to the calling functions to handle sleeps and wakeups.
5692 * The ring buffer can be used anywhere in the kernel and can not
5693 * blindly call wake_up. The layer that uses the ring buffer must be
5694 * responsible for that.
5695 *
5696 * Returns:
667d2412
LJ
5697 * >=0 if data has been transferred, returns the offset of consumed data.
5698 * <0 if no data has been transferred.
8789a9e7 5699 */
13292494 5700int ring_buffer_read_page(struct trace_buffer *buffer,
bce761d7
TSV
5701 struct buffer_data_read_page *data_page,
5702 size_t len, int cpu, int full)
8789a9e7
SR
5703{
5704 struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
5705 struct ring_buffer_event *event;
044fa782 5706 struct buffer_data_page *bpage;
ef7a4a16 5707 struct buffer_page *reader;
ff0ff84a 5708 unsigned long missed_events;
8789a9e7 5709 unsigned long flags;
ef7a4a16 5710 unsigned int commit;
667d2412 5711 unsigned int read;
4f3640f8 5712 u64 save_timestamp;
667d2412 5713 int ret = -1;
8789a9e7 5714
554f786e
SR
5715 if (!cpumask_test_cpu(cpu, buffer->cpumask))
5716 goto out;
5717
474d32b6
SR
5718 /*
5719 * If len is not big enough to hold the page header, then
5720 * we can not copy anything.
5721 */
5722 if (len <= BUF_PAGE_HDR_SIZE)
554f786e 5723 goto out;
474d32b6
SR
5724
5725 len -= BUF_PAGE_HDR_SIZE;
5726
bce761d7
TSV
5727 if (!data_page || !data_page->data)
5728 goto out;
5729 if (data_page->order != buffer->subbuf_order)
554f786e 5730 goto out;
8789a9e7 5731
bce761d7 5732 bpage = data_page->data;
044fa782 5733 if (!bpage)
554f786e 5734 goto out;
8789a9e7 5735
5389f6fa 5736 raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
8789a9e7 5737
ef7a4a16
SR
5738 reader = rb_get_reader_page(cpu_buffer);
5739 if (!reader)
554f786e 5740 goto out_unlock;
8789a9e7 5741
ef7a4a16
SR
5742 event = rb_reader_event(cpu_buffer);
5743
5744 read = reader->read;
fe832be0 5745 commit = rb_page_size(reader);
667d2412 5746
66a8cb95 5747 /* Check if any events were dropped */
ff0ff84a 5748 missed_events = cpu_buffer->lost_events;
66a8cb95 5749
8789a9e7 5750 /*
474d32b6
SR
5751 * If this page has been partially read or
5752 * if len is not big enough to read the rest of the page or
5753 * a writer is still on the page, then
5754 * we must copy the data from the page to the buffer.
5755 * Otherwise, we can simply swap the page with the one passed in.
8789a9e7 5756 */
474d32b6 5757 if (read || (len < (commit - read)) ||
117c3920
VD
5758 cpu_buffer->reader_page == cpu_buffer->commit_page ||
5759 cpu_buffer->mapped) {
667d2412 5760 struct buffer_data_page *rpage = cpu_buffer->reader_page->page;
474d32b6
SR
5761 unsigned int rpos = read;
5762 unsigned int pos = 0;
ef7a4a16 5763 unsigned int size;
8789a9e7 5764
fa8f4a89
SRG
5765 /*
5766 * If a full page is expected, this can still be returned
5767 * if there's been a previous partial read and the
5768 * rest of the page can be read and the commit page is off
5769 * the reader page.
5770 */
5771 if (full &&
5772 (!read || (len < (commit - read)) ||
5773 cpu_buffer->reader_page == cpu_buffer->commit_page))
554f786e 5774 goto out_unlock;
8789a9e7 5775
ef7a4a16
SR
5776 if (len > (commit - read))
5777 len = (commit - read);
5778
69d1b839
SR
5779 /* Always keep the time extend and data together */
5780 size = rb_event_ts_length(event);
ef7a4a16
SR
5781
5782 if (len < size)
554f786e 5783 goto out_unlock;
ef7a4a16 5784
4f3640f8
SR
5785 /* save the current timestamp, since the user will need it */
5786 save_timestamp = cpu_buffer->read_stamp;
5787
ef7a4a16
SR
5788 /* Need to copy one event at a time */
5789 do {
e1e35927
DS
5790 /* We need the size of one event, because
5791 * rb_advance_reader only advances by one event,
5792 * whereas rb_event_ts_length may include the size of
5793 * one or two events.
5794 * We have already ensured there's enough space if this
5795 * is a time extend. */
5796 size = rb_event_length(event);
474d32b6 5797 memcpy(bpage->data + pos, rpage->data + rpos, size);
ef7a4a16
SR
5798
5799 len -= size;
5800
5801 rb_advance_reader(cpu_buffer);
474d32b6
SR
5802 rpos = reader->read;
5803 pos += size;
ef7a4a16 5804
18fab912
HY
5805 if (rpos >= commit)
5806 break;
5807
ef7a4a16 5808 event = rb_reader_event(cpu_buffer);
69d1b839
SR
5809 /* Always keep the time extend and data together */
5810 size = rb_event_ts_length(event);
e1e35927 5811 } while (len >= size);
667d2412
LJ
5812
5813 /* update bpage */
ef7a4a16 5814 local_set(&bpage->commit, pos);
4f3640f8 5815 bpage->time_stamp = save_timestamp;
ef7a4a16 5816
474d32b6
SR
5817 /* we copied everything to the beginning */
5818 read = 0;
8789a9e7 5819 } else {
afbab76a 5820 /* update the entry counter */
77ae365e 5821 cpu_buffer->read += rb_page_entries(reader);
fe832be0 5822 cpu_buffer->read_bytes += rb_page_size(reader);
afbab76a 5823
8789a9e7 5824 /* swap the pages */
044fa782 5825 rb_init_page(bpage);
ef7a4a16 5826 bpage = reader->page;
bce761d7 5827 reader->page = data_page->data;
ef7a4a16 5828 local_set(&reader->write, 0);
778c55d4 5829 local_set(&reader->entries, 0);
ef7a4a16 5830 reader->read = 0;
bce761d7 5831 data_page->data = bpage;
ff0ff84a
SR
5832
5833 /*
5834 * Use the real_end for the data size,
5835 * This gives us a chance to store the lost events
5836 * on the page.
5837 */
5838 if (reader->real_end)
5839 local_set(&bpage->commit, reader->real_end);
8789a9e7 5840 }
667d2412 5841 ret = read;
8789a9e7 5842
66a8cb95 5843 cpu_buffer->lost_events = 0;
2711ca23
SR
5844
5845 commit = local_read(&bpage->commit);
66a8cb95
SR
5846 /*
5847 * Set a flag in the commit field if we lost events
5848 */
ff0ff84a 5849 if (missed_events) {
ff0ff84a
SR
5850 /* If there is room at the end of the page to save the
5851 * missed events, then record it there.
5852 */
139f8400 5853 if (buffer->subbuf_size - commit >= sizeof(missed_events)) {
ff0ff84a
SR
5854 memcpy(&bpage->data[commit], &missed_events,
5855 sizeof(missed_events));
5856 local_add(RB_MISSED_STORED, &bpage->commit);
2711ca23 5857 commit += sizeof(missed_events);
ff0ff84a 5858 }
66a8cb95 5859 local_add(RB_MISSED_EVENTS, &bpage->commit);
ff0ff84a 5860 }
66a8cb95 5861
2711ca23
SR
5862 /*
5863 * This page may be off to user land. Zero it out here.
5864 */
139f8400
TSV
5865 if (commit < buffer->subbuf_size)
5866 memset(&bpage->data[commit], 0, buffer->subbuf_size - commit);
2711ca23 5867
554f786e 5868 out_unlock:
5389f6fa 5869 raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
8789a9e7 5870
554f786e 5871 out:
8789a9e7
SR
5872 return ret;
5873}
d6ce96da 5874EXPORT_SYMBOL_GPL(ring_buffer_read_page);
8789a9e7 5875
bce761d7
TSV
5876/**
5877 * ring_buffer_read_page_data - get pointer to the data in the page.
5878 * @page: the page to get the data from
5879 *
5880 * Returns pointer to the actual data in this page.
5881 */
5882void *ring_buffer_read_page_data(struct buffer_data_read_page *page)
5883{
5884 return page->data;
5885}
5886EXPORT_SYMBOL_GPL(ring_buffer_read_page_data);
5887
2808e31e
TSV
5888/**
5889 * ring_buffer_subbuf_size_get - get size of the sub buffer.
5890 * @buffer: the buffer to get the sub buffer size from
5891 *
5892 * Returns size of the sub buffer, in bytes.
5893 */
5894int ring_buffer_subbuf_size_get(struct trace_buffer *buffer)
5895{
5896 return buffer->subbuf_size + BUF_PAGE_HDR_SIZE;
5897}
5898EXPORT_SYMBOL_GPL(ring_buffer_subbuf_size_get);
5899
5900/**
5901 * ring_buffer_subbuf_order_get - get order of system sub pages in one buffer page.
5902 * @buffer: The ring_buffer to get the system sub page order from
5903 *
5904 * By default, one ring buffer sub page equals to one system page. This parameter
5905 * is configurable, per ring buffer. The size of the ring buffer sub page can be
5906 * extended, but must be an order of system page size.
5907 *
5908 * Returns the order of buffer sub page size, in system pages:
5909 * 0 means the sub buffer size is 1 system page and so forth.
5910 * In case of an error < 0 is returned.
5911 */
5912int ring_buffer_subbuf_order_get(struct trace_buffer *buffer)
5913{
5914 if (!buffer)
5915 return -EINVAL;
5916
5917 return buffer->subbuf_order;
5918}
5919EXPORT_SYMBOL_GPL(ring_buffer_subbuf_order_get);
5920
5921/**
5922 * ring_buffer_subbuf_order_set - set the size of ring buffer sub page.
5923 * @buffer: The ring_buffer to set the new page size.
5924 * @order: Order of the system pages in one sub buffer page
5925 *
5926 * By default, one ring buffer pages equals to one system page. This API can be
5927 * used to set new size of the ring buffer page. The size must be order of
5928 * system page size, that's why the input parameter @order is the order of
5929 * system pages that are allocated for one ring buffer page:
5930 * 0 - 1 system page
5931 * 1 - 2 system pages
5932 * 3 - 4 system pages
5933 * ...
5934 *
5935 * Returns 0 on success or < 0 in case of an error.
5936 */
5937int ring_buffer_subbuf_order_set(struct trace_buffer *buffer, int order)
5938{
8e7b58c2
SRG
5939 struct ring_buffer_per_cpu *cpu_buffer;
5940 struct buffer_page *bpage, *tmp;
f9b94daa
TSV
5941 int old_order, old_size;
5942 int nr_pages;
2808e31e 5943 int psize;
f9b94daa
TSV
5944 int err;
5945 int cpu;
2808e31e
TSV
5946
5947 if (!buffer || order < 0)
5948 return -EINVAL;
5949
5950 if (buffer->subbuf_order == order)
5951 return 0;
5952
5953 psize = (1 << order) * PAGE_SIZE;
5954 if (psize <= BUF_PAGE_HDR_SIZE)
5955 return -EINVAL;
5956
e78fb4ea
SRG
5957 /* Size of a subbuf cannot be greater than the write counter */
5958 if (psize > RB_WRITE_MASK + 1)
5959 return -EINVAL;
5960
f9b94daa
TSV
5961 old_order = buffer->subbuf_order;
5962 old_size = buffer->subbuf_size;
5963
5964 /* prevent another thread from changing buffer sizes */
5965 mutex_lock(&buffer->mutex);
5966 atomic_inc(&buffer->record_disabled);
5967
5968 /* Make sure all commits have finished */
5969 synchronize_rcu();
5970
2808e31e
TSV
5971 buffer->subbuf_order = order;
5972 buffer->subbuf_size = psize - BUF_PAGE_HDR_SIZE;
5973
f9b94daa
TSV
5974 /* Make sure all new buffers are allocated, before deleting the old ones */
5975 for_each_buffer_cpu(buffer, cpu) {
8e7b58c2 5976
f9b94daa
TSV
5977 if (!cpumask_test_cpu(cpu, buffer->cpumask))
5978 continue;
5979
8e7b58c2
SRG
5980 cpu_buffer = buffer->buffers[cpu];
5981
117c3920
VD
5982 if (cpu_buffer->mapped) {
5983 err = -EBUSY;
5984 goto error;
5985 }
5986
353cc219
SRG
5987 /* Update the number of pages to match the new size */
5988 nr_pages = old_size * buffer->buffers[cpu]->nr_pages;
5989 nr_pages = DIV_ROUND_UP(nr_pages, buffer->subbuf_size);
5990
8e7b58c2
SRG
5991 /* we need a minimum of two pages */
5992 if (nr_pages < 2)
5993 nr_pages = 2;
5994
5995 cpu_buffer->nr_pages_to_update = nr_pages;
5996
5997 /* Include the reader page */
5998 nr_pages++;
5999
6000 /* Allocate the new size buffer */
6001 INIT_LIST_HEAD(&cpu_buffer->new_pages);
6002 if (__rb_allocate_pages(cpu_buffer, nr_pages,
6003 &cpu_buffer->new_pages)) {
6004 /* not enough memory for new pages */
f9b94daa
TSV
6005 err = -ENOMEM;
6006 goto error;
6007 }
6008 }
6009
6010 for_each_buffer_cpu(buffer, cpu) {
8e7b58c2 6011
f9b94daa
TSV
6012 if (!cpumask_test_cpu(cpu, buffer->cpumask))
6013 continue;
6014
8e7b58c2
SRG
6015 cpu_buffer = buffer->buffers[cpu];
6016
6017 /* Clear the head bit to make the link list normal to read */
6018 rb_head_page_deactivate(cpu_buffer);
6019
6020 /* Now walk the list and free all the old sub buffers */
6021 list_for_each_entry_safe(bpage, tmp, cpu_buffer->pages, list) {
6022 list_del_init(&bpage->list);
6023 free_buffer_page(bpage);
6024 }
6025 /* The above loop stopped an the last page needing to be freed */
6026 bpage = list_entry(cpu_buffer->pages, struct buffer_page, list);
6027 free_buffer_page(bpage);
6028
6029 /* Free the current reader page */
6030 free_buffer_page(cpu_buffer->reader_page);
6031
6032 /* One page was allocated for the reader page */
6033 cpu_buffer->reader_page = list_entry(cpu_buffer->new_pages.next,
6034 struct buffer_page, list);
6035 list_del_init(&cpu_buffer->reader_page->list);
6036
6037 /* The cpu_buffer pages are a link list with no head */
6038 cpu_buffer->pages = cpu_buffer->new_pages.next;
6039 cpu_buffer->new_pages.next->prev = cpu_buffer->new_pages.prev;
6040 cpu_buffer->new_pages.prev->next = cpu_buffer->new_pages.next;
6041
6042 /* Clear the new_pages list */
6043 INIT_LIST_HEAD(&cpu_buffer->new_pages);
6044
6045 cpu_buffer->head_page
6046 = list_entry(cpu_buffer->pages, struct buffer_page, list);
6047 cpu_buffer->tail_page = cpu_buffer->commit_page = cpu_buffer->head_page;
6048
6049 cpu_buffer->nr_pages = cpu_buffer->nr_pages_to_update;
6050 cpu_buffer->nr_pages_to_update = 0;
6051
6052 free_pages((unsigned long)cpu_buffer->free_page, old_order);
6053 cpu_buffer->free_page = NULL;
6054
6055 rb_head_page_activate(cpu_buffer);
6056
6057 rb_check_pages(cpu_buffer);
f9b94daa
TSV
6058 }
6059
6060 atomic_dec(&buffer->record_disabled);
6061 mutex_unlock(&buffer->mutex);
6062
2808e31e 6063 return 0;
f9b94daa
TSV
6064
6065error:
6066 buffer->subbuf_order = old_order;
6067 buffer->subbuf_size = old_size;
6068
6069 atomic_dec(&buffer->record_disabled);
6070 mutex_unlock(&buffer->mutex);
6071
6072 for_each_buffer_cpu(buffer, cpu) {
8e7b58c2
SRG
6073 cpu_buffer = buffer->buffers[cpu];
6074
6075 if (!cpu_buffer->nr_pages_to_update)
f9b94daa 6076 continue;
8e7b58c2
SRG
6077
6078 list_for_each_entry_safe(bpage, tmp, &cpu_buffer->new_pages, list) {
6079 list_del_init(&bpage->list);
6080 free_buffer_page(bpage);
6081 }
f9b94daa 6082 }
f9b94daa
TSV
6083
6084 return err;
2808e31e
TSV
6085}
6086EXPORT_SYMBOL_GPL(ring_buffer_subbuf_order_set);
6087
117c3920
VD
6088static int rb_alloc_meta_page(struct ring_buffer_per_cpu *cpu_buffer)
6089{
6090 struct page *page;
6091
6092 if (cpu_buffer->meta_page)
6093 return 0;
6094
6095 page = alloc_page(GFP_USER | __GFP_ZERO);
6096 if (!page)
6097 return -ENOMEM;
6098
6099 cpu_buffer->meta_page = page_to_virt(page);
6100
6101 return 0;
6102}
6103
6104static void rb_free_meta_page(struct ring_buffer_per_cpu *cpu_buffer)
6105{
6106 unsigned long addr = (unsigned long)cpu_buffer->meta_page;
6107
6108 free_page(addr);
6109 cpu_buffer->meta_page = NULL;
6110}
6111
6112static void rb_setup_ids_meta_page(struct ring_buffer_per_cpu *cpu_buffer,
6113 unsigned long *subbuf_ids)
6114{
6115 struct trace_buffer_meta *meta = cpu_buffer->meta_page;
6116 unsigned int nr_subbufs = cpu_buffer->nr_pages + 1;
6117 struct buffer_page *first_subbuf, *subbuf;
6118 int id = 0;
6119
6120 subbuf_ids[id] = (unsigned long)cpu_buffer->reader_page->page;
6121 cpu_buffer->reader_page->id = id++;
6122
6123 first_subbuf = subbuf = rb_set_head_page(cpu_buffer);
6124 do {
6125 if (WARN_ON(id >= nr_subbufs))
6126 break;
6127
6128 subbuf_ids[id] = (unsigned long)subbuf->page;
6129 subbuf->id = id;
6130
6131 rb_inc_page(&subbuf);
6132 id++;
6133 } while (subbuf != first_subbuf);
6134
6135 /* install subbuf ID to kern VA translation */
6136 cpu_buffer->subbuf_ids = subbuf_ids;
6137
6138 meta->meta_page_size = PAGE_SIZE;
6139 meta->meta_struct_len = sizeof(*meta);
6140 meta->nr_subbufs = nr_subbufs;
6141 meta->subbuf_size = cpu_buffer->buffer->subbuf_size + BUF_PAGE_HDR_SIZE;
6142
6143 rb_update_meta_page(cpu_buffer);
6144}
6145
6146static struct ring_buffer_per_cpu *
6147rb_get_mapped_buffer(struct trace_buffer *buffer, int cpu)
6148{
6149 struct ring_buffer_per_cpu *cpu_buffer;
6150
6151 if (!cpumask_test_cpu(cpu, buffer->cpumask))
6152 return ERR_PTR(-EINVAL);
6153
6154 cpu_buffer = buffer->buffers[cpu];
6155
6156 mutex_lock(&cpu_buffer->mapping_lock);
6157
6158 if (!cpu_buffer->mapped) {
6159 mutex_unlock(&cpu_buffer->mapping_lock);
6160 return ERR_PTR(-ENODEV);
6161 }
6162
6163 return cpu_buffer;
6164}
6165
6166static void rb_put_mapped_buffer(struct ring_buffer_per_cpu *cpu_buffer)
6167{
6168 mutex_unlock(&cpu_buffer->mapping_lock);
6169}
6170
6171/*
6172 * Fast-path for rb_buffer_(un)map(). Called whenever the meta-page doesn't need
6173 * to be set-up or torn-down.
6174 */
6175static int __rb_inc_dec_mapped(struct ring_buffer_per_cpu *cpu_buffer,
6176 bool inc)
6177{
6178 unsigned long flags;
6179
6180 lockdep_assert_held(&cpu_buffer->mapping_lock);
6181
6182 if (inc && cpu_buffer->mapped == UINT_MAX)
6183 return -EBUSY;
6184
6185 if (WARN_ON(!inc && cpu_buffer->mapped == 0))
6186 return -EINVAL;
6187
6188 mutex_lock(&cpu_buffer->buffer->mutex);
6189 raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
6190
6191 if (inc)
6192 cpu_buffer->mapped++;
6193 else
6194 cpu_buffer->mapped--;
6195
6196 raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
6197 mutex_unlock(&cpu_buffer->buffer->mutex);
6198
6199 return 0;
6200}
6201
6202/*
6203 * +--------------+ pgoff == 0
6204 * | meta page |
6205 * +--------------+ pgoff == 1
6206 * | subbuffer 0 |
6207 * | |
6208 * +--------------+ pgoff == (1 + (1 << subbuf_order))
6209 * | subbuffer 1 |
6210 * | |
6211 * ...
6212 */
6213#ifdef CONFIG_MMU
6214static int __rb_map_vma(struct ring_buffer_per_cpu *cpu_buffer,
6215 struct vm_area_struct *vma)
6216{
6217 unsigned long nr_subbufs, nr_pages, vma_pages, pgoff = vma->vm_pgoff;
6218 unsigned int subbuf_pages, subbuf_order;
6219 struct page **pages;
6220 int p = 0, s = 0;
6221 int err;
6222
6223 /* Refuse MP_PRIVATE or writable mappings */
6224 if (vma->vm_flags & VM_WRITE || vma->vm_flags & VM_EXEC ||
6225 !(vma->vm_flags & VM_MAYSHARE))
6226 return -EPERM;
6227
6228 /*
6229 * Make sure the mapping cannot become writable later. Also tell the VM
6230 * to not touch these pages (VM_DONTCOPY | VM_DONTEXPAND).
6231 */
6232 vm_flags_mod(vma, VM_DONTCOPY | VM_DONTEXPAND | VM_DONTDUMP,
6233 VM_MAYWRITE);
6234
6235 lockdep_assert_held(&cpu_buffer->mapping_lock);
6236
6237 subbuf_order = cpu_buffer->buffer->subbuf_order;
6238 subbuf_pages = 1 << subbuf_order;
6239
6240 nr_subbufs = cpu_buffer->nr_pages + 1; /* + reader-subbuf */
6241 nr_pages = ((nr_subbufs) << subbuf_order) - pgoff + 1; /* + meta-page */
6242
6243 vma_pages = (vma->vm_end - vma->vm_start) >> PAGE_SHIFT;
6244 if (!vma_pages || vma_pages > nr_pages)
6245 return -EINVAL;
6246
6247 nr_pages = vma_pages;
6248
6249 pages = kcalloc(nr_pages, sizeof(*pages), GFP_KERNEL);
6250 if (!pages)
6251 return -ENOMEM;
6252
6253 if (!pgoff) {
6254 pages[p++] = virt_to_page(cpu_buffer->meta_page);
6255
6256 /*
6257 * TODO: Align sub-buffers on their size, once
6258 * vm_insert_pages() supports the zero-page.
6259 */
6260 } else {
6261 /* Skip the meta-page */
6262 pgoff--;
6263
6264 if (pgoff % subbuf_pages) {
6265 err = -EINVAL;
6266 goto out;
6267 }
6268
6269 s += pgoff / subbuf_pages;
6270 }
6271
6272 while (p < nr_pages) {
b9c6820f 6273 struct page *page = virt_to_page((void *)cpu_buffer->subbuf_ids[s]);
117c3920
VD
6274 int off = 0;
6275
6276 if (WARN_ON_ONCE(s >= nr_subbufs)) {
6277 err = -EINVAL;
6278 goto out;
6279 }
6280
6281 for (; off < (1 << (subbuf_order)); off++, page++) {
6282 if (p >= nr_pages)
6283 break;
6284
6285 pages[p++] = page;
6286 }
6287 s++;
6288 }
6289
6290 err = vm_insert_pages(vma, vma->vm_start, pages, &nr_pages);
6291
6292out:
6293 kfree(pages);
6294
6295 return err;
6296}
6297#else
6298static int __rb_map_vma(struct ring_buffer_per_cpu *cpu_buffer,
6299 struct vm_area_struct *vma)
6300{
6301 return -EOPNOTSUPP;
6302}
6303#endif
6304
6305int ring_buffer_map(struct trace_buffer *buffer, int cpu,
6306 struct vm_area_struct *vma)
6307{
6308 struct ring_buffer_per_cpu *cpu_buffer;
6309 unsigned long flags, *subbuf_ids;
6310 int err = 0;
6311
6312 if (!cpumask_test_cpu(cpu, buffer->cpumask))
6313 return -EINVAL;
6314
6315 cpu_buffer = buffer->buffers[cpu];
6316
6317 mutex_lock(&cpu_buffer->mapping_lock);
6318
6319 if (cpu_buffer->mapped) {
6320 err = __rb_map_vma(cpu_buffer, vma);
6321 if (!err)
6322 err = __rb_inc_dec_mapped(cpu_buffer, true);
6323 mutex_unlock(&cpu_buffer->mapping_lock);
6324 return err;
6325 }
6326
6327 /* prevent another thread from changing buffer/sub-buffer sizes */
6328 mutex_lock(&buffer->mutex);
6329
6330 err = rb_alloc_meta_page(cpu_buffer);
6331 if (err)
6332 goto unlock;
6333
6334 /* subbuf_ids include the reader while nr_pages does not */
6335 subbuf_ids = kcalloc(cpu_buffer->nr_pages + 1, sizeof(*subbuf_ids), GFP_KERNEL);
6336 if (!subbuf_ids) {
6337 rb_free_meta_page(cpu_buffer);
6338 err = -ENOMEM;
6339 goto unlock;
6340 }
6341
6342 atomic_inc(&cpu_buffer->resize_disabled);
6343
6344 /*
6345 * Lock all readers to block any subbuf swap until the subbuf IDs are
6346 * assigned.
6347 */
6348 raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
6349 rb_setup_ids_meta_page(cpu_buffer, subbuf_ids);
6350 raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
6351
6352 err = __rb_map_vma(cpu_buffer, vma);
6353 if (!err) {
6354 raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
6355 cpu_buffer->mapped = 1;
6356 raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
6357 } else {
6358 kfree(cpu_buffer->subbuf_ids);
6359 cpu_buffer->subbuf_ids = NULL;
6360 rb_free_meta_page(cpu_buffer);
6361 }
6362
6363unlock:
6364 mutex_unlock(&buffer->mutex);
6365 mutex_unlock(&cpu_buffer->mapping_lock);
6366
6367 return err;
6368}
6369
6370int ring_buffer_unmap(struct trace_buffer *buffer, int cpu)
6371{
6372 struct ring_buffer_per_cpu *cpu_buffer;
6373 unsigned long flags;
6374 int err = 0;
6375
6376 if (!cpumask_test_cpu(cpu, buffer->cpumask))
6377 return -EINVAL;
6378
6379 cpu_buffer = buffer->buffers[cpu];
6380
6381 mutex_lock(&cpu_buffer->mapping_lock);
6382
6383 if (!cpu_buffer->mapped) {
6384 err = -ENODEV;
6385 goto out;
6386 } else if (cpu_buffer->mapped > 1) {
6387 __rb_inc_dec_mapped(cpu_buffer, false);
6388 goto out;
6389 }
6390
6391 mutex_lock(&buffer->mutex);
6392 raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
6393
6394 cpu_buffer->mapped = 0;
6395
6396 raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
6397
6398 kfree(cpu_buffer->subbuf_ids);
6399 cpu_buffer->subbuf_ids = NULL;
6400 rb_free_meta_page(cpu_buffer);
6401 atomic_dec(&cpu_buffer->resize_disabled);
6402
6403 mutex_unlock(&buffer->mutex);
6404
6405out:
6406 mutex_unlock(&cpu_buffer->mapping_lock);
6407
6408 return err;
6409}
6410
6411int ring_buffer_map_get_reader(struct trace_buffer *buffer, int cpu)
6412{
6413 struct ring_buffer_per_cpu *cpu_buffer;
fe832be0
SRG
6414 struct buffer_page *reader;
6415 unsigned long missed_events;
117c3920
VD
6416 unsigned long reader_size;
6417 unsigned long flags;
6418
6419 cpu_buffer = rb_get_mapped_buffer(buffer, cpu);
6420 if (IS_ERR(cpu_buffer))
6421 return (int)PTR_ERR(cpu_buffer);
6422
6423 raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
6424
6425consume:
6426 if (rb_per_cpu_empty(cpu_buffer))
6427 goto out;
6428
6429 reader_size = rb_page_size(cpu_buffer->reader_page);
6430
6431 /*
6432 * There are data to be read on the current reader page, we can
6433 * return to the caller. But before that, we assume the latter will read
6434 * everything. Let's update the kernel reader accordingly.
6435 */
6436 if (cpu_buffer->reader_page->read < reader_size) {
6437 while (cpu_buffer->reader_page->read < reader_size)
6438 rb_advance_reader(cpu_buffer);
6439 goto out;
6440 }
6441
fe832be0
SRG
6442 reader = rb_get_reader_page(cpu_buffer);
6443 if (WARN_ON(!reader))
117c3920
VD
6444 goto out;
6445
fe832be0
SRG
6446 /* Check if any events were dropped */
6447 missed_events = cpu_buffer->lost_events;
6448
6449 if (cpu_buffer->reader_page != cpu_buffer->commit_page) {
6450 if (missed_events) {
6451 struct buffer_data_page *bpage = reader->page;
6452 unsigned int commit;
6453 /*
6454 * Use the real_end for the data size,
6455 * This gives us a chance to store the lost events
6456 * on the page.
6457 */
6458 if (reader->real_end)
6459 local_set(&bpage->commit, reader->real_end);
6460 /*
6461 * If there is room at the end of the page to save the
6462 * missed events, then record it there.
6463 */
6464 commit = rb_page_size(reader);
6465 if (buffer->subbuf_size - commit >= sizeof(missed_events)) {
6466 memcpy(&bpage->data[commit], &missed_events,
6467 sizeof(missed_events));
6468 local_add(RB_MISSED_STORED, &bpage->commit);
6469 }
6470 local_add(RB_MISSED_EVENTS, &bpage->commit);
6471 }
6472 } else {
6473 /*
6474 * There really shouldn't be any missed events if the commit
6475 * is on the reader page.
6476 */
6477 WARN_ON_ONCE(missed_events);
6478 }
6479
6480 cpu_buffer->lost_events = 0;
6481
117c3920
VD
6482 goto consume;
6483
6484out:
6485 /* Some archs do not have data cache coherency between kernel and user-space */
6486 flush_dcache_folio(virt_to_folio(cpu_buffer->reader_page->page));
6487
6488 rb_update_meta_page(cpu_buffer);
6489
6490 raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
6491 rb_put_mapped_buffer(cpu_buffer);
6492
6493 return 0;
6494}
6495
b32614c0
SAS
6496/*
6497 * We only allocate new buffers, never free them if the CPU goes down.
6498 * If we were to free the buffer, then the user would lose any trace that was in
6499 * the buffer.
6500 */
6501int trace_rb_cpu_prepare(unsigned int cpu, struct hlist_node *node)
554f786e 6502{
13292494 6503 struct trace_buffer *buffer;
9b94a8fb
SRRH
6504 long nr_pages_same;
6505 int cpu_i;
6506 unsigned long nr_pages;
554f786e 6507
13292494 6508 buffer = container_of(node, struct trace_buffer, node);
b32614c0
SAS
6509 if (cpumask_test_cpu(cpu, buffer->cpumask))
6510 return 0;
6511
6512 nr_pages = 0;
6513 nr_pages_same = 1;
6514 /* check if all cpu sizes are same */
6515 for_each_buffer_cpu(buffer, cpu_i) {
6516 /* fill in the size from first enabled cpu */
6517 if (nr_pages == 0)
6518 nr_pages = buffer->buffers[cpu_i]->nr_pages;
6519 if (nr_pages != buffer->buffers[cpu_i]->nr_pages) {
6520 nr_pages_same = 0;
6521 break;
554f786e 6522 }
554f786e 6523 }
b32614c0
SAS
6524 /* allocate minimum pages, user can later expand it */
6525 if (!nr_pages_same)
6526 nr_pages = 2;
6527 buffer->buffers[cpu] =
6528 rb_allocate_cpu_buffer(buffer, nr_pages, cpu);
6529 if (!buffer->buffers[cpu]) {
6530 WARN(1, "failed to allocate ring buffer on CPU %u\n",
6531 cpu);
6532 return -ENOMEM;
6533 }
6534 smp_wmb();
6535 cpumask_set_cpu(cpu, buffer->cpumask);
6536 return 0;
554f786e 6537}
6c43e554
SRRH
6538
6539#ifdef CONFIG_RING_BUFFER_STARTUP_TEST
6540/*
6541 * This is a basic integrity check of the ring buffer.
6542 * Late in the boot cycle this test will run when configured in.
6543 * It will kick off a thread per CPU that will go into a loop
6544 * writing to the per cpu ring buffer various sizes of data.
6545 * Some of the data will be large items, some small.
6546 *
6547 * Another thread is created that goes into a spin, sending out
6548 * IPIs to the other CPUs to also write into the ring buffer.
6549 * this is to test the nesting ability of the buffer.
6550 *
6551 * Basic stats are recorded and reported. If something in the
6552 * ring buffer should happen that's not expected, a big warning
6553 * is displayed and all ring buffers are disabled.
6554 */
6555static struct task_struct *rb_threads[NR_CPUS] __initdata;
6556
6557struct rb_test_data {
13292494 6558 struct trace_buffer *buffer;
6c43e554
SRRH
6559 unsigned long events;
6560 unsigned long bytes_written;
6561 unsigned long bytes_alloc;
6562 unsigned long bytes_dropped;
6563 unsigned long events_nested;
6564 unsigned long bytes_written_nested;
6565 unsigned long bytes_alloc_nested;
6566 unsigned long bytes_dropped_nested;
6567 int min_size_nested;
6568 int max_size_nested;
6569 int max_size;
6570 int min_size;
6571 int cpu;
6572 int cnt;
6573};
6574
6575static struct rb_test_data rb_data[NR_CPUS] __initdata;
6576
6577/* 1 meg per cpu */
6578#define RB_TEST_BUFFER_SIZE 1048576
6579
6580static char rb_string[] __initdata =
6581 "abcdefghijklmnopqrstuvwxyz1234567890!@#$%^&*()?+\\"
6582 "?+|:';\",.<>/?abcdefghijklmnopqrstuvwxyz1234567890"
6583 "!@#$%^&*()?+\\?+|:';\",.<>/?abcdefghijklmnopqrstuv";
6584
6585static bool rb_test_started __initdata;
6586
6587struct rb_item {
6588 int size;
6589 char str[];
6590};
6591
6592static __init int rb_write_something(struct rb_test_data *data, bool nested)
6593{
6594 struct ring_buffer_event *event;
6595 struct rb_item *item;
6596 bool started;
6597 int event_len;
6598 int size;
6599 int len;
6600 int cnt;
6601
6602 /* Have nested writes different that what is written */
6603 cnt = data->cnt + (nested ? 27 : 0);
6604
6605 /* Multiply cnt by ~e, to make some unique increment */
40ed29b3 6606 size = (cnt * 68 / 25) % (sizeof(rb_string) - 1);
6c43e554
SRRH
6607
6608 len = size + sizeof(struct rb_item);
6609
6610 started = rb_test_started;
6611 /* read rb_test_started before checking buffer enabled */
6612 smp_rmb();
6613
6614 event = ring_buffer_lock_reserve(data->buffer, len);
6615 if (!event) {
6616 /* Ignore dropped events before test starts. */
6617 if (started) {
6618 if (nested)
6619 data->bytes_dropped += len;
6620 else
6621 data->bytes_dropped_nested += len;
6622 }
6623 return len;
6624 }
6625
6626 event_len = ring_buffer_event_length(event);
6627
6628 if (RB_WARN_ON(data->buffer, event_len < len))
6629 goto out;
6630
6631 item = ring_buffer_event_data(event);
6632 item->size = size;
6633 memcpy(item->str, rb_string, size);
6634
6635 if (nested) {
6636 data->bytes_alloc_nested += event_len;
6637 data->bytes_written_nested += len;
6638 data->events_nested++;
6639 if (!data->min_size_nested || len < data->min_size_nested)
6640 data->min_size_nested = len;
6641 if (len > data->max_size_nested)
6642 data->max_size_nested = len;
6643 } else {
6644 data->bytes_alloc += event_len;
6645 data->bytes_written += len;
6646 data->events++;
6647 if (!data->min_size || len < data->min_size)
6648 data->max_size = len;
6649 if (len > data->max_size)
6650 data->max_size = len;
6651 }
6652
6653 out:
04aabc32 6654 ring_buffer_unlock_commit(data->buffer);
6c43e554
SRRH
6655
6656 return 0;
6657}
6658
6659static __init int rb_test(void *arg)
6660{
6661 struct rb_test_data *data = arg;
6662
6663 while (!kthread_should_stop()) {
6664 rb_write_something(data, false);
6665 data->cnt++;
6666
6667 set_current_state(TASK_INTERRUPTIBLE);
6668 /* Now sleep between a min of 100-300us and a max of 1ms */
6669 usleep_range(((data->cnt % 3) + 1) * 100, 1000);
6670 }
6671
6672 return 0;
6673}
6674
6675static __init void rb_ipi(void *ignore)
6676{
6677 struct rb_test_data *data;
6678 int cpu = smp_processor_id();
6679
6680 data = &rb_data[cpu];
6681 rb_write_something(data, true);
6682}
6683
6684static __init int rb_hammer_test(void *arg)
6685{
6686 while (!kthread_should_stop()) {
6687
6688 /* Send an IPI to all cpus to write data! */
6689 smp_call_function(rb_ipi, NULL, 1);
6690 /* No sleep, but for non preempt, let others run */
6691 schedule();
6692 }
6693
6694 return 0;
6695}
6696
6697static __init int test_ringbuffer(void)
6698{
6699 struct task_struct *rb_hammer;
13292494 6700 struct trace_buffer *buffer;
6c43e554
SRRH
6701 int cpu;
6702 int ret = 0;
6703
a356646a 6704 if (security_locked_down(LOCKDOWN_TRACEFS)) {
ee195452 6705 pr_warn("Lockdown is enabled, skipping ring buffer tests\n");
a356646a
SRV
6706 return 0;
6707 }
6708
6c43e554
SRRH
6709 pr_info("Running ring buffer tests...\n");
6710
6711 buffer = ring_buffer_alloc(RB_TEST_BUFFER_SIZE, RB_FL_OVERWRITE);
6712 if (WARN_ON(!buffer))
6713 return 0;
6714
6715 /* Disable buffer so that threads can't write to it yet */
6716 ring_buffer_record_off(buffer);
6717
6718 for_each_online_cpu(cpu) {
6719 rb_data[cpu].buffer = buffer;
6720 rb_data[cpu].cpu = cpu;
6721 rb_data[cpu].cnt = cpu;
64ed3a04
CH
6722 rb_threads[cpu] = kthread_run_on_cpu(rb_test, &rb_data[cpu],
6723 cpu, "rbtester/%u");
62277de7 6724 if (WARN_ON(IS_ERR(rb_threads[cpu]))) {
6c43e554 6725 pr_cont("FAILED\n");
62277de7 6726 ret = PTR_ERR(rb_threads[cpu]);
6c43e554
SRRH
6727 goto out_free;
6728 }
6c43e554
SRRH
6729 }
6730
6731 /* Now create the rb hammer! */
6732 rb_hammer = kthread_run(rb_hammer_test, NULL, "rbhammer");
62277de7 6733 if (WARN_ON(IS_ERR(rb_hammer))) {
6c43e554 6734 pr_cont("FAILED\n");
62277de7 6735 ret = PTR_ERR(rb_hammer);
6c43e554
SRRH
6736 goto out_free;
6737 }
6738
6739 ring_buffer_record_on(buffer);
6740 /*
6741 * Show buffer is enabled before setting rb_test_started.
6742 * Yes there's a small race window where events could be
6743 * dropped and the thread wont catch it. But when a ring
6744 * buffer gets enabled, there will always be some kind of
6745 * delay before other CPUs see it. Thus, we don't care about
6746 * those dropped events. We care about events dropped after
6747 * the threads see that the buffer is active.
6748 */
6749 smp_wmb();
6750 rb_test_started = true;
6751
6752 set_current_state(TASK_INTERRUPTIBLE);
6753 /* Just run for 10 seconds */;
6754 schedule_timeout(10 * HZ);
6755
6756 kthread_stop(rb_hammer);
6757
6758 out_free:
6759 for_each_online_cpu(cpu) {
6760 if (!rb_threads[cpu])
6761 break;
6762 kthread_stop(rb_threads[cpu]);
6763 }
6764 if (ret) {
6765 ring_buffer_free(buffer);
6766 return ret;
6767 }
6768
6769 /* Report! */
6770 pr_info("finished\n");
6771 for_each_online_cpu(cpu) {
6772 struct ring_buffer_event *event;
6773 struct rb_test_data *data = &rb_data[cpu];
6774 struct rb_item *item;
6775 unsigned long total_events;
6776 unsigned long total_dropped;
6777 unsigned long total_written;
6778 unsigned long total_alloc;
6779 unsigned long total_read = 0;
6780 unsigned long total_size = 0;
6781 unsigned long total_len = 0;
6782 unsigned long total_lost = 0;
6783 unsigned long lost;
6784 int big_event_size;
6785 int small_event_size;
6786
6787 ret = -1;
6788
6789 total_events = data->events + data->events_nested;
6790 total_written = data->bytes_written + data->bytes_written_nested;
6791 total_alloc = data->bytes_alloc + data->bytes_alloc_nested;
6792 total_dropped = data->bytes_dropped + data->bytes_dropped_nested;
6793
6794 big_event_size = data->max_size + data->max_size_nested;
6795 small_event_size = data->min_size + data->min_size_nested;
6796
6797 pr_info("CPU %d:\n", cpu);
6798 pr_info(" events: %ld\n", total_events);
6799 pr_info(" dropped bytes: %ld\n", total_dropped);
6800 pr_info(" alloced bytes: %ld\n", total_alloc);
6801 pr_info(" written bytes: %ld\n", total_written);
6802 pr_info(" biggest event: %d\n", big_event_size);
6803 pr_info(" smallest event: %d\n", small_event_size);
6804
6805 if (RB_WARN_ON(buffer, total_dropped))
6806 break;
6807
6808 ret = 0;
6809
6810 while ((event = ring_buffer_consume(buffer, cpu, NULL, &lost))) {
6811 total_lost += lost;
6812 item = ring_buffer_event_data(event);
6813 total_len += ring_buffer_event_length(event);
6814 total_size += item->size + sizeof(struct rb_item);
6815 if (memcmp(&item->str[0], rb_string, item->size) != 0) {
6816 pr_info("FAILED!\n");
6817 pr_info("buffer had: %.*s\n", item->size, item->str);
6818 pr_info("expected: %.*s\n", item->size, rb_string);
6819 RB_WARN_ON(buffer, 1);
6820 ret = -1;
6821 break;
6822 }
6823 total_read++;
6824 }
6825 if (ret)
6826 break;
6827
6828 ret = -1;
6829
6830 pr_info(" read events: %ld\n", total_read);
6831 pr_info(" lost events: %ld\n", total_lost);
6832 pr_info(" total events: %ld\n", total_lost + total_read);
6833 pr_info(" recorded len bytes: %ld\n", total_len);
6834 pr_info(" recorded size bytes: %ld\n", total_size);
ed888241 6835 if (total_lost) {
6c43e554
SRRH
6836 pr_info(" With dropped events, record len and size may not match\n"
6837 " alloced and written from above\n");
ed888241 6838 } else {
6c43e554
SRRH
6839 if (RB_WARN_ON(buffer, total_len != total_alloc ||
6840 total_size != total_written))
6841 break;
6842 }
6843 if (RB_WARN_ON(buffer, total_lost + total_read != total_events))
6844 break;
6845
6846 ret = 0;
6847 }
6848 if (!ret)
6849 pr_info("Ring buffer PASSED!\n");
6850
6851 ring_buffer_free(buffer);
6852 return 0;
6853}
6854
6855late_initcall(test_ringbuffer);
6856#endif /* CONFIG_RING_BUFFER_STARTUP_TEST */