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