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